Skip to content

Commit a2b89c2

Browse files
committed
Reland "Use crypo RNG for auto ID generation to reduce conflicts (#2872)"
1 parent 867417c commit a2b89c2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/firestore/src/platform_browser/browser_platform.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ import { NoopConnectivityMonitor } from '../remote/connectivity_monitor_noop';
2525
import { BrowserConnectivityMonitor } from './browser_connectivity_monitor';
2626
import { WebChannelConnection } from './webchannel_connection';
2727

28-
// Polyfill for IE and WebWorker
29-
// eslint-disable-next-line no-restricted-globals
30-
const crypto = window['crypto'] || window['msCrypto'] || self['crypto'];
31-
3228
// Implements the Platform API for browsers and some browser-like environments
3329
// (including ReactNative).
3430
export class BrowserPlatform implements Platform {
@@ -84,12 +80,18 @@ export class BrowserPlatform implements Platform {
8480
return new Uint8Array();
8581
}
8682

83+
// Polyfill for IE and WebWorker
84+
// eslint-disable-next-line no-restricted-globals
85+
const crypto =
86+
(this.window &&
87+
(this.window.crypto || (this.window as any)['msCrypto'])) ||
88+
(self && self.crypto);
8789
const v = new Uint8Array(nBytes);
88-
if(!!crypto) {
90+
if (!!crypto) {
8991
crypto.getRandomValues(v);
9092
} else {
9193
// Falls back to Math.random
92-
for(let i = 0; i < nBytes; i++) {
94+
for (let i = 0; i < nBytes; i++) {
9395
v[i] = Math.floor(Math.random() * 256);
9496
}
9597
}

0 commit comments

Comments
 (0)