Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
29 lines
709 B
Plaintext
29 lines
709 B
Plaintext
/* eslint-env browser */
|
|
const perf = typeof performance === 'undefined' ? null : performance
|
|
|
|
const isoCrypto = typeof crypto === 'undefined' ? null : crypto
|
|
|
|
/**
|
|
* @type {function(number):ArrayBuffer}
|
|
*/
|
|
const cryptoRandomBuffer = isoCrypto !== null
|
|
? len => {
|
|
// browser
|
|
const buf = new ArrayBuffer(len)
|
|
const arr = new Uint8Array(buf)
|
|
isoCrypto.getRandomValues(arr)
|
|
return buf
|
|
}
|
|
: len => {
|
|
// polyfill
|
|
const buf = new ArrayBuffer(len)
|
|
const arr = new Uint8Array(buf)
|
|
for (let i = 0; i < len; i++) {
|
|
arr[i] = Math.ceil((Math.random() * 0xFFFFFFFF) >>> 0)
|
|
}
|
|
return buf
|
|
}
|
|
|
|
exports.performance = perf
|
|
exports.cryptoRandomBuffer = cryptoRandomBuffer
|