@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,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Shared site-path checks: decoded, dot-resolved segments and origin tracking.
|
|
3
|
+
*
|
|
4
|
+
* Why the rule is not `path.startsWith("/admin")`:
|
|
5
|
+
* - `/administer-survey` is ordinary site content and must keep working, so the comparison is per
|
|
6
|
+
* SEGMENT, not per prefix.
|
|
7
|
+
* - `/%61dmin` is compared AFTER one percent-decode, not on the raw bytes. Express (which routes on
|
|
8
|
+
* the raw path) and browsers (which do not decode `%61` in a path) do NOT treat it as `/admin`; a
|
|
9
|
+
* proxy or server that decodes before routing does. Refusing it is deliberate over-refusal — the
|
|
10
|
+
* safe direction for a rule whose job is to keep public redirects and fetches off the admin
|
|
11
|
+
* surface.
|
|
12
|
+
* - `/ADMIN` reaches the same Express mount (Express matches paths case-insensitively by default),
|
|
13
|
+
* so the comparison is case-folded.
|
|
14
|
+
* - `/blog/../admin` is `/admin` by the time a browser issues the request, so dot segments are
|
|
15
|
+
* resolved first.
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Two hosts this deployment can never actually reach, used only so `new URL()` has a base to
|
|
20
|
+
* resolve a site-relative reference against. `.invalid` is reserved by RFC 2606 precisely for this.
|
|
21
|
+
*
|
|
22
|
+
* There are TWO because one base is NAMEABLE (t85 independent review, 2026-09-16). With a single
|
|
23
|
+
* base, `//reserved-path-probe.invalid/x` resolves to that very origin, so "did the origin survive"
|
|
24
|
+
* answered yes for a reference that leaves the site — reachable as `/store/buy`'s `returnTo`, which
|
|
25
|
+
* sent `Location: //reserved-path-probe.invalid/x`. Requiring the resolved origin to TRACK whichever
|
|
26
|
+
* base it was given leaves no origin an input can name, which is a structural answer rather than a
|
|
27
|
+
* denylist of the sentinel's spellings (the same reasoning as the probe itself — see
|
|
28
|
+
* {@link checkSiteRelativeTarget}). Both are bare origins, so a reference resolves to the same
|
|
29
|
+
* pathname against either.
|
|
30
|
+
*/
|
|
31
|
+
// eslint-disable-next-line sonarjs/no-clear-text-protocols -- Reserved .invalid probe origin used only for URL parsing; no network transport is invoked.
|
|
32
|
+
const PROBE_ORIGIN = "http://reserved-path-probe.invalid";
|
|
33
|
+
// eslint-disable-next-line sonarjs/no-clear-text-protocols -- Reserved .invalid probe origin used only for URL parsing; no network transport is invoked.
|
|
34
|
+
const SECOND_PROBE_ORIGIN = "http://reserved-path-probe-2.invalid";
|
|
35
|
+
/**
|
|
36
|
+
* Whether `value` contains a C0 or C1 control character.
|
|
37
|
+
*
|
|
38
|
+
* CR/LF in a request target is HTTP request splitting and in a `Location` header is response
|
|
39
|
+
* splitting; NUL and friends are path-truncation tricks.
|
|
40
|
+
*
|
|
41
|
+
* @param required.value - Any string, already decoded or not.
|
|
42
|
+
* @returns `true` if any code unit is in `U+0000`–`U+001F` or `U+007F`–`U+009F`.
|
|
43
|
+
* @complexity O(n) in the string length.
|
|
44
|
+
* @example hasControlCharacter({ value: "/ok" }, {}); // => false
|
|
45
|
+
*/
|
|
46
|
+
export function hasControlCharacter({ value }, _optional = {}) {
|
|
47
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
48
|
+
const code = value.charCodeAt(index);
|
|
49
|
+
if (code <= 0x1f || (code >= 0x7f && code <= 0x9f))
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Resolve `.` and `..` segments, RFC 3986 §5.2.4 style, on an already-decoded path.
|
|
56
|
+
*
|
|
57
|
+
* Done by hand rather than with a second `new URL()` pass because a decoded path can contain a
|
|
58
|
+
* literal `?` or `#` (from `%3F`/`%23`), and the URL parser would treat those as a query/fragment
|
|
59
|
+
* delimiter and TRUNCATE the path — turning `/x#/../admin` into `/x` and losing the very segment
|
|
60
|
+
* this check exists to find. A browser does no such thing: it never decodes `%23` before splitting
|
|
61
|
+
* the path, so the truncation would be a false negative, not a match for real behavior.
|
|
62
|
+
*
|
|
63
|
+
* Empty segments are dropped, so `//admin` collapses to `/admin`. That is deliberately stricter
|
|
64
|
+
* than a URL parser (which preserves `//`); over-matching here only ever refuses more.
|
|
65
|
+
*
|
|
66
|
+
* @param path - A decoded path, expected to start with `/`.
|
|
67
|
+
* @returns The path with dot segments removed, always starting with `/`.
|
|
68
|
+
* @complexity O(n) in the path length.
|
|
69
|
+
* @example resolveDotSegments("/blog/../admin"); // => "/admin"
|
|
70
|
+
*/
|
|
71
|
+
function resolveDotSegments(path) {
|
|
72
|
+
const out = [];
|
|
73
|
+
for (const segment of path.split("/")) {
|
|
74
|
+
if (segment === "" || segment === ".")
|
|
75
|
+
continue;
|
|
76
|
+
if (segment === "..")
|
|
77
|
+
out.pop();
|
|
78
|
+
else
|
|
79
|
+
out.push(segment);
|
|
80
|
+
}
|
|
81
|
+
return `/${out.join("/")}`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The actual per-segment rule, on a pathname string that is guaranteed to have come from a real
|
|
85
|
+
* `URL` (see {@link checkSitePathname}, this function's only caller). Kept private: a bare
|
|
86
|
+
* string carries no proof it was ever parsed — the query/fragment stripping, dot-segment removal
|
|
87
|
+
* relative to a real origin, etc. are exactly what `new URL()` already did before that caller
|
|
88
|
+
* reaches this. Do not export this directly.
|
|
89
|
+
*
|
|
90
|
+
* Check the undecoded first segment too: Express mounts `/admin/..%2fpublic` under `/admin`,
|
|
91
|
+
* even though decoding and resolving dot segments produces `/public`.
|
|
92
|
+
* Order is load-bearing for the decoded check: decode ONCE, then resolve dot segments, then compare
|
|
93
|
+
* the first segment case-folded. Decoding first is what catches `/%61dmin` (over-refusal for Express
|
|
94
|
+
* and browsers, a real match for a decoding proxy — see the module doc); resolving dot segments after the decode is
|
|
95
|
+
* what catches `/blog/%2e%2e/admin`. Exactly one decode pass models one decoding intermediary:
|
|
96
|
+
* `/%2561dmin` would need two, so it stays allowed.
|
|
97
|
+
*
|
|
98
|
+
* @param required.pathname - An already-parsed `URL`'s `.pathname` (no query, no fragment).
|
|
99
|
+
* @param required.reservedSegments - First segments reserved by the host, stored case-folded.
|
|
100
|
+
* @returns `{ kind: "ok" }` when it is ordinary site content; otherwise the arm naming the refusal.
|
|
101
|
+
* @complexity O(n) in the pathname length.
|
|
102
|
+
*/
|
|
103
|
+
function checkDecodedSitePathname({ pathname, reservedSegments }, _optional = {}) {
|
|
104
|
+
let decoded;
|
|
105
|
+
try {
|
|
106
|
+
decoded = decodeURIComponent(pathname);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return { kind: "malformed-encoding" };
|
|
110
|
+
}
|
|
111
|
+
if (decoded.includes("\\") || hasControlCharacter({ value: decoded }, {}))
|
|
112
|
+
return { kind: "disallowed-character" };
|
|
113
|
+
const rawFirst = pathname.slice(1).split("/", 1).join("").toLowerCase();
|
|
114
|
+
if (reservedSegments.has(rawFirst))
|
|
115
|
+
return { kind: "reserved", surface: rawFirst };
|
|
116
|
+
// `resolveDotSegments` always returns a path with a leading "/", so the first segment is
|
|
117
|
+
// everything after it up to the next "/" — `split(..., 1)` yields exactly that one element.
|
|
118
|
+
const first = resolveDotSegments(decoded).slice(1).split("/", 1).join("").toLowerCase();
|
|
119
|
+
if (reservedSegments.has(first))
|
|
120
|
+
return { kind: "reserved", surface: first };
|
|
121
|
+
return { kind: "ok" };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Classify an already-parsed URL's pathname against the admin application's URL space.
|
|
125
|
+
*
|
|
126
|
+
* Takes the {@link URL} itself, not a bare pathname string, so this cannot be handed a raw,
|
|
127
|
+
* unparsed reference by accident: a string has no way to prove it went through `new URL()` first,
|
|
128
|
+
* and this rule's whole design (see the module doc) depends on comparing the DECODED, dot-resolved
|
|
129
|
+
* form a URL parser would actually produce. A caller that only has a raw string — a stored redirect
|
|
130
|
+
* target, anything from user input — must go through {@link checkSiteRelativeTarget} instead, which
|
|
131
|
+
* parses it (and rejects what doesn't parse) before this check ever runs.
|
|
132
|
+
*
|
|
133
|
+
* PRECONDITION: the caller has already established that `url` is on the site's own origin. This
|
|
134
|
+
* function reads only `pathname`, so `new URL("//admin", base)` (host `admin`, pathname `/`)
|
|
135
|
+
* returns `ok`. It deliberately does not compare origins itself: `platform/routing` cannot use
|
|
136
|
+
* `features/origin`'s normalizer, and a string `URL.origin` comparison is wrong for redirect
|
|
137
|
+
* candidates (`https://site./admin`). Callers: `checkSiteRelativeTarget` (probe-origin check),
|
|
138
|
+
* `site-inspection`'s `resolveWithinOrigin`, and `packages/cms/src/redirects/reserved-destination.ts` (the
|
|
139
|
+
* origin oracle's `isSameOrigin`).
|
|
140
|
+
*
|
|
141
|
+
* @param required.url - An already-parsed `URL`; only `url.pathname` is read.
|
|
142
|
+
* @param required.reservedSegments - First segments reserved by the host, stored case-folded.
|
|
143
|
+
* @returns `{ kind: "ok" }` when it is ordinary site content; otherwise the arm naming the refusal.
|
|
144
|
+
* @complexity O(n) in the pathname length.
|
|
145
|
+
* @example checkSitePathname({ url: new URL("/administer-survey", "http://x.invalid"), reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "ok" }
|
|
146
|
+
* @example checkSitePathname({ url: new URL("/%61dmin/settings", "http://x.invalid"), reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "reserved", surface: "admin" }
|
|
147
|
+
*/
|
|
148
|
+
export function checkSitePathname({ url, reservedSegments }, _optional = {}) {
|
|
149
|
+
return checkDecodedSitePathname({ pathname: url.pathname, reservedSegments }, {});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Classify a raw, site-relative reference (a stored redirect target, a caller-supplied path)
|
|
153
|
+
* against the admin application's URL space, including the structural check that it is site-
|
|
154
|
+
* relative at all.
|
|
155
|
+
*
|
|
156
|
+
* The structural pass runs FIRST and matters on its own: `/\evil.example` begins with exactly one
|
|
157
|
+
* slash, so a `startsWith("//")` or scheme test calls it site-relative, while `new URL()` — and
|
|
158
|
+
* every browser following a `Location` — reads the backslash as a separator and lands on
|
|
159
|
+
* `evil.example`. Resolving against a probe origin and asserting the origin TRACKED that base is a
|
|
160
|
+
* structural answer to that instead of a pattern someone has to keep correct — done against two
|
|
161
|
+
* different bases so the probe origin itself is not a nameable way to pass (see
|
|
162
|
+
* {@link PROBE_ORIGIN}).
|
|
163
|
+
*
|
|
164
|
+
* @param required.raw - The untrusted reference, expected to be site-relative.
|
|
165
|
+
* @param required.reservedSegments - First segments reserved by the host, stored case-folded.
|
|
166
|
+
* @returns `{ kind: "ok" }` when it stays on this site and names ordinary content; otherwise the
|
|
167
|
+
* arm naming the refusal.
|
|
168
|
+
* @complexity O(n) in the reference length.
|
|
169
|
+
* @example checkSiteRelativeTarget({ raw: "/new", reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "ok" }
|
|
170
|
+
* @example checkSiteRelativeTarget({ raw: "/\\evil.example", reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "off-origin" }
|
|
171
|
+
*/
|
|
172
|
+
export function checkSiteRelativeTarget({ raw, reservedSegments }, _optional = {}) {
|
|
173
|
+
if (hasControlCharacter({ value: raw }, {}))
|
|
174
|
+
return { kind: "disallowed-character" };
|
|
175
|
+
let parsed;
|
|
176
|
+
let reparsed;
|
|
177
|
+
try {
|
|
178
|
+
parsed = new URL(raw, PROBE_ORIGIN);
|
|
179
|
+
reparsed = new URL(raw, SECOND_PROBE_ORIGIN);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return { kind: "unparseable" };
|
|
183
|
+
}
|
|
184
|
+
if (parsed.origin !== PROBE_ORIGIN || reparsed.origin !== SECOND_PROBE_ORIGIN)
|
|
185
|
+
return { kind: "off-origin" };
|
|
186
|
+
return checkSitePathname({ url: parsed, reservedSegments }, {});
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=site-path.js.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Canonical links, redirect checks and egress decisions must use stored trust evidence,
|
|
2
|
+
* never the raw request Host/:authority, which is attacker-controlled. */
|
|
3
|
+
export type OriginScheme = "https" | "http";
|
|
4
|
+
export type OriginSource = "workspace-setting" | "dev-capability";
|
|
5
|
+
/** Stored trust evidence. Public origins use HTTPS; HTTP is limited to explicit development capabilities. */
|
|
6
|
+
export interface VerifiedOrigin {
|
|
7
|
+
scheme: OriginScheme;
|
|
8
|
+
host: string;
|
|
9
|
+
port?: number;
|
|
10
|
+
basePath?: string;
|
|
11
|
+
verifiedAt: string;
|
|
12
|
+
source: OriginSource;
|
|
13
|
+
}
|
|
14
|
+
export declare class InsecureOriginSourceError extends Error {
|
|
15
|
+
constructor({ message }: {
|
|
16
|
+
message: string;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/** Missing trust evidence is a hard precondition failure. Callers must fail closed rather than
|
|
20
|
+
* guessing an origin from the request or another unverified value. */
|
|
21
|
+
export declare class OriginNotVerifiedError extends Error {
|
|
22
|
+
constructor({ message }: {
|
|
23
|
+
message: string;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** Copy evidence, refusing HTTP public origins and treating public loopback origins as unset.
|
|
27
|
+
* Both writers and registry reads use this boundary, including reads of legacy persisted rows.
|
|
28
|
+
* @throws InsecureOriginSourceError for HTTP public evidence; OriginNotVerifiedError for loopback public evidence.
|
|
29
|
+
* @complexity O(host bytes) time and space; pure, with no ownership or reachability probe.
|
|
30
|
+
* @example createVerifiedOrigin({ scheme: "https", host: "example.com", verifiedAt, source: "workspace-setting" });
|
|
31
|
+
*/
|
|
32
|
+
export declare function createVerifiedOrigin(input: VerifiedOrigin, _options?: Record<string, never>): VerifiedOrigin;
|
|
33
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isLoopbackHost } from "./loopback-host.js";
|
|
2
|
+
export class InsecureOriginSourceError extends Error {
|
|
3
|
+
constructor({ message }) { super(message); }
|
|
4
|
+
}
|
|
5
|
+
/** Missing trust evidence is a hard precondition failure. Callers must fail closed rather than
|
|
6
|
+
* guessing an origin from the request or another unverified value. */
|
|
7
|
+
export class OriginNotVerifiedError extends Error {
|
|
8
|
+
constructor({ message }) { super(message); }
|
|
9
|
+
}
|
|
10
|
+
/** Copy evidence, refusing HTTP public origins and treating public loopback origins as unset.
|
|
11
|
+
* Both writers and registry reads use this boundary, including reads of legacy persisted rows.
|
|
12
|
+
* @throws InsecureOriginSourceError for HTTP public evidence; OriginNotVerifiedError for loopback public evidence.
|
|
13
|
+
* @complexity O(host bytes) time and space; pure, with no ownership or reachability probe.
|
|
14
|
+
* @example createVerifiedOrigin({ scheme: "https", host: "example.com", verifiedAt, source: "workspace-setting" });
|
|
15
|
+
*/
|
|
16
|
+
export function createVerifiedOrigin(input, _options = {}) {
|
|
17
|
+
// Repositories and other adapters must construct evidence through this function; an object
|
|
18
|
+
// literal can bypass the scheme/source invariant even though it satisfies the structural type.
|
|
19
|
+
if (input.scheme === "http" && input.source !== "dev-capability") {
|
|
20
|
+
throw new InsecureOriginSourceError({
|
|
21
|
+
message: `origin scheme 'http' is only legal for source 'dev-capability' (got source '${input.source}')`
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
// Explicit dev capabilities may address a listener; a public site URL never may.
|
|
25
|
+
if (input.source === "workspace-setting" && isLoopbackHost({ host: input.host })) {
|
|
26
|
+
throw new OriginNotVerifiedError({ message: "a loopback host is not a verified public origin" });
|
|
27
|
+
}
|
|
28
|
+
return { ...input };
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -4,8 +4,10 @@
|
|
|
4
4
|
* and never names a concrete server, so any host adapter (plain Node `http`, Express, a Fetch
|
|
5
5
|
* runtime, an in-memory test fake) can serve the same routes.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* The contract itself is types. Beside it live the framework-free pieces every host adapter
|
|
8
|
+
* shares (route table, middleware decorator, JSON route builder, bounded body reading, query
|
|
9
|
+
* parsing, SSE stream, origin checks), all on web-standard `Request`/`Response`. Dispatch onto a
|
|
10
|
+
* concrete server belongs to the host's adapter, which the host passes in as `app`.
|
|
9
11
|
*
|
|
10
12
|
* Overlap, kept deliberately: `@jini-ai/http-kit` has its own Express-bound route-spec shapes
|
|
11
13
|
* (`HttpMethod` in lowercase, `RouteInputContext.params`). Core is the bottom layer and must not
|
|
@@ -41,6 +43,35 @@ export type ServerHandler = (requiredArgs: {
|
|
|
41
43
|
readonly client?: {
|
|
42
44
|
readonly address: string;
|
|
43
45
|
};
|
|
46
|
+
/**
|
|
47
|
+
* Per-request values that middleware hands to the handler (what Express calls `res.locals`).
|
|
48
|
+
* Adapters and `withServerMiddleware` always pass one object per request; it is optional here
|
|
49
|
+
* only so an adapter written before middleware existed still satisfies the contract.
|
|
50
|
+
*/
|
|
51
|
+
readonly locals?: Record<string, unknown>;
|
|
52
|
+
}, optionalArgs?: Record<string, never>) => Response | Promise<Response>;
|
|
53
|
+
/**
|
|
54
|
+
* Runs before a route's handler and may answer instead of it. Call `next()` to continue to the
|
|
55
|
+
* next middleware or the handler and get its `Response` back (to read its status, or to add a
|
|
56
|
+
* header); return a `Response` without calling `next()` to refuse the request. Write values for
|
|
57
|
+
* the handler into `locals`. `route` is the registered method + path pattern that matched (for
|
|
58
|
+
* logs and metrics), never the raw URL.
|
|
59
|
+
*
|
|
60
|
+
* @agent-note Middleware never writes to a socket or framework response object, and never
|
|
61
|
+
* forwards an error by calling something: throw it, and the adapter's error path answers.
|
|
62
|
+
*/
|
|
63
|
+
export type ServerMiddleware = (requiredArgs: {
|
|
64
|
+
readonly request: Request;
|
|
65
|
+
readonly params: Readonly<Record<string, string>>;
|
|
66
|
+
readonly client?: {
|
|
67
|
+
readonly address: string;
|
|
68
|
+
};
|
|
69
|
+
readonly locals: Record<string, unknown>;
|
|
70
|
+
readonly route: {
|
|
71
|
+
readonly method: ServerMethod;
|
|
72
|
+
readonly path: string;
|
|
73
|
+
};
|
|
74
|
+
readonly next: () => Promise<Response>;
|
|
44
75
|
}, optionalArgs?: Record<string, never>) => Response | Promise<Response>;
|
|
45
76
|
/**
|
|
46
77
|
* One route as data: what `Server.route` takes. A package can return `ServerRoute[]` and leave
|
|
@@ -81,4 +112,17 @@ export interface ServerRoute {
|
|
|
81
112
|
export interface Server {
|
|
82
113
|
route(requiredArgs: ServerRoute, optionalArgs?: Record<string, never>): void;
|
|
83
114
|
}
|
|
115
|
+
export * from './server/result.js';
|
|
116
|
+
export * from './server/api-error.js';
|
|
117
|
+
export * from './server/auth.js';
|
|
118
|
+
export * from './server/origin.js';
|
|
119
|
+
export * from './server/security-headers.js';
|
|
120
|
+
export * from './server/same-origin-write.js';
|
|
121
|
+
export * from './server/route-table.js';
|
|
122
|
+
export * from './server/middleware.js';
|
|
123
|
+
export * from './server/query.js';
|
|
124
|
+
export * from './server/request-body.js';
|
|
125
|
+
export * from './server/json-route.js';
|
|
126
|
+
export * from './server/sse.js';
|
|
127
|
+
export * from './server/rate-limit.js';
|
|
84
128
|
//# sourceMappingURL=server.d.ts.map
|
package/dist/contracts/server.js
CHANGED
|
@@ -4,12 +4,26 @@
|
|
|
4
4
|
* and never names a concrete server, so any host adapter (plain Node `http`, Express, a Fetch
|
|
5
5
|
* runtime, an in-memory test fake) can serve the same routes.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* The contract itself is types. Beside it live the framework-free pieces every host adapter
|
|
8
|
+
* shares (route table, middleware decorator, JSON route builder, bounded body reading, query
|
|
9
|
+
* parsing, SSE stream, origin checks), all on web-standard `Request`/`Response`. Dispatch onto a
|
|
10
|
+
* concrete server belongs to the host's adapter, which the host passes in as `app`.
|
|
9
11
|
*
|
|
10
12
|
* Overlap, kept deliberately: `@jini-ai/http-kit` has its own Express-bound route-spec shapes
|
|
11
13
|
* (`HttpMethod` in lowercase, `RouteInputContext.params`). Core is the bottom layer and must not
|
|
12
14
|
* import http-kit, so those are not reused; a later bridge can map one onto the other.
|
|
13
15
|
*/
|
|
14
|
-
export
|
|
16
|
+
export * from './server/result.js';
|
|
17
|
+
export * from './server/api-error.js';
|
|
18
|
+
export * from './server/auth.js';
|
|
19
|
+
export * from './server/origin.js';
|
|
20
|
+
export * from './server/security-headers.js';
|
|
21
|
+
export * from './server/same-origin-write.js';
|
|
22
|
+
export * from './server/route-table.js';
|
|
23
|
+
export * from './server/middleware.js';
|
|
24
|
+
export * from './server/query.js';
|
|
25
|
+
export * from './server/request-body.js';
|
|
26
|
+
export * from './server/json-route.js';
|
|
27
|
+
export * from './server/sse.js';
|
|
28
|
+
export * from './server/rate-limit.js';
|
|
15
29
|
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The storage kernel contract: the ONE async way a repository adapter reaches its database, on every driver.
|
|
3
|
+
* Package: `@jini-ai/core/contracts/storage`. Names no query library and no driver: `Handle` is whatever query handle
|
|
4
|
+
* the host's storage package passes to {@link StorageKernel.run} (`@jini-ai/db` binds its own handle type).
|
|
5
|
+
*
|
|
6
|
+
* A repo is ONE query body for every dialect. It never holds a raw handle, never runs `BEGIN`, never calls a driver
|
|
7
|
+
* API. It asks the kernel:
|
|
8
|
+
*
|
|
9
|
+
* - {@link StorageKernel.run} — run statements on the right executor: the open transaction when the
|
|
10
|
+
* caller is inside {@link StorageKernel.transaction}, else the base handle.
|
|
11
|
+
* - {@link StorageKernel.transaction} — one unit of work. A NESTED call joins the outer one (no
|
|
12
|
+
* savepoint), so a service composing two repos that each wrap themselves still commits or rolls
|
|
13
|
+
* back as one. Calling it from inside a `run()` body throws: that body holds the handle a
|
|
14
|
+
* transaction would wait for.
|
|
15
|
+
* - {@link StorageKernel.lockKey} — the explicit concurrency strategy for "nothing else may
|
|
16
|
+
* interleave here" (read-then-write sequences such as a ledger append). A transaction alone does
|
|
17
|
+
* not give that on Postgres (READ COMMITTED); this does, on every dialect.
|
|
18
|
+
*
|
|
19
|
+
* Three separate things describe a kernel (never inferred from each other, never cached per process):
|
|
20
|
+
* the SQL {@link StorageDialect}, the connection {@link StorageTransport}, and its
|
|
21
|
+
* {@link StorageCapabilities}. Asking for a capability the kernel lacks throws
|
|
22
|
+
* {@link UnsupportedCapabilityError}; nothing silently downgrades (e.g. a transaction never becomes
|
|
23
|
+
* "run the statements one by one").
|
|
24
|
+
*
|
|
25
|
+
* Drivers live in the host's storage package (`@jini-ai/db`: the `sqlite`, `pglite` and `postgres` subpaths).
|
|
26
|
+
* None of them imports a driver package: the consumer passes its own client, pool or class in.
|
|
27
|
+
*/
|
|
28
|
+
/** SQL family: decides which dialect helper fragment applies. */
|
|
29
|
+
export type StorageDialect = "sqlite" | "postgres";
|
|
30
|
+
/**
|
|
31
|
+
* The connection underneath, named by the storage package that opened it (`@jini-ai/db`: `DbTransport`).
|
|
32
|
+
* Open-ended so a new driver needs no change here.
|
|
33
|
+
*/
|
|
34
|
+
export type StorageTransport = string;
|
|
35
|
+
/** What this kernel instance can do. Checked per call; a missing one is an explicit error. */
|
|
36
|
+
export interface StorageCapabilities {
|
|
37
|
+
/** `transaction(fn)` with awaits (reads, logic) between statements. D1 and HTTP drivers: no. */
|
|
38
|
+
readonly interactiveTransactions: boolean;
|
|
39
|
+
/** Several statements applied all-or-nothing without an interactive transaction. */
|
|
40
|
+
readonly atomicBatch: boolean;
|
|
41
|
+
/** DDL inside a transaction rolls back with it. */
|
|
42
|
+
readonly transactionalDdl: boolean;
|
|
43
|
+
/** A consistent copy of the whole database can be taken through the driver (restore points). */
|
|
44
|
+
readonly backup: boolean;
|
|
45
|
+
}
|
|
46
|
+
export type StorageCapability = keyof StorageCapabilities;
|
|
47
|
+
export declare class UnsupportedCapabilityError extends Error {
|
|
48
|
+
readonly capability: StorageCapability;
|
|
49
|
+
readonly transport: StorageTransport;
|
|
50
|
+
constructor(capability: StorageCapability, transport: StorageTransport);
|
|
51
|
+
}
|
|
52
|
+
declare const storageKernelSchema: unique symbol;
|
|
53
|
+
export interface StorageKernel<Handle, Schema = unknown> {
|
|
54
|
+
/** Type-only schema witness: retain Schema independently of the handle's structural method
|
|
55
|
+
* signatures so stores reject kernels missing required tables.
|
|
56
|
+
* Optional so existing host implementations need no runtime property. */
|
|
57
|
+
readonly [storageKernelSchema]?: Schema;
|
|
58
|
+
readonly dialect: StorageDialect;
|
|
59
|
+
readonly transport: StorageTransport;
|
|
60
|
+
readonly capabilities: StorageCapabilities;
|
|
61
|
+
/** Resolves once the driver is open and its schema is in place. Every call below awaits it. */
|
|
62
|
+
readonly ready: Promise<void>;
|
|
63
|
+
run<T>(fn: (handle: Handle) => T | Promise<T>): Promise<T>;
|
|
64
|
+
/** Throws {@link UnsupportedCapabilityError} without `interactiveTransactions`. */
|
|
65
|
+
transaction<T>(fn: () => Promise<T>): Promise<T>;
|
|
66
|
+
/**
|
|
67
|
+
* Inside a transaction: blocks every other transaction that locks the same `key` until this one
|
|
68
|
+
* ends. Postgres: a transaction-scoped advisory lock. SQLite: nothing to do — the kernel's
|
|
69
|
+
* transactions take the database write lock at `BEGIN IMMEDIATE`, which already serializes them.
|
|
70
|
+
* Throws outside a transaction (a lock with no transaction to end it would never be released).
|
|
71
|
+
*/
|
|
72
|
+
lockKey(key: string): Promise<void>;
|
|
73
|
+
/** True inside this kernel's own transaction (the caller's async context). */
|
|
74
|
+
inTransaction(): boolean;
|
|
75
|
+
/** Throws {@link UnsupportedCapabilityError} when this kernel lacks `capability`. */
|
|
76
|
+
require(capability: StorageCapability): void;
|
|
77
|
+
/**
|
|
78
|
+
* A consistent copy of the whole database written to `destPath` (SQLite: the online backup API,
|
|
79
|
+
* a normal SQLite file; embedded Postgres: a data-dir tarball). Throws {@link UnsupportedCapabilityError}
|
|
80
|
+
* without `backup`. Call it outside a transaction.
|
|
81
|
+
*/
|
|
82
|
+
backupTo(destPath: string): Promise<void>;
|
|
83
|
+
close(): Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
export {};
|
|
86
|
+
//# sourceMappingURL=kernel.d.ts.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The storage kernel contract: the ONE async way a repository adapter reaches its database, on every driver.
|
|
3
|
+
* Package: `@jini-ai/core/contracts/storage`. Names no query library and no driver: `Handle` is whatever query handle
|
|
4
|
+
* the host's storage package passes to {@link StorageKernel.run} (`@jini-ai/db` binds its own handle type).
|
|
5
|
+
*
|
|
6
|
+
* A repo is ONE query body for every dialect. It never holds a raw handle, never runs `BEGIN`, never calls a driver
|
|
7
|
+
* API. It asks the kernel:
|
|
8
|
+
*
|
|
9
|
+
* - {@link StorageKernel.run} — run statements on the right executor: the open transaction when the
|
|
10
|
+
* caller is inside {@link StorageKernel.transaction}, else the base handle.
|
|
11
|
+
* - {@link StorageKernel.transaction} — one unit of work. A NESTED call joins the outer one (no
|
|
12
|
+
* savepoint), so a service composing two repos that each wrap themselves still commits or rolls
|
|
13
|
+
* back as one. Calling it from inside a `run()` body throws: that body holds the handle a
|
|
14
|
+
* transaction would wait for.
|
|
15
|
+
* - {@link StorageKernel.lockKey} — the explicit concurrency strategy for "nothing else may
|
|
16
|
+
* interleave here" (read-then-write sequences such as a ledger append). A transaction alone does
|
|
17
|
+
* not give that on Postgres (READ COMMITTED); this does, on every dialect.
|
|
18
|
+
*
|
|
19
|
+
* Three separate things describe a kernel (never inferred from each other, never cached per process):
|
|
20
|
+
* the SQL {@link StorageDialect}, the connection {@link StorageTransport}, and its
|
|
21
|
+
* {@link StorageCapabilities}. Asking for a capability the kernel lacks throws
|
|
22
|
+
* {@link UnsupportedCapabilityError}; nothing silently downgrades (e.g. a transaction never becomes
|
|
23
|
+
* "run the statements one by one").
|
|
24
|
+
*
|
|
25
|
+
* Drivers live in the host's storage package (`@jini-ai/db`: the `sqlite`, `pglite` and `postgres` subpaths).
|
|
26
|
+
* None of them imports a driver package: the consumer passes its own client, pool or class in.
|
|
27
|
+
*/
|
|
28
|
+
export class UnsupportedCapabilityError extends Error {
|
|
29
|
+
capability;
|
|
30
|
+
transport;
|
|
31
|
+
constructor(capability, transport) {
|
|
32
|
+
super(`the ${transport} storage driver does not support ${capability}`);
|
|
33
|
+
this.capability = capability;
|
|
34
|
+
this.transport = transport;
|
|
35
|
+
this.name = "UnsupportedCapabilityError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=kernel.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { StorageKernel } from "./kernel.js";
|
|
2
|
+
/**
|
|
3
|
+
* @file What a migration step is. Steps form ONE ordered history for every dialect; the host's runner
|
|
4
|
+
* (`@jini-ai/db/migrate` `runMigrations`) by default applies each pending one in its own transaction and records it in
|
|
5
|
+
* the consumer's ledger table. A package ships steps as data from its storage-adapter subpath; it never runs them.
|
|
6
|
+
*/
|
|
7
|
+
export interface MigrationContext {
|
|
8
|
+
/** Where the runner copied (or would copy) the database before the first pending step; the caller decides. */
|
|
9
|
+
readonly backupPath?: string;
|
|
10
|
+
/** A host may explicitly continue after selected backup failures; throwing preserves refusal. */
|
|
11
|
+
readonly onBackupError?: (input: {
|
|
12
|
+
error: unknown;
|
|
13
|
+
backupPath: string;
|
|
14
|
+
}) => void;
|
|
15
|
+
/** Runs only after the copy succeeds, before any step changes the source database. */
|
|
16
|
+
readonly onBackupComplete?: (input: {
|
|
17
|
+
backupPath: string;
|
|
18
|
+
}) => Promise<void>;
|
|
19
|
+
/** Something worth telling the operator (a backup taken, a legacy tail applied). */
|
|
20
|
+
note(message: string): void;
|
|
21
|
+
}
|
|
22
|
+
/** `Kernel` is the kernel the step's body needs; a step written against a query-builder handle is `MigrationStep<StorageKernel<ThatHandle<unknown>>>`. */
|
|
23
|
+
export interface MigrationStep<Kernel extends StorageKernel<unknown, unknown> = StorageKernel<unknown>> {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
/** sha256 hex of what the step does, pinned by the consumer (a test re-derives it with `sourceChecksum`). */
|
|
26
|
+
readonly checksum: string;
|
|
27
|
+
/** Runs OUTSIDE the transaction, only when the step is pending (backups: they cannot run inside one). */
|
|
28
|
+
prepare?(kernel: Kernel, context: MigrationContext): Promise<void>;
|
|
29
|
+
/** Runs inside the step's transaction, after the runner holds the migration lock. */
|
|
30
|
+
up(kernel: Kernel, context: MigrationContext): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=migration.d.ts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Generic legacy row shapes and tolerant JSON narrowing; no driver or SQL dependencies. */
|
|
2
|
+
export type DbRow = Record<string, any>;
|
|
3
|
+
/** @module db/core/json
|
|
4
|
+
* Pure JSON parsing utility that converts SQLite TEXT columns to JS values without throwing.
|
|
5
|
+
* Imports no sibling subdirectory.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Safely parse a SQLite TEXT column value to a JS value.
|
|
9
|
+
* Returns `undefined` instead of throwing for non-string, empty, or malformed JSON —
|
|
10
|
+
* callers should narrow the return type before use.
|
|
11
|
+
* @param s - raw column value as stored in SQLite (typically `string | null`)
|
|
12
|
+
* @returns parsed value, or `undefined` if the input is absent or unparseable
|
|
13
|
+
*/
|
|
14
|
+
export declare function parseJsonOrUndef({ s }: {
|
|
15
|
+
s: unknown;
|
|
16
|
+
}, _optional?: Record<string, never>): any;
|
|
17
|
+
/** @module db/core/rows
|
|
18
|
+
* Helpers that normalize raw embedded SQLite driver query results to typed `DbRow` values.
|
|
19
|
+
* Imports no sibling subdirectory; depends only on `core/types`.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Narrow a single `db.prepare(…).get()` result to `DbRow | null`.
|
|
23
|
+
* Returns `null` for falsy or non-object returns so callers can distinguish "no row" from an empty row.
|
|
24
|
+
* @param value - raw return from embedded SQLite driver's `.get()`
|
|
25
|
+
*/
|
|
26
|
+
export declare function row(value: unknown): DbRow | null;
|
|
27
|
+
/**
|
|
28
|
+
* Map a `db.prepare(…).all()` result array to `DbRow[]`, replacing any falsy or non-object
|
|
29
|
+
* entries with an empty object so the array length is always preserved.
|
|
30
|
+
* @param value - raw return from embedded SQLite driver's `.all()`
|
|
31
|
+
*/
|
|
32
|
+
export declare function rows(value: unknown[]): DbRow[];
|
|
33
|
+
//# sourceMappingURL=rows.d.ts.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** @module db/core/json
|
|
2
|
+
* Pure JSON parsing utility that converts SQLite TEXT columns to JS values without throwing.
|
|
3
|
+
* Imports no sibling subdirectory.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Safely parse a SQLite TEXT column value to a JS value.
|
|
7
|
+
* Returns `undefined` instead of throwing for non-string, empty, or malformed JSON —
|
|
8
|
+
* callers should narrow the return type before use.
|
|
9
|
+
* @param s - raw column value as stored in SQLite (typically `string | null`)
|
|
10
|
+
* @returns parsed value, or `undefined` if the input is absent or unparseable
|
|
11
|
+
*/
|
|
12
|
+
export function parseJsonOrUndef({ s }, _optional = {}) {
|
|
13
|
+
if (typeof s !== 'string' || !s)
|
|
14
|
+
return undefined;
|
|
15
|
+
try {
|
|
16
|
+
return JSON.parse(s);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** @module db/core/rows
|
|
23
|
+
* Helpers that normalize raw embedded SQLite driver query results to typed `DbRow` values.
|
|
24
|
+
* Imports no sibling subdirectory; depends only on `core/types`.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Narrow a single `db.prepare(…).get()` result to `DbRow | null`.
|
|
28
|
+
* Returns `null` for falsy or non-object returns so callers can distinguish "no row" from an empty row.
|
|
29
|
+
* @param value - raw return from embedded SQLite driver's `.get()`
|
|
30
|
+
*/
|
|
31
|
+
export function row(value) {
|
|
32
|
+
return value && typeof value === 'object' ? value : null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Map a `db.prepare(…).all()` result array to `DbRow[]`, replacing any falsy or non-object
|
|
36
|
+
* entries with an empty object so the array length is always preserved.
|
|
37
|
+
* @param value - raw return from embedded SQLite driver's `.all()`
|
|
38
|
+
*/
|
|
39
|
+
export function rows(value) {
|
|
40
|
+
return value.map((item) => row(item) ?? {});
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=rows.js.map
|