datastake-daf 0.6.720 → 0.6.722

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/components/index.js +458 -437
  2. package/dist/context/index.js +7 -1
  3. package/dist/hooks/index.js +4660 -20
  4. package/dist/layouts/index.js +455 -432
  5. package/dist/pages/index.js +2014 -452
  6. package/dist/services/index.js +7 -1
  7. package/dist/utils/index.js +455 -432
  8. package/package.json +1 -1
  9. package/src/@daf/core/components/EditForm/form.jsx +3 -6
  10. package/src/@daf/core/components/EditForm/storyConfig2.js +23477 -10904
  11. package/src/@daf/core/components/Icon/configs/index.js +4 -0
  12. package/src/@daf/core/components/Icon/configs/partnerIcon.js +8 -0
  13. package/src/@daf/core/components/Icon/configs/userIcon.js +8 -0
  14. package/src/@daf/core/components/Screens/Admin/AdminViews/components/Users/helper.js +1 -1
  15. package/src/@daf/hooks/useWidgetFetch.js +1 -0
  16. package/src/@daf/pages/dashboards/SupplyChain/components/ChartsContainer/components/Identification/hook.js +1 -1
  17. package/src/@daf/pages/dashboards/UserDashboard/components/AccumulationGraph/hook.js +139 -0
  18. package/src/@daf/pages/dashboards/UserDashboard/components/AccumulationGraph/index.jsx +78 -0
  19. package/src/@daf/pages/dashboards/UserDashboard/components/ContributionsGraph/helper.js +75 -0
  20. package/src/@daf/pages/dashboards/UserDashboard/components/ContributionsGraph/hook.js +46 -0
  21. package/src/@daf/pages/dashboards/UserDashboard/components/ContributionsGraph/index.jsx +49 -0
  22. package/src/@daf/pages/dashboards/UserDashboard/components/CustomSegment/index.jsx +26 -0
  23. package/src/@daf/pages/dashboards/UserDashboard/components/DataChainOfCustody/index.jsx +18 -0
  24. package/src/@daf/pages/dashboards/UserDashboard/components/DataCompilation/index.jsx +69 -0
  25. package/src/@daf/pages/dashboards/UserDashboard/components/DataConsilidation/index.jsx +72 -0
  26. package/src/@daf/pages/dashboards/UserDashboard/components/KeyIndicators/config.js +120 -0
  27. package/src/@daf/pages/dashboards/UserDashboard/components/KeyIndicators/index.jsx +52 -0
  28. package/src/@daf/pages/dashboards/UserDashboard/components/MineSites/config.js +22 -0
  29. package/src/@daf/pages/dashboards/UserDashboard/components/MineSites/helper.js +142 -0
  30. package/src/@daf/pages/dashboards/UserDashboard/components/MineSites/index.jsx +209 -0
  31. package/src/@daf/pages/dashboards/UserDashboard/components/Triangulation/config.js +20 -0
  32. package/src/@daf/pages/dashboards/UserDashboard/components/Triangulation/hook.js +87 -0
  33. package/src/@daf/pages/dashboards/UserDashboard/components/Triangulation/index.jsx +68 -0
  34. package/src/@daf/pages/dashboards/UserDashboard/config.js +24 -24
  35. package/src/@daf/pages/dashboards/UserDashboard/index.jsx +56 -0
  36. package/src/@daf/services/AuthenticationService.js +7 -1
  37. package/src/pages.js +2 -1
  38. package/dist/style/datastake/mapbox-gl.css +0 -330
@@ -13,8 +13,6 @@ var lodash = require('lodash');
13
13
  var PropTypes = require('prop-types');
14
14
  var reactIs = require('react-is');
15
15
  require('react-dom');
16
- var app = require('firebase/app');
17
- var messaging = require('firebase/messaging');
18
16
 
19
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
20
18
 
@@ -1213,7 +1211,7 @@ const isProxy = () => {
1213
1211
  }
1214
1212
  return false;
1215
1213
  };
1216
- const getToken = () => isProxy() ? StorageManager.get('proxy_token') : StorageManager.get('token');
1214
+ const getToken$3 = () => isProxy() ? StorageManager.get('proxy_token') : StorageManager.get('token');
1217
1215
 
1218
1216
  /**
1219
1217
  * Custom params serializer to maintain JSON format for nested objects
@@ -1254,7 +1252,7 @@ class BaseHTTPService {
1254
1252
  timeout = 300000,
1255
1253
  customAxiosConfig = {}
1256
1254
  } = config;
1257
- this.getToken = getToken || (() => null);
1255
+ this.getToken = getToken$3 || (() => null);
1258
1256
  this.getHeaders = getHeaders || (() => ({}));
1259
1257
  this.getBaseURL = getBaseURL || (() => null);
1260
1258
  this.onError = onError || (() => null);
@@ -1592,7 +1590,7 @@ class BaseService extends BaseHTTPService {
1592
1590
 
1593
1591
  // Call super with lazy getters that fetch config at runtime
1594
1592
  super({
1595
- getToken: () => getToken(),
1593
+ getToken: () => getToken$3(),
1596
1594
  getHeaders: () => {
1597
1595
  const config = getServicesConfig();
1598
1596
  return {
@@ -1986,7 +1984,7 @@ var platform = 'browser';
1986
1984
  var browser = true;
1987
1985
  var env = {};
1988
1986
  var argv = [];
1989
- var version = ''; // empty string to avoid regexp issues
1987
+ var version$3 = ''; // empty string to avoid regexp issues
1990
1988
  var versions = {};
1991
1989
  var release = {};
1992
1990
  var config = {};
@@ -2050,7 +2048,7 @@ var browser$1 = {
2050
2048
  browser: browser,
2051
2049
  env: env,
2052
2050
  argv: argv,
2053
- version: version,
2051
+ version: version$3,
2054
2052
  versions: versions,
2055
2053
  on: on,
2056
2054
  addListener: addListener,
@@ -2141,21 +2139,4662 @@ const usePermissions = ({
2141
2139
  return value;
2142
2140
  };
2143
2141
 
2142
+ const getDefaultsFromPostinstall = () => (undefined);
2143
+
2144
+ /**
2145
+ * @license
2146
+ * Copyright 2017 Google LLC
2147
+ *
2148
+ * Licensed under the Apache License, Version 2.0 (the "License");
2149
+ * you may not use this file except in compliance with the License.
2150
+ * You may obtain a copy of the License at
2151
+ *
2152
+ * http://www.apache.org/licenses/LICENSE-2.0
2153
+ *
2154
+ * Unless required by applicable law or agreed to in writing, software
2155
+ * distributed under the License is distributed on an "AS IS" BASIS,
2156
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2157
+ * See the License for the specific language governing permissions and
2158
+ * limitations under the License.
2159
+ */
2160
+ const stringToByteArray$1 = function (str) {
2161
+ // TODO(user): Use native implementations if/when available
2162
+ const out = [];
2163
+ let p = 0;
2164
+ for (let i = 0; i < str.length; i++) {
2165
+ let c = str.charCodeAt(i);
2166
+ if (c < 128) {
2167
+ out[p++] = c;
2168
+ }
2169
+ else if (c < 2048) {
2170
+ out[p++] = (c >> 6) | 192;
2171
+ out[p++] = (c & 63) | 128;
2172
+ }
2173
+ else if ((c & 0xfc00) === 0xd800 &&
2174
+ i + 1 < str.length &&
2175
+ (str.charCodeAt(i + 1) & 0xfc00) === 0xdc00) {
2176
+ // Surrogate Pair
2177
+ c = 0x10000 + ((c & 0x03ff) << 10) + (str.charCodeAt(++i) & 0x03ff);
2178
+ out[p++] = (c >> 18) | 240;
2179
+ out[p++] = ((c >> 12) & 63) | 128;
2180
+ out[p++] = ((c >> 6) & 63) | 128;
2181
+ out[p++] = (c & 63) | 128;
2182
+ }
2183
+ else {
2184
+ out[p++] = (c >> 12) | 224;
2185
+ out[p++] = ((c >> 6) & 63) | 128;
2186
+ out[p++] = (c & 63) | 128;
2187
+ }
2188
+ }
2189
+ return out;
2190
+ };
2191
+ /**
2192
+ * Turns an array of numbers into the string given by the concatenation of the
2193
+ * characters to which the numbers correspond.
2194
+ * @param bytes Array of numbers representing characters.
2195
+ * @return Stringification of the array.
2196
+ */
2197
+ const byteArrayToString = function (bytes) {
2198
+ // TODO(user): Use native implementations if/when available
2199
+ const out = [];
2200
+ let pos = 0, c = 0;
2201
+ while (pos < bytes.length) {
2202
+ const c1 = bytes[pos++];
2203
+ if (c1 < 128) {
2204
+ out[c++] = String.fromCharCode(c1);
2205
+ }
2206
+ else if (c1 > 191 && c1 < 224) {
2207
+ const c2 = bytes[pos++];
2208
+ out[c++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
2209
+ }
2210
+ else if (c1 > 239 && c1 < 365) {
2211
+ // Surrogate Pair
2212
+ const c2 = bytes[pos++];
2213
+ const c3 = bytes[pos++];
2214
+ const c4 = bytes[pos++];
2215
+ const u = (((c1 & 7) << 18) | ((c2 & 63) << 12) | ((c3 & 63) << 6) | (c4 & 63)) -
2216
+ 0x10000;
2217
+ out[c++] = String.fromCharCode(0xd800 + (u >> 10));
2218
+ out[c++] = String.fromCharCode(0xdc00 + (u & 1023));
2219
+ }
2220
+ else {
2221
+ const c2 = bytes[pos++];
2222
+ const c3 = bytes[pos++];
2223
+ out[c++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
2224
+ }
2225
+ }
2226
+ return out.join('');
2227
+ };
2228
+ // We define it as an object literal instead of a class because a class compiled down to es5 can't
2229
+ // be treeshaked. https://github.com/rollup/rollup/issues/1691
2230
+ // Static lookup maps, lazily populated by init_()
2231
+ // TODO(dlarocque): Define this as a class, since we no longer target ES5.
2232
+ const base64 = {
2233
+ /**
2234
+ * Maps bytes to characters.
2235
+ */
2236
+ byteToCharMap_: null,
2237
+ /**
2238
+ * Maps characters to bytes.
2239
+ */
2240
+ charToByteMap_: null,
2241
+ /**
2242
+ * Maps bytes to websafe characters.
2243
+ * @private
2244
+ */
2245
+ byteToCharMapWebSafe_: null,
2246
+ /**
2247
+ * Maps websafe characters to bytes.
2248
+ * @private
2249
+ */
2250
+ charToByteMapWebSafe_: null,
2251
+ /**
2252
+ * Our default alphabet, shared between
2253
+ * ENCODED_VALS and ENCODED_VALS_WEBSAFE
2254
+ */
2255
+ ENCODED_VALS_BASE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789',
2256
+ /**
2257
+ * Our default alphabet. Value 64 (=) is special; it means "nothing."
2258
+ */
2259
+ get ENCODED_VALS() {
2260
+ return this.ENCODED_VALS_BASE + '+/=';
2261
+ },
2262
+ /**
2263
+ * Our websafe alphabet.
2264
+ */
2265
+ get ENCODED_VALS_WEBSAFE() {
2266
+ return this.ENCODED_VALS_BASE + '-_.';
2267
+ },
2268
+ /**
2269
+ * Whether this browser supports the atob and btoa functions. This extension
2270
+ * started at Mozilla but is now implemented by many browsers. We use the
2271
+ * ASSUME_* variables to avoid pulling in the full useragent detection library
2272
+ * but still allowing the standard per-browser compilations.
2273
+ *
2274
+ */
2275
+ HAS_NATIVE_SUPPORT: typeof atob === 'function',
2276
+ /**
2277
+ * Base64-encode an array of bytes.
2278
+ *
2279
+ * @param input An array of bytes (numbers with
2280
+ * value in [0, 255]) to encode.
2281
+ * @param webSafe Boolean indicating we should use the
2282
+ * alternative alphabet.
2283
+ * @return The base64 encoded string.
2284
+ */
2285
+ encodeByteArray(input, webSafe) {
2286
+ if (!Array.isArray(input)) {
2287
+ throw Error('encodeByteArray takes an array as a parameter');
2288
+ }
2289
+ this.init_();
2290
+ const byteToCharMap = webSafe
2291
+ ? this.byteToCharMapWebSafe_
2292
+ : this.byteToCharMap_;
2293
+ const output = [];
2294
+ for (let i = 0; i < input.length; i += 3) {
2295
+ const byte1 = input[i];
2296
+ const haveByte2 = i + 1 < input.length;
2297
+ const byte2 = haveByte2 ? input[i + 1] : 0;
2298
+ const haveByte3 = i + 2 < input.length;
2299
+ const byte3 = haveByte3 ? input[i + 2] : 0;
2300
+ const outByte1 = byte1 >> 2;
2301
+ const outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4);
2302
+ let outByte3 = ((byte2 & 0x0f) << 2) | (byte3 >> 6);
2303
+ let outByte4 = byte3 & 0x3f;
2304
+ if (!haveByte3) {
2305
+ outByte4 = 64;
2306
+ if (!haveByte2) {
2307
+ outByte3 = 64;
2308
+ }
2309
+ }
2310
+ output.push(byteToCharMap[outByte1], byteToCharMap[outByte2], byteToCharMap[outByte3], byteToCharMap[outByte4]);
2311
+ }
2312
+ return output.join('');
2313
+ },
2314
+ /**
2315
+ * Base64-encode a string.
2316
+ *
2317
+ * @param input A string to encode.
2318
+ * @param webSafe If true, we should use the
2319
+ * alternative alphabet.
2320
+ * @return The base64 encoded string.
2321
+ */
2322
+ encodeString(input, webSafe) {
2323
+ // Shortcut for Mozilla browsers that implement
2324
+ // a native base64 encoder in the form of "btoa/atob"
2325
+ if (this.HAS_NATIVE_SUPPORT && !webSafe) {
2326
+ return btoa(input);
2327
+ }
2328
+ return this.encodeByteArray(stringToByteArray$1(input), webSafe);
2329
+ },
2330
+ /**
2331
+ * Base64-decode a string.
2332
+ *
2333
+ * @param input to decode.
2334
+ * @param webSafe True if we should use the
2335
+ * alternative alphabet.
2336
+ * @return string representing the decoded value.
2337
+ */
2338
+ decodeString(input, webSafe) {
2339
+ // Shortcut for Mozilla browsers that implement
2340
+ // a native base64 encoder in the form of "btoa/atob"
2341
+ if (this.HAS_NATIVE_SUPPORT && !webSafe) {
2342
+ return atob(input);
2343
+ }
2344
+ return byteArrayToString(this.decodeStringToByteArray(input, webSafe));
2345
+ },
2346
+ /**
2347
+ * Base64-decode a string.
2348
+ *
2349
+ * In base-64 decoding, groups of four characters are converted into three
2350
+ * bytes. If the encoder did not apply padding, the input length may not
2351
+ * be a multiple of 4.
2352
+ *
2353
+ * In this case, the last group will have fewer than 4 characters, and
2354
+ * padding will be inferred. If the group has one or two characters, it decodes
2355
+ * to one byte. If the group has three characters, it decodes to two bytes.
2356
+ *
2357
+ * @param input Input to decode.
2358
+ * @param webSafe True if we should use the web-safe alphabet.
2359
+ * @return bytes representing the decoded value.
2360
+ */
2361
+ decodeStringToByteArray(input, webSafe) {
2362
+ this.init_();
2363
+ const charToByteMap = webSafe
2364
+ ? this.charToByteMapWebSafe_
2365
+ : this.charToByteMap_;
2366
+ const output = [];
2367
+ for (let i = 0; i < input.length;) {
2368
+ const byte1 = charToByteMap[input.charAt(i++)];
2369
+ const haveByte2 = i < input.length;
2370
+ const byte2 = haveByte2 ? charToByteMap[input.charAt(i)] : 0;
2371
+ ++i;
2372
+ const haveByte3 = i < input.length;
2373
+ const byte3 = haveByte3 ? charToByteMap[input.charAt(i)] : 64;
2374
+ ++i;
2375
+ const haveByte4 = i < input.length;
2376
+ const byte4 = haveByte4 ? charToByteMap[input.charAt(i)] : 64;
2377
+ ++i;
2378
+ if (byte1 == null || byte2 == null || byte3 == null || byte4 == null) {
2379
+ throw new DecodeBase64StringError();
2380
+ }
2381
+ const outByte1 = (byte1 << 2) | (byte2 >> 4);
2382
+ output.push(outByte1);
2383
+ if (byte3 !== 64) {
2384
+ const outByte2 = ((byte2 << 4) & 0xf0) | (byte3 >> 2);
2385
+ output.push(outByte2);
2386
+ if (byte4 !== 64) {
2387
+ const outByte3 = ((byte3 << 6) & 0xc0) | byte4;
2388
+ output.push(outByte3);
2389
+ }
2390
+ }
2391
+ }
2392
+ return output;
2393
+ },
2394
+ /**
2395
+ * Lazy static initialization function. Called before
2396
+ * accessing any of the static map variables.
2397
+ * @private
2398
+ */
2399
+ init_() {
2400
+ if (!this.byteToCharMap_) {
2401
+ this.byteToCharMap_ = {};
2402
+ this.charToByteMap_ = {};
2403
+ this.byteToCharMapWebSafe_ = {};
2404
+ this.charToByteMapWebSafe_ = {};
2405
+ // We want quick mappings back and forth, so we precompute two maps.
2406
+ for (let i = 0; i < this.ENCODED_VALS.length; i++) {
2407
+ this.byteToCharMap_[i] = this.ENCODED_VALS.charAt(i);
2408
+ this.charToByteMap_[this.byteToCharMap_[i]] = i;
2409
+ this.byteToCharMapWebSafe_[i] = this.ENCODED_VALS_WEBSAFE.charAt(i);
2410
+ this.charToByteMapWebSafe_[this.byteToCharMapWebSafe_[i]] = i;
2411
+ // Be forgiving when decoding and correctly decode both encodings.
2412
+ if (i >= this.ENCODED_VALS_BASE.length) {
2413
+ this.charToByteMap_[this.ENCODED_VALS_WEBSAFE.charAt(i)] = i;
2414
+ this.charToByteMapWebSafe_[this.ENCODED_VALS.charAt(i)] = i;
2415
+ }
2416
+ }
2417
+ }
2418
+ }
2419
+ };
2420
+ /**
2421
+ * An error encountered while decoding base64 string.
2422
+ */
2423
+ class DecodeBase64StringError extends Error {
2424
+ constructor() {
2425
+ super(...arguments);
2426
+ this.name = 'DecodeBase64StringError';
2427
+ }
2428
+ }
2429
+ /**
2430
+ * URL-safe base64 encoding
2431
+ */
2432
+ const base64Encode = function (str) {
2433
+ const utf8Bytes = stringToByteArray$1(str);
2434
+ return base64.encodeByteArray(utf8Bytes, true);
2435
+ };
2436
+ /**
2437
+ * URL-safe base64 encoding (without "." padding in the end).
2438
+ * e.g. Used in JSON Web Token (JWT) parts.
2439
+ */
2440
+ const base64urlEncodeWithoutPadding = function (str) {
2441
+ // Use base64url encoding and remove padding in the end (dot characters).
2442
+ return base64Encode(str).replace(/\./g, '');
2443
+ };
2444
+ /**
2445
+ * URL-safe base64 decoding
2446
+ *
2447
+ * NOTE: DO NOT use the global atob() function - it does NOT support the
2448
+ * base64Url variant encoding.
2449
+ *
2450
+ * @param str To be decoded
2451
+ * @return Decoded result, if possible
2452
+ */
2453
+ const base64Decode = function (str) {
2454
+ try {
2455
+ return base64.decodeString(str, true);
2456
+ }
2457
+ catch (e) {
2458
+ console.error('base64Decode failed: ', e);
2459
+ }
2460
+ return null;
2461
+ };
2462
+
2463
+ /**
2464
+ * @license
2465
+ * Copyright 2022 Google LLC
2466
+ *
2467
+ * Licensed under the Apache License, Version 2.0 (the "License");
2468
+ * you may not use this file except in compliance with the License.
2469
+ * You may obtain a copy of the License at
2470
+ *
2471
+ * http://www.apache.org/licenses/LICENSE-2.0
2472
+ *
2473
+ * Unless required by applicable law or agreed to in writing, software
2474
+ * distributed under the License is distributed on an "AS IS" BASIS,
2475
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2476
+ * See the License for the specific language governing permissions and
2477
+ * limitations under the License.
2478
+ */
2479
+ /**
2480
+ * Polyfill for `globalThis` object.
2481
+ * @returns the `globalThis` object for the given environment.
2482
+ * @public
2483
+ */
2484
+ function getGlobal() {
2485
+ if (typeof self !== 'undefined') {
2486
+ return self;
2487
+ }
2488
+ if (typeof window !== 'undefined') {
2489
+ return window;
2490
+ }
2491
+ if (typeof global$1 !== 'undefined') {
2492
+ return global$1;
2493
+ }
2494
+ throw new Error('Unable to locate global object.');
2495
+ }
2496
+
2497
+ /**
2498
+ * @license
2499
+ * Copyright 2022 Google LLC
2500
+ *
2501
+ * Licensed under the Apache License, Version 2.0 (the "License");
2502
+ * you may not use this file except in compliance with the License.
2503
+ * You may obtain a copy of the License at
2504
+ *
2505
+ * http://www.apache.org/licenses/LICENSE-2.0
2506
+ *
2507
+ * Unless required by applicable law or agreed to in writing, software
2508
+ * distributed under the License is distributed on an "AS IS" BASIS,
2509
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2510
+ * See the License for the specific language governing permissions and
2511
+ * limitations under the License.
2512
+ */
2513
+ const getDefaultsFromGlobal = () => getGlobal().__FIREBASE_DEFAULTS__;
2514
+ /**
2515
+ * Attempt to read defaults from a JSON string provided to
2516
+ * process(.)env(.)__FIREBASE_DEFAULTS__ or a JSON file whose path is in
2517
+ * process(.)env(.)__FIREBASE_DEFAULTS_PATH__
2518
+ * The dots are in parens because certain compilers (Vite?) cannot
2519
+ * handle seeing that variable in comments.
2520
+ * See https://github.com/firebase/firebase-js-sdk/issues/6838
2521
+ */
2522
+ const getDefaultsFromEnvVariable = () => {
2523
+ if (typeof process === 'undefined' || typeof process.env === 'undefined') {
2524
+ return;
2525
+ }
2526
+ const defaultsJsonString = process.env.__FIREBASE_DEFAULTS__;
2527
+ if (defaultsJsonString) {
2528
+ return JSON.parse(defaultsJsonString);
2529
+ }
2530
+ };
2531
+ const getDefaultsFromCookie = () => {
2532
+ if (typeof document === 'undefined') {
2533
+ return;
2534
+ }
2535
+ let match;
2536
+ try {
2537
+ match = document.cookie.match(/__FIREBASE_DEFAULTS__=([^;]+)/);
2538
+ }
2539
+ catch (e) {
2540
+ // Some environments such as Angular Universal SSR have a
2541
+ // `document` object but error on accessing `document.cookie`.
2542
+ return;
2543
+ }
2544
+ const decoded = match && base64Decode(match[1]);
2545
+ return decoded && JSON.parse(decoded);
2546
+ };
2547
+ /**
2548
+ * Get the __FIREBASE_DEFAULTS__ object. It checks in order:
2549
+ * (1) if such an object exists as a property of `globalThis`
2550
+ * (2) if such an object was provided on a shell environment variable
2551
+ * (3) if such an object exists in a cookie
2552
+ * @public
2553
+ */
2554
+ const getDefaults = () => {
2555
+ try {
2556
+ return (getDefaultsFromPostinstall() ||
2557
+ getDefaultsFromGlobal() ||
2558
+ getDefaultsFromEnvVariable() ||
2559
+ getDefaultsFromCookie());
2560
+ }
2561
+ catch (e) {
2562
+ /**
2563
+ * Catch-all for being unable to get __FIREBASE_DEFAULTS__ due
2564
+ * to any environment case we have not accounted for. Log to
2565
+ * info instead of swallowing so we can find these unknown cases
2566
+ * and add paths for them if needed.
2567
+ */
2568
+ console.info(`Unable to get __FIREBASE_DEFAULTS__ due to: ${e}`);
2569
+ return;
2570
+ }
2571
+ };
2572
+ /**
2573
+ * Returns Firebase app config stored in the __FIREBASE_DEFAULTS__ object.
2574
+ * @public
2575
+ */
2576
+ const getDefaultAppConfig = () => getDefaults()?.config;
2577
+
2578
+ /**
2579
+ * @license
2580
+ * Copyright 2017 Google LLC
2581
+ *
2582
+ * Licensed under the Apache License, Version 2.0 (the "License");
2583
+ * you may not use this file except in compliance with the License.
2584
+ * You may obtain a copy of the License at
2585
+ *
2586
+ * http://www.apache.org/licenses/LICENSE-2.0
2587
+ *
2588
+ * Unless required by applicable law or agreed to in writing, software
2589
+ * distributed under the License is distributed on an "AS IS" BASIS,
2590
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2591
+ * See the License for the specific language governing permissions and
2592
+ * limitations under the License.
2593
+ */
2594
+ class Deferred {
2595
+ constructor() {
2596
+ this.reject = () => { };
2597
+ this.resolve = () => { };
2598
+ this.promise = new Promise((resolve, reject) => {
2599
+ this.resolve = resolve;
2600
+ this.reject = reject;
2601
+ });
2602
+ }
2603
+ /**
2604
+ * Our API internals are not promisified and cannot because our callback APIs have subtle expectations around
2605
+ * invoking promises inline, which Promises are forbidden to do. This method accepts an optional node-style callback
2606
+ * and returns a node-style callback which will resolve or reject the Deferred's promise.
2607
+ */
2608
+ wrapCallback(callback) {
2609
+ return (error, value) => {
2610
+ if (error) {
2611
+ this.reject(error);
2612
+ }
2613
+ else {
2614
+ this.resolve(value);
2615
+ }
2616
+ if (typeof callback === 'function') {
2617
+ // Attaching noop handler just in case developer wasn't expecting
2618
+ // promises
2619
+ this.promise.catch(() => { });
2620
+ // Some of our callbacks don't expect a value and our own tests
2621
+ // assert that the parameter length is 1
2622
+ if (callback.length === 1) {
2623
+ callback(error);
2624
+ }
2625
+ else {
2626
+ callback(error, value);
2627
+ }
2628
+ }
2629
+ };
2630
+ }
2631
+ }
2632
+ /**
2633
+ * This method checks if indexedDB is supported by current browser/service worker context
2634
+ * @return true if indexedDB is supported by current browser/service worker context
2635
+ */
2636
+ function isIndexedDBAvailable() {
2637
+ try {
2638
+ return typeof indexedDB === 'object';
2639
+ }
2640
+ catch (e) {
2641
+ return false;
2642
+ }
2643
+ }
2644
+ /**
2645
+ * This method validates browser/sw context for indexedDB by opening a dummy indexedDB database and reject
2646
+ * if errors occur during the database open operation.
2647
+ *
2648
+ * @throws exception if current browser/sw context can't run idb.open (ex: Safari iframe, Firefox
2649
+ * private browsing)
2650
+ */
2651
+ function validateIndexedDBOpenable() {
2652
+ return new Promise((resolve, reject) => {
2653
+ try {
2654
+ let preExist = true;
2655
+ const DB_CHECK_NAME = 'validate-browser-context-for-indexeddb-analytics-module';
2656
+ const request = self.indexedDB.open(DB_CHECK_NAME);
2657
+ request.onsuccess = () => {
2658
+ request.result.close();
2659
+ // delete database only when it doesn't pre-exist
2660
+ if (!preExist) {
2661
+ self.indexedDB.deleteDatabase(DB_CHECK_NAME);
2662
+ }
2663
+ resolve(true);
2664
+ };
2665
+ request.onupgradeneeded = () => {
2666
+ preExist = false;
2667
+ };
2668
+ request.onerror = () => {
2669
+ reject(request.error?.message || '');
2670
+ };
2671
+ }
2672
+ catch (error) {
2673
+ reject(error);
2674
+ }
2675
+ });
2676
+ }
2677
+ /**
2678
+ *
2679
+ * This method checks whether cookie is enabled within current browser
2680
+ * @return true if cookie is enabled within current browser
2681
+ */
2682
+ function areCookiesEnabled() {
2683
+ if (typeof navigator === 'undefined' || !navigator.cookieEnabled) {
2684
+ return false;
2685
+ }
2686
+ return true;
2687
+ }
2688
+
2689
+ /**
2690
+ * @license
2691
+ * Copyright 2017 Google LLC
2692
+ *
2693
+ * Licensed under the Apache License, Version 2.0 (the "License");
2694
+ * you may not use this file except in compliance with the License.
2695
+ * You may obtain a copy of the License at
2696
+ *
2697
+ * http://www.apache.org/licenses/LICENSE-2.0
2698
+ *
2699
+ * Unless required by applicable law or agreed to in writing, software
2700
+ * distributed under the License is distributed on an "AS IS" BASIS,
2701
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2702
+ * See the License for the specific language governing permissions and
2703
+ * limitations under the License.
2704
+ */
2705
+ /**
2706
+ * @fileoverview Standardized Firebase Error.
2707
+ *
2708
+ * Usage:
2709
+ *
2710
+ * // TypeScript string literals for type-safe codes
2711
+ * type Err =
2712
+ * 'unknown' |
2713
+ * 'object-not-found'
2714
+ * ;
2715
+ *
2716
+ * // Closure enum for type-safe error codes
2717
+ * // at-enum {string}
2718
+ * var Err = {
2719
+ * UNKNOWN: 'unknown',
2720
+ * OBJECT_NOT_FOUND: 'object-not-found',
2721
+ * }
2722
+ *
2723
+ * let errors: Map<Err, string> = {
2724
+ * 'generic-error': "Unknown error",
2725
+ * 'file-not-found': "Could not find file: {$file}",
2726
+ * };
2727
+ *
2728
+ * // Type-safe function - must pass a valid error code as param.
2729
+ * let error = new ErrorFactory<Err>('service', 'Service', errors);
2730
+ *
2731
+ * ...
2732
+ * throw error.create(Err.GENERIC);
2733
+ * ...
2734
+ * throw error.create(Err.FILE_NOT_FOUND, {'file': fileName});
2735
+ * ...
2736
+ * // Service: Could not file file: foo.txt (service/file-not-found).
2737
+ *
2738
+ * catch (e) {
2739
+ * assert(e.message === "Could not find file: foo.txt.");
2740
+ * if ((e as FirebaseError)?.code === 'service/file-not-found') {
2741
+ * console.log("Could not read file: " + e['file']);
2742
+ * }
2743
+ * }
2744
+ */
2745
+ const ERROR_NAME = 'FirebaseError';
2746
+ // Based on code from:
2747
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types
2748
+ class FirebaseError extends Error {
2749
+ constructor(
2750
+ /** The error code for this error. */
2751
+ code, message,
2752
+ /** Custom data for this error. */
2753
+ customData) {
2754
+ super(message);
2755
+ this.code = code;
2756
+ this.customData = customData;
2757
+ /** The custom name for all FirebaseErrors. */
2758
+ this.name = ERROR_NAME;
2759
+ // Fix For ES5
2760
+ // https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
2761
+ // TODO(dlarocque): Replace this with `new.target`: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
2762
+ // which we can now use since we no longer target ES5.
2763
+ Object.setPrototypeOf(this, FirebaseError.prototype);
2764
+ // Maintains proper stack trace for where our error was thrown.
2765
+ // Only available on V8.
2766
+ if (Error.captureStackTrace) {
2767
+ Error.captureStackTrace(this, ErrorFactory.prototype.create);
2768
+ }
2769
+ }
2770
+ }
2771
+ class ErrorFactory {
2772
+ constructor(service, serviceName, errors) {
2773
+ this.service = service;
2774
+ this.serviceName = serviceName;
2775
+ this.errors = errors;
2776
+ }
2777
+ create(code, ...data) {
2778
+ const customData = data[0] || {};
2779
+ const fullCode = `${this.service}/${code}`;
2780
+ const template = this.errors[code];
2781
+ const message = template ? replaceTemplate(template, customData) : 'Error';
2782
+ // Service Name: Error message (service/code).
2783
+ const fullMessage = `${this.serviceName}: ${message} (${fullCode}).`;
2784
+ const error = new FirebaseError(fullCode, fullMessage, customData);
2785
+ return error;
2786
+ }
2787
+ }
2788
+ function replaceTemplate(template, data) {
2789
+ return template.replace(PATTERN, (_, key) => {
2790
+ const value = data[key];
2791
+ return value != null ? String(value) : `<${key}?>`;
2792
+ });
2793
+ }
2794
+ const PATTERN = /\{\$([^}]+)}/g;
2795
+ /**
2796
+ * Deep equal two objects. Support Arrays and Objects.
2797
+ */
2798
+ function deepEqual(a, b) {
2799
+ if (a === b) {
2800
+ return true;
2801
+ }
2802
+ const aKeys = Object.keys(a);
2803
+ const bKeys = Object.keys(b);
2804
+ for (const k of aKeys) {
2805
+ if (!bKeys.includes(k)) {
2806
+ return false;
2807
+ }
2808
+ const aProp = a[k];
2809
+ const bProp = b[k];
2810
+ if (isObject(aProp) && isObject(bProp)) {
2811
+ if (!deepEqual(aProp, bProp)) {
2812
+ return false;
2813
+ }
2814
+ }
2815
+ else if (aProp !== bProp) {
2816
+ return false;
2817
+ }
2818
+ }
2819
+ for (const k of bKeys) {
2820
+ if (!aKeys.includes(k)) {
2821
+ return false;
2822
+ }
2823
+ }
2824
+ return true;
2825
+ }
2826
+ function isObject(thing) {
2827
+ return thing !== null && typeof thing === 'object';
2828
+ }
2829
+
2830
+ /**
2831
+ * @license
2832
+ * Copyright 2021 Google LLC
2833
+ *
2834
+ * Licensed under the Apache License, Version 2.0 (the "License");
2835
+ * you may not use this file except in compliance with the License.
2836
+ * You may obtain a copy of the License at
2837
+ *
2838
+ * http://www.apache.org/licenses/LICENSE-2.0
2839
+ *
2840
+ * Unless required by applicable law or agreed to in writing, software
2841
+ * distributed under the License is distributed on an "AS IS" BASIS,
2842
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2843
+ * See the License for the specific language governing permissions and
2844
+ * limitations under the License.
2845
+ */
2846
+ function getModularInstance(service) {
2847
+ if (service && service._delegate) {
2848
+ return service._delegate;
2849
+ }
2850
+ else {
2851
+ return service;
2852
+ }
2853
+ }
2854
+
2855
+ /**
2856
+ * Component for service name T, e.g. `auth`, `auth-internal`
2857
+ */
2858
+ class Component {
2859
+ /**
2860
+ *
2861
+ * @param name The public service name, e.g. app, auth, firestore, database
2862
+ * @param instanceFactory Service factory responsible for creating the public interface
2863
+ * @param type whether the service provided by the component is public or private
2864
+ */
2865
+ constructor(name, instanceFactory, type) {
2866
+ this.name = name;
2867
+ this.instanceFactory = instanceFactory;
2868
+ this.type = type;
2869
+ this.multipleInstances = false;
2870
+ /**
2871
+ * Properties to be added to the service namespace
2872
+ */
2873
+ this.serviceProps = {};
2874
+ this.instantiationMode = "LAZY" /* InstantiationMode.LAZY */;
2875
+ this.onInstanceCreated = null;
2876
+ }
2877
+ setInstantiationMode(mode) {
2878
+ this.instantiationMode = mode;
2879
+ return this;
2880
+ }
2881
+ setMultipleInstances(multipleInstances) {
2882
+ this.multipleInstances = multipleInstances;
2883
+ return this;
2884
+ }
2885
+ setServiceProps(props) {
2886
+ this.serviceProps = props;
2887
+ return this;
2888
+ }
2889
+ setInstanceCreatedCallback(callback) {
2890
+ this.onInstanceCreated = callback;
2891
+ return this;
2892
+ }
2893
+ }
2894
+
2895
+ /**
2896
+ * @license
2897
+ * Copyright 2019 Google LLC
2898
+ *
2899
+ * Licensed under the Apache License, Version 2.0 (the "License");
2900
+ * you may not use this file except in compliance with the License.
2901
+ * You may obtain a copy of the License at
2902
+ *
2903
+ * http://www.apache.org/licenses/LICENSE-2.0
2904
+ *
2905
+ * Unless required by applicable law or agreed to in writing, software
2906
+ * distributed under the License is distributed on an "AS IS" BASIS,
2907
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2908
+ * See the License for the specific language governing permissions and
2909
+ * limitations under the License.
2910
+ */
2911
+ const DEFAULT_ENTRY_NAME$1 = '[DEFAULT]';
2912
+
2913
+ /**
2914
+ * @license
2915
+ * Copyright 2019 Google LLC
2916
+ *
2917
+ * Licensed under the Apache License, Version 2.0 (the "License");
2918
+ * you may not use this file except in compliance with the License.
2919
+ * You may obtain a copy of the License at
2920
+ *
2921
+ * http://www.apache.org/licenses/LICENSE-2.0
2922
+ *
2923
+ * Unless required by applicable law or agreed to in writing, software
2924
+ * distributed under the License is distributed on an "AS IS" BASIS,
2925
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2926
+ * See the License for the specific language governing permissions and
2927
+ * limitations under the License.
2928
+ */
2929
+ /**
2930
+ * Provider for instance for service name T, e.g. 'auth', 'auth-internal'
2931
+ * NameServiceMapping[T] is an alias for the type of the instance
2932
+ */
2933
+ class Provider {
2934
+ constructor(name, container) {
2935
+ this.name = name;
2936
+ this.container = container;
2937
+ this.component = null;
2938
+ this.instances = new Map();
2939
+ this.instancesDeferred = new Map();
2940
+ this.instancesOptions = new Map();
2941
+ this.onInitCallbacks = new Map();
2942
+ }
2943
+ /**
2944
+ * @param identifier A provider can provide multiple instances of a service
2945
+ * if this.component.multipleInstances is true.
2946
+ */
2947
+ get(identifier) {
2948
+ // if multipleInstances is not supported, use the default name
2949
+ const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
2950
+ if (!this.instancesDeferred.has(normalizedIdentifier)) {
2951
+ const deferred = new Deferred();
2952
+ this.instancesDeferred.set(normalizedIdentifier, deferred);
2953
+ if (this.isInitialized(normalizedIdentifier) ||
2954
+ this.shouldAutoInitialize()) {
2955
+ // initialize the service if it can be auto-initialized
2956
+ try {
2957
+ const instance = this.getOrInitializeService({
2958
+ instanceIdentifier: normalizedIdentifier
2959
+ });
2960
+ if (instance) {
2961
+ deferred.resolve(instance);
2962
+ }
2963
+ }
2964
+ catch (e) {
2965
+ // when the instance factory throws an exception during get(), it should not cause
2966
+ // a fatal error. We just return the unresolved promise in this case.
2967
+ }
2968
+ }
2969
+ }
2970
+ return this.instancesDeferred.get(normalizedIdentifier).promise;
2971
+ }
2972
+ getImmediate(options) {
2973
+ // if multipleInstances is not supported, use the default name
2974
+ const normalizedIdentifier = this.normalizeInstanceIdentifier(options?.identifier);
2975
+ const optional = options?.optional ?? false;
2976
+ if (this.isInitialized(normalizedIdentifier) ||
2977
+ this.shouldAutoInitialize()) {
2978
+ try {
2979
+ return this.getOrInitializeService({
2980
+ instanceIdentifier: normalizedIdentifier
2981
+ });
2982
+ }
2983
+ catch (e) {
2984
+ if (optional) {
2985
+ return null;
2986
+ }
2987
+ else {
2988
+ throw e;
2989
+ }
2990
+ }
2991
+ }
2992
+ else {
2993
+ // In case a component is not initialized and should/cannot be auto-initialized at the moment, return null if the optional flag is set, or throw
2994
+ if (optional) {
2995
+ return null;
2996
+ }
2997
+ else {
2998
+ throw Error(`Service ${this.name} is not available`);
2999
+ }
3000
+ }
3001
+ }
3002
+ getComponent() {
3003
+ return this.component;
3004
+ }
3005
+ setComponent(component) {
3006
+ if (component.name !== this.name) {
3007
+ throw Error(`Mismatching Component ${component.name} for Provider ${this.name}.`);
3008
+ }
3009
+ if (this.component) {
3010
+ throw Error(`Component for ${this.name} has already been provided`);
3011
+ }
3012
+ this.component = component;
3013
+ // return early without attempting to initialize the component if the component requires explicit initialization (calling `Provider.initialize()`)
3014
+ if (!this.shouldAutoInitialize()) {
3015
+ return;
3016
+ }
3017
+ // if the service is eager, initialize the default instance
3018
+ if (isComponentEager(component)) {
3019
+ try {
3020
+ this.getOrInitializeService({ instanceIdentifier: DEFAULT_ENTRY_NAME$1 });
3021
+ }
3022
+ catch (e) {
3023
+ // when the instance factory for an eager Component throws an exception during the eager
3024
+ // initialization, it should not cause a fatal error.
3025
+ // TODO: Investigate if we need to make it configurable, because some component may want to cause
3026
+ // a fatal error in this case?
3027
+ }
3028
+ }
3029
+ // Create service instances for the pending promises and resolve them
3030
+ // NOTE: if this.multipleInstances is false, only the default instance will be created
3031
+ // and all promises with resolve with it regardless of the identifier.
3032
+ for (const [instanceIdentifier, instanceDeferred] of this.instancesDeferred.entries()) {
3033
+ const normalizedIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
3034
+ try {
3035
+ // `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
3036
+ const instance = this.getOrInitializeService({
3037
+ instanceIdentifier: normalizedIdentifier
3038
+ });
3039
+ instanceDeferred.resolve(instance);
3040
+ }
3041
+ catch (e) {
3042
+ // when the instance factory throws an exception, it should not cause
3043
+ // a fatal error. We just leave the promise unresolved.
3044
+ }
3045
+ }
3046
+ }
3047
+ clearInstance(identifier = DEFAULT_ENTRY_NAME$1) {
3048
+ this.instancesDeferred.delete(identifier);
3049
+ this.instancesOptions.delete(identifier);
3050
+ this.instances.delete(identifier);
3051
+ }
3052
+ // app.delete() will call this method on every provider to delete the services
3053
+ // TODO: should we mark the provider as deleted?
3054
+ async delete() {
3055
+ const services = Array.from(this.instances.values());
3056
+ await Promise.all([
3057
+ ...services
3058
+ .filter(service => 'INTERNAL' in service) // legacy services
3059
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3060
+ .map(service => service.INTERNAL.delete()),
3061
+ ...services
3062
+ .filter(service => '_delete' in service) // modularized services
3063
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3064
+ .map(service => service._delete())
3065
+ ]);
3066
+ }
3067
+ isComponentSet() {
3068
+ return this.component != null;
3069
+ }
3070
+ isInitialized(identifier = DEFAULT_ENTRY_NAME$1) {
3071
+ return this.instances.has(identifier);
3072
+ }
3073
+ getOptions(identifier = DEFAULT_ENTRY_NAME$1) {
3074
+ return this.instancesOptions.get(identifier) || {};
3075
+ }
3076
+ initialize(opts = {}) {
3077
+ const { options = {} } = opts;
3078
+ const normalizedIdentifier = this.normalizeInstanceIdentifier(opts.instanceIdentifier);
3079
+ if (this.isInitialized(normalizedIdentifier)) {
3080
+ throw Error(`${this.name}(${normalizedIdentifier}) has already been initialized`);
3081
+ }
3082
+ if (!this.isComponentSet()) {
3083
+ throw Error(`Component ${this.name} has not been registered yet`);
3084
+ }
3085
+ const instance = this.getOrInitializeService({
3086
+ instanceIdentifier: normalizedIdentifier,
3087
+ options
3088
+ });
3089
+ // resolve any pending promise waiting for the service instance
3090
+ for (const [instanceIdentifier, instanceDeferred] of this.instancesDeferred.entries()) {
3091
+ const normalizedDeferredIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
3092
+ if (normalizedIdentifier === normalizedDeferredIdentifier) {
3093
+ instanceDeferred.resolve(instance);
3094
+ }
3095
+ }
3096
+ return instance;
3097
+ }
3098
+ /**
3099
+ *
3100
+ * @param callback - a function that will be invoked after the provider has been initialized by calling provider.initialize().
3101
+ * The function is invoked SYNCHRONOUSLY, so it should not execute any longrunning tasks in order to not block the program.
3102
+ *
3103
+ * @param identifier An optional instance identifier
3104
+ * @returns a function to unregister the callback
3105
+ */
3106
+ onInit(callback, identifier) {
3107
+ const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
3108
+ const existingCallbacks = this.onInitCallbacks.get(normalizedIdentifier) ??
3109
+ new Set();
3110
+ existingCallbacks.add(callback);
3111
+ this.onInitCallbacks.set(normalizedIdentifier, existingCallbacks);
3112
+ const existingInstance = this.instances.get(normalizedIdentifier);
3113
+ if (existingInstance) {
3114
+ callback(existingInstance, normalizedIdentifier);
3115
+ }
3116
+ return () => {
3117
+ existingCallbacks.delete(callback);
3118
+ };
3119
+ }
3120
+ /**
3121
+ * Invoke onInit callbacks synchronously
3122
+ * @param instance the service instance`
3123
+ */
3124
+ invokeOnInitCallbacks(instance, identifier) {
3125
+ const callbacks = this.onInitCallbacks.get(identifier);
3126
+ if (!callbacks) {
3127
+ return;
3128
+ }
3129
+ for (const callback of callbacks) {
3130
+ try {
3131
+ callback(instance, identifier);
3132
+ }
3133
+ catch {
3134
+ // ignore errors in the onInit callback
3135
+ }
3136
+ }
3137
+ }
3138
+ getOrInitializeService({ instanceIdentifier, options = {} }) {
3139
+ let instance = this.instances.get(instanceIdentifier);
3140
+ if (!instance && this.component) {
3141
+ instance = this.component.instanceFactory(this.container, {
3142
+ instanceIdentifier: normalizeIdentifierForFactory(instanceIdentifier),
3143
+ options
3144
+ });
3145
+ this.instances.set(instanceIdentifier, instance);
3146
+ this.instancesOptions.set(instanceIdentifier, options);
3147
+ /**
3148
+ * Invoke onInit listeners.
3149
+ * Note this.component.onInstanceCreated is different, which is used by the component creator,
3150
+ * while onInit listeners are registered by consumers of the provider.
3151
+ */
3152
+ this.invokeOnInitCallbacks(instance, instanceIdentifier);
3153
+ /**
3154
+ * Order is important
3155
+ * onInstanceCreated() should be called after this.instances.set(instanceIdentifier, instance); which
3156
+ * makes `isInitialized()` return true.
3157
+ */
3158
+ if (this.component.onInstanceCreated) {
3159
+ try {
3160
+ this.component.onInstanceCreated(this.container, instanceIdentifier, instance);
3161
+ }
3162
+ catch {
3163
+ // ignore errors in the onInstanceCreatedCallback
3164
+ }
3165
+ }
3166
+ }
3167
+ return instance || null;
3168
+ }
3169
+ normalizeInstanceIdentifier(identifier = DEFAULT_ENTRY_NAME$1) {
3170
+ if (this.component) {
3171
+ return this.component.multipleInstances ? identifier : DEFAULT_ENTRY_NAME$1;
3172
+ }
3173
+ else {
3174
+ return identifier; // assume multiple instances are supported before the component is provided.
3175
+ }
3176
+ }
3177
+ shouldAutoInitialize() {
3178
+ return (!!this.component &&
3179
+ this.component.instantiationMode !== "EXPLICIT" /* InstantiationMode.EXPLICIT */);
3180
+ }
3181
+ }
3182
+ // undefined should be passed to the service factory for the default instance
3183
+ function normalizeIdentifierForFactory(identifier) {
3184
+ return identifier === DEFAULT_ENTRY_NAME$1 ? undefined : identifier;
3185
+ }
3186
+ function isComponentEager(component) {
3187
+ return component.instantiationMode === "EAGER" /* InstantiationMode.EAGER */;
3188
+ }
3189
+
3190
+ /**
3191
+ * @license
3192
+ * Copyright 2019 Google LLC
3193
+ *
3194
+ * Licensed under the Apache License, Version 2.0 (the "License");
3195
+ * you may not use this file except in compliance with the License.
3196
+ * You may obtain a copy of the License at
3197
+ *
3198
+ * http://www.apache.org/licenses/LICENSE-2.0
3199
+ *
3200
+ * Unless required by applicable law or agreed to in writing, software
3201
+ * distributed under the License is distributed on an "AS IS" BASIS,
3202
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3203
+ * See the License for the specific language governing permissions and
3204
+ * limitations under the License.
3205
+ */
3206
+ /**
3207
+ * ComponentContainer that provides Providers for service name T, e.g. `auth`, `auth-internal`
3208
+ */
3209
+ class ComponentContainer {
3210
+ constructor(name) {
3211
+ this.name = name;
3212
+ this.providers = new Map();
3213
+ }
3214
+ /**
3215
+ *
3216
+ * @param component Component being added
3217
+ * @param overwrite When a component with the same name has already been registered,
3218
+ * if overwrite is true: overwrite the existing component with the new component and create a new
3219
+ * provider with the new component. It can be useful in tests where you want to use different mocks
3220
+ * for different tests.
3221
+ * if overwrite is false: throw an exception
3222
+ */
3223
+ addComponent(component) {
3224
+ const provider = this.getProvider(component.name);
3225
+ if (provider.isComponentSet()) {
3226
+ throw new Error(`Component ${component.name} has already been registered with ${this.name}`);
3227
+ }
3228
+ provider.setComponent(component);
3229
+ }
3230
+ addOrOverwriteComponent(component) {
3231
+ const provider = this.getProvider(component.name);
3232
+ if (provider.isComponentSet()) {
3233
+ // delete the existing provider from the container, so we can register the new component
3234
+ this.providers.delete(component.name);
3235
+ }
3236
+ this.addComponent(component);
3237
+ }
3238
+ /**
3239
+ * getProvider provides a type safe interface where it can only be called with a field name
3240
+ * present in NameServiceMapping interface.
3241
+ *
3242
+ * Firebase SDKs providing services should extend NameServiceMapping interface to register
3243
+ * themselves.
3244
+ */
3245
+ getProvider(name) {
3246
+ if (this.providers.has(name)) {
3247
+ return this.providers.get(name);
3248
+ }
3249
+ // create a Provider for a service that hasn't registered with Firebase
3250
+ const provider = new Provider(name, this);
3251
+ this.providers.set(name, provider);
3252
+ return provider;
3253
+ }
3254
+ getProviders() {
3255
+ return Array.from(this.providers.values());
3256
+ }
3257
+ }
3258
+
3259
+ /**
3260
+ * @license
3261
+ * Copyright 2017 Google LLC
3262
+ *
3263
+ * Licensed under the Apache License, Version 2.0 (the "License");
3264
+ * you may not use this file except in compliance with the License.
3265
+ * You may obtain a copy of the License at
3266
+ *
3267
+ * http://www.apache.org/licenses/LICENSE-2.0
3268
+ *
3269
+ * Unless required by applicable law or agreed to in writing, software
3270
+ * distributed under the License is distributed on an "AS IS" BASIS,
3271
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3272
+ * See the License for the specific language governing permissions and
3273
+ * limitations under the License.
3274
+ */
3275
+ /**
3276
+ * The JS SDK supports 5 log levels and also allows a user the ability to
3277
+ * silence the logs altogether.
3278
+ *
3279
+ * The order is a follows:
3280
+ * DEBUG < VERBOSE < INFO < WARN < ERROR
3281
+ *
3282
+ * All of the log types above the current log level will be captured (i.e. if
3283
+ * you set the log level to `INFO`, errors will still be logged, but `DEBUG` and
3284
+ * `VERBOSE` logs will not)
3285
+ */
3286
+ var LogLevel;
3287
+ (function (LogLevel) {
3288
+ LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
3289
+ LogLevel[LogLevel["VERBOSE"] = 1] = "VERBOSE";
3290
+ LogLevel[LogLevel["INFO"] = 2] = "INFO";
3291
+ LogLevel[LogLevel["WARN"] = 3] = "WARN";
3292
+ LogLevel[LogLevel["ERROR"] = 4] = "ERROR";
3293
+ LogLevel[LogLevel["SILENT"] = 5] = "SILENT";
3294
+ })(LogLevel || (LogLevel = {}));
3295
+ const levelStringToEnum = {
3296
+ 'debug': LogLevel.DEBUG,
3297
+ 'verbose': LogLevel.VERBOSE,
3298
+ 'info': LogLevel.INFO,
3299
+ 'warn': LogLevel.WARN,
3300
+ 'error': LogLevel.ERROR,
3301
+ 'silent': LogLevel.SILENT
3302
+ };
3303
+ /**
3304
+ * The default log level
3305
+ */
3306
+ const defaultLogLevel = LogLevel.INFO;
3307
+ /**
3308
+ * By default, `console.debug` is not displayed in the developer console (in
3309
+ * chrome). To avoid forcing users to have to opt-in to these logs twice
3310
+ * (i.e. once for firebase, and once in the console), we are sending `DEBUG`
3311
+ * logs to the `console.log` function.
3312
+ */
3313
+ const ConsoleMethod = {
3314
+ [LogLevel.DEBUG]: 'log',
3315
+ [LogLevel.VERBOSE]: 'log',
3316
+ [LogLevel.INFO]: 'info',
3317
+ [LogLevel.WARN]: 'warn',
3318
+ [LogLevel.ERROR]: 'error'
3319
+ };
3320
+ /**
3321
+ * The default log handler will forward DEBUG, VERBOSE, INFO, WARN, and ERROR
3322
+ * messages on to their corresponding console counterparts (if the log method
3323
+ * is supported by the current log level)
3324
+ */
3325
+ const defaultLogHandler = (instance, logType, ...args) => {
3326
+ if (logType < instance.logLevel) {
3327
+ return;
3328
+ }
3329
+ const now = new Date().toISOString();
3330
+ const method = ConsoleMethod[logType];
3331
+ if (method) {
3332
+ console[method](`[${now}] ${instance.name}:`, ...args);
3333
+ }
3334
+ else {
3335
+ throw new Error(`Attempted to log a message with an invalid logType (value: ${logType})`);
3336
+ }
3337
+ };
3338
+ class Logger {
3339
+ /**
3340
+ * Gives you an instance of a Logger to capture messages according to
3341
+ * Firebase's logging scheme.
3342
+ *
3343
+ * @param name The name that the logs will be associated with
3344
+ */
3345
+ constructor(name) {
3346
+ this.name = name;
3347
+ /**
3348
+ * The log level of the given Logger instance.
3349
+ */
3350
+ this._logLevel = defaultLogLevel;
3351
+ /**
3352
+ * The main (internal) log handler for the Logger instance.
3353
+ * Can be set to a new function in internal package code but not by user.
3354
+ */
3355
+ this._logHandler = defaultLogHandler;
3356
+ /**
3357
+ * The optional, additional, user-defined log handler for the Logger instance.
3358
+ */
3359
+ this._userLogHandler = null;
3360
+ }
3361
+ get logLevel() {
3362
+ return this._logLevel;
3363
+ }
3364
+ set logLevel(val) {
3365
+ if (!(val in LogLevel)) {
3366
+ throw new TypeError(`Invalid value "${val}" assigned to \`logLevel\``);
3367
+ }
3368
+ this._logLevel = val;
3369
+ }
3370
+ // Workaround for setter/getter having to be the same type.
3371
+ setLogLevel(val) {
3372
+ this._logLevel = typeof val === 'string' ? levelStringToEnum[val] : val;
3373
+ }
3374
+ get logHandler() {
3375
+ return this._logHandler;
3376
+ }
3377
+ set logHandler(val) {
3378
+ if (typeof val !== 'function') {
3379
+ throw new TypeError('Value assigned to `logHandler` must be a function');
3380
+ }
3381
+ this._logHandler = val;
3382
+ }
3383
+ get userLogHandler() {
3384
+ return this._userLogHandler;
3385
+ }
3386
+ set userLogHandler(val) {
3387
+ this._userLogHandler = val;
3388
+ }
3389
+ /**
3390
+ * The functions below are all based on the `console` interface
3391
+ */
3392
+ debug(...args) {
3393
+ this._userLogHandler && this._userLogHandler(this, LogLevel.DEBUG, ...args);
3394
+ this._logHandler(this, LogLevel.DEBUG, ...args);
3395
+ }
3396
+ log(...args) {
3397
+ this._userLogHandler &&
3398
+ this._userLogHandler(this, LogLevel.VERBOSE, ...args);
3399
+ this._logHandler(this, LogLevel.VERBOSE, ...args);
3400
+ }
3401
+ info(...args) {
3402
+ this._userLogHandler && this._userLogHandler(this, LogLevel.INFO, ...args);
3403
+ this._logHandler(this, LogLevel.INFO, ...args);
3404
+ }
3405
+ warn(...args) {
3406
+ this._userLogHandler && this._userLogHandler(this, LogLevel.WARN, ...args);
3407
+ this._logHandler(this, LogLevel.WARN, ...args);
3408
+ }
3409
+ error(...args) {
3410
+ this._userLogHandler && this._userLogHandler(this, LogLevel.ERROR, ...args);
3411
+ this._logHandler(this, LogLevel.ERROR, ...args);
3412
+ }
3413
+ }
3414
+
3415
+ const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
3416
+
3417
+ let idbProxyableTypes;
3418
+ let cursorAdvanceMethods;
3419
+ // This is a function to prevent it throwing up in node environments.
3420
+ function getIdbProxyableTypes() {
3421
+ return (idbProxyableTypes ||
3422
+ (idbProxyableTypes = [
3423
+ IDBDatabase,
3424
+ IDBObjectStore,
3425
+ IDBIndex,
3426
+ IDBCursor,
3427
+ IDBTransaction,
3428
+ ]));
3429
+ }
3430
+ // This is a function to prevent it throwing up in node environments.
3431
+ function getCursorAdvanceMethods() {
3432
+ return (cursorAdvanceMethods ||
3433
+ (cursorAdvanceMethods = [
3434
+ IDBCursor.prototype.advance,
3435
+ IDBCursor.prototype.continue,
3436
+ IDBCursor.prototype.continuePrimaryKey,
3437
+ ]));
3438
+ }
3439
+ const cursorRequestMap = new WeakMap();
3440
+ const transactionDoneMap = new WeakMap();
3441
+ const transactionStoreNamesMap = new WeakMap();
3442
+ const transformCache = new WeakMap();
3443
+ const reverseTransformCache = new WeakMap();
3444
+ function promisifyRequest(request) {
3445
+ const promise = new Promise((resolve, reject) => {
3446
+ const unlisten = () => {
3447
+ request.removeEventListener('success', success);
3448
+ request.removeEventListener('error', error);
3449
+ };
3450
+ const success = () => {
3451
+ resolve(wrap(request.result));
3452
+ unlisten();
3453
+ };
3454
+ const error = () => {
3455
+ reject(request.error);
3456
+ unlisten();
3457
+ };
3458
+ request.addEventListener('success', success);
3459
+ request.addEventListener('error', error);
3460
+ });
3461
+ promise
3462
+ .then((value) => {
3463
+ // Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
3464
+ // (see wrapFunction).
3465
+ if (value instanceof IDBCursor) {
3466
+ cursorRequestMap.set(value, request);
3467
+ }
3468
+ // Catching to avoid "Uncaught Promise exceptions"
3469
+ })
3470
+ .catch(() => { });
3471
+ // This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
3472
+ // is because we create many promises from a single IDBRequest.
3473
+ reverseTransformCache.set(promise, request);
3474
+ return promise;
3475
+ }
3476
+ function cacheDonePromiseForTransaction(tx) {
3477
+ // Early bail if we've already created a done promise for this transaction.
3478
+ if (transactionDoneMap.has(tx))
3479
+ return;
3480
+ const done = new Promise((resolve, reject) => {
3481
+ const unlisten = () => {
3482
+ tx.removeEventListener('complete', complete);
3483
+ tx.removeEventListener('error', error);
3484
+ tx.removeEventListener('abort', error);
3485
+ };
3486
+ const complete = () => {
3487
+ resolve();
3488
+ unlisten();
3489
+ };
3490
+ const error = () => {
3491
+ reject(tx.error || new DOMException('AbortError', 'AbortError'));
3492
+ unlisten();
3493
+ };
3494
+ tx.addEventListener('complete', complete);
3495
+ tx.addEventListener('error', error);
3496
+ tx.addEventListener('abort', error);
3497
+ });
3498
+ // Cache it for later retrieval.
3499
+ transactionDoneMap.set(tx, done);
3500
+ }
3501
+ let idbProxyTraps = {
3502
+ get(target, prop, receiver) {
3503
+ if (target instanceof IDBTransaction) {
3504
+ // Special handling for transaction.done.
3505
+ if (prop === 'done')
3506
+ return transactionDoneMap.get(target);
3507
+ // Polyfill for objectStoreNames because of Edge.
3508
+ if (prop === 'objectStoreNames') {
3509
+ return target.objectStoreNames || transactionStoreNamesMap.get(target);
3510
+ }
3511
+ // Make tx.store return the only store in the transaction, or undefined if there are many.
3512
+ if (prop === 'store') {
3513
+ return receiver.objectStoreNames[1]
3514
+ ? undefined
3515
+ : receiver.objectStore(receiver.objectStoreNames[0]);
3516
+ }
3517
+ }
3518
+ // Else transform whatever we get back.
3519
+ return wrap(target[prop]);
3520
+ },
3521
+ set(target, prop, value) {
3522
+ target[prop] = value;
3523
+ return true;
3524
+ },
3525
+ has(target, prop) {
3526
+ if (target instanceof IDBTransaction &&
3527
+ (prop === 'done' || prop === 'store')) {
3528
+ return true;
3529
+ }
3530
+ return prop in target;
3531
+ },
3532
+ };
3533
+ function replaceTraps(callback) {
3534
+ idbProxyTraps = callback(idbProxyTraps);
3535
+ }
3536
+ function wrapFunction(func) {
3537
+ // Due to expected object equality (which is enforced by the caching in `wrap`), we
3538
+ // only create one new func per func.
3539
+ // Edge doesn't support objectStoreNames (booo), so we polyfill it here.
3540
+ if (func === IDBDatabase.prototype.transaction &&
3541
+ !('objectStoreNames' in IDBTransaction.prototype)) {
3542
+ return function (storeNames, ...args) {
3543
+ const tx = func.call(unwrap(this), storeNames, ...args);
3544
+ transactionStoreNamesMap.set(tx, storeNames.sort ? storeNames.sort() : [storeNames]);
3545
+ return wrap(tx);
3546
+ };
3547
+ }
3548
+ // Cursor methods are special, as the behaviour is a little more different to standard IDB. In
3549
+ // IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
3550
+ // cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
3551
+ // with real promises, so each advance methods returns a new promise for the cursor object, or
3552
+ // undefined if the end of the cursor has been reached.
3553
+ if (getCursorAdvanceMethods().includes(func)) {
3554
+ return function (...args) {
3555
+ // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
3556
+ // the original object.
3557
+ func.apply(unwrap(this), args);
3558
+ return wrap(cursorRequestMap.get(this));
3559
+ };
3560
+ }
3561
+ return function (...args) {
3562
+ // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
3563
+ // the original object.
3564
+ return wrap(func.apply(unwrap(this), args));
3565
+ };
3566
+ }
3567
+ function transformCachableValue(value) {
3568
+ if (typeof value === 'function')
3569
+ return wrapFunction(value);
3570
+ // This doesn't return, it just creates a 'done' promise for the transaction,
3571
+ // which is later returned for transaction.done (see idbObjectHandler).
3572
+ if (value instanceof IDBTransaction)
3573
+ cacheDonePromiseForTransaction(value);
3574
+ if (instanceOfAny(value, getIdbProxyableTypes()))
3575
+ return new Proxy(value, idbProxyTraps);
3576
+ // Return the same value back if we're not going to transform it.
3577
+ return value;
3578
+ }
3579
+ function wrap(value) {
3580
+ // We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
3581
+ // IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
3582
+ if (value instanceof IDBRequest)
3583
+ return promisifyRequest(value);
3584
+ // If we've already transformed this value before, reuse the transformed value.
3585
+ // This is faster, but it also provides object equality.
3586
+ if (transformCache.has(value))
3587
+ return transformCache.get(value);
3588
+ const newValue = transformCachableValue(value);
3589
+ // Not all types are transformed.
3590
+ // These may be primitive types, so they can't be WeakMap keys.
3591
+ if (newValue !== value) {
3592
+ transformCache.set(value, newValue);
3593
+ reverseTransformCache.set(newValue, value);
3594
+ }
3595
+ return newValue;
3596
+ }
3597
+ const unwrap = (value) => reverseTransformCache.get(value);
3598
+
3599
+ /**
3600
+ * Open a database.
3601
+ *
3602
+ * @param name Name of the database.
3603
+ * @param version Schema version.
3604
+ * @param callbacks Additional callbacks.
3605
+ */
3606
+ function openDB(name, version, { blocked, upgrade, blocking, terminated } = {}) {
3607
+ const request = indexedDB.open(name, version);
3608
+ const openPromise = wrap(request);
3609
+ if (upgrade) {
3610
+ request.addEventListener('upgradeneeded', (event) => {
3611
+ upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);
3612
+ });
3613
+ }
3614
+ if (blocked) {
3615
+ request.addEventListener('blocked', (event) => blocked(
3616
+ // Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
3617
+ event.oldVersion, event.newVersion, event));
3618
+ }
3619
+ openPromise
3620
+ .then((db) => {
3621
+ if (terminated)
3622
+ db.addEventListener('close', () => terminated());
3623
+ if (blocking) {
3624
+ db.addEventListener('versionchange', (event) => blocking(event.oldVersion, event.newVersion, event));
3625
+ }
3626
+ })
3627
+ .catch(() => { });
3628
+ return openPromise;
3629
+ }
3630
+ /**
3631
+ * Delete a database.
3632
+ *
3633
+ * @param name Name of the database.
3634
+ */
3635
+ function deleteDB(name, { blocked } = {}) {
3636
+ const request = indexedDB.deleteDatabase(name);
3637
+ if (blocked) {
3638
+ request.addEventListener('blocked', (event) => blocked(
3639
+ // Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
3640
+ event.oldVersion, event));
3641
+ }
3642
+ return wrap(request).then(() => undefined);
3643
+ }
3644
+
3645
+ const readMethods = ['get', 'getKey', 'getAll', 'getAllKeys', 'count'];
3646
+ const writeMethods = ['put', 'add', 'delete', 'clear'];
3647
+ const cachedMethods = new Map();
3648
+ function getMethod(target, prop) {
3649
+ if (!(target instanceof IDBDatabase &&
3650
+ !(prop in target) &&
3651
+ typeof prop === 'string')) {
3652
+ return;
3653
+ }
3654
+ if (cachedMethods.get(prop))
3655
+ return cachedMethods.get(prop);
3656
+ const targetFuncName = prop.replace(/FromIndex$/, '');
3657
+ const useIndex = prop !== targetFuncName;
3658
+ const isWrite = writeMethods.includes(targetFuncName);
3659
+ if (
3660
+ // Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
3661
+ !(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) ||
3662
+ !(isWrite || readMethods.includes(targetFuncName))) {
3663
+ return;
3664
+ }
3665
+ const method = async function (storeName, ...args) {
3666
+ // isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
3667
+ const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');
3668
+ let target = tx.store;
3669
+ if (useIndex)
3670
+ target = target.index(args.shift());
3671
+ // Must reject if op rejects.
3672
+ // If it's a write operation, must reject if tx.done rejects.
3673
+ // Must reject with op rejection first.
3674
+ // Must resolve with op value.
3675
+ // Must handle both promises (no unhandled rejections)
3676
+ return (await Promise.all([
3677
+ target[targetFuncName](...args),
3678
+ isWrite && tx.done,
3679
+ ]))[0];
3680
+ };
3681
+ cachedMethods.set(prop, method);
3682
+ return method;
3683
+ }
3684
+ replaceTraps((oldTraps) => ({
3685
+ ...oldTraps,
3686
+ get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),
3687
+ has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop),
3688
+ }));
3689
+
3690
+ /**
3691
+ * @license
3692
+ * Copyright 2019 Google LLC
3693
+ *
3694
+ * Licensed under the Apache License, Version 2.0 (the "License");
3695
+ * you may not use this file except in compliance with the License.
3696
+ * You may obtain a copy of the License at
3697
+ *
3698
+ * http://www.apache.org/licenses/LICENSE-2.0
3699
+ *
3700
+ * Unless required by applicable law or agreed to in writing, software
3701
+ * distributed under the License is distributed on an "AS IS" BASIS,
3702
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3703
+ * See the License for the specific language governing permissions and
3704
+ * limitations under the License.
3705
+ */
3706
+ class PlatformLoggerServiceImpl {
3707
+ constructor(container) {
3708
+ this.container = container;
3709
+ }
3710
+ // In initial implementation, this will be called by installations on
3711
+ // auth token refresh, and installations will send this string.
3712
+ getPlatformInfoString() {
3713
+ const providers = this.container.getProviders();
3714
+ // Loop through providers and get library/version pairs from any that are
3715
+ // version components.
3716
+ return providers
3717
+ .map(provider => {
3718
+ if (isVersionServiceProvider(provider)) {
3719
+ const service = provider.getImmediate();
3720
+ return `${service.library}/${service.version}`;
3721
+ }
3722
+ else {
3723
+ return null;
3724
+ }
3725
+ })
3726
+ .filter(logString => logString)
3727
+ .join(' ');
3728
+ }
3729
+ }
3730
+ /**
3731
+ *
3732
+ * @param provider check if this provider provides a VersionService
3733
+ *
3734
+ * NOTE: Using Provider<'app-version'> is a hack to indicate that the provider
3735
+ * provides VersionService. The provider is not necessarily a 'app-version'
3736
+ * provider.
3737
+ */
3738
+ function isVersionServiceProvider(provider) {
3739
+ const component = provider.getComponent();
3740
+ return component?.type === "VERSION" /* ComponentType.VERSION */;
3741
+ }
3742
+
3743
+ const name$q = "@firebase/app";
3744
+ const version$1$1 = "0.14.4";
3745
+
3746
+ /**
3747
+ * @license
3748
+ * Copyright 2019 Google LLC
3749
+ *
3750
+ * Licensed under the Apache License, Version 2.0 (the "License");
3751
+ * you may not use this file except in compliance with the License.
3752
+ * You may obtain a copy of the License at
3753
+ *
3754
+ * http://www.apache.org/licenses/LICENSE-2.0
3755
+ *
3756
+ * Unless required by applicable law or agreed to in writing, software
3757
+ * distributed under the License is distributed on an "AS IS" BASIS,
3758
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3759
+ * See the License for the specific language governing permissions and
3760
+ * limitations under the License.
3761
+ */
3762
+ const logger = new Logger('@firebase/app');
3763
+
3764
+ const name$p = "@firebase/app-compat";
3765
+
3766
+ const name$o = "@firebase/analytics-compat";
3767
+
3768
+ const name$n = "@firebase/analytics";
3769
+
3770
+ const name$m = "@firebase/app-check-compat";
3771
+
3772
+ const name$l = "@firebase/app-check";
3773
+
3774
+ const name$k = "@firebase/auth";
3775
+
3776
+ const name$j = "@firebase/auth-compat";
3777
+
3778
+ const name$i = "@firebase/database";
3779
+
3780
+ const name$h = "@firebase/data-connect";
3781
+
3782
+ const name$g = "@firebase/database-compat";
3783
+
3784
+ const name$f = "@firebase/functions";
3785
+
3786
+ const name$e = "@firebase/functions-compat";
3787
+
3788
+ const name$d = "@firebase/installations";
3789
+
3790
+ const name$c = "@firebase/installations-compat";
3791
+
3792
+ const name$b = "@firebase/messaging";
3793
+
3794
+ const name$a = "@firebase/messaging-compat";
3795
+
3796
+ const name$9 = "@firebase/performance";
3797
+
3798
+ const name$8 = "@firebase/performance-compat";
3799
+
3800
+ const name$7 = "@firebase/remote-config";
3801
+
3802
+ const name$6 = "@firebase/remote-config-compat";
3803
+
3804
+ const name$5 = "@firebase/storage";
3805
+
3806
+ const name$4 = "@firebase/storage-compat";
3807
+
3808
+ const name$3 = "@firebase/firestore";
3809
+
3810
+ const name$2$1 = "@firebase/ai";
3811
+
3812
+ const name$1$1 = "@firebase/firestore-compat";
3813
+
3814
+ const name$r = "firebase";
3815
+
3816
+ /**
3817
+ * @license
3818
+ * Copyright 2019 Google LLC
3819
+ *
3820
+ * Licensed under the Apache License, Version 2.0 (the "License");
3821
+ * you may not use this file except in compliance with the License.
3822
+ * You may obtain a copy of the License at
3823
+ *
3824
+ * http://www.apache.org/licenses/LICENSE-2.0
3825
+ *
3826
+ * Unless required by applicable law or agreed to in writing, software
3827
+ * distributed under the License is distributed on an "AS IS" BASIS,
3828
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3829
+ * See the License for the specific language governing permissions and
3830
+ * limitations under the License.
3831
+ */
3832
+ /**
3833
+ * The default app name
3834
+ *
3835
+ * @internal
3836
+ */
3837
+ const DEFAULT_ENTRY_NAME = '[DEFAULT]';
3838
+ const PLATFORM_LOG_STRING = {
3839
+ [name$q]: 'fire-core',
3840
+ [name$p]: 'fire-core-compat',
3841
+ [name$n]: 'fire-analytics',
3842
+ [name$o]: 'fire-analytics-compat',
3843
+ [name$l]: 'fire-app-check',
3844
+ [name$m]: 'fire-app-check-compat',
3845
+ [name$k]: 'fire-auth',
3846
+ [name$j]: 'fire-auth-compat',
3847
+ [name$i]: 'fire-rtdb',
3848
+ [name$h]: 'fire-data-connect',
3849
+ [name$g]: 'fire-rtdb-compat',
3850
+ [name$f]: 'fire-fn',
3851
+ [name$e]: 'fire-fn-compat',
3852
+ [name$d]: 'fire-iid',
3853
+ [name$c]: 'fire-iid-compat',
3854
+ [name$b]: 'fire-fcm',
3855
+ [name$a]: 'fire-fcm-compat',
3856
+ [name$9]: 'fire-perf',
3857
+ [name$8]: 'fire-perf-compat',
3858
+ [name$7]: 'fire-rc',
3859
+ [name$6]: 'fire-rc-compat',
3860
+ [name$5]: 'fire-gcs',
3861
+ [name$4]: 'fire-gcs-compat',
3862
+ [name$3]: 'fire-fst',
3863
+ [name$1$1]: 'fire-fst-compat',
3864
+ [name$2$1]: 'fire-vertex',
3865
+ 'fire-js': 'fire-js', // Platform identifier for JS SDK.
3866
+ [name$r]: 'fire-js-all'
3867
+ };
3868
+
3869
+ /**
3870
+ * @license
3871
+ * Copyright 2019 Google LLC
3872
+ *
3873
+ * Licensed under the Apache License, Version 2.0 (the "License");
3874
+ * you may not use this file except in compliance with the License.
3875
+ * You may obtain a copy of the License at
3876
+ *
3877
+ * http://www.apache.org/licenses/LICENSE-2.0
3878
+ *
3879
+ * Unless required by applicable law or agreed to in writing, software
3880
+ * distributed under the License is distributed on an "AS IS" BASIS,
3881
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3882
+ * See the License for the specific language governing permissions and
3883
+ * limitations under the License.
3884
+ */
3885
+ /**
3886
+ * @internal
3887
+ */
3888
+ const _apps = new Map();
3889
+ /**
3890
+ * @internal
3891
+ */
3892
+ const _serverApps = new Map();
3893
+ /**
3894
+ * Registered components.
3895
+ *
3896
+ * @internal
3897
+ */
3898
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3899
+ const _components = new Map();
3900
+ /**
3901
+ * @param component - the component being added to this app's container
3902
+ *
3903
+ * @internal
3904
+ */
3905
+ function _addComponent(app, component) {
3906
+ try {
3907
+ app.container.addComponent(component);
3908
+ }
3909
+ catch (e) {
3910
+ logger.debug(`Component ${component.name} failed to register with FirebaseApp ${app.name}`, e);
3911
+ }
3912
+ }
3913
+ /**
3914
+ *
3915
+ * @param component - the component to register
3916
+ * @returns whether or not the component is registered successfully
3917
+ *
3918
+ * @internal
3919
+ */
3920
+ function _registerComponent(component) {
3921
+ const componentName = component.name;
3922
+ if (_components.has(componentName)) {
3923
+ logger.debug(`There were multiple attempts to register component ${componentName}.`);
3924
+ return false;
3925
+ }
3926
+ _components.set(componentName, component);
3927
+ // add the component to existing app instances
3928
+ for (const app of _apps.values()) {
3929
+ _addComponent(app, component);
3930
+ }
3931
+ for (const serverApp of _serverApps.values()) {
3932
+ _addComponent(serverApp, component);
3933
+ }
3934
+ return true;
3935
+ }
3936
+ /**
3937
+ *
3938
+ * @param app - FirebaseApp instance
3939
+ * @param name - service name
3940
+ *
3941
+ * @returns the provider for the service with the matching name
3942
+ *
3943
+ * @internal
3944
+ */
3945
+ function _getProvider(app, name) {
3946
+ const heartbeatController = app.container
3947
+ .getProvider('heartbeat')
3948
+ .getImmediate({ optional: true });
3949
+ if (heartbeatController) {
3950
+ void heartbeatController.triggerHeartbeat();
3951
+ }
3952
+ return app.container.getProvider(name);
3953
+ }
3954
+
3955
+ /**
3956
+ * @license
3957
+ * Copyright 2019 Google LLC
3958
+ *
3959
+ * Licensed under the Apache License, Version 2.0 (the "License");
3960
+ * you may not use this file except in compliance with the License.
3961
+ * You may obtain a copy of the License at
3962
+ *
3963
+ * http://www.apache.org/licenses/LICENSE-2.0
3964
+ *
3965
+ * Unless required by applicable law or agreed to in writing, software
3966
+ * distributed under the License is distributed on an "AS IS" BASIS,
3967
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3968
+ * See the License for the specific language governing permissions and
3969
+ * limitations under the License.
3970
+ */
3971
+ const ERRORS = {
3972
+ ["no-app" /* AppError.NO_APP */]: "No Firebase App '{$appName}' has been created - " +
3973
+ 'call initializeApp() first',
3974
+ ["bad-app-name" /* AppError.BAD_APP_NAME */]: "Illegal App name: '{$appName}'",
3975
+ ["duplicate-app" /* AppError.DUPLICATE_APP */]: "Firebase App named '{$appName}' already exists with different options or config",
3976
+ ["app-deleted" /* AppError.APP_DELETED */]: "Firebase App named '{$appName}' already deleted",
3977
+ ["server-app-deleted" /* AppError.SERVER_APP_DELETED */]: 'Firebase Server App has been deleted',
3978
+ ["no-options" /* AppError.NO_OPTIONS */]: 'Need to provide options, when not being deployed to hosting via source.',
3979
+ ["invalid-app-argument" /* AppError.INVALID_APP_ARGUMENT */]: 'firebase.{$appName}() takes either no argument or a ' +
3980
+ 'Firebase App instance.',
3981
+ ["invalid-log-argument" /* AppError.INVALID_LOG_ARGUMENT */]: 'First argument to `onLog` must be null or a function.',
3982
+ ["idb-open" /* AppError.IDB_OPEN */]: 'Error thrown when opening IndexedDB. Original error: {$originalErrorMessage}.',
3983
+ ["idb-get" /* AppError.IDB_GET */]: 'Error thrown when reading from IndexedDB. Original error: {$originalErrorMessage}.',
3984
+ ["idb-set" /* AppError.IDB_WRITE */]: 'Error thrown when writing to IndexedDB. Original error: {$originalErrorMessage}.',
3985
+ ["idb-delete" /* AppError.IDB_DELETE */]: 'Error thrown when deleting from IndexedDB. Original error: {$originalErrorMessage}.',
3986
+ ["finalization-registry-not-supported" /* AppError.FINALIZATION_REGISTRY_NOT_SUPPORTED */]: 'FirebaseServerApp deleteOnDeref field defined but the JS runtime does not support FinalizationRegistry.',
3987
+ ["invalid-server-app-environment" /* AppError.INVALID_SERVER_APP_ENVIRONMENT */]: 'FirebaseServerApp is not for use in browser environments.'
3988
+ };
3989
+ const ERROR_FACTORY$2 = new ErrorFactory('app', 'Firebase', ERRORS);
3990
+
3991
+ /**
3992
+ * @license
3993
+ * Copyright 2019 Google LLC
3994
+ *
3995
+ * Licensed under the Apache License, Version 2.0 (the "License");
3996
+ * you may not use this file except in compliance with the License.
3997
+ * You may obtain a copy of the License at
3998
+ *
3999
+ * http://www.apache.org/licenses/LICENSE-2.0
4000
+ *
4001
+ * Unless required by applicable law or agreed to in writing, software
4002
+ * distributed under the License is distributed on an "AS IS" BASIS,
4003
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4004
+ * See the License for the specific language governing permissions and
4005
+ * limitations under the License.
4006
+ */
4007
+ class FirebaseAppImpl {
4008
+ constructor(options, config, container) {
4009
+ this._isDeleted = false;
4010
+ this._options = { ...options };
4011
+ this._config = { ...config };
4012
+ this._name = config.name;
4013
+ this._automaticDataCollectionEnabled =
4014
+ config.automaticDataCollectionEnabled;
4015
+ this._container = container;
4016
+ this.container.addComponent(new Component('app', () => this, "PUBLIC" /* ComponentType.PUBLIC */));
4017
+ }
4018
+ get automaticDataCollectionEnabled() {
4019
+ this.checkDestroyed();
4020
+ return this._automaticDataCollectionEnabled;
4021
+ }
4022
+ set automaticDataCollectionEnabled(val) {
4023
+ this.checkDestroyed();
4024
+ this._automaticDataCollectionEnabled = val;
4025
+ }
4026
+ get name() {
4027
+ this.checkDestroyed();
4028
+ return this._name;
4029
+ }
4030
+ get options() {
4031
+ this.checkDestroyed();
4032
+ return this._options;
4033
+ }
4034
+ get config() {
4035
+ this.checkDestroyed();
4036
+ return this._config;
4037
+ }
4038
+ get container() {
4039
+ return this._container;
4040
+ }
4041
+ get isDeleted() {
4042
+ return this._isDeleted;
4043
+ }
4044
+ set isDeleted(val) {
4045
+ this._isDeleted = val;
4046
+ }
4047
+ /**
4048
+ * This function will throw an Error if the App has already been deleted -
4049
+ * use before performing API actions on the App.
4050
+ */
4051
+ checkDestroyed() {
4052
+ if (this.isDeleted) {
4053
+ throw ERROR_FACTORY$2.create("app-deleted" /* AppError.APP_DELETED */, { appName: this._name });
4054
+ }
4055
+ }
4056
+ }
4057
+ function initializeApp(_options, rawConfig = {}) {
4058
+ let options = _options;
4059
+ if (typeof rawConfig !== 'object') {
4060
+ const name = rawConfig;
4061
+ rawConfig = { name };
4062
+ }
4063
+ const config = {
4064
+ name: DEFAULT_ENTRY_NAME,
4065
+ automaticDataCollectionEnabled: true,
4066
+ ...rawConfig
4067
+ };
4068
+ const name = config.name;
4069
+ if (typeof name !== 'string' || !name) {
4070
+ throw ERROR_FACTORY$2.create("bad-app-name" /* AppError.BAD_APP_NAME */, {
4071
+ appName: String(name)
4072
+ });
4073
+ }
4074
+ options || (options = getDefaultAppConfig());
4075
+ if (!options) {
4076
+ throw ERROR_FACTORY$2.create("no-options" /* AppError.NO_OPTIONS */);
4077
+ }
4078
+ const existingApp = _apps.get(name);
4079
+ if (existingApp) {
4080
+ // return the existing app if options and config deep equal the ones in the existing app.
4081
+ if (deepEqual(options, existingApp.options) &&
4082
+ deepEqual(config, existingApp.config)) {
4083
+ return existingApp;
4084
+ }
4085
+ else {
4086
+ throw ERROR_FACTORY$2.create("duplicate-app" /* AppError.DUPLICATE_APP */, { appName: name });
4087
+ }
4088
+ }
4089
+ const container = new ComponentContainer(name);
4090
+ for (const component of _components.values()) {
4091
+ container.addComponent(component);
4092
+ }
4093
+ const newApp = new FirebaseAppImpl(options, config, container);
4094
+ _apps.set(name, newApp);
4095
+ return newApp;
4096
+ }
4097
+ /**
4098
+ * Retrieves a {@link @firebase/app#FirebaseApp} instance.
4099
+ *
4100
+ * When called with no arguments, the default app is returned. When an app name
4101
+ * is provided, the app corresponding to that name is returned.
4102
+ *
4103
+ * An exception is thrown if the app being retrieved has not yet been
4104
+ * initialized.
4105
+ *
4106
+ * @example
4107
+ * ```javascript
4108
+ * // Return the default app
4109
+ * const app = getApp();
4110
+ * ```
4111
+ *
4112
+ * @example
4113
+ * ```javascript
4114
+ * // Return a named app
4115
+ * const otherApp = getApp("otherApp");
4116
+ * ```
4117
+ *
4118
+ * @param name - Optional name of the app to return. If no name is
4119
+ * provided, the default is `"[DEFAULT]"`.
4120
+ *
4121
+ * @returns The app corresponding to the provided app name.
4122
+ * If no app name is provided, the default app is returned.
4123
+ *
4124
+ * @public
4125
+ */
4126
+ function getApp(name = DEFAULT_ENTRY_NAME) {
4127
+ const app = _apps.get(name);
4128
+ if (!app && name === DEFAULT_ENTRY_NAME && getDefaultAppConfig()) {
4129
+ return initializeApp();
4130
+ }
4131
+ if (!app) {
4132
+ throw ERROR_FACTORY$2.create("no-app" /* AppError.NO_APP */, { appName: name });
4133
+ }
4134
+ return app;
4135
+ }
4136
+ /**
4137
+ * Registers a library's name and version for platform logging purposes.
4138
+ * @param library - Name of 1p or 3p library (e.g. firestore, angularfire)
4139
+ * @param version - Current version of that library.
4140
+ * @param variant - Bundle variant, e.g., node, rn, etc.
4141
+ *
4142
+ * @public
4143
+ */
4144
+ function registerVersion(libraryKeyOrName, version, variant) {
4145
+ // TODO: We can use this check to whitelist strings when/if we set up
4146
+ // a good whitelist system.
4147
+ let library = PLATFORM_LOG_STRING[libraryKeyOrName] ?? libraryKeyOrName;
4148
+ if (variant) {
4149
+ library += `-${variant}`;
4150
+ }
4151
+ const libraryMismatch = library.match(/\s|\//);
4152
+ const versionMismatch = version.match(/\s|\//);
4153
+ if (libraryMismatch || versionMismatch) {
4154
+ const warning = [
4155
+ `Unable to register library "${library}" with version "${version}":`
4156
+ ];
4157
+ if (libraryMismatch) {
4158
+ warning.push(`library name "${library}" contains illegal characters (whitespace or "/")`);
4159
+ }
4160
+ if (libraryMismatch && versionMismatch) {
4161
+ warning.push('and');
4162
+ }
4163
+ if (versionMismatch) {
4164
+ warning.push(`version name "${version}" contains illegal characters (whitespace or "/")`);
4165
+ }
4166
+ logger.warn(warning.join(' '));
4167
+ return;
4168
+ }
4169
+ _registerComponent(new Component(`${library}-version`, () => ({ library, version }), "VERSION" /* ComponentType.VERSION */));
4170
+ }
4171
+
4172
+ /**
4173
+ * @license
4174
+ * Copyright 2021 Google LLC
4175
+ *
4176
+ * Licensed under the Apache License, Version 2.0 (the "License");
4177
+ * you may not use this file except in compliance with the License.
4178
+ * You may obtain a copy of the License at
4179
+ *
4180
+ * http://www.apache.org/licenses/LICENSE-2.0
4181
+ *
4182
+ * Unless required by applicable law or agreed to in writing, software
4183
+ * distributed under the License is distributed on an "AS IS" BASIS,
4184
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4185
+ * See the License for the specific language governing permissions and
4186
+ * limitations under the License.
4187
+ */
4188
+ const DB_NAME = 'firebase-heartbeat-database';
4189
+ const DB_VERSION = 1;
4190
+ const STORE_NAME = 'firebase-heartbeat-store';
4191
+ let dbPromise$2 = null;
4192
+ function getDbPromise$2() {
4193
+ if (!dbPromise$2) {
4194
+ dbPromise$2 = openDB(DB_NAME, DB_VERSION, {
4195
+ upgrade: (db, oldVersion) => {
4196
+ // We don't use 'break' in this switch statement, the fall-through
4197
+ // behavior is what we want, because if there are multiple versions between
4198
+ // the old version and the current version, we want ALL the migrations
4199
+ // that correspond to those versions to run, not only the last one.
4200
+ // eslint-disable-next-line default-case
4201
+ switch (oldVersion) {
4202
+ case 0:
4203
+ try {
4204
+ db.createObjectStore(STORE_NAME);
4205
+ }
4206
+ catch (e) {
4207
+ // Safari/iOS browsers throw occasional exceptions on
4208
+ // db.createObjectStore() that may be a bug. Avoid blocking
4209
+ // the rest of the app functionality.
4210
+ console.warn(e);
4211
+ }
4212
+ }
4213
+ }
4214
+ }).catch(e => {
4215
+ throw ERROR_FACTORY$2.create("idb-open" /* AppError.IDB_OPEN */, {
4216
+ originalErrorMessage: e.message
4217
+ });
4218
+ });
4219
+ }
4220
+ return dbPromise$2;
4221
+ }
4222
+ async function readHeartbeatsFromIndexedDB(app) {
4223
+ try {
4224
+ const db = await getDbPromise$2();
4225
+ const tx = db.transaction(STORE_NAME);
4226
+ const result = await tx.objectStore(STORE_NAME).get(computeKey(app));
4227
+ // We already have the value but tx.done can throw,
4228
+ // so we need to await it here to catch errors
4229
+ await tx.done;
4230
+ return result;
4231
+ }
4232
+ catch (e) {
4233
+ if (e instanceof FirebaseError) {
4234
+ logger.warn(e.message);
4235
+ }
4236
+ else {
4237
+ const idbGetError = ERROR_FACTORY$2.create("idb-get" /* AppError.IDB_GET */, {
4238
+ originalErrorMessage: e?.message
4239
+ });
4240
+ logger.warn(idbGetError.message);
4241
+ }
4242
+ }
4243
+ }
4244
+ async function writeHeartbeatsToIndexedDB(app, heartbeatObject) {
4245
+ try {
4246
+ const db = await getDbPromise$2();
4247
+ const tx = db.transaction(STORE_NAME, 'readwrite');
4248
+ const objectStore = tx.objectStore(STORE_NAME);
4249
+ await objectStore.put(heartbeatObject, computeKey(app));
4250
+ await tx.done;
4251
+ }
4252
+ catch (e) {
4253
+ if (e instanceof FirebaseError) {
4254
+ logger.warn(e.message);
4255
+ }
4256
+ else {
4257
+ const idbGetError = ERROR_FACTORY$2.create("idb-set" /* AppError.IDB_WRITE */, {
4258
+ originalErrorMessage: e?.message
4259
+ });
4260
+ logger.warn(idbGetError.message);
4261
+ }
4262
+ }
4263
+ }
4264
+ function computeKey(app) {
4265
+ return `${app.name}!${app.options.appId}`;
4266
+ }
4267
+
4268
+ /**
4269
+ * @license
4270
+ * Copyright 2021 Google LLC
4271
+ *
4272
+ * Licensed under the Apache License, Version 2.0 (the "License");
4273
+ * you may not use this file except in compliance with the License.
4274
+ * You may obtain a copy of the License at
4275
+ *
4276
+ * http://www.apache.org/licenses/LICENSE-2.0
4277
+ *
4278
+ * Unless required by applicable law or agreed to in writing, software
4279
+ * distributed under the License is distributed on an "AS IS" BASIS,
4280
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4281
+ * See the License for the specific language governing permissions and
4282
+ * limitations under the License.
4283
+ */
4284
+ const MAX_HEADER_BYTES = 1024;
4285
+ const MAX_NUM_STORED_HEARTBEATS = 30;
4286
+ class HeartbeatServiceImpl {
4287
+ constructor(container) {
4288
+ this.container = container;
4289
+ /**
4290
+ * In-memory cache for heartbeats, used by getHeartbeatsHeader() to generate
4291
+ * the header string.
4292
+ * Stores one record per date. This will be consolidated into the standard
4293
+ * format of one record per user agent string before being sent as a header.
4294
+ * Populated from indexedDB when the controller is instantiated and should
4295
+ * be kept in sync with indexedDB.
4296
+ * Leave public for easier testing.
4297
+ */
4298
+ this._heartbeatsCache = null;
4299
+ const app = this.container.getProvider('app').getImmediate();
4300
+ this._storage = new HeartbeatStorageImpl(app);
4301
+ this._heartbeatsCachePromise = this._storage.read().then(result => {
4302
+ this._heartbeatsCache = result;
4303
+ return result;
4304
+ });
4305
+ }
4306
+ /**
4307
+ * Called to report a heartbeat. The function will generate
4308
+ * a HeartbeatsByUserAgent object, update heartbeatsCache, and persist it
4309
+ * to IndexedDB.
4310
+ * Note that we only store one heartbeat per day. So if a heartbeat for today is
4311
+ * already logged, subsequent calls to this function in the same day will be ignored.
4312
+ */
4313
+ async triggerHeartbeat() {
4314
+ try {
4315
+ const platformLogger = this.container
4316
+ .getProvider('platform-logger')
4317
+ .getImmediate();
4318
+ // This is the "Firebase user agent" string from the platform logger
4319
+ // service, not the browser user agent.
4320
+ const agent = platformLogger.getPlatformInfoString();
4321
+ const date = getUTCDateString();
4322
+ if (this._heartbeatsCache?.heartbeats == null) {
4323
+ this._heartbeatsCache = await this._heartbeatsCachePromise;
4324
+ // If we failed to construct a heartbeats cache, then return immediately.
4325
+ if (this._heartbeatsCache?.heartbeats == null) {
4326
+ return;
4327
+ }
4328
+ }
4329
+ // Do not store a heartbeat if one is already stored for this day
4330
+ // or if a header has already been sent today.
4331
+ if (this._heartbeatsCache.lastSentHeartbeatDate === date ||
4332
+ this._heartbeatsCache.heartbeats.some(singleDateHeartbeat => singleDateHeartbeat.date === date)) {
4333
+ return;
4334
+ }
4335
+ else {
4336
+ // There is no entry for this date. Create one.
4337
+ this._heartbeatsCache.heartbeats.push({ date, agent });
4338
+ // If the number of stored heartbeats exceeds the maximum number of stored heartbeats, remove the heartbeat with the earliest date.
4339
+ // Since this is executed each time a heartbeat is pushed, the limit can only be exceeded by one, so only one needs to be removed.
4340
+ if (this._heartbeatsCache.heartbeats.length > MAX_NUM_STORED_HEARTBEATS) {
4341
+ const earliestHeartbeatIdx = getEarliestHeartbeatIdx(this._heartbeatsCache.heartbeats);
4342
+ this._heartbeatsCache.heartbeats.splice(earliestHeartbeatIdx, 1);
4343
+ }
4344
+ }
4345
+ return this._storage.overwrite(this._heartbeatsCache);
4346
+ }
4347
+ catch (e) {
4348
+ logger.warn(e);
4349
+ }
4350
+ }
4351
+ /**
4352
+ * Returns a base64 encoded string which can be attached to the heartbeat-specific header directly.
4353
+ * It also clears all heartbeats from memory as well as in IndexedDB.
4354
+ *
4355
+ * NOTE: Consuming product SDKs should not send the header if this method
4356
+ * returns an empty string.
4357
+ */
4358
+ async getHeartbeatsHeader() {
4359
+ try {
4360
+ if (this._heartbeatsCache === null) {
4361
+ await this._heartbeatsCachePromise;
4362
+ }
4363
+ // If it's still null or the array is empty, there is no data to send.
4364
+ if (this._heartbeatsCache?.heartbeats == null ||
4365
+ this._heartbeatsCache.heartbeats.length === 0) {
4366
+ return '';
4367
+ }
4368
+ const date = getUTCDateString();
4369
+ // Extract as many heartbeats from the cache as will fit under the size limit.
4370
+ const { heartbeatsToSend, unsentEntries } = extractHeartbeatsForHeader(this._heartbeatsCache.heartbeats);
4371
+ const headerString = base64urlEncodeWithoutPadding(JSON.stringify({ version: 2, heartbeats: heartbeatsToSend }));
4372
+ // Store last sent date to prevent another being logged/sent for the same day.
4373
+ this._heartbeatsCache.lastSentHeartbeatDate = date;
4374
+ if (unsentEntries.length > 0) {
4375
+ // Store any unsent entries if they exist.
4376
+ this._heartbeatsCache.heartbeats = unsentEntries;
4377
+ // This seems more likely than emptying the array (below) to lead to some odd state
4378
+ // since the cache isn't empty and this will be called again on the next request,
4379
+ // and is probably safest if we await it.
4380
+ await this._storage.overwrite(this._heartbeatsCache);
4381
+ }
4382
+ else {
4383
+ this._heartbeatsCache.heartbeats = [];
4384
+ // Do not wait for this, to reduce latency.
4385
+ void this._storage.overwrite(this._heartbeatsCache);
4386
+ }
4387
+ return headerString;
4388
+ }
4389
+ catch (e) {
4390
+ logger.warn(e);
4391
+ return '';
4392
+ }
4393
+ }
4394
+ }
4395
+ function getUTCDateString() {
4396
+ const today = new Date();
4397
+ // Returns date format 'YYYY-MM-DD'
4398
+ return today.toISOString().substring(0, 10);
4399
+ }
4400
+ function extractHeartbeatsForHeader(heartbeatsCache, maxSize = MAX_HEADER_BYTES) {
4401
+ // Heartbeats grouped by user agent in the standard format to be sent in
4402
+ // the header.
4403
+ const heartbeatsToSend = [];
4404
+ // Single date format heartbeats that are not sent.
4405
+ let unsentEntries = heartbeatsCache.slice();
4406
+ for (const singleDateHeartbeat of heartbeatsCache) {
4407
+ // Look for an existing entry with the same user agent.
4408
+ const heartbeatEntry = heartbeatsToSend.find(hb => hb.agent === singleDateHeartbeat.agent);
4409
+ if (!heartbeatEntry) {
4410
+ // If no entry for this user agent exists, create one.
4411
+ heartbeatsToSend.push({
4412
+ agent: singleDateHeartbeat.agent,
4413
+ dates: [singleDateHeartbeat.date]
4414
+ });
4415
+ if (countBytes(heartbeatsToSend) > maxSize) {
4416
+ // If the header would exceed max size, remove the added heartbeat
4417
+ // entry and stop adding to the header.
4418
+ heartbeatsToSend.pop();
4419
+ break;
4420
+ }
4421
+ }
4422
+ else {
4423
+ heartbeatEntry.dates.push(singleDateHeartbeat.date);
4424
+ // If the header would exceed max size, remove the added date
4425
+ // and stop adding to the header.
4426
+ if (countBytes(heartbeatsToSend) > maxSize) {
4427
+ heartbeatEntry.dates.pop();
4428
+ break;
4429
+ }
4430
+ }
4431
+ // Pop unsent entry from queue. (Skipped if adding the entry exceeded
4432
+ // quota and the loop breaks early.)
4433
+ unsentEntries = unsentEntries.slice(1);
4434
+ }
4435
+ return {
4436
+ heartbeatsToSend,
4437
+ unsentEntries
4438
+ };
4439
+ }
4440
+ class HeartbeatStorageImpl {
4441
+ constructor(app) {
4442
+ this.app = app;
4443
+ this._canUseIndexedDBPromise = this.runIndexedDBEnvironmentCheck();
4444
+ }
4445
+ async runIndexedDBEnvironmentCheck() {
4446
+ if (!isIndexedDBAvailable()) {
4447
+ return false;
4448
+ }
4449
+ else {
4450
+ return validateIndexedDBOpenable()
4451
+ .then(() => true)
4452
+ .catch(() => false);
4453
+ }
4454
+ }
4455
+ /**
4456
+ * Read all heartbeats.
4457
+ */
4458
+ async read() {
4459
+ const canUseIndexedDB = await this._canUseIndexedDBPromise;
4460
+ if (!canUseIndexedDB) {
4461
+ return { heartbeats: [] };
4462
+ }
4463
+ else {
4464
+ const idbHeartbeatObject = await readHeartbeatsFromIndexedDB(this.app);
4465
+ if (idbHeartbeatObject?.heartbeats) {
4466
+ return idbHeartbeatObject;
4467
+ }
4468
+ else {
4469
+ return { heartbeats: [] };
4470
+ }
4471
+ }
4472
+ }
4473
+ // overwrite the storage with the provided heartbeats
4474
+ async overwrite(heartbeatsObject) {
4475
+ const canUseIndexedDB = await this._canUseIndexedDBPromise;
4476
+ if (!canUseIndexedDB) {
4477
+ return;
4478
+ }
4479
+ else {
4480
+ const existingHeartbeatsObject = await this.read();
4481
+ return writeHeartbeatsToIndexedDB(this.app, {
4482
+ lastSentHeartbeatDate: heartbeatsObject.lastSentHeartbeatDate ??
4483
+ existingHeartbeatsObject.lastSentHeartbeatDate,
4484
+ heartbeats: heartbeatsObject.heartbeats
4485
+ });
4486
+ }
4487
+ }
4488
+ // add heartbeats
4489
+ async add(heartbeatsObject) {
4490
+ const canUseIndexedDB = await this._canUseIndexedDBPromise;
4491
+ if (!canUseIndexedDB) {
4492
+ return;
4493
+ }
4494
+ else {
4495
+ const existingHeartbeatsObject = await this.read();
4496
+ return writeHeartbeatsToIndexedDB(this.app, {
4497
+ lastSentHeartbeatDate: heartbeatsObject.lastSentHeartbeatDate ??
4498
+ existingHeartbeatsObject.lastSentHeartbeatDate,
4499
+ heartbeats: [
4500
+ ...existingHeartbeatsObject.heartbeats,
4501
+ ...heartbeatsObject.heartbeats
4502
+ ]
4503
+ });
4504
+ }
4505
+ }
4506
+ }
4507
+ /**
4508
+ * Calculate bytes of a HeartbeatsByUserAgent array after being wrapped
4509
+ * in a platform logging header JSON object, stringified, and converted
4510
+ * to base 64.
4511
+ */
4512
+ function countBytes(heartbeatsCache) {
4513
+ // base64 has a restricted set of characters, all of which should be 1 byte.
4514
+ return base64urlEncodeWithoutPadding(
4515
+ // heartbeatsCache wrapper properties
4516
+ JSON.stringify({ version: 2, heartbeats: heartbeatsCache })).length;
4517
+ }
4518
+ /**
4519
+ * Returns the index of the heartbeat with the earliest date.
4520
+ * If the heartbeats array is empty, -1 is returned.
4521
+ */
4522
+ function getEarliestHeartbeatIdx(heartbeats) {
4523
+ if (heartbeats.length === 0) {
4524
+ return -1;
4525
+ }
4526
+ let earliestHeartbeatIdx = 0;
4527
+ let earliestHeartbeatDate = heartbeats[0].date;
4528
+ for (let i = 1; i < heartbeats.length; i++) {
4529
+ if (heartbeats[i].date < earliestHeartbeatDate) {
4530
+ earliestHeartbeatDate = heartbeats[i].date;
4531
+ earliestHeartbeatIdx = i;
4532
+ }
4533
+ }
4534
+ return earliestHeartbeatIdx;
4535
+ }
4536
+
4537
+ /**
4538
+ * @license
4539
+ * Copyright 2019 Google LLC
4540
+ *
4541
+ * Licensed under the Apache License, Version 2.0 (the "License");
4542
+ * you may not use this file except in compliance with the License.
4543
+ * You may obtain a copy of the License at
4544
+ *
4545
+ * http://www.apache.org/licenses/LICENSE-2.0
4546
+ *
4547
+ * Unless required by applicable law or agreed to in writing, software
4548
+ * distributed under the License is distributed on an "AS IS" BASIS,
4549
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4550
+ * See the License for the specific language governing permissions and
4551
+ * limitations under the License.
4552
+ */
4553
+ function registerCoreComponents(variant) {
4554
+ _registerComponent(new Component('platform-logger', container => new PlatformLoggerServiceImpl(container), "PRIVATE" /* ComponentType.PRIVATE */));
4555
+ _registerComponent(new Component('heartbeat', container => new HeartbeatServiceImpl(container), "PRIVATE" /* ComponentType.PRIVATE */));
4556
+ // Register `app` package.
4557
+ registerVersion(name$q, version$1$1, variant);
4558
+ // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation
4559
+ registerVersion(name$q, version$1$1, 'esm2020');
4560
+ // Register platform SDK identifier (no version).
4561
+ registerVersion('fire-js', '');
4562
+ }
4563
+
4564
+ /**
4565
+ * Firebase App
4566
+ *
4567
+ * @remarks This package coordinates the communication between the different Firebase components
4568
+ * @packageDocumentation
4569
+ */
4570
+ registerCoreComponents('');
4571
+
4572
+ var name$2 = "firebase";
4573
+ var version$2 = "12.4.0";
4574
+
4575
+ /**
4576
+ * @license
4577
+ * Copyright 2020 Google LLC
4578
+ *
4579
+ * Licensed under the Apache License, Version 2.0 (the "License");
4580
+ * you may not use this file except in compliance with the License.
4581
+ * You may obtain a copy of the License at
4582
+ *
4583
+ * http://www.apache.org/licenses/LICENSE-2.0
4584
+ *
4585
+ * Unless required by applicable law or agreed to in writing, software
4586
+ * distributed under the License is distributed on an "AS IS" BASIS,
4587
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4588
+ * See the License for the specific language governing permissions and
4589
+ * limitations under the License.
4590
+ */
4591
+ registerVersion(name$2, version$2, 'app');
4592
+
4593
+ const name$1 = "@firebase/installations";
4594
+ const version$1 = "0.6.19";
4595
+
4596
+ /**
4597
+ * @license
4598
+ * Copyright 2019 Google LLC
4599
+ *
4600
+ * Licensed under the Apache License, Version 2.0 (the "License");
4601
+ * you may not use this file except in compliance with the License.
4602
+ * You may obtain a copy of the License at
4603
+ *
4604
+ * http://www.apache.org/licenses/LICENSE-2.0
4605
+ *
4606
+ * Unless required by applicable law or agreed to in writing, software
4607
+ * distributed under the License is distributed on an "AS IS" BASIS,
4608
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4609
+ * See the License for the specific language governing permissions and
4610
+ * limitations under the License.
4611
+ */
4612
+ const PENDING_TIMEOUT_MS = 10000;
4613
+ const PACKAGE_VERSION = `w:${version$1}`;
4614
+ const INTERNAL_AUTH_VERSION = 'FIS_v2';
4615
+ const INSTALLATIONS_API_URL = 'https://firebaseinstallations.googleapis.com/v1';
4616
+ const TOKEN_EXPIRATION_BUFFER = 60 * 60 * 1000; // One hour
4617
+ const SERVICE = 'installations';
4618
+ const SERVICE_NAME = 'Installations';
4619
+
4620
+ /**
4621
+ * @license
4622
+ * Copyright 2019 Google LLC
4623
+ *
4624
+ * Licensed under the Apache License, Version 2.0 (the "License");
4625
+ * you may not use this file except in compliance with the License.
4626
+ * You may obtain a copy of the License at
4627
+ *
4628
+ * http://www.apache.org/licenses/LICENSE-2.0
4629
+ *
4630
+ * Unless required by applicable law or agreed to in writing, software
4631
+ * distributed under the License is distributed on an "AS IS" BASIS,
4632
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4633
+ * See the License for the specific language governing permissions and
4634
+ * limitations under the License.
4635
+ */
4636
+ const ERROR_DESCRIPTION_MAP = {
4637
+ ["missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */]: 'Missing App configuration value: "{$valueName}"',
4638
+ ["not-registered" /* ErrorCode.NOT_REGISTERED */]: 'Firebase Installation is not registered.',
4639
+ ["installation-not-found" /* ErrorCode.INSTALLATION_NOT_FOUND */]: 'Firebase Installation not found.',
4640
+ ["request-failed" /* ErrorCode.REQUEST_FAILED */]: '{$requestName} request failed with error "{$serverCode} {$serverStatus}: {$serverMessage}"',
4641
+ ["app-offline" /* ErrorCode.APP_OFFLINE */]: 'Could not process request. Application offline.',
4642
+ ["delete-pending-registration" /* ErrorCode.DELETE_PENDING_REGISTRATION */]: "Can't delete installation while there is a pending registration request."
4643
+ };
4644
+ const ERROR_FACTORY$1 = new ErrorFactory(SERVICE, SERVICE_NAME, ERROR_DESCRIPTION_MAP);
4645
+ /** Returns true if error is a FirebaseError that is based on an error from the server. */
4646
+ function isServerError(error) {
4647
+ return (error instanceof FirebaseError &&
4648
+ error.code.includes("request-failed" /* ErrorCode.REQUEST_FAILED */));
4649
+ }
4650
+
4651
+ /**
4652
+ * @license
4653
+ * Copyright 2019 Google LLC
4654
+ *
4655
+ * Licensed under the Apache License, Version 2.0 (the "License");
4656
+ * you may not use this file except in compliance with the License.
4657
+ * You may obtain a copy of the License at
4658
+ *
4659
+ * http://www.apache.org/licenses/LICENSE-2.0
4660
+ *
4661
+ * Unless required by applicable law or agreed to in writing, software
4662
+ * distributed under the License is distributed on an "AS IS" BASIS,
4663
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4664
+ * See the License for the specific language governing permissions and
4665
+ * limitations under the License.
4666
+ */
4667
+ function getInstallationsEndpoint({ projectId }) {
4668
+ return `${INSTALLATIONS_API_URL}/projects/${projectId}/installations`;
4669
+ }
4670
+ function extractAuthTokenInfoFromResponse(response) {
4671
+ return {
4672
+ token: response.token,
4673
+ requestStatus: 2 /* RequestStatus.COMPLETED */,
4674
+ expiresIn: getExpiresInFromResponseExpiresIn(response.expiresIn),
4675
+ creationTime: Date.now()
4676
+ };
4677
+ }
4678
+ async function getErrorFromResponse(requestName, response) {
4679
+ const responseJson = await response.json();
4680
+ const errorData = responseJson.error;
4681
+ return ERROR_FACTORY$1.create("request-failed" /* ErrorCode.REQUEST_FAILED */, {
4682
+ requestName,
4683
+ serverCode: errorData.code,
4684
+ serverMessage: errorData.message,
4685
+ serverStatus: errorData.status
4686
+ });
4687
+ }
4688
+ function getHeaders$1({ apiKey }) {
4689
+ return new Headers({
4690
+ 'Content-Type': 'application/json',
4691
+ Accept: 'application/json',
4692
+ 'x-goog-api-key': apiKey
4693
+ });
4694
+ }
4695
+ function getHeadersWithAuth(appConfig, { refreshToken }) {
4696
+ const headers = getHeaders$1(appConfig);
4697
+ headers.append('Authorization', getAuthorizationHeader(refreshToken));
4698
+ return headers;
4699
+ }
4700
+ /**
4701
+ * Calls the passed in fetch wrapper and returns the response.
4702
+ * If the returned response has a status of 5xx, re-runs the function once and
4703
+ * returns the response.
4704
+ */
4705
+ async function retryIfServerError(fn) {
4706
+ const result = await fn();
4707
+ if (result.status >= 500 && result.status < 600) {
4708
+ // Internal Server Error. Retry request.
4709
+ return fn();
4710
+ }
4711
+ return result;
4712
+ }
4713
+ function getExpiresInFromResponseExpiresIn(responseExpiresIn) {
4714
+ // This works because the server will never respond with fractions of a second.
4715
+ return Number(responseExpiresIn.replace('s', '000'));
4716
+ }
4717
+ function getAuthorizationHeader(refreshToken) {
4718
+ return `${INTERNAL_AUTH_VERSION} ${refreshToken}`;
4719
+ }
4720
+
4721
+ /**
4722
+ * @license
4723
+ * Copyright 2019 Google LLC
4724
+ *
4725
+ * Licensed under the Apache License, Version 2.0 (the "License");
4726
+ * you may not use this file except in compliance with the License.
4727
+ * You may obtain a copy of the License at
4728
+ *
4729
+ * http://www.apache.org/licenses/LICENSE-2.0
4730
+ *
4731
+ * Unless required by applicable law or agreed to in writing, software
4732
+ * distributed under the License is distributed on an "AS IS" BASIS,
4733
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4734
+ * See the License for the specific language governing permissions and
4735
+ * limitations under the License.
4736
+ */
4737
+ async function createInstallationRequest({ appConfig, heartbeatServiceProvider }, { fid }) {
4738
+ const endpoint = getInstallationsEndpoint(appConfig);
4739
+ const headers = getHeaders$1(appConfig);
4740
+ // If heartbeat service exists, add the heartbeat string to the header.
4741
+ const heartbeatService = heartbeatServiceProvider.getImmediate({
4742
+ optional: true
4743
+ });
4744
+ if (heartbeatService) {
4745
+ const heartbeatsHeader = await heartbeatService.getHeartbeatsHeader();
4746
+ if (heartbeatsHeader) {
4747
+ headers.append('x-firebase-client', heartbeatsHeader);
4748
+ }
4749
+ }
4750
+ const body = {
4751
+ fid,
4752
+ authVersion: INTERNAL_AUTH_VERSION,
4753
+ appId: appConfig.appId,
4754
+ sdkVersion: PACKAGE_VERSION
4755
+ };
4756
+ const request = {
4757
+ method: 'POST',
4758
+ headers,
4759
+ body: JSON.stringify(body)
4760
+ };
4761
+ const response = await retryIfServerError(() => fetch(endpoint, request));
4762
+ if (response.ok) {
4763
+ const responseValue = await response.json();
4764
+ const registeredInstallationEntry = {
4765
+ fid: responseValue.fid || fid,
4766
+ registrationStatus: 2 /* RequestStatus.COMPLETED */,
4767
+ refreshToken: responseValue.refreshToken,
4768
+ authToken: extractAuthTokenInfoFromResponse(responseValue.authToken)
4769
+ };
4770
+ return registeredInstallationEntry;
4771
+ }
4772
+ else {
4773
+ throw await getErrorFromResponse('Create Installation', response);
4774
+ }
4775
+ }
4776
+
4777
+ /**
4778
+ * @license
4779
+ * Copyright 2019 Google LLC
4780
+ *
4781
+ * Licensed under the Apache License, Version 2.0 (the "License");
4782
+ * you may not use this file except in compliance with the License.
4783
+ * You may obtain a copy of the License at
4784
+ *
4785
+ * http://www.apache.org/licenses/LICENSE-2.0
4786
+ *
4787
+ * Unless required by applicable law or agreed to in writing, software
4788
+ * distributed under the License is distributed on an "AS IS" BASIS,
4789
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4790
+ * See the License for the specific language governing permissions and
4791
+ * limitations under the License.
4792
+ */
4793
+ /** Returns a promise that resolves after given time passes. */
4794
+ function sleep(ms) {
4795
+ return new Promise(resolve => {
4796
+ setTimeout(resolve, ms);
4797
+ });
4798
+ }
4799
+
4800
+ /**
4801
+ * @license
4802
+ * Copyright 2019 Google LLC
4803
+ *
4804
+ * Licensed under the Apache License, Version 2.0 (the "License");
4805
+ * you may not use this file except in compliance with the License.
4806
+ * You may obtain a copy of the License at
4807
+ *
4808
+ * http://www.apache.org/licenses/LICENSE-2.0
4809
+ *
4810
+ * Unless required by applicable law or agreed to in writing, software
4811
+ * distributed under the License is distributed on an "AS IS" BASIS,
4812
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4813
+ * See the License for the specific language governing permissions and
4814
+ * limitations under the License.
4815
+ */
4816
+ function bufferToBase64UrlSafe(array) {
4817
+ const b64 = btoa(String.fromCharCode(...array));
4818
+ return b64.replace(/\+/g, '-').replace(/\//g, '_');
4819
+ }
4820
+
4821
+ /**
4822
+ * @license
4823
+ * Copyright 2019 Google LLC
4824
+ *
4825
+ * Licensed under the Apache License, Version 2.0 (the "License");
4826
+ * you may not use this file except in compliance with the License.
4827
+ * You may obtain a copy of the License at
4828
+ *
4829
+ * http://www.apache.org/licenses/LICENSE-2.0
4830
+ *
4831
+ * Unless required by applicable law or agreed to in writing, software
4832
+ * distributed under the License is distributed on an "AS IS" BASIS,
4833
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4834
+ * See the License for the specific language governing permissions and
4835
+ * limitations under the License.
4836
+ */
4837
+ const VALID_FID_PATTERN = /^[cdef][\w-]{21}$/;
4838
+ const INVALID_FID = '';
4839
+ /**
4840
+ * Generates a new FID using random values from Web Crypto API.
4841
+ * Returns an empty string if FID generation fails for any reason.
4842
+ */
4843
+ function generateFid() {
4844
+ try {
4845
+ // A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
4846
+ // bytes. our implementation generates a 17 byte array instead.
4847
+ const fidByteArray = new Uint8Array(17);
4848
+ const crypto = self.crypto || self.msCrypto;
4849
+ crypto.getRandomValues(fidByteArray);
4850
+ // Replace the first 4 random bits with the constant FID header of 0b0111.
4851
+ fidByteArray[0] = 0b01110000 + (fidByteArray[0] % 0b00010000);
4852
+ const fid = encode(fidByteArray);
4853
+ return VALID_FID_PATTERN.test(fid) ? fid : INVALID_FID;
4854
+ }
4855
+ catch {
4856
+ // FID generation errored
4857
+ return INVALID_FID;
4858
+ }
4859
+ }
4860
+ /** Converts a FID Uint8Array to a base64 string representation. */
4861
+ function encode(fidByteArray) {
4862
+ const b64String = bufferToBase64UrlSafe(fidByteArray);
4863
+ // Remove the 23rd character that was added because of the extra 4 bits at the
4864
+ // end of our 17 byte array, and the '=' padding.
4865
+ return b64String.substr(0, 22);
4866
+ }
4867
+
4868
+ /**
4869
+ * @license
4870
+ * Copyright 2019 Google LLC
4871
+ *
4872
+ * Licensed under the Apache License, Version 2.0 (the "License");
4873
+ * you may not use this file except in compliance with the License.
4874
+ * You may obtain a copy of the License at
4875
+ *
4876
+ * http://www.apache.org/licenses/LICENSE-2.0
4877
+ *
4878
+ * Unless required by applicable law or agreed to in writing, software
4879
+ * distributed under the License is distributed on an "AS IS" BASIS,
4880
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4881
+ * See the License for the specific language governing permissions and
4882
+ * limitations under the License.
4883
+ */
4884
+ /** Returns a string key that can be used to identify the app. */
4885
+ function getKey$1(appConfig) {
4886
+ return `${appConfig.appName}!${appConfig.appId}`;
4887
+ }
4888
+
4889
+ /**
4890
+ * @license
4891
+ * Copyright 2019 Google LLC
4892
+ *
4893
+ * Licensed under the Apache License, Version 2.0 (the "License");
4894
+ * you may not use this file except in compliance with the License.
4895
+ * You may obtain a copy of the License at
4896
+ *
4897
+ * http://www.apache.org/licenses/LICENSE-2.0
4898
+ *
4899
+ * Unless required by applicable law or agreed to in writing, software
4900
+ * distributed under the License is distributed on an "AS IS" BASIS,
4901
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4902
+ * See the License for the specific language governing permissions and
4903
+ * limitations under the License.
4904
+ */
4905
+ const fidChangeCallbacks = new Map();
4906
+ /**
4907
+ * Calls the onIdChange callbacks with the new FID value, and broadcasts the
4908
+ * change to other tabs.
4909
+ */
4910
+ function fidChanged(appConfig, fid) {
4911
+ const key = getKey$1(appConfig);
4912
+ callFidChangeCallbacks(key, fid);
4913
+ broadcastFidChange(key, fid);
4914
+ }
4915
+ function callFidChangeCallbacks(key, fid) {
4916
+ const callbacks = fidChangeCallbacks.get(key);
4917
+ if (!callbacks) {
4918
+ return;
4919
+ }
4920
+ for (const callback of callbacks) {
4921
+ callback(fid);
4922
+ }
4923
+ }
4924
+ function broadcastFidChange(key, fid) {
4925
+ const channel = getBroadcastChannel();
4926
+ if (channel) {
4927
+ channel.postMessage({ key, fid });
4928
+ }
4929
+ closeBroadcastChannel();
4930
+ }
4931
+ let broadcastChannel = null;
4932
+ /** Opens and returns a BroadcastChannel if it is supported by the browser. */
4933
+ function getBroadcastChannel() {
4934
+ if (!broadcastChannel && 'BroadcastChannel' in self) {
4935
+ broadcastChannel = new BroadcastChannel('[Firebase] FID Change');
4936
+ broadcastChannel.onmessage = e => {
4937
+ callFidChangeCallbacks(e.data.key, e.data.fid);
4938
+ };
4939
+ }
4940
+ return broadcastChannel;
4941
+ }
4942
+ function closeBroadcastChannel() {
4943
+ if (fidChangeCallbacks.size === 0 && broadcastChannel) {
4944
+ broadcastChannel.close();
4945
+ broadcastChannel = null;
4946
+ }
4947
+ }
4948
+
4949
+ /**
4950
+ * @license
4951
+ * Copyright 2019 Google LLC
4952
+ *
4953
+ * Licensed under the Apache License, Version 2.0 (the "License");
4954
+ * you may not use this file except in compliance with the License.
4955
+ * You may obtain a copy of the License at
4956
+ *
4957
+ * http://www.apache.org/licenses/LICENSE-2.0
4958
+ *
4959
+ * Unless required by applicable law or agreed to in writing, software
4960
+ * distributed under the License is distributed on an "AS IS" BASIS,
4961
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4962
+ * See the License for the specific language governing permissions and
4963
+ * limitations under the License.
4964
+ */
4965
+ const DATABASE_NAME$1 = 'firebase-installations-database';
4966
+ const DATABASE_VERSION$1 = 1;
4967
+ const OBJECT_STORE_NAME$1 = 'firebase-installations-store';
4968
+ let dbPromise$1 = null;
4969
+ function getDbPromise$1() {
4970
+ if (!dbPromise$1) {
4971
+ dbPromise$1 = openDB(DATABASE_NAME$1, DATABASE_VERSION$1, {
4972
+ upgrade: (db, oldVersion) => {
4973
+ // We don't use 'break' in this switch statement, the fall-through
4974
+ // behavior is what we want, because if there are multiple versions between
4975
+ // the old version and the current version, we want ALL the migrations
4976
+ // that correspond to those versions to run, not only the last one.
4977
+ // eslint-disable-next-line default-case
4978
+ switch (oldVersion) {
4979
+ case 0:
4980
+ db.createObjectStore(OBJECT_STORE_NAME$1);
4981
+ }
4982
+ }
4983
+ });
4984
+ }
4985
+ return dbPromise$1;
4986
+ }
4987
+ /** Assigns or overwrites the record for the given key with the given value. */
4988
+ async function set(appConfig, value) {
4989
+ const key = getKey$1(appConfig);
4990
+ const db = await getDbPromise$1();
4991
+ const tx = db.transaction(OBJECT_STORE_NAME$1, 'readwrite');
4992
+ const objectStore = tx.objectStore(OBJECT_STORE_NAME$1);
4993
+ const oldValue = (await objectStore.get(key));
4994
+ await objectStore.put(value, key);
4995
+ await tx.done;
4996
+ if (!oldValue || oldValue.fid !== value.fid) {
4997
+ fidChanged(appConfig, value.fid);
4998
+ }
4999
+ return value;
5000
+ }
5001
+ /** Removes record(s) from the objectStore that match the given key. */
5002
+ async function remove(appConfig) {
5003
+ const key = getKey$1(appConfig);
5004
+ const db = await getDbPromise$1();
5005
+ const tx = db.transaction(OBJECT_STORE_NAME$1, 'readwrite');
5006
+ await tx.objectStore(OBJECT_STORE_NAME$1).delete(key);
5007
+ await tx.done;
5008
+ }
5009
+ /**
5010
+ * Atomically updates a record with the result of updateFn, which gets
5011
+ * called with the current value. If newValue is undefined, the record is
5012
+ * deleted instead.
5013
+ * @return Updated value
5014
+ */
5015
+ async function update(appConfig, updateFn) {
5016
+ const key = getKey$1(appConfig);
5017
+ const db = await getDbPromise$1();
5018
+ const tx = db.transaction(OBJECT_STORE_NAME$1, 'readwrite');
5019
+ const store = tx.objectStore(OBJECT_STORE_NAME$1);
5020
+ const oldValue = (await store.get(key));
5021
+ const newValue = updateFn(oldValue);
5022
+ if (newValue === undefined) {
5023
+ await store.delete(key);
5024
+ }
5025
+ else {
5026
+ await store.put(newValue, key);
5027
+ }
5028
+ await tx.done;
5029
+ if (newValue && (!oldValue || oldValue.fid !== newValue.fid)) {
5030
+ fidChanged(appConfig, newValue.fid);
5031
+ }
5032
+ return newValue;
5033
+ }
5034
+
5035
+ /**
5036
+ * @license
5037
+ * Copyright 2019 Google LLC
5038
+ *
5039
+ * Licensed under the Apache License, Version 2.0 (the "License");
5040
+ * you may not use this file except in compliance with the License.
5041
+ * You may obtain a copy of the License at
5042
+ *
5043
+ * http://www.apache.org/licenses/LICENSE-2.0
5044
+ *
5045
+ * Unless required by applicable law or agreed to in writing, software
5046
+ * distributed under the License is distributed on an "AS IS" BASIS,
5047
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5048
+ * See the License for the specific language governing permissions and
5049
+ * limitations under the License.
5050
+ */
5051
+ /**
5052
+ * Updates and returns the InstallationEntry from the database.
5053
+ * Also triggers a registration request if it is necessary and possible.
5054
+ */
5055
+ async function getInstallationEntry(installations) {
5056
+ let registrationPromise;
5057
+ const installationEntry = await update(installations.appConfig, oldEntry => {
5058
+ const installationEntry = updateOrCreateInstallationEntry(oldEntry);
5059
+ const entryWithPromise = triggerRegistrationIfNecessary(installations, installationEntry);
5060
+ registrationPromise = entryWithPromise.registrationPromise;
5061
+ return entryWithPromise.installationEntry;
5062
+ });
5063
+ if (installationEntry.fid === INVALID_FID) {
5064
+ // FID generation failed. Waiting for the FID from the server.
5065
+ return { installationEntry: await registrationPromise };
5066
+ }
5067
+ return {
5068
+ installationEntry,
5069
+ registrationPromise
5070
+ };
5071
+ }
5072
+ /**
5073
+ * Creates a new Installation Entry if one does not exist.
5074
+ * Also clears timed out pending requests.
5075
+ */
5076
+ function updateOrCreateInstallationEntry(oldEntry) {
5077
+ const entry = oldEntry || {
5078
+ fid: generateFid(),
5079
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
5080
+ };
5081
+ return clearTimedOutRequest(entry);
5082
+ }
5083
+ /**
5084
+ * If the Firebase Installation is not registered yet, this will trigger the
5085
+ * registration and return an InProgressInstallationEntry.
5086
+ *
5087
+ * If registrationPromise does not exist, the installationEntry is guaranteed
5088
+ * to be registered.
5089
+ */
5090
+ function triggerRegistrationIfNecessary(installations, installationEntry) {
5091
+ if (installationEntry.registrationStatus === 0 /* RequestStatus.NOT_STARTED */) {
5092
+ if (!navigator.onLine) {
5093
+ // Registration required but app is offline.
5094
+ const registrationPromiseWithError = Promise.reject(ERROR_FACTORY$1.create("app-offline" /* ErrorCode.APP_OFFLINE */));
5095
+ return {
5096
+ installationEntry,
5097
+ registrationPromise: registrationPromiseWithError
5098
+ };
5099
+ }
5100
+ // Try registering. Change status to IN_PROGRESS.
5101
+ const inProgressEntry = {
5102
+ fid: installationEntry.fid,
5103
+ registrationStatus: 1 /* RequestStatus.IN_PROGRESS */,
5104
+ registrationTime: Date.now()
5105
+ };
5106
+ const registrationPromise = registerInstallation(installations, inProgressEntry);
5107
+ return { installationEntry: inProgressEntry, registrationPromise };
5108
+ }
5109
+ else if (installationEntry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */) {
5110
+ return {
5111
+ installationEntry,
5112
+ registrationPromise: waitUntilFidRegistration(installations)
5113
+ };
5114
+ }
5115
+ else {
5116
+ return { installationEntry };
5117
+ }
5118
+ }
5119
+ /** This will be executed only once for each new Firebase Installation. */
5120
+ async function registerInstallation(installations, installationEntry) {
5121
+ try {
5122
+ const registeredInstallationEntry = await createInstallationRequest(installations, installationEntry);
5123
+ return set(installations.appConfig, registeredInstallationEntry);
5124
+ }
5125
+ catch (e) {
5126
+ if (isServerError(e) && e.customData.serverCode === 409) {
5127
+ // Server returned a "FID cannot be used" error.
5128
+ // Generate a new ID next time.
5129
+ await remove(installations.appConfig);
5130
+ }
5131
+ else {
5132
+ // Registration failed. Set FID as not registered.
5133
+ await set(installations.appConfig, {
5134
+ fid: installationEntry.fid,
5135
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
5136
+ });
5137
+ }
5138
+ throw e;
5139
+ }
5140
+ }
5141
+ /** Call if FID registration is pending in another request. */
5142
+ async function waitUntilFidRegistration(installations) {
5143
+ // Unfortunately, there is no way of reliably observing when a value in
5144
+ // IndexedDB changes (yet, see https://github.com/WICG/indexed-db-observers),
5145
+ // so we need to poll.
5146
+ let entry = await updateInstallationRequest(installations.appConfig);
5147
+ while (entry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */) {
5148
+ // createInstallation request still in progress.
5149
+ await sleep(100);
5150
+ entry = await updateInstallationRequest(installations.appConfig);
5151
+ }
5152
+ if (entry.registrationStatus === 0 /* RequestStatus.NOT_STARTED */) {
5153
+ // The request timed out or failed in a different call. Try again.
5154
+ const { installationEntry, registrationPromise } = await getInstallationEntry(installations);
5155
+ if (registrationPromise) {
5156
+ return registrationPromise;
5157
+ }
5158
+ else {
5159
+ // if there is no registrationPromise, entry is registered.
5160
+ return installationEntry;
5161
+ }
5162
+ }
5163
+ return entry;
5164
+ }
5165
+ /**
5166
+ * Called only if there is a CreateInstallation request in progress.
5167
+ *
5168
+ * Updates the InstallationEntry in the DB based on the status of the
5169
+ * CreateInstallation request.
5170
+ *
5171
+ * Returns the updated InstallationEntry.
5172
+ */
5173
+ function updateInstallationRequest(appConfig) {
5174
+ return update(appConfig, oldEntry => {
5175
+ if (!oldEntry) {
5176
+ throw ERROR_FACTORY$1.create("installation-not-found" /* ErrorCode.INSTALLATION_NOT_FOUND */);
5177
+ }
5178
+ return clearTimedOutRequest(oldEntry);
5179
+ });
5180
+ }
5181
+ function clearTimedOutRequest(entry) {
5182
+ if (hasInstallationRequestTimedOut(entry)) {
5183
+ return {
5184
+ fid: entry.fid,
5185
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
5186
+ };
5187
+ }
5188
+ return entry;
5189
+ }
5190
+ function hasInstallationRequestTimedOut(installationEntry) {
5191
+ return (installationEntry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */ &&
5192
+ installationEntry.registrationTime + PENDING_TIMEOUT_MS < Date.now());
5193
+ }
5194
+
5195
+ /**
5196
+ * @license
5197
+ * Copyright 2019 Google LLC
5198
+ *
5199
+ * Licensed under the Apache License, Version 2.0 (the "License");
5200
+ * you may not use this file except in compliance with the License.
5201
+ * You may obtain a copy of the License at
5202
+ *
5203
+ * http://www.apache.org/licenses/LICENSE-2.0
5204
+ *
5205
+ * Unless required by applicable law or agreed to in writing, software
5206
+ * distributed under the License is distributed on an "AS IS" BASIS,
5207
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5208
+ * See the License for the specific language governing permissions and
5209
+ * limitations under the License.
5210
+ */
5211
+ async function generateAuthTokenRequest({ appConfig, heartbeatServiceProvider }, installationEntry) {
5212
+ const endpoint = getGenerateAuthTokenEndpoint(appConfig, installationEntry);
5213
+ const headers = getHeadersWithAuth(appConfig, installationEntry);
5214
+ // If heartbeat service exists, add the heartbeat string to the header.
5215
+ const heartbeatService = heartbeatServiceProvider.getImmediate({
5216
+ optional: true
5217
+ });
5218
+ if (heartbeatService) {
5219
+ const heartbeatsHeader = await heartbeatService.getHeartbeatsHeader();
5220
+ if (heartbeatsHeader) {
5221
+ headers.append('x-firebase-client', heartbeatsHeader);
5222
+ }
5223
+ }
5224
+ const body = {
5225
+ installation: {
5226
+ sdkVersion: PACKAGE_VERSION,
5227
+ appId: appConfig.appId
5228
+ }
5229
+ };
5230
+ const request = {
5231
+ method: 'POST',
5232
+ headers,
5233
+ body: JSON.stringify(body)
5234
+ };
5235
+ const response = await retryIfServerError(() => fetch(endpoint, request));
5236
+ if (response.ok) {
5237
+ const responseValue = await response.json();
5238
+ const completedAuthToken = extractAuthTokenInfoFromResponse(responseValue);
5239
+ return completedAuthToken;
5240
+ }
5241
+ else {
5242
+ throw await getErrorFromResponse('Generate Auth Token', response);
5243
+ }
5244
+ }
5245
+ function getGenerateAuthTokenEndpoint(appConfig, { fid }) {
5246
+ return `${getInstallationsEndpoint(appConfig)}/${fid}/authTokens:generate`;
5247
+ }
5248
+
5249
+ /**
5250
+ * @license
5251
+ * Copyright 2019 Google LLC
5252
+ *
5253
+ * Licensed under the Apache License, Version 2.0 (the "License");
5254
+ * you may not use this file except in compliance with the License.
5255
+ * You may obtain a copy of the License at
5256
+ *
5257
+ * http://www.apache.org/licenses/LICENSE-2.0
5258
+ *
5259
+ * Unless required by applicable law or agreed to in writing, software
5260
+ * distributed under the License is distributed on an "AS IS" BASIS,
5261
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5262
+ * See the License for the specific language governing permissions and
5263
+ * limitations under the License.
5264
+ */
5265
+ /**
5266
+ * Returns a valid authentication token for the installation. Generates a new
5267
+ * token if one doesn't exist, is expired or about to expire.
5268
+ *
5269
+ * Should only be called if the Firebase Installation is registered.
5270
+ */
5271
+ async function refreshAuthToken(installations, forceRefresh = false) {
5272
+ let tokenPromise;
5273
+ const entry = await update(installations.appConfig, oldEntry => {
5274
+ if (!isEntryRegistered(oldEntry)) {
5275
+ throw ERROR_FACTORY$1.create("not-registered" /* ErrorCode.NOT_REGISTERED */);
5276
+ }
5277
+ const oldAuthToken = oldEntry.authToken;
5278
+ if (!forceRefresh && isAuthTokenValid(oldAuthToken)) {
5279
+ // There is a valid token in the DB.
5280
+ return oldEntry;
5281
+ }
5282
+ else if (oldAuthToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */) {
5283
+ // There already is a token request in progress.
5284
+ tokenPromise = waitUntilAuthTokenRequest(installations, forceRefresh);
5285
+ return oldEntry;
5286
+ }
5287
+ else {
5288
+ // No token or token expired.
5289
+ if (!navigator.onLine) {
5290
+ throw ERROR_FACTORY$1.create("app-offline" /* ErrorCode.APP_OFFLINE */);
5291
+ }
5292
+ const inProgressEntry = makeAuthTokenRequestInProgressEntry(oldEntry);
5293
+ tokenPromise = fetchAuthTokenFromServer(installations, inProgressEntry);
5294
+ return inProgressEntry;
5295
+ }
5296
+ });
5297
+ const authToken = tokenPromise
5298
+ ? await tokenPromise
5299
+ : entry.authToken;
5300
+ return authToken;
5301
+ }
5302
+ /**
5303
+ * Call only if FID is registered and Auth Token request is in progress.
5304
+ *
5305
+ * Waits until the current pending request finishes. If the request times out,
5306
+ * tries once in this thread as well.
5307
+ */
5308
+ async function waitUntilAuthTokenRequest(installations, forceRefresh) {
5309
+ // Unfortunately, there is no way of reliably observing when a value in
5310
+ // IndexedDB changes (yet, see https://github.com/WICG/indexed-db-observers),
5311
+ // so we need to poll.
5312
+ let entry = await updateAuthTokenRequest(installations.appConfig);
5313
+ while (entry.authToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */) {
5314
+ // generateAuthToken still in progress.
5315
+ await sleep(100);
5316
+ entry = await updateAuthTokenRequest(installations.appConfig);
5317
+ }
5318
+ const authToken = entry.authToken;
5319
+ if (authToken.requestStatus === 0 /* RequestStatus.NOT_STARTED */) {
5320
+ // The request timed out or failed in a different call. Try again.
5321
+ return refreshAuthToken(installations, forceRefresh);
5322
+ }
5323
+ else {
5324
+ return authToken;
5325
+ }
5326
+ }
5327
+ /**
5328
+ * Called only if there is a GenerateAuthToken request in progress.
5329
+ *
5330
+ * Updates the InstallationEntry in the DB based on the status of the
5331
+ * GenerateAuthToken request.
5332
+ *
5333
+ * Returns the updated InstallationEntry.
5334
+ */
5335
+ function updateAuthTokenRequest(appConfig) {
5336
+ return update(appConfig, oldEntry => {
5337
+ if (!isEntryRegistered(oldEntry)) {
5338
+ throw ERROR_FACTORY$1.create("not-registered" /* ErrorCode.NOT_REGISTERED */);
5339
+ }
5340
+ const oldAuthToken = oldEntry.authToken;
5341
+ if (hasAuthTokenRequestTimedOut(oldAuthToken)) {
5342
+ return {
5343
+ ...oldEntry,
5344
+ authToken: { requestStatus: 0 /* RequestStatus.NOT_STARTED */ }
5345
+ };
5346
+ }
5347
+ return oldEntry;
5348
+ });
5349
+ }
5350
+ async function fetchAuthTokenFromServer(installations, installationEntry) {
5351
+ try {
5352
+ const authToken = await generateAuthTokenRequest(installations, installationEntry);
5353
+ const updatedInstallationEntry = {
5354
+ ...installationEntry,
5355
+ authToken
5356
+ };
5357
+ await set(installations.appConfig, updatedInstallationEntry);
5358
+ return authToken;
5359
+ }
5360
+ catch (e) {
5361
+ if (isServerError(e) &&
5362
+ (e.customData.serverCode === 401 || e.customData.serverCode === 404)) {
5363
+ // Server returned a "FID not found" or a "Invalid authentication" error.
5364
+ // Generate a new ID next time.
5365
+ await remove(installations.appConfig);
5366
+ }
5367
+ else {
5368
+ const updatedInstallationEntry = {
5369
+ ...installationEntry,
5370
+ authToken: { requestStatus: 0 /* RequestStatus.NOT_STARTED */ }
5371
+ };
5372
+ await set(installations.appConfig, updatedInstallationEntry);
5373
+ }
5374
+ throw e;
5375
+ }
5376
+ }
5377
+ function isEntryRegistered(installationEntry) {
5378
+ return (installationEntry !== undefined &&
5379
+ installationEntry.registrationStatus === 2 /* RequestStatus.COMPLETED */);
5380
+ }
5381
+ function isAuthTokenValid(authToken) {
5382
+ return (authToken.requestStatus === 2 /* RequestStatus.COMPLETED */ &&
5383
+ !isAuthTokenExpired(authToken));
5384
+ }
5385
+ function isAuthTokenExpired(authToken) {
5386
+ const now = Date.now();
5387
+ return (now < authToken.creationTime ||
5388
+ authToken.creationTime + authToken.expiresIn < now + TOKEN_EXPIRATION_BUFFER);
5389
+ }
5390
+ /** Returns an updated InstallationEntry with an InProgressAuthToken. */
5391
+ function makeAuthTokenRequestInProgressEntry(oldEntry) {
5392
+ const inProgressAuthToken = {
5393
+ requestStatus: 1 /* RequestStatus.IN_PROGRESS */,
5394
+ requestTime: Date.now()
5395
+ };
5396
+ return {
5397
+ ...oldEntry,
5398
+ authToken: inProgressAuthToken
5399
+ };
5400
+ }
5401
+ function hasAuthTokenRequestTimedOut(authToken) {
5402
+ return (authToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */ &&
5403
+ authToken.requestTime + PENDING_TIMEOUT_MS < Date.now());
5404
+ }
5405
+
5406
+ /**
5407
+ * @license
5408
+ * Copyright 2019 Google LLC
5409
+ *
5410
+ * Licensed under the Apache License, Version 2.0 (the "License");
5411
+ * you may not use this file except in compliance with the License.
5412
+ * You may obtain a copy of the License at
5413
+ *
5414
+ * http://www.apache.org/licenses/LICENSE-2.0
5415
+ *
5416
+ * Unless required by applicable law or agreed to in writing, software
5417
+ * distributed under the License is distributed on an "AS IS" BASIS,
5418
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5419
+ * See the License for the specific language governing permissions and
5420
+ * limitations under the License.
5421
+ */
5422
+ /**
5423
+ * Creates a Firebase Installation if there isn't one for the app and
5424
+ * returns the Installation ID.
5425
+ * @param installations - The `Installations` instance.
5426
+ *
5427
+ * @public
5428
+ */
5429
+ async function getId(installations) {
5430
+ const installationsImpl = installations;
5431
+ const { installationEntry, registrationPromise } = await getInstallationEntry(installationsImpl);
5432
+ if (registrationPromise) {
5433
+ registrationPromise.catch(console.error);
5434
+ }
5435
+ else {
5436
+ // If the installation is already registered, update the authentication
5437
+ // token if needed.
5438
+ refreshAuthToken(installationsImpl).catch(console.error);
5439
+ }
5440
+ return installationEntry.fid;
5441
+ }
5442
+
5443
+ /**
5444
+ * @license
5445
+ * Copyright 2019 Google LLC
5446
+ *
5447
+ * Licensed under the Apache License, Version 2.0 (the "License");
5448
+ * you may not use this file except in compliance with the License.
5449
+ * You may obtain a copy of the License at
5450
+ *
5451
+ * http://www.apache.org/licenses/LICENSE-2.0
5452
+ *
5453
+ * Unless required by applicable law or agreed to in writing, software
5454
+ * distributed under the License is distributed on an "AS IS" BASIS,
5455
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5456
+ * See the License for the specific language governing permissions and
5457
+ * limitations under the License.
5458
+ */
5459
+ /**
5460
+ * Returns a Firebase Installations auth token, identifying the current
5461
+ * Firebase Installation.
5462
+ * @param installations - The `Installations` instance.
5463
+ * @param forceRefresh - Force refresh regardless of token expiration.
5464
+ *
5465
+ * @public
5466
+ */
5467
+ async function getToken$2(installations, forceRefresh = false) {
5468
+ const installationsImpl = installations;
5469
+ await completeInstallationRegistration(installationsImpl);
5470
+ // At this point we either have a Registered Installation in the DB, or we've
5471
+ // already thrown an error.
5472
+ const authToken = await refreshAuthToken(installationsImpl, forceRefresh);
5473
+ return authToken.token;
5474
+ }
5475
+ async function completeInstallationRegistration(installations) {
5476
+ const { registrationPromise } = await getInstallationEntry(installations);
5477
+ if (registrationPromise) {
5478
+ // A createInstallation request is in progress. Wait until it finishes.
5479
+ await registrationPromise;
5480
+ }
5481
+ }
5482
+
5483
+ /**
5484
+ * @license
5485
+ * Copyright 2019 Google LLC
5486
+ *
5487
+ * Licensed under the Apache License, Version 2.0 (the "License");
5488
+ * you may not use this file except in compliance with the License.
5489
+ * You may obtain a copy of the License at
5490
+ *
5491
+ * http://www.apache.org/licenses/LICENSE-2.0
5492
+ *
5493
+ * Unless required by applicable law or agreed to in writing, software
5494
+ * distributed under the License is distributed on an "AS IS" BASIS,
5495
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5496
+ * See the License for the specific language governing permissions and
5497
+ * limitations under the License.
5498
+ */
5499
+ function extractAppConfig$1(app) {
5500
+ if (!app || !app.options) {
5501
+ throw getMissingValueError$1('App Configuration');
5502
+ }
5503
+ if (!app.name) {
5504
+ throw getMissingValueError$1('App Name');
5505
+ }
5506
+ // Required app config keys
5507
+ const configKeys = [
5508
+ 'projectId',
5509
+ 'apiKey',
5510
+ 'appId'
5511
+ ];
5512
+ for (const keyName of configKeys) {
5513
+ if (!app.options[keyName]) {
5514
+ throw getMissingValueError$1(keyName);
5515
+ }
5516
+ }
5517
+ return {
5518
+ appName: app.name,
5519
+ projectId: app.options.projectId,
5520
+ apiKey: app.options.apiKey,
5521
+ appId: app.options.appId
5522
+ };
5523
+ }
5524
+ function getMissingValueError$1(valueName) {
5525
+ return ERROR_FACTORY$1.create("missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */, {
5526
+ valueName
5527
+ });
5528
+ }
5529
+
5530
+ /**
5531
+ * @license
5532
+ * Copyright 2020 Google LLC
5533
+ *
5534
+ * Licensed under the Apache License, Version 2.0 (the "License");
5535
+ * you may not use this file except in compliance with the License.
5536
+ * You may obtain a copy of the License at
5537
+ *
5538
+ * http://www.apache.org/licenses/LICENSE-2.0
5539
+ *
5540
+ * Unless required by applicable law or agreed to in writing, software
5541
+ * distributed under the License is distributed on an "AS IS" BASIS,
5542
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5543
+ * See the License for the specific language governing permissions and
5544
+ * limitations under the License.
5545
+ */
5546
+ const INSTALLATIONS_NAME = 'installations';
5547
+ const INSTALLATIONS_NAME_INTERNAL = 'installations-internal';
5548
+ const publicFactory = (container) => {
5549
+ const app = container.getProvider('app').getImmediate();
5550
+ // Throws if app isn't configured properly.
5551
+ const appConfig = extractAppConfig$1(app);
5552
+ const heartbeatServiceProvider = _getProvider(app, 'heartbeat');
5553
+ const installationsImpl = {
5554
+ app,
5555
+ appConfig,
5556
+ heartbeatServiceProvider,
5557
+ _delete: () => Promise.resolve()
5558
+ };
5559
+ return installationsImpl;
5560
+ };
5561
+ const internalFactory = (container) => {
5562
+ const app = container.getProvider('app').getImmediate();
5563
+ // Internal FIS instance relies on public FIS instance.
5564
+ const installations = _getProvider(app, INSTALLATIONS_NAME).getImmediate();
5565
+ const installationsInternal = {
5566
+ getId: () => getId(installations),
5567
+ getToken: (forceRefresh) => getToken$2(installations, forceRefresh)
5568
+ };
5569
+ return installationsInternal;
5570
+ };
5571
+ function registerInstallations() {
5572
+ _registerComponent(new Component(INSTALLATIONS_NAME, publicFactory, "PUBLIC" /* ComponentType.PUBLIC */));
5573
+ _registerComponent(new Component(INSTALLATIONS_NAME_INTERNAL, internalFactory, "PRIVATE" /* ComponentType.PRIVATE */));
5574
+ }
5575
+
5576
+ /**
5577
+ * The Firebase Installations Web SDK.
5578
+ * This SDK does not work in a Node.js environment.
5579
+ *
5580
+ * @packageDocumentation
5581
+ */
5582
+ registerInstallations();
5583
+ registerVersion(name$1, version$1);
5584
+ // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation
5585
+ registerVersion(name$1, version$1, 'esm2020');
5586
+
5587
+ /**
5588
+ * @license
5589
+ * Copyright 2019 Google LLC
5590
+ *
5591
+ * Licensed under the Apache License, Version 2.0 (the "License");
5592
+ * you may not use this file except in compliance with the License.
5593
+ * You may obtain a copy of the License at
5594
+ *
5595
+ * http://www.apache.org/licenses/LICENSE-2.0
5596
+ *
5597
+ * Unless required by applicable law or agreed to in writing, software
5598
+ * distributed under the License is distributed on an "AS IS" BASIS,
5599
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5600
+ * See the License for the specific language governing permissions and
5601
+ * limitations under the License.
5602
+ */
5603
+ const DEFAULT_SW_PATH = '/firebase-messaging-sw.js';
5604
+ const DEFAULT_SW_SCOPE = '/firebase-cloud-messaging-push-scope';
5605
+ const DEFAULT_VAPID_KEY = 'BDOU99-h67HcA6JeFXHbSNMu7e2yNNu3RzoMj8TM4W88jITfq7ZmPvIM1Iv-4_l2LxQcYwhqby2xGpWwzjfAnG4';
5606
+ const ENDPOINT = 'https://fcmregistrations.googleapis.com/v1';
5607
+ const CONSOLE_CAMPAIGN_ID = 'google.c.a.c_id';
5608
+ const CONSOLE_CAMPAIGN_NAME = 'google.c.a.c_l';
5609
+ const CONSOLE_CAMPAIGN_TIME = 'google.c.a.ts';
5610
+ /** Set to '1' if Analytics is enabled for the campaign */
5611
+ const CONSOLE_CAMPAIGN_ANALYTICS_ENABLED = 'google.c.a.e';
5612
+ const DEFAULT_REGISTRATION_TIMEOUT = 10000;
5613
+ var MessageType$1;
5614
+ (function (MessageType) {
5615
+ MessageType[MessageType["DATA_MESSAGE"] = 1] = "DATA_MESSAGE";
5616
+ MessageType[MessageType["DISPLAY_NOTIFICATION"] = 3] = "DISPLAY_NOTIFICATION";
5617
+ })(MessageType$1 || (MessageType$1 = {}));
5618
+
5619
+ /**
5620
+ * @license
5621
+ * Copyright 2018 Google LLC
5622
+ *
5623
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5624
+ * in compliance with the License. You may obtain a copy of the License at
5625
+ *
5626
+ * http://www.apache.org/licenses/LICENSE-2.0
5627
+ *
5628
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
5629
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
5630
+ * or implied. See the License for the specific language governing permissions and limitations under
5631
+ * the License.
5632
+ */
5633
+ var MessageType;
5634
+ (function (MessageType) {
5635
+ MessageType["PUSH_RECEIVED"] = "push-received";
5636
+ MessageType["NOTIFICATION_CLICKED"] = "notification-clicked";
5637
+ })(MessageType || (MessageType = {}));
5638
+
5639
+ /**
5640
+ * @license
5641
+ * Copyright 2017 Google LLC
5642
+ *
5643
+ * Licensed under the Apache License, Version 2.0 (the "License");
5644
+ * you may not use this file except in compliance with the License.
5645
+ * You may obtain a copy of the License at
5646
+ *
5647
+ * http://www.apache.org/licenses/LICENSE-2.0
5648
+ *
5649
+ * Unless required by applicable law or agreed to in writing, software
5650
+ * distributed under the License is distributed on an "AS IS" BASIS,
5651
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5652
+ * See the License for the specific language governing permissions and
5653
+ * limitations under the License.
5654
+ */
5655
+ function arrayToBase64(array) {
5656
+ const uint8Array = new Uint8Array(array);
5657
+ const base64String = btoa(String.fromCharCode(...uint8Array));
5658
+ return base64String.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
5659
+ }
5660
+ function base64ToArray(base64String) {
5661
+ const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
5662
+ const base64 = (base64String + padding)
5663
+ .replace(/\-/g, '+')
5664
+ .replace(/_/g, '/');
5665
+ const rawData = atob(base64);
5666
+ const outputArray = new Uint8Array(rawData.length);
5667
+ for (let i = 0; i < rawData.length; ++i) {
5668
+ outputArray[i] = rawData.charCodeAt(i);
5669
+ }
5670
+ return outputArray;
5671
+ }
5672
+
5673
+ /**
5674
+ * @license
5675
+ * Copyright 2019 Google LLC
5676
+ *
5677
+ * Licensed under the Apache License, Version 2.0 (the "License");
5678
+ * you may not use this file except in compliance with the License.
5679
+ * You may obtain a copy of the License at
5680
+ *
5681
+ * http://www.apache.org/licenses/LICENSE-2.0
5682
+ *
5683
+ * Unless required by applicable law or agreed to in writing, software
5684
+ * distributed under the License is distributed on an "AS IS" BASIS,
5685
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5686
+ * See the License for the specific language governing permissions and
5687
+ * limitations under the License.
5688
+ */
5689
+ const OLD_DB_NAME = 'fcm_token_details_db';
5690
+ /**
5691
+ * The last DB version of 'fcm_token_details_db' was 4. This is one higher, so that the upgrade
5692
+ * callback is called for all versions of the old DB.
5693
+ */
5694
+ const OLD_DB_VERSION = 5;
5695
+ const OLD_OBJECT_STORE_NAME = 'fcm_token_object_Store';
5696
+ async function migrateOldDatabase(senderId) {
5697
+ if ('databases' in indexedDB) {
5698
+ // indexedDb.databases() is an IndexedDB v3 API and does not exist in all browsers. TODO: Remove
5699
+ // typecast when it lands in TS types.
5700
+ const databases = await indexedDB.databases();
5701
+ const dbNames = databases.map(db => db.name);
5702
+ if (!dbNames.includes(OLD_DB_NAME)) {
5703
+ // old DB didn't exist, no need to open.
5704
+ return null;
5705
+ }
5706
+ }
5707
+ let tokenDetails = null;
5708
+ const db = await openDB(OLD_DB_NAME, OLD_DB_VERSION, {
5709
+ upgrade: async (db, oldVersion, newVersion, upgradeTransaction) => {
5710
+ if (oldVersion < 2) {
5711
+ // Database too old, skip migration.
5712
+ return;
5713
+ }
5714
+ if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
5715
+ // Database did not exist. Nothing to do.
5716
+ return;
5717
+ }
5718
+ const objectStore = upgradeTransaction.objectStore(OLD_OBJECT_STORE_NAME);
5719
+ const value = await objectStore.index('fcmSenderId').get(senderId);
5720
+ await objectStore.clear();
5721
+ if (!value) {
5722
+ // No entry in the database, nothing to migrate.
5723
+ return;
5724
+ }
5725
+ if (oldVersion === 2) {
5726
+ const oldDetails = value;
5727
+ if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
5728
+ return;
5729
+ }
5730
+ tokenDetails = {
5731
+ token: oldDetails.fcmToken,
5732
+ createTime: oldDetails.createTime ?? Date.now(),
5733
+ subscriptionOptions: {
5734
+ auth: oldDetails.auth,
5735
+ p256dh: oldDetails.p256dh,
5736
+ endpoint: oldDetails.endpoint,
5737
+ swScope: oldDetails.swScope,
5738
+ vapidKey: typeof oldDetails.vapidKey === 'string'
5739
+ ? oldDetails.vapidKey
5740
+ : arrayToBase64(oldDetails.vapidKey)
5741
+ }
5742
+ };
5743
+ }
5744
+ else if (oldVersion === 3) {
5745
+ const oldDetails = value;
5746
+ tokenDetails = {
5747
+ token: oldDetails.fcmToken,
5748
+ createTime: oldDetails.createTime,
5749
+ subscriptionOptions: {
5750
+ auth: arrayToBase64(oldDetails.auth),
5751
+ p256dh: arrayToBase64(oldDetails.p256dh),
5752
+ endpoint: oldDetails.endpoint,
5753
+ swScope: oldDetails.swScope,
5754
+ vapidKey: arrayToBase64(oldDetails.vapidKey)
5755
+ }
5756
+ };
5757
+ }
5758
+ else if (oldVersion === 4) {
5759
+ const oldDetails = value;
5760
+ tokenDetails = {
5761
+ token: oldDetails.fcmToken,
5762
+ createTime: oldDetails.createTime,
5763
+ subscriptionOptions: {
5764
+ auth: arrayToBase64(oldDetails.auth),
5765
+ p256dh: arrayToBase64(oldDetails.p256dh),
5766
+ endpoint: oldDetails.endpoint,
5767
+ swScope: oldDetails.swScope,
5768
+ vapidKey: arrayToBase64(oldDetails.vapidKey)
5769
+ }
5770
+ };
5771
+ }
5772
+ }
5773
+ });
5774
+ db.close();
5775
+ // Delete all old databases.
5776
+ await deleteDB(OLD_DB_NAME);
5777
+ await deleteDB('fcm_vapid_details_db');
5778
+ await deleteDB('undefined');
5779
+ return checkTokenDetails(tokenDetails) ? tokenDetails : null;
5780
+ }
5781
+ function checkTokenDetails(tokenDetails) {
5782
+ if (!tokenDetails || !tokenDetails.subscriptionOptions) {
5783
+ return false;
5784
+ }
5785
+ const { subscriptionOptions } = tokenDetails;
5786
+ return (typeof tokenDetails.createTime === 'number' &&
5787
+ tokenDetails.createTime > 0 &&
5788
+ typeof tokenDetails.token === 'string' &&
5789
+ tokenDetails.token.length > 0 &&
5790
+ typeof subscriptionOptions.auth === 'string' &&
5791
+ subscriptionOptions.auth.length > 0 &&
5792
+ typeof subscriptionOptions.p256dh === 'string' &&
5793
+ subscriptionOptions.p256dh.length > 0 &&
5794
+ typeof subscriptionOptions.endpoint === 'string' &&
5795
+ subscriptionOptions.endpoint.length > 0 &&
5796
+ typeof subscriptionOptions.swScope === 'string' &&
5797
+ subscriptionOptions.swScope.length > 0 &&
5798
+ typeof subscriptionOptions.vapidKey === 'string' &&
5799
+ subscriptionOptions.vapidKey.length > 0);
5800
+ }
5801
+
5802
+ /**
5803
+ * @license
5804
+ * Copyright 2019 Google LLC
5805
+ *
5806
+ * Licensed under the Apache License, Version 2.0 (the "License");
5807
+ * you may not use this file except in compliance with the License.
5808
+ * You may obtain a copy of the License at
5809
+ *
5810
+ * http://www.apache.org/licenses/LICENSE-2.0
5811
+ *
5812
+ * Unless required by applicable law or agreed to in writing, software
5813
+ * distributed under the License is distributed on an "AS IS" BASIS,
5814
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5815
+ * See the License for the specific language governing permissions and
5816
+ * limitations under the License.
5817
+ */
5818
+ // Exported for tests.
5819
+ const DATABASE_NAME = 'firebase-messaging-database';
5820
+ const DATABASE_VERSION = 1;
5821
+ const OBJECT_STORE_NAME = 'firebase-messaging-store';
5822
+ let dbPromise = null;
5823
+ function getDbPromise() {
5824
+ if (!dbPromise) {
5825
+ dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {
5826
+ upgrade: (upgradeDb, oldVersion) => {
5827
+ // We don't use 'break' in this switch statement, the fall-through behavior is what we want,
5828
+ // because if there are multiple versions between the old version and the current version, we
5829
+ // want ALL the migrations that correspond to those versions to run, not only the last one.
5830
+ // eslint-disable-next-line default-case
5831
+ switch (oldVersion) {
5832
+ case 0:
5833
+ upgradeDb.createObjectStore(OBJECT_STORE_NAME);
5834
+ }
5835
+ }
5836
+ });
5837
+ }
5838
+ return dbPromise;
5839
+ }
5840
+ /** Gets record(s) from the objectStore that match the given key. */
5841
+ async function dbGet(firebaseDependencies) {
5842
+ const key = getKey(firebaseDependencies);
5843
+ const db = await getDbPromise();
5844
+ const tokenDetails = (await db
5845
+ .transaction(OBJECT_STORE_NAME)
5846
+ .objectStore(OBJECT_STORE_NAME)
5847
+ .get(key));
5848
+ if (tokenDetails) {
5849
+ return tokenDetails;
5850
+ }
5851
+ else {
5852
+ // Check if there is a tokenDetails object in the old DB.
5853
+ const oldTokenDetails = await migrateOldDatabase(firebaseDependencies.appConfig.senderId);
5854
+ if (oldTokenDetails) {
5855
+ await dbSet(firebaseDependencies, oldTokenDetails);
5856
+ return oldTokenDetails;
5857
+ }
5858
+ }
5859
+ }
5860
+ /** Assigns or overwrites the record for the given key with the given value. */
5861
+ async function dbSet(firebaseDependencies, tokenDetails) {
5862
+ const key = getKey(firebaseDependencies);
5863
+ const db = await getDbPromise();
5864
+ const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
5865
+ await tx.objectStore(OBJECT_STORE_NAME).put(tokenDetails, key);
5866
+ await tx.done;
5867
+ return tokenDetails;
5868
+ }
5869
+ function getKey({ appConfig }) {
5870
+ return appConfig.appId;
5871
+ }
5872
+
5873
+ /**
5874
+ * @license
5875
+ * Copyright 2017 Google LLC
5876
+ *
5877
+ * Licensed under the Apache License, Version 2.0 (the "License");
5878
+ * you may not use this file except in compliance with the License.
5879
+ * You may obtain a copy of the License at
5880
+ *
5881
+ * http://www.apache.org/licenses/LICENSE-2.0
5882
+ *
5883
+ * Unless required by applicable law or agreed to in writing, software
5884
+ * distributed under the License is distributed on an "AS IS" BASIS,
5885
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5886
+ * See the License for the specific language governing permissions and
5887
+ * limitations under the License.
5888
+ */
5889
+ const ERROR_MAP = {
5890
+ ["missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */]: 'Missing App configuration value: "{$valueName}"',
5891
+ ["only-available-in-window" /* ErrorCode.AVAILABLE_IN_WINDOW */]: 'This method is available in a Window context.',
5892
+ ["only-available-in-sw" /* ErrorCode.AVAILABLE_IN_SW */]: 'This method is available in a service worker context.',
5893
+ ["permission-default" /* ErrorCode.PERMISSION_DEFAULT */]: 'The notification permission was not granted and dismissed instead.',
5894
+ ["permission-blocked" /* ErrorCode.PERMISSION_BLOCKED */]: 'The notification permission was not granted and blocked instead.',
5895
+ ["unsupported-browser" /* ErrorCode.UNSUPPORTED_BROWSER */]: "This browser doesn't support the API's required to use the Firebase SDK.",
5896
+ ["indexed-db-unsupported" /* ErrorCode.INDEXED_DB_UNSUPPORTED */]: "This browser doesn't support indexedDb.open() (ex. Safari iFrame, Firefox Private Browsing, etc)",
5897
+ ["failed-service-worker-registration" /* ErrorCode.FAILED_DEFAULT_REGISTRATION */]: 'We are unable to register the default service worker. {$browserErrorMessage}',
5898
+ ["token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */]: 'A problem occurred while subscribing the user to FCM: {$errorInfo}',
5899
+ ["token-subscribe-no-token" /* ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN */]: 'FCM returned no token when subscribing the user to push.',
5900
+ ["token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */]: 'A problem occurred while unsubscribing the ' +
5901
+ 'user from FCM: {$errorInfo}',
5902
+ ["token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */]: 'A problem occurred while updating the user from FCM: {$errorInfo}',
5903
+ ["token-update-no-token" /* ErrorCode.TOKEN_UPDATE_NO_TOKEN */]: 'FCM returned no token when updating the user to push.',
5904
+ ["use-sw-after-get-token" /* ErrorCode.USE_SW_AFTER_GET_TOKEN */]: 'The useServiceWorker() method may only be called once and must be ' +
5905
+ 'called before calling getToken() to ensure your service worker is used.',
5906
+ ["invalid-sw-registration" /* ErrorCode.INVALID_SW_REGISTRATION */]: 'The input to useServiceWorker() must be a ServiceWorkerRegistration.',
5907
+ ["invalid-bg-handler" /* ErrorCode.INVALID_BG_HANDLER */]: 'The input to setBackgroundMessageHandler() must be a function.',
5908
+ ["invalid-vapid-key" /* ErrorCode.INVALID_VAPID_KEY */]: 'The public VAPID key must be a string.',
5909
+ ["use-vapid-key-after-get-token" /* ErrorCode.USE_VAPID_KEY_AFTER_GET_TOKEN */]: 'The usePublicVapidKey() method may only be called once and must be ' +
5910
+ 'called before calling getToken() to ensure your VAPID key is used.'
5911
+ };
5912
+ const ERROR_FACTORY = new ErrorFactory('messaging', 'Messaging', ERROR_MAP);
5913
+
5914
+ /**
5915
+ * @license
5916
+ * Copyright 2019 Google LLC
5917
+ *
5918
+ * Licensed under the Apache License, Version 2.0 (the "License");
5919
+ * you may not use this file except in compliance with the License.
5920
+ * You may obtain a copy of the License at
5921
+ *
5922
+ * http://www.apache.org/licenses/LICENSE-2.0
5923
+ *
5924
+ * Unless required by applicable law or agreed to in writing, software
5925
+ * distributed under the License is distributed on an "AS IS" BASIS,
5926
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5927
+ * See the License for the specific language governing permissions and
5928
+ * limitations under the License.
5929
+ */
5930
+ async function requestGetToken(firebaseDependencies, subscriptionOptions) {
5931
+ const headers = await getHeaders(firebaseDependencies);
5932
+ const body = getBody(subscriptionOptions);
5933
+ const subscribeOptions = {
5934
+ method: 'POST',
5935
+ headers,
5936
+ body: JSON.stringify(body)
5937
+ };
5938
+ let responseData;
5939
+ try {
5940
+ const response = await fetch(getEndpoint(firebaseDependencies.appConfig), subscribeOptions);
5941
+ responseData = await response.json();
5942
+ }
5943
+ catch (err) {
5944
+ throw ERROR_FACTORY.create("token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */, {
5945
+ errorInfo: err?.toString()
5946
+ });
5947
+ }
5948
+ if (responseData.error) {
5949
+ const message = responseData.error.message;
5950
+ throw ERROR_FACTORY.create("token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */, {
5951
+ errorInfo: message
5952
+ });
5953
+ }
5954
+ if (!responseData.token) {
5955
+ throw ERROR_FACTORY.create("token-subscribe-no-token" /* ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN */);
5956
+ }
5957
+ return responseData.token;
5958
+ }
5959
+ async function requestUpdateToken(firebaseDependencies, tokenDetails) {
5960
+ const headers = await getHeaders(firebaseDependencies);
5961
+ const body = getBody(tokenDetails.subscriptionOptions);
5962
+ const updateOptions = {
5963
+ method: 'PATCH',
5964
+ headers,
5965
+ body: JSON.stringify(body)
5966
+ };
5967
+ let responseData;
5968
+ try {
5969
+ const response = await fetch(`${getEndpoint(firebaseDependencies.appConfig)}/${tokenDetails.token}`, updateOptions);
5970
+ responseData = await response.json();
5971
+ }
5972
+ catch (err) {
5973
+ throw ERROR_FACTORY.create("token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */, {
5974
+ errorInfo: err?.toString()
5975
+ });
5976
+ }
5977
+ if (responseData.error) {
5978
+ const message = responseData.error.message;
5979
+ throw ERROR_FACTORY.create("token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */, {
5980
+ errorInfo: message
5981
+ });
5982
+ }
5983
+ if (!responseData.token) {
5984
+ throw ERROR_FACTORY.create("token-update-no-token" /* ErrorCode.TOKEN_UPDATE_NO_TOKEN */);
5985
+ }
5986
+ return responseData.token;
5987
+ }
5988
+ async function requestDeleteToken(firebaseDependencies, token) {
5989
+ const headers = await getHeaders(firebaseDependencies);
5990
+ const unsubscribeOptions = {
5991
+ method: 'DELETE',
5992
+ headers
5993
+ };
5994
+ try {
5995
+ const response = await fetch(`${getEndpoint(firebaseDependencies.appConfig)}/${token}`, unsubscribeOptions);
5996
+ const responseData = await response.json();
5997
+ if (responseData.error) {
5998
+ const message = responseData.error.message;
5999
+ throw ERROR_FACTORY.create("token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */, {
6000
+ errorInfo: message
6001
+ });
6002
+ }
6003
+ }
6004
+ catch (err) {
6005
+ throw ERROR_FACTORY.create("token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */, {
6006
+ errorInfo: err?.toString()
6007
+ });
6008
+ }
6009
+ }
6010
+ function getEndpoint({ projectId }) {
6011
+ return `${ENDPOINT}/projects/${projectId}/registrations`;
6012
+ }
6013
+ async function getHeaders({ appConfig, installations }) {
6014
+ const authToken = await installations.getToken();
6015
+ return new Headers({
6016
+ 'Content-Type': 'application/json',
6017
+ Accept: 'application/json',
6018
+ 'x-goog-api-key': appConfig.apiKey,
6019
+ 'x-goog-firebase-installations-auth': `FIS ${authToken}`
6020
+ });
6021
+ }
6022
+ function getBody({ p256dh, auth, endpoint, vapidKey }) {
6023
+ const body = {
6024
+ web: {
6025
+ endpoint,
6026
+ auth,
6027
+ p256dh
6028
+ }
6029
+ };
6030
+ if (vapidKey !== DEFAULT_VAPID_KEY) {
6031
+ body.web.applicationPubKey = vapidKey;
6032
+ }
6033
+ return body;
6034
+ }
6035
+
6036
+ /**
6037
+ * @license
6038
+ * Copyright 2019 Google LLC
6039
+ *
6040
+ * Licensed under the Apache License, Version 2.0 (the "License");
6041
+ * you may not use this file except in compliance with the License.
6042
+ * You may obtain a copy of the License at
6043
+ *
6044
+ * http://www.apache.org/licenses/LICENSE-2.0
6045
+ *
6046
+ * Unless required by applicable law or agreed to in writing, software
6047
+ * distributed under the License is distributed on an "AS IS" BASIS,
6048
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6049
+ * See the License for the specific language governing permissions and
6050
+ * limitations under the License.
6051
+ */
6052
+ // UpdateRegistration will be called once every week.
6053
+ const TOKEN_EXPIRATION_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
6054
+ async function getTokenInternal(messaging) {
6055
+ const pushSubscription = await getPushSubscription(messaging.swRegistration, messaging.vapidKey);
6056
+ const subscriptionOptions = {
6057
+ vapidKey: messaging.vapidKey,
6058
+ swScope: messaging.swRegistration.scope,
6059
+ endpoint: pushSubscription.endpoint,
6060
+ auth: arrayToBase64(pushSubscription.getKey('auth')),
6061
+ p256dh: arrayToBase64(pushSubscription.getKey('p256dh'))
6062
+ };
6063
+ const tokenDetails = await dbGet(messaging.firebaseDependencies);
6064
+ if (!tokenDetails) {
6065
+ // No token, get a new one.
6066
+ return getNewToken(messaging.firebaseDependencies, subscriptionOptions);
6067
+ }
6068
+ else if (!isTokenValid(tokenDetails.subscriptionOptions, subscriptionOptions)) {
6069
+ // Invalid token, get a new one.
6070
+ try {
6071
+ await requestDeleteToken(messaging.firebaseDependencies, tokenDetails.token);
6072
+ }
6073
+ catch (e) {
6074
+ // Suppress errors because of #2364
6075
+ console.warn(e);
6076
+ }
6077
+ return getNewToken(messaging.firebaseDependencies, subscriptionOptions);
6078
+ }
6079
+ else if (Date.now() >= tokenDetails.createTime + TOKEN_EXPIRATION_MS) {
6080
+ // Weekly token refresh
6081
+ return updateToken(messaging, {
6082
+ token: tokenDetails.token,
6083
+ createTime: Date.now(),
6084
+ subscriptionOptions
6085
+ });
6086
+ }
6087
+ else {
6088
+ // Valid token, nothing to do.
6089
+ return tokenDetails.token;
6090
+ }
6091
+ }
6092
+ async function updateToken(messaging, tokenDetails) {
6093
+ try {
6094
+ const updatedToken = await requestUpdateToken(messaging.firebaseDependencies, tokenDetails);
6095
+ const updatedTokenDetails = {
6096
+ ...tokenDetails,
6097
+ token: updatedToken,
6098
+ createTime: Date.now()
6099
+ };
6100
+ await dbSet(messaging.firebaseDependencies, updatedTokenDetails);
6101
+ return updatedToken;
6102
+ }
6103
+ catch (e) {
6104
+ throw e;
6105
+ }
6106
+ }
6107
+ async function getNewToken(firebaseDependencies, subscriptionOptions) {
6108
+ const token = await requestGetToken(firebaseDependencies, subscriptionOptions);
6109
+ const tokenDetails = {
6110
+ token,
6111
+ createTime: Date.now(),
6112
+ subscriptionOptions
6113
+ };
6114
+ await dbSet(firebaseDependencies, tokenDetails);
6115
+ return tokenDetails.token;
6116
+ }
6117
+ /**
6118
+ * Gets a PushSubscription for the current user.
6119
+ */
6120
+ async function getPushSubscription(swRegistration, vapidKey) {
6121
+ const subscription = await swRegistration.pushManager.getSubscription();
6122
+ if (subscription) {
6123
+ return subscription;
6124
+ }
6125
+ return swRegistration.pushManager.subscribe({
6126
+ userVisibleOnly: true,
6127
+ // Chrome <= 75 doesn't support base64-encoded VAPID key. For backward compatibility, VAPID key
6128
+ // submitted to pushManager#subscribe must be of type Uint8Array.
6129
+ applicationServerKey: base64ToArray(vapidKey)
6130
+ });
6131
+ }
6132
+ /**
6133
+ * Checks if the saved tokenDetails object matches the configuration provided.
6134
+ */
6135
+ function isTokenValid(dbOptions, currentOptions) {
6136
+ const isVapidKeyEqual = currentOptions.vapidKey === dbOptions.vapidKey;
6137
+ const isEndpointEqual = currentOptions.endpoint === dbOptions.endpoint;
6138
+ const isAuthEqual = currentOptions.auth === dbOptions.auth;
6139
+ const isP256dhEqual = currentOptions.p256dh === dbOptions.p256dh;
6140
+ return isVapidKeyEqual && isEndpointEqual && isAuthEqual && isP256dhEqual;
6141
+ }
6142
+
6143
+ /**
6144
+ * @license
6145
+ * Copyright 2020 Google LLC
6146
+ *
6147
+ * Licensed under the Apache License, Version 2.0 (the "License");
6148
+ * you may not use this file except in compliance with the License.
6149
+ * You may obtain a copy of the License at
6150
+ *
6151
+ * http://www.apache.org/licenses/LICENSE-2.0
6152
+ *
6153
+ * Unless required by applicable law or agreed to in writing, software
6154
+ * distributed under the License is distributed on an "AS IS" BASIS,
6155
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6156
+ * See the License for the specific language governing permissions and
6157
+ * limitations under the License.
6158
+ */
6159
+ function externalizePayload(internalPayload) {
6160
+ const payload = {
6161
+ from: internalPayload.from,
6162
+ // eslint-disable-next-line camelcase
6163
+ collapseKey: internalPayload.collapse_key,
6164
+ // eslint-disable-next-line camelcase
6165
+ messageId: internalPayload.fcmMessageId
6166
+ };
6167
+ propagateNotificationPayload(payload, internalPayload);
6168
+ propagateDataPayload(payload, internalPayload);
6169
+ propagateFcmOptions(payload, internalPayload);
6170
+ return payload;
6171
+ }
6172
+ function propagateNotificationPayload(payload, messagePayloadInternal) {
6173
+ if (!messagePayloadInternal.notification) {
6174
+ return;
6175
+ }
6176
+ payload.notification = {};
6177
+ const title = messagePayloadInternal.notification.title;
6178
+ if (!!title) {
6179
+ payload.notification.title = title;
6180
+ }
6181
+ const body = messagePayloadInternal.notification.body;
6182
+ if (!!body) {
6183
+ payload.notification.body = body;
6184
+ }
6185
+ const image = messagePayloadInternal.notification.image;
6186
+ if (!!image) {
6187
+ payload.notification.image = image;
6188
+ }
6189
+ const icon = messagePayloadInternal.notification.icon;
6190
+ if (!!icon) {
6191
+ payload.notification.icon = icon;
6192
+ }
6193
+ }
6194
+ function propagateDataPayload(payload, messagePayloadInternal) {
6195
+ if (!messagePayloadInternal.data) {
6196
+ return;
6197
+ }
6198
+ payload.data = messagePayloadInternal.data;
6199
+ }
6200
+ function propagateFcmOptions(payload, messagePayloadInternal) {
6201
+ // fcmOptions.link value is written into notification.click_action. see more in b/232072111
6202
+ if (!messagePayloadInternal.fcmOptions &&
6203
+ !messagePayloadInternal.notification?.click_action) {
6204
+ return;
6205
+ }
6206
+ payload.fcmOptions = {};
6207
+ const link = messagePayloadInternal.fcmOptions?.link ??
6208
+ messagePayloadInternal.notification?.click_action;
6209
+ if (!!link) {
6210
+ payload.fcmOptions.link = link;
6211
+ }
6212
+ // eslint-disable-next-line camelcase
6213
+ const analyticsLabel = messagePayloadInternal.fcmOptions?.analytics_label;
6214
+ if (!!analyticsLabel) {
6215
+ payload.fcmOptions.analyticsLabel = analyticsLabel;
6216
+ }
6217
+ }
6218
+
6219
+ /**
6220
+ * @license
6221
+ * Copyright 2019 Google LLC
6222
+ *
6223
+ * Licensed under the Apache License, Version 2.0 (the "License");
6224
+ * you may not use this file except in compliance with the License.
6225
+ * You may obtain a copy of the License at
6226
+ *
6227
+ * http://www.apache.org/licenses/LICENSE-2.0
6228
+ *
6229
+ * Unless required by applicable law or agreed to in writing, software
6230
+ * distributed under the License is distributed on an "AS IS" BASIS,
6231
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6232
+ * See the License for the specific language governing permissions and
6233
+ * limitations under the License.
6234
+ */
6235
+ function isConsoleMessage(data) {
6236
+ // This message has a campaign ID, meaning it was sent using the Firebase Console.
6237
+ return typeof data === 'object' && !!data && CONSOLE_CAMPAIGN_ID in data;
6238
+ }
6239
+
6240
+ /**
6241
+ * @license
6242
+ * Copyright 2019 Google LLC
6243
+ *
6244
+ * Licensed under the Apache License, Version 2.0 (the "License");
6245
+ * you may not use this file except in compliance with the License.
6246
+ * You may obtain a copy of the License at
6247
+ *
6248
+ * http://www.apache.org/licenses/LICENSE-2.0
6249
+ *
6250
+ * Unless required by applicable law or agreed to in writing, software
6251
+ * distributed under the License is distributed on an "AS IS" BASIS,
6252
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6253
+ * See the License for the specific language governing permissions and
6254
+ * limitations under the License.
6255
+ */
6256
+ _mergeStrings('AzSCbw63g1R0nCw85jG8', 'Iaya3yLKwmgvh7cF0q4');
6257
+ function _mergeStrings(s1, s2) {
6258
+ const resultArray = [];
6259
+ for (let i = 0; i < s1.length; i++) {
6260
+ resultArray.push(s1.charAt(i));
6261
+ if (i < s2.length) {
6262
+ resultArray.push(s2.charAt(i));
6263
+ }
6264
+ }
6265
+ return resultArray.join('');
6266
+ }
6267
+
6268
+ /**
6269
+ * @license
6270
+ * Copyright 2019 Google LLC
6271
+ *
6272
+ * Licensed under the Apache License, Version 2.0 (the "License");
6273
+ * you may not use this file except in compliance with the License.
6274
+ * You may obtain a copy of the License at
6275
+ *
6276
+ * http://www.apache.org/licenses/LICENSE-2.0
6277
+ *
6278
+ * Unless required by applicable law or agreed to in writing, software
6279
+ * distributed under the License is distributed on an "AS IS" BASIS,
6280
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6281
+ * See the License for the specific language governing permissions and
6282
+ * limitations under the License.
6283
+ */
6284
+ function extractAppConfig(app) {
6285
+ if (!app || !app.options) {
6286
+ throw getMissingValueError('App Configuration Object');
6287
+ }
6288
+ if (!app.name) {
6289
+ throw getMissingValueError('App Name');
6290
+ }
6291
+ // Required app config keys
6292
+ const configKeys = [
6293
+ 'projectId',
6294
+ 'apiKey',
6295
+ 'appId',
6296
+ 'messagingSenderId'
6297
+ ];
6298
+ const { options } = app;
6299
+ for (const keyName of configKeys) {
6300
+ if (!options[keyName]) {
6301
+ throw getMissingValueError(keyName);
6302
+ }
6303
+ }
6304
+ return {
6305
+ appName: app.name,
6306
+ projectId: options.projectId,
6307
+ apiKey: options.apiKey,
6308
+ appId: options.appId,
6309
+ senderId: options.messagingSenderId
6310
+ };
6311
+ }
6312
+ function getMissingValueError(valueName) {
6313
+ return ERROR_FACTORY.create("missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */, {
6314
+ valueName
6315
+ });
6316
+ }
6317
+
6318
+ /**
6319
+ * @license
6320
+ * Copyright 2020 Google LLC
6321
+ *
6322
+ * Licensed under the Apache License, Version 2.0 (the "License");
6323
+ * you may not use this file except in compliance with the License.
6324
+ * You may obtain a copy of the License at
6325
+ *
6326
+ * http://www.apache.org/licenses/LICENSE-2.0
6327
+ *
6328
+ * Unless required by applicable law or agreed to in writing, software
6329
+ * distributed under the License is distributed on an "AS IS" BASIS,
6330
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6331
+ * See the License for the specific language governing permissions and
6332
+ * limitations under the License.
6333
+ */
6334
+ class MessagingService {
6335
+ constructor(app, installations, analyticsProvider) {
6336
+ // logging is only done with end user consent. Default to false.
6337
+ this.deliveryMetricsExportedToBigQueryEnabled = false;
6338
+ this.onBackgroundMessageHandler = null;
6339
+ this.onMessageHandler = null;
6340
+ this.logEvents = [];
6341
+ this.isLogServiceStarted = false;
6342
+ const appConfig = extractAppConfig(app);
6343
+ this.firebaseDependencies = {
6344
+ app,
6345
+ appConfig,
6346
+ installations,
6347
+ analyticsProvider
6348
+ };
6349
+ }
6350
+ _delete() {
6351
+ return Promise.resolve();
6352
+ }
6353
+ }
6354
+
6355
+ /**
6356
+ * @license
6357
+ * Copyright 2020 Google LLC
6358
+ *
6359
+ * Licensed under the Apache License, Version 2.0 (the "License");
6360
+ * you may not use this file except in compliance with the License.
6361
+ * You may obtain a copy of the License at
6362
+ *
6363
+ * http://www.apache.org/licenses/LICENSE-2.0
6364
+ *
6365
+ * Unless required by applicable law or agreed to in writing, software
6366
+ * distributed under the License is distributed on an "AS IS" BASIS,
6367
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6368
+ * See the License for the specific language governing permissions and
6369
+ * limitations under the License.
6370
+ */
6371
+ async function registerDefaultSw(messaging) {
6372
+ try {
6373
+ messaging.swRegistration = await navigator.serviceWorker.register(DEFAULT_SW_PATH, {
6374
+ scope: DEFAULT_SW_SCOPE
6375
+ });
6376
+ // The timing when browser updates sw when sw has an update is unreliable from experiment. It
6377
+ // leads to version conflict when the SDK upgrades to a newer version in the main page, but sw
6378
+ // is stuck with the old version. For example,
6379
+ // https://github.com/firebase/firebase-js-sdk/issues/2590 The following line reliably updates
6380
+ // sw if there was an update.
6381
+ messaging.swRegistration.update().catch(() => {
6382
+ /* it is non blocking and we don't care if it failed */
6383
+ });
6384
+ await waitForRegistrationActive(messaging.swRegistration);
6385
+ }
6386
+ catch (e) {
6387
+ throw ERROR_FACTORY.create("failed-service-worker-registration" /* ErrorCode.FAILED_DEFAULT_REGISTRATION */, {
6388
+ browserErrorMessage: e?.message
6389
+ });
6390
+ }
6391
+ }
6392
+ /**
6393
+ * Waits for registration to become active. MDN documentation claims that
6394
+ * a service worker registration should be ready to use after awaiting
6395
+ * navigator.serviceWorker.register() but that doesn't seem to be the case in
6396
+ * practice, causing the SDK to throw errors when calling
6397
+ * swRegistration.pushManager.subscribe() too soon after register(). The only
6398
+ * solution seems to be waiting for the service worker registration `state`
6399
+ * to become "active".
6400
+ */
6401
+ async function waitForRegistrationActive(registration) {
6402
+ return new Promise((resolve, reject) => {
6403
+ const rejectTimeout = setTimeout(() => reject(new Error(`Service worker not registered after ${DEFAULT_REGISTRATION_TIMEOUT} ms`)), DEFAULT_REGISTRATION_TIMEOUT);
6404
+ const incomingSw = registration.installing || registration.waiting;
6405
+ if (registration.active) {
6406
+ clearTimeout(rejectTimeout);
6407
+ resolve();
6408
+ }
6409
+ else if (incomingSw) {
6410
+ incomingSw.onstatechange = ev => {
6411
+ if (ev.target?.state === 'activated') {
6412
+ incomingSw.onstatechange = null;
6413
+ clearTimeout(rejectTimeout);
6414
+ resolve();
6415
+ }
6416
+ };
6417
+ }
6418
+ else {
6419
+ clearTimeout(rejectTimeout);
6420
+ reject(new Error('No incoming service worker found.'));
6421
+ }
6422
+ });
6423
+ }
6424
+
6425
+ /**
6426
+ * @license
6427
+ * Copyright 2020 Google LLC
6428
+ *
6429
+ * Licensed under the Apache License, Version 2.0 (the "License");
6430
+ * you may not use this file except in compliance with the License.
6431
+ * You may obtain a copy of the License at
6432
+ *
6433
+ * http://www.apache.org/licenses/LICENSE-2.0
6434
+ *
6435
+ * Unless required by applicable law or agreed to in writing, software
6436
+ * distributed under the License is distributed on an "AS IS" BASIS,
6437
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6438
+ * See the License for the specific language governing permissions and
6439
+ * limitations under the License.
6440
+ */
6441
+ async function updateSwReg(messaging, swRegistration) {
6442
+ if (!swRegistration && !messaging.swRegistration) {
6443
+ await registerDefaultSw(messaging);
6444
+ }
6445
+ if (!swRegistration && !!messaging.swRegistration) {
6446
+ return;
6447
+ }
6448
+ if (!(swRegistration instanceof ServiceWorkerRegistration)) {
6449
+ throw ERROR_FACTORY.create("invalid-sw-registration" /* ErrorCode.INVALID_SW_REGISTRATION */);
6450
+ }
6451
+ messaging.swRegistration = swRegistration;
6452
+ }
6453
+
6454
+ /**
6455
+ * @license
6456
+ * Copyright 2020 Google LLC
6457
+ *
6458
+ * Licensed under the Apache License, Version 2.0 (the "License");
6459
+ * you may not use this file except in compliance with the License.
6460
+ * You may obtain a copy of the License at
6461
+ *
6462
+ * http://www.apache.org/licenses/LICENSE-2.0
6463
+ *
6464
+ * Unless required by applicable law or agreed to in writing, software
6465
+ * distributed under the License is distributed on an "AS IS" BASIS,
6466
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6467
+ * See the License for the specific language governing permissions and
6468
+ * limitations under the License.
6469
+ */
6470
+ async function updateVapidKey(messaging, vapidKey) {
6471
+ if (!!vapidKey) {
6472
+ messaging.vapidKey = vapidKey;
6473
+ }
6474
+ else if (!messaging.vapidKey) {
6475
+ messaging.vapidKey = DEFAULT_VAPID_KEY;
6476
+ }
6477
+ }
6478
+
6479
+ /**
6480
+ * @license
6481
+ * Copyright 2020 Google LLC
6482
+ *
6483
+ * Licensed under the Apache License, Version 2.0 (the "License");
6484
+ * you may not use this file except in compliance with the License.
6485
+ * You may obtain a copy of the License at
6486
+ *
6487
+ * http://www.apache.org/licenses/LICENSE-2.0
6488
+ *
6489
+ * Unless required by applicable law or agreed to in writing, software
6490
+ * distributed under the License is distributed on an "AS IS" BASIS,
6491
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6492
+ * See the License for the specific language governing permissions and
6493
+ * limitations under the License.
6494
+ */
6495
+ async function getToken$1(messaging, options) {
6496
+ if (!navigator) {
6497
+ throw ERROR_FACTORY.create("only-available-in-window" /* ErrorCode.AVAILABLE_IN_WINDOW */);
6498
+ }
6499
+ if (Notification.permission === 'default') {
6500
+ await Notification.requestPermission();
6501
+ }
6502
+ if (Notification.permission !== 'granted') {
6503
+ throw ERROR_FACTORY.create("permission-blocked" /* ErrorCode.PERMISSION_BLOCKED */);
6504
+ }
6505
+ await updateVapidKey(messaging, options?.vapidKey);
6506
+ await updateSwReg(messaging, options?.serviceWorkerRegistration);
6507
+ return getTokenInternal(messaging);
6508
+ }
6509
+
6510
+ /**
6511
+ * @license
6512
+ * Copyright 2019 Google LLC
6513
+ *
6514
+ * Licensed under the Apache License, Version 2.0 (the "License");
6515
+ * you may not use this file except in compliance with the License.
6516
+ * You may obtain a copy of the License at
6517
+ *
6518
+ * http://www.apache.org/licenses/LICENSE-2.0
6519
+ *
6520
+ * Unless required by applicable law or agreed to in writing, software
6521
+ * distributed under the License is distributed on an "AS IS" BASIS,
6522
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6523
+ * See the License for the specific language governing permissions and
6524
+ * limitations under the License.
6525
+ */
6526
+ async function logToScion(messaging, messageType, data) {
6527
+ const eventType = getEventType(messageType);
6528
+ const analytics = await messaging.firebaseDependencies.analyticsProvider.get();
6529
+ analytics.logEvent(eventType, {
6530
+ /* eslint-disable camelcase */
6531
+ message_id: data[CONSOLE_CAMPAIGN_ID],
6532
+ message_name: data[CONSOLE_CAMPAIGN_NAME],
6533
+ message_time: data[CONSOLE_CAMPAIGN_TIME],
6534
+ message_device_time: Math.floor(Date.now() / 1000)
6535
+ /* eslint-enable camelcase */
6536
+ });
6537
+ }
6538
+ function getEventType(messageType) {
6539
+ switch (messageType) {
6540
+ case MessageType.NOTIFICATION_CLICKED:
6541
+ return 'notification_open';
6542
+ case MessageType.PUSH_RECEIVED:
6543
+ return 'notification_foreground';
6544
+ default:
6545
+ throw new Error();
6546
+ }
6547
+ }
6548
+
6549
+ /**
6550
+ * @license
6551
+ * Copyright 2017 Google LLC
6552
+ *
6553
+ * Licensed under the Apache License, Version 2.0 (the "License");
6554
+ * you may not use this file except in compliance with the License.
6555
+ * You may obtain a copy of the License at
6556
+ *
6557
+ * http://www.apache.org/licenses/LICENSE-2.0
6558
+ *
6559
+ * Unless required by applicable law or agreed to in writing, software
6560
+ * distributed under the License is distributed on an "AS IS" BASIS,
6561
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6562
+ * See the License for the specific language governing permissions and
6563
+ * limitations under the License.
6564
+ */
6565
+ async function messageEventListener(messaging, event) {
6566
+ const internalPayload = event.data;
6567
+ if (!internalPayload.isFirebaseMessaging) {
6568
+ return;
6569
+ }
6570
+ if (messaging.onMessageHandler &&
6571
+ internalPayload.messageType === MessageType.PUSH_RECEIVED) {
6572
+ if (typeof messaging.onMessageHandler === 'function') {
6573
+ messaging.onMessageHandler(externalizePayload(internalPayload));
6574
+ }
6575
+ else {
6576
+ messaging.onMessageHandler.next(externalizePayload(internalPayload));
6577
+ }
6578
+ }
6579
+ // Log to Scion if applicable
6580
+ const dataPayload = internalPayload.data;
6581
+ if (isConsoleMessage(dataPayload) &&
6582
+ dataPayload[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED] === '1') {
6583
+ await logToScion(messaging, internalPayload.messageType, dataPayload);
6584
+ }
6585
+ }
6586
+
6587
+ const name = "@firebase/messaging";
6588
+ const version = "0.12.23";
6589
+
6590
+ /**
6591
+ * @license
6592
+ * Copyright 2020 Google LLC
6593
+ *
6594
+ * Licensed under the Apache License, Version 2.0 (the "License");
6595
+ * you may not use this file except in compliance with the License.
6596
+ * You may obtain a copy of the License at
6597
+ *
6598
+ * http://www.apache.org/licenses/LICENSE-2.0
6599
+ *
6600
+ * Unless required by applicable law or agreed to in writing, software
6601
+ * distributed under the License is distributed on an "AS IS" BASIS,
6602
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6603
+ * See the License for the specific language governing permissions and
6604
+ * limitations under the License.
6605
+ */
6606
+ const WindowMessagingFactory = (container) => {
6607
+ const messaging = new MessagingService(container.getProvider('app').getImmediate(), container.getProvider('installations-internal').getImmediate(), container.getProvider('analytics-internal'));
6608
+ navigator.serviceWorker.addEventListener('message', e => messageEventListener(messaging, e));
6609
+ return messaging;
6610
+ };
6611
+ const WindowMessagingInternalFactory = (container) => {
6612
+ const messaging = container
6613
+ .getProvider('messaging')
6614
+ .getImmediate();
6615
+ const messagingInternal = {
6616
+ getToken: (options) => getToken$1(messaging, options)
6617
+ };
6618
+ return messagingInternal;
6619
+ };
6620
+ function registerMessagingInWindow() {
6621
+ _registerComponent(new Component('messaging', WindowMessagingFactory, "PUBLIC" /* ComponentType.PUBLIC */));
6622
+ _registerComponent(new Component('messaging-internal', WindowMessagingInternalFactory, "PRIVATE" /* ComponentType.PRIVATE */));
6623
+ registerVersion(name, version);
6624
+ // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation
6625
+ registerVersion(name, version, 'esm2020');
6626
+ }
6627
+
6628
+ /**
6629
+ * @license
6630
+ * Copyright 2020 Google LLC
6631
+ *
6632
+ * Licensed under the Apache License, Version 2.0 (the "License");
6633
+ * you may not use this file except in compliance with the License.
6634
+ * You may obtain a copy of the License at
6635
+ *
6636
+ * http://www.apache.org/licenses/LICENSE-2.0
6637
+ *
6638
+ * Unless required by applicable law or agreed to in writing, software
6639
+ * distributed under the License is distributed on an "AS IS" BASIS,
6640
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6641
+ * See the License for the specific language governing permissions and
6642
+ * limitations under the License.
6643
+ */
6644
+ /**
6645
+ * Checks if all required APIs exist in the browser.
6646
+ * @returns a Promise that resolves to a boolean.
6647
+ *
6648
+ * @public
6649
+ */
6650
+ async function isWindowSupported() {
6651
+ try {
6652
+ // This throws if open() is unsupported, so adding it to the conditional
6653
+ // statement below can cause an uncaught error.
6654
+ await validateIndexedDBOpenable();
6655
+ }
6656
+ catch (e) {
6657
+ return false;
6658
+ }
6659
+ // firebase-js-sdk/issues/2393 reveals that idb#open in Safari iframe and Firefox private browsing
6660
+ // might be prohibited to run. In these contexts, an error would be thrown during the messaging
6661
+ // instantiating phase, informing the developers to import/call isSupported for special handling.
6662
+ return (typeof window !== 'undefined' &&
6663
+ isIndexedDBAvailable() &&
6664
+ areCookiesEnabled() &&
6665
+ 'serviceWorker' in navigator &&
6666
+ 'PushManager' in window &&
6667
+ 'Notification' in window &&
6668
+ 'fetch' in window &&
6669
+ ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') &&
6670
+ PushSubscription.prototype.hasOwnProperty('getKey'));
6671
+ }
6672
+
6673
+ /**
6674
+ * @license
6675
+ * Copyright 2020 Google LLC
6676
+ *
6677
+ * Licensed under the Apache License, Version 2.0 (the "License");
6678
+ * you may not use this file except in compliance with the License.
6679
+ * You may obtain a copy of the License at
6680
+ *
6681
+ * http://www.apache.org/licenses/LICENSE-2.0
6682
+ *
6683
+ * Unless required by applicable law or agreed to in writing, software
6684
+ * distributed under the License is distributed on an "AS IS" BASIS,
6685
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6686
+ * See the License for the specific language governing permissions and
6687
+ * limitations under the License.
6688
+ */
6689
+ function onMessage$1(messaging, nextOrObserver) {
6690
+ if (!navigator) {
6691
+ throw ERROR_FACTORY.create("only-available-in-window" /* ErrorCode.AVAILABLE_IN_WINDOW */);
6692
+ }
6693
+ messaging.onMessageHandler = nextOrObserver;
6694
+ return () => {
6695
+ messaging.onMessageHandler = null;
6696
+ };
6697
+ }
6698
+
6699
+ /**
6700
+ * @license
6701
+ * Copyright 2017 Google LLC
6702
+ *
6703
+ * Licensed under the Apache License, Version 2.0 (the "License");
6704
+ * you may not use this file except in compliance with the License.
6705
+ * You may obtain a copy of the License at
6706
+ *
6707
+ * http://www.apache.org/licenses/LICENSE-2.0
6708
+ *
6709
+ * Unless required by applicable law or agreed to in writing, software
6710
+ * distributed under the License is distributed on an "AS IS" BASIS,
6711
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6712
+ * See the License for the specific language governing permissions and
6713
+ * limitations under the License.
6714
+ */
6715
+ /**
6716
+ * Retrieves a Firebase Cloud Messaging instance.
6717
+ *
6718
+ * @returns The Firebase Cloud Messaging instance associated with the provided firebase app.
6719
+ *
6720
+ * @public
6721
+ */
6722
+ function getMessagingInWindow(app = getApp()) {
6723
+ // Conscious decision to make this async check non-blocking during the messaging instance
6724
+ // initialization phase for performance consideration. An error would be thrown latter for
6725
+ // developer's information. Developers can then choose to import and call `isSupported` for
6726
+ // special handling.
6727
+ isWindowSupported().then(isSupported => {
6728
+ // If `isWindowSupported()` resolved, but returned false.
6729
+ if (!isSupported) {
6730
+ throw ERROR_FACTORY.create("unsupported-browser" /* ErrorCode.UNSUPPORTED_BROWSER */);
6731
+ }
6732
+ }, _ => {
6733
+ // If `isWindowSupported()` rejected.
6734
+ throw ERROR_FACTORY.create("indexed-db-unsupported" /* ErrorCode.INDEXED_DB_UNSUPPORTED */);
6735
+ });
6736
+ return _getProvider(getModularInstance(app), 'messaging').getImmediate();
6737
+ }
6738
+ /**
6739
+ * Subscribes the {@link Messaging} instance to push notifications. Returns a Firebase Cloud
6740
+ * Messaging registration token that can be used to send push messages to that {@link Messaging}
6741
+ * instance.
6742
+ *
6743
+ * If notification permission isn't already granted, this method asks the user for permission. The
6744
+ * returned promise rejects if the user does not allow the app to show notifications.
6745
+ *
6746
+ * @param messaging - The {@link Messaging} instance.
6747
+ * @param options - Provides an optional vapid key and an optional service worker registration.
6748
+ *
6749
+ * @returns The promise resolves with an FCM registration token.
6750
+ *
6751
+ * @public
6752
+ */
6753
+ async function getToken(messaging, options) {
6754
+ messaging = getModularInstance(messaging);
6755
+ return getToken$1(messaging, options);
6756
+ }
6757
+ /**
6758
+ * When a push message is received and the user is currently on a page for your origin, the
6759
+ * message is passed to the page and an `onMessage()` event is dispatched with the payload of
6760
+ * the push message.
6761
+ *
6762
+ *
6763
+ * @param messaging - The {@link Messaging} instance.
6764
+ * @param nextOrObserver - This function, or observer object with `next` defined,
6765
+ * is called when a message is received and the user is currently viewing your page.
6766
+ * @returns To stop listening for messages execute this returned function.
6767
+ *
6768
+ * @public
6769
+ */
6770
+ function onMessage(messaging, nextOrObserver) {
6771
+ messaging = getModularInstance(messaging);
6772
+ return onMessage$1(messaging, nextOrObserver);
6773
+ }
6774
+
6775
+ /**
6776
+ * The Firebase Cloud Messaging Web SDK.
6777
+ * This SDK does not work in a Node.js environment.
6778
+ *
6779
+ * @packageDocumentation
6780
+ */
6781
+ registerMessagingInWindow();
6782
+
2144
6783
  const useFirebase = (firebaseConfig, vapidKey, onFirebaseMessage = () => {}) => {
2145
- const [app$1] = React.useState(app.initializeApp(firebaseConfig));
2146
- const [messaging$1, setMessaging] = React.useState();
6784
+ const [app] = React.useState(initializeApp(firebaseConfig));
6785
+ const [messaging, setMessaging] = React.useState();
2147
6786
  const [firebaseToken, setFirebaseToken] = React.useState();
2148
6787
  React.useEffect(() => {
2149
- messaging.isSupported().then(supported => {
6788
+ isWindowSupported().then(supported => {
2150
6789
  if (supported) {
2151
- setMessaging(messaging.getMessaging(app$1));
6790
+ setMessaging(getMessagingInWindow(app));
2152
6791
  }
2153
6792
  });
2154
- }, [setMessaging, app$1]);
6793
+ }, [setMessaging, app]);
2155
6794
  const notificationHandler = function (permission) {
2156
- console.log("Notification permission: ", permission, messaging$1);
2157
- if (permission === "granted" && messaging$1) {
2158
- messaging.getToken(messaging$1, {
6795
+ console.log("Notification permission: ", permission, messaging);
6796
+ if (permission === "granted" && messaging) {
6797
+ getToken(messaging, {
2159
6798
  vapidKey
2160
6799
  }).then(token => {
2161
6800
  setFirebaseToken(token);
@@ -2176,13 +6815,13 @@ const useFirebase = (firebaseConfig, vapidKey, onFirebaseMessage = () => {}) =>
2176
6815
  }
2177
6816
  }
2178
6817
  }
2179
- }, [messaging$1]);
6818
+ }, [messaging]);
2180
6819
  React.useEffect(() => {
2181
- if (messaging$1) {
2182
- const unsubscribe = messaging.onMessage(messaging$1, onFirebaseMessage);
6820
+ if (messaging) {
6821
+ const unsubscribe = onMessage(messaging, onFirebaseMessage);
2183
6822
  return () => unsubscribe();
2184
6823
  }
2185
- }, [messaging$1, onFirebaseMessage]);
6824
+ }, [messaging, onFirebaseMessage]);
2186
6825
  return {
2187
6826
  firebaseToken
2188
6827
  };
@@ -2281,7 +6920,8 @@ const useWidgetFetch = ({
2281
6920
  }, []);
2282
6921
  return {
2283
6922
  data,
2284
- loading
6923
+ loading,
6924
+ setData
2285
6925
  };
2286
6926
  };
2287
6927