@vielzeug/archive 26.10.13 → 26.10.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/_confirmation.cjs +5 -1
- package/dist/auth/_confirmation.cjs.map +1 -1
- package/dist/auth/_confirmation.d.ts.map +1 -1
- package/dist/auth/_confirmation.js +5 -1
- package/dist/auth/_confirmation.js.map +1 -1
- package/dist/auth/worker.cjs +1 -1
- package/dist/auth/worker.cjs.map +1 -1
- package/dist/auth/worker.d.ts +2 -2
- package/dist/auth/worker.js +1 -1
- package/dist/auth/worker.js.map +1 -1
- package/dist/contract.cjs +1 -1
- package/dist/contract.cjs.map +1 -1
- package/dist/contract.js +1 -1
- package/dist/contract.js.map +1 -1
- package/dist/worker/auth.cjs +1 -1
- package/dist/worker/auth.cjs.map +1 -1
- package/dist/worker/auth.js +1 -1
- package/dist/worker/auth.js.map +1 -1
- package/dist/worker/http.cjs +1 -1
- package/dist/worker/http.cjs.map +1 -1
- package/dist/worker/http.d.ts +3 -1
- package/dist/worker/http.d.ts.map +1 -1
- package/dist/worker/http.js +1 -1
- package/dist/worker/http.js.map +1 -1
- package/dist/worker/publications.cjs +1 -1
- package/dist/worker/publications.cjs.map +1 -1
- package/dist/worker/publications.d.ts.map +1 -1
- package/dist/worker/publications.js +1 -1
- package/dist/worker/publications.js.map +1 -1
- package/dist/worker/sql.cjs +3 -3
- package/dist/worker/sql.cjs.map +1 -1
- package/dist/worker/sql.d.ts +4 -2
- package/dist/worker/sql.d.ts.map +1 -1
- package/dist/worker/sql.js +3 -3
- package/dist/worker/sql.js.map +1 -1
- package/package.json +2 -2
package/dist/worker/http.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.cjs","names":[],"sources":["../../src/worker/http.ts"],"sourcesContent":["import {\n ARCHIVE_LIMITS,\n type ArchiveErrorCode,\n PUBLISHER_HEADER,\n REQUEST_MARKER_HEADER,\n SESSION_COOKIE_NAME,\n sessionCookieValue,\n} from '../contract';\nimport type { ApiConfig } from './env';\n\n/**\n * HTTP plumbing shared by every route: the one error shape, bounded body reads, strict\n * query/header guards and the CORS contract. Handlers throw ApiError; the entry point\n * sanitizes — no stack, SQL, body, or PII ever reaches a response. Internal to the\n * Worker: clients parse the wire envelope (`archiveErrorResponseSchema`), never this class.\n */\nexport class ApiError extends Error {\n readonly code: ArchiveErrorCode;\n readonly retryAfterSeconds?: number;\n readonly status: number;\n readonly diagnostic?: ArchiveDiagnostic;\n constructor(\n status: number,\n code: ArchiveErrorCode,\n message: string,\n opts?: ErrorOptions & { retryAfterSeconds?: number; diagnostic?: ArchiveDiagnostic },\n ) {\n super(message, opts);\n this.name = 'ApiError';\n this.code = code;\n this.retryAfterSeconds = opts?.retryAfterSeconds;\n this.status = status;\n this.diagnostic = opts?.diagnostic;\n }\n}\n\nexport type ArchiveDiagnostic =\n | { readonly kind: 'configuration'; readonly field: string }\n | { readonly kind: 'database' | 'record' | 'schema' | 'internal' | 'cache' | 'collection' };\n\nexport const badRequest = (message = 'The request is not valid.'): ApiError =>\n new ApiError(400, 'bad-request', message);\nexport const unauthenticated = (): ApiError => new ApiError(401, 'unauthenticated', 'Sign-in is required.');\nexport const forbidden = (message = 'That action is not allowed.'): ApiError => new ApiError(403, 'forbidden', message);\nexport const notFound = (): ApiError => new ApiError(404, 'not-found', 'No such entry.');\nexport const conflict = (message = 'The entry changed since you saw it.'): ApiError =>\n new ApiError(409, 'conflict', message);\nexport const invalid = (message = 'The payload is not valid.'): ApiError =>\n new ApiError(422, 'invalid-payload', message);\n\nexport const json = (status: number, body: unknown, headers: HeadersInit = {}): Response =>\n new Response(JSON.stringify(body), {\n headers: { 'content-type': 'application/json; charset=utf-8', ...headers },\n status,\n });\n\nexport const noStore = { 'cache-control': 'no-store' } as const;\n\nexport const errorBody = (status: number, code: string, message: string, requestId: string): Response =>\n json(status, { error: { code, message, requestId } }, noStore);\n\n/** Sanitizes any thrown value into the public error shape; unknowns become 500. */\nexport function toErrorResponse(error: unknown, requestId: string): Response {\n if (error instanceof ApiError) {\n const headers: HeadersInit =\n error.retryAfterSeconds === undefined ? noStore : { ...noStore, 'retry-after': String(error.retryAfterSeconds) };\n return json(error.status, { error: { code: error.code, message: error.message, requestId } }, headers);\n }\n return errorBody(500, 'internal', 'Something went wrong.', requestId);\n}\n\n/**\n * Rejects query strings with unknown or repeated parameters. Every accepted route\n * names its full allow-list; anything else is a client bug, not something to ignore.\n */\nexport function queryParams(url: URL, allowed: readonly string[]): URLSearchParams {\n const params = url.searchParams;\n const seen = new Set<string>();\n for (const key of params.keys()) {\n if (!allowed.includes(key) || seen.has(key)) throw badRequest();\n seen.add(key);\n }\n return params;\n}\n\n/** Exact-origin plus request-marker guard for every mutation: no wildcards, no nulls. */\nexport function requireMutationGuard(request: Request, config: ApiConfig): void {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) throw forbidden();\n if (request.headers.get(REQUEST_MARKER_HEADER) !== '1') throw forbidden();\n}\n\n/** CORS response headers for one already-allowed origin; absent for foreign origins. */\nexport function corsHeaders(request: Request, config: ApiConfig): Record<string, string> {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) return {};\n return {\n 'access-control-allow-credentials': 'true',\n 'access-control-allow-origin': origin,\n vary: 'Origin',\n };\n}\n\n/** Answers a preflight for an allowed origin; foreign or missing origins get 403. */\nexport function preflight(request: Request, config: ApiConfig): Response {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) {\n return errorBody(403, 'forbidden', 'That action is not allowed.', crypto.randomUUID());\n }\n return new Response(null, {\n headers: {\n 'access-control-allow-credentials': 'true',\n // Header names are case-insensitive; the values derive from the contract constants.\n 'access-control-allow-headers': `Content-Type, ${PUBLISHER_HEADER}, ${REQUEST_MARKER_HEADER}`,\n 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',\n 'access-control-allow-origin': origin,\n 'access-control-max-age': '600',\n vary: 'Origin',\n },\n status: 204,\n });\n}\n\n/** Hard deadline for reading a request body; slow streams fail as 408. */\nexport const HTTP_BODY_TIMEOUT_MS = 10000;\n\nconst bodyTimeout = (): ApiError => new ApiError(408, 'timeout', 'The request body took too long.');\n\n/**\n * Reads a JSON body with a hard byte ceiling and a wall-clock deadline enforced while\n * streaming — a lying or absent Content-Length and a stalled sender are both cut off.\n * Wrong media types are 415, oversized 413, timed out 408, malformed JSON 400; schema\n * validation happens afterwards at the route boundary.\n */\nexport async function readJsonBody(request: Request): Promise<unknown> {\n // Exactly the application/json media type; parameters (charset etc.) may follow.\n const mediaType = (request.headers.get('content-type') ?? '').split(';', 1)[0].trim().toLowerCase();\n if (mediaType !== 'application/json') {\n throw new ApiError(415, 'unsupported-media', 'JSON bodies only.');\n }\n const reader = request.body?.getReader();\n if (!reader) throw badRequest();\n const startedAt = Date.now();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n const chunks: Uint8Array[] = [];\n let total = 0;\n try {\n for (;;) {\n const { done, value } = await Promise.race([\n reader.read(),\n new Promise<never>((_r, reject) => {\n timeoutId = setTimeout(\n () => reject(bodyTimeout()),\n Math.max(0, HTTP_BODY_TIMEOUT_MS - (Date.now() - startedAt)),\n );\n }),\n ]);\n clearTimeout(timeoutId);\n timeoutId = undefined;\n if (done) break;\n total += value.byteLength;\n if (total > ARCHIVE_LIMITS.requestBytes) throw new ApiError(413, 'too-large', 'The request is too large.');\n chunks.push(value);\n }\n } catch (error) {\n // Release the remainder so the failure isn't blocked on a stalled sender.\n reader.cancel().catch(() => undefined);\n throw error;\n } finally {\n if (timeoutId !== undefined) clearTimeout(timeoutId);\n reader.releaseLock();\n }\n const merged = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n merged.set(chunk, offset);\n offset += chunk.byteLength;\n }\n if (total === 0) throw badRequest();\n try {\n return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(merged));\n } catch {\n throw badRequest();\n }\n}\n\nconst SESSION_COOKIE_MAX = 4096;\n\n/**\n * Reads the session cookie. A duplicate cookie name or an oversized header is a\n * malformed request, not a missing session: callers distinguish null (anonymous)\n * from a token to verify.\n */\nexport function sessionToken(request: Request): string | null {\n const header = request.headers.get('cookie');\n if (header === null) return null;\n if (header.length > SESSION_COOKIE_MAX) throw badRequest();\n const matches: string[] = [];\n for (const part of header.split(';')) {\n const eq = part.indexOf('=');\n if (eq === -1) continue;\n if (part.slice(0, eq).trim() === SESSION_COOKIE_NAME) matches.push(part.slice(eq + 1).trim());\n }\n if (matches.length > 1) throw badRequest();\n return matches.length === 1 && matches[0].length > 0 ? matches[0] : null;\n}\n\n/** The expired-session Set-Cookie value the sign-out route and account erasure send. */\nexport const expiredSessionCookie = (secure: boolean): string => sessionCookieValue('', { maxAgeSeconds: 0, secure });\n"],"mappings":"mCAgBA,IAAa,SAAb,cAA8B,KAAM,CAClC,KACA,kBACA,OACA,WACA,YACE,EACA,EACA,EACA,EACA,CACA,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WACZ,KAAK,KAAO,EACZ,KAAK,kBAAoB,GAAM,kBAC/B,KAAK,OAAS,EACd,KAAK,WAAa,GAAM,UAC1B,CACF,EAMa,YAAc,EAAU,8BACnC,IAAI,SAAS,IAAK,cAAe,CAAO,EAC7B,oBAAkC,IAAI,SAAS,IAAK,kBAAmB,sBAAsB,EAC7F,WAAa,EAAU,gCAA4C,IAAI,SAAS,IAAK,YAAa,CAAO,EACzG,aAA2B,IAAI,SAAS,IAAK,YAAa,gBAAgB,EAC1E,UAAY,EAAU,wCACjC,IAAI,SAAS,IAAK,WAAY,CAAO,EAC1B,SAAW,EAAU,8BAChC,IAAI,SAAS,IAAK,kBAAmB,CAAO,EAEjC,MAAQ,EAAgB,EAAe,EAAuB,CAAC,IAC1E,IAAI,SAAS,KAAK,UAAU,CAAI,EAAG,CACjC,QAAS,CAAE,eAAgB,kCAAmC,GAAG,CAAQ,EACzE,QACF,CAAC,EAEU,EAAU,CAA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rB,CACA,IAAM,EAAS,IAAI,WAAW,CAAK,EAC/B,EAAS,EACb,IAAK,IAAM,KAAS,EAClB,EAAO,IAAI,EAAO,CAAM,EACxB,GAAU,EAAM,WAElB,GAAI,IAAU,EAAG,MAAM,WAAW,EAClC,GAAI,CACF,OAAO,KAAK,MAAM,IAAI,YAAY,QAAS,CAAE,MAAO,EAAK,CAAC,CAAC,CAAC,OAAO,CAAM,CAAC,CAC5E,MAAQ,CACN,MAAM,WAAW,CACnB,CACF,CAEA,IAAM,EAAqB,KAO3B,SAAgB,aAAa,EAAiC,CAC5D,IAAM,EAAS,EAAQ,QAAQ,IAAI,QAAQ,EAC3C,GAAI,IAAW,KAAM,OAAO,KAC5B,GAAI,EAAO,OAAS,EAAoB,MAAM,WAAW,EACzD,IAAM,EAAoB,CAAC,EAC3B,IAAK,IAAM,KAAQ,EAAO,MAAM,GAAG,EAAG,CACpC,IAAM,EAAK,EAAK,QAAQ,GAAG,EACvB,IAAO,IACP,EAAK,MAAM,EAAG,CAAE,CAAC,CAAC,KAAK,IAAA,mBAA2B,EAAQ,KAAK,EAAK,MAAM,EAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAC9F,CACA,GAAI,EAAQ,OAAS,EAAG,MAAM,WAAW,EACzC,OAAO,EAAQ,SAAW,GAAK,EAAQ,EAAE,CAAC,OAAS,EAAI,EAAQ,GAAK,IACtE,CAGA,IAAa,qBAAwB,GAA4B,EAAA,mBAAmB,GAAI,CAAE,cAAe,EAAG,QAAO,CAAC"}
|
|
1
|
+
{"version":3,"file":"http.cjs","names":[],"sources":["../../src/worker/http.ts"],"sourcesContent":["import {\n ARCHIVE_LIMITS,\n type ArchiveErrorCode,\n PUBLISHER_HEADER,\n REQUEST_MARKER_HEADER,\n SESSION_COOKIE_NAME,\n sessionCookieValue,\n} from '../contract';\nimport type { ApiConfig } from './env';\n\n/**\n * HTTP plumbing shared by every route: the one error shape, bounded body reads, strict\n * query/header guards and the CORS contract. Handlers throw ApiError; the entry point\n * sanitizes — no stack, SQL, body, or PII ever reaches a response. Internal to the\n * Worker: clients parse the wire envelope (`archiveErrorResponseSchema`), never this class.\n */\nexport class ApiError extends Error {\n readonly code: ArchiveErrorCode;\n readonly retryAfterSeconds?: number;\n readonly status: number;\n readonly diagnostic?: ArchiveDiagnostic;\n constructor(\n status: number,\n code: ArchiveErrorCode,\n message: string,\n opts?: ErrorOptions & { retryAfterSeconds?: number; diagnostic?: ArchiveDiagnostic },\n ) {\n super(message, opts);\n this.name = 'ApiError';\n this.code = code;\n this.retryAfterSeconds = opts?.retryAfterSeconds;\n this.status = status;\n this.diagnostic = opts?.diagnostic;\n }\n}\n\nexport type ArchiveDiagnostic =\n | { readonly kind: 'configuration'; readonly field: string }\n | { readonly kind: 'database' | 'record' | 'schema' | 'internal' | 'cache' | 'collection' };\n\nexport const badRequest = (message = 'The request is not valid.'): ApiError =>\n new ApiError(400, 'bad-request', message);\nexport const unauthenticated = (): ApiError => new ApiError(401, 'unauthenticated', 'Sign-in is required.');\nexport const forbidden = (message = 'That action is not allowed.'): ApiError => new ApiError(403, 'forbidden', message);\nexport const notFound = (): ApiError => new ApiError(404, 'not-found', 'No such entry.');\nexport const conflict = (message = 'The entry changed since you saw it.'): ApiError =>\n new ApiError(409, 'conflict', message);\nexport const invalid = (message = 'The payload is not valid.'): ApiError =>\n new ApiError(422, 'invalid-payload', message);\n\nexport const json = (status: number, body: unknown, headers: HeadersInit = {}): Response =>\n new Response(JSON.stringify(body), {\n headers: { 'content-type': 'application/json; charset=utf-8', ...headers },\n status,\n });\n\nexport const noStore = { 'cache-control': 'no-store' } as const;\n\nexport const errorBody = (status: number, code: string, message: string, requestId: string): Response =>\n json(status, { error: { code, message, requestId } }, noStore);\n\n/** Sanitizes any thrown value into the public error shape; unknowns become 500. */\nexport function toErrorResponse(error: unknown, requestId: string): Response {\n if (error instanceof ApiError) {\n const headers: HeadersInit =\n error.retryAfterSeconds === undefined ? noStore : { ...noStore, 'retry-after': String(error.retryAfterSeconds) };\n return json(error.status, { error: { code: error.code, message: error.message, requestId } }, headers);\n }\n return errorBody(500, 'internal', 'Something went wrong.', requestId);\n}\n\n/**\n * Rejects query strings with unknown or repeated parameters. Every accepted route\n * names its full allow-list; anything else is a client bug, not something to ignore.\n * Keys listed in `repeatable` may carry several values (read back with `getAll`);\n * every other key stays single-valued.\n */\nexport function queryParams(url: URL, allowed: readonly string[], repeatable: readonly string[] = []): URLSearchParams {\n const params = url.searchParams;\n const seen = new Set<string>();\n for (const key of params.keys()) {\n if (!allowed.includes(key) || (seen.has(key) && !repeatable.includes(key))) throw badRequest();\n seen.add(key);\n }\n return params;\n}\n\n/** Exact-origin plus request-marker guard for every mutation: no wildcards, no nulls. */\nexport function requireMutationGuard(request: Request, config: ApiConfig): void {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) throw forbidden();\n if (request.headers.get(REQUEST_MARKER_HEADER) !== '1') throw forbidden();\n}\n\n/** CORS response headers for one already-allowed origin; absent for foreign origins. */\nexport function corsHeaders(request: Request, config: ApiConfig): Record<string, string> {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) return {};\n return {\n 'access-control-allow-credentials': 'true',\n 'access-control-allow-origin': origin,\n vary: 'Origin',\n };\n}\n\n/** Answers a preflight for an allowed origin; foreign or missing origins get 403. */\nexport function preflight(request: Request, config: ApiConfig): Response {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) {\n return errorBody(403, 'forbidden', 'That action is not allowed.', crypto.randomUUID());\n }\n return new Response(null, {\n headers: {\n 'access-control-allow-credentials': 'true',\n // Header names are case-insensitive; the values derive from the contract constants.\n 'access-control-allow-headers': `Content-Type, ${PUBLISHER_HEADER}, ${REQUEST_MARKER_HEADER}`,\n 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',\n 'access-control-allow-origin': origin,\n 'access-control-max-age': '600',\n vary: 'Origin',\n },\n status: 204,\n });\n}\n\n/** Hard deadline for reading a request body; slow streams fail as 408. */\nexport const HTTP_BODY_TIMEOUT_MS = 10000;\n\nconst bodyTimeout = (): ApiError => new ApiError(408, 'timeout', 'The request body took too long.');\n\n/**\n * Reads a JSON body with a hard byte ceiling and a wall-clock deadline enforced while\n * streaming — a lying or absent Content-Length and a stalled sender are both cut off.\n * Wrong media types are 415, oversized 413, timed out 408, malformed JSON 400; schema\n * validation happens afterwards at the route boundary.\n */\nexport async function readJsonBody(request: Request): Promise<unknown> {\n // Exactly the application/json media type; parameters (charset etc.) may follow.\n const mediaType = (request.headers.get('content-type') ?? '').split(';', 1)[0].trim().toLowerCase();\n if (mediaType !== 'application/json') {\n throw new ApiError(415, 'unsupported-media', 'JSON bodies only.');\n }\n const reader = request.body?.getReader();\n if (!reader) throw badRequest();\n const startedAt = Date.now();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n const chunks: Uint8Array[] = [];\n let total = 0;\n try {\n for (;;) {\n const { done, value } = await Promise.race([\n reader.read(),\n new Promise<never>((_r, reject) => {\n timeoutId = setTimeout(\n () => reject(bodyTimeout()),\n Math.max(0, HTTP_BODY_TIMEOUT_MS - (Date.now() - startedAt)),\n );\n }),\n ]);\n clearTimeout(timeoutId);\n timeoutId = undefined;\n if (done) break;\n total += value.byteLength;\n if (total > ARCHIVE_LIMITS.requestBytes) throw new ApiError(413, 'too-large', 'The request is too large.');\n chunks.push(value);\n }\n } catch (error) {\n // Release the remainder so the failure isn't blocked on a stalled sender.\n reader.cancel().catch(() => undefined);\n throw error;\n } finally {\n if (timeoutId !== undefined) clearTimeout(timeoutId);\n reader.releaseLock();\n }\n const merged = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n merged.set(chunk, offset);\n offset += chunk.byteLength;\n }\n if (total === 0) throw badRequest();\n try {\n return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(merged));\n } catch {\n throw badRequest();\n }\n}\n\nconst SESSION_COOKIE_MAX = 4096;\n\n/**\n * Reads the session cookie. A duplicate cookie name or an oversized header is a\n * malformed request, not a missing session: callers distinguish null (anonymous)\n * from a token to verify.\n */\nexport function sessionToken(request: Request): string | null {\n const header = request.headers.get('cookie');\n if (header === null) return null;\n if (header.length > SESSION_COOKIE_MAX) throw badRequest();\n const matches: string[] = [];\n for (const part of header.split(';')) {\n const eq = part.indexOf('=');\n if (eq === -1) continue;\n if (part.slice(0, eq).trim() === SESSION_COOKIE_NAME) matches.push(part.slice(eq + 1).trim());\n }\n if (matches.length > 1) throw badRequest();\n return matches.length === 1 && matches[0].length > 0 ? matches[0] : null;\n}\n\n/** The expired-session Set-Cookie value the sign-out route and account erasure send. */\nexport const expiredSessionCookie = (secure: boolean): string => sessionCookieValue('', { maxAgeSeconds: 0, secure });\n"],"mappings":"mCAgBA,IAAa,SAAb,cAA8B,KAAM,CAClC,KACA,kBACA,OACA,WACA,YACE,EACA,EACA,EACA,EACA,CACA,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WACZ,KAAK,KAAO,EACZ,KAAK,kBAAoB,GAAM,kBAC/B,KAAK,OAAS,EACd,KAAK,WAAa,GAAM,UAC1B,CACF,EAMa,YAAc,EAAU,8BACnC,IAAI,SAAS,IAAK,cAAe,CAAO,EAC7B,oBAAkC,IAAI,SAAS,IAAK,kBAAmB,sBAAsB,EAC7F,WAAa,EAAU,gCAA4C,IAAI,SAAS,IAAK,YAAa,CAAO,EACzG,aAA2B,IAAI,SAAS,IAAK,YAAa,gBAAgB,EAC1E,UAAY,EAAU,wCACjC,IAAI,SAAS,IAAK,WAAY,CAAO,EAC1B,SAAW,EAAU,8BAChC,IAAI,SAAS,IAAK,kBAAmB,CAAO,EAEjC,MAAQ,EAAgB,EAAe,EAAuB,CAAC,IAC1E,IAAI,SAAS,KAAK,UAAU,CAAI,EAAG,CACjC,QAAS,CAAE,eAAgB,kCAAmC,GAAG,CAAQ,EACzE,QACF,CAAC,EAEU,EAAU,CAAE,gBAAiB,UAAW,EAExC,WAAa,EAAgB,EAAc,EAAiB,I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rB,CACA,IAAM,EAAS,IAAI,WAAW,CAAK,EAC/B,EAAS,EACb,IAAK,IAAM,KAAS,EAClB,EAAO,IAAI,EAAO,CAAM,EACxB,GAAU,EAAM,WAElB,GAAI,IAAU,EAAG,MAAM,WAAW,EAClC,GAAI,CACF,OAAO,KAAK,MAAM,IAAI,YAAY,QAAS,CAAE,MAAO,EAAK,CAAC,CAAC,CAAC,OAAO,CAAM,CAAC,CAC5E,MAAQ,CACN,MAAM,WAAW,CACnB,CACF,CAEA,IAAM,EAAqB,KAO3B,SAAgB,aAAa,EAAiC,CAC5D,IAAM,EAAS,EAAQ,QAAQ,IAAI,QAAQ,EAC3C,GAAI,IAAW,KAAM,OAAO,KAC5B,GAAI,EAAO,OAAS,EAAoB,MAAM,WAAW,EACzD,IAAM,EAAoB,CAAC,EAC3B,IAAK,IAAM,KAAQ,EAAO,MAAM,GAAG,EAAG,CACpC,IAAM,EAAK,EAAK,QAAQ,GAAG,EACvB,IAAO,IACP,EAAK,MAAM,EAAG,CAAE,CAAC,CAAC,KAAK,IAAA,mBAA2B,EAAQ,KAAK,EAAK,MAAM,EAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAC9F,CACA,GAAI,EAAQ,OAAS,EAAG,MAAM,WAAW,EACzC,OAAO,EAAQ,SAAW,GAAK,EAAQ,EAAE,CAAC,OAAS,EAAI,EAAQ,GAAK,IACtE,CAGA,IAAa,qBAAwB,GAA4B,EAAA,mBAAmB,GAAI,CAAE,cAAe,EAAG,QAAO,CAAC"}
|
package/dist/worker/http.d.ts
CHANGED
|
@@ -38,8 +38,10 @@ export declare function toErrorResponse(error: unknown, requestId: string): Resp
|
|
|
38
38
|
/**
|
|
39
39
|
* Rejects query strings with unknown or repeated parameters. Every accepted route
|
|
40
40
|
* names its full allow-list; anything else is a client bug, not something to ignore.
|
|
41
|
+
* Keys listed in `repeatable` may carry several values (read back with `getAll`);
|
|
42
|
+
* every other key stays single-valued.
|
|
41
43
|
*/
|
|
42
|
-
export declare function queryParams(url: URL, allowed: readonly string[]): URLSearchParams;
|
|
44
|
+
export declare function queryParams(url: URL, allowed: readonly string[], repeatable?: readonly string[]): URLSearchParams;
|
|
43
45
|
/** Exact-origin plus request-marker guard for every mutation: no wildcards, no nulls. */
|
|
44
46
|
export declare function requireMutationGuard(request: Request, config: ApiConfig): void;
|
|
45
47
|
/** CORS response headers for one already-allowed origin; absent for foreign origins. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/worker/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,gBAAgB,EAKtB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC;;;;;GAKG;AACH,qBAAa,QAAS,SAAQ,KAAK;IACjC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,UAAU,CAAC,EAAE,iBAAiB,CAAC;gBAEtC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,gBAAgB,EACtB,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,YAAY,GAAG;QAAE,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,iBAAiB,CAAA;KAAE;CASvF;AAED,MAAM,MAAM,iBAAiB,GACzB;IAAE,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC1D;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,QAAQ,GAAG,QAAQ,GAAG,UAAU,GAAG,OAAO,GAAG,YAAY,CAAA;CAAE,CAAC;AAE9F,eAAO,MAAM,UAAU,GAAI,gBAAqC,KAAG,QACxB,CAAC;AAC5C,eAAO,MAAM,e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OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQvF;AAED,qFAAqF;AACrF,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,GAAG,QAAQ,CAiBvE;AAED,0EAA0E;AAC1E,eAAO,MAAM,oBAAoB,QAAQ,CAAC;AAI1C;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAkDrE;AAID;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAY5D;AAED,wFAAwF;AACxF,eAAO,MAAM,oBAAoB,GAAI,QAAQ,OAAO,KAAG,MAA8D,CAAC"}
|
package/dist/worker/http.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{ARCHIVE_LIMITS as e,PUBLISHER_HEADER as t,REQUEST_MARKER_HEADER as n,sessionCookieValue as r}from"../contract.js";var ApiError=class extends Error{code;retryAfterSeconds;status;diagnostic;constructor(e,t,n,r){super(n,r),this.name=`ApiError`,this.code=t,this.retryAfterSeconds=r?.retryAfterSeconds,this.status=e,this.diagnostic=r?.diagnostic}},badRequest=(e=`The request is not valid.`)=>new ApiError(400,`bad-request`,e),unauthenticated=()=>new ApiError(401,`unauthenticated`,`Sign-in is required.`),forbidden=(e=`That action is not allowed.`)=>new ApiError(403,`forbidden`,e),notFound=()=>new ApiError(404,`not-found`,`No such entry.`),conflict=(e=`The entry changed since you saw it.`)=>new ApiError(409,`conflict`,e),invalid=(e=`The payload is not valid.`)=>new ApiError(422,`invalid-payload`,e),json=(e,t,n={})=>new Response(JSON.stringify(t),{headers:{"content-type":`application/json; charset=utf-8`,...n},status:e}),i={"cache-control":`no-store`},errorBody=(e,t,n,r)=>json(e,{error:{code:t,message:n,requestId:r}},i);function toErrorResponse(e,t){if(e instanceof ApiError){let n=e.retryAfterSeconds===void 0?i:{...i,"retry-after":String(e.retryAfterSeconds)};return json(e.status,{error:{code:e.code,message:e.message,requestId:t}},n)}return errorBody(500,`internal`,`Something went wrong.`,t)}function queryParams(e,t){let
|
|
1
|
+
import{ARCHIVE_LIMITS as e,PUBLISHER_HEADER as t,REQUEST_MARKER_HEADER as n,sessionCookieValue as r}from"../contract.js";var ApiError=class extends Error{code;retryAfterSeconds;status;diagnostic;constructor(e,t,n,r){super(n,r),this.name=`ApiError`,this.code=t,this.retryAfterSeconds=r?.retryAfterSeconds,this.status=e,this.diagnostic=r?.diagnostic}},badRequest=(e=`The request is not valid.`)=>new ApiError(400,`bad-request`,e),unauthenticated=()=>new ApiError(401,`unauthenticated`,`Sign-in is required.`),forbidden=(e=`That action is not allowed.`)=>new ApiError(403,`forbidden`,e),notFound=()=>new ApiError(404,`not-found`,`No such entry.`),conflict=(e=`The entry changed since you saw it.`)=>new ApiError(409,`conflict`,e),invalid=(e=`The payload is not valid.`)=>new ApiError(422,`invalid-payload`,e),json=(e,t,n={})=>new Response(JSON.stringify(t),{headers:{"content-type":`application/json; charset=utf-8`,...n},status:e}),i={"cache-control":`no-store`},errorBody=(e,t,n,r)=>json(e,{error:{code:t,message:n,requestId:r}},i);function toErrorResponse(e,t){if(e instanceof ApiError){let n=e.retryAfterSeconds===void 0?i:{...i,"retry-after":String(e.retryAfterSeconds)};return json(e.status,{error:{code:e.code,message:e.message,requestId:t}},n)}return errorBody(500,`internal`,`Something went wrong.`,t)}function queryParams(e,t,n=[]){let r=e.searchParams,i=new Set;for(let e of r.keys()){if(!t.includes(e)||i.has(e)&&!n.includes(e))throw badRequest();i.add(e)}return r}function requireMutationGuard(e,t){let n=e.headers.get(`origin`);if(n===null||!t.allowedOrigins.has(n)||e.headers.get(`x-archive-request`)!==`1`)throw forbidden()}function corsHeaders(e,t){let n=e.headers.get(`origin`);return n===null||!t.allowedOrigins.has(n)?{}:{"access-control-allow-credentials":`true`,"access-control-allow-origin":n,vary:`Origin`}}function preflight(e,r){let i=e.headers.get(`origin`);return i===null||!r.allowedOrigins.has(i)?errorBody(403,`forbidden`,`That action is not allowed.`,crypto.randomUUID()):new Response(null,{headers:{"access-control-allow-credentials":`true`,"access-control-allow-headers":`Content-Type, ${t}, ${n}`,"access-control-allow-methods":`GET, POST, PUT, DELETE, OPTIONS`,"access-control-allow-origin":i,"access-control-max-age":`600`,vary:`Origin`},status:204})}var a=1e4,bodyTimeout=()=>new ApiError(408,`timeout`,`The request body took too long.`);async function readJsonBody(t){if((t.headers.get(`content-type`)??``).split(`;`,1)[0].trim().toLowerCase()!==`application/json`)throw new ApiError(415,`unsupported-media`,`JSON bodies only.`);let n=t.body?.getReader();if(!n)throw badRequest();let r=Date.now(),i,o=[],s=0;try{for(;;){let{done:t,value:c}=await Promise.race([n.read(),new Promise((e,t)=>{i=setTimeout(()=>t(bodyTimeout()),Math.max(0,a-(Date.now()-r)))})]);if(clearTimeout(i),i=void 0,t)break;if(s+=c.byteLength,s>e.requestBytes)throw new ApiError(413,`too-large`,`The request is too large.`);o.push(c)}}catch(e){throw n.cancel().catch(()=>void 0),e}finally{i!==void 0&&clearTimeout(i),n.releaseLock()}let c=new Uint8Array(s),l=0;for(let e of o)c.set(e,l),l+=e.byteLength;if(s===0)throw badRequest();try{return JSON.parse(new TextDecoder(`utf-8`,{fatal:!0}).decode(c))}catch{throw badRequest()}}var o=4096;function sessionToken(e){let t=e.headers.get(`cookie`);if(t===null)return null;if(t.length>o)throw badRequest();let n=[];for(let e of t.split(`;`)){let t=e.indexOf(`=`);t!==-1&&e.slice(0,t).trim()===`archive_session`&&n.push(e.slice(t+1).trim())}if(n.length>1)throw badRequest();return n.length===1&&n[0].length>0?n[0]:null}var expiredSessionCookie=e=>r(``,{maxAgeSeconds:0,secure:e});export{ApiError,a as HTTP_BODY_TIMEOUT_MS,badRequest,conflict,corsHeaders,errorBody,expiredSessionCookie,forbidden,invalid,json,i as noStore,notFound,preflight,queryParams,readJsonBody,requireMutationGuard,sessionToken,toErrorResponse,unauthenticated};
|
|
2
2
|
//# sourceMappingURL=http.js.map
|
package/dist/worker/http.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.js","names":[],"sources":["../../src/worker/http.ts"],"sourcesContent":["import {\n ARCHIVE_LIMITS,\n type ArchiveErrorCode,\n PUBLISHER_HEADER,\n REQUEST_MARKER_HEADER,\n SESSION_COOKIE_NAME,\n sessionCookieValue,\n} from '../contract';\nimport type { ApiConfig } from './env';\n\n/**\n * HTTP plumbing shared by every route: the one error shape, bounded body reads, strict\n * query/header guards and the CORS contract. Handlers throw ApiError; the entry point\n * sanitizes — no stack, SQL, body, or PII ever reaches a response. Internal to the\n * Worker: clients parse the wire envelope (`archiveErrorResponseSchema`), never this class.\n */\nexport class ApiError extends Error {\n readonly code: ArchiveErrorCode;\n readonly retryAfterSeconds?: number;\n readonly status: number;\n readonly diagnostic?: ArchiveDiagnostic;\n constructor(\n status: number,\n code: ArchiveErrorCode,\n message: string,\n opts?: ErrorOptions & { retryAfterSeconds?: number; diagnostic?: ArchiveDiagnostic },\n ) {\n super(message, opts);\n this.name = 'ApiError';\n this.code = code;\n this.retryAfterSeconds = opts?.retryAfterSeconds;\n this.status = status;\n this.diagnostic = opts?.diagnostic;\n }\n}\n\nexport type ArchiveDiagnostic =\n | { readonly kind: 'configuration'; readonly field: string }\n | { readonly kind: 'database' | 'record' | 'schema' | 'internal' | 'cache' | 'collection' };\n\nexport const badRequest = (message = 'The request is not valid.'): ApiError =>\n new ApiError(400, 'bad-request', message);\nexport const unauthenticated = (): ApiError => new ApiError(401, 'unauthenticated', 'Sign-in is required.');\nexport const forbidden = (message = 'That action is not allowed.'): ApiError => new ApiError(403, 'forbidden', message);\nexport const notFound = (): ApiError => new ApiError(404, 'not-found', 'No such entry.');\nexport const conflict = (message = 'The entry changed since you saw it.'): ApiError =>\n new ApiError(409, 'conflict', message);\nexport const invalid = (message = 'The payload is not valid.'): ApiError =>\n new ApiError(422, 'invalid-payload', message);\n\nexport const json = (status: number, body: unknown, headers: HeadersInit = {}): Response =>\n new Response(JSON.stringify(body), {\n headers: { 'content-type': 'application/json; charset=utf-8', ...headers },\n status,\n });\n\nexport const noStore = { 'cache-control': 'no-store' } as const;\n\nexport const errorBody = (status: number, code: string, message: string, requestId: string): Response =>\n json(status, { error: { code, message, requestId } }, noStore);\n\n/** Sanitizes any thrown value into the public error shape; unknowns become 500. */\nexport function toErrorResponse(error: unknown, requestId: string): Response {\n if (error instanceof ApiError) {\n const headers: HeadersInit =\n error.retryAfterSeconds === undefined ? noStore : { ...noStore, 'retry-after': String(error.retryAfterSeconds) };\n return json(error.status, { error: { code: error.code, message: error.message, requestId } }, headers);\n }\n return errorBody(500, 'internal', 'Something went wrong.', requestId);\n}\n\n/**\n * Rejects query strings with unknown or repeated parameters. Every accepted route\n * names its full allow-list; anything else is a client bug, not something to ignore.\n */\nexport function queryParams(url: URL, allowed: readonly string[]): URLSearchParams {\n const params = url.searchParams;\n const seen = new Set<string>();\n for (const key of params.keys()) {\n if (!allowed.includes(key) || seen.has(key)) throw badRequest();\n seen.add(key);\n }\n return params;\n}\n\n/** Exact-origin plus request-marker guard for every mutation: no wildcards, no nulls. */\nexport function requireMutationGuard(request: Request, config: ApiConfig): void {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) throw forbidden();\n if (request.headers.get(REQUEST_MARKER_HEADER) !== '1') throw forbidden();\n}\n\n/** CORS response headers for one already-allowed origin; absent for foreign origins. */\nexport function corsHeaders(request: Request, config: ApiConfig): Record<string, string> {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) return {};\n return {\n 'access-control-allow-credentials': 'true',\n 'access-control-allow-origin': origin,\n vary: 'Origin',\n };\n}\n\n/** Answers a preflight for an allowed origin; foreign or missing origins get 403. */\nexport function preflight(request: Request, config: ApiConfig): Response {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) {\n return errorBody(403, 'forbidden', 'That action is not allowed.', crypto.randomUUID());\n }\n return new Response(null, {\n headers: {\n 'access-control-allow-credentials': 'true',\n // Header names are case-insensitive; the values derive from the contract constants.\n 'access-control-allow-headers': `Content-Type, ${PUBLISHER_HEADER}, ${REQUEST_MARKER_HEADER}`,\n 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',\n 'access-control-allow-origin': origin,\n 'access-control-max-age': '600',\n vary: 'Origin',\n },\n status: 204,\n });\n}\n\n/** Hard deadline for reading a request body; slow streams fail as 408. */\nexport const HTTP_BODY_TIMEOUT_MS = 10000;\n\nconst bodyTimeout = (): ApiError => new ApiError(408, 'timeout', 'The request body took too long.');\n\n/**\n * Reads a JSON body with a hard byte ceiling and a wall-clock deadline enforced while\n * streaming — a lying or absent Content-Length and a stalled sender are both cut off.\n * Wrong media types are 415, oversized 413, timed out 408, malformed JSON 400; schema\n * validation happens afterwards at the route boundary.\n */\nexport async function readJsonBody(request: Request): Promise<unknown> {\n // Exactly the application/json media type; parameters (charset etc.) may follow.\n const mediaType = (request.headers.get('content-type') ?? '').split(';', 1)[0].trim().toLowerCase();\n if (mediaType !== 'application/json') {\n throw new ApiError(415, 'unsupported-media', 'JSON bodies only.');\n }\n const reader = request.body?.getReader();\n if (!reader) throw badRequest();\n const startedAt = Date.now();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n const chunks: Uint8Array[] = [];\n let total = 0;\n try {\n for (;;) {\n const { done, value } = await Promise.race([\n reader.read(),\n new Promise<never>((_r, reject) => {\n timeoutId = setTimeout(\n () => reject(bodyTimeout()),\n Math.max(0, HTTP_BODY_TIMEOUT_MS - (Date.now() - startedAt)),\n );\n }),\n ]);\n clearTimeout(timeoutId);\n timeoutId = undefined;\n if (done) break;\n total += value.byteLength;\n if (total > ARCHIVE_LIMITS.requestBytes) throw new ApiError(413, 'too-large', 'The request is too large.');\n chunks.push(value);\n }\n } catch (error) {\n // Release the remainder so the failure isn't blocked on a stalled sender.\n reader.cancel().catch(() => undefined);\n throw error;\n } finally {\n if (timeoutId !== undefined) clearTimeout(timeoutId);\n reader.releaseLock();\n }\n const merged = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n merged.set(chunk, offset);\n offset += chunk.byteLength;\n }\n if (total === 0) throw badRequest();\n try {\n return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(merged));\n } catch {\n throw badRequest();\n }\n}\n\nconst SESSION_COOKIE_MAX = 4096;\n\n/**\n * Reads the session cookie. A duplicate cookie name or an oversized header is a\n * malformed request, not a missing session: callers distinguish null (anonymous)\n * from a token to verify.\n */\nexport function sessionToken(request: Request): string | null {\n const header = request.headers.get('cookie');\n if (header === null) return null;\n if (header.length > SESSION_COOKIE_MAX) throw badRequest();\n const matches: string[] = [];\n for (const part of header.split(';')) {\n const eq = part.indexOf('=');\n if (eq === -1) continue;\n if (part.slice(0, eq).trim() === SESSION_COOKIE_NAME) matches.push(part.slice(eq + 1).trim());\n }\n if (matches.length > 1) throw badRequest();\n return matches.length === 1 && matches[0].length > 0 ? matches[0] : null;\n}\n\n/** The expired-session Set-Cookie value the sign-out route and account erasure send. */\nexport const expiredSessionCookie = (secure: boolean): string => sessionCookieValue('', { maxAgeSeconds: 0, secure });\n"],"mappings":"yHAgBA,IAAa,SAAb,cAA8B,KAAM,CAClC,KACA,kBACA,OACA,WACA,YACE,EACA,EACA,EACA,EACA,CACA,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WACZ,KAAK,KAAO,EACZ,KAAK,kBAAoB,GAAM,kBAC/B,KAAK,OAAS,EACd,KAAK,WAAa,GAAM,UAC1B,CACF,EAMa,YAAc,EAAU,8BACnC,IAAI,SAAS,IAAK,cAAe,CAAO,EAC7B,oBAAkC,IAAI,SAAS,IAAK,kBAAmB,sBAAsB,EAC7F,WAAa,EAAU,gCAA4C,IAAI,SAAS,IAAK,YAAa,CAAO,EACzG,aAA2B,IAAI,SAAS,IAAK,YAAa,gBAAgB,EAC1E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.ts"],"sourcesContent":["import {\n ARCHIVE_LIMITS,\n type ArchiveErrorCode,\n PUBLISHER_HEADER,\n REQUEST_MARKER_HEADER,\n SESSION_COOKIE_NAME,\n sessionCookieValue,\n} from '../contract';\nimport type { ApiConfig } from './env';\n\n/**\n * HTTP plumbing shared by every route: the one error shape, bounded body reads, strict\n * query/header guards and the CORS contract. Handlers throw ApiError; the entry point\n * sanitizes — no stack, SQL, body, or PII ever reaches a response. Internal to the\n * Worker: clients parse the wire envelope (`archiveErrorResponseSchema`), never this class.\n */\nexport class ApiError extends Error {\n readonly code: ArchiveErrorCode;\n readonly retryAfterSeconds?: number;\n readonly status: number;\n readonly diagnostic?: ArchiveDiagnostic;\n constructor(\n status: number,\n code: ArchiveErrorCode,\n message: string,\n opts?: ErrorOptions & { retryAfterSeconds?: number; diagnostic?: ArchiveDiagnostic },\n ) {\n super(message, opts);\n this.name = 'ApiError';\n this.code = code;\n this.retryAfterSeconds = opts?.retryAfterSeconds;\n this.status = status;\n this.diagnostic = opts?.diagnostic;\n }\n}\n\nexport type ArchiveDiagnostic =\n | { readonly kind: 'configuration'; readonly field: string }\n | { readonly kind: 'database' | 'record' | 'schema' | 'internal' | 'cache' | 'collection' };\n\nexport const badRequest = (message = 'The request is not valid.'): ApiError =>\n new ApiError(400, 'bad-request', message);\nexport const unauthenticated = (): ApiError => new ApiError(401, 'unauthenticated', 'Sign-in is required.');\nexport const forbidden = (message = 'That action is not allowed.'): ApiError => new ApiError(403, 'forbidden', message);\nexport const notFound = (): ApiError => new ApiError(404, 'not-found', 'No such entry.');\nexport const conflict = (message = 'The entry changed since you saw it.'): ApiError =>\n new ApiError(409, 'conflict', message);\nexport const invalid = (message = 'The payload is not valid.'): ApiError =>\n new ApiError(422, 'invalid-payload', message);\n\nexport const json = (status: number, body: unknown, headers: HeadersInit = {}): Response =>\n new Response(JSON.stringify(body), {\n headers: { 'content-type': 'application/json; charset=utf-8', ...headers },\n status,\n });\n\nexport const noStore = { 'cache-control': 'no-store' } as const;\n\nexport const errorBody = (status: number, code: string, message: string, requestId: string): Response =>\n json(status, { error: { code, message, requestId } }, noStore);\n\n/** Sanitizes any thrown value into the public error shape; unknowns become 500. */\nexport function toErrorResponse(error: unknown, requestId: string): Response {\n if (error instanceof ApiError) {\n const headers: HeadersInit =\n error.retryAfterSeconds === undefined ? noStore : { ...noStore, 'retry-after': String(error.retryAfterSeconds) };\n return json(error.status, { error: { code: error.code, message: error.message, requestId } }, headers);\n }\n return errorBody(500, 'internal', 'Something went wrong.', requestId);\n}\n\n/**\n * Rejects query strings with unknown or repeated parameters. Every accepted route\n * names its full allow-list; anything else is a client bug, not something to ignore.\n * Keys listed in `repeatable` may carry several values (read back with `getAll`);\n * every other key stays single-valued.\n */\nexport function queryParams(url: URL, allowed: readonly string[], repeatable: readonly string[] = []): URLSearchParams {\n const params = url.searchParams;\n const seen = new Set<string>();\n for (const key of params.keys()) {\n if (!allowed.includes(key) || (seen.has(key) && !repeatable.includes(key))) throw badRequest();\n seen.add(key);\n }\n return params;\n}\n\n/** Exact-origin plus request-marker guard for every mutation: no wildcards, no nulls. */\nexport function requireMutationGuard(request: Request, config: ApiConfig): void {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) throw forbidden();\n if (request.headers.get(REQUEST_MARKER_HEADER) !== '1') throw forbidden();\n}\n\n/** CORS response headers for one already-allowed origin; absent for foreign origins. */\nexport function corsHeaders(request: Request, config: ApiConfig): Record<string, string> {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) return {};\n return {\n 'access-control-allow-credentials': 'true',\n 'access-control-allow-origin': origin,\n vary: 'Origin',\n };\n}\n\n/** Answers a preflight for an allowed origin; foreign or missing origins get 403. */\nexport function preflight(request: Request, config: ApiConfig): Response {\n const origin = request.headers.get('origin');\n if (origin === null || !config.allowedOrigins.has(origin)) {\n return errorBody(403, 'forbidden', 'That action is not allowed.', crypto.randomUUID());\n }\n return new Response(null, {\n headers: {\n 'access-control-allow-credentials': 'true',\n // Header names are case-insensitive; the values derive from the contract constants.\n 'access-control-allow-headers': `Content-Type, ${PUBLISHER_HEADER}, ${REQUEST_MARKER_HEADER}`,\n 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',\n 'access-control-allow-origin': origin,\n 'access-control-max-age': '600',\n vary: 'Origin',\n },\n status: 204,\n });\n}\n\n/** Hard deadline for reading a request body; slow streams fail as 408. */\nexport const HTTP_BODY_TIMEOUT_MS = 10000;\n\nconst bodyTimeout = (): ApiError => new ApiError(408, 'timeout', 'The request body took too long.');\n\n/**\n * Reads a JSON body with a hard byte ceiling and a wall-clock deadline enforced while\n * streaming — a lying or absent Content-Length and a stalled sender are both cut off.\n * Wrong media types are 415, oversized 413, timed out 408, malformed JSON 400; schema\n * validation happens afterwards at the route boundary.\n */\nexport async function readJsonBody(request: Request): Promise<unknown> {\n // Exactly the application/json media type; parameters (charset etc.) may follow.\n const mediaType = (request.headers.get('content-type') ?? '').split(';', 1)[0].trim().toLowerCase();\n if (mediaType !== 'application/json') {\n throw new ApiError(415, 'unsupported-media', 'JSON bodies only.');\n }\n const reader = request.body?.getReader();\n if (!reader) throw badRequest();\n const startedAt = Date.now();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n const chunks: Uint8Array[] = [];\n let total = 0;\n try {\n for (;;) {\n const { done, value } = await Promise.race([\n reader.read(),\n new Promise<never>((_r, reject) => {\n timeoutId = setTimeout(\n () => reject(bodyTimeout()),\n Math.max(0, HTTP_BODY_TIMEOUT_MS - (Date.now() - startedAt)),\n );\n }),\n ]);\n clearTimeout(timeoutId);\n timeoutId = undefined;\n if (done) break;\n total += value.byteLength;\n if (total > ARCHIVE_LIMITS.requestBytes) throw new ApiError(413, 'too-large', 'The request is too large.');\n chunks.push(value);\n }\n } catch (error) {\n // Release the remainder so the failure isn't blocked on a stalled sender.\n reader.cancel().catch(() => undefined);\n throw error;\n } finally {\n if (timeoutId !== undefined) clearTimeout(timeoutId);\n reader.releaseLock();\n }\n const merged = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n merged.set(chunk, offset);\n offset += chunk.byteLength;\n }\n if (total === 0) throw badRequest();\n try {\n return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(merged));\n } catch {\n throw badRequest();\n }\n}\n\nconst SESSION_COOKIE_MAX = 4096;\n\n/**\n * Reads the session cookie. A duplicate cookie name or an oversized header is a\n * malformed request, not a missing session: callers distinguish null (anonymous)\n * from a token to verify.\n */\nexport function sessionToken(request: Request): string | null {\n const header = request.headers.get('cookie');\n if (header === null) return null;\n if (header.length > SESSION_COOKIE_MAX) throw badRequest();\n const matches: string[] = [];\n for (const part of header.split(';')) {\n const eq = part.indexOf('=');\n if (eq === -1) continue;\n if (part.slice(0, eq).trim() === SESSION_COOKIE_NAME) matches.push(part.slice(eq + 1).trim());\n }\n if (matches.length > 1) throw badRequest();\n return matches.length === 1 && matches[0].length > 0 ? matches[0] : null;\n}\n\n/** The expired-session Set-Cookie value the sign-out route and account erasure send. */\nexport const expiredSessionCookie = (secure: boolean): string => sessionCookieValue('', { maxAgeSeconds: 0, secure });\n"],"mappings":"yHAgBA,IAAa,SAAb,cAA8B,KAAM,CAClC,KACA,kBACA,OACA,WACA,YACE,EACA,EACA,EACA,EACA,CACA,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WACZ,KAAK,KAAO,EACZ,KAAK,kBAAoB,GAAM,kBAC/B,KAAK,OAAS,EACd,KAAK,WAAa,GAAM,UAC1B,CACF,EAMa,YAAc,EAAU,8BACnC,IAAI,SAAS,IAAK,cAAe,CAAO,EAC7B,oBAAkC,IAAI,SAAS,IAAK,kBAAmB,sBAAsB,EAC7F,WAAa,EAAU,gCAA4C,IAAI,SAAS,IAAK,YAAa,CAAO,EACzG,aAA2B,IAAI,SAAS,IAAK,YAAa,gBAAgB,EAC1E,UAAY,EAAU,wCACjC,IAAI,SAAS,IAAK,WAAY,CAAO,EAC1B,SAAW,EAAU,8BAChC,IAA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cjs"),r=require("./_pagination.cjs");let i=require("@vielzeug/spell");async function databaseCall(e){try{return await e()}catch(e){throw new t.ApiError(500,`internal`,`The service is not available.`,{cause:e,diagnostic:{kind:`database`}})}}var first=(e,t,...n)=>databaseCall(()=>e.DB.prepare(t).bind(...n).first()),all=(e,t,...n)=>databaseCall(async()=>{let r=await e.DB.prepare(t).bind(...n).all();if(!Array.isArray(r.results))throw Error(`Missing database query results`);return r.results}),run=(e,t,...n)=>databaseCall(()=>e.DB.prepare(t).bind(...n).run()),uuid=()=>crypto.randomUUID(),now=()=>new Date().toISOString(),recordUnavailable=()=>new t.ApiError(503,`record-unavailable`,`The record is not available.`,{diagnostic:{kind:`record`}}),a=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,rowToEntry=(e,t)=>{let n;try{n=JSON.parse(t.payload_json)}catch{throw recordUnavailable()}let r=e.contract.entry.safeParse({author:{id:t.owner_id,name:t.author_name},contentHash:t.content_hash,contentVersion:t.content_version,id:t.id,likeCount:t.like_count,payload:n,publishedAt:t.published_at,revision:t.revision,schemaVersion:t.schema_version,updatedAt:t.updated_at});if(!r.success)throw recordUnavailable();return r.data},rowToOwnedEntry=(t,n)=>{let r=rowToEntry(t,n);if(!e.uuidSchema.safeParse(n.client_key).success)throw recordUnavailable();return{...r,clientKey:n.client_key,hidden:n.hidden_at!==null}},entryById=async(e,t,r)=>{let i=await first(t,n.SQL.publication,r,e.name);return i===null?null:rowToEntry(e,i)},searchMatchFor=n=>{if(n===null)return;if(n.length>e.ARCHIVE_LIMITS.searchChars)throw t.badRequest();let r=(n.match(/[\p{L}\p{N}]+/gu)??[]).slice(0,e.ARCHIVE_LIMITS.searchTerms);if(r.length!==0)return r.map(e=>`"${e}"*`).join(` `)},entryCursorContext=e=>JSON.stringify([e.collection,e.sort,e.author??``,e.filter??``,e.tag??``,e.searchMatch??``]),cursorFromRow=e=>({createdAt:e.published_at,id:e.id,likeCount:e.like_count});async function listEntries(i,o,s){let c=new URL(o.url),l=t.queryParams(c,[`author`,`cursor`,`filter`,`limit`,`search`,`sort`,`tag`]),u=l.get(`sort`)??`recent`,d=e.archiveSortSchema.safeParse(u);if(!d.success)throw t.badRequest();let f=l.get(`filter`);if(f!==null&&!i.filters.includes(f))throw t.badRequest();let p=l.get(`tag`);if(p!==null&&!i.tags.includes(p))throw t.badRequest();let m=l.get(`author`);if(m!==null&&!a.test(m))throw t.badRequest();let h=searchMatchFor(l.get(`search`)),g=entryCursorContext({author:m??void 0,collection:i.name,filter:f??void 0,searchMatch:h,sort:d.data,tag:p??void 0}),{cursor:_,limit:v}=r.forwardPageParams(l,g),y={author:m??void 0,collection:i.name,cursor:_===null?void 0:{createdAt:_.createdAt,id:_.id,likeCount:_.likeCount??0},filter:f??void 0,limit:v+1,searchMatch:h,sort:d.data,tag:p??void 0},b=n.listPublicationsSql(y),x=await all(s,b.sql,...b.parameters),S=x.slice(0,v),C=S.at(-1),w={cursor:x.length>v&&C!==void 0?r.nextCursor(cursorFromRow(C),g):null,entries:S.map(e=>rowToEntry(i,e))};return t.json(200,w,t.noStore)}function collectionMeta(n){let r=e.archiveCollectionMetaSchema.safeParse({filters:[...n.filters].sort(),name:n.name,tags:[...n.tags].sort()});if(!r.success)throw recordUnavailable();return t.json(200,r.data,t.noStore)}async function getEntry(e,n,r){let i=await entryById(e,n,r);if(i===null)throw t.notFound();return t.json(200,i,t.noStore)}async function healthJson(r,i){let a=null,o=null;try{a=(await first(i,n.SQL.schemaVersion))?.applied??0}catch{o=`database-unavailable`}o===null&&a!==1&&(o=`schema-mismatch`);let s=e.archiveHealthSchema.safeParse({dbSchemaVersion:a,features:{likes:r.likesEnabled,publishing:r.publishingEnabled},limits:e.ARCHIVE_LIMITS,reason:o,schemaVersion:1,status:o===null?`ok`:`degraded`});if(!s.success)throw recordUnavailable();return t.json(o===null?200:503,s.data,t.noStore)}var identityJson=(n,r)=>{let i=e.archiveIdentitySchema.safeParse(r);if(!i.success)throw recordUnavailable();return t.json(n,i.data,t.noStore)};function getMe(e){return identityJson(200,e?.publisher?{id:e.publisher.id,name:e.publisher.displayName,operator:e.operator}:null)}async function registerMe(e,r,i){let a=await t.readJsonBody(e),s=o.safeParse(a);if(!s.success)throw t.invalid();let c=s.data.name??``,l=await first(r,n.SQL.publisher,i.subject.issuer,i.subject.subject);if(l!==null)return identityJson(200,{id:l.id,name:l.display_name,operator:i.operator});let u=uuid();await run(r,n.SQL.insertPublisher,u,i.subject.issuer,i.subject.subject,c,now(),i.subject.issuer,i.subject.subject,Date.now());let d=await first(r,n.SQL.publisher,i.subject.issuer,i.subject.subject);if(d===null)throw await first(r,n.SQL.revokedSubject,i.subject.issuer,i.subject.subject,Date.now())===null?t.conflict():new t.ApiError(401,`unauthenticated`,`Sign-in is required.`);return identityJson(201,{id:d.id,name:d.display_name,operator:i.operator})}async function renameMe(e,r,i){let a=requirePublisherRow(i),o=await t.readJsonBody(e),c=s.safeParse(o);if(!c.success)throw t.invalid();return c.data.name!==a.displayName&&await run(r,n.SQL.renamePublisher,c.data.name,a.id,c.data.name),identityJson(200,{id:a.id,name:c.data.name,operator:i.operator})}async function deleteProfile(e,t){return t.publisher!==null&&await run(e,n.SQL.deletePublisher,t.publisher.id),new Response(null,{headers:{"cache-control":`no-store`},status:204})}async function deleteAccountProfile(r,i){let{issuer:a,subject:o}=i.subject;return await databaseCall(()=>r.DB.batch([r.DB.prepare(n.SQL.revokeSubject).bind(a,o,Date.now()+(e.SESSION_MAX_AGE_SECONDS+10)*1e3),r.DB.prepare(n.SQL.deleteSubjectPublisher).bind(a,o)])),new Response(null,{headers:t.noStore,status:204})}async function erasePublisher(e,r,i){let a=requireOperatorRow(r);return await databaseCall(()=>e.DB.batch([e.DB.prepare(n.SQL.auditErasure).bind(uuid(),a.id,now(),i),e.DB.prepare(n.SQL.deletePublisher).bind(i)])),new Response(null,{headers:t.noStore,status:204})}async function listModerationLog(i,a,o){requireOperatorRow(o);let s=`moderation-log`,{cursor:c,limit:l}=r.forwardPage(i,s),u=n.moderationLogSql({cursor:c,limit:l+1}),d=await all(a,u.sql,...u.parameters),f=d.slice(0,l),p=f.at(-1),m=e.archiveModerationLogSchema.safeParse({actions:f.map(e=>({action:e.action,collection:e.collection,createdAt:e.created_at,id:e.id,operatorId:e.operator_id,targetId:e.target_id})),cursor:d.length>l&&p!==void 0?r.nextCursor({createdAt:p.created_at,id:p.id},s):null});if(!m.success)throw recordUnavailable();return t.json(200,m.data,t.noStore)}var requirePublisherRow=e=>{if(e.publisher===null)throw t.forbidden(`Register before publishing.`);return e.publisher},requireOperatorRow=e=>{if(!e.operator)throw t.forbidden(`Operator access is required.`);if(e.publisher===null)throw t.forbidden(`Register before moderating.`);return e.publisher},o=i.s.object({name:e.archiveAliasSchema.optional()}),s=i.s.object({name:e.archiveAliasSchema});async function myPublications(r,i,a){if(a.publisher===null)return t.json(200,{entries:[]},t.noStore);let o=await all(i,n.SQL.ownPublications,a.publisher.id,r.name,e.ARCHIVE_LIMITS.maxPublications);return t.json(200,{entries:o.map(e=>rowToOwnedEntry(r,e))},t.noStore)}async function myLikes(i,o,s){let c=new URL(i.url).searchParams.get(`ids`);if(c===null){let a=`likes:${s.publisher?.id??`unregistered`}`,{cursor:c,limit:l}=r.forwardPage(i,a);if(s.publisher===null)return t.json(200,{cursor:null,ids:[]},t.noStore);let u=n.likesPageSql(s.publisher.id,{cursor:c,limit:l+1}),d=await all(o,u.sql,...u.parameters),f=d.slice(0,l),p=f.at(-1),m=e.archiveLikesPageSchema.safeParse({cursor:d.length>l&&p!==void 0?r.nextCursor({createdAt:p.created_at,id:p.publication_id},a):null,ids:f.map(e=>e.publication_id)});if(!m.success)throw recordUnavailable();return t.json(200,m.data,t.noStore)}t.queryParams(new URL(i.url),[`ids`]);let l=[...new Set(c.split(`,`))];if(l.length>e.ARCHIVE_LIMITS.likeStateIds||l.some(e=>!a.test(e)))throw t.badRequest();if(s.publisher===null)return t.json(200,{ids:[]},t.noStore);let u=n.likedPublicationsSql(s.publisher.id,l),d=await all(o,u.sql,...u.parameters),f=new Set(d.map(e=>e.publication_id)),p={ids:l.filter(e=>f.has(e))},m=e.archiveLikedIdsSchema.safeParse(p);if(!m.success)throw recordUnavailable();return t.json(200,m.data,t.noStore)}var c=i.s.object({clientKey:e.uuidSchema,payload:i.s.unknown()}),l=i.s.object({expectedRevision:i.s.number().int().min(1).max(2**53-1),payload:i.s.unknown()});async function resolveRetryRecord(e,n,r,i){if(r===null||r.expires_at<=Date.now())return null;if(r.payload_hash!==i)throw t.conflict(`That key was used for a different payload.`);if(r.publication_id===null)throw new t.ApiError(410,`gone`,`That publication was removed.`);let a=await entryById(e,n,r.publication_id);return a===null?`vanished`:t.json(200,a,t.noStore)}async function publishEntry(r,i,a,o,s){if(!o.publishingEnabled)throw new t.ApiError(503,`publishing-disabled`,`Publishing is currently disabled.`);let l=requirePublisherRow(s),u=await t.readJsonBody(i),d=c.safeParse(u);if(!d.success)throw t.invalid();let{clientKey:f}=d.data,p=await r.derive(d.data.payload);if(p===null)throw t.invalid();let m=p.canonicalJson,h=p.contentHash,g=l.id,_=r.name,v=await resolveRetryRecord(r,a,await first(a,n.SQL.request,g,_,f),h);if(v!==null&&v!==`vanished`)return v;let y=await first(a,n.SQL.publicationByKey,g,_,f);if(y!==null){if(y.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);if(y.content_hash!==h)throw t.conflict(`That key was used for a different payload.`);return t.json(200,rowToEntry(r,y),t.noStore)}let b=now(),x=Date.now()+e.ARCHIVE_LIMITS.idempotencyRetentionMs,S=JSON.stringify(p.tags);if(((await databaseCall(()=>a.DB.batch([a.DB.prepare(n.SQL.expireRequests).bind(Date.now()),a.DB.prepare(n.SQL.insertPublication).bind(uuid(),g,_,f,1,p.contentVersion,m,h,p.filter,p.title,S,b,b,g,_,e.ARCHIVE_LIMITS.maxPublications,g,_,f,Date.now()),a.DB.prepare(n.SQL.insertRequest).bind(g,_,f,h,b,x,g,_,f,h)])))[1]?.meta?.changes??0)===0){let e=await resolveRetryRecord(r,a,await first(a,n.SQL.request,g,_,f),h);if(e===`vanished`)throw new t.ApiError(410,`gone`,`That publication was removed.`);if(e!==null)return e;let i=await first(a,n.SQL.publicationByKey,g,_,f);if(i!==null&&i.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);if(i!==null&&i.content_hash===h)return t.json(200,rowToEntry(r,i),t.noStore);throw i===null?new t.ApiError(429,`quota`,`Publication quota reached.`):t.conflict(`That key was used for a different payload.`)}let C=await first(a,n.SQL.publicationByKey,g,_,f);if(C===null)throw t.conflict();return t.json(201,rowToEntry(r,C),t.noStore)}async function updateEntry(e,r,i,a,o,s){if(!a.publishingEnabled)throw new t.ApiError(503,`publishing-disabled`,`Publishing is currently disabled.`);let c=requirePublisherRow(o),u=await first(i,n.SQL.publicationAny,s,e.name);if(u===null||u.owner_id!==c.id)throw t.notFound();if(u.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);let d=await t.readJsonBody(r),f=l.safeParse(d);if(!f.success)throw t.invalid();let{expectedRevision:p}=f.data;if(p!==u.revision)throw t.conflict();let m=await e.derive(f.data.payload);if(m===null)throw t.invalid();let h=m.canonicalJson,g=m.contentHash;if(g===u.content_hash){let r=await first(i,n.SQL.publicationAny,s,e.name);if(r===null||r.owner_id!==c.id)throw t.notFound();if(r.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);if(r.revision!==p)throw t.conflict();return t.json(200,rowToEntry(e,r),t.noStore)}let _=JSON.stringify(m.tags);if(((await run(i,n.SQL.updatePublication,1,m.contentVersion,h,g,m.filter,m.title,_,now(),s,c.id,p,g)).meta?.changes??0)===0){let r=await first(i,n.SQL.publicationAny,s,e.name);throw r===null?t.notFound():r.hidden_at===null?t.conflict():t.forbidden(`This entry is under moderation review.`)}let v=await entryById(e,i,s);if(v===null)throw t.notFound();return t.json(200,v,t.noStore)}async function deleteEntry(e,r,i,a){if(i.operator){let t=requireOperatorRow(i);await databaseCall(()=>r.DB.batch([r.DB.prepare(n.SQL.auditRemoval).bind(uuid(),t.id,now(),a,e.name),r.DB.prepare(n.SQL.removePublicationAny).bind(a,e.name)]))}else{let o=requirePublisherRow(i),s=await first(r,n.SQL.publicationAny,a,e.name);if(s===null)return new Response(null,{headers:t.noStore,status:204});if(s.owner_id!==o.id)throw t.notFound();await run(r,n.SQL.removePublication,a,o.id)}return new Response(null,{headers:t.noStore,status:204})}async function setLike(r,i,a,o,s,c){let l=requirePublisherRow(o);if(c&&!a.likesEnabled)throw new t.ApiError(503,`likes-disabled`,`Likes are currently disabled.`);if(await first(i,n.SQL.publication,s,r.name)===null)throw t.notFound();c?await run(i,n.SQL.insertLike,l.id,now(),s):await run(i,n.SQL.removeLike,s,l.id);let u=await first(i,n.SQL.likeState,l.id,s);if(u===null)throw t.notFound();let d={id:u.id,likeCount:u.like_count,liked:u.liked===1},f=e.archiveLikeStateSchema.safeParse(d);if(!f.success)throw recordUnavailable();return t.json(200,f.data,t.noStore)}var u=i.s.object({reason:i.s.string().check(e.isWellFormedText).trim().min(1).max(e.ARCHIVE_LIMITS.reportReasonChars)});async function reportEntry(e,r,i,a,o){let s=requirePublisherRow(a),c=await t.readJsonBody(r),l=u.safeParse(c);if(!l.success)throw t.invalid();if(await first(i,n.SQL.publication,o,e.name)===null)throw t.notFound();return await run(i,n.SQL.insertReport,uuid(),s.id,l.data.reason,now(),o,e.name),new Response(null,{headers:t.noStore,status:204})}async function setHidden(e,r,i,a,o){let s=requireOperatorRow(i);if(await first(r,n.SQL.publicationAny,a,e.name)===null)throw t.notFound();let c=o?r.DB.prepare(n.SQL.hidePublication).bind(now(),a,e.name):r.DB.prepare(n.SQL.unhidePublication).bind(a,e.name);return await databaseCall(()=>r.DB.batch([r.DB.prepare(n.SQL.auditHide).bind(uuid(),s.id,o?`hide`:`unhide`,now(),a,e.name,Number(o)),c])),new Response(null,{headers:t.noStore,status:204})}async function listReports(i,a,o,s){requireOperatorRow(s);let c=new URL(a.url).searchParams.get(`status`)??`unresolved`;if(c!==`unresolved`&&c!==`resolved`)throw t.badRequest();let l=`reports:${i.name}:${c}`,{cursor:u,limit:d}=r.forwardPage(a,l,[`status`]),f=n.reportsPageSql({collection:i.name,resolved:c===`resolved`},{cursor:u,limit:d+1}),p=await all(o,f.sql,...f.parameters),m=p.slice(0,d),h=m.at(-1),g=e.archiveReportsSchema.safeParse({cursor:p.length>d&&h!==void 0?r.nextCursor({createdAt:h.created_at,id:h.id},l):null,reports:m.map(e=>({createdAt:e.created_at,entryId:e.publication_id,entryTitle:e.entry_title,id:e.id,reason:e.reason,reporter:{id:e.reporter_id,name:e.reporter_name},resolvedAt:e.resolved_at}))});if(!g.success)throw recordUnavailable();return t.json(200,g.data,t.noStore)}async function resolveReport(e,r,i,a){if(requireOperatorRow(i),await first(r,n.SQL.reportById,a,e.name)===null)throw t.notFound();return await run(r,n.SQL.resolveReport,now(),a,e.name),new Response(null,{headers:t.noStore,status:204})}async function myReports(i,a,o){let s=`own-reports:${o.publisher?.id??`unregistered`}`,{cursor:c,limit:l}=r.forwardPage(i,s);if(o.publisher===null)return t.json(200,{cursor:null,reports:[]},t.noStore);let u=n.reportsPageSql({publisherId:o.publisher.id},{cursor:c,limit:l+1}),d=await all(a,u.sql,...u.parameters),f=d.slice(0,l),p=f.at(-1),m=e.archiveOwnReportsSchema.safeParse({cursor:d.length>l&&p!==void 0?r.nextCursor({createdAt:p.created_at,id:p.id},s):null,reports:f.map(e=>({collection:e.collection,createdAt:e.created_at,entryId:e.publication_id,id:e.id,reason:e.reason,resolvedAt:e.resolved_at}))});if(!m.success)throw recordUnavailable();return t.json(200,m.data,t.noStore)}exports.collectionMeta=collectionMeta,exports.deleteAccountProfile=deleteAccountProfile,exports.deleteEntry=deleteEntry,exports.deleteProfile=deleteProfile,exports.erasePublisher=erasePublisher,exports.getEntry=getEntry,exports.getMe=getMe,exports.healthJson=healthJson,exports.listEntries=listEntries,exports.listModerationLog=listModerationLog,exports.listReports=listReports,exports.myLikes=myLikes,exports.myPublications=myPublications,exports.myReports=myReports,exports.publishEntry=publishEntry,exports.registerMe=registerMe,exports.renameMe=renameMe,exports.reportEntry=reportEntry,exports.requireOperatorRow=requireOperatorRow,exports.requirePublisherRow=requirePublisherRow,exports.resolveReport=resolveReport,exports.setHidden=setHidden,exports.setLike=setLike,exports.updateEntry=updateEntry;
|
|
1
|
+
const e=require("../contract.cjs"),t=require("./http.cjs"),n=require("./sql.cjs"),r=require("./_pagination.cjs");let i=require("@vielzeug/spell");async function databaseCall(e){try{return await e()}catch(e){throw new t.ApiError(500,`internal`,`The service is not available.`,{cause:e,diagnostic:{kind:`database`}})}}var first=(e,t,...n)=>databaseCall(()=>e.DB.prepare(t).bind(...n).first()),all=(e,t,...n)=>databaseCall(async()=>{let r=await e.DB.prepare(t).bind(...n).all();if(!Array.isArray(r.results))throw Error(`Missing database query results`);return r.results}),run=(e,t,...n)=>databaseCall(()=>e.DB.prepare(t).bind(...n).run()),uuid=()=>crypto.randomUUID(),now=()=>new Date().toISOString(),recordUnavailable=()=>new t.ApiError(503,`record-unavailable`,`The record is not available.`,{diagnostic:{kind:`record`}}),a=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,rowToEntry=(e,t)=>{let n;try{n=JSON.parse(t.payload_json)}catch{throw recordUnavailable()}let r=e.contract.entry.safeParse({author:{id:t.owner_id,name:t.author_name},contentHash:t.content_hash,contentVersion:t.content_version,id:t.id,likeCount:t.like_count,payload:n,publishedAt:t.published_at,revision:t.revision,schemaVersion:t.schema_version,updatedAt:t.updated_at});if(!r.success)throw recordUnavailable();return r.data},rowToOwnedEntry=(t,n)=>{let r=rowToEntry(t,n);if(!e.uuidSchema.safeParse(n.client_key).success)throw recordUnavailable();return{...r,clientKey:n.client_key,hidden:n.hidden_at!==null}},entryById=async(e,t,r)=>{let i=await first(t,n.SQL.publication,r,e.name);return i===null?null:rowToEntry(e,i)},searchMatchFor=n=>{if(n===null)return;if(n.length>e.ARCHIVE_LIMITS.searchChars)throw t.badRequest();let r=(n.match(/[\p{L}\p{N}]+/gu)??[]).slice(0,e.ARCHIVE_LIMITS.searchTerms);if(r.length!==0)return r.map(e=>`"${e}"*`).join(` `)},entryCursorContext=e=>JSON.stringify([e.collection,e.sort,e.author??``,e.filters,e.tags,e.searchMatch??``]),cursorFromRow=e=>({createdAt:e.published_at,id:e.id,likeCount:e.like_count}),o=16,vocabValues=(e,n,r)=>{let i=e.getAll(n);if(i.length>o)throw t.badRequest();let a=[...new Set(i)].sort();if(a.some(e=>!r.includes(e)))throw t.badRequest();return a};async function listEntries(i,o,s){let c=new URL(o.url),l=t.queryParams(c,[`author`,`cursor`,`filter`,`limit`,`search`,`sort`,`tag`],[`filter`,`tag`]),u=l.get(`sort`)??`recent`,d=e.archiveSortSchema.safeParse(u);if(!d.success)throw t.badRequest();let f=vocabValues(l,`filter`,i.filters),p=vocabValues(l,`tag`,i.tags),m=l.get(`author`);if(m!==null&&!a.test(m))throw t.badRequest();let h=searchMatchFor(l.get(`search`)),g=entryCursorContext({author:m??void 0,collection:i.name,filters:f,searchMatch:h,sort:d.data,tags:p}),{cursor:_,limit:v}=r.forwardPageParams(l,g),y={author:m??void 0,collection:i.name,cursor:_===null?void 0:{createdAt:_.createdAt,id:_.id,likeCount:_.likeCount??0},filters:f,limit:v+1,searchMatch:h,sort:d.data,tags:p},b=n.listPublicationsSql(y),x=await all(s,b.sql,...b.parameters),S=x.slice(0,v),C=S.at(-1),w={cursor:x.length>v&&C!==void 0?r.nextCursor(cursorFromRow(C),g):null,entries:S.map(e=>rowToEntry(i,e))};return t.json(200,w,t.noStore)}function collectionMeta(n){let r=e.archiveCollectionMetaSchema.safeParse({filters:[...n.filters].sort(),name:n.name,tags:[...n.tags].sort()});if(!r.success)throw recordUnavailable();return t.json(200,r.data,t.noStore)}async function getEntry(e,n,r){let i=await entryById(e,n,r);if(i===null)throw t.notFound();return t.json(200,i,t.noStore)}async function healthJson(r,i){let a=null,o=null;try{a=(await first(i,n.SQL.schemaVersion))?.applied??0}catch{o=`database-unavailable`}o===null&&a!==1&&(o=`schema-mismatch`);let s=e.archiveHealthSchema.safeParse({dbSchemaVersion:a,features:{likes:r.likesEnabled,publishing:r.publishingEnabled},limits:e.ARCHIVE_LIMITS,reason:o,schemaVersion:1,status:o===null?`ok`:`degraded`});if(!s.success)throw recordUnavailable();return t.json(o===null?200:503,s.data,t.noStore)}var identityJson=(n,r)=>{let i=e.archiveIdentitySchema.safeParse(r);if(!i.success)throw recordUnavailable();return t.json(n,i.data,t.noStore)};function getMe(e){return identityJson(200,e?.publisher?{id:e.publisher.id,name:e.publisher.displayName,operator:e.operator}:null)}async function registerMe(e,r,i){let a=await t.readJsonBody(e),o=s.safeParse(a);if(!o.success)throw t.invalid();let c=o.data.name??``,l=await first(r,n.SQL.publisher,i.subject.issuer,i.subject.subject);if(l!==null)return identityJson(200,{id:l.id,name:l.display_name,operator:i.operator});let u=uuid();await run(r,n.SQL.insertPublisher,u,i.subject.issuer,i.subject.subject,c,now(),i.subject.issuer,i.subject.subject,Date.now());let d=await first(r,n.SQL.publisher,i.subject.issuer,i.subject.subject);if(d===null)throw await first(r,n.SQL.revokedSubject,i.subject.issuer,i.subject.subject,Date.now())===null?t.conflict():new t.ApiError(401,`unauthenticated`,`Sign-in is required.`);return identityJson(201,{id:d.id,name:d.display_name,operator:i.operator})}async function renameMe(e,r,i){let a=requirePublisherRow(i),o=await t.readJsonBody(e),s=c.safeParse(o);if(!s.success)throw t.invalid();return s.data.name!==a.displayName&&await run(r,n.SQL.renamePublisher,s.data.name,a.id,s.data.name),identityJson(200,{id:a.id,name:s.data.name,operator:i.operator})}async function deleteProfile(e,t){return t.publisher!==null&&await run(e,n.SQL.deletePublisher,t.publisher.id),new Response(null,{headers:{"cache-control":`no-store`},status:204})}async function deleteAccountProfile(r,i){let{issuer:a,subject:o}=i.subject;return await databaseCall(()=>r.DB.batch([r.DB.prepare(n.SQL.revokeSubject).bind(a,o,Date.now()+(e.SESSION_MAX_AGE_SECONDS+10)*1e3),r.DB.prepare(n.SQL.deleteSubjectPublisher).bind(a,o)])),new Response(null,{headers:t.noStore,status:204})}async function erasePublisher(e,r,i){let a=requireOperatorRow(r);return await databaseCall(()=>e.DB.batch([e.DB.prepare(n.SQL.auditErasure).bind(uuid(),a.id,now(),i),e.DB.prepare(n.SQL.deletePublisher).bind(i)])),new Response(null,{headers:t.noStore,status:204})}async function listModerationLog(i,a,o){requireOperatorRow(o);let s=`moderation-log`,{cursor:c,limit:l}=r.forwardPage(i,s),u=n.moderationLogSql({cursor:c,limit:l+1}),d=await all(a,u.sql,...u.parameters),f=d.slice(0,l),p=f.at(-1),m=e.archiveModerationLogSchema.safeParse({actions:f.map(e=>({action:e.action,collection:e.collection,createdAt:e.created_at,id:e.id,operatorId:e.operator_id,targetId:e.target_id})),cursor:d.length>l&&p!==void 0?r.nextCursor({createdAt:p.created_at,id:p.id},s):null});if(!m.success)throw recordUnavailable();return t.json(200,m.data,t.noStore)}var requirePublisherRow=e=>{if(e.publisher===null)throw t.forbidden(`Register before publishing.`);return e.publisher},requireOperatorRow=e=>{if(!e.operator)throw t.forbidden(`Operator access is required.`);if(e.publisher===null)throw t.forbidden(`Register before moderating.`);return e.publisher},s=i.s.object({name:e.archiveAliasSchema.optional()}),c=i.s.object({name:e.archiveAliasSchema});async function myPublications(r,i,a){if(a.publisher===null)return t.json(200,{entries:[]},t.noStore);let o=await all(i,n.SQL.ownPublications,a.publisher.id,r.name,e.ARCHIVE_LIMITS.maxPublications);return t.json(200,{entries:o.map(e=>rowToOwnedEntry(r,e))},t.noStore)}async function myLikes(i,o,s){let c=new URL(i.url).searchParams.get(`ids`);if(c===null){let a=`likes:${s.publisher?.id??`unregistered`}`,{cursor:c,limit:l}=r.forwardPage(i,a);if(s.publisher===null)return t.json(200,{cursor:null,ids:[]},t.noStore);let u=n.likesPageSql(s.publisher.id,{cursor:c,limit:l+1}),d=await all(o,u.sql,...u.parameters),f=d.slice(0,l),p=f.at(-1),m=e.archiveLikesPageSchema.safeParse({cursor:d.length>l&&p!==void 0?r.nextCursor({createdAt:p.created_at,id:p.publication_id},a):null,ids:f.map(e=>e.publication_id)});if(!m.success)throw recordUnavailable();return t.json(200,m.data,t.noStore)}t.queryParams(new URL(i.url),[`ids`]);let l=[...new Set(c.split(`,`))];if(l.length>e.ARCHIVE_LIMITS.likeStateIds||l.some(e=>!a.test(e)))throw t.badRequest();if(s.publisher===null)return t.json(200,{ids:[]},t.noStore);let u=n.likedPublicationsSql(s.publisher.id,l),d=await all(o,u.sql,...u.parameters),f=new Set(d.map(e=>e.publication_id)),p={ids:l.filter(e=>f.has(e))},m=e.archiveLikedIdsSchema.safeParse(p);if(!m.success)throw recordUnavailable();return t.json(200,m.data,t.noStore)}var l=i.s.object({clientKey:e.uuidSchema,payload:i.s.unknown()}),u=i.s.object({expectedRevision:i.s.number().int().min(1).max(2**53-1),payload:i.s.unknown()});async function resolveRetryRecord(e,n,r,i){if(r===null||r.expires_at<=Date.now())return null;if(r.payload_hash!==i)throw t.conflict(`That key was used for a different payload.`);if(r.publication_id===null)throw new t.ApiError(410,`gone`,`That publication was removed.`);let a=await entryById(e,n,r.publication_id);return a===null?`vanished`:t.json(200,a,t.noStore)}async function publishEntry(r,i,a,o,s){if(!o.publishingEnabled)throw new t.ApiError(503,`publishing-disabled`,`Publishing is currently disabled.`);let c=requirePublisherRow(s),u=await t.readJsonBody(i),d=l.safeParse(u);if(!d.success)throw t.invalid();let{clientKey:f}=d.data,p=await r.derive(d.data.payload);if(p===null)throw t.invalid();let m=p.canonicalJson,h=p.contentHash,g=c.id,_=r.name,v=await resolveRetryRecord(r,a,await first(a,n.SQL.request,g,_,f),h);if(v!==null&&v!==`vanished`)return v;let y=await first(a,n.SQL.publicationByKey,g,_,f);if(y!==null){if(y.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);if(y.content_hash!==h)throw t.conflict(`That key was used for a different payload.`);return t.json(200,rowToEntry(r,y),t.noStore)}let b=now(),x=Date.now()+e.ARCHIVE_LIMITS.idempotencyRetentionMs,S=JSON.stringify(p.tags);if(((await databaseCall(()=>a.DB.batch([a.DB.prepare(n.SQL.expireRequests).bind(Date.now()),a.DB.prepare(n.SQL.insertPublication).bind(uuid(),g,_,f,1,p.contentVersion,m,h,p.filter,p.title,S,b,b,g,_,e.ARCHIVE_LIMITS.maxPublications,g,_,f,Date.now()),a.DB.prepare(n.SQL.insertRequest).bind(g,_,f,h,b,x,g,_,f,h)])))[1]?.meta?.changes??0)===0){let e=await resolveRetryRecord(r,a,await first(a,n.SQL.request,g,_,f),h);if(e===`vanished`)throw new t.ApiError(410,`gone`,`That publication was removed.`);if(e!==null)return e;let i=await first(a,n.SQL.publicationByKey,g,_,f);if(i!==null&&i.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);if(i!==null&&i.content_hash===h)return t.json(200,rowToEntry(r,i),t.noStore);throw i===null?new t.ApiError(429,`quota`,`Publication quota reached.`):t.conflict(`That key was used for a different payload.`)}let C=await first(a,n.SQL.publicationByKey,g,_,f);if(C===null)throw t.conflict();return t.json(201,rowToEntry(r,C),t.noStore)}async function updateEntry(e,r,i,a,o,s){if(!a.publishingEnabled)throw new t.ApiError(503,`publishing-disabled`,`Publishing is currently disabled.`);let c=requirePublisherRow(o),l=await first(i,n.SQL.publicationAny,s,e.name);if(l===null||l.owner_id!==c.id)throw t.notFound();if(l.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);let d=await t.readJsonBody(r),f=u.safeParse(d);if(!f.success)throw t.invalid();let{expectedRevision:p}=f.data;if(p!==l.revision)throw t.conflict();let m=await e.derive(f.data.payload);if(m===null)throw t.invalid();let h=m.canonicalJson,g=m.contentHash;if(g===l.content_hash){let r=await first(i,n.SQL.publicationAny,s,e.name);if(r===null||r.owner_id!==c.id)throw t.notFound();if(r.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);if(r.revision!==p)throw t.conflict();return t.json(200,rowToEntry(e,r),t.noStore)}let _=JSON.stringify(m.tags);if(((await run(i,n.SQL.updatePublication,1,m.contentVersion,h,g,m.filter,m.title,_,now(),s,c.id,p,g)).meta?.changes??0)===0){let r=await first(i,n.SQL.publicationAny,s,e.name);throw r===null?t.notFound():r.hidden_at===null?t.conflict():t.forbidden(`This entry is under moderation review.`)}let v=await entryById(e,i,s);if(v===null)throw t.notFound();return t.json(200,v,t.noStore)}async function deleteEntry(e,r,i,a){if(i.operator){let t=requireOperatorRow(i);await databaseCall(()=>r.DB.batch([r.DB.prepare(n.SQL.auditRemoval).bind(uuid(),t.id,now(),a,e.name),r.DB.prepare(n.SQL.removePublicationAny).bind(a,e.name)]))}else{let o=requirePublisherRow(i),s=await first(r,n.SQL.publicationAny,a,e.name);if(s===null)return new Response(null,{headers:t.noStore,status:204});if(s.owner_id!==o.id)throw t.notFound();await run(r,n.SQL.removePublication,a,o.id)}return new Response(null,{headers:t.noStore,status:204})}async function setLike(r,i,a,o,s,c){let l=requirePublisherRow(o);if(c&&!a.likesEnabled)throw new t.ApiError(503,`likes-disabled`,`Likes are currently disabled.`);if(await first(i,n.SQL.publication,s,r.name)===null)throw t.notFound();c?await run(i,n.SQL.insertLike,l.id,now(),s):await run(i,n.SQL.removeLike,s,l.id);let u=await first(i,n.SQL.likeState,l.id,s);if(u===null)throw t.notFound();let d={id:u.id,likeCount:u.like_count,liked:u.liked===1},f=e.archiveLikeStateSchema.safeParse(d);if(!f.success)throw recordUnavailable();return t.json(200,f.data,t.noStore)}var d=i.s.object({reason:i.s.string().check(e.isWellFormedText).trim().min(1).max(e.ARCHIVE_LIMITS.reportReasonChars)});async function reportEntry(e,r,i,a,o){let s=requirePublisherRow(a),c=await t.readJsonBody(r),l=d.safeParse(c);if(!l.success)throw t.invalid();if(await first(i,n.SQL.publication,o,e.name)===null)throw t.notFound();return await run(i,n.SQL.insertReport,uuid(),s.id,l.data.reason,now(),o,e.name),new Response(null,{headers:t.noStore,status:204})}async function setHidden(e,r,i,a,o){let s=requireOperatorRow(i);if(await first(r,n.SQL.publicationAny,a,e.name)===null)throw t.notFound();let c=o?r.DB.prepare(n.SQL.hidePublication).bind(now(),a,e.name):r.DB.prepare(n.SQL.unhidePublication).bind(a,e.name);return await databaseCall(()=>r.DB.batch([r.DB.prepare(n.SQL.auditHide).bind(uuid(),s.id,o?`hide`:`unhide`,now(),a,e.name,Number(o)),c])),new Response(null,{headers:t.noStore,status:204})}async function listReports(i,a,o,s){requireOperatorRow(s);let c=new URL(a.url).searchParams.get(`status`)??`unresolved`;if(c!==`unresolved`&&c!==`resolved`)throw t.badRequest();let l=`reports:${i.name}:${c}`,{cursor:u,limit:d}=r.forwardPage(a,l,[`status`]),f=n.reportsPageSql({collection:i.name,resolved:c===`resolved`},{cursor:u,limit:d+1}),p=await all(o,f.sql,...f.parameters),m=p.slice(0,d),h=m.at(-1),g=e.archiveReportsSchema.safeParse({cursor:p.length>d&&h!==void 0?r.nextCursor({createdAt:h.created_at,id:h.id},l):null,reports:m.map(e=>({createdAt:e.created_at,entryId:e.publication_id,entryTitle:e.entry_title,id:e.id,reason:e.reason,reporter:{id:e.reporter_id,name:e.reporter_name},resolvedAt:e.resolved_at}))});if(!g.success)throw recordUnavailable();return t.json(200,g.data,t.noStore)}async function resolveReport(e,r,i,a){if(requireOperatorRow(i),await first(r,n.SQL.reportById,a,e.name)===null)throw t.notFound();return await run(r,n.SQL.resolveReport,now(),a,e.name),new Response(null,{headers:t.noStore,status:204})}async function myReports(i,a,o){let s=`own-reports:${o.publisher?.id??`unregistered`}`,{cursor:c,limit:l}=r.forwardPage(i,s);if(o.publisher===null)return t.json(200,{cursor:null,reports:[]},t.noStore);let u=n.reportsPageSql({publisherId:o.publisher.id},{cursor:c,limit:l+1}),d=await all(a,u.sql,...u.parameters),f=d.slice(0,l),p=f.at(-1),m=e.archiveOwnReportsSchema.safeParse({cursor:d.length>l&&p!==void 0?r.nextCursor({createdAt:p.created_at,id:p.id},s):null,reports:f.map(e=>({collection:e.collection,createdAt:e.created_at,entryId:e.publication_id,id:e.id,reason:e.reason,resolvedAt:e.resolved_at}))});if(!m.success)throw recordUnavailable();return t.json(200,m.data,t.noStore)}exports.collectionMeta=collectionMeta,exports.deleteAccountProfile=deleteAccountProfile,exports.deleteEntry=deleteEntry,exports.deleteProfile=deleteProfile,exports.erasePublisher=erasePublisher,exports.getEntry=getEntry,exports.getMe=getMe,exports.healthJson=healthJson,exports.listEntries=listEntries,exports.listModerationLog=listModerationLog,exports.listReports=listReports,exports.myLikes=myLikes,exports.myPublications=myPublications,exports.myReports=myReports,exports.publishEntry=publishEntry,exports.registerMe=registerMe,exports.renameMe=renameMe,exports.reportEntry=reportEntry,exports.requireOperatorRow=requireOperatorRow,exports.requirePublisherRow=requirePublisherRow,exports.resolveReport=resolveReport,exports.setHidden=setHidden,exports.setLike=setLike,exports.updateEntry=updateEntry;
|
|
2
2
|
//# sourceMappingURL=publications.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"publications.cjs","names":[],"sources":["../../src/worker/publications.ts"],"sourcesContent":["import { s } from '@vielzeug/spell';\nimport type { ArchiveCollection } from '../collection';\nimport {\n ARCHIVE_CONTRACT_VERSION,\n ARCHIVE_LIMITS,\n ARCHIVE_SCHEMA_VERSION,\n type ArchiveModerationAction,\n archiveAliasSchema,\n archiveCollectionMetaSchema,\n archiveHealthSchema,\n archiveIdentitySchema,\n archiveLikedIdsSchema,\n archiveLikeStateSchema,\n archiveLikesPageSchema,\n archiveModerationLogSchema,\n archiveOwnReportsSchema,\n archiveReportsSchema,\n archiveSortSchema,\n isWellFormedText,\n SESSION_MAX_AGE_SECONDS,\n uuidSchema,\n} from '../contract';\nimport { forwardPage, forwardPageParams, nextCursor } from './_pagination';\nimport { JWT_CLOCK_TOLERANCE_SECONDS, type Publisher, type Session } from './auth';\nimport type { ApiConfig, ArchiveWorkerEnv } from './env';\nimport {\n ApiError,\n badRequest,\n conflict,\n forbidden,\n invalid,\n json,\n noStore,\n notFound,\n queryParams,\n readJsonBody,\n} from './http';\nimport {\n likedPublicationsSql,\n likesPageSql,\n listPublicationsSql,\n moderationLogSql,\n type PublicationCursor,\n type PublicationFilter,\n reportsPageSql,\n SQL,\n} from './sql';\n\n/**\n * Route handlers for /api/v1, parameterized by the registered collection. All SQL lives\n * in sql.ts and is bound positionally here in the authored order; the DTO boundary\n * re-validates every stored row before it leaves, content validation lives in the\n * collection's derive step, and moderation state (hidden entries) is visible only\n * through the owner and operator surfaces.\n */\n\ninterface PublicationRow {\n author_name: string;\n client_key: string;\n collection: string;\n content_hash: string;\n content_version: number;\n filter_value: string | null;\n /** Null while visible; set while an operator has removed the entry from public view. */\n hidden_at: string | null;\n id: string;\n like_count: number;\n owner_id: string;\n payload_json: string;\n published_at: string;\n revision: number;\n schema_version: number;\n tags_json: string;\n title: string;\n updated_at: string;\n}\n\nasync function databaseCall<T>(operation: () => Promise<T>): Promise<T> {\n try {\n return await operation();\n } catch (cause) {\n throw new ApiError(500, 'internal', 'The service is not available.', { cause, diagnostic: { kind: 'database' } });\n }\n}\nconst first = <T>(env: ArchiveWorkerEnv, sql: string, ...params: (string | number | null)[]) =>\n databaseCall(() =>\n env.DB.prepare(sql)\n .bind(...params)\n .first<T>(),\n );\nconst all = <T>(env: ArchiveWorkerEnv, sql: string, ...params: (string | number | null)[]) =>\n databaseCall(async () => {\n const result = await env.DB.prepare(sql)\n .bind(...params)\n .all<T>();\n if (!Array.isArray(result.results)) throw new Error('Missing database query results');\n return result.results;\n });\nconst run = (env: ArchiveWorkerEnv, sql: string, ...params: (string | number | null)[]) =>\n databaseCall(() =>\n env.DB.prepare(sql)\n .bind(...params)\n .run(),\n );\n\nconst uuid = (): string => crypto.randomUUID();\nconst now = (): string => new Date().toISOString();\n\n/** A stored row that fails the emitted-DTO contract is unavailable, not malformed data. */\nconst recordUnavailable = (): ApiError =>\n new ApiError(503, 'record-unavailable', 'The record is not available.', { diagnostic: { kind: 'record' } });\n\nconst UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;\n\n/**\n * Maps a joined row to the public DTO through the collection's strict entry schema:\n * stored JSON is parsed and re-validated, stored versions are emitted as-is, and any\n * drift — unknown fields, corrupt payload, mismatched versions — surfaces as 503\n * `record-unavailable` rather than leaking or fabricating data.\n */\nconst rowToEntry = (collection: ArchiveCollection, row: PublicationRow) => {\n let payloadJson: unknown;\n try {\n payloadJson = JSON.parse(row.payload_json);\n } catch {\n throw recordUnavailable();\n }\n const parsed = collection.contract.entry.safeParse({\n author: { id: row.owner_id, name: row.author_name },\n contentHash: row.content_hash,\n contentVersion: row.content_version,\n id: row.id,\n likeCount: row.like_count,\n payload: payloadJson,\n publishedAt: row.published_at,\n revision: row.revision,\n schemaVersion: row.schema_version,\n updatedAt: row.updated_at,\n });\n if (!parsed.success) throw recordUnavailable();\n return parsed.data;\n};\n\nconst rowToOwnedEntry = (collection: ArchiveCollection, row: PublicationRow) => {\n const entry = rowToEntry(collection, row);\n // The private owner DTO adds the create retry key and the moderation state;\n // public routes never emit either.\n const key = uuidSchema.safeParse(row.client_key);\n if (!key.success) throw recordUnavailable();\n return { ...entry, clientKey: row.client_key, hidden: row.hidden_at !== null };\n};\n\nconst entryById = async (collection: ArchiveCollection, env: ArchiveWorkerEnv, id: string) => {\n const row = await first<PublicationRow>(env, SQL.publication, id, collection.name);\n return row === null ? null : rowToEntry(collection, row);\n};\n\n/* ----------------------------------- list --------------------------------- */\n\n/** Normalizes free text into the bounded FTS prefix-match expression the SQL expects. */\nconst searchMatchFor = (search: string | null): string | undefined => {\n if (search === null) return undefined;\n if (search.length > ARCHIVE_LIMITS.searchChars) throw badRequest();\n // Unicode word extraction: punctuation splits terms rather than poisoning them,\n // so \"Hunter's\" and \"fire-ice\" match the words their indexed titles carry.\n const terms = (search.match(/[\\p{L}\\p{N}]+/gu) ?? []).slice(0, ARCHIVE_LIMITS.searchTerms);\n if (terms.length === 0) return undefined;\n return terms.map((t) => `\"${t}\"*`).join(' ');\n};\n\n/**\n * The entry-list cursor context: every query dimension the page's boundary depends\n * on, so a cursor minted under one filter/sort/search and replayed under another is\n * a 400 — the same forward-cursor guard every other list in the package uses.\n */\nconst entryCursorContext = (query: {\n author?: string;\n collection: string;\n filter?: string;\n searchMatch?: string;\n sort: 'liked' | 'recent';\n tag?: string;\n}): string =>\n JSON.stringify([\n query.collection,\n query.sort,\n query.author ?? '',\n query.filter ?? '',\n query.tag ?? '',\n query.searchMatch ?? '',\n ]);\n\nconst cursorFromRow = (row: PublicationRow): PublicationCursor => ({\n createdAt: row.published_at,\n id: row.id,\n likeCount: row.like_count,\n});\n\nexport async function listEntries(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n): Promise<Response> {\n const url = new URL(request.url);\n const params = queryParams(url, ['author', 'cursor', 'filter', 'limit', 'search', 'sort', 'tag']);\n\n const sortRaw = params.get('sort') ?? 'recent';\n const sortParsed = archiveSortSchema.safeParse(sortRaw);\n if (!sortParsed.success) throw badRequest();\n\n const filter = params.get('filter');\n if (filter !== null && !collection.filters.includes(filter)) throw badRequest();\n const tag = params.get('tag');\n if (tag !== null && !collection.tags.includes(tag)) throw badRequest();\n const author = params.get('author');\n if (author !== null && !UUID_RE.test(author)) throw badRequest();\n\n const searchMatch = searchMatchFor(params.get('search'));\n const context = entryCursorContext({\n author: author ?? undefined,\n collection: collection.name,\n filter: filter ?? undefined,\n searchMatch,\n sort: sortParsed.data,\n tag: tag ?? undefined,\n });\n const { cursor, limit } = forwardPageParams(params, context);\n\n const query: PublicationFilter = {\n author: author ?? undefined,\n collection: collection.name,\n cursor:\n cursor === null ? undefined : { createdAt: cursor.createdAt, id: cursor.id, likeCount: cursor.likeCount ?? 0 },\n filter: filter ?? undefined,\n limit: limit + 1,\n searchMatch,\n sort: sortParsed.data,\n tag: tag ?? undefined,\n };\n\n // One bounded query — never a count: the over-read row is the next page's proof.\n const bound = listPublicationsSql(query);\n const fetched = await all<PublicationRow>(env, bound.sql, ...bound.parameters);\n const page = fetched.slice(0, limit);\n const last = page.at(-1);\n\n // Entries are validated per row through the collection's entry schema; the cursor\n // is internally generated, so the page composes validated parts directly.\n const pageDto = {\n cursor: fetched.length > limit && last !== undefined ? nextCursor(cursorFromRow(last), context) : null,\n entries: page.map((row) => rowToEntry(collection, row)),\n };\n return json(200, pageDto, noStore);\n}\n\n/** The collection's public self-description: name plus the declared query vocabularies. */\nexport function collectionMeta(collection: ArchiveCollection): Response {\n const parsed = archiveCollectionMetaSchema.safeParse({\n filters: [...collection.filters].sort(),\n name: collection.name,\n tags: [...collection.tags].sort(),\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n}\n\nexport async function getEntry(collection: ArchiveCollection, env: ArchiveWorkerEnv, id: string): Promise<Response> {\n const entry = await entryById(collection, env, id);\n if (entry === null) throw notFound();\n return json(200, entry, noStore);\n}\n\n/* ---------------------------------- health --------------------------------- */\n\n/**\n * Service health: the feature flags, the wire limits, and the storage schema stamp\n * actually present in the database. A database whose migrations lag the package\n * answers `degraded` instead of pretending to be fine — the most likely deployment\n * fault becomes a one-glance diagnosis.\n */\nexport async function healthJson(config: ApiConfig, env: ArchiveWorkerEnv): Promise<Response> {\n let dbSchemaVersion: number | null = null;\n let reason: 'database-unavailable' | 'schema-mismatch' | null = null;\n try {\n const row = await first<{ applied: number }>(env, SQL.schemaVersion);\n dbSchemaVersion = row?.applied ?? 0;\n } catch {\n reason = 'database-unavailable';\n }\n if (reason === null && dbSchemaVersion !== ARCHIVE_SCHEMA_VERSION) reason = 'schema-mismatch';\n const parsed = archiveHealthSchema.safeParse({\n dbSchemaVersion,\n features: { likes: config.likesEnabled, publishing: config.publishingEnabled },\n limits: ARCHIVE_LIMITS,\n reason,\n schemaVersion: ARCHIVE_CONTRACT_VERSION,\n status: reason === null ? 'ok' : 'degraded',\n });\n if (!parsed.success) throw recordUnavailable();\n return json(reason === null ? 200 : 503, parsed.data, noStore);\n}\n\n/* --------------------------------- identity -------------------------------- */\n\n/** Emits the private identity DTO through the strict shared schema. */\nconst identityJson = (status: number, identity: unknown): Response => {\n const parsed = archiveIdentitySchema.safeParse(identity);\n if (!parsed.success) throw recordUnavailable();\n return json(status, parsed.data, noStore);\n};\n\nexport function getMe(session: Session | null): Response {\n return identityJson(\n 200,\n session?.publisher\n ? { id: session.publisher.id, name: session.publisher.displayName, operator: session.operator }\n : null,\n );\n}\n\nexport async function registerMe(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const body = await readJsonBody(request);\n const parsed = registerBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n const name = parsed.data.name ?? '';\n\n const existing = await first<{ display_name: string; id: string }>(\n env,\n SQL.publisher,\n session.subject.issuer,\n session.subject.subject,\n );\n if (existing !== null) {\n return identityJson(200, { id: existing.id, name: existing.display_name, operator: session.operator });\n }\n const id = uuid();\n await run(\n env,\n SQL.insertPublisher,\n id,\n session.subject.issuer,\n session.subject.subject,\n name,\n now(),\n session.subject.issuer,\n session.subject.subject,\n Date.now(),\n );\n const row = await first<{ display_name: string; id: string }>(\n env,\n SQL.publisher,\n session.subject.issuer,\n session.subject.subject,\n );\n if (row === null) {\n if ((await first(env, SQL.revokedSubject, session.subject.issuer, session.subject.subject, Date.now())) !== null) {\n throw new ApiError(401, 'unauthenticated', 'Sign-in is required.');\n }\n throw conflict();\n }\n return identityJson(201, { id: row.id, name: row.display_name, operator: session.operator });\n}\n\nexport async function renameMe(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const publisher = requirePublisherRow(session);\n const body = await readJsonBody(request);\n const parsed = renameBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n if (parsed.data.name !== publisher.displayName) {\n await run(env, SQL.renamePublisher, parsed.data.name, publisher.id, parsed.data.name);\n }\n return identityJson(200, { id: publisher.id, name: parsed.data.name, operator: session.operator });\n}\n\n/**\n * Archive-profile erasure, independent of the identity provider's account.\n * The credential survives so an issuer can retry coordinated account erasure.\n */\nexport async function deleteProfile(env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n if (session.publisher !== null) await run(env, SQL.deletePublisher, session.publisher.id);\n return new Response(null, {\n headers: {\n 'cache-control': 'no-store',\n },\n status: 204,\n });\n}\n\n/** Revocation and erasure share a transaction, preventing a stale session from recreating the profile. */\nexport async function deleteAccountProfile(env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const { issuer, subject } = session.subject;\n // Issuance may be one tolerance ahead; expiry verification permits another.\n await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.revokeSubject).bind(\n issuer,\n subject,\n Date.now() + (SESSION_MAX_AGE_SECONDS + 2 * JWT_CLOCK_TOLERANCE_SECONDS) * 1000,\n ),\n env.DB.prepare(SQL.deleteSubjectPublisher).bind(issuer, subject),\n ]),\n );\n return new Response(null, { headers: noStore, status: 204 });\n}\n\n/**\n * Operator-side GDPR erasure: removes any publisher's row; FK cascades take its\n * publications, likes and retry keys. Idempotent for the same reason the self-erase\n * is: an already-erased publisher is a 204 with nothing left to remove.\n */\nexport async function erasePublisher(env: ArchiveWorkerEnv, session: Session, publisherId: string): Promise<Response> {\n const operator = requireOperatorRow(session);\n // The terminal moderation action: erasing a publisher removes their row, and FK\n // cascades take the publications, likes and retry keys with it — the same cascade a\n // self-erase walks, applied to any publisher the operator names. The guarded audit\n // and deletion share a transaction, so retries never record fictitious actions.\n await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.auditErasure).bind(uuid(), operator.id, now(), publisherId),\n env.DB.prepare(SQL.deletePublisher).bind(publisherId),\n ]),\n );\n return new Response(null, { headers: noStore, status: 204 });\n}\n\n/** The operator's accountability log: newest actions first, bounded, ids only. */\nexport async function listModerationLog(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n requireOperatorRow(session);\n const context = 'moderation-log';\n const { cursor, limit } = forwardPage(request, context);\n const bound = moderationLogSql({ cursor, limit: limit + 1 });\n const rows = await all<{\n id: string;\n action: ArchiveModerationAction;\n collection: string | null;\n created_at: string;\n operator_id: string;\n target_id: string;\n }>(env, bound.sql, ...bound.parameters);\n const page = rows.slice(0, limit);\n const last = page.at(-1);\n const parsed = archiveModerationLogSchema.safeParse({\n actions: page.map((row) => ({\n action: row.action,\n collection: row.collection,\n createdAt: row.created_at,\n id: row.id,\n operatorId: row.operator_id,\n targetId: row.target_id,\n })),\n cursor:\n rows.length > limit && last !== undefined\n ? nextCursor({ createdAt: last.created_at, id: last.id }, context)\n : null,\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n}\n\nexport const requirePublisherRow = (session: Session): Publisher => {\n if (session.publisher === null) throw forbidden('Register before publishing.');\n return session.publisher;\n};\n\n/** Operator authority plus a registered publisher row: moderation routes need both,\n * the role claim for authority and the row for attribution. */\nexport const requireOperatorRow = (session: Session): Publisher => {\n if (!session.operator) throw forbidden('Operator access is required.');\n if (session.publisher === null) throw forbidden('Register before moderating.');\n return session.publisher;\n};\n\n// Private identity payloads share the one alias rule with the emitted DTOs.\nconst registerBodySchema = s.object({ name: archiveAliasSchema.optional() });\nconst renameBodySchema = s.object({ name: archiveAliasSchema });\n\nexport async function myPublications(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n session: Session,\n): Promise<Response> {\n if (session.publisher === null) return json(200, { entries: [] }, noStore);\n const rows = await all<PublicationRow>(\n env,\n SQL.ownPublications,\n session.publisher.id,\n collection.name,\n ARCHIVE_LIMITS.maxPublications,\n );\n // The private owner DTO adds the create retry key; public routes never emit it.\n return json(200, { entries: rows.map((row) => rowToOwnedEntry(collection, row)) }, noStore);\n}\n\nexport async function myLikes(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const params = new URL(request.url).searchParams;\n const raw = params.get('ids');\n if (raw === null) {\n const context = `likes:${session.publisher?.id ?? 'unregistered'}`;\n const { cursor, limit } = forwardPage(request, context);\n if (session.publisher === null) return json(200, { cursor: null, ids: [] }, noStore);\n const bound = likesPageSql(session.publisher.id, { cursor, limit: limit + 1 });\n const rows = await all<{ created_at: string; publication_id: string }>(env, bound.sql, ...bound.parameters);\n const page = rows.slice(0, limit);\n const last = page.at(-1);\n const parsed = archiveLikesPageSchema.safeParse({\n cursor:\n rows.length > limit && last !== undefined\n ? nextCursor({ createdAt: last.created_at, id: last.publication_id }, context)\n : null,\n ids: page.map((row) => row.publication_id),\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n }\n queryParams(new URL(request.url), ['ids']);\n const ids = [...new Set(raw.split(','))];\n if (ids.length > ARCHIVE_LIMITS.likeStateIds || ids.some((id) => !UUID_RE.test(id))) throw badRequest();\n if (session.publisher === null) return json(200, { ids: [] }, noStore);\n const bound = likedPublicationsSql(session.publisher.id, ids);\n const rows = await all<{ publication_id: string }>(env, bound.sql, ...bound.parameters);\n const liked = new Set(rows.map((r) => r.publication_id));\n const dto = { ids: ids.filter((id) => liked.has(id)) };\n const validated = archiveLikedIdsSchema.safeParse(dto);\n if (!validated.success) throw recordUnavailable();\n return json(200, validated.data, noStore);\n}\n\n/* --------------------------------- mutations ------------------------------- */\n\n// The core validates the request envelope; the collection's derive step validates the payload.\nconst publishBodySchema = s.object({ clientKey: uuidSchema, payload: s.unknown() });\nconst updateBodySchema = s.object({\n expectedRevision: s.number().int().min(1).max(Number.MAX_SAFE_INTEGER),\n payload: s.unknown(),\n});\n\ninterface RetryRecord {\n expires_at: number;\n payload_hash: string;\n publication_id: string | null;\n}\n\n/**\n * The one replay/conflict/gone decision for a create retry key, shared by the\n * pre-read and the post-insert race re-read: a live record for a different payload\n * is a 409, a tombstone (null publication id) is a 410, and a live record for the\n * same payload replays the entry. `'vanished'` means the recorded publication is\n * no longer readable — hidden or raced-deleted — and only the caller knows which:\n * the pre-read falls through to the publication-by-key lookup (a hidden entry is\n * 403 there), the post-insert re-read answers 410.\n */\nasync function resolveRetryRecord(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n record: RetryRecord | null,\n hash: string,\n): Promise<Response | 'vanished' | null> {\n if (record === null || record.expires_at <= Date.now()) return null;\n if (record.payload_hash !== hash) throw conflict('That key was used for a different payload.');\n if (record.publication_id === null) throw new ApiError(410, 'gone', 'That publication was removed.');\n const entry = await entryById(collection, env, record.publication_id);\n if (entry === null) return 'vanished';\n return json(200, entry, noStore);\n}\n\nexport async function publishEntry(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n config: ApiConfig,\n session: Session,\n): Promise<Response> {\n if (!config.publishingEnabled) throw new ApiError(503, 'publishing-disabled', 'Publishing is currently disabled.');\n const publisher = requirePublisherRow(session);\n const body = await readJsonBody(request);\n const parsed = publishBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n const { clientKey } = parsed.data;\n // The derive step owns every content rejection: schema, vocabulary, title, byte bound.\n const derivation = await collection.derive(parsed.data.payload);\n if (derivation === null) throw invalid();\n\n const payloadJson = derivation.canonicalJson;\n const hash = derivation.contentHash;\n const owner = publisher.id;\n const name = collection.name;\n\n // Retry-key inspection first: the request record decides replay vs conflict vs gone.\n // A logically expired record is ignored entirely — retention is enforced by time,\n // not just by the periodic sweep.\n const retry = await first<RetryRecord>(env, SQL.request, owner, name, clientKey);\n const replay = await resolveRetryRecord(collection, env, retry, hash);\n if (replay !== null && replay !== 'vanished') return replay;\n\n const existing = await first<PublicationRow>(env, SQL.publicationByKey, owner, name, clientKey);\n if (existing !== null) {\n // A hidden entry is frozen for its owner too: replaying the create key must not\n // read as a live publication.\n if (existing.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n if (existing.content_hash !== hash) throw conflict('That key was used for a different payload.');\n return json(200, rowToEntry(collection, existing), noStore);\n }\n\n const timestamp = now();\n const expiresAt = Date.now() + ARCHIVE_LIMITS.idempotencyRetentionMs;\n const tagsJson = JSON.stringify(derivation.tags);\n const results = await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.expireRequests).bind(Date.now()),\n env.DB.prepare(SQL.insertPublication).bind(\n uuid(),\n owner,\n name,\n clientKey,\n ARCHIVE_CONTRACT_VERSION,\n derivation.contentVersion,\n payloadJson,\n hash,\n derivation.filter,\n derivation.title,\n tagsJson,\n timestamp,\n timestamp,\n owner,\n name,\n ARCHIVE_LIMITS.maxPublications,\n owner,\n name,\n clientKey,\n Date.now(),\n ),\n env.DB.prepare(SQL.insertRequest).bind(\n owner,\n name,\n clientKey,\n hash,\n timestamp,\n expiresAt,\n owner,\n name,\n clientKey,\n hash,\n ),\n ]),\n );\n if ((results[1]?.meta?.changes ?? 0) === 0) {\n // The guarded insert was skipped: a race already wrote the key, the key's\n // publication was removed, or the owner's live-publication quota is full.\n // Re-reading the request record first distinguishes a tombstone from a race.\n const racedRecord = await first<RetryRecord>(env, SQL.request, owner, name, clientKey);\n const racedReplay = await resolveRetryRecord(collection, env, racedRecord, hash);\n if (racedReplay === 'vanished') throw new ApiError(410, 'gone', 'That publication was removed.');\n if (racedReplay !== null) return racedReplay;\n const raced = await first<PublicationRow>(env, SQL.publicationByKey, owner, name, clientKey);\n if (raced !== null && raced.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n if (raced !== null && raced.content_hash === hash) return json(200, rowToEntry(collection, raced), noStore);\n if (raced !== null) throw conflict('That key was used for a different payload.');\n throw new ApiError(429, 'quota', 'Publication quota reached.');\n }\n\n const created = await first<PublicationRow>(env, SQL.publicationByKey, owner, name, clientKey);\n if (created === null) throw conflict();\n return json(201, rowToEntry(collection, created), noStore);\n}\n\nexport async function updateEntry(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n config: ApiConfig,\n session: Session,\n id: string,\n): Promise<Response> {\n if (!config.publishingEnabled) throw new ApiError(503, 'publishing-disabled', 'Publishing is currently disabled.');\n const publisher = requirePublisherRow(session);\n // The owner's row regardless of moderation state: the author must be told their\n // entry is frozen, not that it vanished.\n const row = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n // Replacing an entry is the author's alone; anyone else sees the entry as absent.\n if (row === null || row.owner_id !== publisher.id) throw notFound();\n // Hidden entries are frozen: an edit must not bypass the operator's decision.\n if (row.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n\n const body = await readJsonBody(request);\n const parsed = updateBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n const { expectedRevision } = parsed.data;\n\n if (expectedRevision !== row.revision) throw conflict();\n\n const derivation = await collection.derive(parsed.data.payload);\n if (derivation === null) throw invalid();\n const payloadJson = derivation.canonicalJson;\n const hash = derivation.contentHash;\n if (hash === row.content_hash) {\n const current = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n if (current === null || current.owner_id !== publisher.id) throw notFound();\n if (current.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n if (current.revision !== expectedRevision) throw conflict();\n return json(200, rowToEntry(collection, current), noStore);\n }\n\n const tagsJson = JSON.stringify(derivation.tags);\n const result = await run(\n env,\n SQL.updatePublication,\n ARCHIVE_CONTRACT_VERSION,\n derivation.contentVersion,\n payloadJson,\n hash,\n derivation.filter,\n derivation.title,\n tagsJson,\n now(),\n id,\n publisher.id,\n expectedRevision,\n hash,\n );\n if ((result.meta?.changes ?? 0) === 0) {\n const current = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n if (current === null) throw notFound();\n if (current.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n throw conflict();\n }\n const updated = await entryById(collection, env, id);\n if (updated === null) throw notFound();\n return json(200, updated, noStore);\n}\n\nexport async function deleteEntry(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n session: Session,\n id: string,\n): Promise<Response> {\n // The owner's row regardless of moderation state: the author may still remove a\n // hidden entry (their content, their call) — hiding freezes edits, not removal.\n // Removing an entry is the author's alone; an operator may remove ANY entry — the\n // terminal moderation action for abusive content. The operator path carries the\n // same registered-publisher-row requirement every moderation route shares: one\n // accountable identity per action. Everyone else sees the entry as absent.\n if (session.operator) {\n const operator = requireOperatorRow(session);\n await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.auditRemoval).bind(uuid(), operator.id, now(), id, collection.name),\n env.DB.prepare(SQL.removePublicationAny).bind(id, collection.name),\n ]),\n );\n } else {\n const publisher = requirePublisherRow(session);\n const row = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n if (row === null) return new Response(null, { headers: noStore, status: 204 });\n if (row.owner_id !== publisher.id) throw notFound();\n await run(env, SQL.removePublication, id, publisher.id);\n }\n return new Response(null, { headers: noStore, status: 204 });\n}\n\nexport async function setLike(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n config: ApiConfig,\n session: Session,\n id: string,\n liked: boolean,\n): Promise<Response> {\n const publisher = requirePublisherRow(session);\n if (liked && !config.likesEnabled) throw new ApiError(503, 'likes-disabled', 'Likes are currently disabled.');\n const row = await first<PublicationRow>(env, SQL.publication, id, collection.name);\n if (row === null) throw notFound();\n if (liked) {\n await run(env, SQL.insertLike, publisher.id, now(), id);\n } else {\n await run(env, SQL.removeLike, id, publisher.id);\n }\n // One consistent snapshot: the count and the viewer's flag read together.\n const state = await first<{ id: string; like_count: number; liked: number }>(env, SQL.likeState, publisher.id, id);\n if (state === null) throw notFound();\n const dto = { id: state.id, likeCount: state.like_count, liked: state.liked === 1 };\n const validated = archiveLikeStateSchema.safeParse(dto);\n if (!validated.success) throw recordUnavailable();\n return json(200, validated.data, noStore);\n}\n\n/* -------------------------------- moderation ------------------------------- */\n\n// One free-text report reason: trimmed, non-empty, well-formed, bounded.\nconst reportBodySchema = s.object({\n reason: s.string().check(isWellFormedText).trim().min(1).max(ARCHIVE_LIMITS.reportReasonChars),\n});\n\n/**\n * A reader's abuse report against one visible entry. Idempotent per (entry, reporter):\n * reporting again is the same 204, never a duplicate row. Hidden or missing entries\n * are 404 — they cannot be reported, they are already actioned or gone.\n */\nexport async function reportEntry(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n session: Session,\n id: string,\n): Promise<Response> {\n const reporter = requirePublisherRow(session);\n const body = await readJsonBody(request);\n const parsed = reportBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n const row = await first<PublicationRow>(env, SQL.publication, id, collection.name);\n if (row === null) throw notFound();\n // Bound in the SQL's authored placeholder order: reporter, reason, created_at,\n // then the WHERE's entry id and collection.\n await run(env, SQL.insertReport, uuid(), reporter.id, parsed.data.reason, now(), id, collection.name);\n return new Response(null, { headers: noStore, status: 204 });\n}\n\n/**\n * The operator soft-hide: removes an entry from every public surface while keeping\n * the row, its likes and its retry key intact. Idempotent — already in the requested\n * state answers the same 204. Operator authority is the session's verified role\n * claim; a registered publisher row is still required (one accountable identity).\n */\nexport async function setHidden(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n session: Session,\n id: string,\n hidden: boolean,\n): Promise<Response> {\n const operator = requireOperatorRow(session);\n // Any collection row, visible or hidden: the operator acts on the storage row. The\n // pre-read establishes existence only. The transactional audit guard decides\n // whether the requested transition is an effective action.\n const row = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n if (row === null) throw notFound();\n const statement = hidden\n ? env.DB.prepare(SQL.hidePublication).bind(now(), id, collection.name)\n : env.DB.prepare(SQL.unhidePublication).bind(id, collection.name);\n await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.auditHide).bind(\n uuid(),\n operator.id,\n hidden ? 'hide' : 'unhide',\n now(),\n id,\n collection.name,\n Number(hidden),\n ),\n statement,\n ]),\n );\n return new Response(null, { headers: noStore, status: 204 });\n}\n\n/** The operator's report queue for one collection: newest first, bounded. */\nexport async function listReports(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n session: Session,\n): Promise<Response> {\n requireOperatorRow(session);\n const status = new URL(request.url).searchParams.get('status') ?? 'unresolved';\n if (status !== 'unresolved' && status !== 'resolved') throw badRequest();\n const context = `reports:${collection.name}:${status}`;\n const { cursor, limit } = forwardPage(request, context, ['status']);\n const bound = reportsPageSql(\n { collection: collection.name, resolved: status === 'resolved' },\n { cursor, limit: limit + 1 },\n );\n const rows = await all<ReportRow>(env, bound.sql, ...bound.parameters);\n const page = rows.slice(0, limit);\n const last = page.at(-1);\n const parsed = archiveReportsSchema.safeParse({\n cursor:\n rows.length > limit && last !== undefined\n ? nextCursor({ createdAt: last.created_at, id: last.id }, context)\n : null,\n reports: page.map((row) => ({\n createdAt: row.created_at,\n entryId: row.publication_id,\n entryTitle: row.entry_title,\n id: row.id,\n reason: row.reason,\n reporter: { id: row.reporter_id, name: row.reporter_name },\n resolvedAt: row.resolved_at,\n })),\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n}\n\ninterface ReportRow {\n collection: string;\n created_at: string;\n entry_title: string;\n id: string;\n publication_id: string;\n reason: string;\n reporter_id: string;\n reporter_name: string;\n resolved_at: string | null;\n}\n\nexport async function resolveReport(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n session: Session,\n id: string,\n): Promise<Response> {\n requireOperatorRow(session);\n const row = await first<{ id: string }>(env, SQL.reportById, id, collection.name);\n if (row === null) throw notFound();\n await run(env, SQL.resolveReport, now(), id, collection.name);\n return new Response(null, { headers: noStore, status: 204 });\n}\n\nexport async function myReports(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const context = `own-reports:${session.publisher?.id ?? 'unregistered'}`;\n const { cursor, limit } = forwardPage(request, context);\n if (session.publisher === null) return json(200, { cursor: null, reports: [] }, noStore);\n const bound = reportsPageSql({ publisherId: session.publisher.id }, { cursor, limit: limit + 1 });\n const rows = await all<ReportRow>(env, bound.sql, ...bound.parameters);\n const page = rows.slice(0, limit);\n const last = page.at(-1);\n const parsed = archiveOwnReportsSchema.safeParse({\n cursor:\n rows.length > limit && last !== undefined\n ? nextCursor({ createdAt: last.created_at, id: last.id }, context)\n : null,\n reports: page.map((row) => ({\n collection: row.collection,\n createdAt: row.created_at,\n entryId: row.publication_id,\n id: row.id,\n reason: row.reason,\n resolvedAt: row.resolved_at,\n })),\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);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,CAAE,CAAC,EAC1F,EAAO,MAAM,IAAe,EAAK,EAAM,IAAK,GAAG,EAAM,UAAU,EAC/D,EAAO,EAAK,MAAM,EAAG,CAAK,EAC1B,EAAO,EAAK,GAAG,EAAE,EACjB,EAAS,EAAA,wBAAwB,UAAU,CAC/C,OACE,EAAK,OAAS,GAAS,IAAS,IAAA,GAC5B,EAAA,WAAW,CAAE,UAAW,EAAK,WAAY,GAAI,EAAK,EAAG,EAAG,CAAO,EAC/D,KACN,QAAS,EAAK,IAAK,IAAS,CAC1B,WAAY,EAAI,WAChB,UAAW,EAAI,WACf,QAAS,EAAI,eACb,GAAI,EAAI,GACR,OAAQ,EAAI,OACZ,WAAY,EAAI,WAClB,EAAE,CACJ,CAAC,EACD,GAAI,CAAC,EAAO,QAAS,MAAM,kBAAkB,EAC7C,OAAO,EAAA,KAAK,IAAK,EAAO,KAAM,EAAA,OAAO,CACvC"}
|
|
1
|
+
{"version":3,"file":"publications.cjs","names":[],"sources":["../../src/worker/publications.ts"],"sourcesContent":["import { s } from '@vielzeug/spell';\nimport type { ArchiveCollection } from '../collection';\nimport {\n ARCHIVE_CONTRACT_VERSION,\n ARCHIVE_LIMITS,\n ARCHIVE_SCHEMA_VERSION,\n type ArchiveModerationAction,\n archiveAliasSchema,\n archiveCollectionMetaSchema,\n archiveHealthSchema,\n archiveIdentitySchema,\n archiveLikedIdsSchema,\n archiveLikeStateSchema,\n archiveLikesPageSchema,\n archiveModerationLogSchema,\n archiveOwnReportsSchema,\n archiveReportsSchema,\n archiveSortSchema,\n isWellFormedText,\n SESSION_MAX_AGE_SECONDS,\n uuidSchema,\n} from '../contract';\nimport { forwardPage, forwardPageParams, nextCursor } from './_pagination';\nimport { JWT_CLOCK_TOLERANCE_SECONDS, type Publisher, type Session } from './auth';\nimport type { ApiConfig, ArchiveWorkerEnv } from './env';\nimport {\n ApiError,\n badRequest,\n conflict,\n forbidden,\n invalid,\n json,\n noStore,\n notFound,\n queryParams,\n readJsonBody,\n} from './http';\nimport {\n likedPublicationsSql,\n likesPageSql,\n listPublicationsSql,\n moderationLogSql,\n type PublicationCursor,\n type PublicationFilter,\n reportsPageSql,\n SQL,\n} from './sql';\n\n/**\n * Route handlers for /api/v1, parameterized by the registered collection. All SQL lives\n * in sql.ts and is bound positionally here in the authored order; the DTO boundary\n * re-validates every stored row before it leaves, content validation lives in the\n * collection's derive step, and moderation state (hidden entries) is visible only\n * through the owner and operator surfaces.\n */\n\ninterface PublicationRow {\n author_name: string;\n client_key: string;\n collection: string;\n content_hash: string;\n content_version: number;\n filter_value: string | null;\n /** Null while visible; set while an operator has removed the entry from public view. */\n hidden_at: string | null;\n id: string;\n like_count: number;\n owner_id: string;\n payload_json: string;\n published_at: string;\n revision: number;\n schema_version: number;\n tags_json: string;\n title: string;\n updated_at: string;\n}\n\nasync function databaseCall<T>(operation: () => Promise<T>): Promise<T> {\n try {\n return await operation();\n } catch (cause) {\n throw new ApiError(500, 'internal', 'The service is not available.', { cause, diagnostic: { kind: 'database' } });\n }\n}\nconst first = <T>(env: ArchiveWorkerEnv, sql: string, ...params: (string | number | null)[]) =>\n databaseCall(() =>\n env.DB.prepare(sql)\n .bind(...params)\n .first<T>(),\n );\nconst all = <T>(env: ArchiveWorkerEnv, sql: string, ...params: (string | number | null)[]) =>\n databaseCall(async () => {\n const result = await env.DB.prepare(sql)\n .bind(...params)\n .all<T>();\n if (!Array.isArray(result.results)) throw new Error('Missing database query results');\n return result.results;\n });\nconst run = (env: ArchiveWorkerEnv, sql: string, ...params: (string | number | null)[]) =>\n databaseCall(() =>\n env.DB.prepare(sql)\n .bind(...params)\n .run(),\n );\n\nconst uuid = (): string => crypto.randomUUID();\nconst now = (): string => new Date().toISOString();\n\n/** A stored row that fails the emitted-DTO contract is unavailable, not malformed data. */\nconst recordUnavailable = (): ApiError =>\n new ApiError(503, 'record-unavailable', 'The record is not available.', { diagnostic: { kind: 'record' } });\n\nconst UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;\n\n/**\n * Maps a joined row to the public DTO through the collection's strict entry schema:\n * stored JSON is parsed and re-validated, stored versions are emitted as-is, and any\n * drift — unknown fields, corrupt payload, mismatched versions — surfaces as 503\n * `record-unavailable` rather than leaking or fabricating data.\n */\nconst rowToEntry = (collection: ArchiveCollection, row: PublicationRow) => {\n let payloadJson: unknown;\n try {\n payloadJson = JSON.parse(row.payload_json);\n } catch {\n throw recordUnavailable();\n }\n const parsed = collection.contract.entry.safeParse({\n author: { id: row.owner_id, name: row.author_name },\n contentHash: row.content_hash,\n contentVersion: row.content_version,\n id: row.id,\n likeCount: row.like_count,\n payload: payloadJson,\n publishedAt: row.published_at,\n revision: row.revision,\n schemaVersion: row.schema_version,\n updatedAt: row.updated_at,\n });\n if (!parsed.success) throw recordUnavailable();\n return parsed.data;\n};\n\nconst rowToOwnedEntry = (collection: ArchiveCollection, row: PublicationRow) => {\n const entry = rowToEntry(collection, row);\n // The private owner DTO adds the create retry key and the moderation state;\n // public routes never emit either.\n const key = uuidSchema.safeParse(row.client_key);\n if (!key.success) throw recordUnavailable();\n return { ...entry, clientKey: row.client_key, hidden: row.hidden_at !== null };\n};\n\nconst entryById = async (collection: ArchiveCollection, env: ArchiveWorkerEnv, id: string) => {\n const row = await first<PublicationRow>(env, SQL.publication, id, collection.name);\n return row === null ? null : rowToEntry(collection, row);\n};\n\n/* ----------------------------------- list --------------------------------- */\n\n/** Normalizes free text into the bounded FTS prefix-match expression the SQL expects. */\nconst searchMatchFor = (search: string | null): string | undefined => {\n if (search === null) return undefined;\n if (search.length > ARCHIVE_LIMITS.searchChars) throw badRequest();\n // Unicode word extraction: punctuation splits terms rather than poisoning them,\n // so \"Hunter's\" and \"fire-ice\" match the words their indexed titles carry.\n const terms = (search.match(/[\\p{L}\\p{N}]+/gu) ?? []).slice(0, ARCHIVE_LIMITS.searchTerms);\n if (terms.length === 0) return undefined;\n return terms.map((t) => `\"${t}\"*`).join(' ');\n};\n\n/**\n * The entry-list cursor context: every query dimension the page's boundary depends\n * on, so a cursor minted under one filter/sort/search and replayed under another is\n * a 400 — the same forward-cursor guard every other list in the package uses.\n */\nconst entryCursorContext = (query: {\n author?: string;\n collection: string;\n filters: readonly string[];\n searchMatch?: string;\n sort: 'liked' | 'recent';\n tags: readonly string[];\n}): string =>\n JSON.stringify([\n query.collection,\n query.sort,\n query.author ?? '',\n query.filters,\n query.tags,\n query.searchMatch ?? '',\n ]);\n\nconst cursorFromRow = (row: PublicationRow): PublicationCursor => ({\n createdAt: row.published_at,\n id: row.id,\n likeCount: row.like_count,\n});\n\n/** Repeated `filter`/`tag` values one entry-list request may carry (OR within each). */\nconst MAX_VOCAB_VALUES = 16;\n\n/** Reads one repeatable vocabulary param: every value must be declared, no repeats\n * past the bound, and the list is deduplicated and sorted so `?tag=a&tag=a` matches\n * `?tag=a` and value order never changes the cursor context. */\nconst vocabValues = (params: URLSearchParams, key: string, declared: readonly string[]): string[] => {\n const raw = params.getAll(key);\n if (raw.length > MAX_VOCAB_VALUES) throw badRequest();\n const values = [...new Set(raw)].sort();\n if (values.some((value) => !declared.includes(value))) throw badRequest();\n return values;\n};\n\nexport async function listEntries(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n): Promise<Response> {\n const url = new URL(request.url);\n const params = queryParams(url, ['author', 'cursor', 'filter', 'limit', 'search', 'sort', 'tag'], ['filter', 'tag']);\n\n const sortRaw = params.get('sort') ?? 'recent';\n const sortParsed = archiveSortSchema.safeParse(sortRaw);\n if (!sortParsed.success) throw badRequest();\n\n const filters = vocabValues(params, 'filter', collection.filters);\n const tags = vocabValues(params, 'tag', collection.tags);\n const author = params.get('author');\n if (author !== null && !UUID_RE.test(author)) throw badRequest();\n\n const searchMatch = searchMatchFor(params.get('search'));\n const context = entryCursorContext({\n author: author ?? undefined,\n collection: collection.name,\n filters,\n searchMatch,\n sort: sortParsed.data,\n tags,\n });\n const { cursor, limit } = forwardPageParams(params, context);\n\n const query: PublicationFilter = {\n author: author ?? undefined,\n collection: collection.name,\n cursor:\n cursor === null ? undefined : { createdAt: cursor.createdAt, id: cursor.id, likeCount: cursor.likeCount ?? 0 },\n filters,\n limit: limit + 1,\n searchMatch,\n sort: sortParsed.data,\n tags,\n };\n\n // One bounded query — never a count: the over-read row is the next page's proof.\n const bound = listPublicationsSql(query);\n const fetched = await all<PublicationRow>(env, bound.sql, ...bound.parameters);\n const page = fetched.slice(0, limit);\n const last = page.at(-1);\n\n // Entries are validated per row through the collection's entry schema; the cursor\n // is internally generated, so the page composes validated parts directly.\n const pageDto = {\n cursor: fetched.length > limit && last !== undefined ? nextCursor(cursorFromRow(last), context) : null,\n entries: page.map((row) => rowToEntry(collection, row)),\n };\n return json(200, pageDto, noStore);\n}\n\n/** The collection's public self-description: name plus the declared query vocabularies. */\nexport function collectionMeta(collection: ArchiveCollection): Response {\n const parsed = archiveCollectionMetaSchema.safeParse({\n filters: [...collection.filters].sort(),\n name: collection.name,\n tags: [...collection.tags].sort(),\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n}\n\nexport async function getEntry(collection: ArchiveCollection, env: ArchiveWorkerEnv, id: string): Promise<Response> {\n const entry = await entryById(collection, env, id);\n if (entry === null) throw notFound();\n return json(200, entry, noStore);\n}\n\n/* ---------------------------------- health --------------------------------- */\n\n/**\n * Service health: the feature flags, the wire limits, and the storage schema stamp\n * actually present in the database. A database whose migrations lag the package\n * answers `degraded` instead of pretending to be fine — the most likely deployment\n * fault becomes a one-glance diagnosis.\n */\nexport async function healthJson(config: ApiConfig, env: ArchiveWorkerEnv): Promise<Response> {\n let dbSchemaVersion: number | null = null;\n let reason: 'database-unavailable' | 'schema-mismatch' | null = null;\n try {\n const row = await first<{ applied: number }>(env, SQL.schemaVersion);\n dbSchemaVersion = row?.applied ?? 0;\n } catch {\n reason = 'database-unavailable';\n }\n if (reason === null && dbSchemaVersion !== ARCHIVE_SCHEMA_VERSION) reason = 'schema-mismatch';\n const parsed = archiveHealthSchema.safeParse({\n dbSchemaVersion,\n features: { likes: config.likesEnabled, publishing: config.publishingEnabled },\n limits: ARCHIVE_LIMITS,\n reason,\n schemaVersion: ARCHIVE_CONTRACT_VERSION,\n status: reason === null ? 'ok' : 'degraded',\n });\n if (!parsed.success) throw recordUnavailable();\n return json(reason === null ? 200 : 503, parsed.data, noStore);\n}\n\n/* --------------------------------- identity -------------------------------- */\n\n/** Emits the private identity DTO through the strict shared schema. */\nconst identityJson = (status: number, identity: unknown): Response => {\n const parsed = archiveIdentitySchema.safeParse(identity);\n if (!parsed.success) throw recordUnavailable();\n return json(status, parsed.data, noStore);\n};\n\nexport function getMe(session: Session | null): Response {\n return identityJson(\n 200,\n session?.publisher\n ? { id: session.publisher.id, name: session.publisher.displayName, operator: session.operator }\n : null,\n );\n}\n\nexport async function registerMe(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const body = await readJsonBody(request);\n const parsed = registerBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n const name = parsed.data.name ?? '';\n\n const existing = await first<{ display_name: string; id: string }>(\n env,\n SQL.publisher,\n session.subject.issuer,\n session.subject.subject,\n );\n if (existing !== null) {\n return identityJson(200, { id: existing.id, name: existing.display_name, operator: session.operator });\n }\n const id = uuid();\n await run(\n env,\n SQL.insertPublisher,\n id,\n session.subject.issuer,\n session.subject.subject,\n name,\n now(),\n session.subject.issuer,\n session.subject.subject,\n Date.now(),\n );\n const row = await first<{ display_name: string; id: string }>(\n env,\n SQL.publisher,\n session.subject.issuer,\n session.subject.subject,\n );\n if (row === null) {\n if ((await first(env, SQL.revokedSubject, session.subject.issuer, session.subject.subject, Date.now())) !== null) {\n throw new ApiError(401, 'unauthenticated', 'Sign-in is required.');\n }\n throw conflict();\n }\n return identityJson(201, { id: row.id, name: row.display_name, operator: session.operator });\n}\n\nexport async function renameMe(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const publisher = requirePublisherRow(session);\n const body = await readJsonBody(request);\n const parsed = renameBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n if (parsed.data.name !== publisher.displayName) {\n await run(env, SQL.renamePublisher, parsed.data.name, publisher.id, parsed.data.name);\n }\n return identityJson(200, { id: publisher.id, name: parsed.data.name, operator: session.operator });\n}\n\n/**\n * Archive-profile erasure, independent of the identity provider's account.\n * The credential survives so an issuer can retry coordinated account erasure.\n */\nexport async function deleteProfile(env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n if (session.publisher !== null) await run(env, SQL.deletePublisher, session.publisher.id);\n return new Response(null, {\n headers: {\n 'cache-control': 'no-store',\n },\n status: 204,\n });\n}\n\n/** Revocation and erasure share a transaction, preventing a stale session from recreating the profile. */\nexport async function deleteAccountProfile(env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const { issuer, subject } = session.subject;\n // Issuance may be one tolerance ahead; expiry verification permits another.\n await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.revokeSubject).bind(\n issuer,\n subject,\n Date.now() + (SESSION_MAX_AGE_SECONDS + 2 * JWT_CLOCK_TOLERANCE_SECONDS) * 1000,\n ),\n env.DB.prepare(SQL.deleteSubjectPublisher).bind(issuer, subject),\n ]),\n );\n return new Response(null, { headers: noStore, status: 204 });\n}\n\n/**\n * Operator-side GDPR erasure: removes any publisher's row; FK cascades take its\n * publications, likes and retry keys. Idempotent for the same reason the self-erase\n * is: an already-erased publisher is a 204 with nothing left to remove.\n */\nexport async function erasePublisher(env: ArchiveWorkerEnv, session: Session, publisherId: string): Promise<Response> {\n const operator = requireOperatorRow(session);\n // The terminal moderation action: erasing a publisher removes their row, and FK\n // cascades take the publications, likes and retry keys with it — the same cascade a\n // self-erase walks, applied to any publisher the operator names. The guarded audit\n // and deletion share a transaction, so retries never record fictitious actions.\n await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.auditErasure).bind(uuid(), operator.id, now(), publisherId),\n env.DB.prepare(SQL.deletePublisher).bind(publisherId),\n ]),\n );\n return new Response(null, { headers: noStore, status: 204 });\n}\n\n/** The operator's accountability log: newest actions first, bounded, ids only. */\nexport async function listModerationLog(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n requireOperatorRow(session);\n const context = 'moderation-log';\n const { cursor, limit } = forwardPage(request, context);\n const bound = moderationLogSql({ cursor, limit: limit + 1 });\n const rows = await all<{\n id: string;\n action: ArchiveModerationAction;\n collection: string | null;\n created_at: string;\n operator_id: string;\n target_id: string;\n }>(env, bound.sql, ...bound.parameters);\n const page = rows.slice(0, limit);\n const last = page.at(-1);\n const parsed = archiveModerationLogSchema.safeParse({\n actions: page.map((row) => ({\n action: row.action,\n collection: row.collection,\n createdAt: row.created_at,\n id: row.id,\n operatorId: row.operator_id,\n targetId: row.target_id,\n })),\n cursor:\n rows.length > limit && last !== undefined\n ? nextCursor({ createdAt: last.created_at, id: last.id }, context)\n : null,\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n}\n\nexport const requirePublisherRow = (session: Session): Publisher => {\n if (session.publisher === null) throw forbidden('Register before publishing.');\n return session.publisher;\n};\n\n/** Operator authority plus a registered publisher row: moderation routes need both,\n * the role claim for authority and the row for attribution. */\nexport const requireOperatorRow = (session: Session): Publisher => {\n if (!session.operator) throw forbidden('Operator access is required.');\n if (session.publisher === null) throw forbidden('Register before moderating.');\n return session.publisher;\n};\n\n// Private identity payloads share the one alias rule with the emitted DTOs.\nconst registerBodySchema = s.object({ name: archiveAliasSchema.optional() });\nconst renameBodySchema = s.object({ name: archiveAliasSchema });\n\nexport async function myPublications(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n session: Session,\n): Promise<Response> {\n if (session.publisher === null) return json(200, { entries: [] }, noStore);\n const rows = await all<PublicationRow>(\n env,\n SQL.ownPublications,\n session.publisher.id,\n collection.name,\n ARCHIVE_LIMITS.maxPublications,\n );\n // The private owner DTO adds the create retry key; public routes never emit it.\n return json(200, { entries: rows.map((row) => rowToOwnedEntry(collection, row)) }, noStore);\n}\n\nexport async function myLikes(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const params = new URL(request.url).searchParams;\n const raw = params.get('ids');\n if (raw === null) {\n const context = `likes:${session.publisher?.id ?? 'unregistered'}`;\n const { cursor, limit } = forwardPage(request, context);\n if (session.publisher === null) return json(200, { cursor: null, ids: [] }, noStore);\n const bound = likesPageSql(session.publisher.id, { cursor, limit: limit + 1 });\n const rows = await all<{ created_at: string; publication_id: string }>(env, bound.sql, ...bound.parameters);\n const page = rows.slice(0, limit);\n const last = page.at(-1);\n const parsed = archiveLikesPageSchema.safeParse({\n cursor:\n rows.length > limit && last !== undefined\n ? nextCursor({ createdAt: last.created_at, id: last.publication_id }, context)\n : null,\n ids: page.map((row) => row.publication_id),\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n }\n queryParams(new URL(request.url), ['ids']);\n const ids = [...new Set(raw.split(','))];\n if (ids.length > ARCHIVE_LIMITS.likeStateIds || ids.some((id) => !UUID_RE.test(id))) throw badRequest();\n if (session.publisher === null) return json(200, { ids: [] }, noStore);\n const bound = likedPublicationsSql(session.publisher.id, ids);\n const rows = await all<{ publication_id: string }>(env, bound.sql, ...bound.parameters);\n const liked = new Set(rows.map((r) => r.publication_id));\n const dto = { ids: ids.filter((id) => liked.has(id)) };\n const validated = archiveLikedIdsSchema.safeParse(dto);\n if (!validated.success) throw recordUnavailable();\n return json(200, validated.data, noStore);\n}\n\n/* --------------------------------- mutations ------------------------------- */\n\n// The core validates the request envelope; the collection's derive step validates the payload.\nconst publishBodySchema = s.object({ clientKey: uuidSchema, payload: s.unknown() });\nconst updateBodySchema = s.object({\n expectedRevision: s.number().int().min(1).max(Number.MAX_SAFE_INTEGER),\n payload: s.unknown(),\n});\n\ninterface RetryRecord {\n expires_at: number;\n payload_hash: string;\n publication_id: string | null;\n}\n\n/**\n * The one replay/conflict/gone decision for a create retry key, shared by the\n * pre-read and the post-insert race re-read: a live record for a different payload\n * is a 409, a tombstone (null publication id) is a 410, and a live record for the\n * same payload replays the entry. `'vanished'` means the recorded publication is\n * no longer readable — hidden or raced-deleted — and only the caller knows which:\n * the pre-read falls through to the publication-by-key lookup (a hidden entry is\n * 403 there), the post-insert re-read answers 410.\n */\nasync function resolveRetryRecord(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n record: RetryRecord | null,\n hash: string,\n): Promise<Response | 'vanished' | null> {\n if (record === null || record.expires_at <= Date.now()) return null;\n if (record.payload_hash !== hash) throw conflict('That key was used for a different payload.');\n if (record.publication_id === null) throw new ApiError(410, 'gone', 'That publication was removed.');\n const entry = await entryById(collection, env, record.publication_id);\n if (entry === null) return 'vanished';\n return json(200, entry, noStore);\n}\n\nexport async function publishEntry(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n config: ApiConfig,\n session: Session,\n): Promise<Response> {\n if (!config.publishingEnabled) throw new ApiError(503, 'publishing-disabled', 'Publishing is currently disabled.');\n const publisher = requirePublisherRow(session);\n const body = await readJsonBody(request);\n const parsed = publishBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n const { clientKey } = parsed.data;\n // The derive step owns every content rejection: schema, vocabulary, title, byte bound.\n const derivation = await collection.derive(parsed.data.payload);\n if (derivation === null) throw invalid();\n\n const payloadJson = derivation.canonicalJson;\n const hash = derivation.contentHash;\n const owner = publisher.id;\n const name = collection.name;\n\n // Retry-key inspection first: the request record decides replay vs conflict vs gone.\n // A logically expired record is ignored entirely — retention is enforced by time,\n // not just by the periodic sweep.\n const retry = await first<RetryRecord>(env, SQL.request, owner, name, clientKey);\n const replay = await resolveRetryRecord(collection, env, retry, hash);\n if (replay !== null && replay !== 'vanished') return replay;\n\n const existing = await first<PublicationRow>(env, SQL.publicationByKey, owner, name, clientKey);\n if (existing !== null) {\n // A hidden entry is frozen for its owner too: replaying the create key must not\n // read as a live publication.\n if (existing.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n if (existing.content_hash !== hash) throw conflict('That key was used for a different payload.');\n return json(200, rowToEntry(collection, existing), noStore);\n }\n\n const timestamp = now();\n const expiresAt = Date.now() + ARCHIVE_LIMITS.idempotencyRetentionMs;\n const tagsJson = JSON.stringify(derivation.tags);\n const results = await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.expireRequests).bind(Date.now()),\n env.DB.prepare(SQL.insertPublication).bind(\n uuid(),\n owner,\n name,\n clientKey,\n ARCHIVE_CONTRACT_VERSION,\n derivation.contentVersion,\n payloadJson,\n hash,\n derivation.filter,\n derivation.title,\n tagsJson,\n timestamp,\n timestamp,\n owner,\n name,\n ARCHIVE_LIMITS.maxPublications,\n owner,\n name,\n clientKey,\n Date.now(),\n ),\n env.DB.prepare(SQL.insertRequest).bind(\n owner,\n name,\n clientKey,\n hash,\n timestamp,\n expiresAt,\n owner,\n name,\n clientKey,\n hash,\n ),\n ]),\n );\n if ((results[1]?.meta?.changes ?? 0) === 0) {\n // The guarded insert was skipped: a race already wrote the key, the key's\n // publication was removed, or the owner's live-publication quota is full.\n // Re-reading the request record first distinguishes a tombstone from a race.\n const racedRecord = await first<RetryRecord>(env, SQL.request, owner, name, clientKey);\n const racedReplay = await resolveRetryRecord(collection, env, racedRecord, hash);\n if (racedReplay === 'vanished') throw new ApiError(410, 'gone', 'That publication was removed.');\n if (racedReplay !== null) return racedReplay;\n const raced = await first<PublicationRow>(env, SQL.publicationByKey, owner, name, clientKey);\n if (raced !== null && raced.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n if (raced !== null && raced.content_hash === hash) return json(200, rowToEntry(collection, raced), noStore);\n if (raced !== null) throw conflict('That key was used for a different payload.');\n throw new ApiError(429, 'quota', 'Publication quota reached.');\n }\n\n const created = await first<PublicationRow>(env, SQL.publicationByKey, owner, name, clientKey);\n if (created === null) throw conflict();\n return json(201, rowToEntry(collection, created), noStore);\n}\n\nexport async function updateEntry(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n config: ApiConfig,\n session: Session,\n id: string,\n): Promise<Response> {\n if (!config.publishingEnabled) throw new ApiError(503, 'publishing-disabled', 'Publishing is currently disabled.');\n const publisher = requirePublisherRow(session);\n // The owner's row regardless of moderation state: the author must be told their\n // entry is frozen, not that it vanished.\n const row = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n // Replacing an entry is the author's alone; anyone else sees the entry as absent.\n if (row === null || row.owner_id !== publisher.id) throw notFound();\n // Hidden entries are frozen: an edit must not bypass the operator's decision.\n if (row.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n\n const body = await readJsonBody(request);\n const parsed = updateBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n const { expectedRevision } = parsed.data;\n\n if (expectedRevision !== row.revision) throw conflict();\n\n const derivation = await collection.derive(parsed.data.payload);\n if (derivation === null) throw invalid();\n const payloadJson = derivation.canonicalJson;\n const hash = derivation.contentHash;\n if (hash === row.content_hash) {\n const current = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n if (current === null || current.owner_id !== publisher.id) throw notFound();\n if (current.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n if (current.revision !== expectedRevision) throw conflict();\n return json(200, rowToEntry(collection, current), noStore);\n }\n\n const tagsJson = JSON.stringify(derivation.tags);\n const result = await run(\n env,\n SQL.updatePublication,\n ARCHIVE_CONTRACT_VERSION,\n derivation.contentVersion,\n payloadJson,\n hash,\n derivation.filter,\n derivation.title,\n tagsJson,\n now(),\n id,\n publisher.id,\n expectedRevision,\n hash,\n );\n if ((result.meta?.changes ?? 0) === 0) {\n const current = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n if (current === null) throw notFound();\n if (current.hidden_at !== null) throw forbidden('This entry is under moderation review.');\n throw conflict();\n }\n const updated = await entryById(collection, env, id);\n if (updated === null) throw notFound();\n return json(200, updated, noStore);\n}\n\nexport async function deleteEntry(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n session: Session,\n id: string,\n): Promise<Response> {\n // The owner's row regardless of moderation state: the author may still remove a\n // hidden entry (their content, their call) — hiding freezes edits, not removal.\n // Removing an entry is the author's alone; an operator may remove ANY entry — the\n // terminal moderation action for abusive content. The operator path carries the\n // same registered-publisher-row requirement every moderation route shares: one\n // accountable identity per action. Everyone else sees the entry as absent.\n if (session.operator) {\n const operator = requireOperatorRow(session);\n await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.auditRemoval).bind(uuid(), operator.id, now(), id, collection.name),\n env.DB.prepare(SQL.removePublicationAny).bind(id, collection.name),\n ]),\n );\n } else {\n const publisher = requirePublisherRow(session);\n const row = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n if (row === null) return new Response(null, { headers: noStore, status: 204 });\n if (row.owner_id !== publisher.id) throw notFound();\n await run(env, SQL.removePublication, id, publisher.id);\n }\n return new Response(null, { headers: noStore, status: 204 });\n}\n\nexport async function setLike(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n config: ApiConfig,\n session: Session,\n id: string,\n liked: boolean,\n): Promise<Response> {\n const publisher = requirePublisherRow(session);\n if (liked && !config.likesEnabled) throw new ApiError(503, 'likes-disabled', 'Likes are currently disabled.');\n const row = await first<PublicationRow>(env, SQL.publication, id, collection.name);\n if (row === null) throw notFound();\n if (liked) {\n await run(env, SQL.insertLike, publisher.id, now(), id);\n } else {\n await run(env, SQL.removeLike, id, publisher.id);\n }\n // One consistent snapshot: the count and the viewer's flag read together.\n const state = await first<{ id: string; like_count: number; liked: number }>(env, SQL.likeState, publisher.id, id);\n if (state === null) throw notFound();\n const dto = { id: state.id, likeCount: state.like_count, liked: state.liked === 1 };\n const validated = archiveLikeStateSchema.safeParse(dto);\n if (!validated.success) throw recordUnavailable();\n return json(200, validated.data, noStore);\n}\n\n/* -------------------------------- moderation ------------------------------- */\n\n// One free-text report reason: trimmed, non-empty, well-formed, bounded.\nconst reportBodySchema = s.object({\n reason: s.string().check(isWellFormedText).trim().min(1).max(ARCHIVE_LIMITS.reportReasonChars),\n});\n\n/**\n * A reader's abuse report against one visible entry. Idempotent per (entry, reporter):\n * reporting again is the same 204, never a duplicate row. Hidden or missing entries\n * are 404 — they cannot be reported, they are already actioned or gone.\n */\nexport async function reportEntry(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n session: Session,\n id: string,\n): Promise<Response> {\n const reporter = requirePublisherRow(session);\n const body = await readJsonBody(request);\n const parsed = reportBodySchema.safeParse(body);\n if (!parsed.success) throw invalid();\n const row = await first<PublicationRow>(env, SQL.publication, id, collection.name);\n if (row === null) throw notFound();\n // Bound in the SQL's authored placeholder order: reporter, reason, created_at,\n // then the WHERE's entry id and collection.\n await run(env, SQL.insertReport, uuid(), reporter.id, parsed.data.reason, now(), id, collection.name);\n return new Response(null, { headers: noStore, status: 204 });\n}\n\n/**\n * The operator soft-hide: removes an entry from every public surface while keeping\n * the row, its likes and its retry key intact. Idempotent — already in the requested\n * state answers the same 204. Operator authority is the session's verified role\n * claim; a registered publisher row is still required (one accountable identity).\n */\nexport async function setHidden(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n session: Session,\n id: string,\n hidden: boolean,\n): Promise<Response> {\n const operator = requireOperatorRow(session);\n // Any collection row, visible or hidden: the operator acts on the storage row. The\n // pre-read establishes existence only. The transactional audit guard decides\n // whether the requested transition is an effective action.\n const row = await first<PublicationRow>(env, SQL.publicationAny, id, collection.name);\n if (row === null) throw notFound();\n const statement = hidden\n ? env.DB.prepare(SQL.hidePublication).bind(now(), id, collection.name)\n : env.DB.prepare(SQL.unhidePublication).bind(id, collection.name);\n await databaseCall(() =>\n env.DB.batch([\n env.DB.prepare(SQL.auditHide).bind(\n uuid(),\n operator.id,\n hidden ? 'hide' : 'unhide',\n now(),\n id,\n collection.name,\n Number(hidden),\n ),\n statement,\n ]),\n );\n return new Response(null, { headers: noStore, status: 204 });\n}\n\n/** The operator's report queue for one collection: newest first, bounded. */\nexport async function listReports(\n collection: ArchiveCollection,\n request: Request,\n env: ArchiveWorkerEnv,\n session: Session,\n): Promise<Response> {\n requireOperatorRow(session);\n const status = new URL(request.url).searchParams.get('status') ?? 'unresolved';\n if (status !== 'unresolved' && status !== 'resolved') throw badRequest();\n const context = `reports:${collection.name}:${status}`;\n const { cursor, limit } = forwardPage(request, context, ['status']);\n const bound = reportsPageSql(\n { collection: collection.name, resolved: status === 'resolved' },\n { cursor, limit: limit + 1 },\n );\n const rows = await all<ReportRow>(env, bound.sql, ...bound.parameters);\n const page = rows.slice(0, limit);\n const last = page.at(-1);\n const parsed = archiveReportsSchema.safeParse({\n cursor:\n rows.length > limit && last !== undefined\n ? nextCursor({ createdAt: last.created_at, id: last.id }, context)\n : null,\n reports: page.map((row) => ({\n createdAt: row.created_at,\n entryId: row.publication_id,\n entryTitle: row.entry_title,\n id: row.id,\n reason: row.reason,\n reporter: { id: row.reporter_id, name: row.reporter_name },\n resolvedAt: row.resolved_at,\n })),\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n}\n\ninterface ReportRow {\n collection: string;\n created_at: string;\n entry_title: string;\n id: string;\n publication_id: string;\n reason: string;\n reporter_id: string;\n reporter_name: string;\n resolved_at: string | null;\n}\n\nexport async function resolveReport(\n collection: ArchiveCollection,\n env: ArchiveWorkerEnv,\n session: Session,\n id: string,\n): Promise<Response> {\n requireOperatorRow(session);\n const row = await first<{ id: string }>(env, SQL.reportById, id, collection.name);\n if (row === null) throw notFound();\n await run(env, SQL.resolveReport, now(), id, collection.name);\n return new Response(null, { headers: noStore, status: 204 });\n}\n\nexport async function myReports(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response> {\n const context = `own-reports:${session.publisher?.id ?? 'unregistered'}`;\n const { cursor, limit } = forwardPage(request, context);\n if (session.publisher === null) return json(200, { cursor: null, reports: [] }, noStore);\n const bound = reportsPageSql({ publisherId: session.publisher.id }, { cursor, limit: limit + 1 });\n const rows = await all<ReportRow>(env, bound.sql, ...bound.parameters);\n const page = rows.slice(0, limit);\n const last = page.at(-1);\n const parsed = archiveOwnReportsSchema.safeParse({\n cursor:\n rows.length > limit && last !== undefined\n ? nextCursor({ createdAt: last.created_at, id: last.id }, context)\n : null,\n reports: page.map((row) => ({\n collection: row.collection,\n createdAt: row.created_at,\n entryId: row.publication_id,\n id: row.id,\n reason: row.reason,\n resolvedAt: row.resolved_at,\n })),\n });\n if (!parsed.success) throw recordUnavailable();\n return json(200, parsed.data, noStore);\n}\n"],"mappings":"kJA6EA,eAAe,aAAgB,EAAyC,CACtE,GAAI,CACF,OAAO,MAAM,EAAU,CACzB,OAAS,EAAO,CACd,MAAM,IAAI,EAAA,SAAS,IAAK,WAAY,gCAAiC,CAAE,QAAO,WAAY,CAAE,KAAM,UAAW,CAAE,CAAC,CAClH,CACF,CACA,IAAM,OAAY,EAAuB,EAAa,GAAG,IACvD,iBACE,EAAI,GAAG,QAAQ,CAAG,CAAC,CAChB,KAAK,GAAG,CAAM,CAAC,CACf,MAAS,CACd,EACI,KAAU,EAAuB,EAAa,GAAG,IACrD,aAAa,SAAY,CACvB,IAAM,EAAS,MAAM,EAAI,GAAG,QAAQ,CAAG,CAAC,CACrC,KAAK,GAAG,CAAM,CAAC,CACf,IAAO,EACV,GAAI,CAAC,MAAM,QAAQ,EAAO,OAAO,EAAG,MAAU,MAAM,gCAAgC,EACpF,OAAO,EAAO,OAChB,CAAC,EACG,KAAO,EAAuB,EAAa,GAAG,IAClD,iBACE,EAAI,GAAG,QAAQ,CAAG,CAAC,CAChB,KAAK,GAAG,CAAM,CAAC,CACf,IAAI,CACT,EAEI,SAAqB,OAAO,WAAW,EACvC,QAAoB,IAAI,KAAK,CAAA,CAAE,YAAY,EAG3C,sBACJ,IAAI,EAAA,SAAS,IAAK,qBAAsB,+BAAgC,CAAE,WAAY,CAAE,KAAM,QAAS,CAAE,CAAC,EAEtG,EAAU,kEAQV,YAAc,EAA+B,IAAwB,CACzE,IAAI,EACJ,GAAI,CACF,EAAc,KAAK,MAAM,EAAI,YAAY,CAC3C,MAAQ,CACN,MAAM,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