id.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. user: "usr",
  9. part: "prt",
  10. } as const
  11. export function schema(prefix: keyof typeof prefixes) {
  12. return z.string().startsWith(prefixes[prefix])
  13. }
  14. const LENGTH = 26
  15. // State for monotonic ID generation
  16. let lastTimestamp = 0
  17. let counter = 0
  18. export function ascending(prefix: keyof typeof prefixes, given?: string) {
  19. return generateID(prefix, false, given)
  20. }
  21. export function descending(prefix: keyof typeof prefixes, given?: string) {
  22. return generateID(prefix, true, given)
  23. }
  24. function generateID(prefix: keyof typeof prefixes, descending: boolean, given?: string): string {
  25. if (!given) {
  26. return create(prefix, descending)
  27. }
  28. if (!given.startsWith(prefixes[prefix])) {
  29. throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
  30. }
  31. return given
  32. }
  33. function randomBase62(length: number): string {
  34. const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  35. let result = ""
  36. const bytes = randomBytes(length)
  37. for (let i = 0; i < length; i++) {
  38. result += chars[bytes[i] % 62]
  39. }
  40. return result
  41. }
  42. export function create(
  43. prefix: keyof typeof prefixes,
  44. descending: boolean,
  45. timestamp?: number,
  46. ): string {
  47. const currentTimestamp = timestamp ?? Date.now()
  48. if (currentTimestamp !== lastTimestamp) {
  49. lastTimestamp = currentTimestamp
  50. counter = 0
  51. }
  52. counter++
  53. let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
  54. now = descending ? ~now : now
  55. const timeBytes = Buffer.alloc(6)
  56. for (let i = 0; i < 6; i++) {
  57. timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
  58. }
  59. return prefixes[prefix] + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12)
  60. }
  61. }