rng-with-intention 0.2.0 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rng-with-intention",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "A random number generator that uses human intention as a seed, designed for divination and contemplative practices",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -8,20 +8,25 @@ const isNode = typeof process !== 'undefined' &&
8
8
  process.versions != null &&
9
9
  process.versions.node != null;
10
10
 
11
+ // Lazy-load Node crypto only when needed
11
12
  let nodeCrypto = null;
12
- if (isNode) {
13
- nodeCrypto = await import('crypto');
13
+ async function getNodeCrypto() {
14
+ if (!nodeCrypto && isNode) {
15
+ nodeCrypto = await import('crypto');
16
+ }
17
+ return nodeCrypto;
14
18
  }
15
19
 
16
20
  /**
17
21
  * Get random bytes - works in both Node.js and browser
18
22
  * @param {number} size - Number of bytes to generate
19
- * @returns {Uint8Array} Random bytes
23
+ * @returns {Promise<Uint8Array>} Random bytes
20
24
  */
21
- export function randomBytes(size) {
22
- if (isNode && nodeCrypto) {
25
+ export async function randomBytes(size) {
26
+ if (isNode) {
23
27
  // Node.js / Electron environment
24
- return nodeCrypto.randomBytes(size);
28
+ const crypto = await getNodeCrypto();
29
+ return crypto.randomBytes(size);
25
30
  } else {
26
31
  // Browser environment (mobile)
27
32
  const bytes = new Uint8Array(size);
@@ -36,9 +41,10 @@ export function randomBytes(size) {
36
41
  * @returns {Promise<Uint8Array>} Hash bytes
37
42
  */
38
43
  export async function sha256(data) {
39
- if (isNode && nodeCrypto) {
44
+ if (isNode) {
40
45
  // Node.js / Electron environment
41
- return nodeCrypto.createHash('sha256').update(data).digest();
46
+ const crypto = await getNodeCrypto();
47
+ return crypto.createHash('sha256').update(data).digest();
42
48
  } else {
43
49
  // Browser environment (mobile)
44
50
  const encoder = new TextEncoder();