@dads-framework/http 0.0.0-stage → 0.2.0

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 (288) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +295 -2
  3. package/dist/http.tsbuildinfo +1 -0
  4. package/dist/index.d.ts +56 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +37 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/lib/create-http-app.d.ts +108 -0
  9. package/dist/lib/create-http-app.d.ts.map +1 -0
  10. package/dist/lib/create-http-app.js +181 -0
  11. package/dist/lib/create-http-app.js.map +1 -0
  12. package/dist/lib/http-errors.d.ts +52 -0
  13. package/dist/lib/http-errors.d.ts.map +1 -0
  14. package/dist/lib/http-errors.js +66 -0
  15. package/dist/lib/http-errors.js.map +1 -0
  16. package/dist/lib/mcp/mcp-tool.d.ts +94 -0
  17. package/dist/lib/mcp/mcp-tool.d.ts.map +1 -0
  18. package/dist/lib/mcp/mcp-tool.js +11 -0
  19. package/dist/lib/mcp/mcp-tool.js.map +1 -0
  20. package/dist/lib/object-value.d.ts +3 -0
  21. package/dist/lib/object-value.d.ts.map +1 -0
  22. package/dist/lib/object-value.js +5 -0
  23. package/dist/lib/object-value.js.map +1 -0
  24. package/dist/lib/openapi/build-document.d.ts +27 -0
  25. package/dist/lib/openapi/build-document.d.ts.map +1 -0
  26. package/dist/lib/openapi/build-document.js +192 -0
  27. package/dist/lib/openapi/build-document.js.map +1 -0
  28. package/dist/lib/openapi/logic/add-discriminators.d.ts +16 -0
  29. package/dist/lib/openapi/logic/add-discriminators.d.ts.map +1 -0
  30. package/dist/lib/openapi/logic/add-discriminators.js +235 -0
  31. package/dist/lib/openapi/logic/add-discriminators.js.map +1 -0
  32. package/dist/lib/openapi/logic/by-code-point.d.ts +2 -0
  33. package/dist/lib/openapi/logic/by-code-point.d.ts.map +1 -0
  34. package/dist/lib/openapi/logic/by-code-point.js +6 -0
  35. package/dist/lib/openapi/logic/by-code-point.js.map +1 -0
  36. package/dist/lib/openapi/logic/capitalize-initial.d.ts +2 -0
  37. package/dist/lib/openapi/logic/capitalize-initial.d.ts.map +1 -0
  38. package/dist/lib/openapi/logic/capitalize-initial.js +4 -0
  39. package/dist/lib/openapi/logic/capitalize-initial.js.map +1 -0
  40. package/dist/lib/openapi/logic/document-record.d.ts +5 -0
  41. package/dist/lib/openapi/logic/document-record.d.ts.map +1 -0
  42. package/dist/lib/openapi/logic/document-record.js +14 -0
  43. package/dist/lib/openapi/logic/document-record.js.map +1 -0
  44. package/dist/lib/openapi/logic/list-order.d.ts +23 -0
  45. package/dist/lib/openapi/logic/list-order.d.ts.map +1 -0
  46. package/dist/lib/openapi/logic/list-order.js +102 -0
  47. package/dist/lib/openapi/logic/list-order.js.map +1 -0
  48. package/dist/lib/openapi/logic/open-response-objects.d.ts +14 -0
  49. package/dist/lib/openapi/logic/open-response-objects.d.ts.map +1 -0
  50. package/dist/lib/openapi/logic/open-response-objects.js +130 -0
  51. package/dist/lib/openapi/logic/open-response-objects.js.map +1 -0
  52. package/dist/lib/openapi/logic/openapi-parts.d.ts +37 -0
  53. package/dist/lib/openapi/logic/openapi-parts.d.ts.map +1 -0
  54. package/dist/lib/openapi/logic/openapi-parts.js +243 -0
  55. package/dist/lib/openapi/logic/openapi-parts.js.map +1 -0
  56. package/dist/lib/openapi/logic/schema-components.d.ts +5 -0
  57. package/dist/lib/openapi/logic/schema-components.d.ts.map +1 -0
  58. package/dist/lib/openapi/logic/schema-components.js +47 -0
  59. package/dist/lib/openapi/logic/schema-components.js.map +1 -0
  60. package/dist/lib/request-log/authorization-warning-faults.d.ts +30 -0
  61. package/dist/lib/request-log/authorization-warning-faults.d.ts.map +1 -0
  62. package/dist/lib/request-log/authorization-warning-faults.js +35 -0
  63. package/dist/lib/request-log/authorization-warning-faults.js.map +1 -0
  64. package/dist/lib/request-log/capture-request-record.d.ts +50 -0
  65. package/dist/lib/request-log/capture-request-record.d.ts.map +1 -0
  66. package/dist/lib/request-log/capture-request-record.js +132 -0
  67. package/dist/lib/request-log/capture-request-record.js.map +1 -0
  68. package/dist/lib/request-log/request-log-sink.d.ts +31 -0
  69. package/dist/lib/request-log/request-log-sink.d.ts.map +1 -0
  70. package/dist/lib/request-log/request-log-sink.js +26 -0
  71. package/dist/lib/request-log/request-log-sink.js.map +1 -0
  72. package/dist/lib/request-log/request-outcome.d.ts +53 -0
  73. package/dist/lib/request-log/request-outcome.d.ts.map +1 -0
  74. package/dist/lib/request-log/request-outcome.js +96 -0
  75. package/dist/lib/request-log/request-outcome.js.map +1 -0
  76. package/dist/lib/request-log/request-record.d.ts +114 -0
  77. package/dist/lib/request-log/request-record.d.ts.map +1 -0
  78. package/dist/lib/request-log/request-record.js +231 -0
  79. package/dist/lib/request-log/request-record.js.map +1 -0
  80. package/dist/lib/request-pipeline/assert-no-app-site-serving.d.ts +8 -0
  81. package/dist/lib/request-pipeline/assert-no-app-site-serving.d.ts.map +1 -0
  82. package/dist/lib/request-pipeline/assert-no-app-site-serving.js +15 -0
  83. package/dist/lib/request-pipeline/assert-no-app-site-serving.js.map +1 -0
  84. package/dist/lib/request-pipeline/carry-audit-actor.d.ts +71 -0
  85. package/dist/lib/request-pipeline/carry-audit-actor.d.ts.map +1 -0
  86. package/dist/lib/request-pipeline/carry-audit-actor.js +79 -0
  87. package/dist/lib/request-pipeline/carry-audit-actor.js.map +1 -0
  88. package/dist/lib/request-pipeline/carry-authorization-consistency.d.ts +4 -0
  89. package/dist/lib/request-pipeline/carry-authorization-consistency.d.ts.map +1 -0
  90. package/dist/lib/request-pipeline/carry-authorization-consistency.js +21 -0
  91. package/dist/lib/request-pipeline/carry-authorization-consistency.js.map +1 -0
  92. package/dist/lib/request-pipeline/enforce-access.d.ts +146 -0
  93. package/dist/lib/request-pipeline/enforce-access.d.ts.map +1 -0
  94. package/dist/lib/request-pipeline/enforce-access.js +238 -0
  95. package/dist/lib/request-pipeline/enforce-access.js.map +1 -0
  96. package/dist/lib/request-pipeline/enforce-scope.d.ts +23 -0
  97. package/dist/lib/request-pipeline/enforce-scope.d.ts.map +1 -0
  98. package/dist/lib/request-pipeline/enforce-scope.js +58 -0
  99. package/dist/lib/request-pipeline/enforce-scope.js.map +1 -0
  100. package/dist/lib/request-pipeline/envelope.d.ts +17 -0
  101. package/dist/lib/request-pipeline/envelope.d.ts.map +1 -0
  102. package/dist/lib/request-pipeline/envelope.js +4 -0
  103. package/dist/lib/request-pipeline/envelope.js.map +1 -0
  104. package/dist/lib/request-pipeline/error-handler.d.ts +14 -0
  105. package/dist/lib/request-pipeline/error-handler.d.ts.map +1 -0
  106. package/dist/lib/request-pipeline/error-handler.js +138 -0
  107. package/dist/lib/request-pipeline/error-handler.js.map +1 -0
  108. package/dist/lib/request-pipeline/feature-state-preview.d.ts +79 -0
  109. package/dist/lib/request-pipeline/feature-state-preview.d.ts.map +1 -0
  110. package/dist/lib/request-pipeline/feature-state-preview.js +77 -0
  111. package/dist/lib/request-pipeline/feature-state-preview.js.map +1 -0
  112. package/dist/lib/request-pipeline/feature-state.d.ts +70 -0
  113. package/dist/lib/request-pipeline/feature-state.d.ts.map +1 -0
  114. package/dist/lib/request-pipeline/feature-state.js +46 -0
  115. package/dist/lib/request-pipeline/feature-state.js.map +1 -0
  116. package/dist/lib/request-pipeline/logic/is-write-method.d.ts +2 -0
  117. package/dist/lib/request-pipeline/logic/is-write-method.d.ts.map +1 -0
  118. package/dist/lib/request-pipeline/logic/is-write-method.js +7 -0
  119. package/dist/lib/request-pipeline/logic/is-write-method.js.map +1 -0
  120. package/dist/lib/request-pipeline/logic/read-request-source.d.ts +19 -0
  121. package/dist/lib/request-pipeline/logic/read-request-source.d.ts.map +1 -0
  122. package/dist/lib/request-pipeline/logic/read-request-source.js +40 -0
  123. package/dist/lib/request-pipeline/logic/read-request-source.js.map +1 -0
  124. package/dist/lib/request-pipeline/logic/single-request-header.d.ts +3 -0
  125. package/dist/lib/request-pipeline/logic/single-request-header.d.ts.map +1 -0
  126. package/dist/lib/request-pipeline/logic/single-request-header.js +16 -0
  127. package/dist/lib/request-pipeline/logic/single-request-header.js.map +1 -0
  128. package/dist/lib/request-pipeline/machine-credential-access.d.ts +19 -0
  129. package/dist/lib/request-pipeline/machine-credential-access.d.ts.map +1 -0
  130. package/dist/lib/request-pipeline/machine-credential-access.js +28 -0
  131. package/dist/lib/request-pipeline/machine-credential-access.js.map +1 -0
  132. package/dist/lib/request-pipeline/rate-limit.d.ts +15 -0
  133. package/dist/lib/request-pipeline/rate-limit.d.ts.map +1 -0
  134. package/dist/lib/request-pipeline/rate-limit.js +40 -0
  135. package/dist/lib/request-pipeline/rate-limit.js.map +1 -0
  136. package/dist/lib/request-pipeline/raw-route-byte-body.d.ts +19 -0
  137. package/dist/lib/request-pipeline/raw-route-byte-body.d.ts.map +1 -0
  138. package/dist/lib/request-pipeline/raw-route-byte-body.js +26 -0
  139. package/dist/lib/request-pipeline/raw-route-byte-body.js.map +1 -0
  140. package/dist/lib/request-pipeline/refuse-visible-record-list.d.ts +16 -0
  141. package/dist/lib/request-pipeline/refuse-visible-record-list.d.ts.map +1 -0
  142. package/dist/lib/request-pipeline/refuse-visible-record-list.js +33 -0
  143. package/dist/lib/request-pipeline/refuse-visible-record-list.js.map +1 -0
  144. package/dist/lib/request-pipeline/register-routes.d.ts +50 -0
  145. package/dist/lib/request-pipeline/register-routes.d.ts.map +1 -0
  146. package/dist/lib/request-pipeline/register-routes.js +217 -0
  147. package/dist/lib/request-pipeline/register-routes.js.map +1 -0
  148. package/dist/lib/request-pipeline/request-id.d.ts +22 -0
  149. package/dist/lib/request-pipeline/request-id.d.ts.map +1 -0
  150. package/dist/lib/request-pipeline/request-id.js +26 -0
  151. package/dist/lib/request-pipeline/request-id.js.map +1 -0
  152. package/dist/lib/request-pipeline/request-language-response.d.ts +14 -0
  153. package/dist/lib/request-pipeline/request-language-response.d.ts.map +1 -0
  154. package/dist/lib/request-pipeline/request-language-response.js +47 -0
  155. package/dist/lib/request-pipeline/request-language-response.js.map +1 -0
  156. package/dist/lib/request-pipeline/request-locale.d.ts +38 -0
  157. package/dist/lib/request-pipeline/request-locale.d.ts.map +1 -0
  158. package/dist/lib/request-pipeline/request-locale.js +102 -0
  159. package/dist/lib/request-pipeline/request-locale.js.map +1 -0
  160. package/dist/lib/request-pipeline/request-origin.d.ts +17 -0
  161. package/dist/lib/request-pipeline/request-origin.d.ts.map +1 -0
  162. package/dist/lib/request-pipeline/request-origin.js +38 -0
  163. package/dist/lib/request-pipeline/request-origin.js.map +1 -0
  164. package/dist/lib/request-pipeline/request-trace.d.ts +10 -0
  165. package/dist/lib/request-pipeline/request-trace.d.ts.map +1 -0
  166. package/dist/lib/request-pipeline/request-trace.js +24 -0
  167. package/dist/lib/request-pipeline/request-trace.js.map +1 -0
  168. package/dist/lib/request-pipeline/route-rate-limit.d.ts +19 -0
  169. package/dist/lib/request-pipeline/route-rate-limit.d.ts.map +1 -0
  170. package/dist/lib/request-pipeline/route-rate-limit.js +32 -0
  171. package/dist/lib/request-pipeline/route-rate-limit.js.map +1 -0
  172. package/dist/lib/request-pipeline/run-route.d.ts +9 -0
  173. package/dist/lib/request-pipeline/run-route.d.ts.map +1 -0
  174. package/dist/lib/request-pipeline/run-route.js +42 -0
  175. package/dist/lib/request-pipeline/run-route.js.map +1 -0
  176. package/dist/lib/request-pipeline/with-deadline.d.ts +12 -0
  177. package/dist/lib/request-pipeline/with-deadline.d.ts.map +1 -0
  178. package/dist/lib/request-pipeline/with-deadline.js +55 -0
  179. package/dist/lib/request-pipeline/with-deadline.js.map +1 -0
  180. package/dist/lib/route/access.d.ts +143 -0
  181. package/dist/lib/route/access.d.ts.map +1 -0
  182. package/dist/lib/route/access.js +97 -0
  183. package/dist/lib/route/access.js.map +1 -0
  184. package/dist/lib/route/capabilities.d.ts +100 -0
  185. package/dist/lib/route/capabilities.d.ts.map +1 -0
  186. package/dist/lib/route/capabilities.js +42 -0
  187. package/dist/lib/route/capabilities.js.map +1 -0
  188. package/dist/lib/route/define-raw-route.d.ts +81 -0
  189. package/dist/lib/route/define-raw-route.d.ts.map +1 -0
  190. package/dist/lib/route/define-raw-route.js +5 -0
  191. package/dist/lib/route/define-raw-route.js.map +1 -0
  192. package/dist/lib/route/delete-route.d.ts +26 -0
  193. package/dist/lib/route/delete-route.d.ts.map +1 -0
  194. package/dist/lib/route/delete-route.js +6 -0
  195. package/dist/lib/route/delete-route.js.map +1 -0
  196. package/dist/lib/route/get-route.d.ts +43 -0
  197. package/dist/lib/route/get-route.d.ts.map +1 -0
  198. package/dist/lib/route/get-route.js +18 -0
  199. package/dist/lib/route/get-route.js.map +1 -0
  200. package/dist/lib/route/list-result.d.ts +11 -0
  201. package/dist/lib/route/list-result.d.ts.map +1 -0
  202. package/dist/lib/route/list-result.js +8 -0
  203. package/dist/lib/route/list-result.js.map +1 -0
  204. package/dist/lib/route/list-route.d.ts +60 -0
  205. package/dist/lib/route/list-route.d.ts.map +1 -0
  206. package/dist/lib/route/list-route.js +68 -0
  207. package/dist/lib/route/list-route.js.map +1 -0
  208. package/dist/lib/route/patch-route.d.ts +27 -0
  209. package/dist/lib/route/patch-route.d.ts.map +1 -0
  210. package/dist/lib/route/patch-route.js +8 -0
  211. package/dist/lib/route/patch-route.js.map +1 -0
  212. package/dist/lib/route/post-no-content-route.d.ts +18 -0
  213. package/dist/lib/route/post-no-content-route.d.ts.map +1 -0
  214. package/dist/lib/route/post-no-content-route.js +8 -0
  215. package/dist/lib/route/post-no-content-route.js.map +1 -0
  216. package/dist/lib/route/post-route.d.ts +34 -0
  217. package/dist/lib/route/post-route.d.ts.map +1 -0
  218. package/dist/lib/route/post-route.js +23 -0
  219. package/dist/lib/route/post-route.js.map +1 -0
  220. package/dist/lib/route/route-definition.d.ts +224 -0
  221. package/dist/lib/route/route-definition.d.ts.map +1 -0
  222. package/dist/lib/route/route-definition.js +32 -0
  223. package/dist/lib/route/route-definition.js.map +1 -0
  224. package/dist/lib/route/route-execution.d.ts +47 -0
  225. package/dist/lib/route/route-execution.d.ts.map +1 -0
  226. package/dist/lib/route/route-execution.js +162 -0
  227. package/dist/lib/route/route-execution.js.map +1 -0
  228. package/dist/lib/route/route-group.d.ts +21 -0
  229. package/dist/lib/route/route-group.d.ts.map +1 -0
  230. package/dist/lib/route/route-group.js +18 -0
  231. package/dist/lib/route/route-group.js.map +1 -0
  232. package/dist/lib/route/route-guards.test-support.d.ts +2 -0
  233. package/dist/lib/route/route-guards.test-support.d.ts.map +1 -0
  234. package/dist/lib/route/route-guards.test-support.js +2 -0
  235. package/dist/lib/route/route-guards.test-support.js.map +1 -0
  236. package/dist/lib/route/sse-route.d.ts +54 -0
  237. package/dist/lib/route/sse-route.d.ts.map +1 -0
  238. package/dist/lib/route/sse-route.js +63 -0
  239. package/dist/lib/route/sse-route.js.map +1 -0
  240. package/dist/lib/route/web-socket-route.d.ts +45 -0
  241. package/dist/lib/route/web-socket-route.d.ts.map +1 -0
  242. package/dist/lib/route/web-socket-route.js +180 -0
  243. package/dist/lib/route/web-socket-route.js.map +1 -0
  244. package/dist/lib/route-catalog/route-catalog-parts.d.ts +27 -0
  245. package/dist/lib/route-catalog/route-catalog-parts.d.ts.map +1 -0
  246. package/dist/lib/route-catalog/route-catalog-parts.js +87 -0
  247. package/dist/lib/route-catalog/route-catalog-parts.js.map +1 -0
  248. package/dist/protocol-mounts/index.d.ts +19 -0
  249. package/dist/protocol-mounts/index.d.ts.map +1 -0
  250. package/dist/protocol-mounts/index.js +19 -0
  251. package/dist/protocol-mounts/index.js.map +1 -0
  252. package/dist/protocol-mounts/lib/mcp/json-rpc.d.ts +54 -0
  253. package/dist/protocol-mounts/lib/mcp/json-rpc.d.ts.map +1 -0
  254. package/dist/protocol-mounts/lib/mcp/json-rpc.js +53 -0
  255. package/dist/protocol-mounts/lib/mcp/json-rpc.js.map +1 -0
  256. package/dist/protocol-mounts/lib/mcp/mcp-endpoint.d.ts +31 -0
  257. package/dist/protocol-mounts/lib/mcp/mcp-endpoint.d.ts.map +1 -0
  258. package/dist/protocol-mounts/lib/mcp/mcp-endpoint.js +179 -0
  259. package/dist/protocol-mounts/lib/mcp/mcp-endpoint.js.map +1 -0
  260. package/dist/protocol-mounts/lib/mcp/mcp-methods.d.ts +68 -0
  261. package/dist/protocol-mounts/lib/mcp/mcp-methods.d.ts.map +1 -0
  262. package/dist/protocol-mounts/lib/mcp/mcp-methods.js +201 -0
  263. package/dist/protocol-mounts/lib/mcp/mcp-methods.js.map +1 -0
  264. package/dist/protocol-mounts/lib/mcp/protected-resource-metadata.d.ts +18 -0
  265. package/dist/protocol-mounts/lib/mcp/protected-resource-metadata.d.ts.map +1 -0
  266. package/dist/protocol-mounts/lib/mcp/protected-resource-metadata.js +34 -0
  267. package/dist/protocol-mounts/lib/mcp/protected-resource-metadata.js.map +1 -0
  268. package/dist/protocol-mounts/lib/protocol-mount/mount-fetch-handler.d.ts +57 -0
  269. package/dist/protocol-mounts/lib/protocol-mount/mount-fetch-handler.d.ts.map +1 -0
  270. package/dist/protocol-mounts/lib/protocol-mount/mount-fetch-handler.js +100 -0
  271. package/dist/protocol-mounts/lib/protocol-mount/mount-fetch-handler.js.map +1 -0
  272. package/dist/protocol-mounts/lib/protocol-mount/to-web-request.d.ts +14 -0
  273. package/dist/protocol-mounts/lib/protocol-mount/to-web-request.d.ts.map +1 -0
  274. package/dist/protocol-mounts/lib/protocol-mount/to-web-request.js +60 -0
  275. package/dist/protocol-mounts/lib/protocol-mount/to-web-request.js.map +1 -0
  276. package/dist/testing/build-test-app.d.ts +47 -0
  277. package/dist/testing/build-test-app.d.ts.map +1 -0
  278. package/dist/testing/build-test-app.js +79 -0
  279. package/dist/testing/build-test-app.js.map +1 -0
  280. package/dist/testing/index.d.ts +3 -0
  281. package/dist/testing/index.d.ts.map +1 -0
  282. package/dist/testing/index.js +3 -0
  283. package/dist/testing/index.js.map +1 -0
  284. package/dist/testing/parse-inject-response.d.ts +32 -0
  285. package/dist/testing/parse-inject-response.d.ts.map +1 -0
  286. package/dist/testing/parse-inject-response.js +39 -0
  287. package/dist/testing/parse-inject-response.js.map +1 -0
  288. package/package.json +55 -4
@@ -0,0 +1,50 @@
1
+ import type { AccessModel } from "@dads-framework/access";
2
+ import type { FastifyInstance } from "fastify";
3
+ import type { RouteCapabilities } from "../route/capabilities.js";
4
+ import type { HttpRoute } from "../route/route-group.js";
5
+ import { type AuditActorForSubject } from "./carry-audit-actor.js";
6
+ import { type AccessPorts, type SubjectResolver } from "./enforce-access.js";
7
+ import { type FeatureStateProtocol } from "./feature-state.js";
8
+ export type RegisterRoutesOptions = AccessPorts & {
9
+ readonly routes: readonly HttpRoute[];
10
+ /** Required when a route declares list visibility or scope access. */
11
+ readonly accessModel?: AccessModel;
12
+ /** App rung of the capability ladder (route > group > app). */
13
+ readonly capabilities?: RouteCapabilities;
14
+ /**
15
+ * Hard deadline for a route's handler. Node's `requestTimeout` only bounds
16
+ * receiving the request from a slow client — nothing built-in stops a hung
17
+ * handler from pinning its socket, so the kit races every execute against
18
+ * this. A number, never disable-able. Default 30s.
19
+ */
20
+ readonly handlerTimeoutMs?: number;
21
+ /**
22
+ * Turns an allowed request's subject into the audit actor its writes are
23
+ * recorded under. The composition root passes what
24
+ * `composeSubjectToAuditActor` from `@dads-framework/audit` built for its own
25
+ * subject kinds; without it the handler keeps the ingress `anonymous`
26
+ * context — writes still succeed, they are just attributed to nobody.
27
+ */
28
+ readonly auditActorForSubject?: AuditActorForSubject;
29
+ /**
30
+ * Optional application evaluator and the header names its clients speak; the
31
+ * kit owns the once-per-request protocol. Omitted, no snapshot is resolved
32
+ * and no hash header is written.
33
+ */
34
+ readonly featureState?: FeatureStateProtocol;
35
+ /**
36
+ * Named machine-credential resolvers. A route declaring
37
+ * `capabilities.machineCredential: name` resolves its caller only through
38
+ * `machineCredentials[name]` (never `subjects`); every other route never
39
+ * consults these. A declared name without a resolver throws at registration.
40
+ */
41
+ readonly machineCredentials?: Readonly<Record<string, SubjectResolver>>;
42
+ };
43
+ /**
44
+ * Compiles a route table onto a Fastify instance. Each handler reads as the
45
+ * composition it is: `enforceAccess` → `runRoute` (parse → handler → parse,
46
+ * under `withDeadline`) → `envelope`. Failures propagate to the kit error
47
+ * handler — nothing here invents statuses beyond the access refusals.
48
+ */
49
+ export declare function registerRoutes(app: FastifyInstance, options: RegisterRoutesOptions): void;
50
+ //# sourceMappingURL=register-routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register-routes.d.ts","sourceRoot":"","sources":["../../../src/lib/request-pipeline/register-routes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,EACV,eAAe,EAIhB,MAAM,SAAS,CAAC;AASjB,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAGlE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEzD,OAAO,EACL,KAAK,oBAAoB,EAG1B,MAAM,wBAAwB,CAAC;AAKhC,OAAO,EAEL,KAAK,WAAW,EAEhB,KAAK,eAAe,EACrB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAGL,KAAK,oBAAoB,EAI1B,MAAM,oBAAoB,CAAC;AAkB5B,MAAM,MAAM,qBAAqB,GAAG,WAAW,GAAG;IAChD,QAAQ,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,CAAC;IACtC,sEAAsE;IACtE,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC;IACnC,+DAA+D;IAC/D,QAAQ,CAAC,YAAY,CAAC,EAAE,iBAAiB,CAAC;IAC1C;;;;;OAKG;IACH,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC;;;;;;OAMG;IACH,QAAQ,CAAC,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;IACrD;;;;OAIG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,oBAAoB,CAAC;IAC7C;;;;;OAKG;IACH,QAAQ,CAAC,kBAAkB,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC;CACzE,CAAC;AA4SF;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,eAAe,EACpB,OAAO,EAAE,qBAAqB,GAC7B,IAAI,CAMN"}
@@ -0,0 +1,217 @@
1
+ import { answeringEachQuestionOnce } from "@dads-framework/request-work";
2
+ import { ACCESS_REFUSAL_HEADER, ACCESS_REFUSAL_VALUE, } from "@dads-framework/contract";
3
+ import { rememberAccessOutcome } from "../request-log/request-outcome.js";
4
+ import { assertNoAppSiteServing } from "./assert-no-app-site-serving.js";
5
+ import { carryResolvedAuditActor, rememberRefusedAuditActor, } from "./carry-audit-actor.js";
6
+ import { attachWrittenAuthorizationConsistencyCookie, carryAuthorizationConsistency, } from "./carry-authorization-consistency.js";
7
+ import { enforceAccess, } from "./enforce-access.js";
8
+ import { compileRouteScopePlan } from "./enforce-scope.js";
9
+ import { envelope } from "./envelope.js";
10
+ import { assertFeatureStateWriteIsCurrent, featureStateContext, resolveFeatureState, writeFeatureStateHash, } from "./feature-state.js";
11
+ import { carryFeatureStatePreview } from "./feature-state-preview.js";
12
+ import { isWriteMethod } from "./logic/is-write-method.js";
13
+ import { assertNoAppMachineCredential, routeOptionsFor, } from "./machine-credential-access.js";
14
+ import { parseBodyAsBytes } from "./raw-route-byte-body.js";
15
+ import { requestIdOf } from "./request-id.js";
16
+ import { addRequestLanguageVaryHeader } from "./request-language-response.js";
17
+ import { consumeBudget, limiterFor, sendRateLimited, } from "./route-rate-limit.js";
18
+ import { runRoute } from "./run-route.js";
19
+ const DEFAULT_HANDLER_TIMEOUT_MS = 30_000;
20
+ function sendAccessRefusal(reply, decision) {
21
+ if (decision.outcome === "unauthenticated") {
22
+ return reply.status(401).send({
23
+ error: { code: "NOT_AUTHENTICATED", message: "Authentication required" },
24
+ });
25
+ }
26
+ // The body stays the bare not-found envelope; only the header says the
27
+ // edge refused. The edge answers the same for a missing record as for one
28
+ // the caller may not reach, so the mark reveals nothing about existence.
29
+ return reply
30
+ .status(404)
31
+ .header(ACCESS_REFUSAL_HEADER, ACCESS_REFUSAL_VALUE)
32
+ .send({
33
+ error: { code: "RESOURCE_NOT_FOUND", message: "Resource not found" },
34
+ });
35
+ }
36
+ /**
37
+ * The per-request scopes every route runs inside: the authorization
38
+ * consistency cookie, and one memory of authorization answers so the edge
39
+ * check and the handler's own identical check reach SpiceDB once. Both end
40
+ * with the request; neither is ever shared between requests.
41
+ */
42
+ function carryRequestScopes(request, work) {
43
+ return carryAuthorizationConsistency(request, () => answeringEachQuestionOnce(work));
44
+ }
45
+ /** enforce access → become the caller → feature preview → feature state → consume budget → run the pipeline → envelope. */
46
+ function buildRouteHandler(definition, options) {
47
+ const timeoutMs = options.handlerTimeoutMs ?? DEFAULT_HANDLER_TIMEOUT_MS;
48
+ const limiter = limiterFor(definition, options.capabilities);
49
+ const scopePlan = compileRouteScopePlan(definition, options.accessModel);
50
+ return async (request, reply) => carryRequestScopes(request, async () => {
51
+ const decision = await enforceAccess(definition.access, request, options, scopePlan);
52
+ rememberAccessOutcome(request, definition.access, decision);
53
+ if (decision.outcome !== "allow") {
54
+ rememberRefusedAuditActor(options.auditActorForSubject, request, decision);
55
+ return sendAccessRefusal(reply, decision);
56
+ }
57
+ const allowed = {
58
+ request,
59
+ reply,
60
+ decision,
61
+ limiter,
62
+ featureState: options.featureState,
63
+ };
64
+ return carryResolvedAuditActor(options.auditActorForSubject, request, decision, () => answerTypedRouteInPreview(definition, allowed, timeoutMs));
65
+ });
66
+ }
67
+ function answerTypedRouteInPreview(definition, allowed, timeoutMs) {
68
+ return withFeaturePreview(definition, allowed, () => answerTypedRoute(definition, allowed, timeoutMs));
69
+ }
70
+ async function answerTypedRoute(definition, allowed, timeoutMs) {
71
+ const { request, reply, decision } = allowed;
72
+ const featureState = await resolveAndWriteFeatureState(definition, allowed);
73
+ const budget = consumeBudget(allowed.limiter, definition, request, decision.authenticatedSubject?.id);
74
+ if (!budget.allowed) {
75
+ return sendRateLimited(request, reply, budget.retryAfterSeconds);
76
+ }
77
+ const result = await runRoute(definition, request, decision, timeoutMs, featureState ?? undefined);
78
+ await refreshFeatureStateHashAfterWrite(definition, allowed);
79
+ if (definition.status === 204) {
80
+ return reply.status(204).send();
81
+ }
82
+ return reply
83
+ .status(definition.status)
84
+ .send(envelope(result.data, result.meta));
85
+ }
86
+ /** enforce access → become the caller → hand the request to the raw handler. No envelope, no deadline. */
87
+ function buildRawHandler(definition, options) {
88
+ const limiter = limiterFor(definition, options.capabilities);
89
+ compileRouteScopePlan(definition, options.accessModel);
90
+ return async (request, reply) => carryRequestScopes(request, async () => {
91
+ const decision = await enforceAccess(definition.access, request, options);
92
+ rememberAccessOutcome(request, definition.access, decision);
93
+ if (decision.outcome !== "allow") {
94
+ rememberRefusedAuditActor(options.auditActorForSubject, request, decision);
95
+ sendAccessRefusal(reply, decision);
96
+ return;
97
+ }
98
+ const allowed = {
99
+ request,
100
+ reply,
101
+ decision,
102
+ limiter,
103
+ featureState: options.featureState,
104
+ };
105
+ await carryResolvedAuditActor(options.auditActorForSubject, request, decision, () => answerRawRouteInPreview(definition, allowed));
106
+ });
107
+ }
108
+ function answerRawRouteInPreview(definition, allowed) {
109
+ return withFeaturePreview(definition, allowed, () => answerRawRoute(definition, allowed));
110
+ }
111
+ async function answerRawRoute(definition, allowed) {
112
+ const { request, reply, decision } = allowed;
113
+ await resolveAndWriteFeatureState(definition, allowed);
114
+ const budget = consumeBudget(allowed.limiter, definition, request, decision.authenticatedSubject?.id);
115
+ if (!budget.allowed) {
116
+ sendRateLimited(request, reply, budget.retryAfterSeconds);
117
+ return;
118
+ }
119
+ await definition.handler({
120
+ request,
121
+ reply,
122
+ subject: decision.subject,
123
+ authenticatedSubject: decision.authenticatedSubject,
124
+ requestId: requestIdOf(request),
125
+ log: request.log,
126
+ });
127
+ await refreshFeatureStateHashAfterWrite(definition, allowed);
128
+ }
129
+ /** Refuses a write the route does not allow while a staff member is signed in as the user. */
130
+ /** Runs `work` inside the caller's feature preview, when the request carries a live one. */
131
+ function withFeaturePreview(definition, allowed, work) {
132
+ return carryFeatureStatePreview({
133
+ protocol: allowed.featureState?.preview,
134
+ request: allowed.request,
135
+ reply: allowed.reply,
136
+ authenticatedSubject: allowed.decision.authenticatedSubject,
137
+ subject: allowed.decision.subject,
138
+ context: featureStateContext(allowed.featureState, allowed.request),
139
+ method: definition.method,
140
+ writesDuringFeaturePreview: definition.capabilities.writesDuringFeaturePreview,
141
+ work,
142
+ });
143
+ }
144
+ async function resolveAndWriteFeatureState(definition, allowed) {
145
+ const protocol = allowed.featureState;
146
+ if (protocol === undefined)
147
+ return null;
148
+ const featureState = await resolveFeatureState({
149
+ protocol,
150
+ request: allowed.request,
151
+ authenticatedSubject: allowed.decision.authenticatedSubject,
152
+ subject: allowed.decision.subject,
153
+ });
154
+ writeFeatureStateHash(allowed.reply, protocol.hashHeader, featureState);
155
+ assertFeatureStateWriteIsCurrent({
156
+ snapshot: featureState,
157
+ clientHashHeader: protocol.clientHashHeader,
158
+ request: allowed.request,
159
+ method: definition.method,
160
+ featureStateWrite: definition.capabilities.featureStateWrite,
161
+ });
162
+ return featureState;
163
+ }
164
+ async function refreshFeatureStateHashAfterWrite(definition, allowed) {
165
+ const protocol = allowed.featureState;
166
+ if (protocol === undefined || !isWriteMethod(definition.method))
167
+ return;
168
+ const postWriteFeatureState = await resolveFeatureState({
169
+ protocol,
170
+ request: allowed.request,
171
+ authenticatedSubject: allowed.decision.authenticatedSubject,
172
+ subject: allowed.decision.subject,
173
+ });
174
+ writeFeatureStateHash(allowed.reply, protocol.hashHeader, postWriteFeatureState);
175
+ }
176
+ function toFastifyRoute(definition, options) {
177
+ return {
178
+ method: definition.method,
179
+ url: definition.path,
180
+ // Typed verbs and raw routes alike: omitted keeps the app's limit.
181
+ ...(definition.bodyLimitBytes === undefined
182
+ ? {}
183
+ : { bodyLimit: definition.bodyLimitBytes }),
184
+ // The early pass reaches hijacked raw/SSE sockets. The send-time pass
185
+ // merges fields a non-hijacked route added while producing its response.
186
+ onRequest: (request, reply, done) => {
187
+ addRequestLanguageVaryHeader(reply);
188
+ // Before Fastify picks the body parser, so only this route is affected.
189
+ if (definition.kind === "raw-route" && definition.bodyAsBytes === true) {
190
+ parseBodyAsBytes(request);
191
+ }
192
+ done();
193
+ },
194
+ onSend: (request, reply, payload, done) => {
195
+ addRequestLanguageVaryHeader(reply);
196
+ attachWrittenAuthorizationConsistencyCookie(request, reply);
197
+ done(null, payload);
198
+ },
199
+ handler: definition.kind === "raw-route"
200
+ ? buildRawHandler(definition, options)
201
+ : buildRouteHandler(definition, options),
202
+ };
203
+ }
204
+ /**
205
+ * Compiles a route table onto a Fastify instance. Each handler reads as the
206
+ * composition it is: `enforceAccess` → `runRoute` (parse → handler → parse,
207
+ * under `withDeadline`) → `envelope`. Failures propagate to the kit error
208
+ * handler — nothing here invents statuses beyond the access refusals.
209
+ */
210
+ export function registerRoutes(app, options) {
211
+ assertNoAppMachineCredential(options.capabilities);
212
+ assertNoAppSiteServing(options.capabilities);
213
+ for (const definition of options.routes) {
214
+ app.route(toFastifyRoute(definition, routeOptionsFor(definition, options)));
215
+ }
216
+ }
217
+ //# sourceMappingURL=register-routes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register-routes.js","sourceRoot":"","sources":["../../../src/lib/request-pipeline/register-routes.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,yBAAyB,EAAE,MAAM,8BAA8B,CAAC;AACzE,OAAO,EACL,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAK1E,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AACzE,OAAO,EAEL,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,2CAA2C,E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sBAAsB,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAC7C,KAAK,MAAM,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QACxC,GAAG,CAAC,KAAK,CAAC,cAAc,CAAC,UAAU,EAAE,eAAe,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAC9E,CAAC;AACH,CAAC"}
@@ -0,0 +1,22 @@
1
+ import type { z } from "zod";
2
+ /**
3
+ * Correlation ingress (docs/architecture/observability.md): trust a sane
4
+ * inbound X-Request-Id (trimmed, capped) or mint one. Owned by this library —
5
+ * no middleware exists; this runs inside createHttpApp for every request.
6
+ */
7
+ /**
8
+ * `request_log`'s primary key. Opaque rather than `entityIdSchema`: a trusted
9
+ * inbound `X-Request-Id` is trimmed and capped, never format-checked, so a
10
+ * caller's non-UUID value must still parse.
11
+ */
12
+ export declare const requestIdSchema: z.core.$ZodBranded<z.ZodString, "RequestId", "out">;
13
+ export type RequestId = z.infer<typeof requestIdSchema>;
14
+ export declare function resolveRequestId(header: unknown): {
15
+ id: string;
16
+ generated: boolean;
17
+ };
18
+ export declare function requestIdOf(request: {
19
+ readonly headers: Record<string, unknown>;
20
+ readonly id: string;
21
+ }): string;
22
+ //# sourceMappingURL=request-id.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-id.d.ts","sourceRoot":"","sources":["../../../src/lib/request-pipeline/request-id.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAI7B;;;;GAIG;AAEH;;;;GAIG;AACH,eAAO,MAAM,eAAe,qDAG3B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AACxD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,OAAO,GAAG;IACjD,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,OAAO,CAAC;CACpB,CAKA;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACrB,GAAG,MAAM,CAIT"}
@@ -0,0 +1,26 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { opaqueIdSchema, REQUEST_ID_HEADER } from "@dads-framework/contract";
3
+ /**
4
+ * Correlation ingress (docs/architecture/observability.md): trust a sane
5
+ * inbound X-Request-Id (trimmed, capped) or mint one. Owned by this library —
6
+ * no middleware exists; this runs inside createHttpApp for every request.
7
+ */
8
+ /**
9
+ * `request_log`'s primary key. Opaque rather than `entityIdSchema`: a trusted
10
+ * inbound `X-Request-Id` is trimmed and capped, never format-checked, so a
11
+ * caller's non-UUID value must still parse.
12
+ */
13
+ export const requestIdSchema = opaqueIdSchema("RequestId", "HTTP request correlation id");
14
+ export function resolveRequestId(header) {
15
+ if (typeof header === "string" && header.trim().length > 0) {
16
+ return { id: header.trim().slice(0, 128), generated: false };
17
+ }
18
+ return { id: randomUUID(), generated: true };
19
+ }
20
+ export function requestIdOf(request) {
21
+ const header = request.headers[REQUEST_ID_HEADER];
22
+ if (typeof header === "string" && header.length > 0)
23
+ return header;
24
+ return request.id;
25
+ }
26
+ //# sourceMappingURL=request-id.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-id.js","sourceRoot":"","sources":["../../../src/lib/request-pipeline/request-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7E;;;;GAIG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,cAAc,CAC3C,WAAW,EACX,6BAA6B,CAC9B,CAAC;AAGF,MAAM,UAAU,gBAAgB,CAAC,MAAe;IAI9C,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3D,OAAO,EAAE,EAAE,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC/D,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;AAC/C,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,OAG3B;IACC,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAClD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,MAAM,CAAC;IACnE,OAAO,OAAO,CAAC,EAAE,CAAC;AACpB,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { FastifyReply } from "fastify";
2
+ /**
3
+ * Mark a route response as selected by the reader's request language.
4
+ *
5
+ * This runs before a raw route may hijack its socket and again before ordinary
6
+ * responses are sent. The second pass preserves Vary fields a route added after
7
+ * the first pass. A wildcard already covers every request field.
8
+ *
9
+ * `Content-Language` is deliberately absent: `ctx.locale` records a reader
10
+ * preference, while canonical and user-authored response data does not yet have
11
+ * one truthful representation language across current endpoints.
12
+ */
13
+ export declare function addRequestLanguageVaryHeader(reply: FastifyReply): void;
14
+ //# sourceMappingURL=request-language-response.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-language-response.d.ts","sourceRoot":"","sources":["../../../src/lib/request-pipeline/request-language-response.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AA6B5C;;;;;;;;;;GAUG;AACH,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAoBtE"}
@@ -0,0 +1,47 @@
1
+ const ACCEPT_LANGUAGE_VARY_FIELD = "Accept-Language";
2
+ function varyFieldNames(header) {
3
+ if (header === undefined)
4
+ return [];
5
+ const text = Array.isArray(header) ? header.join(",") : String(header);
6
+ return text
7
+ .split(",")
8
+ .map((field) => field.trim())
9
+ .filter((field) => field !== "");
10
+ }
11
+ function uniqueVaryFieldNames(...headers) {
12
+ const fieldNames = [];
13
+ const seen = new Set();
14
+ for (const fieldName of headers.flatMap(varyFieldNames)) {
15
+ const normalized = fieldName.toLowerCase();
16
+ if (seen.has(normalized))
17
+ continue;
18
+ seen.add(normalized);
19
+ fieldNames.push(fieldName);
20
+ }
21
+ return fieldNames;
22
+ }
23
+ /**
24
+ * Mark a route response as selected by the reader's request language.
25
+ *
26
+ * This runs before a raw route may hijack its socket and again before ordinary
27
+ * responses are sent. The second pass preserves Vary fields a route added after
28
+ * the first pass. A wildcard already covers every request field.
29
+ *
30
+ * `Content-Language` is deliberately absent: `ctx.locale` records a reader
31
+ * preference, while canonical and user-authored response data does not yet have
32
+ * one truthful representation language across current endpoints.
33
+ */
34
+ export function addRequestLanguageVaryHeader(reply) {
35
+ const fields = uniqueVaryFieldNames(reply.raw.getHeader("vary"), reply.getHeader("vary"));
36
+ const hasWildcard = fields.some((field) => field === "*");
37
+ if (!hasWildcard &&
38
+ !fields.some((field) => field.toLowerCase() === ACCEPT_LANGUAGE_VARY_FIELD.toLowerCase())) {
39
+ fields.push(ACCEPT_LANGUAGE_VARY_FIELD);
40
+ }
41
+ const next = hasWildcard ? "*" : fields.join(", ");
42
+ void reply.header("Vary", next);
43
+ if (!reply.raw.headersSent) {
44
+ reply.raw.setHeader("Vary", next);
45
+ }
46
+ }
47
+ //# sourceMappingURL=request-language-response.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-language-response.js","sourceRoot":"","sources":["../../../src/lib/request-pipeline/request-language-response.ts"],"names":[],"mappings":"AAEA,MAAM,0BAA0B,GAAG,iBAAiB,CAAC;AAErD,SAAS,cAAc,CACrB,MAAuD;IAEvD,IAAI,MAAM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AAC,IAAI,CACV,CAAC,KAAK,EAAE,EAAE,CACR,KAAK,CAAC,WAAW,EAAE,KAAK,0BAA0B,CAAC,WAAW,EAAE,CACnE,EACD,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;IAC1C,CAAC;IACD,MAAM,IAAI,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnD,KAAK,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAChC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;QAC3B,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACpC,CAAC;AACH,CAAC"}
@@ -0,0 +1,38 @@
1
+ import { type Locale } from "@dads-framework/contract";
2
+ /**
3
+ * Which language this request is answered in.
4
+ *
5
+ * The reader's language arrives on `Accept-Language` and is resolved once, here,
6
+ * beside the correlation id — the same shape and the same reason. Server-owned
7
+ * vocabulary (capability labels, workspace destinations, refusal sentences) is
8
+ * stored side by side and resolved against this value, so the wire keeps
9
+ * carrying plain strings and no route contract changes shape.
10
+ *
11
+ * Never throws. A header naming a language this product does not speak, a
12
+ * malformed weight, an absent header and a spoofed array all resolve to
13
+ * English: a request answered in the wrong language is a nuisance, and a
14
+ * request refused for its language is an outage.
15
+ */
16
+ /** What a reader gets when nothing they asked for is available. */
17
+ export declare const DEFAULT_REQUEST_LOCALE: Locale;
18
+ /**
19
+ * The best language this header asks for that the supported list contains.
20
+ *
21
+ * The supported list is a parameter for the same reason the copy kit's is:
22
+ * `SUPPORTED_LOCALES` holds one entry today, so every answer
23
+ * `resolveRequestLocale` can give is English whatever the header said, and a
24
+ * weight or ordering bug is unobservable through it. Name a second language here
25
+ * and the ranking becomes something a test can watch go wrong.
26
+ *
27
+ * Deliberately not on the lib's barrel: the pipeline reads a locale through
28
+ * `requestLocaleOf` and nothing outside this file has a supported list of its
29
+ * own to pass.
30
+ */
31
+ export declare function preferredLocaleOf<T extends string>(header: unknown, supported: readonly T[]): T | undefined;
32
+ /** The first supported language the header asks for, by weight, else English. */
33
+ export declare function resolveRequestLocale(header: unknown): Locale;
34
+ /** The language one request asked for — the pipeline's single read. */
35
+ export declare function requestLocaleOf(request: {
36
+ readonly headers: Record<string, unknown>;
37
+ }): Locale;
38
+ //# sourceMappingURL=request-locale.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-locale.d.ts","sourceRoot":"","sources":["../../../src/lib/request-pipeline/request-locale.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,MAAM,EACZ,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;GAaG;AAEH,mEAAmE;AACnE,eAAO,MAAM,sBAAsB,EAAE,MAA6B,CAAC;AA+DnE;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAChD,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,SAAS,CAAC,EAAE,GACtB,CAAC,GAAG,SAAS,CASf;AAED,iFAAiF;AACjF,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,CAE5D;AAED,uEAAuE;AACvE,wBAAgB,eAAe,CAAC,OAAO,EAAE;IACvC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC3C,GAAG,MAAM,CAET"}
@@ -0,0 +1,102 @@
1
+ import { LOCALE_HEADER, SUPPORTED_LOCALES, } from "@dads-framework/contract";
2
+ /**
3
+ * Which language this request is answered in.
4
+ *
5
+ * The reader's language arrives on `Accept-Language` and is resolved once, here,
6
+ * beside the correlation id — the same shape and the same reason. Server-owned
7
+ * vocabulary (capability labels, workspace destinations, refusal sentences) is
8
+ * stored side by side and resolved against this value, so the wire keeps
9
+ * carrying plain strings and no route contract changes shape.
10
+ *
11
+ * Never throws. A header naming a language this product does not speak, a
12
+ * malformed weight, an absent header and a spoofed array all resolve to
13
+ * English: a request answered in the wrong language is a nuisance, and a
14
+ * request refused for its language is an outage.
15
+ */
16
+ /** What a reader gets when nothing they asked for is available. */
17
+ export const DEFAULT_REQUEST_LOCALE = SUPPORTED_LOCALES[0];
18
+ function isStringArray(value) {
19
+ return (Array.isArray(value) &&
20
+ value.every((item) => typeof item === "string"));
21
+ }
22
+ /** Fastify hands a repeated header through as an array; both spellings parse. */
23
+ function headerText(header) {
24
+ if (typeof header === "string")
25
+ return header;
26
+ if (isStringArray(header))
27
+ return header.join(",");
28
+ return "";
29
+ }
30
+ /**
31
+ * `q=0.9` off one tag's parameters; anything unreadable weighs full.
32
+ *
33
+ * The empty arm is the one that matters: `Number("")` is 0, so a bare `q=`
34
+ * read as a number is indistinguishable from `q=0` — the one weight that means
35
+ * "never send me this", which would silently DROP the tag the reader asked for.
36
+ * Unreadable and refused are opposite answers, and only the client can say
37
+ * `q=0`.
38
+ */
39
+ function qualityOf(parameters) {
40
+ for (const parameter of parameters) {
41
+ const [name, value] = parameter.split("=");
42
+ if (name?.trim().toLowerCase() !== "q")
43
+ continue;
44
+ const written = value?.trim() ?? "";
45
+ if (written === "")
46
+ return 1;
47
+ const quality = Number(written);
48
+ return Number.isFinite(quality) ? quality : 1;
49
+ }
50
+ return 1;
51
+ }
52
+ function parseAcceptLanguage(header) {
53
+ return header.split(",").flatMap((part) => {
54
+ const [rawTag, ...parameters] = part.split(";");
55
+ const tag = rawTag?.trim() ?? "";
56
+ return tag === "" ? [] : [{ tag, quality: qualityOf(parameters) }];
57
+ });
58
+ }
59
+ /** `es-MX` is Spanish: a reader asking for a region wants its language. */
60
+ function supportedLocaleForTag(tag, supported) {
61
+ const normalized = tag.trim().toLowerCase();
62
+ if (normalized === "" || normalized === "*")
63
+ return undefined;
64
+ const exact = supported.find((locale) => locale.toLowerCase() === normalized);
65
+ if (exact !== undefined)
66
+ return exact;
67
+ const primarySubtag = normalized.split("-")[0];
68
+ return supported.find((locale) => locale.toLowerCase() === primarySubtag);
69
+ }
70
+ /**
71
+ * The best language this header asks for that the supported list contains.
72
+ *
73
+ * The supported list is a parameter for the same reason the copy kit's is:
74
+ * `SUPPORTED_LOCALES` holds one entry today, so every answer
75
+ * `resolveRequestLocale` can give is English whatever the header said, and a
76
+ * weight or ordering bug is unobservable through it. Name a second language here
77
+ * and the ranking becomes something a test can watch go wrong.
78
+ *
79
+ * Deliberately not on the lib's barrel: the pipeline reads a locale through
80
+ * `requestLocaleOf` and nothing outside this file has a supported list of its
81
+ * own to pass.
82
+ */
83
+ export function preferredLocaleOf(header, supported) {
84
+ const wanted = parseAcceptLanguage(headerText(header))
85
+ .filter((entry) => entry.quality > 0)
86
+ .sort((left, right) => right.quality - left.quality);
87
+ for (const entry of wanted) {
88
+ const matched = supportedLocaleForTag(entry.tag, supported);
89
+ if (matched !== undefined)
90
+ return matched;
91
+ }
92
+ return undefined;
93
+ }
94
+ /** The first supported language the header asks for, by weight, else English. */
95
+ export function resolveRequestLocale(header) {
96
+ return preferredLocaleOf(header, SUPPORTED_LOCALES) ?? DEFAULT_REQUEST_LOCALE;
97
+ }
98
+ /** The language one request asked for — the pipeline's single read. */
99
+ export function requestLocaleOf(request) {
100
+ return resolveRequestLocale(request.headers[LOCALE_HEADER]);
101
+ }
102
+ //# sourceMappingURL=request-locale.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-locale.js","sourceRoot":"","sources":["../../../src/lib/request-pipeline/request-locale.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,iBAAiB,GAElB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;GAaG;AAEH,mEAAmE;AACnE,MAAM,CAAC,MAAM,sBAAsB,GAAW,iBAAiB,CAAC,CAAC,CAAC,CAAC;AAOnE,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,CACL,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACpB,KA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,SAAS,CAAC,CAAC;QAC5D,IAAI,OAAO,KAAK,SAAS;YAAE,OAAO,OAAO,CAAC;IAC5C,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,oBAAoB,CAAC,MAAe;IAClD,OAAO,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,CAAC,IAAI,sBAAsB,CAAC;AAChF,CAAC;AAED,uEAAuE;AACvE,MAAM,UAAU,eAAe,CAAC,OAE/B;IACC,OAAO,oBAAoB,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC;AAC9D,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { FastifyRequest } from "fastify";
2
+ /**
3
+ * Records the request's origin for everything it goes on to do. `enterWith`
4
+ * for the reason `enterAuditActor` gives: an `onRequest` hook has to return,
5
+ * so it cannot wrap the handler in a callback.
6
+ *
7
+ * Scheme and host come from Fastify, which reads `X-Forwarded-Proto` and
8
+ * `X-Forwarded-Host` only from as many proxy hops as `trustedProxyHops` names.
9
+ * A host that does not form an origin records nothing.
10
+ */
11
+ export declare function enterRequestOrigin(request: FastifyRequest): void;
12
+ /**
13
+ * The origin the current request reached this server on, or `fallback`
14
+ * outside any request (a job, a startup hook, a script).
15
+ */
16
+ export declare function requestOriginOr(fallback: string): string;
17
+ //# sourceMappingURL=request-origin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-origin.d.ts","sourceRoot":"","sources":["../../../src/lib/request-pipeline/request-origin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAa9C;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAGhE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAExD"}
@@ -0,0 +1,38 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ /**
3
+ * The origin the current request reached this server on
4
+ * (`https://site.example`, never a path), kept for the request's own async
5
+ * task. Code far from the route (a storage driver signing a link a browser
6
+ * will open) reads it to name the server the way the browser did.
7
+ */
8
+ const requestOriginContext = new AsyncLocalStorage();
9
+ /**
10
+ * Records the request's origin for everything it goes on to do. `enterWith`
11
+ * for the reason `enterAuditActor` gives: an `onRequest` hook has to return,
12
+ * so it cannot wrap the handler in a callback.
13
+ *
14
+ * Scheme and host come from Fastify, which reads `X-Forwarded-Proto` and
15
+ * `X-Forwarded-Host` only from as many proxy hops as `trustedProxyHops` names.
16
+ * A host that does not form an origin records nothing.
17
+ */
18
+ export function enterRequestOrigin(request) {
19
+ const origin = originOf(request);
20
+ if (origin !== undefined)
21
+ requestOriginContext.enterWith({ origin });
22
+ }
23
+ /**
24
+ * The origin the current request reached this server on, or `fallback`
25
+ * outside any request (a job, a startup hook, a script).
26
+ */
27
+ export function requestOriginOr(fallback) {
28
+ return requestOriginContext.getStore()?.origin ?? fallback;
29
+ }
30
+ function originOf(request) {
31
+ try {
32
+ return new URL(`${request.protocol}://${request.host}`).origin;
33
+ }
34
+ catch {
35
+ return undefined;
36
+ }
37
+ }
38
+ //# sourceMappingURL=request-origin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-origin.js","sourceRoot":"","sources":["../../../src/lib/request-pipeline/request-origin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD;;;;;GAKG;AACH,MAAM,oBAAoB,GAAG,IAAI,iBAAiB,EAE9C,CAAC;AAEL;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAuB;IACxD,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;IACjC,IAAI,MAAM,KAAK,SAAS;QAAE,oBAAoB,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;AACvE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,QAAgB;IAC9C,OAAO,oBAAoB,CAAC,QAAQ,EAAE,EAAE,MAAM,IAAI,QAAQ,CAAC;AAC7D,CAAC;AAED,SAAS,QAAQ,CAAC,OAAuB;IACvC,IAAI,CAAC;QACH,OAAO,IAAI,GAAG,CAAC,GAAG,OAAO,CAAC,QAAQ,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC;IACjE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC"}