@lostgradient/backchannel 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/dist/access-policy-metadata.d.ts +92 -0
- package/dist/access-policy-metadata.js +87 -0
- package/dist/asyncapi-channels.d.ts +46 -0
- package/dist/asyncapi-channels.js +399 -0
- package/dist/asyncapi.d.ts +40 -0
- package/dist/asyncapi.js +165 -0
- package/dist/authentication/api-key.d.ts +4 -0
- package/dist/authentication/api-key.js +114 -0
- package/dist/authentication/audit.d.ts +115 -0
- package/dist/authentication/audit.js +75 -0
- package/dist/authentication/credential-verifiers.d.ts +60 -0
- package/dist/authentication/credential-verifiers.js +102 -0
- package/dist/authentication/crypto.d.ts +7 -0
- package/dist/authentication/crypto.js +140 -0
- package/dist/authentication/index.d.ts +80 -0
- package/dist/authentication/index.js +212 -0
- package/dist/authentication/rate-limiter.d.ts +133 -0
- package/dist/authentication/rate-limiter.js +124 -0
- package/dist/authentication/redaction.d.ts +72 -0
- package/dist/authentication/redaction.js +118 -0
- package/dist/authentication/rotating-api-key-store.d.ts +127 -0
- package/dist/authentication/rotating-api-key-store.js +126 -0
- package/dist/authentication/types.d.ts +337 -0
- package/dist/authentication/types.js +26 -0
- package/dist/authentication.d.ts +1 -0
- package/dist/authentication.js +1 -0
- package/dist/authorization-scope.d.ts +109 -0
- package/dist/authorization-scope.js +144 -0
- package/dist/authorization.d.ts +55 -0
- package/dist/authorization.js +134 -0
- package/dist/bind-feed-lifetime.d.ts +45 -0
- package/dist/bind-feed-lifetime.js +55 -0
- package/dist/byte-arrays.d.ts +5 -0
- package/dist/byte-arrays.js +9 -0
- package/dist/client/catalog-client.d.ts +285 -0
- package/dist/client/catalog-client.js +74 -0
- package/dist/client/in-process-transport.d.ts +19 -0
- package/dist/client/in-process-transport.js +136 -0
- package/dist/client/index.d.ts +14 -0
- package/dist/client/index.js +14 -0
- package/dist/client/json-rpc-http-transport.d.ts +118 -0
- package/dist/client/json-rpc-http-transport.js +127 -0
- package/dist/client/server-sent-event-parser.d.ts +55 -0
- package/dist/client/server-sent-event-parser.js +78 -0
- package/dist/client/sse-subscription-transport.d.ts +72 -0
- package/dist/client/sse-subscription-transport.js +224 -0
- package/dist/client/subscription-client.d.ts +122 -0
- package/dist/client/subscription-client.js +28 -0
- package/dist/connection-lease.d.ts +10 -0
- package/dist/connection-lease.js +8 -0
- package/dist/discovery-filter.d.ts +13 -0
- package/dist/discovery-filter.js +16 -0
- package/dist/discovery-info.d.ts +94 -0
- package/dist/discovery-info.js +38 -0
- package/dist/event-selector.d.ts +7 -0
- package/dist/event-selector.js +7 -0
- package/dist/fault-code.d.ts +84 -0
- package/dist/fault-code.js +114 -0
- package/dist/fault-remediation.d.ts +30 -0
- package/dist/fault-remediation.js +52 -0
- package/dist/fault-to-http.d.ts +11 -0
- package/dist/fault-to-http.js +13 -0
- package/dist/fault-to-json-rpc.d.ts +53 -0
- package/dist/fault-to-json-rpc.js +143 -0
- package/dist/fault-wire-safety.d.ts +45 -0
- package/dist/fault-wire-safety.js +72 -0
- package/dist/generator.d.ts +9 -0
- package/dist/generator.js +9 -0
- package/dist/handler/auth-context-principal.d.ts +37 -0
- package/dist/handler/auth-context-principal.js +67 -0
- package/dist/handler/authentication-responses.d.ts +23 -0
- package/dist/handler/authentication-responses.js +34 -0
- package/dist/handler/json-responses.d.ts +15 -0
- package/dist/handler/json-responses.js +32 -0
- package/dist/handler/operation-handler-authentication.d.ts +64 -0
- package/dist/handler/operation-handler-authentication.js +58 -0
- package/dist/handler/operation-handler-discovery.d.ts +29 -0
- package/dist/handler/operation-handler-discovery.js +55 -0
- package/dist/handler/operation-handler-mounts.d.ts +100 -0
- package/dist/handler/operation-handler-mounts.js +134 -0
- package/dist/handler/operation-handler-subscription.d.ts +34 -0
- package/dist/handler/operation-handler-subscription.js +112 -0
- package/dist/handler/operation-handler.d.ts +162 -0
- package/dist/handler/operation-handler.js +236 -0
- package/dist/handler/workspace-release.d.ts +6 -0
- package/dist/handler/workspace-release.js +75 -0
- package/dist/in-memory-replay-live-backend.d.ts +47 -0
- package/dist/in-memory-replay-live-backend.js +129 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +30 -0
- package/dist/internal/cryptography/encryption.d.ts +108 -0
- package/dist/internal/cryptography/encryption.js +241 -0
- package/dist/internal/cryptography/hash.d.ts +72 -0
- package/dist/internal/cryptography/hash.js +233 -0
- package/dist/internal/cryptography/index.d.ts +4 -0
- package/dist/internal/cryptography/index.js +2 -0
- package/dist/internals.d.ts +58 -0
- package/dist/internals.js +54 -0
- package/dist/json-rpc-dispatch.d.ts +88 -0
- package/dist/json-rpc-dispatch.js +154 -0
- package/dist/json-rpc-framing.d.ts +42 -0
- package/dist/json-rpc-framing.js +50 -0
- package/dist/json-rpc-http.d.ts +36 -0
- package/dist/json-rpc-http.js +191 -0
- package/dist/json-rpc-parse.d.ts +69 -0
- package/dist/json-rpc-parse.js +206 -0
- package/dist/json-rpc-protocol.d.ts +76 -0
- package/dist/json-rpc-protocol.js +54 -0
- package/dist/json-rpc-session-runtime.d.ts +55 -0
- package/dist/json-rpc-session-runtime.js +111 -0
- package/dist/json-rpc-websocket-subscription-pump.d.ts +9 -0
- package/dist/json-rpc-websocket-subscription-pump.js +46 -0
- package/dist/json-rpc-websocket-subscriptions.d.ts +10 -0
- package/dist/json-rpc-websocket-subscriptions.js +33 -0
- package/dist/json-rpc-websocket-types.d.ts +34 -0
- package/dist/json-rpc-websocket-types.js +1 -0
- package/dist/json-rpc-websocket-validation.d.ts +45 -0
- package/dist/json-rpc-websocket-validation.js +154 -0
- package/dist/json-rpc-websocket.d.ts +20 -0
- package/dist/json-rpc-websocket.js +363 -0
- package/dist/json-schema-utilities.d.ts +5 -0
- package/dist/json-schema-utilities.js +17 -0
- package/dist/mcp-discovery-names.d.ts +11 -0
- package/dist/mcp-discovery-names.js +11 -0
- package/dist/openapi-canonical-json.d.ts +10 -0
- package/dist/openapi-canonical-json.js +23 -0
- package/dist/openapi-error-responses.d.ts +15 -0
- package/dist/openapi-error-responses.js +104 -0
- package/dist/openapi-schemas.d.ts +17 -0
- package/dist/openapi-schemas.js +94 -0
- package/dist/openapi.d.ts +60 -0
- package/dist/openapi.js +351 -0
- package/dist/openrpc-document-schema.d.ts +255 -0
- package/dist/openrpc-document-schema.js +84 -0
- package/dist/openrpc-errors.d.ts +93 -0
- package/dist/openrpc-errors.js +134 -0
- package/dist/openrpc.d.ts +66 -0
- package/dist/openrpc.js +260 -0
- package/dist/operation-catalog/behavior-types.d.ts +34 -0
- package/dist/operation-catalog/behavior-types.js +1 -0
- package/dist/operation-catalog/behavior-validation.d.ts +3 -0
- package/dist/operation-catalog/behavior-validation.js +298 -0
- package/dist/operation-catalog/dispatch-allowlist.d.ts +4 -0
- package/dist/operation-catalog/dispatch-allowlist.js +7 -0
- package/dist/operation-catalog/dispatch-preparation.d.ts +24 -0
- package/dist/operation-catalog/dispatch-preparation.js +49 -0
- package/dist/operation-catalog/index.d.ts +19 -0
- package/dist/operation-catalog/index.js +17 -0
- package/dist/operation-catalog/operation-dispatch.d.ts +14 -0
- package/dist/operation-catalog/operation-dispatch.js +115 -0
- package/dist/operation-catalog/operation-interceptors.d.ts +15 -0
- package/dist/operation-catalog/operation-interceptors.js +1 -0
- package/dist/operation-catalog/operation-metadata.d.ts +5 -0
- package/dist/operation-catalog/operation-metadata.js +104 -0
- package/dist/operation-catalog/pipeline-helpers.d.ts +70 -0
- package/dist/operation-catalog/pipeline-helpers.js +242 -0
- package/dist/operation-catalog/pipeline-stages.d.ts +23 -0
- package/dist/operation-catalog/pipeline-stages.js +226 -0
- package/dist/operation-catalog/pipeline.d.ts +7 -0
- package/dist/operation-catalog/pipeline.js +33 -0
- package/dist/operation-catalog/registry.d.ts +17 -0
- package/dist/operation-catalog/registry.js +132 -0
- package/dist/operation-catalog/stream-pipeline.d.ts +37 -0
- package/dist/operation-catalog/stream-pipeline.js +129 -0
- package/dist/operation-catalog/subscription-lifecycle.d.ts +6 -0
- package/dist/operation-catalog/subscription-lifecycle.js +27 -0
- package/dist/operation-catalog/types.d.ts +392 -0
- package/dist/operation-catalog/types.js +40 -0
- package/dist/operation-catalog/universal-faults.d.ts +11 -0
- package/dist/operation-catalog/universal-faults.js +15 -0
- package/dist/operation-catalog-snapshot.d.ts +215 -0
- package/dist/operation-catalog-snapshot.js +128 -0
- package/dist/operation-catalog.d.ts +1 -0
- package/dist/operation-catalog.js +1 -0
- package/dist/operation-description-audit.d.ts +44 -0
- package/dist/operation-description-audit.js +90 -0
- package/dist/operation-fault-helpers.d.ts +21 -0
- package/dist/operation-fault-helpers.js +32 -0
- package/dist/operation-fault.d.ts +245 -0
- package/dist/operation-fault.js +235 -0
- package/dist/operation-registry.d.ts +46 -0
- package/dist/operation-registry.js +46 -0
- package/dist/operations/closable-iterable.d.ts +23 -0
- package/dist/operations/closable-iterable.js +51 -0
- package/dist/operations/get-principal.d.ts +106 -0
- package/dist/operations/get-principal.js +107 -0
- package/dist/operations/sse-stream.d.ts +67 -0
- package/dist/operations/sse-stream.js +264 -0
- package/dist/principal.d.ts +253 -0
- package/dist/principal.js +254 -0
- package/dist/replay-live-feed-internals.d.ts +47 -0
- package/dist/replay-live-feed-internals.js +215 -0
- package/dist/replay-live-feed-types.d.ts +66 -0
- package/dist/replay-live-feed-types.js +7 -0
- package/dist/replay-live-feed.d.ts +49 -0
- package/dist/replay-live-feed.js +126 -0
- package/dist/rest-binding.d.ts +215 -0
- package/dist/rest-binding.js +95 -0
- package/dist/rest-body.d.ts +17 -0
- package/dist/rest-body.js +95 -0
- package/dist/route-model.d.ts +305 -0
- package/dist/route-model.js +320 -0
- package/dist/runtime/cors.d.ts +161 -0
- package/dist/runtime/cors.js +265 -0
- package/dist/scripts/generate-operation-client.d.ts +186 -0
- package/dist/scripts/generate-operation-client.js +561 -0
- package/dist/scripts/operation-behaviors.d.ts +3 -0
- package/dist/scripts/operation-behaviors.js +16 -0
- package/dist/scripts/operation-client-aliases.d.ts +32 -0
- package/dist/scripts/operation-client-aliases.js +255 -0
- package/dist/scripts/operation-client-domains.d.ts +21 -0
- package/dist/scripts/operation-client-domains.js +74 -0
- package/dist/scripts/operation-client-schema.d.ts +46 -0
- package/dist/scripts/operation-client-schema.js +310 -0
- package/dist/scripts/operation-client-svelte.d.ts +3 -0
- package/dist/scripts/operation-client-svelte.js +61 -0
- package/dist/sequence-cursor.d.ts +46 -0
- package/dist/sequence-cursor.js +62 -0
- package/dist/sse-error-payload-schema.d.ts +12 -0
- package/dist/sse-error-payload-schema.js +33 -0
- package/dist/stdio-session.d.ts +71 -0
- package/dist/stdio-session.js +347 -0
- package/dist/zod-json-schema.d.ts +22 -0
- package/dist/zod-json-schema.js +49 -0
- package/package.json +38 -3
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
function unwrap(schema) {
|
|
3
|
+
for (;;) {
|
|
4
|
+
if (schema instanceof z.ZodOptional ||
|
|
5
|
+
schema instanceof z.ZodNullable ||
|
|
6
|
+
schema instanceof z.ZodDefault) {
|
|
7
|
+
schema = schema.unwrap();
|
|
8
|
+
continue;
|
|
9
|
+
}
|
|
10
|
+
if (schema instanceof z.ZodUnion) {
|
|
11
|
+
const options = schema.options.filter((option) => !(option instanceof z.ZodNull) && !(option instanceof z.ZodUndefined));
|
|
12
|
+
if (options.length === 1) {
|
|
13
|
+
schema = options[0];
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return schema;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function field(schema, name, operation) {
|
|
21
|
+
const object = unwrap(schema);
|
|
22
|
+
if (!(object instanceof z.ZodObject) || !Object.hasOwn(object.shape, name)) {
|
|
23
|
+
throw new Error(`operation "${operation}" behavior references missing field "${name}"`);
|
|
24
|
+
}
|
|
25
|
+
return object.shape[name];
|
|
26
|
+
}
|
|
27
|
+
function accepts(producer, consumer, pagination = false) {
|
|
28
|
+
if (!pagination && !acceptsAbsence(producer, consumer))
|
|
29
|
+
return false;
|
|
30
|
+
if (!isComparable(unwrap(producer)) || !isComparable(unwrap(consumer)))
|
|
31
|
+
return false;
|
|
32
|
+
try {
|
|
33
|
+
const source = z.toJSONSchema(unwrap(producer));
|
|
34
|
+
const target = z.toJSONSchema(unwrap(consumer));
|
|
35
|
+
if (JSON.stringify(canonicalSchema(source)) === JSON.stringify(canonicalSchema(target)))
|
|
36
|
+
return true;
|
|
37
|
+
return stringSubset(source, target);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function acceptsAbsence(producer, consumer) {
|
|
44
|
+
return ['null', 'undefined'].every((kind) => !allows(producer, kind) || allows(consumer, kind));
|
|
45
|
+
}
|
|
46
|
+
function allows(schema, kind) {
|
|
47
|
+
const terminatingTypes = kind === 'null' ? [z.ZodNull, z.ZodNullable] : [z.ZodUndefined, z.ZodOptional];
|
|
48
|
+
if (terminatingTypes.some((type) => schema instanceof type))
|
|
49
|
+
return true;
|
|
50
|
+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable)
|
|
51
|
+
return allows(schema.unwrap(), kind);
|
|
52
|
+
if (schema instanceof z.ZodUnion)
|
|
53
|
+
return schema.options.some((option) => allows(option, kind));
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
function stringValues(schema) {
|
|
57
|
+
if (typeof schema['const'] === 'string')
|
|
58
|
+
return [schema['const']];
|
|
59
|
+
if (Array.isArray(schema['enum']) && schema['enum'].every((value) => typeof value === 'string'))
|
|
60
|
+
return schema['enum'];
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
function stringSubset(source, target) {
|
|
64
|
+
if (source['type'] !== 'string' || target['type'] !== 'string')
|
|
65
|
+
return false;
|
|
66
|
+
const supported = new Set([
|
|
67
|
+
'$schema',
|
|
68
|
+
'description',
|
|
69
|
+
'type',
|
|
70
|
+
'enum',
|
|
71
|
+
'const',
|
|
72
|
+
'minLength',
|
|
73
|
+
'maxLength',
|
|
74
|
+
'pattern',
|
|
75
|
+
'format',
|
|
76
|
+
]);
|
|
77
|
+
if (Object.keys(target).some((key) => !supported.has(key)))
|
|
78
|
+
return false;
|
|
79
|
+
const values = stringValues(source);
|
|
80
|
+
const accepted = stringValues(target);
|
|
81
|
+
if (accepted && (!values || values.some((value) => !accepted.includes(value))))
|
|
82
|
+
return false;
|
|
83
|
+
for (const key of ['pattern', 'format'])
|
|
84
|
+
if (target[key] !== undefined && source[key] !== target[key])
|
|
85
|
+
return false;
|
|
86
|
+
return lengthSubset(source, target, values);
|
|
87
|
+
}
|
|
88
|
+
function lengthSubset(source, target, values) {
|
|
89
|
+
for (const [key, compare] of [
|
|
90
|
+
['minLength', (left, right) => left >= right],
|
|
91
|
+
['maxLength', (left, right) => left <= right],
|
|
92
|
+
]) {
|
|
93
|
+
const bound = target[key];
|
|
94
|
+
if (bound === undefined)
|
|
95
|
+
continue;
|
|
96
|
+
if (typeof bound !== 'number')
|
|
97
|
+
return false;
|
|
98
|
+
if (values) {
|
|
99
|
+
if (values.some((value) => !compare(value.length, bound)))
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
else if (typeof source[key] !== 'number' || !compare(source[key], bound))
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
function canonicalSchema(value, key = '') {
|
|
108
|
+
if (Array.isArray(value)) {
|
|
109
|
+
const items = value.map((entry) => canonicalSchema(entry));
|
|
110
|
+
return key === 'enum' || key === 'required'
|
|
111
|
+
? items.toSorted((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)))
|
|
112
|
+
: items;
|
|
113
|
+
}
|
|
114
|
+
if (value === null || typeof value !== 'object')
|
|
115
|
+
return value;
|
|
116
|
+
return Object.fromEntries(Object.entries(value)
|
|
117
|
+
.toSorted(([left], [right]) => left.localeCompare(right))
|
|
118
|
+
.map(([entryKey, entry]) => [entryKey, canonicalSchema(entry, entryKey)]));
|
|
119
|
+
}
|
|
120
|
+
function isComparable(schema, seen = new Set()) {
|
|
121
|
+
if (seen.has(schema))
|
|
122
|
+
return true;
|
|
123
|
+
seen.add(schema);
|
|
124
|
+
if (schema instanceof z.ZodDefault ||
|
|
125
|
+
schema instanceof z.ZodPrefault ||
|
|
126
|
+
schema instanceof z.ZodCatch ||
|
|
127
|
+
schema instanceof z.ZodLazy ||
|
|
128
|
+
hasCallbackChecks(schema))
|
|
129
|
+
return false;
|
|
130
|
+
return childSchemas(schema).every((child) => isComparable(child, seen));
|
|
131
|
+
}
|
|
132
|
+
function hasCallbackChecks(schema) {
|
|
133
|
+
return (schema.def.checks ?? []).some((check) => {
|
|
134
|
+
const kind = checkKind(check);
|
|
135
|
+
return kind === 'custom' || kind === 'overwrite';
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function hasOverwrite(schema) {
|
|
139
|
+
return (schema.def.checks ?? []).some((check) => checkKind(check) === 'overwrite');
|
|
140
|
+
}
|
|
141
|
+
function checkKind(check) {
|
|
142
|
+
const internals = Object.getOwnPropertyDescriptor(check, '_zod')?.value;
|
|
143
|
+
return internals?.def?.check;
|
|
144
|
+
}
|
|
145
|
+
function rejectTransforms(schema, operation, seen = new Set()) {
|
|
146
|
+
if (seen.has(schema))
|
|
147
|
+
return;
|
|
148
|
+
seen.add(schema);
|
|
149
|
+
if (schema instanceof z.ZodPipe ||
|
|
150
|
+
schema instanceof z.ZodTransform ||
|
|
151
|
+
schema instanceof z.ZodLazy ||
|
|
152
|
+
hasOverwrite(schema))
|
|
153
|
+
throw new Error(`operation "${operation}" behavior does not support schema transforms`);
|
|
154
|
+
for (const child of childSchemas(schema))
|
|
155
|
+
rejectTransforms(child, operation, seen);
|
|
156
|
+
}
|
|
157
|
+
function narrowed(schema, when, name) {
|
|
158
|
+
const base = unwrap(schema);
|
|
159
|
+
const variants = base instanceof z.ZodUnion ? base.options : [base];
|
|
160
|
+
const matches = variants.filter((variant) => Object.entries(when).every(([key, value]) => {
|
|
161
|
+
try {
|
|
162
|
+
const discriminator = unwrap(field(variant, key, name));
|
|
163
|
+
if (hasCallbackChecks(discriminator))
|
|
164
|
+
return false;
|
|
165
|
+
if (discriminator instanceof z.ZodLiteral)
|
|
166
|
+
return discriminator.values.has(value);
|
|
167
|
+
if (discriminator instanceof z.ZodEnum)
|
|
168
|
+
return discriminator.options.includes(value);
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}));
|
|
175
|
+
if (matches.length !== 1)
|
|
176
|
+
throw new Error(`operation "${name}" behavior observation does not select one variant`);
|
|
177
|
+
return matches[0];
|
|
178
|
+
}
|
|
179
|
+
function childSchemas(schema) {
|
|
180
|
+
const children = schema instanceof z.ZodObject ? Object.values(schema.shape) : [];
|
|
181
|
+
const visited = new Set();
|
|
182
|
+
const pending = [schema.def];
|
|
183
|
+
while (pending.length) {
|
|
184
|
+
const value = pending.pop();
|
|
185
|
+
if (value instanceof z.ZodType) {
|
|
186
|
+
children.push(value);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (value === null || typeof value !== 'object' || visited.has(value))
|
|
190
|
+
continue;
|
|
191
|
+
visited.add(value);
|
|
192
|
+
for (const descriptor of Object.values(Object.getOwnPropertyDescriptors(value))) {
|
|
193
|
+
if ('value' in descriptor)
|
|
194
|
+
pending.push(descriptor.value);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return children;
|
|
198
|
+
}
|
|
199
|
+
function validatePage(operation, page) {
|
|
200
|
+
const name = operation.name;
|
|
201
|
+
const items = unwrap(field(operation.outputSchema, page.items, name));
|
|
202
|
+
if (!(items instanceof z.ZodArray))
|
|
203
|
+
throw new Error(`operation "${name}" pagination items must be an array`);
|
|
204
|
+
const item = unwrap(items.element);
|
|
205
|
+
const variants = item instanceof z.ZodUnion ? item.options : [item];
|
|
206
|
+
for (const key of page.itemKey)
|
|
207
|
+
for (const variant of variants)
|
|
208
|
+
field(variant, key, name);
|
|
209
|
+
for (const filter of page.cursorFilters)
|
|
210
|
+
field(operation.inputSchema, filter, name);
|
|
211
|
+
if (!(unwrap(field(operation.outputSchema, page.total, name)) instanceof z.ZodNumber))
|
|
212
|
+
throw new Error(`operation "${name}" pagination total must be a number`);
|
|
213
|
+
if (!accepts(field(operation.outputSchema, page.nextCursor, name), field(operation.inputSchema, page.cursorInput, name), true))
|
|
214
|
+
throw new Error(`operation "${name}" pagination cursors are incompatible`);
|
|
215
|
+
}
|
|
216
|
+
function validateRead(operation, behavior) {
|
|
217
|
+
const name = operation.name;
|
|
218
|
+
const forbidden = [
|
|
219
|
+
'invalidates',
|
|
220
|
+
'observedInput',
|
|
221
|
+
'replay',
|
|
222
|
+
'responseDisposition',
|
|
223
|
+
'conflictReasons',
|
|
224
|
+
'effects',
|
|
225
|
+
];
|
|
226
|
+
if (forbidden.some((key) => Object.hasOwn(behavior, key)))
|
|
227
|
+
throw new Error(`operation "${name}" has conflicting read and command behavior`);
|
|
228
|
+
if (operation.destructive)
|
|
229
|
+
throw new Error(`operation "${name}" cannot be a destructive read`);
|
|
230
|
+
for (const key of behavior.key ?? [])
|
|
231
|
+
field(operation.inputSchema, key, name);
|
|
232
|
+
if (behavior.pagination)
|
|
233
|
+
validatePage(operation, behavior.pagination);
|
|
234
|
+
if (behavior.cursor)
|
|
235
|
+
field(operation.eventSchema ?? operation.outputSchema, behavior.cursor, name);
|
|
236
|
+
}
|
|
237
|
+
function lookupRead(byName, target, name) {
|
|
238
|
+
const read = byName.get(target);
|
|
239
|
+
if (read?.behavior?.kind !== 'read')
|
|
240
|
+
throw new Error(`operation "${name}" references missing read "${target}"`);
|
|
241
|
+
return read;
|
|
242
|
+
}
|
|
243
|
+
function validateObservation(operation, observed, byName) {
|
|
244
|
+
const name = operation.name;
|
|
245
|
+
const read = lookupRead(byName, observed.read, name);
|
|
246
|
+
const output = narrowed(read.outputSchema, observed.when, name);
|
|
247
|
+
for (const key of [observed.key, observed.revision]) {
|
|
248
|
+
if (!accepts(field(output, key, name), field(operation.inputSchema, key, name)))
|
|
249
|
+
throw new Error(`operation "${name}" observation field "${key}" is incompatible`);
|
|
250
|
+
}
|
|
251
|
+
if (!read.behavior.key?.includes(observed.key))
|
|
252
|
+
throw new Error(`operation "${name}" observation key is not declared by its read`);
|
|
253
|
+
if (!accepts(field(operation.inputSchema, observed.key, name), field(read.inputSchema, observed.key, name)))
|
|
254
|
+
throw new Error(`operation "${name}" observation key is incompatible with its read`);
|
|
255
|
+
}
|
|
256
|
+
function validateReplay(operation, replay, byName) {
|
|
257
|
+
const name = operation.name;
|
|
258
|
+
field(operation.inputSchema, replay.key, name);
|
|
259
|
+
const resource = field(operation.outputSchema, replay.resource, name);
|
|
260
|
+
const read = lookupRead(byName, replay.read, name);
|
|
261
|
+
if (!read.behavior.key?.includes(replay.resource) ||
|
|
262
|
+
!accepts(resource, field(read.inputSchema, replay.resource, name)))
|
|
263
|
+
throw new Error(`operation "${name}" replay resource is incompatible with its read`);
|
|
264
|
+
}
|
|
265
|
+
function validateCommand(operation, behavior, byName) {
|
|
266
|
+
const name = operation.name;
|
|
267
|
+
if (['key', 'pagination', 'cursor'].some((key) => Object.hasOwn(behavior, key)))
|
|
268
|
+
throw new Error(`operation "${name}" has conflicting read and command behavior`);
|
|
269
|
+
for (const target of behavior.invalidates ?? [])
|
|
270
|
+
lookupRead(byName, target, name);
|
|
271
|
+
for (const reason of behavior.conflictReasons ?? []) {
|
|
272
|
+
if (!operation.conflictReasonTokens?.includes(reason))
|
|
273
|
+
throw new Error(`operation "${name}" references unknown conflict reason "${reason}"`);
|
|
274
|
+
}
|
|
275
|
+
if (behavior.observedInput)
|
|
276
|
+
validateObservation(operation, behavior.observedInput, byName);
|
|
277
|
+
if (behavior.replay)
|
|
278
|
+
validateReplay(operation, behavior.replay, byName);
|
|
279
|
+
}
|
|
280
|
+
/** Check declarative references before a host can start. */
|
|
281
|
+
export function validateOperationBehaviors(operations) {
|
|
282
|
+
const byName = new Map(operations.map((operation) => [operation.name, operation]));
|
|
283
|
+
for (const operation of operations) {
|
|
284
|
+
const behavior = operation.behavior;
|
|
285
|
+
if (!behavior)
|
|
286
|
+
continue;
|
|
287
|
+
rejectTransforms(operation.inputSchema, operation.name);
|
|
288
|
+
rejectTransforms(operation.outputSchema, operation.name);
|
|
289
|
+
if (operation.eventSchema)
|
|
290
|
+
rejectTransforms(operation.eventSchema, operation.name);
|
|
291
|
+
if (behavior.kind === 'read')
|
|
292
|
+
validateRead(operation, behavior);
|
|
293
|
+
else if (behavior.kind === 'command')
|
|
294
|
+
validateCommand(operation, behavior, byName);
|
|
295
|
+
else
|
|
296
|
+
throw new Error(`operation "${operation.name}" has invalid behavior kind`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { OperationFault } from "../operation-fault.js";
|
|
3
|
+
import { type DispatchContext, type DispatchResult, type ErasedOperation } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Look up an operation by name. Emits the `looked-up` trace marker on
|
|
6
|
+
* success. On failure (unknown operation) emits no marker, matching the
|
|
7
|
+
* existing dispatch behavior where `MethodNotFound` ends the pipeline
|
|
8
|
+
* before any marker is recorded.
|
|
9
|
+
*/
|
|
10
|
+
export declare function lookupOperation(operationName: string, context: DispatchContext): DispatchResult<ErasedOperation>;
|
|
11
|
+
/**
|
|
12
|
+
* Safely run `schema.safeParse(value)`. Maps any thrown exception or
|
|
13
|
+
* failed parse to `{ code: 'EngineFailure', message: 'internal error', data: {} }`.
|
|
14
|
+
* On success returns the parsed data so Zod transforms, defaults, and
|
|
15
|
+
* refinements are honored.
|
|
16
|
+
*
|
|
17
|
+
* The registry intentionally erases operation-specific output types. A
|
|
18
|
+
* name-only dispatch cannot prove a domain result to its caller, so the
|
|
19
|
+
* parsed value remains unknown at this boundary. Zod's parser is still the
|
|
20
|
+
* source of truth for transforms, defaults, and refinements.
|
|
21
|
+
*/
|
|
22
|
+
export declare function validateOutputAgainstSchema(schema: z.ZodType, value: unknown): DispatchResult<unknown>;
|
|
23
|
+
/** Wrap an `OperationFault` in the standard `DispatchResult` failure shape. */
|
|
24
|
+
export declare function dispatchFailure(fault: OperationFault): DispatchResult<never>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { tracePipeline } from "./pipeline-stages.js";
|
|
3
|
+
import {} from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Look up an operation by name. Emits the `looked-up` trace marker on
|
|
6
|
+
* success. On failure (unknown operation) emits no marker, matching the
|
|
7
|
+
* existing dispatch behavior where `MethodNotFound` ends the pipeline
|
|
8
|
+
* before any marker is recorded.
|
|
9
|
+
*/
|
|
10
|
+
export function lookupOperation(operationName, context) {
|
|
11
|
+
const operation = context.registry.get(operationName);
|
|
12
|
+
if (operation === undefined) {
|
|
13
|
+
return dispatchFailure({
|
|
14
|
+
code: 'MethodNotFound',
|
|
15
|
+
message: `unknown operation: ${operationName}`,
|
|
16
|
+
data: { method: operationName },
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
tracePipeline(context.pipelineTrace, 'looked-up');
|
|
20
|
+
return { ok: true, value: operation };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Safely run `schema.safeParse(value)`. Maps any thrown exception or
|
|
24
|
+
* failed parse to `{ code: 'EngineFailure', message: 'internal error', data: {} }`.
|
|
25
|
+
* On success returns the parsed data so Zod transforms, defaults, and
|
|
26
|
+
* refinements are honored.
|
|
27
|
+
*
|
|
28
|
+
* The registry intentionally erases operation-specific output types. A
|
|
29
|
+
* name-only dispatch cannot prove a domain result to its caller, so the
|
|
30
|
+
* parsed value remains unknown at this boundary. Zod's parser is still the
|
|
31
|
+
* source of truth for transforms, defaults, and refinements.
|
|
32
|
+
*/
|
|
33
|
+
export function validateOutputAgainstSchema(schema, value) {
|
|
34
|
+
let parseResult;
|
|
35
|
+
try {
|
|
36
|
+
parseResult = schema.safeParse(value);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return dispatchFailure({ code: 'EngineFailure', message: 'internal error', data: {} });
|
|
40
|
+
}
|
|
41
|
+
if (!parseResult.success) {
|
|
42
|
+
return dispatchFailure({ code: 'EngineFailure', message: 'internal error', data: {} });
|
|
43
|
+
}
|
|
44
|
+
return { ok: true, value: parseResult.data };
|
|
45
|
+
}
|
|
46
|
+
/** Wrap an `OperationFault` in the standard `DispatchResult` failure shape. */
|
|
47
|
+
export function dispatchFailure(fault) {
|
|
48
|
+
return { ok: false, fault };
|
|
49
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport-neutral operation catalog and the single dispatch pipeline.
|
|
3
|
+
*
|
|
4
|
+
* `executeOperation` is the only function transport adapters call to invoke an
|
|
5
|
+
* operation. REST, JSON-RPC HTTP, JSON-RPC WebSocket, and stdio all share one
|
|
6
|
+
* access check, one input validation step, one authorization hook, and one
|
|
7
|
+
* error classifier.
|
|
8
|
+
*
|
|
9
|
+
* @module server/operation-catalog
|
|
10
|
+
*/
|
|
11
|
+
export type { FaultCode } from "../fault-code.js";
|
|
12
|
+
export { DISPATCH_ALLOWLIST } from "./dispatch-allowlist.js";
|
|
13
|
+
export { classifyEngineError } from "./pipeline-helpers.js";
|
|
14
|
+
export { executeOperation } from "./pipeline.js";
|
|
15
|
+
export { createOperationRegistry, type OperationRegistryOptions } from "./registry.js";
|
|
16
|
+
export { SubscriptionElementValidationError, executeStream, executeSubscription, } from "./stream-pipeline.js";
|
|
17
|
+
export { OPERATION_NAME_PATTERN, isValidOperationName, validateOperationName, type AuthorizationDecision, type DispatchContext, type DispatchResult, type ErasedOperation, type FaultPolicy, type McpToolMetadata, type OperationBehavior, type OperationContext, type OperationDefinition, type OperationInvocationResult, type OperationKind, type OperationRegistry, type ParameterizedAccessHint, type PipelineTrace, type PipelineTraceMarker, type RegistrableOperation, type StreamOperationInvocation, type SubscriptionOperationInvocation, type SubscriptionStartEnvelope, type TransportAvailability, type UnknownKeyDisposition, type UnknownKeyPolicy, } from "./types.js";
|
|
18
|
+
export { UNIVERSAL_FAULT_DEFAULTS } from "./universal-faults.js";
|
|
19
|
+
export type { OperationHostFactory, OperationInterceptor } from "./operation-interceptors.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport-neutral operation catalog and the single dispatch pipeline.
|
|
3
|
+
*
|
|
4
|
+
* `executeOperation` is the only function transport adapters call to invoke an
|
|
5
|
+
* operation. REST, JSON-RPC HTTP, JSON-RPC WebSocket, and stdio all share one
|
|
6
|
+
* access check, one input validation step, one authorization hook, and one
|
|
7
|
+
* error classifier.
|
|
8
|
+
*
|
|
9
|
+
* @module server/operation-catalog
|
|
10
|
+
*/
|
|
11
|
+
export { DISPATCH_ALLOWLIST } from "./dispatch-allowlist.js";
|
|
12
|
+
export { classifyEngineError } from "./pipeline-helpers.js";
|
|
13
|
+
export { executeOperation } from "./pipeline.js";
|
|
14
|
+
export { createOperationRegistry } from "./registry.js";
|
|
15
|
+
export { SubscriptionElementValidationError, executeStream, executeSubscription, } from "./stream-pipeline.js";
|
|
16
|
+
export { OPERATION_NAME_PATTERN, isValidOperationName, validateOperationName, } from "./types.js";
|
|
17
|
+
export { UNIVERSAL_FAULT_DEFAULTS } from "./universal-faults.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DispatchContext, DispatchResult, OperationContext, OperationKind, OperationMetadata } from "./types.js";
|
|
2
|
+
type DispatchableOperation<Host> = {
|
|
3
|
+
readonly rawInput: unknown;
|
|
4
|
+
readonly context: DispatchContext<Host>;
|
|
5
|
+
};
|
|
6
|
+
type InvokableOperation<Input, SchemaOutput, Host> = OperationMetadata<Input, SchemaOutput, Host> & {
|
|
7
|
+
readonly kind?: OperationKind;
|
|
8
|
+
readonly invoke: (context: OperationContext<Input, Host>) => Promise<unknown>;
|
|
9
|
+
};
|
|
10
|
+
/** Run the common pipeline before any unary, stream, or subscription invocation. */
|
|
11
|
+
export declare function dispatchOperation<Input, SchemaOutput, Host>(operation: InvokableOperation<Input, SchemaOutput, Host>, { rawInput, context }: DispatchableOperation<Host>): Promise<DispatchResult<unknown>>;
|
|
12
|
+
/** Evaluate transport, access, input, and application policy without invoking the operation. */
|
|
13
|
+
export declare function checkOperationPolicy<Input, SchemaOutput, Host>(operation: OperationMetadata<Input, SchemaOutput, Host>, rawInput: unknown, context: DispatchContext<Host>): Promise<DispatchResult<void>>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { classifyEngineError } from "./pipeline-helpers.js";
|
|
2
|
+
import { checkAccess, checkAuthorization, checkTransport, parseAndApplyUnknownKeyPolicy, tracePipeline, } from "./pipeline-stages.js";
|
|
3
|
+
import { closeAbandonedSubscription, isSubscriptionInvocation } from "./subscription-lifecycle.js";
|
|
4
|
+
/** Run the common pipeline before any unary, stream, or subscription invocation. */
|
|
5
|
+
export async function dispatchOperation(operation, { rawInput, context }) {
|
|
6
|
+
const prepared = await prepareTypedInput(operation, rawInput, context);
|
|
7
|
+
if (!prepared.ok)
|
|
8
|
+
return prepared;
|
|
9
|
+
try {
|
|
10
|
+
if (context.transport === 'mcp')
|
|
11
|
+
context.signal?.throwIfAborted();
|
|
12
|
+
return await invokeWithInterceptors(operation, prepared.value, context);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
return { ok: false, fault: classifyEngineError(error, operation, context.faultPolicy) };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function invokeWithInterceptors(operation, invocation, context) {
|
|
19
|
+
let settled = false;
|
|
20
|
+
let started;
|
|
21
|
+
const invoke = async () => {
|
|
22
|
+
if (settled)
|
|
23
|
+
return { ok: false, fault: { code: 'EngineFailure', message: 'internal error', data: {} } };
|
|
24
|
+
if (context.transport === 'mcp')
|
|
25
|
+
context.signal?.throwIfAborted();
|
|
26
|
+
const value = await operation.invoke(invocation);
|
|
27
|
+
tracePipeline(context.pipelineTrace, 'invoked');
|
|
28
|
+
if (operation.kind === 'subscription' && isSubscriptionInvocation(value)) {
|
|
29
|
+
if (settled)
|
|
30
|
+
await closeAbandonedSubscription(value);
|
|
31
|
+
else
|
|
32
|
+
started = value;
|
|
33
|
+
}
|
|
34
|
+
return { ok: true, value };
|
|
35
|
+
};
|
|
36
|
+
if (context.interceptors === undefined || context.interceptors.length === 0)
|
|
37
|
+
return invoke();
|
|
38
|
+
let result;
|
|
39
|
+
try {
|
|
40
|
+
result = await callInterceptors(operation, invocation, context.interceptors, invoke);
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
settled = true;
|
|
45
|
+
if (started !== undefined && !retainsSubscription(result, started)) {
|
|
46
|
+
await closeAbandonedSubscription(started);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function callInterceptors(operation, invocation, interceptors, invoke) {
|
|
51
|
+
const { kind, summary, tags, destructive, access } = operation;
|
|
52
|
+
const { input, principal, transport, signal } = invocation;
|
|
53
|
+
const interception = {
|
|
54
|
+
name: operation.name,
|
|
55
|
+
metadata: { ...(kind === undefined ? {} : { kind }), summary, tags, destructive, access },
|
|
56
|
+
input,
|
|
57
|
+
principal,
|
|
58
|
+
transport,
|
|
59
|
+
...(signal === undefined ? {} : { signal }),
|
|
60
|
+
};
|
|
61
|
+
const chain = interceptors.reduceRight((next, interceptor) => {
|
|
62
|
+
let called;
|
|
63
|
+
return () => interceptor(interception, () => (called ??= next()));
|
|
64
|
+
}, invoke);
|
|
65
|
+
return chain();
|
|
66
|
+
}
|
|
67
|
+
function retainsSubscription(result, started) {
|
|
68
|
+
return (result?.ok === true &&
|
|
69
|
+
isSubscriptionInvocation(result.value) &&
|
|
70
|
+
(result.value.close === started.close ||
|
|
71
|
+
(result.value.subscriptionId === started.subscriptionId &&
|
|
72
|
+
result.value.iterable === started.iterable)));
|
|
73
|
+
}
|
|
74
|
+
async function prepareTypedInput(operation, rawInput, context) {
|
|
75
|
+
const transportFailure = checkTransport(operation, context);
|
|
76
|
+
if (transportFailure !== null)
|
|
77
|
+
return transportFailure;
|
|
78
|
+
tracePipeline(context.pipelineTrace, 'transport-checked');
|
|
79
|
+
const accessFailure = checkAccess(operation, context);
|
|
80
|
+
if (accessFailure !== null)
|
|
81
|
+
return accessFailure;
|
|
82
|
+
tracePipeline(context.pipelineTrace, 'access-checked');
|
|
83
|
+
const parseOutcome = parseAndApplyUnknownKeyPolicy(operation, rawInput, context.transport === 'http-rest' ? 'http' : 'jsonRpc', context.pipelineTrace);
|
|
84
|
+
if (parseOutcome.kind === 'failure')
|
|
85
|
+
return { ok: false, fault: parseOutcome.fault };
|
|
86
|
+
const { principal, transport, signal } = context;
|
|
87
|
+
const source = context.host;
|
|
88
|
+
let host;
|
|
89
|
+
try {
|
|
90
|
+
host =
|
|
91
|
+
typeof source === 'function'
|
|
92
|
+
? await source({ principal, transport })
|
|
93
|
+
: source;
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return { ok: false, fault: { code: 'EngineFailure', message: 'internal error', data: {} } };
|
|
97
|
+
}
|
|
98
|
+
const invocation = {
|
|
99
|
+
input: parseOutcome.input,
|
|
100
|
+
principal,
|
|
101
|
+
host,
|
|
102
|
+
transport,
|
|
103
|
+
...(signal === undefined ? {} : { signal }),
|
|
104
|
+
};
|
|
105
|
+
const authorizationFailure = await checkAuthorization(operation, { ...invocation });
|
|
106
|
+
if (authorizationFailure !== null)
|
|
107
|
+
return authorizationFailure;
|
|
108
|
+
tracePipeline(context.pipelineTrace, 'authorized');
|
|
109
|
+
return { ok: true, value: invocation };
|
|
110
|
+
}
|
|
111
|
+
/** Evaluate transport, access, input, and application policy without invoking the operation. */
|
|
112
|
+
export async function checkOperationPolicy(operation, rawInput, context) {
|
|
113
|
+
const result = await prepareTypedInput(operation, rawInput, context);
|
|
114
|
+
return result.ok ? { ok: true, value: undefined } : result;
|
|
115
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { TransportKind } from "../operation-fault.js";
|
|
2
|
+
import type { Principal } from "../principal.js";
|
|
3
|
+
import type { DispatchResult, ErasedOperation, OperationContext } from "./types.js";
|
|
4
|
+
/** Resolves the operation host from an admitted caller and its transport. */
|
|
5
|
+
export type OperationHostFactory<Host> = (context: {
|
|
6
|
+
readonly principal: Principal;
|
|
7
|
+
readonly transport: TransportKind;
|
|
8
|
+
}) => Host | Promise<Host>;
|
|
9
|
+
/** Operation details available to interceptors after access checks and input validation. */
|
|
10
|
+
export type OperationInterceptorContext = Pick<OperationContext<unknown>, 'input' | 'principal' | 'transport' | 'signal'> & {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly metadata: Pick<ErasedOperation, 'kind' | 'summary' | 'tags' | 'destructive' | 'access'>;
|
|
13
|
+
};
|
|
14
|
+
/** Wrap invocation of an authorized operation. Successful results still undergo output validation. */
|
|
15
|
+
export type OperationInterceptor = (context: OperationInterceptorContext, next: () => Promise<DispatchResult<unknown>>) => Promise<DispatchResult<unknown>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { OperationDefinitionBase } from "./types.js";
|
|
2
|
+
type OperationMetadata = Omit<OperationDefinitionBase<unknown, unknown>, 'authorize'>;
|
|
3
|
+
/** Select metadata explicitly, so registry entries never expose authoring callbacks. */
|
|
4
|
+
export declare function snapshotOperationMetadata(operation: OperationMetadata): OperationMetadata;
|
|
5
|
+
export {};
|