diff options
| author | Dax Raad <[email protected]> | 2025-06-20 17:39:52 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-06-20 17:39:59 -0400 |
| commit | 460338ca5392eba2941fe328c37114a6f5ececac (patch) | |
| tree | cc1021ea2927ac083b6429782e03530c9560be4a | |
| parent | 53c18a64b48e715d65491392a013769d69ab0295 (diff) | |
| download | opencode-460338ca5392eba2941fe328c37114a6f5ececac.tar.gz opencode-460338ca5392eba2941fe328c37114a6f5ececac.zip | |
make IDs more random
| -rw-r--r-- | packages/opencode/src/id/id.ts | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/packages/opencode/src/id/id.ts b/packages/opencode/src/id/id.ts index 5544ff6f1..cf9a30421 100644 --- a/packages/opencode/src/id/id.ts +++ b/packages/opencode/src/id/id.ts @@ -41,6 +41,17 @@ export namespace Identifier { return given } + function randomBase62(length: number): string { + const chars = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + let result = "" + const bytes = randomBytes(length) + for (let i = 0; i < length; i++) { + result += chars[bytes[i] % 62] + } + return result + } + function generateNewID( prefix: keyof typeof prefixes, descending: boolean, @@ -62,14 +73,11 @@ export namespace Identifier { timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff)) } - const randLength = (LENGTH - 12) / 2 - const random = randomBytes(randLength) - return ( prefixes[prefix] + "_" + timeBytes.toString("hex") + - random.toString("hex") + randomBase62(LENGTH - 12) ) } } |
