@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,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `FaultCode` is the stable, machine-readable discriminant that the server's
|
|
3
|
+
* operation pipeline attaches to every fault. Transport adapters serialize it
|
|
4
|
+
* onto the wire so clients can branch on the failure programmatically rather
|
|
5
|
+
* than string-matching a human message:
|
|
6
|
+
*
|
|
7
|
+
* - REST operation routes: the HTTP status derived from this code, plus a
|
|
8
|
+
* `{ error, weftCode?, data?, retryable, remediation? }` body. The body
|
|
9
|
+
* never carries the `FaultCode` itself: its optional `weftCode` is the
|
|
10
|
+
* fine-grained `WeftErrorCode` of the originating error class (see
|
|
11
|
+
* `shapeRestFaultAsJson` in `operation-fault.ts`). `EngineFailure` is
|
|
12
|
+
* masked to exactly `{ "error": "Internal server error", "retryable": false }`.
|
|
13
|
+
* - JSON-RPC: `error.data.weftCode` is this coarse `FaultCode`, alongside
|
|
14
|
+
* `error.data.httpStatus` (see `fault-to-json-rpc.ts`). The fine-grained
|
|
15
|
+
* `WeftErrorCode` is not carried on this transport.
|
|
16
|
+
* - Both transports, and an MCP tool error's `structuredContent`, carry
|
|
17
|
+
* `retryable` (always, from {@link FAULT_CODE_RETRYABLE}) and
|
|
18
|
+
* `remediation` (when the fault sets one).
|
|
19
|
+
*
|
|
20
|
+
* The same key therefore names different vocabularies per transport; see
|
|
21
|
+
* `documentation/reference/api-errors.md#wire-envelopes`.
|
|
22
|
+
*
|
|
23
|
+
* The type lives in its own file because the server (which produces faults)
|
|
24
|
+
* and the client (which consumes them off the wire) both need it, and it
|
|
25
|
+
* imports nothing, so it stays with the transport-neutral operation layer.
|
|
26
|
+
* `core/fault-code.ts` keeps `FAULT_CODE_TO_FAILURE_CATEGORY`, the mapping onto
|
|
27
|
+
* Weft's execution taxonomy.
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Every stable fault code name, in declaration order. The full vocabulary is
|
|
31
|
+
* closed for v1; new codes can be added additively, and the maps keyed by
|
|
32
|
+
* {@link FaultCode} force every new code to be handled at compile time.
|
|
33
|
+
*/
|
|
34
|
+
export const FAULT_CODES = [
|
|
35
|
+
'Unauthorized',
|
|
36
|
+
'Forbidden',
|
|
37
|
+
'NotFound',
|
|
38
|
+
'Conflict',
|
|
39
|
+
'Unprocessable',
|
|
40
|
+
'Timeout',
|
|
41
|
+
'PayloadTooLarge',
|
|
42
|
+
'NotImplemented',
|
|
43
|
+
'UnsupportedTransport',
|
|
44
|
+
'SubscriptionOverflow',
|
|
45
|
+
'InvalidParams',
|
|
46
|
+
'MethodNotFound',
|
|
47
|
+
'StaleRevision',
|
|
48
|
+
'RateLimited',
|
|
49
|
+
'Unavailable',
|
|
50
|
+
'UnsupportedCapability',
|
|
51
|
+
'EngineFailure',
|
|
52
|
+
];
|
|
53
|
+
/**
|
|
54
|
+
* Whether the caller can expect the identical call to succeed if it simply tries again later.
|
|
55
|
+
* Every fault puts this on the wire as `retryable`: in the REST body, in the JSON-RPC
|
|
56
|
+
* `error.data`, and in an MCP tool error's `structuredContent`.
|
|
57
|
+
*
|
|
58
|
+
* The rule: a code is `true` only when the same call, repeated unchanged after a wait, can
|
|
59
|
+
* succeed with no change by the caller. A fault that needs fresh state (re-read the resource, take
|
|
60
|
+
* a new revision) or different input, credentials or capabilities is `false`; its `remediation`
|
|
61
|
+
* says what to change. `retryable` belongs to the code, not to the call site, so this table owns
|
|
62
|
+
* it. A code that covers both a transient and a permanent case is `false`.
|
|
63
|
+
*
|
|
64
|
+
* A new {@link FaultCode} cannot compile until it has a value here, and `fault-code.test.ts`
|
|
65
|
+
* fails when the table and {@link FAULT_CODES} disagree.
|
|
66
|
+
*/
|
|
67
|
+
export const FAULT_CODE_RETRYABLE = Object.freeze({
|
|
68
|
+
// The caller must present a credential, or a different one.
|
|
69
|
+
Unauthorized: false,
|
|
70
|
+
Forbidden: false,
|
|
71
|
+
// The resource is absent, or the request contradicts its current state: different input or a
|
|
72
|
+
// changed target is needed.
|
|
73
|
+
NotFound: false,
|
|
74
|
+
Conflict: false,
|
|
75
|
+
Unprocessable: false,
|
|
76
|
+
// The operation ran out of time, not out of options: the same call can finish within its
|
|
77
|
+
// budget on a quieter server. A timed-out `weft.workflows.update` stays queued, and its
|
|
78
|
+
// `data.updateId` finds the answer, which `remediation` can say.
|
|
79
|
+
Timeout: true,
|
|
80
|
+
PayloadTooLarge: false,
|
|
81
|
+
NotImplemented: false,
|
|
82
|
+
UnsupportedTransport: false,
|
|
83
|
+
// The subscriber fell behind and events were dropped; resuming needs a new cursor, so the
|
|
84
|
+
// identical call is not the repair.
|
|
85
|
+
SubscriptionOverflow: false,
|
|
86
|
+
InvalidParams: false,
|
|
87
|
+
MethodNotFound: false,
|
|
88
|
+
// The caller's revision is behind the resource: re-read, then send the new revision.
|
|
89
|
+
StaleRevision: false,
|
|
90
|
+
RateLimited: true,
|
|
91
|
+
Unavailable: true,
|
|
92
|
+
UnsupportedCapability: false,
|
|
93
|
+
// A server defect. It discloses nothing and promises nothing.
|
|
94
|
+
EngineFailure: false,
|
|
95
|
+
});
|
|
96
|
+
/** Whether `code`'s fault tells the caller that repeating the identical call later can succeed. */
|
|
97
|
+
export function isFaultRetryable(code) {
|
|
98
|
+
return FAULT_CODE_RETRYABLE[code];
|
|
99
|
+
}
|
|
100
|
+
const faultCodes = new Set(FAULT_CODES);
|
|
101
|
+
/**
|
|
102
|
+
* Type guard: narrows an unknown wire value to a known {@link FaultCode}.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { isFaultCode } from '@lostgradient/backchannel';
|
|
107
|
+
*
|
|
108
|
+
* isFaultCode('NotFound'); // true
|
|
109
|
+
* isFaultCode('teapot'); // false
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export function isFaultCode(value) {
|
|
113
|
+
return faultCodes.has(value);
|
|
114
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a fault tells the caller about the way on: `remediation` (one line of plain text, what to do
|
|
3
|
+
* next) and, from the code's table, `retryable`. {@link faultWireAdvice} is the one place the REST,
|
|
4
|
+
* JSON-RPC and MCP projections get both from, so the transports cannot drift.
|
|
5
|
+
*
|
|
6
|
+
* @module fault-remediation
|
|
7
|
+
*/
|
|
8
|
+
import type { OperationFault } from "./operation-fault.js";
|
|
9
|
+
/** The longest `remediation`, in characters (Unicode code points). */
|
|
10
|
+
export declare const MAX_REMEDIATION_LENGTH = 500;
|
|
11
|
+
/**
|
|
12
|
+
* Why `value` cannot be a fault's `remediation`, or `undefined` when it can. A remediation is one
|
|
13
|
+
* line of plain text: 1 to {@link MAX_REMEDIATION_LENGTH} characters, containing no character in
|
|
14
|
+
* the Unicode control category (`Cc`), a newline or a tab included.
|
|
15
|
+
*/
|
|
16
|
+
export declare function remediationProblem(value: unknown): string | undefined;
|
|
17
|
+
/** Throws a `TypeError` when `value` is not a valid `remediation` ({@link remediationProblem}). */
|
|
18
|
+
export declare function assertValidRemediation(value: unknown): asserts value is string;
|
|
19
|
+
/** `fault` with `remediation` set; throws a `TypeError` when the text is not valid. */
|
|
20
|
+
export declare function withRemediation<Fault extends OperationFault>(fault: Fault, remediation: string): Fault;
|
|
21
|
+
/**
|
|
22
|
+
* The advice every wire projection (REST, JSON-RPC, MCP) puts on a fault: `retryable` always, from
|
|
23
|
+
* the code's single table, and `remediation` only when the fault carries a valid one. An invalid
|
|
24
|
+
* value is dropped rather than thrown, as a projection must always answer. `EngineFailure`
|
|
25
|
+
* discloses nothing, so it never carries a remediation even when the fault object holds one.
|
|
26
|
+
*/
|
|
27
|
+
export declare function faultWireAdvice(fault: OperationFault): {
|
|
28
|
+
readonly retryable: boolean;
|
|
29
|
+
readonly remediation?: string;
|
|
30
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a fault tells the caller about the way on: `remediation` (one line of plain text, what to do
|
|
3
|
+
* next) and, from the code's table, `retryable`. {@link faultWireAdvice} is the one place the REST,
|
|
4
|
+
* JSON-RPC and MCP projections get both from, so the transports cannot drift.
|
|
5
|
+
*
|
|
6
|
+
* @module fault-remediation
|
|
7
|
+
*/
|
|
8
|
+
import { FAULT_CODE_RETRYABLE } from "./fault-code.js";
|
|
9
|
+
/** The longest `remediation`, in characters (Unicode code points). */
|
|
10
|
+
export const MAX_REMEDIATION_LENGTH = 500;
|
|
11
|
+
const CONTROL_CHARACTER = /\p{Cc}/u;
|
|
12
|
+
/**
|
|
13
|
+
* Why `value` cannot be a fault's `remediation`, or `undefined` when it can. A remediation is one
|
|
14
|
+
* line of plain text: 1 to {@link MAX_REMEDIATION_LENGTH} characters, containing no character in
|
|
15
|
+
* the Unicode control category (`Cc`), a newline or a tab included.
|
|
16
|
+
*/
|
|
17
|
+
export function remediationProblem(value) {
|
|
18
|
+
if (typeof value !== 'string')
|
|
19
|
+
return 'must be a string';
|
|
20
|
+
if (value.length === 0)
|
|
21
|
+
return 'must not be empty';
|
|
22
|
+
if (Array.from(value).length > MAX_REMEDIATION_LENGTH) {
|
|
23
|
+
return `must be at most ${MAX_REMEDIATION_LENGTH} characters`;
|
|
24
|
+
}
|
|
25
|
+
if (CONTROL_CHARACTER.test(value))
|
|
26
|
+
return 'must be one line with no control characters';
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
/** Throws a `TypeError` when `value` is not a valid `remediation` ({@link remediationProblem}). */
|
|
30
|
+
export function assertValidRemediation(value) {
|
|
31
|
+
const problem = remediationProblem(value);
|
|
32
|
+
if (problem !== undefined)
|
|
33
|
+
throw new TypeError(`A fault's remediation ${problem}`);
|
|
34
|
+
}
|
|
35
|
+
/** `fault` with `remediation` set; throws a `TypeError` when the text is not valid. */
|
|
36
|
+
export function withRemediation(fault, remediation) {
|
|
37
|
+
assertValidRemediation(remediation);
|
|
38
|
+
return { ...fault, remediation };
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The advice every wire projection (REST, JSON-RPC, MCP) puts on a fault: `retryable` always, from
|
|
42
|
+
* the code's single table, and `remediation` only when the fault carries a valid one. An invalid
|
|
43
|
+
* value is dropped rather than thrown, as a projection must always answer. `EngineFailure`
|
|
44
|
+
* discloses nothing, so it never carries a remediation even when the fault object holds one.
|
|
45
|
+
*/
|
|
46
|
+
export function faultWireAdvice(fault) {
|
|
47
|
+
const retryable = FAULT_CODE_RETRYABLE[fault.code];
|
|
48
|
+
const { remediation } = fault;
|
|
49
|
+
if (fault.code === 'EngineFailure' || remediation === undefined)
|
|
50
|
+
return { retryable };
|
|
51
|
+
return remediationProblem(remediation) === undefined ? { retryable, remediation } : { retryable };
|
|
52
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `faultToHttpResponse` — pure mapper from a transport-neutral
|
|
3
|
+
* `OperationFault` into a `Response` shaped for the REST surface.
|
|
4
|
+
*
|
|
5
|
+
* This fallback uses the same flat, audited projection as every explicit REST
|
|
6
|
+
* binding. Keeping one mapper prevents a binding that omits `shapeFault` from
|
|
7
|
+
* accidentally exposing a broader nested payload or raw EngineFailure detail.
|
|
8
|
+
*/
|
|
9
|
+
import { type OperationFault, type RestFaultResponseOptions } from "./operation-fault.js";
|
|
10
|
+
/** Maps an {@link OperationFault} to its REST response. */
|
|
11
|
+
export declare function faultToHttpResponse(fault: OperationFault, options?: RestFaultResponseOptions): Response;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `faultToHttpResponse` — pure mapper from a transport-neutral
|
|
3
|
+
* `OperationFault` into a `Response` shaped for the REST surface.
|
|
4
|
+
*
|
|
5
|
+
* This fallback uses the same flat, audited projection as every explicit REST
|
|
6
|
+
* binding. Keeping one mapper prevents a binding that omits `shapeFault` from
|
|
7
|
+
* accidentally exposing a broader nested payload or raw EngineFailure detail.
|
|
8
|
+
*/
|
|
9
|
+
import { shapeRestFaultAsJson, } from "./operation-fault.js";
|
|
10
|
+
/** Maps an {@link OperationFault} to its REST response. */
|
|
11
|
+
export function faultToHttpResponse(fault, options) {
|
|
12
|
+
return shapeRestFaultAsJson(fault, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `faultToJsonRpcError` — pure mapper from a transport-neutral
|
|
3
|
+
* `OperationFault` into the JSON-RPC 2.0 error object the dispatchers send.
|
|
4
|
+
*
|
|
5
|
+
* Wire shape: `{ code, message, data }`. The numeric `code` follows the
|
|
6
|
+
* JSON-RPC spec for reserved errors (`InvalidParams: -32602`,
|
|
7
|
+
* `MethodNotFound: -32601`) and the Weft domain band -32010..-32099 for
|
|
8
|
+
* everything else. This stable error-code allocation ensures cross-transport
|
|
9
|
+
* parity.
|
|
10
|
+
*
|
|
11
|
+
* Every `data` object carries `{ weftCode, httpStatus, retryable }` plus the typed
|
|
12
|
+
* fault-specific payload, and `remediation` when the fault carries one. The
|
|
13
|
+
* first two are a uniform machine-readable handle: clients can route on
|
|
14
|
+
* `weftCode` when they want symbolic names, or on `httpStatus` (number) when
|
|
15
|
+
* they want HTTP-equivalent semantics. `retryable` says whether repeating the
|
|
16
|
+
* identical call later can succeed, and `remediation` what to do next (see
|
|
17
|
+
* `faultWireAdvice` in `operation-fault.ts`).
|
|
18
|
+
*
|
|
19
|
+
* Here `weftCode` is the coarse `FaultCode` (`fault.code`, e.g. `NotFound`),
|
|
20
|
+
* NOT the fine-grained `WeftErrorCode` (e.g. `WorkflowNotFoundError`) that
|
|
21
|
+
* the REST body's top-level `weftCode` carries (`shapeRestFaultAsJson` in
|
|
22
|
+
* `operation-fault.ts`). Because the envelope keys are written last, a
|
|
23
|
+
* fine-grained `weftCode` inside the fault's own data does not survive onto
|
|
24
|
+
* this wire. The asymmetry is the current wire contract; see
|
|
25
|
+
* `documentation/reference/api-errors.md#wire-envelopes`.
|
|
26
|
+
*
|
|
27
|
+
* No engine and no I/O, apart from one diagnostic when a malformed fault is
|
|
28
|
+
* replaced by the masked internal error (see `wireSafeFault`). The fault
|
|
29
|
+
* decides the wire shape.
|
|
30
|
+
*/
|
|
31
|
+
import { type FaultCode } from "./fault-code.js";
|
|
32
|
+
import { type OperationFault } from "./operation-fault.js";
|
|
33
|
+
export type JsonRpcError = {
|
|
34
|
+
code: number;
|
|
35
|
+
message: string;
|
|
36
|
+
data: Record<string, unknown>;
|
|
37
|
+
};
|
|
38
|
+
/** Maps an {@link OperationFault} to a JSON-RPC error object. */
|
|
39
|
+
export declare function faultToJsonRpcError(unsafeFault: OperationFault): JsonRpcError;
|
|
40
|
+
/**
|
|
41
|
+
* The `error.data` of a JSON-RPC error a transport answers itself, before or beside the operation
|
|
42
|
+
* pipeline (a frame too large, a refused first frame, an unknown subscription): the envelope keys
|
|
43
|
+
* `faultToJsonRpcError` writes (`weftCode`, `httpStatus`, `retryable`) for the fault `code`, with
|
|
44
|
+
* `payload` as the fault's own fields. A transport that writes the keys by hand drops the ones a
|
|
45
|
+
* later field adds, so every such error builds its `data` here.
|
|
46
|
+
*/
|
|
47
|
+
export declare function faultErrorData(code: FaultCode, payload?: Record<string, unknown>): Record<string, unknown>;
|
|
48
|
+
/**
|
|
49
|
+
* The fields of a fault's `data` a wire projection may send: the deny-by-default allowlist for its
|
|
50
|
+
* code. The JSON-RPC envelope and the Server-Sent Events error frame both build their `data` here,
|
|
51
|
+
* so a field is never sent by one transport and withheld by another.
|
|
52
|
+
*/
|
|
53
|
+
export declare function extractFaultDataPayload(fault: OperationFault): Record<string, unknown>;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `faultToJsonRpcError` — pure mapper from a transport-neutral
|
|
3
|
+
* `OperationFault` into the JSON-RPC 2.0 error object the dispatchers send.
|
|
4
|
+
*
|
|
5
|
+
* Wire shape: `{ code, message, data }`. The numeric `code` follows the
|
|
6
|
+
* JSON-RPC spec for reserved errors (`InvalidParams: -32602`,
|
|
7
|
+
* `MethodNotFound: -32601`) and the Weft domain band -32010..-32099 for
|
|
8
|
+
* everything else. This stable error-code allocation ensures cross-transport
|
|
9
|
+
* parity.
|
|
10
|
+
*
|
|
11
|
+
* Every `data` object carries `{ weftCode, httpStatus, retryable }` plus the typed
|
|
12
|
+
* fault-specific payload, and `remediation` when the fault carries one. The
|
|
13
|
+
* first two are a uniform machine-readable handle: clients can route on
|
|
14
|
+
* `weftCode` when they want symbolic names, or on `httpStatus` (number) when
|
|
15
|
+
* they want HTTP-equivalent semantics. `retryable` says whether repeating the
|
|
16
|
+
* identical call later can succeed, and `remediation` what to do next (see
|
|
17
|
+
* `faultWireAdvice` in `operation-fault.ts`).
|
|
18
|
+
*
|
|
19
|
+
* Here `weftCode` is the coarse `FaultCode` (`fault.code`, e.g. `NotFound`),
|
|
20
|
+
* NOT the fine-grained `WeftErrorCode` (e.g. `WorkflowNotFoundError`) that
|
|
21
|
+
* the REST body's top-level `weftCode` carries (`shapeRestFaultAsJson` in
|
|
22
|
+
* `operation-fault.ts`). Because the envelope keys are written last, a
|
|
23
|
+
* fine-grained `weftCode` inside the fault's own data does not survive onto
|
|
24
|
+
* this wire. The asymmetry is the current wire contract; see
|
|
25
|
+
* `documentation/reference/api-errors.md#wire-envelopes`.
|
|
26
|
+
*
|
|
27
|
+
* No engine and no I/O, apart from one diagnostic when a malformed fault is
|
|
28
|
+
* replaced by the masked internal error (see `wireSafeFault`). The fault
|
|
29
|
+
* decides the wire shape.
|
|
30
|
+
*/
|
|
31
|
+
import { FAULT_CODE_RETRYABLE } from "./fault-code.js";
|
|
32
|
+
import { faultWireAdvice } from "./fault-remediation.js";
|
|
33
|
+
import { usableRetryAfterSeconds, wireSafeFault } from "./fault-wire-safety.js";
|
|
34
|
+
import { FAULT_CODE_TO_HTTP_STATUS, FAULT_CODE_TO_JSON_RPC_CODE, } from "./operation-fault.js";
|
|
35
|
+
/** Maps an {@link OperationFault} to a JSON-RPC error object. */
|
|
36
|
+
export function faultToJsonRpcError(unsafeFault) {
|
|
37
|
+
const fault = wireSafeFault(unsafeFault);
|
|
38
|
+
const code = FAULT_CODE_TO_JSON_RPC_CODE[fault.code];
|
|
39
|
+
const httpStatus = FAULT_CODE_TO_HTTP_STATUS[fault.code];
|
|
40
|
+
// Envelope keys are written LAST so a future payload field accidentally
|
|
41
|
+
// named `weftCode` or `httpStatus` cannot overwrite the canonical metadata.
|
|
42
|
+
const { retryable, remediation } = faultWireAdvice(fault);
|
|
43
|
+
const data = {
|
|
44
|
+
...extractFaultDataPayload(fault),
|
|
45
|
+
weftCode: fault.code,
|
|
46
|
+
httpStatus,
|
|
47
|
+
retryable,
|
|
48
|
+
...(remediation === undefined ? {} : { remediation }),
|
|
49
|
+
};
|
|
50
|
+
return { code, message: fault.message, data };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The `error.data` of a JSON-RPC error a transport answers itself, before or beside the operation
|
|
54
|
+
* pipeline (a frame too large, a refused first frame, an unknown subscription): the envelope keys
|
|
55
|
+
* `faultToJsonRpcError` writes (`weftCode`, `httpStatus`, `retryable`) for the fault `code`, with
|
|
56
|
+
* `payload` as the fault's own fields. A transport that writes the keys by hand drops the ones a
|
|
57
|
+
* later field adds, so every such error builds its `data` here.
|
|
58
|
+
*/
|
|
59
|
+
export function faultErrorData(code, payload = {}) {
|
|
60
|
+
return {
|
|
61
|
+
...payload,
|
|
62
|
+
weftCode: code,
|
|
63
|
+
httpStatus: FAULT_CODE_TO_HTTP_STATUS[code],
|
|
64
|
+
retryable: FAULT_CODE_RETRYABLE[code],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const EMPTY = {};
|
|
68
|
+
const FAULT_DATA_EXTRACTORS = {
|
|
69
|
+
NotImplemented: () => EMPTY,
|
|
70
|
+
EngineFailure: () => EMPTY,
|
|
71
|
+
Unauthorized: (data) => ({ reason: data.reason }),
|
|
72
|
+
Forbidden: (data) => ({ reason: data.reason }),
|
|
73
|
+
Unprocessable: (data) => ({ reason: data.reason }),
|
|
74
|
+
Conflict: dataForConflict,
|
|
75
|
+
NotFound: dataForNotFound,
|
|
76
|
+
Timeout: dataForTimeout,
|
|
77
|
+
PayloadTooLarge: (data) => ({ maxBytes: data.maxBytes }),
|
|
78
|
+
UnsupportedTransport: dataForUnsupportedTransport,
|
|
79
|
+
SubscriptionOverflow: dataForSubscriptionOverflow,
|
|
80
|
+
InvalidParams: dataForInvalidParams,
|
|
81
|
+
MethodNotFound: (data) => ({ method: data.method }),
|
|
82
|
+
StaleRevision: (data) => ({
|
|
83
|
+
currentRevision: data.currentRevision,
|
|
84
|
+
...(data.expectedRevision === undefined ? {} : { expectedRevision: data.expectedRevision }),
|
|
85
|
+
}),
|
|
86
|
+
RateLimited: (data) => filterDefined({ retryAfterSeconds: usableRetryAfterSeconds(data.retryAfterSeconds) }),
|
|
87
|
+
Unavailable: (data) => filterDefined({
|
|
88
|
+
reason: data.reason,
|
|
89
|
+
retryAfterSeconds: usableRetryAfterSeconds(data.retryAfterSeconds),
|
|
90
|
+
}),
|
|
91
|
+
UnsupportedCapability: (data) => ({ capability: data.capability }),
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* The fields of a fault's `data` a wire projection may send: the deny-by-default allowlist for its
|
|
95
|
+
* code. The JSON-RPC envelope and the Server-Sent Events error frame both build their `data` here,
|
|
96
|
+
* so a field is never sent by one transport and withheld by another.
|
|
97
|
+
*/
|
|
98
|
+
export function extractFaultDataPayload(fault) {
|
|
99
|
+
// The cast is sound because `FAULT_DATA_EXTRACTORS` is typed so each key
|
|
100
|
+
// maps to an extractor for that fault's data — TypeScript cannot narrow
|
|
101
|
+
// `fault.data` through the dynamic lookup, so we delegate to the table.
|
|
102
|
+
const extractor = FAULT_DATA_EXTRACTORS[fault.code];
|
|
103
|
+
return extractor(fault.data);
|
|
104
|
+
}
|
|
105
|
+
function dataForConflict(data) {
|
|
106
|
+
return filterDefined({
|
|
107
|
+
reason: data.reason,
|
|
108
|
+
missingTypes: data.missingTypes === undefined ? undefined : [...data.missingTypes],
|
|
109
|
+
missingWorkflowCount: data.missingWorkflowCount,
|
|
110
|
+
samplesTruncated: data.samplesTruncated,
|
|
111
|
+
currentGeneration: data.currentGeneration,
|
|
112
|
+
compatibilityReasons: data.compatibilityReasons === undefined ? undefined : [...data.compatibilityReasons],
|
|
113
|
+
sourceValidationReasons: data.sourceValidationReasons === undefined ? undefined : [...data.sourceValidationReasons],
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function dataForNotFound(data) {
|
|
117
|
+
return data.identifier === undefined
|
|
118
|
+
? { resource: data.resource }
|
|
119
|
+
: { resource: data.resource, identifier: data.identifier };
|
|
120
|
+
}
|
|
121
|
+
function dataForTimeout(data) {
|
|
122
|
+
return {
|
|
123
|
+
...(data.operationName === undefined ? {} : { operationName: data.operationName }),
|
|
124
|
+
...(data.updateId === undefined ? {} : { updateId: data.updateId }),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function dataForUnsupportedTransport(data) {
|
|
128
|
+
return { transport: data.transport, supported: [...data.supported] };
|
|
129
|
+
}
|
|
130
|
+
function dataForSubscriptionOverflow(data) {
|
|
131
|
+
return { subscriptionId: data.subscriptionId, droppedCount: data.droppedCount };
|
|
132
|
+
}
|
|
133
|
+
function dataForInvalidParams(data) {
|
|
134
|
+
return { issues: data.issues.map((issue) => ({ ...issue, path: [...issue.path] })) };
|
|
135
|
+
}
|
|
136
|
+
function filterDefined(input) {
|
|
137
|
+
const output = {};
|
|
138
|
+
for (const [key, value] of Object.entries(input)) {
|
|
139
|
+
if (value !== undefined)
|
|
140
|
+
output[key] = value;
|
|
141
|
+
}
|
|
142
|
+
return output;
|
|
143
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What keeps a fault from putting a value on the wire that JSON cannot carry: the factories refuse
|
|
3
|
+
* a non-finite number in a fault's `data`, and the wire projections answer a fault that holds one
|
|
4
|
+
* as the masked internal error instead of throwing.
|
|
5
|
+
*
|
|
6
|
+
* @module fault-wire-safety
|
|
7
|
+
*/
|
|
8
|
+
import type { OperationFault } from "./operation-fault.js";
|
|
9
|
+
/**
|
|
10
|
+
* Refuse a fault whose `data` carries a number that JSON cannot represent.
|
|
11
|
+
* `NaN` and `Infinity` serialize as `null`, so a `StaleRevision` with a
|
|
12
|
+
* non-finite `currentRevision`, for one, would put a `null` on the wire where
|
|
13
|
+
* the schema requires a revision. That is a programming error in whoever built
|
|
14
|
+
* the fault, so it throws rather than producing a body that violates the
|
|
15
|
+
* contract. The one exception is `retryAfterSeconds`, a hint a client can do
|
|
16
|
+
* without: {@link usableRetryAfterSeconds} drops it from every projection.
|
|
17
|
+
*
|
|
18
|
+
* Called by the fault factories. The wire projections use {@link wireSafeFault}
|
|
19
|
+
* instead, so a fault written as an object literal still gets a reply.
|
|
20
|
+
*/
|
|
21
|
+
export declare function assertFiniteFaultData(fault: OperationFault): void;
|
|
22
|
+
/**
|
|
23
|
+
* The fault a wire projection may send. A fault whose `data` holds a non-finite
|
|
24
|
+
* number is a programming error that slipped past the factories (for example,
|
|
25
|
+
* a fault written as an object literal). Throwing from a projection would leave
|
|
26
|
+
* the request without a reply (a JSON-RPC WebSocket client would wait forever),
|
|
27
|
+
* so the projection logs it and answers with the masked internal error instead,
|
|
28
|
+
* keeping the request's id.
|
|
29
|
+
*/
|
|
30
|
+
export declare function wireSafeFault(fault: OperationFault): OperationFault;
|
|
31
|
+
/**
|
|
32
|
+
* Build a `StaleRevision` fault. Throws a `TypeError` when `currentRevision` or
|
|
33
|
+
* `expectedRevision` is a non-finite number ({@link assertFiniteFaultData}).
|
|
34
|
+
*/
|
|
35
|
+
export declare function staleRevisionFault(message: string, data: Extract<OperationFault, {
|
|
36
|
+
code: 'StaleRevision';
|
|
37
|
+
}>['data']): Extract<OperationFault, {
|
|
38
|
+
code: 'StaleRevision';
|
|
39
|
+
}>;
|
|
40
|
+
/**
|
|
41
|
+
* A `retryAfterSeconds` a client can act on: finite and not negative. `NaN`
|
|
42
|
+
* and `Infinity` do not survive JSON (they serialize as `null`), and a
|
|
43
|
+
* negative delay is meaningless, so every wire projection drops them.
|
|
44
|
+
*/
|
|
45
|
+
export declare function usableRetryAfterSeconds(seconds: number | undefined): number | undefined;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What keeps a fault from putting a value on the wire that JSON cannot carry: the factories refuse
|
|
3
|
+
* a non-finite number in a fault's `data`, and the wire projections answer a fault that holds one
|
|
4
|
+
* as the masked internal error instead of throwing.
|
|
5
|
+
*
|
|
6
|
+
* @module fault-wire-safety
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Refuse a fault whose `data` carries a number that JSON cannot represent.
|
|
10
|
+
* `NaN` and `Infinity` serialize as `null`, so a `StaleRevision` with a
|
|
11
|
+
* non-finite `currentRevision`, for one, would put a `null` on the wire where
|
|
12
|
+
* the schema requires a revision. That is a programming error in whoever built
|
|
13
|
+
* the fault, so it throws rather than producing a body that violates the
|
|
14
|
+
* contract. The one exception is `retryAfterSeconds`, a hint a client can do
|
|
15
|
+
* without: {@link usableRetryAfterSeconds} drops it from every projection.
|
|
16
|
+
*
|
|
17
|
+
* Called by the fault factories. The wire projections use {@link wireSafeFault}
|
|
18
|
+
* instead, so a fault written as an object literal still gets a reply.
|
|
19
|
+
*/
|
|
20
|
+
export function assertFiniteFaultData(fault) {
|
|
21
|
+
const field = findNonFiniteFaultField(fault);
|
|
22
|
+
if (field !== undefined) {
|
|
23
|
+
throw new TypeError(`The ${fault.code} fault's data.${field} must be a finite number, got ${String(faultDataValue(fault, field))}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** The first `data` field (other than `retryAfterSeconds`) holding a non-finite number. */
|
|
27
|
+
function findNonFiniteFaultField(fault) {
|
|
28
|
+
for (const [field, value] of Object.entries(fault.data)) {
|
|
29
|
+
if (field === 'retryAfterSeconds')
|
|
30
|
+
continue;
|
|
31
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
32
|
+
return field;
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
function faultDataValue(fault, field) {
|
|
37
|
+
return fault.data[field];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The fault a wire projection may send. A fault whose `data` holds a non-finite
|
|
41
|
+
* number is a programming error that slipped past the factories (for example,
|
|
42
|
+
* a fault written as an object literal). Throwing from a projection would leave
|
|
43
|
+
* the request without a reply (a JSON-RPC WebSocket client would wait forever),
|
|
44
|
+
* so the projection logs it and answers with the masked internal error instead,
|
|
45
|
+
* keeping the request's id.
|
|
46
|
+
*/
|
|
47
|
+
export function wireSafeFault(fault) {
|
|
48
|
+
const field = findNonFiniteFaultField(fault);
|
|
49
|
+
if (field === undefined)
|
|
50
|
+
return fault;
|
|
51
|
+
console.error(`[weft] The ${fault.code} fault's data.${field} is not a finite number (${String(faultDataValue(fault, field))}); answering with an internal error instead.`);
|
|
52
|
+
return { code: 'EngineFailure', message: 'internal error', data: {} };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build a `StaleRevision` fault. Throws a `TypeError` when `currentRevision` or
|
|
56
|
+
* `expectedRevision` is a non-finite number ({@link assertFiniteFaultData}).
|
|
57
|
+
*/
|
|
58
|
+
export function staleRevisionFault(message, data) {
|
|
59
|
+
const fault = { code: 'StaleRevision', message, data };
|
|
60
|
+
assertFiniteFaultData(fault);
|
|
61
|
+
return fault;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A `retryAfterSeconds` a client can act on: finite and not negative. `NaN`
|
|
65
|
+
* and `Infinity` do not survive JSON (they serialize as `null`), and a
|
|
66
|
+
* negative delay is meaningless, so every wire projection drops them.
|
|
67
|
+
*/
|
|
68
|
+
export function usableRetryAfterSeconds(seconds) {
|
|
69
|
+
if (seconds === undefined || !Number.isFinite(seconds) || seconds < 0)
|
|
70
|
+
return undefined;
|
|
71
|
+
return seconds;
|
|
72
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `./generator` entry: the client generator, which lives in `scripts/` beside its helpers and
|
|
3
|
+
* its tests. The workspace contract wants every export to point at a `src` file, so this entry
|
|
4
|
+
* re-exports it. Unlike the `.`, `./internals` and `./client` entries it may import `node:` modules,
|
|
5
|
+
* so the browser type-check leaves it out.
|
|
6
|
+
*
|
|
7
|
+
* @module generator
|
|
8
|
+
*/
|
|
9
|
+
export { createOperationClientSources, type OperationClientConnection, type OperationClientExamples, type OperationClientOptions, type OperationClientSubscriptionRoutes, type OperationDomainName, } from "./scripts/generate-operation-client.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `./generator` entry: the client generator, which lives in `scripts/` beside its helpers and
|
|
3
|
+
* its tests. The workspace contract wants every export to point at a `src` file, so this entry
|
|
4
|
+
* re-exports it. Unlike the `.`, `./internals` and `./client` entries it may import `node:` modules,
|
|
5
|
+
* so the browser type-check leaves it out.
|
|
6
|
+
*
|
|
7
|
+
* @module generator
|
|
8
|
+
*/
|
|
9
|
+
export { createOperationClientSources, } from "./scripts/generate-operation-client.js";
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { AuthContext } from "../authentication.js";
|
|
2
|
+
import { type ScopeVocabulary } from "../authorization-scope.js";
|
|
3
|
+
import { type Principal } from "../principal.js";
|
|
4
|
+
/**
|
|
5
|
+
* Convert the REST transport's `authContext` into a `Principal`. The
|
|
6
|
+
* authenticator (`serve()`) reports method + optional claims; this maps
|
|
7
|
+
* that transport authentication result into the `Principal` used by the
|
|
8
|
+
* operation pipeline.
|
|
9
|
+
* Returns `anonymousPrincipal()` when no context is provided (public
|
|
10
|
+
* request). A forwarded `principal` is used verbatim; without one, a `jwt`
|
|
11
|
+
* context is rebuilt from its claims with built-in scopes only, because the
|
|
12
|
+
* package scopes a handler registered are known only to its authenticator.
|
|
13
|
+
* Forward the authenticator's `principal` to keep them.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { authContextToPrincipal } from '@lostgradient/backchannel';
|
|
18
|
+
*
|
|
19
|
+
* const principal = authContextToPrincipal({ method: 'api-key' });
|
|
20
|
+
* console.log(principal.method); // 'api-key'
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function authContextToPrincipal(authContext: AuthContext | undefined, scopeVocabulary?: ScopeVocabulary): Principal;
|
|
24
|
+
/**
|
|
25
|
+
* Give a trusted local caller the scopes from the supplied host vocabulary.
|
|
26
|
+
* A caller also needs an explicit credential for any additional package scope.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { authContextToPrincipal, createScopeVocabulary, unauthenticatedLocalAuthContext } from '@lostgradient/backchannel';
|
|
31
|
+
*
|
|
32
|
+
* const vocabulary = createScopeVocabulary(['inventory:read']);
|
|
33
|
+
* const principal = authContextToPrincipal(unauthenticatedLocalAuthContext(vocabulary));
|
|
34
|
+
* console.log(principal.method); // 'unauthenticated-local'
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function unauthenticatedLocalAuthContext(scopeVocabulary?: ScopeVocabulary): AuthContext;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { EMPTY_SCOPE_VOCABULARY } from "../authorization-scope.js";
|
|
2
|
+
import { anonymousPrincipal, principalFromApiKey, principalFromJwtClaims, principalFromMutualTls, principalFromUnauthenticatedLocal, SHARED_API_KEY_SUBJECT, SHARED_MUTUAL_TLS_SUBJECT, } from "../principal.js";
|
|
3
|
+
/**
|
|
4
|
+
* Convert the REST transport's `authContext` into a `Principal`. The
|
|
5
|
+
* authenticator (`serve()`) reports method + optional claims; this maps
|
|
6
|
+
* that transport authentication result into the `Principal` used by the
|
|
7
|
+
* operation pipeline.
|
|
8
|
+
* Returns `anonymousPrincipal()` when no context is provided (public
|
|
9
|
+
* request). A forwarded `principal` is used verbatim; without one, a `jwt`
|
|
10
|
+
* context is rebuilt from its claims with built-in scopes only, because the
|
|
11
|
+
* package scopes a handler registered are known only to its authenticator.
|
|
12
|
+
* Forward the authenticator's `principal` to keep them.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { authContextToPrincipal } from '@lostgradient/backchannel';
|
|
17
|
+
*
|
|
18
|
+
* const principal = authContextToPrincipal({ method: 'api-key' });
|
|
19
|
+
* console.log(principal.method); // 'api-key'
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function authContextToPrincipal(authContext, scopeVocabulary = EMPTY_SCOPE_VOCABULARY) {
|
|
23
|
+
if (authContext === undefined)
|
|
24
|
+
return anonymousPrincipal();
|
|
25
|
+
if (authContext.principal !== undefined)
|
|
26
|
+
return authContext.principal;
|
|
27
|
+
switch (authContext.method) {
|
|
28
|
+
case 'jwt': {
|
|
29
|
+
if (authContext.claims === undefined) {
|
|
30
|
+
throw new Error('authContextToPrincipal: jwt authContext reached the pipeline without claims — ' +
|
|
31
|
+
'authenticator contract violation');
|
|
32
|
+
}
|
|
33
|
+
return principalFromJwtClaims(authContext.claims, { scopeVocabulary });
|
|
34
|
+
}
|
|
35
|
+
case 'api-key':
|
|
36
|
+
return principalFromApiKey({ subject: SHARED_API_KEY_SUBJECT, scopes: [] });
|
|
37
|
+
case 'mtls':
|
|
38
|
+
return principalFromMutualTls({ subject: SHARED_MUTUAL_TLS_SUBJECT, scopes: [] });
|
|
39
|
+
case 'unauthenticated-local':
|
|
40
|
+
return principalFromUnauthenticatedLocal(scopeVocabulary);
|
|
41
|
+
case 'public':
|
|
42
|
+
return anonymousPrincipal();
|
|
43
|
+
default: {
|
|
44
|
+
const requiresPrincipal = authContext.method;
|
|
45
|
+
throw new Error(`${requiresPrincipal} authentication requires a verified principal`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Give a trusted local caller the scopes from the supplied host vocabulary.
|
|
51
|
+
* A caller also needs an explicit credential for any additional package scope.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { authContextToPrincipal, createScopeVocabulary, unauthenticatedLocalAuthContext } from '@lostgradient/backchannel';
|
|
56
|
+
*
|
|
57
|
+
* const vocabulary = createScopeVocabulary(['inventory:read']);
|
|
58
|
+
* const principal = authContextToPrincipal(unauthenticatedLocalAuthContext(vocabulary));
|
|
59
|
+
* console.log(principal.method); // 'unauthenticated-local'
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function unauthenticatedLocalAuthContext(scopeVocabulary = EMPTY_SCOPE_VOCABULARY) {
|
|
63
|
+
return Object.freeze({
|
|
64
|
+
method: 'unauthenticated-local',
|
|
65
|
+
principal: principalFromUnauthenticatedLocal(scopeVocabulary),
|
|
66
|
+
});
|
|
67
|
+
}
|