uniku 0.0.5 → 0.0.7
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/README.md +175 -57
- package/build/cuid2/cuid2.mjs +1 -132
- package/build/cuid2/cuid2.mjs.map +1 -1
- package/build/ksuid/ksuid.d.mts +30 -0
- package/build/ksuid/ksuid.d.mts.map +1 -0
- package/build/ksuid/ksuid.mjs +2 -0
- package/build/ksuid/ksuid.mjs.map +1 -0
- package/build/nanoid/nanoid.d.mts.map +1 -1
- package/build/nanoid/nanoid.mjs +1 -91
- package/build/nanoid/nanoid.mjs.map +1 -1
- package/build/random-CPlgK_62.mjs +2 -0
- package/build/{random-B5WWq7hM.mjs.map → random-CPlgK_62.mjs.map} +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/ulid/ulid.mjs +1 -191
- package/build/ulid/ulid.mjs.map +1 -1
- package/build/uuid/v4.mjs +1 -34
- package/build/uuid/v4.mjs.map +1 -1
- package/build/uuid/v7.d.mts.map +1 -1
- package/build/uuid/v7.mjs +1 -83
- package/build/uuid/v7.mjs.map +1 -1
- package/build/uuid-DcbjGX9x.mjs +2 -0
- package/build/{uuid-BEUMa9NG.mjs.map → uuid-DcbjGX9x.mjs.map} +1 -1
- package/package.json +12 -1
- package/build/random-B5WWq7hM.mjs +0 -21
- package/build/random-pool-CRCqpq0I.mjs +0 -100
- package/build/random-pool-CRCqpq0I.mjs.map +0 -1
- package/build/uuid-BEUMa9NG.mjs +0 -43
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
//#region src/common/random-pool.ts
|
|
2
|
-
/**
|
|
3
|
-
* Thread-safe random byte pool utilities using Atomics (when SharedArrayBuffer is available).
|
|
4
|
-
* Falls back to regular pooling in environments without SharedArrayBuffer.
|
|
5
|
-
*
|
|
6
|
-
* This module provides STATELESS utilities - each consumer creates and manages its own pool.
|
|
7
|
-
* Pass the pool state to each function (C-style) for explicit, predictable behavior.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* // Each module creates its own pool
|
|
12
|
-
* let pool: RandomPool | undefined
|
|
13
|
-
*
|
|
14
|
-
* function getBytes(count: number): number {
|
|
15
|
-
* if (!pool) pool = createPool(count)
|
|
16
|
-
* return getPooledBytes(pool, count)
|
|
17
|
-
* }
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
const POOL_SIZE_MULTIPLIER = 128;
|
|
21
|
-
const POOL_HEADER_SIZE = 4;
|
|
22
|
-
/**
|
|
23
|
-
* Create a new random pool. Uses SharedArrayBuffer + Atomics when available
|
|
24
|
-
* for thread-safety in Web Worker scenarios.
|
|
25
|
-
*
|
|
26
|
-
* @param minSize - Minimum bytes needed per request (pool = minSize * 128)
|
|
27
|
-
*/
|
|
28
|
-
function createPool(minSize) {
|
|
29
|
-
const totalSize = POOL_HEADER_SIZE + minSize * POOL_SIZE_MULTIPLIER;
|
|
30
|
-
let buffer;
|
|
31
|
-
let useAtomics = false;
|
|
32
|
-
if (typeof SharedArrayBuffer !== "undefined") try {
|
|
33
|
-
buffer = new SharedArrayBuffer(totalSize);
|
|
34
|
-
useAtomics = true;
|
|
35
|
-
} catch {
|
|
36
|
-
buffer = new ArrayBuffer(totalSize);
|
|
37
|
-
}
|
|
38
|
-
else buffer = new ArrayBuffer(totalSize);
|
|
39
|
-
const offset = new Int32Array(buffer, 0, 1);
|
|
40
|
-
const bytes = new Uint8Array(buffer, POOL_HEADER_SIZE);
|
|
41
|
-
crypto.getRandomValues(bytes);
|
|
42
|
-
return {
|
|
43
|
-
buffer,
|
|
44
|
-
bytes,
|
|
45
|
-
offset,
|
|
46
|
-
useAtomics
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Get pooled random bytes. Thread-safe when SharedArrayBuffer is available.
|
|
51
|
-
* Returns the starting index in the pool for the requested bytes.
|
|
52
|
-
*
|
|
53
|
-
* @param pool - The pool state (from createPool)
|
|
54
|
-
* @param count - Number of bytes needed
|
|
55
|
-
* @returns Starting offset in pool.bytes
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```ts
|
|
59
|
-
* const offset = getPooledBytes(pool, 21)
|
|
60
|
-
* for (let i = offset; i < offset + 21; i++) {
|
|
61
|
-
* // Use pool.bytes[i]
|
|
62
|
-
* }
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
function getPooledBytes(pool, count) {
|
|
66
|
-
if (pool.useAtomics) {
|
|
67
|
-
const startOffset = Atomics.add(pool.offset, 0, count);
|
|
68
|
-
if (startOffset + count <= pool.bytes.length) return startOffset;
|
|
69
|
-
if (Atomics.compareExchange(pool.offset, 0, startOffset + count, count) === startOffset + count) {
|
|
70
|
-
crypto.getRandomValues(pool.bytes);
|
|
71
|
-
return 0;
|
|
72
|
-
}
|
|
73
|
-
return getPooledBytes(pool, count);
|
|
74
|
-
} else {
|
|
75
|
-
let currentOffset = pool.offset[0];
|
|
76
|
-
if (currentOffset + count > pool.bytes.length) {
|
|
77
|
-
crypto.getRandomValues(pool.bytes);
|
|
78
|
-
currentOffset = 0;
|
|
79
|
-
}
|
|
80
|
-
pool.offset[0] = currentOffset + count;
|
|
81
|
-
return currentOffset;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Get a Uint8Array slice of random bytes from the pool.
|
|
86
|
-
*
|
|
87
|
-
* Note: Returns a subarray VIEW into the pool. If you need the bytes to persist
|
|
88
|
-
* beyond the current synchronous operation, use getRandomBytesCopy() instead.
|
|
89
|
-
*
|
|
90
|
-
* @param pool - The pool state
|
|
91
|
-
* @param count - Number of bytes needed
|
|
92
|
-
*/
|
|
93
|
-
function getRandomBytes(pool, count) {
|
|
94
|
-
const offset = getPooledBytes(pool, count);
|
|
95
|
-
return pool.bytes.subarray(offset, offset + count);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
//#endregion
|
|
99
|
-
export { getPooledBytes as n, getRandomBytes as r, createPool as t };
|
|
100
|
-
//# sourceMappingURL=random-pool-CRCqpq0I.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"random-pool-CRCqpq0I.mjs","names":["buffer: SharedArrayBuffer | ArrayBuffer"],"sources":["../src/common/random-pool.ts"],"sourcesContent":["/**\n * Thread-safe random byte pool utilities using Atomics (when SharedArrayBuffer is available).\n * Falls back to regular pooling in environments without SharedArrayBuffer.\n *\n * This module provides STATELESS utilities - each consumer creates and manages its own pool.\n * Pass the pool state to each function (C-style) for explicit, predictable behavior.\n *\n * @example\n * ```ts\n * // Each module creates its own pool\n * let pool: RandomPool | undefined\n *\n * function getBytes(count: number): number {\n * if (!pool) pool = createPool(count)\n * return getPooledBytes(pool, count)\n * }\n * ```\n */\n\nconst POOL_SIZE_MULTIPLIER = 128\nconst POOL_HEADER_SIZE = 4 // 4 bytes for Int32 offset counter\n\n/**\n * Pool state - passed to all pool functions.\n * Each module creates and owns its own pool instance.\n */\nexport type RandomPool = {\n buffer: SharedArrayBuffer | ArrayBuffer\n bytes: Uint8Array\n offset: Int32Array\n useAtomics: boolean\n}\n\n/**\n * Create a new random pool. Uses SharedArrayBuffer + Atomics when available\n * for thread-safety in Web Worker scenarios.\n *\n * @param minSize - Minimum bytes needed per request (pool = minSize * 128)\n */\nexport function createPool(minSize: number): RandomPool {\n const poolSize = minSize * POOL_SIZE_MULTIPLIER\n const totalSize = POOL_HEADER_SIZE + poolSize\n\n let buffer: SharedArrayBuffer | ArrayBuffer\n let useAtomics = false\n\n // Try SharedArrayBuffer first (thread-safe with Atomics)\n if (typeof SharedArrayBuffer !== 'undefined') {\n try {\n buffer = new SharedArrayBuffer(totalSize)\n useAtomics = true\n } catch {\n // SharedArrayBuffer may be disabled (security restrictions)\n buffer = new ArrayBuffer(totalSize)\n }\n } else {\n buffer = new ArrayBuffer(totalSize)\n }\n\n // First 4 bytes = atomic offset counter, rest = random bytes\n const offset = new Int32Array(buffer, 0, 1)\n const bytes = new Uint8Array(buffer, POOL_HEADER_SIZE)\n crypto.getRandomValues(bytes)\n\n return { buffer, bytes, offset, useAtomics }\n}\n\n/**\n * Get pooled random bytes. Thread-safe when SharedArrayBuffer is available.\n * Returns the starting index in the pool for the requested bytes.\n *\n * @param pool - The pool state (from createPool)\n * @param count - Number of bytes needed\n * @returns Starting offset in pool.bytes\n *\n * @example\n * ```ts\n * const offset = getPooledBytes(pool, 21)\n * for (let i = offset; i < offset + 21; i++) {\n * // Use pool.bytes[i]\n * }\n * ```\n */\nexport function getPooledBytes(pool: RandomPool, count: number): number {\n if (pool.useAtomics) {\n // Thread-safe path using Atomics\n const startOffset = Atomics.add(pool.offset, 0, count)\n\n if (startOffset + count <= pool.bytes.length) {\n return startOffset\n }\n\n // Pool exhausted - need to refill\n // Use compareExchange to ensure only one thread refills\n const resetResult = Atomics.compareExchange(pool.offset, 0, startOffset + count, count)\n if (resetResult === startOffset + count) {\n // We won the race - refill the pool\n crypto.getRandomValues(pool.bytes)\n return 0\n }\n\n // Another thread is refilling or already refilled - retry\n return getPooledBytes(pool, count)\n } else {\n // Single-threaded path\n let currentOffset = pool.offset[0]\n\n if (currentOffset + count > pool.bytes.length) {\n crypto.getRandomValues(pool.bytes)\n currentOffset = 0\n }\n\n pool.offset[0] = currentOffset + count\n return currentOffset\n }\n}\n\n/**\n * Get a Uint8Array slice of random bytes from the pool.\n *\n * Note: Returns a subarray VIEW into the pool. If you need the bytes to persist\n * beyond the current synchronous operation, use getRandomBytesCopy() instead.\n *\n * @param pool - The pool state\n * @param count - Number of bytes needed\n */\nexport function getRandomBytes(pool: RandomPool, count: number): Uint8Array {\n const offset = getPooledBytes(pool, count)\n return pool.bytes.subarray(offset, offset + count)\n}\n\n/**\n * Get a COPY of random bytes from the pool.\n * Use this when bytes need to persist (e.g., returned to caller who may use them later).\n *\n * @param pool - The pool state\n * @param count - Number of bytes needed\n */\nexport function getRandomBytesCopy(pool: RandomPool, count: number): Uint8Array {\n const offset = getPooledBytes(pool, count)\n return pool.bytes.slice(offset, offset + count)\n}\n\n/**\n * Fill the provided buffer with random bytes from the pool.\n *\n * @param pool - The pool state\n * @param out - Buffer to fill\n */\nexport function fillRandomBytes(pool: RandomPool, out: Uint8Array): void {\n const offset = getPooledBytes(pool, out.length)\n out.set(pool.bytes.subarray(offset, offset + out.length))\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,MAAM,uBAAuB;AAC7B,MAAM,mBAAmB;;;;;;;AAmBzB,SAAgB,WAAW,SAA6B;CAEtD,MAAM,YAAY,mBADD,UAAU;CAG3B,IAAIA;CACJ,IAAI,aAAa;AAGjB,KAAI,OAAO,sBAAsB,YAC/B,KAAI;AACF,WAAS,IAAI,kBAAkB,UAAU;AACzC,eAAa;SACP;AAEN,WAAS,IAAI,YAAY,UAAU;;KAGrC,UAAS,IAAI,YAAY,UAAU;CAIrC,MAAM,SAAS,IAAI,WAAW,QAAQ,GAAG,EAAE;CAC3C,MAAM,QAAQ,IAAI,WAAW,QAAQ,iBAAiB;AACtD,QAAO,gBAAgB,MAAM;AAE7B,QAAO;EAAE;EAAQ;EAAO;EAAQ;EAAY;;;;;;;;;;;;;;;;;;AAmB9C,SAAgB,eAAe,MAAkB,OAAuB;AACtE,KAAI,KAAK,YAAY;EAEnB,MAAM,cAAc,QAAQ,IAAI,KAAK,QAAQ,GAAG,MAAM;AAEtD,MAAI,cAAc,SAAS,KAAK,MAAM,OACpC,QAAO;AAMT,MADoB,QAAQ,gBAAgB,KAAK,QAAQ,GAAG,cAAc,OAAO,MAAM,KACnE,cAAc,OAAO;AAEvC,UAAO,gBAAgB,KAAK,MAAM;AAClC,UAAO;;AAIT,SAAO,eAAe,MAAM,MAAM;QAC7B;EAEL,IAAI,gBAAgB,KAAK,OAAO;AAEhC,MAAI,gBAAgB,QAAQ,KAAK,MAAM,QAAQ;AAC7C,UAAO,gBAAgB,KAAK,MAAM;AAClC,mBAAgB;;AAGlB,OAAK,OAAO,KAAK,gBAAgB;AACjC,SAAO;;;;;;;;;;;;AAaX,SAAgB,eAAe,MAAkB,OAA2B;CAC1E,MAAM,SAAS,eAAe,MAAM,MAAM;AAC1C,QAAO,KAAK,MAAM,SAAS,QAAQ,SAAS,MAAM"}
|
package/build/uuid-BEUMa9NG.mjs
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
//#region src/uuid/common/hex.ts
|
|
2
|
-
const HEX_TABLE = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
3
|
-
const ASCII_0 = 48;
|
|
4
|
-
const ASCII_9 = 57;
|
|
5
|
-
const ASCII_A = 65;
|
|
6
|
-
const ASCII_F = 70;
|
|
7
|
-
const ASCII_a = 97;
|
|
8
|
-
const ASCII_f = 102;
|
|
9
|
-
function hexValue(code) {
|
|
10
|
-
if (code >= ASCII_0 && code <= ASCII_9) return code - ASCII_0;
|
|
11
|
-
if (code >= ASCII_A && code <= ASCII_F) return code - ASCII_A + 10;
|
|
12
|
-
if (code >= ASCII_a && code <= ASCII_f) return code - ASCII_a + 10;
|
|
13
|
-
return -1;
|
|
14
|
-
}
|
|
15
|
-
function hexToBytes(hex, out) {
|
|
16
|
-
if (hex.length !== out.length * 2) throw new Error(`hex string must be ${out.length * 2} chars, got ${hex.length}`);
|
|
17
|
-
for (let i = 0; i < out.length; i += 1) {
|
|
18
|
-
const hi = hexValue(hex.charCodeAt(i * 2));
|
|
19
|
-
const lo = hexValue(hex.charCodeAt(i * 2 + 1));
|
|
20
|
-
if (hi === -1 || lo === -1) throw new Error("hex string contains invalid characters");
|
|
21
|
-
out[i] = hi << 4 | lo;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region src/uuid/common/uuid.ts
|
|
27
|
-
const UUID_BYTE_LENGTH = 16;
|
|
28
|
-
const UUID_STRING_LENGTH = 36;
|
|
29
|
-
function formatUuid(bytes) {
|
|
30
|
-
return HEX_TABLE[bytes[0]] + HEX_TABLE[bytes[1]] + HEX_TABLE[bytes[2]] + HEX_TABLE[bytes[3]] + "-" + HEX_TABLE[bytes[4]] + HEX_TABLE[bytes[5]] + "-" + HEX_TABLE[bytes[6]] + HEX_TABLE[bytes[7]] + "-" + HEX_TABLE[bytes[8]] + HEX_TABLE[bytes[9]] + "-" + HEX_TABLE[bytes[10]] + HEX_TABLE[bytes[11]] + HEX_TABLE[bytes[12]] + HEX_TABLE[bytes[13]] + HEX_TABLE[bytes[14]] + HEX_TABLE[bytes[15]];
|
|
31
|
-
}
|
|
32
|
-
function parseUuid(value) {
|
|
33
|
-
if (value.length !== UUID_STRING_LENGTH) throw new Error("uuid string must be 36 characters");
|
|
34
|
-
if (value[8] !== "-" || value[13] !== "-" || value[18] !== "-" || value[23] !== "-") throw new Error("uuid string has invalid separators");
|
|
35
|
-
const hex = value.slice(0, 8) + value.slice(9, 13) + value.slice(14, 18) + value.slice(19, 23) + value.slice(24);
|
|
36
|
-
const bytes = new Uint8Array(UUID_BYTE_LENGTH);
|
|
37
|
-
hexToBytes(hex, bytes);
|
|
38
|
-
return bytes;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
//#endregion
|
|
42
|
-
export { parseUuid as n, formatUuid as t };
|
|
43
|
-
//# sourceMappingURL=uuid-BEUMa9NG.mjs.map
|