@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
|
@@ -0,0 +1,83 @@
|
|
|
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 { dedupe_rest_errors, derive_route_errors, to_error_schemas } from './error_schemas.js';
|
|
15
|
+
import { is_null_schema, middleware_spec_applies } from './schema_helpers.js';
|
|
16
|
+
import { format_route_key } from './surface_query.js';
|
|
17
|
+
/**
|
|
18
|
+
* Resolve a route's error map: the union of the definitions derived from it
|
|
19
|
+
* (`derive_route_errors`, narrowed to the slots it has), those of each
|
|
20
|
+
* middleware layer that runs on it (`middleware_spec_applies`), in list order,
|
|
21
|
+
* and its own `errors`. Nothing replaces anything: a definition at a status
|
|
22
|
+
* another layer declares is one more branch there, and a repeat counts once.
|
|
23
|
+
*
|
|
24
|
+
* @param spec - the route spec
|
|
25
|
+
* @param middleware_specs - the app's middleware layers, of which those that apply to the route contribute
|
|
26
|
+
* @returns the route's error map
|
|
27
|
+
*/
|
|
28
|
+
export const resolve_route_error_map = (spec, middleware_specs) => {
|
|
29
|
+
const all = derive_route_errors({
|
|
30
|
+
method: spec.method,
|
|
31
|
+
auth: spec.auth,
|
|
32
|
+
has_input: !is_null_schema(spec.input),
|
|
33
|
+
has_params: spec.params !== undefined,
|
|
34
|
+
has_query: spec.query !== undefined,
|
|
35
|
+
rate_limit: spec.rate_limit
|
|
36
|
+
});
|
|
37
|
+
if (middleware_specs) {
|
|
38
|
+
for (const mw of middleware_specs) {
|
|
39
|
+
if (mw.errors && middleware_spec_applies(mw, spec.path))
|
|
40
|
+
all.push(...mw.errors);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (spec.errors)
|
|
44
|
+
all.push(...spec.errors);
|
|
45
|
+
const defs = dedupe_rest_errors(all);
|
|
46
|
+
return { defs, schemas: to_error_schemas(defs) };
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Resolve every route's error map (`resolve_route_error_map`) against one
|
|
50
|
+
* middleware list, keyed by `format_route_key` (`METHOD path`).
|
|
51
|
+
*
|
|
52
|
+
* @param route_specs - the app's routes
|
|
53
|
+
* @param middleware_specs - the app's middleware layers
|
|
54
|
+
* @returns each route's error map
|
|
55
|
+
*/
|
|
56
|
+
export const resolve_app_route_error_maps = (route_specs, middleware_specs) => {
|
|
57
|
+
const route_error_maps = new Map();
|
|
58
|
+
for (const spec of route_specs) {
|
|
59
|
+
route_error_maps.set(format_route_key(spec), resolve_route_error_map(spec, middleware_specs));
|
|
60
|
+
}
|
|
61
|
+
return route_error_maps;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* A route's error map from a map built by `resolve_app_route_error_maps`, or,
|
|
65
|
+
* with no map, resolved from the route alone — without middleware.
|
|
66
|
+
*
|
|
67
|
+
* @param route_error_maps - the app's route error maps, if the caller has them
|
|
68
|
+
* @param spec - the route spec
|
|
69
|
+
* @returns the route's error map
|
|
70
|
+
* @throws Error naming the route's key when a map is given with no entry for
|
|
71
|
+
* the route — a map built over other routes, which would silently drop the
|
|
72
|
+
* middleware's definitions
|
|
73
|
+
*/
|
|
74
|
+
export const get_route_error_map = (route_error_maps, spec) => {
|
|
75
|
+
if (route_error_maps == null)
|
|
76
|
+
return resolve_route_error_map(spec);
|
|
77
|
+
const key = format_route_key(spec);
|
|
78
|
+
const route_error_map = route_error_maps.get(key);
|
|
79
|
+
if (!route_error_map) {
|
|
80
|
+
throw new Error(`get_route_error_map: the route error maps have no entry for '${key}'`);
|
|
81
|
+
}
|
|
82
|
+
return route_error_map;
|
|
83
|
+
};
|
|
@@ -18,7 +18,8 @@ import type { z } from 'zod';
|
|
|
18
18
|
import { type AnySchema } from '@fuzdev/fuz_schema/schema.ts';
|
|
19
19
|
import type { Logger } from '@fuzdev/fuz_util/log.ts';
|
|
20
20
|
import type { Db } from '../db/db.js';
|
|
21
|
-
import { type
|
|
21
|
+
import { type RestErrorDef } from './error_schemas.js';
|
|
22
|
+
import type { RateLimitKey } from '../rate_limiter.js';
|
|
22
23
|
import type { MiddlewareSpec } from './middleware_spec.js';
|
|
23
24
|
import { type RouteAuth } from './auth_shape.js';
|
|
24
25
|
import { type ValidationOutput, type ValidationSchema } from '../schema_issues.js';
|
|
@@ -162,6 +163,15 @@ export interface RouteSpec {
|
|
|
162
163
|
* misleading `null` body.
|
|
163
164
|
*/
|
|
164
165
|
raw_body?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* The route answers JSON-RPC 2.0 envelopes, its refusals included, not REST
|
|
168
|
+
* bodies — the pair `create_rpc_endpoint` builds. A JSON-RPC error envelope
|
|
169
|
+
* it answers is left out of the DEV error check and
|
|
170
|
+
* `assert_response_matches_spec`: no REST error definition describes one,
|
|
171
|
+
* and the dispatch core checks its own outputs. On any other route an
|
|
172
|
+
* envelope-shaped body is checked like any error body.
|
|
173
|
+
*/
|
|
174
|
+
jsonrpc?: boolean;
|
|
165
175
|
/**
|
|
166
176
|
* Marks a route that issues a credential — a session cookie — while
|
|
167
177
|
* declaring no auth itself: login, signup, bootstrap. A forged cross-site
|
|
@@ -194,21 +204,23 @@ export interface RouteSpec {
|
|
|
194
204
|
/**
|
|
195
205
|
* Rate limit key type — declares what this route's rate limiter is keyed on.
|
|
196
206
|
*
|
|
197
|
-
* When set, 429
|
|
207
|
+
* When set, 429 `rate_limit_exceeded` is derived (`derive_route_errors`).
|
|
198
208
|
* The actual `RateLimiter` instance is still wired imperatively in the handler —
|
|
199
209
|
* this field is metadata for surface introspection and policy invariants.
|
|
200
210
|
*/
|
|
201
211
|
rate_limit?: RateLimitKey;
|
|
202
212
|
/**
|
|
203
|
-
*
|
|
213
|
+
* The handler's error definitions (`rest_error`), one per code it answers
|
|
214
|
+
* with (a 404 for not-found, a 409 for a conflict).
|
|
204
215
|
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
216
|
+
* What the framework's own middleware answers (the input 400s, auth
|
|
217
|
+
* 401/403, rate limit 429) is derived from `method`, `auth`, the input
|
|
218
|
+
* slots, and `rate_limit` (`derive_route_errors`), and the middleware
|
|
219
|
+
* layers declare their own. The route's error map
|
|
220
|
+
* (`resolve_route_error_map`) is the union of all three: a definition here at
|
|
221
|
+
* a status another layer declares adds a branch there and replaces nothing.
|
|
210
222
|
*/
|
|
211
|
-
errors?:
|
|
223
|
+
errors?: ReadonlyArray<RestErrorDef>;
|
|
212
224
|
/**
|
|
213
225
|
* Whether to wrap the handler in a database transaction.
|
|
214
226
|
*
|
|
@@ -270,7 +282,7 @@ export declare const apply_middleware_specs: (app: Hono, specs: Array<Middleware
|
|
|
270
282
|
* For each spec: resolves auth to guards via the provided resolver,
|
|
271
283
|
* adds input validation middleware (for routes with non-null input schemas),
|
|
272
284
|
* runs the optional authorization phase to resolve the acting actor + build
|
|
273
|
-
* the request context, wraps handler with DEV-only output
|
|
285
|
+
* the request context, wraps handler with DEV-only output
|
|
274
286
|
* validation, wraps with error catch layer (a thrown JSON-RPC error,
|
|
275
287
|
* recognized via `to_thrown_jsonrpc_error`, maps to its status; any other
|
|
276
288
|
* throw to a 500), and registers the route.
|
|
@@ -301,18 +313,22 @@ export declare const apply_middleware_specs: (app: Hono, specs: Array<Middleware
|
|
|
301
313
|
* registries (`compile_action_registry`) share the same helper with
|
|
302
314
|
* `input` only — `ActionSpec` has no `query` shape.
|
|
303
315
|
*
|
|
304
|
-
* Every schema slot (`params`, `query`, `input`, `output
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
* route and the slot. A `@fuzdev/fuz_schema` `params` or `query`
|
|
308
|
-
* object schema (`get_schema_entries`), as the path params and
|
|
309
|
-
* are an object: one that isn't throws naming the route.
|
|
316
|
+
* Every schema slot (`params`, `query`, `input`, `output`) must hold a Zod
|
|
317
|
+
* or a `@fuzdev/fuz_schema` schema (`assert_validation_schema`), and every
|
|
318
|
+
* `errors` entry an error definition (`rest_error`): anything else throws
|
|
319
|
+
* naming the route and the slot. A `@fuzdev/fuz_schema` `params` or `query`
|
|
320
|
+
* must be an object schema (`get_schema_entries`), as the path params and
|
|
321
|
+
* query string are an object: one that isn't throws naming the route.
|
|
322
|
+
*
|
|
323
|
+
* In DEV each handler's 2xx JSON responses are checked against its `output`.
|
|
324
|
+
* Error responses are checked by the DEV error check
|
|
325
|
+
* (`create_dev_route_error_check`), which `create_app_server` mounts outermost.
|
|
310
326
|
*
|
|
311
327
|
* @param resolve_auth_guards - maps `RouteAuth` to middleware — use `fuz_auth_guard_resolver` from `auth/auth_guard_resolver.ts`
|
|
312
328
|
* @param authorize - optional authorization phase; runs after the pre-authorization guards and before the post-authorization guards
|
|
313
329
|
* @param db - used for transaction wrapping and `RouteContext`
|
|
314
330
|
* @mutates `app`
|
|
315
|
-
* @throws Error if two specs share the same `method` + `path` (each combination must be unique), if a schema slot holds a value of neither library, if any spec violates a cross-axis auth invariant or the actor-acting biconditional, or if a fuz_schema `params` or `query` is not an object schema
|
|
331
|
+
* @throws Error if two specs share the same `method` + `path` (each combination must be unique), if a schema slot holds a value of neither library or an `errors` entry is not a definition, if any spec violates a cross-axis auth invariant or the actor-acting biconditional, or if a fuz_schema `params` or `query` is not an object schema
|
|
316
332
|
*/
|
|
317
333
|
export declare const apply_route_specs: (app: Hono, specs: Array<RouteSpec>, resolve_auth_guards: AuthGuardResolver, log: Logger, db: Db, authorize?: AuthorizationHandler) => void;
|
|
318
334
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route_spec.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/route_spec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAW,IAAI,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AACtE,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAAa,KAAK,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEzE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"route_spec.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/route_spec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAW,IAAI,EAAE,iBAAiB,E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 'esm-env';
|
|
17
17
|
import { is_schema } from '@fuzdev/fuz_schema/schema.ts';
|
|
18
18
|
import { get_schema_entries } from '@fuzdev/fuz_schema/introspect.ts';
|
|
19
|
+
import { format_route_key } from './surface_query.js';
|
|
19
20
|
import { is_db_checkout_error } from '../db/db_checkout_error.js';
|
|
20
|
-
import { ERROR_INVALID_JSON_BODY, ERROR_INVALID_REQUEST_BODY, ERROR_INVALID_ROUTE_PARAMS, ERROR_INVALID_QUERY_PARAMS, ERROR_SERVICE_UNAVAILABLE, ERROR_UNSUPPORTED_CONTENT_TYPE } from './error_schemas.js';
|
|
21
|
+
import { ERROR_INVALID_JSON_BODY, ERROR_INVALID_REQUEST_BODY, ERROR_INVALID_ROUTE_PARAMS, ERROR_INVALID_QUERY_PARAMS, ERROR_INTERNAL_ERROR, ERROR_SERVICE_UNAVAILABLE, ERROR_UNSUPPORTED_CONTENT_TYPE } from './error_schemas.js';
|
|
21
22
|
import { is_post_content_type_refused } from './simple_request.js';
|
|
22
23
|
import { jsonrpc_error_code_to_http_status, jsonrpc_error_code_to_name, dev_only, to_thrown_jsonrpc_error } from './jsonrpc_errors.js';
|
|
23
|
-
import { is_null_schema
|
|
24
|
+
import { is_null_schema } from './schema_helpers.js';
|
|
25
|
+
import { is_json_response } from './route_error_check.js';
|
|
24
26
|
import { dispatch_with_post_commit_rollback } from './pending_effects.js';
|
|
25
27
|
import { assert_route_auth_acting_biconditional, assert_route_auth_invariants } from './auth_shape.js';
|
|
26
28
|
import { assert_validation_schema, parse_any, to_wire_issues } from '../schema_issues.js';
|
|
@@ -150,41 +152,29 @@ const create_query_validation = (query_schema) => {
|
|
|
150
152
|
};
|
|
151
153
|
return [validate];
|
|
152
154
|
};
|
|
153
|
-
/** Whether a JSON body is the catch layer's `{error: 'internal_error'}` shape. */
|
|
154
|
-
const is_internal_error_body = (body) => body !== null &&
|
|
155
|
-
typeof body === 'object' &&
|
|
156
|
-
body.error === 'internal_error';
|
|
157
|
-
/** Whether a JSON body is the catch layer's failed-checkout `{error: 'service_unavailable'}`. */
|
|
158
|
-
const is_service_unavailable_body = (body) => body !== null &&
|
|
159
|
-
typeof body === 'object' &&
|
|
160
|
-
body.error === ERROR_SERVICE_UNAVAILABLE;
|
|
161
155
|
/**
|
|
162
|
-
* Wrap a handler with DEV-only output
|
|
156
|
+
* Wrap a handler with DEV-only output validation.
|
|
163
157
|
*
|
|
164
|
-
* In development, validates 2xx JSON responses against the output schema
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
* log carries the library's own issues.
|
|
171
|
-
*
|
|
172
|
-
* does not throw. Non-JSON responses (SSE, binary) are skipped. In
|
|
173
|
-
* production, returns the handler unchanged.
|
|
158
|
+
* In development, validates 2xx JSON responses against the output schema,
|
|
159
|
+
* logging an error on a mismatch and returning the response unchanged — it
|
|
160
|
+
* never throws. Non-2xx responses are the DEV error check's
|
|
161
|
+
* (`create_dev_route_error_check`), which `create_app_server` mounts
|
|
162
|
+
* outermost so it sees what every layer answers. Non-JSON responses (SSE,
|
|
163
|
+
* binary) are skipped. The schema is Zod or `@fuzdev/fuz_schema`, parsed
|
|
164
|
+
* with `parse_any`; the log carries the library's own issues. In production,
|
|
165
|
+
* returns the handler unchanged.
|
|
174
166
|
*/
|
|
175
|
-
const wrap_output_validation = (handler, output_schema,
|
|
167
|
+
const wrap_output_validation = (handler, output_schema, log) => {
|
|
176
168
|
if (!DEV)
|
|
177
169
|
return handler;
|
|
178
|
-
if (is_null_schema(output_schema)
|
|
170
|
+
if (is_null_schema(output_schema))
|
|
179
171
|
return handler;
|
|
180
172
|
return async (c, next) => {
|
|
181
173
|
const response = await handler(c, next);
|
|
182
|
-
|
|
183
|
-
const content_type = response.headers.get('Content-Type');
|
|
184
|
-
if (!content_type?.includes('application/json'))
|
|
174
|
+
if (!response.ok)
|
|
185
175
|
return response;
|
|
186
|
-
|
|
187
|
-
if (!
|
|
176
|
+
// Only validate JSON responses — streaming responses (SSE) would hang on .json().
|
|
177
|
+
if (!is_json_response(response))
|
|
188
178
|
return response;
|
|
189
179
|
let body;
|
|
190
180
|
try {
|
|
@@ -194,20 +184,9 @@ const wrap_output_validation = (handler, output_schema, error_schemas, log) => {
|
|
|
194
184
|
// clone() or json() failed on a response claiming application/json
|
|
195
185
|
return response;
|
|
196
186
|
}
|
|
197
|
-
|
|
198
|
-
// declares it — checking it against a route's 500 schema would only
|
|
199
|
-
// misreport a schema problem the route can't fix
|
|
200
|
-
if (response.status === 500 && is_internal_error_body(body))
|
|
201
|
-
return response;
|
|
202
|
-
// the same for a failed pool checkout's 503, which any route can answer
|
|
203
|
-
if (response.status === 503 && is_service_unavailable_body(body))
|
|
204
|
-
return response;
|
|
205
|
-
const result = parse_any(schema, body);
|
|
187
|
+
const result = parse_any(output_schema, body);
|
|
206
188
|
if (!result.ok) {
|
|
207
|
-
|
|
208
|
-
? 'Output schema mismatch'
|
|
209
|
-
: `Error schema mismatch (${response.status})`;
|
|
210
|
-
log.error(`${label}: ${c.req.method} ${c.req.path}`, result.issues);
|
|
189
|
+
log.error(`Output schema mismatch: ${c.req.method} ${c.req.path}`, result.issues);
|
|
211
190
|
}
|
|
212
191
|
return response;
|
|
213
192
|
};
|
|
@@ -227,7 +206,7 @@ export const apply_middleware_specs = (app, specs) => {
|
|
|
227
206
|
*
|
|
228
207
|
* Catches a thrown JSON-RPC error — a `ThrownJsonrpcError` from any copy of
|
|
229
208
|
* fuz_app, recognized by its brand (`to_thrown_jsonrpc_error`) — and maps it
|
|
230
|
-
* to a flat REST
|
|
209
|
+
* to a flat REST error body (`{error: <reason>, message?, ...rest_data}`)
|
|
231
210
|
* at the matching HTTP status.
|
|
232
211
|
* A failed pool checkout (`DbCheckoutError`) answers the flat 503
|
|
233
212
|
* `{error: 'service_unavailable'}` with no `message`, byte for byte the Rust
|
|
@@ -252,7 +231,7 @@ export const apply_middleware_specs = (app, specs) => {
|
|
|
252
231
|
* REST callers key on HTTP status + `error` reason, and the JSON-RPC code
|
|
253
232
|
* is recoverable via `http_status_to_jsonrpc_error_code(status)` on the
|
|
254
233
|
* rare consumer that wants it. Keeping the shape transport-shaped (REST
|
|
255
|
-
* emits
|
|
234
|
+
* emits the flat body; JSON-RPC dispatcher emits the JSON-RPC envelope) avoids
|
|
256
235
|
* a hybrid envelope that has to be normalized on the way out.
|
|
257
236
|
*/
|
|
258
237
|
const wrap_error_catch = (handler, log) => {
|
|
@@ -278,7 +257,7 @@ const wrap_error_catch = (handler, log) => {
|
|
|
278
257
|
// validation-issue redaction), production gets the bare error code.
|
|
279
258
|
log.error('Unhandled handler error', err);
|
|
280
259
|
const body = {
|
|
281
|
-
error:
|
|
260
|
+
error: ERROR_INTERNAL_ERROR,
|
|
282
261
|
message: dev_only(() => (err instanceof Error ? err.message : undefined))
|
|
283
262
|
};
|
|
284
263
|
return c.json(body, 500);
|
|
@@ -325,7 +304,7 @@ const build_rest_error_body = (err) => {
|
|
|
325
304
|
* For each spec: resolves auth to guards via the provided resolver,
|
|
326
305
|
* adds input validation middleware (for routes with non-null input schemas),
|
|
327
306
|
* runs the optional authorization phase to resolve the acting actor + build
|
|
328
|
-
* the request context, wraps handler with DEV-only output
|
|
307
|
+
* the request context, wraps handler with DEV-only output
|
|
329
308
|
* validation, wraps with error catch layer (a thrown JSON-RPC error,
|
|
330
309
|
* recognized via `to_thrown_jsonrpc_error`, maps to its status; any other
|
|
331
310
|
* throw to a 500), and registers the route.
|
|
@@ -356,23 +335,27 @@ const build_rest_error_body = (err) => {
|
|
|
356
335
|
* registries (`compile_action_registry`) share the same helper with
|
|
357
336
|
* `input` only — `ActionSpec` has no `query` shape.
|
|
358
337
|
*
|
|
359
|
-
* Every schema slot (`params`, `query`, `input`, `output
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
* route and the slot. A `@fuzdev/fuz_schema` `params` or `query`
|
|
363
|
-
* object schema (`get_schema_entries`), as the path params and
|
|
364
|
-
* are an object: one that isn't throws naming the route.
|
|
338
|
+
* Every schema slot (`params`, `query`, `input`, `output`) must hold a Zod
|
|
339
|
+
* or a `@fuzdev/fuz_schema` schema (`assert_validation_schema`), and every
|
|
340
|
+
* `errors` entry an error definition (`rest_error`): anything else throws
|
|
341
|
+
* naming the route and the slot. A `@fuzdev/fuz_schema` `params` or `query`
|
|
342
|
+
* must be an object schema (`get_schema_entries`), as the path params and
|
|
343
|
+
* query string are an object: one that isn't throws naming the route.
|
|
344
|
+
*
|
|
345
|
+
* In DEV each handler's 2xx JSON responses are checked against its `output`.
|
|
346
|
+
* Error responses are checked by the DEV error check
|
|
347
|
+
* (`create_dev_route_error_check`), which `create_app_server` mounts outermost.
|
|
365
348
|
*
|
|
366
349
|
* @param resolve_auth_guards - maps `RouteAuth` to middleware — use `fuz_auth_guard_resolver` from `auth/auth_guard_resolver.ts`
|
|
367
350
|
* @param authorize - optional authorization phase; runs after the pre-authorization guards and before the post-authorization guards
|
|
368
351
|
* @param db - used for transaction wrapping and `RouteContext`
|
|
369
352
|
* @mutates `app`
|
|
370
|
-
* @throws Error if two specs share the same `method` + `path` (each combination must be unique), if a schema slot holds a value of neither library, if any spec violates a cross-axis auth invariant or the actor-acting biconditional, or if a fuz_schema `params` or `query` is not an object schema
|
|
353
|
+
* @throws Error if two specs share the same `method` + `path` (each combination must be unique), if a schema slot holds a value of neither library or an `errors` entry is not a definition, if any spec violates a cross-axis auth invariant or the actor-acting biconditional, or if a fuz_schema `params` or `query` is not an object schema
|
|
371
354
|
*/
|
|
372
355
|
export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, authorize) => {
|
|
373
356
|
const registered = new Set();
|
|
374
357
|
for (const spec of specs) {
|
|
375
|
-
const route_key =
|
|
358
|
+
const route_key = format_route_key(spec);
|
|
376
359
|
if (registered.has(route_key)) {
|
|
377
360
|
throw new Error(`Duplicate route: ${route_key} — each method+path combination must be unique`);
|
|
378
361
|
}
|
|
@@ -387,7 +370,6 @@ export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, auth
|
|
|
387
370
|
const params_validation = create_params_validation(spec.params);
|
|
388
371
|
const query_validation = create_query_validation(spec.query);
|
|
389
372
|
const input_validation = create_input_validation(spec.input, spec.method);
|
|
390
|
-
const merged_errors = merge_error_schemas(spec, null);
|
|
391
373
|
const authorization = authorize
|
|
392
374
|
? [
|
|
393
375
|
async (c, next) => {
|
|
@@ -418,13 +400,13 @@ export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, auth
|
|
|
418
400
|
};
|
|
419
401
|
// Step 2: error catch layer — a thrown error becomes its REST body here
|
|
420
402
|
handler = wrap_error_catch(handler, log);
|
|
421
|
-
// Step 3: output validation
|
|
422
|
-
handler = wrap_output_validation(handler, spec.output,
|
|
403
|
+
// Step 3: output validation of a 2xx body
|
|
404
|
+
handler = wrap_output_validation(handler, spec.output, log);
|
|
423
405
|
app.on(spec.method, [spec.path], ...content_type_check, ...params_validation, ...query_validation, ...pre_authorization_guards, ...authorization, ...post_authorization_guards, ...input_validation, handler);
|
|
424
406
|
}
|
|
425
407
|
};
|
|
426
|
-
// throws unless every schema slot holds a Zod or fuz_schema schema
|
|
427
|
-
// `
|
|
408
|
+
// throws unless every schema slot holds a Zod or fuz_schema schema and every
|
|
409
|
+
// `errors` entry is a definition; `params` and `query` may be absent
|
|
428
410
|
const assert_schema_slots = (spec, route_key) => {
|
|
429
411
|
const label = `Route "${route_key}"`;
|
|
430
412
|
if (spec.params !== undefined)
|
|
@@ -433,10 +415,14 @@ const assert_schema_slots = (spec, route_key) => {
|
|
|
433
415
|
assert_validation_schema(spec.query, `${label}: query`);
|
|
434
416
|
assert_validation_schema(spec.input, `${label}: input`);
|
|
435
417
|
assert_validation_schema(spec.output, `${label}: output`);
|
|
436
|
-
if (spec.errors) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
418
|
+
if (spec.errors !== undefined) {
|
|
419
|
+
if (!Array.isArray(spec.errors)) {
|
|
420
|
+
throw new Error(`${label}: errors is not a list of error definitions (\`rest_error\`)`);
|
|
421
|
+
}
|
|
422
|
+
for (const [i, def] of spec.errors.entries()) {
|
|
423
|
+
if (!is_schema(def?.schema)) {
|
|
424
|
+
throw new Error(`${label}: errors[${i}] is not an error definition (\`rest_error\`)`);
|
|
425
|
+
}
|
|
440
426
|
}
|
|
441
427
|
}
|
|
442
428
|
};
|
|
@@ -7,9 +7,6 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
10
|
-
import type { RouteAuth } from './auth_shape.js';
|
|
11
|
-
import type { RouteMethod } from './route_spec.js';
|
|
12
|
-
import { type RateLimitKey, type RouteErrorSchemas } from './error_schemas.js';
|
|
13
10
|
import { type ParseAnyResult, type ValidationOutput, type ValidationSchema } from '../schema_issues.js';
|
|
14
11
|
/**
|
|
15
12
|
* Check if a schema is exactly `z.null()`, or a `@fuzdev/fuz_schema`
|
|
@@ -238,34 +235,4 @@ export declare const middleware_spec_applies: (spec: {
|
|
|
238
235
|
readonly path: string;
|
|
239
236
|
}> | null;
|
|
240
237
|
}, route_path: string) => boolean;
|
|
241
|
-
/**
|
|
242
|
-
* Copy an error-schema map without its entries left `undefined`, so a
|
|
243
|
-
* status declared `undefined` reads as absent: it overrides nothing beneath
|
|
244
|
-
* it in a merge.
|
|
245
|
-
*
|
|
246
|
-
* @param errors - a route's or middleware's error schemas, keyed by status
|
|
247
|
-
* @returns the entries that hold a schema
|
|
248
|
-
*/
|
|
249
|
-
export declare const strip_undefined_error_schemas: (errors: RouteErrorSchemas) => Record<number, ValidationSchema>;
|
|
250
|
-
/**
|
|
251
|
-
* Merge auto-derived, middleware, and explicit error schemas for a route spec.
|
|
252
|
-
*
|
|
253
|
-
* Merge order: derived -> middleware -> explicit route errors.
|
|
254
|
-
* Later layers override earlier ones for the same status code; an entry left
|
|
255
|
-
* `undefined` is absent (`strip_undefined_error_schemas`), so it leaves the
|
|
256
|
-
* layer beneath it in place.
|
|
257
|
-
*
|
|
258
|
-
* @param spec - the route spec (reads `method`, `auth`, `input`, `params`, `query`, `rate_limit`, `errors`)
|
|
259
|
-
* @param middleware_errors - errors contributed by middleware whose path matches the route
|
|
260
|
-
* @returns merged error schemas, or `null` if empty
|
|
261
|
-
*/
|
|
262
|
-
export declare const merge_error_schemas: (spec: {
|
|
263
|
-
method: RouteMethod;
|
|
264
|
-
auth: RouteAuth;
|
|
265
|
-
input: ValidationSchema;
|
|
266
|
-
params?: ValidationSchema;
|
|
267
|
-
query?: ValidationSchema;
|
|
268
|
-
rate_limit?: RateLimitKey;
|
|
269
|
-
errors?: RouteErrorSchemas;
|
|
270
|
-
}, middleware_errors?: RouteErrorSchemas | null) => Record<number, ValidationSchema> | null;
|
|
271
238
|
//# sourceMappingURL=schema_helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema_helpers.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/schema_helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAQH,OAAO,
|
|
1
|
+
{"version":3,"file":"schema_helpers.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/schema_helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAQH,OAAO,EAIN,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,MAAM,qBAAqB,CAAC;AAE7B;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,gBAAgB,KAAG,OAEc,CAAC;AAEzE;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,gBAAgB,KAAG,OAEmB,CAAC;AAE9E;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,cAAc,GAAI,YAAY,OAAO,KAAG,OAUpD,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,eAAe,GAAI,OAAO,gBAAgB,EAAE,YAAY,OAAO,KAAG,OACP,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;GAmBG;A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ACtE,EACD,YAAY,MAAM,KAChB,OAE0F,CAAC"}
|
|
@@ -12,7 +12,6 @@ import { to_error_message } from '@fuzdev/fuz_util/error.ts';
|
|
|
12
12
|
import { is_schema } from '@fuzdev/fuz_schema/schema.ts';
|
|
13
13
|
import { unwrap_schema } from '@fuzdev/fuz_schema/introspect.ts';
|
|
14
14
|
import { to_json_schema } from '@fuzdev/fuz_schema/json_schema.ts';
|
|
15
|
-
import { derive_error_schemas } from './error_schemas.js';
|
|
16
15
|
import { assert_validation_schema, parse_any, unknown_key_targets } from '../schema_issues.js';
|
|
17
16
|
/**
|
|
18
17
|
* Check if a schema is exactly `z.null()`, or a `@fuzdev/fuz_schema`
|
|
@@ -457,47 +456,3 @@ export const middleware_paths_overlap = (a, b) => {
|
|
|
457
456
|
*/
|
|
458
457
|
export const middleware_spec_applies = (spec, route_path) => middleware_applies(spec.path, route_path) &&
|
|
459
458
|
!(spec.carve_outs ?? []).some((carve_out) => middleware_applies(carve_out.path, route_path));
|
|
460
|
-
/**
|
|
461
|
-
* Copy an error-schema map without its entries left `undefined`, so a
|
|
462
|
-
* status declared `undefined` reads as absent: it overrides nothing beneath
|
|
463
|
-
* it in a merge.
|
|
464
|
-
*
|
|
465
|
-
* @param errors - a route's or middleware's error schemas, keyed by status
|
|
466
|
-
* @returns the entries that hold a schema
|
|
467
|
-
*/
|
|
468
|
-
export const strip_undefined_error_schemas = (errors) => {
|
|
469
|
-
const defined = {};
|
|
470
|
-
for (const [status, schema] of Object.entries(errors)) {
|
|
471
|
-
if (schema !== undefined)
|
|
472
|
-
defined[Number(status)] = schema;
|
|
473
|
-
}
|
|
474
|
-
return defined;
|
|
475
|
-
};
|
|
476
|
-
/**
|
|
477
|
-
* Merge auto-derived, middleware, and explicit error schemas for a route spec.
|
|
478
|
-
*
|
|
479
|
-
* Merge order: derived -> middleware -> explicit route errors.
|
|
480
|
-
* Later layers override earlier ones for the same status code; an entry left
|
|
481
|
-
* `undefined` is absent (`strip_undefined_error_schemas`), so it leaves the
|
|
482
|
-
* layer beneath it in place.
|
|
483
|
-
*
|
|
484
|
-
* @param spec - the route spec (reads `method`, `auth`, `input`, `params`, `query`, `rate_limit`, `errors`)
|
|
485
|
-
* @param middleware_errors - errors contributed by middleware whose path matches the route
|
|
486
|
-
* @returns merged error schemas, or `null` if empty
|
|
487
|
-
*/
|
|
488
|
-
export const merge_error_schemas = (spec, middleware_errors) => {
|
|
489
|
-
const derived = derive_error_schemas({
|
|
490
|
-
auth: spec.auth,
|
|
491
|
-
has_input: !is_null_schema(spec.input),
|
|
492
|
-
has_params: !!spec.params,
|
|
493
|
-
has_query: !!spec.query,
|
|
494
|
-
rate_limit: spec.rate_limit,
|
|
495
|
-
checks_content_type: spec.method === 'POST'
|
|
496
|
-
});
|
|
497
|
-
const merged = {
|
|
498
|
-
...derived,
|
|
499
|
-
...(middleware_errors && strip_undefined_error_schemas(middleware_errors)),
|
|
500
|
-
...(spec.errors && strip_undefined_error_schemas(spec.errors))
|
|
501
|
-
};
|
|
502
|
-
return Object.keys(merged).length > 0 ? merged : null;
|
|
503
|
-
};
|
package/dist/http/surface.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* App surface generation — JSON-serializable attack surface from route and middleware specs.
|
|
3
3
|
*
|
|
4
|
-
* Pure schema helpers (`is_null_schema`, `schema_to_surface`, `middleware_applies`,
|
|
5
|
-
* `
|
|
4
|
+
* Pure schema helpers (`is_null_schema`, `schema_to_surface`, `middleware_applies`,
|
|
5
|
+
* `middleware_spec_applies`) live in `http/schema_helpers.ts`, and each route's
|
|
6
|
+
* error map (`resolve_route_error_map`) in `http/route_errors.ts`.
|
|
6
7
|
*
|
|
7
8
|
* @module
|
|
8
9
|
*/
|
|
@@ -11,10 +12,11 @@ import type { EventSpec } from '../realtime/sse.js';
|
|
|
11
12
|
import type { MiddlewareSpec } from './middleware_spec.js';
|
|
12
13
|
import type { RouteSpec } from './route_spec.js';
|
|
13
14
|
import type { RouteAuth } from './auth_shape.js';
|
|
14
|
-
import type { RateLimitKey
|
|
15
|
+
import type { RateLimitKey } from '../rate_limiter.js';
|
|
15
16
|
import type { RpcAction } from '../actions/action_types.js';
|
|
16
17
|
import type { ActionKind } from '../actions/action_spec.js';
|
|
17
18
|
import type { ResolvedWsEndpointSpec } from '../actions/ws_endpoint_spec.js';
|
|
19
|
+
import { type RouteErrorMap } from './route_errors.js';
|
|
18
20
|
import { type Sensitivity } from '../sensitivity.js';
|
|
19
21
|
/** A route in the generated attack surface (JSON-serializable). */
|
|
20
22
|
export interface AppSurfaceRoute {
|
|
@@ -166,6 +168,12 @@ export interface AppSurfaceSpec {
|
|
|
166
168
|
surface: AppSurface;
|
|
167
169
|
route_specs: Array<RouteSpec>;
|
|
168
170
|
middleware_specs: Array<MiddlewareSpec>;
|
|
171
|
+
/**
|
|
172
|
+
* Each route's error map (`resolve_route_error_map` against `middleware_specs`),
|
|
173
|
+
* keyed `METHOD path` (`format_route_key`) — the one the surface writes, and
|
|
174
|
+
* the one the DEV error check and the test helpers read.
|
|
175
|
+
*/
|
|
176
|
+
route_error_maps: ReadonlyMap<string, RouteErrorMap>;
|
|
169
177
|
rpc_endpoints: Array<RpcEndpointSpec>;
|
|
170
178
|
ws_endpoints: Array<ResolvedWsEndpointSpec>;
|
|
171
179
|
}
|
|
@@ -191,15 +199,13 @@ export interface GenerateAppSurfaceOptions {
|
|
|
191
199
|
* it — so the surface records the exact patterns the upgrade gate matches.
|
|
192
200
|
*/
|
|
193
201
|
ws_endpoints?: ReadonlyArray<ResolvedWsEndpointSpec>;
|
|
202
|
+
/**
|
|
203
|
+
* Each route's error map, keyed `METHOD path`, when the caller has built
|
|
204
|
+
* it (`resolve_app_route_error_maps` over the same routes and middleware);
|
|
205
|
+
* resolved here otherwise.
|
|
206
|
+
*/
|
|
207
|
+
route_error_maps?: ReadonlyMap<string, RouteErrorMap>;
|
|
194
208
|
}
|
|
195
|
-
/**
|
|
196
|
-
* Collect error schemas from all middleware that applies to a route path.
|
|
197
|
-
* An entry left `undefined` is absent (`strip_undefined_error_schemas`),
|
|
198
|
-
* so it leaves an earlier layer's schema for that status in place.
|
|
199
|
-
*
|
|
200
|
-
* @returns merged middleware error schemas, or `null` if none
|
|
201
|
-
*/
|
|
202
|
-
export declare const collect_middleware_errors: (middleware: Array<MiddlewareSpec>, route_path: string) => RouteErrorSchemas | null;
|
|
203
209
|
/**
|
|
204
210
|
* Convert a `@fuzdev/fuz_schema` env schema to surface entries, one per
|
|
205
211
|
* entry in order, reading each field's `meta`: the description from its
|
|
@@ -226,7 +232,8 @@ export declare const events_to_surface: (event_specs: Array<EventSpec>, diagnost
|
|
|
226
232
|
*/
|
|
227
233
|
export declare const generate_app_surface: (options: GenerateAppSurfaceOptions) => AppSurface;
|
|
228
234
|
/**
|
|
229
|
-
* Create an `AppSurfaceSpec` — the surface bundled with its source specs
|
|
235
|
+
* Create an `AppSurfaceSpec` — the surface bundled with its source specs and
|
|
236
|
+
* each route's error map, built once and read by both.
|
|
230
237
|
*/
|
|
231
238
|
export declare const create_app_surface_spec: (options: GenerateAppSurfaceOptions) => AppSurfaceSpec;
|
|
232
239
|
//# sourceMappingURL=surface.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"surface.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/surface.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"surface.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/surface.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAoB,KAAK,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEhF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,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