File tree Expand file tree Collapse file tree 2 files changed +9
-13
lines changed
Expand file tree Collapse file tree 2 files changed +9
-13
lines changed Original file line number Diff line number Diff line change @@ -16,15 +16,10 @@ const selectEntropyFunction = (puidConfig: PuidConfig): EntropyFunction => {
1616 if ( puidConfig . entropyBytes ) return [ false , puidConfig . entropyBytes as EntropyByBytes ]
1717
1818 // Prefer Web Crypto in environments where it's available
19- const globalScope : any = globalThis as any
20- const getRandomValuesFn = globalScope ?. crypto ?. getRandomValues as ( ( buf : Uint8Array ) => Uint8Array ) | undefined
21- if ( typeof getRandomValuesFn === 'function' )
22- return [
23- true ,
24- ( buffer : Uint8Array ) => {
25- getRandomValuesFn . call ( globalScope . crypto , buffer )
26- }
27- ]
19+ type CryptoLike = { getRandomValues ?: ( b : Uint8Array ) => void }
20+ const cryptoObj = ( globalThis as { crypto ?: CryptoLike } ) . crypto
21+ const gv = cryptoObj ?. getRandomValues ?. bind ( cryptoObj ) as ( ( b : Uint8Array ) => void ) | undefined
22+ if ( gv ) return [ true , gv ]
2823
2924 // Fallback to Node's randomBytes
3025 return [ false , randomBytes ]
Original file line number Diff line number Diff line change @@ -13,9 +13,10 @@ const selectEntropyFunction = (puidConfig: PuidConfig): EntropyFunction => {
1313 if ( puidConfig . entropyValues ) return [ true , puidConfig . entropyValues as EntropyByValues ]
1414 if ( puidConfig . entropyBytes ) return [ false , puidConfig . entropyBytes as EntropyByBytes ]
1515
16- const g : any = globalThis as any
17- const gv = g ?. crypto ?. getRandomValues as ( ( buf : Uint8Array ) => Uint8Array ) | undefined
18- if ( typeof gv === 'function' ) return [ true , ( buffer : Uint8Array ) => { gv . call ( g . crypto , buffer ) } ]
16+ type CryptoLike = { getRandomValues ?: ( b : Uint8Array ) => void }
17+ const cryptoObj = ( globalThis as { crypto ?: CryptoLike } ) . crypto
18+ const gv = cryptoObj ?. getRandomValues ?. bind ( cryptoObj ) as ( ( b : Uint8Array ) => void ) | undefined
19+ if ( gv ) return [ true , gv ]
1920
2021 // No Node randomBytes fallback in web entry
2122 throw new Error ( 'No entropy source available: provide entropyValues or ensure Web Crypto is available' )
@@ -41,7 +42,7 @@ export default (puidConfig: PuidConfig = {}): PuidResult => {
4142
4243 // In web build, if no entropy provided, require Web Crypto
4344 if ( ! puidConfig . entropyBytes && ! puidConfig . entropyValues ) {
44- const hasWebCrypto = typeof ( globalThis as any ) ? .crypto ?. getRandomValues === 'function'
45+ const hasWebCrypto = typeof ( globalThis as { crypto ?: { getRandomValues ?: ( b : Uint8Array ) => void } } ) . crypto ?. getRandomValues === 'function'
4546 if ( ! hasWebCrypto ) return { error : new Error ( 'Web Crypto getRandomValues not available. Provide entropyValues.' ) }
4647 }
4748
You can’t perform that action at this time.
0 commit comments