implement generateUserId for node & clients that dont support crypto
This commit is contained in:
parent
a19cfa1465
commit
c97130abc4
15
src/Utils.js
15
src/Utils.js
@ -821,9 +821,18 @@ export default function Utils (Y) {
|
||||
Y.utils.createSmallLookupBuffer = createSmallLookupBuffer
|
||||
|
||||
function generateUserId () {
|
||||
let arr = new Uint32Array(1)
|
||||
crypto.getRandomValues(arr)
|
||||
return arr[0]
|
||||
if (crypto.getRandomValue != null) {
|
||||
// browser
|
||||
let arr = new Uint32Array(1)
|
||||
crypto.getRandomValues(arr)
|
||||
return arr[0]
|
||||
} else if (crypto.randomBytes != null) {
|
||||
// node
|
||||
let buf = crypto.randomBytes(4)
|
||||
return new Uint32Array(buf.buffer)[0]
|
||||
} else {
|
||||
Math.ceil(Math.random() * 0xFFFFFFFF)
|
||||
}
|
||||
}
|
||||
Y.utils.generateUserId = generateUserId
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user