summaryrefslogtreecommitdiffhomepage
path: root/js/src/id
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-05-30 20:47:56 -0400
committerDax Raad <[email protected]>2025-05-30 20:48:36 -0400
commitf3da73553c45f17e04b1e77cb13eb0fca714d1bd (patch)
treea24317a19e1ab2a89da50db669dc6894f15d00d1 /js/src/id
parent9a26b3058ffc1023e5c7e54b6d571c903d15888e (diff)
downloadopencode-f3da73553c45f17e04b1e77cb13eb0fca714d1bd.tar.gz
opencode-f3da73553c45f17e04b1e77cb13eb0fca714d1bd.zip
sync
Diffstat (limited to 'js/src/id')
-rw-r--r--js/src/id/id.ts74
1 files changed, 0 insertions, 74 deletions
diff --git a/js/src/id/id.ts b/js/src/id/id.ts
deleted file mode 100644
index 62c6a12bf..000000000
--- a/js/src/id/id.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import { z } from "zod";
-import { randomBytes } from "crypto";
-
-export namespace Identifier {
- const prefixes = {
- session: "ses",
- message: "msg",
- } as const;
-
- export function schema(prefix: keyof typeof prefixes) {
- return z.string().startsWith(prefixes[prefix]);
- }
-
- const LENGTH = 26;
-
- // State for monotonic ID generation
- let lastTimestamp = 0;
- let counter = 0;
-
- export function ascending(prefix: keyof typeof prefixes, given?: string) {
- return generateID(prefix, false, given);
- }
-
- export function descending(prefix: keyof typeof prefixes, given?: string) {
- return generateID(prefix, true, given);
- }
-
- function generateID(
- prefix: keyof typeof prefixes,
- descending: boolean,
- given?: string,
- ): string {
- if (!given) {
- return generateNewID(prefix, descending);
- }
-
- if (!given.startsWith(prefixes[prefix])) {
- throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`);
- }
- return given;
- }
-
- function generateNewID(
- prefix: keyof typeof prefixes,
- descending: boolean,
- ): string {
- const currentTimestamp = Date.now();
-
- if (currentTimestamp !== lastTimestamp) {
- lastTimestamp = currentTimestamp;
- counter = 0;
- }
- counter++;
-
- let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter);
-
- now = descending ? ~now : now;
-
- const timeBytes = Buffer.alloc(6);
- for (let i = 0; i < 6; i++) {
- 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")
- );
- }
-}