@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,63 @@
1
+ import { defineRawRoute, } from "./define-raw-route.js";
2
+ /**
3
+ * `sseRoute` — the kit primitive for a Server-Sent-Events stream, so no route
4
+ * hand-rolls the same framing twice (docs/architecture/http-edge.md). It is a
5
+ * thin, typed shape over {@link defineRawRoute}: it still declares `access` and
6
+ * flows through the same session/permission wall as every route, but it OWNS the
7
+ * SSE mechanics a stream must get right — hijacking the socket, the
8
+ * `text/event-stream` headers, the keep-alive heartbeat, the `:ready` flush, and
9
+ * tearing the subscription down when the client disconnects.
10
+ *
11
+ * Authorization the kit cannot express (e.g. "may this subject view this org?")
12
+ * stays in the handler: it runs BEFORE any stream opens, denies with the normal
13
+ * error envelope, and only calls `ctx.open(...)` once the caller is cleared —
14
+ * so a denial never leaks a hijacked socket.
15
+ */
16
+ /** Silent-comment keep-alive cadence — proxies/browsers drop an idle stream otherwise. */
17
+ export const DEFAULT_SSE_HEARTBEAT_MS = 15_000;
18
+ /** Declare an SSE route. `access` is required — the same wall as every verb. */
19
+ export function sseRoute(config) {
20
+ return defineRawRoute({
21
+ method: "GET",
22
+ path: config.path,
23
+ summary: config.summary,
24
+ access: config.access,
25
+ ...(config.capabilities === undefined
26
+ ? {}
27
+ : { capabilities: config.capabilities }),
28
+ openapi: config.openapi,
29
+ handler: async (ctx) => {
30
+ const open = (onOpen) => {
31
+ const { reply, request } = ctx;
32
+ reply.hijack();
33
+ reply.raw.writeHead(200, {
34
+ "content-type": "text/event-stream",
35
+ "cache-control": String(reply.getHeader("cache-control") ?? "no-cache"),
36
+ connection: "keep-alive",
37
+ });
38
+ const stream = {
39
+ send: (data) => {
40
+ reply.raw.write(`data: ${JSON.stringify(data)}\n\n`);
41
+ },
42
+ comment: (text = "") => {
43
+ reply.raw.write(`:${text}\n\n`);
44
+ },
45
+ };
46
+ const release = onOpen(stream);
47
+ const heartbeat = setInterval(() => {
48
+ stream.comment();
49
+ }, config.heartbeatMs ?? DEFAULT_SSE_HEARTBEAT_MS);
50
+ heartbeat.unref();
51
+ request.raw.once("close", () => {
52
+ clearInterval(heartbeat);
53
+ release();
54
+ });
55
+ // Flush headers so the client (and tests) know the stream is ready
56
+ // before any delta arrives.
57
+ stream.comment("ready");
58
+ };
59
+ await config.handler({ ...ctx, open });
60
+ },
61
+ });
62
+ }
63
+ //# sourceMappingURL=sse-route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse-route.js","sourceRoot":"","sources":["../../../src/lib/route/sse-route.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,cAAc,GAIf,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;GAaG;AAEH,0FAA0F;AAC1F,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC;AAsC/C,gFAAgF;AAChF,MAAM,UAAU,QAAQ,CAAC,MAAsB;IAC7C,OAAO,cAAc,CAAC;QACpB,MAAM,EAAE,KAAK;QACb,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,GAAG,CAAC,MAAM,CAAC,YAAY,KAAK,SAAS;YACnC,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,EAAE,YAAY,EAAE,MAAM,CAAC,YAAY,EAAE,CAAC;QAC1C,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE;YACrB,MAAM,IAAI,GAAG,CAAC,MAAyC,EAAQ,EAAE;gBAC/D,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC;gBAC/B,KAAK,CAAC,MAAM,EAAE,CAAC;gBACf,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE;oBACvB,cAAc,EAAE,mBAAmB;oBACnC,eAAe,EAAE,MAAM,CACrB,KAAK,CAAC,SAAS,CAAC,eAAe,CAAC,IAAI,UAAU,CAC/C;oBACD,UAAU,EAAE,YAAY;iBACzB,CAAC,CAAC;gBAEH,MAAM,MAAM,GAAc;oBACxB,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE;wBACb,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;oBACvD,CAAC;oBACD,OAAO,EAAE,CAAC,IAAI,GAAG,EAAE,EAAE,EAAE;wBACrB,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,MAAM,CAAC,CAAC;oBAClC,CAAC;iBACF,CAAC;gBAEF,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;gBAC/B,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE;oBACjC,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnB,CAAC,EAAE,MAAM,CAAC,WAAW,IAAI,wBAAwB,CAAC,CAAC;gBACnD,SAAS,CAAC,KAAK,EAAE,CAAC;gBAClB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;oBAC7B,aAAa,CAAC,SAAS,CAAC,CAAC;oBACzB,OAAO,EAAE,CAAC;gBACZ,CAAC,CAAC,CAAC;gBAEH,mEAAmE;gBACnE,4BAA4B;gBAC5B,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YAC1B,CAAC,CAAC;YAEF,MAAM,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;QACzC,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,45 @@
1
+ import type { FastifyInstance } from "fastify";
2
+ import type { RouteAccess } from "./access.js";
3
+ import type { RouteCapabilities } from "./capabilities.js";
4
+ import { type RawOpenApiOperation, type RawRouteBlueprint, type RawRouteContext } from "./define-raw-route.js";
5
+ /** One open socket, as a route handler sees it. Text frames only. */
6
+ export type WebSocketConnection = {
7
+ send(text: string): void;
8
+ /** A protocol ping; the peer's pong arrives through `onPong`. */
9
+ ping(): void;
10
+ onPong(listener: () => void): void;
11
+ onMessage(listener: (text: string) => void): void;
12
+ /** Runs once, however the socket ended. */
13
+ onClose(listener: () => void): void;
14
+ close(code: number, reason: string): void;
15
+ /** Drops the socket without a close handshake (a dead peer). */
16
+ terminate(): void;
17
+ };
18
+ export type WebSocketRouteContext = RawRouteContext & {
19
+ /**
20
+ * Completes the upgrade and calls `onOpen` with the open socket. Call it
21
+ * ONLY after the handler has authorized the request; to refuse, throw or
22
+ * send an error and return without calling it.
23
+ */
24
+ readonly accept: (onOpen: (connection: WebSocketConnection) => void) => void;
25
+ };
26
+ export type WebSocketRouteConfig = {
27
+ readonly path: string;
28
+ readonly summary: string;
29
+ readonly access: RouteAccess;
30
+ readonly capabilities?: RouteCapabilities;
31
+ readonly openapi: RawOpenApiOperation;
32
+ readonly handler: (ctx: WebSocketRouteContext) => Promise<void>;
33
+ };
34
+ /** Whether a mounted route's URL (its prefix included) is a declared WebSocket route. */
35
+ export declare function isWebSocketRoutePath(url: string): boolean;
36
+ /** Declare a WebSocket route. `access` is required, the same wall as every verb. */
37
+ export declare function webSocketRoute(config: WebSocketRouteConfig): RawRouteBlueprint;
38
+ /**
39
+ * Routes upgrade requests through Fastify, once per app. A request no
40
+ * WebSocket route claims gets its normal answer (a 404, a 401) written to
41
+ * the socket, which then closes. Every socket still open when the app
42
+ * closes is closed with 1001.
43
+ */
44
+ export declare function acceptWebSocketUpgrades(app: FastifyInstance): void;
45
+ //# sourceMappingURL=web-socket-route.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-socket-route.d.ts","sourceRoot":"","sources":["../../../src/lib/route/web-socket-route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAM/C,OAAO,KAAK,EAAE,WAAW,EAA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AAgBF,yFAAyF;AACzF,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAGzD;AAUD,oFAAoF;AACpF,wBAAgB,cAAc,CAC5B,MAAM,EAAE,oBAAoB,GAC3B,iBAAiB,CA2DnB;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,eAAe,GAAG,IAAI,CAgClE"}
@@ -0,0 +1,180 @@
1
+ import { ServerResponse } from "node:http";
2
+ import { WebSocketServer } from "ws";
3
+ import { defineRawRoute, } from "./define-raw-route.js";
4
+ /**
5
+ * `webSocketRoute`: the kit primitive for a WebSocket a client opens with a
6
+ * GET upgrade. Like `sseRoute`, it is a raw route: the upgrade request flows
7
+ * through the same access wall, machine-credential resolver, rate limit and
8
+ * request log as every route, and a refusal answers with the normal status
9
+ * and error envelope before any socket opens. Only a handler that has
10
+ * authorized the caller calls `accept`.
11
+ *
12
+ * Node hands an upgrade request to the server's `upgrade` event, not to
13
+ * Fastify. Mounting a WebSocket route installs one `upgrade` listener
14
+ * (`acceptWebSocketUpgrades`) that routes the request through Fastify with a
15
+ * response bound to the raw socket, as `@fastify/websocket` does. An app with
16
+ * no WebSocket route installs nothing, so its upgrade handling is unchanged.
17
+ */
18
+ /** The largest message a client may send; a channel's own messages are tiny. */
19
+ const MAX_PAYLOAD_BYTES = 64 * 1024;
20
+ /** Close code for a server going away (deploy, shutdown). */
21
+ const GOING_AWAY = 1001;
22
+ /** Every path a `webSocketRoute` declared, so the app knows a mounted route is one. */
23
+ const webSocketPaths = new Set();
24
+ /** Whether a mounted route's URL (its prefix included) is a declared WebSocket route. */
25
+ export function isWebSocketRoutePath(url) {
26
+ for (const path of webSocketPaths)
27
+ if (url.endsWith(path))
28
+ return true;
29
+ return false;
30
+ }
31
+ const pendingUpgrades = new WeakMap();
32
+ const appsAcceptingUpgrades = new WeakSet();
33
+ const upgrades = new WebSocketServer({
34
+ noServer: true,
35
+ clientTracking: false,
36
+ maxPayload: MAX_PAYLOAD_BYTES,
37
+ });
38
+ /** Declare a WebSocket route. `access` is required, the same wall as every verb. */
39
+ export function webSocketRoute(config) {
40
+ webSocketPaths.add(config.path);
41
+ return defineRawRoute({
42
+ method: "GET",
43
+ path: config.path,
44
+ summary: config.summary,
45
+ access: config.access,
46
+ ...(config.capabilities === undefined
47
+ ? {}
48
+ : { capabilities: config.capabilities }),
49
+ openapi: config.openapi,
50
+ handler: async (ctx) => {
51
+ const pending = pendingUpgrades.get(ctx.request.raw);
52
+ if (pending === undefined) {
53
+ // A plain GET: nothing to upgrade.
54
+ await ctx.reply
55
+ .status(426)
56
+ .header("upgrade", "websocket")
57
+ .send({
58
+ error: {
59
+ code: "UPGRADE_REQUIRED",
60
+ message: "This route answers only a WebSocket upgrade",
61
+ },
62
+ });
63
+ return;
64
+ }
65
+ const accept = (onOpen) => {
66
+ pending.claimed = true;
67
+ ctx.reply.hijack();
68
+ upgrades.handleUpgrade(ctx.request.raw, pending.socket, pending.head, (socket) => {
69
+ // A bad frame (too large, malformed) is an error on the socket;
70
+ // unheard, it would take the whole process down.
71
+ socket.on("error", (error) => {
72
+ ctx.log.warn({
73
+ event: "websocket.socket_error",
74
+ path: config.path,
75
+ error: error.message,
76
+ });
77
+ socket.terminate();
78
+ });
79
+ if (pending.app.closing) {
80
+ socket.close(GOING_AWAY, "server closing");
81
+ return;
82
+ }
83
+ pending.app.open.add(socket);
84
+ socket.once("close", () => pending.app.open.delete(socket));
85
+ onOpen(connectionOver(socket));
86
+ });
87
+ };
88
+ await config.handler({ ...ctx, accept });
89
+ },
90
+ });
91
+ }
92
+ /**
93
+ * Routes upgrade requests through Fastify, once per app. A request no
94
+ * WebSocket route claims gets its normal answer (a 404, a 401) written to
95
+ * the socket, which then closes. Every socket still open when the app
96
+ * closes is closed with 1001.
97
+ */
98
+ export function acceptWebSocketUpgrades(app) {
99
+ if (appsAcceptingUpgrades.has(app.server))
100
+ return;
101
+ appsAcceptingUpgrades.add(app.server);
102
+ const sockets = { open: new Set(), closing: false };
103
+ app.server.on("upgrade", (request, socket, head) => {
104
+ const pending = {
105
+ socket,
106
+ head,
107
+ app: sockets,
108
+ claimed: false,
109
+ };
110
+ pendingUpgrades.set(request, pending);
111
+ socket.on("error", () => socket.destroy());
112
+ const response = new ServerResponse(request);
113
+ response.assignSocket(asNetSocket(socket));
114
+ response.on("finish", () => {
115
+ if (!pending.claimed)
116
+ socket.end();
117
+ });
118
+ app.routing(request, response);
119
+ });
120
+ app.addHook("preClose", (done) => {
121
+ // A socket accepted from here on is closed at once, too.
122
+ sockets.closing = true;
123
+ for (const client of sockets.open) {
124
+ client.close(GOING_AWAY, "server closing");
125
+ }
126
+ done();
127
+ });
128
+ }
129
+ function connectionOver(socket) {
130
+ return {
131
+ send: (text) => {
132
+ if (socket.readyState === socket.OPEN)
133
+ socket.send(text);
134
+ },
135
+ ping: () => {
136
+ if (socket.readyState === socket.OPEN)
137
+ socket.ping();
138
+ },
139
+ onPong: (listener) => {
140
+ socket.on("pong", listener);
141
+ },
142
+ onMessage: (listener) => {
143
+ socket.on("message", (data, isBinary) => {
144
+ if (!isBinary)
145
+ listener(textOf(data));
146
+ });
147
+ },
148
+ onClose: (listener) => {
149
+ socket.once("close", listener);
150
+ },
151
+ close: (code, reason) => {
152
+ socket.close(code, reason);
153
+ },
154
+ terminate: () => {
155
+ socket.terminate();
156
+ },
157
+ };
158
+ }
159
+ function textOf(data) {
160
+ if (Array.isArray(data))
161
+ return Buffer.concat(data).toString("utf8");
162
+ if (data instanceof ArrayBuffer)
163
+ return Buffer.from(data).toString("utf8");
164
+ return data.toString("utf8");
165
+ }
166
+ /** Node's `upgrade` event hands the connection's socket as a Duplex; it is the net socket. */
167
+ function asNetSocket(socket) {
168
+ const candidate = socket;
169
+ if (!isNetSocket(candidate)) {
170
+ throw new Error("an upgrade request arrived without a network socket");
171
+ }
172
+ return candidate;
173
+ }
174
+ function isNetSocket(value) {
175
+ return (typeof value === "object" &&
176
+ value !== null &&
177
+ "remoteAddress" in value &&
178
+ "setTimeout" in value);
179
+ }
180
+ //# sourceMappingURL=web-socket-route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-socket-route.js","sourceRoot":"","sources":["../../../src/lib/route/web-socket-route.ts"],"names":[],"mappings":"AACA,OAAO,EAAwB,cAAc,EAAE,MAAM,WAAW,CAAC;AAGjE,OAAO,EAAgC,eAAe,EAAE,MAAM,IAAI,CAAC;AAInE,OAAO,EACL,cAAc,GAIf,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;GAaG;AAEH,gFAAgF;AAChF,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC;AACpC,6DAA6D;AAC7D,MAAM,UAAU,GAAG,IAAI,CAAC;AA6CxB,uFAAuF;AACvF,MAAM,cAAc,GAAG,IAAI,GAAG,EAAU,CAAC;AAEzC,yFAAyF;AACzF,MAAM,UAAU,oBAAoB,CAAC,GAAW;IAC9C,KAAK,MAAM,IAAI,IAAI,cAAc;QAAE,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;IACvE,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,eAAe,GAAG,IAAI,OAAO,EAAmC,CAAC;AACvE,MAAM,qBAAqB,GAAG,IAAI,OAAO,EAAU,CAAC;AACpD,MAAM,QAAQ,GAAG,IAAI,eAAe,CAAC;IACnC,QAAQ,EAAE,IAAI;IACd,cAAc,EAAE,KAAK;IACrB,UAAU,EAAE,iBAAiB;CAC9B,CAAC,CAAC;AAEH,oFAAoF;AACpF,MAAM,UAAU,cAAc,CAC5B,MAA4B;IAE5B,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAChC,OAAO,cAAc,CAAC;QACpB,MAAM,EAAE,KAAK;QACb,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,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+ /**
2
+ * A route catalog (a product's inventory of its route table: `routes`, each
3
+ * with a `tag`, beside whatever else the product lists) as parts, for
4
+ * committing it: one file per route tag holding that tag's routes, plus
5
+ * `_shared.json` holding every other key. Two pull requests that add routes
6
+ * under different tags then change different files instead of both
7
+ * inserting into one sorted list.
8
+ *
9
+ * File names follow the OpenAPI parts (`openApiPartFileName`), so a tag's
10
+ * catalog part and its OpenAPI part share a name. `bundleRouteCatalogParts`
11
+ * puts the catalog back together byte for byte: `$comment` first, then
12
+ * `routes` in `compareCatalogRoutes` order, then the shared keys in their
13
+ * own order.
14
+ */
15
+ /** The order a catalog lists its routes in: by path, then method. */
16
+ export declare function compareCatalogRoutes(first: {
17
+ readonly path: string;
18
+ readonly method: string;
19
+ }, second: {
20
+ readonly path: string;
21
+ readonly method: string;
22
+ }): number;
23
+ /** Splits a catalog into its parts, keyed by file name. */
24
+ export declare function splitRouteCatalog(catalog: unknown): ReadonlyMap<string, Record<string, unknown>>;
25
+ /** Puts a catalog's parts back into the catalog, byte for byte. */
26
+ export declare function bundleRouteCatalogParts(parts: ReadonlyMap<string, unknown>): Record<string, unknown>;
27
+ //# sourceMappingURL=route-catalog-parts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route-catalog-parts.d.ts","sourceRoot":"","sources":["../../../src/lib/route-catalog/route-catalog-parts.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;GAaG;AAEH,qEAAqE;AACrE,wBAAgB,oBAAoB,CAClC,KAAK,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EACzD,MAAM,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACzD,MAAM,CAIR;AAwBD,2DAA2D;AAC3D,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,OAAO,GACf,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAuB9C;AAED,mEAAmE;AACnE,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,GAClC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAazB"}
@@ -0,0 +1,87 @@
1
+ import { z } from "zod";
2
+ import { byCodePoint } from "../openapi/logic/by-code-point.js";
3
+ import { assertArtifactTagFile, openApiPartFileName, SHARED_OPENAPI_PART, } from "../openapi/logic/openapi-parts.js";
4
+ /**
5
+ * A route catalog (a product's inventory of its route table: `routes`, each
6
+ * with a `tag`, beside whatever else the product lists) as parts, for
7
+ * committing it: one file per route tag holding that tag's routes, plus
8
+ * `_shared.json` holding every other key. Two pull requests that add routes
9
+ * under different tags then change different files instead of both
10
+ * inserting into one sorted list.
11
+ *
12
+ * File names follow the OpenAPI parts (`openApiPartFileName`), so a tag's
13
+ * catalog part and its OpenAPI part share a name. `bundleRouteCatalogParts`
14
+ * puts the catalog back together byte for byte: `$comment` first, then
15
+ * `routes` in `compareCatalogRoutes` order, then the shared keys in their
16
+ * own order.
17
+ */
18
+ /** The order a catalog lists its routes in: by path, then method. */
19
+ export function compareCatalogRoutes(first, second) {
20
+ return `${first.path} ${first.method}`.localeCompare(`${second.path} ${second.method}`);
21
+ }
22
+ // foreign-spec: a catalog route keeps every key its product serializes.
23
+ const catalogRouteSchema = z.looseObject({
24
+ method: z.string(),
25
+ path: z.string(),
26
+ tag: z.string(),
27
+ });
28
+ // foreign-spec: the catalog's other keys travel as the product wrote them.
29
+ const routeCatalogSchema = z.looseObject({
30
+ routes: z.array(catalogRouteSchema),
31
+ });
32
+ // foreign-spec: the shared part is the catalog without its routes.
33
+ const sharedCatalogPartSchema = z.looseObject({});
34
+ const catalogTagPartSchema = z.strictObject({
35
+ tag: z.string(),
36
+ routes: z.array(catalogRouteSchema),
37
+ });
38
+ /** Splits a catalog into its parts, keyed by file name. */
39
+ export function splitRouteCatalog(catalog) {
40
+ const { routes, ...shared } = routeCatalogSchema.parse(catalog);
41
+ const byTag = new Map();
42
+ for (const route of routes) {
43
+ const tagRoutes = byTag.get(route.tag) ?? [];
44
+ tagRoutes.push(route);
45
+ byTag.set(route.tag, tagRoutes);
46
+ }
47
+ const parts = new Map();
48
+ parts.set(SHARED_OPENAPI_PART, shared);
49
+ for (const tag of [...byTag.keys()].sort(byCodePoint)) {
50
+ const fileName = openApiPartFileName(tag);
51
+ if (parts.has(fileName)) {
52
+ throw new Error(`catalog tag "${tag}" would share ${fileName}`);
53
+ }
54
+ const tagRoutes = byTag.get(tag) ?? [];
55
+ parts.set(fileName, {
56
+ tag,
57
+ routes: [...tagRoutes].sort(compareCatalogRoutes),
58
+ });
59
+ }
60
+ return parts;
61
+ }
62
+ /** Puts a catalog's parts back into the catalog, byte for byte. */
63
+ export function bundleRouteCatalogParts(parts) {
64
+ const shared = sharedCatalogPartSchema.parse(parts.get(SHARED_OPENAPI_PART));
65
+ const routes = [];
66
+ for (const [fileName, value] of parts) {
67
+ if (fileName === SHARED_OPENAPI_PART)
68
+ continue;
69
+ routes.push(...parseCatalogTagPart(fileName, value).routes);
70
+ }
71
+ const { $comment, ...rest } = shared;
72
+ return {
73
+ ...($comment === undefined ? {} : { $comment }),
74
+ routes: routes.sort(compareCatalogRoutes),
75
+ ...rest,
76
+ };
77
+ }
78
+ function parseCatalogTagPart(fileName, value) {
79
+ const part = catalogTagPartSchema.parse(value);
80
+ assertArtifactTagFile(fileName, part.tag);
81
+ const stray = part.routes.find((route) => route.tag !== part.tag);
82
+ if (stray !== undefined) {
83
+ throw new Error(`${fileName} holds ${stray.method} ${stray.path}, tagged "${stray.tag}"`);
84
+ }
85
+ return part;
86
+ }
87
+ //# sourceMappingURL=route-catalog-parts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route-catalog-parts.js","sourceRoot":"","sources":["../../../src/lib/route-catalog/route-catalog-parts.ts"],"names":[],"mappings":"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AAI,KAAK,EAAE,CAAC;QACtC,IAAI,QAAQ,KAAK,mBAAmB;YAAE,SAAS;QAC/C,MAAM,CAAC,IAAI,CAAC,GAAG,mBAAmB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC;IAC9D,CAAC;IACD,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;IACrC,OAAO;QACL,GAAG,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC;QAC/C,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC;QACzC,GAAG,IAAI;KACR,CAAC;AACJ,CAAC;AAED,SAAS,mBAAmB,CAC1B,QAAgB,EAChB,KAAc;IAEd,MAAM,IAAI,GAAG,oBAAoB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC/C,qBAAqB,CAAC,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1C,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,CAAC,CAAC;IAClE,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CACb,GAAG,QAAQ,UAAU,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,IAAI,aAAa,KAAK,CAAC,GAAG,GAAG,CACzE,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * `@dads-framework/http/protocol-mounts` — the bridge for mounting a fetch-style
3
+ * handler (Better Auth's) on the kit's Fastify app. A secondary entry point
4
+ * rather than part of the main barrel on purpose: only the two composition
5
+ * roots register a protocol mount, so nothing else pays to compile the
6
+ * WHATWG `Request` subclass here, and a program without the DOM lib never
7
+ * meets it.
8
+ *
9
+ * The remote MCP endpoint and its RFC 9728 metadata live here for the same
10
+ * reason: fetch handlers only a composition root mounts. The tool seam they
11
+ * serve (`defineMcpTool`, `McpToolSet`) is in the main barrel, because
12
+ * domains author tools.
13
+ */
14
+ export { mountFetchHandler, type FetchHandler, type ProtocolExchange, type ProtocolMountOptions, type ProtocolRateLimit, } from "./lib/protocol-mount/mount-fetch-handler.js";
15
+ export { toWebRequest, type IncomingProtocolRequest, } from "./lib/protocol-mount/to-web-request.js";
16
+ export { createMcpEndpoint, type McpEndpoint, type McpEndpointOptions, } from "./lib/mcp/mcp-endpoint.js";
17
+ export { LATEST_PROTOCOL_VERSION as MCP_LATEST_PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS as MCP_SUPPORTED_PROTOCOL_VERSIONS, type McpFailure, type McpServerInfo, } from "./lib/mcp/mcp-methods.js";
18
+ export { createProtectedResourceMetadataHandler, protectedResourceMetadataDocument, type ProtectedResourceMetadataOptions, } from "./lib/mcp/protected-resource-metadata.js";
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/protocol-mounts/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EACL,iBAAiB,EACjB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,GACvB,MAAM,6CAA6C,CAAC;AACrD,OAAO,EACL,YAAY,EACZ,KAAK,uBAAuB,GAC7B,MAAM,wCAAwC,CAAC;AAChD,OAAO,EACL,iBAAiB,EACjB,KAAK,WAAW,EAChB,KAAK,kBAAkB,GACxB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,uBAAuB,IAAI,2BAA2B,EACtD,2BAA2B,IAAI,+BAA+B,EAC9D,KAAK,UAAU,EACf,KAAK,aAAa,GACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,sCAAsC,EACtC,iCAAiC,EACjC,KAAK,gCAAgC,GACtC,MAAM,0CAA0C,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * `@dads-framework/http/protocol-mounts` — the bridge for mounting a fetch-style
3
+ * handler (Better Auth's) on the kit's Fastify app. A secondary entry point
4
+ * rather than part of the main barrel on purpose: only the two composition
5
+ * roots register a protocol mount, so nothing else pays to compile the
6
+ * WHATWG `Request` subclass here, and a program without the DOM lib never
7
+ * meets it.
8
+ *
9
+ * The remote MCP endpoint and its RFC 9728 metadata live here for the same
10
+ * reason: fetch handlers only a composition root mounts. The tool seam they
11
+ * serve (`defineMcpTool`, `McpToolSet`) is in the main barrel, because
12
+ * domains author tools.
13
+ */
14
+ export { mountFetchHandler, } from "./lib/protocol-mount/mount-fetch-handler.js";
15
+ export { toWebRequest, } from "./lib/protocol-mount/to-web-request.js";
16
+ export { createMcpEndpoint, } from "./lib/mcp/mcp-endpoint.js";
17
+ export { LATEST_PROTOCOL_VERSION as MCP_LATEST_PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS as MCP_SUPPORTED_PROTOCOL_VERSIONS, } from "./lib/mcp/mcp-methods.js";
18
+ export { createProtectedResourceMetadataHandler, protectedResourceMetadataDocument, } from "./lib/mcp/protected-resource-metadata.js";
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/protocol-mounts/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EACL,iBAAiB,GAKlB,MAAM,6CAA6C,CAAC;AACrD,OAAO,EACL,YAAY,GAEb,MAAM,wCAAwC,CAAC;AAChD,OAAO,EACL,iBAAiB,GAGlB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,uBAAuB,IAAI,2BAA2B,EACtD,2BAA2B,IAAI,+BAA+B,GAG/D,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,sCAAsC,EACtC,iCAAiC,GAElC,MAAM,0CAA0C,CAAC"}
@@ -0,0 +1,54 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * The JSON-RPC 2.0 envelope MCP rides on: what one inbound message may be,
4
+ * and the two answer shapes. Shapes only — what each method does lives in
5
+ * `mcp-methods.ts`, and the HTTP framing in `mcp-endpoint.ts`.
6
+ */
7
+ export type JsonRpcId = string | number;
8
+ export type JsonRpcSuccess = Readonly<{
9
+ jsonrpc: "2.0";
10
+ id: JsonRpcId;
11
+ result: unknown;
12
+ }>;
13
+ export type JsonRpcFailure = Readonly<{
14
+ jsonrpc: "2.0";
15
+ id: JsonRpcId | null;
16
+ error: Readonly<{
17
+ code: number;
18
+ message: string;
19
+ data?: unknown;
20
+ }>;
21
+ }>;
22
+ export type JsonRpcAnswer = JsonRpcSuccess | JsonRpcFailure;
23
+ /** The error codes this endpoint answers with (JSON-RPC 2.0 §5.1). */
24
+ export declare const JSON_RPC_ERROR: {
25
+ readonly parseError: -32700;
26
+ readonly invalidRequest: -32600;
27
+ readonly methodNotFound: -32601;
28
+ readonly invalidParams: -32602;
29
+ readonly internalError: -32603;
30
+ };
31
+ declare const requestSchema: z.ZodObject<{
32
+ jsonrpc: z.ZodLiteral<"2.0">;
33
+ id: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
34
+ method: z.ZodString;
35
+ params: z.ZodOptional<z.ZodUnknown>;
36
+ }, z.core.$strip>;
37
+ export type JsonRpcRequest = z.infer<typeof requestSchema>;
38
+ /** One inbound message, sorted by what the endpoint owes it. */
39
+ export type InboundMessage = Readonly<{
40
+ kind: "request";
41
+ request: JsonRpcRequest;
42
+ }> | Readonly<{
43
+ kind: "notification";
44
+ method: string;
45
+ }> | Readonly<{
46
+ kind: "answer";
47
+ }> | Readonly<{
48
+ kind: "invalid";
49
+ }>;
50
+ export declare function classifyMessage(message: unknown): InboundMessage;
51
+ export declare function jsonRpcResult(id: JsonRpcId, result: unknown): JsonRpcSuccess;
52
+ export declare function jsonRpcError(id: JsonRpcId | null, code: number, message: string, data?: unknown): JsonRpcFailure;
53
+ export {};
54
+ //# sourceMappingURL=json-rpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-rpc.d.ts","sourceRoot":"","sources":["../../../../src/protocol-mounts/lib/mcp/json-rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;GAIG;AAEH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;AAExC,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC;IACpC,OAAO,EAAE,KAAK,CAAC;IACf,EAAE,EAAE,SAAS,CAAC;IACd,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC;IACpC,OAAO,EAAE,KAAK,CAAC;IACf,EAAE,EAAE,SAAS,GAAG,IAAI,CAAC;IACrB,KAAK,EAAE,QAAQ,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACpE,CAAC,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,cAAc,GAAG,cAAc,CAAC;AAE5D,sEAAsE;AACtE,eAAO,MAAM,cAAc;aACzB,UAAU,EAAE,CAAC,KAAK;aAClB,cAAc,EAAE,CAAC,KAAK;aACtB,cAAc,EAAE,CAAC,KAAK;aACtB,aAAa,EAAE,CAAC,KAAK;aACrB,aAAa,EAAE,CAAC,KAAK;CACb,CAAC;AAIX,QAAA,MAAM,aAAa;;;;;iBAKjB,CAAC;AAcH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAE3D,gEAAgE;AAChE,MAAM,MAAM,cAAc,GACtB,QAAQ,CAAC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,cAAc,CAAA;CAAE,CAAC,GACtD,QAAQ,CAAC;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,GAClD,QAAQ,CAAC;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC,GAC5B,QAAQ,CAAC;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC,CAAC;AAElC,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,cAAc,CAWhE;AAMD,wBAAgB,aAAa,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,GAAG,cAAc,CAE5E;AAED,wBAAgB,YAAY,CAC1B,EAAE,EAAE,SAAS,GAAG,IAAI,EACpB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,OAAO,GACb,cAAc,CAMhB"}
@@ -0,0 +1,53 @@
1
+ import { z } from "zod";
2
+ /** The error codes this endpoint answers with (JSON-RPC 2.0 §5.1). */
3
+ export const JSON_RPC_ERROR = {
4
+ parseError: -32700,
5
+ invalidRequest: -32600,
6
+ methodNotFound: -32601,
7
+ invalidParams: -32602,
8
+ internalError: -32603,
9
+ };
10
+ const idSchema = z.union([z.string(), z.number()]);
11
+ const requestSchema = z.object({
12
+ jsonrpc: z.literal("2.0"),
13
+ id: idSchema,
14
+ method: z.string().min(1),
15
+ params: z.unknown().optional(),
16
+ });
17
+ const notificationSchema = z.object({
18
+ jsonrpc: z.literal("2.0"),
19
+ method: z.string().min(1),
20
+ params: z.unknown().optional(),
21
+ });
22
+ /** A client's answer to a server request. This server sends none, so it only acknowledges them. */
23
+ const answerSchema = z.object({
24
+ jsonrpc: z.literal("2.0"),
25
+ id: idSchema.nullable(),
26
+ });
27
+ export function classifyMessage(message) {
28
+ const request = requestSchema.safeParse(message);
29
+ if (request.success)
30
+ return { kind: "request", request: request.data };
31
+ const notification = notificationSchema.safeParse(message);
32
+ if (notification.success) {
33
+ return { kind: "notification", method: notification.data.method };
34
+ }
35
+ if (answerSchema.safeParse(message).success && !hasMethod(message)) {
36
+ return { kind: "answer" };
37
+ }
38
+ return { kind: "invalid" };
39
+ }
40
+ function hasMethod(message) {
41
+ return typeof message === "object" && message !== null && "method" in message;
42
+ }
43
+ export function jsonRpcResult(id, result) {
44
+ return { jsonrpc: "2.0", id, result };
45
+ }
46
+ export function jsonRpcError(id, code, message, data) {
47
+ return {
48
+ jsonrpc: "2.0",
49
+ id,
50
+ error: data === undefined ? { code, message } : { code, message, data },
51
+ };
52
+ }
53
+ //# sourceMappingURL=json-rpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-rpc.js","sourceRoot":"","sources":["../../../../src/protocol-mounts/lib/mcp/json-rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAwBxB,sEAAsE;AACtE,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,UAAU,EAAE,CAAC,KAAK;IAClB,cAAc,EAAE,CAAC,KAAK;IACtB,cAAc,EAAE,CAAC,KAAK;IACtB,aAAa,EAAE,CAAC,KAAK;IACrB,aAAa,EAAE,CAAC,KAAK;CACb,CAAC;AAEX,MAAM,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AAEnD,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC;IAC7B,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC;IACzB,EAAE,EAAE,QAAQ;IACZ,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACzB,MAAM,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE;CAC/B,CAAC,CAAC;AAEH,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC;IAClC,OAAO,EAAE,CAAC,CAAC,OAA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AE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,YAAY,CAC1B,EAAoB,EACpB,IAAY,EACZ,OAAe,EACf,IAAc;IAEd,OAAO;QACL,OAAO,EAAE,KAAK;QACd,EAAE;QACF,KAAK,EAAE,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE;KACxE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,31 @@
1
+ import type { ResolveMcpBearer } from "../../../index.js";
2
+ import { type McpMethodsOptions } from "./mcp-methods.js";
3
+ /**
4
+ * A remote MCP server as one fetch handler: the Streamable HTTP transport,
5
+ * stateless and answering in plain JSON (no SSE, no `Mcp-Session-Id`), behind
6
+ * OAuth bearer tokens the way the MCP authorization spec describes a
7
+ * protected resource. Framework-free, like Better Auth's handler, so a
8
+ * product mounts it with `mountFetchHandler`
9
+ * (docs/architecture/http-edge.md#protocol-mounts).
10
+ *
11
+ * The order of the walls is the order a request meets them: origin (the
12
+ * DNS-rebinding guard), method, bearer, then the body. A caller who is not
13
+ * signed in learns nothing about the body rules.
14
+ */
15
+ export type McpEndpointOptions = McpMethodsOptions & Readonly<{
16
+ resolveBearer: ResolveMcpBearer;
17
+ /** This resource's RFC 9728 metadata URL, named in every 401 and 403. */
18
+ resourceMetadataUrl: string;
19
+ /**
20
+ * Browser origins allowed to call. A request with no `Origin` is a
21
+ * server-side client and passes; one with any other origin is refused.
22
+ */
23
+ allowedOrigins: readonly string[];
24
+ /** The request body's ceiling; the mount's own body limit should match it. */
25
+ maxRequestBytes: number;
26
+ /** The request's correlation id. Defaults to `x-request-id`, else a new one. */
27
+ requestIdOf?: (request: Request) => string;
28
+ }>;
29
+ export type McpEndpoint = (request: Request) => Promise<Response>;
30
+ export declare function createMcpEndpoint(options: McpEndpointOptions): McpEndpoint;
31
+ //# sourceMappingURL=mcp-endpoint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-endpoint.d.ts","sourceRoot":"","sources":["../../../../src/protocol-mounts/lib/mcp/mcp-endpoint.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAU1D,OAAO,EAEL,KAAK,iBAAiB,EAEvB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;;;GAWG;AAEH,MAAM,MAAM,kBAAkB,GAAG,iBAAiB,GAChD,QAAQ,CAAC;IACP,aAAa,EAAE,gBAAgB,CAAC;IAChC,yEAAyE;IACzE,mBAAmB,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,8EAA8E;IAC9E,eAAe,EAAE,MAAM,CAAC;IACxB,gFAAgF;IAChF,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,CAAC;CAC5C,CAAC,CAAC;AAEL,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAKlE,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,WAAW,CA0F1E"}