From e261f11f3917ba38ca11cc624840cdc983cc1161 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:56:10 +0800 Subject: [PATCH] Import packages/plugin/src/ids.ts --- packages/plugin/src/ids.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 packages/plugin/src/ids.ts diff --git a/packages/plugin/src/ids.ts b/packages/plugin/src/ids.ts new file mode 100644 index 0000000..f9d79da --- /dev/null +++ b/packages/plugin/src/ids.ts @@ -0,0 +1,17 @@ +import { randomBytes } from 'node:crypto' + +export function uuidV7(now = Date.now()): string { + if (!Number.isSafeInteger(now) || now < 0 || now > 0xffffffffffff) { + throw new RangeError('UUIDv7 timestamp must be a non-negative 48-bit integer') + } + const bytes = randomBytes(16) + let timestamp = BigInt(now) + for (let index = 5; index >= 0; index -= 1) { + bytes[index] = Number(timestamp & 0xffn) + timestamp >>= 8n + } + bytes[6] = 0x70 | (bytes[6]! & 0x0f) + bytes[8] = 0x80 | (bytes[8]! & 0x3f) + const hex = bytes.toString('hex') + return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}` +}