@pezkuwi/x-randomvalues 14.0.7 → 14.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/LICENSE +201 -0
- package/build/README.md +3 -0
- package/build/browser.js +6 -0
- package/build/cjs/browser.d.ts +3 -0
- package/build/cjs/browser.js +11 -0
- package/build/cjs/fallback.js +18 -0
- package/build/cjs/node.js +13 -0
- package/build/cjs/packageInfo.js +4 -0
- package/build/cjs/react-native.js +35 -0
- package/build/cjs/shim.js +5 -0
- package/build/fallback.d.ts +1 -0
- package/build/fallback.js +15 -0
- package/build/node.d.ts +3 -0
- package/build/node.js +7 -0
- package/build/package.json +197 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/packageInfo.js +1 -0
- package/build/react-native.d.ts +12 -0
- package/build/react-native.js +31 -0
- package/build/shim.d.ts +1 -0
- package/build/shim.js +3 -0
- package/build-deno/README.md +3 -0
- package/build-deno/browser.ts +10 -0
- package/build-deno/fallback.ts +22 -0
- package/build-deno/mod.ts +2 -0
- package/build-deno/node.ts +12 -0
- package/build-deno/packageInfo.ts +3 -0
- package/build-deno/react-native.ts +57 -0
- package/build-deno/shim.ts +5 -0
- package/{cjs → build-tsc-cjs}/packageInfo.js +1 -1
- package/{packageInfo.js → build-tsc-esm/packageInfo.js} +1 -1
- package/package.json +13 -175
- package/src/browser.ts +14 -0
- package/src/fallback.spec.ts +22 -0
- package/src/fallback.ts +28 -0
- package/src/mod.ts +4 -0
- package/src/node.ts +17 -0
- package/src/packageInfo.ts +6 -0
- package/src/react-native.ts +64 -0
- package/src/shim.ts +7 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +17 -0
- package/tsconfig.spec.tsbuildinfo +1 -0
- /package/{cjs → build}/browser.d.ts +0 -0
- /package/{fallback.d.ts → build/cjs/fallback.d.ts} +0 -0
- /package/{node.d.ts → build/cjs/node.d.ts} +0 -0
- /package/{cjs → build/cjs}/package.json +0 -0
- /package/{packageInfo.d.ts → build/cjs/packageInfo.d.ts} +0 -0
- /package/{react-native.d.ts → build/cjs/react-native.d.ts} +0 -0
- /package/{shim.d.ts → build/cjs/shim.d.ts} +0 -0
- /package/{browser.d.ts → build-tsc/browser.d.ts} +0 -0
- /package/{cjs → build-tsc}/fallback.d.ts +0 -0
- /package/{cjs → build-tsc}/node.d.ts +0 -0
- /package/{cjs → build-tsc}/packageInfo.d.ts +0 -0
- /package/{cjs → build-tsc}/react-native.d.ts +0 -0
- /package/{cjs → build-tsc}/shim.d.ts +0 -0
- /package/{cjs → build-tsc-cjs}/browser.js +0 -0
- /package/{cjs → build-tsc-cjs}/fallback.js +0 -0
- /package/{cjs → build-tsc-cjs}/node.js +0 -0
- /package/{cjs → build-tsc-cjs}/react-native.js +0 -0
- /package/{cjs → build-tsc-cjs}/shim.js +0 -0
- /package/{browser.js → build-tsc-esm/browser.js} +0 -0
- /package/{fallback.js → build-tsc-esm/fallback.js} +0 -0
- /package/{node.js → build-tsc-esm/node.js} +0 -0
- /package/{react-native.js → build-tsc-esm/react-native.js} +0 -0
- /package/{shim.js → build-tsc-esm/shim.js} +0 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
|
|
2
|
+
import nodeCrypto from 'node:crypto';
|
|
3
|
+
|
|
4
|
+
import { extractGlobal } from 'https://deno.land/x/pezkuwi/x-global/mod.ts';
|
|
5
|
+
|
|
6
|
+
export { packageInfo } from './packageInfo.ts';
|
|
7
|
+
|
|
8
|
+
export const crypto = /*#__PURE__*/ extractGlobal('crypto', nodeCrypto.webcrypto);
|
|
9
|
+
|
|
10
|
+
export function getRandomValues <T extends Uint8Array> (output: T): T {
|
|
11
|
+
return crypto.getRandomValues(output);
|
|
12
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { NativeModules } from 'https://esm.sh/react-native';
|
|
4
|
+
|
|
5
|
+
import { base64Decode } from 'https://deno.land/x/pezkuwi/wasm-util/base64.ts';
|
|
6
|
+
import { xglobal } from 'https://deno.land/x/pezkuwi/x-global/mod.ts';
|
|
7
|
+
|
|
8
|
+
import { crypto as cryptoBrowser, getRandomValues as getRandomValuesBrowser } from './browser.ts';
|
|
9
|
+
|
|
10
|
+
export { packageInfo } from './packageInfo.ts';
|
|
11
|
+
|
|
12
|
+
interface RNExt {
|
|
13
|
+
ExpoRandom: {
|
|
14
|
+
getRandomBase64String: (length: number) => string;
|
|
15
|
+
};
|
|
16
|
+
RNGetRandomValues: {
|
|
17
|
+
getRandomBase64: (length: number) => string;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
*
|
|
24
|
+
* A getRandomValues util that detects and uses the available RN
|
|
25
|
+
* random utiliy generation functions.
|
|
26
|
+
**/
|
|
27
|
+
function getRandomValuesRn (output: Uint8Array): Uint8Array {
|
|
28
|
+
if (!NativeModules['ExpoRandom'] && !(NativeModules as RNExt).RNGetRandomValues) {
|
|
29
|
+
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.');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return base64Decode(
|
|
33
|
+
(NativeModules as RNExt).RNGetRandomValues
|
|
34
|
+
? (NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length)
|
|
35
|
+
: (NativeModules as RNExt).ExpoRandom.getRandomBase64String(output.length),
|
|
36
|
+
output
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const hasNativeRNModules = !!NativeModules['ExpoRandom'] || !!(NativeModules as RNExt).RNGetRandomValues;
|
|
41
|
+
const hasNativeCrypto = typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function';
|
|
42
|
+
|
|
43
|
+
export const getRandomValues = (
|
|
44
|
+
hasNativeRNModules
|
|
45
|
+
? getRandomValuesRn
|
|
46
|
+
: hasNativeCrypto
|
|
47
|
+
? getRandomValuesBrowser
|
|
48
|
+
: () => {
|
|
49
|
+
throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues.');
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
export const crypto = (
|
|
54
|
+
getRandomValues === getRandomValuesBrowser
|
|
55
|
+
? cryptoBrowser
|
|
56
|
+
: { getRandomValues }
|
|
57
|
+
);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.packageInfo = void 0;
|
|
4
|
-
exports.packageInfo = { name: '@pezkuwi/x-randomvalues', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '14.0.
|
|
4
|
+
exports.packageInfo = { name: '@pezkuwi/x-randomvalues', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '14.0.10' };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const packageInfo = { name: '@pezkuwi/x-randomvalues', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '14.0.
|
|
1
|
+
export const packageInfo = { name: '@pezkuwi/x-randomvalues', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '14.0.10' };
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"author": "
|
|
2
|
+
"author": "Pezkuwi Team <team@pezkuwichain.app>",
|
|
3
3
|
"bugs": "https://github.com/pezkuwichain/pezkuwi-common/issues",
|
|
4
4
|
"description": "A cross-environment window.crypto.getRandomValues replacement",
|
|
5
5
|
"engines": {
|
|
@@ -15,183 +15,21 @@
|
|
|
15
15
|
},
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"type": "module",
|
|
18
|
-
"version": "14.0.
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"react-native": "./cjs/react-native.js",
|
|
23
|
-
"types": "./node.d.ts",
|
|
24
|
-
"exports": {
|
|
25
|
-
"./cjs/package.json": "./cjs/package.json",
|
|
26
|
-
"./cjs/*": "./cjs/*.js",
|
|
27
|
-
".": {
|
|
28
|
-
"types": "./node.d.ts",
|
|
29
|
-
"react-native": {
|
|
30
|
-
"module": {
|
|
31
|
-
"types": "./react-native.d.ts",
|
|
32
|
-
"default": "./react-native.js"
|
|
33
|
-
},
|
|
34
|
-
"require": {
|
|
35
|
-
"types": "./cjs/react-native.d.ts",
|
|
36
|
-
"default": "./cjs/react-native.js"
|
|
37
|
-
},
|
|
38
|
-
"default": {
|
|
39
|
-
"types": "./react-native.d.ts",
|
|
40
|
-
"default": "./react-native.js"
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
"browser": {
|
|
44
|
-
"module": {
|
|
45
|
-
"types": "./browser.d.ts",
|
|
46
|
-
"default": "./browser.js"
|
|
47
|
-
},
|
|
48
|
-
"require": {
|
|
49
|
-
"types": "./cjs/browser.d.ts",
|
|
50
|
-
"default": "./cjs/browser.js"
|
|
51
|
-
},
|
|
52
|
-
"default": {
|
|
53
|
-
"types": "./browser.d.ts",
|
|
54
|
-
"default": "./browser.js"
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
"node": {
|
|
58
|
-
"module": {
|
|
59
|
-
"types": "./node.d.ts",
|
|
60
|
-
"default": "./node.js"
|
|
61
|
-
},
|
|
62
|
-
"require": {
|
|
63
|
-
"types": "./cjs/node.d.ts",
|
|
64
|
-
"default": "./cjs/node.js"
|
|
65
|
-
},
|
|
66
|
-
"default": {
|
|
67
|
-
"types": "./node.d.ts",
|
|
68
|
-
"default": "./node.js"
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
"./browser": {
|
|
73
|
-
"module": {
|
|
74
|
-
"types": "./browser.d.ts",
|
|
75
|
-
"default": "./browser.js"
|
|
76
|
-
},
|
|
77
|
-
"require": {
|
|
78
|
-
"types": "./cjs/browser.d.ts",
|
|
79
|
-
"default": "./cjs/browser.js"
|
|
80
|
-
},
|
|
81
|
-
"default": {
|
|
82
|
-
"types": "./browser.d.ts",
|
|
83
|
-
"default": "./browser.js"
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
"./fallback": {
|
|
87
|
-
"module": {
|
|
88
|
-
"types": "./fallback.d.ts",
|
|
89
|
-
"default": "./fallback.js"
|
|
90
|
-
},
|
|
91
|
-
"require": {
|
|
92
|
-
"types": "./cjs/fallback.d.ts",
|
|
93
|
-
"default": "./cjs/fallback.js"
|
|
94
|
-
},
|
|
95
|
-
"default": {
|
|
96
|
-
"types": "./fallback.d.ts",
|
|
97
|
-
"default": "./fallback.js"
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
"./node": {
|
|
101
|
-
"module": {
|
|
102
|
-
"types": "./node.d.ts",
|
|
103
|
-
"default": "./node.js"
|
|
104
|
-
},
|
|
105
|
-
"require": {
|
|
106
|
-
"types": "./cjs/node.d.ts",
|
|
107
|
-
"default": "./cjs/node.js"
|
|
108
|
-
},
|
|
109
|
-
"default": {
|
|
110
|
-
"types": "./node.d.ts",
|
|
111
|
-
"default": "./node.js"
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
"./package.json": {
|
|
115
|
-
"require": "./cjs/package.json",
|
|
116
|
-
"default": "./package.json"
|
|
117
|
-
},
|
|
118
|
-
"./packageInfo.js": {
|
|
119
|
-
"module": {
|
|
120
|
-
"types": "./packageInfo.d.ts",
|
|
121
|
-
"default": "./packageInfo.js"
|
|
122
|
-
},
|
|
123
|
-
"require": {
|
|
124
|
-
"types": "./cjs/packageInfo.d.ts",
|
|
125
|
-
"default": "./cjs/packageInfo.js"
|
|
126
|
-
},
|
|
127
|
-
"default": {
|
|
128
|
-
"types": "./packageInfo.d.ts",
|
|
129
|
-
"default": "./packageInfo.js"
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
"./packageInfo": {
|
|
133
|
-
"module": {
|
|
134
|
-
"types": "./packageInfo.d.ts",
|
|
135
|
-
"default": "./packageInfo.js"
|
|
136
|
-
},
|
|
137
|
-
"require": {
|
|
138
|
-
"types": "./cjs/packageInfo.d.ts",
|
|
139
|
-
"default": "./cjs/packageInfo.js"
|
|
140
|
-
},
|
|
141
|
-
"default": {
|
|
142
|
-
"types": "./packageInfo.d.ts",
|
|
143
|
-
"default": "./packageInfo.js"
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
"./react-native": {
|
|
147
|
-
"module": {
|
|
148
|
-
"types": "./react-native.d.ts",
|
|
149
|
-
"default": "./react-native.js"
|
|
150
|
-
},
|
|
151
|
-
"require": {
|
|
152
|
-
"types": "./cjs/react-native.d.ts",
|
|
153
|
-
"default": "./cjs/react-native.js"
|
|
154
|
-
},
|
|
155
|
-
"default": {
|
|
156
|
-
"types": "./react-native.d.ts",
|
|
157
|
-
"default": "./react-native.js"
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
"./shim.js": {
|
|
161
|
-
"module": {
|
|
162
|
-
"types": "./shim.d.ts",
|
|
163
|
-
"default": "./shim.js"
|
|
164
|
-
},
|
|
165
|
-
"require": {
|
|
166
|
-
"types": "./cjs/shim.d.ts",
|
|
167
|
-
"default": "./cjs/shim.js"
|
|
168
|
-
},
|
|
169
|
-
"default": {
|
|
170
|
-
"types": "./shim.d.ts",
|
|
171
|
-
"default": "./shim.js"
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
"./shim": {
|
|
175
|
-
"module": {
|
|
176
|
-
"types": "./shim.d.ts",
|
|
177
|
-
"default": "./shim.js"
|
|
178
|
-
},
|
|
179
|
-
"require": {
|
|
180
|
-
"types": "./cjs/shim.d.ts",
|
|
181
|
-
"default": "./cjs/shim.js"
|
|
182
|
-
},
|
|
183
|
-
"default": {
|
|
184
|
-
"types": "./shim.d.ts",
|
|
185
|
-
"default": "./shim.js"
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
},
|
|
18
|
+
"version": "14.0.10",
|
|
19
|
+
"browser": "browser.js",
|
|
20
|
+
"main": "node.js",
|
|
21
|
+
"react-native": "react-native.js",
|
|
189
22
|
"dependencies": {
|
|
190
|
-
"@pezkuwi/x-global": "14.0.
|
|
23
|
+
"@pezkuwi/x-global": "14.0.10",
|
|
191
24
|
"tslib": "^2.8.0"
|
|
192
25
|
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@pezkuwi/util": "14.0.10",
|
|
28
|
+
"@pezkuwi/wasm-util": "^7.5.3",
|
|
29
|
+
"@types/react-native": "^0.73.0"
|
|
30
|
+
},
|
|
193
31
|
"peerDependencies": {
|
|
194
|
-
"@pezkuwi/util": "14.0.
|
|
32
|
+
"@pezkuwi/util": "14.0.10",
|
|
195
33
|
"@pezkuwi/wasm-util": "*"
|
|
196
34
|
}
|
|
197
|
-
}
|
|
35
|
+
}
|
package/src/browser.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2017-2025 @pezkuwi/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { xglobal } from '@pezkuwi/x-global';
|
|
5
|
+
|
|
6
|
+
export { packageInfo } from './packageInfo.js';
|
|
7
|
+
|
|
8
|
+
export const crypto = xglobal.crypto;
|
|
9
|
+
|
|
10
|
+
// getRandomValues needs to be called on the crypto object,
|
|
11
|
+
// hence the need for the wrapper function
|
|
12
|
+
export function getRandomValues <T extends Uint8Array> (arr: T): T {
|
|
13
|
+
return crypto.getRandomValues(arr);
|
|
14
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright 2017-2025 @pezkuwi/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { insecureRandomValues } from './fallback.js';
|
|
7
|
+
|
|
8
|
+
describe('fallback (insecure)', (): void => {
|
|
9
|
+
it('subsequent results does not match', (): void => {
|
|
10
|
+
expect(
|
|
11
|
+
insecureRandomValues(new Uint8Array(32)).toString()
|
|
12
|
+
).not.toEqual(
|
|
13
|
+
insecureRandomValues(new Uint8Array(32)).toString()
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('generates with the supplied length', (): void => {
|
|
18
|
+
expect(
|
|
19
|
+
insecureRandomValues(new Uint8Array(128))
|
|
20
|
+
).toHaveLength(128);
|
|
21
|
+
});
|
|
22
|
+
});
|
package/src/fallback.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright 2017-2025 @pezkuwi/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Adapted from https://github.com/LinusU/react-native-get-random-values/blob/85f48393821c23b83b89a8177f56d3a81dc8b733/index.js
|
|
5
|
+
//
|
|
6
|
+
// Copyright (c) 2018, 2020 Linus Unnebäck
|
|
7
|
+
// SPDX-License-Identifier: MIT
|
|
8
|
+
|
|
9
|
+
let warned = false;
|
|
10
|
+
|
|
11
|
+
export function insecureRandomValues <T extends Uint8Array> (arr: T): T {
|
|
12
|
+
if (!warned) {
|
|
13
|
+
console.warn('Using an insecure random number generator, this should only happen when running in a debugger without support for crypto');
|
|
14
|
+
warned = true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let r = 0;
|
|
18
|
+
|
|
19
|
+
for (let i = 0, count = arr.length; i < count; i++) {
|
|
20
|
+
if ((i & 0b11) === 0) {
|
|
21
|
+
r = Math.random() * 0x100000000;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
arr[i] = (r >>> ((i & 0b11) << 3)) & 0xff;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return arr;
|
|
28
|
+
}
|
package/src/mod.ts
ADDED
package/src/node.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright 2017-2025 @pezkuwi/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Needs Node 15+ for webcrypto
|
|
5
|
+
import nodeCrypto from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
import { extractGlobal } from '@pezkuwi/x-global';
|
|
8
|
+
|
|
9
|
+
export { packageInfo } from './packageInfo.js';
|
|
10
|
+
|
|
11
|
+
export const crypto = /*#__PURE__*/ extractGlobal('crypto', nodeCrypto.webcrypto);
|
|
12
|
+
|
|
13
|
+
// getRandomValues needs to be called on the crypto object,
|
|
14
|
+
// hence the need for the wrapper function
|
|
15
|
+
export function getRandomValues <T extends Uint8Array> (output: T): T {
|
|
16
|
+
return crypto.getRandomValues(output);
|
|
17
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright 2017-2026 @pezkuwi/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Do not edit, auto-generated by @pezkuwi/dev
|
|
5
|
+
|
|
6
|
+
export const packageInfo = { name: '@pezkuwi/x-randomvalues', path: 'auto', type: 'auto', version: '14.0.10' };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright 2017-2025 @pezkuwi/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Adapted from https://github.com/LinusU/react-native-get-random-values/blob/85f48393821c23b83b89a8177f56d3a81dc8b733/index.js
|
|
5
|
+
//
|
|
6
|
+
// Copyright (c) 2018, 2020 Linus Unnebäck
|
|
7
|
+
// SPDX-License-Identifier: MIT
|
|
8
|
+
|
|
9
|
+
import { NativeModules } from 'react-native';
|
|
10
|
+
|
|
11
|
+
import { base64Decode } from '@pezkuwi/wasm-util/base64';
|
|
12
|
+
import { xglobal } from '@pezkuwi/x-global';
|
|
13
|
+
|
|
14
|
+
import { crypto as cryptoBrowser, getRandomValues as getRandomValuesBrowser } from './browser.js';
|
|
15
|
+
|
|
16
|
+
export { packageInfo } from './packageInfo.js';
|
|
17
|
+
|
|
18
|
+
interface RNExt {
|
|
19
|
+
ExpoRandom: {
|
|
20
|
+
getRandomBase64String: (length: number) => string;
|
|
21
|
+
};
|
|
22
|
+
RNGetRandomValues: {
|
|
23
|
+
getRandomBase64: (length: number) => string;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
*
|
|
30
|
+
* A getRandomValues util that detects and uses the available RN
|
|
31
|
+
* random utiliy generation functions.
|
|
32
|
+
**/
|
|
33
|
+
function getRandomValuesRn (output: Uint8Array): Uint8Array {
|
|
34
|
+
if (!NativeModules['ExpoRandom'] && !(NativeModules as RNExt).RNGetRandomValues) {
|
|
35
|
+
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.');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return base64Decode(
|
|
39
|
+
(NativeModules as RNExt).RNGetRandomValues
|
|
40
|
+
? (NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length)
|
|
41
|
+
: (NativeModules as RNExt).ExpoRandom.getRandomBase64String(output.length),
|
|
42
|
+
output
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Check for native RN modules first (highest priority)
|
|
47
|
+
const hasNativeRNModules = !!NativeModules['ExpoRandom'] || !!(NativeModules as RNExt).RNGetRandomValues;
|
|
48
|
+
const hasNativeCrypto = typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function';
|
|
49
|
+
|
|
50
|
+
export const getRandomValues = (
|
|
51
|
+
hasNativeRNModules
|
|
52
|
+
? getRandomValuesRn
|
|
53
|
+
: hasNativeCrypto
|
|
54
|
+
? getRandomValuesBrowser
|
|
55
|
+
: () => {
|
|
56
|
+
throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues.');
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
export const crypto = (
|
|
61
|
+
getRandomValues === getRandomValuesBrowser
|
|
62
|
+
? cryptoBrowser
|
|
63
|
+
: { getRandomValues }
|
|
64
|
+
);
|
package/src/shim.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": "..",
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"rootDir": "./src"
|
|
7
|
+
},
|
|
8
|
+
"exclude": [
|
|
9
|
+
"**/*.spec.ts",
|
|
10
|
+
"**/mod.ts"
|
|
11
|
+
],
|
|
12
|
+
"references": [
|
|
13
|
+
{ "path": "../x-global/tsconfig.build.json" }
|
|
14
|
+
]
|
|
15
|
+
}
|