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

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 +24 -0
  18. package/dist/errors/refusal.js +78 -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
@@ -0,0 +1,34 @@
1
+ import { ResponseError } from '../errors/index.js';
2
+ /** The application error one decoded error response or `stream.error` frame of `invocation` carries. */
3
+ export function responseError(error, invocation) {
4
+ return new ResponseError(error.code, error.message, invocation, 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 type * as codecs from '@astrale-os/kernel-protocol/codecs';
2
2
  import * as protocol from '@astrale-os/kernel-protocol/invocation';
3
3
  import type { ControlledSignal } from '../index.js';
4
4
  import type { TransportResult } from '../index.js';
5
- import type { ExchangeOwner } from './exchange.js';
5
+ import type { ExchangeClose, ExchangeOwner } from './exchange.js';
6
6
  import type { WebSocketConstructor } from './transport.js';
7
7
  import { WebSocketExchange } from './exchange.js';
8
8
  interface ConnectionOwner {
@@ -29,11 +29,15 @@ export declare class WebSocketConnection implements ExchangeOwner {
29
29
  get isAvailable(): boolean;
30
30
  connect(control: ControlledSignal): Promise<void>;
31
31
  dispatch(request: protocol.Request, control: ControlledSignal, maximumBufferedItems: number): Promise<TransportResult>;
32
- finish(exchange: WebSocketExchange, reusable: boolean, closeCode?: 1002 | 1008 | 1011): void;
32
+ finish(exchange: WebSocketExchange, close?: ExchangeClose): void;
33
33
  cancel(exchange: WebSocketExchange, requestId: protocol.RequestId): void;
34
- shutdown(reason?: Error, closeCode?: number, closeReason?: string): void;
34
+ /** Closes the connection on its owner's behalf, failing what it still carries with `error`. */
35
+ shutdown(error: Error): void;
36
+ private end;
35
37
  private onOpen;
36
38
  private onMessage;
39
+ /** Hands one message to its exchange, or answers the close it breaks the connection with. */
40
+ private receive;
37
41
  private onFailure;
38
42
  }
39
43
  export {};
@@ -1,8 +1,8 @@
1
1
  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
- import { asError } from '../index.js';
5
- import { WebSocketExchange } from './exchange.js';
4
+ import { asError, requireLive } from '../index.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 {
@@ -46,12 +46,10 @@ export class WebSocketConnection {
46
46
  return !this.closedState;
47
47
  }
48
48
  async connect(control) {
49
- if (control.signal.aborted) {
50
- throw asError(control.signal.reason);
51
- }
49
+ requireLive(control);
52
50
  let abort;
53
51
  const cancelled = new Promise((_resolve, reject) => {
54
- abort = () => reject(asError(control.signal.reason));
52
+ abort = () => reject(control.signal.reason);
55
53
  control.signal.addEventListener('abort', abort, { once: true });
56
54
  });
57
55
  try {
@@ -89,11 +87,15 @@ export class WebSocketConnection {
89
87
  this.active.set(key, exchange);
90
88
  return exchange.start((data) => this.socket.send(socketData(data)), encoded);
91
89
  }
92
- finish(exchange, reusable, closeCode = 1002) {
90
+ finish(exchange, close) {
93
91
  removeExchange(this.active, exchange);
94
- if (!reusable) {
95
- this.shutdown(invocationFailure('WebSocket connection was closed after a binding failure.', 'receive'), closeCode, closeReason(closeCode));
96
- }
92
+ if (close === undefined)
93
+ return;
94
+ this.end({
95
+ code: close,
96
+ reason: closeReason(close),
97
+ error: invocationFailure('WebSocket connection was closed after a binding failure.', 'receive'),
98
+ });
97
99
  }
98
100
  cancel(exchange, requestId) {
99
101
  const key = requestKey(requestId);
@@ -101,35 +103,42 @@ export class WebSocketConnection {
101
103
  return;
102
104
  this.active.delete(key);
103
105
  this.cancelled.add(key);
106
+ let close;
104
107
  try {
105
108
  const encoded = this.codec.encode(protocol.decodeCancelRequest({ version: protocol.VERSION, kind: 'cancel', requestId }));
106
109
  const bytes = frameSize(encoded);
107
110
  if (bytes > this.maximumRequestBytes ||
108
111
  socketBufferedAmount(this.socket) + bytes > this.maximumBufferedBytes) {
109
- throw new WebSocketBindingFailure('WebSocket cancellation exceeds the connection send policy.', 1008, 'Client send policy exceeded');
112
+ const policy = 'WebSocket cancellation exceeds the connection send policy.';
113
+ close = cancelClose(1008, 'Client send policy exceeded', new ProtocolError(policy));
114
+ }
115
+ else {
116
+ this.socket.send(socketData(encoded));
110
117
  }
111
- this.socket.send(socketData(encoded));
112
118
  }
113
119
  catch (error) {
114
- this.shutdown(invocationFailure('Failed to send WebSocket cancellation.', 'send', {
115
- cause: asError(error),
116
- delivery: 'unknown',
117
- }), error instanceof WebSocketBindingFailure ? error.closeCode : 1011, error instanceof WebSocketBindingFailure ? error.closeReason : 'Client send failed');
120
+ close = cancelClose(1011, 'Client send failed', error);
118
121
  }
122
+ if (close !== undefined)
123
+ this.end(close);
124
+ }
125
+ /** Closes the connection on its owner's behalf, failing what it still carries with `error`. */
126
+ shutdown(error) {
127
+ this.end({ code: 1000, reason: 'Client closed', error });
119
128
  }
120
- shutdown(reason = invocationFailure('WebSocket connection was closed.', 'closed'), closeCode = 1000, closeReason = 'Client closed') {
129
+ end(close) {
121
130
  if (this.closedState)
122
131
  return;
123
132
  this.closedState = true;
124
- this.rejectReady(reason);
133
+ this.rejectReady(close.error);
125
134
  const active = [...this.active.values()];
126
135
  this.active.clear();
127
136
  this.cancelled.clear();
128
137
  for (const exchange of active)
129
- exchange.connectionFailed(reason);
138
+ exchange.connectionFailed(close.error);
130
139
  try {
131
140
  if (this.socket.readyState === WS_CONNECTING || this.socket.readyState === WS_OPEN) {
132
- this.socket.close(closeCode, closeReason);
141
+ this.socket.close(close.code, close.reason);
133
142
  }
134
143
  }
135
144
  catch {
@@ -142,7 +151,11 @@ export class WebSocketConnection {
142
151
  return;
143
152
  const expected = protocol.websocket.subprotocolFor(this.codec);
144
153
  if (this.socket.protocol !== expected) {
145
- this.shutdown(invocationFailure(`server selected WebSocket subprotocol ${JSON.stringify(this.socket.protocol)}; expected ${JSON.stringify(expected)}.`, 'connect'), 1002, 'Subprotocol mismatch');
154
+ this.end({
155
+ code: 1002,
156
+ reason: 'Subprotocol mismatch',
157
+ error: invocationFailure(`server selected WebSocket subprotocol ${JSON.stringify(this.socket.protocol)}; expected ${JSON.stringify(expected)}.`, 'connect'),
158
+ });
146
159
  return;
147
160
  }
148
161
  this.opened = true;
@@ -150,29 +163,42 @@ export class WebSocketConnection {
150
163
  }
151
164
  onMessage(event) {
152
165
  this.incoming = this.incoming
153
- .then(async () => {
154
- const frame = await normalizeFrame(event.data);
155
- if (frameSize(frame) > this.maximumResponseBytes) {
156
- throw new WebSocketBindingFailure(`WebSocket frame exceeds maximumResponseBytes (${this.maximumResponseBytes}).`, 1009, 'Message too big');
157
- }
158
- const admitted = decodeFrame(this.codec, frame);
159
- const key = requestKey(admitted.requestId);
160
- const exchange = this.active.get(key);
161
- if (exchange !== undefined) {
162
- exchange.receive(admitted);
163
- return;
164
- }
165
- if (this.cancelled.has(key)) {
166
- if (isTerminal(admitted))
167
- this.cancelled.delete(key);
168
- return;
169
- }
170
- throw new ProtocolError('server sent an unsolicited WebSocket frame.');
171
- })
172
- .catch((error) => {
173
- const failure = bindingFailure(error);
174
- this.shutdown(failure, failure.closeCode, failure.closeReason);
175
- });
166
+ .then(() => this.receive(event.data))
167
+ .then((close) => {
168
+ if (close !== undefined)
169
+ this.end(close);
170
+ }, (error) => this.end(protocolClose(error)));
171
+ }
172
+ /** Hands one message to its exchange, or answers the close it breaks the connection with. */
173
+ async receive(data) {
174
+ const frame = await normalizeFrame(data);
175
+ if (frameSize(frame) > this.maximumResponseBytes) {
176
+ return {
177
+ code: 1009,
178
+ reason: 'Message too big',
179
+ error: new ProtocolError(`WebSocket frame exceeds maximumResponseBytes (${this.maximumResponseBytes}).`),
180
+ };
181
+ }
182
+ const admitted = decodeFrame(this.codec, frame);
183
+ if (admitted === undefined) {
184
+ return {
185
+ code: 1003,
186
+ reason: 'Unsupported message type',
187
+ error: new ProtocolError('server returned a message type incompatible with the selected subprotocol.'),
188
+ };
189
+ }
190
+ const key = requestKey(admitted.requestId);
191
+ const exchange = this.active.get(key);
192
+ if (exchange !== undefined) {
193
+ exchange.receive(admitted);
194
+ return undefined;
195
+ }
196
+ if (this.cancelled.has(key)) {
197
+ if (isTerminal(admitted))
198
+ this.cancelled.delete(key);
199
+ return undefined;
200
+ }
201
+ throw new ProtocolError('server sent an unsolicited WebSocket frame.');
176
202
  }
177
203
  onFailure(message) {
178
204
  if (this.closedState)
@@ -205,52 +231,43 @@ function socketData(input) {
205
231
  const copy = new Uint8Array(input);
206
232
  return copy.buffer;
207
233
  }
234
+ /**
235
+ * The message `input` decoded, or undefined when its type is not the subprotocol's. A decoder
236
+ * failure is thrown as it is: the connection's protocol close names every such failure alike.
237
+ */
208
238
  function decodeFrame(codec, input) {
209
- try {
210
- const binary = typeof input === 'string' ? undefined : protocol.websocket.decodeBinary(input);
211
- if (binary !== undefined)
212
- return binary;
213
- if ((codec.binary && typeof input === 'string') ||
214
- (!codec.binary && typeof input !== 'string')) {
215
- throw new WebSocketBindingFailure('server returned a message type incompatible with the selected subprotocol.', 1003, 'Unsupported message type');
216
- }
217
- const decoded = codec.decode(input);
218
- if (isKind(decoded, 'redirect'))
219
- return protocol.decodeRedirectResponse(decoded);
220
- if (isKind(decoded, 'stale'))
221
- return protocol.decodeStale(decoded);
222
- if (isKind(decoded, 'miss'))
223
- return protocol.decodeMiss(decoded);
224
- if (isStreamFrame(decoded))
225
- return protocol.decodeStreamFrame(decoded);
226
- return protocol.decodeResponse(decoded);
227
- }
228
- catch (error) {
229
- if (error instanceof WebSocketBindingFailure)
230
- throw error;
231
- throw new ProtocolError('server returned an invalid WebSocket frame.', {
232
- cause: asError(error),
233
- });
239
+ const binary = typeof input === 'string' ? undefined : protocol.websocket.decodeBinary(input);
240
+ if (binary !== undefined)
241
+ return binary;
242
+ if ((codec.binary && typeof input === 'string') || (!codec.binary && typeof input !== 'string')) {
243
+ return undefined;
234
244
  }
245
+ const decoded = codec.decode(input);
246
+ if (isKind(decoded, 'redirect'))
247
+ return protocol.decodeRedirectResponse(decoded);
248
+ if (isKind(decoded, 'stale'))
249
+ return protocol.decodeStale(decoded);
250
+ if (isKind(decoded, 'miss'))
251
+ return protocol.decodeMiss(decoded);
252
+ if (isStreamFrame(decoded))
253
+ return protocol.decodeStreamFrame(decoded);
254
+ return protocol.decodeResponse(decoded);
235
255
  }
236
- class WebSocketBindingFailure extends ProtocolError {
237
- closeCode;
238
- closeReason;
239
- constructor(message, closeCode, closeReason) {
240
- super(message);
241
- this.closeCode = closeCode;
242
- this.closeReason = closeReason;
243
- }
256
+ /** The close a failure the connection did not judge itself ends it with: a protocol breach. */
257
+ function protocolClose(input) {
258
+ const message = input instanceof ProtocolError ? input.message : 'server returned an invalid WebSocket frame.';
259
+ return { code: 1002, reason: 'Protocol error', error: new ProtocolError(message) };
244
260
  }
245
- function bindingFailure(input) {
246
- if (input instanceof WebSocketBindingFailure)
247
- return input;
248
- const error = input instanceof ProtocolError
249
- ? input
250
- : new ProtocolError('server returned an invalid WebSocket frame.', {
251
- cause: asError(input),
252
- });
253
- return new WebSocketBindingFailure(error.message, 1002, 'Protocol error');
261
+ /** The close a cancellation the connection could not send ends it with. */
262
+ function cancelClose(code, reason, cause) {
263
+ return {
264
+ code,
265
+ reason,
266
+ error: invocationFailure('Failed to send WebSocket cancellation.', 'send', {
267
+ cause: asError(cause),
268
+ delivery: 'unknown',
269
+ }),
270
+ };
254
271
  }
255
272
  function closeReason(code) {
256
273
  if (code === 1008)
@@ -268,12 +285,6 @@ function isStreamFrame(input) {
268
285
  : undefined;
269
286
  return typeof kind === 'string' && kind.startsWith('stream.');
270
287
  }
271
- function isKind(input, kind) {
272
- return (typeof input === 'object' &&
273
- input !== null &&
274
- !Array.isArray(input) &&
275
- input.kind === kind);
276
- }
277
288
  function requestKey(requestId) {
278
289
  return requestId === null ? 'null:' : `${typeof requestId}:${String(requestId)}`;
279
290
  }
@@ -1,15 +1,16 @@
1
- import type { Value } from '@astrale-os/kernel-dsl/value';
2
1
  import type * as protocol from '@astrale-os/kernel-protocol/invocation';
3
- import type { Stream } from '../../client/index.js';
4
2
  import type { ControlledSignal } from '../index.js';
5
3
  import type { TransportResult } from '../index.js';
4
+ /** The close code of an exchange's failure: a protocol or policy breach, or a failed send. */
5
+ export type ExchangeClose = 1002 | 1008 | 1011;
6
6
  export interface ExchangeOwner {
7
- finish(exchange: WebSocketExchange, reusable: boolean, closeCode?: 1002 | 1008 | 1011): void;
7
+ /** Frees the exchange's slot; `close` ends the connection, which is otherwise reusable. */
8
+ finish(exchange: WebSocketExchange, close?: ExchangeClose): void;
8
9
  cancel(exchange: WebSocketExchange, requestId: protocol.RequestId): void;
9
10
  }
10
11
  export type WebSocketInbound = protocol.BinaryResponse | protocol.RedirectResponse | protocol.Stale | protocol.Miss | protocol.Response | protocol.StreamFrame;
11
12
  /** One correlated invocation within a multiplexed WebSocket connection. */
12
- export declare class WebSocketExchange implements Stream {
13
+ export declare class WebSocketExchange {
13
14
  private readonly owner;
14
15
  private readonly requestId;
15
16
  private readonly control;
@@ -18,29 +19,21 @@ export declare class WebSocketExchange implements Stream {
18
19
  private resolveResult;
19
20
  private rejectResult;
20
21
  private readonly abort;
21
- private state;
22
- private claimed;
23
- private index;
24
- private readonly values;
25
- private readonly waiters;
26
- private terminal;
27
- private sent;
28
- private invocation?;
22
+ private phase;
29
23
  constructor(owner: ExchangeOwner, requestId: protocol.RequestId, control: ControlledSignal, maximumBufferedItems: number);
30
24
  start(send: (data: string | Uint8Array) => void, encoded: string | Uint8Array): Promise<TransportResult>;
31
25
  receive(input: WebSocketInbound): void;
32
26
  connectionFailed(error: Error): void;
33
- [Symbol.asyncIterator](): AsyncIterator<Value>;
34
- cancel(_reason?: string): Promise<void>;
27
+ private get invocation();
35
28
  private receiveResponse;
36
29
  private receiveStreamFrame;
37
- private next;
30
+ /** The consumer's cancellation: drops what it has not read and tells the server. */
31
+ private cancel;
38
32
  private complete;
39
33
  private reject;
34
+ /** Fails the pending result or the stream; a cancelled exchange drops what was not read. */
40
35
  private fail;
41
- private cancelLocally;
42
36
  private finish;
43
- private finishCancelled;
44
- private flushTerminal;
45
- private abortFailure;
46
37
  }
38
+ /** Whether `input` is a plain object whose `kind` is `kind`. */
39
+ export declare function isKind(input: unknown, kind: string): boolean;