@powerhousedao/switchboard 6.2.3-dev.10 → 6.2.3-dev.100

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 (310) hide show
  1. package/.tsbuild/test/ai-gateway.test.d.ts +2 -0
  2. package/.tsbuild/test/ai-gateway.test.d.ts.map +1 -0
  3. package/.tsbuild/test/attachments/byte-grants.test.d.ts +2 -0
  4. package/.tsbuild/test/attachments/byte-grants.test.d.ts.map +1 -0
  5. package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts +2 -0
  6. package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts.map +1 -0
  7. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
  8. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
  9. package/.tsbuild/test/attachments/url-signer.test.d.ts +2 -0
  10. package/.tsbuild/test/attachments/url-signer.test.d.ts.map +1 -0
  11. package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
  12. package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
  13. package/.tsbuild/test/boot-create-signature-policy.test.d.ts +2 -0
  14. package/.tsbuild/test/boot-create-signature-policy.test.d.ts.map +1 -0
  15. package/.tsbuild/test/boot-desktop-auth.test.d.ts +2 -0
  16. package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
  17. package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
  18. package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
  19. package/.tsbuild/test/boot-helpers.d.ts +5 -0
  20. package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
  21. package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
  22. package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
  23. package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
  24. package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
  25. package/.tsbuild/test/boot-mcp-enabled.test.d.ts +2 -0
  26. package/.tsbuild/test/boot-mcp-enabled.test.d.ts.map +1 -0
  27. package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
  28. package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
  29. package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
  30. package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
  31. package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
  32. package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
  33. package/.tsbuild/test/boot-ready-drives.test.d.ts +2 -0
  34. package/.tsbuild/test/boot-ready-drives.test.d.ts.map +1 -0
  35. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts +2 -0
  36. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts.map +1 -0
  37. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts +2 -0
  38. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts.map +1 -0
  39. package/.tsbuild/test/boot-without-renown.test.d.ts +2 -0
  40. package/.tsbuild/test/boot-without-renown.test.d.ts.map +1 -0
  41. package/.tsbuild/test/boot-without-require-signatures.test.d.ts +2 -0
  42. package/.tsbuild/test/boot-without-require-signatures.test.d.ts.map +1 -0
  43. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts +2 -0
  44. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts.map +1 -0
  45. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
  46. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
  47. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
  48. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
  49. package/.tsbuild/test/builder-defaults.test.d.ts +2 -0
  50. package/.tsbuild/test/builder-defaults.test.d.ts.map +1 -0
  51. package/.tsbuild/test/config-from-env.test.d.ts +2 -0
  52. package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
  53. package/.tsbuild/test/config-merge.test.d.ts +2 -0
  54. package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
  55. package/.tsbuild/test/config-schema.test.d.ts +2 -0
  56. package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
  57. package/.tsbuild/test/create-signature-policy.test.d.ts +2 -0
  58. package/.tsbuild/test/create-signature-policy.test.d.ts.map +1 -0
  59. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
  60. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
  61. package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
  62. package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
  63. package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
  64. package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
  65. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
  66. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
  67. package/.tsbuild/test/fatal-shutdown.test.d.ts +2 -0
  68. package/.tsbuild/test/fatal-shutdown.test.d.ts.map +1 -0
  69. package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
  70. package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
  71. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts +2 -0
  72. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts.map +1 -0
  73. package/.tsbuild/test/observability-signals.test.d.ts +2 -0
  74. package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
  75. package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
  76. package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
  77. package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
  78. package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
  79. package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
  80. package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
  81. package/.tsbuild/test/pglite-store.test.d.ts +2 -0
  82. package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
  83. package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
  84. package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
  85. package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
  86. package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
  87. package/.tsbuild/test/port-probe.test.d.ts +2 -0
  88. package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
  89. package/.tsbuild/test/processor-timing-log.test.d.ts +2 -0
  90. package/.tsbuild/test/processor-timing-log.test.d.ts.map +1 -0
  91. package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
  92. package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
  93. package/.tsbuild/test/renown-trust-policy.test.d.ts +2 -0
  94. package/.tsbuild/test/renown-trust-policy.test.d.ts.map +1 -0
  95. package/.tsbuild/test/served-schemas.test.d.ts +2 -0
  96. package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
  97. package/.tsbuild/test/shutdown.test.d.ts +2 -0
  98. package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
  99. package/.tsbuild/test/snapshot-writer.d.ts +18 -0
  100. package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
  101. package/.tsbuild/test/store-lock.test.d.ts +2 -0
  102. package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
  103. package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
  104. package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
  105. package/.tsbuild/test/unsupported-stored-documents.test.d.ts +2 -0
  106. package/.tsbuild/test/unsupported-stored-documents.test.d.ts.map +1 -0
  107. package/.tsbuild/test/workflow/harness.d.ts +56 -0
  108. package/.tsbuild/test/workflow/harness.d.ts.map +1 -0
  109. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts +2 -0
  110. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts.map +1 -0
  111. package/.tsbuild/test/workflow/oauth.test.d.ts +2 -0
  112. package/.tsbuild/test/workflow/oauth.test.d.ts.map +1 -0
  113. package/.tsbuild/test/workflow/resolvers.test.d.ts +2 -0
  114. package/.tsbuild/test/workflow/resolvers.test.d.ts.map +1 -0
  115. package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
  116. package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
  117. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts +2 -0
  118. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts.map +1 -0
  119. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts +2 -0
  120. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts.map +1 -0
  121. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts +2 -0
  122. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts.map +1 -0
  123. package/.tsbuild/test/workflow/webhook-signed.test.d.ts +2 -0
  124. package/.tsbuild/test/workflow/webhook-signed.test.d.ts.map +1 -0
  125. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts +2 -0
  126. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts.map +1 -0
  127. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts +2 -0
  128. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts.map +1 -0
  129. package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
  130. package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
  131. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
  132. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
  133. package/.tsbuild/test/workflow-read-gate.test.d.ts +2 -0
  134. package/.tsbuild/test/workflow-read-gate.test.d.ts.map +1 -0
  135. package/.tsbuild/test/workflow-runtime.test.d.ts +2 -0
  136. package/.tsbuild/test/workflow-runtime.test.d.ts.map +1 -0
  137. package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
  138. package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
  139. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  140. package/.tsbuild/tsdown.config.d.ts.map +1 -1
  141. package/.tsbuild/vitest.config.d.ts.map +1 -1
  142. package/Auth.md +144 -2
  143. package/CHANGELOG.md +1284 -0
  144. package/README.md +102 -22
  145. package/dist/boot.d.mts +46 -0
  146. package/dist/boot.d.mts.map +1 -0
  147. package/dist/boot.mjs +67 -0
  148. package/dist/boot.mjs.map +1 -0
  149. package/dist/config/from-env.d.mts +15 -0
  150. package/dist/config/from-env.d.mts.map +1 -0
  151. package/dist/config/from-env.mjs +4 -0
  152. package/dist/config/schema.d.mts +3 -0
  153. package/dist/config/schema.mjs +4 -0
  154. package/dist/from-env-FH4fPeIE.mjs +481 -0
  155. package/dist/from-env-FH4fPeIE.mjs.map +1 -0
  156. package/dist/index.d.mts +2 -1
  157. package/dist/index.mjs +84 -84
  158. package/dist/index.mjs.map +1 -1
  159. package/dist/install-packages.d.mts +2 -1
  160. package/dist/install-packages.mjs +2 -2
  161. package/dist/install-packages.mjs.map +1 -1
  162. package/dist/mcp-SsBK14Jm.mjs +32 -0
  163. package/dist/mcp-SsBK14Jm.mjs.map +1 -0
  164. package/dist/migrate.d.mts +2 -1
  165. package/dist/migrate.mjs +7 -5
  166. package/dist/migrate.mjs.map +1 -1
  167. package/dist/plugin-host-CK9_tVak.d.mts +46 -0
  168. package/dist/plugin-host-CK9_tVak.d.mts.map +1 -0
  169. package/dist/plugins/ai.d.mts +37 -0
  170. package/dist/plugins/ai.d.mts.map +1 -0
  171. package/dist/plugins/ai.mjs +400 -0
  172. package/dist/plugins/ai.mjs.map +1 -0
  173. package/dist/plugins/attachments.d.mts +33 -0
  174. package/dist/plugins/attachments.d.mts.map +1 -0
  175. package/dist/plugins/attachments.mjs +4 -0
  176. package/dist/plugins/mcp.d.mts +31 -0
  177. package/dist/plugins/mcp.d.mts.map +1 -0
  178. package/dist/plugins/mcp.mjs +4 -0
  179. package/dist/plugins/privacy.d.mts +22 -0
  180. package/dist/plugins/privacy.d.mts.map +1 -0
  181. package/dist/plugins/privacy.mjs +4 -0
  182. package/dist/plugins/workflows.d.mts +79 -0
  183. package/dist/plugins/workflows.d.mts.map +1 -0
  184. package/dist/plugins/workflows.mjs +4 -0
  185. package/dist/postgres-url-DTDhBjXX.mjs +11 -0
  186. package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
  187. package/dist/privacy-BO8Q25xI.mjs +44 -0
  188. package/dist/privacy-BO8Q25xI.mjs.map +1 -0
  189. package/dist/privacy-DxIJGH4P.mjs +91 -0
  190. package/dist/privacy-DxIJGH4P.mjs.map +1 -0
  191. package/dist/renown-layers-DmbWLt46.mjs +42 -0
  192. package/dist/renown-layers-DmbWLt46.mjs.map +1 -0
  193. package/dist/schema-CsY8bjsu.mjs +329 -0
  194. package/dist/schema-CsY8bjsu.mjs.map +1 -0
  195. package/dist/schema-MAUTnmj1.d.mts +251 -0
  196. package/dist/schema-MAUTnmj1.d.mts.map +1 -0
  197. package/dist/server-CirQ9hyu.mjs +2732 -0
  198. package/dist/server-CirQ9hyu.mjs.map +1 -0
  199. package/dist/server.d.mts +129 -189
  200. package/dist/server.d.mts.map +1 -1
  201. package/dist/server.mjs +5 -5
  202. package/dist/types-CidZdkhU.d.mts +314 -0
  203. package/dist/types-CidZdkhU.d.mts.map +1 -0
  204. package/dist/utils-Cm0TE6dP.mjs +148 -0
  205. package/dist/utils-Cm0TE6dP.mjs.map +1 -0
  206. package/dist/utils.d.mts +30 -5
  207. package/dist/utils.d.mts.map +1 -1
  208. package/dist/utils.mjs +5 -4
  209. package/dist/worker-pool-gyxJnCxA.mjs +304 -0
  210. package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
  211. package/dist/workflow-runtime-CF5pc8mt.d.mts +33 -0
  212. package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -0
  213. package/dist/workflows-C9BQlyT0.mjs +1943 -0
  214. package/dist/workflows-C9BQlyT0.mjs.map +1 -0
  215. package/package.json +85 -24
  216. package/scripts/install-packages.sh +0 -0
  217. package/test/ai-gateway.test.ts +612 -0
  218. package/test/attachment-reference-read-model.test.ts +28 -20
  219. package/test/attachments/auth.test.ts +85 -0
  220. package/test/attachments/byte-grants.test.ts +290 -0
  221. package/test/attachments/byte-routes-read-gate.integration.test.ts +314 -0
  222. package/test/attachments/download-target.test.ts +211 -32
  223. package/test/attachments/index.test.ts +47 -4
  224. package/test/attachments/reserve-metadata-parity.test.ts +96 -0
  225. package/test/attachments/routes-integration.test.ts +60 -4
  226. package/test/attachments/routes.test.ts +297 -35
  227. package/test/attachments/service-config.test.ts +41 -19
  228. package/test/attachments/url-signer.test.ts +105 -0
  229. package/test/boot-auth-config.test.ts +109 -0
  230. package/test/boot-create-signature-policy.test.ts +110 -0
  231. package/test/boot-desktop-auth.test.ts +278 -0
  232. package/test/boot-env-isolation.test.ts +26 -0
  233. package/test/boot-helpers.ts +19 -0
  234. package/test/boot-inspection-enabled.test.ts +140 -0
  235. package/test/boot-mcp-auth.test.ts +449 -0
  236. package/test/boot-mcp-enabled.test.ts +119 -0
  237. package/test/boot-port-zero.test.ts +101 -0
  238. package/test/boot-preconditions.test.ts +213 -0
  239. package/test/boot-privacy.test.ts +232 -0
  240. package/test/boot-ready-drives.test.ts +141 -0
  241. package/test/boot-undo-v3-flag.test.ts +120 -0
  242. package/test/boot-unsupported-stored-documents.test.ts +214 -0
  243. package/test/boot-without-renown.test.ts +83 -0
  244. package/test/boot-without-require-signatures.test.ts +76 -0
  245. package/test/boot-worker-trust-policy.test.ts +91 -0
  246. package/test/boot-workflow-singleton-conflict.test.ts +109 -0
  247. package/test/boot-workflow-singleton-retry.test.ts +122 -0
  248. package/test/builder-defaults.test.ts +80 -0
  249. package/test/config-from-env.test.ts +963 -0
  250. package/test/config-merge.test.ts +248 -0
  251. package/test/config-schema.test.ts +167 -0
  252. package/test/create-signature-policy.test.ts +69 -0
  253. package/test/default-drive-protocol-versions.test.ts +79 -0
  254. package/test/default-drive-slug.test.ts +265 -0
  255. package/test/delegated-publish.test.ts +435 -0
  256. package/test/durable-node-fs-pg16.test.ts +80 -0
  257. package/test/fatal-shutdown.test.ts +205 -0
  258. package/test/goldens/packages.graphql +797 -0
  259. package/test/goldens/privacy.graphql +853 -0
  260. package/test/goldens/reactor-drive-nodes.graphql +827 -0
  261. package/test/goldens/resolvers.json +327 -0
  262. package/test/goldens/supergraph.graphql +5918 -0
  263. package/test/goldens/workflow-runtime.graphql +1548 -0
  264. package/test/inspection-throttle.test.ts +23 -0
  265. package/test/keyframe-reclaim-store.test.ts +156 -0
  266. package/test/observability-signals.test.ts +131 -0
  267. package/test/permissions-backfill.test.ts +486 -0
  268. package/test/pglite-dialect.test.ts +237 -2
  269. package/test/pglite-preflight.test.ts +313 -0
  270. package/test/pglite-storage-dirs.test.ts +113 -0
  271. package/test/pglite-store.test.ts +441 -0
  272. package/test/pglite-version.test.ts +9 -1
  273. package/test/plugin-config-schemas.test.ts +83 -0
  274. package/test/plugin-presence.test.ts +262 -0
  275. package/test/port-probe.test.ts +45 -0
  276. package/test/processor-timing-log.test.ts +109 -0
  277. package/test/reactor-feature-flags.test.ts +20 -0
  278. package/test/redact-webhook-path.test.ts +9 -0
  279. package/test/renown-storage-path.test.ts +40 -0
  280. package/test/renown-trust-policy.test.ts +313 -0
  281. package/test/served-schemas.test.ts +134 -0
  282. package/test/shutdown.test.ts +278 -0
  283. package/test/snapshot-writer.ts +94 -0
  284. package/test/store-lock.test.ts +65 -0
  285. package/test/two-switchboards.test.ts +146 -0
  286. package/test/unsupported-stored-documents.test.ts +72 -0
  287. package/test/worker-pool.test.ts +27 -1
  288. package/test/workflow/harness.ts +289 -0
  289. package/test/workflow/oauth-apps-config.test.ts +66 -0
  290. package/test/workflow/oauth.test.ts +286 -0
  291. package/test/workflow/resolvers.test.ts +488 -0
  292. package/test/workflow/runtime-host.test.ts +19 -0
  293. package/test/workflow/waitpoint-routes.test.ts +306 -0
  294. package/test/workflow/waitpoint-runtime.test.ts +204 -0
  295. package/test/workflow/webhook-dedupe.test.ts +295 -0
  296. package/test/workflow/webhook-signed.test.ts +509 -0
  297. package/test/workflow/webhook-trigger-test.test.ts +82 -0
  298. package/test/workflow/webhook-unsigned.test.ts +345 -0
  299. package/test/workflow-reactor-access.test.ts +384 -0
  300. package/test/workflow-reactor-enforcement.test.ts +808 -0
  301. package/test/workflow-read-gate.test.ts +213 -0
  302. package/test/workflow-runtime.test.ts +1254 -0
  303. package/test/workflows-plugin-config.test.ts +77 -0
  304. package/tsconfig.json +11 -2
  305. package/tsdown.config.ts +13 -1
  306. package/vitest.config.ts +65 -0
  307. package/dist/server-CiKkVE6g.mjs +0 -1555
  308. package/dist/server-CiKkVE6g.mjs.map +0 -1
  309. package/dist/utils-Baw7rThP.mjs +0 -80
  310. package/dist/utils-Baw7rThP.mjs.map +0 -1
@@ -1,11 +1,12 @@
1
1
  import { PGlite } from "@electric-sql/pglite";
2
2
  import {
3
+ AttachmentAlreadyExists,
3
4
  AttachmentBuilder,
4
5
  S3AttachmentBackend,
5
6
  type AttachmentBuildResult,
6
7
  createRemoteAttachmentService,
7
8
  } from "@powerhousedao/reactor-attachments";
8
- import type { API } from "@powerhousedao/reactor-api";
9
+ import type { API, IAttachmentAccessService } from "@powerhousedao/reactor-api";
9
10
  import { createHttpAdapter } from "@powerhousedao/reactor-api";
10
11
  import { Kysely } from "kysely";
11
12
  import { PGliteDialect } from "kysely-pglite-dialect";
@@ -22,6 +23,17 @@ import { registerAttachmentRoutes } from "../../src/attachments/index.js";
22
23
  const EMPTY_STRING_SHA256 =
23
24
  "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
24
25
 
26
+ const DOC_ID = "doc-1";
27
+ const ALLOW_ALL: IAttachmentAccessService = {
28
+ canReadAttachment: (request) =>
29
+ Promise.resolve({
30
+ kind: "allowed",
31
+ documentId: request.documentId as never,
32
+ ref: request.attachmentRef as never,
33
+ }),
34
+ admitCaller: () => Promise.resolve({ kind: "admitted" }),
35
+ };
36
+
25
37
  describe("attachment routes through the real Express middleware stack", () => {
26
38
  let attachments: AttachmentBuildResult;
27
39
  let kysely: Kysely<unknown>;
@@ -42,10 +54,11 @@ describe("attachment routes through the real Express middleware stack", () => {
42
54
  registerAttachmentRoutes({
43
55
  httpAdapter: adapter,
44
56
  attachments,
57
+ attachmentAccess: ALLOW_ALL,
45
58
  authService: undefined,
46
59
  } as unknown as API);
47
60
 
48
- server = await adapter.listen(0);
61
+ server = await adapter.listen(0, undefined, "127.0.0.1");
49
62
  const addr = server.address();
50
63
  if (!addr || typeof addr === "string") throw new Error("no addr");
51
64
  baseUrl = `http://127.0.0.1:${addr.port}`;
@@ -82,7 +95,7 @@ describe("attachment routes through the real Express middleware stack", () => {
82
95
  expect(result.hash).not.toBe(EMPTY_STRING_SHA256);
83
96
  expect(result.header.sizeBytes).toBe(bytes.byteLength);
84
97
 
85
- const got = await service.get(result.ref);
98
+ const got = await service.get(result.ref, { documentId: DOC_ID });
86
99
  expect(got.header.sizeBytes).toBe(bytes.byteLength);
87
100
  expect(got.header.mimeType).toBe("application/json");
88
101
  const reader = got.body.getReader();
@@ -101,6 +114,48 @@ describe("attachment routes through the real Express middleware stack", () => {
101
114
  }
102
115
  expect(new TextDecoder().decode(merged)).toBe(payload);
103
116
  });
117
+
118
+ it("HEAD and GET /attachments/:hash serve a file name above U+00FF", async () => {
119
+ const fileName = "opłata – 報告 📎.pdf";
120
+ const service = createRemoteAttachmentService({ remoteUrl: baseUrl });
121
+ const upload = await service.reserve({
122
+ mimeType: "application/pdf",
123
+ fileName,
124
+ });
125
+ const bytes = new TextEncoder().encode("%PDF-unicode-name");
126
+ const { hash } = await upload.send(
127
+ new ReadableStream<Uint8Array>({
128
+ start(controller) {
129
+ controller.enqueue(bytes);
130
+ controller.close();
131
+ },
132
+ }),
133
+ );
134
+
135
+ for (const method of ["HEAD", "GET"]) {
136
+ const res = await fetch(
137
+ `${baseUrl}/attachments/${hash}?documentId=${DOC_ID}`,
138
+ { method },
139
+ );
140
+ expect(res.status).toBe(200);
141
+ const meta = JSON.parse(res.headers.get("attachment-metadata")!) as {
142
+ fileName: string;
143
+ };
144
+ expect(meta.fileName).toBe(fileName);
145
+ await res.arrayBuffer();
146
+ }
147
+
148
+ // A hash-first reserve of the same bytes is what the client sends on the
149
+ // next upload; it stats the hash first.
150
+ await expect(
151
+ service.reserve({
152
+ mimeType: "application/pdf",
153
+ fileName,
154
+ clientHash: hash,
155
+ sizeBytes: bytes.byteLength,
156
+ }),
157
+ ).rejects.toBeInstanceOf(AttachmentAlreadyExists);
158
+ });
104
159
  });
105
160
 
106
161
  describe("authenticated S3 reservation production path", () => {
@@ -138,9 +193,10 @@ describe("authenticated S3 reservation production path", () => {
138
193
  registerAttachmentRoutes({
139
194
  httpAdapter: adapter,
140
195
  attachments,
196
+ attachmentAccess: ALLOW_ALL,
141
197
  authService: { verifyBearer },
142
198
  } as unknown as API);
143
- const server = await adapter.listen(0);
199
+ const server = await adapter.listen(0, undefined, "127.0.0.1");
144
200
  const address = server.address();
145
201
  if (!address || typeof address === "string") throw new Error("no addr");
146
202
  const baseUrl = `http://127.0.0.1:${address.port}`;
@@ -5,14 +5,21 @@ import {
5
5
  } from "@powerhousedao/reactor-attachments";
6
6
  import { Kysely } from "kysely";
7
7
  import { PGliteDialect } from "kysely-pglite-dialect";
8
- import { mkdtemp, rm } from "node:fs/promises";
9
- import type { IncomingMessage, ServerResponse } from "node:http";
8
+ import { mkdtemp, readdir, rm } from "node:fs/promises";
9
+ import {
10
+ validateHeaderValue,
11
+ type IncomingMessage,
12
+ type ServerResponse,
13
+ } from "node:http";
10
14
  import { tmpdir } from "node:os";
11
15
  import { join } from "node:path";
12
16
  import { Readable, Writable } from "node:stream";
17
+ import type { IAttachmentAccessService } from "@powerhousedao/reactor-api";
13
18
  import { afterEach, beforeEach, describe, expect, it } from "vitest";
14
19
  import {
15
20
  buildContentDisposition,
21
+ buildMetadataHeader,
22
+ buildPendingHeader,
16
23
  makeDeleteReservationHandler,
17
24
  makeDownloadHandler,
18
25
  makeGetReservationHandler,
@@ -23,6 +30,22 @@ import {
23
30
  quoteFilename,
24
31
  } from "../../src/attachments/routes.js";
25
32
 
33
+ // Byte-serving mechanics under a granted read; grant decisions are tested in
34
+ // byte-grants.test.ts.
35
+ const ALLOW_ALL: IAttachmentAccessService = {
36
+ canReadAttachment: (request) =>
37
+ Promise.resolve({
38
+ kind: "allowed",
39
+ documentId: request.documentId as never,
40
+ ref: request.attachmentRef as never,
41
+ }),
42
+ admitCaller: () => Promise.resolve({ kind: "admitted" }),
43
+ };
44
+ const grantedDownload = (attachments: AttachmentBuildResult) =>
45
+ makeDownloadHandler(attachments, ALLOW_ALL, null);
46
+ const grantedStat = (attachments: AttachmentBuildResult) =>
47
+ makeStatHandler(attachments, ALLOW_ALL, null);
48
+
26
49
  type CapturedRes = ServerResponse & {
27
50
  _headers: Record<string, string>;
28
51
  _body: Buffer;
@@ -46,7 +69,7 @@ function makeReq(opts: {
46
69
  params?: Record<string, string>;
47
70
  };
48
71
  req.method = opts.method;
49
- req.url = opts.url ?? "/";
72
+ req.url = opts.url ?? "/?documentId=doc-1";
50
73
  req.headers = {};
51
74
  if (opts.params) req.params = opts.params;
52
75
  return req as unknown as IncomingMessage;
@@ -70,13 +93,25 @@ function makeRes(): CapturedRes {
70
93
  statusCode: 200,
71
94
  _headers: headers,
72
95
  setHeader(name: string, value: string | number | readonly string[]) {
96
+ // Same check Node's ServerResponse applies, so a header value it would
97
+ // reject fails here instead of passing silently.
98
+ validateHeaderValue(name, String(value));
73
99
  headers[name.toLowerCase()] = String(value);
74
100
  },
75
101
  getHeader(name: string) {
76
102
  return headers[name.toLowerCase()];
77
103
  },
104
+ removeHeader(name: string) {
105
+ delete headers[name.toLowerCase()];
106
+ },
78
107
  _done: done,
79
108
  });
109
+ // Like ServerResponse: headers count as sent once the first byte is written.
110
+ Object.defineProperty(writable, "headersSent", {
111
+ get(): boolean {
112
+ return chunks.length > 0;
113
+ },
114
+ });
80
115
  Object.defineProperty(writable, "_body", {
81
116
  get(): Buffer {
82
117
  return Buffer.concat(chunks);
@@ -213,7 +248,7 @@ describe("attachment routes", () => {
213
248
  expect(row!.deleted_at_utc).not.toBeNull();
214
249
 
215
250
  // Download
216
- const downloadHandler = makeDownloadHandler(attachments);
251
+ const downloadHandler = grantedDownload(attachments);
217
252
  const downloadReq = makeReq({
218
253
  method: "GET",
219
254
  params: { hash: upload.hash },
@@ -262,7 +297,7 @@ describe("attachment routes", () => {
262
297
  hash: string;
263
298
  };
264
299
 
265
- const downloadHandler = makeDownloadHandler(attachments);
300
+ const downloadHandler = grantedDownload(attachments);
266
301
  const downloadReq = makeReq({
267
302
  method: "GET",
268
303
  params: { hash: upload.hash },
@@ -305,7 +340,7 @@ describe("attachment routes", () => {
305
340
  hash: string;
306
341
  };
307
342
 
308
- const statHandler = makeStatHandler(attachments);
343
+ const statHandler = grantedStat(attachments);
309
344
  const statReq = makeReq({
310
345
  method: "HEAD",
311
346
  params: { hash: upload.hash },
@@ -364,9 +399,9 @@ describe("attachment routes", () => {
364
399
  };
365
400
 
366
401
  const { createServer } = await import("node:http");
367
- const statHandler = makeStatHandler(attachments);
402
+ const statHandler = grantedStat(attachments);
368
403
  const server = createServer((req, res) => {
369
- const m = /^\/attachments\/([^/]+)$/.exec(req.url ?? "/");
404
+ const m = /^\/attachments\/([^/?]+)(?:\?|$)/.exec(req.url ?? "/");
370
405
  if (!m) {
371
406
  res.statusCode = 404;
372
407
  res.end();
@@ -377,12 +412,14 @@ describe("attachment routes", () => {
377
412
  };
378
413
  void statHandler(req, res);
379
414
  });
380
- await new Promise<void>((resolve) => server.listen(0, resolve));
415
+ await new Promise<void>((resolve) =>
416
+ server.listen(0, "127.0.0.1", resolve),
417
+ );
381
418
  const port = (server.address() as { port: number }).port;
382
419
 
383
420
  try {
384
421
  const response = await fetch(
385
- `http://127.0.0.1:${port}/attachments/${upload.hash}`,
422
+ `http://127.0.0.1:${port}/attachments/${upload.hash}?documentId=doc-1`,
386
423
  {
387
424
  method: "HEAD",
388
425
  },
@@ -403,7 +440,7 @@ describe("attachment routes", () => {
403
440
  });
404
441
 
405
442
  it("HEAD stat returns 404 for unknown hash", async () => {
406
- const handler = makeStatHandler(attachments);
443
+ const handler = grantedStat(attachments);
407
444
  const req = makeReq({ method: "HEAD", params: { hash: "a".repeat(64) } });
408
445
  const res = makeRes();
409
446
  await handler(req, res);
@@ -412,7 +449,7 @@ describe("attachment routes", () => {
412
449
  });
413
450
 
414
451
  it("HEAD stat returns 400 for malformed hash", async () => {
415
- const handler = makeStatHandler(attachments);
452
+ const handler = grantedStat(attachments);
416
453
  const req = makeReq({ method: "HEAD", params: { hash: "not-a-hash" } });
417
454
  const res = makeRes();
418
455
  await handler(req, res);
@@ -514,7 +551,7 @@ describe("attachment routes", () => {
514
551
  });
515
552
 
516
553
  it("GET download returns 404 for unknown hash", async () => {
517
- const handler = makeDownloadHandler(attachments);
554
+ const handler = grantedDownload(attachments);
518
555
  const req = makeReq({
519
556
  method: "GET",
520
557
  params: { hash: "a".repeat(64) },
@@ -526,7 +563,7 @@ describe("attachment routes", () => {
526
563
  });
527
564
 
528
565
  it("GET download returns 400 for malformed hash", async () => {
529
- const handler = makeDownloadHandler(attachments);
566
+ const handler = grantedDownload(attachments);
530
567
  const req = makeReq({
531
568
  method: "GET",
532
569
  params: { hash: "not-a-hash" },
@@ -565,7 +602,7 @@ describe("attachment routes", () => {
565
602
  hash: string;
566
603
  };
567
604
 
568
- const handler = makeDownloadHandler(attachments);
605
+ const handler = grantedDownload(attachments);
569
606
  const req = makeReq({
570
607
  method: "GET",
571
608
  params: { hash: upload.hash.toUpperCase() },
@@ -603,7 +640,7 @@ describe("attachment routes", () => {
603
640
  hash: string;
604
641
  };
605
642
 
606
- const statHandler = makeStatHandler(attachments);
643
+ const statHandler = grantedStat(attachments);
607
644
  const statReq = makeReq({
608
645
  method: "HEAD",
609
646
  params: { hash: upload.hash.toUpperCase() },
@@ -651,7 +688,7 @@ describe("attachment routes", () => {
651
688
  throw new Error(secret);
652
689
  };
653
690
  try {
654
- const handler = makeDownloadHandler(attachments);
691
+ const handler = grantedDownload(attachments);
655
692
  const req = makeReq({
656
693
  method: "GET",
657
694
  params: { hash: "a".repeat(64) },
@@ -668,6 +705,101 @@ describe("attachment routes", () => {
668
705
  }
669
706
  });
670
707
 
708
+ async function uploadText(payload: string): Promise<string> {
709
+ const reserveRes = makeRes();
710
+ await makeReserveHandler(attachments)(
711
+ makeReq({
712
+ method: "POST",
713
+ body: JSON.stringify({ mimeType: "text/plain", fileName: "a.txt" }),
714
+ }),
715
+ reserveRes,
716
+ );
717
+ await waitFor(reserveRes);
718
+ const { reservationId } = JSON.parse(reserveRes._body.toString("utf8")) as {
719
+ reservationId: string;
720
+ };
721
+ const uploadRes = makeRes();
722
+ await makeUploadHandler(attachments)(
723
+ makeReq({ method: "PUT", params: { reservationId }, body: payload }),
724
+ uploadRes,
725
+ );
726
+ await waitFor(uploadRes);
727
+ return (JSON.parse(uploadRes._body.toString("utf8")) as { hash: string })
728
+ .hash;
729
+ }
730
+
731
+ // The blob store is a directory; its metadata lives in the database. When
732
+ // the directory is lost (a pod-local tmpdir after a restart) the metadata
733
+ // still says "available". Reading such a blob used to raise an unhandled
734
+ // stream error that took the whole process down.
735
+ it("GET download returns 404 when the blob file is missing from disk", async () => {
736
+ const hash = await uploadText("gone");
737
+ const files = await readdir(storagePath, {
738
+ recursive: true,
739
+ withFileTypes: true,
740
+ });
741
+ const blobs = files.filter((f) => f.isFile() && f.name.startsWith(hash));
742
+ expect(blobs.length).toBeGreaterThan(0);
743
+ for (const f of blobs) await rm(join(f.parentPath, f.name));
744
+
745
+ const res = makeRes();
746
+ await grantedDownload(attachments)(
747
+ makeReq({ method: "GET", params: { hash } }),
748
+ res,
749
+ );
750
+ await waitFor(res);
751
+
752
+ expect(res.statusCode).toBe(404);
753
+ expect(JSON.parse(res._body.toString("utf8"))).toEqual({
754
+ error: "Attachment not found",
755
+ });
756
+ // The download headers were set before the read failed; none may leak
757
+ // onto the JSON error.
758
+ expect(res.getHeader("content-length")).toBeUndefined();
759
+ expect(res.getHeader("content-disposition")).toBeUndefined();
760
+ expect(res.getHeader("attachment-metadata")).toBeUndefined();
761
+ });
762
+
763
+ it("GET download aborts the response when the blob fails mid-stream", async () => {
764
+ const hash = await uploadText("partial");
765
+ const originalGet = attachments.store.get.bind(attachments.store);
766
+ const real = await originalGet(hash as never);
767
+ let sentFirst = false;
768
+ attachments.store.get = () =>
769
+ Promise.resolve({
770
+ header: real.header,
771
+ // First chunk flows to the response; the read fails only after it
772
+ // has been written, the way a disk error partway through a file would.
773
+ body: new ReadableStream<Uint8Array>({
774
+ async pull(controller) {
775
+ if (!sentFirst) {
776
+ sentFirst = true;
777
+ controller.enqueue(new TextEncoder().encode("part"));
778
+ return;
779
+ }
780
+ await new Promise((resolve) => setTimeout(resolve, 20));
781
+ controller.error(new Error("EIO: read failed"));
782
+ },
783
+ }) as never,
784
+ });
785
+ try {
786
+ const res = makeRes();
787
+ const closed = new Promise<void>((resolve) => res.once("close", resolve));
788
+ await grantedDownload(attachments)(
789
+ makeReq({ method: "GET", params: { hash } }),
790
+ res,
791
+ );
792
+ await closed;
793
+ // Bytes were already on the wire: the only honest signal left is to
794
+ // cut the connection, never to finish it as if it were complete.
795
+ expect(res._body.toString("utf8")).toBe("part");
796
+ expect(res.destroyed).toBe(true);
797
+ expect(res.writableFinished).toBe(false);
798
+ } finally {
799
+ attachments.store.get = originalGet;
800
+ }
801
+ });
802
+
671
803
  it("identical uploads dedupe to the same hash", async () => {
672
804
  const reserveHandler = makeReserveHandler(attachments);
673
805
  const uploadHandler = makeUploadHandler(attachments);
@@ -899,6 +1031,7 @@ describe("attachment routes", () => {
899
1031
  describe("pending state: stat and download 202 responses", () => {
900
1032
  async function createPendingReservation(
901
1033
  content: string,
1034
+ fileName = "pending.txt",
902
1035
  ): Promise<{ hash: string }> {
903
1036
  const { createHash } = await import("node:crypto");
904
1037
  const hash = createHash("sha256").update(content, "utf8").digest("hex");
@@ -908,7 +1041,7 @@ describe("attachment routes", () => {
908
1041
  method: "POST",
909
1042
  body: JSON.stringify({
910
1043
  mimeType: "text/plain",
911
- fileName: "pending.txt",
1044
+ fileName,
912
1045
  clientHash: hash,
913
1046
  sizeBytes: Buffer.byteLength(content, "utf8"),
914
1047
  }),
@@ -923,7 +1056,7 @@ describe("attachment routes", () => {
923
1056
  it("HEAD stat returns 202 with Retry-After and Attachment-Pending for pending hash", async () => {
924
1057
  const { hash } = await createPendingReservation("pending content data");
925
1058
 
926
- const handler = makeStatHandler(attachments);
1059
+ const handler = grantedStat(attachments);
927
1060
  const req = makeReq({ method: "HEAD", params: { hash } });
928
1061
  const res = makeRes();
929
1062
  await handler(req, res);
@@ -948,7 +1081,7 @@ describe("attachment routes", () => {
948
1081
  it("HEAD stat 202 for pending hash does not set Content-Disposition or Attachment-Metadata", async () => {
949
1082
  const { hash } = await createPendingReservation("no metadata content");
950
1083
 
951
- const handler = makeStatHandler(attachments);
1084
+ const handler = grantedStat(attachments);
952
1085
  const req = makeReq({ method: "HEAD", params: { hash } });
953
1086
  const res = makeRes();
954
1087
  await handler(req, res);
@@ -962,7 +1095,7 @@ describe("attachment routes", () => {
962
1095
  it("GET download returns 202 with Retry-After and Attachment-Pending for pending hash", async () => {
963
1096
  const { hash } = await createPendingReservation("download pending data");
964
1097
 
965
- const handler = makeDownloadHandler(attachments);
1098
+ const handler = grantedDownload(attachments);
966
1099
  const req = makeReq({ method: "GET", params: { hash } });
967
1100
  const res = makeRes();
968
1101
  await handler(req, res);
@@ -984,7 +1117,7 @@ describe("attachment routes", () => {
984
1117
  // the response as a successful zero-byte attachment.
985
1118
  const { hash } = await createPendingReservation("check-headers content");
986
1119
 
987
- const handler = makeDownloadHandler(attachments);
1120
+ const handler = grantedDownload(attachments);
988
1121
  const req = makeReq({ method: "GET", params: { hash } });
989
1122
  const res = makeRes();
990
1123
  await handler(req, res);
@@ -995,10 +1128,36 @@ describe("attachment routes", () => {
995
1128
  expect(res.getHeader("attachment-metadata")).toBeFalsy();
996
1129
  });
997
1130
 
1131
+ it.each(["opłata.pdf", "a – b’s.txt", "📎.png", "報告書.pdf"])(
1132
+ "HEAD and GET 202 carry a parseable Attachment-Pending for %s",
1133
+ async (fileName) => {
1134
+ const { hash } = await createPendingReservation(
1135
+ `pending ${fileName}`,
1136
+ fileName,
1137
+ );
1138
+
1139
+ for (const [method, handler] of [
1140
+ ["HEAD", grantedStat(attachments)],
1141
+ ["GET", grantedDownload(attachments)],
1142
+ ] as const) {
1143
+ const req = makeReq({ method, params: { hash } });
1144
+ const res = makeRes();
1145
+ await handler(req, res);
1146
+ await waitFor(res);
1147
+
1148
+ expect(res.statusCode).toBe(202);
1149
+ const parsed = JSON.parse(
1150
+ res.getHeader("attachment-pending") as string,
1151
+ ) as { fileName: string };
1152
+ expect(parsed.fileName).toBe(fileName);
1153
+ }
1154
+ },
1155
+ );
1156
+
998
1157
  it("GET download 202 body is empty (not a zero-byte attachment)", async () => {
999
1158
  const { hash } = await createPendingReservation("empty body assertion");
1000
1159
 
1001
- const handler = makeDownloadHandler(attachments);
1160
+ const handler = grantedDownload(attachments);
1002
1161
  const req = makeReq({ method: "GET", params: { hash } });
1003
1162
  const res = makeRes();
1004
1163
  await handler(req, res);
@@ -1044,6 +1203,32 @@ describe("attachment routes", () => {
1044
1203
  ).toBeNull();
1045
1204
  });
1046
1205
 
1206
+ it("parseReserveOptions accepts an empty file in hash-first mode", () => {
1207
+ const emptyHash =
1208
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
1209
+ const opts = parseReserveOptions({
1210
+ mimeType: "application/octet-stream",
1211
+ fileName: "empty",
1212
+ clientHash: emptyHash,
1213
+ sizeBytes: 0,
1214
+ });
1215
+ expect(opts).not.toBeNull();
1216
+ expect(opts!.sizeBytes).toBe(0);
1217
+ });
1218
+
1219
+ it("parseReserveOptions rejects a negative or fractional sizeBytes", () => {
1220
+ for (const sizeBytes of [-1, 1.5]) {
1221
+ expect(
1222
+ parseReserveOptions({
1223
+ mimeType: "text/plain",
1224
+ fileName: "file.txt",
1225
+ clientHash: "a".repeat(64),
1226
+ sizeBytes,
1227
+ }),
1228
+ ).toBeNull();
1229
+ }
1230
+ });
1231
+
1047
1232
  it("parseReserveOptions rejects when clientHash is present but sizeBytes is absent", () => {
1048
1233
  expect(
1049
1234
  parseReserveOptions({
@@ -1068,17 +1253,6 @@ describe("attachment routes", () => {
1068
1253
  });
1069
1254
  });
1070
1255
 
1071
- it("parseReserveOptions rejects sizeBytes of zero", () => {
1072
- expect(
1073
- parseReserveOptions({
1074
- mimeType: "text/plain",
1075
- fileName: "file.txt",
1076
- clientHash: "a".repeat(64),
1077
- sizeBytes: 0,
1078
- }),
1079
- ).toBeNull();
1080
- });
1081
-
1082
1256
  it("parseReserveOptions rejects negative sizeBytes", () => {
1083
1257
  expect(
1084
1258
  parseReserveOptions({
@@ -1215,6 +1389,54 @@ describe("attachment routes", () => {
1215
1389
  }
1216
1390
  });
1217
1391
 
1392
+ const NON_LATIN1_NAMES = [
1393
+ "opłata.pdf",
1394
+ "a – b’s.txt",
1395
+ "📎 notes.png",
1396
+ "報告書.pdf",
1397
+ "résumé.pdf",
1398
+ 'quote"back\\slash.txt',
1399
+ ];
1400
+
1401
+ it.each(NON_LATIN1_NAMES)(
1402
+ "buildMetadataHeader output is a valid header that parses back to %s",
1403
+ (fileName) => {
1404
+ const value = buildMetadataHeader({
1405
+ mimeType: "application/pdf",
1406
+ fileName,
1407
+ sizeBytes: 1,
1408
+ extension: null,
1409
+ createdAtUtc: "2026-01-01T00:00:00.000Z",
1410
+ lastAccessedAtUtc: "2026-01-01T00:00:00.000Z",
1411
+ });
1412
+ expect(() =>
1413
+ validateHeaderValue("Attachment-Metadata", value),
1414
+ ).not.toThrow();
1415
+ expect(value).toMatch(/^[\x20-\x7e]*$/);
1416
+ expect((JSON.parse(value) as { fileName: string }).fileName).toBe(
1417
+ fileName,
1418
+ );
1419
+ },
1420
+ );
1421
+
1422
+ it.each(NON_LATIN1_NAMES)(
1423
+ "buildPendingHeader output is a valid header that parses back to %s",
1424
+ (fileName) => {
1425
+ const value = buildPendingHeader({
1426
+ expiresAtUtc: "2026-01-01T00:00:00.000Z",
1427
+ mimeType: "application/pdf",
1428
+ fileName,
1429
+ sizeBytes: 1,
1430
+ });
1431
+ expect(() =>
1432
+ validateHeaderValue("Attachment-Pending", value),
1433
+ ).not.toThrow();
1434
+ expect((JSON.parse(value) as { fileName: string }).fileName).toBe(
1435
+ fileName,
1436
+ );
1437
+ },
1438
+ );
1439
+
1218
1440
  it("buildContentDisposition encodes RFC-5987-reserved chars in the encoded form", () => {
1219
1441
  const value = buildContentDisposition("a'b(c)*!.txt");
1220
1442
  expect(value).toContain("filename*=UTF-8''a%27b%28c%29%2A%21.txt");
@@ -1238,7 +1460,7 @@ describe("attachment routes", () => {
1238
1460
  it("download with non-ASCII fileName produces RFC 6266 Content-Disposition", async () => {
1239
1461
  const reserveHandler = makeReserveHandler(attachments);
1240
1462
  const uploadHandler = makeUploadHandler(attachments);
1241
- const downloadHandler = makeDownloadHandler(attachments);
1463
+ const downloadHandler = grantedDownload(attachments);
1242
1464
 
1243
1465
  const reserveReq = makeReq({
1244
1466
  method: "POST",
@@ -1281,5 +1503,45 @@ describe("attachment routes", () => {
1281
1503
  ) as { fileName: string };
1282
1504
  expect(meta.fileName).toBe("résumé.pdf");
1283
1505
  });
1506
+
1507
+ it("HEAD and GET answer 200 with a parseable Attachment-Metadata for a name above U+00FF", async () => {
1508
+ const fileName = "opłata – 報告 📎.pdf";
1509
+ const reserveReq = makeReq({
1510
+ method: "POST",
1511
+ body: JSON.stringify({ mimeType: "application/pdf", fileName }),
1512
+ });
1513
+ const reserveRes = makeRes();
1514
+ await makeReserveHandler(attachments)(reserveReq, reserveRes);
1515
+ await waitFor(reserveRes);
1516
+ expect(reserveRes.statusCode).toBe(201);
1517
+ const { reservationId } = JSON.parse(
1518
+ reserveRes._body.toString("utf8"),
1519
+ ) as { reservationId: string };
1520
+
1521
+ const uploadRes = makeRes();
1522
+ await makeUploadHandler(attachments)(
1523
+ makeReq({ method: "PUT", params: { reservationId }, body: "pdf" }),
1524
+ uploadRes,
1525
+ );
1526
+ await waitFor(uploadRes);
1527
+ expect(uploadRes.statusCode).toBe(200);
1528
+ const { hash } = JSON.parse(uploadRes._body.toString("utf8")) as {
1529
+ hash: string;
1530
+ };
1531
+
1532
+ for (const [method, handler] of [
1533
+ ["HEAD", grantedStat(attachments)],
1534
+ ["GET", grantedDownload(attachments)],
1535
+ ] as const) {
1536
+ const res = makeRes();
1537
+ await handler(makeReq({ method, params: { hash } }), res);
1538
+ await waitFor(res);
1539
+ expect(res.statusCode).toBe(200);
1540
+ const meta = JSON.parse(
1541
+ res.getHeader("attachment-metadata") as string,
1542
+ ) as { fileName: string };
1543
+ expect(meta.fileName).toBe(fileName);
1544
+ }
1545
+ });
1284
1546
  });
1285
1547
  });