From 4f9c89d8eff0b33da4cb84a04af84ce4df000a0c Mon Sep 17 00:00:00 2001 From: Cinnamon Date: Wed, 26 Aug 2020 14:14:30 -0700 Subject: [PATCH] Inline a buffer function from core node, to test browser polyfill problem --- src/util/detRandom.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/util/detRandom.ts b/src/util/detRandom.ts index f61fc90c..525641d0 100644 --- a/src/util/detRandom.ts +++ b/src/util/detRandom.ts @@ -21,13 +21,25 @@ import { LowLevelCrypto } from '../crypto/crypto'; // Consider using md5 instead since this is not security-critical. // Consider memoizing these functions for speed. +let readUInt32BE = (buffer : Buffer): number => { + // take first 4 bytes of hash and convert to unsigned 32 bit integer (big-endian) + // copied here from core node because some browser polyfill can't handle it otherwise + // https://github.com/nodejs/node/blob/44161274821a2e81e7a5706c06cf8aa8bd2aa972/lib/internal/buffer.js#L291-L302 + let offset = 0; + const first = buffer[offset]; + const last = buffer[offset + 3]; + if (first === undefined || last === undefined) { throw '???' } + return first * 2 ** 24 + + buffer[++offset] * 2 ** 16 + + buffer[++offset] * 2 ** 8 + + last; +} + export let detRandom = (s: string | Buffer): number => { // return a random-ish float between 0 and 1, deterministically derived from a hash of the string let hashBuffer = LowLevelCrypto.sha256(s); - // take first 4 bytes of hash and convert to unsigned 32 bit integer (big-endian) - // https://github.com/nodejs/node/blob/44161274821a2e81e7a5706c06cf8aa8bd2aa972/lib/internal/buffer.js#L291-L302 - let randInt = hashBuffer.slice(4).readUInt32BE(); + let randInt = readUInt32BE(hashBuffer); // divide by max possible value return randInt / 2 ** 32; };