UNPKG

19.3 kBJavaScriptView Raw
1import { Component, ComponentContainer } from '@firebase/component';
2import { Logger, setUserLogHandler, setLogLevel as setLogLevel$1 } from '@firebase/logger';
3import { ErrorFactory, deepEqual } from '@firebase/util';
4export { FirebaseError } from '@firebase/util';
5
6/**
7 * @license
8 * Copyright 2019 Google LLC
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22class PlatformLoggerServiceImpl {
23 constructor(container) {
24 this.container = container;
25 }
26 // In initial implementation, this will be called by installations on
27 // auth token refresh, and installations will send this string.
28 getPlatformInfoString() {
29 const providers = this.container.getProviders();
30 // Loop through providers and get library/version pairs from any that are
31 // version components.
32 return providers
33 .map(provider => {
34 if (isVersionServiceProvider(provider)) {
35 const service = provider.getImmediate();
36 return `${service.library}/${service.version}`;
37 }
38 else {
39 return null;
40 }
41 })
42 .filter(logString => logString)
43 .join(' ');
44 }
45}
46/**
47 *
48 * @param provider check if this provider provides a VersionService
49 *
50 * NOTE: Using Provider<'app-version'> is a hack to indicate that the provider
51 * provides VersionService. The provider is not necessarily a 'app-version'
52 * provider.
53 */
54function isVersionServiceProvider(provider) {
55 const component = provider.getComponent();
56 return (component === null || component === void 0 ? void 0 : component.type) === "VERSION" /* VERSION */;
57}
58
59const name$o = "@firebase/app";
60const version$1 = "0.7.10";
61
62/**
63 * @license
64 * Copyright 2019 Google LLC
65 *
66 * Licensed under the Apache License, Version 2.0 (the "License");
67 * you may not use this file except in compliance with the License.
68 * You may obtain a copy of the License at
69 *
70 * http://www.apache.org/licenses/LICENSE-2.0
71 *
72 * Unless required by applicable law or agreed to in writing, software
73 * distributed under the License is distributed on an "AS IS" BASIS,
74 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
75 * See the License for the specific language governing permissions and
76 * limitations under the License.
77 */
78const logger = new Logger('@firebase/app');
79
80const name$n = "@firebase/app-compat";
81
82const name$m = "@firebase/analytics-compat";
83
84const name$l = "@firebase/analytics";
85
86const name$k = "@firebase/app-check-compat";
87
88const name$j = "@firebase/app-check";
89
90const name$i = "@firebase/auth";
91
92const name$h = "@firebase/auth-compat";
93
94const name$g = "@firebase/database";
95
96const name$f = "@firebase/database-compat";
97
98const name$e = "@firebase/functions";
99
100const name$d = "@firebase/functions-compat";
101
102const name$c = "@firebase/installations";
103
104const name$b = "@firebase/installations-compat";
105
106const name$a = "@firebase/messaging";
107
108const name$9 = "@firebase/messaging-compat";
109
110const name$8 = "@firebase/performance";
111
112const name$7 = "@firebase/performance-compat";
113
114const name$6 = "@firebase/remote-config";
115
116const name$5 = "@firebase/remote-config-compat";
117
118const name$4 = "@firebase/storage";
119
120const name$3 = "@firebase/storage-compat";
121
122const name$2 = "@firebase/firestore";
123
124const name$1 = "@firebase/firestore-compat";
125
126const name = "firebase";
127const version = "9.6.0";
128
129/**
130 * @license
131 * Copyright 2019 Google LLC
132 *
133 * Licensed under the Apache License, Version 2.0 (the "License");
134 * you may not use this file except in compliance with the License.
135 * You may obtain a copy of the License at
136 *
137 * http://www.apache.org/licenses/LICENSE-2.0
138 *
139 * Unless required by applicable law or agreed to in writing, software
140 * distributed under the License is distributed on an "AS IS" BASIS,
141 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
142 * See the License for the specific language governing permissions and
143 * limitations under the License.
144 */
145/**
146 * The default app name
147 *
148 * @internal
149 */
150const DEFAULT_ENTRY_NAME = '[DEFAULT]';
151const PLATFORM_LOG_STRING = {
152 [name$o]: 'fire-core',
153 [name$n]: 'fire-core-compat',
154 [name$l]: 'fire-analytics',
155 [name$m]: 'fire-analytics-compat',
156 [name$j]: 'fire-app-check',
157 [name$k]: 'fire-app-check-compat',
158 [name$i]: 'fire-auth',
159 [name$h]: 'fire-auth-compat',
160 [name$g]: 'fire-rtdb',
161 [name$f]: 'fire-rtdb-compat',
162 [name$e]: 'fire-fn',
163 [name$d]: 'fire-fn-compat',
164 [name$c]: 'fire-iid',
165 [name$b]: 'fire-iid-compat',
166 [name$a]: 'fire-fcm',
167 [name$9]: 'fire-fcm-compat',
168 [name$8]: 'fire-perf',
169 [name$7]: 'fire-perf-compat',
170 [name$6]: 'fire-rc',
171 [name$5]: 'fire-rc-compat',
172 [name$4]: 'fire-gcs',
173 [name$3]: 'fire-gcs-compat',
174 [name$2]: 'fire-fst',
175 [name$1]: 'fire-fst-compat',
176 'fire-js': 'fire-js',
177 [name]: 'fire-js-all'
178};
179
180/**
181 * @license
182 * Copyright 2019 Google LLC
183 *
184 * Licensed under the Apache License, Version 2.0 (the "License");
185 * you may not use this file except in compliance with the License.
186 * You may obtain a copy of the License at
187 *
188 * http://www.apache.org/licenses/LICENSE-2.0
189 *
190 * Unless required by applicable law or agreed to in writing, software
191 * distributed under the License is distributed on an "AS IS" BASIS,
192 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
193 * See the License for the specific language governing permissions and
194 * limitations under the License.
195 */
196/**
197 * @internal
198 */
199const _apps = new Map();
200/**
201 * Registered components.
202 *
203 * @internal
204 */
205// eslint-disable-next-line @typescript-eslint/no-explicit-any
206const _components = new Map();
207/**
208 * @param component - the component being added to this app's container
209 *
210 * @internal
211 */
212function _addComponent(app, component) {
213 try {
214 app.container.addComponent(component);
215 }
216 catch (e) {
217 logger.debug(`Component ${component.name} failed to register with FirebaseApp ${app.name}`, e);
218 }
219}
220/**
221 *
222 * @internal
223 */
224function _addOrOverwriteComponent(app, component) {
225 app.container.addOrOverwriteComponent(component);
226}
227/**
228 *
229 * @param component - the component to register
230 * @returns whether or not the component is registered successfully
231 *
232 * @internal
233 */
234function _registerComponent(component) {
235 const componentName = component.name;
236 if (_components.has(componentName)) {
237 logger.debug(`There were multiple attempts to register component ${componentName}.`);
238 return false;
239 }
240 _components.set(componentName, component);
241 // add the component to existing app instances
242 for (const app of _apps.values()) {
243 _addComponent(app, component);
244 }
245 return true;
246}
247/**
248 *
249 * @param app - FirebaseApp instance
250 * @param name - service name
251 *
252 * @returns the provider for the service with the matching name
253 *
254 * @internal
255 */
256function _getProvider(app, name) {
257 return app.container.getProvider(name);
258}
259/**
260 *
261 * @param app - FirebaseApp instance
262 * @param name - service name
263 * @param instanceIdentifier - service instance identifier in case the service supports multiple instances
264 *
265 * @internal
266 */
267function _removeServiceInstance(app, name, instanceIdentifier = DEFAULT_ENTRY_NAME) {
268 _getProvider(app, name).clearInstance(instanceIdentifier);
269}
270/**
271 * Test only
272 *
273 * @internal
274 */
275function _clearComponents() {
276 _components.clear();
277}
278
279/**
280 * @license
281 * Copyright 2019 Google LLC
282 *
283 * Licensed under the Apache License, Version 2.0 (the "License");
284 * you may not use this file except in compliance with the License.
285 * You may obtain a copy of the License at
286 *
287 * http://www.apache.org/licenses/LICENSE-2.0
288 *
289 * Unless required by applicable law or agreed to in writing, software
290 * distributed under the License is distributed on an "AS IS" BASIS,
291 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
292 * See the License for the specific language governing permissions and
293 * limitations under the License.
294 */
295const ERRORS = {
296 ["no-app" /* NO_APP */]: "No Firebase App '{$appName}' has been created - " +
297 'call Firebase App.initializeApp()',
298 ["bad-app-name" /* BAD_APP_NAME */]: "Illegal App name: '{$appName}",
299 ["duplicate-app" /* DUPLICATE_APP */]: "Firebase App named '{$appName}' already exists with different options or config",
300 ["app-deleted" /* APP_DELETED */]: "Firebase App named '{$appName}' already deleted",
301 ["invalid-app-argument" /* INVALID_APP_ARGUMENT */]: 'firebase.{$appName}() takes either no argument or a ' +
302 'Firebase App instance.',
303 ["invalid-log-argument" /* INVALID_LOG_ARGUMENT */]: 'First argument to `onLog` must be null or a function.'
304};
305const ERROR_FACTORY = new ErrorFactory('app', 'Firebase', ERRORS);
306
307/**
308 * @license
309 * Copyright 2019 Google LLC
310 *
311 * Licensed under the Apache License, Version 2.0 (the "License");
312 * you may not use this file except in compliance with the License.
313 * You may obtain a copy of the License at
314 *
315 * http://www.apache.org/licenses/LICENSE-2.0
316 *
317 * Unless required by applicable law or agreed to in writing, software
318 * distributed under the License is distributed on an "AS IS" BASIS,
319 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
320 * See the License for the specific language governing permissions and
321 * limitations under the License.
322 */
323class FirebaseAppImpl {
324 constructor(options, config, container) {
325 this._isDeleted = false;
326 this._options = Object.assign({}, options);
327 this._config = Object.assign({}, config);
328 this._name = config.name;
329 this._automaticDataCollectionEnabled =
330 config.automaticDataCollectionEnabled;
331 this._container = container;
332 this.container.addComponent(new Component('app', () => this, "PUBLIC" /* PUBLIC */));
333 }
334 get automaticDataCollectionEnabled() {
335 this.checkDestroyed();
336 return this._automaticDataCollectionEnabled;
337 }
338 set automaticDataCollectionEnabled(val) {
339 this.checkDestroyed();
340 this._automaticDataCollectionEnabled = val;
341 }
342 get name() {
343 this.checkDestroyed();
344 return this._name;
345 }
346 get options() {
347 this.checkDestroyed();
348 return this._options;
349 }
350 get config() {
351 this.checkDestroyed();
352 return this._config;
353 }
354 get container() {
355 return this._container;
356 }
357 get isDeleted() {
358 return this._isDeleted;
359 }
360 set isDeleted(val) {
361 this._isDeleted = val;
362 }
363 /**
364 * This function will throw an Error if the App has already been deleted -
365 * use before performing API actions on the App.
366 */
367 checkDestroyed() {
368 if (this.isDeleted) {
369 throw ERROR_FACTORY.create("app-deleted" /* APP_DELETED */, { appName: this._name });
370 }
371 }
372}
373
374/**
375 * @license
376 * Copyright 2019 Google LLC
377 *
378 * Licensed under the Apache License, Version 2.0 (the "License");
379 * you may not use this file except in compliance with the License.
380 * You may obtain a copy of the License at
381 *
382 * http://www.apache.org/licenses/LICENSE-2.0
383 *
384 * Unless required by applicable law or agreed to in writing, software
385 * distributed under the License is distributed on an "AS IS" BASIS,
386 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
387 * See the License for the specific language governing permissions and
388 * limitations under the License.
389 */
390/**
391 * The current SDK version.
392 *
393 * @public
394 */
395const SDK_VERSION = version;
396function initializeApp(options, rawConfig = {}) {
397 if (typeof rawConfig !== 'object') {
398 const name = rawConfig;
399 rawConfig = { name };
400 }
401 const config = Object.assign({ name: DEFAULT_ENTRY_NAME, automaticDataCollectionEnabled: false }, rawConfig);
402 const name = config.name;
403 if (typeof name !== 'string' || !name) {
404 throw ERROR_FACTORY.create("bad-app-name" /* BAD_APP_NAME */, {
405 appName: String(name)
406 });
407 }
408 const existingApp = _apps.get(name);
409 if (existingApp) {
410 // return the existing app if options and config deep equal the ones in the existing app.
411 if (deepEqual(options, existingApp.options) &&
412 deepEqual(config, existingApp.config)) {
413 return existingApp;
414 }
415 else {
416 throw ERROR_FACTORY.create("duplicate-app" /* DUPLICATE_APP */, { appName: name });
417 }
418 }
419 const container = new ComponentContainer(name);
420 for (const component of _components.values()) {
421 container.addComponent(component);
422 }
423 const newApp = new FirebaseAppImpl(options, config, container);
424 _apps.set(name, newApp);
425 return newApp;
426}
427/**
428 * Retrieves a {@link @firebase/app#FirebaseApp} instance.
429 *
430 * When called with no arguments, the default app is returned. When an app name
431 * is provided, the app corresponding to that name is returned.
432 *
433 * An exception is thrown if the app being retrieved has not yet been
434 * initialized.
435 *
436 * @example
437 * ```javascript
438 * // Return the default app
439 * const app = getApp();
440 * ```
441 *
442 * @example
443 * ```javascript
444 * // Return a named app
445 * const otherApp = getApp("otherApp");
446 * ```
447 *
448 * @param name - Optional name of the app to return. If no name is
449 * provided, the default is `"[DEFAULT]"`.
450 *
451 * @returns The app corresponding to the provided app name.
452 * If no app name is provided, the default app is returned.
453 *
454 * @public
455 */
456function getApp(name = DEFAULT_ENTRY_NAME) {
457 const app = _apps.get(name);
458 if (!app) {
459 throw ERROR_FACTORY.create("no-app" /* NO_APP */, { appName: name });
460 }
461 return app;
462}
463/**
464 * A (read-only) array of all initialized apps.
465 * @public
466 */
467function getApps() {
468 return Array.from(_apps.values());
469}
470/**
471 * Renders this app unusable and frees the resources of all associated
472 * services.
473 *
474 * @example
475 * ```javascript
476 * deleteApp(app)
477 * .then(function() {
478 * console.log("App deleted successfully");
479 * })
480 * .catch(function(error) {
481 * console.log("Error deleting app:", error);
482 * });
483 * ```
484 *
485 * @public
486 */
487async function deleteApp(app) {
488 const name = app.name;
489 if (_apps.has(name)) {
490 _apps.delete(name);
491 await Promise.all(app.container
492 .getProviders()
493 .map(provider => provider.delete()));
494 app.isDeleted = true;
495 }
496}
497/**
498 * Registers a library's name and version for platform logging purposes.
499 * @param library - Name of 1p or 3p library (e.g. firestore, angularfire)
500 * @param version - Current version of that library.
501 * @param variant - Bundle variant, e.g., node, rn, etc.
502 *
503 * @public
504 */
505function registerVersion(libraryKeyOrName, version, variant) {
506 var _a;
507 // TODO: We can use this check to whitelist strings when/if we set up
508 // a good whitelist system.
509 let library = (_a = PLATFORM_LOG_STRING[libraryKeyOrName]) !== null && _a !== void 0 ? _a : libraryKeyOrName;
510 if (variant) {
511 library += `-${variant}`;
512 }
513 const libraryMismatch = library.match(/\s|\//);
514 const versionMismatch = version.match(/\s|\//);
515 if (libraryMismatch || versionMismatch) {
516 const warning = [
517 `Unable to register library "${library}" with version "${version}":`
518 ];
519 if (libraryMismatch) {
520 warning.push(`library name "${library}" contains illegal characters (whitespace or "/")`);
521 }
522 if (libraryMismatch && versionMismatch) {
523 warning.push('and');
524 }
525 if (versionMismatch) {
526 warning.push(`version name "${version}" contains illegal characters (whitespace or "/")`);
527 }
528 logger.warn(warning.join(' '));
529 return;
530 }
531 _registerComponent(new Component(`${library}-version`, () => ({ library, version }), "VERSION" /* VERSION */));
532}
533/**
534 * Sets log handler for all Firebase SDKs.
535 * @param logCallback - An optional custom log handler that executes user code whenever
536 * the Firebase SDK makes a logging call.
537 *
538 * @public
539 */
540function onLog(logCallback, options) {
541 if (logCallback !== null && typeof logCallback !== 'function') {
542 throw ERROR_FACTORY.create("invalid-log-argument" /* INVALID_LOG_ARGUMENT */);
543 }
544 setUserLogHandler(logCallback, options);
545}
546/**
547 * Sets log level for all Firebase SDKs.
548 *
549 * All of the log types above the current log level are captured (i.e. if
550 * you set the log level to `info`, errors are logged, but `debug` and
551 * `verbose` logs are not).
552 *
553 * @public
554 */
555function setLogLevel(logLevel) {
556 setLogLevel$1(logLevel);
557}
558
559/**
560 * @license
561 * Copyright 2019 Google LLC
562 *
563 * Licensed under the Apache License, Version 2.0 (the "License");
564 * you may not use this file except in compliance with the License.
565 * You may obtain a copy of the License at
566 *
567 * http://www.apache.org/licenses/LICENSE-2.0
568 *
569 * Unless required by applicable law or agreed to in writing, software
570 * distributed under the License is distributed on an "AS IS" BASIS,
571 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
572 * See the License for the specific language governing permissions and
573 * limitations under the License.
574 */
575function registerCoreComponents(variant) {
576 _registerComponent(new Component('platform-logger', container => new PlatformLoggerServiceImpl(container), "PRIVATE" /* PRIVATE */));
577 // Register `app` package.
578 registerVersion(name$o, version$1, variant);
579 // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
580 registerVersion(name$o, version$1, 'esm2017');
581 // Register platform SDK identifier (no version).
582 registerVersion('fire-js', '');
583}
584
585/**
586 * Firebase App
587 *
588 * @remarks This package coordinates the communication between the different Firebase components
589 * @packageDocumentation
590 */
591registerCoreComponents('');
592
593export { SDK_VERSION, DEFAULT_ENTRY_NAME as _DEFAULT_ENTRY_NAME, _addComponent, _addOrOverwriteComponent, _apps, _clearComponents, _components, _getProvider, _registerComponent, _removeServiceInstance, deleteApp, getApp, getApps, initializeApp, onLog, registerVersion, setLogLevel };
594//# sourceMappingURL=index.esm2017.js.map