@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,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthenticatedPrincipal construction and the Principal discriminated union.
|
|
3
|
+
*
|
|
4
|
+
* Principals are the transport-neutral representation of "who made this call."
|
|
5
|
+
* Every transport edge (REST, JSON-RPC HTTP, JSON-RPC WS upgrade, stdio
|
|
6
|
+
* admission) validates credentials and produces exactly one of these shapes
|
|
7
|
+
* before any operation pipeline runs.
|
|
8
|
+
*
|
|
9
|
+
* Credential *validation* (signature verification, expiry, revocation, format
|
|
10
|
+
* checks) is the transport edge's responsibility — these factories assume the
|
|
11
|
+
* inputs they receive describe a credential that has already been accepted.
|
|
12
|
+
* This stable principal model ensures consistent authentication across all
|
|
13
|
+
* transport layers.
|
|
14
|
+
*
|
|
15
|
+
* The discriminator string for API keys is `'api-key'` (kebab-case) to match
|
|
16
|
+
* the existing `AuthMethod` literal in `authentication.ts`. Keep them aligned;
|
|
17
|
+
* the bridge from `AuthResult` to `Principal` relies on byte-identical strings.
|
|
18
|
+
*/
|
|
19
|
+
import { EMPTY_SCOPE_VOCABULARY, extractScopesFromClaims, } from "./authorization-scope.js";
|
|
20
|
+
/**
|
|
21
|
+
* Build an `AuthenticatedPrincipal` from a JWT claims payload. Scopes come
|
|
22
|
+
* from the `scope`, `scp`, and `permissions` claims; only scopes in the host vocabulary and
|
|
23
|
+
* those in `options.additionalScopes` (the package scopes the admitting
|
|
24
|
+
* handler registered) are kept.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { createScopeVocabulary, principalFromJwtClaims } from '@lostgradient/backchannel';
|
|
29
|
+
*
|
|
30
|
+
* const scopeVocabulary = createScopeVocabulary(['inventory:read', 'inventory:write']);
|
|
31
|
+
* const principal = principalFromJwtClaims(
|
|
32
|
+
* { sub: 'user-123', scope: 'inventory:read inventory:write' },
|
|
33
|
+
* { scopeVocabulary },
|
|
34
|
+
* );
|
|
35
|
+
* console.log(principal.hasScope('inventory:read'));
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function principalFromJwtClaims(claims, options = {}) {
|
|
39
|
+
const scopes = extractScopesFromClaims(claims, options.additionalScopes, options.scopeVocabulary);
|
|
40
|
+
const subject = typeof claims['sub'] === 'string' ? claims['sub'] : undefined;
|
|
41
|
+
return {
|
|
42
|
+
method: 'jwt',
|
|
43
|
+
scopes,
|
|
44
|
+
claims,
|
|
45
|
+
subject,
|
|
46
|
+
hasScope(scope) {
|
|
47
|
+
return scopes.has(scope);
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Build an `AuthenticatedPrincipal` from an API-key admission result.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { principalFromApiKey } from '@lostgradient/backchannel';
|
|
57
|
+
*
|
|
58
|
+
* const principal = principalFromApiKey({
|
|
59
|
+
* subject: 'automation',
|
|
60
|
+
* scopes: ['inventory:read'],
|
|
61
|
+
* });
|
|
62
|
+
* console.log(principal.subject);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function principalFromApiKey(options) {
|
|
66
|
+
const scopes = new Set(options.scopes);
|
|
67
|
+
return {
|
|
68
|
+
method: 'api-key',
|
|
69
|
+
scopes,
|
|
70
|
+
claims: undefined,
|
|
71
|
+
subject: options.subject,
|
|
72
|
+
hasScope(scope) {
|
|
73
|
+
return scopes.has(scope);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Build an `AuthenticatedPrincipal` from an mTLS admission result.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { principalFromMutualTls } from '@lostgradient/backchannel';
|
|
83
|
+
*
|
|
84
|
+
* const principal = principalFromMutualTls({
|
|
85
|
+
* subject: 'client-cert-subject',
|
|
86
|
+
* scopes: ['settings:read'],
|
|
87
|
+
* });
|
|
88
|
+
* console.log(principal.method);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export function principalFromMutualTls(options) {
|
|
92
|
+
const scopes = new Set(options.scopes);
|
|
93
|
+
return {
|
|
94
|
+
method: 'mtls',
|
|
95
|
+
scopes,
|
|
96
|
+
claims: undefined,
|
|
97
|
+
subject: options.subject,
|
|
98
|
+
hasScope(scope) {
|
|
99
|
+
return scopes.has(scope);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The single unauthenticated principal.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* import { anonymousPrincipal } from '@lostgradient/backchannel';
|
|
109
|
+
*
|
|
110
|
+
* const principal = anonymousPrincipal();
|
|
111
|
+
* console.log(principal.method);
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export function anonymousPrincipal() {
|
|
115
|
+
return ANONYMOUS;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Build the privileged `stdio-local` principal for the Phase 13
|
|
119
|
+
* runtime stdio subcommand. Admission is gated at the CLI boundary
|
|
120
|
+
* (`--startup-token <hex>` or `--allow-unauthenticated-local-admin`);
|
|
121
|
+
* once admitted, the session has every scope in the host’s vocabulary because it's
|
|
122
|
+
* running as a local process that can already invoke the binary directly.
|
|
123
|
+
* It holds no package scope: no handler's `additionalScopes` reach it, so
|
|
124
|
+
* an operation that requires a package scope is `Forbidden` to it.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* import { createScopeVocabulary, principalFromStdioLocal } from '@lostgradient/backchannel';
|
|
129
|
+
*
|
|
130
|
+
* const vocabulary = createScopeVocabulary(['inventory:write']);
|
|
131
|
+
* const principal = principalFromStdioLocal(vocabulary);
|
|
132
|
+
* console.log(principal.hasScope('inventory:write'));
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export function principalFromStdioLocal(vocabulary = EMPTY_SCOPE_VOCABULARY) {
|
|
136
|
+
const scopes = new Set(vocabulary.scopes);
|
|
137
|
+
return {
|
|
138
|
+
method: 'stdio-local',
|
|
139
|
+
scopes,
|
|
140
|
+
claims: undefined,
|
|
141
|
+
subject: 'stdio-local',
|
|
142
|
+
hasScope(scope) {
|
|
143
|
+
return scopes.has(scope);
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Build the principal a `serve()` with no authentication gives every request.
|
|
149
|
+
* The server identifies no caller, so every caller is the one local user and
|
|
150
|
+
* holds every scope supplied by the host, so every built-in
|
|
151
|
+
* operation is reachable. It holds no package scope, as with `stdio-local`, so
|
|
152
|
+
* an operation that needs a scope registered through `additionalScopes` still
|
|
153
|
+
* needs a credential holding it. The same instance is returned every time, so
|
|
154
|
+
* two requests compare as the same owner.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* import { createScopeVocabulary, principalFromUnauthenticatedLocal } from '@lostgradient/backchannel';
|
|
159
|
+
*
|
|
160
|
+
* const vocabulary = createScopeVocabulary(['inventory:write']);
|
|
161
|
+
* const principal = principalFromUnauthenticatedLocal(vocabulary);
|
|
162
|
+
* console.log(principal.method); // 'unauthenticated-local'
|
|
163
|
+
* console.log(principal.hasScope('inventory:write')); // true
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
export function principalFromUnauthenticatedLocal(vocabulary = EMPTY_SCOPE_VOCABULARY) {
|
|
167
|
+
let principal = localPrincipals.get(vocabulary);
|
|
168
|
+
if (principal === undefined) {
|
|
169
|
+
principal = createUnauthenticatedLocalPrincipal(vocabulary);
|
|
170
|
+
localPrincipals.set(vocabulary, principal);
|
|
171
|
+
}
|
|
172
|
+
return principal;
|
|
173
|
+
}
|
|
174
|
+
const ANONYMOUS = { method: 'unauthenticated' };
|
|
175
|
+
/**
|
|
176
|
+
* Whether a principal was rebuilt from a credential the caller presented on
|
|
177
|
+
* this request and names that caller distinctly (`jwt`, `api-key`, or `mtls`
|
|
178
|
+
* with a subject of its own). Only such a principal separates one caller from
|
|
179
|
+
* another by itself.
|
|
180
|
+
*
|
|
181
|
+
* `stdio-local` and `unauthenticated-local` are minted by the host and shared
|
|
182
|
+
* by every caller it serves, an anonymous principal is a shared singleton, and
|
|
183
|
+
* the shared fallback subjects ({@link SHARED_API_KEY_SUBJECT},
|
|
184
|
+
* {@link SHARED_MUTUAL_TLS_SUBJECT}) are common to every static key or mTLS
|
|
185
|
+
* caller, so none of them is credential-backed: a session bound to one needs a
|
|
186
|
+
* per-session secret of its own to be isolated from other callers.
|
|
187
|
+
*/
|
|
188
|
+
export function isCredentialBacked(principal) {
|
|
189
|
+
if (!CREDENTIAL_BACKED_METHODS.has(principal.method))
|
|
190
|
+
return false;
|
|
191
|
+
if (!isAuthenticated(principal))
|
|
192
|
+
return false;
|
|
193
|
+
// Every static API key, and every mTLS caller admitted without a distinct
|
|
194
|
+
// identity, maps to one shared fallback subject, so two such callers present
|
|
195
|
+
// the same principal. Presenting a credential does not separate them.
|
|
196
|
+
// A credential with no subject names nobody, so it separates nobody either.
|
|
197
|
+
return principal.subject !== undefined && !SHARED_FALLBACK_SUBJECTS.has(principal.subject);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* An allowlist, so a principal method added later is treated as host-minted
|
|
201
|
+
* (and its sessions keep needing a token) until someone decides otherwise.
|
|
202
|
+
*/
|
|
203
|
+
const CREDENTIAL_BACKED_METHODS = new Set([
|
|
204
|
+
'jwt',
|
|
205
|
+
'api-key',
|
|
206
|
+
'mtls',
|
|
207
|
+
]);
|
|
208
|
+
/** The subject every statically configured API key is admitted under. */
|
|
209
|
+
export const SHARED_API_KEY_SUBJECT = 'api-key-caller';
|
|
210
|
+
/** The subject an mTLS caller is admitted under when it carries no identity of its own. */
|
|
211
|
+
export const SHARED_MUTUAL_TLS_SUBJECT = 'mtls-caller';
|
|
212
|
+
const SHARED_FALLBACK_SUBJECTS = new Set([
|
|
213
|
+
SHARED_API_KEY_SUBJECT,
|
|
214
|
+
SHARED_MUTUAL_TLS_SUBJECT,
|
|
215
|
+
]);
|
|
216
|
+
/** Type guard separating authenticated from unauthenticated principals. */
|
|
217
|
+
export function isAuthenticated(principal) {
|
|
218
|
+
return principal.method !== 'unauthenticated';
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* A scope set that refuses to change. `Object.freeze` cannot freeze a `Set`'s
|
|
222
|
+
* contents, and the local principal is one shared instance whose `scopes` are
|
|
223
|
+
* public, so a caller that widened or emptied the set would change what every
|
|
224
|
+
* request is allowed to do.
|
|
225
|
+
*/
|
|
226
|
+
class ImmutableScopeSet extends Set {
|
|
227
|
+
constructor(scopes) {
|
|
228
|
+
super();
|
|
229
|
+
for (const scope of scopes)
|
|
230
|
+
super.add(scope);
|
|
231
|
+
}
|
|
232
|
+
add() {
|
|
233
|
+
throw new TypeError('The scopes of the unauthenticated-local principal are immutable');
|
|
234
|
+
}
|
|
235
|
+
delete() {
|
|
236
|
+
throw new TypeError('The scopes of the unauthenticated-local principal are immutable');
|
|
237
|
+
}
|
|
238
|
+
clear() {
|
|
239
|
+
throw new TypeError('The scopes of the unauthenticated-local principal are immutable');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const localPrincipals = new WeakMap();
|
|
243
|
+
function createUnauthenticatedLocalPrincipal(vocabulary) {
|
|
244
|
+
const scopes = new ImmutableScopeSet(vocabulary.scopes);
|
|
245
|
+
return Object.freeze({
|
|
246
|
+
method: 'unauthenticated-local',
|
|
247
|
+
scopes,
|
|
248
|
+
claims: undefined,
|
|
249
|
+
subject: 'unauthenticated-local',
|
|
250
|
+
hasScope(scope) {
|
|
251
|
+
return scopes.has(scope);
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Private replay/live-drain algorithm shared by every `createReplayLiveFeed()`
|
|
3
|
+
* instance (`workflow-event-feed.ts`'s per-workflow feed and
|
|
4
|
+
* `fleet-event-feed.ts`'s fleet-wide feed). Split out of
|
|
5
|
+
* `workflow-event-feed.ts` purely to keep that file under the repository's
|
|
6
|
+
* implementation-file line limit — nothing here is public API.
|
|
7
|
+
*
|
|
8
|
+
* @module server/replay-live-feed-internals
|
|
9
|
+
*/
|
|
10
|
+
import type { ReplayLiveFeedBackend, ReplayLiveSubscribeOptions } from "./replay-live-feed-types.js";
|
|
11
|
+
import type { SequencedEventEnvelope } from "./sequence-cursor.js";
|
|
12
|
+
/** Builds a queue that runs the operations it is given one at a time, in order. */
|
|
13
|
+
export declare function createSerialOperationQueue(): <T>(operation: () => Promise<T>) => Promise<T>;
|
|
14
|
+
type DurableSubscriptionOptions = {
|
|
15
|
+
readonly pollIntervalMs: number;
|
|
16
|
+
readonly lifecycleSignal?: AbortSignal;
|
|
17
|
+
};
|
|
18
|
+
/** Builds the subscription a replay-and-live feed hands out: replay first, then live envelopes. */
|
|
19
|
+
export declare function createDurableSubscription<TEnvelope extends SequencedEventEnvelope>(backend: ReplayLiveFeedBackend<TEnvelope>, options: DurableSubscriptionOptions, args?: ReplayLiveSubscribeOptions<TEnvelope>): AsyncIterable<TEnvelope>;
|
|
20
|
+
/** Thrown when a replay window exceeds the caller's configured limit. */
|
|
21
|
+
export declare class ReplayWindowExceededError extends Error {
|
|
22
|
+
/** How many envelopes the replay window held. */
|
|
23
|
+
readonly count: number;
|
|
24
|
+
/** The most envelopes the caller allowed. */
|
|
25
|
+
readonly limit: number;
|
|
26
|
+
constructor(
|
|
27
|
+
/** How many envelopes the replay window held. */
|
|
28
|
+
count: number,
|
|
29
|
+
/** The most envelopes the caller allowed. */
|
|
30
|
+
limit: number);
|
|
31
|
+
}
|
|
32
|
+
export declare function replayUpTo<TEnvelope extends SequencedEventEnvelope>(backend: ReplayLiveFeedBackend<TEnvelope>, afterSequence: number, snapshot: number, signal: AbortSignal | undefined, replayOptions: ReplayLiveSubscribeOptions<TEnvelope> | undefined, requestedCursor?: string, decodeCursor?: (cursor: string) => {
|
|
33
|
+
sequence: number;
|
|
34
|
+
} | 'gap' | 'invalid'): AsyncIterable<TEnvelope>;
|
|
35
|
+
export declare function replayAfterCursor(afterSequence: number, requestedCursor: string | undefined, decodeCursor: ((cursor: string) => {
|
|
36
|
+
sequence: number;
|
|
37
|
+
} | 'gap' | 'invalid') | undefined): number;
|
|
38
|
+
export declare function shouldDeliverEnvelope<TEnvelope extends SequencedEventEnvelope>(envelope: TEnvelope, replayOptions: ReplayLiveSubscribeOptions<TEnvelope> | undefined): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Park until a live append wakes the drain. The waker is installed first and the buffer, overflow
|
|
41
|
+
* flag and signal are re-checked afterwards, so an append, overflow or abort that lands between the
|
|
42
|
+
* caller's last look and the arming is never slept through; in that case the waker is withdrawn
|
|
43
|
+
* and the wait returns at once.
|
|
44
|
+
*/
|
|
45
|
+
export declare function armAndWait(buffer: SequencedEventEnvelope[], overflowed: () => boolean, signal: AbortSignal | undefined, installWaker: (fn: (() => void) | null) => void): Promise<void>;
|
|
46
|
+
export declare function drainLive<TEnvelope extends SequencedEventEnvelope>(buffer: TEnvelope[], snapshot: number, signal: AbortSignal | undefined, overflowed: () => boolean, installWaker: (fn: (() => void) | null) => void): AsyncIterable<TEnvelope>;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Private replay/live-drain algorithm shared by every `createReplayLiveFeed()`
|
|
3
|
+
* instance (`workflow-event-feed.ts`'s per-workflow feed and
|
|
4
|
+
* `fleet-event-feed.ts`'s fleet-wide feed). Split out of
|
|
5
|
+
* `workflow-event-feed.ts` purely to keep that file under the repository's
|
|
6
|
+
* implementation-file line limit — nothing here is public API.
|
|
7
|
+
*
|
|
8
|
+
* @module server/replay-live-feed-internals
|
|
9
|
+
*/
|
|
10
|
+
/** Builds a queue that runs the operations it is given one at a time, in order. */
|
|
11
|
+
export function createSerialOperationQueue() {
|
|
12
|
+
let queue = Promise.resolve();
|
|
13
|
+
return (operation) => {
|
|
14
|
+
const result = queue.then(operation, operation);
|
|
15
|
+
queue = result.then(() => undefined, () => undefined);
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Builds the subscription a replay-and-live feed hands out: replay first, then live envelopes. */
|
|
20
|
+
export function createDurableSubscription(backend, options, args) {
|
|
21
|
+
const requestedAfter = decodeRequestedCursor(args?.fromCursor);
|
|
22
|
+
const signal = args?.signal === undefined
|
|
23
|
+
? options.lifecycleSignal
|
|
24
|
+
: options.lifecycleSignal === undefined
|
|
25
|
+
? args.signal
|
|
26
|
+
: AbortSignal.any([args.signal, options.lifecycleSignal]);
|
|
27
|
+
let waker = null;
|
|
28
|
+
let cleanedUp = false;
|
|
29
|
+
const wake = () => {
|
|
30
|
+
const pending = waker;
|
|
31
|
+
waker = null;
|
|
32
|
+
pending?.();
|
|
33
|
+
};
|
|
34
|
+
const unsubscribe = backend.subscribeLive(wake);
|
|
35
|
+
const cleanup = () => {
|
|
36
|
+
if (cleanedUp)
|
|
37
|
+
return;
|
|
38
|
+
cleanedUp = true;
|
|
39
|
+
signal?.removeEventListener('abort', cleanup);
|
|
40
|
+
unsubscribe();
|
|
41
|
+
wake();
|
|
42
|
+
};
|
|
43
|
+
signal?.addEventListener('abort', cleanup, { once: true });
|
|
44
|
+
if (signal?.aborted)
|
|
45
|
+
cleanup();
|
|
46
|
+
async function* generator() {
|
|
47
|
+
try {
|
|
48
|
+
if (signal?.aborted)
|
|
49
|
+
return;
|
|
50
|
+
const snapshot = await backend.snapshotTailSequence();
|
|
51
|
+
yield* replayUpTo(backend, requestedAfter, snapshot, signal, args);
|
|
52
|
+
if (signal?.aborted)
|
|
53
|
+
return;
|
|
54
|
+
args?.onReplayComplete?.();
|
|
55
|
+
yield* tailDurableEvents(backend, snapshot, signal, args, options.pollIntervalMs, (next) => {
|
|
56
|
+
waker = next;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
cleanup();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return generator();
|
|
64
|
+
}
|
|
65
|
+
async function* tailDurableEvents(backend, snapshot, signal, args, pollIntervalMs, installWaker) {
|
|
66
|
+
let deliveredSequence = snapshot;
|
|
67
|
+
while (true) {
|
|
68
|
+
if (signal?.aborted)
|
|
69
|
+
break;
|
|
70
|
+
let found = false;
|
|
71
|
+
for await (const envelope of backend.replay({ afterSequence: deliveredSequence })) {
|
|
72
|
+
if (signal?.aborted)
|
|
73
|
+
break;
|
|
74
|
+
found = true;
|
|
75
|
+
deliveredSequence = Math.max(deliveredSequence, envelope.sequence);
|
|
76
|
+
if (shouldDeliverEnvelope(envelope, args))
|
|
77
|
+
yield envelope;
|
|
78
|
+
}
|
|
79
|
+
if (found)
|
|
80
|
+
continue;
|
|
81
|
+
await waitForAppendOrPoll(pollIntervalMs, signal, installWaker);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function decodeRequestedCursor(cursor) {
|
|
85
|
+
if (cursor === undefined)
|
|
86
|
+
return -1;
|
|
87
|
+
if (!/^(?:-1|\d+)$/.test(cursor))
|
|
88
|
+
throw new Error('Invalid cursor');
|
|
89
|
+
const sequence = Number(cursor);
|
|
90
|
+
if (!Number.isSafeInteger(sequence) || sequence < -1)
|
|
91
|
+
throw new Error('Invalid cursor');
|
|
92
|
+
return sequence;
|
|
93
|
+
}
|
|
94
|
+
async function waitForAppendOrPoll(pollIntervalMs, signal, installWaker) {
|
|
95
|
+
let timer;
|
|
96
|
+
await new Promise((resolve) => {
|
|
97
|
+
const finish = () => {
|
|
98
|
+
if (timer !== undefined)
|
|
99
|
+
clearTimeout(timer);
|
|
100
|
+
installWaker(null);
|
|
101
|
+
resolve();
|
|
102
|
+
};
|
|
103
|
+
installWaker(finish);
|
|
104
|
+
timer = setTimeout(finish, pollIntervalMs);
|
|
105
|
+
if (signal?.aborted)
|
|
106
|
+
finish();
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/** Thrown when a replay window exceeds the caller's configured limit. */
|
|
110
|
+
export class ReplayWindowExceededError extends Error {
|
|
111
|
+
count;
|
|
112
|
+
limit;
|
|
113
|
+
constructor(
|
|
114
|
+
/** How many envelopes the replay window held. */
|
|
115
|
+
count,
|
|
116
|
+
/** The most envelopes the caller allowed. */
|
|
117
|
+
limit) {
|
|
118
|
+
super(`Replay window is ${count} events; maximum is ${limit}.`);
|
|
119
|
+
this.count = count;
|
|
120
|
+
this.limit = limit;
|
|
121
|
+
this.name = 'ReplayWindowExceededError';
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
export async function* replayUpTo(backend, afterSequence, snapshot, signal, replayOptions, requestedCursor, decodeCursor) {
|
|
125
|
+
let replayCount = 0;
|
|
126
|
+
const replayAfter = replayAfterCursor(afterSequence, requestedCursor, decodeCursor);
|
|
127
|
+
const backendOptions = createBackendReplayOptions(replayAfter, replayOptions?.fromCursor);
|
|
128
|
+
for await (const envelope of backend.replay(backendOptions)) {
|
|
129
|
+
if (envelope.sequence > snapshot)
|
|
130
|
+
break;
|
|
131
|
+
if (signal?.aborted)
|
|
132
|
+
return;
|
|
133
|
+
if (!shouldDeliverEnvelope(envelope, replayOptions))
|
|
134
|
+
continue;
|
|
135
|
+
replayCount = updateReplayCount(envelope, replayOptions, replayCount);
|
|
136
|
+
yield envelope;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function updateReplayCount(envelope, replayOptions, replayCount) {
|
|
140
|
+
if (!shouldCountReplayEnvelope(envelope, replayOptions))
|
|
141
|
+
return replayCount;
|
|
142
|
+
const nextCount = replayCount + 1;
|
|
143
|
+
const replayLimit = replayOptions?.replayLimit;
|
|
144
|
+
if (replayLimit !== undefined && nextCount > replayLimit) {
|
|
145
|
+
throw createReplayLimitError(replayOptions, nextCount, replayLimit);
|
|
146
|
+
}
|
|
147
|
+
return nextCount;
|
|
148
|
+
}
|
|
149
|
+
function createBackendReplayOptions(afterSequence, requestedCursor) {
|
|
150
|
+
return requestedCursor === undefined ? { afterSequence } : { afterSequence, requestedCursor };
|
|
151
|
+
}
|
|
152
|
+
export function replayAfterCursor(afterSequence, requestedCursor, decodeCursor) {
|
|
153
|
+
if (requestedCursor === undefined || decodeCursor === undefined)
|
|
154
|
+
return afterSequence;
|
|
155
|
+
const decoded = decodeCursor(requestedCursor);
|
|
156
|
+
if (decoded === 'invalid')
|
|
157
|
+
throw new Error('Invalid cursor');
|
|
158
|
+
return decoded === 'gap' ? -1 : afterSequence;
|
|
159
|
+
}
|
|
160
|
+
export function shouldDeliverEnvelope(envelope, replayOptions) {
|
|
161
|
+
return replayOptions?.filterEnvelope?.(envelope) ?? true;
|
|
162
|
+
}
|
|
163
|
+
function shouldCountReplayEnvelope(envelope, replayOptions) {
|
|
164
|
+
return replayOptions?.countReplayEnvelope?.(envelope) ?? true;
|
|
165
|
+
}
|
|
166
|
+
function createReplayLimitError(replayOptions, count, limit) {
|
|
167
|
+
return (replayOptions?.createReplayLimitError?.(count, limit) ??
|
|
168
|
+
new ReplayWindowExceededError(count, limit));
|
|
169
|
+
}
|
|
170
|
+
function flushPendingBuffer(buffer, watermark) {
|
|
171
|
+
const batch = [];
|
|
172
|
+
let newWatermark = watermark;
|
|
173
|
+
let head = buffer.shift();
|
|
174
|
+
while (head !== undefined) {
|
|
175
|
+
if (head.sequence > newWatermark) {
|
|
176
|
+
batch.push(head);
|
|
177
|
+
newWatermark = head.sequence;
|
|
178
|
+
}
|
|
179
|
+
head = buffer.shift();
|
|
180
|
+
}
|
|
181
|
+
return { batch, newWatermark };
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Park until a live append wakes the drain. The waker is installed first and the buffer, overflow
|
|
185
|
+
* flag and signal are re-checked afterwards, so an append, overflow or abort that lands between the
|
|
186
|
+
* caller's last look and the arming is never slept through; in that case the waker is withdrawn
|
|
187
|
+
* and the wait returns at once.
|
|
188
|
+
*/
|
|
189
|
+
export async function armAndWait(buffer, overflowed, signal, installWaker) {
|
|
190
|
+
const armed = new Promise((resolve) => {
|
|
191
|
+
installWaker(resolve);
|
|
192
|
+
});
|
|
193
|
+
if (buffer.length > 0 || overflowed() || signal?.aborted) {
|
|
194
|
+
installWaker(null);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
await armed;
|
|
198
|
+
}
|
|
199
|
+
export async function* drainLive(buffer, snapshot, signal, overflowed, installWaker) {
|
|
200
|
+
let watermark = snapshot;
|
|
201
|
+
while (true) {
|
|
202
|
+
if (signal?.aborted || overflowed())
|
|
203
|
+
break;
|
|
204
|
+
const { batch, newWatermark } = flushPendingBuffer(buffer, watermark);
|
|
205
|
+
watermark = newWatermark;
|
|
206
|
+
for (const envelope of batch) {
|
|
207
|
+
if (signal?.aborted || overflowed())
|
|
208
|
+
return;
|
|
209
|
+
yield envelope;
|
|
210
|
+
}
|
|
211
|
+
if (batch.length > 0)
|
|
212
|
+
continue;
|
|
213
|
+
await armAndWait(buffer, overflowed, signal, installWaker);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The types of a replay-and-live feed: its storage backend, the feed over it, and its subscribe
|
|
3
|
+
* options. The implementation is `replay-live-feed.ts`.
|
|
4
|
+
*
|
|
5
|
+
* @module server/replay-live-feed-types
|
|
6
|
+
*/
|
|
7
|
+
import type { Cursor, SequencedEventEnvelope } from "./sequence-cursor.js";
|
|
8
|
+
/** The storage a {@link ReplayLiveFeed} reads: a replayable log plus a live subscription. Only `replay`, `snapshotTailSequence` and `subscribeLive` are required. */
|
|
9
|
+
export type ReplayLiveFeedBackend<TEnvelope extends SequencedEventEnvelope> = {
|
|
10
|
+
/** Yields the retained envelopes after the given sequence. */
|
|
11
|
+
replay(options: {
|
|
12
|
+
afterSequence: number;
|
|
13
|
+
requestedCursor?: Cursor;
|
|
14
|
+
}): AsyncIterable<TEnvelope>;
|
|
15
|
+
/** The sequence of the newest envelope, which bounds the replay. */
|
|
16
|
+
snapshotTailSequence(): Promise<number>;
|
|
17
|
+
/** Calls the listener for each new envelope and returns a function that stops it. */
|
|
18
|
+
subscribeLive(listener: (envelope: TEnvelope) => void): () => void;
|
|
19
|
+
/** Decodes a backend-specific cursor, or reports it as `gap` or `invalid`. */
|
|
20
|
+
decodeCursor?(cursor: Cursor): {
|
|
21
|
+
sequence: number;
|
|
22
|
+
} | 'gap' | 'invalid';
|
|
23
|
+
/** Encodes a sequence as a backend-specific cursor. */
|
|
24
|
+
encodeCursor?(sequence: number): Cursor;
|
|
25
|
+
/** The sequence of the oldest retained envelope, so the feed can detect a gap. */
|
|
26
|
+
snapshotFirstRetainedSequence?(): Promise<number>;
|
|
27
|
+
/** Builds the envelope that tells a subscriber its cursor fell outside the retained window. */
|
|
28
|
+
createGapEnvelope?(options: {
|
|
29
|
+
requestedCursor: Cursor;
|
|
30
|
+
firstRetainedSequence: number;
|
|
31
|
+
continuationCursor: Cursor;
|
|
32
|
+
}): TEnvelope;
|
|
33
|
+
};
|
|
34
|
+
/** A replay-and-live feed over one {@link ReplayLiveFeedBackend}: replay from a cursor, then follow live envelopes without a gap or a duplicate. */
|
|
35
|
+
export type ReplayLiveFeed<TEnvelope extends SequencedEventEnvelope> = {
|
|
36
|
+
/** Yields the retained envelopes after `fromCursor`, up to `limit`. */
|
|
37
|
+
replay(options?: {
|
|
38
|
+
fromCursor?: Cursor;
|
|
39
|
+
limit?: number;
|
|
40
|
+
}): AsyncIterable<TEnvelope>;
|
|
41
|
+
/** Yields the envelopes after `fromCursor`, then live ones, until the signal aborts. */
|
|
42
|
+
subscribe(options?: ReplayLiveSubscribeOptions<TEnvelope>): AsyncIterable<TEnvelope>;
|
|
43
|
+
/** Decodes a cursor to its sequence, or reports it as `gap` or `invalid`. */
|
|
44
|
+
decodeCursor(cursor: Cursor): {
|
|
45
|
+
sequence: number;
|
|
46
|
+
} | 'gap' | 'invalid';
|
|
47
|
+
/** Releases the feed's live subscription. */
|
|
48
|
+
dispose(): void;
|
|
49
|
+
};
|
|
50
|
+
/** Options for {@link ReplayLiveFeed.subscribe}: where to resume, how to stop, and how to bound and filter the replay. */
|
|
51
|
+
export type ReplayLiveSubscribeOptions<TEnvelope extends SequencedEventEnvelope> = {
|
|
52
|
+
/** Resume after this cursor. */
|
|
53
|
+
fromCursor?: Cursor;
|
|
54
|
+
/** Ends the subscription when aborted. */
|
|
55
|
+
signal?: AbortSignal;
|
|
56
|
+
/** The most envelopes a replay may deliver. */
|
|
57
|
+
replayLimit?: number;
|
|
58
|
+
/** Drops the envelopes for which this returns false. */
|
|
59
|
+
filterEnvelope?: (envelope: TEnvelope) => boolean;
|
|
60
|
+
/** Whether a replayed envelope counts toward `replayLimit`. */
|
|
61
|
+
countReplayEnvelope?: (envelope: TEnvelope) => boolean;
|
|
62
|
+
/** Builds the error thrown when the replay exceeds `replayLimit`. */
|
|
63
|
+
createReplayLimitError?: (count: number, limit: number) => unknown;
|
|
64
|
+
/** Called once the replay has been delivered and live envelopes begin. */
|
|
65
|
+
onReplayComplete?: () => void;
|
|
66
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
type Cursor = string;
|
|
2
|
+
type SequencedEventEnvelope = {
|
|
3
|
+
readonly sequence: number;
|
|
4
|
+
readonly cursor: Cursor;
|
|
5
|
+
};
|
|
6
|
+
type ReplayLiveFeedBackend<T extends SequencedEventEnvelope> = {
|
|
7
|
+
replay(options: {
|
|
8
|
+
afterSequence: number;
|
|
9
|
+
requestedCursor?: Cursor;
|
|
10
|
+
}): AsyncIterable<T>;
|
|
11
|
+
snapshotTailSequence(): Promise<number>;
|
|
12
|
+
subscribeLive(listener: (envelope: T) => void): () => void;
|
|
13
|
+
decodeCursor?(cursor: Cursor): {
|
|
14
|
+
sequence: number;
|
|
15
|
+
} | 'gap' | 'invalid';
|
|
16
|
+
encodeCursor?(sequence: number): Cursor;
|
|
17
|
+
snapshotFirstRetainedSequence?(): Promise<number>;
|
|
18
|
+
createGapEnvelope?(options: {
|
|
19
|
+
requestedCursor: Cursor;
|
|
20
|
+
firstRetainedSequence: number;
|
|
21
|
+
continuationCursor: Cursor;
|
|
22
|
+
}): T;
|
|
23
|
+
};
|
|
24
|
+
type ReplayLiveSubscribeOptions<T> = {
|
|
25
|
+
fromCursor?: Cursor;
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
replayLimit?: number;
|
|
28
|
+
filterEnvelope?: (envelope: T) => boolean;
|
|
29
|
+
countReplayEnvelope?: (envelope: T) => boolean;
|
|
30
|
+
createReplayLimitError?: (count: number, limit: number) => unknown;
|
|
31
|
+
onReplayComplete?: () => void;
|
|
32
|
+
};
|
|
33
|
+
type WorkflowEventFeedOptions = {
|
|
34
|
+
liveBufferSize?: number;
|
|
35
|
+
};
|
|
36
|
+
type ReplayLiveFeed<T extends SequencedEventEnvelope> = {
|
|
37
|
+
replay(options?: {
|
|
38
|
+
fromCursor?: Cursor;
|
|
39
|
+
limit?: number;
|
|
40
|
+
}): AsyncIterable<T>;
|
|
41
|
+
subscribe(options?: ReplayLiveSubscribeOptions<T>): AsyncIterable<T>;
|
|
42
|
+
decodeCursor(cursor: Cursor): {
|
|
43
|
+
sequence: number;
|
|
44
|
+
} | 'gap' | 'invalid';
|
|
45
|
+
dispose(): void;
|
|
46
|
+
};
|
|
47
|
+
/** Builds a {@link ReplayLiveFeed} over a backend. `liveBufferSize` bounds the envelopes buffered for a subscriber that reads slowly. */
|
|
48
|
+
export declare function createReplayLiveFeed<TEnvelope extends SequencedEventEnvelope>(backend: ReplayLiveFeedBackend<TEnvelope>, options?: WorkflowEventFeedOptions): ReplayLiveFeed<TEnvelope>;
|
|
49
|
+
export {};
|