Files
Satoshi Qazi Muhammed 81e25b008b x-randomvalues: declare NativeModules locally; resolve braces to @pezkuwi/braces
x-randomvalues read one export from react-native, NativeModules, and
took its types from the deprecated @types/react-native, which depends
on react-native itself: react-native 0.83, metro 0.83 and with them
image-size (open advisory, fixed only in 2.x, which metro 0.83 does not
accept). The two native modules it looks for are now declared in
src/react-native-modules.d.ts; the casts to RNExt are gone and each
module is checked before use. The published react-native.d.ts does not
reference react-native, and react-native/metro leave the dependency
tree.

braces resolves to @pezkuwi/braces 3.0.4: braces 3.0.3, the latest upstream release, has an open advisory with no fixed version (CVE-2026-93687, stack exhaustion through deeply nested patterns); the fork rejects nesting deeper than maxDepth (500) with a SyntaxError.
2026-10-10 04:01:02 +04:00

55 lines
1.9 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/x-randomvalues authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Adapted from https://github.com/LinusU/react-native-get-random-values/blob/85f48393821c23b83b89a8177f56d3a81dc8b733/index.js
//
// Copyright (c) 2018, 2020 Linus Unnebäck
// SPDX-License-Identifier: MIT
import { NativeModules } from 'react-native';
import { base64Decode } from '@pezkuwi/wasm-util/base64';
import { xglobal } from '@pezkuwi/x-global';
import { crypto as cryptoBrowser, getRandomValues as getRandomValuesBrowser } from './browser.js';
export { packageInfo } from './packageInfo.js';
/**
* @internal
*
* A getRandomValues util that detects and uses the available RN
* random utiliy generation functions.
**/
function getRandomValuesRn (output: Uint8Array): Uint8Array {
const { ExpoRandom, RNGetRandomValues } = NativeModules;
if (RNGetRandomValues) {
return base64Decode(RNGetRandomValues.getRandomBase64(output.length), output);
} else if (ExpoRandom) {
return base64Decode(ExpoRandom.getRandomBase64String(output.length), output);
}
throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues and no React Native secure RNG module is available.');
}
// Check for native RN modules first (highest priority)
const hasNativeRNModules = !!NativeModules.ExpoRandom || !!NativeModules.RNGetRandomValues;
const hasNativeCrypto = typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function';
export const getRandomValues = (
hasNativeRNModules
? getRandomValuesRn
: hasNativeCrypto
? getRandomValuesBrowser
: () => {
throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues.');
}
);
export const crypto = (
getRandomValues === getRandomValuesBrowser
? cryptoBrowser
: { getRandomValues }
);