| 197 | } |
| 198 | |
| 199 | export function generateUuidV7(date = new Date()): string { |
| 200 | const bytes = randomBytes(16); |
| 201 | let ms = BigInt(date.getTime()); |
| 202 | for (let i = 5; i >= 0; i--) { |
| 203 | bytes[i] = Number(ms & 0xffn); |
| 204 | ms >>= 8n; |
| 205 | } |
| 206 | bytes[6] = (bytes[6] & 0x0f) | 0x70; |
| 207 | bytes[8] = (bytes[8] & 0x3f) | 0x80; |
| 208 | const hex = bytes.toString("hex"); |
| 209 | return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; |
| 210 | } |
| 211 | |
| 212 | function shortId(): string { |
| 213 | return randomBytes(4).toString("hex"); |