mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-10-24 20:45:01 +00:00
Merge pull request #11 from pezkuwichain/deps/no-unfixed-advisories
x-randomvalues: declare NativeModules locally; resolve braces to @pezkuwi/braces
This commit is contained in:
5 files changed
+80
-1885
No files matched your search
@@ -40,6 +40,7 @@
|
||||
"@types/node": "^22.7.5"
|
||||
},
|
||||
"resolutions": {
|
||||
"braces": "npm:@pezkuwi/braces@^3.0.4",
|
||||
"typescript": "^5.5.4"
|
||||
}
|
||||
}
|
||||
@@ -23,9 +23,6 @@
|
||||
"@pezkuwi/x-global": "14.0.26",
|
||||
"tslib": "^2.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react-native": "^0.73.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@pezkuwi/util": "14.0.26",
|
||||
"@pezkuwi/wasm-util": "*"
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright 2017-2026 @pezkuwi/x-randomvalues authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// The one React Native export react-native.ts reads, and only the native
|
||||
// modules it looks for. Declared here instead of installing react-native for
|
||||
// its types, which brought in its metro and CLI tree as well.
|
||||
declare module 'react-native' {
|
||||
interface RandomModules {
|
||||
ExpoRandom?: {
|
||||
getRandomBase64String: (length: number) => string;
|
||||
};
|
||||
RNGetRandomValues?: {
|
||||
getRandomBase64: (length: number) => string;
|
||||
};
|
||||
}
|
||||
|
||||
export const NativeModules: RandomModules;
|
||||
}
|
||||
@@ -15,15 +15,6 @@ import { crypto as cryptoBrowser, getRandomValues as getRandomValuesBrowser } fr
|
||||
|
||||
export { packageInfo } from './packageInfo.js';
|
||||
|
||||
interface RNExt {
|
||||
ExpoRandom: {
|
||||
getRandomBase64String: (length: number) => string;
|
||||
};
|
||||
RNGetRandomValues: {
|
||||
getRandomBase64: (length: number) => string;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
@@ -31,20 +22,19 @@ interface RNExt {
|
||||
* random utiliy generation functions.
|
||||
**/
|
||||
function getRandomValuesRn (output: Uint8Array): Uint8Array {
|
||||
if (!NativeModules['ExpoRandom'] && !(NativeModules as RNExt).RNGetRandomValues) {
|
||||
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.');
|
||||
const { ExpoRandom, RNGetRandomValues } = NativeModules;
|
||||
|
||||
if (RNGetRandomValues) {
|
||||
return base64Decode(RNGetRandomValues.getRandomBase64(output.length), output);
|
||||
} else if (ExpoRandom) {
|
||||
return base64Decode(ExpoRandom.getRandomBase64String(output.length), output);
|
||||
}
|
||||
|
||||
return base64Decode(
|
||||
(NativeModules as RNExt).RNGetRandomValues
|
||||
? (NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length)
|
||||
: (NativeModules as RNExt).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 as RNExt).RNGetRandomValues;
|
||||
const hasNativeRNModules = !!NativeModules.ExpoRandom || !!NativeModules.RNGetRandomValues;
|
||||
const hasNativeCrypto = typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function';
|
||||
|
||||
export const getRandomValues = (
|
||||
|
||||
Reference in new issue
Block a user