@astrale-os/kernel-client 1.0.0-beta.4 → 1.0.0-beta.41
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/dist/auth/index.d.ts +1 -0
- package/dist/client/admission.js +3 -1
- package/dist/client/client.d.ts +19 -2
- package/dist/client/client.js +62 -30
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.js +2 -2
- package/dist/client/request.d.ts +8 -2
- package/dist/client/request.js +28 -7
- package/dist/client/result.d.ts +10 -1
- package/dist/client/result.js +10 -4
- package/dist/client/routing-control.d.ts +11 -15
- package/dist/client/routing-control.js +21 -29
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/index.js +3 -0
- package/dist/errors/query-input.d.ts +47 -0
- package/dist/errors/query-input.js +85 -0
- package/dist/errors/refusal.d.ts +24 -0
- package/dist/errors/refusal.js +78 -0
- package/dist/errors/response-too-large-error.d.ts +11 -0
- package/dist/errors/response-too-large-error.js +15 -0
- package/dist/errors/stale-route-error.d.ts +1 -1
- package/dist/errors/stale-route-error.js +1 -1
- package/dist/{schema → execution}/admission.d.ts +2 -2
- package/dist/{schema → execution}/admission.js +3 -3
- package/dist/execution/contracts.d.ts +76 -0
- package/dist/execution/contracts.js +1 -0
- package/dist/execution/deadline.d.ts +18 -0
- package/dist/execution/deadline.js +25 -0
- package/dist/execution/execution.d.ts +61 -0
- package/dist/execution/execution.js +132 -0
- package/dist/execution/index.d.ts +4 -0
- package/dist/execution/index.js +3 -0
- package/dist/graph/domain/binding.d.ts +2 -2
- package/dist/graph/domain/binding.js +24 -17
- package/dist/graph/domain/conformance.d.ts +12 -0
- package/dist/graph/domain/conformance.js +18 -0
- package/dist/graph/domain/index.d.ts +1 -0
- package/dist/graph/domain/index.js +1 -1
- package/dist/graph/domain/provenance.d.ts +0 -2
- package/dist/graph/domain/provenance.js +0 -4
- package/dist/graph/graph.d.ts +17 -3
- package/dist/graph/graph.js +4 -1
- package/dist/graph/index.d.ts +3 -3
- package/dist/graph/index.js +1 -1
- package/dist/graph/page-contract.d.ts +66 -1
- package/dist/graph/provenance.d.ts +4 -5
- package/dist/graph/provenance.js +5 -6
- package/dist/graph/query.d.ts +10 -4
- package/dist/graph/query.js +149 -7
- package/dist/graph/query.typecheck.d.ts +79 -0
- package/dist/graph/query.typecheck.js +34 -0
- package/dist/pacer/index.d.ts +1 -1
- package/dist/pacer/index.js +1 -1
- package/dist/pacer/retry/index.d.ts +1 -0
- package/dist/pacer/retry/index.js +1 -0
- package/dist/pacer/retry/refusals.d.ts +7 -0
- package/dist/pacer/retry/refusals.js +10 -0
- package/dist/pacer/retry/retry.d.ts +2 -0
- package/dist/pacer/retry/retry.js +33 -1
- package/dist/schema/contracts.d.ts +77 -9
- package/dist/schema/index.d.ts +3 -2
- package/dist/schema/index.js +1 -1
- package/dist/schema/installation.d.ts +76 -0
- package/dist/schema/installation.js +82 -0
- package/dist/schema/schema.d.ts +22 -13
- package/dist/schema/schema.js +67 -48
- package/dist/session/admission.js +10 -3
- package/dist/session/bound.d.ts +12 -3
- package/dist/session/bound.js +66 -20
- package/dist/session/content/api.d.ts +20 -2
- package/dist/session/content/api.js +17 -7
- package/dist/session/content/files.d.ts +12 -0
- package/dist/session/content/files.js +47 -0
- package/dist/session/content/index.d.ts +1 -1
- package/dist/session/content/input.d.ts +13 -2
- package/dist/session/content/input.js +25 -4
- package/dist/session/content/retry.d.ts +5 -1
- package/dist/session/content/retry.js +21 -31
- package/dist/session/context/index.d.ts +1 -1
- package/dist/session/context/index.js +1 -1
- package/dist/session/context/operation.d.ts +2 -0
- package/dist/session/context/operation.js +5 -0
- package/dist/session/credential/credentials.js +43 -28
- package/dist/session/discovery/bundle.d.ts +9 -4
- package/dist/session/discovery/bundle.js +28 -35
- package/dist/session/discovery/discovery.d.ts +1 -1
- package/dist/session/discovery/discovery.js +8 -11
- package/dist/session/discovery/release.js +18 -28
- package/dist/session/discovery/snapshot.d.ts +5 -1
- package/dist/session/discovery/transport.d.ts +35 -3
- package/dist/session/discovery/transport.js +40 -1
- package/dist/session/durable.d.ts +13 -0
- package/dist/session/durable.js +39 -0
- package/dist/session/exchange/exchange.d.ts +3 -2
- package/dist/session/exchange/exchange.js +10 -7
- package/dist/session/exchange/index.d.ts +1 -1
- package/dist/session/exchange/index.js +1 -1
- package/dist/session/exchange/presentation.js +28 -10
- package/dist/session/exchange/source.d.ts +7 -7
- package/dist/session/exchange/source.js +26 -27
- package/dist/session/index.d.ts +5 -4
- package/dist/session/index.js +0 -1
- package/dist/session/options.d.ts +0 -2
- package/dist/session/options.js +0 -3
- package/dist/session/reference.d.ts +27 -6
- package/dist/session/reference.js +18 -3
- package/dist/session/retry.d.ts +12 -0
- package/dist/session/retry.js +23 -0
- package/dist/session/routing/auth.d.ts +9 -3
- package/dist/session/routing/auth.js +4 -3
- package/dist/session/routing/cache.d.ts +12 -5
- package/dist/session/routing/cache.js +56 -48
- package/dist/session/routing/dispatch.d.ts +26 -3
- package/dist/session/routing/dispatch.js +112 -67
- package/dist/session/routing/envelope.d.ts +3 -3
- package/dist/session/routing/index.d.ts +1 -1
- package/dist/session/routing/legacy/release-route.d.ts +12 -0
- package/dist/session/routing/legacy/release-route.js +6 -0
- package/dist/session/routing/recovery.d.ts +3 -6
- package/dist/session/routing/recovery.js +3 -14
- package/dist/session/routing/request.d.ts +6 -1
- package/dist/session/routing/request.js +13 -2
- package/dist/session/routing/result.d.ts +1 -1
- package/dist/session/routing/result.js +5 -1
- package/dist/session/routing/store.js +4 -5
- package/dist/session/session.d.ts +31 -5
- package/dist/session/session.js +67 -20
- package/dist/session/session.typecheck.js +20 -6
- package/dist/session/views/index.d.ts +2 -2
- package/dist/session/views/index.js +2 -2
- package/dist/session/views/kernel.d.ts +31 -0
- package/dist/session/views/kernel.js +30 -0
- package/dist/session/views/legacy/catalog.d.ts +21 -0
- package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
- package/dist/session/views/legacy/index.d.ts +14 -0
- package/dist/session/views/legacy/index.js +14 -0
- package/dist/session/views/legacy/resolve.d.ts +46 -0
- package/dist/session/views/legacy/resolve.js +83 -0
- package/dist/session/views/legacy/unplaced.d.ts +22 -0
- package/dist/session/views/legacy/unplaced.js +26 -0
- package/dist/session/views/read.d.ts +49 -0
- package/dist/session/views/read.js +148 -0
- package/dist/session/views/view.d.ts +48 -0
- package/dist/session/views/view.js +74 -0
- package/dist/store/memory.d.ts +1 -3
- package/dist/store/memory.js +73 -105
- package/dist/store/shape.d.ts +33 -12
- package/dist/store/shape.js +125 -16
- package/dist/store/store.d.ts +6 -6
- package/dist/store/store.js +1 -1
- package/dist/transport/auto/decision.d.ts +26 -14
- package/dist/transport/auto/decision.js +35 -32
- package/dist/transport/auto/fallback.d.ts +1 -1
- package/dist/transport/auto/fallback.js +5 -10
- package/dist/transport/auto/transport.d.ts +0 -2
- package/dist/transport/auto/transport.js +24 -32
- package/dist/transport/deadline.d.ts +2 -0
- package/dist/transport/deadline.js +18 -0
- package/dist/transport/failure.d.ts +28 -1
- package/dist/transport/failure.js +63 -1
- package/dist/transport/http/admission.js +7 -34
- package/dist/transport/http/binary-request.d.ts +44 -0
- package/dist/transport/http/binary-request.js +110 -0
- package/dist/transport/http/binary.js +3 -3
- package/dist/transport/http/byte-stream.d.ts +2 -1
- package/dist/transport/http/byte-stream.js +12 -8
- package/dist/transport/http/index.d.ts +1 -1
- package/dist/transport/http/index.js +1 -1
- package/dist/transport/http/response.d.ts +3 -1
- package/dist/transport/http/response.js +7 -9
- package/dist/transport/http/stream.d.ts +8 -13
- package/dist/transport/http/stream.js +34 -81
- package/dist/transport/http/transport.d.ts +10 -0
- package/dist/transport/http/transport.js +87 -32
- package/dist/transport/http/unprocessed.d.ts +5 -0
- package/dist/transport/http/unprocessed.js +28 -0
- package/dist/transport/index.d.ts +1 -1
- package/dist/transport/index.js +1 -1
- package/dist/transport/stream.d.ts +52 -0
- package/dist/transport/stream.js +101 -0
- package/dist/transport/transport.d.ts +10 -0
- package/dist/transport/unary.d.ts +8 -0
- package/dist/transport/unary.js +34 -0
- package/dist/transport/websocket/connection.d.ts +7 -3
- package/dist/transport/websocket/connection.js +103 -92
- package/dist/transport/websocket/exchange.d.ts +12 -19
- package/dist/transport/websocket/exchange.js +86 -184
- package/dist/transport/websocket/transport.js +11 -19
- package/package.json +9 -7
- package/dist/session/execution.d.ts +0 -65
- package/dist/session/execution.js +0 -34
- package/dist/session/views/catalog.d.ts +0 -19
- package/dist/session/views/resolve.d.ts +0 -13
- package/dist/session/views/resolve.js +0 -21
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Value } from '@astrale-os/kernel-dsl/value';
|
|
2
|
+
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
+
import type { Stream } from '../client/index.js';
|
|
4
|
+
import type { ResponseError } from '../errors/index.js';
|
|
5
|
+
/** A frame that follows a stream's `stream.start`. */
|
|
6
|
+
export type StreamProgress = Exclude<protocol.StreamFrame, protocol.StreamStartFrame>;
|
|
7
|
+
/** What one accepted frame does to its stream. */
|
|
8
|
+
export type FrameStep = {
|
|
9
|
+
readonly kind: 'item';
|
|
10
|
+
readonly value: Value;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: 'end';
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: 'error';
|
|
15
|
+
readonly error: ResponseError;
|
|
16
|
+
};
|
|
17
|
+
/** The binding that feeds one stream its frames. */
|
|
18
|
+
export interface FrameSource {
|
|
19
|
+
/**
|
|
20
|
+
* Answers a read the stream cannot serve from its buffer. A binding that reads on demand answers
|
|
21
|
+
* every read itself, its ending included; a binding that pushes items omits it.
|
|
22
|
+
*/
|
|
23
|
+
readonly read?: () => Promise<IteratorResult<Value>>;
|
|
24
|
+
/** Stops the stream for its consumer. */
|
|
25
|
+
readonly cancel: (reason: string) => Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
/** The frames of one stream after its `stream.start`, sequenced, and read by exactly one consumer. */
|
|
28
|
+
export declare class FrameStream implements Stream {
|
|
29
|
+
readonly invocation: protocol.InvocationId;
|
|
30
|
+
private readonly source;
|
|
31
|
+
private readonly maximumBufferedItems;
|
|
32
|
+
private claimed;
|
|
33
|
+
private index;
|
|
34
|
+
private readonly items;
|
|
35
|
+
private readonly waiters;
|
|
36
|
+
private ending;
|
|
37
|
+
constructor(invocation: protocol.InvocationId, source: FrameSource, maximumBufferedItems?: number);
|
|
38
|
+
[Symbol.asyncIterator](): AsyncIterator<Value>;
|
|
39
|
+
cancel(reason?: string): Promise<void>;
|
|
40
|
+
/** The step `frame` takes; an item out of index order or a wrong end count is a ProtocolError. */
|
|
41
|
+
accept(frame: StreamProgress): FrameStep;
|
|
42
|
+
/** Hands `value` to the oldest waiting read, or buffers it; false when the buffer is full. */
|
|
43
|
+
push(value: Value): boolean;
|
|
44
|
+
/** Ends the stream: its buffered items stay readable, then every read is done. */
|
|
45
|
+
end(): void;
|
|
46
|
+
/** Fails the stream: its buffered items stay readable, then every read rejects with `error`. */
|
|
47
|
+
fail(error: Error): void;
|
|
48
|
+
/** Drops the buffered items the consumer has not read. */
|
|
49
|
+
discard(): void;
|
|
50
|
+
private next;
|
|
51
|
+
private settle;
|
|
52
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { ProtocolError } from '../errors/index.js';
|
|
2
|
+
import { responseError } from './unary.js';
|
|
3
|
+
/** The frames of one stream after its `stream.start`, sequenced, and read by exactly one consumer. */
|
|
4
|
+
export class FrameStream {
|
|
5
|
+
invocation;
|
|
6
|
+
source;
|
|
7
|
+
maximumBufferedItems;
|
|
8
|
+
claimed = false;
|
|
9
|
+
index = 0;
|
|
10
|
+
items = [];
|
|
11
|
+
waiters = [];
|
|
12
|
+
ending = { kind: 'open' };
|
|
13
|
+
constructor(invocation, source, maximumBufferedItems = Number.POSITIVE_INFINITY) {
|
|
14
|
+
this.invocation = invocation;
|
|
15
|
+
this.source = source;
|
|
16
|
+
this.maximumBufferedItems = maximumBufferedItems;
|
|
17
|
+
}
|
|
18
|
+
[Symbol.asyncIterator]() {
|
|
19
|
+
if (this.claimed)
|
|
20
|
+
throw new TypeError('streams admit exactly one consumer.');
|
|
21
|
+
this.claimed = true;
|
|
22
|
+
return {
|
|
23
|
+
next: () => this.next(),
|
|
24
|
+
return: async () => {
|
|
25
|
+
await this.cancel('stream consumer stopped');
|
|
26
|
+
return { done: true, value: undefined };
|
|
27
|
+
},
|
|
28
|
+
throw: async (error) => {
|
|
29
|
+
await this.cancel('stream consumer failed');
|
|
30
|
+
throw error;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
cancel(reason = 'stream cancelled') {
|
|
35
|
+
return this.source.cancel(reason);
|
|
36
|
+
}
|
|
37
|
+
/** The step `frame` takes; an item out of index order or a wrong end count is a ProtocolError. */
|
|
38
|
+
accept(frame) {
|
|
39
|
+
if (frame.kind === 'stream.item') {
|
|
40
|
+
if (frame.index !== this.index) {
|
|
41
|
+
throw new ProtocolError(`stream item ${frame.index} does not match expected index ${this.index}.`);
|
|
42
|
+
}
|
|
43
|
+
this.index += 1;
|
|
44
|
+
return { kind: 'item', value: frame.value };
|
|
45
|
+
}
|
|
46
|
+
if (frame.kind === 'stream.error') {
|
|
47
|
+
return { kind: 'error', error: responseError(frame.error, this.invocation) };
|
|
48
|
+
}
|
|
49
|
+
if (frame.count !== this.index) {
|
|
50
|
+
throw new ProtocolError(`stream ended with count ${frame.count}; ${this.index} items were received.`);
|
|
51
|
+
}
|
|
52
|
+
return { kind: 'end' };
|
|
53
|
+
}
|
|
54
|
+
/** Hands `value` to the oldest waiting read, or buffers it; false when the buffer is full. */
|
|
55
|
+
push(value) {
|
|
56
|
+
const waiter = this.waiters.shift();
|
|
57
|
+
if (waiter !== undefined) {
|
|
58
|
+
waiter.resolve({ done: false, value });
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (this.items.length >= this.maximumBufferedItems)
|
|
62
|
+
return false;
|
|
63
|
+
this.items.push(value);
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
/** Ends the stream: its buffered items stay readable, then every read is done. */
|
|
67
|
+
end() {
|
|
68
|
+
this.settle({ kind: 'ended' });
|
|
69
|
+
}
|
|
70
|
+
/** Fails the stream: its buffered items stay readable, then every read rejects with `error`. */
|
|
71
|
+
fail(error) {
|
|
72
|
+
this.settle({ kind: 'failed', error });
|
|
73
|
+
}
|
|
74
|
+
/** Drops the buffered items the consumer has not read. */
|
|
75
|
+
discard() {
|
|
76
|
+
this.items.length = 0;
|
|
77
|
+
}
|
|
78
|
+
next() {
|
|
79
|
+
const value = this.items.shift();
|
|
80
|
+
if (value !== undefined)
|
|
81
|
+
return Promise.resolve({ done: false, value });
|
|
82
|
+
if (this.ending.kind === 'ended')
|
|
83
|
+
return Promise.resolve({ done: true, value: undefined });
|
|
84
|
+
if (this.ending.kind === 'failed')
|
|
85
|
+
return Promise.reject(this.ending.error);
|
|
86
|
+
if (this.source.read !== undefined)
|
|
87
|
+
return this.source.read();
|
|
88
|
+
return new Promise((resolve, reject) => this.waiters.push({ resolve, reject }));
|
|
89
|
+
}
|
|
90
|
+
settle(ending) {
|
|
91
|
+
if (this.ending.kind !== 'open')
|
|
92
|
+
return;
|
|
93
|
+
this.ending = ending;
|
|
94
|
+
for (const waiter of this.waiters.splice(0)) {
|
|
95
|
+
if (ending.kind === 'ended')
|
|
96
|
+
waiter.resolve({ done: true, value: undefined });
|
|
97
|
+
else
|
|
98
|
+
waiter.reject(ending.error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -4,6 +4,14 @@ export interface RequestOptions {
|
|
|
4
4
|
readonly signal?: AbortSignal;
|
|
5
5
|
readonly timeoutMs?: number;
|
|
6
6
|
readonly idempotencyKey?: protocol.IdempotencyKey;
|
|
7
|
+
/**
|
|
8
|
+
* The maintenance session a root request expects to act within, by its install execution id. It
|
|
9
|
+
* grants nothing: the Kernel refuses the request `MAINTENANCE_SESSION_STALE` unless that session
|
|
10
|
+
* is the Instance's live one. The Client carries it in the source request, never to a destination.
|
|
11
|
+
*/
|
|
12
|
+
readonly maintenance?: {
|
|
13
|
+
readonly session: string;
|
|
14
|
+
};
|
|
7
15
|
}
|
|
8
16
|
export interface TransportRequest {
|
|
9
17
|
readonly call: protocol.Call;
|
|
@@ -14,6 +22,8 @@ export interface TransportRequest {
|
|
|
14
22
|
readonly routing?: protocol.Routing;
|
|
15
23
|
/** Source-Kernel Schema Registration precondition; omitted after routing. */
|
|
16
24
|
readonly schema?: protocol.SchemaTarget;
|
|
25
|
+
/** Source-Kernel maintenance session guard; omitted after routing. */
|
|
26
|
+
readonly maintenance?: protocol.Maintenance;
|
|
17
27
|
}
|
|
18
28
|
/** Correlated reserved control decoded independently from application results and errors. */
|
|
19
29
|
export interface StaleControl {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ErrorPayload } from '@astrale-os/kernel-protocol/errors';
|
|
2
|
+
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
+
import type { TransportResult } from './transport.js';
|
|
4
|
+
import { ResponseError } from '../errors/index.js';
|
|
5
|
+
/** The application error one decoded error response or `stream.error` frame of `invocation` carries. */
|
|
6
|
+
export declare function responseError(error: ErrorPayload, invocation: protocol.InvocationId): ResponseError;
|
|
7
|
+
/** The Transport result of one decoded unary response that carries no application error. */
|
|
8
|
+
export declare function unaryResult(response: Exclude<protocol.Response, protocol.ErrorResponse>): TransportResult;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ResponseError } from '../errors/index.js';
|
|
2
|
+
/** The application error one decoded error response or `stream.error` frame of `invocation` carries. */
|
|
3
|
+
export function responseError(error, invocation) {
|
|
4
|
+
return new ResponseError(error.code, error.message, invocation, error.data);
|
|
5
|
+
}
|
|
6
|
+
/** The Transport result of one decoded unary response that carries no application error. */
|
|
7
|
+
export function unaryResult(response) {
|
|
8
|
+
if ('kind' in response) {
|
|
9
|
+
if (response.kind === 'redirect') {
|
|
10
|
+
return Object.freeze({
|
|
11
|
+
kind: 'redirect',
|
|
12
|
+
invocation: response.invocation,
|
|
13
|
+
redirect: response.redirect,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
if (response.kind === 'stale') {
|
|
17
|
+
return Object.freeze({
|
|
18
|
+
kind: 'stale',
|
|
19
|
+
current: response.current,
|
|
20
|
+
invocation: response.invocation,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return Object.freeze({ kind: 'miss', invocation: response.invocation });
|
|
24
|
+
}
|
|
25
|
+
if ('execution' in response) {
|
|
26
|
+
return {
|
|
27
|
+
kind: 'execution',
|
|
28
|
+
execution: response.execution,
|
|
29
|
+
...(response.replayed === undefined ? {} : { replayed: response.replayed }),
|
|
30
|
+
invocation: response.invocation,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return { kind: 'value', value: response.result, invocation: response.invocation };
|
|
34
|
+
}
|
|
@@ -2,7 +2,7 @@ import type * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
|
2
2
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
3
|
import type { ControlledSignal } from '../index.js';
|
|
4
4
|
import type { TransportResult } from '../index.js';
|
|
5
|
-
import type { ExchangeOwner } from './exchange.js';
|
|
5
|
+
import type { ExchangeClose, ExchangeOwner } from './exchange.js';
|
|
6
6
|
import type { WebSocketConstructor } from './transport.js';
|
|
7
7
|
import { WebSocketExchange } from './exchange.js';
|
|
8
8
|
interface ConnectionOwner {
|
|
@@ -29,11 +29,15 @@ export declare class WebSocketConnection implements ExchangeOwner {
|
|
|
29
29
|
get isAvailable(): boolean;
|
|
30
30
|
connect(control: ControlledSignal): Promise<void>;
|
|
31
31
|
dispatch(request: protocol.Request, control: ControlledSignal, maximumBufferedItems: number): Promise<TransportResult>;
|
|
32
|
-
finish(exchange: WebSocketExchange,
|
|
32
|
+
finish(exchange: WebSocketExchange, close?: ExchangeClose): void;
|
|
33
33
|
cancel(exchange: WebSocketExchange, requestId: protocol.RequestId): void;
|
|
34
|
-
|
|
34
|
+
/** Closes the connection on its owner's behalf, failing what it still carries with `error`. */
|
|
35
|
+
shutdown(error: Error): void;
|
|
36
|
+
private end;
|
|
35
37
|
private onOpen;
|
|
36
38
|
private onMessage;
|
|
39
|
+
/** Hands one message to its exchange, or answers the close it breaks the connection with. */
|
|
40
|
+
private receive;
|
|
37
41
|
private onFailure;
|
|
38
42
|
}
|
|
39
43
|
export {};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
2
|
import { ProtocolError } from '../../errors/index.js';
|
|
3
3
|
import { invocationFailure } from '../failure.js';
|
|
4
|
-
import { asError } from '../index.js';
|
|
5
|
-
import { WebSocketExchange } from './exchange.js';
|
|
4
|
+
import { asError, requireLive } from '../index.js';
|
|
5
|
+
import { isKind, WebSocketExchange } from './exchange.js';
|
|
6
6
|
const WS_CONNECTING = 0;
|
|
7
7
|
const WS_OPEN = 1;
|
|
8
8
|
export class WebSocketConnection {
|
|
@@ -46,12 +46,10 @@ export class WebSocketConnection {
|
|
|
46
46
|
return !this.closedState;
|
|
47
47
|
}
|
|
48
48
|
async connect(control) {
|
|
49
|
-
|
|
50
|
-
throw asError(control.signal.reason);
|
|
51
|
-
}
|
|
49
|
+
requireLive(control);
|
|
52
50
|
let abort;
|
|
53
51
|
const cancelled = new Promise((_resolve, reject) => {
|
|
54
|
-
abort = () => reject(
|
|
52
|
+
abort = () => reject(control.signal.reason);
|
|
55
53
|
control.signal.addEventListener('abort', abort, { once: true });
|
|
56
54
|
});
|
|
57
55
|
try {
|
|
@@ -89,11 +87,15 @@ export class WebSocketConnection {
|
|
|
89
87
|
this.active.set(key, exchange);
|
|
90
88
|
return exchange.start((data) => this.socket.send(socketData(data)), encoded);
|
|
91
89
|
}
|
|
92
|
-
finish(exchange,
|
|
90
|
+
finish(exchange, close) {
|
|
93
91
|
removeExchange(this.active, exchange);
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
if (close === undefined)
|
|
93
|
+
return;
|
|
94
|
+
this.end({
|
|
95
|
+
code: close,
|
|
96
|
+
reason: closeReason(close),
|
|
97
|
+
error: invocationFailure('WebSocket connection was closed after a binding failure.', 'receive'),
|
|
98
|
+
});
|
|
97
99
|
}
|
|
98
100
|
cancel(exchange, requestId) {
|
|
99
101
|
const key = requestKey(requestId);
|
|
@@ -101,35 +103,42 @@ export class WebSocketConnection {
|
|
|
101
103
|
return;
|
|
102
104
|
this.active.delete(key);
|
|
103
105
|
this.cancelled.add(key);
|
|
106
|
+
let close;
|
|
104
107
|
try {
|
|
105
108
|
const encoded = this.codec.encode(protocol.decodeCancelRequest({ version: protocol.VERSION, kind: 'cancel', requestId }));
|
|
106
109
|
const bytes = frameSize(encoded);
|
|
107
110
|
if (bytes > this.maximumRequestBytes ||
|
|
108
111
|
socketBufferedAmount(this.socket) + bytes > this.maximumBufferedBytes) {
|
|
109
|
-
|
|
112
|
+
const policy = 'WebSocket cancellation exceeds the connection send policy.';
|
|
113
|
+
close = cancelClose(1008, 'Client send policy exceeded', new ProtocolError(policy));
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
this.socket.send(socketData(encoded));
|
|
110
117
|
}
|
|
111
|
-
this.socket.send(socketData(encoded));
|
|
112
118
|
}
|
|
113
119
|
catch (error) {
|
|
114
|
-
|
|
115
|
-
cause: asError(error),
|
|
116
|
-
delivery: 'unknown',
|
|
117
|
-
}), error instanceof WebSocketBindingFailure ? error.closeCode : 1011, error instanceof WebSocketBindingFailure ? error.closeReason : 'Client send failed');
|
|
120
|
+
close = cancelClose(1011, 'Client send failed', error);
|
|
118
121
|
}
|
|
122
|
+
if (close !== undefined)
|
|
123
|
+
this.end(close);
|
|
124
|
+
}
|
|
125
|
+
/** Closes the connection on its owner's behalf, failing what it still carries with `error`. */
|
|
126
|
+
shutdown(error) {
|
|
127
|
+
this.end({ code: 1000, reason: 'Client closed', error });
|
|
119
128
|
}
|
|
120
|
-
|
|
129
|
+
end(close) {
|
|
121
130
|
if (this.closedState)
|
|
122
131
|
return;
|
|
123
132
|
this.closedState = true;
|
|
124
|
-
this.rejectReady(
|
|
133
|
+
this.rejectReady(close.error);
|
|
125
134
|
const active = [...this.active.values()];
|
|
126
135
|
this.active.clear();
|
|
127
136
|
this.cancelled.clear();
|
|
128
137
|
for (const exchange of active)
|
|
129
|
-
exchange.connectionFailed(
|
|
138
|
+
exchange.connectionFailed(close.error);
|
|
130
139
|
try {
|
|
131
140
|
if (this.socket.readyState === WS_CONNECTING || this.socket.readyState === WS_OPEN) {
|
|
132
|
-
this.socket.close(
|
|
141
|
+
this.socket.close(close.code, close.reason);
|
|
133
142
|
}
|
|
134
143
|
}
|
|
135
144
|
catch {
|
|
@@ -142,7 +151,11 @@ export class WebSocketConnection {
|
|
|
142
151
|
return;
|
|
143
152
|
const expected = protocol.websocket.subprotocolFor(this.codec);
|
|
144
153
|
if (this.socket.protocol !== expected) {
|
|
145
|
-
this.
|
|
154
|
+
this.end({
|
|
155
|
+
code: 1002,
|
|
156
|
+
reason: 'Subprotocol mismatch',
|
|
157
|
+
error: invocationFailure(`server selected WebSocket subprotocol ${JSON.stringify(this.socket.protocol)}; expected ${JSON.stringify(expected)}.`, 'connect'),
|
|
158
|
+
});
|
|
146
159
|
return;
|
|
147
160
|
}
|
|
148
161
|
this.opened = true;
|
|
@@ -150,29 +163,42 @@ export class WebSocketConnection {
|
|
|
150
163
|
}
|
|
151
164
|
onMessage(event) {
|
|
152
165
|
this.incoming = this.incoming
|
|
153
|
-
.then(
|
|
154
|
-
|
|
155
|
-
if (
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
166
|
+
.then(() => this.receive(event.data))
|
|
167
|
+
.then((close) => {
|
|
168
|
+
if (close !== undefined)
|
|
169
|
+
this.end(close);
|
|
170
|
+
}, (error) => this.end(protocolClose(error)));
|
|
171
|
+
}
|
|
172
|
+
/** Hands one message to its exchange, or answers the close it breaks the connection with. */
|
|
173
|
+
async receive(data) {
|
|
174
|
+
const frame = await normalizeFrame(data);
|
|
175
|
+
if (frameSize(frame) > this.maximumResponseBytes) {
|
|
176
|
+
return {
|
|
177
|
+
code: 1009,
|
|
178
|
+
reason: 'Message too big',
|
|
179
|
+
error: new ProtocolError(`WebSocket frame exceeds maximumResponseBytes (${this.maximumResponseBytes}).`),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const admitted = decodeFrame(this.codec, frame);
|
|
183
|
+
if (admitted === undefined) {
|
|
184
|
+
return {
|
|
185
|
+
code: 1003,
|
|
186
|
+
reason: 'Unsupported message type',
|
|
187
|
+
error: new ProtocolError('server returned a message type incompatible with the selected subprotocol.'),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
const key = requestKey(admitted.requestId);
|
|
191
|
+
const exchange = this.active.get(key);
|
|
192
|
+
if (exchange !== undefined) {
|
|
193
|
+
exchange.receive(admitted);
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
if (this.cancelled.has(key)) {
|
|
197
|
+
if (isTerminal(admitted))
|
|
198
|
+
this.cancelled.delete(key);
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
throw new ProtocolError('server sent an unsolicited WebSocket frame.');
|
|
176
202
|
}
|
|
177
203
|
onFailure(message) {
|
|
178
204
|
if (this.closedState)
|
|
@@ -205,52 +231,43 @@ function socketData(input) {
|
|
|
205
231
|
const copy = new Uint8Array(input);
|
|
206
232
|
return copy.buffer;
|
|
207
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* The message `input` decoded, or undefined when its type is not the subprotocol's. A decoder
|
|
236
|
+
* failure is thrown as it is: the connection's protocol close names every such failure alike.
|
|
237
|
+
*/
|
|
208
238
|
function decodeFrame(codec, input) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
(!codec.binary && typeof input !== 'string')) {
|
|
215
|
-
throw new WebSocketBindingFailure('server returned a message type incompatible with the selected subprotocol.', 1003, 'Unsupported message type');
|
|
216
|
-
}
|
|
217
|
-
const decoded = codec.decode(input);
|
|
218
|
-
if (isKind(decoded, 'redirect'))
|
|
219
|
-
return protocol.decodeRedirectResponse(decoded);
|
|
220
|
-
if (isKind(decoded, 'stale'))
|
|
221
|
-
return protocol.decodeStale(decoded);
|
|
222
|
-
if (isKind(decoded, 'miss'))
|
|
223
|
-
return protocol.decodeMiss(decoded);
|
|
224
|
-
if (isStreamFrame(decoded))
|
|
225
|
-
return protocol.decodeStreamFrame(decoded);
|
|
226
|
-
return protocol.decodeResponse(decoded);
|
|
227
|
-
}
|
|
228
|
-
catch (error) {
|
|
229
|
-
if (error instanceof WebSocketBindingFailure)
|
|
230
|
-
throw error;
|
|
231
|
-
throw new ProtocolError('server returned an invalid WebSocket frame.', {
|
|
232
|
-
cause: asError(error),
|
|
233
|
-
});
|
|
239
|
+
const binary = typeof input === 'string' ? undefined : protocol.websocket.decodeBinary(input);
|
|
240
|
+
if (binary !== undefined)
|
|
241
|
+
return binary;
|
|
242
|
+
if ((codec.binary && typeof input === 'string') || (!codec.binary && typeof input !== 'string')) {
|
|
243
|
+
return undefined;
|
|
234
244
|
}
|
|
245
|
+
const decoded = codec.decode(input);
|
|
246
|
+
if (isKind(decoded, 'redirect'))
|
|
247
|
+
return protocol.decodeRedirectResponse(decoded);
|
|
248
|
+
if (isKind(decoded, 'stale'))
|
|
249
|
+
return protocol.decodeStale(decoded);
|
|
250
|
+
if (isKind(decoded, 'miss'))
|
|
251
|
+
return protocol.decodeMiss(decoded);
|
|
252
|
+
if (isStreamFrame(decoded))
|
|
253
|
+
return protocol.decodeStreamFrame(decoded);
|
|
254
|
+
return protocol.decodeResponse(decoded);
|
|
235
255
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
super(message);
|
|
241
|
-
this.closeCode = closeCode;
|
|
242
|
-
this.closeReason = closeReason;
|
|
243
|
-
}
|
|
256
|
+
/** The close a failure the connection did not judge itself ends it with: a protocol breach. */
|
|
257
|
+
function protocolClose(input) {
|
|
258
|
+
const message = input instanceof ProtocolError ? input.message : 'server returned an invalid WebSocket frame.';
|
|
259
|
+
return { code: 1002, reason: 'Protocol error', error: new ProtocolError(message) };
|
|
244
260
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
:
|
|
251
|
-
cause: asError(
|
|
252
|
-
|
|
253
|
-
|
|
261
|
+
/** The close a cancellation the connection could not send ends it with. */
|
|
262
|
+
function cancelClose(code, reason, cause) {
|
|
263
|
+
return {
|
|
264
|
+
code,
|
|
265
|
+
reason,
|
|
266
|
+
error: invocationFailure('Failed to send WebSocket cancellation.', 'send', {
|
|
267
|
+
cause: asError(cause),
|
|
268
|
+
delivery: 'unknown',
|
|
269
|
+
}),
|
|
270
|
+
};
|
|
254
271
|
}
|
|
255
272
|
function closeReason(code) {
|
|
256
273
|
if (code === 1008)
|
|
@@ -268,12 +285,6 @@ function isStreamFrame(input) {
|
|
|
268
285
|
: undefined;
|
|
269
286
|
return typeof kind === 'string' && kind.startsWith('stream.');
|
|
270
287
|
}
|
|
271
|
-
function isKind(input, kind) {
|
|
272
|
-
return (typeof input === 'object' &&
|
|
273
|
-
input !== null &&
|
|
274
|
-
!Array.isArray(input) &&
|
|
275
|
-
input.kind === kind);
|
|
276
|
-
}
|
|
277
288
|
function requestKey(requestId) {
|
|
278
289
|
return requestId === null ? 'null:' : `${typeof requestId}:${String(requestId)}`;
|
|
279
290
|
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import type { Value } from '@astrale-os/kernel-dsl/value';
|
|
2
1
|
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
-
import type { Stream } from '../../client/index.js';
|
|
4
2
|
import type { ControlledSignal } from '../index.js';
|
|
5
3
|
import type { TransportResult } from '../index.js';
|
|
4
|
+
/** The close code of an exchange's failure: a protocol or policy breach, or a failed send. */
|
|
5
|
+
export type ExchangeClose = 1002 | 1008 | 1011;
|
|
6
6
|
export interface ExchangeOwner {
|
|
7
|
-
|
|
7
|
+
/** Frees the exchange's slot; `close` ends the connection, which is otherwise reusable. */
|
|
8
|
+
finish(exchange: WebSocketExchange, close?: ExchangeClose): void;
|
|
8
9
|
cancel(exchange: WebSocketExchange, requestId: protocol.RequestId): void;
|
|
9
10
|
}
|
|
10
11
|
export type WebSocketInbound = protocol.BinaryResponse | protocol.RedirectResponse | protocol.Stale | protocol.Miss | protocol.Response | protocol.StreamFrame;
|
|
11
12
|
/** One correlated invocation within a multiplexed WebSocket connection. */
|
|
12
|
-
export declare class WebSocketExchange
|
|
13
|
+
export declare class WebSocketExchange {
|
|
13
14
|
private readonly owner;
|
|
14
15
|
private readonly requestId;
|
|
15
16
|
private readonly control;
|
|
@@ -18,29 +19,21 @@ export declare class WebSocketExchange implements Stream {
|
|
|
18
19
|
private resolveResult;
|
|
19
20
|
private rejectResult;
|
|
20
21
|
private readonly abort;
|
|
21
|
-
private
|
|
22
|
-
private claimed;
|
|
23
|
-
private index;
|
|
24
|
-
private readonly values;
|
|
25
|
-
private readonly waiters;
|
|
26
|
-
private terminal;
|
|
27
|
-
private sent;
|
|
28
|
-
private invocation?;
|
|
22
|
+
private phase;
|
|
29
23
|
constructor(owner: ExchangeOwner, requestId: protocol.RequestId, control: ControlledSignal, maximumBufferedItems: number);
|
|
30
24
|
start(send: (data: string | Uint8Array) => void, encoded: string | Uint8Array): Promise<TransportResult>;
|
|
31
25
|
receive(input: WebSocketInbound): void;
|
|
32
26
|
connectionFailed(error: Error): void;
|
|
33
|
-
|
|
34
|
-
cancel(_reason?: string): Promise<void>;
|
|
27
|
+
private get invocation();
|
|
35
28
|
private receiveResponse;
|
|
36
29
|
private receiveStreamFrame;
|
|
37
|
-
|
|
30
|
+
/** The consumer's cancellation: drops what it has not read and tells the server. */
|
|
31
|
+
private cancel;
|
|
38
32
|
private complete;
|
|
39
33
|
private reject;
|
|
34
|
+
/** Fails the pending result or the stream; a cancelled exchange drops what was not read. */
|
|
40
35
|
private fail;
|
|
41
|
-
private cancelLocally;
|
|
42
36
|
private finish;
|
|
43
|
-
private finishCancelled;
|
|
44
|
-
private flushTerminal;
|
|
45
|
-
private abortFailure;
|
|
46
37
|
}
|
|
38
|
+
/** Whether `input` is a plain object whose `kind` is `kind`. */
|
|
39
|
+
export declare function isKind(input: unknown, kind: string): boolean;
|