Skip to content

Set up Storage modularization #3499

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 3 commits into from
Nov 6, 2020
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
5 changes: 5 additions & 0 deletions .changeset/silly-boats-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/storage': patch
---

Refactored Storage to allow for modularization.
2 changes: 1 addition & 1 deletion config/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ module.exports = {
'assertionStyle': 'as'
}
],
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-explicit-any': ['error', { 'ignoreRestArgs': true }],
'@typescript-eslint/no-namespace': [
'error',
{
Expand Down
6 changes: 5 additions & 1 deletion packages/storage/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ module.exports = {
'import/no-extraneous-dependencies': [
'error',
{
'packageDir': [path.resolve(__dirname, '../../'), __dirname]
'packageDir': [
path.resolve(__dirname, '../../'),
__dirname,
path.resolve(__dirname, 'exp')
]
}
]
}
Expand Down
10 changes: 10 additions & 0 deletions packages/storage/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../config/api-extractor.json",
// Point it to your entry point d.ts file.
"mainEntryPointFilePath": "<projectFolder>/exp/dist/exp/index.d.ts",
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "<projectFolder>/exp/dist/<unscopedPackageName>.d.ts",
"publicTrimmedFilePath": "<projectFolder>/exp/dist/<unscopedPackageName>-public.d.ts"
}
}
109 changes: 109 additions & 0 deletions packages/storage/compat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @license
* Copyright 2020 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 firebase from '@firebase/app';
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 { ReferenceCompat } from './reference';
import { StorageServiceCompat } from './service';
import { StorageService } from '../src/service';
import * as types from '@firebase/storage-types';
import {
Component,
ComponentType,
ComponentContainer
} from '@firebase/component';

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

/**
* Type constant for Firebase Storage.
*/
const STORAGE_TYPE = 'storage';

function factory(
container: ComponentContainer,
url?: string
): types.FirebaseStorage {
// Dependencies
// TODO: This should eventually be 'app-compat'
const app = container.getProvider('app').getImmediate();
const authProvider = container.getProvider('auth-internal');

// TODO: get StorageService instance from component framework instead
// of creating a new one.
const storageServiceCompat: StorageServiceCompat = new StorageServiceCompat(
app,
new StorageService(app, authProvider, new XhrIoPool(), url)
);
return storageServiceCompat;
}

export function registerStorage(instance: _FirebaseNamespace): void {
const namespaceExports = {
// no-inline
TaskState,
TaskEvent,
StringFormat,
Storage: StorageService,
Reference: ReferenceCompat
};
instance.INTERNAL.registerComponent(
new Component(STORAGE_TYPE, factory, ComponentType.PUBLIC)
.setServiceProps(namespaceExports)
.setMultipleInstances(true)
);

instance.registerVersion(name, version);
}

registerStorage(firebase as _FirebaseNamespace);

/**
* Define extension behavior for `registerStorage`
*/
declare module '@firebase/app-types' {
interface FirebaseNamespace {
storage?: {
(app?: FirebaseApp): types.FirebaseStorage;
Storage: typeof types.FirebaseStorage;

StringFormat: {
BASE64: types.StringFormat;
BASE64URL: types.StringFormat;
DATA_URL: types.StringFormat;
RAW: types.StringFormat;
};
TaskEvent: {
STATE_CHANGED: types.TaskEvent;
};
TaskState: {
CANCELED: types.TaskState;
ERROR: types.TaskState;
PAUSED: types.TaskState;
RUNNING: types.TaskState;
SUCCESS: types.TaskState;
};
};
}
interface FirebaseApp {
storage?(storageBucket?: string): types.FirebaseStorage;
}
}
42 changes: 42 additions & 0 deletions packages/storage/compat/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license
* Copyright 2020 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 * as types from '@firebase/storage-types';
import { ListResult } from '../src/list';
import { ReferenceCompat } from './reference';
import { StorageServiceCompat } from './service';

export class ListResultCompat implements types.ListResult {
constructor(
private readonly _delegate: ListResult,
private readonly _service: StorageServiceCompat
) {}

get prefixes(): ReferenceCompat[] {
return this._delegate.prefixes.map(
ref => new ReferenceCompat(ref, this._service)
);
}
get items(): ReferenceCompat[] {
return this._delegate.items.map(
ref => new ReferenceCompat(ref, this._service)
);
}
get nextPageToken(): string | null {
return this._delegate.nextPageToken || null;
}
}
Loading