@astrale-os/kernel-client 1.0.0-beta.21 → 1.0.0-beta.23
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 +6 -2
- package/dist/client/client.js +32 -21
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client/request.d.ts +7 -2
- package/dist/client/request.js +11 -7
- package/dist/graph/domain/binding.d.ts +2 -2
- package/dist/graph/graph.d.ts +17 -3
- package/dist/graph/graph.js +4 -1
- package/dist/graph/index.d.ts +2 -2
- package/dist/graph/page-contract.d.ts +53 -1
- package/dist/graph/query.d.ts +10 -4
- package/dist/graph/query.js +125 -2
- package/dist/graph/query.typecheck.d.ts +77 -0
- package/dist/graph/query.typecheck.js +34 -0
- package/dist/session/bound.js +25 -31
- 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 -16
- package/dist/session/durable.js +13 -19
- package/dist/session/index.d.ts +1 -1
- package/dist/session/retry.d.ts +12 -0
- package/dist/session/retry.js +23 -0
- package/dist/session/routing/dispatch.d.ts +7 -3
- package/dist/session/routing/dispatch.js +23 -13
- package/dist/session/routing/request.d.ts +2 -1
- package/dist/session/routing/request.js +2 -1
- package/dist/session/session.typecheck.js +15 -2
- package/dist/store/store.d.ts +6 -6
- package/dist/transport/http/admission.js +7 -42
- package/dist/transport/http/binary-request.d.ts +44 -0
- package/dist/transport/http/binary-request.js +110 -0
- package/dist/transport/http/index.d.ts +1 -1
- package/dist/transport/http/index.js +1 -1
- package/dist/transport/http/response.d.ts +0 -1
- package/dist/transport/http/response.js +0 -6
- package/dist/transport/http/transport.d.ts +10 -0
- package/dist/transport/http/transport.js +71 -17
- package/dist/transport/unary.d.ts +7 -0
- package/dist/transport/unary.js +34 -0
- package/dist/transport/websocket/connection.js +1 -7
- package/dist/transport/websocket/exchange.d.ts +2 -0
- package/dist/transport/websocket/exchange.js +13 -43
- package/package.json +5 -5
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ClientError } from '../../errors/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* One admitted binary request body, whose kind says whether it can be sent again: a Blob is read
|
|
4
|
+
* anew by every send, a stream of exactly `length` bytes is consumed by the one send that reads it.
|
|
5
|
+
*/
|
|
6
|
+
export type BinaryBody = {
|
|
7
|
+
readonly kind: 'replayable';
|
|
8
|
+
readonly body: Blob;
|
|
9
|
+
readonly length: number;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: 'once';
|
|
12
|
+
readonly body: ReadableStream<Uint8Array>;
|
|
13
|
+
readonly length: number;
|
|
14
|
+
};
|
|
15
|
+
/** One send of a stream body: what Fetch reads, and the caller's stream failure it met. */
|
|
16
|
+
export interface StreamSend {
|
|
17
|
+
readonly kind: 'once';
|
|
18
|
+
readonly length: number;
|
|
19
|
+
/** Counted copy of the caller's stream, ending exactly after its declared length. */
|
|
20
|
+
readonly body: ReadableStream<Uint8Array>;
|
|
21
|
+
/** The failure of the caller's stream, or of its length, when one ended the send. */
|
|
22
|
+
readonly failure: () => ClientError | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Abort the request when the stream fails, before its copy errors: a Fetch may otherwise end the
|
|
25
|
+
* failed body as though it were complete (workerd does), and the server would answer it.
|
|
26
|
+
*/
|
|
27
|
+
readonly abortOnFailure: (abort: () => void) => void;
|
|
28
|
+
/** Cancel the caller's stream unless it was read through its end. */
|
|
29
|
+
readonly release: (reason: unknown) => void;
|
|
30
|
+
}
|
|
31
|
+
/** A Blob body, which every send reads again. */
|
|
32
|
+
export declare function replayableBody(blob: Blob): BinaryBody;
|
|
33
|
+
/**
|
|
34
|
+
* Admit one binary body. A stream is snapshotted with its declared length and must be unread: the
|
|
35
|
+
* request that sends it consumes it, and nothing can send it again. A stream is recognized by its
|
|
36
|
+
* reader, so one from another realm or a polyfill is accepted too.
|
|
37
|
+
*/
|
|
38
|
+
export declare function acceptBinaryBody(input: unknown): BinaryBody;
|
|
39
|
+
/** Cancel a stream body that no request has started to read; a Blob needs nothing. */
|
|
40
|
+
export declare function cancelUnsent(body: BinaryBody | undefined): void;
|
|
41
|
+
/** Send a stream once: count its bytes and fail it when they differ from its declared length. */
|
|
42
|
+
export declare function sendStream(source: Extract<BinaryBody, {
|
|
43
|
+
kind: 'once';
|
|
44
|
+
}>): StreamSend;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ClientError } from '../../errors/index.js';
|
|
2
|
+
/** A Blob body, which every send reads again. */
|
|
3
|
+
export function replayableBody(blob) {
|
|
4
|
+
return Object.freeze({ kind: 'replayable', body: blob, length: blob.size });
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Admit one binary body. A stream is snapshotted with its declared length and must be unread: the
|
|
8
|
+
* request that sends it consumes it, and nothing can send it again. A stream is recognized by its
|
|
9
|
+
* reader, so one from another realm or a polyfill is accepted too.
|
|
10
|
+
*/
|
|
11
|
+
export function acceptBinaryBody(input) {
|
|
12
|
+
if (typeof Blob !== 'undefined' && input instanceof Blob)
|
|
13
|
+
return replayableBody(input);
|
|
14
|
+
if (input !== null && typeof input === 'object' && !Array.isArray(input)) {
|
|
15
|
+
const { body, length } = input;
|
|
16
|
+
if (isReadableStream(body)) {
|
|
17
|
+
if (typeof length !== 'number' || !Number.isSafeInteger(length) || length < 0) {
|
|
18
|
+
throw new TypeError('Binary stream length must be a non-negative safe integer.');
|
|
19
|
+
}
|
|
20
|
+
if (body.locked)
|
|
21
|
+
throw new TypeError('Binary stream is already being read.');
|
|
22
|
+
return Object.freeze({ kind: 'once', body, length });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
throw new TypeError('Binary input must be a Blob, File, or byte stream with its exact length.');
|
|
26
|
+
}
|
|
27
|
+
function isReadableStream(input) {
|
|
28
|
+
return (input !== null &&
|
|
29
|
+
typeof input === 'object' &&
|
|
30
|
+
typeof input.getReader === 'function');
|
|
31
|
+
}
|
|
32
|
+
/** Cancel a stream body that no request has started to read; a Blob needs nothing. */
|
|
33
|
+
export function cancelUnsent(body) {
|
|
34
|
+
if (body?.kind !== 'once' || body.body.locked)
|
|
35
|
+
return;
|
|
36
|
+
body.body
|
|
37
|
+
.cancel(new ClientError('Binary request ended before its stream was sent.'))
|
|
38
|
+
.catch(() => undefined);
|
|
39
|
+
}
|
|
40
|
+
/** Send a stream once: count its bytes and fail it when they differ from its declared length. */
|
|
41
|
+
export function sendStream(source) {
|
|
42
|
+
const reader = source.body.getReader();
|
|
43
|
+
let sent = 0;
|
|
44
|
+
let finished = false;
|
|
45
|
+
let failure;
|
|
46
|
+
let abort;
|
|
47
|
+
const release = (reason) => {
|
|
48
|
+
if (finished)
|
|
49
|
+
return;
|
|
50
|
+
finished = true;
|
|
51
|
+
reader.cancel(reason).then(() => reader.releaseLock(), () => reader.releaseLock());
|
|
52
|
+
};
|
|
53
|
+
const stop = (controller, cause) => {
|
|
54
|
+
failure ??= cause;
|
|
55
|
+
release(cause);
|
|
56
|
+
abort?.();
|
|
57
|
+
controller.error(cause);
|
|
58
|
+
};
|
|
59
|
+
const body = new ReadableStream({
|
|
60
|
+
async pull(controller) {
|
|
61
|
+
let next;
|
|
62
|
+
try {
|
|
63
|
+
next = await reader.read();
|
|
64
|
+
}
|
|
65
|
+
catch (cause) {
|
|
66
|
+
if (!finished) {
|
|
67
|
+
stop(controller, new ClientError(`Binary request stream failed after ${sent} of its ${source.length} declared bytes.`, { cause }));
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
// Fetch cancelled the send while this read was pending.
|
|
72
|
+
if (finished)
|
|
73
|
+
return;
|
|
74
|
+
if (next.done) {
|
|
75
|
+
if (sent !== source.length) {
|
|
76
|
+
stop(controller, new ClientError(`Binary request stream ended after ${sent} bytes; expected ${source.length}.`));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
finished = true;
|
|
80
|
+
reader.releaseLock();
|
|
81
|
+
controller.close();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const chunk = next.value;
|
|
85
|
+
if (!(chunk instanceof Uint8Array)) {
|
|
86
|
+
stop(controller, new ClientError('Binary request stream yielded a non-byte chunk.'));
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
sent += chunk.byteLength;
|
|
90
|
+
if (sent > source.length) {
|
|
91
|
+
stop(controller, new ClientError(`Binary request stream exceeded its declared length of ${source.length} bytes.`));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
controller.enqueue(chunk);
|
|
95
|
+
},
|
|
96
|
+
cancel: release,
|
|
97
|
+
}, { highWaterMark: 0 });
|
|
98
|
+
return Object.freeze({
|
|
99
|
+
kind: 'once',
|
|
100
|
+
length: source.length,
|
|
101
|
+
body,
|
|
102
|
+
failure: () => failure,
|
|
103
|
+
abortOnFailure: (next) => {
|
|
104
|
+
abort = next;
|
|
105
|
+
if (failure !== undefined)
|
|
106
|
+
next();
|
|
107
|
+
},
|
|
108
|
+
release,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
@@ -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';
|
|
@@ -10,6 +10,5 @@ export declare function codecForResponse(contentType: string | null): codecs.Cod
|
|
|
10
10
|
export declare function readBoundedBody(response: Response, maximumBytes: number, signal: AbortSignal): Promise<Uint8Array>;
|
|
11
11
|
export declare function requireId(expected: protocol.RequestId, received: protocol.RequestId): void;
|
|
12
12
|
export declare function decodeProtocol<T>(label: string, decode: () => T): T;
|
|
13
|
-
export declare function normalizeMediaType(input: string | null): string | undefined;
|
|
14
13
|
export declare function binaryHeaders(input: Headers, names: readonly string[]): Readonly<Record<string, string>>;
|
|
15
14
|
export declare function requireNoStore(response: Response): void;
|
|
@@ -56,12 +56,6 @@ export function decodeProtocol(label, decode) {
|
|
|
56
56
|
throw new ProtocolError(`server returned an invalid ${label}.`, { cause: asError(error) });
|
|
57
57
|
}
|
|
58
58
|
}
|
|
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
59
|
export function binaryHeaders(input, names) {
|
|
66
60
|
const output = {};
|
|
67
61
|
for (const name of names) {
|
|
@@ -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,11 +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 { ProtocolError, ResponseError, TransportError } from '../../errors/index.js';
|
|
3
|
+
import { ClientError, ProtocolError, ResponseError, TransportError } from '../../errors/index.js';
|
|
4
4
|
import { invocationFailure } from '../failure.js';
|
|
5
5
|
import { asError, 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';
|
|
8
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
|
+
}
|
|
9
22
|
/** HTTP implementation of the universal invocation protocol. */
|
|
10
23
|
export class HttpTransport {
|
|
11
24
|
url;
|
|
@@ -23,6 +36,7 @@ export class HttpTransport {
|
|
|
23
36
|
this.timeoutMs = configuration.timeoutMs;
|
|
24
37
|
this.maximumRequestBytes = configuration.maximumRequestBytes;
|
|
25
38
|
this.maximumResponseBytes = configuration.maximumResponseBytes;
|
|
39
|
+
bodyDispatch.set(this, (input, body, options) => this.dispatchBody(input, body, options));
|
|
26
40
|
}
|
|
27
41
|
async dispatch(input, options = {}) {
|
|
28
42
|
return this.dispatchWith(input, options);
|
|
@@ -31,12 +45,30 @@ export class HttpTransport {
|
|
|
31
45
|
async dispatchBinary(input, body, options = {}) {
|
|
32
46
|
if (!(body instanceof Blob))
|
|
33
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) {
|
|
34
52
|
if (options.idempotencyKey !== undefined) {
|
|
53
|
+
cancelUnsent(body);
|
|
35
54
|
throw new TypeError('Binary requests cannot carry an idempotency key.');
|
|
36
55
|
}
|
|
37
56
|
return this.dispatchWith(input, options, body);
|
|
38
57
|
}
|
|
39
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;
|
|
40
72
|
const requestId = this.nextRequestId();
|
|
41
73
|
const timeoutMs = positiveInteger(options.timeoutMs ?? this.timeoutMs, 'timeoutMs');
|
|
42
74
|
const codec = codecs.json;
|
|
@@ -48,22 +80,36 @@ export class HttpTransport {
|
|
|
48
80
|
...invocationInput,
|
|
49
81
|
...(options.idempotencyKey === undefined ? {} : { idempotencyKey: options.idempotencyKey }),
|
|
50
82
|
});
|
|
51
|
-
const encoded =
|
|
83
|
+
const encoded = binary === undefined
|
|
52
84
|
? codec.encode(request)
|
|
53
85
|
: protocol.http.encodeBinaryRequestEnvelope(request);
|
|
54
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());
|
|
55
91
|
let handedOff = false;
|
|
56
92
|
let phase = 'connect';
|
|
93
|
+
// The status of the response being received, which a failure to read it still reports.
|
|
94
|
+
let status;
|
|
57
95
|
let delivery = 'not-sent';
|
|
58
96
|
try {
|
|
59
97
|
requireLive(control);
|
|
60
|
-
if (
|
|
98
|
+
if (binary === undefined && encodedByteLength(encoded) > this.maximumRequestBytes) {
|
|
61
99
|
throw invocationFailure(`request exceeds maximumRequestBytes (${this.maximumRequestBytes}).`, 'send');
|
|
62
100
|
}
|
|
63
101
|
phase = 'send';
|
|
64
102
|
delivery = 'unknown';
|
|
65
|
-
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
|
+
}
|
|
66
111
|
phase = 'receive';
|
|
112
|
+
status = response.status;
|
|
67
113
|
requireLive(control);
|
|
68
114
|
const result = await admitResponse(response, requestId, {
|
|
69
115
|
maximumBytes: this.maximumResponseBytes,
|
|
@@ -75,35 +121,42 @@ export class HttpTransport {
|
|
|
75
121
|
return result;
|
|
76
122
|
}
|
|
77
123
|
catch (error) {
|
|
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;
|
|
78
129
|
// A request the server refused unprocessed, even when sent again, was never delivered.
|
|
79
130
|
const proven = phase === 'send' && unprocessed(error) ? 'not-sent' : delivery;
|
|
80
|
-
throw transportFailure(error, control, options.signal, timeoutMs, this.url, phase, proven);
|
|
131
|
+
throw transportFailure(error, control, options.signal, timeoutMs, this.url, phase, proven, status);
|
|
81
132
|
}
|
|
82
133
|
finally {
|
|
83
134
|
if (!handedOff)
|
|
84
135
|
control.close();
|
|
85
136
|
}
|
|
86
137
|
}
|
|
87
|
-
async fetchResponse(codec, encoded, trace, control,
|
|
138
|
+
async fetchResponse(codec, encoded, trace, control, binary) {
|
|
88
139
|
const init = {
|
|
89
140
|
method: 'POST',
|
|
90
141
|
redirect: 'manual',
|
|
91
142
|
credentials: 'omit',
|
|
92
143
|
referrerPolicy: 'no-referrer',
|
|
93
|
-
// Fetch owns Content-Length; the protocol carries
|
|
144
|
+
// Fetch owns Content-Length; the protocol carries the exact length across Fetch runtimes.
|
|
94
145
|
headers: {
|
|
95
146
|
...this.headers,
|
|
96
147
|
...protocol.http.encodeTraceHeaders(trace),
|
|
97
|
-
'content-type':
|
|
148
|
+
'content-type': binary === undefined ? codec.contentType : protocol.http.BINARY_REQUEST_CONTENT_TYPE,
|
|
98
149
|
accept: codec.contentType,
|
|
99
|
-
...(
|
|
150
|
+
...(binary === undefined
|
|
100
151
|
? {}
|
|
101
152
|
: {
|
|
102
153
|
[protocol.http.REQUEST_ENVELOPE_FIELD]: encoded,
|
|
103
|
-
[protocol.http.BINARY_LENGTH_FIELD]: String(
|
|
154
|
+
[protocol.http.BINARY_LENGTH_FIELD]: String(binary.length),
|
|
104
155
|
}),
|
|
105
156
|
},
|
|
106
|
-
body:
|
|
157
|
+
body: binary === undefined ? toBody(encoded) : binary.body,
|
|
158
|
+
// Fetch sends a stream body without buffering it only in half duplex.
|
|
159
|
+
...(binary?.kind === 'once' ? { duplex: 'half' } : {}),
|
|
107
160
|
signal: control.signal,
|
|
108
161
|
};
|
|
109
162
|
try {
|
|
@@ -112,8 +165,10 @@ export class HttpTransport {
|
|
|
112
165
|
catch (error) {
|
|
113
166
|
// The server processed none of it: send it once more, on the connection that replaces the
|
|
114
167
|
// one that refused it, under the same timeout and signal (CLIENT-HTTP-UNPROCESSED-RESEND).
|
|
115
|
-
|
|
168
|
+
// A stream body was consumed by the refused attempt: it is never sent again.
|
|
169
|
+
if (!unprocessed(error) || control.signal.aborted || binary?.kind === 'once') {
|
|
116
170
|
throw error;
|
|
171
|
+
}
|
|
117
172
|
return this.fetchImpl(this.url, init);
|
|
118
173
|
}
|
|
119
174
|
}
|
|
@@ -132,7 +187,7 @@ function toBody(input) {
|
|
|
132
187
|
function encodedByteLength(input) {
|
|
133
188
|
return typeof input === 'string' ? new TextEncoder().encode(input).byteLength : input.byteLength;
|
|
134
189
|
}
|
|
135
|
-
function transportFailure(input, control, parent, timeoutMs, url, phase, delivery) {
|
|
190
|
+
function transportFailure(input, control, parent, timeoutMs, url, phase, delivery, status) {
|
|
136
191
|
if (input instanceof ResponseError || input instanceof ProtocolError)
|
|
137
192
|
return input;
|
|
138
193
|
if (control.timedOut) {
|
|
@@ -145,8 +200,7 @@ function transportFailure(input, control, parent, timeoutMs, url, phase, deliver
|
|
|
145
200
|
return asError(parent.reason ?? input);
|
|
146
201
|
return input instanceof TransportError
|
|
147
202
|
? input
|
|
148
|
-
: invocationFailure(
|
|
149
|
-
|
|
150
|
-
delivery
|
|
151
|
-
});
|
|
203
|
+
: invocationFailure(status === undefined
|
|
204
|
+
? `Failed to communicate with the server at ${url}.`
|
|
205
|
+
: `Failed to read the HTTP ${status} response of the server at ${url}.`, phase, { cause: asError(input), delivery });
|
|
152
206
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
+
import type { TransportResult } from './transport.js';
|
|
3
|
+
import { ResponseError } from '../errors/index.js';
|
|
4
|
+
/** The application error one decoded error response carries. */
|
|
5
|
+
export declare function responseError(response: protocol.ErrorResponse): ResponseError;
|
|
6
|
+
/** The Transport result of one decoded unary response that carries no application error. */
|
|
7
|
+
export declare function unaryResult(response: Exclude<protocol.Response, protocol.ErrorResponse>): TransportResult;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ResponseError } from '../errors/index.js';
|
|
2
|
+
/** The application error one decoded error response carries. */
|
|
3
|
+
export function responseError(response) {
|
|
4
|
+
return new ResponseError(response.error.code, response.error.message, response.invocation, response.error.data);
|
|
5
|
+
}
|
|
6
|
+
/** The Transport result of one decoded unary response that carries no application error. */
|
|
7
|
+
export function unaryResult(response) {
|
|
8
|
+
if ('kind' in response) {
|
|
9
|
+
if (response.kind === 'redirect') {
|
|
10
|
+
return Object.freeze({
|
|
11
|
+
kind: 'redirect',
|
|
12
|
+
invocation: response.invocation,
|
|
13
|
+
redirect: response.redirect,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
if (response.kind === 'stale') {
|
|
17
|
+
return Object.freeze({
|
|
18
|
+
kind: 'stale',
|
|
19
|
+
current: response.current,
|
|
20
|
+
invocation: response.invocation,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return Object.freeze({ kind: 'miss', invocation: response.invocation });
|
|
24
|
+
}
|
|
25
|
+
if ('execution' in response) {
|
|
26
|
+
return {
|
|
27
|
+
kind: 'execution',
|
|
28
|
+
execution: response.execution,
|
|
29
|
+
...(response.replayed === undefined ? {} : { replayed: response.replayed }),
|
|
30
|
+
invocation: response.invocation,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return { kind: 'value', value: response.result, invocation: response.invocation };
|
|
34
|
+
}
|
|
@@ -2,7 +2,7 @@ import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
|
2
2
|
import { ProtocolError } from '../../errors/index.js';
|
|
3
3
|
import { invocationFailure } from '../failure.js';
|
|
4
4
|
import { asError } from '../index.js';
|
|
5
|
-
import { WebSocketExchange } from './exchange.js';
|
|
5
|
+
import { isKind, WebSocketExchange } from './exchange.js';
|
|
6
6
|
const WS_CONNECTING = 0;
|
|
7
7
|
const WS_OPEN = 1;
|
|
8
8
|
export class WebSocketConnection {
|
|
@@ -268,12 +268,6 @@ function isStreamFrame(input) {
|
|
|
268
268
|
: undefined;
|
|
269
269
|
return typeof kind === 'string' && kind.startsWith('stream.');
|
|
270
270
|
}
|
|
271
|
-
function isKind(input, kind) {
|
|
272
|
-
return (typeof input === 'object' &&
|
|
273
|
-
input !== null &&
|
|
274
|
-
!Array.isArray(input) &&
|
|
275
|
-
input.kind === kind);
|
|
276
|
-
}
|
|
277
271
|
function requestKey(requestId) {
|
|
278
272
|
return requestId === null ? 'null:' : `${typeof requestId}:${String(requestId)}`;
|
|
279
273
|
}
|
|
@@ -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 { invocationFailure } from '../failure.js';
|
|
4
4
|
import { asError } from '../index.js';
|
|
5
|
+
import { responseError, unaryResult } from '../unary.js';
|
|
5
6
|
/** One correlated invocation within a multiplexed WebSocket connection. */
|
|
6
7
|
export class WebSocketExchange {
|
|
7
8
|
owner;
|
|
@@ -115,54 +116,22 @@ export class WebSocketExchange {
|
|
|
115
116
|
});
|
|
116
117
|
return;
|
|
117
118
|
}
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (start.kind !== 'stream.start')
|
|
119
|
+
if (isStreamFrame(input)) {
|
|
120
|
+
requireId(this.requestId, input.requestId);
|
|
121
|
+
if (input.kind !== 'stream.start') {
|
|
122
122
|
throw new ProtocolError('stream did not begin with stream.start.');
|
|
123
|
+
}
|
|
123
124
|
this.state = 'stream';
|
|
124
|
-
this.invocation =
|
|
125
|
-
this.resolveResult({ kind: 'stream', stream: this, invocation:
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
if (isKind(input, 'redirect')) {
|
|
129
|
-
const response = input;
|
|
130
|
-
requireId(this.requestId, response.requestId);
|
|
131
|
-
this.complete({
|
|
132
|
-
kind: 'redirect',
|
|
133
|
-
invocation: response.invocation,
|
|
134
|
-
redirect: response.redirect,
|
|
135
|
-
});
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
if (isKind(input, 'stale')) {
|
|
139
|
-
const stale = input;
|
|
140
|
-
requireId(this.requestId, stale.requestId);
|
|
141
|
-
this.complete(Object.freeze({ kind: 'stale', current: stale.current, invocation: stale.invocation }));
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
if (isKind(input, 'miss')) {
|
|
145
|
-
const miss = input;
|
|
146
|
-
requireId(this.requestId, miss.requestId);
|
|
147
|
-
this.complete(Object.freeze({ kind: 'miss', invocation: miss.invocation }));
|
|
125
|
+
this.invocation = input.invocation;
|
|
126
|
+
this.resolveResult({ kind: 'stream', stream: this, invocation: input.invocation });
|
|
148
127
|
return;
|
|
149
128
|
}
|
|
150
129
|
const response = input;
|
|
151
130
|
requireId(this.requestId, response.requestId);
|
|
152
|
-
if ('error' in response)
|
|
153
|
-
this.reject(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if ('execution' in response) {
|
|
157
|
-
this.complete({
|
|
158
|
-
kind: 'execution',
|
|
159
|
-
execution: response.execution,
|
|
160
|
-
...(response.replayed === undefined ? {} : { replayed: response.replayed }),
|
|
161
|
-
invocation: response.invocation,
|
|
162
|
-
});
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
this.complete({ kind: 'value', value: response.result, invocation: response.invocation });
|
|
131
|
+
if ('error' in response)
|
|
132
|
+
this.reject(responseError(response));
|
|
133
|
+
else
|
|
134
|
+
this.complete(unaryResult(response));
|
|
166
135
|
}
|
|
167
136
|
receiveStreamFrame(frame) {
|
|
168
137
|
requireId(this.requestId, frame.requestId);
|
|
@@ -305,7 +274,8 @@ function requireId(expected, received) {
|
|
|
305
274
|
function isStreamFrame(input) {
|
|
306
275
|
return 'kind' in input && typeof input.kind === 'string' && input.kind.startsWith('stream.');
|
|
307
276
|
}
|
|
308
|
-
|
|
277
|
+
/** Whether `input` is a plain object whose `kind` is `kind`. */
|
|
278
|
+
export function isKind(input, kind) {
|
|
309
279
|
return (typeof input === 'object' &&
|
|
310
280
|
input !== null &&
|
|
311
281
|
!Array.isArray(input) &&
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrale-os/kernel-client",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.23",
|
|
4
4
|
"description": "Universal Client — protocol-first, transport-agnostic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astrale",
|
|
@@ -65,13 +65,13 @@
|
|
|
65
65
|
"access": "public"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@astrale-os/kernel-core": "1.0.0-beta.
|
|
69
|
-
"@astrale-os/kernel-dsl": "1.0.0-beta.
|
|
70
|
-
"@astrale-os/kernel-protocol": "1.0.0-beta.
|
|
68
|
+
"@astrale-os/kernel-core": "1.0.0-beta.20",
|
|
69
|
+
"@astrale-os/kernel-dsl": "1.0.0-beta.13",
|
|
70
|
+
"@astrale-os/kernel-protocol": "1.0.0-beta.20",
|
|
71
71
|
"@tanstack/pacer": "0.20.1"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@astrale-os/kernel-server": "1.0.0-beta.
|
|
74
|
+
"@astrale-os/kernel-server": "1.0.0-beta.20",
|
|
75
75
|
"@astrale/typescript-config": "npm:@jsr/astrale__typescript-config@~1.1.0",
|
|
76
76
|
"@types/node": "^22.0.0",
|
|
77
77
|
"typescript": "7.0.2",
|