Skip to content

Commit f9dc50e

Browse files
authored
Set up Storage modularization (#3499)
Refactor storage for modularization.
1 parent 996004f commit f9dc50e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2470
-1055
lines changed

.changeset/silly-boats-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/storage': patch
3+
---
4+
5+
Refactored Storage to allow for modularization.

config/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ module.exports = {
184184
'assertionStyle': 'as'
185185
}
186186
],
187-
'@typescript-eslint/no-explicit-any': 'error',
187+
'@typescript-eslint/no-explicit-any': ['error', { 'ignoreRestArgs': true }],
188188
'@typescript-eslint/no-namespace': [
189189
'error',
190190
{

packages/storage/.eslintrc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ module.exports = {
3737
'import/no-extraneous-dependencies': [
3838
'error',
3939
{
40-
'packageDir': [path.resolve(__dirname, '../../'), __dirname]
40+
'packageDir': [
41+
path.resolve(__dirname, '../../'),
42+
__dirname,
43+
path.resolve(__dirname, 'exp')
44+
]
4145
}
4246
]
4347
}

packages/storage/api-extractor.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../config/api-extractor.json",
3+
// Point it to your entry point d.ts file.
4+
"mainEntryPointFilePath": "<projectFolder>/exp/dist/exp/index.d.ts",
5+
"dtsRollup": {
6+
"enabled": true,
7+
"untrimmedFilePath": "<projectFolder>/exp/dist/<unscopedPackageName>.d.ts",
8+
"publicTrimmedFilePath": "<projectFolder>/exp/dist/<unscopedPackageName>-public.d.ts"
9+
}
10+
}

packages/storage/compat/index.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import firebase from '@firebase/app';
19+
import { _FirebaseNamespace } from '@firebase/app-types/private';
20+
import { StringFormat } from '../src/implementation/string';
21+
import { TaskEvent, TaskState } from '../src/implementation/taskenums';
22+
23+
import { XhrIoPool } from '../src/implementation/xhriopool';
24+
import { ReferenceCompat } from './reference';
25+
import { StorageServiceCompat } from './service';
26+
import { StorageService } from '../src/service';
27+
import * as types from '@firebase/storage-types';
28+
import {
29+
Component,
30+
ComponentType,
31+
ComponentContainer
32+
} from '@firebase/component';
33+
34+
import { name, version } from '../package.json';
35+
36+
/**
37+
* Type constant for Firebase Storage.
38+
*/
39+
const STORAGE_TYPE = 'storage';
40+
41+
function factory(
42+
container: ComponentContainer,
43+
url?: string
44+
): types.FirebaseStorage {
45+
// Dependencies
46+
// TODO: This should eventually be 'app-compat'
47+
const app = container.getProvider('app').getImmediate();
48+
const authProvider = container.getProvider('auth-internal');
49+
50+
// TODO: get StorageService instance from component framework instead
51+
// of creating a new one.
52+
const storageServiceCompat: StorageServiceCompat = new StorageServiceCompat(
53+
app,
54+
new StorageService(app, authProvider, new XhrIoPool(), url)
55+
);
56+
return storageServiceCompat;
57+
}
58+
59+
export function registerStorage(instance: _FirebaseNamespace): void {
60+
const namespaceExports = {
61+
// no-inline
62+
TaskState,
63+
TaskEvent,
64+
StringFormat,
65+
Storage: StorageService,
66+
Reference: ReferenceCompat
67+
};
68+
instance.INTERNAL.registerComponent(
69+
new Component(STORAGE_TYPE, factory, ComponentType.PUBLIC)
70+
.setServiceProps(namespaceExports)
71+
.setMultipleInstances(true)
72+
);
73+
74+
instance.registerVersion(name, version);
75+
}
76+
77+
registerStorage(firebase as _FirebaseNamespace);
78+
79+
/**
80+
* Define extension behavior for `registerStorage`
81+
*/
82+
declare module '@firebase/app-types' {
83+
interface FirebaseNamespace {
84+
storage?: {
85+
(app?: FirebaseApp): types.FirebaseStorage;
86+
Storage: typeof types.FirebaseStorage;
87+
88+
StringFormat: {
89+
BASE64: types.StringFormat;
90+
BASE64URL: types.StringFormat;
91+
DATA_URL: types.StringFormat;
92+
RAW: types.StringFormat;
93+
};
94+
TaskEvent: {
95+
STATE_CHANGED: types.TaskEvent;
96+
};
97+
TaskState: {
98+
CANCELED: types.TaskState;
99+
ERROR: types.TaskState;
100+
PAUSED: types.TaskState;
101+
RUNNING: types.TaskState;
102+
SUCCESS: types.TaskState;
103+
};
104+
};
105+
}
106+
interface FirebaseApp {
107+
storage?(storageBucket?: string): types.FirebaseStorage;
108+
}
109+
}

packages/storage/compat/list.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import * as types from '@firebase/storage-types';
19+
import { ListResult } from '../src/list';
20+
import { ReferenceCompat } from './reference';
21+
import { StorageServiceCompat } from './service';
22+
23+
export class ListResultCompat implements types.ListResult {
24+
constructor(
25+
private readonly _delegate: ListResult,
26+
private readonly _service: StorageServiceCompat
27+
) {}
28+
29+
get prefixes(): ReferenceCompat[] {
30+
return this._delegate.prefixes.map(
31+
ref => new ReferenceCompat(ref, this._service)
32+
);
33+
}
34+
get items(): ReferenceCompat[] {
35+
return this._delegate.items.map(
36+
ref => new ReferenceCompat(ref, this._service)
37+
);
38+
}
39+
get nextPageToken(): string | null {
40+
return this._delegate.nextPageToken || null;
41+
}
42+
}

0 commit comments

Comments
 (0)