@fuzdev/fuz_app 0.162.0 → 0.164.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 (280) hide show
  1. package/README.md +2 -1
  2. package/dist/actions/action_event_data.d.ts +1 -1
  3. package/dist/actions/action_rpc.d.ts.map +1 -1
  4. package/dist/actions/action_rpc.js +4 -0
  5. package/dist/actions/action_spec.d.ts +3 -3
  6. package/dist/actions/action_spec.d.ts.map +1 -1
  7. package/dist/auth/CLAUDE.md +39 -36
  8. package/dist/auth/account_action_specs.d.ts +441 -115
  9. package/dist/auth/account_action_specs.d.ts.map +1 -1
  10. package/dist/auth/account_action_specs.js +47 -46
  11. package/dist/auth/account_route_schema.d.ts +98 -62
  12. package/dist/auth/account_route_schema.d.ts.map +1 -1
  13. package/dist/auth/account_route_schema.js +45 -45
  14. package/dist/auth/account_routes.js +1 -1
  15. package/dist/auth/account_schema.d.ts +54 -27
  16. package/dist/auth/account_schema.d.ts.map +1 -1
  17. package/dist/auth/account_schema.js +16 -18
  18. package/dist/auth/admin_action_specs.d.ts +76 -76
  19. package/dist/auth/admin_action_specs.js +2 -4
  20. package/dist/auth/admin_actions.d.ts +4 -4
  21. package/dist/auth/admin_actions.d.ts.map +1 -1
  22. package/dist/auth/admin_actions.js +3 -3
  23. package/dist/auth/all_cell_actions.d.ts +3 -3
  24. package/dist/auth/all_cell_actions.d.ts.map +1 -1
  25. package/dist/auth/all_cell_actions.js +1 -1
  26. package/dist/auth/api_token.d.ts +4 -4
  27. package/dist/auth/api_token.d.ts.map +1 -1
  28. package/dist/auth/api_token.js +3 -3
  29. package/dist/auth/audit_log_route_schema.d.ts +1 -1
  30. package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
  31. package/dist/auth/audit_log_route_schema.js +4 -5
  32. package/dist/auth/audit_log_schema.d.ts +46 -66
  33. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  34. package/dist/auth/audit_log_schema.js +34 -30
  35. package/dist/auth/bootstrap_route_schema.d.ts +146 -52
  36. package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
  37. package/dist/auth/bootstrap_route_schema.js +33 -19
  38. package/dist/auth/cell_grant_action_specs.d.ts +1 -1
  39. package/dist/auth/cell_grant_action_specs.js +1 -1
  40. package/dist/auth/cell_grant_actions.d.ts +4 -4
  41. package/dist/auth/cell_grant_actions.d.ts.map +1 -1
  42. package/dist/auth/cell_grant_actions.js +1 -1
  43. package/dist/auth/credential_type_schema.d.ts +16 -29
  44. package/dist/auth/credential_type_schema.d.ts.map +1 -1
  45. package/dist/auth/credential_type_schema.js +14 -18
  46. package/dist/auth/daemon_token.d.ts +4 -4
  47. package/dist/auth/daemon_token.d.ts.map +1 -1
  48. package/dist/auth/daemon_token.js +3 -3
  49. package/dist/auth/daemon_token_middleware.d.ts +1 -1
  50. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  51. package/dist/auth/daemon_token_middleware.js +5 -5
  52. package/dist/auth/grant_path_schema.d.ts +15 -29
  53. package/dist/auth/grant_path_schema.d.ts.map +1 -1
  54. package/dist/auth/grant_path_schema.js +13 -17
  55. package/dist/auth/middleware.js +4 -4
  56. package/dist/auth/migrations.js +1 -1
  57. package/dist/auth/name_schema.d.ts +7 -6
  58. package/dist/auth/name_schema.d.ts.map +1 -1
  59. package/dist/auth/name_schema.js +8 -9
  60. package/dist/auth/password.d.ts +15 -7
  61. package/dist/auth/password.d.ts.map +1 -1
  62. package/dist/auth/password.js +16 -9
  63. package/dist/auth/role_grant_offer_action_specs.d.ts +2 -2
  64. package/dist/auth/role_grant_offer_action_specs.js +2 -2
  65. package/dist/auth/role_grant_offer_actions.d.ts +4 -4
  66. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  67. package/dist/auth/role_grant_offer_actions.js +3 -3
  68. package/dist/auth/role_grant_offer_notifications.js +2 -2
  69. package/dist/auth/role_grant_offer_schema.d.ts +2 -2
  70. package/dist/auth/role_grant_offer_schema.js +2 -2
  71. package/dist/auth/role_schema.d.ts +47 -55
  72. package/dist/auth/role_schema.d.ts.map +1 -1
  73. package/dist/auth/role_schema.js +29 -35
  74. package/dist/auth/scope_kind_schema.d.ts +22 -26
  75. package/dist/auth/scope_kind_schema.d.ts.map +1 -1
  76. package/dist/auth/scope_kind_schema.js +19 -19
  77. package/dist/auth/self_service_role_action_specs.js +1 -1
  78. package/dist/auth/self_service_role_actions.d.ts +6 -6
  79. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  80. package/dist/auth/self_service_role_actions.js +7 -6
  81. package/dist/auth/signup_route_schema.d.ts +71 -21
  82. package/dist/auth/signup_route_schema.d.ts.map +1 -1
  83. package/dist/auth/signup_route_schema.js +17 -19
  84. package/dist/auth/standard_rpc_actions.d.ts +1 -1
  85. package/dist/auth/standard_rpc_actions.js +1 -1
  86. package/dist/auth/token_lifetime.d.ts +33 -9
  87. package/dist/auth/token_lifetime.d.ts.map +1 -1
  88. package/dist/auth/token_lifetime.js +8 -7
  89. package/dist/auth/token_scope.d.ts +99 -25
  90. package/dist/auth/token_scope.d.ts.map +1 -1
  91. package/dist/auth/token_scope.js +57 -35
  92. package/dist/db/CLAUDE.md +2 -1
  93. package/dist/db/file_fact_url.d.ts +11 -6
  94. package/dist/db/file_fact_url.d.ts.map +1 -1
  95. package/dist/db/file_fact_url.js +5 -5
  96. package/dist/db/schema_ready.d.ts +0 -5
  97. package/dist/db/schema_ready.d.ts.map +1 -1
  98. package/dist/db/schema_ready.js +0 -5
  99. package/dist/http/CLAUDE.md +104 -71
  100. package/dist/http/auth_shape.d.ts +48 -29
  101. package/dist/http/auth_shape.d.ts.map +1 -1
  102. package/dist/http/auth_shape.js +12 -85
  103. package/dist/http/body_limit.d.ts +12 -0
  104. package/dist/http/body_limit.d.ts.map +1 -1
  105. package/dist/http/body_limit.js +15 -0
  106. package/dist/http/body_limit_schema.d.ts +20 -0
  107. package/dist/http/body_limit_schema.d.ts.map +1 -0
  108. package/dist/http/body_limit_schema.js +20 -0
  109. package/dist/http/common_routes.d.ts +1 -1
  110. package/dist/http/common_routes.d.ts.map +1 -1
  111. package/dist/http/common_routes.js +11 -14
  112. package/dist/http/db_routes.d.ts.map +1 -1
  113. package/dist/http/db_routes.js +59 -69
  114. package/dist/http/dev_route_error_check.d.ts +70 -0
  115. package/dist/http/dev_route_error_check.d.ts.map +1 -0
  116. package/dist/http/dev_route_error_check.js +119 -0
  117. package/dist/http/error_schemas.d.ts +365 -189
  118. package/dist/http/error_schemas.d.ts.map +1 -1
  119. package/dist/http/error_schemas.js +256 -149
  120. package/dist/http/health_schema.d.ts +10 -6
  121. package/dist/http/health_schema.d.ts.map +1 -1
  122. package/dist/http/health_schema.js +3 -3
  123. package/dist/http/middleware_spec.d.ts +8 -3
  124. package/dist/http/middleware_spec.d.ts.map +1 -1
  125. package/dist/http/origin.d.ts +12 -43
  126. package/dist/http/origin.d.ts.map +1 -1
  127. package/dist/http/origin.js +10 -6
  128. package/dist/http/route_error_check.d.ts +158 -0
  129. package/dist/http/route_error_check.d.ts.map +1 -0
  130. package/dist/http/route_error_check.js +247 -0
  131. package/dist/http/route_errors.d.ts +80 -0
  132. package/dist/http/route_errors.d.ts.map +1 -0
  133. package/dist/http/route_errors.js +83 -0
  134. package/dist/http/route_spec.d.ts +33 -17
  135. package/dist/http/route_spec.d.ts.map +1 -1
  136. package/dist/http/route_spec.js +48 -62
  137. package/dist/http/schema_helpers.d.ts +0 -33
  138. package/dist/http/schema_helpers.d.ts.map +1 -1
  139. package/dist/http/schema_helpers.js +0 -45
  140. package/dist/http/surface.d.ts +19 -12
  141. package/dist/http/surface.d.ts.map +1 -1
  142. package/dist/http/surface.js +25 -38
  143. package/dist/http/surface_query.d.ts +11 -2
  144. package/dist/http/surface_query.d.ts.map +1 -1
  145. package/dist/http/surface_query.js +7 -1
  146. package/dist/primitive_schemas.d.ts +2 -19
  147. package/dist/primitive_schemas.d.ts.map +1 -1
  148. package/dist/primitive_schemas.js +2 -20
  149. package/dist/primitive_schemas_fuz.d.ts +39 -5
  150. package/dist/primitive_schemas_fuz.d.ts.map +1 -1
  151. package/dist/primitive_schemas_fuz.js +36 -7
  152. package/dist/rate_limiter.d.ts +13 -0
  153. package/dist/rate_limiter.d.ts.map +1 -1
  154. package/dist/rate_limiter.js +20 -2
  155. package/dist/schema_issues.d.ts +29 -11
  156. package/dist/schema_issues.d.ts.map +1 -1
  157. package/dist/schema_issues.js +11 -10
  158. package/dist/schema_meta.d.ts +8 -1
  159. package/dist/schema_meta.d.ts.map +1 -1
  160. package/dist/schema_meta.js +2 -1
  161. package/dist/server/app_server.d.ts +3 -1
  162. package/dist/server/app_server.d.ts.map +1 -1
  163. package/dist/server/app_server.js +24 -10
  164. package/dist/server/serve_fact_route.d.ts.map +1 -1
  165. package/dist/server/serve_fact_route.js +8 -31
  166. package/dist/server/surface_route.d.ts.map +1 -1
  167. package/dist/server/surface_route.js +5 -6
  168. package/dist/server/x_accel.d.ts +12 -7
  169. package/dist/server/x_accel.d.ts.map +1 -1
  170. package/dist/server/x_accel.js +7 -9
  171. package/dist/testing/CLAUDE.md +55 -44
  172. package/dist/testing/admin_integration.d.ts +3 -3
  173. package/dist/testing/admin_integration.d.ts.map +1 -1
  174. package/dist/testing/admin_integration.js +2 -2
  175. package/dist/testing/adversarial_404.d.ts +4 -18
  176. package/dist/testing/adversarial_404.d.ts.map +1 -1
  177. package/dist/testing/adversarial_404.js +19 -51
  178. package/dist/testing/adversarial_headers.d.ts +3 -4
  179. package/dist/testing/adversarial_headers.d.ts.map +1 -1
  180. package/dist/testing/adversarial_headers.js +8 -6
  181. package/dist/testing/adversarial_input.d.ts.map +1 -1
  182. package/dist/testing/adversarial_input.js +18 -17
  183. package/dist/testing/assertions.d.ts +33 -11
  184. package/dist/testing/assertions.d.ts.map +1 -1
  185. package/dist/testing/assertions.js +52 -12
  186. package/dist/testing/attack_surface.d.ts +5 -35
  187. package/dist/testing/attack_surface.d.ts.map +1 -1
  188. package/dist/testing/attack_surface.js +29 -91
  189. package/dist/testing/auth_apps.d.ts.map +1 -1
  190. package/dist/testing/auth_apps.js +2 -1
  191. package/dist/testing/cross_backend/action_manifest.d.ts +126 -56
  192. package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
  193. package/dist/testing/cross_backend/action_manifest.js +11 -11
  194. package/dist/testing/cross_backend/action_manifest_parity.d.ts +1 -1
  195. package/dist/testing/cross_backend/action_manifest_parity.d.ts.map +1 -1
  196. package/dist/testing/cross_backend/cell_crud.d.ts.map +1 -1
  197. package/dist/testing/cross_backend/cell_crud.js +2 -1
  198. package/dist/testing/cross_backend/conformance_table.js +1 -1
  199. package/dist/testing/cross_backend/default_spine_surface.d.ts +2 -2
  200. package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
  201. package/dist/testing/cross_backend/default_spine_surface.js +2 -2
  202. package/dist/testing/cross_backend/fact_serving.js +3 -3
  203. package/dist/testing/cross_backend/identity_parity.d.ts.map +1 -1
  204. package/dist/testing/cross_backend/identity_parity.js +39 -6
  205. package/dist/testing/cross_backend/origin_conformance.d.ts +278 -102
  206. package/dist/testing/cross_backend/origin_conformance.d.ts.map +1 -1
  207. package/dist/testing/cross_backend/origin_conformance.js +21 -21
  208. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts +3 -3
  209. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  210. package/dist/testing/cross_backend/rust_spine_consumer_surface.js +7 -1
  211. package/dist/testing/cross_backend/setup.d.ts +14 -1
  212. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  213. package/dist/testing/cross_backend/setup.js +68 -48
  214. package/dist/testing/cross_backend/spine_suites.d.ts +3 -3
  215. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  216. package/dist/testing/cross_backend/standard.d.ts +4 -4
  217. package/dist/testing/cross_backend/standard.d.ts.map +1 -1
  218. package/dist/testing/cross_backend/testing_origin_check.d.ts +147 -71
  219. package/dist/testing/cross_backend/testing_origin_check.d.ts.map +1 -1
  220. package/dist/testing/cross_backend/testing_origin_check.js +16 -13
  221. package/dist/testing/cross_backend/testing_reset_actions.d.ts +70 -27
  222. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  223. package/dist/testing/cross_backend/token_lifetime.d.ts.map +1 -1
  224. package/dist/testing/cross_backend/token_lifetime.js +34 -1
  225. package/dist/testing/data_exposure.d.ts.map +1 -1
  226. package/dist/testing/data_exposure.js +5 -4
  227. package/dist/testing/error_coverage.d.ts +24 -34
  228. package/dist/testing/error_coverage.d.ts.map +1 -1
  229. package/dist/testing/error_coverage.js +46 -89
  230. package/dist/testing/integration.d.ts.map +1 -1
  231. package/dist/testing/integration.js +6 -14
  232. package/dist/testing/integration_helpers.d.ts +37 -9
  233. package/dist/testing/integration_helpers.d.ts.map +1 -1
  234. package/dist/testing/integration_helpers.js +86 -40
  235. package/dist/testing/middleware.d.ts +3 -5
  236. package/dist/testing/middleware.d.ts.map +1 -1
  237. package/dist/testing/middleware.js +5 -4
  238. package/dist/testing/rate_limiting.d.ts +1 -1
  239. package/dist/testing/rate_limiting.d.ts.map +1 -1
  240. package/dist/testing/rate_limiting.js +5 -4
  241. package/dist/testing/round_trip.d.ts.map +1 -1
  242. package/dist/testing/round_trip.js +5 -3
  243. package/dist/testing/schema_generators.d.ts +10 -3
  244. package/dist/testing/schema_generators.d.ts.map +1 -1
  245. package/dist/testing/schema_generators.js +23 -11
  246. package/dist/testing/stubs.d.ts +10 -1
  247. package/dist/testing/stubs.d.ts.map +1 -1
  248. package/dist/testing/stubs.js +15 -5
  249. package/dist/testing/surface_invariants.d.ts +36 -115
  250. package/dist/testing/surface_invariants.d.ts.map +1 -1
  251. package/dist/testing/surface_invariants.js +106 -269
  252. package/dist/testing/token_scope_fixtures.d.ts +24 -0
  253. package/dist/testing/token_scope_fixtures.d.ts.map +1 -0
  254. package/dist/testing/token_scope_fixtures.js +49 -0
  255. package/dist/testing/transports/bootstrap.d.ts +1 -1
  256. package/dist/testing/transports/bootstrap.d.ts.map +1 -1
  257. package/dist/testing/transports/bootstrap.js +13 -12
  258. package/dist/ui/BootstrapForm.svelte +4 -2
  259. package/dist/ui/BootstrapForm.svelte.d.ts.map +1 -1
  260. package/dist/ui/CLAUDE.md +4 -2
  261. package/dist/ui/SignupForm.svelte +4 -2
  262. package/dist/ui/SignupForm.svelte.d.ts.map +1 -1
  263. package/dist/ui/auth_state.svelte.d.ts.map +1 -1
  264. package/dist/ui/auth_state.svelte.js +2 -8
  265. package/dist/ui/backend_probe.svelte.js +3 -3
  266. package/dist/ui/table_state.svelte.d.ts.map +1 -1
  267. package/dist/ui/table_state.svelte.js +1 -8
  268. package/dist/ui/ui_fetch.d.ts +12 -2
  269. package/dist/ui/ui_fetch.d.ts.map +1 -1
  270. package/dist/ui/ui_fetch.js +22 -4
  271. package/package.json +1 -1
  272. package/dist/auth/audit_log_schema_fuz.d.ts +0 -53
  273. package/dist/auth/audit_log_schema_fuz.d.ts.map +0 -1
  274. package/dist/auth/audit_log_schema_fuz.js +0 -50
  275. package/dist/auth/role_schema_fuz.d.ts +0 -30
  276. package/dist/auth/role_schema_fuz.d.ts.map +0 -1
  277. package/dist/auth/role_schema_fuz.js +0 -30
  278. package/dist/auth/scope_kind_schema_fuz.d.ts +0 -31
  279. package/dist/auth/scope_kind_schema_fuz.d.ts.map +0 -1
  280. package/dist/auth/scope_kind_schema_fuz.js +0 -31
@@ -0,0 +1,70 @@
1
+ /**
2
+ * The DEV-only error check: an outer middleware that logs every JSON error
3
+ * response its route doesn't declare.
4
+ *
5
+ * Mounted outermost by `create_app_server`, so it sees what every layer
6
+ * answers — a middleware's refusal (the body limit, the origin check, the
7
+ * auth guards) as much as a handler's or the error catch's. After the request
8
+ * it finds the route the request reached (`create_route_lookup`: Hono's own
9
+ * router over the route specs in mount order, `HEAD` read as `GET`) and checks the
10
+ * response against that route's error map (`check_route_error`): an error
11
+ * status the map lacks, or a body off the status's schema, is logged. A 500
12
+ * `internal_error` and a 503 `service_unavailable` are always accepted, a
13
+ * JSON-RPC error envelope on a `jsonrpc` route is not a REST error body, a
14
+ * non-JSON response is skipped, and so is a request no route spec matches
15
+ * (an unmatched path's 404 is Hono's plain text, no JSON convention to hold
16
+ * it to).
17
+ *
18
+ * Logs, never throws — a throw inside it, its logger's included, is swallowed
19
+ * — and leaves the response unchanged. Outside DEV
20
+ * `create_dev_route_error_check` returns `null` and nothing is mounted.
21
+ *
22
+ * @module
23
+ */
24
+ import type { MiddlewareHandler } from 'hono';
25
+ import type { Logger } from '@fuzdev/fuz_util/log.ts';
26
+ import { type RouteErrorMap, type RouteErrorMapSpec } from './route_errors.js';
27
+ /**
28
+ * A lookup of the route a request reaches, by Hono's own router — the
29
+ * `SmartRouter` over a `RegExpRouter` and a `TrieRouter` that `new Hono()`
30
+ * builds: the routes are added in mount order, and a request's first match is
31
+ * the route Hono dispatches it to — `HEAD` read as `GET`, which Hono answers
32
+ * it with.
33
+ *
34
+ * Its router holds only the route specs, while the app's also holds every
35
+ * middleware and non-spec route (WS upgrades, static serving), which can put
36
+ * the app on a different router than the lookup's — a `TrieRouter` where the
37
+ * lookup has a `RegExpRouter`. Where the two differ the cost is a missed DEV
38
+ * log, and rarely a wrong route.
39
+ *
40
+ * @param routes - the routes, in the order they were mounted
41
+ * @param to_value - what to keep beside each route
42
+ * @returns a function from a request's method and path to the matched route's
43
+ * value, or `undefined` when none matches
44
+ */
45
+ export declare const create_route_lookup: <TRoute extends {
46
+ readonly method: string;
47
+ readonly path: string;
48
+ }, T>(routes: ReadonlyArray<TRoute>, to_value: (route: TRoute) => T) => ((method: string, path: string) => T | undefined);
49
+ /** What `create_dev_route_error_check` reads of a route spec. */
50
+ export interface DevRouteErrorCheckSpec extends RouteErrorMapSpec {
51
+ readonly jsonrpc?: boolean;
52
+ }
53
+ /** Options for `create_dev_route_error_check`. */
54
+ export interface DevRouteErrorCheckOptions {
55
+ /** The app's route specs, in mount order. */
56
+ route_specs: ReadonlyArray<DevRouteErrorCheckSpec>;
57
+ /** Each route's error map, keyed `METHOD path` (`resolve_app_route_error_maps`). */
58
+ route_error_maps: ReadonlyMap<string, RouteErrorMap>;
59
+ /** Receives each response its route doesn't declare. */
60
+ log: Logger;
61
+ }
62
+ /**
63
+ * Build the DEV error check (module doc), or `null` outside DEV.
64
+ *
65
+ * @param options - the routes, their error maps, and the logger
66
+ * @returns the middleware to mount outermost, or `null` in production
67
+ * @throws Error when `route_error_maps` lacks a route of `route_specs` (`get_route_error_map`)
68
+ */
69
+ export declare const create_dev_route_error_check: (options: DevRouteErrorCheckOptions) => MiddlewareHandler | null;
70
+ //# sourceMappingURL=dev_route_error_check.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dev_route_error_check.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/dev_route_error_check.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAK9C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAQtD,OAAO,EAAuB,KAAK,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEpG;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,mBAAmB,GAC/B,MAAM,SAAS;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EACjE,CAAC,EAED,QAAQ,aAAa,CAAC,MAAM,CAAC,EAC7B,UAAU,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,KAC5B,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC,GAAG,SAAS,CAclD,CAAC;AAEF,iEAAiE;AACjE,MAAM,WAAW,sBAAuB,SAAQ,iBAAiB;IAChE,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED,kDAAkD;AAClD,MAAM,WAAW,yBAAyB;IACzC,6CAA6C;IAC7C,WAAW,EAAE,aAAa,CAAC,sBAAsB,CAAC,CAAC;IACnD,oFAAoF;IACpF,gBAAgB,EAAE,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACrD,wDAAwD;IACxD,GAAG,EAAE,MAAM,CAAC;CACZ;AAED;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,GACxC,SAAS,yBAAyB,KAChC,iBAAiB,GAAG,IA2CtB,CAAC"}
@@ -0,0 +1,119 @@
1
+ /**
2
+ * The DEV-only error check: an outer middleware that logs every JSON error
3
+ * response its route doesn't declare.
4
+ *
5
+ * Mounted outermost by `create_app_server`, so it sees what every layer
6
+ * answers — a middleware's refusal (the body limit, the origin check, the
7
+ * auth guards) as much as a handler's or the error catch's. After the request
8
+ * it finds the route the request reached (`create_route_lookup`: Hono's own
9
+ * router over the route specs in mount order, `HEAD` read as `GET`) and checks the
10
+ * response against that route's error map (`check_route_error`): an error
11
+ * status the map lacks, or a body off the status's schema, is logged. A 500
12
+ * `internal_error` and a 503 `service_unavailable` are always accepted, a
13
+ * JSON-RPC error envelope on a `jsonrpc` route is not a REST error body, a
14
+ * non-JSON response is skipped, and so is a request no route spec matches
15
+ * (an unmatched path's 404 is Hono's plain text, no JSON convention to hold
16
+ * it to).
17
+ *
18
+ * Logs, never throws — a throw inside it, its logger's included, is swallowed
19
+ * — and leaves the response unchanged. Outside DEV
20
+ * `create_dev_route_error_check` returns `null` and nothing is mounted.
21
+ *
22
+ * @module
23
+ */
24
+ import { RegExpRouter } from 'hono/router/reg-exp-router';
25
+ import { SmartRouter } from 'hono/router/smart-router';
26
+ import { TrieRouter } from 'hono/router/trie-router';
27
+ import { DEV } from 'esm-env';
28
+ import { check_route_error, format_route_error_check, is_error_status, is_json_response } from './route_error_check.js';
29
+ import { get_route_error_map } from './route_errors.js';
30
+ /**
31
+ * A lookup of the route a request reaches, by Hono's own router — the
32
+ * `SmartRouter` over a `RegExpRouter` and a `TrieRouter` that `new Hono()`
33
+ * builds: the routes are added in mount order, and a request's first match is
34
+ * the route Hono dispatches it to — `HEAD` read as `GET`, which Hono answers
35
+ * it with.
36
+ *
37
+ * Its router holds only the route specs, while the app's also holds every
38
+ * middleware and non-spec route (WS upgrades, static serving), which can put
39
+ * the app on a different router than the lookup's — a `TrieRouter` where the
40
+ * lookup has a `RegExpRouter`. Where the two differ the cost is a missed DEV
41
+ * log, and rarely a wrong route.
42
+ *
43
+ * @param routes - the routes, in the order they were mounted
44
+ * @param to_value - what to keep beside each route
45
+ * @returns a function from a request's method and path to the matched route's
46
+ * value, or `undefined` when none matches
47
+ */
48
+ export const create_route_lookup = (routes, to_value) => {
49
+ const values = routes.map(to_value);
50
+ // `new Hono()`'s own router: a `RegExpRouter`, or a `TrieRouter` where the
51
+ // route set has a template the first can't compile — the two differ on an
52
+ // edge or two (`/w/*/c` on `/w//c`), so the lookup takes the one Hono would
53
+ const router = new SmartRouter({
54
+ routers: [new RegExpRouter(), new TrieRouter()]
55
+ });
56
+ routes.forEach((route, i) => router.add(route.method, route.path, i));
57
+ return (method, path) => {
58
+ const [matches] = router.match(method === 'HEAD' ? 'GET' : method, path);
59
+ const first = matches[0];
60
+ return first === undefined ? undefined : values[first[0]];
61
+ };
62
+ };
63
+ /**
64
+ * Build the DEV error check (module doc), or `null` outside DEV.
65
+ *
66
+ * @param options - the routes, their error maps, and the logger
67
+ * @returns the middleware to mount outermost, or `null` in production
68
+ * @throws Error when `route_error_maps` lacks a route of `route_specs` (`get_route_error_map`)
69
+ */
70
+ export const create_dev_route_error_check = (options) => {
71
+ if (!DEV)
72
+ return null;
73
+ const { route_error_maps, log } = options;
74
+ const find_route = create_route_lookup(options.route_specs, (spec) => ({
75
+ errors: get_route_error_map(route_error_maps, spec),
76
+ jsonrpc: spec.jsonrpc ?? false
77
+ }));
78
+ // named, so an assembled app's route table shows it
79
+ const dev_route_error_check = async (c, next) => {
80
+ await next();
81
+ // the check never changes the response: a throw in it, a logger's
82
+ // included, is logged if it can be and swallowed, so it never reaches `onError`
83
+ try {
84
+ const response = c.res;
85
+ if (!is_error_status(response.status))
86
+ return;
87
+ if (!is_json_response(response))
88
+ return;
89
+ const route = find_route(c.req.method, c.req.path);
90
+ if (!route)
91
+ return;
92
+ let body;
93
+ try {
94
+ body = await response.clone().json();
95
+ }
96
+ catch {
97
+ // a body claiming JSON that isn't
98
+ return;
99
+ }
100
+ const check = check_route_error(route.errors, response.status, body, route.jsonrpc);
101
+ if (check.ok)
102
+ return;
103
+ const message = format_route_error_check(check, response.status, `${c.req.method} ${c.req.path}`);
104
+ if (check.kind === 'schema_mismatch')
105
+ log.error(message, check.issues);
106
+ else
107
+ log.error(message, body);
108
+ }
109
+ catch (err) {
110
+ try {
111
+ log.error('dev_route_error_check failed:', err);
112
+ }
113
+ catch {
114
+ // the logger itself threw; nothing left to tell
115
+ }
116
+ }
117
+ };
118
+ return dev_route_error_check;
119
+ };