@astrale-os/kernel-client 1.0.0-beta.4 → 1.0.0-beta.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/index.d.ts +1 -0
- package/dist/client/admission.js +3 -1
- package/dist/client/client.d.ts +19 -2
- package/dist/client/client.js +62 -30
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.js +2 -2
- package/dist/client/request.d.ts +8 -2
- package/dist/client/request.js +28 -7
- package/dist/client/result.d.ts +10 -1
- package/dist/client/result.js +10 -4
- package/dist/client/routing-control.d.ts +11 -15
- package/dist/client/routing-control.js +21 -29
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/index.js +3 -0
- package/dist/errors/query-input.d.ts +47 -0
- package/dist/errors/query-input.js +85 -0
- package/dist/errors/refusal.d.ts +24 -0
- package/dist/errors/refusal.js +78 -0
- package/dist/errors/response-too-large-error.d.ts +11 -0
- package/dist/errors/response-too-large-error.js +15 -0
- package/dist/errors/stale-route-error.d.ts +1 -1
- package/dist/errors/stale-route-error.js +1 -1
- package/dist/{schema → execution}/admission.d.ts +2 -2
- package/dist/{schema → execution}/admission.js +3 -3
- package/dist/execution/contracts.d.ts +76 -0
- package/dist/execution/contracts.js +1 -0
- package/dist/execution/deadline.d.ts +18 -0
- package/dist/execution/deadline.js +25 -0
- package/dist/execution/execution.d.ts +61 -0
- package/dist/execution/execution.js +132 -0
- package/dist/execution/index.d.ts +4 -0
- package/dist/execution/index.js +3 -0
- package/dist/graph/domain/binding.d.ts +2 -2
- package/dist/graph/domain/binding.js +24 -17
- package/dist/graph/domain/conformance.d.ts +12 -0
- package/dist/graph/domain/conformance.js +18 -0
- package/dist/graph/domain/index.d.ts +1 -0
- package/dist/graph/domain/index.js +1 -1
- package/dist/graph/domain/provenance.d.ts +0 -2
- package/dist/graph/domain/provenance.js +0 -4
- package/dist/graph/graph.d.ts +17 -3
- package/dist/graph/graph.js +4 -1
- package/dist/graph/index.d.ts +3 -3
- package/dist/graph/index.js +1 -1
- package/dist/graph/page-contract.d.ts +66 -1
- package/dist/graph/provenance.d.ts +4 -5
- package/dist/graph/provenance.js +5 -6
- package/dist/graph/query.d.ts +10 -4
- package/dist/graph/query.js +149 -7
- package/dist/graph/query.typecheck.d.ts +79 -0
- package/dist/graph/query.typecheck.js +34 -0
- package/dist/pacer/index.d.ts +1 -1
- package/dist/pacer/index.js +1 -1
- package/dist/pacer/retry/index.d.ts +1 -0
- package/dist/pacer/retry/index.js +1 -0
- package/dist/pacer/retry/refusals.d.ts +7 -0
- package/dist/pacer/retry/refusals.js +10 -0
- package/dist/pacer/retry/retry.d.ts +2 -0
- package/dist/pacer/retry/retry.js +33 -1
- package/dist/schema/contracts.d.ts +77 -9
- package/dist/schema/index.d.ts +3 -2
- package/dist/schema/index.js +1 -1
- package/dist/schema/installation.d.ts +76 -0
- package/dist/schema/installation.js +82 -0
- package/dist/schema/schema.d.ts +22 -13
- package/dist/schema/schema.js +67 -48
- package/dist/session/admission.js +10 -3
- package/dist/session/bound.d.ts +12 -3
- package/dist/session/bound.js +66 -20
- package/dist/session/content/api.d.ts +20 -2
- package/dist/session/content/api.js +17 -7
- package/dist/session/content/files.d.ts +12 -0
- package/dist/session/content/files.js +47 -0
- package/dist/session/content/index.d.ts +1 -1
- package/dist/session/content/input.d.ts +13 -2
- package/dist/session/content/input.js +25 -4
- package/dist/session/content/retry.d.ts +5 -1
- package/dist/session/content/retry.js +21 -31
- package/dist/session/context/index.d.ts +1 -1
- package/dist/session/context/index.js +1 -1
- package/dist/session/context/operation.d.ts +2 -0
- package/dist/session/context/operation.js +5 -0
- package/dist/session/credential/credentials.js +43 -28
- package/dist/session/discovery/bundle.d.ts +9 -4
- package/dist/session/discovery/bundle.js +28 -35
- package/dist/session/discovery/discovery.d.ts +1 -1
- package/dist/session/discovery/discovery.js +8 -11
- package/dist/session/discovery/release.js +18 -28
- package/dist/session/discovery/snapshot.d.ts +5 -1
- package/dist/session/discovery/transport.d.ts +35 -3
- package/dist/session/discovery/transport.js +40 -1
- package/dist/session/durable.d.ts +13 -0
- package/dist/session/durable.js +39 -0
- package/dist/session/exchange/exchange.d.ts +3 -2
- package/dist/session/exchange/exchange.js +10 -7
- package/dist/session/exchange/index.d.ts +1 -1
- package/dist/session/exchange/index.js +1 -1
- package/dist/session/exchange/presentation.js +28 -10
- package/dist/session/exchange/source.d.ts +7 -7
- package/dist/session/exchange/source.js +26 -27
- package/dist/session/index.d.ts +5 -4
- package/dist/session/index.js +0 -1
- package/dist/session/options.d.ts +0 -2
- package/dist/session/options.js +0 -3
- package/dist/session/reference.d.ts +27 -6
- package/dist/session/reference.js +18 -3
- package/dist/session/retry.d.ts +12 -0
- package/dist/session/retry.js +23 -0
- package/dist/session/routing/auth.d.ts +9 -3
- package/dist/session/routing/auth.js +4 -3
- package/dist/session/routing/cache.d.ts +12 -5
- package/dist/session/routing/cache.js +56 -48
- package/dist/session/routing/dispatch.d.ts +26 -3
- package/dist/session/routing/dispatch.js +112 -67
- package/dist/session/routing/envelope.d.ts +3 -3
- package/dist/session/routing/index.d.ts +1 -1
- package/dist/session/routing/legacy/release-route.d.ts +12 -0
- package/dist/session/routing/legacy/release-route.js +6 -0
- package/dist/session/routing/recovery.d.ts +3 -6
- package/dist/session/routing/recovery.js +3 -14
- package/dist/session/routing/request.d.ts +6 -1
- package/dist/session/routing/request.js +13 -2
- package/dist/session/routing/result.d.ts +1 -1
- package/dist/session/routing/result.js +5 -1
- package/dist/session/routing/store.js +4 -5
- package/dist/session/session.d.ts +31 -5
- package/dist/session/session.js +67 -20
- package/dist/session/session.typecheck.js +20 -6
- package/dist/session/views/index.d.ts +2 -2
- package/dist/session/views/index.js +2 -2
- package/dist/session/views/kernel.d.ts +31 -0
- package/dist/session/views/kernel.js +30 -0
- package/dist/session/views/legacy/catalog.d.ts +21 -0
- package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
- package/dist/session/views/legacy/index.d.ts +14 -0
- package/dist/session/views/legacy/index.js +14 -0
- package/dist/session/views/legacy/resolve.d.ts +46 -0
- package/dist/session/views/legacy/resolve.js +83 -0
- package/dist/session/views/legacy/unplaced.d.ts +22 -0
- package/dist/session/views/legacy/unplaced.js +26 -0
- package/dist/session/views/read.d.ts +49 -0
- package/dist/session/views/read.js +148 -0
- package/dist/session/views/view.d.ts +48 -0
- package/dist/session/views/view.js +74 -0
- package/dist/store/memory.d.ts +1 -3
- package/dist/store/memory.js +73 -105
- package/dist/store/shape.d.ts +33 -12
- package/dist/store/shape.js +125 -16
- package/dist/store/store.d.ts +6 -6
- package/dist/store/store.js +1 -1
- package/dist/transport/auto/decision.d.ts +26 -14
- package/dist/transport/auto/decision.js +35 -32
- package/dist/transport/auto/fallback.d.ts +1 -1
- package/dist/transport/auto/fallback.js +5 -10
- package/dist/transport/auto/transport.d.ts +0 -2
- package/dist/transport/auto/transport.js +24 -32
- package/dist/transport/deadline.d.ts +2 -0
- package/dist/transport/deadline.js +18 -0
- package/dist/transport/failure.d.ts +28 -1
- package/dist/transport/failure.js +63 -1
- package/dist/transport/http/admission.js +7 -34
- package/dist/transport/http/binary-request.d.ts +44 -0
- package/dist/transport/http/binary-request.js +110 -0
- package/dist/transport/http/binary.js +3 -3
- package/dist/transport/http/byte-stream.d.ts +2 -1
- package/dist/transport/http/byte-stream.js +12 -8
- package/dist/transport/http/index.d.ts +1 -1
- package/dist/transport/http/index.js +1 -1
- package/dist/transport/http/response.d.ts +3 -1
- package/dist/transport/http/response.js +7 -9
- package/dist/transport/http/stream.d.ts +8 -13
- package/dist/transport/http/stream.js +34 -81
- package/dist/transport/http/transport.d.ts +10 -0
- package/dist/transport/http/transport.js +87 -32
- package/dist/transport/http/unprocessed.d.ts +5 -0
- package/dist/transport/http/unprocessed.js +28 -0
- package/dist/transport/index.d.ts +1 -1
- package/dist/transport/index.js +1 -1
- package/dist/transport/stream.d.ts +52 -0
- package/dist/transport/stream.js +101 -0
- package/dist/transport/transport.d.ts +10 -0
- package/dist/transport/unary.d.ts +8 -0
- package/dist/transport/unary.js +34 -0
- package/dist/transport/websocket/connection.d.ts +7 -3
- package/dist/transport/websocket/connection.js +103 -92
- package/dist/transport/websocket/exchange.d.ts +12 -19
- package/dist/transport/websocket/exchange.js +86 -184
- package/dist/transport/websocket/transport.js +11 -19
- package/package.json +9 -7
- package/dist/session/execution.d.ts +0 -65
- package/dist/session/execution.js +0 -34
- package/dist/session/views/catalog.d.ts +0 -19
- package/dist/session/views/resolve.d.ts +0 -13
- package/dist/session/views/resolve.js +0 -21
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ProtocolError } from '../../errors/index.js';
|
|
2
|
-
import {
|
|
2
|
+
import { abortFailure } from '../failure.js';
|
|
3
|
+
import { tooLarge } from './response.js';
|
|
3
4
|
/** Single-consumer HTTP byte stream preserving invocation lifetime and backpressure. */
|
|
4
5
|
export class HttpByteStream {
|
|
5
6
|
maximumBytes;
|
|
@@ -10,12 +11,12 @@ export class HttpByteStream {
|
|
|
10
11
|
terminal = false;
|
|
11
12
|
bytes = 0;
|
|
12
13
|
interruption;
|
|
13
|
-
constructor(body, maximumBytes, control, expectedBytes) {
|
|
14
|
+
constructor(body, maximumBytes, control, invocation, expectedBytes) {
|
|
14
15
|
this.maximumBytes = maximumBytes;
|
|
15
16
|
this.control = control;
|
|
16
17
|
this.expectedBytes = expectedBytes;
|
|
17
18
|
this.reader = body.getReader();
|
|
18
|
-
const abort = () => void this.interrupt(control.
|
|
19
|
+
const abort = () => void this.interrupt(abortFailure(control, 'streaming binary exceeded its invocation timeout.', 'streaming binary was cancelled.', { delivery: 'unknown', invocation }));
|
|
19
20
|
if (control.signal.aborted)
|
|
20
21
|
abort();
|
|
21
22
|
else
|
|
@@ -67,19 +68,22 @@ export class HttpByteStream {
|
|
|
67
68
|
throw new ProtocolError(`streaming binary exceeded its declared length (${this.expectedBytes}).`);
|
|
68
69
|
}
|
|
69
70
|
if (this.bytes > this.maximumBytes) {
|
|
70
|
-
throw
|
|
71
|
+
throw tooLarge('streaming binary', this.maximumBytes);
|
|
71
72
|
}
|
|
72
73
|
return { done: false, value: new Uint8Array(next.value) };
|
|
73
74
|
}
|
|
74
75
|
catch (error) {
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
// A Fetch body errors a pending read with its signal's raw reason; the interruption is the
|
|
77
|
+
// public failure that abort maps to.
|
|
78
|
+
const failure = this.interruption ?? error;
|
|
79
|
+
await this.cancel(failure);
|
|
80
|
+
throw failure;
|
|
77
81
|
}
|
|
78
82
|
}
|
|
79
|
-
async interrupt(
|
|
83
|
+
async interrupt(failure) {
|
|
80
84
|
if (this.terminal || this.interruption !== undefined)
|
|
81
85
|
return;
|
|
82
|
-
this.interruption =
|
|
86
|
+
this.interruption = failure;
|
|
83
87
|
await this.cancelReader(this.interruption);
|
|
84
88
|
this.control.close();
|
|
85
89
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { HttpTransport } from './transport.js';
|
|
1
|
+
export { dispatchBinaryBody, HttpTransport } from './transport.js';
|
|
2
2
|
export type { Fetch, HttpTransportOptions } from './configuration.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { HttpTransport } from './transport.js';
|
|
1
|
+
export { dispatchBinaryBody, HttpTransport } from './transport.js';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
2
2
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
+
import { ResponseTooLargeError } from '../../errors/index.js';
|
|
3
4
|
import { type ControlledSignal } from '../index.js';
|
|
4
5
|
/** The body budget and cancellation lifetime shared by all response representations. */
|
|
5
6
|
export interface ResponseContext {
|
|
@@ -8,8 +9,9 @@ export interface ResponseContext {
|
|
|
8
9
|
}
|
|
9
10
|
export declare function codecForResponse(contentType: string | null): codecs.Codec | undefined;
|
|
10
11
|
export declare function readBoundedBody(response: Response, maximumBytes: number, signal: AbortSignal): Promise<Uint8Array>;
|
|
12
|
+
/** The failure of an answer that passed the client's `maximumResponseBytes`. */
|
|
13
|
+
export declare function tooLarge(what: string, maximumBytes: number): ResponseTooLargeError;
|
|
11
14
|
export declare function requireId(expected: protocol.RequestId, received: protocol.RequestId): void;
|
|
12
15
|
export declare function decodeProtocol<T>(label: string, decode: () => T): T;
|
|
13
|
-
export declare function normalizeMediaType(input: string | null): string | undefined;
|
|
14
16
|
export declare function binaryHeaders(input: Headers, names: readonly string[]): Readonly<Record<string, string>>;
|
|
15
17
|
export declare function requireNoStore(response: Response): void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
2
2
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
-
import { ProtocolError, ResponseError } from '../../errors/index.js';
|
|
3
|
+
import { ProtocolError, ResponseError, ResponseTooLargeError } from '../../errors/index.js';
|
|
4
4
|
import { asError } from '../index.js';
|
|
5
5
|
export function codecForResponse(contentType) {
|
|
6
6
|
return protocol.http.codecForContentType(contentType, [codecs.json]);
|
|
@@ -8,7 +8,7 @@ export function codecForResponse(contentType) {
|
|
|
8
8
|
export async function readBoundedBody(response, maximumBytes, signal) {
|
|
9
9
|
const declared = response.headers.get('content-length');
|
|
10
10
|
if (declared !== null && Number(declared) > maximumBytes) {
|
|
11
|
-
throw
|
|
11
|
+
throw tooLarge('response', maximumBytes);
|
|
12
12
|
}
|
|
13
13
|
if (response.body === null)
|
|
14
14
|
return new Uint8Array();
|
|
@@ -24,7 +24,7 @@ export async function readBoundedBody(response, maximumBytes, signal) {
|
|
|
24
24
|
break;
|
|
25
25
|
total += next.value.byteLength;
|
|
26
26
|
if (total > maximumBytes) {
|
|
27
|
-
throw
|
|
27
|
+
throw tooLarge('response', maximumBytes);
|
|
28
28
|
}
|
|
29
29
|
chunks.push(next.value);
|
|
30
30
|
}
|
|
@@ -41,6 +41,10 @@ export async function readBoundedBody(response, maximumBytes, signal) {
|
|
|
41
41
|
}
|
|
42
42
|
return output;
|
|
43
43
|
}
|
|
44
|
+
/** The failure of an answer that passed the client's `maximumResponseBytes`. */
|
|
45
|
+
export function tooLarge(what, maximumBytes) {
|
|
46
|
+
return new ResponseTooLargeError(`${what} exceeds maximumResponseBytes (${maximumBytes}).`, maximumBytes);
|
|
47
|
+
}
|
|
44
48
|
export function requireId(expected, received) {
|
|
45
49
|
if (received !== expected) {
|
|
46
50
|
throw new ProtocolError(`response id ${JSON.stringify(received)} does not match request id ${JSON.stringify(expected)}.`);
|
|
@@ -56,12 +60,6 @@ export function decodeProtocol(label, decode) {
|
|
|
56
60
|
throw new ProtocolError(`server returned an invalid ${label}.`, { cause: asError(error) });
|
|
57
61
|
}
|
|
58
62
|
}
|
|
59
|
-
export function normalizeMediaType(input) {
|
|
60
|
-
if (input === null)
|
|
61
|
-
return undefined;
|
|
62
|
-
const value = input.split(';', 1)[0]?.trim().toLowerCase();
|
|
63
|
-
return value === '' ? undefined : value;
|
|
64
|
-
}
|
|
65
63
|
export function binaryHeaders(input, names) {
|
|
66
64
|
const output = {};
|
|
67
65
|
for (const name of names) {
|
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
import type { Value } from '@astrale-os/kernel-dsl/value';
|
|
2
1
|
import type * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
3
2
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
4
|
-
import type {
|
|
5
|
-
import {
|
|
6
|
-
|
|
3
|
+
import type { ControlledSignal } from '../index.js';
|
|
4
|
+
import { FrameStream } from '../stream.js';
|
|
5
|
+
/** The HTTP binding of one value stream: reads its length-prefixed frames as its consumer reads. */
|
|
6
|
+
export declare class HttpStream {
|
|
7
7
|
private readonly frames;
|
|
8
8
|
private readonly codec;
|
|
9
9
|
private readonly requestId;
|
|
10
10
|
private readonly control;
|
|
11
|
-
invocation
|
|
12
|
-
private
|
|
13
|
-
private terminal;
|
|
14
|
-
private index;
|
|
11
|
+
private invocation;
|
|
12
|
+
private released;
|
|
15
13
|
private interruption;
|
|
16
14
|
private constructor();
|
|
17
|
-
static open(response: Response, codec: codecs.Codec, requestId: protocol.RequestId, maximumFrameBytes: number, control: ControlledSignal): Promise<
|
|
18
|
-
|
|
19
|
-
cancel(reason?: string): Promise<void>;
|
|
20
|
-
private next;
|
|
15
|
+
static open(response: Response, codec: codecs.Codec, requestId: protocol.RequestId, maximumFrameBytes: number, control: ControlledSignal): Promise<FrameStream>;
|
|
16
|
+
private read;
|
|
21
17
|
private readFrame;
|
|
22
18
|
private finish;
|
|
23
19
|
private interrupt;
|
|
24
20
|
private stop;
|
|
25
|
-
private abortFailure;
|
|
26
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';
|