1import { toByteArray } from 'base64-js'; 2import { UnavailabilityError } from 'expo-modules-core'; 3import ExpoRandom from './ExpoRandom'; 4const warnIsDeprecated = (functionName) => console.warn(`expo-random is deprecated in favor of expo-crypto: use ExpoCrypto.${functionName}()instead. https://docs.expo.dev/versions/latest/sdk/crypto/`); 5function assertByteCount(value, methodName) { 6 warnIsDeprecated('assertByteCount'); 7 if (typeof value !== 'number' || 8 isNaN(value) || 9 Math.floor(value) < 0 || 10 Math.floor(value) > 1024) { 11 throw new TypeError(`expo-random: ${methodName}(${value}) expected a valid number from range 0...1024`); 12 } 13} 14// @needsAudit 15/** 16 * Generates completely random bytes using native implementations. The `byteCount` property 17 * is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`. 18 * Falls back to `Math.random` during development to prevent issues with React Native Debugger. 19 * @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`. 20 * @return An array of random bytes with the same length as the `byteCount`. 21 */ 22export function getRandomBytes(byteCount) { 23 warnIsDeprecated('getRandomBytes'); 24 assertByteCount(byteCount, 'getRandomBytes'); 25 const validByteCount = Math.floor(byteCount); 26 if (__DEV__) { 27 if (!global.nativeCallSyncHook || global.__REMOTEDEV__) { 28 // remote javascript debugging is enabled 29 const array = new Uint8Array(validByteCount); 30 for (let i = 0; i < validByteCount; i++) { 31 array[i] = Math.floor(Math.random() * 256); 32 } 33 return array; 34 } 35 } 36 if (ExpoRandom.getRandomBytes) { 37 return ExpoRandom.getRandomBytes(validByteCount); 38 } 39 else if (ExpoRandom.getRandomBase64String) { 40 const base64 = ExpoRandom.getRandomBase64String(validByteCount); 41 return toByteArray(base64); 42 } 43 else { 44 throw new UnavailabilityError('expo-random', 'getRandomBytes'); 45 } 46} 47// @needsAudit 48/** 49 * Generates completely random bytes using native implementations. The `byteCount` property 50 * is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`. 51 * @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`. 52 * @return A promise that fulfills with an array of random bytes with the same length as the `byteCount`. 53 */ 54export async function getRandomBytesAsync(byteCount) { 55 warnIsDeprecated('getRandomBytesAsync'); 56 assertByteCount(byteCount, 'getRandomBytesAsync'); 57 const validByteCount = Math.floor(byteCount); 58 if (ExpoRandom.getRandomBytesAsync) { 59 return await ExpoRandom.getRandomBytesAsync(validByteCount); 60 } 61 else if (ExpoRandom.getRandomBase64StringAsync) { 62 const base64 = await ExpoRandom.getRandomBase64StringAsync(validByteCount); 63 return toByteArray(base64); 64 } 65 else { 66 throw new UnavailabilityError('expo-random', 'getRandomBytesAsync'); 67 } 68} 69//# sourceMappingURL=Random.js.map