id.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import z from "zod"
  2. const prefixes = {
  3. session: "ses",
  4. message: "msg",
  5. permission: "per",
  6. user: "usr",
  7. part: "prt",
  8. pty: "pty",
  9. } as const
  10. const LENGTH = 26
  11. let lastTimestamp = 0
  12. let counter = 0
  13. type Prefix = keyof typeof prefixes
  14. export namespace Identifier {
  15. export function schema(prefix: Prefix) {
  16. return z.string().startsWith(prefixes[prefix])
  17. }
  18. export function ascending(prefix: Prefix, given?: string) {
  19. return generateID(prefix, false, given)
  20. }
  21. export function descending(prefix: Prefix, given?: string) {
  22. return generateID(prefix, true, given)
  23. }
  24. }
  25. function generateID(prefix: Prefix, descending: boolean, given?: string): string {
  26. if (!given) {
  27. return create(prefix, descending)
  28. }
  29. if (!given.startsWith(prefixes[prefix])) {
  30. throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
  31. }
  32. return given
  33. }
  34. function create(prefix: Prefix, descending: boolean, timestamp?: number): string {
  35. const currentTimestamp = timestamp ?? Date.now()
  36. if (currentTimestamp !== lastTimestamp) {
  37. lastTimestamp = currentTimestamp
  38. counter = 0
  39. }
  40. counter += 1
  41. let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
  42. if (descending) {
  43. now = ~now
  44. }
  45. const timeBytes = new Uint8Array(6)
  46. for (let i = 0; i < 6; i += 1) {
  47. timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
  48. }
  49. return prefixes[prefix] + "_" + bytesToHex(timeBytes) + randomBase62(LENGTH - 12)
  50. }
  51. function bytesToHex(bytes: Uint8Array): string {
  52. let hex = ""
  53. for (let i = 0; i < bytes.length; i += 1) {
  54. hex += bytes[i].toString(16).padStart(2, "0")
  55. }
  56. return hex
  57. }
  58. function randomBase62(length: number): string {
  59. const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  60. const bytes = getRandomBytes(length)
  61. let result = ""
  62. for (let i = 0; i < length; i += 1) {
  63. result += chars[bytes[i] % 62]
  64. }
  65. return result
  66. }
  67. function getRandomBytes(length: number): Uint8Array {
  68. const bytes = new Uint8Array(length)
  69. const cryptoObj = typeof globalThis !== "undefined" ? globalThis.crypto : undefined
  70. if (cryptoObj && typeof cryptoObj.getRandomValues === "function") {
  71. cryptoObj.getRandomValues(bytes)
  72. return bytes
  73. }
  74. for (let i = 0; i < length; i += 1) {
  75. bytes[i] = Math.floor(Math.random() * 256)
  76. }
  77. return bytes
  78. }