refactor: unwrap Identifier namespace + self-reexport (#22932)

This commit is contained in:
Kit Langton
2026-04-16 17:37:51 -04:00
committed by GitHub
parent 0abc0d541a
commit 3d3e50ebf0

View File

@@ -1,8 +1,7 @@
import z from "zod" import z from "zod"
import { randomBytes } from "crypto" import { randomBytes } from "crypto"
export namespace Identifier { const prefixes = {
const prefixes = {
event: "evt", event: "evt",
session: "ses", session: "ses",
message: "msg", message: "msg",
@@ -14,27 +13,27 @@ export namespace Identifier {
tool: "tool", tool: "tool",
workspace: "wrk", workspace: "wrk",
entry: "ent", entry: "ent",
} as const } as const
export function schema(prefix: keyof typeof prefixes) { export function schema(prefix: keyof typeof prefixes) {
return z.string().startsWith(prefixes[prefix]) return z.string().startsWith(prefixes[prefix])
} }
const LENGTH = 26 const LENGTH = 26
// State for monotonic ID generation // State for monotonic ID generation
let lastTimestamp = 0 let lastTimestamp = 0
let counter = 0 let counter = 0
export function ascending(prefix: keyof typeof prefixes, given?: string) { export function ascending(prefix: keyof typeof prefixes, given?: string) {
return generateID(prefix, "ascending", given) return generateID(prefix, "ascending", given)
} }
export function descending(prefix: keyof typeof prefixes, given?: string) { export function descending(prefix: keyof typeof prefixes, given?: string) {
return generateID(prefix, "descending", given) return generateID(prefix, "descending", given)
} }
function generateID(prefix: keyof typeof prefixes, direction: "descending" | "ascending", given?: string): string { function generateID(prefix: keyof typeof prefixes, direction: "descending" | "ascending", given?: string): string {
if (!given) { if (!given) {
return create(prefixes[prefix], direction) return create(prefixes[prefix], direction)
} }
@@ -43,9 +42,9 @@ export namespace Identifier {
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`) throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
} }
return given return given
} }
function randomBase62(length: number): string { function randomBase62(length: number): string {
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
let result = "" let result = ""
const bytes = randomBytes(length) const bytes = randomBytes(length)
@@ -53,9 +52,9 @@ export namespace Identifier {
result += chars[bytes[i] % 62] result += chars[bytes[i] % 62]
} }
return result return result
} }
export function create(prefix: string, direction: "descending" | "ascending", timestamp?: number): string { export function create(prefix: string, direction: "descending" | "ascending", timestamp?: number): string {
const currentTimestamp = timestamp ?? Date.now() const currentTimestamp = timestamp ?? Date.now()
if (currentTimestamp !== lastTimestamp) { if (currentTimestamp !== lastTimestamp) {
@@ -74,13 +73,14 @@ export namespace Identifier {
} }
return prefix + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12) return prefix + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12)
} }
/** Extract timestamp from an ascending ID. Does not work with descending IDs. */ /** Extract timestamp from an ascending ID. Does not work with descending IDs. */
export function timestamp(id: string): number { export function timestamp(id: string): number {
const prefix = id.split("_")[0] const prefix = id.split("_")[0]
const hex = id.slice(prefix.length + 1, prefix.length + 13) const hex = id.slice(prefix.length + 1, prefix.length + 13)
const encoded = BigInt("0x" + hex) const encoded = BigInt("0x" + hex)
return Number(encoded / BigInt(0x1000)) return Number(encoded / BigInt(0x1000))
}
} }
export * as Identifier from "./id"