mirror of
https://github.com/musix-org/musix-oss
synced 2024-11-14 16:00:17 +00:00
537 lines
20 KiB
JavaScript
537 lines
20 KiB
JavaScript
import { ErrorFactory, deepCopy, deepExtend, contains, createSubscribe, isBrowser, isNode } from '@firebase/util';
|
|
import { Logger } from '@firebase/logger';
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
const ERRORS = {
|
|
["no-app" /* NO_APP */]: "No Firebase App '{$appName}' has been created - " +
|
|
'call Firebase App.initializeApp()',
|
|
["bad-app-name" /* BAD_APP_NAME */]: "Illegal App name: '{$appName}",
|
|
["duplicate-app" /* DUPLICATE_APP */]: "Firebase App named '{$appName}' already exists",
|
|
["app-deleted" /* APP_DELETED */]: "Firebase App named '{$appName}' already deleted",
|
|
["invalid-app-argument" /* INVALID_APP_ARGUMENT */]: 'firebase.{$appName}() takes either no argument or a ' +
|
|
'Firebase App instance.'
|
|
};
|
|
const ERROR_FACTORY = new ErrorFactory('app', 'Firebase', ERRORS);
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
const DEFAULT_ENTRY_NAME = '[DEFAULT]';
|
|
|
|
/**
|
|
* @license
|
|
* Copyright 2017 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.
|
|
*/
|
|
// An array to capture listeners before the true auth functions
|
|
// exist
|
|
let tokenListeners = [];
|
|
/**
|
|
* Global context object for a collection of services using
|
|
* a shared authentication state.
|
|
*/
|
|
class FirebaseAppImpl {
|
|
constructor(options, config, firebase_) {
|
|
this.firebase_ = firebase_;
|
|
this.isDeleted_ = false;
|
|
this.services_ = {};
|
|
this.name_ = config.name;
|
|
this.automaticDataCollectionEnabled_ =
|
|
config.automaticDataCollectionEnabled || false;
|
|
this.options_ = deepCopy(options);
|
|
this.INTERNAL = {
|
|
getUid: () => null,
|
|
getToken: () => Promise.resolve(null),
|
|
addAuthTokenListener: (callback) => {
|
|
tokenListeners.push(callback);
|
|
// Make sure callback is called, asynchronously, in the absence of the auth module
|
|
setTimeout(() => callback(null), 0);
|
|
},
|
|
removeAuthTokenListener: callback => {
|
|
tokenListeners = tokenListeners.filter(listener => listener !== callback);
|
|
}
|
|
};
|
|
}
|
|
get automaticDataCollectionEnabled() {
|
|
this.checkDestroyed_();
|
|
return this.automaticDataCollectionEnabled_;
|
|
}
|
|
set automaticDataCollectionEnabled(val) {
|
|
this.checkDestroyed_();
|
|
this.automaticDataCollectionEnabled_ = val;
|
|
}
|
|
get name() {
|
|
this.checkDestroyed_();
|
|
return this.name_;
|
|
}
|
|
get options() {
|
|
this.checkDestroyed_();
|
|
return this.options_;
|
|
}
|
|
delete() {
|
|
return new Promise(resolve => {
|
|
this.checkDestroyed_();
|
|
resolve();
|
|
})
|
|
.then(() => {
|
|
this.firebase_.INTERNAL.removeApp(this.name_);
|
|
const services = [];
|
|
for (const serviceKey of Object.keys(this.services_)) {
|
|
for (const instanceKey of Object.keys(this.services_[serviceKey])) {
|
|
services.push(this.services_[serviceKey][instanceKey]);
|
|
}
|
|
}
|
|
return Promise.all(services
|
|
.filter(service => 'INTERNAL' in service)
|
|
.map(service => service.INTERNAL.delete()));
|
|
})
|
|
.then(() => {
|
|
this.isDeleted_ = true;
|
|
this.services_ = {};
|
|
});
|
|
}
|
|
/**
|
|
* Return a service instance associated with this app (creating it
|
|
* on demand), identified by the passed instanceIdentifier.
|
|
*
|
|
* NOTE: Currently storage and functions are the only ones that are leveraging this
|
|
* functionality. They invoke it by calling:
|
|
*
|
|
* ```javascript
|
|
* firebase.app().storage('STORAGE BUCKET ID')
|
|
* ```
|
|
*
|
|
* The service name is passed to this already
|
|
* @internal
|
|
*/
|
|
_getService(name, instanceIdentifier = DEFAULT_ENTRY_NAME) {
|
|
this.checkDestroyed_();
|
|
if (!this.services_[name]) {
|
|
this.services_[name] = {};
|
|
}
|
|
if (!this.services_[name][instanceIdentifier]) {
|
|
/**
|
|
* If a custom instance has been defined (i.e. not '[DEFAULT]')
|
|
* then we will pass that instance on, otherwise we pass `null`
|
|
*/
|
|
const instanceSpecifier = instanceIdentifier !== DEFAULT_ENTRY_NAME
|
|
? instanceIdentifier
|
|
: undefined;
|
|
const service = this.firebase_.INTERNAL.factories[name](this, this.extendApp.bind(this), instanceSpecifier);
|
|
this.services_[name][instanceIdentifier] = service;
|
|
}
|
|
return this.services_[name][instanceIdentifier];
|
|
}
|
|
/**
|
|
* Remove a service instance from the cache, so we will create a new instance for this service
|
|
* when people try to get this service again.
|
|
*
|
|
* NOTE: currently only firestore is using this functionality to support firestore shutdown.
|
|
*
|
|
* @param name The service name
|
|
* @param instanceIdentifier instance identifier in case multiple instances are allowed
|
|
* @internal
|
|
*/
|
|
_removeServiceInstance(name, instanceIdentifier = DEFAULT_ENTRY_NAME) {
|
|
if (this.services_[name] && this.services_[name][instanceIdentifier]) {
|
|
delete this.services_[name][instanceIdentifier];
|
|
}
|
|
}
|
|
/**
|
|
* Callback function used to extend an App instance at the time
|
|
* of service instance creation.
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
extendApp(props) {
|
|
// Copy the object onto the FirebaseAppImpl prototype
|
|
deepExtend(this, props);
|
|
/**
|
|
* If the app has overwritten the addAuthTokenListener stub, forward
|
|
* the active token listeners on to the true fxn.
|
|
*
|
|
* TODO: This function is required due to our current module
|
|
* structure. Once we are able to rely strictly upon a single module
|
|
* implementation, this code should be refactored and Auth should
|
|
* provide these stubs and the upgrade logic
|
|
*/
|
|
if (props.INTERNAL && props.INTERNAL.addAuthTokenListener) {
|
|
tokenListeners.forEach(listener => {
|
|
this.INTERNAL.addAuthTokenListener(listener);
|
|
});
|
|
tokenListeners = [];
|
|
}
|
|
}
|
|
/**
|
|
* This function will throw an Error if the App has already been deleted -
|
|
* use before performing API actions on the App.
|
|
*/
|
|
checkDestroyed_() {
|
|
if (this.isDeleted_) {
|
|
throw ERROR_FACTORY.create("app-deleted" /* APP_DELETED */, { appName: this.name_ });
|
|
}
|
|
}
|
|
}
|
|
// Prevent dead-code elimination of these methods w/o invalid property
|
|
// copying.
|
|
(FirebaseAppImpl.prototype.name && FirebaseAppImpl.prototype.options) ||
|
|
FirebaseAppImpl.prototype.delete ||
|
|
console.log('dc');
|
|
|
|
const version = "6.6.0";
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
const logger = new Logger('@firebase/app');
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
/**
|
|
* 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.
|
|
*/
|
|
function createFirebaseNamespaceCore(firebaseAppImpl) {
|
|
const apps = {};
|
|
const factories = {};
|
|
const appHooks = {};
|
|
// A namespace is a plain JavaScript Object.
|
|
const namespace = {
|
|
// Hack to prevent Babel from modifying the object returned
|
|
// as the firebase namespace.
|
|
// @ts-ignore
|
|
__esModule: true,
|
|
initializeApp,
|
|
// @ts-ignore
|
|
app,
|
|
// @ts-ignore
|
|
apps: null,
|
|
SDK_VERSION: version,
|
|
INTERNAL: {
|
|
registerService,
|
|
removeApp,
|
|
factories,
|
|
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['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) {
|
|
const app = apps[name];
|
|
callAppHooks(app, 'delete');
|
|
delete apps[name];
|
|
}
|
|
/**
|
|
* Get the App object for a given name (or DEFAULT).
|
|
*/
|
|
function app(name) {
|
|
name = name || DEFAULT_ENTRY_NAME;
|
|
if (!contains(apps, name)) {
|
|
throw ERROR_FACTORY.create("no-app" /* NO_APP */, { appName: name });
|
|
}
|
|
return apps[name];
|
|
}
|
|
// @ts-ignore
|
|
app['App'] = firebaseAppImpl;
|
|
function initializeApp(options, rawConfig = {}) {
|
|
if (typeof rawConfig !== 'object' || rawConfig === null) {
|
|
const name = rawConfig;
|
|
rawConfig = { name };
|
|
}
|
|
const config = rawConfig;
|
|
if (config.name === undefined) {
|
|
config.name = DEFAULT_ENTRY_NAME;
|
|
}
|
|
const { name } = config;
|
|
if (typeof name !== 'string' || !name) {
|
|
throw ERROR_FACTORY.create("bad-app-name" /* BAD_APP_NAME */, {
|
|
appName: String(name)
|
|
});
|
|
}
|
|
if (contains(apps, name)) {
|
|
throw ERROR_FACTORY.create("duplicate-app" /* DUPLICATE_APP */, { appName: name });
|
|
}
|
|
const app = new firebaseAppImpl(options, config, namespace);
|
|
apps[name] = app;
|
|
callAppHooks(app, 'create');
|
|
return app;
|
|
}
|
|
/*
|
|
* Return an array of all the non-deleted FirebaseApps.
|
|
*/
|
|
function getApps() {
|
|
// Make a copy so caller cannot mutate the apps list.
|
|
return Object.keys(apps).map(name => apps[name]);
|
|
}
|
|
/*
|
|
* Register a Firebase Service.
|
|
*
|
|
* firebase.INTERNAL.registerService()
|
|
*
|
|
* TODO: Implement serviceProperties.
|
|
*/
|
|
function registerService(name, createService, serviceProperties, appHook, allowMultipleInstances = false) {
|
|
// If re-registering a service that already exists, return existing service
|
|
if (factories[name]) {
|
|
logger.debug(`There were multiple attempts to register service ${name}.`);
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
return namespace[name];
|
|
}
|
|
// Capture the service factory for later service instantiation
|
|
factories[name] = createService;
|
|
// Capture the appHook, if passed
|
|
if (appHook) {
|
|
appHooks[name] = appHook;
|
|
// Run the **new** app hook on all existing apps
|
|
getApps().forEach(app => {
|
|
appHook('create', app);
|
|
});
|
|
}
|
|
// The Service namespace is an accessor function ...
|
|
function serviceNamespace(appArg = app()) {
|
|
// @ts-ignore
|
|
if (typeof appArg[name] !== 'function') {
|
|
// Invalid argument.
|
|
// This happens in the following case: firebase.storage('gs:/')
|
|
throw ERROR_FACTORY.create("invalid-app-argument" /* INVALID_APP_ARGUMENT */, {
|
|
appName: name
|
|
});
|
|
}
|
|
// Forward service instance lookup to the FirebaseApp.
|
|
// @ts-ignore
|
|
return appArg[name]();
|
|
}
|
|
// ... and a container for service-level properties.
|
|
if (serviceProperties !== undefined) {
|
|
deepExtend(serviceNamespace, serviceProperties);
|
|
}
|
|
// Monkey-patch the serviceNamespace onto the firebase namespace
|
|
// @ts-ignore
|
|
namespace[name] = serviceNamespace;
|
|
// Patch the FirebaseAppImpl prototype
|
|
// @ts-ignore
|
|
firebaseAppImpl.prototype[name] =
|
|
// 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) {
|
|
const serviceFxn = this._getService.bind(this, name);
|
|
return serviceFxn.apply(this, allowMultipleInstances ? args : []);
|
|
};
|
|
return serviceNamespace;
|
|
}
|
|
function callAppHooks(app, eventName) {
|
|
for (const serviceName of Object.keys(factories)) {
|
|
// Ignore virtual services
|
|
const factoryName = useAsService(app, serviceName);
|
|
if (factoryName === null) {
|
|
return;
|
|
}
|
|
if (appHooks[factoryName]) {
|
|
appHooks[factoryName](eventName, app);
|
|
}
|
|
}
|
|
}
|
|
// Map the requested service to a registered service name
|
|
// (used to map auth to serverAuth service when needed).
|
|
function useAsService(app, name) {
|
|
if (name === 'serverAuth') {
|
|
return null;
|
|
}
|
|
const useService = name;
|
|
return useService;
|
|
}
|
|
return namespace;
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
/**
|
|
* Return a firebase namespace object.
|
|
*
|
|
* In production, this will be called exactly once and the result
|
|
* assigned to the 'firebase' global. It may be called multiple times
|
|
* in unit tests.
|
|
*/
|
|
function createFirebaseNamespace() {
|
|
const namespace = createFirebaseNamespaceCore(FirebaseAppImpl);
|
|
namespace.INTERNAL = Object.assign({}, namespace.INTERNAL, { createFirebaseNamespace,
|
|
extendNamespace,
|
|
createSubscribe,
|
|
ErrorFactory,
|
|
deepExtend });
|
|
/**
|
|
* Patch the top-level firebase namespace with additional properties.
|
|
*
|
|
* firebase.INTERNAL.extendNamespace()
|
|
*/
|
|
function extendNamespace(props) {
|
|
deepExtend(namespace, props);
|
|
}
|
|
return namespace;
|
|
}
|
|
|
|
/**
|
|
* @license
|
|
* Copyright 2017 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.
|
|
*/
|
|
// Firebase Lite detection
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
if (isBrowser() && self.firebase !== undefined) {
|
|
logger.warn(`
|
|
Warning: Firebase is already defined in the global scope. Please make sure
|
|
Firebase library is only loaded once.
|
|
`);
|
|
// eslint-disable-next-line
|
|
const sdkVersion = self.firebase.SDK_VERSION;
|
|
if (sdkVersion && sdkVersion.indexOf('LITE') >= 0) {
|
|
logger.warn(`
|
|
Warning: You are trying to load Firebase while using Firebase Performance standalone script.
|
|
You should load Firebase Performance with this instance of Firebase to avoid loading duplicate code.
|
|
`);
|
|
}
|
|
}
|
|
const firebaseNamespace = createFirebaseNamespace();
|
|
const initializeApp = firebaseNamespace.initializeApp;
|
|
// TODO: This 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
|
|
firebaseNamespace.initializeApp = function (...args) {
|
|
// Environment check before initializing app
|
|
// Do the check in initializeApp, so people have a chance to disable it by setting logLevel
|
|
// in @firebase/logger
|
|
if (isNode()) {
|
|
logger.warn(`
|
|
Warning: This is a browser-targeted Firebase bundle but it appears it is being
|
|
run in a Node environment. If running in a Node environment, make sure you
|
|
are using the bundle specified by the "main" field in package.json.
|
|
|
|
If you are using Webpack, you can specify "main" as the first item in
|
|
"resolve.mainFields":
|
|
https://webpack.js.org/configuration/resolve/#resolvemainfields
|
|
|
|
If using Rollup, use the rollup-plugin-node-resolve plugin and specify "main"
|
|
as the first item in "mainFields", e.g. ['main', 'module'].
|
|
https://github.com/rollup/rollup-plugin-node-resolve
|
|
`);
|
|
}
|
|
return initializeApp.apply(undefined, args);
|
|
};
|
|
const firebase = firebaseNamespace;
|
|
|
|
export default firebase;
|
|
export { firebase };
|
|
//# sourceMappingURL=index.esm2017.js.map
|