summaryrefslogtreecommitdiffhomepage
path: root/packages/util/src
diff options
context:
space:
mode:
authorAdam <[email protected]>2025-12-18 15:47:16 -0600
committerAdam <[email protected]>2025-12-18 15:47:21 -0600
commitd57b963141a99c1e49271de2bdde2b08b6546f57 (patch)
tree1ec82c7cf8929b9613c804b48d67ac2f20bd562b /packages/util/src
parent0ebcaff92717ba0cb3ca122064cabc7622a2dd68 (diff)
downloadopencode-d57b963141a99c1e49271de2bdde2b08b6546f57.tar.gz
opencode-d57b963141a99c1e49271de2bdde2b08b6546f57.zip
fix: id
Diffstat (limited to 'packages/util/src')
-rw-r--r--packages/util/src/identifier.ts95
1 files changed, 22 insertions, 73 deletions
diff --git a/packages/util/src/identifier.ts b/packages/util/src/identifier.ts
index 272507f0a..ba28a351b 100644
--- a/packages/util/src/identifier.ts
+++ b/packages/util/src/identifier.ts
@@ -1,99 +1,48 @@
-import z from "zod"
+import { randomBytes } from "crypto"
export namespace Identifier {
- const prefixes = {
- session: "ses",
- message: "msg",
- permission: "per",
- user: "usr",
- part: "prt",
- pty: "pty",
- } as const
-
- export type Prefix = keyof typeof prefixes
- type CryptoLike = {
- getRandomValues<T extends ArrayBufferView>(array: T): T
- }
-
- const TOTAL_LENGTH = 26
- const RANDOM_LENGTH = TOTAL_LENGTH - 12
- const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ const LENGTH = 26
+ // State for monotonic ID generation
let lastTimestamp = 0
let counter = 0
- const fillRandomBytes = (buffer: Uint8Array) => {
- const cryptoLike = (globalThis as { crypto?: CryptoLike }).crypto
- if (cryptoLike?.getRandomValues) {
- cryptoLike.getRandomValues(buffer)
- return buffer
- }
- for (let i = 0; i < buffer.length; i++) {
- buffer[i] = Math.floor(Math.random() * 256)
- }
- return buffer
+ export function ascending() {
+ return create(false)
}
- const randomBase62 = (length: number) => {
- const bytes = fillRandomBytes(new Uint8Array(length))
+ export function descending() {
+ return create(true)
+ }
+
+ function randomBase62(length: number): string {
+ const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
let result = ""
+ const bytes = randomBytes(length)
for (let i = 0; i < length; i++) {
- result += BASE62[bytes[i] % BASE62.length]
+ result += chars[bytes[i] % 62]
}
return result
}
- const createSuffix = (descending: boolean, timestamp?: number) => {
+ export function create(descending: boolean, timestamp?: number): string {
const currentTimestamp = timestamp ?? Date.now()
+
if (currentTimestamp !== lastTimestamp) {
lastTimestamp = currentTimestamp
counter = 0
}
- counter += 1
+ counter++
- let value = BigInt(currentTimestamp) * 0x1000n + BigInt(counter)
- if (descending) value = ~value
+ let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
- const timeBytes = new Uint8Array(6)
- for (let i = 0; i < 6; i++) {
- timeBytes[i] = Number((value >> BigInt(40 - 8 * i)) & 0xffn)
- }
- const hex = Array.from(timeBytes)
- .map((byte) => byte.toString(16).padStart(2, "0"))
- .join("")
- return hex + randomBase62(RANDOM_LENGTH)
- }
+ now = descending ? ~now : now
- const generateID = (prefix: Prefix, descending: boolean, given?: string, timestamp?: number) => {
- if (given) {
- const expected = `${prefixes[prefix]}_`
- if (!given.startsWith(expected)) throw new Error(`ID ${given} does not start with ${expected}`)
- return given
+ const timeBytes = Buffer.alloc(6)
+ for (let i = 0; i < 6; i++) {
+ timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
}
- return `${prefixes[prefix]}_${createSuffix(descending, timestamp)}`
- }
-
- export const schema = (prefix: Prefix) => z.string().startsWith(`${prefixes[prefix]}_`)
-
- export function ascending(): string
- export function ascending(prefix: Prefix, given?: string): string
- export function ascending(prefix?: Prefix, given?: string) {
- if (prefix) return generateID(prefix, false, given)
- return create(false)
- }
-
- export function descending(): string
- export function descending(prefix: Prefix, given?: string): string
- export function descending(prefix?: Prefix, given?: string) {
- if (prefix) return generateID(prefix, true, given)
- return create(true)
- }
-
- export function create(descending: boolean, timestamp?: number) {
- return createSuffix(descending, timestamp)
- }
- export function createPrefixed(prefix: Prefix, descending: boolean, timestamp?: number) {
- return generateID(prefix, descending, undefined, timestamp)
+ return timeBytes.toString("hex") + randomBase62(LENGTH - 12)
}
}