@cloudglides/nox 1.1.0 → 1.1.2
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.browser.js +10 -0
- 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.browser.js +141 -0
- package/src/rng.js +6 -5
- package/src/utils/entropy.browser.js +63 -0
- package/src/utils/entropy.js +51 -12
package/src/rng.js
CHANGED
|
@@ -61,7 +61,7 @@ export class RNG {
|
|
|
61
61
|
if (min > max) {
|
|
62
62
|
throw new Error('min must be less than or equal to max');
|
|
63
63
|
}
|
|
64
|
-
|
|
64
|
+
|
|
65
65
|
const count = Math.floor((max - min) / step) + 1;
|
|
66
66
|
const idx = this.nextInt(count);
|
|
67
67
|
return min + idx * step;
|
|
@@ -84,7 +84,7 @@ export class RNG {
|
|
|
84
84
|
if (typeof fn !== 'function') {
|
|
85
85
|
throw new TypeError('fn must be a function');
|
|
86
86
|
}
|
|
87
|
-
|
|
87
|
+
|
|
88
88
|
const result = [];
|
|
89
89
|
for (let i = 0; i < count; i++) {
|
|
90
90
|
result.push(fn(this, i));
|
|
@@ -99,7 +99,7 @@ export class RNG {
|
|
|
99
99
|
if (count < 0) {
|
|
100
100
|
throw new RangeError('count must be non-negative');
|
|
101
101
|
}
|
|
102
|
-
|
|
102
|
+
|
|
103
103
|
const result = new Array(count);
|
|
104
104
|
for (let i = 0; i < count; i++) {
|
|
105
105
|
result[i] = this.nextFloat();
|
|
@@ -114,7 +114,7 @@ export class RNG {
|
|
|
114
114
|
if (count < 0) {
|
|
115
115
|
throw new RangeError('count must be non-negative');
|
|
116
116
|
}
|
|
117
|
-
|
|
117
|
+
|
|
118
118
|
const result = new Array(count);
|
|
119
119
|
for (let i = 0; i < count; i++) {
|
|
120
120
|
result[i] = this.nextInt(max);
|
|
@@ -129,7 +129,7 @@ export class RNG {
|
|
|
129
129
|
if (count < 0) {
|
|
130
130
|
throw new RangeError('count must be non-negative');
|
|
131
131
|
}
|
|
132
|
-
|
|
132
|
+
|
|
133
133
|
const result = new Array(count);
|
|
134
134
|
for (let i = 0; i < count; i++) {
|
|
135
135
|
result[i] = this.bool(probability);
|
|
@@ -139,3 +139,4 @@ export class RNG {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
export const rng = () => new RNG();
|
|
142
|
+
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
let cryptoCache = null;
|
|
2
|
+
let cryptoCacheTime = 0;
|
|
3
|
+
|
|
4
|
+
export const fromPerformance = () => {
|
|
5
|
+
try {
|
|
6
|
+
if (typeof performance !== 'undefined' && performance.now) {
|
|
7
|
+
const t = performance.now();
|
|
8
|
+
return BigInt(Math.floor(t * 1000)) ^ BigInt(Math.floor(t * 1000000) % 1000000);
|
|
9
|
+
}
|
|
10
|
+
} catch {
|
|
11
|
+
// fallback
|
|
12
|
+
}
|
|
13
|
+
return BigInt(Date.now());
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const fromMemory = () => {
|
|
17
|
+
return BigInt(Date.now());
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const fromCrypto = (bytes = 8) => {
|
|
21
|
+
try {
|
|
22
|
+
const now = Date.now();
|
|
23
|
+
|
|
24
|
+
if (cryptoCache && (now - cryptoCacheTime) < 100) {
|
|
25
|
+
return cryptoCache;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let val = 0n;
|
|
29
|
+
|
|
30
|
+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
31
|
+
const arr = new Uint8Array(Math.max(bytes, 8));
|
|
32
|
+
crypto.getRandomValues(arr);
|
|
33
|
+
for (let i = 0; i < arr.length; i++) {
|
|
34
|
+
val = (val << 8n) | BigInt(arr[i]);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
return BigInt(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
cryptoCache = val;
|
|
41
|
+
cryptoCacheTime = now;
|
|
42
|
+
return val;
|
|
43
|
+
} catch {
|
|
44
|
+
return BigInt(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const combined = () => {
|
|
49
|
+
const perf = fromPerformance();
|
|
50
|
+
const mem = fromMemory();
|
|
51
|
+
const crypto = fromCrypto();
|
|
52
|
+
|
|
53
|
+
let mix = perf ^ mem ^ crypto;
|
|
54
|
+
mix = mix ^ (mix >> 33n);
|
|
55
|
+
mix = (mix * 0xff51afd7ed558ccdn) & ((1n << 64n) - 1n);
|
|
56
|
+
|
|
57
|
+
return mix !== 0n ? mix : 1n;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const clearCryptoCache = () => {
|
|
61
|
+
cryptoCache = null;
|
|
62
|
+
cryptoCacheTime = 0;
|
|
63
|
+
};
|
package/src/utils/entropy.js
CHANGED
|
@@ -1,19 +1,46 @@
|
|
|
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
|
+
// Only require in true Node environment
|
|
11
|
+
if (isNode && !isBrowser) {
|
|
12
|
+
try {
|
|
13
|
+
// Avoid static analysis by constructing module name dynamically
|
|
14
|
+
const moduleName = ['perf', '_hooks'].join('');
|
|
15
|
+
performanceImpl = require(moduleName).performance;
|
|
16
|
+
} catch {}
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
randomBytesImpl = require('crypto').randomBytes;
|
|
20
|
+
} catch {}
|
|
21
|
+
}
|
|
22
|
+
|
|
7
23
|
export const fromPerformance = () => {
|
|
8
|
-
|
|
9
|
-
|
|
24
|
+
try {
|
|
25
|
+
if (performanceImpl && typeof performanceImpl.now === 'function') {
|
|
26
|
+
const t = performanceImpl.now();
|
|
27
|
+
return BigInt(Math.floor(t * 1000)) ^ BigInt(Math.floor(t * 1000000) % 1000000);
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
// fallback
|
|
31
|
+
}
|
|
32
|
+
return BigInt(Date.now());
|
|
10
33
|
};
|
|
11
34
|
|
|
12
35
|
export const fromMemory = () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
36
|
+
try {
|
|
37
|
+
if (isNode && !isBrowser && typeof process !== 'undefined' && process.memoryUsage) {
|
|
38
|
+
const mem = process.memoryUsage();
|
|
39
|
+
const total = mem.heapUsed + mem.external + mem.heapTotal;
|
|
40
|
+
return BigInt(Math.floor(total)) ^ BigInt(Date.now());
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
// fallback
|
|
17
44
|
}
|
|
18
45
|
return BigInt(Date.now());
|
|
19
46
|
};
|
|
@@ -21,14 +48,26 @@ export const fromMemory = () => {
|
|
|
21
48
|
export const fromCrypto = (bytes = 8) => {
|
|
22
49
|
try {
|
|
23
50
|
const now = Date.now();
|
|
51
|
+
|
|
24
52
|
if (cryptoCache && (now - cryptoCacheTime) < 100) {
|
|
25
53
|
return cryptoCache;
|
|
26
54
|
}
|
|
27
55
|
|
|
28
|
-
const buf = randomBytes(Math.max(bytes, 8));
|
|
29
56
|
let val = 0n;
|
|
30
|
-
|
|
31
|
-
|
|
57
|
+
|
|
58
|
+
if (randomBytesImpl) {
|
|
59
|
+
const buf = randomBytesImpl(Math.max(bytes, 8));
|
|
60
|
+
for (let i = 0; i < buf.length; i++) {
|
|
61
|
+
val = (val << 8n) | BigInt(buf[i]);
|
|
62
|
+
}
|
|
63
|
+
} else if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
64
|
+
const arr = new Uint8Array(Math.max(bytes, 8));
|
|
65
|
+
crypto.getRandomValues(arr);
|
|
66
|
+
for (let i = 0; i < arr.length; i++) {
|
|
67
|
+
val = (val << 8n) | BigInt(arr[i]);
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
return BigInt(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
32
71
|
}
|
|
33
72
|
|
|
34
73
|
cryptoCache = val;
|