@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.
- 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 +39 -36
- package/dist/auth/account_action_specs.d.ts +441 -115
- package/dist/auth/account_action_specs.d.ts.map +1 -1
- package/dist/auth/account_action_specs.js +47 -46
- package/dist/auth/account_route_schema.d.ts +98 -62
- package/dist/auth/account_route_schema.d.ts.map +1 -1
- package/dist/auth/account_route_schema.js +45 -45
- package/dist/auth/account_routes.js +1 -1
- package/dist/auth/account_schema.d.ts +54 -27
- package/dist/auth/account_schema.d.ts.map +1 -1
- package/dist/auth/account_schema.js +16 -18
- package/dist/auth/admin_action_specs.d.ts +76 -76
- package/dist/auth/admin_action_specs.js +2 -4
- package/dist/auth/admin_actions.d.ts +4 -4
- package/dist/auth/admin_actions.d.ts.map +1 -1
- package/dist/auth/admin_actions.js +3 -3
- package/dist/auth/all_cell_actions.d.ts +3 -3
- package/dist/auth/all_cell_actions.d.ts.map +1 -1
- package/dist/auth/all_cell_actions.js +1 -1
- package/dist/auth/api_token.d.ts +4 -4
- package/dist/auth/api_token.d.ts.map +1 -1
- package/dist/auth/api_token.js +3 -3
- 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 +46 -66
- package/dist/auth/audit_log_schema.d.ts.map +1 -1
- package/dist/auth/audit_log_schema.js +34 -30
- package/dist/auth/bootstrap_route_schema.d.ts +146 -52
- package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
- package/dist/auth/bootstrap_route_schema.js +33 -19
- package/dist/auth/cell_grant_action_specs.d.ts +1 -1
- package/dist/auth/cell_grant_action_specs.js +1 -1
- package/dist/auth/cell_grant_actions.d.ts +4 -4
- package/dist/auth/cell_grant_actions.d.ts.map +1 -1
- package/dist/auth/cell_grant_actions.js +1 -1
- package/dist/auth/credential_type_schema.d.ts +16 -29
- package/dist/auth/credential_type_schema.d.ts.map +1 -1
- package/dist/auth/credential_type_schema.js +14 -18
- 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/grant_path_schema.d.ts +15 -29
- package/dist/auth/grant_path_schema.d.ts.map +1 -1
- package/dist/auth/grant_path_schema.js +13 -17
- package/dist/auth/middleware.js +4 -4
- package/dist/auth/migrations.js +1 -1
- package/dist/auth/name_schema.d.ts +7 -6
- package/dist/auth/name_schema.d.ts.map +1 -1
- package/dist/auth/name_schema.js +8 -9
- package/dist/auth/password.d.ts +15 -7
- package/dist/auth/password.d.ts.map +1 -1
- package/dist/auth/password.js +16 -9
- package/dist/auth/role_grant_offer_action_specs.d.ts +2 -2
- package/dist/auth/role_grant_offer_action_specs.js +2 -2
- package/dist/auth/role_grant_offer_actions.d.ts +4 -4
- package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_actions.js +3 -3
- package/dist/auth/role_grant_offer_notifications.js +2 -2
- package/dist/auth/role_grant_offer_schema.d.ts +2 -2
- package/dist/auth/role_grant_offer_schema.js +2 -2
- package/dist/auth/role_schema.d.ts +47 -55
- package/dist/auth/role_schema.d.ts.map +1 -1
- package/dist/auth/role_schema.js +29 -35
- package/dist/auth/scope_kind_schema.d.ts +22 -26
- package/dist/auth/scope_kind_schema.d.ts.map +1 -1
- package/dist/auth/scope_kind_schema.js +19 -19
- package/dist/auth/self_service_role_action_specs.js +1 -1
- package/dist/auth/self_service_role_actions.d.ts +6 -6
- package/dist/auth/self_service_role_actions.d.ts.map +1 -1
- package/dist/auth/self_service_role_actions.js +7 -6
- package/dist/auth/signup_route_schema.d.ts +71 -21
- package/dist/auth/signup_route_schema.d.ts.map +1 -1
- package/dist/auth/signup_route_schema.js +17 -19
- package/dist/auth/standard_rpc_actions.d.ts +1 -1
- package/dist/auth/standard_rpc_actions.js +1 -1
- package/dist/auth/token_lifetime.d.ts +33 -9
- package/dist/auth/token_lifetime.d.ts.map +1 -1
- package/dist/auth/token_lifetime.js +8 -7
- package/dist/auth/token_scope.d.ts +99 -25
- package/dist/auth/token_scope.d.ts.map +1 -1
- package/dist/auth/token_scope.js +57 -35
- 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.d.ts +2 -19
- package/dist/primitive_schemas.d.ts.map +1 -1
- package/dist/primitive_schemas.js +2 -20
- package/dist/primitive_schemas_fuz.d.ts +39 -5
- package/dist/primitive_schemas_fuz.d.ts.map +1 -1
- package/dist/primitive_schemas_fuz.js +36 -7
- 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 +55 -44
- package/dist/testing/admin_integration.d.ts +3 -3
- package/dist/testing/admin_integration.d.ts.map +1 -1
- package/dist/testing/admin_integration.js +2 -2
- 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/default_spine_surface.d.ts +2 -2
- package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/default_spine_surface.js +2 -2
- package/dist/testing/cross_backend/fact_serving.js +3 -3
- package/dist/testing/cross_backend/identity_parity.d.ts.map +1 -1
- package/dist/testing/cross_backend/identity_parity.js +39 -6
- 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 +3 -3
- 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 +14 -1
- package/dist/testing/cross_backend/setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/setup.js +68 -48
- package/dist/testing/cross_backend/spine_suites.d.ts +3 -3
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/standard.d.ts +4 -4
- package/dist/testing/cross_backend/standard.d.ts.map +1 -1
- 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/cross_backend/token_lifetime.d.ts.map +1 -1
- package/dist/testing/cross_backend/token_lifetime.js +34 -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 +6 -14
- 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/testing/token_scope_fixtures.d.ts +24 -0
- package/dist/testing/token_scope_fixtures.d.ts.map +1 -0
- package/dist/testing/token_scope_fixtures.js +49 -0
- package/dist/testing/transports/bootstrap.d.ts +1 -1
- package/dist/testing/transports/bootstrap.d.ts.map +1 -1
- package/dist/testing/transports/bootstrap.js +13 -12
- package/dist/ui/BootstrapForm.svelte +4 -2
- package/dist/ui/BootstrapForm.svelte.d.ts.map +1 -1
- package/dist/ui/CLAUDE.md +4 -2
- package/dist/ui/SignupForm.svelte +4 -2
- package/dist/ui/SignupForm.svelte.d.ts.map +1 -1
- 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/auth/audit_log_schema_fuz.d.ts +0 -53
- package/dist/auth/audit_log_schema_fuz.d.ts.map +0 -1
- package/dist/auth/audit_log_schema_fuz.js +0 -50
- package/dist/auth/role_schema_fuz.d.ts +0 -30
- package/dist/auth/role_schema_fuz.d.ts.map +0 -1
- package/dist/auth/role_schema_fuz.js +0 -30
- package/dist/auth/scope_kind_schema_fuz.d.ts +0 -31
- package/dist/auth/scope_kind_schema_fuz.d.ts.map +0 -1
- 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
|
+
};
|