@fuzdev/fuz_app 0.164.0 → 0.166.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/dist/actions/CLAUDE.md +2 -2
- 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 +24 -15
- package/dist/actions/action_spec.d.ts +2 -2
- package/dist/actions/perform_action.d.ts +2 -1
- package/dist/actions/perform_action.d.ts.map +1 -1
- package/dist/auth/account_action_specs.d.ts +8 -8
- package/dist/auth/account_route_schema.d.ts +197 -164
- package/dist/auth/account_route_schema.d.ts.map +1 -1
- package/dist/auth/account_route_schema.js +19 -17
- package/dist/auth/account_routes.d.ts.map +1 -1
- package/dist/auth/account_routes.js +285 -294
- package/dist/auth/admin_action_specs.d.ts +64 -64
- package/dist/auth/audit_log_route_schema.d.ts +16 -8
- package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
- package/dist/auth/audit_log_route_schema.js +4 -3
- package/dist/auth/audit_log_routes.d.ts +1 -1
- package/dist/auth/audit_log_routes.d.ts.map +1 -1
- package/dist/auth/audit_log_routes.js +112 -111
- package/dist/auth/audit_log_schema.d.ts +22 -22
- package/dist/auth/bootstrap_route_schema.d.ts +46 -69
- package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
- package/dist/auth/bootstrap_route_schema.js +8 -7
- package/dist/auth/bootstrap_routes.d.ts.map +1 -1
- package/dist/auth/bootstrap_routes.js +100 -98
- package/dist/auth/role_grant_offer_action_specs.d.ts +4 -4
- package/dist/auth/signup_route_schema.d.ts +9 -4
- package/dist/auth/signup_route_schema.d.ts.map +1 -1
- package/dist/auth/signup_route_schema.js +5 -3
- package/dist/auth/signup_routes.d.ts.map +1 -1
- package/dist/auth/signup_routes.js +170 -170
- package/dist/http/CLAUDE.md +50 -4
- 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 +13 -11
- package/dist/http/db_routes.d.ts.map +1 -1
- package/dist/http/db_routes.js +25 -28
- package/dist/http/error_schemas.d.ts +5 -1
- package/dist/http/error_schemas.d.ts.map +1 -1
- package/dist/http/error_schemas.js +5 -1
- package/dist/http/route_error_check.d.ts +21 -0
- package/dist/http/route_error_check.d.ts.map +1 -1
- package/dist/http/route_error_check.js +37 -1
- package/dist/http/route_result.d.ts +101 -0
- package/dist/http/route_result.d.ts.map +1 -0
- package/dist/http/route_result.js +56 -0
- package/dist/http/route_spec.d.ts +110 -6
- package/dist/http/route_spec.d.ts.map +1 -1
- package/dist/http/route_spec.js +82 -14
- package/dist/http/schema_helpers.d.ts +15 -1
- package/dist/http/schema_helpers.d.ts.map +1 -1
- package/dist/http/schema_helpers.js +12 -3
- package/dist/rate_limiter.d.ts +0 -9
- package/dist/rate_limiter.d.ts.map +1 -1
- package/dist/rate_limiter.js +0 -15
- package/dist/server/serve_fact_route.d.ts.map +1 -1
- package/dist/server/serve_fact_route.js +23 -21
- package/dist/server/surface_route.d.ts +1 -1
- package/dist/server/surface_route.d.ts.map +1 -1
- package/dist/server/surface_route.js +4 -2
- package/dist/testing/CLAUDE.md +80 -28
- package/dist/testing/admin_integration.js +1 -1
- package/dist/testing/adversarial_404.d.ts.map +1 -1
- package/dist/testing/adversarial_404.js +13 -8
- package/dist/testing/adversarial_input.js +2 -2
- package/dist/testing/assertions.d.ts +13 -3
- package/dist/testing/assertions.d.ts.map +1 -1
- package/dist/testing/assertions.js +21 -4
- package/dist/testing/attack_surface.d.ts +12 -0
- package/dist/testing/attack_surface.d.ts.map +1 -1
- package/dist/testing/attack_surface.js +8 -5
- package/dist/testing/cross_backend/in_process_setup.d.ts +7 -3
- package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/in_process_setup.js +11 -4
- package/dist/testing/cross_backend/login_security.d.ts.map +1 -1
- package/dist/testing/cross_backend/login_security.js +3 -2
- package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_consumer_surface.js +2 -6
- package/dist/testing/cross_backend/spine_suites.d.ts +3 -2
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +5 -2
- package/dist/testing/cross_backend/standard.d.ts +31 -0
- package/dist/testing/cross_backend/standard.d.ts.map +1 -1
- package/dist/testing/cross_backend/standard.js +9 -3
- package/dist/testing/data_exposure.d.ts +7 -0
- package/dist/testing/data_exposure.d.ts.map +1 -1
- package/dist/testing/data_exposure.js +3 -3
- package/dist/testing/error_coverage.d.ts +35 -33
- package/dist/testing/error_coverage.d.ts.map +1 -1
- package/dist/testing/error_coverage.js +53 -43
- package/dist/testing/integration.d.ts +28 -1
- package/dist/testing/integration.d.ts.map +1 -1
- package/dist/testing/integration.js +51 -18
- package/dist/testing/integration_helpers.d.ts.map +1 -1
- package/dist/testing/integration_helpers.js +4 -15
- package/dist/testing/middleware_gate_probes.d.ts +90 -0
- package/dist/testing/middleware_gate_probes.d.ts.map +1 -0
- package/dist/testing/middleware_gate_probes.js +179 -0
- package/dist/testing/round_trip.d.ts +7 -0
- package/dist/testing/round_trip.d.ts.map +1 -1
- package/dist/testing/round_trip.js +3 -2
- package/dist/testing/schema_generators.d.ts +31 -9
- package/dist/testing/schema_generators.d.ts.map +1 -1
- package/dist/testing/schema_generators.js +53 -11
- package/dist/testing/stubs.d.ts +14 -3
- package/dist/testing/stubs.d.ts.map +1 -1
- package/dist/testing/stubs.js +21 -6
- package/dist/testing/transports/raw_http.d.ts +10 -2
- package/dist/testing/transports/raw_http.d.ts.map +1 -1
- package/dist/testing/transports/raw_http.js +10 -4
- package/package.json +1 -1
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Routes are defined as data (method, path, auth, input/output schemas, handler),
|
|
5
5
|
* then applied to Hono. The attack surface is generated from the specs —
|
|
6
|
-
* always accurate, always complete.
|
|
6
|
+
* always accurate, always complete. A handler returns a result
|
|
7
|
+
* (`http/route_result.ts`) and `apply_route_specs` builds the response; a spec
|
|
8
|
+
* is made by `define_route` (or `define_route_shape` + `attach_route_handler`),
|
|
9
|
+
* which checks the handler's results against the route.
|
|
7
10
|
*
|
|
8
11
|
* Input/output schemas align with SAES `ActionSpec` conventions, and each is
|
|
9
12
|
* a Zod or `@fuzdev/fuz_schema` schema (`ValidationSchema`):
|
|
@@ -18,11 +21,12 @@ import type { z } from 'zod';
|
|
|
18
21
|
import { type AnySchema } from '@fuzdev/fuz_schema/schema.ts';
|
|
19
22
|
import type { Logger } from '@fuzdev/fuz_util/log.ts';
|
|
20
23
|
import type { Db } from '../db/db.js';
|
|
21
|
-
import { type RestErrorDef } from './error_schemas.js';
|
|
24
|
+
import { type RestErrorDef, type REST_AUTHENTICATION_REQUIRED, type REST_CREDENTIAL_TYPE_REQUIRED, type REST_INSUFFICIENT_PERMISSIONS, type REST_INTERNAL_ERROR, type REST_RATE_LIMIT_EXCEEDED, type REST_SERVICE_UNAVAILABLE, type REST_TOKEN_SCOPE_REQUIRED } from './error_schemas.js';
|
|
22
25
|
import type { RateLimitKey } from '../rate_limiter.js';
|
|
23
26
|
import type { MiddlewareSpec } from './middleware_spec.js';
|
|
27
|
+
import type { ReadonlyJson, RouteFailOf, RouteOk, RouteRaw, RouteResult } from './route_result.js';
|
|
24
28
|
import { type RouteAuth } from './auth_shape.js';
|
|
25
|
-
import { type ValidationOutput, type ValidationSchema } from '../schema_issues.js';
|
|
29
|
+
import { type ValidationInput, type ValidationOutput, type ValidationSchema } from '../schema_issues.js';
|
|
26
30
|
/**
|
|
27
31
|
* Two-phase auth guard set returned by `AuthGuardResolver`.
|
|
28
32
|
*
|
|
@@ -105,12 +109,20 @@ export interface RouteContext {
|
|
|
105
109
|
}
|
|
106
110
|
/**
|
|
107
111
|
* Route handler function — receives the Hono context and a `RouteContext`
|
|
108
|
-
* with per-request deps (db, pending_effects)
|
|
112
|
+
* with per-request deps (db, pending_effects), and returns a result
|
|
113
|
+
* (`route_ok`, `route_fail`, `route_raw` in `http/route_result.ts`) that
|
|
114
|
+
* `apply_route_specs` turns into the response.
|
|
115
|
+
*
|
|
116
|
+
* The type a `RouteSpec` stores, any result: `define_route` and
|
|
117
|
+
* `attach_route_handler` check a handler against its route
|
|
118
|
+
* (`TypedRouteHandler`) before it is stored.
|
|
109
119
|
*
|
|
110
120
|
* TypeScript allows fewer params, so handlers that don't need `route`
|
|
111
121
|
* can use `(c) => ...` without changes.
|
|
112
122
|
*/
|
|
113
|
-
export type RouteHandler = (c: Context, route: RouteContext) =>
|
|
123
|
+
export type RouteHandler = (c: Context, route: RouteContext) => RouteResult | Promise<RouteResult>;
|
|
124
|
+
/** Marks a `RouteSpec` made by `define_route` or `attach_route_handler`; no runtime property. */
|
|
125
|
+
declare const ROUTE_SPEC_BRAND: unique symbol;
|
|
114
126
|
/**
|
|
115
127
|
* A single route definition — the unit of the surface map.
|
|
116
128
|
*
|
|
@@ -123,6 +135,7 @@ export interface RouteSpec {
|
|
|
123
135
|
method: RouteMethod;
|
|
124
136
|
path: string;
|
|
125
137
|
auth: RouteAuth;
|
|
138
|
+
/** Returns a result the framework answers with; see `RouteHandler`. */
|
|
126
139
|
handler: RouteHandler;
|
|
127
140
|
description: string;
|
|
128
141
|
/**
|
|
@@ -232,7 +245,96 @@ export interface RouteSpec {
|
|
|
232
245
|
* that manages its own transaction like signup).
|
|
233
246
|
*/
|
|
234
247
|
transaction?: boolean;
|
|
248
|
+
/**
|
|
249
|
+
* Present only in the types: a spec whose handler was checked against its
|
|
250
|
+
* route, which only `define_route` and `attach_route_handler` make. A spread
|
|
251
|
+
* of a spec keeps it, and is not checked again.
|
|
252
|
+
*/
|
|
253
|
+
readonly [ROUTE_SPEC_BRAND]: true;
|
|
235
254
|
}
|
|
255
|
+
/** The catch layers' answers, which a handler may also give on any route (`ALWAYS_ACCEPTED_ERRORS`). */
|
|
256
|
+
export type RouteAlwaysErrors = typeof REST_INTERNAL_ERROR | typeof REST_SERVICE_UNAVAILABLE;
|
|
257
|
+
/**
|
|
258
|
+
* The derived definitions a handler may itself answer with, read off its
|
|
259
|
+
* route's `auth` and `rate_limit` types: the 401 and the auth 403s (a handler
|
|
260
|
+
* that re-checks admission) and the 429 of a rate-limited route. The input
|
|
261
|
+
* 400s, the actor codes, and the 415 are the pipeline's alone, and left out.
|
|
262
|
+
*/
|
|
263
|
+
export type RouteHandlerDerivedErrors<TAuth extends RouteAuth, TRateLimit> = (TAuth['account'] extends 'required' ? typeof REST_AUTHENTICATION_REQUIRED : TAuth['actor'] extends 'required' ? typeof REST_AUTHENTICATION_REQUIRED : never) | (TAuth['required_scope'] extends string ? typeof REST_TOKEN_SCOPE_REQUIRED : never) | (TAuth['credential_types'] extends ReadonlyArray<string> ? typeof REST_CREDENTIAL_TYPE_REQUIRED : never) | (TAuth['roles'] extends ReadonlyArray<string> ? typeof REST_INSUFFICIENT_PERMISSIONS : never) | (TRateLimit extends RateLimitKey ? typeof REST_RATE_LIMIT_EXCEEDED : never);
|
|
264
|
+
/**
|
|
265
|
+
* What a handler of a route may return: a success typed by the input side of
|
|
266
|
+
* its `output`, a raw response, or a failure of a definition the route
|
|
267
|
+
* allows — its `errors`, the derived ones a handler may give
|
|
268
|
+
* (`RouteHandlerDerivedErrors`), and the catch layers' (`RouteAlwaysErrors`).
|
|
269
|
+
*/
|
|
270
|
+
export type RouteHandlerResult<TErrors extends ReadonlyArray<RestErrorDef>, TOutput extends ValidationSchema, TAuth extends RouteAuth, TRateLimit> = RouteOk<ReadonlyJson<ValidationInput<TOutput>>> | RouteRaw | RouteFailOf<TErrors[number] | RouteAlwaysErrors | RouteHandlerDerivedErrors<TAuth, TRateLimit>>;
|
|
271
|
+
/** A route handler checked against its route; see `RouteHandlerResult`. */
|
|
272
|
+
export type TypedRouteHandler<TErrors extends ReadonlyArray<RestErrorDef>, TOutput extends ValidationSchema, TAuth extends RouteAuth, TRateLimit> = (c: Context, route: RouteContext) => RouteHandlerResult<TErrors, TOutput, TAuth, TRateLimit> | Promise<RouteHandlerResult<TErrors, TOutput, TAuth, TRateLimit>>;
|
|
273
|
+
/** The fields of a route that carry no type parameter. */
|
|
274
|
+
type RouteShapePlain = Omit<RouteSpec, 'handler' | 'errors' | 'output' | 'auth' | 'rate_limit' | typeof ROUTE_SPEC_BRAND>;
|
|
275
|
+
/**
|
|
276
|
+
* A route minus its handler, with the types its handler is checked against
|
|
277
|
+
* kept: what `define_route_shape` returns and `attach_route_handler` takes,
|
|
278
|
+
* the hono-free half of a route a cross-process suite builds its surface from.
|
|
279
|
+
*/
|
|
280
|
+
export type RouteShape<TErrors extends ReadonlyArray<RestErrorDef> = ReadonlyArray<RestErrorDef>, TOutput extends ValidationSchema = ValidationSchema, TAuth extends RouteAuth = RouteAuth, TRateLimit extends RateLimitKey | undefined = RateLimitKey | undefined> = RouteShapePlain & {
|
|
281
|
+
auth: TAuth;
|
|
282
|
+
output: TOutput;
|
|
283
|
+
errors?: TErrors;
|
|
284
|
+
rate_limit?: TRateLimit;
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* Refuses an `errors` list whose definitions were erased to `RestErrorDef`
|
|
288
|
+
* (a code typed `string`), which would allow a handler any failure.
|
|
289
|
+
*/
|
|
290
|
+
export type KnownRouteErrors<TErrors extends ReadonlyArray<RestErrorDef>> = string extends TErrors[number]['code'] ? ReadonlyArray<never> : TErrors;
|
|
291
|
+
/**
|
|
292
|
+
* Define a route: its handler is checked against the route's `output`,
|
|
293
|
+
* `errors`, `auth`, and `rate_limit` (`TypedRouteHandler`), and the spec is
|
|
294
|
+
* returned as it is, typed as a `RouteSpec`.
|
|
295
|
+
*
|
|
296
|
+
* A literal `errors` list is read as given; a list typed
|
|
297
|
+
* `Array<RestErrorDef>` is refused (`KnownRouteErrors`).
|
|
298
|
+
*
|
|
299
|
+
* @param spec - the route, its handler returning `route_ok`, `route_fail`, or `route_raw`
|
|
300
|
+
* @returns `spec`
|
|
301
|
+
*/
|
|
302
|
+
export declare const define_route: <const TErrors extends ReadonlyArray<RestErrorDef> = [], TOutput extends ValidationSchema = ValidationSchema, const TAuth extends RouteAuth = RouteAuth, TRateLimit extends RateLimitKey | undefined = undefined>(spec: RouteShape<TErrors, TOutput, TAuth, TRateLimit> & {
|
|
303
|
+
errors?: KnownRouteErrors<TErrors>;
|
|
304
|
+
handler: TypedRouteHandler<TErrors, TOutput, TAuth, TRateLimit>;
|
|
305
|
+
}) => RouteSpec;
|
|
306
|
+
/**
|
|
307
|
+
* Define a route minus its handler, keeping the types `attach_route_handler`
|
|
308
|
+
* checks its handler against.
|
|
309
|
+
*
|
|
310
|
+
* @param shape - the route without `handler`
|
|
311
|
+
* @returns `shape`
|
|
312
|
+
*/
|
|
313
|
+
export declare const define_route_shape: <const TErrors extends ReadonlyArray<RestErrorDef> = [], TOutput extends ValidationSchema = ValidationSchema, const TAuth extends RouteAuth = RouteAuth, TRateLimit extends RateLimitKey | undefined = undefined>(shape: RouteShape<TErrors, TOutput, TAuth, TRateLimit> & {
|
|
314
|
+
errors?: KnownRouteErrors<TErrors>;
|
|
315
|
+
}) => RouteShape<TErrors, TOutput, TAuth, TRateLimit>;
|
|
316
|
+
/**
|
|
317
|
+
* Attach a handler to a route shape (`define_route_shape`), checked as
|
|
318
|
+
* `define_route` checks one.
|
|
319
|
+
*
|
|
320
|
+
* @param shape - the route without `handler`
|
|
321
|
+
* @param handler - the handler, returning `route_ok`, `route_fail`, or `route_raw`
|
|
322
|
+
* @returns a new spec, `shape` with `handler`
|
|
323
|
+
*/
|
|
324
|
+
export declare const attach_route_handler: <TErrors extends ReadonlyArray<RestErrorDef>, TOutput extends ValidationSchema, TAuth extends RouteAuth, TRateLimit extends RateLimitKey | undefined>(shape: RouteShape<TErrors, TOutput, TAuth, TRateLimit> & {
|
|
325
|
+
errors?: KnownRouteErrors<TErrors>;
|
|
326
|
+
}, handler: TypedRouteHandler<TErrors, TOutput, TAuth, TRateLimit>) => RouteSpec;
|
|
327
|
+
/**
|
|
328
|
+
* Build the response for a handler's result: a success as JSON at 200, a
|
|
329
|
+
* failure as its body at its definition's status with the headers the
|
|
330
|
+
* definition derives (`Retry-After`), a raw result as it is. Headers and
|
|
331
|
+
* cookies set on `c` before carry over, as Hono builds the response from `c`.
|
|
332
|
+
*
|
|
333
|
+
* @param c - the Hono context
|
|
334
|
+
* @param result - the handler's result
|
|
335
|
+
* @throws TypeError when `result` is not a result `route_ok`, `route_fail`, or `route_raw` made: a `Response`, a success without a value, a failure without its status or definition, a raw result without a response, `undefined` or `null`
|
|
336
|
+
*/
|
|
337
|
+
export declare const to_route_response: (c: Context, result: RouteResult) => Response;
|
|
236
338
|
/**
|
|
237
339
|
* Get validated input from the Hono context.
|
|
238
340
|
*
|
|
@@ -283,7 +385,8 @@ export declare const apply_middleware_specs: (app: Hono, specs: Array<Middleware
|
|
|
283
385
|
* adds input validation middleware (for routes with non-null input schemas),
|
|
284
386
|
* runs the optional authorization phase to resolve the acting actor + build
|
|
285
387
|
* the request context, wraps handler with DEV-only output
|
|
286
|
-
* validation,
|
|
388
|
+
* validation, builds each response from its handler's result
|
|
389
|
+
* (`to_route_response`), wraps with error catch layer (a thrown JSON-RPC error,
|
|
287
390
|
* recognized via `to_thrown_jsonrpc_error`, maps to its status; any other
|
|
288
391
|
* throw to a 500), and registers the route.
|
|
289
392
|
*
|
|
@@ -338,4 +441,5 @@ export declare const apply_route_specs: (app: Hono, specs: Array<RouteSpec>, res
|
|
|
338
441
|
* @returns a new array — the input specs are not mutated
|
|
339
442
|
*/
|
|
340
443
|
export declare const prefix_route_specs: (prefix: string, specs: Array<RouteSpec>) => Array<RouteSpec>;
|
|
444
|
+
export {};
|
|
341
445
|
//# sourceMappingURL=route_spec.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route_spec.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/route_spec.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"route_spec.d.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I,EAAE,OAAO,KAAK,CAAC,cAAc,CAAC,KAAG,IAIhF,CAAC;AAsGF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,eAAO,MAAM,iBAAiB,GAC7B,KAAK,IAAI,EACT,OAAO,KAAK,CAAC,SAAS,CAAC,EACvB,qBAAqB,iBAAiB,EACtC,KAAK,MAAM,EACX,IAAI,EAAE,EACN,YAAY,oBAAoB,KAC9B,IA+EF,CAAC;AAmCF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,MAAM,EAAE,OAAO,KAAK,CAAC,SAAS,CAAC,KAAG,KAAK,CAAC,SAAS,CAK3F,CAAC"}
|
package/dist/http/route_spec.js
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Routes are defined as data (method, path, auth, input/output schemas, handler),
|
|
5
5
|
* then applied to Hono. The attack surface is generated from the specs —
|
|
6
|
-
* always accurate, always complete.
|
|
6
|
+
* always accurate, always complete. A handler returns a result
|
|
7
|
+
* (`http/route_result.ts`) and `apply_route_specs` builds the response; a spec
|
|
8
|
+
* is made by `define_route` (or `define_route_shape` + `attach_route_handler`),
|
|
9
|
+
* which checks the handler's results against the route.
|
|
7
10
|
*
|
|
8
11
|
* Input/output schemas align with SAES `ActionSpec` conventions, and each is
|
|
9
12
|
* a Zod or `@fuzdev/fuz_schema` schema (`ValidationSchema`):
|
|
@@ -26,6 +29,66 @@ import { is_json_response } from './route_error_check.js';
|
|
|
26
29
|
import { dispatch_with_post_commit_rollback } from './pending_effects.js';
|
|
27
30
|
import { assert_route_auth_acting_biconditional, assert_route_auth_invariants } from './auth_shape.js';
|
|
28
31
|
import { assert_validation_schema, parse_any, to_wire_issues } from '../schema_issues.js';
|
|
32
|
+
/**
|
|
33
|
+
* Define a route: its handler is checked against the route's `output`,
|
|
34
|
+
* `errors`, `auth`, and `rate_limit` (`TypedRouteHandler`), and the spec is
|
|
35
|
+
* returned as it is, typed as a `RouteSpec`.
|
|
36
|
+
*
|
|
37
|
+
* A literal `errors` list is read as given; a list typed
|
|
38
|
+
* `Array<RestErrorDef>` is refused (`KnownRouteErrors`).
|
|
39
|
+
*
|
|
40
|
+
* @param spec - the route, its handler returning `route_ok`, `route_fail`, or `route_raw`
|
|
41
|
+
* @returns `spec`
|
|
42
|
+
*/
|
|
43
|
+
// @__NO_SIDE_EFFECTS__
|
|
44
|
+
export const define_route = (spec) => spec;
|
|
45
|
+
/**
|
|
46
|
+
* Define a route minus its handler, keeping the types `attach_route_handler`
|
|
47
|
+
* checks its handler against.
|
|
48
|
+
*
|
|
49
|
+
* @param shape - the route without `handler`
|
|
50
|
+
* @returns `shape`
|
|
51
|
+
*/
|
|
52
|
+
// @__NO_SIDE_EFFECTS__
|
|
53
|
+
export const define_route_shape = (shape) => shape;
|
|
54
|
+
/**
|
|
55
|
+
* Attach a handler to a route shape (`define_route_shape`), checked as
|
|
56
|
+
* `define_route` checks one.
|
|
57
|
+
*
|
|
58
|
+
* @param shape - the route without `handler`
|
|
59
|
+
* @param handler - the handler, returning `route_ok`, `route_fail`, or `route_raw`
|
|
60
|
+
* @returns a new spec, `shape` with `handler`
|
|
61
|
+
*/
|
|
62
|
+
// @__NO_SIDE_EFFECTS__
|
|
63
|
+
export const attach_route_handler = (shape, handler) => ({ ...shape, handler });
|
|
64
|
+
/**
|
|
65
|
+
* Build the response for a handler's result: a success as JSON at 200, a
|
|
66
|
+
* failure as its body at its definition's status with the headers the
|
|
67
|
+
* definition derives (`Retry-After`), a raw result as it is. Headers and
|
|
68
|
+
* cookies set on `c` before carry over, as Hono builds the response from `c`.
|
|
69
|
+
*
|
|
70
|
+
* @param c - the Hono context
|
|
71
|
+
* @param result - the handler's result
|
|
72
|
+
* @throws TypeError when `result` is not a result `route_ok`, `route_fail`, or `route_raw` made: a `Response`, a success without a value, a failure without its status or definition, a raw result without a response, `undefined` or `null`
|
|
73
|
+
*/
|
|
74
|
+
export const to_route_response = (c, result) => {
|
|
75
|
+
// each branch checks the field it reads, so a malformed or nullish result
|
|
76
|
+
// falls through to the throw — a `Response` has its own `ok` and `status`,
|
|
77
|
+
// but no `value` or `def`
|
|
78
|
+
if (result?.ok === true) {
|
|
79
|
+
if (result.value !== undefined)
|
|
80
|
+
return c.json(result.value);
|
|
81
|
+
}
|
|
82
|
+
else if (result?.ok === 'raw') {
|
|
83
|
+
if (result.response instanceof Response)
|
|
84
|
+
return result.response;
|
|
85
|
+
}
|
|
86
|
+
else if (!result?.ok && result?.def !== undefined && typeof result.status === 'number') {
|
|
87
|
+
const body = result.body;
|
|
88
|
+
return c.json(body, result.status, result.def.headers?.(body));
|
|
89
|
+
}
|
|
90
|
+
throw new TypeError(`${c.req.method} ${c.req.path}: a route handler returned ${result instanceof Response ? 'a Response' : 'a malformed result'} — return \`route_ok\`, \`route_fail\`, or \`route_raw\``);
|
|
91
|
+
};
|
|
29
92
|
export function get_route_input(c, _schema) {
|
|
30
93
|
return c.get('validated_input');
|
|
31
94
|
}
|
|
@@ -213,13 +276,13 @@ export const apply_middleware_specs = (app, specs) => {
|
|
|
213
276
|
* spine's `db_unavailable_response`.
|
|
214
277
|
* Catches anything else, a foreign error class carrying a JSON-RPC `code`
|
|
215
278
|
* included, and maps to `{error: 'internal_error', message?}`
|
|
216
|
-
* at 500 (`message` populated only in DEV).
|
|
217
|
-
*
|
|
218
|
-
* something is thrown.
|
|
279
|
+
* at 500 (`message` populated only in DEV). A handler's returned result is
|
|
280
|
+
* unaffected — the catch layer only activates when something is thrown.
|
|
219
281
|
*
|
|
220
|
-
* The flat shape matches what middleware and
|
|
221
|
-
*
|
|
222
|
-
* `c.json(failure.body, status)` from the dispatcher's authorization
|
|
282
|
+
* The flat shape matches what middleware and handler failures produce
|
|
283
|
+
* (`c.json({error: ERROR_FOO}, status)` in a middleware, `route_fail(def)` in a
|
|
284
|
+
* handler, `c.json(failure.body, status)` from the dispatcher's authorization
|
|
285
|
+
* phase),
|
|
223
286
|
* so REST callers see one error envelope across every emit site. The
|
|
224
287
|
* `<reason>` string comes from `err.data.reason` when set (consumer-supplied
|
|
225
288
|
* canonical reason code) or from `jsonrpc_error_code_to_name(err.code)`
|
|
@@ -305,7 +368,8 @@ const build_rest_error_body = (err) => {
|
|
|
305
368
|
* adds input validation middleware (for routes with non-null input schemas),
|
|
306
369
|
* runs the optional authorization phase to resolve the acting actor + build
|
|
307
370
|
* the request context, wraps handler with DEV-only output
|
|
308
|
-
* validation,
|
|
371
|
+
* validation, builds each response from its handler's result
|
|
372
|
+
* (`to_route_response`), wraps with error catch layer (a thrown JSON-RPC error,
|
|
309
373
|
* recognized via `to_thrown_jsonrpc_error`, maps to its status; any other
|
|
310
374
|
* throw to a 500), and registers the route.
|
|
311
375
|
*
|
|
@@ -380,7 +444,8 @@ export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, auth
|
|
|
380
444
|
}
|
|
381
445
|
]
|
|
382
446
|
: [];
|
|
383
|
-
// Step 1: adapt RouteHandler → Handler (construct RouteContext, call
|
|
447
|
+
// Step 1: adapt RouteHandler → Handler (construct RouteContext, call
|
|
448
|
+
// spec.handler, build the response from its result)
|
|
384
449
|
const use_transaction = spec.transaction ?? spec.method !== 'GET';
|
|
385
450
|
const inner = spec.handler;
|
|
386
451
|
// Discard post-commit effects a handler queued (`emit_after_commit`) if it
|
|
@@ -388,15 +453,18 @@ export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, auth
|
|
|
388
453
|
// those effects would announce state that never committed. The eager
|
|
389
454
|
// `pending_effects` queue (attempt audits, run outside the transaction) is
|
|
390
455
|
// untouched. Shared with the action dispatcher (actions/perform_action.ts)
|
|
391
|
-
// via `dispatch_with_post_commit_rollback`.
|
|
392
|
-
|
|
456
|
+
// via `dispatch_with_post_commit_rollback`. The response is built inside
|
|
457
|
+
// the dispatch, so a result that can't become one (a bare `Response`, a
|
|
458
|
+
// malformed result, a body `JSON.stringify` refuses) throws before the
|
|
459
|
+
// commit and rolls back.
|
|
460
|
+
let handler = async (c) => {
|
|
393
461
|
const route_context = {
|
|
394
462
|
pending_effects: c.var.pending_effects,
|
|
395
463
|
post_commit_effects: c.var.post_commit_effects
|
|
396
464
|
};
|
|
397
|
-
return dispatch_with_post_commit_rollback(c.var.post_commit_effects, () => use_transaction
|
|
398
|
-
? db.transaction(async (tx) => inner(c, { db: tx, ...route_context }))
|
|
399
|
-
: inner(c, { db, ...route_context }));
|
|
465
|
+
return dispatch_with_post_commit_rollback(c.var.post_commit_effects, async () => use_transaction
|
|
466
|
+
? db.transaction(async (tx) => to_route_response(c, await inner(c, { db: tx, ...route_context })))
|
|
467
|
+
: to_route_response(c, await inner(c, { db, ...route_context })));
|
|
400
468
|
};
|
|
401
469
|
// Step 2: error catch layer — a thrown error becomes its REST body here
|
|
402
470
|
handler = wrap_error_catch(handler, log);
|
|
@@ -222,9 +222,23 @@ export declare const middleware_applies: (mw_path: string, route_path: string) =
|
|
|
222
222
|
* @param b - the other
|
|
223
223
|
*/
|
|
224
224
|
export declare const middleware_paths_overlap: (a: string, b: string) => boolean;
|
|
225
|
+
/**
|
|
226
|
+
* Check if a middleware layer carves a route path out: one of its
|
|
227
|
+
* `carve_outs` applies to it (`middleware_applies`), whether or not its own
|
|
228
|
+
* `path` does.
|
|
229
|
+
*
|
|
230
|
+
* @param spec - the layer's optional `carve_outs`
|
|
231
|
+
* @param route_path - the route path to check
|
|
232
|
+
*/
|
|
233
|
+
export declare const middleware_carves_out: (spec: {
|
|
234
|
+
readonly carve_outs?: ReadonlyArray<{
|
|
235
|
+
readonly path: string;
|
|
236
|
+
}> | null;
|
|
237
|
+
}, route_path: string) => boolean;
|
|
225
238
|
/**
|
|
226
239
|
* Check if a middleware layer runs on a route path: its `path` pattern
|
|
227
|
-
* applies (`middleware_applies`) and none of its `carve_outs` does
|
|
240
|
+
* applies (`middleware_applies`) and none of its `carve_outs` does
|
|
241
|
+
* (`middleware_carves_out`).
|
|
228
242
|
*
|
|
229
243
|
* @param spec - the layer's `path` and optional `carve_outs`
|
|
230
244
|
* @param route_path - the route path to check
|
|
@@ -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,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;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,OAAO,KAAG,OAavD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,eAAO,MAAM,gCAAgC,GAAI,CAAC,SAAS,gBAAgB,EAC1E,QAAQ,CAAC,EACT,OAAO,OAAO,KACZ,cAAc,CAAC,gBAAgB,CAAC,CAAC,CAAC,CA0BpC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,IAAI,CAAC;AA8FxC;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,uBAAuB,GAAI,QAAQ,gBAAgB,KAAG,OAGW,CAAC;AAc/E,sCAAsC;AACtC,MAAM,WAAW,sBAAsB;IACtC;;;;;;OAMG;IACH,EAAE,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;IACxB,wEAAwE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E,aAAa,CAAC;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,GAAG,IAAI,CAAC;CACtE,EACD,YAAY,MAAM,KAChB,OAAgG,CAAC"}
|
|
@@ -447,12 +447,21 @@ export const middleware_paths_overlap = (a, b) => {
|
|
|
447
447
|
const base = (pattern) => pattern.endsWith('/*') ? pattern.slice(0, -2) : pattern;
|
|
448
448
|
return middleware_applies(a, base(b)) || middleware_applies(b, base(a));
|
|
449
449
|
};
|
|
450
|
+
/**
|
|
451
|
+
* Check if a middleware layer carves a route path out: one of its
|
|
452
|
+
* `carve_outs` applies to it (`middleware_applies`), whether or not its own
|
|
453
|
+
* `path` does.
|
|
454
|
+
*
|
|
455
|
+
* @param spec - the layer's optional `carve_outs`
|
|
456
|
+
* @param route_path - the route path to check
|
|
457
|
+
*/
|
|
458
|
+
export const middleware_carves_out = (spec, route_path) => (spec.carve_outs ?? []).some((carve_out) => middleware_applies(carve_out.path, route_path));
|
|
450
459
|
/**
|
|
451
460
|
* Check if a middleware layer runs on a route path: its `path` pattern
|
|
452
|
-
* applies (`middleware_applies`) and none of its `carve_outs` does
|
|
461
|
+
* applies (`middleware_applies`) and none of its `carve_outs` does
|
|
462
|
+
* (`middleware_carves_out`).
|
|
453
463
|
*
|
|
454
464
|
* @param spec - the layer's `path` and optional `carve_outs`
|
|
455
465
|
* @param route_path - the route path to check
|
|
456
466
|
*/
|
|
457
|
-
export const middleware_spec_applies = (spec, route_path) => middleware_applies(spec.path, route_path) &&
|
|
458
|
-
!(spec.carve_outs ?? []).some((carve_out) => middleware_applies(carve_out.path, route_path));
|
|
467
|
+
export const middleware_spec_applies = (spec, route_path) => middleware_applies(spec.path, route_path) && !middleware_carves_out(spec, route_path);
|
package/dist/rate_limiter.d.ts
CHANGED
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
*
|
|
24
24
|
* @module
|
|
25
25
|
*/
|
|
26
|
-
import type { Context } from 'hono';
|
|
27
26
|
import { type Infer } from '@fuzdev/fuz_schema/schema.ts';
|
|
28
27
|
/**
|
|
29
28
|
* Rate limit key type — declares what a route or RPC action's rate limiter
|
|
@@ -395,12 +394,4 @@ export declare const reserve_optional: (limiter: RateLimiter | null, key: string
|
|
|
395
394
|
* @param options - override individual options; unset fields use `default_login_ip_rate_limit`
|
|
396
395
|
*/
|
|
397
396
|
export declare const create_rate_limiter: (options?: Partial<RateLimiterOptions>) => RateLimiter;
|
|
398
|
-
/**
|
|
399
|
-
* Build a 429 rate-limit-exceeded JSON response with `Retry-After` header.
|
|
400
|
-
*
|
|
401
|
-
* @param c - Hono context
|
|
402
|
-
* @param retry_after - seconds until the client should retry
|
|
403
|
-
* @returns a 429 Response
|
|
404
|
-
*/
|
|
405
|
-
export declare const rate_limit_exceeded_response: (c: Context, retry_after: number) => Response;
|
|
406
397
|
//# sourceMappingURL=rate_limiter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate_limiter.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/rate_limiter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;
|
|
1
|
+
{"version":3,"file":"rate_limiter.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/rate_limiter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,OAAO,EAAU,KAAK,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAIlE;;;;;;;;;GASG;AACH,eAAO,MAAM,YAAY,mJAAoC,CAAC;AAC9D,MAAM,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,YAAY,CAAC,CAAC;AAEtD;;;;;;GAMG;AACH,eAAO,MAAM,6BAA6B,SAAU,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAClC;;;;OAIG;IACH,YAAY,EAAE,MAAM,CAAC;IACrB,mEAAmE;IACnE,SAAS,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,mB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;AACH,eAAO,MAAM,gBAAgB,GAC5B,SAAS,WAAW,GAAG,IAAI,EAC3B,KAAK,MAAM,GAAG,IAAI,EAClB,UAAU,uBAAuB,KAC/B,OAAO,CAAC,sBAAsB,CAGhC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,OAAO,CAAC,kBAAkB,CAAC,KAAG,WAE3E,CAAC"}
|
package/dist/rate_limiter.js
CHANGED
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
*/
|
|
26
26
|
import { LruMap } from '@fuzdev/fuz_util/lru_map.ts';
|
|
27
27
|
import { one_of } from '@fuzdev/fuz_schema/schema.ts';
|
|
28
|
-
import { REST_RATE_LIMIT_EXCEEDED } from './http/error_schemas.js';
|
|
29
28
|
import { PERIODIC_INTERVAL_MS_MAX } from './periodic.js';
|
|
30
29
|
/**
|
|
31
30
|
* Rate limit key type — declares what a route or RPC action's rate limiter
|
|
@@ -598,17 +597,3 @@ export const reserve_optional = async (limiter, key, options) => {
|
|
|
598
597
|
export const create_rate_limiter = (options) => {
|
|
599
598
|
return new RateLimiter({ ...default_login_ip_rate_limit, ...options });
|
|
600
599
|
};
|
|
601
|
-
/**
|
|
602
|
-
* Build a 429 rate-limit-exceeded JSON response with `Retry-After` header.
|
|
603
|
-
*
|
|
604
|
-
* @param c - Hono context
|
|
605
|
-
* @param retry_after - seconds until the client should retry
|
|
606
|
-
* @returns a 429 Response
|
|
607
|
-
*/
|
|
608
|
-
export const rate_limit_exceeded_response = (c, retry_after) => {
|
|
609
|
-
const body = { error: REST_RATE_LIMIT_EXCEEDED.code, retry_after };
|
|
610
|
-
return c.json(body, {
|
|
611
|
-
status: REST_RATE_LIMIT_EXCEEDED.status,
|
|
612
|
-
headers: REST_RATE_LIMIT_EXCEEDED.headers(body)
|
|
613
|
-
});
|
|
614
|
-
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serve_fact_route.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/server/serve_fact_route.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmIG;AAMH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AActD,OAAO,
|
|
1
|
+
{"version":3,"file":"serve_fact_route.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/server/serve_fact_route.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmIG;AAMH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AActD,OAAO,EAIN,KAAK,SAAS,EACd,MAAM,uBAAuB,CAAC;AAU/B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAkBjD,MAAM,WAAW,+BAA+B;IAC/C,kFAAkF;IAClF,SAAS,EAAE,MAAM,CAAC;IAClB;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,GAAG,EAAE,MAAM,CAAC;CACZ;AAsJD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,iCAAiC,GAC7C,SAAS,+BAA+B,KACtC,SAyDF,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,4BAA4B,GACxC,SAAS,+BAA+B,KACtC,SA8CF,CAAC"}
|
|
@@ -138,7 +138,9 @@ import { literal, object } from '@fuzdev/fuz_schema/schema.ts';
|
|
|
138
138
|
import { build_request_context, get_public_account_id, get_request_context, has_scoped_role } from '../auth/request_context.js';
|
|
139
139
|
import { ROLE_ADMIN } from '../auth/role_schema.js';
|
|
140
140
|
import { query_active_actors_by_account } from '../auth/account_queries.js';
|
|
141
|
-
import { get_route_params } from '../http/route_spec.js';
|
|
141
|
+
import { define_route, get_route_params } from '../http/route_spec.js';
|
|
142
|
+
import { route_fail, route_raw } from '../http/route_result.js';
|
|
143
|
+
import { REST_INTERNAL_ERROR } from '../http/error_schemas.js';
|
|
142
144
|
import { query_get_fact, query_get_fact_meta } from '../db/fact_queries.js';
|
|
143
145
|
import { to_uint8 } from '../db/fact_store.js';
|
|
144
146
|
import { query_cell_get } from '../db/cell_queries.js';
|
|
@@ -179,7 +181,7 @@ const serve_fact_bytes = async (c, route, hash, config) => {
|
|
|
179
181
|
const { facts_dir, x_accel, log } = config;
|
|
180
182
|
const meta = await query_get_fact_meta({ db: route.db }, hash);
|
|
181
183
|
if (!meta) {
|
|
182
|
-
return c.body(null, 404);
|
|
184
|
+
return route_raw(c.body(null, 404));
|
|
183
185
|
}
|
|
184
186
|
const content_type = meta.content_type ?? 'application/octet-stream';
|
|
185
187
|
const size = String(meta.size);
|
|
@@ -189,39 +191,39 @@ const serve_fact_bytes = async (c, route, hash, config) => {
|
|
|
189
191
|
if (!row || row.bytes === null) {
|
|
190
192
|
// Race: meta said embedded but bytes vanished. Treat as not-found.
|
|
191
193
|
log.warn(`serve_fact: embedded bytes missing for ${hash} (meta said embedded, row=${row ? 'present' : 'null'})`);
|
|
192
|
-
return c.body(null, 404);
|
|
194
|
+
return route_raw(c.body(null, 404));
|
|
193
195
|
}
|
|
194
196
|
const bytes = to_uint8(row.bytes);
|
|
195
|
-
return c.body(bytes, 200, {
|
|
197
|
+
return route_raw(c.body(bytes, 200, {
|
|
196
198
|
'Content-Type': content_type,
|
|
197
199
|
'Content-Length': size,
|
|
198
200
|
'Cache-Control': FACT_CACHE_CONTROL,
|
|
199
201
|
...FACT_SECURITY_HEADERS
|
|
200
|
-
});
|
|
202
|
+
}));
|
|
201
203
|
}
|
|
202
204
|
// External — defense-in-depth re-validate the URL before trusting it
|
|
203
205
|
// to address the filesystem: its shape, then that it is this hash's file
|
|
204
206
|
const parsed = parse_file_fact_url(meta.external_url);
|
|
205
207
|
if (!parsed) {
|
|
206
208
|
log.error(`serve_fact: external_url for ${hash} is not a file:<shard>/<rest> URL; answering 404`);
|
|
207
|
-
return c.body(null, 404);
|
|
209
|
+
return route_raw(c.body(null, 404));
|
|
208
210
|
}
|
|
209
211
|
if (!file_fact_url_addresses(parsed, hash)) {
|
|
210
212
|
log.error(`serve_fact: external_url for ${hash} addresses another fact's file; answering 404`);
|
|
211
|
-
return c.body(null, 404);
|
|
213
|
+
return route_raw(c.body(null, 404));
|
|
212
214
|
}
|
|
213
215
|
const { shard, rest } = parsed;
|
|
214
216
|
if (x_accel !== undefined) {
|
|
215
217
|
// Production: hand off to nginx via the validated `internal;` facts location.
|
|
216
218
|
// nginx answers with that location's own `add_header` set, not these
|
|
217
219
|
// headers, which is why the validator requires the fact pair there.
|
|
218
|
-
return c.body(null, 200, {
|
|
220
|
+
return route_raw(c.body(null, 200, {
|
|
219
221
|
'Content-Type': content_type,
|
|
220
222
|
'Content-Length': size,
|
|
221
223
|
'Cache-Control': FACT_CACHE_CONTROL,
|
|
222
224
|
'X-Accel-Redirect': `${x_accel.redirect_prefix}${shard}/${rest}`,
|
|
223
225
|
...FACT_SECURITY_HEADERS
|
|
224
|
-
});
|
|
226
|
+
}));
|
|
225
227
|
}
|
|
226
228
|
// Dev / tests: stream from disk. Open before committing the 200 so a
|
|
227
229
|
// missing file answers 404 and any other open failure a logged 500, as the
|
|
@@ -234,10 +236,10 @@ const serve_fact_bytes = async (c, route, hash, config) => {
|
|
|
234
236
|
catch (err) {
|
|
235
237
|
if (err.code === 'ENOENT') {
|
|
236
238
|
log.warn(`serve_fact: disk file for ${hash} is missing; answering 404`);
|
|
237
|
-
return c.body(null, 404);
|
|
239
|
+
return route_raw(c.body(null, 404));
|
|
238
240
|
}
|
|
239
241
|
log.error(`serve_fact: disk open for ${hash} failed`, err);
|
|
240
|
-
return
|
|
242
|
+
return route_fail(REST_INTERNAL_ERROR);
|
|
241
243
|
}
|
|
242
244
|
const headers = {
|
|
243
245
|
'Content-Type': content_type,
|
|
@@ -249,10 +251,10 @@ const serve_fact_bytes = async (c, route, hash, config) => {
|
|
|
249
251
|
// unread, so a stream built here would hold the handle open until GC
|
|
250
252
|
if (c.req.method === 'HEAD') {
|
|
251
253
|
await handle.close();
|
|
252
|
-
return c.body(null, 200, headers);
|
|
254
|
+
return route_raw(c.body(null, 200, headers));
|
|
253
255
|
}
|
|
254
256
|
const web_stream = Readable.toWeb(handle.createReadStream());
|
|
255
|
-
return c.body(web_stream, 200, headers);
|
|
257
|
+
return route_raw(c.body(web_stream, 200, headers));
|
|
256
258
|
};
|
|
257
259
|
/**
|
|
258
260
|
* Resolve the caller's `RequestContext` on a pure-public route from the
|
|
@@ -297,7 +299,7 @@ const build_public_request_context = async (c, route) => {
|
|
|
297
299
|
export const create_serve_cell_fact_route_spec = (options) => {
|
|
298
300
|
const { facts_dir, x_accel, log } = options;
|
|
299
301
|
const config = { facts_dir, x_accel, log };
|
|
300
|
-
return {
|
|
302
|
+
return define_route({
|
|
301
303
|
method: 'GET',
|
|
302
304
|
path: '/api/cells/:cell_id/facts/:hash',
|
|
303
305
|
auth: { account: 'none', actor: 'none' },
|
|
@@ -312,7 +314,7 @@ export const create_serve_cell_fact_route_spec = (options) => {
|
|
|
312
314
|
// Resolve the named cell. Missing / soft-deleted → 404 (masked).
|
|
313
315
|
const cell = await query_cell_get({ db: route.db }, cell_id);
|
|
314
316
|
if (!cell) {
|
|
315
|
-
return c.body(null, 404);
|
|
317
|
+
return route_raw(c.body(null, 404));
|
|
316
318
|
}
|
|
317
319
|
// Per-reference view check — scoped to *this* cell only. It runs
|
|
318
320
|
// before the edge check, so a cell the caller can't view does the
|
|
@@ -332,18 +334,18 @@ export const create_serve_cell_fact_route_spec = (options) => {
|
|
|
332
334
|
if (!can_view_cell(req_ctx, cell, grants) &&
|
|
333
335
|
!(await cell_steward_admits({ db: route.db }, req_ctx, cell))) {
|
|
334
336
|
// 404 (not 403) so an unviewable cell→fact edge doesn't leak.
|
|
335
|
-
return c.body(null, 404);
|
|
337
|
+
return route_raw(c.body(null, 404));
|
|
336
338
|
}
|
|
337
339
|
// The cell→fact edge: the cell must actually reference the hash.
|
|
338
340
|
// `cell.refs` is auto-derived from `data` on every write, so this is
|
|
339
341
|
// the authoritative "does this cell reference these bytes" check.
|
|
340
342
|
// Missing edge → 404 (masked), never "exists elsewhere".
|
|
341
343
|
if (!cell.refs?.includes(hash)) {
|
|
342
|
-
return c.body(null, 404);
|
|
344
|
+
return route_raw(c.body(null, 404));
|
|
343
345
|
}
|
|
344
346
|
return serve_fact_bytes(c, route, hash, config);
|
|
345
347
|
}
|
|
346
|
-
};
|
|
348
|
+
});
|
|
347
349
|
};
|
|
348
350
|
/**
|
|
349
351
|
* Build the admin-only bare-hash `GET /api/facts/:hash` `RouteSpec`.
|
|
@@ -366,7 +368,7 @@ export const create_serve_cell_fact_route_spec = (options) => {
|
|
|
366
368
|
export const create_serve_fact_route_spec = (options) => {
|
|
367
369
|
const { facts_dir, x_accel, log } = options;
|
|
368
370
|
const config = { facts_dir, x_accel, log };
|
|
369
|
-
return {
|
|
371
|
+
return define_route({
|
|
370
372
|
method: 'GET',
|
|
371
373
|
path: '/api/facts/:hash',
|
|
372
374
|
auth: {
|
|
@@ -399,9 +401,9 @@ export const create_serve_fact_route_spec = (options) => {
|
|
|
399
401
|
// fires only on such a regression, so it fails loud as well.
|
|
400
402
|
if (!has_scoped_role(get_request_context(c), ROLE_ADMIN, null)) {
|
|
401
403
|
log.error('bare-hash fact read reached the handler without a global admin grant — the auth gate did not run');
|
|
402
|
-
return c.body(null, 404);
|
|
404
|
+
return route_raw(c.body(null, 404));
|
|
403
405
|
}
|
|
404
406
|
return serve_fact_bytes(c, route, hash, config);
|
|
405
407
|
}
|
|
406
|
-
};
|
|
408
|
+
});
|
|
407
409
|
};
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
10
|
-
import type
|
|
10
|
+
import { type RouteSpec } from '../http/route_spec.js';
|
|
11
11
|
import type { AppSurface } from '../http/surface.js';
|
|
12
12
|
/** Options for the surface explorer route. */
|
|
13
13
|
export interface SurfaceRouteOptions {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"surface_route.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/server/surface_route.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"surface_route.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/server/surface_route.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EAAgB,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAErE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAIrD,8CAA8C;AAC9C,MAAM,WAAW,mBAAmB;IACnC,2GAA2G;IAC3G,OAAO,EAAE,UAAU,CAAC;CACpB;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,yBAAyB,GAAI,SAAS,mBAAmB,KAAG,SAoBtE,CAAC"}
|