SPB Git

Repository Public

ids.ts 3.8 KB 64 lines typescript

1// Identifiants opaques de /api/v1 : ils encodent la boîte + l'identifiant JMAP, et sont revérifiés contre les droits de l'appelant à chaque usage.2import crypto from "node:crypto"3import { bad } from "../util.js"45const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/6const hexToUuid = (h: string) => `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`78function enc(prefix: string, mailboxId: string, jmapId: string): string {9  return `${prefix}_${Buffer.concat([Buffer.from(mailboxId.replace(/-/g, ""), "hex"), Buffer.from(jmapId, "utf8")]).toString("base64url")}`10}11function dec(prefix: string, id: string): { mailboxId: string; jmapId: string } | null {12  if (typeof id !== "string" || !id.startsWith(`${prefix}_`) || id.length > 400) return null13  const raw = id.slice(prefix.length + 1)14  if (!/^[A-Za-z0-9_-]+$/.test(raw)) return null15  const buf = Buffer.from(raw, "base64url")16  if (buf.length < 17) return null17  const mailboxId = hexToUuid(buf.subarray(0, 16).toString("hex"))18  const jmapId = buf.subarray(16).toString("utf8")19  if (!UUID_RE.test(mailboxId) || !/^[A-Za-z0-9_-]{1,255}$/.test(jmapId)) return null20  return { mailboxId, jmapId }21}22export const msgId = (mailboxId: string, emailId: string) => enc("msg", mailboxId, emailId)23export const threadId = (mailboxId: string, id: string) => enc("thr", mailboxId, id)24export const folderId = (mailboxId: string, id: string) => enc("fld", mailboxId, id)25export const attachmentId = (blobId: string) => `att_${Buffer.from(blobId, "utf8").toString("base64url")}`26export function parseMsgId(id: string) { const r = dec("msg", id); if (!r) throw bad("not_found", 404, "Unknown message"); return r }27export function parseThreadId(id: string) { const r = dec("thr", id); if (!r) throw bad("not_found", 404, "Unknown thread"); return r }28export function parseFolderId(id: string) { return dec("fld", id) }29export function parseAttachmentId(id: string): string {30  if (!/^att_[A-Za-z0-9_-]{1,600}$/.test(id)) throw bad("not_found", 404, "Unknown attachment")31  return Buffer.from(id.slice(4), "base64url").toString("utf8")32}33export const newOutboundId = () => `msg_out_${crypto.randomBytes(15).toString("base64url")}`34export const newFileId = () => `file_${crypto.randomBytes(18).toString("base64url")}`3536// ---- curseurs de pagination : opaques, stables, liés au filtre qui les a produits37export type ListCursor = { t: string; k: string; h: string }38export function encodeCursor(c: object): string { return Buffer.from(JSON.stringify({ v: 1, ...c }), "utf8").toString("base64url") }39export function decodeCursor<T>(raw: string | undefined, check: (c: any) => boolean): T | null {40  if (!raw) return null41  try {42    if (raw.length > 2000) throw new Error()43    const c = JSON.parse(Buffer.from(raw, "base64url").toString("utf8"))44    if (!c || c.v !== 1 || !check(c)) throw new Error()45    return c as T46  } catch {47    throw bad("invalid_cursor", 400, "The cursor is invalid or does not belong to this query")48  }49}50export const filterHash = (o: unknown) => crypto.createHash("sha256").update(JSON.stringify(o)).digest("base64url").slice(0, 12)5152// ---- jeton d'état pour la synchronisation incrémentale53export const stateToken = (mailboxId: string, state: string) => `st_${Buffer.from(JSON.stringify({ m: mailboxId, s: state }), "utf8").toString("base64url")}`54export function parseStateToken(tok: string): { m: string; s: string } {55  try {56    if (!tok.startsWith("st_") || tok.length > 600) throw new Error()57    const c = JSON.parse(Buffer.from(tok.slice(3), "base64url").toString("utf8"))58    if (typeof c.m !== "string" || typeof c.s !== "string" || !UUID_RE.test(c.m)) throw new Error()59    return c60  } catch {61    throw bad("resync_required", 409, "Unknown state token: perform a full resynchronisation")62  }63}64
Files main
Menu

Theme