@vielzeug/archive 0.0.0-stage → 26.10.6

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.
Files changed (112) hide show
  1. package/README.md +81 -2
  2. package/dist/_json.cjs +2 -0
  3. package/dist/_json.cjs.map +1 -0
  4. package/dist/_json.d.ts +4 -0
  5. package/dist/_json.d.ts.map +1 -0
  6. package/dist/_json.js +2 -0
  7. package/dist/_json.js.map +1 -0
  8. package/dist/auth/config.cjs +2 -0
  9. package/dist/auth/config.cjs.map +1 -0
  10. package/dist/auth/config.d.ts +88 -0
  11. package/dist/auth/config.d.ts.map +1 -0
  12. package/dist/auth/config.js +2 -0
  13. package/dist/auth/config.js.map +1 -0
  14. package/dist/auth/errors.cjs +2 -0
  15. package/dist/auth/errors.cjs.map +1 -0
  16. package/dist/auth/errors.d.ts +10 -0
  17. package/dist/auth/errors.d.ts.map +1 -0
  18. package/dist/auth/errors.js +2 -0
  19. package/dist/auth/errors.js.map +1 -0
  20. package/dist/auth/store.cjs +19 -0
  21. package/dist/auth/store.cjs.map +1 -0
  22. package/dist/auth/store.d.ts +45 -0
  23. package/dist/auth/store.d.ts.map +1 -0
  24. package/dist/auth/store.js +19 -0
  25. package/dist/auth/store.js.map +1 -0
  26. package/dist/auth/worker.cjs +16 -0
  27. package/dist/auth/worker.cjs.map +1 -0
  28. package/dist/auth/worker.d.ts +47 -0
  29. package/dist/auth/worker.d.ts.map +1 -0
  30. package/dist/auth/worker.js +16 -0
  31. package/dist/auth/worker.js.map +1 -0
  32. package/dist/auth.cjs +1 -0
  33. package/dist/auth.d.ts +6 -0
  34. package/dist/auth.d.ts.map +1 -0
  35. package/dist/auth.js +1 -0
  36. package/dist/collection.cjs +2 -0
  37. package/dist/collection.cjs.map +1 -0
  38. package/dist/collection.d.ts +131 -0
  39. package/dist/collection.d.ts.map +1 -0
  40. package/dist/collection.js +2 -0
  41. package/dist/collection.js.map +1 -0
  42. package/dist/contract.cjs +2 -0
  43. package/dist/contract.cjs.map +1 -0
  44. package/dist/contract.d.ts +227 -0
  45. package/dist/contract.d.ts.map +1 -0
  46. package/dist/contract.js +2 -0
  47. package/dist/contract.js.map +1 -0
  48. package/dist/errors.cjs +2 -0
  49. package/dist/errors.cjs.map +1 -0
  50. package/dist/errors.d.ts +12 -0
  51. package/dist/errors.d.ts.map +1 -0
  52. package/dist/errors.js +2 -0
  53. package/dist/errors.js.map +1 -0
  54. package/dist/index.cjs +1 -0
  55. package/dist/index.d.ts +4 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +1 -0
  58. package/dist/worker/_pagination.cjs +2 -0
  59. package/dist/worker/_pagination.cjs.map +1 -0
  60. package/dist/worker/_pagination.d.ts +18 -0
  61. package/dist/worker/_pagination.d.ts.map +1 -0
  62. package/dist/worker/_pagination.js +2 -0
  63. package/dist/worker/_pagination.js.map +1 -0
  64. package/dist/worker/_service.cjs +2 -0
  65. package/dist/worker/_service.cjs.map +1 -0
  66. package/dist/worker/_service.d.ts +5 -0
  67. package/dist/worker/_service.d.ts.map +1 -0
  68. package/dist/worker/_service.js +2 -0
  69. package/dist/worker/_service.js.map +1 -0
  70. package/dist/worker/auth.cjs +2 -0
  71. package/dist/worker/auth.cjs.map +1 -0
  72. package/dist/worker/auth.d.ts +34 -0
  73. package/dist/worker/auth.d.ts.map +1 -0
  74. package/dist/worker/auth.js +2 -0
  75. package/dist/worker/auth.js.map +1 -0
  76. package/dist/worker/env.cjs +2 -0
  77. package/dist/worker/env.cjs.map +1 -0
  78. package/dist/worker/env.d.ts +73 -0
  79. package/dist/worker/env.d.ts.map +1 -0
  80. package/dist/worker/env.js +2 -0
  81. package/dist/worker/env.js.map +1 -0
  82. package/dist/worker/http.cjs +2 -0
  83. package/dist/worker/http.cjs.map +1 -0
  84. package/dist/worker/http.d.ts +66 -0
  85. package/dist/worker/http.d.ts.map +1 -0
  86. package/dist/worker/http.js +2 -0
  87. package/dist/worker/http.js.map +1 -0
  88. package/dist/worker/publications.cjs +2 -0
  89. package/dist/worker/publications.cjs.map +1 -0
  90. package/dist/worker/publications.d.ts +60 -0
  91. package/dist/worker/publications.d.ts.map +1 -0
  92. package/dist/worker/publications.js +2 -0
  93. package/dist/worker/publications.js.map +1 -0
  94. package/dist/worker/sql.cjs +60 -0
  95. package/dist/worker/sql.cjs.map +1 -0
  96. package/dist/worker/sql.d.ts +86 -0
  97. package/dist/worker/sql.d.ts.map +1 -0
  98. package/dist/worker/sql.js +60 -0
  99. package/dist/worker/sql.js.map +1 -0
  100. package/dist/worker/worker.cjs +2 -0
  101. package/dist/worker/worker.cjs.map +1 -0
  102. package/dist/worker/worker.d.ts +72 -0
  103. package/dist/worker/worker.d.ts.map +1 -0
  104. package/dist/worker/worker.js +2 -0
  105. package/dist/worker/worker.js.map +1 -0
  106. package/dist/worker.cjs +1 -0
  107. package/dist/worker.d.ts +4 -0
  108. package/dist/worker.d.ts.map +1 -0
  109. package/dist/worker.js +1 -0
  110. package/migrations/0001_community_archive.sql +173 -0
  111. package/migrations/auth/0001_auth.sql +65 -0
  112. package/package.json +72 -4
@@ -0,0 +1,2 @@
1
+ const e=require("./http.cjs");let t=require("jose");var n=new Set([`127.0.0.1`,`localhost`,`[::1]`]),isLoopback=e=>n.has(e.hostname),configError=t=>new e.ApiError(503,`unavailable`,`The service is temporarily unavailable.`,{diagnostic:{field:t,kind:`configuration`}}),requireVar=(e,t)=>{let n=e[t];if(typeof n!=`string`||n.length===0)throw configError(t);return n},requireUrl=(e,t)=>{try{return new URL(e)}catch{throw configError(t)}},validateUrl=(e,t,n)=>{if(e.username!==``||e.password!==``||e.hash!==``)throw configError(n);if(t===`local`){if(e.protocol!==`http:`||!isLoopback(e))throw configError(n);return e}if(e.protocol!==`https:`||isLoopback(e))throw configError(n);return e},validateOrigin=(e,t,n)=>{if(e===`null`||e===`*`)throw configError(n);let r=validateUrl(requireUrl(e,n),t,n);if(r.pathname!==`/`||r.search!==``)throw configError(n);let i=r.origin;if(i!==e)throw configError(n);return i},flag=(e,t)=>{if(e===`true`)return!0;if(e===`false`)return!1;throw configError(t)},r=`dev-only-insecure-pepper`,rateLimitPepperFor=(e,t)=>{let n=e.RATE_LIMIT_KEY_SECRET;if(typeof n!=`string`||n.length===0){if(t===`local`)return r;throw configError(`RATE_LIMIT_KEY_SECRET`)}if(t===`production`&&n.length<32)throw configError(`RATE_LIMIT_KEY_SECRET`);return n},i=new WeakMap;function apiConfig(e){let n=i.get(e);if(n)return n;let r=requireVar(e,`APP_ENV`);if(r!==`local`&&r!==`production`)throw configError(`APP_ENV`);let a=r;validateUrl(requireUrl(requireVar(e,`JWT_ISSUER`),`JWT_ISSUER`),a,`JWT_ISSUER`);let o=validateUrl(requireUrl(requireVar(e,`JWT_JWKS_URL`),`JWT_JWKS_URL`),a,`JWT_JWKS_URL`),s=requireVar(e,`JWT_AUDIENCE`),c=new Set(requireVar(e,`ALLOWED_ORIGINS`).split(`,`).map(e=>e.trim()).filter(e=>e.length>0).map(e=>validateOrigin(e,a,`ALLOWED_ORIGINS`)));if(c.size===0)throw configError(`ALLOWED_ORIGINS`);if(typeof e.DB?.prepare!=`function`||typeof e.DB?.batch!=`function`)throw configError(`DB`);if(typeof e.READ_LIMIT?.limit!=`function`)throw configError(`READ_LIMIT`);if(typeof e.WRITE_LIMIT?.limit!=`function`)throw configError(`WRITE_LIMIT`);if(typeof e.AUTH_ISSUER_SERVICE?.fetch!=`function`)throw configError(`AUTH_ISSUER_SERVICE`);let l={allowedOrigins:c,appEnv:a,audience:s,issuer:requireVar(e,`JWT_ISSUER`),jwks:(0,t.createRemoteJWKSet)(o),likesEnabled:flag(requireVar(e,`LIKES_ENABLED`),`LIKES_ENABLED`),publishingEnabled:flag(requireVar(e,`PUBLISHING_ENABLED`),`PUBLISHING_ENABLED`),rateLimitPepper:rateLimitPepperFor(e,a)};return i.set(e,l),l}exports.apiConfig=apiConfig;
2
+ //# sourceMappingURL=env.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.cjs","names":[],"sources":["../../src/worker/env.ts"],"sourcesContent":["import { createRemoteJWKSet } from 'jose';\nimport { ApiError } from './http';\n\n/**\n * Worker configuration. Every value the worker trusts comes from the validated Env —\n * never from request-supplied URLs or headers. Anything missing or malformed fails\n * closed (503) rather than falling back to a permissive default.\n */\nexport type AppEnv = 'local' | 'production';\n\nexport interface ApiConfig {\n allowedOrigins: ReadonlySet<string>;\n appEnv: AppEnv;\n audience: string;\n issuer: string;\n jwks: ReturnType<typeof createRemoteJWKSet>;\n likesEnabled: boolean;\n publishingEnabled: boolean;\n rateLimitPepper: string;\n}\n\n/** The rate-limit binding subset the core calls; the runtime's RateLimit satisfies it. */\nexport interface RateLimiterBinding {\n limit(input: { key: string }): Promise<{ success: boolean }>;\n}\n\n/** The D1 statement subset the core binds; null params model nullable columns. */\nexport interface D1Statement {\n all<T>(): Promise<{ results?: T[] }>;\n bind(...params: (string | number | null)[]): D1Statement;\n first<T>(): Promise<T | null>;\n run(): Promise<{ meta?: { changes?: number } }>;\n}\n\n/** The D1 database subset the core uses; a deployment's generated D1Database satisfies it. */\nexport interface D1Like {\n batch(statements: D1Statement[]): Promise<{ meta?: { changes?: number } }[]>;\n prepare(sql: string): D1Statement;\n}\n\n/**\n * The bindings the archive Worker reads. The package declares its own minimal\n * structural types instead of `@cloudflare/workers-types`: a deployment's generated\n * Env (with real D1Database/RateLimit bindings) satisfies this shape structurally.\n */\nexport interface ArchiveWorkerEnv {\n ALLOWED_ORIGINS: string;\n APP_ENV: string;\n /** Required issuer session-validation service; every private request checks live authority. */\n AUTH_ISSUER_SERVICE: AuthIssuerServiceBinding;\n DB: D1Like;\n JWT_AUDIENCE: string;\n JWT_ISSUER: string;\n JWT_JWKS_URL: string;\n LIKES_ENABLED: string;\n PUBLISHING_ENABLED: string;\n RATE_LIMIT_KEY_SECRET: string;\n READ_LIMIT: RateLimiterBinding;\n WRITE_LIMIT: RateLimiterBinding;\n}\n\nexport interface AuthIssuerServiceBinding {\n fetch(request: Request): Promise<Response>;\n}\n\nconst LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '[::1]']);\n\nconst isLoopback = (url: URL): boolean => LOOPBACK_HOSTS.has(url.hostname);\n\n// Configuration faults never name the offending field — the response must not\n// leak Env names or framework details, only a generic temporary unavailability.\nconst configError = (field: string): ApiError =>\n new ApiError(503, 'unavailable', 'The service is temporarily unavailable.', {\n diagnostic: { field, kind: 'configuration' },\n });\n\nconst requireVar = (env: ArchiveWorkerEnv, name: string): string => {\n const value = env[name as keyof ArchiveWorkerEnv];\n if (typeof value !== 'string' || value.length === 0) throw configError(name);\n return value;\n};\n\nconst requireUrl = (raw: string, name: string): URL => {\n try {\n return new URL(raw);\n } catch {\n throw configError(name);\n }\n};\n\n/** Validates one trusted endpoint URL against the environment's transport rules. */\nconst validateUrl = (url: URL, appEnv: AppEnv, name: string): URL => {\n // No embedded credentials or fragments can ever be part of a trusted endpoint.\n if (url.username !== '' || url.password !== '' || url.hash !== '') throw configError(name);\n if (appEnv === 'local') {\n // Loopback HTTP is permitted only in local development.\n if (url.protocol !== 'http:' || !isLoopback(url)) throw configError(name);\n return url;\n }\n if (url.protocol !== 'https:' || isLoopback(url)) throw configError(name);\n return url;\n};\n\n/**\n * An allowed origin is an exact canonical origin: scheme + host (+port) and\n * nothing else — no wildcard, `null` origin, credentials, path, query or fragment.\n * Local permits loopback HTTP; production requires non-loopback HTTPS.\n */\nconst validateOrigin = (raw: string, appEnv: AppEnv, name: string): string => {\n if (raw === 'null' || raw === '*') throw configError(name);\n const url = validateUrl(requireUrl(raw, name), appEnv, name);\n if (url.pathname !== '/' || url.search !== '') throw configError(name);\n const canonical = url.origin;\n if (canonical !== raw) throw configError(name);\n return canonical;\n};\n\nconst flag = (raw: string, name: string): boolean => {\n if (raw === 'true') return true;\n if (raw === 'false') return false;\n throw configError(name);\n};\n\n/** Local-only fallback pepper for rate-limit keys; never a production secret. */\nconst DEV_RATE_LIMIT_PEPPER = 'dev-only-insecure-pepper';\n\n/**\n * The deployment secret behind rate-limit key HMACs: a deployment-specific\n * pepper makes the hashed keys useless outside this deployment. Production\n * fails closed on a missing, empty or too-short value; local development only\n * may fall back to the constant insecure pepper — local only, never production.\n */\nconst rateLimitPepperFor = (env: ArchiveWorkerEnv, appEnv: AppEnv): string => {\n const raw = env.RATE_LIMIT_KEY_SECRET;\n if (typeof raw !== 'string' || raw.length === 0) {\n if (appEnv === 'local') return DEV_RATE_LIMIT_PEPPER;\n throw configError('RATE_LIMIT_KEY_SECRET');\n }\n if (appEnv === 'production' && raw.length < 32) throw configError('RATE_LIMIT_KEY_SECRET');\n return raw;\n};\n\nconst configs = new WeakMap<ArchiveWorkerEnv, ApiConfig>();\n\n/** Parses and validates the env once per bindings object; throws ApiError(503). */\nexport function apiConfig(env: ArchiveWorkerEnv): ApiConfig {\n const cached = configs.get(env);\n if (cached) return cached;\n\n const appEnvRaw = requireVar(env, 'APP_ENV');\n if (appEnvRaw !== 'local' && appEnvRaw !== 'production') throw configError('APP_ENV');\n const appEnv = appEnvRaw;\n\n // The issuer URL is validated for transport/safety but kept as the exact\n // configured string for JWT claim matching.\n validateUrl(requireUrl(requireVar(env, 'JWT_ISSUER'), 'JWT_ISSUER'), appEnv, 'JWT_ISSUER');\n const jwksUrl = validateUrl(requireUrl(requireVar(env, 'JWT_JWKS_URL'), 'JWT_JWKS_URL'), appEnv, 'JWT_JWKS_URL');\n const audience = requireVar(env, 'JWT_AUDIENCE');\n\n const origins = new Set(\n requireVar(env, 'ALLOWED_ORIGINS')\n .split(',')\n .map((o) => o.trim())\n .filter((o) => o.length > 0)\n .map((o) => validateOrigin(o, appEnv, 'ALLOWED_ORIGINS')),\n );\n if (origins.size === 0) throw configError('ALLOWED_ORIGINS');\n\n if (typeof env.DB?.prepare !== 'function' || typeof env.DB?.batch !== 'function') throw configError('DB');\n if (typeof env.READ_LIMIT?.limit !== 'function') throw configError('READ_LIMIT');\n if (typeof env.WRITE_LIMIT?.limit !== 'function') throw configError('WRITE_LIMIT');\n if (typeof env.AUTH_ISSUER_SERVICE?.fetch !== 'function') throw configError('AUTH_ISSUER_SERVICE');\n\n const config: ApiConfig = {\n allowedOrigins: origins,\n appEnv,\n audience,\n // The exact configured issuer string is the match target — a trailing slash\n // is meaningful, never normalized away.\n issuer: requireVar(env, 'JWT_ISSUER'),\n jwks: createRemoteJWKSet(jwksUrl),\n likesEnabled: flag(requireVar(env, 'LIKES_ENABLED'), 'LIKES_ENABLED'),\n publishingEnabled: flag(requireVar(env, 'PUBLISHING_ENABLED'), 'PUBLISHING_ENABLED'),\n rateLimitPepper: rateLimitPepperFor(env, appEnv),\n };\n configs.set(env, config);\n return config;\n}\n"],"mappings":"oDAiEA,IAAM,EAAiB,IAAI,IAAI,CAAC,YAAa,YAAa,OAAO,CAAC,EAE5D,WAAc,GAAsB,EAAe,IAAI,EAAI,QAAQ,EAInE,YAAe,GACnB,IAAI,EAAA,SAAS,IAAK,cAAe,0CAA2C,CAC1E,WAAY,CAAE,QAAO,KAAM,eAAgB,CAC7C,CAAC,EAEG,YAAc,EAAuB,IAAyB,CAClE,IAAM,EAAQ,EAAI,GAClB,GAAI,OAAO,GAAU,UAAY,EAAM,SAAW,EAAG,MAAM,YAAY,CAAI,EAC3E,OAAO,CACT,EAEM,YAAc,EAAa,IAAsB,CACrD,GAAI,CACF,OAAO,IAAI,IAAI,CAAG,CACpB,MAAQ,CACN,MAAM,YAAY,CAAI,CACxB,CACF,EAGM,aAAe,EAAU,EAAgB,IAAsB,CAEnE,GAAI,EAAI,WAAa,IAAM,EAAI,WAAa,IAAM,EAAI,OAAS,GAAI,MAAM,YAAY,CAAI,EACzF,GAAI,IAAW,QAAS,CAEtB,GAAI,EAAI,WAAa,SAAW,CAAC,WAAW,CAAG,EAAG,MAAM,YAAY,CAAI,EACxE,OAAO,CACT,CACA,GAAI,EAAI,WAAa,UAAY,WAAW,CAAG,EAAG,MAAM,YAAY,CAAI,EACxE,OAAO,CACT,EAOM,gBAAkB,EAAa,EA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AC3B,IAAK,GAAM,eAAe,EAAG,EAAQ,iBAAiB,CAAC,CAC5D,EACA,GAAI,EAAQ,OAAS,EAAG,MAAM,YAAY,iBAAiB,EAE3D,GAAI,OAAO,EAAI,IAAI,SAAY,YAAc,OAAO,EAAI,IAAI,OAAU,WAAY,MAAM,YAAY,IAAI,EACxG,GAAI,OAAO,EAAI,YAAY,OAAU,WAAY,MAAM,YAAY,YAAY,EAC/E,GAAI,OAAO,EAAI,aAAa,OAAU,WAAY,MAAM,YAAY,aAAa,EACjF,GAAI,OAAO,EAAI,qBAAqB,OAAU,WAAY,MAAM,YAAY,qBAAqB,EAEjG,IAAM,EAAoB,CACxB,eAAgB,EAChB,SACA,WAGA,OAAQ,WAAW,EAAK,YAAY,EACpC,MAAA,EAAM,EAAA,mBAAA,CAAmB,CAAO,EAChC,aAAc,KAAK,WAAW,EAAK,eAAe,EAAG,eAAe,EACpE,kBAAmB,KAAK,WAAW,EAAK,oBAAoB,EAAG,oBAAoB,EACnF,gBAAiB,mBAAmB,EAAK,CAAM,CACjD,EAEA,OADA,EAAQ,IAAI,EAAK,CAAM,EAChB,CACT"}
@@ -0,0 +1,73 @@
1
+ import { createRemoteJWKSet } from 'jose';
2
+ /**
3
+ * Worker configuration. Every value the worker trusts comes from the validated Env —
4
+ * never from request-supplied URLs or headers. Anything missing or malformed fails
5
+ * closed (503) rather than falling back to a permissive default.
6
+ */
7
+ export type AppEnv = 'local' | 'production';
8
+ export interface ApiConfig {
9
+ allowedOrigins: ReadonlySet<string>;
10
+ appEnv: AppEnv;
11
+ audience: string;
12
+ issuer: string;
13
+ jwks: ReturnType<typeof createRemoteJWKSet>;
14
+ likesEnabled: boolean;
15
+ publishingEnabled: boolean;
16
+ rateLimitPepper: string;
17
+ }
18
+ /** The rate-limit binding subset the core calls; the runtime's RateLimit satisfies it. */
19
+ export interface RateLimiterBinding {
20
+ limit(input: {
21
+ key: string;
22
+ }): Promise<{
23
+ success: boolean;
24
+ }>;
25
+ }
26
+ /** The D1 statement subset the core binds; null params model nullable columns. */
27
+ export interface D1Statement {
28
+ all<T>(): Promise<{
29
+ results?: T[];
30
+ }>;
31
+ bind(...params: (string | number | null)[]): D1Statement;
32
+ first<T>(): Promise<T | null>;
33
+ run(): Promise<{
34
+ meta?: {
35
+ changes?: number;
36
+ };
37
+ }>;
38
+ }
39
+ /** The D1 database subset the core uses; a deployment's generated D1Database satisfies it. */
40
+ export interface D1Like {
41
+ batch(statements: D1Statement[]): Promise<{
42
+ meta?: {
43
+ changes?: number;
44
+ };
45
+ }[]>;
46
+ prepare(sql: string): D1Statement;
47
+ }
48
+ /**
49
+ * The bindings the archive Worker reads. The package declares its own minimal
50
+ * structural types instead of `@cloudflare/workers-types`: a deployment's generated
51
+ * Env (with real D1Database/RateLimit bindings) satisfies this shape structurally.
52
+ */
53
+ export interface ArchiveWorkerEnv {
54
+ ALLOWED_ORIGINS: string;
55
+ APP_ENV: string;
56
+ /** Required issuer session-validation service; every private request checks live authority. */
57
+ AUTH_ISSUER_SERVICE: AuthIssuerServiceBinding;
58
+ DB: D1Like;
59
+ JWT_AUDIENCE: string;
60
+ JWT_ISSUER: string;
61
+ JWT_JWKS_URL: string;
62
+ LIKES_ENABLED: string;
63
+ PUBLISHING_ENABLED: string;
64
+ RATE_LIMIT_KEY_SECRET: string;
65
+ READ_LIMIT: RateLimiterBinding;
66
+ WRITE_LIMIT: RateLimiterBinding;
67
+ }
68
+ export interface AuthIssuerServiceBinding {
69
+ fetch(request: Request): Promise<Response>;
70
+ }
71
+ /** Parses and validates the env once per bindings object; throws ApiError(503). */
72
+ export declare function apiConfig(env: ArchiveWorkerEnv): ApiConfig;
73
+ //# sourceMappingURL=env.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/worker/env.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,MAAM,CAAC;AAG1C;;;;GAIG;AACH,MAAM,MAAM,MAAM,GAA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;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,+FAA+F;IAC/F,mBAAmB,EAAE,wBAAwB,CAAC;IAC9C,EAAE,EAAE,MAAM,CAAC;IACX,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,UAAU,EAAE,kBAAkB,CAAC;IAC/B,WAAW,EAAE,kBAAkB,CAAC;CACjC;AAED,MAAM,WAAW,wBAAwB;IACvC,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC5C;AAiFD,mFAAmF;AACnF,wBAAgB,SAAS,CAAC,GAAG,EAAE,gBAAgB,GAAG,SAAS,CA0C1D"}
@@ -0,0 +1,2 @@
1
+ import{ApiError as e}from"./http.js";import{createRemoteJWKSet as t}from"jose";var n=new Set([`127.0.0.1`,`localhost`,`[::1]`]),isLoopback=e=>n.has(e.hostname),configError=t=>new e(503,`unavailable`,`The service is temporarily unavailable.`,{diagnostic:{field:t,kind:`configuration`}}),requireVar=(e,t)=>{let n=e[t];if(typeof n!=`string`||n.length===0)throw configError(t);return n},requireUrl=(e,t)=>{try{return new URL(e)}catch{throw configError(t)}},validateUrl=(e,t,n)=>{if(e.username!==``||e.password!==``||e.hash!==``)throw configError(n);if(t===`local`){if(e.protocol!==`http:`||!isLoopback(e))throw configError(n);return e}if(e.protocol!==`https:`||isLoopback(e))throw configError(n);return e},validateOrigin=(e,t,n)=>{if(e===`null`||e===`*`)throw configError(n);let r=validateUrl(requireUrl(e,n),t,n);if(r.pathname!==`/`||r.search!==``)throw configError(n);let i=r.origin;if(i!==e)throw configError(n);return i},flag=(e,t)=>{if(e===`true`)return!0;if(e===`false`)return!1;throw configError(t)},r=`dev-only-insecure-pepper`,rateLimitPepperFor=(e,t)=>{let n=e.RATE_LIMIT_KEY_SECRET;if(typeof n!=`string`||n.length===0){if(t===`local`)return r;throw configError(`RATE_LIMIT_KEY_SECRET`)}if(t===`production`&&n.length<32)throw configError(`RATE_LIMIT_KEY_SECRET`);return n},i=new WeakMap;function apiConfig(e){let n=i.get(e);if(n)return n;let r=requireVar(e,`APP_ENV`);if(r!==`local`&&r!==`production`)throw configError(`APP_ENV`);let a=r;validateUrl(requireUrl(requireVar(e,`JWT_ISSUER`),`JWT_ISSUER`),a,`JWT_ISSUER`);let o=validateUrl(requireUrl(requireVar(e,`JWT_JWKS_URL`),`JWT_JWKS_URL`),a,`JWT_JWKS_URL`),s=requireVar(e,`JWT_AUDIENCE`),c=new Set(requireVar(e,`ALLOWED_ORIGINS`).split(`,`).map(e=>e.trim()).filter(e=>e.length>0).map(e=>validateOrigin(e,a,`ALLOWED_ORIGINS`)));if(c.size===0)throw configError(`ALLOWED_ORIGINS`);if(typeof e.DB?.prepare!=`function`||typeof e.DB?.batch!=`function`)throw configError(`DB`);if(typeof e.READ_LIMIT?.limit!=`function`)throw configError(`READ_LIMIT`);if(typeof e.WRITE_LIMIT?.limit!=`function`)throw configError(`WRITE_LIMIT`);if(typeof e.AUTH_ISSUER_SERVICE?.fetch!=`function`)throw configError(`AUTH_ISSUER_SERVICE`);let l={allowedOrigins:c,appEnv:a,audience:s,issuer:requireVar(e,`JWT_ISSUER`),jwks:t(o),likesEnabled:flag(requireVar(e,`LIKES_ENABLED`),`LIKES_ENABLED`),publishingEnabled:flag(requireVar(e,`PUBLISHING_ENABLED`),`PUBLISHING_ENABLED`),rateLimitPepper:rateLimitPepperFor(e,a)};return i.set(e,l),l}export{apiConfig};
2
+ //# sourceMappingURL=env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.js","names":[],"sources":["../../src/worker/env.ts"],"sourcesContent":["import { createRemoteJWKSet } from 'jose';\nimport { ApiError } from './http';\n\n/**\n * Worker configuration. Every value the worker trusts comes from the validated Env —\n * never from request-supplied URLs or headers. Anything missing or malformed fails\n * closed (503) rather than falling back to a permissive default.\n */\nexport type AppEnv = 'local' | 'production';\n\nexport interface ApiConfig {\n allowedOrigins: ReadonlySet<string>;\n appEnv: AppEnv;\n audience: string;\n issuer: string;\n jwks: ReturnType<typeof createRemoteJWKSet>;\n likesEnabled: boolean;\n publishingEnabled: boolean;\n rateLimitPepper: string;\n}\n\n/** The rate-limit binding subset the core calls; the runtime's RateLimit satisfies it. */\nexport interface RateLimiterBinding {\n limit(input: { key: string }): Promise<{ success: boolean }>;\n}\n\n/** The D1 statement subset the core binds; null params model nullable columns. */\nexport interface D1Statement {\n all<T>(): Promise<{ results?: T[] }>;\n bind(...params: (string | number | null)[]): D1Statement;\n first<T>(): Promise<T | null>;\n run(): Promise<{ meta?: { changes?: number } }>;\n}\n\n/** The D1 database subset the core uses; a deployment's generated D1Database satisfies it. */\nexport interface D1Like {\n batch(statements: D1Statement[]): Promise<{ meta?: { changes?: number } }[]>;\n prepare(sql: string): D1Statement;\n}\n\n/**\n * The bindings the archive Worker reads. The package declares its own minimal\n * structural types instead of `@cloudflare/workers-types`: a deployment's generated\n * Env (with real D1Database/RateLimit bindings) satisfies this shape structurally.\n */\nexport interface ArchiveWorkerEnv {\n ALLOWED_ORIGINS: string;\n APP_ENV: string;\n /** Required issuer session-validation service; every private request checks live authority. */\n AUTH_ISSUER_SERVICE: AuthIssuerServiceBinding;\n DB: D1Like;\n JWT_AUDIENCE: string;\n JWT_ISSUER: string;\n JWT_JWKS_URL: string;\n LIKES_ENABLED: string;\n PUBLISHING_ENABLED: string;\n RATE_LIMIT_KEY_SECRET: string;\n READ_LIMIT: RateLimiterBinding;\n WRITE_LIMIT: RateLimiterBinding;\n}\n\nexport interface AuthIssuerServiceBinding {\n fetch(request: Request): Promise<Response>;\n}\n\nconst LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '[::1]']);\n\nconst isLoopback = (url: URL): boolean => LOOPBACK_HOSTS.has(url.hostname);\n\n// Configuration faults never name the offending field — the response must not\n// leak Env names or framework details, only a generic temporary unavailability.\nconst configError = (field: string): ApiError =>\n new ApiError(503, 'unavailable', 'The service is temporarily unavailable.', {\n diagnostic: { field, kind: 'configuration' },\n });\n\nconst requireVar = (env: ArchiveWorkerEnv, name: string): string => {\n const value = env[name as keyof ArchiveWorkerEnv];\n if (typeof value !== 'string' || value.length === 0) throw configError(name);\n return value;\n};\n\nconst requireUrl = (raw: string, name: string): URL => {\n try {\n return new URL(raw);\n } catch {\n throw configError(name);\n }\n};\n\n/** Validates one trusted endpoint URL against the environment's transport rules. */\nconst validateUrl = (url: URL, appEnv: AppEnv, name: string): URL => {\n // No embedded credentials or fragments can ever be part of a trusted endpoint.\n if (url.username !== '' || url.password !== '' || url.hash !== '') throw configError(name);\n if (appEnv === 'local') {\n // Loopback HTTP is permitted only in local development.\n if (url.protocol !== 'http:' || !isLoopback(url)) throw configError(name);\n return url;\n }\n if (url.protocol !== 'https:' || isLoopback(url)) throw configError(name);\n return url;\n};\n\n/**\n * An allowed origin is an exact canonical origin: scheme + host (+port) and\n * nothing else — no wildcard, `null` origin, credentials, path, query or fragment.\n * Local permits loopback HTTP; production requires non-loopback HTTPS.\n */\nconst validateOrigin = (raw: string, appEnv: AppEnv, name: string): string => {\n if (raw === 'null' || raw === '*') throw configError(name);\n const url = validateUrl(requireUrl(raw, name), appEnv, name);\n if (url.pathname !== '/' || url.search !== '') throw configError(name);\n const canonical = url.origin;\n if (canonical !== raw) throw configError(name);\n return canonical;\n};\n\nconst flag = (raw: string, name: string): boolean => {\n if (raw === 'true') return true;\n if (raw === 'false') return false;\n throw configError(name);\n};\n\n/** Local-only fallback pepper for rate-limit keys; never a production secret. */\nconst DEV_RATE_LIMIT_PEPPER = 'dev-only-insecure-pepper';\n\n/**\n * The deployment secret behind rate-limit key HMACs: a deployment-specific\n * pepper makes the hashed keys useless outside this deployment. Production\n * fails closed on a missing, empty or too-short value; local development only\n * may fall back to the constant insecure pepper — local only, never production.\n */\nconst rateLimitPepperFor = (env: ArchiveWorkerEnv, appEnv: AppEnv): string => {\n const raw = env.RATE_LIMIT_KEY_SECRET;\n if (typeof raw !== 'string' || raw.length === 0) {\n if (appEnv === 'local') return DEV_RATE_LIMIT_PEPPER;\n throw configError('RATE_LIMIT_KEY_SECRET');\n }\n if (appEnv === 'production' && raw.length < 32) throw configError('RATE_LIMIT_KEY_SECRET');\n return raw;\n};\n\nconst configs = new WeakMap<ArchiveWorkerEnv, ApiConfig>();\n\n/** Parses and validates the env once per bindings object; throws ApiError(503). */\nexport function apiConfig(env: ArchiveWorkerEnv): ApiConfig {\n const cached = configs.get(env);\n if (cached) return cached;\n\n const appEnvRaw = requireVar(env, 'APP_ENV');\n if (appEnvRaw !== 'local' && appEnvRaw !== 'production') throw configError('APP_ENV');\n const appEnv = appEnvRaw;\n\n // The issuer URL is validated for transport/safety but kept as the exact\n // configured string for JWT claim matching.\n validateUrl(requireUrl(requireVar(env, 'JWT_ISSUER'), 'JWT_ISSUER'), appEnv, 'JWT_ISSUER');\n const jwksUrl = validateUrl(requireUrl(requireVar(env, 'JWT_JWKS_URL'), 'JWT_JWKS_URL'), appEnv, 'JWT_JWKS_URL');\n const audience = requireVar(env, 'JWT_AUDIENCE');\n\n const origins = new Set(\n requireVar(env, 'ALLOWED_ORIGINS')\n .split(',')\n .map((o) => o.trim())\n .filter((o) => o.length > 0)\n .map((o) => validateOrigin(o, appEnv, 'ALLOWED_ORIGINS')),\n );\n if (origins.size === 0) throw configError('ALLOWED_ORIGINS');\n\n if (typeof env.DB?.prepare !== 'function' || typeof env.DB?.batch !== 'function') throw configError('DB');\n if (typeof env.READ_LIMIT?.limit !== 'function') throw configError('READ_LIMIT');\n if (typeof env.WRITE_LIMIT?.limit !== 'function') throw configError('WRITE_LIMIT');\n if (typeof env.AUTH_ISSUER_SERVICE?.fetch !== 'function') throw configError('AUTH_ISSUER_SERVICE');\n\n const config: ApiConfig = {\n allowedOrigins: origins,\n appEnv,\n audience,\n // The exact configured issuer string is the match target — a trailing slash\n // is meaningful, never normalized away.\n issuer: requireVar(env, 'JWT_ISSUER'),\n jwks: createRemoteJWKSet(jwksUrl),\n likesEnabled: flag(requireVar(env, 'LIKES_ENABLED'), 'LIKES_ENABLED'),\n publishingEnabled: flag(requireVar(env, 'PUBLISHING_ENABLED'), 'PUBLISHING_ENABLED'),\n rateLimitPepper: rateLimitPepperFor(env, appEnv),\n };\n configs.set(env, config);\n return config;\n}\n"],"mappings":"+EAiEA,IAAM,EAAiB,IAAI,IAAI,CAAC,YAAa,YAAa,OAAO,CAAC,EAE5D,WAAc,GAAsB,EAAe,IAAI,EAAI,QAAQ,EAInE,YAAe,GACnB,IAAI,EAAS,IAAK,cAAe,0CAA2C,CAC1E,WAAY,CAAE,QAAO,KAAM,eAAgB,CAC7C,CAAC,EAEG,YAAc,EAAuB,IAAyB,CAClE,IAAM,EAAQ,EAAI,GAClB,GAAI,OAAO,GAAU,UAAY,EAAM,SAAW,EAAG,MAAM,YAAY,CAAI,EAC3E,OAAO,CACT,EAEM,YAAc,EAAa,IAAsB,CACrD,GAAI,CACF,OAAO,IAAI,IAAI,CAAG,CACpB,MAAQ,CACN,MAAM,YAAY,CAAI,CACxB,CACF,EAGM,aAAe,EAAU,EAAgB,IAAsB,CAEnE,GAAI,EAAI,WAAa,IAAM,EAAI,WAAa,IAAM,EAAI,OAAS,GAAI,MAAM,YAAY,CAAI,EACzF,GAAI,IAAW,QAAS,CAEtB,GAAI,EAAI,WAAa,SAAW,CAAC,WAAW,CAAG,EAAG,MAAM,YAAY,CAAI,EACxE,OAAO,CACT,CACA,GAAI,EAAI,WAAa,UAAY,WAAW,CAAG,EAAG,MAAM,YAAY,CAAI,EACxE,OAAO,CACT,EAOM,gBAAkB,EAAa,EAAgB,IAAyB,CAC5E,GAAI,IAAQ,QAAU,IAAQ,IAAK,MAAM,YAAY,CAAI,EACzD,IAA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AS,EAAG,MAAM,YAAY,iBAAiB,EAE3D,GAAI,OAAO,EAAI,IAAI,SAAY,YAAc,OAAO,EAAI,IAAI,OAAU,WAAY,MAAM,YAAY,IAAI,EACxG,GAAI,OAAO,EAAI,YAAY,OAAU,WAAY,MAAM,YAAY,YAAY,EAC/E,GAAI,OAAO,EAAI,aAAa,OAAU,WAAY,MAAM,YAAY,aAAa,EACjF,GAAI,OAAO,EAAI,qBAAqB,OAAU,WAAY,MAAM,YAAY,qBAAqB,EAEjG,IAAM,EAAoB,CACxB,eAAgB,EAChB,SACA,WAGA,OAAQ,WAAW,EAAK,YAAY,EACpC,KAAM,EAAmB,CAAO,EAChC,aAAc,KAAK,WAAW,EAAK,eAAe,EAAG,eAAe,EACpE,kBAAmB,KAAK,WAAW,EAAK,oBAAoB,EAAG,oBAAoB,EACnF,gBAAiB,mBAAmB,EAAK,CAAM,CACjD,EAEA,OADA,EAAQ,IAAI,EAAK,CAAM,EAChB,CACT"}
@@ -0,0 +1,2 @@
1
+ const e=require("../contract.cjs");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}),t={"cache-control":`no-store`},errorBody=(e,n,r,i)=>json(e,{error:{code:n,message:r,requestId:i}},t);function toErrorResponse(e,n){if(e instanceof ApiError){let r=e.retryAfterSeconds===void 0?t:{...t,"retry-after":String(e.retryAfterSeconds)};return json(e.status,{error:{code:e.code,message:e.message,requestId:n}},r)}return errorBody(500,`internal`,`Something went wrong.`,n)}function queryParams(e,t){let n=e.searchParams,r=new Set;for(let e of n.keys()){if(!t.includes(e)||r.has(e))throw badRequest();r.add(e)}return n}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(t,n){let r=t.headers.get(`origin`);return r===null||!n.allowedOrigins.has(r)?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, ${e.PUBLISHER_HEADER}, ${e.REQUEST_MARKER_HEADER}`,"access-control-allow-methods":`GET, POST, PUT, DELETE, OPTIONS`,"access-control-allow-origin":r,"access-control-max-age":`600`,vary:`Origin`},status:204})}var n=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 r=t.body?.getReader();if(!r)throw badRequest();let i=Date.now(),a,o=[],s=0;try{for(;;){let{done:t,value:c}=await Promise.race([r.read(),new Promise((e,t)=>{a=setTimeout(()=>t(bodyTimeout()),Math.max(0,n-(Date.now()-i)))})]);if(clearTimeout(a),a=void 0,t)break;if(s+=c.byteLength,s>e.ARCHIVE_LIMITS.requestBytes)throw new ApiError(413,`too-large`,`The request is too large.`);o.push(c)}}catch(e){throw r.cancel().catch(()=>void 0),e}finally{a!==void 0&&clearTimeout(a),r.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 r=4096;function sessionToken(e){let t=e.headers.get(`cookie`);if(t===null)return null;if(t.length>r)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=t=>e.sessionCookieValue(``,{maxAgeSeconds:0,secure:t});exports.ApiError=ApiError,exports.HTTP_BODY_TIMEOUT_MS=n,exports.badRequest=badRequest,exports.conflict=conflict,exports.corsHeaders=corsHeaders,exports.errorBody=errorBody,exports.expiredSessionCookie=expiredSessionCookie,exports.forbidden=forbidden,exports.invalid=invalid,exports.json=json,exports.noStore=t,exports.notFound=notFound,exports.preflight=preflight,exports.queryParams=queryParams,exports.readJsonBody=readJsonBody,exports.requireMutationGuard=requireMutationGuard,exports.sessionToken=sessionToken,exports.toErrorResponse=toErrorResponse,exports.unauthenticated=unauthenticated;
2
+ //# sourceMappingURL=http.cjs.map
@@ -0,0 +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,CAAE,gBAAiB,UAAW,EAExC,WAAa,EAAgB,EAAc,EAAiB,IACvE,KAAK,EAAQ,CAAE,MAAO,CAAE,OAAM,UAAS,WAAU,CAAE,EAAG,CAAO,EAG/D,SAAgB,gBAAgB,EAAgB,EAA6B,CAC3E,GAAI,aAAiB,SAAU,CAC7B,IAAM,EACJ,EAAM,oBAAsB,IAAA,GAAY,EAAU,CAAE,GAAG,EAAS,cAAe,OAAO,EAAM,iBAAiB,CAAE,EACjH,OAAO,KAAK,EAAM,OAAQ,CAAE,MAAO,CAAE,KAAM,EAAM,KAAM,QAAS,EAA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M,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"}
@@ -0,0 +1,66 @@
1
+ import { type ArchiveErrorCode } from '../contract';
2
+ import type { ApiConfig } from './env';
3
+ /**
4
+ * HTTP plumbing shared by every route: the one error shape, bounded body reads, strict
5
+ * query/header guards and the CORS contract. Handlers throw ApiError; the entry point
6
+ * sanitizes — no stack, SQL, body, or PII ever reaches a response. Internal to the
7
+ * Worker: clients parse the wire envelope (`archiveErrorResponseSchema`), never this class.
8
+ */
9
+ export declare class ApiError extends Error {
10
+ readonly code: ArchiveErrorCode;
11
+ readonly retryAfterSeconds?: number;
12
+ readonly status: number;
13
+ readonly diagnostic?: ArchiveDiagnostic;
14
+ constructor(status: number, code: ArchiveErrorCode, message: string, opts?: ErrorOptions & {
15
+ retryAfterSeconds?: number;
16
+ diagnostic?: ArchiveDiagnostic;
17
+ });
18
+ }
19
+ export type ArchiveDiagnostic = {
20
+ readonly kind: 'configuration';
21
+ readonly field: string;
22
+ } | {
23
+ readonly kind: 'database' | 'record' | 'schema' | 'internal' | 'cache' | 'collection';
24
+ };
25
+ export declare const badRequest: (message?: string) => ApiError;
26
+ export declare const unauthenticated: () => ApiError;
27
+ export declare const forbidden: (message?: string) => ApiError;
28
+ export declare const notFound: () => ApiError;
29
+ export declare const conflict: (message?: string) => ApiError;
30
+ export declare const invalid: (message?: string) => ApiError;
31
+ export declare const json: (status: number, body: unknown, headers?: HeadersInit) => Response;
32
+ export declare const noStore: {
33
+ readonly 'cache-control': "no-store";
34
+ };
35
+ export declare const errorBody: (status: number, code: string, message: string, requestId: string) => Response;
36
+ /** Sanitizes any thrown value into the public error shape; unknowns become 500. */
37
+ export declare function toErrorResponse(error: unknown, requestId: string): Response;
38
+ /**
39
+ * Rejects query strings with unknown or repeated parameters. Every accepted route
40
+ * names its full allow-list; anything else is a client bug, not something to ignore.
41
+ */
42
+ export declare function queryParams(url: URL, allowed: readonly string[]): URLSearchParams;
43
+ /** Exact-origin plus request-marker guard for every mutation: no wildcards, no nulls. */
44
+ export declare function requireMutationGuard(request: Request, config: ApiConfig): void;
45
+ /** CORS response headers for one already-allowed origin; absent for foreign origins. */
46
+ export declare function corsHeaders(request: Request, config: ApiConfig): Record<string, string>;
47
+ /** Answers a preflight for an allowed origin; foreign or missing origins get 403. */
48
+ export declare function preflight(request: Request, config: ApiConfig): Response;
49
+ /** Hard deadline for reading a request body; slow streams fail as 408. */
50
+ export declare const HTTP_BODY_TIMEOUT_MS = 10000;
51
+ /**
52
+ * Reads a JSON body with a hard byte ceiling and a wall-clock deadline enforced while
53
+ * streaming — a lying or absent Content-Length and a stalled sender are both cut off.
54
+ * Wrong media types are 415, oversized 413, timed out 408, malformed JSON 400; schema
55
+ * validation happens afterwards at the route boundary.
56
+ */
57
+ export declare function readJsonBody(request: Request): Promise<unknown>;
58
+ /**
59
+ * Reads the session cookie. A duplicate cookie name or an oversized header is a
60
+ * malformed request, not a missing session: callers distinguish null (anonymous)
61
+ * from a token to verify.
62
+ */
63
+ export declare function sessionToken(request: Request): string | null;
64
+ /** The expired-session Set-Cookie value the sign-out route and account erasure send. */
65
+ export declare const expiredSessionCookie: (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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 n=e.searchParams,r=new Set;for(let e of n.keys()){if(!t.includes(e)||r.has(e))throw badRequest();r.add(e)}return n}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
+ //# sourceMappingURL=http.js.map
@@ -0,0 +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,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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AAS,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,EAAmB,GAAI,CAAE,cAAe,EAAG,QAAO,CAAC"}
@@ -0,0 +1,2 @@
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(` `)},o=i.s.object({c:i.s.object({id:i.s.string().uuid(),likeCount:i.s.number().int().min(0).max(2**53-1),publishedAt:i.s.string().isoDateTime()}),d:i.s.enum([`next`,`previous`]),q:i.s.object({a:i.s.string().uuid().nullable(),e:i.s.string().max(e.ARCHIVE_LIMITS.vocabChars).nullable(),g:i.s.string().max(e.ARCHIVE_LIMITS.vocabChars),m:i.s.string().max(e.ARCHIVE_LIMITS.searchChars*e.ARCHIVE_LIMITS.searchTerms).nullable(),s:i.s.enum([`liked`,`recent`]),t:i.s.string().max(e.ARCHIVE_LIMITS.vocabChars).nullable()})}),cursorFromRow=e=>({id:e.id,likeCount:e.like_count,publishedAt:e.published_at}),encodeCursor=(e,t,n)=>r.encodeCursorValue({c:cursorFromRow(e),d:t,q:{a:n.author??null,e:n.filter??null,g:n.collection,m:n.searchMatch??null,s:n.sort,t:n.tag??null}}),decodeCursor=(e,n)=>{let i=r.decodeCursorValue(e,o),{q:a}=i;if((a.a??void 0)!==n.author||a.s!==n.sort||a.g!==n.collection||(a.e??void 0)!==n.filter||(a.m??void 0)!==n.searchMatch||(a.t??void 0)!==n.tag)throw t.badRequest();return i};async function listEntries(r,i,o){let s=new URL(i.url),c=t.queryParams(s,[`author`,`cursor`,`filter`,`limit`,`search`,`sort`,`tag`]),l=c.get(`sort`)??`recent`,u=e.archiveSortSchema.safeParse(l);if(!u.success)throw t.badRequest();let d=c.get(`limit`),f=d===null?e.ARCHIVE_LIMITS.pageSize:Number(d);if(!Number.isInteger(f)||f<1||f>e.ARCHIVE_LIMITS.maxPageSize)throw t.badRequest();let p=c.get(`filter`);if(p!==null&&!r.filters.includes(p))throw t.badRequest();let m=c.get(`tag`);if(m!==null&&!r.tags.includes(m))throw t.badRequest();let h=c.get(`author`);if(h!==null&&!a.test(h))throw t.badRequest();let g=searchMatchFor(c.get(`search`)),_={author:h??void 0,collection:r.name,filter:p??void 0,limit:f+1,searchMatch:g,sort:u.data,tag:m??void 0},v=c.get(`cursor`),y=v===null?null:decodeCursor(v,_),b=y?.d??`next`;y!==null&&(_.cursor=y.c),_.direction=b;let fetchRows=async e=>{let t=n.listPublicationsSql(e);return all(o,t.sql,...t.parameters)},x=await fetchRows(_),S=x.length>f,C=x.slice(0,f);b===`previous`&&C.reverse();let w=!1;if(y!==null&&C.length>0){let e={..._,limit:1};b===`next`?(e.cursor=cursorFromRow(C[0]),e.direction=`previous`):(e.cursor=cursorFromRow(C[C.length-1]),e.direction=`next`),w=(await fetchRows(e)).length>0}let T=C.length>0&&(b===`next`&&S||b===`previous`&&w)?encodeCursor(C[C.length-1],`next`,_):null,E=C.length>0&&(b===`previous`&&S||b===`next`&&w)?encodeCursor(C[0],`previous`,_):null,D={cursor:T,entries:C.map(e=>rowToEntry(r,e)),previous:E};return t.json(200,D,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 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=Date.now(),y=await first(a,n.SQL.request,g,_,f);if(y!==null&&y.expires_at>v){if(y.payload_hash!==h)throw t.conflict(`That key was used for a different payload.`);if(y.publication_id===null)throw new t.ApiError(410,`gone`,`That publication was removed.`);let e=await entryById(r,a,y.publication_id);if(e!==null)return t.json(200,e,t.noStore)}let b=await first(a,n.SQL.publicationByKey,g,_,f);if(b!==null){if(b.hidden_at!==null)throw t.forbidden(`This entry is under moderation review.`);if(b.content_hash!==h)throw t.conflict(`That key was used for a different payload.`);return t.json(200,rowToEntry(r,b),t.noStore)}let x=now(),S=Date.now()+e.ARCHIVE_LIMITS.idempotencyRetentionMs,C=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,C,x,x,g,_,e.ARCHIVE_LIMITS.maxPublications,g,_,f,Date.now()),a.DB.prepare(n.SQL.insertRequest).bind(g,_,f,h,x,S,g,_,f,h)])))[1]?.meta?.changes??0)===0){let e=await first(a,n.SQL.request,g,_,f);if(e!==null&&e.expires_at>Date.now()){if(e.publication_id===null)throw new t.ApiError(410,`gone`,`That publication was removed.`);if(e.payload_hash===h){let n=await entryById(r,a,e.publication_id);if(n!==null)return t.json(200,n,t.noStore);throw new t.ApiError(410,`gone`,`That publication was removed.`)}throw t.conflict(`That key was used for a different payload.`)}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 w=await first(a,n.SQL.publicationByKey,g,_,f);if(w===null)throw t.conflict();return t.json(201,rowToEntry(r,w),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
+ //# sourceMappingURL=publications.cjs.map
@@ -0,0 +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 { decodeCursorValue, encodeCursorValue, forwardPage, 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\ntype CursorDirection = 'next' | 'previous';\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/** The cursor carries direction plus filter context so a replayed/mismatched cursor is 400. */\ninterface CursorPayload {\n c: PublicationCursor;\n d: CursorDirection;\n q: {\n a: string | null;\n e: string | null;\n g: string;\n m: string | null;\n s: 'liked' | 'recent';\n t: string | null;\n };\n}\n\nconst cursorPayloadSchema = s.object({\n c: s.object({\n id: s.string().uuid(),\n likeCount: s.number().int().min(0).max(Number.MAX_SAFE_INTEGER),\n publishedAt: s.string().isoDateTime(),\n }),\n d: s.enum(['next', 'previous'] as const),\n q: s.object({\n a: s.string().uuid().nullable(),\n e: s.string().max(ARCHIVE_LIMITS.vocabChars).nullable(),\n g: s.string().max(ARCHIVE_LIMITS.vocabChars),\n m: s\n .string()\n .max(ARCHIVE_LIMITS.searchChars * ARCHIVE_LIMITS.searchTerms)\n .nullable(),\n s: s.enum(['liked', 'recent'] as const),\n t: s.string().max(ARCHIVE_LIMITS.vocabChars).nullable(),\n }),\n});\n\nconst cursorFromRow = (row: PublicationRow): PublicationCursor => ({\n id: row.id,\n likeCount: row.like_count,\n publishedAt: row.published_at,\n});\n\nconst encodeCursor = (row: PublicationRow, direction: CursorDirection, query: PublicationFilter): string =>\n encodeCursorValue({\n c: cursorFromRow(row),\n d: direction,\n q: {\n a: query.author ?? null,\n e: query.filter ?? null,\n g: query.collection,\n m: query.searchMatch ?? null,\n s: query.sort,\n t: query.tag ?? null,\n },\n } satisfies CursorPayload);\n\nconst decodeCursor = (raw: string, query: PublicationFilter): CursorPayload => {\n const payload = decodeCursorValue(raw, cursorPayloadSchema);\n const { q } = payload;\n if (\n (q.a ?? undefined) !== query.author ||\n q.s !== query.sort ||\n q.g !== query.collection ||\n (q.e ?? undefined) !== query.filter ||\n (q.m ?? undefined) !== query.searchMatch ||\n (q.t ?? undefined) !== query.tag\n ) {\n throw badRequest();\n }\n return payload;\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 limitRaw = params.get('limit');\n const limit = limitRaw === null ? ARCHIVE_LIMITS.pageSize : Number(limitRaw);\n if (!Number.isInteger(limit) || limit < 1 || limit > ARCHIVE_LIMITS.maxPageSize) 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 query: PublicationFilter = {\n author: author ?? undefined,\n collection: collection.name,\n filter: filter ?? undefined,\n limit: limit + 1,\n searchMatch,\n sort: sortParsed.data,\n tag: tag ?? undefined,\n };\n\n // The cursor tells the fetch direction; the first page always walks forward.\n const cursorRaw = params.get('cursor');\n const decoded = cursorRaw === null ? null : decodeCursor(cursorRaw, query);\n const direction: CursorDirection = decoded?.d ?? 'next';\n if (decoded !== null) query.cursor = decoded.c;\n query.direction = direction;\n\n const fetchRows = async (f: PublicationFilter): Promise<PublicationRow[]> => {\n const bound = listPublicationsSql(f);\n return all<PublicationRow>(env, bound.sql, ...bound.parameters);\n };\n\n const fetched = await fetchRows(query);\n const hasMore = fetched.length > limit;\n // 'previous' reads ASC off the cursor; display flips back to normal DESC order.\n const page = fetched.slice(0, limit);\n if (direction === 'previous') page.reverse();\n\n // Bounded index probes — never a count query — decide has-more in the opposite\n // direction. A first (uncursored) page needs no backward probe: it IS the top.\n let hasOpposite = false;\n if (decoded !== null && page.length > 0) {\n const probe: PublicationFilter = { ...query, limit: 1 };\n if (direction === 'next') {\n probe.cursor = cursorFromRow(page[0]);\n probe.direction = 'previous';\n } else {\n probe.cursor = cursorFromRow(page[page.length - 1]);\n probe.direction = 'next';\n }\n hasOpposite = (await fetchRows(probe)).length > 0;\n }\n\n const next =\n page.length > 0 && ((direction === 'next' && hasMore) || (direction === 'previous' && hasOpposite))\n ? encodeCursor(page[page.length - 1], 'next', query)\n : null;\n const previous =\n page.length > 0 && ((direction === 'previous' && hasMore) || (direction === 'next' && hasOpposite))\n ? encodeCursor(page[0], 'previous', query)\n : null;\n\n // Entries are validated per row through the collection's entry schema; the cursors\n // are internally generated, so the page composes validated parts directly.\n const pageDto = { cursor: next, entries: page.map((row) => rowToEntry(collection, row)), previous };\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\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 nowMs = Date.now();\n const retry = await first<{ expires_at: number; payload_hash: string; publication_id: string | null }>(\n env,\n SQL.request,\n owner,\n name,\n clientKey,\n );\n if (retry !== null && retry.expires_at > nowMs) {\n if (retry.payload_hash !== hash) throw conflict('That key was used for a different payload.');\n if (retry.publication_id === null) {\n throw new ApiError(410, 'gone', 'That publication was removed.');\n }\n const entry = await entryById(collection, env, retry.publication_id);\n if (entry !== null) return json(200, entry, noStore);\n }\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 request = await first<{ expires_at: number; payload_hash: string; publication_id: string | null }>(\n env,\n SQL.request,\n owner,\n name,\n clientKey,\n );\n if (request !== null && request.expires_at > Date.now()) {\n if (request.publication_id === null) throw new ApiError(410, 'gone', 'That publication was removed.');\n if (request.payload_hash === hash) {\n const entry = await entryById(collection, env, request.publication_id);\n if (entry !== null) return json(200, entry, noStore);\n // The publication vanished between checks: the key is a tombstone.\n throw new ApiError(410, 'gone', 'That publication was removed.');\n }\n throw conflict('That key was used for a different payload.');\n }\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,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S,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"}
@@ -0,0 +1,60 @@
1
+ import type { ArchiveCollection } from '../collection';
2
+ import { type Publisher, type Session } from './auth';
3
+ import type { ApiConfig, ArchiveWorkerEnv } from './env';
4
+ export declare function listEntries(collection: ArchiveCollection, request: Request, env: ArchiveWorkerEnv): Promise<Response>;
5
+ /** The collection's public self-description: name plus the declared query vocabularies. */
6
+ export declare function collectionMeta(collection: ArchiveCollection): Response;
7
+ export declare function getEntry(collection: ArchiveCollection, env: ArchiveWorkerEnv, id: string): Promise<Response>;
8
+ /**
9
+ * Service health: the feature flags, the wire limits, and the storage schema stamp
10
+ * actually present in the database. A database whose migrations lag the package
11
+ * answers `degraded` instead of pretending to be fine — the most likely deployment
12
+ * fault becomes a one-glance diagnosis.
13
+ */
14
+ export declare function healthJson(config: ApiConfig, env: ArchiveWorkerEnv): Promise<Response>;
15
+ export declare function getMe(session: Session | null): Response;
16
+ export declare function registerMe(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response>;
17
+ export declare function renameMe(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response>;
18
+ /**
19
+ * Archive-profile erasure, independent of the identity provider's account.
20
+ * The credential survives so an issuer can retry coordinated account erasure.
21
+ */
22
+ export declare function deleteProfile(env: ArchiveWorkerEnv, session: Session): Promise<Response>;
23
+ /** Revocation and erasure share a transaction, preventing a stale session from recreating the profile. */
24
+ export declare function deleteAccountProfile(env: ArchiveWorkerEnv, session: Session): Promise<Response>;
25
+ /**
26
+ * Operator-side GDPR erasure: removes any publisher's row; FK cascades take its
27
+ * publications, likes and retry keys. Idempotent for the same reason the self-erase
28
+ * is: an already-erased publisher is a 204 with nothing left to remove.
29
+ */
30
+ export declare function erasePublisher(env: ArchiveWorkerEnv, session: Session, publisherId: string): Promise<Response>;
31
+ /** The operator's accountability log: newest actions first, bounded, ids only. */
32
+ export declare function listModerationLog(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response>;
33
+ export declare const requirePublisherRow: (session: Session) => Publisher;
34
+ /** Operator authority plus a registered publisher row: moderation routes need both,
35
+ * the role claim for authority and the row for attribution. */
36
+ export declare const requireOperatorRow: (session: Session) => Publisher;
37
+ export declare function myPublications(collection: ArchiveCollection, env: ArchiveWorkerEnv, session: Session): Promise<Response>;
38
+ export declare function myLikes(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response>;
39
+ export declare function publishEntry(collection: ArchiveCollection, request: Request, env: ArchiveWorkerEnv, config: ApiConfig, session: Session): Promise<Response>;
40
+ export declare function updateEntry(collection: ArchiveCollection, request: Request, env: ArchiveWorkerEnv, config: ApiConfig, session: Session, id: string): Promise<Response>;
41
+ export declare function deleteEntry(collection: ArchiveCollection, env: ArchiveWorkerEnv, session: Session, id: string): Promise<Response>;
42
+ export declare function setLike(collection: ArchiveCollection, env: ArchiveWorkerEnv, config: ApiConfig, session: Session, id: string, liked: boolean): Promise<Response>;
43
+ /**
44
+ * A reader's abuse report against one visible entry. Idempotent per (entry, reporter):
45
+ * reporting again is the same 204, never a duplicate row. Hidden or missing entries
46
+ * are 404 — they cannot be reported, they are already actioned or gone.
47
+ */
48
+ export declare function reportEntry(collection: ArchiveCollection, request: Request, env: ArchiveWorkerEnv, session: Session, id: string): Promise<Response>;
49
+ /**
50
+ * The operator soft-hide: removes an entry from every public surface while keeping
51
+ * the row, its likes and its retry key intact. Idempotent — already in the requested
52
+ * state answers the same 204. Operator authority is the session's verified role
53
+ * claim; a registered publisher row is still required (one accountable identity).
54
+ */
55
+ export declare function setHidden(collection: ArchiveCollection, env: ArchiveWorkerEnv, session: Session, id: string, hidden: boolean): Promise<Response>;
56
+ /** The operator's report queue for one collection: newest first, bounded. */
57
+ export declare function listReports(collection: ArchiveCollection, request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response>;
58
+ export declare function resolveReport(collection: ArchiveCollection, env: ArchiveWorkerEnv, session: Session, id: string): Promise<Response>;
59
+ export declare function myReports(request: Request, env: ArchiveWorkerEnv, session: Session): Promise<Response>;
60
+ //# sourceMappingURL=publications.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publications.d.ts","sourceRoot":"","sources":["../../src/worker/publications.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAsBvD,OAAO,EAA+B,KAAK,SAAS,EAAE,KAAK,OAAO,EAAE,MAAM,QAAQ,CAAC;AACnF,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AA0NzD,wBAAsB,WAAW,CAC/B,UAAU,EAAE,iBAAiB,EAC7B,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,gBAAgB,GACpB,OAAO,CAAC,QAAQ,CAAC,CA4EnB;AAED,2FAA2F;AAC3F,wBAAgB,cAAc,CAAC,UAAU,EAAE,iBAAiB,GAAG,QAAQ,CAQtE;AAED,wBAAsB,QAAQ,CAAC,UAAU,EAAE,iBAAiB,EAAE,GAAG,EAAE,gBAAgB,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAIlH;AAID;;;;;GAKG;AACH,wBAAsB,UAAU,CAAC,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAoB5F;AAWD,wBAAgB,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,GAAG,QAAQ,CAOvD;AAED,wB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