Small adjustment to x-* exports (#1809)

This commit is contained in:
Jaco
2023-04-14 10:22:53 +03:00
committed by GitHub
parent 38ac05203e
commit 4ec2cf9e43
3 changed files with 22 additions and 13 deletions
+15 -3
View File
@@ -5,6 +5,18 @@ import { extractGlobal } from '@polkadot/x-global';
export { packageInfo } from './packageInfo.js';
// There are _still_ some older environments (specifically RN < 0.70), that does
// not have proper BigInt support - a non-working fallback is provided for those
export const BigInt = /*#__PURE__*/ extractGlobal('BigInt', () => Number.NaN);
/**
* @internal
*
* There are _still_ some older environments (specifically RN < 0.70), that does
* not have proper BigInt support - a non-working fallback is provided for those.
*
* We detect availability of BigInt upon usage, so this is purely to allow functional
* compilation & bundling. Since we have operators such as *+-/ top-level, a number-ish
* result is used here.
*/
function invalidFallback (): number {
return Number.NaN;
}
export const BigInt = /*#__PURE__*/ extractGlobal('BigInt', invalidFallback);
+1
View File
@@ -2,6 +2,7 @@
// 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
+6 -10
View File
@@ -10,7 +10,7 @@ import { NativeModules } from 'react-native';
import { base64Decode } from '@polkadot/wasm-util/base64';
import { xglobal } from '@polkadot/x-global';
import { getRandomValues as getRandomValuesGlobal } from './browser.js';
import { getRandomValues as getRandomValuesBrowser } from './browser.js';
import { insecureRandomValues } from './fallback.js';
export { packageInfo } from './packageInfo.js';
@@ -24,17 +24,13 @@ interface RNExt {
}
}
interface GlobalExt {
nativeCallSyncHook: unknown;
}
/**
* @internal
*
* A getRandomValues util that detects and uses the available RN
* random utiliy generation functions.
**/
function getRandomValuesNative (output: Uint8Array): Uint8Array {
function getRandomValuesRn (output: Uint8Array): Uint8Array {
return base64Decode(
(NativeModules as RNExt).RNGetRandomValues
? (NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length)
@@ -44,9 +40,9 @@ function getRandomValuesNative (output: Uint8Array): Uint8Array {
}
export const getRandomValues = (
typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function'
? getRandomValuesGlobal
: (typeof (xglobal as unknown as GlobalExt).nativeCallSyncHook === 'undefined' || !NativeModules.ExpoRandom)
(typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function')
? getRandomValuesBrowser
: (typeof xglobal.nativeCallSyncHook === 'undefined' || !NativeModules.ExpoRandom)
? insecureRandomValues
: getRandomValuesNative
: getRandomValuesRn
);