@astrale-os/kernel-client 1.0.0-beta.5 → 1.0.0-beta.50
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/authorization.d.ts +10 -5
- package/dist/auth/authorization.js +14 -1
- package/dist/auth/index.d.ts +1 -0
- package/dist/client/admission.js +3 -1
- package/dist/client/client.d.ts +70 -4
- package/dist/client/client.js +209 -32
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.js +2 -2
- package/dist/client/level.d.ts +58 -0
- package/dist/client/level.js +159 -0
- package/dist/client/request.d.ts +7 -2
- package/dist/client/request.js +11 -7
- package/dist/client/result.d.ts +14 -1
- package/dist/client/result.js +19 -8
- 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/inexpressible-error.d.ts +23 -0
- package/dist/errors/inexpressible-error.js +26 -0
- package/dist/errors/query-input.d.ts +47 -0
- package/dist/errors/query-input.js +85 -0
- package/dist/errors/refusal.d.ts +12 -0
- package/dist/errors/refusal.js +44 -6
- 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 +2 -2
- package/dist/{session/execution.d.ts → execution/contracts.d.ts} +22 -34
- 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 +20 -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 +144 -5
- package/dist/graph/query.typecheck.d.ts +81 -0
- package/dist/graph/query.typecheck.js +34 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/schema/contracts.d.ts +54 -79
- package/dist/schema/index.d.ts +2 -2
- package/dist/schema/index.js +1 -1
- package/dist/schema/installation.d.ts +36 -64
- package/dist/schema/installation.js +67 -131
- package/dist/schema/schema.d.ts +10 -16
- package/dist/schema/schema.js +57 -34
- package/dist/session/admission.js +10 -3
- package/dist/session/bound.d.ts +12 -3
- package/dist/session/bound.js +36 -32
- 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 +2 -2
- package/dist/session/context/index.js +2 -2
- package/dist/session/context/operation.d.ts +2 -0
- package/dist/session/context/operation.js +5 -0
- package/dist/session/context/policy.d.ts +2 -0
- package/dist/session/context/policy.js +6 -3
- package/dist/session/credential/credentials.d.ts +2 -1
- package/dist/session/credential/credentials.js +62 -29
- package/dist/session/discovery/bundle.d.ts +13 -4
- package/dist/session/discovery/bundle.js +85 -36
- package/dist/session/discovery/discovery.d.ts +1 -1
- package/dist/session/discovery/discovery.js +8 -11
- package/dist/session/discovery/index.d.ts +1 -0
- package/dist/session/discovery/index.js +5 -1
- package/dist/session/discovery/release.d.ts +2 -0
- package/dist/session/discovery/release.js +34 -31
- package/dist/session/discovery/response.d.ts +4 -0
- package/dist/session/discovery/response.js +10 -6
- package/dist/session/discovery/snapshot.d.ts +5 -1
- package/dist/session/discovery/transport.d.ts +37 -3
- package/dist/session/discovery/transport.js +62 -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 +4 -2
- package/dist/session/options.js +3 -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 -69
- 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 +2 -1
- package/dist/session/routing/request.js +2 -1
- 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 +70 -22
- 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 +61 -112
- 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 +28 -14
- package/dist/transport/auto/decision.js +39 -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 +10 -3
- package/dist/transport/auto/transport.js +41 -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 +8 -35
- 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/configuration.d.ts +7 -0
- package/dist/transport/http/configuration.js +2 -0
- package/dist/transport/http/index.d.ts +1 -1
- package/dist/transport/http/index.js +1 -1
- package/dist/transport/http/response.d.ts +13 -1
- package/dist/transport/http/response.js +22 -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 +14 -1
- package/dist/transport/http/transport.js +110 -35
- package/dist/transport/http/unprocessed.d.ts +5 -0
- package/dist/transport/http/unprocessed.js +28 -0
- package/dist/transport/index.d.ts +2 -1
- package/dist/transport/index.js +2 -1
- package/dist/transport/level.d.ts +42 -0
- package/dist/transport/level.js +58 -0
- package/dist/transport/stream.d.ts +52 -0
- package/dist/transport/stream.js +101 -0
- package/dist/transport/transport.d.ts +33 -0
- package/dist/transport/unary.d.ts +9 -0
- package/dist/transport/unary.js +36 -0
- package/dist/transport/websocket/connection.d.ts +13 -4
- package/dist/transport/websocket/connection.js +123 -96
- package/dist/transport/websocket/exchange.d.ts +15 -19
- package/dist/transport/websocket/exchange.js +87 -183
- package/dist/transport/websocket/transport.d.ts +19 -1
- package/dist/transport/websocket/transport.js +50 -21
- package/package.json +9 -7
- 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,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
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Level } from '@astrale-os/kernel-core/wire';
|
|
1
2
|
import type { invocation as protocol, routes } from '@astrale-os/kernel-protocol';
|
|
2
3
|
import type { Result } from '../client/result.js';
|
|
3
4
|
export interface RequestOptions {
|
|
@@ -24,6 +25,11 @@ export interface TransportRequest {
|
|
|
24
25
|
readonly schema?: protocol.SchemaTarget;
|
|
25
26
|
/** Source-Kernel maintenance session guard; omitted after routing. */
|
|
26
27
|
readonly maintenance?: protocol.Maintenance;
|
|
28
|
+
/**
|
|
29
|
+
* The wire level `call` is written at, which the Transport states to the Host in place of its
|
|
30
|
+
* current one; absent, the current level. It is never a member of the envelope.
|
|
31
|
+
*/
|
|
32
|
+
readonly level?: Level;
|
|
27
33
|
}
|
|
28
34
|
/** Correlated reserved control decoded independently from application results and errors. */
|
|
29
35
|
export interface StaleControl {
|
|
@@ -44,9 +50,36 @@ export interface DeploymentRetired extends routes.DeploymentRetired {
|
|
|
44
50
|
readonly invocation: protocol.InvocationId;
|
|
45
51
|
}
|
|
46
52
|
export type TransportResult = Result | StaleControl | MissControl | DeploymentRetired;
|
|
53
|
+
/**
|
|
54
|
+
* The embedding-environment adapter of a Client. What it tells of wire levels is its whole
|
|
55
|
+
* contract with its Client, and every part of it is exported:
|
|
56
|
+
*
|
|
57
|
+
* - it states to the Host the level a request names (`TransportRequest.level`);
|
|
58
|
+
* - it states the levels of each result it returns and each error a Host answered with beside
|
|
59
|
+
* that object, with `served`; an answer no level is stated for is read as the answer of a Host
|
|
60
|
+
* at level 0, which told none;
|
|
61
|
+
* - when its connection fixes one level, it tells it with `fixed`, and throws `ConnectionMoved`
|
|
62
|
+
* when the connection is at another level than the request it is given is written at.
|
|
63
|
+
*
|
|
64
|
+
* A Transport that wraps another keeps that contract. The levels are kept beside an answer and
|
|
65
|
+
* never as members of it, so one that returns or throws another object than the one it was given, a
|
|
66
|
+
* copy included, restates them: `served(copy, servedOf(answer).written, servedOf(answer).host)`.
|
|
67
|
+
* It lets `ConnectionMoved` through, and passes `fixed` and `timeoutMs` on.
|
|
68
|
+
*/
|
|
47
69
|
export interface Transport {
|
|
48
70
|
/** Primary embedding-environment adapter for a complete invocation boundary. */
|
|
49
71
|
dispatch(input: TransportRequest, options?: RequestOptions): Promise<TransportResult>;
|
|
72
|
+
/**
|
|
73
|
+
* The wire level every request of this Transport's connection is written at, when its connection
|
|
74
|
+
* fixes one for every message; it connects when it has to. A Client asks before it writes a
|
|
75
|
+
* request, and writes it for that level. Absent, or `undefined`: each request states its own.
|
|
76
|
+
*/
|
|
77
|
+
readonly fixed?: (options?: RequestOptions) => Promise<Level | undefined>;
|
|
78
|
+
/**
|
|
79
|
+
* The deadline of one dispatch whose options name none, in milliseconds. A Client that gives a
|
|
80
|
+
* request to the Transport a second time gives it what is left of the first deadline.
|
|
81
|
+
*/
|
|
82
|
+
readonly timeoutMs?: number;
|
|
50
83
|
/** Optional owned-resource shutdown used by Client.close(). */
|
|
51
84
|
readonly close?: () => void;
|
|
52
85
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Level } from '@astrale-os/kernel-core/wire';
|
|
2
|
+
import type { ErrorPayload } from '@astrale-os/kernel-protocol/errors';
|
|
3
|
+
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
4
|
+
import type { TransportResult } from './transport.js';
|
|
5
|
+
import { ResponseError } from '../errors/index.js';
|
|
6
|
+
/** The application error one decoded error response or `stream.error` frame of `invocation` carries. */
|
|
7
|
+
export declare function responseError(error: ErrorPayload, invocation: protocol.InvocationId, level?: Level, host?: Level | undefined): ResponseError;
|
|
8
|
+
/** The Transport result of one decoded unary response that carries no application error. */
|
|
9
|
+
export declare function unaryResult(response: Exclude<protocol.Response, protocol.ErrorResponse>): TransportResult;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ResponseError } from '../errors/index.js';
|
|
2
|
+
import { served } from './level.js';
|
|
3
|
+
/** The application error one decoded error response or `stream.error` frame of `invocation` carries. */
|
|
4
|
+
export function responseError(error, invocation, level, host = level) {
|
|
5
|
+
const answered = new ResponseError(error.code, error.message, invocation, error.data);
|
|
6
|
+
return level === undefined ? answered : served(answered, level, host);
|
|
7
|
+
}
|
|
8
|
+
/** The Transport result of one decoded unary response that carries no application error. */
|
|
9
|
+
export function unaryResult(response) {
|
|
10
|
+
if ('kind' in response) {
|
|
11
|
+
if (response.kind === 'redirect') {
|
|
12
|
+
return Object.freeze({
|
|
13
|
+
kind: 'redirect',
|
|
14
|
+
invocation: response.invocation,
|
|
15
|
+
redirect: response.redirect,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
if (response.kind === 'stale') {
|
|
19
|
+
return Object.freeze({
|
|
20
|
+
kind: 'stale',
|
|
21
|
+
current: response.current,
|
|
22
|
+
invocation: response.invocation,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return Object.freeze({ kind: 'miss', invocation: response.invocation });
|
|
26
|
+
}
|
|
27
|
+
if ('execution' in response) {
|
|
28
|
+
return {
|
|
29
|
+
kind: 'execution',
|
|
30
|
+
execution: response.execution,
|
|
31
|
+
...(response.replayed === undefined ? {} : { replayed: response.replayed }),
|
|
32
|
+
invocation: response.invocation,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return { kind: 'value', value: response.result, invocation: response.invocation };
|
|
36
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
2
|
+
import { type Level } from '@astrale-os/kernel-core/wire';
|
|
2
3
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
4
|
import type { ControlledSignal } from '../index.js';
|
|
4
5
|
import type { TransportResult } from '../index.js';
|
|
5
|
-
import type { ExchangeOwner } from './exchange.js';
|
|
6
|
+
import type { ExchangeClose, ExchangeOwner } from './exchange.js';
|
|
6
7
|
import type { WebSocketConstructor } from './transport.js';
|
|
7
8
|
import { WebSocketExchange } from './exchange.js';
|
|
8
9
|
interface ConnectionOwner {
|
|
@@ -15,25 +16,33 @@ export declare class WebSocketConnection implements ExchangeOwner {
|
|
|
15
16
|
private readonly maximumResponseBytes;
|
|
16
17
|
private readonly maximumBufferedBytes;
|
|
17
18
|
private readonly maximumActiveInvocations;
|
|
19
|
+
private readonly levels;
|
|
18
20
|
private readonly socket;
|
|
19
21
|
private readonly ready;
|
|
20
22
|
private resolveReady;
|
|
21
23
|
private rejectReady;
|
|
22
24
|
private opened;
|
|
23
25
|
private closedState;
|
|
26
|
+
private selected;
|
|
24
27
|
private readonly active;
|
|
25
28
|
private readonly cancelled;
|
|
26
29
|
private incoming;
|
|
27
|
-
constructor(owner: ConnectionOwner, WebSocketConstructor: WebSocketConstructor, url: string, codec: codecs.Codec, maximumRequestBytes: number, maximumResponseBytes: number, maximumBufferedBytes: number, maximumActiveInvocations: number);
|
|
30
|
+
constructor(owner: ConnectionOwner, WebSocketConstructor: WebSocketConstructor, url: string, codec: codecs.Codec, maximumRequestBytes: number, maximumResponseBytes: number, maximumBufferedBytes: number, maximumActiveInvocations: number, levels: protocol.Levels);
|
|
28
31
|
get isOpen(): boolean;
|
|
29
32
|
get isAvailable(): boolean;
|
|
33
|
+
/** The wire level the Host selected for every message of this connection. */
|
|
34
|
+
get level(): Level;
|
|
30
35
|
connect(control: ControlledSignal): Promise<void>;
|
|
31
36
|
dispatch(request: protocol.Request, control: ControlledSignal, maximumBufferedItems: number): Promise<TransportResult>;
|
|
32
|
-
finish(exchange: WebSocketExchange,
|
|
37
|
+
finish(exchange: WebSocketExchange, close?: ExchangeClose): void;
|
|
33
38
|
cancel(exchange: WebSocketExchange, requestId: protocol.RequestId): void;
|
|
34
|
-
|
|
39
|
+
/** Closes the connection on its owner's behalf, failing what it still carries with `error`. */
|
|
40
|
+
shutdown(error: Error): void;
|
|
41
|
+
private end;
|
|
35
42
|
private onOpen;
|
|
36
43
|
private onMessage;
|
|
44
|
+
/** Hands one message to its exchange, or answers the close it breaks the connection with. */
|
|
45
|
+
private receive;
|
|
37
46
|
private onFailure;
|
|
38
47
|
}
|
|
39
48
|
export {};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { Levels } from '@astrale-os/kernel-core/wire';
|
|
1
2
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
3
|
import { ProtocolError } from '../../errors/index.js';
|
|
3
4
|
import { invocationFailure } from '../failure.js';
|
|
4
|
-
import { asError } from '../index.js';
|
|
5
|
-
import { WebSocketExchange } from './exchange.js';
|
|
5
|
+
import { asError, requireLive } from '../index.js';
|
|
6
|
+
import { isKind, WebSocketExchange } from './exchange.js';
|
|
6
7
|
const WS_CONNECTING = 0;
|
|
7
8
|
const WS_OPEN = 1;
|
|
8
9
|
export class WebSocketConnection {
|
|
@@ -12,27 +13,32 @@ export class WebSocketConnection {
|
|
|
12
13
|
maximumResponseBytes;
|
|
13
14
|
maximumBufferedBytes;
|
|
14
15
|
maximumActiveInvocations;
|
|
16
|
+
levels;
|
|
15
17
|
socket;
|
|
16
18
|
ready;
|
|
17
19
|
resolveReady;
|
|
18
20
|
rejectReady;
|
|
19
21
|
opened = false;
|
|
20
22
|
closedState = false;
|
|
23
|
+
selected;
|
|
21
24
|
active = new Map();
|
|
22
25
|
cancelled = new Set();
|
|
23
26
|
incoming = Promise.resolve();
|
|
24
|
-
constructor(owner, WebSocketConstructor, url, codec, maximumRequestBytes, maximumResponseBytes, maximumBufferedBytes, maximumActiveInvocations) {
|
|
27
|
+
constructor(owner, WebSocketConstructor, url, codec, maximumRequestBytes, maximumResponseBytes, maximumBufferedBytes, maximumActiveInvocations, levels) {
|
|
25
28
|
this.owner = owner;
|
|
26
29
|
this.codec = codec;
|
|
27
30
|
this.maximumRequestBytes = maximumRequestBytes;
|
|
28
31
|
this.maximumResponseBytes = maximumResponseBytes;
|
|
29
32
|
this.maximumBufferedBytes = maximumBufferedBytes;
|
|
30
33
|
this.maximumActiveInvocations = maximumActiveInvocations;
|
|
34
|
+
this.levels = levels;
|
|
31
35
|
this.ready = new Promise((resolve, reject) => {
|
|
32
36
|
this.resolveReady = resolve;
|
|
33
37
|
this.rejectReady = reject;
|
|
34
38
|
});
|
|
35
|
-
|
|
39
|
+
// A client at level 0 offers the one plain token, as it always has.
|
|
40
|
+
const offer = protocol.websocket.offerFor(codec, levels);
|
|
41
|
+
this.socket = new WebSocketConstructor(url, offer.length === 1 ? offer[0] : [...offer]);
|
|
36
42
|
this.socket.binaryType = 'arraybuffer';
|
|
37
43
|
this.socket.addEventListener('open', () => this.onOpen(), { once: true });
|
|
38
44
|
this.socket.addEventListener('message', (event) => this.onMessage(event));
|
|
@@ -45,13 +51,17 @@ export class WebSocketConnection {
|
|
|
45
51
|
get isAvailable() {
|
|
46
52
|
return !this.closedState;
|
|
47
53
|
}
|
|
54
|
+
/** The wire level the Host selected for every message of this connection. */
|
|
55
|
+
get level() {
|
|
56
|
+
if (this.selected === undefined)
|
|
57
|
+
throw new TypeError('WebSocket connection is not open.');
|
|
58
|
+
return this.selected;
|
|
59
|
+
}
|
|
48
60
|
async connect(control) {
|
|
49
|
-
|
|
50
|
-
throw asError(control.signal.reason);
|
|
51
|
-
}
|
|
61
|
+
requireLive(control);
|
|
52
62
|
let abort;
|
|
53
63
|
const cancelled = new Promise((_resolve, reject) => {
|
|
54
|
-
abort = () => reject(
|
|
64
|
+
abort = () => reject(control.signal.reason);
|
|
55
65
|
control.signal.addEventListener('abort', abort, { once: true });
|
|
56
66
|
});
|
|
57
67
|
try {
|
|
@@ -89,11 +99,15 @@ export class WebSocketConnection {
|
|
|
89
99
|
this.active.set(key, exchange);
|
|
90
100
|
return exchange.start((data) => this.socket.send(socketData(data)), encoded);
|
|
91
101
|
}
|
|
92
|
-
finish(exchange,
|
|
102
|
+
finish(exchange, close) {
|
|
93
103
|
removeExchange(this.active, exchange);
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
|
|
104
|
+
if (close === undefined)
|
|
105
|
+
return;
|
|
106
|
+
this.end({
|
|
107
|
+
code: close,
|
|
108
|
+
reason: closeReason(close),
|
|
109
|
+
error: invocationFailure('WebSocket connection was closed after a binding failure.', 'receive'),
|
|
110
|
+
});
|
|
97
111
|
}
|
|
98
112
|
cancel(exchange, requestId) {
|
|
99
113
|
const key = requestKey(requestId);
|
|
@@ -101,35 +115,42 @@ export class WebSocketConnection {
|
|
|
101
115
|
return;
|
|
102
116
|
this.active.delete(key);
|
|
103
117
|
this.cancelled.add(key);
|
|
118
|
+
let close;
|
|
104
119
|
try {
|
|
105
120
|
const encoded = this.codec.encode(protocol.decodeCancelRequest({ version: protocol.VERSION, kind: 'cancel', requestId }));
|
|
106
121
|
const bytes = frameSize(encoded);
|
|
107
122
|
if (bytes > this.maximumRequestBytes ||
|
|
108
123
|
socketBufferedAmount(this.socket) + bytes > this.maximumBufferedBytes) {
|
|
109
|
-
|
|
124
|
+
const policy = 'WebSocket cancellation exceeds the connection send policy.';
|
|
125
|
+
close = cancelClose(1008, 'Client send policy exceeded', new ProtocolError(policy));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.socket.send(socketData(encoded));
|
|
110
129
|
}
|
|
111
|
-
this.socket.send(socketData(encoded));
|
|
112
130
|
}
|
|
113
131
|
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');
|
|
132
|
+
close = cancelClose(1011, 'Client send failed', error);
|
|
118
133
|
}
|
|
134
|
+
if (close !== undefined)
|
|
135
|
+
this.end(close);
|
|
119
136
|
}
|
|
120
|
-
|
|
137
|
+
/** Closes the connection on its owner's behalf, failing what it still carries with `error`. */
|
|
138
|
+
shutdown(error) {
|
|
139
|
+
this.end({ code: 1000, reason: 'Client closed', error });
|
|
140
|
+
}
|
|
141
|
+
end(close) {
|
|
121
142
|
if (this.closedState)
|
|
122
143
|
return;
|
|
123
144
|
this.closedState = true;
|
|
124
|
-
this.rejectReady(
|
|
145
|
+
this.rejectReady(close.error);
|
|
125
146
|
const active = [...this.active.values()];
|
|
126
147
|
this.active.clear();
|
|
127
148
|
this.cancelled.clear();
|
|
128
149
|
for (const exchange of active)
|
|
129
|
-
exchange.connectionFailed(
|
|
150
|
+
exchange.connectionFailed(close.error);
|
|
130
151
|
try {
|
|
131
152
|
if (this.socket.readyState === WS_CONNECTING || this.socket.readyState === WS_OPEN) {
|
|
132
|
-
this.socket.close(
|
|
153
|
+
this.socket.close(close.code, close.reason);
|
|
133
154
|
}
|
|
134
155
|
}
|
|
135
156
|
catch {
|
|
@@ -140,39 +161,60 @@ export class WebSocketConnection {
|
|
|
140
161
|
onOpen() {
|
|
141
162
|
if (this.closedState)
|
|
142
163
|
return;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
164
|
+
// The Host selects one of the offered tokens: a level this client reads.
|
|
165
|
+
// A Workers socket that selected none reports `null`.
|
|
166
|
+
const level = protocol.websocket.subprotocolLevel(this.socket.protocol ?? '', this.codec);
|
|
167
|
+
if (level === undefined || !Levels.includes(this.levels, level)) {
|
|
168
|
+
const expected = protocol.websocket.offerFor(this.codec, this.levels);
|
|
169
|
+
this.end({
|
|
170
|
+
code: 1002,
|
|
171
|
+
reason: 'Subprotocol mismatch',
|
|
172
|
+
error: invocationFailure(`server selected WebSocket subprotocol ${JSON.stringify(this.socket.protocol)}; expected ${expected.map((token) => JSON.stringify(token)).join(' or ')}.`, 'connect'),
|
|
173
|
+
});
|
|
146
174
|
return;
|
|
147
175
|
}
|
|
176
|
+
this.selected = level;
|
|
148
177
|
this.opened = true;
|
|
149
178
|
this.resolveReady();
|
|
150
179
|
}
|
|
151
180
|
onMessage(event) {
|
|
152
181
|
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
|
-
|
|
182
|
+
.then(() => this.receive(event.data))
|
|
183
|
+
.then((close) => {
|
|
184
|
+
if (close !== undefined)
|
|
185
|
+
this.end(close);
|
|
186
|
+
}, (error) => this.end(protocolClose(error)));
|
|
187
|
+
}
|
|
188
|
+
/** Hands one message to its exchange, or answers the close it breaks the connection with. */
|
|
189
|
+
async receive(data) {
|
|
190
|
+
const frame = await normalizeFrame(data);
|
|
191
|
+
if (frameSize(frame) > this.maximumResponseBytes) {
|
|
192
|
+
return {
|
|
193
|
+
code: 1009,
|
|
194
|
+
reason: 'Message too big',
|
|
195
|
+
error: new ProtocolError(`WebSocket frame exceeds maximumResponseBytes (${this.maximumResponseBytes}).`),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
const admitted = decodeFrame(this.codec, frame);
|
|
199
|
+
if (admitted === undefined) {
|
|
200
|
+
return {
|
|
201
|
+
code: 1003,
|
|
202
|
+
reason: 'Unsupported message type',
|
|
203
|
+
error: new ProtocolError('server returned a message type incompatible with the selected subprotocol.'),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
const key = requestKey(admitted.requestId);
|
|
207
|
+
const exchange = this.active.get(key);
|
|
208
|
+
if (exchange !== undefined) {
|
|
209
|
+
exchange.receive(admitted);
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
if (this.cancelled.has(key)) {
|
|
213
|
+
if (isTerminal(admitted))
|
|
214
|
+
this.cancelled.delete(key);
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
throw new ProtocolError('server sent an unsolicited WebSocket frame.');
|
|
176
218
|
}
|
|
177
219
|
onFailure(message) {
|
|
178
220
|
if (this.closedState)
|
|
@@ -205,52 +247,43 @@ function socketData(input) {
|
|
|
205
247
|
const copy = new Uint8Array(input);
|
|
206
248
|
return copy.buffer;
|
|
207
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* The message `input` decoded, or undefined when its type is not the subprotocol's. A decoder
|
|
252
|
+
* failure is thrown as it is: the connection's protocol close names every such failure alike.
|
|
253
|
+
*/
|
|
208
254
|
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
|
-
});
|
|
255
|
+
const binary = typeof input === 'string' ? undefined : protocol.websocket.decodeBinary(input);
|
|
256
|
+
if (binary !== undefined)
|
|
257
|
+
return binary;
|
|
258
|
+
if ((codec.binary && typeof input === 'string') || (!codec.binary && typeof input !== 'string')) {
|
|
259
|
+
return undefined;
|
|
234
260
|
}
|
|
261
|
+
const decoded = codec.decode(input);
|
|
262
|
+
if (isKind(decoded, 'redirect'))
|
|
263
|
+
return protocol.decodeRedirectResponse(decoded);
|
|
264
|
+
if (isKind(decoded, 'stale'))
|
|
265
|
+
return protocol.decodeStale(decoded);
|
|
266
|
+
if (isKind(decoded, 'miss'))
|
|
267
|
+
return protocol.decodeMiss(decoded);
|
|
268
|
+
if (isStreamFrame(decoded))
|
|
269
|
+
return protocol.decodeStreamFrame(decoded);
|
|
270
|
+
return protocol.decodeResponse(decoded);
|
|
235
271
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
super(message);
|
|
241
|
-
this.closeCode = closeCode;
|
|
242
|
-
this.closeReason = closeReason;
|
|
243
|
-
}
|
|
272
|
+
/** The close a failure the connection did not judge itself ends it with: a protocol breach. */
|
|
273
|
+
function protocolClose(input) {
|
|
274
|
+
const message = input instanceof ProtocolError ? input.message : 'server returned an invalid WebSocket frame.';
|
|
275
|
+
return { code: 1002, reason: 'Protocol error', error: new ProtocolError(message) };
|
|
244
276
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
:
|
|
251
|
-
cause: asError(
|
|
252
|
-
|
|
253
|
-
|
|
277
|
+
/** The close a cancellation the connection could not send ends it with. */
|
|
278
|
+
function cancelClose(code, reason, cause) {
|
|
279
|
+
return {
|
|
280
|
+
code,
|
|
281
|
+
reason,
|
|
282
|
+
error: invocationFailure('Failed to send WebSocket cancellation.', 'send', {
|
|
283
|
+
cause: asError(cause),
|
|
284
|
+
delivery: 'unknown',
|
|
285
|
+
}),
|
|
286
|
+
};
|
|
254
287
|
}
|
|
255
288
|
function closeReason(code) {
|
|
256
289
|
if (code === 1008)
|
|
@@ -268,12 +301,6 @@ function isStreamFrame(input) {
|
|
|
268
301
|
: undefined;
|
|
269
302
|
return typeof kind === 'string' && kind.startsWith('stream.');
|
|
270
303
|
}
|
|
271
|
-
function isKind(input, kind) {
|
|
272
|
-
return (typeof input === 'object' &&
|
|
273
|
-
input !== null &&
|
|
274
|
-
!Array.isArray(input) &&
|
|
275
|
-
input.kind === kind);
|
|
276
|
-
}
|
|
277
304
|
function requestKey(requestId) {
|
|
278
305
|
return requestId === null ? 'null:' : `${typeof requestId}:${String(requestId)}`;
|
|
279
306
|
}
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Level } from '@astrale-os/kernel-core/wire';
|
|
2
2
|
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
-
import type { Stream } from '../../client/index.js';
|
|
4
3
|
import type { ControlledSignal } from '../index.js';
|
|
5
4
|
import type { TransportResult } from '../index.js';
|
|
5
|
+
/** The close code of an exchange's failure: a protocol or policy breach, or a failed send. */
|
|
6
|
+
export type ExchangeClose = 1002 | 1008 | 1011;
|
|
6
7
|
export interface ExchangeOwner {
|
|
7
|
-
|
|
8
|
+
/** Frees the exchange's slot; `close` ends the connection, which is otherwise reusable. */
|
|
9
|
+
finish(exchange: WebSocketExchange, close?: ExchangeClose): void;
|
|
8
10
|
cancel(exchange: WebSocketExchange, requestId: protocol.RequestId): void;
|
|
11
|
+
/** The wire level the selected subprotocol fixed for the connection. */
|
|
12
|
+
readonly level: Level;
|
|
9
13
|
}
|
|
10
14
|
export type WebSocketInbound = protocol.BinaryResponse | protocol.RedirectResponse | protocol.Stale | protocol.Miss | protocol.Response | protocol.StreamFrame;
|
|
11
15
|
/** One correlated invocation within a multiplexed WebSocket connection. */
|
|
12
|
-
export declare class WebSocketExchange
|
|
16
|
+
export declare class WebSocketExchange {
|
|
13
17
|
private readonly owner;
|
|
14
18
|
private readonly requestId;
|
|
15
19
|
private readonly control;
|
|
@@ -18,29 +22,21 @@ export declare class WebSocketExchange implements Stream {
|
|
|
18
22
|
private resolveResult;
|
|
19
23
|
private rejectResult;
|
|
20
24
|
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?;
|
|
25
|
+
private phase;
|
|
29
26
|
constructor(owner: ExchangeOwner, requestId: protocol.RequestId, control: ControlledSignal, maximumBufferedItems: number);
|
|
30
27
|
start(send: (data: string | Uint8Array) => void, encoded: string | Uint8Array): Promise<TransportResult>;
|
|
31
28
|
receive(input: WebSocketInbound): void;
|
|
32
29
|
connectionFailed(error: Error): void;
|
|
33
|
-
|
|
34
|
-
cancel(_reason?: string): Promise<void>;
|
|
30
|
+
private get invocation();
|
|
35
31
|
private receiveResponse;
|
|
36
32
|
private receiveStreamFrame;
|
|
37
|
-
|
|
33
|
+
/** The consumer's cancellation: drops what it has not read and tells the server. */
|
|
34
|
+
private cancel;
|
|
38
35
|
private complete;
|
|
39
36
|
private reject;
|
|
37
|
+
/** Fails the pending result or the stream; a cancelled exchange drops what was not read. */
|
|
40
38
|
private fail;
|
|
41
|
-
private cancelLocally;
|
|
42
39
|
private finish;
|
|
43
|
-
private finishCancelled;
|
|
44
|
-
private flushTerminal;
|
|
45
|
-
private abortFailure;
|
|
46
40
|
}
|
|
41
|
+
/** Whether `input` is a plain object whose `kind` is `kind`. */
|
|
42
|
+
export declare function isKind(input: unknown, kind: string): boolean;
|