@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,33 @@
|
|
|
1
|
+
import { dispatchFailure, lookupOperation, validateOutputAgainstSchema, } from "./dispatch-preparation.js";
|
|
2
|
+
import { tracePipeline } from "./pipeline-stages.js";
|
|
3
|
+
import {} from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Single request/response dispatch pipeline. Every request/response transport
|
|
6
|
+
* call goes through the same transport, access, input validation,
|
|
7
|
+
* authorization, invocation, and output-validation stages.
|
|
8
|
+
*/
|
|
9
|
+
export async function executeOperation(operationName, rawInput, context) {
|
|
10
|
+
const lookup = lookupOperation(operationName, context);
|
|
11
|
+
if (!lookup.ok)
|
|
12
|
+
return lookup;
|
|
13
|
+
const operation = lookup.value;
|
|
14
|
+
const operationKind = operation.kind ?? 'unary';
|
|
15
|
+
// REST streaming bindings use this pipeline so shapeSuccess can emit SSE.
|
|
16
|
+
// Request/response JSON-RPC transports must reject stream operations before invoke.
|
|
17
|
+
if (operationKind === 'subscription' ||
|
|
18
|
+
(operationKind === 'stream' && context.transport !== 'http-rest')) {
|
|
19
|
+
return dispatchFailure({
|
|
20
|
+
code: 'Unprocessable',
|
|
21
|
+
message: `operation "${operation.name}" is not unary`,
|
|
22
|
+
data: { reason: `operation kind is "${operationKind}"` },
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const dispatched = await operation.dispatch(rawInput, context);
|
|
26
|
+
if (!dispatched.ok)
|
|
27
|
+
return dispatched;
|
|
28
|
+
const output = dispatched.value;
|
|
29
|
+
const outputResult = validateOutputAgainstSchema(operation.outputSchema, output);
|
|
30
|
+
if (outputResult.ok)
|
|
31
|
+
tracePipeline(context.pipelineTrace, 'output-validated');
|
|
32
|
+
return outputResult;
|
|
33
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { OperationInterceptor } from "./operation-interceptors.js";
|
|
2
|
+
import { type FaultPolicy, type OperationRegistry, type RegistrableOperation } from "./types.js";
|
|
3
|
+
/** Policies shared by every operation registered by this host. */
|
|
4
|
+
export type OperationRegistryOptions = {
|
|
5
|
+
/** Classification and masking settings for operation faults. */
|
|
6
|
+
readonly faultPolicy?: FaultPolicy | undefined;
|
|
7
|
+
/** Ordered interceptors for authorized operation invocations. */
|
|
8
|
+
readonly interceptors?: readonly OperationInterceptor[] | undefined;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Build an immutable registry. Throws on duplicate names, invalid names,
|
|
12
|
+
* non-object input schemas, and unsafe top-level schema keys.
|
|
13
|
+
*
|
|
14
|
+
* `faultPolicy` is the host's say over thrown faults (see {@link FaultPolicy}); omitted, a
|
|
15
|
+
* thrown fault that its operation does not declare answers `EngineFailure`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createOperationRegistry<Host = unknown>(operations: ReadonlyArray<RegistrableOperation<Host>>, options?: OperationRegistryOptions): OperationRegistry;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { validateOperationBehaviors } from "./behavior-validation.js";
|
|
3
|
+
import { snapshotOperationMetadata } from "./operation-metadata.js";
|
|
4
|
+
import { UNSAFE_PROTOTYPE_KEYS } from "./pipeline-helpers.js";
|
|
5
|
+
import { validateOperationName, } from "./types.js";
|
|
6
|
+
function objectInputSchema(operation) {
|
|
7
|
+
if (!(operation.inputSchema instanceof z.ZodObject)) {
|
|
8
|
+
throw new Error(`operation "${operation.name}" inputSchema must be a z.ZodObject (got ${operation.inputSchema.constructor.name}); wrappers like .optional() / transforms hide the top-level shape from the unknown-key policy check`);
|
|
9
|
+
}
|
|
10
|
+
return operation.inputSchema;
|
|
11
|
+
}
|
|
12
|
+
function assertSafeDeclaredKeys(operation) {
|
|
13
|
+
const inputSchema = objectInputSchema(operation);
|
|
14
|
+
const declaredKeys = Object.keys(inputSchema.shape);
|
|
15
|
+
const unsafeDeclared = declaredKeys.filter((key) => UNSAFE_PROTOTYPE_KEYS.has(key));
|
|
16
|
+
if (unsafeDeclared.length > 0) {
|
|
17
|
+
throw new Error(`operation "${operation.name}" inputSchema declares unsafe top-level keys: ${unsafeDeclared.join(', ')}. Names that match a prototype-pollution vector (__proto__, constructor, prototype) are forbidden as schema keys.`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Mirror the `OperationDefinition` discriminated union at runtime:
|
|
22
|
+
* `kind: 'stream'` and `kind: 'subscription'` MUST carry an `eventSchema`,
|
|
23
|
+
* and `kind: 'unary'` (or absent) MUST NOT. The TypeScript discriminated
|
|
24
|
+
* union enforces this for callers using `defineOperation`, but
|
|
25
|
+
* `createOperationRegistry` accepts any `RegistrableOperation`-shaped
|
|
26
|
+
* value — including hand-rolled object literals constructed in test
|
|
27
|
+
* fixtures or third-party adapters that bypass `defineOperation`. Without
|
|
28
|
+
* this assertion a malformed registry would produce a runtime
|
|
29
|
+
* `EngineFailure` only on the first request, not at registry assembly.
|
|
30
|
+
*/
|
|
31
|
+
function assertKindAndEventSchemaAgree(operation) {
|
|
32
|
+
const kind = operation.kind ?? 'unary';
|
|
33
|
+
if (kind === 'unary') {
|
|
34
|
+
if (operation.eventSchema !== undefined) {
|
|
35
|
+
throw new Error(`operation "${operation.name}" has kind: 'unary' (or unset) but declares an eventSchema. eventSchema is reserved for kind: 'stream' or kind: 'subscription'.`);
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (operation.eventSchema === undefined) {
|
|
40
|
+
throw new Error(`operation "${operation.name}" has kind: '${kind}' but no eventSchema. Stream and subscription operations must declare an eventSchema so the dispatcher can validate each yielded element.`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Mirror the required `destructive` field at runtime. The TypeScript type
|
|
45
|
+
* forces every `defineOperation` caller to declare it, but
|
|
46
|
+
* `createOperationRegistry` accepts any `RegistrableOperation`-shaped value —
|
|
47
|
+
* including hand-rolled object literals from test fixtures or third-party
|
|
48
|
+
* adapters that bypass `defineOperation`. A missing or non-boolean
|
|
49
|
+
* `destructive` would let an operation slip past the explicit-declaration
|
|
50
|
+
* quality floor that the CLI confirmation gate, dashboard bulk-action
|
|
51
|
+
* confirmations, and MCP exposure all depend on. Reject at assembly so the
|
|
52
|
+
* error points at the registry, not at the first request.
|
|
53
|
+
*/
|
|
54
|
+
function assertDestructiveDeclared(operation) {
|
|
55
|
+
if (typeof operation.destructive !== 'boolean') {
|
|
56
|
+
throw new Error(`operation "${operation.name}" must declare an explicit boolean "destructive" flag (got ${typeof operation.destructive}). Every operation has to state whether it irreversibly mutates state — there is no implicit default. Mark cancel/purge/bulk-delete/raw-storage-write style operations true and read-only operations false.`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function assertMcpMetadataAgrees(operation) {
|
|
60
|
+
// `mcpTool` describes an engine-served workflow tool; it means nothing on an operation that is
|
|
61
|
+
// not exposed. An exposable operation without it is served by dispatch (`createOperationMcpMount`).
|
|
62
|
+
if (!operation.mcpExposable && operation.mcpTool !== undefined) {
|
|
63
|
+
throw new Error(`operation "${operation.name}" declares mcpTool metadata but is not mcpExposable`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The single point where an operation's `Host` is erased. `dispatch` is
|
|
68
|
+
* contravariant in `Host`, so an operation registered against `BureauHost`
|
|
69
|
+
* is not assignable to the `unknown`-hosted `ErasedOperation` the pipeline
|
|
70
|
+
* works with, and no transport can name a host type for a registry that
|
|
71
|
+
* mixes operations. Erasing here is sound because the host a transport puts
|
|
72
|
+
* into every `DispatchContext` is the one the registry's operations were
|
|
73
|
+
* registered against: `createOperationRegistry<Host>` took them as
|
|
74
|
+
* `RegistrableOperation<Host>`, and the handler that owns the registry is
|
|
75
|
+
* constructed with that same `Host`.
|
|
76
|
+
*/
|
|
77
|
+
function eraseHost(operation) {
|
|
78
|
+
return operation.dispatch;
|
|
79
|
+
}
|
|
80
|
+
function freezeOperation(operation, { faultPolicy, interceptors }) {
|
|
81
|
+
const metadata = snapshotOperationMetadata(operation);
|
|
82
|
+
const invoke = eraseHost(operation);
|
|
83
|
+
const dispatch = (input, context) => invoke(input, {
|
|
84
|
+
...context,
|
|
85
|
+
...(context.faultPolicy === undefined && faultPolicy !== undefined ? { faultPolicy } : {}),
|
|
86
|
+
...(interceptors === undefined ? {} : { interceptors }),
|
|
87
|
+
});
|
|
88
|
+
if (operation.kind === 'stream' || operation.kind === 'subscription') {
|
|
89
|
+
return Object.freeze({
|
|
90
|
+
...metadata,
|
|
91
|
+
kind: operation.kind,
|
|
92
|
+
eventSchema: operation.eventSchema,
|
|
93
|
+
dispatch,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return Object.freeze({ ...metadata, kind: 'unary', dispatch });
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Build an immutable registry. Throws on duplicate names, invalid names,
|
|
100
|
+
* non-object input schemas, and unsafe top-level schema keys.
|
|
101
|
+
*
|
|
102
|
+
* `faultPolicy` is the host's say over thrown faults (see {@link FaultPolicy}); omitted, a
|
|
103
|
+
* thrown fault that its operation does not declare answers `EngineFailure`.
|
|
104
|
+
*/
|
|
105
|
+
export function createOperationRegistry(operations, options = {}) {
|
|
106
|
+
const policies = {
|
|
107
|
+
faultPolicy: options.faultPolicy,
|
|
108
|
+
interceptors: options.interceptors === undefined ? undefined : Object.freeze([...options.interceptors]),
|
|
109
|
+
};
|
|
110
|
+
const byName = new Map();
|
|
111
|
+
for (const operation of operations) {
|
|
112
|
+
if (byName.has(operation.name)) {
|
|
113
|
+
throw new Error(`duplicate operation name in registry: ${operation.name}`);
|
|
114
|
+
}
|
|
115
|
+
validateOperationName(operation.name);
|
|
116
|
+
assertSafeDeclaredKeys(operation);
|
|
117
|
+
assertKindAndEventSchemaAgree(operation);
|
|
118
|
+
assertDestructiveDeclared(operation);
|
|
119
|
+
assertMcpMetadataAgrees(operation);
|
|
120
|
+
byName.set(operation.name, freezeOperation(operation, policies));
|
|
121
|
+
}
|
|
122
|
+
const ordered = Object.freeze([...byName.values()]);
|
|
123
|
+
validateOperationBehaviors(ordered);
|
|
124
|
+
return {
|
|
125
|
+
get(name) {
|
|
126
|
+
return byName.get(name);
|
|
127
|
+
},
|
|
128
|
+
list() {
|
|
129
|
+
return ordered;
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { OperationFault } from "../operation-fault.js";
|
|
2
|
+
import { type DispatchContext, type DispatchResult } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown when an element emitted by a subscription or stream fails
|
|
5
|
+
* per-element schema validation.
|
|
6
|
+
*/
|
|
7
|
+
export declare class SubscriptionElementValidationError extends Error {
|
|
8
|
+
/** The fault that validation produced. */
|
|
9
|
+
readonly fault: OperationFault;
|
|
10
|
+
/** Stable machine-readable discriminant; equals the class name, as a `WeftError`'s does. */
|
|
11
|
+
readonly code = "SubscriptionElementValidationError";
|
|
12
|
+
constructor(
|
|
13
|
+
/** The fault that validation produced. */
|
|
14
|
+
fault: OperationFault);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Execute a `kind: 'stream'` operation and return a schema-validating
|
|
18
|
+
* async iterable for its emitted elements.
|
|
19
|
+
*/
|
|
20
|
+
export declare function executeStream(operationName: string, rawInput: unknown, context: DispatchContext): Promise<DispatchResult<AsyncIterable<unknown>>>;
|
|
21
|
+
/**
|
|
22
|
+
* Execute a `kind: 'subscription'` operation and return its validated
|
|
23
|
+
* subscribe envelope, schema-validating element iterable, and close hook.
|
|
24
|
+
*/
|
|
25
|
+
export declare function executeSubscription(operationName: string, rawInput: unknown, context: DispatchContext): Promise<DispatchResult<{
|
|
26
|
+
subscriptionId: string;
|
|
27
|
+
envelope: unknown;
|
|
28
|
+
iterable: AsyncIterable<unknown>;
|
|
29
|
+
close: () => Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Settles once the subscription has delivered everything that was already
|
|
32
|
+
* recorded, when the operation's iterable reports it. A transport that
|
|
33
|
+
* wants a replay-complete marker (the SSE ping) reads it here, because the
|
|
34
|
+
* validating wrapper around `iterable` cannot carry it.
|
|
35
|
+
*/
|
|
36
|
+
replayComplete?: Promise<void>;
|
|
37
|
+
}>>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { closeAbandonedSubscription, isAsyncIterable, isSubscriptionInvocation, } from "./subscription-lifecycle.js";
|
|
3
|
+
import { dispatchFailure, lookupOperation, validateOutputAgainstSchema, } from "./dispatch-preparation.js";
|
|
4
|
+
import { tracePipeline } from "./pipeline-stages.js";
|
|
5
|
+
import {} from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Error thrown when an element emitted by a subscription or stream fails
|
|
8
|
+
* per-element schema validation.
|
|
9
|
+
*/
|
|
10
|
+
export class SubscriptionElementValidationError extends Error {
|
|
11
|
+
fault;
|
|
12
|
+
/** Stable machine-readable discriminant; equals the class name, as a `WeftError`'s does. */
|
|
13
|
+
code = 'SubscriptionElementValidationError';
|
|
14
|
+
constructor(
|
|
15
|
+
/** The fault that validation produced. */
|
|
16
|
+
fault) {
|
|
17
|
+
super('subscription element failed schema validation');
|
|
18
|
+
this.fault = fault;
|
|
19
|
+
this.name = this.code;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Execute a `kind: 'stream'` operation and return a schema-validating
|
|
24
|
+
* async iterable for its emitted elements.
|
|
25
|
+
*/
|
|
26
|
+
export async function executeStream(operationName, rawInput, context) {
|
|
27
|
+
const prepared = await prepareLongLivedOperation(operationName, rawInput, context, 'stream');
|
|
28
|
+
if (!prepared.ok)
|
|
29
|
+
return prepared;
|
|
30
|
+
const { operation, eventSchema } = prepared.value;
|
|
31
|
+
const dispatched = await operation.dispatch(rawInput, context);
|
|
32
|
+
if (!dispatched.ok)
|
|
33
|
+
return dispatched;
|
|
34
|
+
const invocation = dispatched.value;
|
|
35
|
+
if (!isAsyncIterable(invocation)) {
|
|
36
|
+
return dispatchFailure({ code: 'EngineFailure', message: 'internal error', data: {} });
|
|
37
|
+
}
|
|
38
|
+
tracePipeline(context.pipelineTrace, 'output-validated');
|
|
39
|
+
return {
|
|
40
|
+
ok: true,
|
|
41
|
+
value: validateElements(invocation, eventSchema),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Execute a `kind: 'subscription'` operation and return its validated
|
|
46
|
+
* subscribe envelope, schema-validating element iterable, and close hook.
|
|
47
|
+
*/
|
|
48
|
+
export async function executeSubscription(operationName, rawInput, context) {
|
|
49
|
+
const prepared = await prepareLongLivedOperation(operationName, rawInput, context, 'subscription');
|
|
50
|
+
if (!prepared.ok)
|
|
51
|
+
return prepared;
|
|
52
|
+
const { operation, eventSchema } = prepared.value;
|
|
53
|
+
const dispatched = await operation.dispatch(rawInput, context);
|
|
54
|
+
if (!dispatched.ok)
|
|
55
|
+
return dispatched;
|
|
56
|
+
const invocation = dispatched.value;
|
|
57
|
+
if (!isSubscriptionInvocation(invocation)) {
|
|
58
|
+
return dispatchFailure({ code: 'EngineFailure', message: 'internal error', data: {} });
|
|
59
|
+
}
|
|
60
|
+
const envelope = validateOutputAgainstSchema(operation.outputSchema, invocation.envelope);
|
|
61
|
+
if (!envelope.ok) {
|
|
62
|
+
await closeAbandonedSubscription(invocation);
|
|
63
|
+
return envelope;
|
|
64
|
+
}
|
|
65
|
+
tracePipeline(context.pipelineTrace, 'output-validated');
|
|
66
|
+
return {
|
|
67
|
+
ok: true,
|
|
68
|
+
value: {
|
|
69
|
+
subscriptionId: invocation.subscriptionId,
|
|
70
|
+
envelope: envelope.value,
|
|
71
|
+
iterable: validateElements(invocation.iterable, eventSchema),
|
|
72
|
+
close: invocation.close,
|
|
73
|
+
...replayCompleteOf(invocation.iterable),
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async function prepareLongLivedOperation(operationName, rawInput, context, expectedKind) {
|
|
78
|
+
const lookup = lookupOperation(operationName, context);
|
|
79
|
+
if (!lookup.ok)
|
|
80
|
+
return lookup;
|
|
81
|
+
const operation = lookup.value;
|
|
82
|
+
void rawInput;
|
|
83
|
+
if ((operation.kind ?? 'unary') !== expectedKind) {
|
|
84
|
+
return dispatchFailure({
|
|
85
|
+
code: 'Unprocessable',
|
|
86
|
+
message: `operation "${operation.name}" is not ${expectedKind}`,
|
|
87
|
+
data: { reason: `operation kind is "${operation.kind ?? 'unary'}"` },
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
// The discriminated union guarantees `eventSchema` is non-undefined on
|
|
91
|
+
// any operation whose kind is 'stream' or 'subscription'. The kind check
|
|
92
|
+
// above narrows to one of those two variants; if `eventSchema` is
|
|
93
|
+
// somehow still missing we fail loudly because that would mean the
|
|
94
|
+
// registry was assembled from a non-conforming source. A `defineOperation`
|
|
95
|
+
// caller cannot reach this branch — TypeScript rejects the literal.
|
|
96
|
+
if (operation.eventSchema === undefined) {
|
|
97
|
+
return dispatchFailure({
|
|
98
|
+
code: 'EngineFailure',
|
|
99
|
+
message: 'internal error',
|
|
100
|
+
data: {},
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const eventSchema = operation.eventSchema;
|
|
104
|
+
return { ok: true, value: { operation, eventSchema } };
|
|
105
|
+
}
|
|
106
|
+
async function* validateElements(iterable, eventSchema) {
|
|
107
|
+
for await (const element of iterable) {
|
|
108
|
+
let parsed;
|
|
109
|
+
try {
|
|
110
|
+
parsed = eventSchema.safeParse(element);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
throw new SubscriptionElementValidationError(elementValidationFault());
|
|
114
|
+
}
|
|
115
|
+
if (!parsed.success) {
|
|
116
|
+
throw new SubscriptionElementValidationError(elementValidationFault());
|
|
117
|
+
}
|
|
118
|
+
yield parsed.data;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function replayCompleteOf(iterable) {
|
|
122
|
+
if ('replayComplete' in iterable && iterable.replayComplete instanceof Promise) {
|
|
123
|
+
return { replayComplete: iterable.replayComplete };
|
|
124
|
+
}
|
|
125
|
+
return {};
|
|
126
|
+
}
|
|
127
|
+
function elementValidationFault() {
|
|
128
|
+
return { code: 'EngineFailure', message: 'internal error', data: {} };
|
|
129
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SubscriptionOperationInvocation } from "./types.js";
|
|
2
|
+
/** Whether the invoke result owns a subscription that must be closed after use. */
|
|
3
|
+
export declare function isSubscriptionInvocation(value: unknown): value is SubscriptionOperationInvocation<unknown, unknown>;
|
|
4
|
+
export declare function isAsyncIterable(value: unknown): value is AsyncIterable<unknown>;
|
|
5
|
+
/** Release an invocation that never reached a transport. */
|
|
6
|
+
export declare function closeAbandonedSubscription(invocation: SubscriptionOperationInvocation<unknown, unknown>): Promise<void>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Whether the invoke result owns a subscription that must be closed after use. */
|
|
2
|
+
export function isSubscriptionInvocation(value) {
|
|
3
|
+
return (value !== null &&
|
|
4
|
+
typeof value === 'object' &&
|
|
5
|
+
'subscriptionId' in value &&
|
|
6
|
+
typeof value.subscriptionId === 'string' &&
|
|
7
|
+
'envelope' in value &&
|
|
8
|
+
'iterable' in value &&
|
|
9
|
+
isAsyncIterable(value.iterable) &&
|
|
10
|
+
'close' in value &&
|
|
11
|
+
typeof value.close === 'function');
|
|
12
|
+
}
|
|
13
|
+
export function isAsyncIterable(value) {
|
|
14
|
+
return (value !== null &&
|
|
15
|
+
typeof value === 'object' &&
|
|
16
|
+
Symbol.asyncIterator in value &&
|
|
17
|
+
typeof value[Symbol.asyncIterator] === 'function');
|
|
18
|
+
}
|
|
19
|
+
/** Release an invocation that never reached a transport. */
|
|
20
|
+
export async function closeAbandonedSubscription(invocation) {
|
|
21
|
+
try {
|
|
22
|
+
await invocation.close();
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// Cleanup failure must not replace the original dispatch fault.
|
|
26
|
+
}
|
|
27
|
+
}
|