UNPKG

991 BJavaScriptView Raw
1import { UnavailabilityError } from '@unimodules/core';
2import { toByteArray } from 'base64-js';
3import ExpoRandom from './ExpoRandom';
4function assertByteCount(value) {
5 if (typeof value !== 'number' ||
6 isNaN(value) ||
7 Math.floor(value) < 0 ||
8 Math.floor(value) > 1024) {
9 throw new TypeError(`expo-random: getRandomBytesAsync(${value}) expected a valid number from range 0...1024`);
10 }
11}
12export async function getRandomBytesAsync(byteCount) {
13 assertByteCount(byteCount);
14 const validByteCount = Math.floor(byteCount);
15 if (ExpoRandom.getRandomBytesAsync) {
16 return await ExpoRandom.getRandomBytesAsync(validByteCount);
17 }
18 else if (ExpoRandom.getRandomBase64StringAsync) {
19 const base64 = await ExpoRandom.getRandomBase64StringAsync(validByteCount);
20 return toByteArray(base64);
21 }
22 else {
23 throw new UnavailabilityError('expo-random', 'getRandomBytesAsync');
24 }
25}
26//# sourceMappingURL=Random.js.map
\No newline at end of file