@astrale-os/kernel-client 1.0.0-beta.3 → 1.0.0-beta.30
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 +19 -0
- package/dist/errors/refusal.js +63 -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 +64 -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 +145 -7
- package/dist/graph/query.typecheck.d.ts +77 -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 +10 -2
- package/dist/session/content/api.js +13 -4
- package/dist/session/content/index.d.ts +1 -1
- package/dist/session/content/input.d.ts +6 -1
- package/dist/session/content/input.js +12 -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 +4 -3
- 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 +9 -5
- package/dist/session/reference.js +7 -2
- 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 +60 -19
- 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 +7 -5
- 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
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
-
import { ProtocolError
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { decodeProtocol, requireId } from './response.js';
|
|
2
|
+
import { ProtocolError } from '../../errors/index.js';
|
|
3
|
+
import { abortFailure, withInvocation } from '../failure.js';
|
|
4
|
+
import { FrameStream } from '../stream.js';
|
|
5
|
+
import { decodeProtocol, requireId, tooLarge } from './response.js';
|
|
6
|
+
/** The HTTP binding of one value stream: reads its length-prefixed frames as its consumer reads. */
|
|
6
7
|
export class HttpStream {
|
|
7
8
|
frames;
|
|
8
9
|
codec;
|
|
9
10
|
requestId;
|
|
10
11
|
control;
|
|
11
12
|
invocation;
|
|
12
|
-
|
|
13
|
-
terminal = false;
|
|
14
|
-
index = 0;
|
|
13
|
+
released = false;
|
|
15
14
|
interruption;
|
|
16
15
|
constructor(frames, codec, requestId, control) {
|
|
17
16
|
this.frames = frames;
|
|
18
17
|
this.codec = codec;
|
|
19
18
|
this.requestId = requestId;
|
|
20
19
|
this.control = control;
|
|
21
|
-
const abort = () => void this.interrupt(
|
|
20
|
+
const abort = () => void this.interrupt(abortFailure(control, 'stream exceeded its invocation timeout.', 'stream was cancelled.', {
|
|
21
|
+
delivery: 'unknown',
|
|
22
|
+
invocation: this.invocation,
|
|
23
|
+
}));
|
|
22
24
|
if (control.signal.aborted)
|
|
23
25
|
abort();
|
|
24
26
|
else
|
|
@@ -27,66 +29,42 @@ export class HttpStream {
|
|
|
27
29
|
static async open(response, codec, requestId, maximumFrameBytes, control) {
|
|
28
30
|
if (response.body === null)
|
|
29
31
|
throw new ProtocolError('stream has no body.');
|
|
30
|
-
const
|
|
31
|
-
const first = await
|
|
32
|
+
const binding = new HttpStream(new LengthPrefixedFrames(response.body.getReader(), maximumFrameBytes), codec, requestId, control);
|
|
33
|
+
const first = await binding.readFrame();
|
|
32
34
|
if (first === null || first.kind !== 'stream.start') {
|
|
33
|
-
await
|
|
35
|
+
await binding.stop('invalid stream start');
|
|
34
36
|
throw new ProtocolError('stream did not begin with stream.start.');
|
|
35
37
|
}
|
|
36
|
-
|
|
38
|
+
binding.invocation = first.invocation;
|
|
39
|
+
const stream = new FrameStream(first.invocation, {
|
|
40
|
+
read: () => binding.read(stream),
|
|
41
|
+
cancel: (reason) => binding.stop(reason),
|
|
42
|
+
});
|
|
37
43
|
return stream;
|
|
38
44
|
}
|
|
39
|
-
|
|
40
|
-
if (this.claimed)
|
|
41
|
-
throw new TypeError('streams admit exactly one consumer.');
|
|
42
|
-
this.claimed = true;
|
|
43
|
-
return {
|
|
44
|
-
next: () => this.next(),
|
|
45
|
-
return: async () => {
|
|
46
|
-
await this.cancel('stream consumer stopped');
|
|
47
|
-
return { done: true, value: undefined };
|
|
48
|
-
},
|
|
49
|
-
throw: async (error) => {
|
|
50
|
-
await this.cancel('stream consumer failed');
|
|
51
|
-
throw error;
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
async cancel(reason = 'stream cancelled') {
|
|
56
|
-
await this.stop(reason);
|
|
57
|
-
}
|
|
58
|
-
async next() {
|
|
45
|
+
async read(stream) {
|
|
59
46
|
if (this.interruption !== undefined)
|
|
60
47
|
throw this.interruption;
|
|
61
|
-
if (this.
|
|
48
|
+
if (this.released)
|
|
62
49
|
return { done: true, value: undefined };
|
|
63
50
|
try {
|
|
64
51
|
const frame = await this.readFrame();
|
|
65
52
|
if (frame === null) {
|
|
66
53
|
throw new ProtocolError('stream ended without a terminal frame.');
|
|
67
54
|
}
|
|
68
|
-
if (frame.kind === 'stream.
|
|
69
|
-
|
|
70
|
-
throw new ProtocolError(`stream item ${frame.index} does not match expected index ${this.index}.`);
|
|
71
|
-
}
|
|
72
|
-
this.index += 1;
|
|
73
|
-
return { done: false, value: frame.value };
|
|
74
|
-
}
|
|
75
|
-
if (frame.kind === 'stream.error') {
|
|
76
|
-
await this.finish();
|
|
77
|
-
throw new ResponseError(frame.error.code, frame.error.message, this.invocation, frame.error.data);
|
|
78
|
-
}
|
|
79
|
-
if (frame.kind !== 'stream.end') {
|
|
80
|
-
throw new ProtocolError(`Unexpected ${frame.kind} frame inside stream.`);
|
|
81
|
-
}
|
|
82
|
-
if (frame.count !== this.index) {
|
|
83
|
-
throw new ProtocolError(`stream ended with count ${frame.count}; ${this.index} items were received.`);
|
|
55
|
+
if (frame.kind === 'stream.start') {
|
|
56
|
+
throw new ProtocolError('Unexpected stream.start frame inside stream.');
|
|
84
57
|
}
|
|
58
|
+
const step = stream.accept(frame);
|
|
59
|
+
if (step.kind === 'item')
|
|
60
|
+
return { done: false, value: step.value };
|
|
85
61
|
await this.finish();
|
|
62
|
+
if (step.kind === 'error')
|
|
63
|
+
throw step.error;
|
|
86
64
|
return { done: true, value: undefined };
|
|
87
65
|
}
|
|
88
66
|
catch (error) {
|
|
89
|
-
const failure = this.interruption ??
|
|
67
|
+
const failure = this.interruption ?? withInvocation(error, stream.invocation);
|
|
90
68
|
await this.stop(failure);
|
|
91
69
|
throw failure;
|
|
92
70
|
}
|
|
@@ -102,52 +80,27 @@ export class HttpStream {
|
|
|
102
80
|
return frame;
|
|
103
81
|
}
|
|
104
82
|
async finish() {
|
|
105
|
-
if (this.
|
|
83
|
+
if (this.released)
|
|
106
84
|
return;
|
|
107
85
|
await this.frames.requireEnd();
|
|
108
|
-
this.
|
|
86
|
+
this.released = true;
|
|
109
87
|
this.control.close();
|
|
110
88
|
}
|
|
111
89
|
async interrupt(reason) {
|
|
112
|
-
if (this.
|
|
90
|
+
if (this.released || this.interruption !== undefined)
|
|
113
91
|
return;
|
|
114
92
|
this.interruption = reason;
|
|
115
93
|
await this.frames.cancel(reason);
|
|
116
94
|
this.control.close();
|
|
117
95
|
}
|
|
118
96
|
async stop(reason) {
|
|
119
|
-
if (this.
|
|
97
|
+
if (this.released)
|
|
120
98
|
return;
|
|
121
|
-
this.
|
|
99
|
+
this.released = true;
|
|
122
100
|
this.control.cancel(reason);
|
|
123
101
|
await this.frames.cancel(reason);
|
|
124
102
|
this.control.close();
|
|
125
103
|
}
|
|
126
|
-
abortFailure(reason) {
|
|
127
|
-
if (this.control.timedOut) {
|
|
128
|
-
return invocationFailure('stream exceeded its invocation timeout.', 'timeout', {
|
|
129
|
-
cause: asError(reason),
|
|
130
|
-
delivery: 'unknown',
|
|
131
|
-
invocation: this.invocation,
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
return asError(reason ?? new Error('stream was cancelled.'));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
function retainInvocationId(input, invocation) {
|
|
138
|
-
if (input instanceof ProtocolError && input.invocation === undefined) {
|
|
139
|
-
return new ProtocolError(input.message, { cause: input, invocation });
|
|
140
|
-
}
|
|
141
|
-
if (input instanceof TransportError &&
|
|
142
|
-
input.context.kind === 'invocation' &&
|
|
143
|
-
input.context.invocation === undefined) {
|
|
144
|
-
return invocationFailure(input.message, input.phase, {
|
|
145
|
-
cause: input,
|
|
146
|
-
delivery: input.context.delivery,
|
|
147
|
-
invocation,
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
return input;
|
|
151
104
|
}
|
|
152
105
|
class LengthPrefixedFrames {
|
|
153
106
|
reader;
|
|
@@ -165,7 +118,7 @@ class LengthPrefixedFrames {
|
|
|
165
118
|
return null;
|
|
166
119
|
const length = new DataView(header.buffer, header.byteOffset, header.byteLength).getUint32(0);
|
|
167
120
|
if (length > this.maximumFrameBytes) {
|
|
168
|
-
throw
|
|
121
|
+
throw tooLarge('stream frame', this.maximumFrameBytes);
|
|
169
122
|
}
|
|
170
123
|
return (await this.take(length, false));
|
|
171
124
|
}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { RequestOptions, Transport, TransportRequest, TransportResult } from '../index.js';
|
|
2
|
+
import type { BinaryBody } from './binary-request.js';
|
|
2
3
|
import type { HttpTransportOptions } from './configuration.js';
|
|
4
|
+
/**
|
|
5
|
+
* Package-private binary invocation of an admitted body, which may be a one-shot stream;
|
|
6
|
+
* intentionally absent from the public HttpTransport facade, whose callers could not resend a
|
|
7
|
+
* stream a Redirect answers.
|
|
8
|
+
*/
|
|
9
|
+
export declare function dispatchBinaryBody(transport: HttpTransport, input: TransportRequest, body: BinaryBody, options?: RequestOptions): Promise<TransportResult>;
|
|
3
10
|
/** HTTP implementation of the universal invocation protocol. */
|
|
4
11
|
export declare class HttpTransport implements Transport {
|
|
5
12
|
readonly url: string;
|
|
@@ -13,7 +20,10 @@ export declare class HttpTransport implements Transport {
|
|
|
13
20
|
dispatch(input: TransportRequest, options?: RequestOptions): Promise<TransportResult>;
|
|
14
21
|
/** HTTP-only binary invocation; each call may replay a re-readable Blob. */
|
|
15
22
|
dispatchBinary(input: TransportRequest, body: Blob, options?: RequestOptions): Promise<TransportResult>;
|
|
23
|
+
/** A Blob may be replayed; a stream is read once, by this call, and must yield its length. */
|
|
24
|
+
private dispatchBody;
|
|
16
25
|
private dispatchWith;
|
|
26
|
+
private send;
|
|
17
27
|
private fetchResponse;
|
|
18
28
|
private nextRequestId;
|
|
19
29
|
}
|
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
import * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
2
2
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
-
import {
|
|
4
|
-
import { invocationFailure } from '../failure.js';
|
|
5
|
-
import {
|
|
3
|
+
import { ClientError } from '../../errors/index.js';
|
|
4
|
+
import { invocationFailure, requestFailure } from '../failure.js';
|
|
5
|
+
import { controlledSignal, positiveInteger, requireLive } from '../index.js';
|
|
6
6
|
import { admitResponse } from './admission.js';
|
|
7
|
+
import { cancelUnsent, replayableBody, sendStream } from './binary-request.js';
|
|
7
8
|
import { capture } from './configuration.js';
|
|
9
|
+
import { unprocessed } from './unprocessed.js';
|
|
10
|
+
const bodyDispatch = new WeakMap();
|
|
11
|
+
/**
|
|
12
|
+
* Package-private binary invocation of an admitted body, which may be a one-shot stream;
|
|
13
|
+
* intentionally absent from the public HttpTransport facade, whose callers could not resend a
|
|
14
|
+
* stream a Redirect answers.
|
|
15
|
+
*/
|
|
16
|
+
export async function dispatchBinaryBody(transport, input, body, options = {}) {
|
|
17
|
+
const dispatch = bodyDispatch.get(transport);
|
|
18
|
+
if (dispatch === undefined)
|
|
19
|
+
throw new TypeError('Binary body dispatch requires an HttpTransport.');
|
|
20
|
+
return dispatch(input, body, options);
|
|
21
|
+
}
|
|
8
22
|
/** HTTP implementation of the universal invocation protocol. */
|
|
9
23
|
export class HttpTransport {
|
|
10
24
|
url;
|
|
@@ -22,6 +36,7 @@ export class HttpTransport {
|
|
|
22
36
|
this.timeoutMs = configuration.timeoutMs;
|
|
23
37
|
this.maximumRequestBytes = configuration.maximumRequestBytes;
|
|
24
38
|
this.maximumResponseBytes = configuration.maximumResponseBytes;
|
|
39
|
+
bodyDispatch.set(this, (input, body, options) => this.dispatchBody(input, body, options));
|
|
25
40
|
}
|
|
26
41
|
async dispatch(input, options = {}) {
|
|
27
42
|
return this.dispatchWith(input, options);
|
|
@@ -30,12 +45,30 @@ export class HttpTransport {
|
|
|
30
45
|
async dispatchBinary(input, body, options = {}) {
|
|
31
46
|
if (!(body instanceof Blob))
|
|
32
47
|
throw new TypeError('Binary request body must be a Blob.');
|
|
48
|
+
return this.dispatchBody(input, replayableBody(body), options);
|
|
49
|
+
}
|
|
50
|
+
/** A Blob may be replayed; a stream is read once, by this call, and must yield its length. */
|
|
51
|
+
async dispatchBody(input, body, options) {
|
|
33
52
|
if (options.idempotencyKey !== undefined) {
|
|
53
|
+
cancelUnsent(body);
|
|
34
54
|
throw new TypeError('Binary requests cannot carry an idempotency key.');
|
|
35
55
|
}
|
|
36
56
|
return this.dispatchWith(input, options, body);
|
|
37
57
|
}
|
|
38
58
|
async dispatchWith(input, options, binaryBody) {
|
|
59
|
+
// The stream is claimed first: whatever ends this call before reading it through cancels it.
|
|
60
|
+
const sent = binaryBody?.kind === 'once' ? sendStream(binaryBody) : binaryBody;
|
|
61
|
+
try {
|
|
62
|
+
return await this.send(input, options, sent);
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
if (sent?.kind === 'once') {
|
|
66
|
+
sent.release(new ClientError('Binary request settled before its stream ended.'));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async send(input, options, binary) {
|
|
71
|
+
const streamed = binary?.kind === 'once' ? binary : undefined;
|
|
39
72
|
const requestId = this.nextRequestId();
|
|
40
73
|
const timeoutMs = positiveInteger(options.timeoutMs ?? this.timeoutMs, 'timeoutMs');
|
|
41
74
|
const codec = codecs.json;
|
|
@@ -47,22 +80,36 @@ export class HttpTransport {
|
|
|
47
80
|
...invocationInput,
|
|
48
81
|
...(options.idempotencyKey === undefined ? {} : { idempotencyKey: options.idempotencyKey }),
|
|
49
82
|
});
|
|
50
|
-
const encoded =
|
|
83
|
+
const encoded = binary === undefined
|
|
51
84
|
? codec.encode(request)
|
|
52
85
|
: protocol.http.encodeBinaryRequestEnvelope(request);
|
|
53
86
|
const control = controlledSignal(options.signal, timeoutMs);
|
|
87
|
+
// A failed stream aborts the request, so no Fetch sends its truncated body as complete. The
|
|
88
|
+
// request fails with the stream's ClientError whatever the abort reason, so it is the plain
|
|
89
|
+
// AbortError: workerd logs any other abort reason as an uncaught error.
|
|
90
|
+
streamed?.abortOnFailure(() => control.cancel());
|
|
54
91
|
let handedOff = false;
|
|
55
92
|
let phase = 'connect';
|
|
93
|
+
// The status of the response being received, which a failure to read it still reports.
|
|
94
|
+
let status;
|
|
56
95
|
let delivery = 'not-sent';
|
|
57
96
|
try {
|
|
58
97
|
requireLive(control);
|
|
59
|
-
if (
|
|
98
|
+
if (binary === undefined && encodedByteLength(encoded) > this.maximumRequestBytes) {
|
|
60
99
|
throw invocationFailure(`request exceeds maximumRequestBytes (${this.maximumRequestBytes}).`, 'send');
|
|
61
100
|
}
|
|
62
101
|
phase = 'send';
|
|
63
102
|
delivery = 'unknown';
|
|
64
|
-
const response = await this.fetchResponse(codec, encoded, trace, control,
|
|
103
|
+
const response = await this.fetchResponse(codec, encoded, trace, control, binary);
|
|
104
|
+
// A Fetch that ignores the abort may still end a failed stream body as though it were
|
|
105
|
+
// complete: the server then answered bytes the caller never sent, and its answer is dropped.
|
|
106
|
+
const failed = streamed?.failure();
|
|
107
|
+
if (failed !== undefined) {
|
|
108
|
+
response.body?.cancel().catch(() => undefined);
|
|
109
|
+
throw failed;
|
|
110
|
+
}
|
|
65
111
|
phase = 'receive';
|
|
112
|
+
status = response.status;
|
|
66
113
|
requireLive(control);
|
|
67
114
|
const result = await admitResponse(response, requestId, {
|
|
68
115
|
maximumBytes: this.maximumResponseBytes,
|
|
@@ -74,36 +121,62 @@ export class HttpTransport {
|
|
|
74
121
|
return result;
|
|
75
122
|
}
|
|
76
123
|
catch (error) {
|
|
77
|
-
|
|
124
|
+
// The caller's stream failed, or yielded other than its declared length: the ClientError
|
|
125
|
+
// naming it, with the stream's own failure as its cause, is the failure of this request.
|
|
126
|
+
const rejected = streamed?.failure();
|
|
127
|
+
if (rejected !== undefined)
|
|
128
|
+
throw rejected;
|
|
129
|
+
// A request the server refused unprocessed, even when sent again, was never delivered.
|
|
130
|
+
throw requestFailure(error, control, {
|
|
131
|
+
phase,
|
|
132
|
+
delivery: phase === 'send' && unprocessed(error) ? 'not-sent' : delivery,
|
|
133
|
+
timeout: `request timed out after ${timeoutMs}ms.`,
|
|
134
|
+
failure: status === undefined
|
|
135
|
+
? `Failed to communicate with the server at ${this.url}.`
|
|
136
|
+
: `Failed to read the HTTP ${status} response of the server at ${this.url}.`,
|
|
137
|
+
});
|
|
78
138
|
}
|
|
79
139
|
finally {
|
|
80
140
|
if (!handedOff)
|
|
81
141
|
control.close();
|
|
82
142
|
}
|
|
83
143
|
}
|
|
84
|
-
async fetchResponse(codec, encoded, trace, control,
|
|
144
|
+
async fetchResponse(codec, encoded, trace, control, binary) {
|
|
85
145
|
const init = {
|
|
86
146
|
method: 'POST',
|
|
87
147
|
redirect: 'manual',
|
|
88
148
|
credentials: 'omit',
|
|
89
149
|
referrerPolicy: 'no-referrer',
|
|
90
|
-
// Fetch owns Content-Length; the protocol carries
|
|
150
|
+
// Fetch owns Content-Length; the protocol carries the exact length across Fetch runtimes.
|
|
91
151
|
headers: {
|
|
92
152
|
...this.headers,
|
|
93
153
|
...protocol.http.encodeTraceHeaders(trace),
|
|
94
|
-
'content-type':
|
|
154
|
+
'content-type': binary === undefined ? codec.contentType : protocol.http.BINARY_REQUEST_CONTENT_TYPE,
|
|
95
155
|
accept: codec.contentType,
|
|
96
|
-
...(
|
|
156
|
+
...(binary === undefined
|
|
97
157
|
? {}
|
|
98
158
|
: {
|
|
99
159
|
[protocol.http.REQUEST_ENVELOPE_FIELD]: encoded,
|
|
100
|
-
[protocol.http.BINARY_LENGTH_FIELD]: String(
|
|
160
|
+
[protocol.http.BINARY_LENGTH_FIELD]: String(binary.length),
|
|
101
161
|
}),
|
|
102
162
|
},
|
|
103
|
-
body:
|
|
163
|
+
body: binary === undefined ? toBody(encoded) : binary.body,
|
|
164
|
+
// Fetch sends a stream body without buffering it only in half duplex.
|
|
165
|
+
...(binary?.kind === 'once' ? { duplex: 'half' } : {}),
|
|
104
166
|
signal: control.signal,
|
|
105
167
|
};
|
|
106
|
-
|
|
168
|
+
try {
|
|
169
|
+
return await this.fetchImpl(this.url, init);
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
// The server processed none of it: send it once more, on the connection that replaces the
|
|
173
|
+
// one that refused it, under the same timeout and signal (CLIENT-HTTP-UNPROCESSED-RESEND).
|
|
174
|
+
// A stream body was consumed by the refused attempt: it is never sent again.
|
|
175
|
+
if (!unprocessed(error) || control.signal.aborted || binary?.kind === 'once') {
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
return this.fetchImpl(this.url, init);
|
|
179
|
+
}
|
|
107
180
|
}
|
|
108
181
|
nextRequestId() {
|
|
109
182
|
const requestId = this.nextId;
|
|
@@ -120,21 +193,3 @@ function toBody(input) {
|
|
|
120
193
|
function encodedByteLength(input) {
|
|
121
194
|
return typeof input === 'string' ? new TextEncoder().encode(input).byteLength : input.byteLength;
|
|
122
195
|
}
|
|
123
|
-
function transportFailure(input, control, parent, timeoutMs, url, phase, delivery) {
|
|
124
|
-
if (input instanceof ResponseError || input instanceof ProtocolError)
|
|
125
|
-
return input;
|
|
126
|
-
if (control.timedOut) {
|
|
127
|
-
return invocationFailure(`request timed out after ${timeoutMs}ms.`, 'timeout', {
|
|
128
|
-
cause: asError(input),
|
|
129
|
-
delivery,
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
if (parent?.aborted)
|
|
133
|
-
return asError(parent.reason ?? input);
|
|
134
|
-
return input instanceof TransportError
|
|
135
|
-
? input
|
|
136
|
-
: invocationFailure(`Failed to communicate with the server at ${url}.`, phase, {
|
|
137
|
-
cause: asError(input),
|
|
138
|
-
delivery,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The failure undici's Fetch reports for a request that an HTTP/2 GOAWAY with NO_ERROR refused: one
|
|
3
|
+
* whose stream it never opened, or opened above the GOAWAY's last stream identifier. The server
|
|
4
|
+
* processed none of them (RFC 9113, Section 6.8). undici sends such a request again itself only
|
|
5
|
+
* when its body can be replayed, which a Fetch request body, always a stream, cannot.
|
|
6
|
+
*/
|
|
7
|
+
const GOAWAY_REFUSAL = Object.freeze({
|
|
8
|
+
code: 'UND_ERR_INFO',
|
|
9
|
+
message: 'HTTP/2: "GOAWAY" frame received with code 0',
|
|
10
|
+
});
|
|
11
|
+
/** Causes a Fetch failure wraps before the one that names the refusal. */
|
|
12
|
+
const MAXIMUM_CAUSES = 4;
|
|
13
|
+
/**
|
|
14
|
+
* Whether a Fetch failure proves that the server never processed the request. Only the exact
|
|
15
|
+
* refusal above does: any other failure, or a refusal reported in other words, proves nothing.
|
|
16
|
+
*/
|
|
17
|
+
export function unprocessed(failure) {
|
|
18
|
+
let cause = failure;
|
|
19
|
+
for (let depth = 0; depth <= MAXIMUM_CAUSES; depth++) {
|
|
20
|
+
if (typeof cause !== 'object' || cause === null)
|
|
21
|
+
return false;
|
|
22
|
+
const { code, message } = cause;
|
|
23
|
+
if (code === GOAWAY_REFUSAL.code && message === GOAWAY_REFUSAL.message)
|
|
24
|
+
return true;
|
|
25
|
+
cause = cause.cause;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export type { RequestOptions, DeploymentRetired, MissControl, StaleControl, Transport, TransportRequest, TransportResult, } from './transport.js';
|
|
2
|
-
export { asError, controlledSignal, positiveInteger, requireLive, type ControlledSignal, } from './deadline.js';
|
|
2
|
+
export { asError, controlledSignal, pause, positiveInteger, requireLive, type ControlledSignal, } from './deadline.js';
|
package/dist/transport/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { asError, controlledSignal, positiveInteger, requireLive, } from './deadline.js';
|
|
1
|
+
export { asError, controlledSignal, pause, positiveInteger, requireLive, } from './deadline.js';
|
|
@@ -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;
|