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 5 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
3 changes: 3 additions & 0 deletions common/api-review/storage.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export function getMetadata(ref: StorageReference): Promise<FullMetadata>;
// @public
export function getStorage(app?: FirebaseApp, bucketUrl?: string): FirebaseStorage;

// @public
export function getStream(ref: StorageReference, maxDownloadSizeBytes?: number): NodeJS.ReadableStream;

// Warning: (ae-forgotten-export) The symbol "StorageError" needs to be exported by the entry point index.d.ts
//
// @internal (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/.run/All Tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<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 index.node.ts</extra-mocha-options>
<test-kind>PATTERN</test-kind>
<test-pattern>test/{,!(browser)/**/}*.test.ts</test-pattern>
<method v="2" />
Expand Down
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 @@ -106,7 +106,7 @@ const es2017Plugins = [
const es2017Builds = [
// Node
{
input: './src/index.ts',
input: './src/index.node.ts',
output: {
file: pkg.main,
format: 'cjs',
Expand Down
64 changes: 64 additions & 0 deletions packages/storage/src/api.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @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.
*/

import {StorageReference} from "./public-types";

/**
* Downloads the data at the object's location. Returns an error if the object
* is not found.
*
* To use this functionality, you have to whitelist your app's origin in your
* Cloud Storage bucket. See also
* https://cloud.google.com/storage/docs/configuring-cors
*
* This API is not available in Node.
*
* @public
* @param ref - StorageReference where data should be download.
* @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
* retrieve.
* @returns A Promise that resolves with a Blob containing the object's bytes
*/
function getBlob(
Copy link
Member

Choose a reason for hiding this comment

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

Should it be exported here and in api.node.ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would immediately add it to the public API. Let me suppress the warning for now.

ref: StorageReference,
maxDownloadSizeBytes?: number
): Promise<Blob> {
throw new Error("Not implemented");
}

/**
* Downloads the data at the object's location. Raises an error event if the
* object is not found.
*
* This API is only available in Node.
*
* @public
* @param ref - StorageReference where data should be download.
* @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
* retrieve.
* @returns A stream with the object's data as bytes
*/
export function getStream(
ref: StorageReference,
maxDownloadSizeBytes?: number
): NodeJS.ReadableStream {
throw new Error("getStream() is only supported by NodeJS builds");
}

// TODO(getbytes): Export getBlob/getStream

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

import {StorageReference} from "./public-types";

/**
* Downloads the data at the object's location. Returns an error if the object
* is not found.
*
* To use this functionality, you have to whitelist your app's origin in your
* Cloud Storage bucket. See also
* https://cloud.google.com/storage/docs/configuring-cors
*
* This API is not available in Node.
*
* @public
* @param ref - StorageReference where data should be download.
* @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
* retrieve.
* @returns A Promise that resolves with a Blob containing the object's bytes
*/
function getBlob(
ref: StorageReference,
maxDownloadSizeBytes?: number
): Promise<Blob> {
throw new Error("getBlob() is only available in Browser-like environments");
}

/**
* Downloads the data at the object's location. Raises an error event if the
* object is not found.
*
* This API is only available in Node.
*
* @public
* @param ref - StorageReference where data should be download.
* @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
* retrieve.
* @returns A stream with the object's data as bytes
*/
export function getStream(
ref: StorageReference,
maxDownloadSizeBytes?: number
): NodeJS.ReadableStream {
throw new Error("Not implemented");
}

// TODO(getbytes): Export getBlob/getStream

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