@fuzdev/fuz_app 0.163.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.
- package/README.md +2 -1
- package/dist/actions/action_event_data.d.ts +1 -1
- package/dist/actions/action_rpc.d.ts.map +1 -1
- package/dist/actions/action_rpc.js +4 -0
- package/dist/actions/action_spec.d.ts +3 -3
- package/dist/actions/action_spec.d.ts.map +1 -1
- package/dist/auth/CLAUDE.md +1 -1
- package/dist/auth/account_route_schema.d.ts +3 -27
- package/dist/auth/account_route_schema.d.ts.map +1 -1
- package/dist/auth/account_route_schema.js +6 -20
- package/dist/auth/admin_action_specs.d.ts +56 -56
- package/dist/auth/audit_log_route_schema.d.ts +1 -1
- package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
- package/dist/auth/audit_log_route_schema.js +4 -5
- package/dist/auth/audit_log_schema.d.ts +18 -18
- package/dist/auth/bootstrap_route_schema.d.ts +1 -30
- package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
- package/dist/auth/bootstrap_route_schema.js +3 -8
- package/dist/auth/daemon_token.d.ts +4 -4
- package/dist/auth/daemon_token.d.ts.map +1 -1
- package/dist/auth/daemon_token.js +3 -3
- package/dist/auth/daemon_token_middleware.d.ts +1 -1
- package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
- package/dist/auth/daemon_token_middleware.js +5 -5
- package/dist/auth/middleware.js +4 -4
- package/dist/auth/role_grant_offer_action_specs.d.ts +2 -2
- package/dist/auth/signup_route_schema.d.ts.map +1 -1
- package/dist/auth/signup_route_schema.js +3 -7
- package/dist/db/CLAUDE.md +2 -1
- package/dist/db/file_fact_url.d.ts +11 -6
- package/dist/db/file_fact_url.d.ts.map +1 -1
- package/dist/db/file_fact_url.js +5 -5
- package/dist/db/schema_ready.d.ts +0 -5
- package/dist/db/schema_ready.d.ts.map +1 -1
- package/dist/db/schema_ready.js +0 -5
- package/dist/http/CLAUDE.md +104 -71
- package/dist/http/auth_shape.d.ts +48 -29
- package/dist/http/auth_shape.d.ts.map +1 -1
- package/dist/http/auth_shape.js +12 -85
- package/dist/http/body_limit.d.ts +12 -0
- package/dist/http/body_limit.d.ts.map +1 -1
- package/dist/http/body_limit.js +15 -0
- package/dist/http/body_limit_schema.d.ts +20 -0
- package/dist/http/body_limit_schema.d.ts.map +1 -0
- package/dist/http/body_limit_schema.js +20 -0
- package/dist/http/common_routes.d.ts +1 -1
- package/dist/http/common_routes.d.ts.map +1 -1
- package/dist/http/common_routes.js +11 -14
- package/dist/http/db_routes.d.ts.map +1 -1
- package/dist/http/db_routes.js +59 -69
- package/dist/http/dev_route_error_check.d.ts +70 -0
- package/dist/http/dev_route_error_check.d.ts.map +1 -0
- package/dist/http/dev_route_error_check.js +119 -0
- package/dist/http/error_schemas.d.ts +365 -189
- package/dist/http/error_schemas.d.ts.map +1 -1
- package/dist/http/error_schemas.js +256 -149
- package/dist/http/health_schema.d.ts +10 -6
- package/dist/http/health_schema.d.ts.map +1 -1
- package/dist/http/health_schema.js +3 -3
- package/dist/http/middleware_spec.d.ts +8 -3
- package/dist/http/middleware_spec.d.ts.map +1 -1
- package/dist/http/origin.d.ts +12 -43
- package/dist/http/origin.d.ts.map +1 -1
- package/dist/http/origin.js +10 -6
- package/dist/http/route_error_check.d.ts +158 -0
- package/dist/http/route_error_check.d.ts.map +1 -0
- package/dist/http/route_error_check.js +247 -0
- package/dist/http/route_errors.d.ts +80 -0
- package/dist/http/route_errors.d.ts.map +1 -0
- package/dist/http/route_errors.js +83 -0
- package/dist/http/route_spec.d.ts +33 -17
- package/dist/http/route_spec.d.ts.map +1 -1
- package/dist/http/route_spec.js +48 -62
- package/dist/http/schema_helpers.d.ts +0 -33
- package/dist/http/schema_helpers.d.ts.map +1 -1
- package/dist/http/schema_helpers.js +0 -45
- package/dist/http/surface.d.ts +19 -12
- package/dist/http/surface.d.ts.map +1 -1
- package/dist/http/surface.js +25 -38
- package/dist/http/surface_query.d.ts +11 -2
- package/dist/http/surface_query.d.ts.map +1 -1
- package/dist/http/surface_query.js +7 -1
- package/dist/primitive_schemas_fuz.d.ts +17 -3
- package/dist/primitive_schemas_fuz.d.ts.map +1 -1
- package/dist/primitive_schemas_fuz.js +13 -3
- package/dist/rate_limiter.d.ts +13 -0
- package/dist/rate_limiter.d.ts.map +1 -1
- package/dist/rate_limiter.js +20 -2
- package/dist/schema_issues.d.ts +29 -11
- package/dist/schema_issues.d.ts.map +1 -1
- package/dist/schema_issues.js +11 -10
- package/dist/schema_meta.d.ts +8 -1
- package/dist/schema_meta.d.ts.map +1 -1
- package/dist/schema_meta.js +2 -1
- package/dist/server/app_server.d.ts +3 -1
- package/dist/server/app_server.d.ts.map +1 -1
- package/dist/server/app_server.js +24 -10
- package/dist/server/serve_fact_route.d.ts.map +1 -1
- package/dist/server/serve_fact_route.js +8 -31
- package/dist/server/surface_route.d.ts.map +1 -1
- package/dist/server/surface_route.js +5 -6
- package/dist/server/x_accel.d.ts +12 -7
- package/dist/server/x_accel.d.ts.map +1 -1
- package/dist/server/x_accel.js +7 -9
- package/dist/testing/CLAUDE.md +33 -39
- package/dist/testing/admin_integration.js +1 -1
- package/dist/testing/adversarial_404.d.ts +4 -18
- package/dist/testing/adversarial_404.d.ts.map +1 -1
- package/dist/testing/adversarial_404.js +19 -51
- package/dist/testing/adversarial_headers.d.ts +3 -4
- package/dist/testing/adversarial_headers.d.ts.map +1 -1
- package/dist/testing/adversarial_headers.js +8 -6
- package/dist/testing/adversarial_input.d.ts.map +1 -1
- package/dist/testing/adversarial_input.js +18 -17
- package/dist/testing/assertions.d.ts +33 -11
- package/dist/testing/assertions.d.ts.map +1 -1
- package/dist/testing/assertions.js +52 -12
- package/dist/testing/attack_surface.d.ts +5 -35
- package/dist/testing/attack_surface.d.ts.map +1 -1
- package/dist/testing/attack_surface.js +29 -91
- package/dist/testing/auth_apps.d.ts.map +1 -1
- package/dist/testing/auth_apps.js +2 -1
- package/dist/testing/cross_backend/action_manifest.d.ts +126 -56
- package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
- package/dist/testing/cross_backend/action_manifest.js +11 -11
- package/dist/testing/cross_backend/action_manifest_parity.d.ts +1 -1
- package/dist/testing/cross_backend/action_manifest_parity.d.ts.map +1 -1
- package/dist/testing/cross_backend/cell_crud.d.ts.map +1 -1
- package/dist/testing/cross_backend/cell_crud.js +2 -1
- package/dist/testing/cross_backend/conformance_table.js +1 -1
- package/dist/testing/cross_backend/fact_serving.js +3 -3
- package/dist/testing/cross_backend/origin_conformance.d.ts +278 -102
- package/dist/testing/cross_backend/origin_conformance.d.ts.map +1 -1
- package/dist/testing/cross_backend/origin_conformance.js +21 -21
- package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_consumer_surface.js +7 -1
- package/dist/testing/cross_backend/setup.d.ts +13 -0
- package/dist/testing/cross_backend/setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/setup.js +24 -7
- package/dist/testing/cross_backend/testing_origin_check.d.ts +147 -71
- package/dist/testing/cross_backend/testing_origin_check.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_origin_check.js +16 -13
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +70 -27
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/data_exposure.d.ts.map +1 -1
- package/dist/testing/data_exposure.js +5 -4
- package/dist/testing/error_coverage.d.ts +24 -34
- package/dist/testing/error_coverage.d.ts.map +1 -1
- package/dist/testing/error_coverage.js +46 -89
- package/dist/testing/integration.d.ts.map +1 -1
- package/dist/testing/integration.js +5 -13
- package/dist/testing/integration_helpers.d.ts +37 -9
- package/dist/testing/integration_helpers.d.ts.map +1 -1
- package/dist/testing/integration_helpers.js +86 -40
- package/dist/testing/middleware.d.ts +3 -5
- package/dist/testing/middleware.d.ts.map +1 -1
- package/dist/testing/middleware.js +5 -4
- package/dist/testing/rate_limiting.d.ts +1 -1
- package/dist/testing/rate_limiting.d.ts.map +1 -1
- package/dist/testing/rate_limiting.js +5 -4
- package/dist/testing/round_trip.d.ts.map +1 -1
- package/dist/testing/round_trip.js +5 -3
- package/dist/testing/schema_generators.d.ts +10 -3
- package/dist/testing/schema_generators.d.ts.map +1 -1
- package/dist/testing/schema_generators.js +23 -11
- package/dist/testing/stubs.d.ts +10 -1
- package/dist/testing/stubs.d.ts.map +1 -1
- package/dist/testing/stubs.js +15 -5
- package/dist/testing/surface_invariants.d.ts +36 -115
- package/dist/testing/surface_invariants.d.ts.map +1 -1
- package/dist/testing/surface_invariants.js +106 -269
- package/dist/ui/CLAUDE.md +4 -2
- package/dist/ui/auth_state.svelte.d.ts.map +1 -1
- package/dist/ui/auth_state.svelte.js +2 -8
- package/dist/ui/backend_probe.svelte.js +3 -3
- package/dist/ui/table_state.svelte.d.ts.map +1 -1
- package/dist/ui/table_state.svelte.js +1 -8
- package/dist/ui/ui_fetch.d.ts +12 -2
- package/dist/ui/ui_fetch.d.ts.map +1 -1
- package/dist/ui/ui_fetch.js +22 -4
- package/package.json +1 -1
package/dist/http/origin.d.ts
CHANGED
|
@@ -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 {
|
|
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
|
|
44
|
-
*
|
|
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:
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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 @@
|
|
|
1
|
-
{"version":3,"file":"origin.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/origin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,
|
|
1
|
+
{"version":3,"file":"origin.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/origin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,EAAU,KAAK,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAGlE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG/D;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,kBAAkB,GAAI,GAAG,OAAO,KAAG,OAC8B,CAAC;AAE/E;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,88BAiBjC,CAAC;AACH,MAAM,MAAM,sBAAsB,GAAG,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAE1E;;;GAGG;AACH,eAAO,MAAM,uBAAuB,0/BAAwD,CAAC;AAC7F,MAAM,MAAM,uBAAuB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE5E,wDAAwD;AACxD,eAAO,MAAM,8BAA8B,EAAE,QAAQ,CAAC,MAAM,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAwB5F,CAAC;AAEF,0DAA0D;AAC1D,MAAM,WAAW,mBAAmB;IACnC,QAAQ,CAAC,IAAI,EAAE,uBAAuB,CAAC;IACvC,qEAAqE;IACrE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,oEAAoE;IACpE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACxB;AAKD;;;;;;;;GAQG;AACH,eAAO,MAAM,4BAA4B,GAAI,OAAO,mBAAmB,KAAG,MAOzE,CAAC;AAwBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,qBAAqB,GACjC,KAAK,MAAM,KACT,MAAM,CACR;IAAE,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAAC,QAAQ,EAAE,KAAK,CAAC,aAAa,CAAC,CAAA;CAAE,EAC1D;IAAE,MAAM,EAAE,KAAK,CAAC,mBAAmB,CAAC,CAAA;CAAE,CAqBtC,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC7B,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"}
|
package/dist/http/origin.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* @module
|
|
20
20
|
*/
|
|
21
21
|
import { escape_regexp } from '@fuzdev/fuz_util/regexp.ts';
|
|
22
|
-
import {
|
|
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
|
|
44
|
-
*
|
|
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 =
|
|
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
|
-
/**
|
|
65
|
-
|
|
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,158 @@
|
|
|
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 a Hono path template to a `RegExp` over request paths, reading it
|
|
113
|
+
* as Hono 4's routers do: the template is split on `/` with each `{…}` group
|
|
114
|
+
* kept whole, and each segment is a `*`, a param — a whole segment
|
|
115
|
+
* `:name` (one non-empty segment) or `:name{re}` (the pattern `re`, which may
|
|
116
|
+
* span segments) — or literal text, so a `:` inside a segment (`files:upload`,
|
|
117
|
+
* `:x-:y`'s second) is no param of its own; a literal segment holding a `*`
|
|
118
|
+
* (`/a*`), a wildcard in Hono, throws rather than be guessed at. A template
|
|
119
|
+
* ending in `?` expands
|
|
120
|
+
* its optional params as Hono's `checkOptionalParameter` does. A trailing
|
|
121
|
+
* `/*` matches the prefix with anything after it or nothing (so `/api/*`
|
|
122
|
+
* matches `/api`), a `*` elsewhere one non-empty segment, and `*` alone every
|
|
123
|
+
* path. Trailing slashes are strict, as Hono's are by default. Pinned against
|
|
124
|
+
* Hono itself by the route-lookup tests.
|
|
125
|
+
*
|
|
126
|
+
* @param template - the route's path template (`RouteSpec.path`)
|
|
127
|
+
* @returns an anchored `RegExp` over a request path
|
|
128
|
+
* @throws Error on a segment holding a `*` that is not the whole segment
|
|
129
|
+
* (`/a*`), a wildcard in Hono this lookup refuses to guess at
|
|
130
|
+
*/
|
|
131
|
+
export declare const compile_route_path: (template: string) => RegExp;
|
|
132
|
+
/** A route a request can be matched to: its method, its compiled path, and what the caller keeps beside it. */
|
|
133
|
+
export interface MountedRoute<T> {
|
|
134
|
+
readonly method: string;
|
|
135
|
+
readonly pattern: RegExp;
|
|
136
|
+
readonly value: T;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Compile routes for `find_mounted_route`, in mount order.
|
|
140
|
+
*
|
|
141
|
+
* @param routes - the routes, in the order they were mounted
|
|
142
|
+
* @param to_value - what to keep beside each route
|
|
143
|
+
*/
|
|
144
|
+
export declare const to_mounted_routes: <TRoute extends {
|
|
145
|
+
readonly method: string;
|
|
146
|
+
readonly path: string;
|
|
147
|
+
}, T>(routes: ReadonlyArray<TRoute>, to_value: (route: TRoute) => T) => Array<MountedRoute<T>>;
|
|
148
|
+
/**
|
|
149
|
+
* The first route in mount order that a request matches, as Hono picks the
|
|
150
|
+
* handler — a `HEAD` read as a `GET`, which Hono answers it with.
|
|
151
|
+
*
|
|
152
|
+
* @param routes - the routes, compiled by `to_mounted_routes`
|
|
153
|
+
* @param method - the request method
|
|
154
|
+
* @param path - the request path
|
|
155
|
+
* @returns the matched route's value, or `undefined` when no route matches
|
|
156
|
+
*/
|
|
157
|
+
export declare const find_mounted_route: <T>(routes: ReadonlyArray<MountedRoute<T>>, method: string, path: string) => T | undefined;
|
|
158
|
+
//# 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,GAC7B,QAAQ,aAAa,EACrB,QAAQ,MAAM,EACd,MAAM,OAAO,EACb,SAAS,OAAO,KACd,eAWF,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,wBAAwB,GACpC,OAAO,OAAO,CAAC,eAAe,EAAE;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,CAAC,EAC7C,QAAQ,MAAM,EACd,OAAO,MAAM,KACX,MAG8C,CAAC;AASlD,4EAA4E;AAC5E,MAAM,WAAW,UAAU;IAC1B,sEAAsE;IACtE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,6DAA6D;IAC7D,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,0DAA0D;IAC1D,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC3B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,GAAI,SAAS,MAAM,KAAG,UAAU,GAAG,IAIhE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,UAAU,MAAM,KAAG,KAAK,CAAC,MAAM,CAmBnE,CAAC;AAwDF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,kBAAkB,GAAI,UAAU,MAAM,KAAG,MASrD,CAAC;AAEF,+GAA+G;AAC/G,MAAM,WAAW,YAAY,CAAC,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CAClB;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAC7B,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,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,247 @@
|
|
|
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
|
+
/** The regexp source of a template with no optional param. */
|
|
165
|
+
const to_route_path_source = (template) => {
|
|
166
|
+
const segments = split_route_template(template);
|
|
167
|
+
let source = '';
|
|
168
|
+
for (let i = 0; i < segments.length; i++) {
|
|
169
|
+
const segment = segments[i];
|
|
170
|
+
if (segment === '*') {
|
|
171
|
+
// a trailing `/*` is the prefix with anything after it, or nothing
|
|
172
|
+
source += i === segments.length - 1 ? '(?:/.*)?' : '/[^/]+';
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const param = PARAM_SEGMENT.exec(segment);
|
|
176
|
+
if (param) {
|
|
177
|
+
source += param[2] ? `/(?:${param[2]})` : '/[^/]+';
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (segment.includes('*')) {
|
|
181
|
+
// Hono reads a `*` inside a segment as a wildcard (`/a*` matches `/abc`);
|
|
182
|
+
// refuse it rather than guess its reach
|
|
183
|
+
throw new Error(`compile_route_path: ${JSON.stringify(template)} has a \`*\` inside a segment (${JSON.stringify(segment)}), which this lookup does not read`);
|
|
184
|
+
}
|
|
185
|
+
source += `/${escape_regexp(segment)}`;
|
|
186
|
+
}
|
|
187
|
+
return source;
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* Compile a Hono path template to a `RegExp` over request paths, reading it
|
|
191
|
+
* as Hono 4's routers do: the template is split on `/` with each `{…}` group
|
|
192
|
+
* kept whole, and each segment is a `*`, a param — a whole segment
|
|
193
|
+
* `:name` (one non-empty segment) or `:name{re}` (the pattern `re`, which may
|
|
194
|
+
* span segments) — or literal text, so a `:` inside a segment (`files:upload`,
|
|
195
|
+
* `:x-:y`'s second) is no param of its own; a literal segment holding a `*`
|
|
196
|
+
* (`/a*`), a wildcard in Hono, throws rather than be guessed at. A template
|
|
197
|
+
* ending in `?` expands
|
|
198
|
+
* its optional params as Hono's `checkOptionalParameter` does. A trailing
|
|
199
|
+
* `/*` matches the prefix with anything after it or nothing (so `/api/*`
|
|
200
|
+
* matches `/api`), a `*` elsewhere one non-empty segment, and `*` alone every
|
|
201
|
+
* path. Trailing slashes are strict, as Hono's are by default. Pinned against
|
|
202
|
+
* Hono itself by the route-lookup tests.
|
|
203
|
+
*
|
|
204
|
+
* @param template - the route's path template (`RouteSpec.path`)
|
|
205
|
+
* @returns an anchored `RegExp` over a request path
|
|
206
|
+
* @throws Error on a segment holding a `*` that is not the whole segment
|
|
207
|
+
* (`/a*`), a wildcard in Hono this lookup refuses to guess at
|
|
208
|
+
*/
|
|
209
|
+
export const compile_route_path = (template) => {
|
|
210
|
+
if (template === '*')
|
|
211
|
+
return /^.*$/;
|
|
212
|
+
const expanded = expand_optional_params(template);
|
|
213
|
+
const source = expanded
|
|
214
|
+
? `(?:${expanded.map((t) => (t === '/' ? '/' : to_route_path_source(t))).join('|')})`
|
|
215
|
+
: template === '/'
|
|
216
|
+
? '/'
|
|
217
|
+
: to_route_path_source(template);
|
|
218
|
+
return new RegExp(`^${source}$`);
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Compile routes for `find_mounted_route`, in mount order.
|
|
222
|
+
*
|
|
223
|
+
* @param routes - the routes, in the order they were mounted
|
|
224
|
+
* @param to_value - what to keep beside each route
|
|
225
|
+
*/
|
|
226
|
+
export const to_mounted_routes = (routes, to_value) => routes.map((route) => ({
|
|
227
|
+
method: route.method,
|
|
228
|
+
pattern: compile_route_path(route.path),
|
|
229
|
+
value: to_value(route)
|
|
230
|
+
}));
|
|
231
|
+
/**
|
|
232
|
+
* The first route in mount order that a request matches, as Hono picks the
|
|
233
|
+
* handler — a `HEAD` read as a `GET`, which Hono answers it with.
|
|
234
|
+
*
|
|
235
|
+
* @param routes - the routes, compiled by `to_mounted_routes`
|
|
236
|
+
* @param method - the request method
|
|
237
|
+
* @param path - the request path
|
|
238
|
+
* @returns the matched route's value, or `undefined` when no route matches
|
|
239
|
+
*/
|
|
240
|
+
export const find_mounted_route = (routes, method, path) => {
|
|
241
|
+
const read_method = method === 'HEAD' ? 'GET' : method;
|
|
242
|
+
for (const route of routes) {
|
|
243
|
+
if (route.method === read_method && route.pattern.test(path))
|
|
244
|
+
return route.value;
|
|
245
|
+
}
|
|
246
|
+
return undefined;
|
|
247
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A route's error map: every error definition it can answer with — derived
|
|
3
|
+
* from its slots (`derive_route_errors`), declared by each middleware layer
|
|
4
|
+
* that runs on it, and its own `errors` — and the status map they group into.
|
|
5
|
+
*
|
|
6
|
+
* Built once per app (`resolve_app_route_error_maps`, carried as
|
|
7
|
+
* `AppSurfaceSpec.route_error_maps`) and read by every reader of a route's
|
|
8
|
+
* declared errors: the surface, the DEV error check, the test helpers, error
|
|
9
|
+
* coverage, and the attack-surface suite. The maps are keyed `METHOD path`
|
|
10
|
+
* (`format_route_key`).
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import type { RouteAuth } from './auth_shape.js';
|
|
15
|
+
import type { RouteMethod } from './route_spec.js';
|
|
16
|
+
import { type ErrorSchemas, type RestErrorDef } from './error_schemas.js';
|
|
17
|
+
import type { MiddlewareSpec } from './middleware_spec.js';
|
|
18
|
+
import type { RateLimitKey } from '../rate_limiter.js';
|
|
19
|
+
import type { ValidationSchema } from '../schema_issues.js';
|
|
20
|
+
/**
|
|
21
|
+
* A route's error map: every error definition the route can answer with, and
|
|
22
|
+
* the status map they group into. Built by `resolve_route_error_map`, and read by
|
|
23
|
+
* every reader of a route's declared errors — the surface, the DEV error
|
|
24
|
+
* check, the test helpers, error coverage, and the attack-surface suite.
|
|
25
|
+
*/
|
|
26
|
+
export interface RouteErrorMap {
|
|
27
|
+
/**
|
|
28
|
+
* The definitions, in order — derived, then each applying middleware's, then
|
|
29
|
+
* the route's own — with each repeat left out (`dedupe_rest_errors`). Two
|
|
30
|
+
* shapes of one code at one status are both kept.
|
|
31
|
+
*/
|
|
32
|
+
readonly defs: ReadonlyArray<RestErrorDef>;
|
|
33
|
+
/** The definitions grouped by status (`to_error_schemas`): one schema, or a `union` of them. */
|
|
34
|
+
readonly schemas: ErrorSchemas;
|
|
35
|
+
}
|
|
36
|
+
/** What `resolve_route_error_map` reads of a route spec. */
|
|
37
|
+
export interface RouteErrorMapSpec {
|
|
38
|
+
readonly method: RouteMethod;
|
|
39
|
+
readonly path: string;
|
|
40
|
+
readonly auth: RouteAuth;
|
|
41
|
+
readonly input: ValidationSchema;
|
|
42
|
+
readonly params?: ValidationSchema;
|
|
43
|
+
readonly query?: ValidationSchema;
|
|
44
|
+
readonly rate_limit?: RateLimitKey;
|
|
45
|
+
readonly errors?: ReadonlyArray<RestErrorDef>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Resolve a route's error map: the union of the definitions derived from it
|
|
49
|
+
* (`derive_route_errors`, narrowed to the slots it has), those of each
|
|
50
|
+
* middleware layer that runs on it (`middleware_spec_applies`), in list order,
|
|
51
|
+
* and its own `errors`. Nothing replaces anything: a definition at a status
|
|
52
|
+
* another layer declares is one more branch there, and a repeat counts once.
|
|
53
|
+
*
|
|
54
|
+
* @param spec - the route spec
|
|
55
|
+
* @param middleware_specs - the app's middleware layers, of which those that apply to the route contribute
|
|
56
|
+
* @returns the route's error map
|
|
57
|
+
*/
|
|
58
|
+
export declare const resolve_route_error_map: (spec: RouteErrorMapSpec, middleware_specs?: ReadonlyArray<Pick<MiddlewareSpec, "path" | "carve_outs" | "errors">> | null) => RouteErrorMap;
|
|
59
|
+
/**
|
|
60
|
+
* Resolve every route's error map (`resolve_route_error_map`) against one
|
|
61
|
+
* middleware list, keyed by `format_route_key` (`METHOD path`).
|
|
62
|
+
*
|
|
63
|
+
* @param route_specs - the app's routes
|
|
64
|
+
* @param middleware_specs - the app's middleware layers
|
|
65
|
+
* @returns each route's error map
|
|
66
|
+
*/
|
|
67
|
+
export declare const resolve_app_route_error_maps: (route_specs: ReadonlyArray<RouteErrorMapSpec>, middleware_specs?: ReadonlyArray<Pick<MiddlewareSpec, "path" | "carve_outs" | "errors">> | null) => Map<string, RouteErrorMap>;
|
|
68
|
+
/**
|
|
69
|
+
* A route's error map from a map built by `resolve_app_route_error_maps`, or,
|
|
70
|
+
* with no map, resolved from the route alone — without middleware.
|
|
71
|
+
*
|
|
72
|
+
* @param route_error_maps - the app's route error maps, if the caller has them
|
|
73
|
+
* @param spec - the route spec
|
|
74
|
+
* @returns the route's error map
|
|
75
|
+
* @throws Error naming the route's key when a map is given with no entry for
|
|
76
|
+
* the route — a map built over other routes, which would silently drop the
|
|
77
|
+
* middleware's definitions
|
|
78
|
+
*/
|
|
79
|
+
export declare const get_route_error_map: (route_error_maps: ReadonlyMap<string, RouteErrorMap> | null | undefined, spec: RouteErrorMapSpec) => RouteErrorMap;
|
|
80
|
+
//# sourceMappingURL=route_errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route_errors.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/route_errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAIN,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAG3D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAE5D;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC7B;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,YAAY,CAAC,CAAC;IAC3C,gGAAgG;IAChG,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;CAC/B;AAED,4DAA4D;AAC5D,MAAM,WAAW,iBAAiB;IACjC,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAC;IACjC,QAAQ,CAAC,MAAM,CAAC,EAAE,gBAAgB,CAAC;IACnC,QAAQ,CAAC,KAAK,CAAC,EAAE,gBAAgB,CAAC;IAClC,QAAQ,CAAC,UAAU,CAAC,EAAE,YAAY,CAAC;IACnC,QAAQ,CAAC,MAAM,CAAC,EAAE,aAAa,CAAC,YAAY,CAAC,CAAC;CAC9C;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,GACnC,MAAM,iBAAiB,EACvB,mBAAmB,aAAa,CAAC,IAAI,CAAC,cAAc,EAAE,MAAM,GAAG,YAAY,GAAG,QAAQ,CAAC,CAAC,GAAG,IAAI,KAC7F,aAiBF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,4BAA4B,GACxC,aAAa,aAAa,CAAC,iBAAiB,CAAC,EAC7C,mBAAmB,aAAa,CAAC,IAAI,CAAC,cAAc,EAAE,MAAM,GAAG,YAAY,GAAG,QAAQ,CAAC,CAAC,GAAG,IAAI,KAC7F,GAAG,CAAC,MAAM,EAAE,aAAa,CAM3B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,GAC/B,kBAAkB,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,GAAG,IAAI,GAAG,SAAS,EACvE,MAAM,iBAAiB,KACrB,aAQF,CAAC"}
|