@nifrajs/core 2.1.0 → 2.3.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 -2
- package/dist/assurance.d.ts +48 -4
- package/dist/assurance.d.ts.map +1 -1
- package/dist/assurance.js +87 -9
- package/dist/assurance.js.map +1 -1
- package/dist/binary.d.ts +54 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +90 -0
- package/dist/binary.js.map +1 -0
- package/dist/budget.d.ts +1 -1
- package/dist/budget.js +1 -1
- package/dist/capabilities.d.ts +19 -2
- package/dist/capabilities.d.ts.map +1 -1
- package/dist/capabilities.js +60 -8
- package/dist/capabilities.js.map +1 -1
- package/dist/classification.d.ts +1 -1
- package/dist/classification.js +1 -1
- package/dist/diff.d.ts +6 -6
- package/dist/diff.js +6 -6
- package/dist/durable-adapters.d.ts.map +1 -1
- package/dist/durable-adapters.js +36 -70
- package/dist/durable-adapters.js.map +1 -1
- package/dist/durable-execution.d.ts +2 -2
- package/dist/durable-execution.js +2 -2
- package/dist/idempotency.d.ts +3 -3
- package/dist/idempotency.js +4 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/internal/brand.d.ts +1 -1
- package/dist/internal/brand.js +1 -1
- package/dist/internal/route-catalog.d.ts +58 -0
- package/dist/internal/route-catalog.d.ts.map +1 -0
- package/dist/internal/route-catalog.js +58 -0
- package/dist/internal/route-catalog.js.map +1 -0
- package/dist/internal/route-execution.d.ts +75 -0
- package/dist/internal/route-execution.d.ts.map +1 -0
- package/dist/internal/route-execution.js +2 -0
- package/dist/internal/route-execution.js.map +1 -0
- package/dist/internal/same-origin.d.ts +29 -0
- package/dist/internal/same-origin.d.ts.map +1 -0
- package/dist/internal/same-origin.js +47 -0
- package/dist/internal/same-origin.js.map +1 -0
- package/dist/internal/secret.d.ts +1 -1
- package/dist/internal/secret.js +1 -1
- package/dist/ledger.d.ts +9 -9
- package/dist/ledger.js +5 -5
- package/dist/reflection.d.ts +1 -1
- package/dist/router/pattern.d.ts +26 -1
- package/dist/router/pattern.d.ts.map +1 -1
- package/dist/router/pattern.js +164 -8
- package/dist/router/pattern.js.map +1 -1
- package/dist/router/router.d.ts.map +1 -1
- package/dist/router/router.js +52 -4
- package/dist/router/router.js.map +1 -1
- package/dist/schema/standard.d.ts +2 -2
- package/dist/schema/standard.js +2 -2
- package/dist/seo.d.ts +3 -3
- package/dist/seo.js +4 -4
- package/dist/server/body.d.ts +2 -2
- package/dist/server/body.js +2 -2
- package/dist/server/context.d.ts +18 -18
- package/dist/server/contract.d.ts +7 -7
- package/dist/server/contract.js +3 -3
- package/dist/server/cookies.d.ts +4 -4
- package/dist/server/cookies.js +8 -8
- package/dist/server/edge.d.ts +83 -0
- package/dist/server/edge.d.ts.map +1 -0
- package/dist/server/edge.js +71 -0
- package/dist/server/edge.js.map +1 -0
- package/dist/server/install.d.ts +2 -0
- package/dist/server/install.d.ts.map +1 -1
- package/dist/server/install.js +2 -0
- package/dist/server/install.js.map +1 -1
- package/dist/server/logger.d.ts +7 -7
- package/dist/server/logger.js +4 -4
- package/dist/server/registry.d.ts +5 -5
- package/dist/server/response-contract-lane.d.ts +91 -0
- package/dist/server/response-contract-lane.d.ts.map +1 -0
- package/dist/server/response-contract-lane.js +81 -0
- package/dist/server/response-contract-lane.js.map +1 -0
- package/dist/server/server.d.ts +52 -91
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/server.js +130 -186
- package/dist/server/server.js.map +1 -1
- package/dist/server/sse.d.ts +4 -4
- package/dist/server/sse.js +7 -7
- package/dist/server/webhook.d.ts +1 -1
- package/dist/server/webhook.js +5 -5
- package/dist/server/websocket.d.ts +19 -19
- package/dist/server/websocket.js +9 -9
- package/dist/server/ws-bun.d.ts +4 -4
- package/dist/server/ws-bun.js +3 -3
- package/dist/server.d.ts +3 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +3 -1
- package/dist/server.js.map +1 -1
- package/dist/transport-codec.d.ts +13 -1
- package/dist/transport-codec.d.ts.map +1 -1
- package/dist/transport-codec.js +48 -9
- package/dist/transport-codec.js.map +1 -1
- package/package.json +12 -2
- package/src/assurance.ts +154 -8
- package/src/binary.ts +128 -0
- package/src/budget.ts +1 -1
- package/src/capabilities.ts +71 -9
- package/src/classification.ts +1 -1
- package/src/diff.ts +8 -8
- package/src/durable-adapters.ts +40 -70
- package/src/durable-execution.ts +2 -2
- package/src/idempotency.ts +4 -4
- package/src/index.ts +3 -3
- package/src/internal/brand.ts +1 -1
- package/src/internal/route-catalog.ts +94 -0
- package/src/internal/route-execution.ts +106 -0
- package/src/internal/same-origin.ts +43 -0
- package/src/internal/secret.ts +1 -1
- package/src/ledger.ts +11 -11
- package/src/reflection.ts +1 -1
- package/src/router/pattern.ts +179 -8
- package/src/router/router.ts +80 -6
- package/src/schema/standard.ts +2 -2
- package/src/seo.ts +6 -6
- package/src/server/body.ts +2 -2
- package/src/server/context.ts +18 -18
- package/src/server/contract.ts +13 -13
- package/src/server/cookies.ts +8 -8
- package/src/server/edge.ts +150 -0
- package/src/server/install.ts +5 -0
- package/src/server/logger.ts +8 -8
- package/src/server/registry.ts +5 -5
- package/src/server/response-contract-lane.ts +164 -0
- package/src/server/server.ts +159 -366
- package/src/server/sse.ts +11 -11
- package/src/server/webhook.ts +5 -5
- package/src/server/websocket.ts +22 -22
- package/src/server/ws-bun.ts +4 -4
- package/src/server.ts +8 -5
- package/src/transport-codec.ts +63 -10
package/src/server/logger.ts
CHANGED
|
@@ -35,12 +35,12 @@ const SENSITIVE_KEY_PARTS: ReadonlyArray<string> = [
|
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* Tunes redaction. Key-name redaction always runs; the rest is **opt-in**:
|
|
38
|
-
* - `keyParts`
|
|
39
|
-
* - `valuePatterns`
|
|
38
|
+
* - `keyParts` - extra case-insensitive key fragments, added to the built-in denylist.
|
|
39
|
+
* - `valuePatterns` - regexes matched against string **values** *and* the log message; each match is
|
|
40
40
|
* replaced with the placeholder. This is the value-scanning hook for secrets that land in a value or
|
|
41
|
-
* message (e.g. `err.message`), which key-name redaction can't catch. Off unless provided
|
|
41
|
+
* message (e.g. `err.message`), which key-name redaction can't catch. Off unless provided - the
|
|
42
42
|
* default path does no value scanning, so it stays allocation-light. See {@link commonSecretPatterns}.
|
|
43
|
-
* - `placeholder`
|
|
43
|
+
* - `placeholder` - the replacement string (default `[REDACTED]`).
|
|
44
44
|
*/
|
|
45
45
|
export interface RedactOptions {
|
|
46
46
|
readonly keyParts?: readonly string[]
|
|
@@ -49,7 +49,7 @@ export interface RedactOptions {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
* A conservative, high-signal set of patterns for {@link RedactOptions.valuePatterns}
|
|
52
|
+
* A conservative, high-signal set of patterns for {@link RedactOptions.valuePatterns} - opt in by
|
|
53
53
|
* passing it (or a subset) to `jsonLogger`/`redactLogFields`. Covers bearer tokens, JWTs, emails, and a
|
|
54
54
|
* few well-known key formats (Stripe, GitHub, AWS access-key ids). Chosen to minimize false positives;
|
|
55
55
|
* add your own (e.g. internal id formats) as needed. Every pattern is global so all matches are scrubbed.
|
|
@@ -106,7 +106,7 @@ function redactValue(value: unknown, seen: WeakSet<object>, config: RedactConfig
|
|
|
106
106
|
if (Array.isArray(value)) return value.map((item) => redactValue(item, seen, config))
|
|
107
107
|
const out: Record<string, unknown> = {}
|
|
108
108
|
for (const [key, val] of Object.entries(value)) {
|
|
109
|
-
// A sensitive key wins outright (don't scan it)
|
|
109
|
+
// A sensitive key wins outright (don't scan it) - the whole value is replaced.
|
|
110
110
|
out[key] = isSensitiveKey(key, config.keyParts)
|
|
111
111
|
? config.placeholder
|
|
112
112
|
: redactValue(val, seen, config)
|
|
@@ -129,7 +129,7 @@ function writeToStderr(line: string): void {
|
|
|
129
129
|
|
|
130
130
|
/**
|
|
131
131
|
* The default logger: one redacted JSON object per line. `write` is injectable for tests or
|
|
132
|
-
* alternative sinks (defaults to stderr). `options` tunes redaction
|
|
132
|
+
* alternative sinks (defaults to stderr). `options` tunes redaction - pass `valuePatterns` (e.g.
|
|
133
133
|
* {@link commonSecretPatterns}) to also scrub secrets embedded in values + the message. Framework keys
|
|
134
134
|
* (`level`, `message`, `time`) always win over user fields of the same name.
|
|
135
135
|
*/
|
|
@@ -158,7 +158,7 @@ export function jsonLogger(
|
|
|
158
158
|
|
|
159
159
|
const noop = (): void => undefined
|
|
160
160
|
|
|
161
|
-
/** Discards everything
|
|
161
|
+
/** Discards everything - for tests, or when log output is handled elsewhere. */
|
|
162
162
|
export const silentLogger: Logger = {
|
|
163
163
|
debug: noop,
|
|
164
164
|
info: noop,
|
package/src/server/registry.ts
CHANGED
|
@@ -108,20 +108,20 @@ export type WsRouteInfoFor<
|
|
|
108
108
|
/**
|
|
109
109
|
* Merge a new route into the registry, combining methods that share a path.
|
|
110
110
|
*
|
|
111
|
-
* SCALING CEILING (measured
|
|
112
|
-
* fluent chain hits TS2589 at ~95-100 routes. This intersection is NOT the cause
|
|
111
|
+
* SCALING CEILING (measured - see many-routes.test-d.ts and the isolation study below): a single
|
|
112
|
+
* fluent chain hits TS2589 at ~95-100 routes. This intersection is NOT the cause - in isolation it
|
|
113
113
|
* accumulates 1000+ routes cleanly, and heavy per-route `Params<Path>`/schema inference alone
|
|
114
114
|
* reaches 600+. The wall is an INTERACTION unique to the fluent builder: each `.get(path, handler)`
|
|
115
115
|
* both (a) computes the handler's context type from the path (`c.params.id` inferred from `:id`)
|
|
116
116
|
* AND (b) returns `Server<AddRoute<growing-R, …>, Ctx>`. Neither alone strains the compiler; the
|
|
117
|
-
* PRODUCT
|
|
118
|
-
* N steps
|
|
117
|
+
* PRODUCT - recomputing the handler context while re-threading the ever-larger registry at each of
|
|
118
|
+
* N steps - exhausts TypeScript's per-expression instantiation budget around N≈95. It is therefore
|
|
119
119
|
* O(N) and inherent to any builder that infers handler context AND accumulates a typed route
|
|
120
120
|
* registry (Elysia/tRPC/hono's typed clients cap the same way); it is not fixable by reshaping
|
|
121
121
|
* AddRoute. Past ~90 routes, use a path that DOESN'T form the product: split into domain groups and
|
|
122
122
|
* `.merge()` them (each group is a short chain; a merge is one `R & R2` intersection with no
|
|
123
123
|
* per-call context work), or contract-first `implement()` (the registry is one object type declared
|
|
124
|
-
* upfront
|
|
124
|
+
* upfront - no grow-R-per-call, so no ceiling at all).
|
|
125
125
|
*/
|
|
126
126
|
export type AddRoute<
|
|
127
127
|
R extends Registry,
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Making a route's declared `response` schema mean something at runtime.
|
|
3
|
+
*
|
|
4
|
+
* ## The gap this closes
|
|
5
|
+
*
|
|
6
|
+
* A `response` schema is a LOWER bound, not an upper one. It says "at least these fields, of these
|
|
7
|
+
* types"; it never said "only these". TypeScript does not close the gap either - excess-property
|
|
8
|
+
* checking fires on a fresh object literal in an annotated position, and a handler is neither: it is a
|
|
9
|
+
* contextually-typed argument that usually returns a variable. So this compiles, and ships every field:
|
|
10
|
+
*
|
|
11
|
+
* app.get("/me", { response: PublicUser }, async () => {
|
|
12
|
+
* const user = await db.users.find(id) // { id, name, email, passwordHash, ... }
|
|
13
|
+
* return user // all of it goes on the wire
|
|
14
|
+
* })
|
|
15
|
+
*
|
|
16
|
+
* The client's type says `{ id, name }`, so the leak is invisible from both ends. And it can appear
|
|
17
|
+
* with no code change at all: add a column to `users`, and the next deploy ships it to browsers.
|
|
18
|
+
*
|
|
19
|
+
* ## Why the behaviour follows the schema, not this module
|
|
20
|
+
*
|
|
21
|
+
* Standard Schema exposes exactly one operation - `validate` - and no way to enumerate a schema's
|
|
22
|
+
* declared keys. So a projection of "just the declared fields" cannot be written generically here;
|
|
23
|
+
* the only clean value available is whatever `validate()` returns. That is deliberate on the spec's
|
|
24
|
+
* part, and it means enforcement inherits each validator's own semantics:
|
|
25
|
+
*
|
|
26
|
+
* - a STRIPPING schema (zod, valibot) returns a cleaned value -> the undeclared fields are removed
|
|
27
|
+
* - a STRICT schema (`@nifrajs/schema`'s `t.object`) reports issues -> the response is a violation
|
|
28
|
+
*
|
|
29
|
+
* That is not an inconsistency to paper over: a strict schema has already declared that extra fields
|
|
30
|
+
* are an error, and a stripping one has declared they are ignorable. Enforcement honours what the
|
|
31
|
+
* author wrote rather than overriding it.
|
|
32
|
+
*/
|
|
33
|
+
import type { StandardIssue, StandardResult, StandardSchemaV1 } from "../schema/standard.ts"
|
|
34
|
+
import { INSTALL_RESPONSE_CONTRACT } from "./install.ts"
|
|
35
|
+
import type { IdentityPlugin } from "./plugin.ts"
|
|
36
|
+
import type { AnyServer } from "./server.ts"
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* How hard a declared `response` schema is held.
|
|
40
|
+
*
|
|
41
|
+
* - `warn` - check and log; the response is sent UNCHANGED, so enabling it cannot break anything.
|
|
42
|
+
* - `enforce` - serialize the validated value, so undeclared data cannot reach the wire.
|
|
43
|
+
*
|
|
44
|
+
* There is no `off`: not installing the plugin IS off, and that is what keeps this module out of an
|
|
45
|
+
* app's bundle entirely rather than shipping a disabled branch to everyone.
|
|
46
|
+
*/
|
|
47
|
+
export type ResponseContractMode = "warn" | "enforce"
|
|
48
|
+
|
|
49
|
+
/** The outcome of checking one handler result against its declared response schema. */
|
|
50
|
+
export type ResponseContractOutcome =
|
|
51
|
+
/** Nothing to report. `value` is what should be serialized (the original result, or the cleaned one). */
|
|
52
|
+
| { readonly kind: "ok"; readonly value: unknown }
|
|
53
|
+
/** `warn` only: the payload differed from the contract. The ORIGINAL result is still served. */
|
|
54
|
+
| { readonly kind: "warn"; readonly value: unknown; readonly message: string }
|
|
55
|
+
/** `enforce` only: the payload cannot be reconciled with the contract, so it must not be sent. */
|
|
56
|
+
| { readonly kind: "violation"; readonly message: string }
|
|
57
|
+
|
|
58
|
+
const isPlainObject = (v: unknown): v is Record<string, unknown> =>
|
|
59
|
+
typeof v === "object" && v !== null && !Array.isArray(v)
|
|
60
|
+
|
|
61
|
+
/** Top-level keys present in the handler's result but absent from the validated value. */
|
|
62
|
+
function droppedKeys(result: unknown, value: unknown): string[] {
|
|
63
|
+
if (!isPlainObject(result) || !isPlainObject(value)) return []
|
|
64
|
+
return Object.keys(result).filter((k) => !(k in value))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function describeIssues(issues: ReadonlyArray<StandardIssue>): string {
|
|
68
|
+
return issues
|
|
69
|
+
.map((issue) => {
|
|
70
|
+
const path = Array.isArray(issue.path)
|
|
71
|
+
? issue.path
|
|
72
|
+
.map((seg) =>
|
|
73
|
+
String(typeof seg === "object" && seg !== null ? (seg as { key: unknown }).key : seg),
|
|
74
|
+
)
|
|
75
|
+
.join(".")
|
|
76
|
+
: ""
|
|
77
|
+
return path === "" ? issue.message : `${path}: ${issue.message}`
|
|
78
|
+
})
|
|
79
|
+
.join("; ")
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Check one result against the route's declared response schema.
|
|
84
|
+
*
|
|
85
|
+
* Returns synchronously when the schema does (the common case), so an app that opts in does not pay a
|
|
86
|
+
* microtask per response for a check that had no work to do.
|
|
87
|
+
*/
|
|
88
|
+
export function checkResponseContract(
|
|
89
|
+
schema: StandardSchemaV1,
|
|
90
|
+
result: unknown,
|
|
91
|
+
mode: "warn" | "enforce",
|
|
92
|
+
): ResponseContractOutcome | Promise<ResponseContractOutcome> {
|
|
93
|
+
// A handler may return a raw Response as deliberate control flow (a redirect, a stream). There is no
|
|
94
|
+
// JSON payload to hold to the contract, and re-serializing one would corrupt it.
|
|
95
|
+
if (result instanceof Response || result === undefined) return { kind: "ok", value: result }
|
|
96
|
+
const settled = schema["~standard"].validate(result)
|
|
97
|
+
return settled instanceof Promise
|
|
98
|
+
? settled.then((r) => interpret(r, result, mode))
|
|
99
|
+
: interpret(settled, result, mode)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function interpret(
|
|
103
|
+
settled: StandardResult<unknown>,
|
|
104
|
+
result: unknown,
|
|
105
|
+
mode: "warn" | "enforce",
|
|
106
|
+
): ResponseContractOutcome {
|
|
107
|
+
if (settled.issues !== undefined) {
|
|
108
|
+
const message = `response does not satisfy its declared contract: ${describeIssues(settled.issues)}`
|
|
109
|
+
// `warn` never changes what is served - it reports and gets out of the way, so switching it on can
|
|
110
|
+
// never be the thing that broke production.
|
|
111
|
+
return mode === "warn"
|
|
112
|
+
? { kind: "warn", value: result, message }
|
|
113
|
+
: { kind: "violation", message }
|
|
114
|
+
}
|
|
115
|
+
const dropped = droppedKeys(result, settled.value)
|
|
116
|
+
if (mode === "enforce") return { kind: "ok", value: settled.value }
|
|
117
|
+
return dropped.length === 0
|
|
118
|
+
? { kind: "ok", value: result }
|
|
119
|
+
: {
|
|
120
|
+
kind: "warn",
|
|
121
|
+
value: result,
|
|
122
|
+
message: `response carries fields its contract does not declare: ${dropped.join(", ")}. They are being sent; \`responseContract: "enforce"\` would strip them.`,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* What the server holds when the plugin is installed. The kernel calls `check` through this object and
|
|
128
|
+
* never imports the implementation, so an app that does not install the plugin does not carry it.
|
|
129
|
+
*/
|
|
130
|
+
export interface ResponseContractRuntime {
|
|
131
|
+
readonly mode: ResponseContractMode
|
|
132
|
+
check(
|
|
133
|
+
schema: StandardSchemaV1,
|
|
134
|
+
result: unknown,
|
|
135
|
+
): ResponseContractOutcome | Promise<ResponseContractOutcome>
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
interface ResponseContractInstallable {
|
|
139
|
+
[INSTALL_RESPONSE_CONTRACT](runtime: ResponseContractRuntime): void
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Hold every route's declared `response` schema to what the handler actually returned.
|
|
144
|
+
*
|
|
145
|
+
* app.use(responseContract("enforce"))
|
|
146
|
+
*
|
|
147
|
+
* Install it before the routes it should cover - like `idempotency()`, the decision is made per route
|
|
148
|
+
* at registration, so routes registered earlier are not retroactively covered.
|
|
149
|
+
*
|
|
150
|
+
* A contracted route leaves the fused and native fast paths, because the check needs the handler's
|
|
151
|
+
* value before it becomes bytes. That is the same trade an idempotent route makes, and the reason this
|
|
152
|
+
* is opt-in rather than always on.
|
|
153
|
+
*/
|
|
154
|
+
export function responseContract(mode: ResponseContractMode = "warn"): IdentityPlugin {
|
|
155
|
+
const runtime: ResponseContractRuntime = {
|
|
156
|
+
mode,
|
|
157
|
+
check: (schema, result) => checkResponseContract(schema, result, mode),
|
|
158
|
+
}
|
|
159
|
+
const apply = <S extends AnyServer>(app: S): S => {
|
|
160
|
+
;(app as unknown as ResponseContractInstallable)[INSTALL_RESPONSE_CONTRACT](runtime)
|
|
161
|
+
return app
|
|
162
|
+
}
|
|
163
|
+
return Object.assign(apply, { pluginName: "nifra:response-contract" }) as IdentityPlugin
|
|
164
|
+
}
|