-
Notifications
You must be signed in to change notification settings - Fork 874
/
firebaseNamespaceCore.ts
293 lines (261 loc) · 8.77 KB
/
firebaseNamespaceCore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* @license
* Copyright 2019 Google Inc.
*
* 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 {
FirebaseApp,
FirebaseOptions,
FirebaseNamespace,
FirebaseAppConfig
} from '@firebase/app-types';
import {
_FirebaseApp,
_FirebaseNamespace,
FirebaseService,
FirebaseServiceNamespace
} from '@firebase/app-types/private';
import { deepExtend, contains } from '@firebase/util';
import { FirebaseAppImpl } from './firebaseApp';
import { ERROR_FACTORY, AppError } from './errors';
import { FirebaseAppLiteImpl } from './lite/firebaseAppLite';
import { DEFAULT_ENTRY_NAME, PLATFORM_LOG_STRING } from './constants';
import { version } from '../../firebase/package.json';
import { logger } from './logger';
import { Component, ComponentType, Name } from '@firebase/component';
/**
* Because auth can't share code with other components, we attach the utility functions
* in an internal namespace to share code.
* This function return a firebase namespace object without
* any utility functions, so it can be shared between the regular firebaseNamespace and
* the lite version.
*/
export function createFirebaseNamespaceCore(
firebaseAppImpl: typeof FirebaseAppImpl | typeof FirebaseAppLiteImpl
): FirebaseNamespace {
const apps: { [name: string]: FirebaseApp } = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const components = new Map<string, Component<any>>();
// A namespace is a plain JavaScript Object.
const namespace: FirebaseNamespace = {
// Hack to prevent Babel from modifying the object returned
// as the firebase namespace.
// @ts-ignore
__esModule: true,
initializeApp,
// @ts-ignore
app,
registerVersion,
// @ts-ignore
apps: null,
SDK_VERSION: version,
INTERNAL: {
registerComponent,
removeApp,
components,
useAsService
}
};
// Inject a circular default export to allow Babel users who were previously
// using:
//
// import firebase from 'firebase';
// which becomes: var firebase = require('firebase').default;
//
// instead of
//
// import * as firebase from 'firebase';
// which becomes: var firebase = require('firebase');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(namespace as any)['default'] = namespace;
// firebase.apps is a read-only getter.
Object.defineProperty(namespace, 'apps', {
get: getApps
});
/**
* Called by App.delete() - but before any services associated with the App
* are deleted.
*/
function removeApp(name: string): void {
delete apps[name];
}
/**
* Get the App object for a given name (or DEFAULT).
*/
function app(name?: string): FirebaseApp {
name = name || DEFAULT_ENTRY_NAME;
if (!contains(apps, name)) {
throw ERROR_FACTORY.create(AppError.NO_APP, { appName: name });
}
return apps[name];
}
// @ts-ignore
app['App'] = firebaseAppImpl;
/**
* Create a new App instance (name must be unique).
*/
function initializeApp(
options: FirebaseOptions,
config?: FirebaseAppConfig
): FirebaseApp;
function initializeApp(options: FirebaseOptions, name?: string): FirebaseApp;
function initializeApp(
options: FirebaseOptions,
rawConfig = {}
): FirebaseApp {
if (typeof rawConfig !== 'object' || rawConfig === null) {
const name = rawConfig;
rawConfig = { name };
}
const config = rawConfig as FirebaseAppConfig;
if (config.name === undefined) {
config.name = DEFAULT_ENTRY_NAME;
}
const { name } = config;
if (typeof name !== 'string' || !name) {
throw ERROR_FACTORY.create(AppError.BAD_APP_NAME, {
appName: String(name)
});
}
if (contains(apps, name)) {
throw ERROR_FACTORY.create(AppError.DUPLICATE_APP, { appName: name });
}
const app = new firebaseAppImpl(
options,
config,
namespace as _FirebaseNamespace
);
apps[name] = app;
return app;
}
/*
* Return an array of all the non-deleted FirebaseApps.
*/
function getApps(): FirebaseApp[] {
// Make a copy so caller cannot mutate the apps list.
return Object.keys(apps).map(name => apps[name]);
}
function registerComponent(
component: Component
): FirebaseServiceNamespace<FirebaseService> | null {
const componentName = component.name;
if (components.has(componentName)) {
logger.debug(
`There were multiple attempts to register component ${componentName}.`
);
return component.type === ComponentType.PUBLIC
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
(namespace as any)[componentName]
: null;
}
components.set(componentName, component);
// create service namespace for public components
if (component.type === ComponentType.PUBLIC) {
// The Service namespace is an accessor function ...
const serviceNamespace = (
appArg: FirebaseApp = app()
): FirebaseService => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (typeof (appArg as any)[componentName] !== 'function') {
// Invalid argument.
// This happens in the following case: firebase.storage('gs:/')
throw ERROR_FACTORY.create(AppError.INVALID_APP_ARGUMENT, {
appName: componentName
});
}
// Forward service instance lookup to the FirebaseApp.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (appArg as any)[componentName]();
};
// ... and a container for service-level properties.
if (component.serviceProps !== undefined) {
deepExtend(serviceNamespace, component.serviceProps);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(namespace as any)[componentName] = serviceNamespace;
// Patch the FirebaseAppImpl prototype
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(firebaseAppImpl.prototype as any)[componentName] =
// TODO: The eslint disable can be removed and the 'ignoreRestArgs'
// option added to the no-explicit-any rule when ESlint releases it.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function(...args: any) {
const serviceFxn = this._getService.bind(this, componentName);
return serviceFxn.apply(
this,
component.multipleInstances ? args : []
);
};
}
// add the component to existing app instances
for (const appName of Object.keys(apps)) {
(apps[appName] as _FirebaseApp)._addComponent(component);
}
return component.type === ComponentType.PUBLIC
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
(namespace as any)[componentName]
: null;
}
function registerVersion(
libraryKeyOrName: string,
version: string,
variant?: string
): void {
// TODO: We can use this check to whitelist strings when/if we set up
// a good whitelist system.
let library = PLATFORM_LOG_STRING[libraryKeyOrName] ?? libraryKeyOrName;
if (variant) {
library += `-${variant}`;
}
const libraryMismatch = library.match(/\s|\//);
const versionMismatch = version.match(/\s|\//);
if (libraryMismatch || versionMismatch) {
const warning = [
`Unable to register library "${library}" with version "${version}":`
];
if (libraryMismatch) {
warning.push(
`library name "${library}" contains illegal characters (whitespace or "/")`
);
}
if (libraryMismatch && versionMismatch) {
warning.push('and');
}
if (versionMismatch) {
warning.push(
`version name "${version}" contains illegal characters (whitespace or "/")`
);
}
logger.warn(warning.join(' '));
return;
}
registerComponent(
new Component(
`${library}-version` as Name,
() => ({ library, version }),
ComponentType.VERSION
)
);
}
// Map the requested service to a registered service name
// (used to map auth to serverAuth service when needed).
function useAsService(app: FirebaseApp, name: string): string | null {
if (name === 'serverAuth') {
return null;
}
const useService = name;
return useService;
}
return namespace;
}