Skip to content

Commit 11fa619

Browse files
committed
Revert "Use crypo RNG for auto ID generation to reduce conflicts. (#2764)"
This reverts commit a85f81d.
1 parent 0be90d3 commit 11fa619

File tree

5 files changed

+2
-41
lines changed

5 files changed

+2
-41
lines changed

packages/firestore/src/platform/platform.ts

-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ export interface Platform {
4343
/** Converts a binary string to a Base64 encoded string. */
4444
btoa(raw: string): string;
4545

46-
/**
47-
* Generates `nBytes` of random bytes. If `nBytes` is negative, an empty array
48-
* will be returned.
49-
*/
50-
randomBytes(nBytes: number): Uint8Array;
51-
5246
/** The Platform's 'window' implementation or null if not available. */
5347
readonly window: Window | null;
5448

packages/firestore/src/platform_browser/browser_platform.ts

-10
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,4 @@ export class BrowserPlatform implements Platform {
6868
btoa(raw: string): string {
6969
return btoa(raw);
7070
}
71-
72-
randomBytes(nBytes: number): Uint8Array {
73-
if (nBytes <= 0) {
74-
return new Uint8Array();
75-
}
76-
77-
const v = new Uint8Array(nBytes);
78-
crypto.getRandomValues(v);
79-
return v;
80-
}
8171
}

packages/firestore/src/platform_node/node_platform.ts

-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { randomBytes } from 'crypto';
1918
import { inspect } from 'util';
2019

2120
import { DatabaseId, DatabaseInfo } from '../core/database_info';
@@ -76,12 +75,4 @@ export class NodePlatform implements Platform {
7675
btoa(raw: string): string {
7776
return new Buffer(raw, 'binary').toString('base64');
7877
}
79-
80-
randomBytes(nBytes: number): Uint8Array {
81-
if (nBytes <= 0) {
82-
return new Uint8Array();
83-
}
84-
85-
return randomBytes(nBytes);
86-
}
8778
}

packages/firestore/src/util/misc.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
import { assert } from './assert';
19-
import { PlatformSupport } from '../platform/platform';
2019

2120
export type EventHandler<E> = (value: E) => void;
2221
export interface Indexable {
@@ -29,17 +28,8 @@ export class AutoId {
2928
const chars =
3029
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
3130
let autoId = '';
32-
while (autoId.length < 20) {
33-
const bytes = PlatformSupport.getPlatform().randomBytes(40);
34-
bytes.forEach(b => {
35-
// Length of `chars` is 62. We only take bytes between 0 and 62*4-1
36-
// (both inclusive). The value is then evenly mapped to indices of `char`
37-
// via a modulo operation.
38-
const maxValue = 62 * 4 - 1;
39-
if (autoId.length < 20 && b <= maxValue) {
40-
autoId += chars.charAt(b % 62);
41-
}
42-
});
31+
for (let i = 0; i < 20; i++) {
32+
autoId += chars.charAt(Math.floor(Math.random() * chars.length));
4333
}
4434
assert(autoId.length === 20, 'Invalid auto ID: ' + autoId);
4535
return autoId;

packages/firestore/test/util/test_platform.ts

-4
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,6 @@ export class TestPlatform implements Platform {
266266
btoa(raw: string): string {
267267
return this.basePlatform.btoa(raw);
268268
}
269-
270-
randomBytes(nBytes: number): Uint8Array {
271-
return this.basePlatform.randomBytes(nBytes);
272-
}
273269
}
274270

275271
/** Returns true if we are running under Node. */

0 commit comments

Comments
 (0)