@kyneta/random 1.5.0
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/LICENSE +21 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/package.json +42 -0
- package/src/__tests__/random.test.ts +34 -0
- package/src/index.ts +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Duane Johnson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Generate a random hex string from `n` cryptographically random bytes.
|
|
4
|
+
*/
|
|
5
|
+
declare function randomHex(byteCount: number): string;
|
|
6
|
+
/**
|
|
7
|
+
* Generate a random peer ID — a 16-character hex string.
|
|
8
|
+
*
|
|
9
|
+
* Used as keys in CRDT version vectors, so must be unique
|
|
10
|
+
* but does not require cryptographic unpredictability.
|
|
11
|
+
*/
|
|
12
|
+
declare function randomPeerId(): string;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { randomHex, randomPeerId };
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;AAOA;;iBAAgB,SAAA,CAAU,SAAA;;;AAiB1B;;;;iBAAgB,YAAA,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* Generate a random hex string from `n` cryptographically random bytes.
|
|
4
|
+
*/
|
|
5
|
+
function randomHex(byteCount) {
|
|
6
|
+
if (byteCount === 0) return "";
|
|
7
|
+
const bytes = new Uint8Array(byteCount);
|
|
8
|
+
crypto.getRandomValues(bytes);
|
|
9
|
+
let hex = "";
|
|
10
|
+
for (let i = 0; i < bytes.length; i++) hex += (bytes[i] ?? 0).toString(16).padStart(2, "0");
|
|
11
|
+
return hex;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Generate a random peer ID — a 16-character hex string.
|
|
15
|
+
*
|
|
16
|
+
* Used as keys in CRDT version vectors, so must be unique
|
|
17
|
+
* but does not require cryptographic unpredictability.
|
|
18
|
+
*/
|
|
19
|
+
function randomPeerId() {
|
|
20
|
+
return randomHex(8);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { randomHex, randomPeerId };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["// Avoids crypto.randomUUID() which is restricted to secure contexts\n// (HTTPS or localhost) and throws on plain HTTP over LAN addresses.\n// crypto.getRandomValues() has no such restriction.\n\n/**\n * Generate a random hex string from `n` cryptographically random bytes.\n */\nexport function randomHex(byteCount: number): string {\n if (byteCount === 0) return \"\"\n const bytes = new Uint8Array(byteCount)\n crypto.getRandomValues(bytes)\n let hex = \"\"\n for (let i = 0; i < bytes.length; i++) {\n hex += (bytes[i] ?? 0).toString(16).padStart(2, \"0\")\n }\n return hex\n}\n\n/**\n * Generate a random peer ID — a 16-character hex string.\n *\n * Used as keys in CRDT version vectors, so must be unique\n * but does not require cryptographic unpredictability.\n */\nexport function randomPeerId(): string {\n return randomHex(8)\n}\n"],"mappings":";;;;AAOA,SAAgB,UAAU,WAA2B;AACnD,KAAI,cAAc,EAAG,QAAO;CAC5B,MAAM,QAAQ,IAAI,WAAW,UAAU;AACvC,QAAO,gBAAgB,MAAM;CAC7B,IAAI,MAAM;AACV,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,IAChC,SAAQ,MAAM,MAAM,GAAG,SAAS,GAAG,CAAC,SAAS,GAAG,IAAI;AAEtD,QAAO;;;;;;;;AAST,SAAgB,eAAuB;AACrC,QAAO,UAAU,EAAE"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kyneta/random",
|
|
3
|
+
"version": "1.5.0",
|
|
4
|
+
"description": "Secure-context-free random ID primitives for the kyneta monorepo",
|
|
5
|
+
"author": "Duane Johnson",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/halecraft/kyneta",
|
|
10
|
+
"directory": "packages/random"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"module": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./src": "./src/index.ts",
|
|
30
|
+
"./src/*": "./src/*"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsdown": "^0.21.9",
|
|
34
|
+
"typescript": "^5.9.2",
|
|
35
|
+
"vitest": "^4.0.17"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsdown",
|
|
39
|
+
"test": "verify logic",
|
|
40
|
+
"verify": "verify"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { randomHex, randomPeerId } from "../index.js"
|
|
3
|
+
|
|
4
|
+
describe("randomHex", () => {
|
|
5
|
+
it("returns a string of length 2n", () => {
|
|
6
|
+
for (const n of [1, 4, 8, 16, 32]) {
|
|
7
|
+
expect(randomHex(n).length).toBe(n * 2)
|
|
8
|
+
}
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it("contains only lowercase hex characters", () => {
|
|
12
|
+
for (let i = 0; i < 20; i++) {
|
|
13
|
+
expect(randomHex(16)).toMatch(/^[0-9a-f]+$/)
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it("returns empty string for zero bytes", () => {
|
|
18
|
+
expect(randomHex(0)).toBe("")
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it("produces distinct values across 100 calls", () => {
|
|
22
|
+
const ids = new Set<string>()
|
|
23
|
+
for (let i = 0; i < 100; i++) {
|
|
24
|
+
ids.add(randomHex(8))
|
|
25
|
+
}
|
|
26
|
+
expect(ids.size).toBe(100)
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
describe("randomPeerId", () => {
|
|
31
|
+
it("returns a 16-char hex string", () => {
|
|
32
|
+
expect(randomPeerId()).toMatch(/^[0-9a-f]{16}$/)
|
|
33
|
+
})
|
|
34
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Avoids crypto.randomUUID() which is restricted to secure contexts
|
|
2
|
+
// (HTTPS or localhost) and throws on plain HTTP over LAN addresses.
|
|
3
|
+
// crypto.getRandomValues() has no such restriction.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate a random hex string from `n` cryptographically random bytes.
|
|
7
|
+
*/
|
|
8
|
+
export function randomHex(byteCount: number): string {
|
|
9
|
+
if (byteCount === 0) return ""
|
|
10
|
+
const bytes = new Uint8Array(byteCount)
|
|
11
|
+
crypto.getRandomValues(bytes)
|
|
12
|
+
let hex = ""
|
|
13
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
14
|
+
hex += (bytes[i] ?? 0).toString(16).padStart(2, "0")
|
|
15
|
+
}
|
|
16
|
+
return hex
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Generate a random peer ID — a 16-character hex string.
|
|
21
|
+
*
|
|
22
|
+
* Used as keys in CRDT version vectors, so must be unique
|
|
23
|
+
* but does not require cryptographic unpredictability.
|
|
24
|
+
*/
|
|
25
|
+
export function randomPeerId(): string {
|
|
26
|
+
return randomHex(8)
|
|
27
|
+
}
|