@jini-ai/core 0.7.2 → 0.7.3-next.2
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 +40 -12
- package/dist/admin-runtime/admin-api-error.d.ts +68 -0
- package/dist/admin-runtime/admin-api-error.js +72 -0
- package/dist/admin-runtime/admin-config-error.d.ts +8 -0
- package/dist/admin-runtime/admin-config-error.js +11 -0
- package/dist/admin-runtime/controller-store.d.ts +20 -0
- package/dist/admin-runtime/controller-store.js +32 -0
- package/dist/admin-runtime/index.d.ts +13 -0
- package/dist/admin-runtime/index.js +12 -0
- package/dist/admin-runtime/panel-access.d.ts +88 -0
- package/dist/admin-runtime/panel-access.js +76 -0
- package/dist/admin-runtime/query-suffix.d.ts +5 -0
- package/dist/admin-runtime/query-suffix.js +5 -0
- package/dist/agent-handles/element-handles.d.ts +134 -0
- package/dist/agent-handles/element-handles.js +45 -0
- package/dist/agent-handles/field-descriptor.d.ts +28 -0
- package/dist/agent-handles/field-descriptor.js +3 -0
- package/dist/agent-handles/handle.d.ts +109 -0
- package/dist/agent-handles/handle.js +109 -0
- package/dist/agent-handles/index.d.ts +9 -0
- package/dist/agent-handles/index.js +6 -0
- package/dist/agent-handles/list-handles.d.ts +46 -0
- package/dist/agent-handles/list-handles.js +67 -0
- package/dist/agent-handles/secret-attrs.d.ts +14 -0
- package/dist/agent-handles/secret-attrs.js +13 -0
- package/dist/approval-batch-chain.d.ts +45 -0
- package/dist/approval-batch-chain.js +75 -0
- package/dist/approval.d.ts +33 -14
- package/dist/approval.js +57 -43
- package/dist/batch-tool-inputs.js +24 -20
- package/dist/contracts/admin-module.d.ts +166 -0
- package/dist/contracts/admin-module.js +14 -0
- package/dist/contracts/agents/definition.d.ts +459 -0
- package/dist/contracts/agents/definition.js +2 -0
- package/dist/contracts/agents/index.d.ts +7 -0
- package/dist/contracts/agents/index.js +2 -0
- package/dist/contracts/agents/launch.d.ts +64 -0
- package/dist/contracts/agents/launch.js +2 -0
- package/dist/contracts/agents/model-catalog.d.ts +213 -0
- package/dist/contracts/agents/model-catalog.js +4 -0
- package/dist/contracts/agents/native-tool-policy.d.ts +42 -0
- package/dist/contracts/agents/native-tool-policy.js +2 -0
- package/dist/contracts/ai-trace/capture.d.ts +17 -0
- package/dist/contracts/ai-trace/capture.js +43 -0
- package/dist/contracts/ai-trace/classify.d.ts +12 -0
- package/dist/contracts/ai-trace/classify.js +16 -0
- package/dist/contracts/ai-trace/index.d.ts +13 -0
- package/dist/contracts/ai-trace/index.js +6 -0
- package/dist/contracts/ai-trace/recorder.d.ts +64 -0
- package/dist/contracts/ai-trace/recorder.js +283 -0
- package/dist/contracts/ai-trace/sinks.d.ts +51 -0
- package/dist/contracts/ai-trace/sinks.js +153 -0
- package/dist/contracts/ai-trace/types.d.ts +123 -0
- package/dist/contracts/ai-trace/types.js +17 -0
- package/dist/contracts/ai-trace/usage.d.ts +16 -0
- package/dist/contracts/ai-trace/usage.js +45 -0
- package/dist/contracts/blob.d.ts +31 -0
- package/dist/contracts/blob.js +10 -0
- package/dist/contracts/capabilities.d.ts +95 -0
- package/dist/contracts/capabilities.js +111 -0
- package/dist/contracts/charts.d.ts +75 -0
- package/dist/contracts/charts.js +4 -0
- package/dist/contracts/env.d.ts +96 -0
- package/dist/contracts/env.js +6 -0
- package/dist/contracts/events/change-set.d.ts +106 -0
- package/dist/contracts/events/change-set.js +2 -0
- package/dist/contracts/events/command.d.ts +118 -0
- package/dist/contracts/events/command.js +2 -0
- package/dist/contracts/events/ports.d.ts +172 -0
- package/dist/contracts/events/ports.js +2 -0
- package/dist/contracts/events.d.ts +5 -0
- package/dist/contracts/events.js +2 -0
- package/dist/contracts/forms.d.ts +110 -0
- package/dist/contracts/forms.js +4 -0
- package/dist/contracts/hosted-mcp/config.d.ts +19 -0
- package/dist/contracts/hosted-mcp/record.d.ts +61 -4
- package/dist/contracts/mail/email-sender.d.ts +118 -0
- package/dist/contracts/mail/email-sender.js +20 -0
- package/dist/contracts/mail/smtp-payload.d.ts +32 -0
- package/dist/contracts/mail/smtp-payload.js +2 -0
- package/dist/contracts/mail.d.ts +2 -0
- package/dist/contracts/mail.js +1 -1
- package/dist/contracts/observability.d.ts +235 -0
- package/dist/contracts/observability.js +28 -0
- package/dist/contracts/plugin-sdk.d.ts +186 -0
- package/dist/contracts/plugin-sdk.js +39 -0
- package/dist/contracts/runtime/command.d.ts +41 -0
- package/dist/contracts/runtime/command.js +60 -0
- package/dist/contracts/runtime/environment.d.ts +18 -0
- package/dist/contracts/runtime/environment.js +26 -0
- package/dist/contracts/runtime/file-lock.d.ts +94 -0
- package/dist/contracts/runtime/file-lock.js +25 -0
- package/dist/contracts/runtime/fs.d.ts +33 -0
- package/dist/contracts/runtime/fs.js +2 -0
- package/dist/contracts/runtime/http.d.ts +89 -0
- package/dist/contracts/runtime/http.js +2 -0
- package/dist/contracts/runtime/platform.d.ts +3 -0
- package/dist/contracts/runtime/platform.js +2 -0
- package/dist/contracts/runtime/process.d.ts +40 -0
- package/dist/contracts/runtime/process.js +31 -0
- package/dist/contracts/runtime/reader.d.ts +11 -0
- package/dist/contracts/runtime/reader.js +2 -0
- package/dist/contracts/runtime/terminal.d.ts +99 -0
- package/dist/contracts/runtime/terminal.js +2 -0
- package/dist/contracts/runtime/testing.d.ts +84 -0
- package/dist/contracts/runtime/testing.js +116 -0
- package/dist/contracts/runtime.d.ts +10 -0
- package/dist/contracts/runtime.js +10 -0
- package/dist/contracts/server/api-error.d.ts +43 -0
- package/dist/contracts/server/api-error.js +49 -0
- package/dist/contracts/server/auth.d.ts +20 -0
- package/dist/contracts/server/auth.js +21 -0
- package/dist/contracts/server/conformance.d.ts +25 -0
- package/dist/contracts/server/conformance.js +50 -0
- package/dist/contracts/server/json-route.d.ts +176 -0
- package/dist/contracts/server/json-route.js +155 -0
- package/dist/contracts/server/middleware.d.ts +35 -0
- package/dist/contracts/server/middleware.js +56 -0
- package/dist/contracts/server/origin.d.ts +44 -0
- package/dist/contracts/server/origin.js +32 -0
- package/dist/contracts/server/query.d.ts +16 -0
- package/dist/contracts/server/query.js +69 -0
- package/dist/contracts/server/rate-limit.d.ts +20 -0
- package/dist/contracts/server/rate-limit.js +8 -0
- package/dist/contracts/server/request-body.d.ts +26 -0
- package/dist/contracts/server/request-body.js +82 -0
- package/dist/contracts/server/result.d.ts +17 -0
- package/dist/contracts/server/result.js +5 -0
- package/dist/contracts/server/route-table.d.ts +54 -0
- package/dist/contracts/server/route-table.js +119 -0
- package/dist/contracts/server/same-origin-write.d.ts +18 -0
- package/dist/contracts/server/same-origin-write.js +31 -0
- package/dist/contracts/server/security-headers.d.ts +30 -0
- package/dist/contracts/server/security-headers.js +35 -0
- package/dist/contracts/server/sse.d.ts +82 -0
- package/dist/contracts/server/sse.js +103 -0
- package/dist/contracts/server/testing.d.ts +59 -0
- package/dist/contracts/server/testing.js +68 -0
- package/dist/contracts/server/verified-origin/configured-origin.d.ts +37 -0
- package/dist/contracts/server/verified-origin/configured-origin.js +103 -0
- package/dist/contracts/server/verified-origin/index.d.ts +7 -0
- package/dist/contracts/server/verified-origin/index.js +6 -0
- package/dist/contracts/server/verified-origin/loopback-host.d.ts +9 -0
- package/dist/contracts/server/verified-origin/loopback-host.js +17 -0
- package/dist/contracts/server/verified-origin/origin.d.ts +64 -0
- package/dist/contracts/server/verified-origin/origin.js +162 -0
- package/dist/contracts/server/verified-origin/ports.d.ts +38 -0
- package/dist/contracts/server/verified-origin/ports.js +2 -0
- package/dist/contracts/server/verified-origin/repo.memory.d.ts +32 -0
- package/dist/contracts/server/verified-origin/repo.memory.js +34 -0
- package/dist/contracts/server/verified-origin/site-path.d.ts +113 -0
- package/dist/contracts/server/verified-origin/site-path.js +188 -0
- package/dist/contracts/server/verified-origin/types.d.ts +33 -0
- package/dist/contracts/server/verified-origin/types.js +30 -0
- package/dist/contracts/server.d.ts +46 -2
- package/dist/contracts/server.js +17 -3
- package/dist/contracts/storage/kernel.d.ts +86 -0
- package/dist/contracts/storage/kernel.js +38 -0
- package/dist/contracts/storage/migration.d.ts +32 -0
- package/dist/contracts/storage/migration.js +2 -0
- package/dist/contracts/storage/rows.d.ts +33 -0
- package/dist/contracts/storage/rows.js +42 -0
- package/dist/contracts/storage/sqlite.d.ts +113 -0
- package/dist/contracts/storage/sqlite.js +8 -0
- package/dist/contracts/storage/values.d.ts +25 -0
- package/dist/contracts/storage/values.js +25 -0
- package/dist/contracts/storage.d.ts +8 -0
- package/dist/contracts/storage.js +6 -0
- package/dist/contracts/vault/invariants.d.ts +26 -0
- package/dist/contracts/vault/invariants.js +49 -0
- package/dist/contracts/vault.d.ts +1 -0
- package/dist/contracts/vault.js +1 -1
- package/dist/env/coercers.d.ts +21 -0
- package/dist/env/coercers.js +68 -0
- package/dist/env/describe-env-for-diagnostics.d.ts +16 -0
- package/dist/env/describe-env-for-diagnostics.js +23 -0
- package/dist/env/format-env-issue.d.ts +13 -0
- package/dist/env/format-env-issue.js +22 -0
- package/dist/env/parse-env.d.ts +24 -0
- package/dist/env/parse-env.js +102 -0
- package/dist/env/value-echo.d.ts +17 -0
- package/dist/env/value-echo.js +42 -0
- package/dist/errors/approved-cause.d.ts +36 -0
- package/dist/errors/approved-cause.js +17 -0
- package/dist/errors/bindings.d.ts +61 -0
- package/dist/errors/bindings.js +57 -0
- package/dist/errors/catalog.d.ts +84 -0
- package/dist/errors/catalog.js +151 -0
- package/dist/errors/disclosure.d.ts +48 -0
- package/dist/errors/disclosure.js +27 -0
- package/dist/errors/error-id.d.ts +26 -0
- package/dist/errors/error-id.js +23 -0
- package/dist/errors/idioms.d.ts +73 -0
- package/dist/errors/idioms.js +85 -0
- package/dist/errors/incident-text.d.ts +16 -0
- package/dist/errors/incident-text.js +77 -0
- package/dist/errors/reporter.d.ts +94 -0
- package/dist/errors/reporter.js +152 -0
- package/dist/errors/sentences.d.ts +68 -0
- package/dist/errors/sentences.js +93 -0
- package/dist/errors/testing.d.ts +50 -0
- package/dist/errors/testing.js +44 -0
- package/dist/fetch/cache.d.ts +12 -5
- package/dist/fetch/cache.js +46 -47
- package/dist/fetch/index.d.ts +1 -1
- package/dist/fetch/loader-read.d.ts +6 -2
- package/dist/fetch/loader-read.js +4 -4
- package/dist/format-error-for-log.d.ts +15 -0
- package/dist/format-error-for-log.js +55 -0
- package/dist/gated-mutations/gateway.d.ts +2 -0
- package/dist/gated-mutations/gateway.js +4 -1
- package/dist/host-read-vocabulary.d.ts +47 -0
- package/dist/host-read-vocabulary.js +44 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +4 -0
- package/dist/list-page.d.ts +93 -0
- package/dist/list-page.js +154 -0
- package/dist/model-facing-tool-errors.d.ts +67 -5
- package/dist/model-facing-tool-errors.js +155 -19
- package/dist/net/endpoint-policy.d.ts +39 -0
- package/dist/net/endpoint-policy.js +218 -0
- package/dist/net/fetch-timeouts.d.ts +38 -0
- package/dist/net/fetch-timeouts.js +31 -0
- package/dist/net/index.d.ts +4 -0
- package/dist/net/index.js +4 -0
- package/dist/net/loopback.d.ts +13 -0
- package/dist/net/loopback.js +39 -0
- package/dist/optimistic-concurrency.d.ts +183 -0
- package/dist/optimistic-concurrency.js +222 -0
- package/dist/primitives/errors.d.ts +9 -0
- package/dist/primitives/errors.js +12 -0
- package/dist/primitives/ids.d.ts +0 -2
- package/dist/primitives/ids.js +1 -4
- package/dist/primitives/index.d.ts +3 -0
- package/dist/primitives/index.js +2 -0
- package/dist/primitives/logger.d.ts +36 -1
- package/dist/primitives/logger.js +125 -9
- package/dist/primitives/node-shape.d.ts +24 -0
- package/dist/primitives/node-shape.js +21 -0
- package/dist/primitives/time.d.ts +9 -2
- package/dist/primitives/time.js +4 -3
- package/dist/provider-registry.js +26 -20
- package/dist/records.d.ts +40 -7
- package/dist/records.js +37 -3
- package/dist/redact.d.ts +1 -84
- package/dist/redact.js +2 -826
- package/dist/redaction-collectors.d.ts +7 -0
- package/dist/redaction-collectors.js +152 -0
- package/dist/redaction-exact-secrets.d.ts +14 -0
- package/dist/redaction-exact-secrets.js +115 -0
- package/dist/redaction-labels.d.ts +14 -0
- package/dist/redaction-labels.js +156 -0
- package/dist/redaction-patterns.d.ts +54 -0
- package/dist/redaction-patterns.js +178 -0
- package/dist/redaction-spans.d.ts +31 -0
- package/dist/redaction-spans.js +83 -0
- package/dist/registration-kit.d.ts +1 -1
- package/dist/revision-conflict-error.d.ts +18 -0
- package/dist/revision-conflict-error.js +20 -0
- package/dist/secret-shapes.d.ts +50 -0
- package/dist/secret-shapes.js +105 -0
- package/dist/settings/agent-tools.js +22 -8
- package/dist/settings/agent-value-write-tools.d.ts +4 -1
- package/dist/settings/agent-value-write-tools.js +7 -6
- package/dist/settings/agent-writable-preferences.d.ts +3 -1
- package/dist/settings/agent-writable-preferences.js +1 -1
- package/dist/settings/agent-write-denylist.d.ts +7 -4
- package/dist/settings/agent-write-denylist.js +1 -1
- package/dist/settings/{express/cms-adapter.d.ts → cms-adapter.d.ts} +3 -2
- package/dist/settings/{express/cms-adapter.js → cms-adapter.js} +21 -11
- package/dist/settings/definitions-dispatch.js +3 -1
- package/dist/settings/ensure-definitions.js +3 -1
- package/dist/settings/errors.d.ts +1 -13
- package/dist/settings/errors.js +2 -15
- package/dist/settings/index.d.ts +11 -2
- package/dist/settings/index.js +10 -2
- package/dist/settings/purge-service.js +63 -86
- package/dist/settings/{express/contracts.d.ts → route-policy.d.ts} +53 -47
- package/dist/settings/{express/shared.js → route-policy.js} +1 -16
- package/dist/settings/settings.d.ts +19 -11
- package/dist/settings/settings.js +9 -11
- package/dist/settings/tool-registrations.d.ts +3 -3
- package/dist/settings/tool-registrations.js +130 -125
- package/dist/settings/types.d.ts +1 -1
- package/dist/settings/write-definition-rename.d.ts +37 -0
- package/dist/settings/write-definition-rename.js +119 -0
- package/dist/settings/write-definition-retype.d.ts +61 -0
- package/dist/settings/write-definition-retype.js +109 -0
- package/dist/settings/write-definition-status.d.ts +81 -0
- package/dist/settings/write-definition-status.js +121 -0
- package/dist/settings/write-reset.d.ts +34 -0
- package/dist/settings/write-reset.js +73 -0
- package/dist/settings/write-service.d.ts +16 -287
- package/dist/settings/write-service.js +49 -681
- package/dist/settings/write-value-revisions.d.ts +68 -0
- package/dist/settings/write-value-revisions.js +124 -0
- package/dist/settings/write-values.d.ts +96 -0
- package/dist/settings/write-values.js +83 -0
- package/dist/surface-exchanges.d.ts +2 -2
- package/dist/text/dictionary-translator.d.ts +19 -0
- package/dist/text/dictionary-translator.js +14 -0
- package/dist/text/format-byte-size.d.ts +25 -0
- package/dist/text/format-byte-size.js +25 -0
- package/dist/text/format-timestamp.d.ts +22 -0
- package/dist/text/format-timestamp.js +57 -0
- package/dist/text/index.d.ts +12 -0
- package/dist/text/index.js +9 -0
- package/dist/text/is-abort-error.d.ts +5 -0
- package/dist/text/is-abort-error.js +5 -0
- package/dist/text/mime.d.ts +5 -0
- package/dist/text/mime.js +21 -0
- package/dist/text/redact-urls.d.ts +7 -0
- package/dist/text/redact-urls.js +5 -0
- package/dist/text/resolve-active-tab-id.d.ts +13 -0
- package/dist/text/resolve-active-tab-id.js +15 -0
- package/dist/text/sanitize-untrusted-text.d.ts +1 -1
- package/dist/text/sanitize-untrusted-text.js +2 -1
- package/dist/text/tagged-text-suppression.d.ts +103 -0
- package/dist/text/tagged-text-suppression.js +143 -0
- package/dist/text/template-i18n.d.ts +37 -0
- package/dist/text/template-i18n.js +48 -0
- package/dist/tool-call-text.d.ts +59 -0
- package/dist/tool-call-text.js +62 -0
- package/dist/tool-input-envelope.js +8 -4
- package/dist/tool-input-normalization.js +8 -1
- package/dist/tool-input-repairs.d.ts +58 -0
- package/dist/tool-input-repairs.js +154 -0
- package/dist/tool-tokens.js +1 -1
- package/dist/user-facing-error.d.ts +1 -1
- package/dist/user-facing-error.js +3 -1
- package/package.json +235 -14
- package/plugin-directory.d.ts +2 -2
- package/plugin-directory.js +16 -10
- package/dist/settings/express/contracts.js +0 -2
- package/dist/settings/express/http-request.d.ts +0 -33
- package/dist/settings/express/http-request.js +0 -50
- package/dist/settings/express/http.d.ts +0 -10
- package/dist/settings/express/http.js +0 -196
- package/dist/settings/express/index.d.ts +0 -5
- package/dist/settings/express/index.js +0 -4
- package/dist/settings/express/shared.d.ts +0 -68
- package/dist/settings/express/stream.d.ts +0 -24
- package/dist/settings/express/stream.js +0 -171
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/** Decodes one `:name` segment; malformed percent-encoding is "no match" (`null`), per the contract, not a thrown 500. */
|
|
2
|
+
function decodeParam(segment) {
|
|
3
|
+
try {
|
|
4
|
+
return decodeURIComponent(segment);
|
|
5
|
+
}
|
|
6
|
+
catch {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/** Compares one fixed segment; `lenient` ignores letter case. */
|
|
11
|
+
function sameLiteral({ want, got, lenient }) {
|
|
12
|
+
return lenient ? want.toLowerCase() === got.toLowerCase() : want === got;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The pathname the patterns are compared with. A lenient table accepts one trailing slash
|
|
16
|
+
* (`/runs/` for `/runs`) and nothing more: `//` is still a different path, and `/` stays `/`.
|
|
17
|
+
*/
|
|
18
|
+
function comparablePath({ pathname, lenient }) {
|
|
19
|
+
return lenient && pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;
|
|
20
|
+
}
|
|
21
|
+
/** One segment against its pattern segment: `null` = this route does not fit. */
|
|
22
|
+
function matchSegment({ want, got, lenient }) {
|
|
23
|
+
if (!want.startsWith(':'))
|
|
24
|
+
return sameLiteral({ want, got, lenient }) ? 'literal' : null;
|
|
25
|
+
// An empty segment is a different path (Express's `:name` needs one or more characters), not a bad escape.
|
|
26
|
+
if (got === '')
|
|
27
|
+
return null;
|
|
28
|
+
const value = decodeParam(got);
|
|
29
|
+
return value === null ? 'malformed' : { name: want.slice(1), value };
|
|
30
|
+
}
|
|
31
|
+
function matchSegments({ pattern, pathname, lenient }) {
|
|
32
|
+
const expected = pattern.split('/');
|
|
33
|
+
const actual = comparablePath({ pathname, lenient }).split('/');
|
|
34
|
+
if (expected.length !== actual.length)
|
|
35
|
+
return null;
|
|
36
|
+
const params = {};
|
|
37
|
+
let malformed = false;
|
|
38
|
+
for (let index = 0; index < expected.length; index += 1) {
|
|
39
|
+
const step = matchSegment({ want: expected[index] ?? '', got: actual[index] ?? '', lenient });
|
|
40
|
+
if (step === null)
|
|
41
|
+
return null;
|
|
42
|
+
if (step === 'malformed')
|
|
43
|
+
malformed = true;
|
|
44
|
+
else if (step !== 'literal')
|
|
45
|
+
params[step.name] = step.value;
|
|
46
|
+
}
|
|
47
|
+
// A later literal that differs still means "no match", so the verdict waits for the whole path.
|
|
48
|
+
return malformed ? 'malformed' : { params };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Matches a registered `pattern` (e.g. `/notes/:id`) against a URL `pathname` with the contract's
|
|
52
|
+
* path syntax. Returns the decoded `:name` params, or `null`. One matcher, so every adapter agrees.
|
|
53
|
+
*/
|
|
54
|
+
export function matchServerPath({ pattern, pathname }, _optional = {}) {
|
|
55
|
+
const found = matchSegments({ pattern, pathname, lenient: false });
|
|
56
|
+
return found === null || found === 'malformed' ? null : found.params;
|
|
57
|
+
}
|
|
58
|
+
/** `:name` segments become `:`, so paths that differ only in param names compare equal (and, for a lenient table, in letter case). */
|
|
59
|
+
function pathShape({ path, lenient }) {
|
|
60
|
+
return path.split('/').map((segment) => (segment.startsWith(':') ? ':' : lenient ? segment.toLowerCase() : segment)).join('/');
|
|
61
|
+
}
|
|
62
|
+
/** Negative when `a` wins: at the first segment where one is literal and the other `:param`, the literal wins. */
|
|
63
|
+
function bySpecificity(a, b) {
|
|
64
|
+
const left = a.split('/');
|
|
65
|
+
const right = b.split('/');
|
|
66
|
+
for (let index = 0; index < left.length; index += 1) {
|
|
67
|
+
const aParam = (left[index] ?? '').startsWith(':');
|
|
68
|
+
const bParam = (right[index] ?? '').startsWith(':');
|
|
69
|
+
if (aParam !== bParam)
|
|
70
|
+
return aParam ? 1 : -1;
|
|
71
|
+
}
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
/** Every route whose pattern fits `pathname`, and whether a lenient table saw a pattern that fits except for a bad percent-escape. */
|
|
75
|
+
function collect({ routes, pathname, lenient }) {
|
|
76
|
+
const matched = [];
|
|
77
|
+
let malformed = false;
|
|
78
|
+
for (const route of routes) {
|
|
79
|
+
const found = matchSegments({ pattern: route.path, pathname, lenient });
|
|
80
|
+
if (found === 'malformed')
|
|
81
|
+
malformed = lenient || malformed;
|
|
82
|
+
else if (found)
|
|
83
|
+
matched.push({ route, params: found.params });
|
|
84
|
+
}
|
|
85
|
+
return { matched, malformed };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Creates an empty route table. `route()` refuses a second registration of the same method + path
|
|
89
|
+
* shape; `match()` applies the contract's precedence (literal beats `:param`, registration order
|
|
90
|
+
* never decides) and answers a HEAD from the GET route when no HEAD route exists.
|
|
91
|
+
* @complexity `match` is O(routes × segments); fine for the few hundred routes a host mounts.
|
|
92
|
+
*/
|
|
93
|
+
export function createRouteTable(_required = {}, { lenientPaths = false } = {}) {
|
|
94
|
+
const routes = [];
|
|
95
|
+
return {
|
|
96
|
+
route(route) {
|
|
97
|
+
const shape = pathShape({ path: route.path, lenient: lenientPaths });
|
|
98
|
+
if (routes.some((existing) => existing.method === route.method && pathShape({ path: existing.path, lenient: lenientPaths }) === shape)) {
|
|
99
|
+
throw new Error(`The route ${route.method} ${route.path} is registered twice. Remove one of the two registrations.`);
|
|
100
|
+
}
|
|
101
|
+
routes.push({ method: route.method, path: route.path, handler: route.handler });
|
|
102
|
+
},
|
|
103
|
+
match({ method, pathname }) {
|
|
104
|
+
const { matched, malformed } = collect({ routes, pathname, lenient: lenientPaths });
|
|
105
|
+
if (matched.length === 0)
|
|
106
|
+
return malformed ? { kind: 'malformed-path' } : { kind: 'not-found' };
|
|
107
|
+
const best = (wanted) => matched.filter((m) => m.route.method === wanted)
|
|
108
|
+
.sort((a, b) => bySpecificity(a.route.path, b.route.path))[0];
|
|
109
|
+
const hit = best(method) ?? (method === 'HEAD' ? best('GET') : undefined);
|
|
110
|
+
if (hit)
|
|
111
|
+
return { kind: 'route', route: hit.route, params: hit.params };
|
|
112
|
+
const allow = new Set(matched.map((m) => m.route.method));
|
|
113
|
+
if (allow.has('GET'))
|
|
114
|
+
allow.add('HEAD');
|
|
115
|
+
return { kind: 'method-not-allowed', allow: [...allow] };
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=route-table.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The CSRF gate for browser writes.
|
|
3
|
+
*
|
|
4
|
+
* Why on top of a `SameSite=Strict` session cookie: a browser request must prove it came from
|
|
5
|
+
* this origin's own scripts. A browser always sends `Origin` on a cross-origin or POST fetch,
|
|
6
|
+
* and a sandboxed frame's requests carry `Origin: null`, which never matches: the frame must
|
|
7
|
+
* go through the host page. Fetch metadata, when the browser sends it, must agree. This is
|
|
8
|
+
* browser protection, not proof against a client that forges every header; such a client
|
|
9
|
+
* still needs the session cookie.
|
|
10
|
+
*/
|
|
11
|
+
/** True only for a browser request whose `Origin` is exactly this server's host.
|
|
12
|
+
* @complexity O(1). */
|
|
13
|
+
export declare function isSameOriginBrowserWrite(required: {
|
|
14
|
+
readonly origin: string | undefined;
|
|
15
|
+
readonly host: string | undefined;
|
|
16
|
+
readonly fetchSite: string | undefined;
|
|
17
|
+
}, _optional?: Record<string, never>): boolean;
|
|
18
|
+
//# sourceMappingURL=same-origin-write.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The CSRF gate for browser writes.
|
|
3
|
+
*
|
|
4
|
+
* Why on top of a `SameSite=Strict` session cookie: a browser request must prove it came from
|
|
5
|
+
* this origin's own scripts. A browser always sends `Origin` on a cross-origin or POST fetch,
|
|
6
|
+
* and a sandboxed frame's requests carry `Origin: null`, which never matches: the frame must
|
|
7
|
+
* go through the host page. Fetch metadata, when the browser sends it, must agree. This is
|
|
8
|
+
* browser protection, not proof against a client that forges every header; such a client
|
|
9
|
+
* still needs the session cookie.
|
|
10
|
+
*/
|
|
11
|
+
/** The host of a bare http(s) origin string, or null for anything else (including "null"). */
|
|
12
|
+
function originHost({ origin }, _optional = {}) {
|
|
13
|
+
try {
|
|
14
|
+
const url = new URL(origin);
|
|
15
|
+
return (url.protocol === "http:" || url.protocol === "https:") && url.origin === origin ? url.host : null;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** True only for a browser request whose `Origin` is exactly this server's host.
|
|
22
|
+
* @complexity O(1). */
|
|
23
|
+
export function isSameOriginBrowserWrite(required, _optional = {}) {
|
|
24
|
+
const { origin, host, fetchSite } = required;
|
|
25
|
+
if (!origin || !host)
|
|
26
|
+
return false;
|
|
27
|
+
if (fetchSite !== undefined && fetchSite !== "same-origin")
|
|
28
|
+
return false;
|
|
29
|
+
return originHost({ origin }) === host;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=same-origin-write.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Pure declared-host extensions for a page's report-only security policy.
|
|
3
|
+
*/
|
|
4
|
+
export declare const CSP_REPORT_ONLY_HEADER = "Content-Security-Policy-Report-Only";
|
|
5
|
+
export type SecurityHeaderSet = Readonly<Record<string, string>>;
|
|
6
|
+
/** Exact HTTP(S) origins a page declared, per policy directive. */
|
|
7
|
+
export interface DeclaredPageHosts {
|
|
8
|
+
readonly script: readonly string[];
|
|
9
|
+
readonly frame: readonly string[];
|
|
10
|
+
readonly connect: readonly string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Adds declared hosts to the directives they belong to. Anything but an exact origin is
|
|
14
|
+
* dropped, so a declaration can never become policy syntax.
|
|
15
|
+
* @returns The extended policy; a policy without a directive is left without it. O(directives × hosts), no I/O.
|
|
16
|
+
*/
|
|
17
|
+
export declare function addDeclaredHostsToPolicy({ policy, hosts }: {
|
|
18
|
+
policy: string;
|
|
19
|
+
hosts: DeclaredPageHosts;
|
|
20
|
+
}, _optional?: Record<string, never>): string;
|
|
21
|
+
/** A header set with `hosts` added to its report-only policy; the rest is unchanged. O(addDeclaredHostsToPolicy). */
|
|
22
|
+
export declare function withDeclaredHosts({ headers, hosts }: {
|
|
23
|
+
headers: SecurityHeaderSet;
|
|
24
|
+
hosts: DeclaredPageHosts;
|
|
25
|
+
}, _optional?: Record<string, never>): SecurityHeaderSet;
|
|
26
|
+
/** An exact HTTP(S) origin string (no path, no policy syntax). */
|
|
27
|
+
export declare function isDeclaredOrigin({ value }: {
|
|
28
|
+
value: string;
|
|
29
|
+
}, _optional?: Record<string, never>): boolean;
|
|
30
|
+
//# sourceMappingURL=security-headers.d.ts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Pure declared-host extensions for a page's report-only security policy.
|
|
3
|
+
*/
|
|
4
|
+
export const CSP_REPORT_ONLY_HEADER = 'Content-Security-Policy-Report-Only';
|
|
5
|
+
const HOST_DIRECTIVES = [["script", "script-src"], ["frame", "frame-src"], ["connect", "connect-src"]];
|
|
6
|
+
/**
|
|
7
|
+
* Adds declared hosts to the directives they belong to. Anything but an exact origin is
|
|
8
|
+
* dropped, so a declaration can never become policy syntax.
|
|
9
|
+
* @returns The extended policy; a policy without a directive is left without it. O(directives × hosts), no I/O.
|
|
10
|
+
*/
|
|
11
|
+
export function addDeclaredHostsToPolicy({ policy, hosts }, _optional = {}) {
|
|
12
|
+
return policy.split("; ").map(directive => {
|
|
13
|
+
const kind = HOST_DIRECTIVES.find(([, name]) => directive === name || directive.startsWith(`${name} `))?.[0];
|
|
14
|
+
if (!kind)
|
|
15
|
+
return directive;
|
|
16
|
+
const present = directive.split(" ");
|
|
17
|
+
return [directive, ...[...new Set(hosts[kind])].filter(host => isDeclaredOrigin({ value: host }) && !present.includes(host))].join(" ");
|
|
18
|
+
}).join("; ");
|
|
19
|
+
}
|
|
20
|
+
/** A header set with `hosts` added to its report-only policy; the rest is unchanged. O(addDeclaredHostsToPolicy). */
|
|
21
|
+
export function withDeclaredHosts({ headers, hosts }, _optional = {}) {
|
|
22
|
+
const policy = headers[CSP_REPORT_ONLY_HEADER];
|
|
23
|
+
return policy === undefined ? headers : { ...headers, [CSP_REPORT_ONLY_HEADER]: addDeclaredHostsToPolicy({ policy, hosts }) };
|
|
24
|
+
}
|
|
25
|
+
/** An exact HTTP(S) origin string (no path, no policy syntax). */
|
|
26
|
+
export function isDeclaredOrigin({ value }, _optional = {}) {
|
|
27
|
+
try {
|
|
28
|
+
const url = new URL(value);
|
|
29
|
+
return (url.protocol === 'https:' || url.protocol === 'http:') && url.origin === value;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=security-headers.js.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Server-Sent Events as a web `Response`: a bounded, pull-based stream any host adapter can
|
|
3
|
+
* write out, with no Node API and no ambient timer (the keepalive scheduler is injected).
|
|
4
|
+
*/
|
|
5
|
+
/** The minimal shape any event needs to flow through an `SseStream`: a cursor for `Last-Event-ID` reconnect bookkeeping and a `kind` used as the wire `event:` field. */
|
|
6
|
+
export interface SseEvent {
|
|
7
|
+
readonly opaqueCursor: string;
|
|
8
|
+
readonly kind: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Cap on events queued for one client. A stalled or malicious client would otherwise let an
|
|
12
|
+
* unbounded producer grow the queue without limit (SEC-006). Once exceeded, the connection is
|
|
13
|
+
* dropped rather than accepting unbounded memory growth.
|
|
14
|
+
*/
|
|
15
|
+
export declare const DEFAULT_MAX_QUEUED_SSE_EVENTS = 1000;
|
|
16
|
+
/** Keepalive timer the host supplies, so core holds no timer of its own. */
|
|
17
|
+
export interface SseSchedulerPort {
|
|
18
|
+
every(required: {
|
|
19
|
+
callback: () => void;
|
|
20
|
+
intervalMs: number;
|
|
21
|
+
}, optional?: Record<string, never>): unknown;
|
|
22
|
+
cancel(required: {
|
|
23
|
+
handle: unknown;
|
|
24
|
+
}, optional?: Record<string, never>): void;
|
|
25
|
+
}
|
|
26
|
+
/** One open SSE stream: the `Response` to return, and the handle the producer writes through. */
|
|
27
|
+
export interface SseStream<E extends SseEvent> {
|
|
28
|
+
readonly response: Response;
|
|
29
|
+
/**
|
|
30
|
+
* Queues one event. A no-op once the stream has closed.
|
|
31
|
+
*
|
|
32
|
+
* `backlog: true` says "this frame is history the caller already holds in memory": it is queued without
|
|
33
|
+
* counting against `maxQueuedEvents`, so a synchronous replay burst larger than the cap is delivered
|
|
34
|
+
* whole instead of dropping the client. The cost is one formatted frame per replayed event held until
|
|
35
|
+
* the client reads it, the same order as the replay the caller already built. Live frames keep the cap.
|
|
36
|
+
*/
|
|
37
|
+
send(required: {
|
|
38
|
+
event: E;
|
|
39
|
+
}, optional?: {
|
|
40
|
+
readonly backlog?: boolean;
|
|
41
|
+
}): void;
|
|
42
|
+
/** Ends the stream after the frames already queued. Idempotent. */
|
|
43
|
+
end(): void;
|
|
44
|
+
/** `true` once the stream has closed for any reason. */
|
|
45
|
+
readonly closed: boolean;
|
|
46
|
+
}
|
|
47
|
+
/** Options for {@link createSseStream}. */
|
|
48
|
+
export interface SseStreamOptions<E extends SseEvent> {
|
|
49
|
+
/** Aborts the stream (the client left). */
|
|
50
|
+
signal?: AbortSignal;
|
|
51
|
+
/** Queued-frame ceiling before the connection is dropped. Defaults to {@link DEFAULT_MAX_QUEUED_SSE_EVENTS}. */
|
|
52
|
+
maxQueuedEvents?: number;
|
|
53
|
+
/** Milliseconds between keepalive comment frames. Defaults to 15000; `false` schedules no timer and writes nothing while idle. Keepalive comment frames prevent intermediaries from closing an idle connection. */
|
|
54
|
+
keepAliveMs?: number | false;
|
|
55
|
+
/** Overrides the wire format. Defaults to `id/event/data`. */
|
|
56
|
+
formatEvent?: (required: {
|
|
57
|
+
event: E;
|
|
58
|
+
}) => string;
|
|
59
|
+
/** When true for an event, the stream ends right after writing it. */
|
|
60
|
+
isEndEvent?: (required: {
|
|
61
|
+
event: E;
|
|
62
|
+
}) => boolean;
|
|
63
|
+
/** Invoked once, the first time the stream closes for any reason. */
|
|
64
|
+
onClose?: () => void;
|
|
65
|
+
/** Extra response headers. */
|
|
66
|
+
headers?: Readonly<Record<string, string>>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Opens an SSE stream over a pull-based `ReadableStream`: the consumer pulls one frame at a time,
|
|
70
|
+
* so a slow client never makes the queue grow beyond the cap.
|
|
71
|
+
*/
|
|
72
|
+
export declare function createSseStream<E extends SseEvent>({ scheduler }: {
|
|
73
|
+
scheduler: SseSchedulerPort;
|
|
74
|
+
}, options?: SseStreamOptions<E>): SseStream<E>;
|
|
75
|
+
/**
|
|
76
|
+
* Reads a reconnect cursor from the standard `Last-Event-ID` header, falling back to an `afterCursor` query parameter (the header wins: browsers set it on `EventSource` reconnect).
|
|
77
|
+
* A repeated `afterCursor` names no cursor: Express handed a route an array for it and the route refused it, so which of the values the client meant is not guessed here either.
|
|
78
|
+
*/
|
|
79
|
+
export declare function lastEventCursor({ request }: {
|
|
80
|
+
readonly request: Request;
|
|
81
|
+
}, _optional?: Record<string, never>): string | null;
|
|
82
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Server-Sent Events as a web `Response`: a bounded, pull-based stream any host adapter can
|
|
3
|
+
* write out, with no Node API and no ambient timer (the keepalive scheduler is injected).
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Cap on events queued for one client. A stalled or malicious client would otherwise let an
|
|
7
|
+
* unbounded producer grow the queue without limit (SEC-006). Once exceeded, the connection is
|
|
8
|
+
* dropped rather than accepting unbounded memory growth.
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_MAX_QUEUED_SSE_EVENTS = 1000;
|
|
11
|
+
const defaultFormat = ({ event }) => `id: ${event.opaqueCursor}\nevent: ${event.kind}\ndata: ${JSON.stringify(event)}\n\n`;
|
|
12
|
+
/**
|
|
13
|
+
* Opens an SSE stream over a pull-based `ReadableStream`: the consumer pulls one frame at a time,
|
|
14
|
+
* so a slow client never makes the queue grow beyond the cap.
|
|
15
|
+
*/
|
|
16
|
+
export function createSseStream({ scheduler }, options = {}) {
|
|
17
|
+
const state = { frames: [], counted: 0, closed: false, ending: false, wake: undefined };
|
|
18
|
+
const max = options.maxQueuedEvents ?? DEFAULT_MAX_QUEUED_SSE_EVENTS;
|
|
19
|
+
const format = options.formatEvent ?? defaultFormat;
|
|
20
|
+
const encoder = new TextEncoder();
|
|
21
|
+
let timer;
|
|
22
|
+
const close = () => {
|
|
23
|
+
if (state.closed)
|
|
24
|
+
return;
|
|
25
|
+
state.closed = true;
|
|
26
|
+
state.frames.length = 0;
|
|
27
|
+
state.counted = 0;
|
|
28
|
+
if (timer !== undefined)
|
|
29
|
+
scheduler.cancel({ handle: timer });
|
|
30
|
+
options.onClose?.();
|
|
31
|
+
state.wake?.();
|
|
32
|
+
};
|
|
33
|
+
const push = ({ text, counted }) => {
|
|
34
|
+
if (state.closed || state.ending)
|
|
35
|
+
return;
|
|
36
|
+
// A client that stopped reading is disconnected rather than buffered without limit: a closed
|
|
37
|
+
// stream is an observable signal it can reconnect from, silently dropped frames are not.
|
|
38
|
+
if (counted && state.counted >= max) {
|
|
39
|
+
close();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
state.frames.push({ text, counted });
|
|
43
|
+
if (counted)
|
|
44
|
+
state.counted += 1;
|
|
45
|
+
state.wake?.();
|
|
46
|
+
};
|
|
47
|
+
// Keepalive pings use the same bounded queue as live events so an idle, stalled connection cannot accumulate pings outside the cap.
|
|
48
|
+
if (options.keepAliveMs !== false)
|
|
49
|
+
timer = scheduler.every({ callback: () => push({ text: ': ping\n\n', counted: true }), intervalMs: options.keepAliveMs ?? 15_000 });
|
|
50
|
+
options.signal?.addEventListener('abort', close, { once: true });
|
|
51
|
+
// A client that disconnected while the handler was still setting up never fires 'abort' again.
|
|
52
|
+
if (options.signal?.aborted)
|
|
53
|
+
close();
|
|
54
|
+
const body = new ReadableStream({
|
|
55
|
+
pull: (controller) => pullFrame({ state, controller, encoder, close }),
|
|
56
|
+
cancel: close,
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
response: new Response(body, { status: 200, headers: sseHeaders(options.headers) }),
|
|
60
|
+
send({ event }, { backlog = false } = {}) {
|
|
61
|
+
push({ text: format({ event }), counted: !backlog });
|
|
62
|
+
if (options.isEndEvent?.({ event }))
|
|
63
|
+
state.ending = true;
|
|
64
|
+
if (state.ending)
|
|
65
|
+
state.wake?.();
|
|
66
|
+
},
|
|
67
|
+
end() { state.ending = true; state.wake?.(); },
|
|
68
|
+
get closed() { return state.closed; },
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function sseHeaders(extra) {
|
|
72
|
+
return { 'content-type': 'text/event-stream', 'cache-control': 'no-cache, no-transform', connection: 'keep-alive', ...extra };
|
|
73
|
+
}
|
|
74
|
+
async function pullFrame({ state, controller, encoder, close }) {
|
|
75
|
+
while (!state.closed || state.frames.length > 0) {
|
|
76
|
+
const frame = state.frames.shift();
|
|
77
|
+
if (frame !== undefined) {
|
|
78
|
+
if (frame.counted)
|
|
79
|
+
state.counted -= 1;
|
|
80
|
+
controller.enqueue(encoder.encode(frame.text));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (state.ending)
|
|
84
|
+
break;
|
|
85
|
+
await new Promise((resolve) => { state.wake = resolve; });
|
|
86
|
+
state.wake = undefined;
|
|
87
|
+
}
|
|
88
|
+
close();
|
|
89
|
+
controller.close();
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Reads a reconnect cursor from the standard `Last-Event-ID` header, falling back to an `afterCursor` query parameter (the header wins: browsers set it on `EventSource` reconnect).
|
|
93
|
+
* A repeated `afterCursor` names no cursor: Express handed a route an array for it and the route refused it, so which of the values the client meant is not guessed here either.
|
|
94
|
+
*/
|
|
95
|
+
export function lastEventCursor({ request }, _optional = {}) {
|
|
96
|
+
const header = request.headers.get('last-event-id');
|
|
97
|
+
if (header && header.length > 0)
|
|
98
|
+
return header;
|
|
99
|
+
const values = new URL(request.url).searchParams.getAll('afterCursor');
|
|
100
|
+
const [only] = values;
|
|
101
|
+
return values.length === 1 && only !== undefined && only.length > 0 ? only : null;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file In-memory `Server` for tests: routes registered on it are dispatched with a web `Request`,
|
|
3
|
+
* no socket involved.
|
|
4
|
+
*/
|
|
5
|
+
import type { RequestWithOriginHeaders } from '../../origin-validation.js';
|
|
6
|
+
import type { TimingSafeByteComparison } from '../../token.js';
|
|
7
|
+
import type { Server } from '../server.js';
|
|
8
|
+
import type { RouteHostContext } from './json-route.js';
|
|
9
|
+
import type { SameOriginPolicyPort } from './origin.js';
|
|
10
|
+
import { type RouteTableOptions } from './route-table.js';
|
|
11
|
+
/** Every request passes. For route tests whose subject is not origin policy. */
|
|
12
|
+
export declare const allowAllOrigins: SameOriginPolicyPort;
|
|
13
|
+
/** Every request is refused, so a route's 403 path runs with core's real FORBIDDEN answer. */
|
|
14
|
+
export declare const denyAllOrigins: SameOriginPolicyPort;
|
|
15
|
+
/** An origin policy whose verdict a test can flip, recording each request it was asked about. */
|
|
16
|
+
export interface RecordingOriginPolicy extends SameOriginPolicyPort {
|
|
17
|
+
readonly calls: RequestWithOriginHeaders[];
|
|
18
|
+
set(required: {
|
|
19
|
+
readonly allow: boolean;
|
|
20
|
+
}, optional?: Record<string, never>): void;
|
|
21
|
+
}
|
|
22
|
+
/** Creates a recording origin policy. It allows every request until `set({ allow: false })`. */
|
|
23
|
+
export declare function createRecordingOriginPolicy(_required?: {}, optional?: {
|
|
24
|
+
readonly allow?: boolean;
|
|
25
|
+
}): RecordingOriginPolicy;
|
|
26
|
+
/** A constant-time comparison port that records every pair it was asked to compare. */
|
|
27
|
+
export interface RecordingTimingSafeEqual extends TimingSafeByteComparison {
|
|
28
|
+
readonly calls: ReadonlyArray<{
|
|
29
|
+
readonly left: Uint8Array;
|
|
30
|
+
readonly right: Uint8Array;
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A byte-by-byte equality that records its calls. A gate that replaces the injected comparison with
|
|
35
|
+
* `===` never calls it, so a test asserting `calls` fails; one that calls it with unequal lengths
|
|
36
|
+
* breaks the real `timingSafeEqual` contract, which this fake also refuses by throwing like Node does.
|
|
37
|
+
*/
|
|
38
|
+
export declare function fakeTimingSafeEqual(_required?: {}, _optional?: Record<string, never>): RecordingTimingSafeEqual;
|
|
39
|
+
/** A RouteHostContext for tests. It allows every origin unless a policy is passed. */
|
|
40
|
+
export declare function createTestRouteHost(_required?: {}, optional?: {
|
|
41
|
+
readonly originPolicy?: SameOriginPolicyPort;
|
|
42
|
+
readonly onInternalError?: RouteHostContext['onInternalError'];
|
|
43
|
+
}): RouteHostContext;
|
|
44
|
+
/** An in-memory server and the way to send it a request. */
|
|
45
|
+
export interface InMemoryServer {
|
|
46
|
+
readonly server: Server;
|
|
47
|
+
fetch(request: Request, optional?: {
|
|
48
|
+
client?: {
|
|
49
|
+
readonly address: string;
|
|
50
|
+
};
|
|
51
|
+
locals?: Record<string, unknown>;
|
|
52
|
+
}): Promise<Response>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Creates an empty in-memory server. Unmatched requests answer 404, or 405 with `allow` when only the method differs.
|
|
56
|
+
* @param optional.lenientPaths - Match paths as a host that replaced Express routes does (see `RouteTableOptions`); a malformed `:name` segment then answers 400.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createInMemoryServer(_required?: {}, { lenientPaths }?: RouteTableOptions): InMemoryServer;
|
|
59
|
+
//# sourceMappingURL=testing.d.ts.map
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createRouteTable } from './route-table.js';
|
|
2
|
+
/** Every request passes. For route tests whose subject is not origin policy. */
|
|
3
|
+
export const allowAllOrigins = Object.freeze({ isSameOrigin: () => true });
|
|
4
|
+
/** Every request is refused, so a route's 403 path runs with core's real FORBIDDEN answer. */
|
|
5
|
+
export const denyAllOrigins = Object.freeze({ isSameOrigin: () => false });
|
|
6
|
+
/** Creates a recording origin policy. It allows every request until `set({ allow: false })`. */
|
|
7
|
+
export function createRecordingOriginPolicy(_required = {}, optional = {}) {
|
|
8
|
+
let allow = optional.allow ?? true;
|
|
9
|
+
const calls = [];
|
|
10
|
+
return {
|
|
11
|
+
calls,
|
|
12
|
+
set: ({ allow: next }) => { allow = next; },
|
|
13
|
+
isSameOrigin: ({ req }) => { calls.push(req); return allow; },
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A byte-by-byte equality that records its calls. A gate that replaces the injected comparison with
|
|
18
|
+
* `===` never calls it, so a test asserting `calls` fails; one that calls it with unequal lengths
|
|
19
|
+
* breaks the real `timingSafeEqual` contract, which this fake also refuses by throwing like Node does.
|
|
20
|
+
*/
|
|
21
|
+
export function fakeTimingSafeEqual(_required = {}, _optional = {}) {
|
|
22
|
+
const calls = [];
|
|
23
|
+
const compare = ({ left, right }) => {
|
|
24
|
+
calls.push({ left, right });
|
|
25
|
+
if (left.length !== right.length)
|
|
26
|
+
throw new RangeError('Input buffers must have the same byte length');
|
|
27
|
+
return left.every((byte, index) => byte === right[index]);
|
|
28
|
+
};
|
|
29
|
+
return Object.assign(compare, { calls });
|
|
30
|
+
}
|
|
31
|
+
/** A RouteHostContext for tests. It allows every origin unless a policy is passed. */
|
|
32
|
+
export function createTestRouteHost(_required = {}, optional = {}) {
|
|
33
|
+
const { originPolicy = allowAllOrigins, onInternalError } = optional;
|
|
34
|
+
return onInternalError ? { originPolicy, onInternalError } : { originPolicy };
|
|
35
|
+
}
|
|
36
|
+
const PLAIN_ERRORS = {
|
|
37
|
+
400: { code: 'BAD_REQUEST', message: 'The address is not valid percent-encoding.' },
|
|
38
|
+
404: { code: 'NOT_FOUND', message: 'No route matches this address.' },
|
|
39
|
+
405: { code: 'METHOD_NOT_ALLOWED', message: 'This address does not accept this method.' },
|
|
40
|
+
};
|
|
41
|
+
const plain = (status, headers = {}) => new Response(JSON.stringify({ error: PLAIN_ERRORS[status] }), { status, headers: { 'content-type': 'application/json; charset=utf-8', ...headers } });
|
|
42
|
+
/**
|
|
43
|
+
* Creates an empty in-memory server. Unmatched requests answer 404, or 405 with `allow` when only the method differs.
|
|
44
|
+
* @param optional.lenientPaths - Match paths as a host that replaced Express routes does (see `RouteTableOptions`); a malformed `:name` segment then answers 400.
|
|
45
|
+
*/
|
|
46
|
+
export function createInMemoryServer(_required = {}, { lenientPaths = false } = {}) {
|
|
47
|
+
const table = createRouteTable({}, { lenientPaths });
|
|
48
|
+
return {
|
|
49
|
+
server: table,
|
|
50
|
+
async fetch(request, { client, locals } = {}) {
|
|
51
|
+
const found = table.match({ method: request.method, pathname: new URL(request.url).pathname });
|
|
52
|
+
if (found.kind === 'not-found')
|
|
53
|
+
return plain(404);
|
|
54
|
+
if (found.kind === 'malformed-path')
|
|
55
|
+
return plain(400);
|
|
56
|
+
if (found.kind === 'method-not-allowed')
|
|
57
|
+
return plain(405, { allow: found.allow.join(', ') });
|
|
58
|
+
const response = await found.route.handler({ request, params: found.params, ...(client ? { client } : {}), ...(locals ? { locals } : {}) });
|
|
59
|
+
return request.method === 'HEAD' ? headResponse(response) : response;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** HEAD drops the GET body; cancelling it stops its producer (an SSE stream's keepalive and subscription). */
|
|
64
|
+
async function headResponse(response) {
|
|
65
|
+
await response.body?.cancel();
|
|
66
|
+
return new Response(null, { status: response.status, headers: response.headers });
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=testing.js.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type VerifiedOrigin } from "./types.js";
|
|
2
|
+
/** Operator declaration is evidence of configured trust, not DNS or ownership proof. */
|
|
3
|
+
export interface ConfiguredOriginRequired {
|
|
4
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
5
|
+
envVarName: string;
|
|
6
|
+
clock: {
|
|
7
|
+
nowIso(): string;
|
|
8
|
+
};
|
|
9
|
+
warn: (message: string) => void;
|
|
10
|
+
}
|
|
11
|
+
/** Accept a caller-named operator declaration or return undefined with a refusal warning.
|
|
12
|
+
* No ambient environment lookup. The injected clock stamps accepted evidence only.
|
|
13
|
+
* @complexity O(configured URL length) time/space; no network or storage effects.
|
|
14
|
+
* @example resolveConfiguredOrigin({ env, envVarName: "APP_PUBLIC_URL", clock, warn });
|
|
15
|
+
*/
|
|
16
|
+
export declare function resolveConfiguredOrigin(required: ConfiguredOriginRequired): VerifiedOrigin | undefined;
|
|
17
|
+
export type OriginBootPlan = {
|
|
18
|
+
kind: "configured";
|
|
19
|
+
origin: VerifiedOrigin;
|
|
20
|
+
} | {
|
|
21
|
+
kind: "dev-seed";
|
|
22
|
+
} | {
|
|
23
|
+
kind: "none";
|
|
24
|
+
};
|
|
25
|
+
export interface OriginBootRequired extends ConfiguredOriginRequired {
|
|
26
|
+
/** The host decides whether this runtime may seed a development capability. */
|
|
27
|
+
allowDevSeed: () => boolean;
|
|
28
|
+
missingOriginWarning: string;
|
|
29
|
+
}
|
|
30
|
+
/** Pure boot decision; registration effects and runtime selection stay in the host. */
|
|
31
|
+
/** Choose configured evidence, an allowed development seed, or no registration.
|
|
32
|
+
* Warnings use caller-supplied policy; registration effects remain host-owned.
|
|
33
|
+
* @complexity O(configured URL length) time/space.
|
|
34
|
+
* @example planOriginBoot({ env, envVarName, clock, warn, allowDevSeed, missingOriginWarning });
|
|
35
|
+
*/
|
|
36
|
+
export declare function planOriginBoot(required: OriginBootRequired): OriginBootPlan;
|
|
37
|
+
//# sourceMappingURL=configured-origin.d.ts.map
|