Skip to content

Commit 434e650

Browse files
committed
Address comments
1 parent ea4967a commit 434e650

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

packages/firestore/src/platform/platform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface Platform {
4646
/**
4747
* Generates `nBytes` of random bytes.
4848
*
49-
* If `nBytes <= 0` , an error will be thrown.
49+
* If `nBytes < 0` , an error will be thrown.
5050
*/
5151
randomBytes(nBytes: number): Uint8Array;
5252

packages/firestore/src/platform_browser/browser_platform.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,21 @@ export class BrowserPlatform implements Platform {
7676
}
7777

7878
randomBytes(nBytes: number): Uint8Array {
79-
debugAssert(nBytes >= 0, 'Number of random bytes must not be negative');
79+
debugAssert(nBytes >= 0, `Expecting non-negative nBytes, got: ${nBytes}`);
8080

81-
// Polyfill for IE and WebWorker
81+
// Polyfills for IE and WebWorker by using `self` and `msCrypto` when `crypto` is not available.
8282
const crypto =
8383
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8484
typeof self !== 'undefined' && (self.crypto || (self as any)['msCrypto']);
85-
const v = new Uint8Array(nBytes);
85+
const bytes = new Uint8Array(nBytes);
8686
if (crypto) {
87-
crypto.getRandomValues(v);
87+
crypto.getRandomValues(bytes);
8888
} else {
8989
// Falls back to Math.random
9090
for (let i = 0; i < nBytes; i++) {
91-
v[i] = Math.floor(Math.random() * 256);
91+
bytes[i] = Math.floor(Math.random() * 256);
9292
}
9393
}
94-
return v;
94+
return bytes;
9595
}
9696
}

packages/firestore/src/platform_node/node_platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { NoopConnectivityMonitor } from './../remote/connectivity_monitor_noop';
2828

2929
import { GrpcConnection } from './grpc_connection';
3030
import { loadProtos } from './load_protos';
31-
import { validatePositiveNumber } from '../util/input_validation';
31+
import { debugAssert } from '../util/assert';
3232

3333
export class NodePlatform implements Platform {
3434
readonly base64Available = true;
@@ -79,7 +79,7 @@ export class NodePlatform implements Platform {
7979
}
8080

8181
randomBytes(nBytes: number): Uint8Array {
82-
validatePositiveNumber('randomBytes', 1, nBytes);
82+
debugAssert(nBytes >= 0, `Expecting non-negative nBytes, got: ${nBytes}`);
8383

8484
return randomBytes(nBytes);
8585
}

packages/firestore/src/util/misc.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,27 @@ export class AutoId {
2828
// Alphanumeric characters
2929
const chars =
3030
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
31+
// The largest byte value that is a multiple of `char.length`.
32+
const maxMultiple = Math.floor(256 / chars.length) * chars.length;
33+
debugAssert(
34+
0 < maxMultiple && maxMultiple < 256,
35+
`Expect maxMultiple to be (0, 256), but got ${maxMultiple}`
36+
);
37+
3138
let autoId = '';
32-
while (autoId.length < 20) {
39+
const targetLength = 20;
40+
while (autoId.length < targetLength) {
3341
const bytes = PlatformSupport.getPlatform().randomBytes(40);
3442
for (let i = 0; i < bytes.length; ++i) {
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 && bytes[i] <= maxValue) {
40-
autoId += chars.charAt(bytes[i] % 62);
43+
// Only accept values that are [0, maxMultiple), this ensures they can
44+
// be evenly mapped to indices of `chars` via a modulo operation.
45+
if (autoId.length < targetLength && bytes[i] < maxMultiple) {
46+
autoId += chars.charAt(bytes[i] % chars.length);
4147
}
4248
}
4349
}
44-
debugAssert(autoId.length === 20, 'Invalid auto ID: ' + autoId);
50+
debugAssert(autoId.length === targetLength, 'Invalid auto ID: ' + autoId);
51+
4552
return autoId;
4653
}
4754
}

0 commit comments

Comments
 (0)