Skip to content

Add Node.js support to Storage v9 #4985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions config/webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ const path = require('path');
const webpack = require('webpack');

/**
* A regular expression used to replace Firestore's platform specific modules,
* which are located under 'packages/firestore/src/platform/'.
* A regular expression used to replace Firestore's and Storage's platform-
* specific modules, which are located under
* 'packages/(component)/src/platform/'.
*/
const FIRESTORE_PLATFORM_RE = /^(.*)\/platform\/([^.\/]*)(\.ts)?$/;
const PLATFORM_RE = /^(.*)\/platform\/([^.\/]*)(\.ts)?$/;

module.exports = {
mode: 'development',
Expand Down Expand Up @@ -100,16 +101,13 @@ module.exports = {
symlinks: false
},
plugins: [
new webpack.NormalModuleReplacementPlugin(
FIRESTORE_PLATFORM_RE,
resource => {
const targetPlatform = process.env.TEST_PLATFORM || 'browser';
resource.request = resource.request.replace(
FIRESTORE_PLATFORM_RE,
`$1/platform/${targetPlatform}/$2.ts`
);
}
),
new webpack.NormalModuleReplacementPlugin(PLATFORM_RE, resource => {
const targetPlatform = process.env.TEST_PLATFORM || 'browser';
resource.request = resource.request.replace(
PLATFORM_RE,
`$1/platform/${targetPlatform}/$2.ts`
);
}),
new webpack.EnvironmentPlugin([
'RTDB_EMULATOR_PORT',
'RTDB_EMULATOR_NAMESPACE'
Expand Down
19 changes: 19 additions & 0 deletions packages/storage/.run/All Tests.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<component name="ProjectRunConfigurationManager">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is basically the entire reason behind this PR. I want to be able to debug Storage tests in IntelliJ. Since it is not harmful for VSCode users, I'd like to leave it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Should we follow the same folder structure in Firestore, i.e. .idea/runConfigurations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like IntelliJ changed the locations. All new profiles are added under .run

<configuration default="false" name="All Tests" type="mocha-javascript-test-runner">
<node-interpreter>project</node-interpreter>
<node-options />
<mocha-package>$PROJECT_DIR$/../../node_modules/mocha</mocha-package>
<working-directory>$PROJECT_DIR$</working-directory>
<pass-parent-env>true</pass-parent-env>
<envs>
<env name="TS_NODE_COMPILER_OPTIONS" value="{&quot;module&quot;:&quot;commonjs&quot;}" />
<env name="TS_NODE_FILES" value="true" />
<env name="TS_NODE_CACHE" value="NO" />
</envs>
<ui>bdd</ui>
<extra-mocha-options>--require ts-node/register/type-check --require index.ts</extra-mocha-options>
<test-kind>PATTERN</test-kind>
<test-pattern>test/{,!(browser)/**/}*.test.ts</test-pattern>
<method v="2" />
</configuration>
</component>
4 changes: 2 additions & 2 deletions packages/storage/exp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
// eslint-disable-next-line import/no-extraneous-dependencies
} from '@firebase/app-exp';

import { XhrIoPool } from '../src/implementation/xhriopool';
import { ConnectionPool } from '../src/implementation/connectionPool';
import {
StorageService as StorageServiceInternal,
useStorageEmulator as useEmulatorInternal
Expand Down Expand Up @@ -76,7 +76,7 @@ function factory(
app,
authProvider,
appCheckProvider,
new XhrIoPool(),
new ConnectionPool(),
url,
SDK_VERSION
);
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/exp/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@firebase/storage-exp",
"description": "A tree-shakeable version of the Storage SDK",
"main": "./dist/index.browser.cjs.js",
"main": "./dist/index.node.cjs.js",
"module": "./dist/index.browser.esm2017.js",
"browser": "./dist/index.browser.esm2017.js",
"esm5": "./dist/index.browser.esm5.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { _FirebaseNamespace } from '@firebase/app-types/private';
import { StringFormat } from './src/implementation/string';
import { TaskEvent, TaskState } from './src/implementation/taskenums';

import { XhrIoPool } from './src/implementation/xhriopool';
import { ConnectionPool } from './src/implementation/connectionPool';
import { ReferenceCompat } from './compat/reference';
import { StorageServiceCompat } from './compat/service';
import { StorageService } from './src/service';
Expand Down Expand Up @@ -59,7 +59,7 @@ function factory(
app,
authProvider,
appCheckProvider,
new XhrIoPool(),
new ConnectionPool(),
url,
firebase.SDK_VERSION
)
Expand Down
9 changes: 6 additions & 3 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
"build:exp:release": "yarn build:exp && yarn build:compat",
"build:deps": "lerna run --scope @firebase/storage --include-dependencies build",
"dev": "rollup -c -w",
"test": "run-p test:browser lint",
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test:browser",
"test": "run-p test:browser test:node lint",
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test:browser test:node",
"test:browser:compat:unit": "karma start --single-run --compat --unit",
"test:browser:exp:unit": "karma start --single-run --exp --unit",
"test:browser:compat:integration": "karma start --single-run --compat --integration",
"test:browser:exp:integration": "karma start --single-run --exp --integration",
"test:browser:compat": "karma start --single-run --compat",
"test:browser:exp": "karma start --single-run --exp",
"test:browser": "karma start --single-run",
"test:node": "TS_NODE_FILES=true TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --file index.ts --config ../../config/mocharc.node.js",
"test:debug": "karma start --browser=Chrome",
"prettier": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
"api-report": "api-extractor run --local --verbose && ts-node-script ../../repo-scripts/prune-dts/prune-dts.ts --input exp/dist/storage-public.d.ts --output exp/dist/storage-public.d.ts",
Expand All @@ -40,6 +41,7 @@
"@firebase/storage-types": "0.4.1",
"@firebase/util": "1.1.0",
"@firebase/component": "0.5.3",
"node-fetch": "2.6.1",
"tslib": "^2.1.0"
},
"peerDependencies": {
Expand All @@ -50,6 +52,7 @@
"@firebase/app": "0.6.27",
"@firebase/auth": "0.16.7",
"rollup": "2.35.1",
"@rollup/plugin-alias": "3.1.1",
"@rollup/plugin-json": "4.1.0",
"rollup-plugin-typescript2": "0.29.0",
"typescript": "4.2.2"
Expand All @@ -63,4 +66,4 @@
"url": "https://github.com/firebase/firebase-js-sdk/issues"
},
"typings": "dist/index.d.ts"
}
}
7 changes: 5 additions & 2 deletions packages/storage/rollup.config.compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import json from '@rollup/plugin-json';
import typescriptPlugin from 'rollup-plugin-typescript2';
import typescript from 'typescript';
import alias from '@rollup/plugin-alias';
import pkg from './package.json';

import { getImportPathTransformer } from '../../scripts/exp/ts-transform-import-path';

const { generateAliasConfig } = require('./rollup.shared');

const deps = [
...Object.keys(Object.assign({}, pkg.peerDependencies, pkg.dependencies)),
'@firebase/storage'
Expand Down Expand Up @@ -59,7 +62,7 @@ const es5Builds = [
sourcemap: true
}
],
plugins: es5BuildPlugins,
plugins: [alias(generateAliasConfig('browser')), ...es5BuildPlugins],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)),
treeshake: {
moduleSideEffects: false
Expand Down Expand Up @@ -98,7 +101,7 @@ const es2017Builds = [
format: 'es',
sourcemap: true
},
plugins: es2017BuildPlugins,
plugins: [alias(generateAliasConfig('browser')), ...es2017BuildPlugins],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)),
treeshake: {
moduleSideEffects: false
Expand Down
41 changes: 27 additions & 14 deletions packages/storage/rollup.config.exp.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import json from '@rollup/plugin-json';
import typescriptPlugin from 'rollup-plugin-typescript2';
import typescript from 'typescript';
import pkgExp from './exp/package.json';
import alias from '@rollup/plugin-alias';
import pkg from './package.json';
import path from 'path';
import { importPathTransformer } from '../../scripts/exp/ts-transform-import-path';

const { generateAliasConfig } = require('./rollup.shared');

const deps = [
...Object.keys(Object.assign({}, pkg.peerDependencies, pkg.dependencies)),
'@firebase/app'
Expand All @@ -38,14 +41,15 @@ const es5Plugins = [
];

const es5Builds = [
// Browser
{
input: './exp/index.ts',
output: {
file: path.resolve('./exp', pkgExp.esm5),
format: 'es',
sourcemap: true
},
plugins: es5Plugins,
plugins: [alias(generateAliasConfig('browser')), ...es5Plugins],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)),
treeshake: {
moduleSideEffects: false
Expand All @@ -68,21 +72,30 @@ const es2017Plugins = [
];

const es2017Builds = [
// Node
{
input: './exp/index.ts',
output: [
{
file: path.resolve('./exp', pkgExp.main),
format: 'cjs',
sourcemap: true
},
{
file: path.resolve('./exp', pkgExp.browser),
format: 'es',
sourcemap: true
}
],
plugins: es2017Plugins,
output: {
file: path.resolve('./exp', pkgExp.main),
format: 'cjs',
sourcemap: true
},
plugins: [alias(generateAliasConfig('node')), ...es2017Plugins],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)),
treeshake: {
moduleSideEffects: false
}
},

// Browser
{
input: './exp/index.ts',
output: {
file: path.resolve('./exp', pkgExp.browser),
format: 'es',
sourcemap: true
},
plugins: [alias(generateAliasConfig('browser')), ...es2017Plugins],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)),
treeshake: {
moduleSideEffects: false
Expand Down
7 changes: 5 additions & 2 deletions packages/storage/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import json from '@rollup/plugin-json';
import typescriptPlugin from 'rollup-plugin-typescript2';
import typescript from 'typescript';
import alias from '@rollup/plugin-alias';
import pkg from './package.json';

const { generateAliasConfig } = require('./rollup.shared');

const deps = Object.keys(
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
);
Expand All @@ -40,7 +43,7 @@ const es5Builds = [
{ file: pkg.main, format: 'cjs', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true }
],
plugins: es5BuildPlugins,
plugins: [alias(generateAliasConfig('browser')), ...es5BuildPlugins],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)),
treeshake: {
moduleSideEffects: false
Expand Down Expand Up @@ -71,7 +74,7 @@ const es2017Builds = [
format: 'es',
sourcemap: true
},
plugins: es2017BuildPlugins,
plugins: [alias(generateAliasConfig('browser')), ...es2017BuildPlugins],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)),
treeshake: {
moduleSideEffects: false
Expand Down
34 changes: 34 additions & 0 deletions packages/storage/rollup.shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Returns an replacement configuration for `@rollup/plugin-alias` that replaces
* references to platform-specific files with implementations for the provided
* target platform.
*/
function generateAliasConfig(platform) {
return {
entries: [
{
find: /^(.*)\/platform\/([^.\/]*)(\.ts)?$/,
replacement: `$1\/platform/${platform}/$2.ts`
}
]
};
}

exports.generateAliasConfig = generateAliasConfig;
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
*/

/**
* XHR headers
* Network headers
*/
export interface Headers {
[name: string]: string | number;
[name: string]: string;
}

/**
* A lightweight wrapper around XMLHttpRequest with a
* goog.net.XhrIo-like interface.
*/
export interface XhrIo {
export interface Connection {
send(
url: string,
method: string,
body?: ArrayBufferView | Blob | string | null,
headers?: Headers
): Promise<XhrIo>;
): Promise<void>;

getErrorCode(): ErrorCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
/**
* @fileoverview Replacement for goog.net.XhrIoPool that works with fbs.XhrIo.
*/
import { XhrIo } from './xhrio';
import { NetworkXhrIo } from './xhrio_network';
import { Connection } from './connection';
import { newConnection } from '../platform/connection';

/**
* Factory-like class for creating XhrIo instances.
*/
export class XhrIoPool {
createXhrIo(): XhrIo {
return new NetworkXhrIo();
export class ConnectionPool {
createConnection(): Connection {
return newConnection();
}
}
Loading