@astrale-os/kernel-client 1.0.0-beta.3 → 1.0.0-beta.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/dist/auth/index.d.ts +1 -0
  2. package/dist/client/admission.js +3 -1
  3. package/dist/client/client.d.ts +19 -2
  4. package/dist/client/client.js +62 -30
  5. package/dist/client/index.d.ts +3 -3
  6. package/dist/client/index.js +2 -2
  7. package/dist/client/request.d.ts +8 -2
  8. package/dist/client/request.js +28 -7
  9. package/dist/client/result.d.ts +10 -1
  10. package/dist/client/result.js +10 -4
  11. package/dist/client/routing-control.d.ts +11 -15
  12. package/dist/client/routing-control.js +21 -29
  13. package/dist/errors/index.d.ts +3 -0
  14. package/dist/errors/index.js +3 -0
  15. package/dist/errors/query-input.d.ts +47 -0
  16. package/dist/errors/query-input.js +85 -0
  17. package/dist/errors/refusal.d.ts +19 -0
  18. package/dist/errors/refusal.js +63 -0
  19. package/dist/errors/response-too-large-error.d.ts +11 -0
  20. package/dist/errors/response-too-large-error.js +15 -0
  21. package/dist/errors/stale-route-error.d.ts +1 -1
  22. package/dist/errors/stale-route-error.js +1 -1
  23. package/dist/{schema → execution}/admission.d.ts +2 -2
  24. package/dist/{schema → execution}/admission.js +3 -3
  25. package/dist/execution/contracts.d.ts +76 -0
  26. package/dist/execution/contracts.js +1 -0
  27. package/dist/execution/deadline.d.ts +18 -0
  28. package/dist/execution/deadline.js +25 -0
  29. package/dist/execution/execution.d.ts +61 -0
  30. package/dist/execution/execution.js +132 -0
  31. package/dist/execution/index.d.ts +4 -0
  32. package/dist/execution/index.js +3 -0
  33. package/dist/graph/domain/binding.d.ts +2 -2
  34. package/dist/graph/domain/binding.js +24 -17
  35. package/dist/graph/domain/conformance.d.ts +12 -0
  36. package/dist/graph/domain/conformance.js +18 -0
  37. package/dist/graph/domain/index.d.ts +1 -0
  38. package/dist/graph/domain/index.js +1 -1
  39. package/dist/graph/domain/provenance.d.ts +0 -2
  40. package/dist/graph/domain/provenance.js +0 -4
  41. package/dist/graph/graph.d.ts +17 -3
  42. package/dist/graph/graph.js +4 -1
  43. package/dist/graph/index.d.ts +3 -3
  44. package/dist/graph/index.js +1 -1
  45. package/dist/graph/page-contract.d.ts +64 -1
  46. package/dist/graph/provenance.d.ts +4 -5
  47. package/dist/graph/provenance.js +5 -6
  48. package/dist/graph/query.d.ts +10 -4
  49. package/dist/graph/query.js +145 -7
  50. package/dist/graph/query.typecheck.d.ts +77 -0
  51. package/dist/graph/query.typecheck.js +34 -0
  52. package/dist/pacer/index.d.ts +1 -1
  53. package/dist/pacer/index.js +1 -1
  54. package/dist/pacer/retry/index.d.ts +1 -0
  55. package/dist/pacer/retry/index.js +1 -0
  56. package/dist/pacer/retry/refusals.d.ts +7 -0
  57. package/dist/pacer/retry/refusals.js +10 -0
  58. package/dist/pacer/retry/retry.d.ts +2 -0
  59. package/dist/pacer/retry/retry.js +33 -1
  60. package/dist/schema/contracts.d.ts +77 -9
  61. package/dist/schema/index.d.ts +3 -2
  62. package/dist/schema/index.js +1 -1
  63. package/dist/schema/installation.d.ts +76 -0
  64. package/dist/schema/installation.js +82 -0
  65. package/dist/schema/schema.d.ts +22 -13
  66. package/dist/schema/schema.js +67 -48
  67. package/dist/session/admission.js +10 -3
  68. package/dist/session/bound.d.ts +12 -3
  69. package/dist/session/bound.js +66 -20
  70. package/dist/session/content/api.d.ts +10 -2
  71. package/dist/session/content/api.js +13 -4
  72. package/dist/session/content/index.d.ts +1 -1
  73. package/dist/session/content/input.d.ts +6 -1
  74. package/dist/session/content/input.js +12 -4
  75. package/dist/session/content/retry.d.ts +5 -1
  76. package/dist/session/content/retry.js +21 -31
  77. package/dist/session/context/index.d.ts +1 -1
  78. package/dist/session/context/index.js +1 -1
  79. package/dist/session/context/operation.d.ts +2 -0
  80. package/dist/session/context/operation.js +5 -0
  81. package/dist/session/credential/credentials.js +43 -28
  82. package/dist/session/discovery/bundle.d.ts +9 -4
  83. package/dist/session/discovery/bundle.js +28 -35
  84. package/dist/session/discovery/discovery.d.ts +1 -1
  85. package/dist/session/discovery/discovery.js +8 -11
  86. package/dist/session/discovery/release.js +18 -28
  87. package/dist/session/discovery/snapshot.d.ts +5 -1
  88. package/dist/session/discovery/transport.d.ts +35 -3
  89. package/dist/session/discovery/transport.js +40 -1
  90. package/dist/session/durable.d.ts +13 -0
  91. package/dist/session/durable.js +39 -0
  92. package/dist/session/exchange/exchange.d.ts +3 -2
  93. package/dist/session/exchange/exchange.js +10 -7
  94. package/dist/session/exchange/index.d.ts +1 -1
  95. package/dist/session/exchange/index.js +1 -1
  96. package/dist/session/exchange/presentation.js +28 -10
  97. package/dist/session/exchange/source.d.ts +7 -7
  98. package/dist/session/exchange/source.js +26 -27
  99. package/dist/session/index.d.ts +4 -3
  100. package/dist/session/index.js +0 -1
  101. package/dist/session/options.d.ts +0 -2
  102. package/dist/session/options.js +0 -3
  103. package/dist/session/reference.d.ts +9 -5
  104. package/dist/session/reference.js +7 -2
  105. package/dist/session/retry.d.ts +12 -0
  106. package/dist/session/retry.js +23 -0
  107. package/dist/session/routing/auth.d.ts +9 -3
  108. package/dist/session/routing/auth.js +4 -3
  109. package/dist/session/routing/cache.d.ts +12 -5
  110. package/dist/session/routing/cache.js +56 -48
  111. package/dist/session/routing/dispatch.d.ts +26 -3
  112. package/dist/session/routing/dispatch.js +112 -67
  113. package/dist/session/routing/envelope.d.ts +3 -3
  114. package/dist/session/routing/index.d.ts +1 -1
  115. package/dist/session/routing/legacy/release-route.d.ts +12 -0
  116. package/dist/session/routing/legacy/release-route.js +6 -0
  117. package/dist/session/routing/recovery.d.ts +3 -6
  118. package/dist/session/routing/recovery.js +3 -14
  119. package/dist/session/routing/request.d.ts +6 -1
  120. package/dist/session/routing/request.js +13 -2
  121. package/dist/session/routing/result.d.ts +1 -1
  122. package/dist/session/routing/result.js +5 -1
  123. package/dist/session/routing/store.js +4 -5
  124. package/dist/session/session.d.ts +31 -5
  125. package/dist/session/session.js +60 -19
  126. package/dist/session/session.typecheck.js +20 -6
  127. package/dist/session/views/index.d.ts +2 -2
  128. package/dist/session/views/index.js +2 -2
  129. package/dist/session/views/kernel.d.ts +31 -0
  130. package/dist/session/views/kernel.js +30 -0
  131. package/dist/session/views/legacy/catalog.d.ts +21 -0
  132. package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
  133. package/dist/session/views/legacy/index.d.ts +14 -0
  134. package/dist/session/views/legacy/index.js +14 -0
  135. package/dist/session/views/legacy/resolve.d.ts +46 -0
  136. package/dist/session/views/legacy/resolve.js +83 -0
  137. package/dist/session/views/legacy/unplaced.d.ts +22 -0
  138. package/dist/session/views/legacy/unplaced.js +26 -0
  139. package/dist/session/views/read.d.ts +49 -0
  140. package/dist/session/views/read.js +148 -0
  141. package/dist/session/views/view.d.ts +48 -0
  142. package/dist/session/views/view.js +74 -0
  143. package/dist/store/memory.d.ts +1 -3
  144. package/dist/store/memory.js +73 -105
  145. package/dist/store/shape.d.ts +33 -12
  146. package/dist/store/shape.js +125 -16
  147. package/dist/store/store.d.ts +6 -6
  148. package/dist/store/store.js +1 -1
  149. package/dist/transport/auto/decision.d.ts +26 -14
  150. package/dist/transport/auto/decision.js +35 -32
  151. package/dist/transport/auto/fallback.d.ts +1 -1
  152. package/dist/transport/auto/fallback.js +5 -10
  153. package/dist/transport/auto/transport.d.ts +0 -2
  154. package/dist/transport/auto/transport.js +24 -32
  155. package/dist/transport/deadline.d.ts +2 -0
  156. package/dist/transport/deadline.js +18 -0
  157. package/dist/transport/failure.d.ts +28 -1
  158. package/dist/transport/failure.js +63 -1
  159. package/dist/transport/http/admission.js +7 -34
  160. package/dist/transport/http/binary-request.d.ts +44 -0
  161. package/dist/transport/http/binary-request.js +110 -0
  162. package/dist/transport/http/binary.js +3 -3
  163. package/dist/transport/http/byte-stream.d.ts +2 -1
  164. package/dist/transport/http/byte-stream.js +12 -8
  165. package/dist/transport/http/index.d.ts +1 -1
  166. package/dist/transport/http/index.js +1 -1
  167. package/dist/transport/http/response.d.ts +3 -1
  168. package/dist/transport/http/response.js +7 -9
  169. package/dist/transport/http/stream.d.ts +8 -13
  170. package/dist/transport/http/stream.js +34 -81
  171. package/dist/transport/http/transport.d.ts +10 -0
  172. package/dist/transport/http/transport.js +87 -32
  173. package/dist/transport/http/unprocessed.d.ts +5 -0
  174. package/dist/transport/http/unprocessed.js +28 -0
  175. package/dist/transport/index.d.ts +1 -1
  176. package/dist/transport/index.js +1 -1
  177. package/dist/transport/stream.d.ts +52 -0
  178. package/dist/transport/stream.js +101 -0
  179. package/dist/transport/transport.d.ts +10 -0
  180. package/dist/transport/unary.d.ts +8 -0
  181. package/dist/transport/unary.js +34 -0
  182. package/dist/transport/websocket/connection.d.ts +7 -3
  183. package/dist/transport/websocket/connection.js +103 -92
  184. package/dist/transport/websocket/exchange.d.ts +12 -19
  185. package/dist/transport/websocket/exchange.js +86 -184
  186. package/dist/transport/websocket/transport.js +11 -19
  187. package/package.json +7 -5
  188. package/dist/session/execution.d.ts +0 -65
  189. package/dist/session/execution.js +0 -34
  190. package/dist/session/views/catalog.d.ts +0 -19
  191. package/dist/session/views/resolve.d.ts +0 -13
  192. package/dist/session/views/resolve.js +0 -21
@@ -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,8 +1,9 @@
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, ResponseError } from '../../errors/index.js';
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';
@@ -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 new ResponseError(envelope.error.code, envelope.error.message, envelope.invocation, envelope.error.data);
68
- }
67
+ if (envelope.requestId === null && 'error' in envelope)
68
+ throw responseError(envelope.error, envelope.invocation);
69
69
  requireId(requestId, envelope.requestId);
70
- if ('kind' in envelope && envelope.kind === 'stale') {
71
- const stale = decodeProtocol('stale response', () => protocol.decodeStale(envelope));
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);
92
72
  requireLive(control);
93
- return { kind: 'value', value: envelope.result, invocation: envelope.invocation };
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 new ProtocolError(`streaming binary exceeds maximumResponseBytes (${maximumBytes}).`);
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 { asError } from '../index.js';
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.signal.reason);
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 new ProtocolError(`streaming binary exceeds maximumResponseBytes (${this.maximumBytes}).`);
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
- await this.cancel(error);
76
- throw error;
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(reason) {
83
+ async interrupt(failure) {
80
84
  if (this.terminal || this.interruption !== undefined)
81
85
  return;
82
- this.interruption = asError(reason ?? new Error('streaming binary was cancelled.'));
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 new ProtocolError(`response exceeds maximumResponseBytes (${maximumBytes}).`);
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 new ProtocolError(`response exceeds maximumResponseBytes (${maximumBytes}).`);
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 { Stream } from '../../client/index.js';
5
- import { type ControlledSignal } from '../index.js';
6
- export declare class HttpStream implements Stream {
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: protocol.InvocationId;
12
- private claimed;
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<HttpStream>;
18
- [Symbol.asyncIterator](): AsyncIterator<Value>;
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
  }