11import { Buffer } from 'buffer' ;
22
3+ type RandomBytesFunction = ( size : number ) => Uint8Array ;
4+
35/**
46 * Normalizes our expected stringified form of a function across versions of node
57 * @param fn - The function to stringify
@@ -8,11 +10,20 @@ export function normalizedFunctionString(fn: Function): string {
810 return fn . toString ( ) . replace ( 'function(' , 'function (' ) ;
911}
1012
11- function insecureRandomBytes ( size : number ) : Uint8Array {
13+ const isReactNative =
14+ typeof global . navigator === 'object' && global . navigator . product === 'ReactNative' ;
15+
16+ const insecureWarning = isReactNative
17+ ? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
18+ : 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.' ;
19+
20+ const insecureRandomBytes : RandomBytesFunction = function insecureRandomBytes ( size : number ) {
21+ console . warn ( insecureWarning ) ;
22+
1223 const result = Buffer . alloc ( size ) ;
1324 for ( let i = 0 ; i < size ; ++ i ) result [ i ] = Math . floor ( Math . random ( ) * 256 ) ;
1425 return result ;
15- }
26+ } ;
1627
1728/* We do not want to have to include DOM types just for this check */
1829// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -22,22 +33,34 @@ declare let require: Function;
2233declare let global : any ;
2334declare const self : unknown ;
2435
25- export let randomBytes = insecureRandomBytes ;
26- if ( typeof window !== 'undefined' && window . crypto && window . crypto . getRandomValues ) {
27- randomBytes = size => window . crypto . getRandomValues ( Buffer . alloc ( size ) ) ;
28- } else {
36+ const detectRandomBytes = ( ) : RandomBytesFunction => {
37+ if ( typeof window !== 'undefined' ) {
38+ // browser crypto implementation(s)
39+ const target = window . crypto || window . msCrypto ; // allow for IE11
40+ if ( target && target . getRandomValues ) {
41+ return size => target . getRandomValues ( Buffer . alloc ( size ) ) ;
42+ }
43+ }
44+
45+ if ( typeof global !== 'undefined' && global . crypto && global . crypto . getRandomValues ) {
46+ // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
47+ return size => global . crypto . getRandomValues ( Buffer . alloc ( size ) ) ;
48+ }
49+
50+ let requiredRandomBytes : RandomBytesFunction | null | undefined ;
2951 try {
3052 // eslint-disable-next-line @typescript-eslint/no-var-requires
31- randomBytes = require ( 'crypto' ) . randomBytes ;
53+ requiredRandomBytes = require ( 'crypto' ) . randomBytes ;
3254 } catch ( e ) {
3355 // keep the fallback
3456 }
3557
3658 // NOTE: in transpiled cases the above require might return null/undefined
37- if ( randomBytes == null ) {
38- randomBytes = insecureRandomBytes ;
39- }
40- }
59+
60+ return requiredRandomBytes || insecureRandomBytes ;
61+ } ;
62+
63+ export const randomBytes = detectRandomBytes ( ) ;
4164
4265export function isUint8Array ( value : unknown ) : value is Uint8Array {
4366 return Object . prototype . toString . call ( value ) === '[object Uint8Array]' ;
0 commit comments