No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.esm2017.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import { ErrorFactory, contains, deepExtend, createSubscribe, isBrowser } from '@firebase/util';
  2. import { Component } from '@firebase/component';
  3. import * as modularAPIs from '@firebase/app';
  4. import { _addComponent, deleteApp, _DEFAULT_ENTRY_NAME, _addOrOverwriteComponent, registerVersion } from '@firebase/app';
  5. import { Logger } from '@firebase/logger';
  6. /**
  7. * @license
  8. * Copyright 2020 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. */
  22. /**
  23. * Global context object for a collection of services using
  24. * a shared authentication state.
  25. *
  26. * marked as internal because it references internal types exported from @firebase/app
  27. * @internal
  28. */
  29. class FirebaseAppImpl {
  30. constructor(_delegate, firebase) {
  31. this._delegate = _delegate;
  32. this.firebase = firebase;
  33. // add itself to container
  34. _addComponent(_delegate, new Component('app-compat', () => this, "PUBLIC" /* ComponentType.PUBLIC */));
  35. this.container = _delegate.container;
  36. }
  37. get automaticDataCollectionEnabled() {
  38. return this._delegate.automaticDataCollectionEnabled;
  39. }
  40. set automaticDataCollectionEnabled(val) {
  41. this._delegate.automaticDataCollectionEnabled = val;
  42. }
  43. get name() {
  44. return this._delegate.name;
  45. }
  46. get options() {
  47. return this._delegate.options;
  48. }
  49. delete() {
  50. return new Promise(resolve => {
  51. this._delegate.checkDestroyed();
  52. resolve();
  53. }).then(() => {
  54. this.firebase.INTERNAL.removeApp(this.name);
  55. return deleteApp(this._delegate);
  56. });
  57. }
  58. /**
  59. * Return a service instance associated with this app (creating it
  60. * on demand), identified by the passed instanceIdentifier.
  61. *
  62. * NOTE: Currently storage and functions are the only ones that are leveraging this
  63. * functionality. They invoke it by calling:
  64. *
  65. * ```javascript
  66. * firebase.app().storage('STORAGE BUCKET ID')
  67. * ```
  68. *
  69. * The service name is passed to this already
  70. * @internal
  71. */
  72. _getService(name, instanceIdentifier = _DEFAULT_ENTRY_NAME) {
  73. var _a;
  74. this._delegate.checkDestroyed();
  75. // Initialize instance if InstatiationMode is `EXPLICIT`.
  76. const provider = this._delegate.container.getProvider(name);
  77. if (!provider.isInitialized() &&
  78. ((_a = provider.getComponent()) === null || _a === void 0 ? void 0 : _a.instantiationMode) === "EXPLICIT" /* InstantiationMode.EXPLICIT */) {
  79. provider.initialize();
  80. }
  81. // getImmediate will always succeed because _getService is only called for registered components.
  82. return provider.getImmediate({
  83. identifier: instanceIdentifier
  84. });
  85. }
  86. /**
  87. * Remove a service instance from the cache, so we will create a new instance for this service
  88. * when people try to get it again.
  89. *
  90. * NOTE: currently only firestore uses this functionality to support firestore shutdown.
  91. *
  92. * @param name The service name
  93. * @param instanceIdentifier instance identifier in case multiple instances are allowed
  94. * @internal
  95. */
  96. _removeServiceInstance(name, instanceIdentifier = _DEFAULT_ENTRY_NAME) {
  97. this._delegate.container
  98. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  99. .getProvider(name)
  100. .clearInstance(instanceIdentifier);
  101. }
  102. /**
  103. * @param component the component being added to this app's container
  104. * @internal
  105. */
  106. _addComponent(component) {
  107. _addComponent(this._delegate, component);
  108. }
  109. _addOrOverwriteComponent(component) {
  110. _addOrOverwriteComponent(this._delegate, component);
  111. }
  112. toJSON() {
  113. return {
  114. name: this.name,
  115. automaticDataCollectionEnabled: this.automaticDataCollectionEnabled,
  116. options: this.options
  117. };
  118. }
  119. }
  120. // TODO: investigate why the following needs to be commented out
  121. // Prevent dead-code elimination of these methods w/o invalid property
  122. // copying.
  123. // (FirebaseAppImpl.prototype.name && FirebaseAppImpl.prototype.options) ||
  124. // FirebaseAppImpl.prototype.delete ||
  125. // console.log('dc');
  126. /**
  127. * @license
  128. * Copyright 2019 Google LLC
  129. *
  130. * Licensed under the Apache License, Version 2.0 (the "License");
  131. * you may not use this file except in compliance with the License.
  132. * You may obtain a copy of the License at
  133. *
  134. * http://www.apache.org/licenses/LICENSE-2.0
  135. *
  136. * Unless required by applicable law or agreed to in writing, software
  137. * distributed under the License is distributed on an "AS IS" BASIS,
  138. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  139. * See the License for the specific language governing permissions and
  140. * limitations under the License.
  141. */
  142. const ERRORS = {
  143. ["no-app" /* AppError.NO_APP */]: "No Firebase App '{$appName}' has been created - " +
  144. 'call Firebase App.initializeApp()',
  145. ["invalid-app-argument" /* AppError.INVALID_APP_ARGUMENT */]: 'firebase.{$appName}() takes either no argument or a ' +
  146. 'Firebase App instance.'
  147. };
  148. const ERROR_FACTORY = new ErrorFactory('app-compat', 'Firebase', ERRORS);
  149. /**
  150. * @license
  151. * Copyright 2019 Google LLC
  152. *
  153. * Licensed under the Apache License, Version 2.0 (the "License");
  154. * you may not use this file except in compliance with the License.
  155. * You may obtain a copy of the License at
  156. *
  157. * http://www.apache.org/licenses/LICENSE-2.0
  158. *
  159. * Unless required by applicable law or agreed to in writing, software
  160. * distributed under the License is distributed on an "AS IS" BASIS,
  161. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  162. * See the License for the specific language governing permissions and
  163. * limitations under the License.
  164. */
  165. /**
  166. * Because auth can't share code with other components, we attach the utility functions
  167. * in an internal namespace to share code.
  168. * This function return a firebase namespace object without
  169. * any utility functions, so it can be shared between the regular firebaseNamespace and
  170. * the lite version.
  171. */
  172. function createFirebaseNamespaceCore(firebaseAppImpl) {
  173. const apps = {};
  174. // // eslint-disable-next-line @typescript-eslint/no-explicit-any
  175. // const components = new Map<string, Component<any>>();
  176. // A namespace is a plain JavaScript Object.
  177. const namespace = {
  178. // Hack to prevent Babel from modifying the object returned
  179. // as the firebase namespace.
  180. // @ts-ignore
  181. __esModule: true,
  182. initializeApp: initializeAppCompat,
  183. // @ts-ignore
  184. app,
  185. registerVersion: modularAPIs.registerVersion,
  186. setLogLevel: modularAPIs.setLogLevel,
  187. onLog: modularAPIs.onLog,
  188. // @ts-ignore
  189. apps: null,
  190. SDK_VERSION: modularAPIs.SDK_VERSION,
  191. INTERNAL: {
  192. registerComponent: registerComponentCompat,
  193. removeApp,
  194. useAsService,
  195. modularAPIs
  196. }
  197. };
  198. // Inject a circular default export to allow Babel users who were previously
  199. // using:
  200. //
  201. // import firebase from 'firebase';
  202. // which becomes: var firebase = require('firebase').default;
  203. //
  204. // instead of
  205. //
  206. // import * as firebase from 'firebase';
  207. // which becomes: var firebase = require('firebase');
  208. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  209. namespace['default'] = namespace;
  210. // firebase.apps is a read-only getter.
  211. Object.defineProperty(namespace, 'apps', {
  212. get: getApps
  213. });
  214. /**
  215. * Called by App.delete() - but before any services associated with the App
  216. * are deleted.
  217. */
  218. function removeApp(name) {
  219. delete apps[name];
  220. }
  221. /**
  222. * Get the App object for a given name (or DEFAULT).
  223. */
  224. function app(name) {
  225. name = name || modularAPIs._DEFAULT_ENTRY_NAME;
  226. if (!contains(apps, name)) {
  227. throw ERROR_FACTORY.create("no-app" /* AppError.NO_APP */, { appName: name });
  228. }
  229. return apps[name];
  230. }
  231. // @ts-ignore
  232. app['App'] = firebaseAppImpl;
  233. /**
  234. * Create a new App instance (name must be unique).
  235. *
  236. * This function is idempotent. It can be called more than once and return the same instance using the same options and config.
  237. */
  238. function initializeAppCompat(options, rawConfig = {}) {
  239. const app = modularAPIs.initializeApp(options, rawConfig);
  240. if (contains(apps, app.name)) {
  241. return apps[app.name];
  242. }
  243. const appCompat = new firebaseAppImpl(app, namespace);
  244. apps[app.name] = appCompat;
  245. return appCompat;
  246. }
  247. /*
  248. * Return an array of all the non-deleted FirebaseApps.
  249. */
  250. function getApps() {
  251. // Make a copy so caller cannot mutate the apps list.
  252. return Object.keys(apps).map(name => apps[name]);
  253. }
  254. function registerComponentCompat(component) {
  255. const componentName = component.name;
  256. const componentNameWithoutCompat = componentName.replace('-compat', '');
  257. if (modularAPIs._registerComponent(component) &&
  258. component.type === "PUBLIC" /* ComponentType.PUBLIC */) {
  259. // create service namespace for public components
  260. // The Service namespace is an accessor function ...
  261. const serviceNamespace = (appArg = app()) => {
  262. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  263. if (typeof appArg[componentNameWithoutCompat] !== 'function') {
  264. // Invalid argument.
  265. // This happens in the following case: firebase.storage('gs:/')
  266. throw ERROR_FACTORY.create("invalid-app-argument" /* AppError.INVALID_APP_ARGUMENT */, {
  267. appName: componentName
  268. });
  269. }
  270. // Forward service instance lookup to the FirebaseApp.
  271. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  272. return appArg[componentNameWithoutCompat]();
  273. };
  274. // ... and a container for service-level properties.
  275. if (component.serviceProps !== undefined) {
  276. deepExtend(serviceNamespace, component.serviceProps);
  277. }
  278. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  279. namespace[componentNameWithoutCompat] = serviceNamespace;
  280. // Patch the FirebaseAppImpl prototype
  281. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  282. firebaseAppImpl.prototype[componentNameWithoutCompat] =
  283. // TODO: The eslint disable can be removed and the 'ignoreRestArgs'
  284. // option added to the no-explicit-any rule when ESlint releases it.
  285. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  286. function (...args) {
  287. const serviceFxn = this._getService.bind(this, componentName);
  288. return serviceFxn.apply(this, component.multipleInstances ? args : []);
  289. };
  290. }
  291. return component.type === "PUBLIC" /* ComponentType.PUBLIC */
  292. ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
  293. namespace[componentNameWithoutCompat]
  294. : null;
  295. }
  296. // Map the requested service to a registered service name
  297. // (used to map auth to serverAuth service when needed).
  298. function useAsService(app, name) {
  299. if (name === 'serverAuth') {
  300. return null;
  301. }
  302. const useService = name;
  303. return useService;
  304. }
  305. return namespace;
  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. */
  323. /**
  324. * Return a firebase namespace object.
  325. *
  326. * In production, this will be called exactly once and the result
  327. * assigned to the 'firebase' global. It may be called multiple times
  328. * in unit tests.
  329. */
  330. function createFirebaseNamespace() {
  331. const namespace = createFirebaseNamespaceCore(FirebaseAppImpl);
  332. namespace.INTERNAL = Object.assign(Object.assign({}, namespace.INTERNAL), { createFirebaseNamespace,
  333. extendNamespace,
  334. createSubscribe,
  335. ErrorFactory,
  336. deepExtend });
  337. /**
  338. * Patch the top-level firebase namespace with additional properties.
  339. *
  340. * firebase.INTERNAL.extendNamespace()
  341. */
  342. function extendNamespace(props) {
  343. deepExtend(namespace, props);
  344. }
  345. return namespace;
  346. }
  347. const firebase$1 = createFirebaseNamespace();
  348. /**
  349. * @license
  350. * Copyright 2019 Google LLC
  351. *
  352. * Licensed under the Apache License, Version 2.0 (the "License");
  353. * you may not use this file except in compliance with the License.
  354. * You may obtain a copy of the License at
  355. *
  356. * http://www.apache.org/licenses/LICENSE-2.0
  357. *
  358. * Unless required by applicable law or agreed to in writing, software
  359. * distributed under the License is distributed on an "AS IS" BASIS,
  360. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  361. * See the License for the specific language governing permissions and
  362. * limitations under the License.
  363. */
  364. const logger = new Logger('@firebase/app-compat');
  365. const name = "@firebase/app-compat";
  366. const version = "0.2.1";
  367. /**
  368. * @license
  369. * Copyright 2019 Google LLC
  370. *
  371. * Licensed under the Apache License, Version 2.0 (the "License");
  372. * you may not use this file except in compliance with the License.
  373. * You may obtain a copy of the License at
  374. *
  375. * http://www.apache.org/licenses/LICENSE-2.0
  376. *
  377. * Unless required by applicable law or agreed to in writing, software
  378. * distributed under the License is distributed on an "AS IS" BASIS,
  379. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  380. * See the License for the specific language governing permissions and
  381. * limitations under the License.
  382. */
  383. function registerCoreComponents(variant) {
  384. // Register `app` package.
  385. registerVersion(name, version, variant);
  386. }
  387. /**
  388. * @license
  389. * Copyright 2020 Google LLC
  390. *
  391. * Licensed under the Apache License, Version 2.0 (the "License");
  392. * you may not use this file except in compliance with the License.
  393. * You may obtain a copy of the License at
  394. *
  395. * http://www.apache.org/licenses/LICENSE-2.0
  396. *
  397. * Unless required by applicable law or agreed to in writing, software
  398. * distributed under the License is distributed on an "AS IS" BASIS,
  399. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  400. * See the License for the specific language governing permissions and
  401. * limitations under the License.
  402. */
  403. // Firebase Lite detection
  404. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  405. if (isBrowser() && self.firebase !== undefined) {
  406. logger.warn(`
  407. Warning: Firebase is already defined in the global scope. Please make sure
  408. Firebase library is only loaded once.
  409. `);
  410. // eslint-disable-next-line
  411. const sdkVersion = self.firebase.SDK_VERSION;
  412. if (sdkVersion && sdkVersion.indexOf('LITE') >= 0) {
  413. logger.warn(`
  414. Warning: You are trying to load Firebase while using Firebase Performance standalone script.
  415. You should load Firebase Performance with this instance of Firebase to avoid loading duplicate code.
  416. `);
  417. }
  418. }
  419. const firebase = firebase$1;
  420. registerCoreComponents();
  421. export { firebase as default };
  422. //# sourceMappingURL=index.esm2017.js.map