-
Notifications
You must be signed in to change notification settings - Fork 258
UUID convenience class #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2835523
67adbb4
1ca2b8e
ae3b5b8
89af99b
d9b4b6f
521dd26
0d20db2
c1db757
22b763a
9e12b98
73d9019
23bccbd
219d9d1
ea62754
2b22e07
2555095
b468485
1ac502c
a845a9d
e3a4129
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,7 @@ | ||||||
import { Buffer } from 'buffer'; | ||||||
|
||||||
type RandomBytesFunction = (size: number) => Uint8Array; | ||||||
|
||||||
/** | ||||||
* Normalizes our expected stringified form of a function across versions of node | ||||||
* @param fn - The function to stringify | ||||||
|
@@ -8,11 +10,20 @@ export function normalizedFunctionString(fn: Function): string { | |||||
return fn.toString().replace('function(', 'function ('); | ||||||
} | ||||||
|
||||||
function insecureRandomBytes(size: number): Uint8Array { | ||||||
const isReactNative = | ||||||
typeof global.navigator === 'object' && global.navigator.product === 'ReactNative'; | ||||||
|
||||||
const insecureWarning = isReactNative | ||||||
? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.' | ||||||
: 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.'; | ||||||
|
||||||
const insecureRandomBytes: RandomBytesFunction = function insecureRandomBytes(size: number) { | ||||||
console.warn(insecureWarning); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be better to use
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, but wouldn't this throw in a browser context without crypto? |
||||||
|
||||||
const result = Buffer.alloc(size); | ||||||
for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256); | ||||||
return result; | ||||||
} | ||||||
}; | ||||||
|
||||||
/* We do not want to have to include DOM types just for this check */ | ||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||
|
@@ -22,22 +33,34 @@ declare let require: Function; | |||||
declare let global: any; | ||||||
declare const self: unknown; | ||||||
|
||||||
export let randomBytes = insecureRandomBytes; | ||||||
if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) { | ||||||
randomBytes = size => window.crypto.getRandomValues(Buffer.alloc(size)); | ||||||
} else { | ||||||
const detectRandomBytes = (): RandomBytesFunction => { | ||||||
if (typeof window !== 'undefined') { | ||||||
// browser crypto implementation(s) | ||||||
const target = window.crypto || window.msCrypto; // allow for IE11 | ||||||
if (target && target.getRandomValues) { | ||||||
return size => target.getRandomValues(Buffer.alloc(size)); | ||||||
} | ||||||
} | ||||||
|
||||||
if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) { | ||||||
// allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global | ||||||
return size => global.crypto.getRandomValues(Buffer.alloc(size)); | ||||||
} | ||||||
|
||||||
let requiredRandomBytes: RandomBytesFunction | null | undefined; | ||||||
try { | ||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||||||
randomBytes = require('crypto').randomBytes; | ||||||
requiredRandomBytes = require('crypto').randomBytes; | ||||||
} catch (e) { | ||||||
// keep the fallback | ||||||
} | ||||||
|
||||||
// NOTE: in transpiled cases the above require might return null/undefined | ||||||
if (randomBytes == null) { | ||||||
randomBytes = insecureRandomBytes; | ||||||
} | ||||||
} | ||||||
|
||||||
return requiredRandomBytes || insecureRandomBytes; | ||||||
}; | ||||||
|
||||||
export const randomBytes = detectRandomBytes(); | ||||||
|
||||||
export function isUint8Array(value: unknown): value is Uint8Array { | ||||||
return Object.prototype.toString.call(value) === '[object Uint8Array]'; | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.