@cloudglides/nox 1.1.0 → 1.1.1
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/example/README.md +26 -0
- package/example/index.html +13 -0
- package/example/package-lock.json +1636 -0
- package/example/package.json +20 -0
- package/example/src/App.css +161 -0
- package/example/src/App.jsx +176 -0
- package/example/src/index.css +55 -0
- package/example/src/main.jsx +10 -0
- package/example/vite.config.js +11 -0
- package/package.json +9 -8
- package/src/core.js +2 -0
- package/src/index.browser.js +31 -0
- package/src/index.js +1 -0
- package/src/presets.js +7 -0
- package/src/rng.js +6 -5
- package/src/utils/entropy.browser.js +63 -0
- package/src/utils/entropy.js +55 -12
package/src/utils/entropy.js
CHANGED
|
@@ -1,19 +1,50 @@
|
|
|
1
|
-
import { performance } from 'perf_hooks';
|
|
2
|
-
import { randomBytes } from 'crypto';
|
|
3
|
-
|
|
4
1
|
let cryptoCache = null;
|
|
5
2
|
let cryptoCacheTime = 0;
|
|
6
3
|
|
|
4
|
+
const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
|
|
5
|
+
const isBrowser = typeof window !== 'undefined';
|
|
6
|
+
|
|
7
|
+
let performanceImpl = typeof performance !== 'undefined' ? performance : null;
|
|
8
|
+
let randomBytesImpl = null;
|
|
9
|
+
|
|
10
|
+
// Avoid parsing require() for browser - use indirect method
|
|
11
|
+
if (isNode) {
|
|
12
|
+
try {
|
|
13
|
+
const getRequire = new Function('return require');
|
|
14
|
+
const req = getRequire();
|
|
15
|
+
const perf_hooks = req('perf_hooks');
|
|
16
|
+
performanceImpl = perf_hooks.performance;
|
|
17
|
+
} catch {}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const getRequire = new Function('return require');
|
|
21
|
+
const req = getRequire();
|
|
22
|
+
const crypto_mod = req('crypto');
|
|
23
|
+
randomBytesImpl = crypto_mod.randomBytes;
|
|
24
|
+
} catch {}
|
|
25
|
+
}
|
|
26
|
+
|
|
7
27
|
export const fromPerformance = () => {
|
|
8
|
-
|
|
9
|
-
|
|
28
|
+
try {
|
|
29
|
+
if (performanceImpl && performanceImpl.now) {
|
|
30
|
+
const t = performanceImpl.now();
|
|
31
|
+
return BigInt(Math.floor(t * 1000)) ^ BigInt(Math.floor(t * 1000000) % 1000000);
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
// fallback
|
|
35
|
+
}
|
|
36
|
+
return BigInt(Date.now());
|
|
10
37
|
};
|
|
11
38
|
|
|
12
39
|
export const fromMemory = () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
40
|
+
try {
|
|
41
|
+
if (isNode && typeof process !== 'undefined' && process.memoryUsage) {
|
|
42
|
+
const mem = process.memoryUsage();
|
|
43
|
+
const total = mem.heapUsed + mem.external + mem.heapTotal;
|
|
44
|
+
return BigInt(Math.floor(total)) ^ BigInt(Date.now());
|
|
45
|
+
}
|
|
46
|
+
} catch {
|
|
47
|
+
// fallback
|
|
17
48
|
}
|
|
18
49
|
return BigInt(Date.now());
|
|
19
50
|
};
|
|
@@ -21,14 +52,26 @@ export const fromMemory = () => {
|
|
|
21
52
|
export const fromCrypto = (bytes = 8) => {
|
|
22
53
|
try {
|
|
23
54
|
const now = Date.now();
|
|
55
|
+
|
|
24
56
|
if (cryptoCache && (now - cryptoCacheTime) < 100) {
|
|
25
57
|
return cryptoCache;
|
|
26
58
|
}
|
|
27
59
|
|
|
28
|
-
const buf = randomBytes(Math.max(bytes, 8));
|
|
29
60
|
let val = 0n;
|
|
30
|
-
|
|
31
|
-
|
|
61
|
+
|
|
62
|
+
if (randomBytesImpl) {
|
|
63
|
+
const buf = randomBytesImpl(Math.max(bytes, 8));
|
|
64
|
+
for (let i = 0; i < buf.length; i++) {
|
|
65
|
+
val = (val << 8n) | BigInt(buf[i]);
|
|
66
|
+
}
|
|
67
|
+
} else if (isBrowser && typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
68
|
+
const arr = new Uint8Array(Math.max(bytes, 8));
|
|
69
|
+
crypto.getRandomValues(arr);
|
|
70
|
+
for (let i = 0; i < arr.length; i++) {
|
|
71
|
+
val = (val << 8n) | BigInt(arr[i]);
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
return BigInt(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
32
75
|
}
|
|
33
76
|
|
|
34
77
|
cryptoCache = val;
|