@fuzdev/fuz_app 0.163.0 → 0.165.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 (199) 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 +1 -1
  8. package/dist/auth/account_route_schema.d.ts +3 -27
  9. package/dist/auth/account_route_schema.d.ts.map +1 -1
  10. package/dist/auth/account_route_schema.js +6 -20
  11. package/dist/auth/admin_action_specs.d.ts +56 -56
  12. package/dist/auth/audit_log_route_schema.d.ts +1 -1
  13. package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
  14. package/dist/auth/audit_log_route_schema.js +4 -5
  15. package/dist/auth/audit_log_schema.d.ts +18 -18
  16. package/dist/auth/bootstrap_route_schema.d.ts +1 -30
  17. package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
  18. package/dist/auth/bootstrap_route_schema.js +3 -8
  19. package/dist/auth/daemon_token.d.ts +4 -4
  20. package/dist/auth/daemon_token.d.ts.map +1 -1
  21. package/dist/auth/daemon_token.js +3 -3
  22. package/dist/auth/daemon_token_middleware.d.ts +1 -1
  23. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  24. package/dist/auth/daemon_token_middleware.js +5 -5
  25. package/dist/auth/middleware.js +4 -4
  26. package/dist/auth/role_grant_offer_action_specs.d.ts +2 -2
  27. package/dist/auth/signup_route_schema.d.ts.map +1 -1
  28. package/dist/auth/signup_route_schema.js +3 -7
  29. package/dist/db/CLAUDE.md +2 -1
  30. package/dist/db/file_fact_url.d.ts +11 -6
  31. package/dist/db/file_fact_url.d.ts.map +1 -1
  32. package/dist/db/file_fact_url.js +5 -5
  33. package/dist/db/schema_ready.d.ts +0 -5
  34. package/dist/db/schema_ready.d.ts.map +1 -1
  35. package/dist/db/schema_ready.js +0 -5
  36. package/dist/http/CLAUDE.md +105 -71
  37. package/dist/http/auth_shape.d.ts +48 -29
  38. package/dist/http/auth_shape.d.ts.map +1 -1
  39. package/dist/http/auth_shape.js +12 -85
  40. package/dist/http/body_limit.d.ts +12 -0
  41. package/dist/http/body_limit.d.ts.map +1 -1
  42. package/dist/http/body_limit.js +15 -0
  43. package/dist/http/body_limit_schema.d.ts +20 -0
  44. package/dist/http/body_limit_schema.d.ts.map +1 -0
  45. package/dist/http/body_limit_schema.js +20 -0
  46. package/dist/http/common_routes.d.ts +1 -1
  47. package/dist/http/common_routes.d.ts.map +1 -1
  48. package/dist/http/common_routes.js +11 -14
  49. package/dist/http/db_routes.d.ts.map +1 -1
  50. package/dist/http/db_routes.js +59 -69
  51. package/dist/http/dev_route_error_check.d.ts +70 -0
  52. package/dist/http/dev_route_error_check.d.ts.map +1 -0
  53. package/dist/http/dev_route_error_check.js +119 -0
  54. package/dist/http/error_schemas.d.ts +365 -189
  55. package/dist/http/error_schemas.d.ts.map +1 -1
  56. package/dist/http/error_schemas.js +256 -149
  57. package/dist/http/health_schema.d.ts +10 -6
  58. package/dist/http/health_schema.d.ts.map +1 -1
  59. package/dist/http/health_schema.js +3 -3
  60. package/dist/http/middleware_spec.d.ts +8 -3
  61. package/dist/http/middleware_spec.d.ts.map +1 -1
  62. package/dist/http/origin.d.ts +12 -43
  63. package/dist/http/origin.d.ts.map +1 -1
  64. package/dist/http/origin.js +10 -6
  65. package/dist/http/route_error_check.d.ts +179 -0
  66. package/dist/http/route_error_check.d.ts.map +1 -0
  67. package/dist/http/route_error_check.js +283 -0
  68. package/dist/http/route_errors.d.ts +80 -0
  69. package/dist/http/route_errors.d.ts.map +1 -0
  70. package/dist/http/route_errors.js +83 -0
  71. package/dist/http/route_spec.d.ts +33 -17
  72. package/dist/http/route_spec.d.ts.map +1 -1
  73. package/dist/http/route_spec.js +48 -62
  74. package/dist/http/schema_helpers.d.ts +15 -34
  75. package/dist/http/schema_helpers.d.ts.map +1 -1
  76. package/dist/http/schema_helpers.js +11 -47
  77. package/dist/http/surface.d.ts +19 -12
  78. package/dist/http/surface.d.ts.map +1 -1
  79. package/dist/http/surface.js +25 -38
  80. package/dist/http/surface_query.d.ts +11 -2
  81. package/dist/http/surface_query.d.ts.map +1 -1
  82. package/dist/http/surface_query.js +7 -1
  83. package/dist/primitive_schemas_fuz.d.ts +17 -3
  84. package/dist/primitive_schemas_fuz.d.ts.map +1 -1
  85. package/dist/primitive_schemas_fuz.js +13 -3
  86. package/dist/rate_limiter.d.ts +13 -0
  87. package/dist/rate_limiter.d.ts.map +1 -1
  88. package/dist/rate_limiter.js +20 -2
  89. package/dist/schema_issues.d.ts +29 -11
  90. package/dist/schema_issues.d.ts.map +1 -1
  91. package/dist/schema_issues.js +11 -10
  92. package/dist/schema_meta.d.ts +8 -1
  93. package/dist/schema_meta.d.ts.map +1 -1
  94. package/dist/schema_meta.js +2 -1
  95. package/dist/server/app_server.d.ts +3 -1
  96. package/dist/server/app_server.d.ts.map +1 -1
  97. package/dist/server/app_server.js +24 -10
  98. package/dist/server/serve_fact_route.d.ts.map +1 -1
  99. package/dist/server/serve_fact_route.js +8 -31
  100. package/dist/server/surface_route.d.ts.map +1 -1
  101. package/dist/server/surface_route.js +5 -6
  102. package/dist/server/x_accel.d.ts +12 -7
  103. package/dist/server/x_accel.d.ts.map +1 -1
  104. package/dist/server/x_accel.js +7 -9
  105. package/dist/testing/CLAUDE.md +105 -61
  106. package/dist/testing/admin_integration.js +2 -2
  107. package/dist/testing/adversarial_404.d.ts +4 -18
  108. package/dist/testing/adversarial_404.d.ts.map +1 -1
  109. package/dist/testing/adversarial_404.js +20 -52
  110. package/dist/testing/adversarial_headers.d.ts +3 -4
  111. package/dist/testing/adversarial_headers.d.ts.map +1 -1
  112. package/dist/testing/adversarial_headers.js +8 -6
  113. package/dist/testing/adversarial_input.d.ts.map +1 -1
  114. package/dist/testing/adversarial_input.js +20 -19
  115. package/dist/testing/assertions.d.ts +46 -14
  116. package/dist/testing/assertions.d.ts.map +1 -1
  117. package/dist/testing/assertions.js +73 -16
  118. package/dist/testing/attack_surface.d.ts +17 -35
  119. package/dist/testing/attack_surface.d.ts.map +1 -1
  120. package/dist/testing/attack_surface.js +36 -95
  121. package/dist/testing/auth_apps.d.ts.map +1 -1
  122. package/dist/testing/auth_apps.js +2 -1
  123. package/dist/testing/cross_backend/action_manifest.d.ts +126 -56
  124. package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
  125. package/dist/testing/cross_backend/action_manifest.js +11 -11
  126. package/dist/testing/cross_backend/action_manifest_parity.d.ts +1 -1
  127. package/dist/testing/cross_backend/action_manifest_parity.d.ts.map +1 -1
  128. package/dist/testing/cross_backend/cell_crud.d.ts.map +1 -1
  129. package/dist/testing/cross_backend/cell_crud.js +2 -1
  130. package/dist/testing/cross_backend/conformance_table.js +1 -1
  131. package/dist/testing/cross_backend/fact_serving.js +3 -3
  132. package/dist/testing/cross_backend/in_process_setup.d.ts +7 -3
  133. package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
  134. package/dist/testing/cross_backend/in_process_setup.js +11 -4
  135. package/dist/testing/cross_backend/origin_conformance.d.ts +278 -102
  136. package/dist/testing/cross_backend/origin_conformance.d.ts.map +1 -1
  137. package/dist/testing/cross_backend/origin_conformance.js +21 -21
  138. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  139. package/dist/testing/cross_backend/rust_spine_consumer_surface.js +7 -1
  140. package/dist/testing/cross_backend/setup.d.ts +13 -0
  141. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  142. package/dist/testing/cross_backend/setup.js +24 -7
  143. package/dist/testing/cross_backend/spine_suites.d.ts +3 -2
  144. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  145. package/dist/testing/cross_backend/spine_suites.js +5 -2
  146. package/dist/testing/cross_backend/standard.d.ts +31 -0
  147. package/dist/testing/cross_backend/standard.d.ts.map +1 -1
  148. package/dist/testing/cross_backend/standard.js +9 -3
  149. package/dist/testing/cross_backend/testing_origin_check.d.ts +147 -71
  150. package/dist/testing/cross_backend/testing_origin_check.d.ts.map +1 -1
  151. package/dist/testing/cross_backend/testing_origin_check.js +16 -13
  152. package/dist/testing/cross_backend/testing_reset_actions.d.ts +70 -27
  153. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  154. package/dist/testing/data_exposure.d.ts +7 -0
  155. package/dist/testing/data_exposure.d.ts.map +1 -1
  156. package/dist/testing/data_exposure.js +8 -7
  157. package/dist/testing/error_coverage.d.ts +53 -61
  158. package/dist/testing/error_coverage.d.ts.map +1 -1
  159. package/dist/testing/error_coverage.js +86 -119
  160. package/dist/testing/integration.d.ts +28 -1
  161. package/dist/testing/integration.d.ts.map +1 -1
  162. package/dist/testing/integration.js +54 -29
  163. package/dist/testing/integration_helpers.d.ts +37 -9
  164. package/dist/testing/integration_helpers.d.ts.map +1 -1
  165. package/dist/testing/integration_helpers.js +75 -40
  166. package/dist/testing/middleware.d.ts +3 -5
  167. package/dist/testing/middleware.d.ts.map +1 -1
  168. package/dist/testing/middleware.js +5 -4
  169. package/dist/testing/middleware_gate_probes.d.ts +90 -0
  170. package/dist/testing/middleware_gate_probes.d.ts.map +1 -0
  171. package/dist/testing/middleware_gate_probes.js +179 -0
  172. package/dist/testing/rate_limiting.d.ts +1 -1
  173. package/dist/testing/rate_limiting.d.ts.map +1 -1
  174. package/dist/testing/rate_limiting.js +5 -4
  175. package/dist/testing/round_trip.d.ts +7 -0
  176. package/dist/testing/round_trip.d.ts.map +1 -1
  177. package/dist/testing/round_trip.js +8 -5
  178. package/dist/testing/schema_generators.d.ts +40 -11
  179. package/dist/testing/schema_generators.d.ts.map +1 -1
  180. package/dist/testing/schema_generators.js +71 -17
  181. package/dist/testing/stubs.d.ts +10 -1
  182. package/dist/testing/stubs.d.ts.map +1 -1
  183. package/dist/testing/stubs.js +15 -5
  184. package/dist/testing/surface_invariants.d.ts +36 -115
  185. package/dist/testing/surface_invariants.d.ts.map +1 -1
  186. package/dist/testing/surface_invariants.js +106 -269
  187. package/dist/testing/transports/raw_http.d.ts +10 -2
  188. package/dist/testing/transports/raw_http.d.ts.map +1 -1
  189. package/dist/testing/transports/raw_http.js +10 -4
  190. package/dist/ui/CLAUDE.md +4 -2
  191. package/dist/ui/auth_state.svelte.d.ts.map +1 -1
  192. package/dist/ui/auth_state.svelte.js +2 -8
  193. package/dist/ui/backend_probe.svelte.js +3 -3
  194. package/dist/ui/table_state.svelte.d.ts.map +1 -1
  195. package/dist/ui/table_state.svelte.js +1 -8
  196. package/dist/ui/ui_fetch.d.ts +12 -2
  197. package/dist/ui/ui_fetch.d.ts.map +1 -1
  198. package/dist/ui/ui_fetch.js +22 -4
  199. package/package.json +1 -1
@@ -20,7 +20,7 @@
20
20
  */
21
21
  import type { Result } from '@fuzdev/fuz_util/result.ts';
22
22
  import type { Context, Handler } from 'hono';
23
- import { z } from 'zod';
23
+ import { type Infer } from '@fuzdev/fuz_schema/schema.ts';
24
24
  import type { MiddlewareCarveOut } from './middleware_spec.js';
25
25
  /**
26
26
  * True when the request looks like it originated from a browser context — it
@@ -40,49 +40,18 @@ import type { MiddlewareCarveOut } from './middleware_spec.js';
40
40
  */
41
41
  export declare const is_browser_context: (c: Context) => boolean;
42
42
  /**
43
- * Why one allowlist entry was refused. The wire names both spines emit — the
44
- * Rust spine's `OriginPatternError::kind` returns the same strings.
43
+ * Why one allowlist entry was refused, a `@fuzdev/fuz_schema` `one_of` whose
44
+ * `def` is the names. The wire names both spines emit — the Rust spine's
45
+ * `OriginPatternError::kind` returns the same strings.
45
46
  */
46
- export declare const OriginPatternErrorKind: z.ZodEnum<{
47
- empty_entry: "empty_entry";
48
- not_printable_ascii: "not_printable_ascii";
49
- missing_scheme: "missing_scheme";
50
- unsupported_scheme: "unsupported_scheme";
51
- has_path: "has_path";
52
- has_userinfo: "has_userinfo";
53
- missing_host: "missing_host";
54
- invalid_host_char: "invalid_host_char";
55
- unbracketed_ipv6: "unbracketed_ipv6";
56
- partial_wildcard: "partial_wildcard";
57
- non_canonical_ipv4: "non_canonical_ipv4";
58
- unclosed_ipv6: "unclosed_ipv6";
59
- invalid_ipv6: "invalid_ipv6";
60
- invalid_after_ipv6: "invalid_after_ipv6";
61
- invalid_port: "invalid_port";
62
- default_port: "default_port";
63
- }>;
64
- export type OriginPatternErrorKind = z.infer<typeof OriginPatternErrorKind>;
65
- /** Why an allowlist value was refused: `no_origins` (a blank value) or an entry's kind. */
66
- export declare const AllowedOriginsErrorKind: z.ZodEnum<{
67
- empty_entry: "empty_entry";
68
- not_printable_ascii: "not_printable_ascii";
69
- missing_scheme: "missing_scheme";
70
- unsupported_scheme: "unsupported_scheme";
71
- has_path: "has_path";
72
- has_userinfo: "has_userinfo";
73
- missing_host: "missing_host";
74
- invalid_host_char: "invalid_host_char";
75
- unbracketed_ipv6: "unbracketed_ipv6";
76
- partial_wildcard: "partial_wildcard";
77
- non_canonical_ipv4: "non_canonical_ipv4";
78
- unclosed_ipv6: "unclosed_ipv6";
79
- invalid_ipv6: "invalid_ipv6";
80
- invalid_after_ipv6: "invalid_after_ipv6";
81
- invalid_port: "invalid_port";
82
- default_port: "default_port";
83
- no_origins: "no_origins";
84
- }>;
85
- export type AllowedOriginsErrorKind = z.infer<typeof AllowedOriginsErrorKind>;
47
+ export declare const OriginPatternErrorKind: import("@fuzdev/fuz_schema/schema.ts").Schema<"empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | "partial_wildcard" | "non_canonical_ipv4" | "unclosed_ipv6" | "invalid_ipv6" | "invalid_after_ipv6" | "invalid_port" | "default_port", "empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | "partial_wildcard" | "non_canonical_ipv4" | "unclosed_ipv6" | "invalid_ipv6" | "invalid_after_ipv6" | "invalid_port" | "default_port", "one_of", readonly ["empty_entry", "not_printable_ascii", "missing_scheme", "unsupported_scheme", "has_path", "has_userinfo", "missing_host", "invalid_host_char", "unbracketed_ipv6", "partial_wildcard", "non_canonical_ipv4", "unclosed_ipv6", "invalid_ipv6", "invalid_after_ipv6", "invalid_port", "default_port"]>;
48
+ export type OriginPatternErrorKind = Infer<typeof OriginPatternErrorKind>;
49
+ /**
50
+ * Why an allowlist value was refused: `no_origins` (a blank value) or an
51
+ * entry's kind. A `one_of` whose `def` is the names.
52
+ */
53
+ export declare const AllowedOriginsErrorKind: import("@fuzdev/fuz_schema/schema.ts").Schema<"empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | "partial_wildcard" | "non_canonical_ipv4" | "unclosed_ipv6" | "invalid_ipv6" | "invalid_after_ipv6" | "invalid_port" | "default_port" | "no_origins", "empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | "partial_wildcard" | "non_canonical_ipv4" | "unclosed_ipv6" | "invalid_ipv6" | "invalid_after_ipv6" | "invalid_port" | "default_port" | "no_origins", "one_of", readonly ["no_origins", "empty_entry", "not_printable_ascii", "missing_scheme", "unsupported_scheme", "has_path", "has_userinfo", "missing_host", "invalid_host_char", "unbracketed_ipv6", "partial_wildcard", "non_canonical_ipv4", "unclosed_ipv6", "invalid_ipv6", "invalid_after_ipv6", "invalid_port", "default_port"]>;
54
+ export type AllowedOriginsErrorKind = Infer<typeof AllowedOriginsErrorKind>;
86
55
  /** The operator-facing reason for each refusal kind. */
87
56
  export declare const ALLOWED_ORIGINS_ERROR_MESSAGES: Readonly<Record<AllowedOriginsErrorKind, string>>;
88
57
  /** One reason `parse_allowed_origins` refused a value. */
@@ -1 +1 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C7B,sCAAsC;IACtC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,+CAA+C;IAC/C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uFAAuF;IACvF,QAAQ,CAAC,WAAW,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;IACnD,4DAA4D;IAC5D,QAAQ,CAAC,IAAI,EAAE,QAAQ,GAAG,KAAK,GAAG,OAAO,CAAC;CAC1C;AAWD;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,KAAG,aAAa,GAAG,SAGlE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAC/B,QAAQ,MAAM,EACd,kBAAkB,aAAa,CAAC,MAAM,CAAC,KACrC,OAAqF,CAAC;AAEzF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,qBAAqB,GAEhC,kBAAkB,aAAa,CAAC,MAAM,CAAC,EACvC,aAAY,aAAa,CAAC,kBAAkB,CAAM,KAChD,OAaF,CAAC"}
@@ -19,7 +19,7 @@
19
19
  * @module
20
20
  */
21
21
  import { escape_regexp } from '@fuzdev/fuz_util/regexp.ts';
22
- import { z } from 'zod';
22
+ import { one_of } from '@fuzdev/fuz_schema/schema.ts';
23
23
  import { ERROR_FORBIDDEN_ORIGIN } from './error_schemas.js';
24
24
  import { middleware_applies } from './schema_helpers.js';
25
25
  /**
@@ -40,10 +40,11 @@ import { middleware_applies } from './schema_helpers.js';
40
40
  */
41
41
  export const is_browser_context = (c) => c.req.header('Origin') !== undefined || c.req.header('Referer') !== undefined;
42
42
  /**
43
- * Why one allowlist entry was refused. The wire names both spines emit — the
44
- * Rust spine's `OriginPatternError::kind` returns the same strings.
43
+ * Why one allowlist entry was refused, a `@fuzdev/fuz_schema` `one_of` whose
44
+ * `def` is the names. The wire names both spines emit — the Rust spine's
45
+ * `OriginPatternError::kind` returns the same strings.
45
46
  */
46
- export const OriginPatternErrorKind = z.enum([
47
+ export const OriginPatternErrorKind = one_of([
47
48
  'empty_entry',
48
49
  'not_printable_ascii',
49
50
  'missing_scheme',
@@ -61,8 +62,11 @@ export const OriginPatternErrorKind = z.enum([
61
62
  'invalid_port',
62
63
  'default_port'
63
64
  ]);
64
- /** Why an allowlist value was refused: `no_origins` (a blank value) or an entry's kind. */
65
- export const AllowedOriginsErrorKind = z.enum(['no_origins', ...OriginPatternErrorKind.options]);
65
+ /**
66
+ * Why an allowlist value was refused: `no_origins` (a blank value) or an
67
+ * entry's kind. A `one_of` whose `def` is the names.
68
+ */
69
+ export const AllowedOriginsErrorKind = one_of(['no_origins', ...OriginPatternErrorKind.def]);
66
70
  /** The operator-facing reason for each refusal kind. */
67
71
  export const ALLOWED_ORIGINS_ERROR_MESSAGES = {
68
72
  no_origins: 'no origins supplied — list at least one',
@@ -0,0 +1,179 @@
1
+ /**
2
+ * Whether an error response is one its route declares — the one rule the DEV
3
+ * error check (`http/dev_route_error_check.ts`) and the test helpers
4
+ * (`assert_response_matches_spec`) share.
5
+ *
6
+ * A JSON body at an error status (400 and up) is declared when the route's
7
+ * error map (`resolve_route_error_map`) has that status and the body parses
8
+ * against it. Three exemptions: a 500 `internal_error` and a 503
9
+ * `service_unavailable` (`ALWAYS_ACCEPTED_ERRORS`), which the catch layers
10
+ * answer on any route and no route declares, and, on a `jsonrpc` route, a
11
+ * JSON-RPC error envelope, which no REST error definition describes. A
12
+ * non-JSON response, by `is_json_response`, is not checked at all.
13
+ *
14
+ * Also the hono-free route lookup the test helpers run (`find_route_spec`):
15
+ * a route's Hono path template read segment by segment (`split_route_template`,
16
+ * `parse_route_param`) and compiled to a `RegExp` (`compile_route_path`),
17
+ * and the first route in mount order matching a request, `HEAD` read as
18
+ * `GET` (`find_mounted_route`). The DEV check, on the Hono side, looks routes
19
+ * up with Hono's own router (`create_route_lookup`); the two are pinned
20
+ * against Hono and each other by the route-lookup tests.
21
+ *
22
+ * Hono-free and `esm-env`-free.
23
+ *
24
+ * @module
25
+ */
26
+ import { type RestErrorDef } from './error_schemas.js';
27
+ import type { RouteErrorMap } from './route_errors.js';
28
+ import { type AnyIssue } from '../schema_issues.js';
29
+ /**
30
+ * The errors any route can answer with and none declares: the catch layer's
31
+ * 500 `internal_error` for a generic throw, and a failed pool checkout's 503
32
+ * `service_unavailable`. Always accepted by `check_route_error`.
33
+ */
34
+ export declare const ALWAYS_ACCEPTED_ERRORS: ReadonlyArray<RestErrorDef>;
35
+ /**
36
+ * Whether a status is an error status, the ones `check_route_error` reads:
37
+ * 400 and up.
38
+ *
39
+ * @param status - the HTTP status
40
+ */
41
+ export declare const is_error_status: (status: number) => boolean;
42
+ /**
43
+ * Whether a response is JSON by its `Content-Type`, the gate before any
44
+ * error check: a non-JSON response (SSE, bytes, Hono's plain-text 404) is no
45
+ * error body to check, whatever its text looks like.
46
+ *
47
+ * @param response - the response
48
+ */
49
+ export declare const is_json_response: (response: Response) => boolean;
50
+ /** What `check_route_error` found of an error response. */
51
+ export type RouteErrorCheck = {
52
+ ok: true;
53
+ } | {
54
+ ok: false;
55
+ kind: 'undeclared_status';
56
+ } | {
57
+ ok: false;
58
+ kind: 'schema_mismatch';
59
+ issues: Array<AnyIssue>;
60
+ };
61
+ /**
62
+ * Check a JSON error body against its route's error map.
63
+ *
64
+ * @param errors - the route's error map
65
+ * @param status - the response status, an error status (`is_error_status`)
66
+ * @param body - the parsed JSON body
67
+ * @param jsonrpc - whether the route answers JSON-RPC envelopes (`RouteSpec.jsonrpc`)
68
+ * @returns `ok`, or why not: no definition at the status, or a body off the status's schema
69
+ */
70
+ export declare const check_route_error: (errors: RouteErrorMap, status: number, body: unknown, jsonrpc: boolean) => RouteErrorCheck;
71
+ /**
72
+ * Format a failed `check_route_error` as one line: `Undeclared error status
73
+ * (S): METHOD path` or `Error schema mismatch (S): METHOD path`. The DEV
74
+ * check logs it, with the issues or the body as a second argument; the test
75
+ * helpers (`assert_response_matches_spec`, `assert_error_response_declared`)
76
+ * throw it with the issues or the body as JSON after a `: `.
77
+ *
78
+ * @param check - the failed check
79
+ * @param status - the response status
80
+ * @param route - what names the request, `METHOD path`
81
+ */
82
+ export declare const format_route_error_check: (check: Exclude<RouteErrorCheck, {
83
+ ok: true;
84
+ }>, status: number, route: string) => string;
85
+ /** A param segment of a route template, as `parse_route_param` reads it. */
86
+ export interface RouteParam {
87
+ /** The param's name, the key of its value in the route's `params`. */
88
+ readonly name: string;
89
+ /** The `re` of `:name{re}`, or `null` for a bare `:name`. */
90
+ readonly pattern: string | null;
91
+ /** Whether the segment ends in `?`, an optional param. */
92
+ readonly optional: boolean;
93
+ }
94
+ /**
95
+ * Read a template segment as Hono reads a param: a whole segment `:name` or
96
+ * `:name{re}`, either with a trailing `?`. A segment that doesn't start with
97
+ * `:` is literal text, a `:` inside it (`files:upload`) included.
98
+ *
99
+ * @param segment - one segment of a template split by `split_route_template`
100
+ * @returns the param, or `null` for a segment that is not one
101
+ */
102
+ export declare const parse_route_param: (segment: string) => RouteParam | null;
103
+ /**
104
+ * A template's segments, split on `/` with each `{…}` group kept whole, as
105
+ * Hono's `splitRoutingPath` splits them; a leading `/` gives no empty first
106
+ * segment.
107
+ *
108
+ * @param template - a route's path template
109
+ */
110
+ export declare const split_route_template: (template: string) => Array<string>;
111
+ /**
112
+ * Compile the `re` of a `:name{re}` param to the `RegExp` a value of the param
113
+ * must match, grouped and anchored at both ends, `^(?:re)$`, as
114
+ * `compile_route_path` reads it inside a template. That is Hono's RegExpRouter
115
+ * reading; its TrieRouter, which SmartRouter falls back to for some route
116
+ * sets, anchors `^re$` and leaves a top-level alternation ungrouped, so
117
+ * `^a|b$` takes `ab` where the grouped form refuses it. The grouped form is
118
+ * the stricter of the two: a value it accepts both routers accept. Test it
119
+ * against the value as Hono reads it (`decode_route_path`).
120
+ *
121
+ * @param pattern - the param's `re` (`RouteParam.pattern`)
122
+ */
123
+ export declare const compile_route_param_pattern: (pattern: string) => RegExp;
124
+ /**
125
+ * A request path, or a part of one, as Hono's router reads it: percent-decoded
126
+ * by `decodeURI` with `%25` kept, so an encoded `/`, `#`, `?` or `%` stays
127
+ * encoded, or the raw text when decoding fails.
128
+ *
129
+ * @param path - the request pathname, or a percent-encoded segment of one
130
+ */
131
+ export declare const decode_route_path: (path: string) => string;
132
+ /**
133
+ * Compile a Hono path template to a `RegExp` over request paths, reading it
134
+ * as Hono 4's routers do: the template is split on `/` with each `{…}` group
135
+ * kept whole, and each segment is a `*`, a param — a whole segment
136
+ * `:name` (one non-empty segment) or `:name{re}` (the pattern `re`, which may
137
+ * span segments) — or literal text, so a `:` inside a segment (`files:upload`,
138
+ * `:x-:y`'s second) is no param of its own; a literal segment holding a `*`
139
+ * (`/a*`), a wildcard in Hono, throws rather than be guessed at. A template
140
+ * ending in `?` expands
141
+ * its optional params as Hono's `checkOptionalParameter` does. A trailing
142
+ * `/*` matches the prefix with anything after it or nothing (so `/api/*`
143
+ * matches `/api`), a `*` elsewhere one non-empty segment, and `*` alone every
144
+ * path. Trailing slashes are strict, as Hono's are by default. Pinned against
145
+ * Hono itself by the route-lookup tests.
146
+ *
147
+ * @param template - the route's path template (`RouteSpec.path`)
148
+ * @returns an anchored `RegExp` over a request path
149
+ * @throws Error on a segment holding a `*` that is not the whole segment
150
+ * (`/a*`), a wildcard in Hono this lookup refuses to guess at
151
+ */
152
+ export declare const compile_route_path: (template: string) => RegExp;
153
+ /** A route a request can be matched to: its method, its compiled path, and what the caller keeps beside it. */
154
+ export interface MountedRoute<T> {
155
+ readonly method: string;
156
+ readonly pattern: RegExp;
157
+ readonly value: T;
158
+ }
159
+ /**
160
+ * Compile routes for `find_mounted_route`, in mount order.
161
+ *
162
+ * @param routes - the routes, in the order they were mounted
163
+ * @param to_value - what to keep beside each route
164
+ */
165
+ export declare const to_mounted_routes: <TRoute extends {
166
+ readonly method: string;
167
+ readonly path: string;
168
+ }, T>(routes: ReadonlyArray<TRoute>, to_value: (route: TRoute) => T) => Array<MountedRoute<T>>;
169
+ /**
170
+ * The first route in mount order that a request matches, as Hono picks the
171
+ * handler — a `HEAD` read as a `GET`, which Hono answers it with.
172
+ *
173
+ * @param routes - the routes, compiled by `to_mounted_routes`
174
+ * @param method - the request method
175
+ * @param path - the request path
176
+ * @returns the matched route's value, or `undefined` when no route matches
177
+ */
178
+ export declare const find_mounted_route: <T>(routes: ReadonlyArray<MountedRoute<T>>, method: string, path: string) => T | undefined;
179
+ //# sourceMappingURL=route_error_check.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route_error_check.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/route_error_check.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAIH,OAAO,EAGN,KAAK,YAAY,EACjB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAa,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/D;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,aAAa,CAAC,YAAY,CAG9D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAI,QAAQ,MAAM,KAAG,OAAwB,CAAC;AAE1E;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAAI,UAAU,QAAQ,KAAG,OACsB,CAAC;AAE7E,2DAA2D;AAC3D,MAAM,MAAM,eAAe,GACxB;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GACZ;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,mBAAmB,CAAA;CAAE,GACxC;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,iBAAiB,CAAC;IAAC,MAAM,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAA;CAAE,CAAC;AAEnE;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB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AAC,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,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAKpB,CAAC;AAEL;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EACnC,QAAQ,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EACtC,QAAQ,MAAM,EACd,MAAM,MAAM,KACV,CAAC,GAAG,SAMN,CAAC"}
@@ -0,0 +1,283 @@
1
+ /**
2
+ * Whether an error response is one its route declares — the one rule the DEV
3
+ * error check (`http/dev_route_error_check.ts`) and the test helpers
4
+ * (`assert_response_matches_spec`) share.
5
+ *
6
+ * A JSON body at an error status (400 and up) is declared when the route's
7
+ * error map (`resolve_route_error_map`) has that status and the body parses
8
+ * against it. Three exemptions: a 500 `internal_error` and a 503
9
+ * `service_unavailable` (`ALWAYS_ACCEPTED_ERRORS`), which the catch layers
10
+ * answer on any route and no route declares, and, on a `jsonrpc` route, a
11
+ * JSON-RPC error envelope, which no REST error definition describes. A
12
+ * non-JSON response, by `is_json_response`, is not checked at all.
13
+ *
14
+ * Also the hono-free route lookup the test helpers run (`find_route_spec`):
15
+ * a route's Hono path template read segment by segment (`split_route_template`,
16
+ * `parse_route_param`) and compiled to a `RegExp` (`compile_route_path`),
17
+ * and the first route in mount order matching a request, `HEAD` read as
18
+ * `GET` (`find_mounted_route`). The DEV check, on the Hono side, looks routes
19
+ * up with Hono's own router (`create_route_lookup`); the two are pinned
20
+ * against Hono and each other by the route-lookup tests.
21
+ *
22
+ * Hono-free and `esm-env`-free.
23
+ *
24
+ * @module
25
+ */
26
+ import { is } from '@fuzdev/fuz_schema/schema.ts';
27
+ import { REST_INTERNAL_ERROR, REST_SERVICE_UNAVAILABLE } from './error_schemas.js';
28
+ import { is_jsonrpc_error_response } from './jsonrpc_helpers.js';
29
+ import { parse_any } from '../schema_issues.js';
30
+ /**
31
+ * The errors any route can answer with and none declares: the catch layer's
32
+ * 500 `internal_error` for a generic throw, and a failed pool checkout's 503
33
+ * `service_unavailable`. Always accepted by `check_route_error`.
34
+ */
35
+ export const ALWAYS_ACCEPTED_ERRORS = [
36
+ REST_INTERNAL_ERROR,
37
+ REST_SERVICE_UNAVAILABLE
38
+ ];
39
+ /**
40
+ * Whether a status is an error status, the ones `check_route_error` reads:
41
+ * 400 and up.
42
+ *
43
+ * @param status - the HTTP status
44
+ */
45
+ export const is_error_status = (status) => status >= 400;
46
+ /**
47
+ * Whether a response is JSON by its `Content-Type`, the gate before any
48
+ * error check: a non-JSON response (SSE, bytes, Hono's plain-text 404) is no
49
+ * error body to check, whatever its text looks like.
50
+ *
51
+ * @param response - the response
52
+ */
53
+ export const is_json_response = (response) => response.headers.get('Content-Type')?.includes('application/json') ?? false;
54
+ /**
55
+ * Check a JSON error body against its route's error map.
56
+ *
57
+ * @param errors - the route's error map
58
+ * @param status - the response status, an error status (`is_error_status`)
59
+ * @param body - the parsed JSON body
60
+ * @param jsonrpc - whether the route answers JSON-RPC envelopes (`RouteSpec.jsonrpc`)
61
+ * @returns `ok`, or why not: no definition at the status, or a body off the status's schema
62
+ */
63
+ export const check_route_error = (errors, status, body, jsonrpc) => {
64
+ // the JSON-RPC endpoint answers its refusals as envelopes, which no REST
65
+ // error definition describes; the dispatch core checks its own outputs
66
+ if (jsonrpc && is_jsonrpc_error_response(body))
67
+ return { ok: true };
68
+ if (ALWAYS_ACCEPTED_ERRORS.some((def) => def.status === status && is(def.schema, body))) {
69
+ return { ok: true };
70
+ }
71
+ const schema = errors.schemas[status];
72
+ if (!schema)
73
+ return { ok: false, kind: 'undeclared_status' };
74
+ const result = parse_any(schema, body);
75
+ return result.ok ? { ok: true } : { ok: false, kind: 'schema_mismatch', issues: result.issues };
76
+ };
77
+ /**
78
+ * Format a failed `check_route_error` as one line: `Undeclared error status
79
+ * (S): METHOD path` or `Error schema mismatch (S): METHOD path`. The DEV
80
+ * check logs it, with the issues or the body as a second argument; the test
81
+ * helpers (`assert_response_matches_spec`, `assert_error_response_declared`)
82
+ * throw it with the issues or the body as JSON after a `: `.
83
+ *
84
+ * @param check - the failed check
85
+ * @param status - the response status
86
+ * @param route - what names the request, `METHOD path`
87
+ */
88
+ export const format_route_error_check = (check, status, route) => check.kind === 'undeclared_status'
89
+ ? `Undeclared error status (${status}): ${route}`
90
+ : `Error schema mismatch (${status}): ${route}`;
91
+ // --- route lookup ---
92
+ const escape_regexp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
93
+ /** A param segment as Hono reads one: `:name`, or `:name{re}` (`getPattern`). */
94
+ const PARAM_SEGMENT = /^:([^{}]+)(?:\{(.+)\})?$/;
95
+ /**
96
+ * Read a template segment as Hono reads a param: a whole segment `:name` or
97
+ * `:name{re}`, either with a trailing `?`. A segment that doesn't start with
98
+ * `:` is literal text, a `:` inside it (`files:upload`) included.
99
+ *
100
+ * @param segment - one segment of a template split by `split_route_template`
101
+ * @returns the param, or `null` for a segment that is not one
102
+ */
103
+ export const parse_route_param = (segment) => {
104
+ const optional = segment.startsWith(':') && segment.endsWith('?');
105
+ const param = PARAM_SEGMENT.exec(optional ? segment.slice(0, -1) : segment);
106
+ return param ? { name: param[1], pattern: param[2] ?? null, optional } : null;
107
+ };
108
+ /**
109
+ * A template's segments, split on `/` with each `{…}` group kept whole, as
110
+ * Hono's `splitRoutingPath` splits them; a leading `/` gives no empty first
111
+ * segment.
112
+ *
113
+ * @param template - a route's path template
114
+ */
115
+ export const split_route_template = (template) => {
116
+ const groups = [];
117
+ const marked = template.replace(/\{[^}]+\}/g, (group, index) => {
118
+ const mark = `@${index}`;
119
+ groups.push([mark, group]);
120
+ return mark;
121
+ });
122
+ const segments = marked.split('/');
123
+ if (segments[0] === '')
124
+ segments.shift();
125
+ for (let i = groups.length - 1; i >= 0; i--) {
126
+ const [mark, group] = groups[i];
127
+ for (let j = segments.length - 1; j >= 0; j--) {
128
+ if (segments[j].includes(mark)) {
129
+ segments[j] = segments[j].replace(mark, group);
130
+ break;
131
+ }
132
+ }
133
+ }
134
+ return segments;
135
+ };
136
+ /**
137
+ * The templates an optional param expands to, as Hono's
138
+ * `checkOptionalParameter` expands them: a template ending in `?` that holds a
139
+ * param is one template without each optional segment and one with it.
140
+ * `null` for a template with none.
141
+ */
142
+ const expand_optional_params = (template) => {
143
+ if (!template.endsWith('?') || !template.includes(':'))
144
+ return null;
145
+ const results = [];
146
+ let base = '';
147
+ for (const segment of template.split('/')) {
148
+ if (segment !== '' && !segment.includes(':')) {
149
+ base += `/${segment}`;
150
+ }
151
+ else if (segment.includes(':')) {
152
+ if (segment.endsWith('?')) {
153
+ results.push(results.length === 0 && base === '' ? '/' : base);
154
+ base += `/${segment.slice(0, -1)}`;
155
+ results.push(base);
156
+ }
157
+ else {
158
+ base += `/${segment}`;
159
+ }
160
+ }
161
+ }
162
+ return results.filter((v, i, a) => a.indexOf(v) === i);
163
+ };
164
+ /**
165
+ * The regexp source of a `{re}` param's pattern, grouped so an alternation
166
+ * stays inside it, as Hono's RegExpRouter reads it.
167
+ */
168
+ const to_route_param_source = (pattern) => `(?:${pattern})`;
169
+ /**
170
+ * Compile the `re` of a `:name{re}` param to the `RegExp` a value of the param
171
+ * must match, grouped and anchored at both ends, `^(?:re)$`, as
172
+ * `compile_route_path` reads it inside a template. That is Hono's RegExpRouter
173
+ * reading; its TrieRouter, which SmartRouter falls back to for some route
174
+ * sets, anchors `^re$` and leaves a top-level alternation ungrouped, so
175
+ * `^a|b$` takes `ab` where the grouped form refuses it. The grouped form is
176
+ * the stricter of the two: a value it accepts both routers accept. Test it
177
+ * against the value as Hono reads it (`decode_route_path`).
178
+ *
179
+ * @param pattern - the param's `re` (`RouteParam.pattern`)
180
+ */
181
+ export const compile_route_param_pattern = (pattern) => new RegExp(`^${to_route_param_source(pattern)}$`);
182
+ /**
183
+ * A request path, or a part of one, as Hono's router reads it: percent-decoded
184
+ * by `decodeURI` with `%25` kept, so an encoded `/`, `#`, `?` or `%` stays
185
+ * encoded, or the raw text when decoding fails.
186
+ *
187
+ * @param path - the request pathname, or a percent-encoded segment of one
188
+ */
189
+ export const decode_route_path = (path) => {
190
+ if (!path.includes('%'))
191
+ return path;
192
+ try {
193
+ return decodeURI(path.replaceAll('%25', '%2525'));
194
+ }
195
+ catch {
196
+ // a malformed escape: Hono matches the raw path
197
+ return path;
198
+ }
199
+ };
200
+ /** The regexp source of a template with no optional param. */
201
+ const to_route_path_source = (template) => {
202
+ const segments = split_route_template(template);
203
+ let source = '';
204
+ for (let i = 0; i < segments.length; i++) {
205
+ const segment = segments[i];
206
+ if (segment === '*') {
207
+ // a trailing `/*` is the prefix with anything after it, or nothing
208
+ source += i === segments.length - 1 ? '(?:/.*)?' : '/[^/]+';
209
+ continue;
210
+ }
211
+ const param = PARAM_SEGMENT.exec(segment);
212
+ if (param) {
213
+ source += param[2] ? `/${to_route_param_source(param[2])}` : '/[^/]+';
214
+ continue;
215
+ }
216
+ if (segment.includes('*')) {
217
+ // Hono reads a `*` inside a segment as a wildcard (`/a*` matches `/abc`);
218
+ // refuse it rather than guess its reach
219
+ throw new Error(`compile_route_path: ${JSON.stringify(template)} has a \`*\` inside a segment (${JSON.stringify(segment)}), which this lookup does not read`);
220
+ }
221
+ source += `/${escape_regexp(segment)}`;
222
+ }
223
+ return source;
224
+ };
225
+ /**
226
+ * Compile a Hono path template to a `RegExp` over request paths, reading it
227
+ * as Hono 4's routers do: the template is split on `/` with each `{…}` group
228
+ * kept whole, and each segment is a `*`, a param — a whole segment
229
+ * `:name` (one non-empty segment) or `:name{re}` (the pattern `re`, which may
230
+ * span segments) — or literal text, so a `:` inside a segment (`files:upload`,
231
+ * `:x-:y`'s second) is no param of its own; a literal segment holding a `*`
232
+ * (`/a*`), a wildcard in Hono, throws rather than be guessed at. A template
233
+ * ending in `?` expands
234
+ * its optional params as Hono's `checkOptionalParameter` does. A trailing
235
+ * `/*` matches the prefix with anything after it or nothing (so `/api/*`
236
+ * matches `/api`), a `*` elsewhere one non-empty segment, and `*` alone every
237
+ * path. Trailing slashes are strict, as Hono's are by default. Pinned against
238
+ * Hono itself by the route-lookup tests.
239
+ *
240
+ * @param template - the route's path template (`RouteSpec.path`)
241
+ * @returns an anchored `RegExp` over a request path
242
+ * @throws Error on a segment holding a `*` that is not the whole segment
243
+ * (`/a*`), a wildcard in Hono this lookup refuses to guess at
244
+ */
245
+ export const compile_route_path = (template) => {
246
+ if (template === '*')
247
+ return /^.*$/;
248
+ const expanded = expand_optional_params(template);
249
+ const source = expanded
250
+ ? `(?:${expanded.map((t) => (t === '/' ? '/' : to_route_path_source(t))).join('|')})`
251
+ : template === '/'
252
+ ? '/'
253
+ : to_route_path_source(template);
254
+ return new RegExp(`^${source}$`);
255
+ };
256
+ /**
257
+ * Compile routes for `find_mounted_route`, in mount order.
258
+ *
259
+ * @param routes - the routes, in the order they were mounted
260
+ * @param to_value - what to keep beside each route
261
+ */
262
+ export const to_mounted_routes = (routes, to_value) => routes.map((route) => ({
263
+ method: route.method,
264
+ pattern: compile_route_path(route.path),
265
+ value: to_value(route)
266
+ }));
267
+ /**
268
+ * The first route in mount order that a request matches, as Hono picks the
269
+ * handler — a `HEAD` read as a `GET`, which Hono answers it with.
270
+ *
271
+ * @param routes - the routes, compiled by `to_mounted_routes`
272
+ * @param method - the request method
273
+ * @param path - the request path
274
+ * @returns the matched route's value, or `undefined` when no route matches
275
+ */
276
+ export const find_mounted_route = (routes, method, path) => {
277
+ const read_method = method === 'HEAD' ? 'GET' : method;
278
+ for (const route of routes) {
279
+ if (route.method === read_method && route.pattern.test(path))
280
+ return route.value;
281
+ }
282
+ return undefined;
283
+ };