summaryrefslogtreecommitdiffhomepage
path: root/packages
diff options
context:
space:
mode:
authorKit Langton <[email protected]>2026-04-16 19:48:24 -0400
committerGitHub <[email protected]>2026-04-16 23:48:24 +0000
commite2d161dfdd54fdd30f8e36e8cf4f46e261dab96e (patch)
treebfbaf91445e2c3f4729d6accf332dfbad20b1a8f /packages
parent23d48a7cf1af47870ef39def684eb8d569c66f4b (diff)
downloadopencode-e2d161dfdd54fdd30f8e36e8cf4f46e261dab96e.tar.gz
opencode-e2d161dfdd54fdd30f8e36e8cf4f46e261dab96e.zip
refactor: unwrap Identifier namespace + self-reexport (#22963)
Diffstat (limited to 'packages')
-rw-r--r--packages/opencode/src/id/id.ts132
1 files changed, 66 insertions, 66 deletions
diff --git a/packages/opencode/src/id/id.ts b/packages/opencode/src/id/id.ts
index 3d4cddf53..46c210fa5 100644
--- a/packages/opencode/src/id/id.ts
+++ b/packages/opencode/src/id/id.ts
@@ -1,86 +1,86 @@
import z from "zod"
import { randomBytes } from "crypto"
-export namespace Identifier {
- const prefixes = {
- event: "evt",
- session: "ses",
- message: "msg",
- permission: "per",
- question: "que",
- user: "usr",
- part: "prt",
- pty: "pty",
- tool: "tool",
- workspace: "wrk",
- entry: "ent",
- } as const
-
- export function schema(prefix: keyof typeof prefixes) {
- return z.string().startsWith(prefixes[prefix])
- }
-
- const LENGTH = 26
+const prefixes = {
+ event: "evt",
+ session: "ses",
+ message: "msg",
+ permission: "per",
+ question: "que",
+ user: "usr",
+ part: "prt",
+ pty: "pty",
+ tool: "tool",
+ workspace: "wrk",
+ entry: "ent",
+} as const
+
+export function schema(prefix: keyof typeof prefixes) {
+ return z.string().startsWith(prefixes[prefix])
+}
- // State for monotonic ID generation
- let lastTimestamp = 0
- let counter = 0
+const LENGTH = 26
- export function ascending(prefix: keyof typeof prefixes, given?: string) {
- return generateID(prefix, "ascending", given)
- }
+// State for monotonic ID generation
+let lastTimestamp = 0
+let counter = 0
- export function descending(prefix: keyof typeof prefixes, given?: string) {
- return generateID(prefix, "descending", given)
- }
+export function ascending(prefix: keyof typeof prefixes, given?: string) {
+ return generateID(prefix, "ascending", given)
+}
- function generateID(prefix: keyof typeof prefixes, direction: "descending" | "ascending", given?: string): string {
- if (!given) {
- return create(prefixes[prefix], direction)
- }
+export function descending(prefix: keyof typeof prefixes, given?: string) {
+ return generateID(prefix, "descending", given)
+}
- if (!given.startsWith(prefixes[prefix])) {
- throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
- }
- return given
+function generateID(prefix: keyof typeof prefixes, direction: "descending" | "ascending", given?: string): string {
+ if (!given) {
+ return create(prefixes[prefix], direction)
}
- 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
+ if (!given.startsWith(prefixes[prefix])) {
+ throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
}
+ return given
+}
- export function create(prefix: string, direction: "descending" | "ascending", timestamp?: number): string {
- const currentTimestamp = timestamp ?? Date.now()
+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
+}
- if (currentTimestamp !== lastTimestamp) {
- lastTimestamp = currentTimestamp
- counter = 0
- }
- counter++
+export function create(prefix: string, direction: "descending" | "ascending", timestamp?: number): string {
+ const currentTimestamp = timestamp ?? Date.now()
- let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
+ if (currentTimestamp !== lastTimestamp) {
+ lastTimestamp = currentTimestamp
+ counter = 0
+ }
+ counter++
- now = direction === "descending" ? ~now : now
+ let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
- const timeBytes = Buffer.alloc(6)
- for (let i = 0; i < 6; i++) {
- timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
- }
+ now = direction === "descending" ? ~now : now
- return prefix + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12)
+ const timeBytes = Buffer.alloc(6)
+ for (let i = 0; i < 6; i++) {
+ timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
}
- /** Extract timestamp from an ascending ID. Does not work with descending IDs. */
- export function timestamp(id: string): number {
- const prefix = id.split("_")[0]
- const hex = id.slice(prefix.length + 1, prefix.length + 13)
- const encoded = BigInt("0x" + hex)
- return Number(encoded / BigInt(0x1000))
- }
+ return prefix + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12)
}
+
+/** Extract timestamp from an ascending ID. Does not work with descending IDs. */
+export function timestamp(id: string): number {
+ const prefix = id.split("_")[0]
+ const hex = id.slice(prefix.length + 1, prefix.length + 13)
+ const encoded = BigInt("0x" + hex)
+ return Number(encoded / BigInt(0x1000))
+}
+
+export * as Identifier from "./id"