@react-native-module/get-random-values
Version:
implementation of getRandomValues for React Native
31 lines (26 loc) • 1.28 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var index = require('../node_modules/fast-base64-decode/index.js');
var errors = require('./errors.js');
var lib = require('./lib.js');
// Web API Same
function getRandomValues(array) {
// For web
// If you're running react-native debug mode (Chrome debug)
// calling request is replaced globalThis.crypto.getRandomValues
// If you ignore this, you may get error on getRandomBase64
if (globalThis.crypto) {
if (globalThis.crypto.getRandomValues) {
return globalThis.crypto.getRandomValues(array);
}
}
if (!(array instanceof Int8Array || array instanceof Uint8Array || array instanceof Int16Array || array instanceof Uint16Array || array instanceof Int32Array || array instanceof Uint32Array || array instanceof Uint8ClampedArray)) {
throw new errors.TypeMismatchError('Expected an integer array');
}
if (array.byteLength > 65536) {
throw new errors.QuotaExceededError('Can only request a maximum of 65536 bytes');
}
index(lib.getRandomBase64(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength));
return array;
}
exports.getRandomValues = getRandomValues;