@astrale-os/kernel-client 1.0.0-beta.45 → 1.0.0-beta.46
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/client/client.d.ts +51 -2
- package/dist/client/client.js +158 -13
- package/dist/client/level.d.ts +58 -0
- package/dist/client/level.js +159 -0
- package/dist/client/result.d.ts +4 -0
- package/dist/client/result.js +10 -5
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/errors/inexpressible-error.d.ts +23 -0
- package/dist/errors/inexpressible-error.js +26 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/transport/auto/decision.d.ts +2 -0
- package/dist/transport/auto/decision.js +4 -0
- package/dist/transport/auto/transport.d.ts +10 -1
- package/dist/transport/auto/transport.js +17 -0
- package/dist/transport/http/admission.js +3 -3
- package/dist/transport/http/configuration.d.ts +7 -0
- package/dist/transport/http/configuration.js +2 -0
- package/dist/transport/http/response.d.ts +10 -0
- package/dist/transport/http/response.js +15 -0
- package/dist/transport/http/transport.d.ts +4 -1
- package/dist/transport/http/transport.js +25 -5
- package/dist/transport/index.d.ts +1 -0
- package/dist/transport/index.js +1 -0
- package/dist/transport/level.d.ts +42 -0
- package/dist/transport/level.js +58 -0
- package/dist/transport/transport.d.ts +33 -0
- package/dist/transport/unary.d.ts +2 -1
- package/dist/transport/unary.js +4 -2
- package/dist/transport/websocket/connection.d.ts +6 -1
- package/dist/transport/websocket/connection.js +21 -5
- package/dist/transport/websocket/exchange.d.ts +3 -0
- package/dist/transport/websocket/exchange.js +6 -4
- package/dist/transport/websocket/transport.d.ts +19 -1
- package/dist/transport/websocket/transport.js +48 -11
- package/package.json +5 -5
|
@@ -6,6 +6,10 @@ export class AutoDecision {
|
|
|
6
6
|
get closed() {
|
|
7
7
|
return this.state.kind === 'closed';
|
|
8
8
|
}
|
|
9
|
+
/** The route every request follows, once one is chosen. */
|
|
10
|
+
get route() {
|
|
11
|
+
return this.state.kind === 'chosen' ? this.state.via : undefined;
|
|
12
|
+
}
|
|
9
13
|
/** Claim the probe when undecided; otherwise report the state to follow. */
|
|
10
14
|
begin() {
|
|
11
15
|
if (this.state.kind !== 'undecided')
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Level } from '@astrale-os/kernel-core/wire';
|
|
2
|
+
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
1
3
|
import type { Fetch } from '../http/index.js';
|
|
2
4
|
import type { RequestOptions, Transport, TransportRequest, TransportResult } from '../index.js';
|
|
3
5
|
import type { WebSocketConstructor } from '../websocket/index.js';
|
|
@@ -17,16 +19,23 @@ export interface AutoTransportOptions {
|
|
|
17
19
|
readonly maximumActiveInvocations?: number;
|
|
18
20
|
readonly maximumBufferedStreamItems?: number;
|
|
19
21
|
readonly onWarning?: (warning: ClientWarning) => void;
|
|
22
|
+
/** The wire History both bindings state; absent, the Invocation History of this build. */
|
|
23
|
+
readonly levels?: protocol.Levels;
|
|
20
24
|
}
|
|
21
25
|
/** Sticky WebSocket-first Transport with one delivery-aware HTTP fallback decision. */
|
|
22
26
|
export declare class AutoTransport implements Transport {
|
|
23
27
|
private readonly websocket;
|
|
24
28
|
private readonly http;
|
|
25
29
|
private readonly decision;
|
|
26
|
-
|
|
30
|
+
readonly timeoutMs: number;
|
|
27
31
|
private readonly onWarning;
|
|
28
32
|
constructor(options: AutoTransportOptions);
|
|
29
33
|
dispatch(input: TransportRequest, options?: RequestOptions): Promise<TransportResult>;
|
|
34
|
+
/**
|
|
35
|
+
* The level of the WebSocket connection once WebSocket is the chosen route; none over HTTP, and
|
|
36
|
+
* none before the route is chosen: the first request probes it.
|
|
37
|
+
*/
|
|
38
|
+
fixed(options?: RequestOptions): Promise<Level | undefined>;
|
|
30
39
|
private dispatchWithBudget;
|
|
31
40
|
close(): void;
|
|
32
41
|
private tryWebsocket;
|
|
@@ -34,6 +34,7 @@ export class AutoTransport {
|
|
|
34
34
|
...(options.maximumResponseBytes === undefined
|
|
35
35
|
? {}
|
|
36
36
|
: { maximumResponseBytes: options.maximumResponseBytes }),
|
|
37
|
+
...(options.levels === undefined ? {} : { levels: options.levels }),
|
|
37
38
|
});
|
|
38
39
|
this.websocket = new WebSocketTransport({
|
|
39
40
|
url: options.endpoints.websocket,
|
|
@@ -54,6 +55,7 @@ export class AutoTransport {
|
|
|
54
55
|
...(options.maximumBufferedStreamItems === undefined
|
|
55
56
|
? {}
|
|
56
57
|
: { maximumBufferedStreamItems: options.maximumBufferedStreamItems }),
|
|
58
|
+
...(options.levels === undefined ? {} : { levels: options.levels }),
|
|
57
59
|
});
|
|
58
60
|
}
|
|
59
61
|
async dispatch(input, options) {
|
|
@@ -61,6 +63,21 @@ export class AutoTransport {
|
|
|
61
63
|
const budget = new InvocationBudget(options, this.timeoutMs);
|
|
62
64
|
return this.dispatchWithBudget(input, budget);
|
|
63
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* The level of the WebSocket connection once WebSocket is the chosen route; none over HTTP, and
|
|
68
|
+
* none before the route is chosen: the first request probes it.
|
|
69
|
+
*/
|
|
70
|
+
async fixed(options) {
|
|
71
|
+
if (this.decision.route !== 'websocket')
|
|
72
|
+
return undefined;
|
|
73
|
+
try {
|
|
74
|
+
return await this.websocket.fixed(options);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// The dispatch that follows meets the same failure and reports it.
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
64
81
|
async dispatchWithBudget(input, budget) {
|
|
65
82
|
let ticket = this.decision.begin();
|
|
66
83
|
while (ticket.kind === 'probing') {
|
|
@@ -10,7 +10,7 @@ import { decodeDeploymentRetired } from './retired.js';
|
|
|
10
10
|
import { HttpStream } from './stream.js';
|
|
11
11
|
/** Admit exactly one response representation, preserving reserved-control precedence. */
|
|
12
12
|
export async function admitResponse(response, requestId, context) {
|
|
13
|
-
const { control, maximumBytes } = context;
|
|
13
|
+
const { control, maximumBytes, level, host } = context;
|
|
14
14
|
if (response.status >= 300 && response.status < 400) {
|
|
15
15
|
throw new ProtocolError('server returned a network redirect.');
|
|
16
16
|
}
|
|
@@ -65,10 +65,10 @@ export async function admitResponse(response, requestId, context) {
|
|
|
65
65
|
const decoded = decodeProtocol('response body', () => responseCodec.decode(body));
|
|
66
66
|
const envelope = decodeProtocol('response envelope', () => protocol.decodeResponse(decoded));
|
|
67
67
|
if (envelope.requestId === null && 'error' in envelope)
|
|
68
|
-
throw responseError(envelope.error, envelope.invocation);
|
|
68
|
+
throw responseError(envelope.error, envelope.invocation, level, host);
|
|
69
69
|
requireId(requestId, envelope.requestId);
|
|
70
70
|
if ('error' in envelope)
|
|
71
|
-
throw responseError(envelope.error, envelope.invocation);
|
|
71
|
+
throw responseError(envelope.error, envelope.invocation, level, host);
|
|
72
72
|
requireLive(control);
|
|
73
73
|
return unaryResult(envelope);
|
|
74
74
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
1
2
|
/** Web Fetch capability used by the built-in HTTP transport. */
|
|
2
3
|
export type Fetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
3
4
|
export interface HttpTransportOptions {
|
|
@@ -10,6 +11,11 @@ export interface HttpTransportOptions {
|
|
|
10
11
|
readonly maximumRequestBytes?: number;
|
|
11
12
|
/** Maximum unary, binary, or individual stream-frame body size. */
|
|
12
13
|
readonly maximumResponseBytes?: number;
|
|
14
|
+
/**
|
|
15
|
+
* The wire History whose current level the transport states and whose floor bounds what it
|
|
16
|
+
* reads; absent, the Invocation History of this build.
|
|
17
|
+
*/
|
|
18
|
+
readonly levels?: protocol.Levels;
|
|
13
19
|
}
|
|
14
20
|
/** Captured transport configuration, admitted once before any invocation. */
|
|
15
21
|
export interface Configuration {
|
|
@@ -19,6 +25,7 @@ export interface Configuration {
|
|
|
19
25
|
readonly timeoutMs: number;
|
|
20
26
|
readonly maximumRequestBytes: number;
|
|
21
27
|
readonly maximumResponseBytes: number;
|
|
28
|
+
readonly levels: protocol.Levels;
|
|
22
29
|
}
|
|
23
30
|
/** Admit static options and detach the caller's request headers. */
|
|
24
31
|
export declare function capture(options: HttpTransportOptions): Configuration;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
2
|
import { asError, positiveInteger } from '../index.js';
|
|
3
|
+
import { captureLevels } from '../level.js';
|
|
3
4
|
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
4
5
|
const DEFAULT_MAXIMUM_REQUEST_BYTES = 16 * 1024 * 1024;
|
|
5
6
|
const DEFAULT_MAXIMUM_RESPONSE_BYTES = 16 * 1024 * 1024;
|
|
@@ -15,6 +16,7 @@ export function capture(options) {
|
|
|
15
16
|
timeoutMs: positiveInteger(options.timeoutMs ?? DEFAULT_TIMEOUT_MS, 'timeoutMs'),
|
|
16
17
|
maximumRequestBytes: positiveInteger(options.maximumRequestBytes ?? DEFAULT_MAXIMUM_REQUEST_BYTES, 'maximumRequestBytes'),
|
|
17
18
|
maximumResponseBytes: positiveInteger(options.maximumResponseBytes ?? DEFAULT_MAXIMUM_RESPONSE_BYTES, 'maximumResponseBytes'),
|
|
19
|
+
levels: captureLevels(options.levels),
|
|
18
20
|
};
|
|
19
21
|
}
|
|
20
22
|
function normalizeUrl(input) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Level } from '@astrale-os/kernel-core/wire';
|
|
1
2
|
import * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
2
3
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
4
|
import { ResponseTooLargeError } from '../../errors/index.js';
|
|
@@ -6,7 +7,16 @@ import { type ControlledSignal } from '../index.js';
|
|
|
6
7
|
export interface ResponseContext {
|
|
7
8
|
readonly maximumBytes: number;
|
|
8
9
|
readonly control: ControlledSignal;
|
|
10
|
+
/** The wire level the response is written at. */
|
|
11
|
+
readonly level: Level;
|
|
12
|
+
/** The Host's own wire level. */
|
|
13
|
+
readonly host: Level;
|
|
9
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* The wire level the Host of a response tells: level 0 when it tells none. A Host tells its level
|
|
17
|
+
* only to a request that stated one.
|
|
18
|
+
*/
|
|
19
|
+
export declare function hostLevel(response: Response): Level;
|
|
10
20
|
export declare function codecForResponse(contentType: string | null): codecs.Codec | undefined;
|
|
11
21
|
export declare function readBoundedBody(response: Response, maximumBytes: number, signal: AbortSignal): Promise<Uint8Array>;
|
|
12
22
|
/** The failure of an answer that passed the client's `maximumResponseBytes`. */
|
|
@@ -1,7 +1,22 @@
|
|
|
1
|
+
import { acceptLevel } from '@astrale-os/kernel-core/wire';
|
|
1
2
|
import * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
2
3
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
4
|
import { ProtocolError, ResponseError, ResponseTooLargeError } from '../../errors/index.js';
|
|
4
5
|
import { asError } from '../index.js';
|
|
6
|
+
/**
|
|
7
|
+
* The wire level the Host of a response tells: level 0 when it tells none. A Host tells its level
|
|
8
|
+
* only to a request that stated one.
|
|
9
|
+
*/
|
|
10
|
+
export function hostLevel(response) {
|
|
11
|
+
const field = response.headers.get(protocol.http.LEVEL_FIELD);
|
|
12
|
+
if (field === null)
|
|
13
|
+
return acceptLevel(0);
|
|
14
|
+
const level = protocol.http.decodeLevel(field);
|
|
15
|
+
if (level === undefined) {
|
|
16
|
+
throw new ProtocolError(`server told an invalid wire level ${JSON.stringify(field)}.`);
|
|
17
|
+
}
|
|
18
|
+
return level;
|
|
19
|
+
}
|
|
5
20
|
export function codecForResponse(contentType) {
|
|
6
21
|
return protocol.http.codecForContentType(contentType, [codecs.json]);
|
|
7
22
|
}
|
|
@@ -12,9 +12,12 @@ export declare class HttpTransport implements Transport {
|
|
|
12
12
|
readonly url: string;
|
|
13
13
|
private readonly fetchImpl;
|
|
14
14
|
private readonly headers;
|
|
15
|
-
|
|
15
|
+
readonly timeoutMs: number;
|
|
16
16
|
private readonly maximumRequestBytes;
|
|
17
17
|
private readonly maximumResponseBytes;
|
|
18
|
+
private readonly levels;
|
|
19
|
+
/** What a request written at the current level accepts. */
|
|
20
|
+
private readonly accept;
|
|
18
21
|
private nextId;
|
|
19
22
|
constructor(options: HttpTransportOptions);
|
|
20
23
|
dispatch(input: TransportRequest, options?: RequestOptions): Promise<TransportResult>;
|
|
@@ -3,9 +3,11 @@ import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
|
3
3
|
import { ClientError } from '../../errors/index.js';
|
|
4
4
|
import { invocationFailure, requestFailure } from '../failure.js';
|
|
5
5
|
import { controlledSignal, positiveInteger, requireLive } from '../index.js';
|
|
6
|
+
import { served, writtenLevel } from '../level.js';
|
|
6
7
|
import { admitResponse } from './admission.js';
|
|
7
8
|
import { cancelUnsent, replayableBody, sendStream } from './binary-request.js';
|
|
8
9
|
import { capture } from './configuration.js';
|
|
10
|
+
import { hostLevel } from './response.js';
|
|
9
11
|
import { unprocessed } from './unprocessed.js';
|
|
10
12
|
const bodyDispatch = new WeakMap();
|
|
11
13
|
/**
|
|
@@ -27,9 +29,18 @@ export class HttpTransport {
|
|
|
27
29
|
timeoutMs;
|
|
28
30
|
maximumRequestBytes;
|
|
29
31
|
maximumResponseBytes;
|
|
32
|
+
levels;
|
|
33
|
+
/** What a request written at the current level accepts. */
|
|
34
|
+
accept;
|
|
30
35
|
nextId = 1;
|
|
31
36
|
constructor(options) {
|
|
32
37
|
const configuration = capture(options);
|
|
38
|
+
this.levels = configuration.levels;
|
|
39
|
+
// A build that knows no wire change states no level, as the builds released before it.
|
|
40
|
+
this.accept =
|
|
41
|
+
configuration.levels.current === 0
|
|
42
|
+
? protocol.http.acceptFor(codecs.json)
|
|
43
|
+
: protocol.http.acceptFor(codecs.json, configuration.levels.current);
|
|
33
44
|
this.url = configuration.url;
|
|
34
45
|
this.fetchImpl = configuration.fetch;
|
|
35
46
|
this.headers = configuration.headers;
|
|
@@ -72,7 +83,10 @@ export class HttpTransport {
|
|
|
72
83
|
const requestId = this.nextRequestId();
|
|
73
84
|
const timeoutMs = positiveInteger(options.timeoutMs ?? this.timeoutMs, 'timeoutMs');
|
|
74
85
|
const codec = codecs.json;
|
|
75
|
-
const { trace: traceInput, ...invocationInput } = input;
|
|
86
|
+
const { trace: traceInput, level: writtenInput, ...invocationInput } = input;
|
|
87
|
+
// The request states the level it is written at; the Host answers at that level at most.
|
|
88
|
+
const written = writtenLevel(writtenInput, this.levels);
|
|
89
|
+
const accept = written === this.levels.current ? this.accept : protocol.http.acceptFor(codec, written);
|
|
76
90
|
const trace = traceInput === undefined ? undefined : protocol.admitTrace(traceInput);
|
|
77
91
|
const request = protocol.decodeRequest({
|
|
78
92
|
version: protocol.VERSION,
|
|
@@ -100,7 +114,7 @@ export class HttpTransport {
|
|
|
100
114
|
}
|
|
101
115
|
phase = 'send';
|
|
102
116
|
delivery = 'unknown';
|
|
103
|
-
const response = await this.fetchResponse(codec, encoded, trace, control, binary);
|
|
117
|
+
const response = await this.fetchResponse(codec, encoded, accept, trace, control, binary);
|
|
104
118
|
// A Fetch that ignores the abort may still end a failed stream body as though it were
|
|
105
119
|
// complete: the server then answered bytes the caller never sent, and its answer is dropped.
|
|
106
120
|
const failed = streamed?.failure();
|
|
@@ -111,14 +125,20 @@ export class HttpTransport {
|
|
|
111
125
|
phase = 'receive';
|
|
112
126
|
status = response.status;
|
|
113
127
|
requireLive(control);
|
|
128
|
+
// The Host read the request, and wrote this response, at the level the request stated or
|
|
129
|
+
// at its own, whichever is lower.
|
|
130
|
+
const host = hostLevel(response);
|
|
131
|
+
const level = host < written ? host : written;
|
|
114
132
|
const result = await admitResponse(response, requestId, {
|
|
115
133
|
maximumBytes: this.maximumResponseBytes,
|
|
116
134
|
control,
|
|
135
|
+
level,
|
|
136
|
+
host,
|
|
117
137
|
});
|
|
118
138
|
handedOff =
|
|
119
139
|
result.kind === 'stream' ||
|
|
120
140
|
(result.kind === 'binary' && !(result.value.body instanceof Uint8Array));
|
|
121
|
-
return result;
|
|
141
|
+
return served(result, level, host);
|
|
122
142
|
}
|
|
123
143
|
catch (error) {
|
|
124
144
|
// The caller's stream failed, or yielded other than its declared length: the ClientError
|
|
@@ -141,7 +161,7 @@ export class HttpTransport {
|
|
|
141
161
|
control.close();
|
|
142
162
|
}
|
|
143
163
|
}
|
|
144
|
-
async fetchResponse(codec, encoded, trace, control, binary) {
|
|
164
|
+
async fetchResponse(codec, encoded, accept, trace, control, binary) {
|
|
145
165
|
const init = {
|
|
146
166
|
method: 'POST',
|
|
147
167
|
redirect: 'manual',
|
|
@@ -152,7 +172,7 @@ export class HttpTransport {
|
|
|
152
172
|
...this.headers,
|
|
153
173
|
...protocol.http.encodeTraceHeaders(trace),
|
|
154
174
|
'content-type': binary === undefined ? codec.contentType : protocol.http.BINARY_REQUEST_CONTENT_TYPE,
|
|
155
|
-
accept
|
|
175
|
+
accept,
|
|
156
176
|
...(binary === undefined
|
|
157
177
|
? {}
|
|
158
178
|
: {
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export type { RequestOptions, DeploymentRetired, MissControl, StaleControl, Transport, TransportRequest, TransportResult, } from './transport.js';
|
|
2
|
+
export { ConnectionMoved, served, servedOf, type Served } from './level.js';
|
|
2
3
|
export { asError, controlledSignal, pause, positiveInteger, requireLive, type ControlledSignal, } from './deadline.js';
|
package/dist/transport/index.js
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Levels, type Level } from '@astrale-os/kernel-core/wire';
|
|
2
|
+
/** The wire levels of one answer: the level it is written at and the level of its Host. */
|
|
3
|
+
export interface Served {
|
|
4
|
+
/** The wire level the answer is written at: the one its request stated or the Host's, whichever is lower. */
|
|
5
|
+
readonly written: Level;
|
|
6
|
+
/** The Host's own wire level, as it told it. */
|
|
7
|
+
readonly host: Level;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* `answer`, a Transport result or the error a Host answered with, as written at `written` by a Host
|
|
11
|
+
* at `host`. The levels are kept beside the object and never as members of it: a caller receives
|
|
12
|
+
* the result its call declares, and nothing else. A Transport that states no level for an answer
|
|
13
|
+
* says its Host told none.
|
|
14
|
+
*/
|
|
15
|
+
export declare function served<Answer extends object>(answer: Answer, written: Level, host?: Level): Answer;
|
|
16
|
+
/**
|
|
17
|
+
* The wire levels a Transport stated for `answer`: level 0 for both when it stated none. A Transport
|
|
18
|
+
* that wraps another reads them here to restate them for the object it returns in its place.
|
|
19
|
+
*/
|
|
20
|
+
export declare function servedOf(answer: object): Served;
|
|
21
|
+
/**
|
|
22
|
+
* The connection of a Transport is at another wire level than the request it was given is written
|
|
23
|
+
* at: it was made again, towards a Host at another level, after it told its level. Nothing was
|
|
24
|
+
* sent. A Client writes the request for `level` and gives it once more; no caller sees this.
|
|
25
|
+
*/
|
|
26
|
+
export declare class ConnectionMoved extends Error {
|
|
27
|
+
/** The wire level of the connection now. */
|
|
28
|
+
readonly level: Level;
|
|
29
|
+
constructor(
|
|
30
|
+
/** The wire level of the connection now. */
|
|
31
|
+
level: Level);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The wire level one request is written at: the Transport's current level unless the request names
|
|
35
|
+
* a lower one it reads.
|
|
36
|
+
*/
|
|
37
|
+
export declare function writtenLevel(input: Level | undefined, levels: Levels): Level;
|
|
38
|
+
/**
|
|
39
|
+
* The floor and the current level a Transport states, detached from their History: those of the
|
|
40
|
+
* Invocation History of this build when none is given.
|
|
41
|
+
*/
|
|
42
|
+
export declare function captureLevels(input: Levels | undefined): Levels;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { acceptLevel, Levels } from '@astrale-os/kernel-core/wire';
|
|
2
|
+
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
+
const SILENT = Object.freeze({ written: acceptLevel(0), host: acceptLevel(0) });
|
|
4
|
+
const answers = new WeakMap();
|
|
5
|
+
/**
|
|
6
|
+
* `answer`, a Transport result or the error a Host answered with, as written at `written` by a Host
|
|
7
|
+
* at `host`. The levels are kept beside the object and never as members of it: a caller receives
|
|
8
|
+
* the result its call declares, and nothing else. A Transport that states no level for an answer
|
|
9
|
+
* says its Host told none.
|
|
10
|
+
*/
|
|
11
|
+
export function served(answer, written, host = written) {
|
|
12
|
+
if (written !== 0 || host !== 0) {
|
|
13
|
+
answers.set(answer, Object.freeze({ written: acceptLevel(written), host: acceptLevel(host) }));
|
|
14
|
+
}
|
|
15
|
+
return answer;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The wire levels a Transport stated for `answer`: level 0 for both when it stated none. A Transport
|
|
19
|
+
* that wraps another reads them here to restate them for the object it returns in its place.
|
|
20
|
+
*/
|
|
21
|
+
export function servedOf(answer) {
|
|
22
|
+
return answers.get(answer) ?? SILENT;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The connection of a Transport is at another wire level than the request it was given is written
|
|
26
|
+
* at: it was made again, towards a Host at another level, after it told its level. Nothing was
|
|
27
|
+
* sent. A Client writes the request for `level` and gives it once more; no caller sees this.
|
|
28
|
+
*/
|
|
29
|
+
export class ConnectionMoved extends Error {
|
|
30
|
+
level;
|
|
31
|
+
constructor(
|
|
32
|
+
/** The wire level of the connection now. */
|
|
33
|
+
level) {
|
|
34
|
+
super(`The connection is now at wire level ${level}.`);
|
|
35
|
+
this.level = level;
|
|
36
|
+
this.name = 'ConnectionMoved';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The wire level one request is written at: the Transport's current level unless the request names
|
|
41
|
+
* a lower one it reads.
|
|
42
|
+
*/
|
|
43
|
+
export function writtenLevel(input, levels) {
|
|
44
|
+
if (input === undefined)
|
|
45
|
+
return levels.current;
|
|
46
|
+
const level = acceptLevel(input);
|
|
47
|
+
if (!Levels.includes(levels, level)) {
|
|
48
|
+
throw new TypeError(`A request cannot be written at wire level ${level}: this transport speaks levels ${levels.floor} to ${levels.current}.`);
|
|
49
|
+
}
|
|
50
|
+
return level;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The floor and the current level a Transport states, detached from their History: those of the
|
|
54
|
+
* Invocation History of this build when none is given.
|
|
55
|
+
*/
|
|
56
|
+
export function captureLevels(input) {
|
|
57
|
+
return Levels(input ?? protocol.history);
|
|
58
|
+
}
|
|
@@ -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
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import type { Level } from '@astrale-os/kernel-core/wire';
|
|
1
2
|
import type { ErrorPayload } from '@astrale-os/kernel-protocol/errors';
|
|
2
3
|
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
4
|
import type { TransportResult } from './transport.js';
|
|
4
5
|
import { ResponseError } from '../errors/index.js';
|
|
5
6
|
/** 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
|
+
export declare function responseError(error: ErrorPayload, invocation: protocol.InvocationId, level?: Level, host?: Level | undefined): ResponseError;
|
|
7
8
|
/** The Transport result of one decoded unary response that carries no application error. */
|
|
8
9
|
export declare function unaryResult(response: Exclude<protocol.Response, protocol.ErrorResponse>): TransportResult;
|
package/dist/transport/unary.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ResponseError } from '../errors/index.js';
|
|
2
|
+
import { served } from './level.js';
|
|
2
3
|
/** The application error one decoded error response or `stream.error` frame of `invocation` carries. */
|
|
3
|
-
export function responseError(error, invocation) {
|
|
4
|
-
|
|
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);
|
|
5
7
|
}
|
|
6
8
|
/** The Transport result of one decoded unary response that carries no application error. */
|
|
7
9
|
export function unaryResult(response) {
|
|
@@ -1,4 +1,5 @@
|
|
|
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';
|
|
@@ -15,18 +16,22 @@ 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
37
|
finish(exchange: WebSocketExchange, close?: ExchangeClose): void;
|
|
@@ -1,3 +1,4 @@
|
|
|
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';
|
|
@@ -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,6 +51,12 @@ 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
61
|
requireLive(control);
|
|
50
62
|
let abort;
|
|
@@ -149,15 +161,19 @@ export class WebSocketConnection {
|
|
|
149
161
|
onOpen() {
|
|
150
162
|
if (this.closedState)
|
|
151
163
|
return;
|
|
152
|
-
|
|
153
|
-
|
|
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);
|
|
154
169
|
this.end({
|
|
155
170
|
code: 1002,
|
|
156
171
|
reason: 'Subprotocol mismatch',
|
|
157
|
-
error: invocationFailure(`server selected WebSocket subprotocol ${JSON.stringify(this.socket.protocol)}; expected ${JSON.stringify(
|
|
172
|
+
error: invocationFailure(`server selected WebSocket subprotocol ${JSON.stringify(this.socket.protocol)}; expected ${expected.map((token) => JSON.stringify(token)).join(' or ')}.`, 'connect'),
|
|
158
173
|
});
|
|
159
174
|
return;
|
|
160
175
|
}
|
|
176
|
+
this.selected = level;
|
|
161
177
|
this.opened = true;
|
|
162
178
|
this.resolveReady();
|
|
163
179
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Level } from '@astrale-os/kernel-core/wire';
|
|
1
2
|
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
3
|
import type { ControlledSignal } from '../index.js';
|
|
3
4
|
import type { TransportResult } from '../index.js';
|
|
@@ -7,6 +8,8 @@ export interface ExchangeOwner {
|
|
|
7
8
|
/** Frees the exchange's slot; `close` ends the connection, which is otherwise reusable. */
|
|
8
9
|
finish(exchange: WebSocketExchange, close?: ExchangeClose): void;
|
|
9
10
|
cancel(exchange: WebSocketExchange, requestId: protocol.RequestId): void;
|
|
11
|
+
/** The wire level the selected subprotocol fixed for the connection. */
|
|
12
|
+
readonly level: Level;
|
|
10
13
|
}
|
|
11
14
|
export type WebSocketInbound = protocol.BinaryResponse | protocol.RedirectResponse | protocol.Stale | protocol.Miss | protocol.Response | protocol.StreamFrame;
|
|
12
15
|
/** One correlated invocation within a multiplexed WebSocket connection. */
|
|
@@ -2,6 +2,7 @@ import * as errors from '@astrale-os/kernel-protocol/errors';
|
|
|
2
2
|
import { ProtocolError, ResponseError } from '../../errors/index.js';
|
|
3
3
|
import { abortFailure, invocationFailure, withInvocation } from '../failure.js';
|
|
4
4
|
import { asError } from '../index.js';
|
|
5
|
+
import { served } from '../level.js';
|
|
5
6
|
import { FrameStream } from '../stream.js';
|
|
6
7
|
import { responseError, unaryResult } from '../unary.js';
|
|
7
8
|
/** One correlated invocation within a multiplexed WebSocket connection. */
|
|
@@ -101,13 +102,14 @@ export class WebSocketExchange {
|
|
|
101
102
|
}
|
|
102
103
|
const stream = new FrameStream(input.invocation, { cancel: async () => this.cancel() }, this.maximumBufferedItems);
|
|
103
104
|
this.phase = { kind: 'stream', stream };
|
|
104
|
-
this.resolveResult({ kind: 'stream', stream, invocation: input.invocation });
|
|
105
|
+
this.resolveResult(served({ kind: 'stream', stream, invocation: input.invocation }, this.owner.level));
|
|
105
106
|
return;
|
|
106
107
|
}
|
|
107
108
|
const response = input;
|
|
108
109
|
requireId(this.requestId, response.requestId);
|
|
109
|
-
if ('error' in response)
|
|
110
|
-
this.reject(responseError(response.error, response.invocation));
|
|
110
|
+
if ('error' in response) {
|
|
111
|
+
this.reject(responseError(response.error, response.invocation, this.owner.level));
|
|
112
|
+
}
|
|
111
113
|
else
|
|
112
114
|
this.complete(unaryResult(response));
|
|
113
115
|
}
|
|
@@ -141,7 +143,7 @@ export class WebSocketExchange {
|
|
|
141
143
|
}
|
|
142
144
|
complete(result) {
|
|
143
145
|
this.finish({ kind: 'reuse' });
|
|
144
|
-
this.resolveResult(result);
|
|
146
|
+
this.resolveResult(served(result, this.owner.level));
|
|
145
147
|
}
|
|
146
148
|
reject(error) {
|
|
147
149
|
this.finish({ kind: 'reuse' });
|