@firebase/util 1.10.0 → 1.10.1
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.
- package/dist/index.cjs.js +3 -0
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm2017.js +3 -0
- package/dist/index.esm2017.js.map +1 -1
- package/dist/index.node.cjs.js +329 -324
- package/dist/index.node.cjs.js.map +1 -1
- package/dist/node-esm/index.node.esm.js +3 -0
- package/dist/node-esm/index.node.esm.js.map +1 -1
- package/package.json +4 -3
- package/dist/index.esm5.js +0 -2129
- package/dist/index.esm5.js.map +0 -1
package/dist/index.node.cjs.js
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var tslib = require('tslib');
|
|
6
|
-
|
|
7
5
|
/**
|
|
8
6
|
* @license
|
|
9
7
|
* Copyright 2017 Google LLC
|
|
@@ -23,7 +21,7 @@ var tslib = require('tslib');
|
|
|
23
21
|
/**
|
|
24
22
|
* @fileoverview Firebase constants. Some of these (@defines) can be overridden at compile-time.
|
|
25
23
|
*/
|
|
26
|
-
|
|
24
|
+
const CONSTANTS = {
|
|
27
25
|
/**
|
|
28
26
|
* @define {boolean} Whether this is the client Node.js SDK.
|
|
29
27
|
*/
|
|
@@ -57,7 +55,7 @@ var CONSTANTS = {
|
|
|
57
55
|
/**
|
|
58
56
|
* Throws an error if the provided assertion is falsy
|
|
59
57
|
*/
|
|
60
|
-
|
|
58
|
+
const assert = function (assertion, message) {
|
|
61
59
|
if (!assertion) {
|
|
62
60
|
throw assertionError(message);
|
|
63
61
|
}
|
|
@@ -65,7 +63,7 @@ var assert = function (assertion, message) {
|
|
|
65
63
|
/**
|
|
66
64
|
* Returns an Error object suitable for throwing.
|
|
67
65
|
*/
|
|
68
|
-
|
|
66
|
+
const assertionError = function (message) {
|
|
69
67
|
return new Error('Firebase Database (' +
|
|
70
68
|
CONSTANTS.SDK_VERSION +
|
|
71
69
|
') INTERNAL ASSERT FAILED: ' +
|
|
@@ -88,12 +86,12 @@ var assertionError = function (message) {
|
|
|
88
86
|
* See the License for the specific language governing permissions and
|
|
89
87
|
* limitations under the License.
|
|
90
88
|
*/
|
|
91
|
-
|
|
89
|
+
const stringToByteArray$1 = function (str) {
|
|
92
90
|
// TODO(user): Use native implementations if/when available
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
for (
|
|
96
|
-
|
|
91
|
+
const out = [];
|
|
92
|
+
let p = 0;
|
|
93
|
+
for (let i = 0; i < str.length; i++) {
|
|
94
|
+
let c = str.charCodeAt(i);
|
|
97
95
|
if (c < 128) {
|
|
98
96
|
out[p++] = c;
|
|
99
97
|
}
|
|
@@ -125,32 +123,32 @@ var stringToByteArray$1 = function (str) {
|
|
|
125
123
|
* @param bytes Array of numbers representing characters.
|
|
126
124
|
* @return Stringification of the array.
|
|
127
125
|
*/
|
|
128
|
-
|
|
126
|
+
const byteArrayToString = function (bytes) {
|
|
129
127
|
// TODO(user): Use native implementations if/when available
|
|
130
|
-
|
|
131
|
-
|
|
128
|
+
const out = [];
|
|
129
|
+
let pos = 0, c = 0;
|
|
132
130
|
while (pos < bytes.length) {
|
|
133
|
-
|
|
131
|
+
const c1 = bytes[pos++];
|
|
134
132
|
if (c1 < 128) {
|
|
135
133
|
out[c++] = String.fromCharCode(c1);
|
|
136
134
|
}
|
|
137
135
|
else if (c1 > 191 && c1 < 224) {
|
|
138
|
-
|
|
136
|
+
const c2 = bytes[pos++];
|
|
139
137
|
out[c++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
|
|
140
138
|
}
|
|
141
139
|
else if (c1 > 239 && c1 < 365) {
|
|
142
140
|
// Surrogate Pair
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
141
|
+
const c2 = bytes[pos++];
|
|
142
|
+
const c3 = bytes[pos++];
|
|
143
|
+
const c4 = bytes[pos++];
|
|
144
|
+
const u = (((c1 & 7) << 18) | ((c2 & 63) << 12) | ((c3 & 63) << 6) | (c4 & 63)) -
|
|
147
145
|
0x10000;
|
|
148
146
|
out[c++] = String.fromCharCode(0xd800 + (u >> 10));
|
|
149
147
|
out[c++] = String.fromCharCode(0xdc00 + (u & 1023));
|
|
150
148
|
}
|
|
151
149
|
else {
|
|
152
|
-
|
|
153
|
-
|
|
150
|
+
const c2 = bytes[pos++];
|
|
151
|
+
const c3 = bytes[pos++];
|
|
154
152
|
out[c++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
|
155
153
|
}
|
|
156
154
|
}
|
|
@@ -159,7 +157,8 @@ var byteArrayToString = function (bytes) {
|
|
|
159
157
|
// We define it as an object literal instead of a class because a class compiled down to es5 can't
|
|
160
158
|
// be treeshaked. https://github.com/rollup/rollup/issues/1691
|
|
161
159
|
// Static lookup maps, lazily populated by init_()
|
|
162
|
-
|
|
160
|
+
// TODO(dlarocque): Define this as a class, since we no longer target ES5.
|
|
161
|
+
const base64 = {
|
|
163
162
|
/**
|
|
164
163
|
* Maps bytes to characters.
|
|
165
164
|
*/
|
|
@@ -212,25 +211,25 @@ var base64 = {
|
|
|
212
211
|
* alternative alphabet.
|
|
213
212
|
* @return The base64 encoded string.
|
|
214
213
|
*/
|
|
215
|
-
encodeByteArray
|
|
214
|
+
encodeByteArray(input, webSafe) {
|
|
216
215
|
if (!Array.isArray(input)) {
|
|
217
216
|
throw Error('encodeByteArray takes an array as a parameter');
|
|
218
217
|
}
|
|
219
218
|
this.init_();
|
|
220
|
-
|
|
219
|
+
const byteToCharMap = webSafe
|
|
221
220
|
? this.byteToCharMapWebSafe_
|
|
222
221
|
: this.byteToCharMap_;
|
|
223
|
-
|
|
224
|
-
for (
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
222
|
+
const output = [];
|
|
223
|
+
for (let i = 0; i < input.length; i += 3) {
|
|
224
|
+
const byte1 = input[i];
|
|
225
|
+
const haveByte2 = i + 1 < input.length;
|
|
226
|
+
const byte2 = haveByte2 ? input[i + 1] : 0;
|
|
227
|
+
const haveByte3 = i + 2 < input.length;
|
|
228
|
+
const byte3 = haveByte3 ? input[i + 2] : 0;
|
|
229
|
+
const outByte1 = byte1 >> 2;
|
|
230
|
+
const outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4);
|
|
231
|
+
let outByte3 = ((byte2 & 0x0f) << 2) | (byte3 >> 6);
|
|
232
|
+
let outByte4 = byte3 & 0x3f;
|
|
234
233
|
if (!haveByte3) {
|
|
235
234
|
outByte4 = 64;
|
|
236
235
|
if (!haveByte2) {
|
|
@@ -249,7 +248,7 @@ var base64 = {
|
|
|
249
248
|
* alternative alphabet.
|
|
250
249
|
* @return The base64 encoded string.
|
|
251
250
|
*/
|
|
252
|
-
encodeString
|
|
251
|
+
encodeString(input, webSafe) {
|
|
253
252
|
// Shortcut for Mozilla browsers that implement
|
|
254
253
|
// a native base64 encoder in the form of "btoa/atob"
|
|
255
254
|
if (this.HAS_NATIVE_SUPPORT && !webSafe) {
|
|
@@ -265,7 +264,7 @@ var base64 = {
|
|
|
265
264
|
* alternative alphabet.
|
|
266
265
|
* @return string representing the decoded value.
|
|
267
266
|
*/
|
|
268
|
-
decodeString
|
|
267
|
+
decodeString(input, webSafe) {
|
|
269
268
|
// Shortcut for Mozilla browsers that implement
|
|
270
269
|
// a native base64 encoder in the form of "btoa/atob"
|
|
271
270
|
if (this.HAS_NATIVE_SUPPORT && !webSafe) {
|
|
@@ -288,33 +287,33 @@ var base64 = {
|
|
|
288
287
|
* @param webSafe True if we should use the web-safe alphabet.
|
|
289
288
|
* @return bytes representing the decoded value.
|
|
290
289
|
*/
|
|
291
|
-
decodeStringToByteArray
|
|
290
|
+
decodeStringToByteArray(input, webSafe) {
|
|
292
291
|
this.init_();
|
|
293
|
-
|
|
292
|
+
const charToByteMap = webSafe
|
|
294
293
|
? this.charToByteMapWebSafe_
|
|
295
294
|
: this.charToByteMap_;
|
|
296
|
-
|
|
297
|
-
for (
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
295
|
+
const output = [];
|
|
296
|
+
for (let i = 0; i < input.length;) {
|
|
297
|
+
const byte1 = charToByteMap[input.charAt(i++)];
|
|
298
|
+
const haveByte2 = i < input.length;
|
|
299
|
+
const byte2 = haveByte2 ? charToByteMap[input.charAt(i)] : 0;
|
|
301
300
|
++i;
|
|
302
|
-
|
|
303
|
-
|
|
301
|
+
const haveByte3 = i < input.length;
|
|
302
|
+
const byte3 = haveByte3 ? charToByteMap[input.charAt(i)] : 64;
|
|
304
303
|
++i;
|
|
305
|
-
|
|
306
|
-
|
|
304
|
+
const haveByte4 = i < input.length;
|
|
305
|
+
const byte4 = haveByte4 ? charToByteMap[input.charAt(i)] : 64;
|
|
307
306
|
++i;
|
|
308
307
|
if (byte1 == null || byte2 == null || byte3 == null || byte4 == null) {
|
|
309
308
|
throw new DecodeBase64StringError();
|
|
310
309
|
}
|
|
311
|
-
|
|
310
|
+
const outByte1 = (byte1 << 2) | (byte2 >> 4);
|
|
312
311
|
output.push(outByte1);
|
|
313
312
|
if (byte3 !== 64) {
|
|
314
|
-
|
|
313
|
+
const outByte2 = ((byte2 << 4) & 0xf0) | (byte3 >> 2);
|
|
315
314
|
output.push(outByte2);
|
|
316
315
|
if (byte4 !== 64) {
|
|
317
|
-
|
|
316
|
+
const outByte3 = ((byte3 << 6) & 0xc0) | byte4;
|
|
318
317
|
output.push(outByte3);
|
|
319
318
|
}
|
|
320
319
|
}
|
|
@@ -326,14 +325,14 @@ var base64 = {
|
|
|
326
325
|
* accessing any of the static map variables.
|
|
327
326
|
* @private
|
|
328
327
|
*/
|
|
329
|
-
init_
|
|
328
|
+
init_() {
|
|
330
329
|
if (!this.byteToCharMap_) {
|
|
331
330
|
this.byteToCharMap_ = {};
|
|
332
331
|
this.charToByteMap_ = {};
|
|
333
332
|
this.byteToCharMapWebSafe_ = {};
|
|
334
333
|
this.charToByteMapWebSafe_ = {};
|
|
335
334
|
// We want quick mappings back and forth, so we precompute two maps.
|
|
336
|
-
for (
|
|
335
|
+
for (let i = 0; i < this.ENCODED_VALS.length; i++) {
|
|
337
336
|
this.byteToCharMap_[i] = this.ENCODED_VALS.charAt(i);
|
|
338
337
|
this.charToByteMap_[this.byteToCharMap_[i]] = i;
|
|
339
338
|
this.byteToCharMapWebSafe_[i] = this.ENCODED_VALS_WEBSAFE.charAt(i);
|
|
@@ -350,27 +349,24 @@ var base64 = {
|
|
|
350
349
|
/**
|
|
351
350
|
* An error encountered while decoding base64 string.
|
|
352
351
|
*/
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
_this.name = 'DecodeBase64StringError';
|
|
358
|
-
return _this;
|
|
352
|
+
class DecodeBase64StringError extends Error {
|
|
353
|
+
constructor() {
|
|
354
|
+
super(...arguments);
|
|
355
|
+
this.name = 'DecodeBase64StringError';
|
|
359
356
|
}
|
|
360
|
-
|
|
361
|
-
}(Error));
|
|
357
|
+
}
|
|
362
358
|
/**
|
|
363
359
|
* URL-safe base64 encoding
|
|
364
360
|
*/
|
|
365
|
-
|
|
366
|
-
|
|
361
|
+
const base64Encode = function (str) {
|
|
362
|
+
const utf8Bytes = stringToByteArray$1(str);
|
|
367
363
|
return base64.encodeByteArray(utf8Bytes, true);
|
|
368
364
|
};
|
|
369
365
|
/**
|
|
370
366
|
* URL-safe base64 encoding (without "." padding in the end).
|
|
371
367
|
* e.g. Used in JSON Web Token (JWT) parts.
|
|
372
368
|
*/
|
|
373
|
-
|
|
369
|
+
const base64urlEncodeWithoutPadding = function (str) {
|
|
374
370
|
// Use base64url encoding and remove padding in the end (dot characters).
|
|
375
371
|
return base64Encode(str).replace(/\./g, '');
|
|
376
372
|
};
|
|
@@ -383,7 +379,7 @@ var base64urlEncodeWithoutPadding = function (str) {
|
|
|
383
379
|
* @param str To be decoded
|
|
384
380
|
* @return Decoded result, if possible
|
|
385
381
|
*/
|
|
386
|
-
|
|
382
|
+
const base64Decode = function (str) {
|
|
387
383
|
try {
|
|
388
384
|
return base64.decodeString(str, true);
|
|
389
385
|
}
|
|
@@ -437,7 +433,7 @@ function deepExtend(target, source) {
|
|
|
437
433
|
case Date:
|
|
438
434
|
// Treat Dates like scalars; if the target date object had any child
|
|
439
435
|
// properties - they will be lost!
|
|
440
|
-
|
|
436
|
+
const dateValue = source;
|
|
441
437
|
return new Date(dateValue.getTime());
|
|
442
438
|
case Object:
|
|
443
439
|
if (target === undefined) {
|
|
@@ -452,7 +448,7 @@ function deepExtend(target, source) {
|
|
|
452
448
|
// Not a plain Object - treat it as a scalar.
|
|
453
449
|
return source;
|
|
454
450
|
}
|
|
455
|
-
for (
|
|
451
|
+
for (const prop in source) {
|
|
456
452
|
// use isValidKey to guard against prototype pollution. See https://snyk.io/vuln/SNYK-JS-LODASH-450202
|
|
457
453
|
if (!source.hasOwnProperty(prop) || !isValidKey(prop)) {
|
|
458
454
|
continue;
|
|
@@ -515,9 +511,7 @@ function getGlobal() {
|
|
|
515
511
|
* See the License for the specific language governing permissions and
|
|
516
512
|
* limitations under the License.
|
|
517
513
|
*/
|
|
518
|
-
|
|
519
|
-
return getGlobal().__FIREBASE_DEFAULTS__;
|
|
520
|
-
};
|
|
514
|
+
const getDefaultsFromGlobal = () => getGlobal().__FIREBASE_DEFAULTS__;
|
|
521
515
|
/**
|
|
522
516
|
* Attempt to read defaults from a JSON string provided to
|
|
523
517
|
* process(.)env(.)__FIREBASE_DEFAULTS__ or a JSON file whose path is in
|
|
@@ -526,20 +520,20 @@ var getDefaultsFromGlobal = function () {
|
|
|
526
520
|
* handle seeing that variable in comments.
|
|
527
521
|
* See https://github.com/firebase/firebase-js-sdk/issues/6838
|
|
528
522
|
*/
|
|
529
|
-
|
|
523
|
+
const getDefaultsFromEnvVariable = () => {
|
|
530
524
|
if (typeof process === 'undefined' || typeof process.env === 'undefined') {
|
|
531
525
|
return;
|
|
532
526
|
}
|
|
533
|
-
|
|
527
|
+
const defaultsJsonString = process.env.__FIREBASE_DEFAULTS__;
|
|
534
528
|
if (defaultsJsonString) {
|
|
535
529
|
return JSON.parse(defaultsJsonString);
|
|
536
530
|
}
|
|
537
531
|
};
|
|
538
|
-
|
|
532
|
+
const getDefaultsFromCookie = () => {
|
|
539
533
|
if (typeof document === 'undefined') {
|
|
540
534
|
return;
|
|
541
535
|
}
|
|
542
|
-
|
|
536
|
+
let match;
|
|
543
537
|
try {
|
|
544
538
|
match = document.cookie.match(/__FIREBASE_DEFAULTS__=([^;]+)/);
|
|
545
539
|
}
|
|
@@ -548,7 +542,7 @@ var getDefaultsFromCookie = function () {
|
|
|
548
542
|
// `document` object but error on accessing `document.cookie`.
|
|
549
543
|
return;
|
|
550
544
|
}
|
|
551
|
-
|
|
545
|
+
const decoded = match && base64Decode(match[1]);
|
|
552
546
|
return decoded && JSON.parse(decoded);
|
|
553
547
|
};
|
|
554
548
|
/**
|
|
@@ -558,7 +552,7 @@ var getDefaultsFromCookie = function () {
|
|
|
558
552
|
* (3) if such an object exists in a cookie
|
|
559
553
|
* @public
|
|
560
554
|
*/
|
|
561
|
-
|
|
555
|
+
const getDefaults = () => {
|
|
562
556
|
try {
|
|
563
557
|
return (getDefaultsFromGlobal() ||
|
|
564
558
|
getDefaultsFromEnvVariable() ||
|
|
@@ -571,7 +565,7 @@ var getDefaults = function () {
|
|
|
571
565
|
* info instead of swallowing so we can find these unknown cases
|
|
572
566
|
* and add paths for them if needed.
|
|
573
567
|
*/
|
|
574
|
-
console.info(
|
|
568
|
+
console.info(`Unable to get __FIREBASE_DEFAULTS__ due to: ${e}`);
|
|
575
569
|
return;
|
|
576
570
|
}
|
|
577
571
|
};
|
|
@@ -581,24 +575,24 @@ var getDefaults = function () {
|
|
|
581
575
|
* @returns a URL host formatted like `127.0.0.1:9999` or `[::1]:4000` if available
|
|
582
576
|
* @public
|
|
583
577
|
*/
|
|
584
|
-
|
|
578
|
+
const getDefaultEmulatorHost = (productName) => { var _a, _b; return (_b = (_a = getDefaults()) === null || _a === void 0 ? void 0 : _a.emulatorHosts) === null || _b === void 0 ? void 0 : _b[productName]; };
|
|
585
579
|
/**
|
|
586
580
|
* Returns emulator hostname and port stored in the __FIREBASE_DEFAULTS__ object
|
|
587
581
|
* for the given product.
|
|
588
582
|
* @returns a pair of hostname and port like `["::1", 4000]` if available
|
|
589
583
|
* @public
|
|
590
584
|
*/
|
|
591
|
-
|
|
592
|
-
|
|
585
|
+
const getDefaultEmulatorHostnameAndPort = (productName) => {
|
|
586
|
+
const host = getDefaultEmulatorHost(productName);
|
|
593
587
|
if (!host) {
|
|
594
588
|
return undefined;
|
|
595
589
|
}
|
|
596
|
-
|
|
590
|
+
const separatorIndex = host.lastIndexOf(':'); // Finding the last since IPv6 addr also has colons.
|
|
597
591
|
if (separatorIndex <= 0 || separatorIndex + 1 === host.length) {
|
|
598
|
-
throw new Error(
|
|
592
|
+
throw new Error(`Invalid host ${host} with no separate hostname and port!`);
|
|
599
593
|
}
|
|
600
594
|
// eslint-disable-next-line no-restricted-globals
|
|
601
|
-
|
|
595
|
+
const port = parseInt(host.substring(separatorIndex + 1), 10);
|
|
602
596
|
if (host[0] === '[') {
|
|
603
597
|
// Bracket-quoted `[ipv6addr]:port` => return "ipv6addr" (without brackets).
|
|
604
598
|
return [host.substring(1, separatorIndex - 1), port];
|
|
@@ -611,13 +605,13 @@ var getDefaultEmulatorHostnameAndPort = function (productName) {
|
|
|
611
605
|
* Returns Firebase app config stored in the __FIREBASE_DEFAULTS__ object.
|
|
612
606
|
* @public
|
|
613
607
|
*/
|
|
614
|
-
|
|
608
|
+
const getDefaultAppConfig = () => { var _a; return (_a = getDefaults()) === null || _a === void 0 ? void 0 : _a.config; };
|
|
615
609
|
/**
|
|
616
610
|
* Returns an experimental setting on the __FIREBASE_DEFAULTS__ object (properties
|
|
617
611
|
* prefixed by "_")
|
|
618
612
|
* @public
|
|
619
613
|
*/
|
|
620
|
-
|
|
614
|
+
const getExperimentalSetting = (name) => { var _a; return (_a = getDefaults()) === null || _a === void 0 ? void 0 : _a[`_${name}`]; };
|
|
621
615
|
|
|
622
616
|
/**
|
|
623
617
|
* @license
|
|
@@ -635,14 +629,13 @@ var getExperimentalSetting = function (name) { var _a; return (_a = getDefaults(
|
|
|
635
629
|
* See the License for the specific language governing permissions and
|
|
636
630
|
* limitations under the License.
|
|
637
631
|
*/
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
this.
|
|
642
|
-
this.
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
_this.reject = reject;
|
|
632
|
+
class Deferred {
|
|
633
|
+
constructor() {
|
|
634
|
+
this.reject = () => { };
|
|
635
|
+
this.resolve = () => { };
|
|
636
|
+
this.promise = new Promise((resolve, reject) => {
|
|
637
|
+
this.resolve = resolve;
|
|
638
|
+
this.reject = reject;
|
|
646
639
|
});
|
|
647
640
|
}
|
|
648
641
|
/**
|
|
@@ -650,19 +643,18 @@ var Deferred = /** @class */ (function () {
|
|
|
650
643
|
* invoking promises inline, which Promises are forbidden to do. This method accepts an optional node-style callback
|
|
651
644
|
* and returns a node-style callback which will resolve or reject the Deferred's promise.
|
|
652
645
|
*/
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
return function (error, value) {
|
|
646
|
+
wrapCallback(callback) {
|
|
647
|
+
return (error, value) => {
|
|
656
648
|
if (error) {
|
|
657
|
-
|
|
649
|
+
this.reject(error);
|
|
658
650
|
}
|
|
659
651
|
else {
|
|
660
|
-
|
|
652
|
+
this.resolve(value);
|
|
661
653
|
}
|
|
662
654
|
if (typeof callback === 'function') {
|
|
663
655
|
// Attaching noop handler just in case developer wasn't expecting
|
|
664
656
|
// promises
|
|
665
|
-
|
|
657
|
+
this.promise.catch(() => { });
|
|
666
658
|
// Some of our callbacks don't expect a value and our own tests
|
|
667
659
|
// assert that the parameter length is 1
|
|
668
660
|
if (callback.length === 1) {
|
|
@@ -673,9 +665,8 @@ var Deferred = /** @class */ (function () {
|
|
|
673
665
|
}
|
|
674
666
|
}
|
|
675
667
|
};
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
}());
|
|
668
|
+
}
|
|
669
|
+
}
|
|
679
670
|
|
|
680
671
|
/**
|
|
681
672
|
* @license
|
|
@@ -698,24 +689,24 @@ function createMockUserToken(token, projectId) {
|
|
|
698
689
|
throw new Error('The "uid" field is no longer supported by mockUserToken. Please use "sub" instead for Firebase Auth User ID.');
|
|
699
690
|
}
|
|
700
691
|
// Unsecured JWTs use "none" as the algorithm.
|
|
701
|
-
|
|
692
|
+
const header = {
|
|
702
693
|
alg: 'none',
|
|
703
694
|
type: 'JWT'
|
|
704
695
|
};
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
696
|
+
const project = projectId || 'demo-project';
|
|
697
|
+
const iat = token.iat || 0;
|
|
698
|
+
const sub = token.sub || token.user_id;
|
|
708
699
|
if (!sub) {
|
|
709
700
|
throw new Error("mockUserToken must contain 'sub' or 'user_id' field!");
|
|
710
701
|
}
|
|
711
|
-
|
|
702
|
+
const payload = Object.assign({
|
|
712
703
|
// Set all required fields to decent defaults
|
|
713
|
-
iss:
|
|
704
|
+
iss: `https://securetoken.google.com/${project}`, aud: project, iat, exp: iat + 3600, auth_time: iat, sub, user_id: sub, firebase: {
|
|
714
705
|
sign_in_provider: 'custom',
|
|
715
706
|
identities: {}
|
|
716
707
|
} }, token);
|
|
717
708
|
// Unsecured JWTs use the empty string as a signature.
|
|
718
|
-
|
|
709
|
+
const signature = '';
|
|
719
710
|
return [
|
|
720
711
|
base64urlEncodeWithoutPadding(JSON.stringify(header)),
|
|
721
712
|
base64urlEncodeWithoutPadding(JSON.stringify(payload)),
|
|
@@ -774,7 +765,7 @@ function isMobileCordova() {
|
|
|
774
765
|
// Node detection logic from: https://github.com/iliakan/detect-node/
|
|
775
766
|
function isNode() {
|
|
776
767
|
var _a;
|
|
777
|
-
|
|
768
|
+
const forceEnvironment = (_a = getDefaults()) === null || _a === void 0 ? void 0 : _a.forceEnvironment;
|
|
778
769
|
if (forceEnvironment === 'node') {
|
|
779
770
|
return true;
|
|
780
771
|
}
|
|
@@ -813,7 +804,7 @@ function isCloudflareWorker() {
|
|
|
813
804
|
navigator.userAgent === 'Cloudflare-Workers');
|
|
814
805
|
}
|
|
815
806
|
function isBrowserExtension() {
|
|
816
|
-
|
|
807
|
+
const runtime = typeof chrome === 'object'
|
|
817
808
|
? chrome.runtime
|
|
818
809
|
: typeof browser === 'object'
|
|
819
810
|
? browser.runtime
|
|
@@ -834,7 +825,7 @@ function isElectron() {
|
|
|
834
825
|
}
|
|
835
826
|
/** Detects Internet Explorer. */
|
|
836
827
|
function isIE() {
|
|
837
|
-
|
|
828
|
+
const ua = getUA();
|
|
838
829
|
return ua.indexOf('MSIE ') >= 0 || ua.indexOf('Trident/') >= 0;
|
|
839
830
|
}
|
|
840
831
|
/** Detects Universal Windows Platform apps. */
|
|
@@ -876,25 +867,25 @@ function isIndexedDBAvailable() {
|
|
|
876
867
|
* private browsing)
|
|
877
868
|
*/
|
|
878
869
|
function validateIndexedDBOpenable() {
|
|
879
|
-
return new Promise(
|
|
870
|
+
return new Promise((resolve, reject) => {
|
|
880
871
|
try {
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
872
|
+
let preExist = true;
|
|
873
|
+
const DB_CHECK_NAME = 'validate-browser-context-for-indexeddb-analytics-module';
|
|
874
|
+
const request = self.indexedDB.open(DB_CHECK_NAME);
|
|
875
|
+
request.onsuccess = () => {
|
|
876
|
+
request.result.close();
|
|
886
877
|
// delete database only when it doesn't pre-exist
|
|
887
|
-
if (!
|
|
888
|
-
self.indexedDB.deleteDatabase(
|
|
878
|
+
if (!preExist) {
|
|
879
|
+
self.indexedDB.deleteDatabase(DB_CHECK_NAME);
|
|
889
880
|
}
|
|
890
881
|
resolve(true);
|
|
891
882
|
};
|
|
892
|
-
|
|
893
|
-
|
|
883
|
+
request.onupgradeneeded = () => {
|
|
884
|
+
preExist = false;
|
|
894
885
|
};
|
|
895
|
-
|
|
886
|
+
request.onerror = () => {
|
|
896
887
|
var _a;
|
|
897
|
-
reject(((_a =
|
|
888
|
+
reject(((_a = request.error) === null || _a === void 0 ? void 0 : _a.message) || '');
|
|
898
889
|
};
|
|
899
890
|
}
|
|
900
891
|
catch (error) {
|
|
@@ -930,62 +921,96 @@ function areCookiesEnabled() {
|
|
|
930
921
|
* See the License for the specific language governing permissions and
|
|
931
922
|
* limitations under the License.
|
|
932
923
|
*/
|
|
933
|
-
|
|
924
|
+
/**
|
|
925
|
+
* @fileoverview Standardized Firebase Error.
|
|
926
|
+
*
|
|
927
|
+
* Usage:
|
|
928
|
+
*
|
|
929
|
+
* // TypeScript string literals for type-safe codes
|
|
930
|
+
* type Err =
|
|
931
|
+
* 'unknown' |
|
|
932
|
+
* 'object-not-found'
|
|
933
|
+
* ;
|
|
934
|
+
*
|
|
935
|
+
* // Closure enum for type-safe error codes
|
|
936
|
+
* // at-enum {string}
|
|
937
|
+
* var Err = {
|
|
938
|
+
* UNKNOWN: 'unknown',
|
|
939
|
+
* OBJECT_NOT_FOUND: 'object-not-found',
|
|
940
|
+
* }
|
|
941
|
+
*
|
|
942
|
+
* let errors: Map<Err, string> = {
|
|
943
|
+
* 'generic-error': "Unknown error",
|
|
944
|
+
* 'file-not-found': "Could not find file: {$file}",
|
|
945
|
+
* };
|
|
946
|
+
*
|
|
947
|
+
* // Type-safe function - must pass a valid error code as param.
|
|
948
|
+
* let error = new ErrorFactory<Err>('service', 'Service', errors);
|
|
949
|
+
*
|
|
950
|
+
* ...
|
|
951
|
+
* throw error.create(Err.GENERIC);
|
|
952
|
+
* ...
|
|
953
|
+
* throw error.create(Err.FILE_NOT_FOUND, {'file': fileName});
|
|
954
|
+
* ...
|
|
955
|
+
* // Service: Could not file file: foo.txt (service/file-not-found).
|
|
956
|
+
*
|
|
957
|
+
* catch (e) {
|
|
958
|
+
* assert(e.message === "Could not find file: foo.txt.");
|
|
959
|
+
* if ((e as FirebaseError)?.code === 'service/file-not-found') {
|
|
960
|
+
* console.log("Could not read file: " + e['file']);
|
|
961
|
+
* }
|
|
962
|
+
* }
|
|
963
|
+
*/
|
|
964
|
+
const ERROR_NAME = 'FirebaseError';
|
|
934
965
|
// Based on code from:
|
|
935
966
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
function FirebaseError(
|
|
967
|
+
class FirebaseError extends Error {
|
|
968
|
+
constructor(
|
|
939
969
|
/** The error code for this error. */
|
|
940
970
|
code, message,
|
|
941
971
|
/** Custom data for this error. */
|
|
942
972
|
customData) {
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
973
|
+
super(message);
|
|
974
|
+
this.code = code;
|
|
975
|
+
this.customData = customData;
|
|
946
976
|
/** The custom name for all FirebaseErrors. */
|
|
947
|
-
|
|
977
|
+
this.name = ERROR_NAME;
|
|
948
978
|
// Fix For ES5
|
|
949
979
|
// https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
|
|
950
|
-
|
|
980
|
+
// TODO(dlarocque): Replace this with `new.target`: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
|
|
981
|
+
// which we can now use since we no longer target ES5.
|
|
982
|
+
Object.setPrototypeOf(this, FirebaseError.prototype);
|
|
951
983
|
// Maintains proper stack trace for where our error was thrown.
|
|
952
984
|
// Only available on V8.
|
|
953
985
|
if (Error.captureStackTrace) {
|
|
954
|
-
Error.captureStackTrace(
|
|
986
|
+
Error.captureStackTrace(this, ErrorFactory.prototype.create);
|
|
955
987
|
}
|
|
956
|
-
return _this;
|
|
957
988
|
}
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
function ErrorFactory(service, serviceName, errors) {
|
|
989
|
+
}
|
|
990
|
+
class ErrorFactory {
|
|
991
|
+
constructor(service, serviceName, errors) {
|
|
962
992
|
this.service = service;
|
|
963
993
|
this.serviceName = serviceName;
|
|
964
994
|
this.errors = errors;
|
|
965
995
|
}
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
var customData = data[0] || {};
|
|
972
|
-
var fullCode = "".concat(this.service, "/").concat(code);
|
|
973
|
-
var template = this.errors[code];
|
|
974
|
-
var message = template ? replaceTemplate(template, customData) : 'Error';
|
|
996
|
+
create(code, ...data) {
|
|
997
|
+
const customData = data[0] || {};
|
|
998
|
+
const fullCode = `${this.service}/${code}`;
|
|
999
|
+
const template = this.errors[code];
|
|
1000
|
+
const message = template ? replaceTemplate(template, customData) : 'Error';
|
|
975
1001
|
// Service Name: Error message (service/code).
|
|
976
|
-
|
|
977
|
-
|
|
1002
|
+
const fullMessage = `${this.serviceName}: ${message} (${fullCode}).`;
|
|
1003
|
+
const error = new FirebaseError(fullCode, fullMessage, customData);
|
|
978
1004
|
return error;
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
}());
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
982
1007
|
function replaceTemplate(template, data) {
|
|
983
|
-
return template.replace(PATTERN,
|
|
984
|
-
|
|
985
|
-
return value != null ? String(value) :
|
|
1008
|
+
return template.replace(PATTERN, (_, key) => {
|
|
1009
|
+
const value = data[key];
|
|
1010
|
+
return value != null ? String(value) : `<${key}?>`;
|
|
986
1011
|
});
|
|
987
1012
|
}
|
|
988
|
-
|
|
1013
|
+
const PATTERN = /\{\$([^}]+)}/g;
|
|
989
1014
|
|
|
990
1015
|
/**
|
|
991
1016
|
* @license
|
|
@@ -1044,10 +1069,10 @@ function stringify(data) {
|
|
|
1044
1069
|
* - May return with invalid / incomplete claims if there's no native base64 decoding support.
|
|
1045
1070
|
* - Doesn't check if the token is actually valid.
|
|
1046
1071
|
*/
|
|
1047
|
-
|
|
1048
|
-
|
|
1072
|
+
const decode = function (token) {
|
|
1073
|
+
let header = {}, claims = {}, data = {}, signature = '';
|
|
1049
1074
|
try {
|
|
1050
|
-
|
|
1075
|
+
const parts = token.split('.');
|
|
1051
1076
|
header = jsonEval(base64Decode(parts[0]) || '');
|
|
1052
1077
|
claims = jsonEval(base64Decode(parts[1]) || '');
|
|
1053
1078
|
signature = parts[2];
|
|
@@ -1056,10 +1081,10 @@ var decode = function (token) {
|
|
|
1056
1081
|
}
|
|
1057
1082
|
catch (e) { }
|
|
1058
1083
|
return {
|
|
1059
|
-
header
|
|
1060
|
-
claims
|
|
1061
|
-
data
|
|
1062
|
-
signature
|
|
1084
|
+
header,
|
|
1085
|
+
claims,
|
|
1086
|
+
data,
|
|
1087
|
+
signature
|
|
1063
1088
|
};
|
|
1064
1089
|
};
|
|
1065
1090
|
/**
|
|
@@ -1070,10 +1095,10 @@ var decode = function (token) {
|
|
|
1070
1095
|
* - May return a false negative if there's no native base64 decoding support.
|
|
1071
1096
|
* - Doesn't check if the token is actually valid.
|
|
1072
1097
|
*/
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1098
|
+
const isValidTimestamp = function (token) {
|
|
1099
|
+
const claims = decode(token).claims;
|
|
1100
|
+
const now = Math.floor(new Date().getTime() / 1000);
|
|
1101
|
+
let validSince = 0, validUntil = 0;
|
|
1077
1102
|
if (typeof claims === 'object') {
|
|
1078
1103
|
if (claims.hasOwnProperty('nbf')) {
|
|
1079
1104
|
validSince = claims['nbf'];
|
|
@@ -1102,8 +1127,8 @@ var isValidTimestamp = function (token) {
|
|
|
1102
1127
|
* - May return null if there's no native base64 decoding support.
|
|
1103
1128
|
* - Doesn't check if the token is actually valid.
|
|
1104
1129
|
*/
|
|
1105
|
-
|
|
1106
|
-
|
|
1130
|
+
const issuedAtTime = function (token) {
|
|
1131
|
+
const claims = decode(token).claims;
|
|
1107
1132
|
if (typeof claims === 'object' && claims.hasOwnProperty('iat')) {
|
|
1108
1133
|
return claims['iat'];
|
|
1109
1134
|
}
|
|
@@ -1116,8 +1141,8 @@ var issuedAtTime = function (token) {
|
|
|
1116
1141
|
* - May return a false negative if there's no native base64 decoding support.
|
|
1117
1142
|
* - Doesn't check if the token is actually valid.
|
|
1118
1143
|
*/
|
|
1119
|
-
|
|
1120
|
-
|
|
1144
|
+
const isValidFormat = function (token) {
|
|
1145
|
+
const decoded = decode(token), claims = decoded.claims;
|
|
1121
1146
|
return !!claims && typeof claims === 'object' && claims.hasOwnProperty('iat');
|
|
1122
1147
|
};
|
|
1123
1148
|
/**
|
|
@@ -1127,8 +1152,8 @@ var isValidFormat = function (token) {
|
|
|
1127
1152
|
* - May return a false negative if there's no native base64 decoding support.
|
|
1128
1153
|
* - Doesn't check if the token is actually valid.
|
|
1129
1154
|
*/
|
|
1130
|
-
|
|
1131
|
-
|
|
1155
|
+
const isAdmin = function (token) {
|
|
1156
|
+
const claims = decode(token).claims;
|
|
1132
1157
|
return typeof claims === 'object' && claims['admin'] === true;
|
|
1133
1158
|
};
|
|
1134
1159
|
|
|
@@ -1160,7 +1185,7 @@ function safeGet(obj, key) {
|
|
|
1160
1185
|
}
|
|
1161
1186
|
}
|
|
1162
1187
|
function isEmpty(obj) {
|
|
1163
|
-
for (
|
|
1188
|
+
for (const key in obj) {
|
|
1164
1189
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
1165
1190
|
return false;
|
|
1166
1191
|
}
|
|
@@ -1168,8 +1193,8 @@ function isEmpty(obj) {
|
|
|
1168
1193
|
return true;
|
|
1169
1194
|
}
|
|
1170
1195
|
function map(obj, fn, contextObj) {
|
|
1171
|
-
|
|
1172
|
-
for (
|
|
1196
|
+
const res = {};
|
|
1197
|
+
for (const key in obj) {
|
|
1173
1198
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
1174
1199
|
res[key] = fn.call(contextObj, obj[key], key, obj);
|
|
1175
1200
|
}
|
|
@@ -1183,15 +1208,14 @@ function deepEqual(a, b) {
|
|
|
1183
1208
|
if (a === b) {
|
|
1184
1209
|
return true;
|
|
1185
1210
|
}
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
for (
|
|
1189
|
-
var k = aKeys_1[_i];
|
|
1211
|
+
const aKeys = Object.keys(a);
|
|
1212
|
+
const bKeys = Object.keys(b);
|
|
1213
|
+
for (const k of aKeys) {
|
|
1190
1214
|
if (!bKeys.includes(k)) {
|
|
1191
1215
|
return false;
|
|
1192
1216
|
}
|
|
1193
|
-
|
|
1194
|
-
|
|
1217
|
+
const aProp = a[k];
|
|
1218
|
+
const bProp = b[k];
|
|
1195
1219
|
if (isObject(aProp) && isObject(bProp)) {
|
|
1196
1220
|
if (!deepEqual(aProp, bProp)) {
|
|
1197
1221
|
return false;
|
|
@@ -1201,8 +1225,7 @@ function deepEqual(a, b) {
|
|
|
1201
1225
|
return false;
|
|
1202
1226
|
}
|
|
1203
1227
|
}
|
|
1204
|
-
for (
|
|
1205
|
-
var k = bKeys_1[_a];
|
|
1228
|
+
for (const k of bKeys) {
|
|
1206
1229
|
if (!aKeys.includes(k)) {
|
|
1207
1230
|
return false;
|
|
1208
1231
|
}
|
|
@@ -1233,10 +1256,9 @@ function isObject(thing) {
|
|
|
1233
1256
|
* Rejects if the given promise doesn't resolve in timeInMS milliseconds.
|
|
1234
1257
|
* @internal
|
|
1235
1258
|
*/
|
|
1236
|
-
function promiseWithTimeout(promise, timeInMS) {
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
setTimeout(function () { return deferredPromise.reject('timeout!'); }, timeInMS);
|
|
1259
|
+
function promiseWithTimeout(promise, timeInMS = 2000) {
|
|
1260
|
+
const deferredPromise = new Deferred();
|
|
1261
|
+
setTimeout(() => deferredPromise.reject('timeout!'), timeInMS);
|
|
1240
1262
|
promise.then(deferredPromise.resolve, deferredPromise.reject);
|
|
1241
1263
|
return deferredPromise.promise;
|
|
1242
1264
|
}
|
|
@@ -1263,20 +1285,16 @@ function promiseWithTimeout(promise, timeInMS) {
|
|
|
1263
1285
|
* Note: You must prepend it with ? when adding it to a URL.
|
|
1264
1286
|
*/
|
|
1265
1287
|
function querystring(querystringParams) {
|
|
1266
|
-
|
|
1267
|
-
|
|
1288
|
+
const params = [];
|
|
1289
|
+
for (const [key, value] of Object.entries(querystringParams)) {
|
|
1268
1290
|
if (Array.isArray(value)) {
|
|
1269
|
-
value.forEach(
|
|
1291
|
+
value.forEach(arrayVal => {
|
|
1270
1292
|
params.push(encodeURIComponent(key) + '=' + encodeURIComponent(arrayVal));
|
|
1271
1293
|
});
|
|
1272
1294
|
}
|
|
1273
1295
|
else {
|
|
1274
1296
|
params.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
|
|
1275
1297
|
}
|
|
1276
|
-
};
|
|
1277
|
-
for (var _i = 0, _a = Object.entries(querystringParams); _i < _a.length; _i++) {
|
|
1278
|
-
var _b = _a[_i], key = _b[0], value = _b[1];
|
|
1279
|
-
_loop_1(key, value);
|
|
1280
1298
|
}
|
|
1281
1299
|
return params.length ? '&' + params.join('&') : '';
|
|
1282
1300
|
}
|
|
@@ -1285,11 +1303,11 @@ function querystring(querystringParams) {
|
|
|
1285
1303
|
* (e.g. {arg: 'val', arg2: 'val2'})
|
|
1286
1304
|
*/
|
|
1287
1305
|
function querystringDecode(querystring) {
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
tokens.forEach(
|
|
1306
|
+
const obj = {};
|
|
1307
|
+
const tokens = querystring.replace(/^\?/, '').split('&');
|
|
1308
|
+
tokens.forEach(token => {
|
|
1291
1309
|
if (token) {
|
|
1292
|
-
|
|
1310
|
+
const [key, value] = token.split('=');
|
|
1293
1311
|
obj[decodeURIComponent(key)] = decodeURIComponent(value);
|
|
1294
1312
|
}
|
|
1295
1313
|
});
|
|
@@ -1299,11 +1317,11 @@ function querystringDecode(querystring) {
|
|
|
1299
1317
|
* Extract the query string part of a URL, including the leading question mark (if present).
|
|
1300
1318
|
*/
|
|
1301
1319
|
function extractQuerystring(url) {
|
|
1302
|
-
|
|
1320
|
+
const queryStart = url.indexOf('?');
|
|
1303
1321
|
if (!queryStart) {
|
|
1304
1322
|
return '';
|
|
1305
1323
|
}
|
|
1306
|
-
|
|
1324
|
+
const fragmentStart = url.indexOf('#', queryStart);
|
|
1307
1325
|
return url.substring(queryStart, fragmentStart > 0 ? fragmentStart : undefined);
|
|
1308
1326
|
}
|
|
1309
1327
|
|
|
@@ -1346,8 +1364,8 @@ function extractQuerystring(url) {
|
|
|
1346
1364
|
* @final
|
|
1347
1365
|
* @struct
|
|
1348
1366
|
*/
|
|
1349
|
-
|
|
1350
|
-
|
|
1367
|
+
class Sha1 {
|
|
1368
|
+
constructor() {
|
|
1351
1369
|
/**
|
|
1352
1370
|
* Holds the previous values of accumulated variables a-e in the compress_
|
|
1353
1371
|
* function.
|
|
@@ -1380,12 +1398,12 @@ var Sha1 = /** @class */ (function () {
|
|
|
1380
1398
|
this.total_ = 0;
|
|
1381
1399
|
this.blockSize = 512 / 8;
|
|
1382
1400
|
this.pad_[0] = 128;
|
|
1383
|
-
for (
|
|
1401
|
+
for (let i = 1; i < this.blockSize; ++i) {
|
|
1384
1402
|
this.pad_[i] = 0;
|
|
1385
1403
|
}
|
|
1386
1404
|
this.reset();
|
|
1387
1405
|
}
|
|
1388
|
-
|
|
1406
|
+
reset() {
|
|
1389
1407
|
this.chain_[0] = 0x67452301;
|
|
1390
1408
|
this.chain_[1] = 0xefcdab89;
|
|
1391
1409
|
this.chain_[2] = 0x98badcfe;
|
|
@@ -1393,21 +1411,21 @@ var Sha1 = /** @class */ (function () {
|
|
|
1393
1411
|
this.chain_[4] = 0xc3d2e1f0;
|
|
1394
1412
|
this.inbuf_ = 0;
|
|
1395
1413
|
this.total_ = 0;
|
|
1396
|
-
}
|
|
1414
|
+
}
|
|
1397
1415
|
/**
|
|
1398
1416
|
* Internal compress helper function.
|
|
1399
1417
|
* @param buf Block to compress.
|
|
1400
1418
|
* @param offset Offset of the block in the buffer.
|
|
1401
1419
|
* @private
|
|
1402
1420
|
*/
|
|
1403
|
-
|
|
1421
|
+
compress_(buf, offset) {
|
|
1404
1422
|
if (!offset) {
|
|
1405
1423
|
offset = 0;
|
|
1406
1424
|
}
|
|
1407
|
-
|
|
1425
|
+
const W = this.W_;
|
|
1408
1426
|
// get 16 big endian words
|
|
1409
1427
|
if (typeof buf === 'string') {
|
|
1410
|
-
for (
|
|
1428
|
+
for (let i = 0; i < 16; i++) {
|
|
1411
1429
|
// TODO(user): [bug 8140122] Recent versions of Safari for Mac OS and iOS
|
|
1412
1430
|
// have a bug that turns the post-increment ++ operator into pre-increment
|
|
1413
1431
|
// during JIT compilation. We have code that depends heavily on SHA-1 for
|
|
@@ -1425,7 +1443,7 @@ var Sha1 = /** @class */ (function () {
|
|
|
1425
1443
|
}
|
|
1426
1444
|
}
|
|
1427
1445
|
else {
|
|
1428
|
-
for (
|
|
1446
|
+
for (let i = 0; i < 16; i++) {
|
|
1429
1447
|
W[i] =
|
|
1430
1448
|
(buf[offset] << 24) |
|
|
1431
1449
|
(buf[offset + 1] << 16) |
|
|
@@ -1435,18 +1453,18 @@ var Sha1 = /** @class */ (function () {
|
|
|
1435
1453
|
}
|
|
1436
1454
|
}
|
|
1437
1455
|
// expand to 80 words
|
|
1438
|
-
for (
|
|
1439
|
-
|
|
1456
|
+
for (let i = 16; i < 80; i++) {
|
|
1457
|
+
const t = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
|
|
1440
1458
|
W[i] = ((t << 1) | (t >>> 31)) & 0xffffffff;
|
|
1441
1459
|
}
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1460
|
+
let a = this.chain_[0];
|
|
1461
|
+
let b = this.chain_[1];
|
|
1462
|
+
let c = this.chain_[2];
|
|
1463
|
+
let d = this.chain_[3];
|
|
1464
|
+
let e = this.chain_[4];
|
|
1465
|
+
let f, k;
|
|
1448
1466
|
// TODO(user): Try to unroll this loop to speed up the computation.
|
|
1449
|
-
for (
|
|
1467
|
+
for (let i = 0; i < 80; i++) {
|
|
1450
1468
|
if (i < 40) {
|
|
1451
1469
|
if (i < 20) {
|
|
1452
1470
|
f = d ^ (b & (c ^ d));
|
|
@@ -1467,7 +1485,7 @@ var Sha1 = /** @class */ (function () {
|
|
|
1467
1485
|
k = 0xca62c1d6;
|
|
1468
1486
|
}
|
|
1469
1487
|
}
|
|
1470
|
-
|
|
1488
|
+
const t = (((a << 5) | (a >>> 27)) + f + e + k + W[i]) & 0xffffffff;
|
|
1471
1489
|
e = d;
|
|
1472
1490
|
d = c;
|
|
1473
1491
|
c = ((b << 30) | (b >>> 2)) & 0xffffffff;
|
|
@@ -1479,8 +1497,8 @@ var Sha1 = /** @class */ (function () {
|
|
|
1479
1497
|
this.chain_[2] = (this.chain_[2] + c) & 0xffffffff;
|
|
1480
1498
|
this.chain_[3] = (this.chain_[3] + d) & 0xffffffff;
|
|
1481
1499
|
this.chain_[4] = (this.chain_[4] + e) & 0xffffffff;
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1500
|
+
}
|
|
1501
|
+
update(bytes, length) {
|
|
1484
1502
|
// TODO(johnlenz): tighten the function signature and remove this check
|
|
1485
1503
|
if (bytes == null) {
|
|
1486
1504
|
return;
|
|
@@ -1488,11 +1506,11 @@ var Sha1 = /** @class */ (function () {
|
|
|
1488
1506
|
if (length === undefined) {
|
|
1489
1507
|
length = bytes.length;
|
|
1490
1508
|
}
|
|
1491
|
-
|
|
1492
|
-
|
|
1509
|
+
const lengthMinusBlock = length - this.blockSize;
|
|
1510
|
+
let n = 0;
|
|
1493
1511
|
// Using local instead of member variables gives ~5% speedup on Firefox 16.
|
|
1494
|
-
|
|
1495
|
-
|
|
1512
|
+
const buf = this.buf_;
|
|
1513
|
+
let inbuf = this.inbuf_;
|
|
1496
1514
|
// The outer while loop should execute at most twice.
|
|
1497
1515
|
while (n < length) {
|
|
1498
1516
|
// When we have no data in the block to top up, we can directly process the
|
|
@@ -1534,11 +1552,11 @@ var Sha1 = /** @class */ (function () {
|
|
|
1534
1552
|
}
|
|
1535
1553
|
this.inbuf_ = inbuf;
|
|
1536
1554
|
this.total_ += length;
|
|
1537
|
-
}
|
|
1555
|
+
}
|
|
1538
1556
|
/** @override */
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1557
|
+
digest() {
|
|
1558
|
+
const digest = [];
|
|
1559
|
+
let totalBits = this.total_ * 8;
|
|
1542
1560
|
// Add pad 0x80 0x00*.
|
|
1543
1561
|
if (this.inbuf_ < 56) {
|
|
1544
1562
|
this.update(this.pad_, 56 - this.inbuf_);
|
|
@@ -1547,22 +1565,21 @@ var Sha1 = /** @class */ (function () {
|
|
|
1547
1565
|
this.update(this.pad_, this.blockSize - (this.inbuf_ - 56));
|
|
1548
1566
|
}
|
|
1549
1567
|
// Add # bits.
|
|
1550
|
-
for (
|
|
1568
|
+
for (let i = this.blockSize - 1; i >= 56; i--) {
|
|
1551
1569
|
this.buf_[i] = totalBits & 255;
|
|
1552
1570
|
totalBits /= 256; // Don't use bit-shifting here!
|
|
1553
1571
|
}
|
|
1554
1572
|
this.compress_(this.buf_);
|
|
1555
|
-
|
|
1556
|
-
for (
|
|
1557
|
-
for (
|
|
1573
|
+
let n = 0;
|
|
1574
|
+
for (let i = 0; i < 5; i++) {
|
|
1575
|
+
for (let j = 24; j >= 0; j -= 8) {
|
|
1558
1576
|
digest[n] = (this.chain_[i] >> j) & 255;
|
|
1559
1577
|
++n;
|
|
1560
1578
|
}
|
|
1561
1579
|
}
|
|
1562
1580
|
return digest;
|
|
1563
|
-
}
|
|
1564
|
-
|
|
1565
|
-
}());
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1566
1583
|
|
|
1567
1584
|
/**
|
|
1568
1585
|
* Helper to make a Subscribe function (just like Promise helps make a
|
|
@@ -1573,21 +1590,20 @@ var Sha1 = /** @class */ (function () {
|
|
|
1573
1590
|
* @param onNoObservers Callback when count of Observers goes to zero.
|
|
1574
1591
|
*/
|
|
1575
1592
|
function createSubscribe(executor, onNoObservers) {
|
|
1576
|
-
|
|
1593
|
+
const proxy = new ObserverProxy(executor, onNoObservers);
|
|
1577
1594
|
return proxy.subscribe.bind(proxy);
|
|
1578
1595
|
}
|
|
1579
1596
|
/**
|
|
1580
1597
|
* Implement fan-out for any number of Observers attached via a subscribe
|
|
1581
1598
|
* function.
|
|
1582
1599
|
*/
|
|
1583
|
-
|
|
1600
|
+
class ObserverProxy {
|
|
1584
1601
|
/**
|
|
1585
1602
|
* @param executor Function which can make calls to a single Observer
|
|
1586
1603
|
* as a proxy.
|
|
1587
1604
|
* @param onNoObservers Callback when count of Observers goes to zero.
|
|
1588
1605
|
*/
|
|
1589
|
-
|
|
1590
|
-
var _this = this;
|
|
1606
|
+
constructor(executor, onNoObservers) {
|
|
1591
1607
|
this.observers = [];
|
|
1592
1608
|
this.unsubscribes = [];
|
|
1593
1609
|
this.observerCount = 0;
|
|
@@ -1599,39 +1615,38 @@ var ObserverProxy = /** @class */ (function () {
|
|
|
1599
1615
|
// synchronously after the creation of the subscribe function
|
|
1600
1616
|
// can still receive the very first value generated in the executor.
|
|
1601
1617
|
this.task
|
|
1602
|
-
.then(
|
|
1603
|
-
executor(
|
|
1618
|
+
.then(() => {
|
|
1619
|
+
executor(this);
|
|
1604
1620
|
})
|
|
1605
|
-
.catch(
|
|
1606
|
-
|
|
1621
|
+
.catch(e => {
|
|
1622
|
+
this.error(e);
|
|
1607
1623
|
});
|
|
1608
1624
|
}
|
|
1609
|
-
|
|
1610
|
-
this.forEachObserver(
|
|
1625
|
+
next(value) {
|
|
1626
|
+
this.forEachObserver((observer) => {
|
|
1611
1627
|
observer.next(value);
|
|
1612
1628
|
});
|
|
1613
|
-
}
|
|
1614
|
-
|
|
1615
|
-
this.forEachObserver(
|
|
1629
|
+
}
|
|
1630
|
+
error(error) {
|
|
1631
|
+
this.forEachObserver((observer) => {
|
|
1616
1632
|
observer.error(error);
|
|
1617
1633
|
});
|
|
1618
1634
|
this.close(error);
|
|
1619
|
-
}
|
|
1620
|
-
|
|
1621
|
-
this.forEachObserver(
|
|
1635
|
+
}
|
|
1636
|
+
complete() {
|
|
1637
|
+
this.forEachObserver((observer) => {
|
|
1622
1638
|
observer.complete();
|
|
1623
1639
|
});
|
|
1624
1640
|
this.close();
|
|
1625
|
-
}
|
|
1641
|
+
}
|
|
1626
1642
|
/**
|
|
1627
1643
|
* Subscribe function that can be used to add an Observer to the fan-out list.
|
|
1628
1644
|
*
|
|
1629
1645
|
* - We require that no event is sent to a subscriber synchronously to their
|
|
1630
1646
|
* call to subscribe().
|
|
1631
1647
|
*/
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
var observer;
|
|
1648
|
+
subscribe(nextOrObserver, error, complete) {
|
|
1649
|
+
let observer;
|
|
1635
1650
|
if (nextOrObserver === undefined &&
|
|
1636
1651
|
error === undefined &&
|
|
1637
1652
|
complete === undefined) {
|
|
@@ -1648,8 +1663,8 @@ var ObserverProxy = /** @class */ (function () {
|
|
|
1648
1663
|
else {
|
|
1649
1664
|
observer = {
|
|
1650
1665
|
next: nextOrObserver,
|
|
1651
|
-
error
|
|
1652
|
-
complete
|
|
1666
|
+
error,
|
|
1667
|
+
complete
|
|
1653
1668
|
};
|
|
1654
1669
|
}
|
|
1655
1670
|
if (observer.next === undefined) {
|
|
@@ -1661,16 +1676,16 @@ var ObserverProxy = /** @class */ (function () {
|
|
|
1661
1676
|
if (observer.complete === undefined) {
|
|
1662
1677
|
observer.complete = noop;
|
|
1663
1678
|
}
|
|
1664
|
-
|
|
1679
|
+
const unsub = this.unsubscribeOne.bind(this, this.observers.length);
|
|
1665
1680
|
// Attempt to subscribe to a terminated Observable - we
|
|
1666
1681
|
// just respond to the Observer with the final error or complete
|
|
1667
1682
|
// event.
|
|
1668
1683
|
if (this.finalized) {
|
|
1669
1684
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
1670
|
-
this.task.then(
|
|
1685
|
+
this.task.then(() => {
|
|
1671
1686
|
try {
|
|
1672
|
-
if (
|
|
1673
|
-
observer.error(
|
|
1687
|
+
if (this.finalError) {
|
|
1688
|
+
observer.error(this.finalError);
|
|
1674
1689
|
}
|
|
1675
1690
|
else {
|
|
1676
1691
|
observer.complete();
|
|
@@ -1684,10 +1699,10 @@ var ObserverProxy = /** @class */ (function () {
|
|
|
1684
1699
|
}
|
|
1685
1700
|
this.observers.push(observer);
|
|
1686
1701
|
return unsub;
|
|
1687
|
-
}
|
|
1702
|
+
}
|
|
1688
1703
|
// Unsubscribe is synchronous - we guarantee that no events are sent to
|
|
1689
1704
|
// any unsubscribed Observer.
|
|
1690
|
-
|
|
1705
|
+
unsubscribeOne(i) {
|
|
1691
1706
|
if (this.observers === undefined || this.observers[i] === undefined) {
|
|
1692
1707
|
return;
|
|
1693
1708
|
}
|
|
@@ -1696,29 +1711,28 @@ var ObserverProxy = /** @class */ (function () {
|
|
|
1696
1711
|
if (this.observerCount === 0 && this.onNoObservers !== undefined) {
|
|
1697
1712
|
this.onNoObservers(this);
|
|
1698
1713
|
}
|
|
1699
|
-
}
|
|
1700
|
-
|
|
1714
|
+
}
|
|
1715
|
+
forEachObserver(fn) {
|
|
1701
1716
|
if (this.finalized) {
|
|
1702
1717
|
// Already closed by previous event....just eat the additional values.
|
|
1703
1718
|
return;
|
|
1704
1719
|
}
|
|
1705
1720
|
// Since sendOne calls asynchronously - there is no chance that
|
|
1706
1721
|
// this.observers will become undefined.
|
|
1707
|
-
for (
|
|
1722
|
+
for (let i = 0; i < this.observers.length; i++) {
|
|
1708
1723
|
this.sendOne(i, fn);
|
|
1709
1724
|
}
|
|
1710
|
-
}
|
|
1725
|
+
}
|
|
1711
1726
|
// Call the Observer via one of it's callback function. We are careful to
|
|
1712
1727
|
// confirm that the observe has not been unsubscribed since this asynchronous
|
|
1713
1728
|
// function had been queued.
|
|
1714
|
-
|
|
1715
|
-
var _this = this;
|
|
1729
|
+
sendOne(i, fn) {
|
|
1716
1730
|
// Execute the callback asynchronously
|
|
1717
1731
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
1718
|
-
this.task.then(
|
|
1719
|
-
if (
|
|
1732
|
+
this.task.then(() => {
|
|
1733
|
+
if (this.observers !== undefined && this.observers[i] !== undefined) {
|
|
1720
1734
|
try {
|
|
1721
|
-
fn(
|
|
1735
|
+
fn(this.observers[i]);
|
|
1722
1736
|
}
|
|
1723
1737
|
catch (e) {
|
|
1724
1738
|
// Ignore exceptions raised in Observers or missing methods of an
|
|
@@ -1730,9 +1744,8 @@ var ObserverProxy = /** @class */ (function () {
|
|
|
1730
1744
|
}
|
|
1731
1745
|
}
|
|
1732
1746
|
});
|
|
1733
|
-
}
|
|
1734
|
-
|
|
1735
|
-
var _this = this;
|
|
1747
|
+
}
|
|
1748
|
+
close(err) {
|
|
1736
1749
|
if (this.finalized) {
|
|
1737
1750
|
return;
|
|
1738
1751
|
}
|
|
@@ -1742,26 +1755,21 @@ var ObserverProxy = /** @class */ (function () {
|
|
|
1742
1755
|
}
|
|
1743
1756
|
// Proxy is no longer needed - garbage collect references
|
|
1744
1757
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
1745
|
-
this.task.then(
|
|
1746
|
-
|
|
1747
|
-
|
|
1758
|
+
this.task.then(() => {
|
|
1759
|
+
this.observers = undefined;
|
|
1760
|
+
this.onNoObservers = undefined;
|
|
1748
1761
|
});
|
|
1749
|
-
}
|
|
1750
|
-
|
|
1751
|
-
}());
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1752
1764
|
/** Turn synchronous function into one called asynchronously. */
|
|
1753
1765
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
1754
1766
|
function async(fn, onError) {
|
|
1755
|
-
return
|
|
1756
|
-
var args = [];
|
|
1757
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1758
|
-
args[_i] = arguments[_i];
|
|
1759
|
-
}
|
|
1767
|
+
return (...args) => {
|
|
1760
1768
|
Promise.resolve(true)
|
|
1761
|
-
.then(
|
|
1762
|
-
fn
|
|
1769
|
+
.then(() => {
|
|
1770
|
+
fn(...args);
|
|
1763
1771
|
})
|
|
1764
|
-
.catch(
|
|
1772
|
+
.catch((error) => {
|
|
1765
1773
|
if (onError) {
|
|
1766
1774
|
onError(error);
|
|
1767
1775
|
}
|
|
@@ -1775,8 +1783,7 @@ function implementsAnyMethods(obj, methods) {
|
|
|
1775
1783
|
if (typeof obj !== 'object' || obj === null) {
|
|
1776
1784
|
return false;
|
|
1777
1785
|
}
|
|
1778
|
-
for (
|
|
1779
|
-
var method = methods_1[_i];
|
|
1786
|
+
for (const method of methods) {
|
|
1780
1787
|
if (method in obj && typeof obj[method] === 'function') {
|
|
1781
1788
|
return true;
|
|
1782
1789
|
}
|
|
@@ -1812,8 +1819,8 @@ function noop() {
|
|
|
1812
1819
|
* @param maxCount The maximum number of argument to allow for the function call
|
|
1813
1820
|
* @param argCount The actual number of arguments provided.
|
|
1814
1821
|
*/
|
|
1815
|
-
|
|
1816
|
-
|
|
1822
|
+
const validateArgCount = function (fnName, minCount, maxCount, argCount) {
|
|
1823
|
+
let argError;
|
|
1817
1824
|
if (argCount < minCount) {
|
|
1818
1825
|
argError = 'at least ' + minCount;
|
|
1819
1826
|
}
|
|
@@ -1821,7 +1828,7 @@ var validateArgCount = function (fnName, minCount, maxCount, argCount) {
|
|
|
1821
1828
|
argError = maxCount === 0 ? 'none' : 'no more than ' + maxCount;
|
|
1822
1829
|
}
|
|
1823
1830
|
if (argError) {
|
|
1824
|
-
|
|
1831
|
+
const error = fnName +
|
|
1825
1832
|
' failed: Was called with ' +
|
|
1826
1833
|
argCount +
|
|
1827
1834
|
(argCount === 1 ? ' argument.' : ' arguments.') +
|
|
@@ -1839,7 +1846,7 @@ var validateArgCount = function (fnName, minCount, maxCount, argCount) {
|
|
|
1839
1846
|
* @return The prefix to add to the error thrown for validation.
|
|
1840
1847
|
*/
|
|
1841
1848
|
function errorPrefix(fnName, argName) {
|
|
1842
|
-
return
|
|
1849
|
+
return `${fnName} failed: ${argName} argument `;
|
|
1843
1850
|
}
|
|
1844
1851
|
/**
|
|
1845
1852
|
* @param fnName
|
|
@@ -1904,17 +1911,17 @@ function validateContextObject(fnName, argumentName, context, optional) {
|
|
|
1904
1911
|
* @param {string} str
|
|
1905
1912
|
* @return {Array}
|
|
1906
1913
|
*/
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
for (
|
|
1911
|
-
|
|
1914
|
+
const stringToByteArray = function (str) {
|
|
1915
|
+
const out = [];
|
|
1916
|
+
let p = 0;
|
|
1917
|
+
for (let i = 0; i < str.length; i++) {
|
|
1918
|
+
let c = str.charCodeAt(i);
|
|
1912
1919
|
// Is this the lead surrogate in a surrogate pair?
|
|
1913
1920
|
if (c >= 0xd800 && c <= 0xdbff) {
|
|
1914
|
-
|
|
1921
|
+
const high = c - 0xd800; // the high 10 bits.
|
|
1915
1922
|
i++;
|
|
1916
1923
|
assert(i < str.length, 'Surrogate pair missing trail surrogate.');
|
|
1917
|
-
|
|
1924
|
+
const low = str.charCodeAt(i) - 0xdc00; // the low 10 bits.
|
|
1918
1925
|
c = 0x10000 + (high << 10) + low;
|
|
1919
1926
|
}
|
|
1920
1927
|
if (c < 128) {
|
|
@@ -1943,10 +1950,10 @@ var stringToByteArray = function (str) {
|
|
|
1943
1950
|
* @param {string} str
|
|
1944
1951
|
* @return {number}
|
|
1945
1952
|
*/
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
for (
|
|
1949
|
-
|
|
1953
|
+
const stringLength = function (str) {
|
|
1954
|
+
let p = 0;
|
|
1955
|
+
for (let i = 0; i < str.length; i++) {
|
|
1956
|
+
const c = str.charCodeAt(i);
|
|
1950
1957
|
if (c < 128) {
|
|
1951
1958
|
p++;
|
|
1952
1959
|
}
|
|
@@ -1986,9 +1993,9 @@ var stringLength = function (str) {
|
|
|
1986
1993
|
* Generates a new uuid.
|
|
1987
1994
|
* @public
|
|
1988
1995
|
*/
|
|
1989
|
-
|
|
1990
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
|
|
1991
|
-
|
|
1996
|
+
const uuidv4 = function () {
|
|
1997
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
1998
|
+
const r = (Math.random() * 16) | 0, v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
1992
1999
|
return v.toString(16);
|
|
1993
2000
|
});
|
|
1994
2001
|
};
|
|
@@ -2012,18 +2019,18 @@ var uuidv4 = function () {
|
|
|
2012
2019
|
/**
|
|
2013
2020
|
* The amount of milliseconds to exponentially increase.
|
|
2014
2021
|
*/
|
|
2015
|
-
|
|
2022
|
+
const DEFAULT_INTERVAL_MILLIS = 1000;
|
|
2016
2023
|
/**
|
|
2017
2024
|
* The factor to backoff by.
|
|
2018
2025
|
* Should be a number greater than 1.
|
|
2019
2026
|
*/
|
|
2020
|
-
|
|
2027
|
+
const DEFAULT_BACKOFF_FACTOR = 2;
|
|
2021
2028
|
/**
|
|
2022
2029
|
* The maximum milliseconds to increase to.
|
|
2023
2030
|
*
|
|
2024
2031
|
* <p>Visible for testing
|
|
2025
2032
|
*/
|
|
2026
|
-
|
|
2033
|
+
const MAX_VALUE_MILLIS = 4 * 60 * 60 * 1000; // Four hours, like iOS and Android.
|
|
2027
2034
|
/**
|
|
2028
2035
|
* The percentage of backoff time to randomize by.
|
|
2029
2036
|
* See
|
|
@@ -2032,22 +2039,20 @@ var MAX_VALUE_MILLIS = 4 * 60 * 60 * 1000; // Four hours, like iOS and Android.
|
|
|
2032
2039
|
*
|
|
2033
2040
|
* <p>Visible for testing
|
|
2034
2041
|
*/
|
|
2035
|
-
|
|
2042
|
+
const RANDOM_FACTOR = 0.5;
|
|
2036
2043
|
/**
|
|
2037
2044
|
* Based on the backoff method from
|
|
2038
2045
|
* https://github.com/google/closure-library/blob/master/closure/goog/math/exponentialbackoff.js.
|
|
2039
2046
|
* Extracted here so we don't need to pass metadata and a stateful ExponentialBackoff object around.
|
|
2040
2047
|
*/
|
|
2041
|
-
function calculateBackoffMillis(backoffCount, intervalMillis, backoffFactor) {
|
|
2042
|
-
if (intervalMillis === void 0) { intervalMillis = DEFAULT_INTERVAL_MILLIS; }
|
|
2043
|
-
if (backoffFactor === void 0) { backoffFactor = DEFAULT_BACKOFF_FACTOR; }
|
|
2048
|
+
function calculateBackoffMillis(backoffCount, intervalMillis = DEFAULT_INTERVAL_MILLIS, backoffFactor = DEFAULT_BACKOFF_FACTOR) {
|
|
2044
2049
|
// Calculates an exponentially increasing value.
|
|
2045
2050
|
// Deviation: calculates value from count and a constant interval, so we only need to save value
|
|
2046
2051
|
// and count to restore state.
|
|
2047
|
-
|
|
2052
|
+
const currBaseValue = intervalMillis * Math.pow(backoffFactor, backoffCount);
|
|
2048
2053
|
// A random "fuzz" to avoid waves of retries.
|
|
2049
2054
|
// Deviation: randomFactor is required.
|
|
2050
|
-
|
|
2055
|
+
const randomWait = Math.round(
|
|
2051
2056
|
// A fraction of the backoff value to add/subtract.
|
|
2052
2057
|
// Deviation: changes multiplication order to improve readability.
|
|
2053
2058
|
RANDOM_FACTOR *
|
|
@@ -2081,17 +2086,17 @@ function calculateBackoffMillis(backoffCount, intervalMillis, backoffFactor) {
|
|
|
2081
2086
|
*/
|
|
2082
2087
|
function ordinal(i) {
|
|
2083
2088
|
if (!Number.isFinite(i)) {
|
|
2084
|
-
return
|
|
2089
|
+
return `${i}`;
|
|
2085
2090
|
}
|
|
2086
2091
|
return i + indicator(i);
|
|
2087
2092
|
}
|
|
2088
2093
|
function indicator(i) {
|
|
2089
2094
|
i = Math.abs(i);
|
|
2090
|
-
|
|
2095
|
+
const cent = i % 100;
|
|
2091
2096
|
if (cent >= 10 && cent <= 20) {
|
|
2092
2097
|
return 'th';
|
|
2093
2098
|
}
|
|
2094
|
-
|
|
2099
|
+
const dec = i % 10;
|
|
2095
2100
|
if (dec === 1) {
|
|
2096
2101
|
return 'st';
|
|
2097
2102
|
}
|