id.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import z from "zod"
  2. import { randomBytes } from "crypto"
  3. export namespace Identifier {
  4. const prefixes = {
  5. session: "ses",
  6. message: "msg",
  7. permission: "per",
  8. question: "que",
  9. user: "usr",
  10. part: "prt",
  11. pty: "pty",
  12. tool: "tool",
  13. } as const
  14. export function schema(prefix: keyof typeof prefixes) {
  15. return z.string().startsWith(prefixes[prefix])
  16. }
  17. const LENGTH = 26
  18. // State for monotonic ID generation
  19. let lastTimestamp = 0
  20. let counter = 0
  21. export function ascending(prefix: keyof typeof prefixes, given?: string) {
  22. return generateID(prefix, false, given)
  23. }
  24. export function descending(prefix: keyof typeof prefixes, given?: string) {
  25. return generateID(prefix, true, given)
  26. }
  27. function generateID(prefix: keyof typeof prefixes, descending: boolean, given?: string): string {
  28. if (!given) {
  29. return create(prefix, descending)
  30. }
  31. if (!given.startsWith(prefixes[prefix])) {
  32. throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
  33. }
  34. return given
  35. }
  36. function randomBase62(length: number): string {
  37. const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  38. let result = ""
  39. const bytes = randomBytes(length)
  40. for (let i = 0; i < length; i++) {
  41. result += chars[bytes[i] % 62]
  42. }
  43. return result
  44. }
  45. export function create(prefix: keyof typeof prefixes, descending: boolean, timestamp?: number): string {
  46. const currentTimestamp = timestamp ?? Date.now()
  47. if (currentTimestamp !== lastTimestamp) {
  48. lastTimestamp = currentTimestamp
  49. counter = 0
  50. }
  51. counter++
  52. let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
  53. now = descending ? ~now : now
  54. const timeBytes = Buffer.alloc(6)
  55. for (let i = 0; i < 6; i++) {
  56. timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
  57. }
  58. return prefixes[prefix] + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12)
  59. }
  60. /** Extract timestamp from an ascending ID. Does not work with descending IDs. */
  61. export function timestamp(id: string): number {
  62. const prefix = id.split("_")[0]
  63. const hex = id.slice(prefix.length + 1, prefix.length + 13)
  64. const encoded = BigInt("0x" + hex)
  65. return Number(encoded / BigInt(0x1000))
  66. }
  67. }