summaryrefslogtreecommitdiffhomepage
path: root/js/src/id
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-05-18 02:50:38 -0400
committerDax Raad <[email protected]>2025-05-26 12:40:17 -0400
commitbcd2fd68b7fa00af055f558049994c2975d9515d (patch)
tree350e419b64919ea87859a65f7eb9bff5c29efca2 /js/src/id
parentd0d67029f4baad7389b5ba072379c2ff44a22dc4 (diff)
downloadopencode-bcd2fd68b7fa00af055f558049994c2975d9515d.tar.gz
opencode-bcd2fd68b7fa00af055f558049994c2975d9515d.zip
sync
Diffstat (limited to 'js/src/id')
-rw-r--r--js/src/id/id.ts38
1 files changed, 32 insertions, 6 deletions
diff --git a/js/src/id/id.ts b/js/src/id/id.ts
index 5ad2fb91d..e4744f172 100644
--- a/js/src/id/id.ts
+++ b/js/src/id/id.ts
@@ -1,5 +1,5 @@
-import { ulid } from "ulid";
import { z } from "zod";
+import { randomBytes } from "crypto";
export namespace Identifier {
const prefixes = {
@@ -7,18 +7,44 @@ export namespace Identifier {
message: "msg",
} as const;
- export function create(
+ export function schema(prefix: keyof typeof prefixes) {
+ return z.string().startsWith(prefixes[prefix]);
+ }
+
+ const LENGTH = 24;
+
+ 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) {
if (given.startsWith(prefixes[prefix])) return given;
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`);
}
- return [prefixes[prefix], ulid()].join("_");
- }
- export function schema(prefix: keyof typeof prefixes) {
- return z.string().startsWith(prefixes[prefix]);
+ let now = BigInt(Date.now());
+
+ if (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 prefix + "_" + timeBytes.toString("hex") + random.toString("hex");
}
}