Skip to content

Add Node build for Firebase Storage #5468

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
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions packages/storage/.run/All Tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<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>
<extra-mocha-options>--require ts-node/register/type-check --require src/index.node.ts</extra-mocha-options>
<test-kind>PATTERN</test-kind>
<test-pattern>test/{,!(browser)/**/}*.test.ts</test-pattern>
<method v="2" />
</configuration>
</component>
</component>
2 changes: 1 addition & 1 deletion packages/storage/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function (config) {

function getTestFiles(argv) {
let unitTestFiles = ['test/unit/*'];
let integrationTestFiles = ['test/integration/*'];
let integrationTestFiles = ['test/integration/*', 'test/browser/*'];

if (argv.unit) {
return unitTestFiles;
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test:browser:unit": "karma start --single-run --unit",
"test:browser:integration": "karma start --single-run --integration",
"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 src/index.ts --config ../../config/mocharc.node.js",
"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 src/index.node.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 dist/storage-public.d.ts --output dist/storage-public.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const es2017Plugins = [
const es2017Builds = [
// Node
{
input: './src/index.ts',
input: './src/index.node.ts',
output: {
file: pkg.main,
format: 'cjs',
Expand Down
20 changes: 20 additions & 0 deletions packages/storage/src/api.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @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.
*/

// TODO(mrschmidt): Add getBlob()

export {};
20 changes: 20 additions & 0 deletions packages/storage/src/api.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @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.
*/

// TODO(mrschmidt): Add getStream()
Copy link
Member

@Feiyang1 Feiyang1 Sep 20, 2021

Choose a reason for hiding this comment

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

Not a big fan of having different API for browser and node.
It makes it harder for developers to write isomorphic code for use cases like SSR. It also makes documentation harder, as we need to document the differences. We will also need to provide 2 typing files.

What does getStream do? Is it possible to make it part of getBlob with a flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we can combine getBlob and getStream. The Blob API is not available in Node, and Streams are not available for Browsers.

I have two suggestions:

  • Remove getStream()
  • Export getBytes()/getBlob() for all platforms, but make it throw for platforms that are not supported.

I updated the PR to throw.

Copy link
Member

Choose a reason for hiding this comment

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

Okay. I agree with throwing and keeping the same API for browser and node. We have taken the same approach in Auth to keep their node and browser API consistent.


export {};
3 changes: 1 addition & 2 deletions packages/storage/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export {
TaskEvent as _TaskEvent,
TaskState as _TaskState
} from './implementation/taskenums';
export { StringFormat };

/**
* Uploads data to this object's location.
Expand Down Expand Up @@ -290,8 +291,6 @@ export function _getChild(ref: StorageReference, childPath: string): Reference {
return _getChildInternal(ref as Reference, childPath);
}

export { StringFormat } from './implementation/string';

/**
* Gets a {@link FirebaseStorage} instance for the given Firebase app.
* @public
Expand Down
78 changes: 78 additions & 0 deletions packages/storage/src/index.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Cloud Storage for Firebase
*
* @packageDocumentation
*/

/**
* @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.
*/

// eslint-disable-next-line import/no-extraneous-dependencies
import {
_registerComponent,
registerVersion,
SDK_VERSION
} from '@firebase/app';

import { FirebaseStorageImpl } from './service';
import {
Component,
ComponentType,
ComponentContainer,
InstanceFactoryOptions
} from '@firebase/component';

import { name, version } from '../package.json';

import { FirebaseStorage } from './public-types';
import { STORAGE_TYPE } from './constants';
import { ConnectionPool } from './implementation/connectionPool';

export * from './api';
export * from './api.node';

function factory(
container: ComponentContainer,
{ instanceIdentifier: url }: InstanceFactoryOptions
): FirebaseStorage {
const app = container.getProvider('app').getImmediate();
const authProvider = container.getProvider('auth-internal');
const appCheckProvider = container.getProvider('app-check-internal');

return new FirebaseStorageImpl(
app,
authProvider,
appCheckProvider,
new ConnectionPool(),
url,
SDK_VERSION
);
}

function registerStorage(): void {
_registerComponent(
new Component(
STORAGE_TYPE,
factory,
ComponentType.PUBLIC
).setMultipleInstances(true)
);

registerVersion(name, version);
}

registerStorage();
2 changes: 1 addition & 1 deletion packages/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import { name, version } from '../package.json';
import { FirebaseStorage } from './public-types';
import { STORAGE_TYPE } from './constants';

export { StringFormat } from '../src/implementation/string';
export * from './api';
export * from './api.browser';

function factory(
container: ComponentContainer,
Expand Down