1 | import { UnavailabilityError } from '@unimodules/core';
|
2 | import { toByteArray } from 'base64-js';
|
3 | import ExpoRandom from './ExpoRandom';
|
4 | function 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 | }
|
12 | export 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 |
|
\ | No newline at end of file |