@astrale-os/kernel-client 1.0.0-beta.5 → 1.0.0-beta.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/authorization.d.ts +10 -5
- package/dist/auth/authorization.js +14 -1
- package/dist/auth/index.d.ts +1 -0
- package/dist/client/admission.js +3 -1
- package/dist/client/client.d.ts +70 -4
- package/dist/client/client.js +209 -32
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.js +2 -2
- package/dist/client/level.d.ts +58 -0
- package/dist/client/level.js +159 -0
- package/dist/client/request.d.ts +7 -2
- package/dist/client/request.js +11 -7
- package/dist/client/result.d.ts +14 -1
- package/dist/client/result.js +19 -8
- package/dist/client/routing-control.d.ts +11 -15
- package/dist/client/routing-control.js +21 -29
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/index.js +3 -0
- package/dist/errors/inexpressible-error.d.ts +23 -0
- package/dist/errors/inexpressible-error.js +26 -0
- package/dist/errors/query-input.d.ts +47 -0
- package/dist/errors/query-input.js +85 -0
- package/dist/errors/refusal.d.ts +12 -0
- package/dist/errors/refusal.js +44 -6
- package/dist/errors/response-too-large-error.d.ts +11 -0
- package/dist/errors/response-too-large-error.js +15 -0
- package/dist/errors/stale-route-error.d.ts +1 -1
- package/dist/errors/stale-route-error.js +1 -1
- package/dist/{schema → execution}/admission.d.ts +2 -2
- package/dist/{schema → execution}/admission.js +2 -2
- package/dist/{session/execution.d.ts → execution/contracts.d.ts} +22 -34
- package/dist/execution/contracts.js +1 -0
- package/dist/execution/deadline.d.ts +18 -0
- package/dist/execution/deadline.js +25 -0
- package/dist/execution/execution.d.ts +61 -0
- package/dist/execution/execution.js +132 -0
- package/dist/execution/index.d.ts +4 -0
- package/dist/execution/index.js +3 -0
- package/dist/graph/domain/binding.d.ts +2 -2
- package/dist/graph/domain/binding.js +20 -17
- package/dist/graph/domain/conformance.d.ts +12 -0
- package/dist/graph/domain/conformance.js +18 -0
- package/dist/graph/domain/index.d.ts +1 -0
- package/dist/graph/domain/index.js +1 -1
- package/dist/graph/domain/provenance.d.ts +0 -2
- package/dist/graph/domain/provenance.js +0 -4
- package/dist/graph/graph.d.ts +17 -3
- package/dist/graph/graph.js +4 -1
- package/dist/graph/index.d.ts +3 -3
- package/dist/graph/index.js +1 -1
- package/dist/graph/page-contract.d.ts +66 -1
- package/dist/graph/provenance.d.ts +4 -5
- package/dist/graph/provenance.js +5 -6
- package/dist/graph/query.d.ts +10 -4
- package/dist/graph/query.js +144 -5
- package/dist/graph/query.typecheck.d.ts +81 -0
- package/dist/graph/query.typecheck.js +34 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/schema/contracts.d.ts +54 -79
- package/dist/schema/index.d.ts +2 -2
- package/dist/schema/index.js +1 -1
- package/dist/schema/installation.d.ts +36 -64
- package/dist/schema/installation.js +67 -131
- package/dist/schema/schema.d.ts +10 -16
- package/dist/schema/schema.js +57 -34
- package/dist/session/admission.js +10 -3
- package/dist/session/bound.d.ts +12 -3
- package/dist/session/bound.js +36 -32
- package/dist/session/content/api.d.ts +20 -2
- package/dist/session/content/api.js +17 -7
- package/dist/session/content/files.d.ts +12 -0
- package/dist/session/content/files.js +47 -0
- package/dist/session/content/index.d.ts +1 -1
- package/dist/session/content/input.d.ts +13 -2
- package/dist/session/content/input.js +25 -4
- package/dist/session/content/retry.d.ts +5 -1
- package/dist/session/content/retry.js +21 -31
- package/dist/session/context/index.d.ts +2 -2
- package/dist/session/context/index.js +2 -2
- package/dist/session/context/operation.d.ts +2 -0
- package/dist/session/context/operation.js +5 -0
- package/dist/session/context/policy.d.ts +2 -0
- package/dist/session/context/policy.js +6 -3
- package/dist/session/credential/credentials.d.ts +2 -1
- package/dist/session/credential/credentials.js +62 -29
- package/dist/session/discovery/bundle.d.ts +13 -4
- package/dist/session/discovery/bundle.js +85 -36
- package/dist/session/discovery/discovery.d.ts +1 -1
- package/dist/session/discovery/discovery.js +8 -11
- package/dist/session/discovery/index.d.ts +1 -0
- package/dist/session/discovery/index.js +5 -1
- package/dist/session/discovery/release.d.ts +2 -0
- package/dist/session/discovery/release.js +34 -31
- package/dist/session/discovery/response.d.ts +4 -0
- package/dist/session/discovery/response.js +10 -6
- package/dist/session/discovery/snapshot.d.ts +5 -1
- package/dist/session/discovery/transport.d.ts +37 -3
- package/dist/session/discovery/transport.js +62 -1
- package/dist/session/durable.d.ts +13 -0
- package/dist/session/durable.js +39 -0
- package/dist/session/exchange/exchange.d.ts +3 -2
- package/dist/session/exchange/exchange.js +10 -7
- package/dist/session/exchange/index.d.ts +1 -1
- package/dist/session/exchange/index.js +1 -1
- package/dist/session/exchange/presentation.js +28 -10
- package/dist/session/exchange/source.d.ts +7 -7
- package/dist/session/exchange/source.js +26 -27
- package/dist/session/index.d.ts +5 -4
- package/dist/session/index.js +0 -1
- package/dist/session/options.d.ts +4 -2
- package/dist/session/options.js +3 -3
- package/dist/session/reference.d.ts +27 -6
- package/dist/session/reference.js +18 -3
- package/dist/session/retry.d.ts +12 -0
- package/dist/session/retry.js +23 -0
- package/dist/session/routing/auth.d.ts +9 -3
- package/dist/session/routing/auth.js +4 -3
- package/dist/session/routing/cache.d.ts +12 -5
- package/dist/session/routing/cache.js +56 -48
- package/dist/session/routing/dispatch.d.ts +26 -3
- package/dist/session/routing/dispatch.js +112 -69
- package/dist/session/routing/envelope.d.ts +3 -3
- package/dist/session/routing/index.d.ts +1 -1
- package/dist/session/routing/legacy/release-route.d.ts +12 -0
- package/dist/session/routing/legacy/release-route.js +6 -0
- package/dist/session/routing/recovery.d.ts +3 -6
- package/dist/session/routing/recovery.js +3 -14
- package/dist/session/routing/request.d.ts +2 -1
- package/dist/session/routing/request.js +2 -1
- package/dist/session/routing/result.d.ts +1 -1
- package/dist/session/routing/result.js +5 -1
- package/dist/session/routing/store.js +4 -5
- package/dist/session/session.d.ts +31 -5
- package/dist/session/session.js +70 -22
- package/dist/session/session.typecheck.js +20 -6
- package/dist/session/views/index.d.ts +2 -2
- package/dist/session/views/index.js +2 -2
- package/dist/session/views/kernel.d.ts +31 -0
- package/dist/session/views/kernel.js +30 -0
- package/dist/session/views/legacy/catalog.d.ts +21 -0
- package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
- package/dist/session/views/legacy/index.d.ts +14 -0
- package/dist/session/views/legacy/index.js +14 -0
- package/dist/session/views/legacy/resolve.d.ts +46 -0
- package/dist/session/views/legacy/resolve.js +83 -0
- package/dist/session/views/legacy/unplaced.d.ts +22 -0
- package/dist/session/views/legacy/unplaced.js +26 -0
- package/dist/session/views/read.d.ts +49 -0
- package/dist/session/views/read.js +148 -0
- package/dist/session/views/view.d.ts +48 -0
- package/dist/session/views/view.js +74 -0
- package/dist/store/memory.d.ts +1 -3
- package/dist/store/memory.js +61 -112
- package/dist/store/shape.d.ts +33 -12
- package/dist/store/shape.js +125 -16
- package/dist/store/store.d.ts +6 -6
- package/dist/store/store.js +1 -1
- package/dist/transport/auto/decision.d.ts +28 -14
- package/dist/transport/auto/decision.js +39 -32
- package/dist/transport/auto/fallback.d.ts +1 -1
- package/dist/transport/auto/fallback.js +5 -10
- package/dist/transport/auto/transport.d.ts +10 -3
- package/dist/transport/auto/transport.js +41 -32
- package/dist/transport/deadline.d.ts +2 -0
- package/dist/transport/deadline.js +18 -0
- package/dist/transport/failure.d.ts +28 -1
- package/dist/transport/failure.js +63 -1
- package/dist/transport/http/admission.js +8 -35
- package/dist/transport/http/binary-request.d.ts +44 -0
- package/dist/transport/http/binary-request.js +110 -0
- package/dist/transport/http/binary.js +3 -3
- package/dist/transport/http/byte-stream.d.ts +2 -1
- package/dist/transport/http/byte-stream.js +12 -8
- package/dist/transport/http/configuration.d.ts +7 -0
- package/dist/transport/http/configuration.js +2 -0
- package/dist/transport/http/index.d.ts +1 -1
- package/dist/transport/http/index.js +1 -1
- package/dist/transport/http/response.d.ts +13 -1
- package/dist/transport/http/response.js +22 -9
- package/dist/transport/http/stream.d.ts +8 -13
- package/dist/transport/http/stream.js +34 -81
- package/dist/transport/http/transport.d.ts +14 -1
- package/dist/transport/http/transport.js +110 -35
- package/dist/transport/http/unprocessed.d.ts +5 -0
- package/dist/transport/http/unprocessed.js +28 -0
- package/dist/transport/index.d.ts +2 -1
- package/dist/transport/index.js +2 -1
- package/dist/transport/level.d.ts +42 -0
- package/dist/transport/level.js +58 -0
- package/dist/transport/stream.d.ts +52 -0
- package/dist/transport/stream.js +101 -0
- package/dist/transport/transport.d.ts +33 -0
- package/dist/transport/unary.d.ts +9 -0
- package/dist/transport/unary.js +36 -0
- package/dist/transport/websocket/connection.d.ts +13 -4
- package/dist/transport/websocket/connection.js +123 -96
- package/dist/transport/websocket/exchange.d.ts +15 -19
- package/dist/transport/websocket/exchange.js +87 -183
- package/dist/transport/websocket/transport.d.ts +19 -1
- package/dist/transport/websocket/transport.js +50 -21
- package/package.json +9 -7
- package/dist/session/execution.js +0 -34
- package/dist/session/views/catalog.d.ts +0 -19
- package/dist/session/views/resolve.d.ts +0 -13
- package/dist/session/views/resolve.js +0 -21
|
@@ -1,7 +1,34 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
+
import type { ControlledSignal } from './deadline.js';
|
|
3
|
+
import { ProtocolError, TransportError, type InvocationTransportErrorOptions } from '../errors/index.js';
|
|
2
4
|
type InvocationFailureEvidence = Omit<InvocationTransportErrorOptions, 'delivery' | 'phase'> & {
|
|
3
5
|
readonly delivery?: InvocationTransportErrorOptions['delivery'];
|
|
4
6
|
};
|
|
7
|
+
/** Where a request stood when it stopped, and what its binding calls a failure there. */
|
|
8
|
+
export interface RequestStop {
|
|
9
|
+
readonly phase: 'connect' | 'send' | 'receive';
|
|
10
|
+
readonly delivery: InvocationTransportErrorOptions['delivery'];
|
|
11
|
+
/** The message of the failure the request's deadline is. */
|
|
12
|
+
readonly timeout: string;
|
|
13
|
+
/** The message of a failure to communicate. */
|
|
14
|
+
readonly failure: string;
|
|
15
|
+
}
|
|
5
16
|
/** Create invocation transport evidence with the common not-sent default. */
|
|
6
17
|
export declare function invocationFailure(message: string, phase: InvocationTransportErrorOptions['phase'], evidence?: InvocationFailureEvidence): TransportError;
|
|
18
|
+
/** The failure of an invocation its deadline ended. */
|
|
19
|
+
export declare function timeoutFailure(message: string, evidence?: InvocationFailureEvidence): TransportError;
|
|
20
|
+
/** The error a cancelled request fails with: its abort reason, or `otherwise` when it gave none. */
|
|
21
|
+
export declare function abortError(reason: unknown, otherwise: unknown): Error;
|
|
22
|
+
/**
|
|
23
|
+
* The error a request ends with when its control aborts: `timeout` when its deadline fired,
|
|
24
|
+
* otherwise its caller's abort reason, or `cancelled` when the abort gave none.
|
|
25
|
+
*/
|
|
26
|
+
export declare function abortFailure(control: ControlledSignal, timeout: string, cancelled: string, evidence?: Omit<InvocationFailureEvidence, 'cause'>): Error;
|
|
27
|
+
/**
|
|
28
|
+
* The public error of a request that threw `input`: a Kernel answer or protocol breach as it is,
|
|
29
|
+
* then its deadline, then its caller's cancellation, then a transport failure where it stood.
|
|
30
|
+
*/
|
|
31
|
+
export declare function requestFailure(input: unknown, control: ControlledSignal, stop: RequestStop): unknown;
|
|
32
|
+
/** `input` naming `invocation`, when it is a protocol or invocation failure that names none. */
|
|
33
|
+
export declare function withInvocation<T>(input: T, invocation: protocol.InvocationId | undefined): T | ProtocolError | TransportError;
|
|
7
34
|
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { TransportError } from '../errors/index.js';
|
|
1
|
+
import { ProtocolError, ResponseError, ResponseTooLargeError, TransportError, } from '../errors/index.js';
|
|
2
|
+
import { asError } from './deadline.js';
|
|
2
3
|
/** Create invocation transport evidence with the common not-sent default. */
|
|
3
4
|
export function invocationFailure(message, phase, evidence = {}) {
|
|
4
5
|
return TransportError.invocation(message, {
|
|
@@ -7,3 +8,64 @@ export function invocationFailure(message, phase, evidence = {}) {
|
|
|
7
8
|
delivery: evidence.delivery ?? 'not-sent',
|
|
8
9
|
});
|
|
9
10
|
}
|
|
11
|
+
/** The failure of an invocation its deadline ended. */
|
|
12
|
+
export function timeoutFailure(message, evidence = {}) {
|
|
13
|
+
return invocationFailure(message, 'timeout', evidence);
|
|
14
|
+
}
|
|
15
|
+
/** The error a cancelled request fails with: its abort reason, or `otherwise` when it gave none. */
|
|
16
|
+
export function abortError(reason, otherwise) {
|
|
17
|
+
return asError(reason ?? otherwise);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The error a request ends with when its control aborts: `timeout` when its deadline fired,
|
|
21
|
+
* otherwise its caller's abort reason, or `cancelled` when the abort gave none.
|
|
22
|
+
*/
|
|
23
|
+
export function abortFailure(control, timeout, cancelled, evidence = {}) {
|
|
24
|
+
const reason = control.signal.reason;
|
|
25
|
+
if (control.timedOut)
|
|
26
|
+
return timeoutFailure(timeout, { ...evidence, cause: asError(reason) });
|
|
27
|
+
return abortError(reason, new Error(cancelled));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The public error of a request that threw `input`: a Kernel answer or protocol breach as it is,
|
|
31
|
+
* then its deadline, then its caller's cancellation, then a transport failure where it stood.
|
|
32
|
+
*/
|
|
33
|
+
export function requestFailure(input, control, stop) {
|
|
34
|
+
if (input instanceof ResponseError || input instanceof ProtocolError)
|
|
35
|
+
return input;
|
|
36
|
+
if (control.timedOut) {
|
|
37
|
+
return timeoutFailure(stop.timeout, { cause: asError(input), delivery: stop.delivery });
|
|
38
|
+
}
|
|
39
|
+
if (control.signal.aborted)
|
|
40
|
+
return abortError(control.signal.reason, input);
|
|
41
|
+
if (input instanceof TransportError)
|
|
42
|
+
return input;
|
|
43
|
+
return invocationFailure(stop.failure, stop.phase, {
|
|
44
|
+
cause: asError(input),
|
|
45
|
+
delivery: stop.delivery,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** `input` naming `invocation`, when it is a protocol or invocation failure that names none. */
|
|
49
|
+
export function withInvocation(input, invocation) {
|
|
50
|
+
if (invocation === undefined)
|
|
51
|
+
return input;
|
|
52
|
+
if (input instanceof ResponseTooLargeError && input.invocation === undefined) {
|
|
53
|
+
return new ResponseTooLargeError(input.message, input.maximumResponseBytes, {
|
|
54
|
+
cause: input,
|
|
55
|
+
invocation,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
if (input instanceof ProtocolError && input.invocation === undefined) {
|
|
59
|
+
return new ProtocolError(input.message, { cause: input, invocation });
|
|
60
|
+
}
|
|
61
|
+
if (input instanceof TransportError &&
|
|
62
|
+
input.context.kind === 'invocation' &&
|
|
63
|
+
input.context.invocation === undefined) {
|
|
64
|
+
return invocationFailure(input.message, input.phase, {
|
|
65
|
+
cause: input,
|
|
66
|
+
delivery: input.context.delivery,
|
|
67
|
+
invocation,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return input;
|
|
71
|
+
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
2
|
import * as retirement from '@astrale-os/kernel-protocol/routes';
|
|
3
|
-
import { ProtocolError
|
|
3
|
+
import { ProtocolError } from '../../errors/index.js';
|
|
4
4
|
import { invocationFailure } from '../failure.js';
|
|
5
5
|
import { requireLive } from '../index.js';
|
|
6
|
+
import { responseError, unaryResult } from '../unary.js';
|
|
6
7
|
import { decodeBinary } from './binary.js';
|
|
7
8
|
import { codecForResponse, decodeProtocol, readBoundedBody, requireId, requireNoStore, } from './response.js';
|
|
8
9
|
import { decodeDeploymentRetired } from './retired.js';
|
|
9
10
|
import { HttpStream } from './stream.js';
|
|
10
11
|
/** Admit exactly one response representation, preserving reserved-control precedence. */
|
|
11
12
|
export async function admitResponse(response, requestId, context) {
|
|
12
|
-
const { control, maximumBytes } = context;
|
|
13
|
+
const { control, maximumBytes, level, host } = context;
|
|
13
14
|
if (response.status >= 300 && response.status < 400) {
|
|
14
15
|
throw new ProtocolError('server returned a network redirect.');
|
|
15
16
|
}
|
|
@@ -63,39 +64,11 @@ export async function admitResponse(response, requestId, context) {
|
|
|
63
64
|
requireLive(control);
|
|
64
65
|
const decoded = decodeProtocol('response body', () => responseCodec.decode(body));
|
|
65
66
|
const envelope = decodeProtocol('response envelope', () => protocol.decodeResponse(decoded));
|
|
66
|
-
if (envelope.requestId === null && 'error' in envelope)
|
|
67
|
-
throw
|
|
68
|
-
}
|
|
67
|
+
if (envelope.requestId === null && 'error' in envelope)
|
|
68
|
+
throw responseError(envelope.error, envelope.invocation, level, host);
|
|
69
69
|
requireId(requestId, envelope.requestId);
|
|
70
|
-
if ('
|
|
71
|
-
|
|
72
|
-
requireLive(control);
|
|
73
|
-
return Object.freeze({
|
|
74
|
-
kind: 'stale',
|
|
75
|
-
current: stale.current,
|
|
76
|
-
invocation: stale.invocation,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
if ('kind' in envelope && envelope.kind === 'miss') {
|
|
80
|
-
decodeProtocol('miss response', () => protocol.decodeMiss(envelope));
|
|
81
|
-
requireLive(control);
|
|
82
|
-
return Object.freeze({ kind: 'miss', invocation: envelope.invocation });
|
|
83
|
-
}
|
|
84
|
-
if ('kind' in envelope && envelope.kind === 'redirect') {
|
|
85
|
-
const response = decodeProtocol('redirect response', () => protocol.decodeRedirectResponse(envelope));
|
|
86
|
-
requireLive(control);
|
|
87
|
-
return redirectResult(response);
|
|
88
|
-
}
|
|
89
|
-
if ('error' in envelope) {
|
|
90
|
-
throw new ResponseError(envelope.error.code, envelope.error.message, envelope.invocation, envelope.error.data);
|
|
91
|
-
}
|
|
70
|
+
if ('error' in envelope)
|
|
71
|
+
throw responseError(envelope.error, envelope.invocation, level, host);
|
|
92
72
|
requireLive(control);
|
|
93
|
-
return
|
|
94
|
-
}
|
|
95
|
-
function redirectResult(response) {
|
|
96
|
-
return Object.freeze({
|
|
97
|
-
kind: 'redirect',
|
|
98
|
-
invocation: response.invocation,
|
|
99
|
-
redirect: response.redirect,
|
|
100
|
-
});
|
|
73
|
+
return unaryResult(envelope);
|
|
101
74
|
}
|
|
@@ -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
|
+
}
|
|
@@ -3,7 +3,7 @@ import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
|
3
3
|
import { ProtocolError } from '../../errors/index.js';
|
|
4
4
|
import { requireLive } from '../index.js';
|
|
5
5
|
import { HttpByteStream } from './byte-stream.js';
|
|
6
|
-
import { binaryHeaders, decodeProtocol, readBoundedBody } from './response.js';
|
|
6
|
+
import { binaryHeaders, decodeProtocol, readBoundedBody, tooLarge, } from './response.js';
|
|
7
7
|
/** Admit buffered or streaming binary output within the invocation's lifetime. */
|
|
8
8
|
export async function decodeBinary(response, requestId, binaryId, streaming, context) {
|
|
9
9
|
const { control, maximumBytes } = context;
|
|
@@ -29,14 +29,14 @@ export async function decodeBinary(response, requestId, binaryId, streaming, con
|
|
|
29
29
|
throw new ProtocolError(`streaming binary has an invalid ${protocol.http.BINARY_LENGTH_FIELD}.`);
|
|
30
30
|
}
|
|
31
31
|
if (expectedBytes !== undefined && expectedBytes > maximumBytes) {
|
|
32
|
-
throw
|
|
32
|
+
throw tooLarge('streaming binary', maximumBytes);
|
|
33
33
|
}
|
|
34
34
|
requireLive(control);
|
|
35
35
|
return {
|
|
36
36
|
kind: 'binary',
|
|
37
37
|
invocation,
|
|
38
38
|
value: {
|
|
39
|
-
body: new HttpByteStream(response.body, maximumBytes, control, expectedBytes),
|
|
39
|
+
body: new HttpByteStream(response.body, maximumBytes, control, invocation, expectedBytes),
|
|
40
40
|
// The byte stream enforces this exact count through its final read.
|
|
41
41
|
...(expectedBytes === undefined ? {} : { length: expectedBytes }),
|
|
42
42
|
mediaType,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
1
2
|
import type { ControlledSignal } from '../index.js';
|
|
2
3
|
/** Single-consumer HTTP byte stream preserving invocation lifetime and backpressure. */
|
|
3
4
|
export declare class HttpByteStream implements AsyncIterable<Uint8Array> {
|
|
@@ -9,7 +10,7 @@ export declare class HttpByteStream implements AsyncIterable<Uint8Array> {
|
|
|
9
10
|
private terminal;
|
|
10
11
|
private bytes;
|
|
11
12
|
private interruption;
|
|
12
|
-
constructor(body: ReadableStream<Uint8Array>, maximumBytes: number, control: ControlledSignal, expectedBytes?: number | undefined);
|
|
13
|
+
constructor(body: ReadableStream<Uint8Array>, maximumBytes: number, control: ControlledSignal, invocation: protocol.InvocationId, expectedBytes?: number | undefined);
|
|
13
14
|
[Symbol.asyncIterator](): AsyncIterator<Uint8Array>;
|
|
14
15
|
cancel(reason?: unknown): Promise<void>;
|
|
15
16
|
private next;
|
|
@@ -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,3 +1,4 @@
|
|
|
1
|
+
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
1
2
|
/** Web Fetch capability used by the built-in HTTP transport. */
|
|
2
3
|
export type Fetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
3
4
|
export interface HttpTransportOptions {
|
|
@@ -10,6 +11,11 @@ export interface HttpTransportOptions {
|
|
|
10
11
|
readonly maximumRequestBytes?: number;
|
|
11
12
|
/** Maximum unary, binary, or individual stream-frame body size. */
|
|
12
13
|
readonly maximumResponseBytes?: number;
|
|
14
|
+
/**
|
|
15
|
+
* The wire History whose current level the transport states and whose floor bounds what it
|
|
16
|
+
* reads; absent, the Invocation History of this build.
|
|
17
|
+
*/
|
|
18
|
+
readonly levels?: protocol.Levels;
|
|
13
19
|
}
|
|
14
20
|
/** Captured transport configuration, admitted once before any invocation. */
|
|
15
21
|
export interface Configuration {
|
|
@@ -19,6 +25,7 @@ export interface Configuration {
|
|
|
19
25
|
readonly timeoutMs: number;
|
|
20
26
|
readonly maximumRequestBytes: number;
|
|
21
27
|
readonly maximumResponseBytes: number;
|
|
28
|
+
readonly levels: protocol.Levels;
|
|
22
29
|
}
|
|
23
30
|
/** Admit static options and detach the caller's request headers. */
|
|
24
31
|
export declare function capture(options: HttpTransportOptions): Configuration;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
2
|
import { asError, positiveInteger } from '../index.js';
|
|
3
|
+
import { captureLevels } from '../level.js';
|
|
3
4
|
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
4
5
|
const DEFAULT_MAXIMUM_REQUEST_BYTES = 16 * 1024 * 1024;
|
|
5
6
|
const DEFAULT_MAXIMUM_RESPONSE_BYTES = 16 * 1024 * 1024;
|
|
@@ -15,6 +16,7 @@ export function capture(options) {
|
|
|
15
16
|
timeoutMs: positiveInteger(options.timeoutMs ?? DEFAULT_TIMEOUT_MS, 'timeoutMs'),
|
|
16
17
|
maximumRequestBytes: positiveInteger(options.maximumRequestBytes ?? DEFAULT_MAXIMUM_REQUEST_BYTES, 'maximumRequestBytes'),
|
|
17
18
|
maximumResponseBytes: positiveInteger(options.maximumResponseBytes ?? DEFAULT_MAXIMUM_RESPONSE_BYTES, 'maximumResponseBytes'),
|
|
19
|
+
levels: captureLevels(options.levels),
|
|
18
20
|
};
|
|
19
21
|
}
|
|
20
22
|
function normalizeUrl(input) {
|
|
@@ -1,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,15 +1,27 @@
|
|
|
1
|
+
import { type Level } from '@astrale-os/kernel-core/wire';
|
|
1
2
|
import * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
2
3
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
4
|
+
import { ResponseTooLargeError } from '../../errors/index.js';
|
|
3
5
|
import { type ControlledSignal } from '../index.js';
|
|
4
6
|
/** The body budget and cancellation lifetime shared by all response representations. */
|
|
5
7
|
export interface ResponseContext {
|
|
6
8
|
readonly maximumBytes: number;
|
|
7
9
|
readonly control: ControlledSignal;
|
|
10
|
+
/** The wire level the response is written at. */
|
|
11
|
+
readonly level: Level;
|
|
12
|
+
/** The Host's own wire level. */
|
|
13
|
+
readonly host: Level;
|
|
8
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* The wire level the Host of a response tells: level 0 when it tells none. A Host tells its level
|
|
17
|
+
* only to a request that stated one.
|
|
18
|
+
*/
|
|
19
|
+
export declare function hostLevel(response: Response): Level;
|
|
9
20
|
export declare function codecForResponse(contentType: string | null): codecs.Codec | undefined;
|
|
10
21
|
export declare function readBoundedBody(response: Response, maximumBytes: number, signal: AbortSignal): Promise<Uint8Array>;
|
|
22
|
+
/** The failure of an answer that passed the client's `maximumResponseBytes`. */
|
|
23
|
+
export declare function tooLarge(what: string, maximumBytes: number): ResponseTooLargeError;
|
|
11
24
|
export declare function requireId(expected: protocol.RequestId, received: protocol.RequestId): void;
|
|
12
25
|
export declare function decodeProtocol<T>(label: string, decode: () => T): T;
|
|
13
|
-
export declare function normalizeMediaType(input: string | null): string | undefined;
|
|
14
26
|
export declare function binaryHeaders(input: Headers, names: readonly string[]): Readonly<Record<string, string>>;
|
|
15
27
|
export declare function requireNoStore(response: Response): void;
|
|
@@ -1,14 +1,29 @@
|
|
|
1
|
+
import { acceptLevel } from '@astrale-os/kernel-core/wire';
|
|
1
2
|
import * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
2
3
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
-
import { ProtocolError, ResponseError } from '../../errors/index.js';
|
|
4
|
+
import { ProtocolError, ResponseError, ResponseTooLargeError } from '../../errors/index.js';
|
|
4
5
|
import { asError } from '../index.js';
|
|
6
|
+
/**
|
|
7
|
+
* The wire level the Host of a response tells: level 0 when it tells none. A Host tells its level
|
|
8
|
+
* only to a request that stated one.
|
|
9
|
+
*/
|
|
10
|
+
export function hostLevel(response) {
|
|
11
|
+
const field = response.headers.get(protocol.http.LEVEL_FIELD);
|
|
12
|
+
if (field === null)
|
|
13
|
+
return acceptLevel(0);
|
|
14
|
+
const level = protocol.http.decodeLevel(field);
|
|
15
|
+
if (level === undefined) {
|
|
16
|
+
throw new ProtocolError(`server told an invalid wire level ${JSON.stringify(field)}.`);
|
|
17
|
+
}
|
|
18
|
+
return level;
|
|
19
|
+
}
|
|
5
20
|
export function codecForResponse(contentType) {
|
|
6
21
|
return protocol.http.codecForContentType(contentType, [codecs.json]);
|
|
7
22
|
}
|
|
8
23
|
export async function readBoundedBody(response, maximumBytes, signal) {
|
|
9
24
|
const declared = response.headers.get('content-length');
|
|
10
25
|
if (declared !== null && Number(declared) > maximumBytes) {
|
|
11
|
-
throw
|
|
26
|
+
throw tooLarge('response', maximumBytes);
|
|
12
27
|
}
|
|
13
28
|
if (response.body === null)
|
|
14
29
|
return new Uint8Array();
|
|
@@ -24,7 +39,7 @@ export async function readBoundedBody(response, maximumBytes, signal) {
|
|
|
24
39
|
break;
|
|
25
40
|
total += next.value.byteLength;
|
|
26
41
|
if (total > maximumBytes) {
|
|
27
|
-
throw
|
|
42
|
+
throw tooLarge('response', maximumBytes);
|
|
28
43
|
}
|
|
29
44
|
chunks.push(next.value);
|
|
30
45
|
}
|
|
@@ -41,6 +56,10 @@ export async function readBoundedBody(response, maximumBytes, signal) {
|
|
|
41
56
|
}
|
|
42
57
|
return output;
|
|
43
58
|
}
|
|
59
|
+
/** The failure of an answer that passed the client's `maximumResponseBytes`. */
|
|
60
|
+
export function tooLarge(what, maximumBytes) {
|
|
61
|
+
return new ResponseTooLargeError(`${what} exceeds maximumResponseBytes (${maximumBytes}).`, maximumBytes);
|
|
62
|
+
}
|
|
44
63
|
export function requireId(expected, received) {
|
|
45
64
|
if (received !== expected) {
|
|
46
65
|
throw new ProtocolError(`response id ${JSON.stringify(received)} does not match request id ${JSON.stringify(expected)}.`);
|
|
@@ -56,12 +75,6 @@ export function decodeProtocol(label, decode) {
|
|
|
56
75
|
throw new ProtocolError(`server returned an invalid ${label}.`, { cause: asError(error) });
|
|
57
76
|
}
|
|
58
77
|
}
|
|
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
78
|
export function binaryHeaders(input, names) {
|
|
66
79
|
const output = {};
|
|
67
80
|
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
|
}
|