@astrale-os/kernel-client 1.0.0-beta.26 → 1.0.0-beta.27

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 (68) hide show
  1. package/dist/client/admission.js +3 -1
  2. package/dist/client/client.d.ts +16 -3
  3. package/dist/client/client.js +36 -15
  4. package/dist/client/index.d.ts +2 -2
  5. package/dist/client/index.js +1 -1
  6. package/dist/client/routing-control.d.ts +11 -15
  7. package/dist/client/routing-control.js +21 -29
  8. package/dist/errors/query-input.js +2 -1
  9. package/dist/errors/stale-route-error.d.ts +1 -1
  10. package/dist/errors/stale-route-error.js +1 -1
  11. package/dist/graph/page-contract.d.ts +6 -1
  12. package/dist/graph/query.js +9 -3
  13. package/dist/session/admission.js +4 -2
  14. package/dist/session/credential/credentials.js +32 -28
  15. package/dist/session/discovery/bundle.d.ts +9 -4
  16. package/dist/session/discovery/bundle.js +28 -35
  17. package/dist/session/discovery/discovery.d.ts +1 -1
  18. package/dist/session/discovery/discovery.js +7 -10
  19. package/dist/session/discovery/release.js +18 -28
  20. package/dist/session/discovery/transport.d.ts +35 -3
  21. package/dist/session/discovery/transport.js +40 -1
  22. package/dist/session/exchange/exchange.d.ts +3 -2
  23. package/dist/session/exchange/exchange.js +10 -7
  24. package/dist/session/exchange/index.d.ts +1 -1
  25. package/dist/session/exchange/index.js +1 -1
  26. package/dist/session/exchange/presentation.js +28 -10
  27. package/dist/session/exchange/source.d.ts +7 -7
  28. package/dist/session/exchange/source.js +26 -27
  29. package/dist/session/routing/auth.d.ts +9 -3
  30. package/dist/session/routing/auth.js +4 -3
  31. package/dist/session/routing/cache.d.ts +12 -5
  32. package/dist/session/routing/cache.js +56 -48
  33. package/dist/session/routing/dispatch.d.ts +2 -1
  34. package/dist/session/routing/dispatch.js +83 -61
  35. package/dist/session/routing/index.d.ts +1 -1
  36. package/dist/session/routing/recovery.d.ts +3 -6
  37. package/dist/session/routing/recovery.js +3 -14
  38. package/dist/session/routing/store.js +4 -5
  39. package/dist/store/memory.d.ts +1 -3
  40. package/dist/store/memory.js +61 -112
  41. package/dist/store/shape.d.ts +33 -12
  42. package/dist/store/shape.js +125 -16
  43. package/dist/store/store.js +1 -1
  44. package/dist/transport/auto/decision.d.ts +26 -14
  45. package/dist/transport/auto/decision.js +35 -32
  46. package/dist/transport/auto/fallback.d.ts +1 -1
  47. package/dist/transport/auto/fallback.js +5 -10
  48. package/dist/transport/auto/transport.d.ts +0 -2
  49. package/dist/transport/auto/transport.js +24 -32
  50. package/dist/transport/failure.d.ts +28 -1
  51. package/dist/transport/failure.js +57 -1
  52. package/dist/transport/http/admission.js +2 -2
  53. package/dist/transport/http/binary.js +1 -1
  54. package/dist/transport/http/byte-stream.d.ts +2 -1
  55. package/dist/transport/http/byte-stream.js +10 -7
  56. package/dist/transport/http/stream.d.ts +8 -13
  57. package/dist/transport/http/stream.js +32 -79
  58. package/dist/transport/http/transport.js +11 -22
  59. package/dist/transport/stream.d.ts +52 -0
  60. package/dist/transport/stream.js +101 -0
  61. package/dist/transport/unary.d.ts +3 -2
  62. package/dist/transport/unary.js +3 -3
  63. package/dist/transport/websocket/connection.d.ts +7 -3
  64. package/dist/transport/websocket/connection.js +102 -85
  65. package/dist/transport/websocket/exchange.d.ts +10 -19
  66. package/dist/transport/websocket/exchange.js +72 -153
  67. package/dist/transport/websocket/transport.js +11 -19
  68. package/package.json +5 -5
@@ -1,7 +1,8 @@
1
1
  import * as errors from '@astrale-os/kernel-protocol/errors';
2
2
  import { ProtocolError, ResponseError } from '../../errors/index.js';
3
- import { invocationFailure } from '../failure.js';
3
+ import { abortFailure, invocationFailure, withInvocation } from '../failure.js';
4
4
  import { asError } from '../index.js';
5
+ import { FrameStream } from '../stream.js';
5
6
  import { responseError, unaryResult } from '../unary.js';
6
7
  /** One correlated invocation within a multiplexed WebSocket connection. */
7
8
  export class WebSocketExchange {
@@ -13,14 +14,7 @@ export class WebSocketExchange {
13
14
  resolveResult;
14
15
  rejectResult;
15
16
  abort;
16
- state = 'response';
17
- claimed = false;
18
- index = 0;
19
- values = [];
20
- waiters = [];
21
- terminal;
22
- sent = false;
23
- invocation;
17
+ phase = { kind: 'unsent' };
24
18
  constructor(owner, requestId, control, maximumBufferedItems) {
25
19
  this.owner = owner;
26
20
  this.requestId = requestId;
@@ -30,12 +24,15 @@ export class WebSocketExchange {
30
24
  this.resolveResult = resolve;
31
25
  this.rejectResult = reject;
32
26
  });
33
- this.abort = () => this.cancelLocally(this.abortFailure());
27
+ this.abort = () => this.fail(abortFailure(control, 'WebSocket invocation exceeded its timeout.', 'WebSocket invocation was cancelled.', {
28
+ delivery: this.phase.kind === 'unsent' ? 'not-sent' : 'unknown',
29
+ invocation: this.invocation,
30
+ }), { kind: 'cancel' });
34
31
  if (!control.signal.aborted)
35
32
  control.signal.addEventListener('abort', this.abort, { once: true });
36
33
  }
37
34
  start(send, encoded) {
38
- if (this.state === 'finished')
35
+ if (this.phase.kind === 'finished')
39
36
  return this.result;
40
37
  if (this.control.signal.aborted) {
41
38
  this.abort();
@@ -43,63 +40,44 @@ export class WebSocketExchange {
43
40
  }
44
41
  try {
45
42
  send(encoded);
46
- this.sent = true;
43
+ // A socket may fail the exchange while it sends.
44
+ if (this.phase.kind === 'unsent')
45
+ this.phase = { kind: 'sent' };
47
46
  }
48
47
  catch (error) {
49
48
  this.fail(invocationFailure('Failed to send the WebSocket invocation.', 'send', {
50
49
  cause: asError(error),
51
- }), 1011);
50
+ }), { kind: 'close', code: 1011 });
52
51
  }
53
52
  return this.result;
54
53
  }
55
54
  receive(input) {
56
55
  try {
57
- if (this.state === 'response') {
56
+ const phase = this.phase;
57
+ if (phase.kind === 'unsent' || phase.kind === 'sent') {
58
58
  this.receiveResponse(input);
59
59
  return;
60
60
  }
61
- if (this.state !== 'stream' || !isStreamFrame(input)) {
61
+ if (phase.kind !== 'stream' || !isStreamFrame(input)) {
62
62
  throw new ProtocolError('server sent an unexpected WebSocket response.');
63
63
  }
64
- this.receiveStreamFrame(input);
64
+ this.receiveStreamFrame(phase.stream, input);
65
65
  }
66
66
  catch (error) {
67
- const failure = protocolFailure(error, this.invocation);
68
- this.fail(failure, failure instanceof WebSocketPolicyError ? 1008 : 1002);
67
+ this.fail(protocolFailure(error, this.invocation), { kind: 'close', code: 1002 });
69
68
  }
70
69
  }
71
70
  connectionFailed(error) {
72
- this.fail(this.sent
73
- ? invocationFailure(error.message, 'receive', {
71
+ this.fail(this.phase.kind === 'unsent'
72
+ ? error
73
+ : invocationFailure(error.message, 'receive', {
74
74
  cause: error,
75
75
  delivery: 'unknown',
76
76
  ...(this.invocation === undefined ? {} : { invocation: this.invocation }),
77
- })
78
- : error);
77
+ }), { kind: 'close', code: 1002 });
79
78
  }
80
- [Symbol.asyncIterator]() {
81
- if (this.claimed)
82
- throw new TypeError('streams admit exactly one consumer.');
83
- this.claimed = true;
84
- return {
85
- next: () => this.next(),
86
- return: async () => {
87
- await this.cancel('stream consumer stopped');
88
- return { done: true, value: undefined };
89
- },
90
- throw: async (error) => {
91
- await this.cancel('stream consumer failed');
92
- throw error;
93
- },
94
- };
95
- }
96
- async cancel(_reason = 'stream cancelled') {
97
- if (this.state === 'finished')
98
- return;
99
- this.values.length = 0;
100
- this.terminal = true;
101
- this.finishCancelled();
102
- this.flushTerminal();
79
+ get invocation() {
80
+ return this.phase.kind === 'stream' ? this.phase.stream.invocation : undefined;
103
81
  }
104
82
  receiveResponse(input) {
105
83
  if (isKind(input, 'binary')) {
@@ -121,149 +99,90 @@ export class WebSocketExchange {
121
99
  if (input.kind !== 'stream.start') {
122
100
  throw new ProtocolError('stream did not begin with stream.start.');
123
101
  }
124
- this.state = 'stream';
125
- this.invocation = input.invocation;
126
- this.resolveResult({ kind: 'stream', stream: this, invocation: input.invocation });
102
+ const stream = new FrameStream(input.invocation, { cancel: async () => this.cancel() }, this.maximumBufferedItems);
103
+ this.phase = { kind: 'stream', stream };
104
+ this.resolveResult({ kind: 'stream', stream, invocation: input.invocation });
127
105
  return;
128
106
  }
129
107
  const response = input;
130
108
  requireId(this.requestId, response.requestId);
131
109
  if ('error' in response)
132
- this.reject(responseError(response));
110
+ this.reject(responseError(response.error, response.invocation));
133
111
  else
134
112
  this.complete(unaryResult(response));
135
113
  }
136
- receiveStreamFrame(frame) {
114
+ receiveStreamFrame(stream, frame) {
137
115
  requireId(this.requestId, frame.requestId);
138
116
  if (frame.kind === 'stream.start') {
139
117
  throw new ProtocolError('server sent a duplicate stream.start frame.');
140
118
  }
141
- if (frame.kind === 'stream.item') {
142
- if (frame.index !== this.index) {
143
- throw new ProtocolError(`stream item ${frame.index} does not match expected index ${this.index}.`);
144
- }
145
- this.index += 1;
146
- const waiter = this.waiters.shift();
147
- if (waiter !== undefined)
148
- waiter.resolve({ done: false, value: frame.value });
149
- else {
150
- if (this.values.length >= this.maximumBufferedItems) {
151
- throw new WebSocketPolicyError(`stream exceeds maximumBufferedStreamItems (${this.maximumBufferedItems}).`);
152
- }
153
- this.values.push(frame.value);
119
+ const step = stream.accept(frame);
120
+ if (step.kind === 'item') {
121
+ if (!stream.push(step.value)) {
122
+ const overflow = new ProtocolError(`stream exceeds maximumBufferedStreamItems (${this.maximumBufferedItems}).`);
123
+ this.fail(protocolFailure(overflow, this.invocation), { kind: 'close', code: 1008 });
154
124
  }
155
125
  return;
156
126
  }
157
- if (frame.kind === 'stream.end' && frame.count !== this.index) {
158
- throw new ProtocolError(`stream ended with count ${frame.count}; ${this.index} items were received.`);
159
- }
160
- this.terminal =
161
- frame.kind === 'stream.error'
162
- ? new ResponseError(frame.error.code, frame.error.message, this.invocation, frame.error.data)
163
- : true;
164
- this.finish(true);
165
- this.flushTerminal();
127
+ this.finish({ kind: 'reuse' });
128
+ if (step.kind === 'error')
129
+ stream.fail(step.error);
130
+ else
131
+ stream.end();
166
132
  }
167
- next() {
168
- const value = this.values.shift();
169
- if (value !== undefined)
170
- return Promise.resolve({ done: false, value });
171
- if (this.terminal === true)
172
- return Promise.resolve({ done: true, value: undefined });
173
- if (this.terminal instanceof Error)
174
- return Promise.reject(this.terminal);
175
- return new Promise((resolve, reject) => this.waiters.push({ resolve, reject }));
133
+ /** The consumer's cancellation: drops what it has not read and tells the server. */
134
+ cancel() {
135
+ const phase = this.phase;
136
+ if (phase.kind !== 'stream')
137
+ return;
138
+ phase.stream.discard();
139
+ this.finish({ kind: 'cancel' });
140
+ phase.stream.end();
176
141
  }
177
142
  complete(result) {
178
- this.finish(true);
143
+ this.finish({ kind: 'reuse' });
179
144
  this.resolveResult(result);
180
145
  }
181
146
  reject(error) {
182
- this.finish(true);
147
+ this.finish({ kind: 'reuse' });
183
148
  this.rejectResult(error);
184
149
  }
185
- fail(error, closeCode = 1002) {
186
- if (this.state === 'finished')
150
+ /** Fails the pending result or the stream; a cancelled exchange drops what was not read. */
151
+ fail(error, release) {
152
+ const phase = this.phase;
153
+ if (phase.kind === 'finished')
187
154
  return;
188
- if (this.state === 'response')
155
+ if (phase.kind !== 'stream')
189
156
  this.rejectResult(error);
190
157
  else {
191
- this.terminal = error;
192
- this.flushTerminal();
158
+ if (release.kind === 'cancel')
159
+ phase.stream.discard();
160
+ phase.stream.fail(error);
193
161
  }
194
- this.finish(false, closeCode);
195
- }
196
- cancelLocally(error) {
197
- if (this.state === 'finished')
198
- return;
199
- if (this.state === 'response')
200
- this.rejectResult(error);
201
- else {
202
- this.values.length = 0;
203
- this.terminal = error;
204
- this.flushTerminal();
205
- }
206
- this.finishCancelled();
207
- }
208
- finish(reusable, closeCode) {
209
- if (this.state === 'finished')
210
- return;
211
- this.state = 'finished';
212
- this.control.signal.removeEventListener('abort', this.abort);
213
- this.control.close();
214
- this.owner.finish(this, reusable, closeCode);
162
+ this.finish(release);
215
163
  }
216
- finishCancelled() {
217
- if (this.state === 'finished')
164
+ finish(release) {
165
+ if (this.phase.kind === 'finished')
218
166
  return;
219
- this.state = 'finished';
167
+ this.phase = { kind: 'finished' };
220
168
  this.control.signal.removeEventListener('abort', this.abort);
221
169
  this.control.close();
222
- this.owner.cancel(this, this.requestId);
223
- }
224
- flushTerminal() {
225
- if (this.terminal === undefined)
226
- return;
227
- for (const waiter of this.waiters.splice(0)) {
228
- if (this.terminal === true)
229
- waiter.resolve({ done: true, value: undefined });
230
- else
231
- waiter.reject(this.terminal);
232
- }
233
- }
234
- abortFailure() {
235
- if (this.control.timedOut) {
236
- return invocationFailure('WebSocket invocation exceeded its timeout.', 'timeout', {
237
- cause: asError(this.control.signal.reason),
238
- delivery: this.sent ? 'unknown' : 'not-sent',
239
- ...(this.invocation === undefined ? {} : { invocation: this.invocation }),
240
- });
241
- }
242
- return asError(this.control.signal.reason ?? new Error('WebSocket invocation was cancelled.'));
170
+ if (release.kind === 'cancel')
171
+ this.owner.cancel(this, this.requestId);
172
+ else if (release.kind === 'close')
173
+ this.owner.finish(this, release.code);
174
+ else
175
+ this.owner.finish(this);
243
176
  }
244
177
  }
245
- class WebSocketPolicyError extends ProtocolError {
246
- }
178
+ /** The error of a frame the exchange cannot accept, naming the invocation it belongs to. */
247
179
  function protocolFailure(input, invocation) {
248
- if (input instanceof ResponseError)
249
- return input;
250
- if (input instanceof WebSocketPolicyError) {
251
- return input.invocation !== undefined || invocation === undefined
252
- ? input
253
- : new WebSocketPolicyError(input.message, { cause: input, invocation });
254
- }
255
- if (input instanceof ProtocolError) {
256
- return input.invocation !== undefined || invocation === undefined
257
- ? input
258
- : new ProtocolError(input.message, { cause: input, invocation });
259
- }
260
- if (input instanceof errors.ProtocolError) {
261
- return new ProtocolError(input.message, { cause: input, invocation });
180
+ if (input instanceof ResponseError || input instanceof ProtocolError) {
181
+ return withInvocation(input, invocation);
262
182
  }
263
- return new ProtocolError('server returned an invalid WebSocket frame.', {
264
- cause: asError(input),
265
- invocation,
266
- });
183
+ return new ProtocolError(input instanceof errors.ProtocolError
184
+ ? input.message
185
+ : 'server returned an invalid WebSocket frame.', { cause: asError(input), invocation });
267
186
  }
268
187
  function requireId(expected, received) {
269
188
  if (received !== expected) {
@@ -1,7 +1,7 @@
1
1
  import * as codecs from '@astrale-os/kernel-protocol/codecs';
2
2
  import * as protocol from '@astrale-os/kernel-protocol/invocation';
3
- import { ProtocolError, ResponseError, TransportError } from '../../errors/index.js';
4
- import { invocationFailure } from '../failure.js';
3
+ import { TransportError } from '../../errors/index.js';
4
+ import { invocationFailure, requestFailure } from '../failure.js';
5
5
  import { asError, controlledSignal, positiveInteger, requireLive } from '../index.js';
6
6
  import { WebSocketConnection } from './connection.js';
7
7
  const DEFAULT_TIMEOUT_MS = 30_000;
@@ -64,7 +64,15 @@ export class WebSocketTransport {
64
64
  return await connection.dispatch(request, control, this.maximumBufferedStreamItems);
65
65
  }
66
66
  catch (error) {
67
- throw transportFailure(error, control, options.signal, timeoutMs, this.url);
67
+ // The connection and its exchange already judged their own failures, deadline included.
68
+ if (error instanceof TransportError)
69
+ throw error;
70
+ throw requestFailure(error, control, {
71
+ phase: 'connect',
72
+ delivery: 'not-sent',
73
+ timeout: `WebSocket invocation timed out after ${timeoutMs}ms.`,
74
+ failure: `Failed to communicate with the server at ${this.url}.`,
75
+ });
68
76
  }
69
77
  finally {
70
78
  if (!owned)
@@ -131,19 +139,3 @@ function admitWebSocket(input) {
131
139
  }
132
140
  return implementation;
133
141
  }
134
- function transportFailure(input, control, parent, timeoutMs, url) {
135
- if (input instanceof ResponseError || input instanceof ProtocolError)
136
- return input;
137
- if (input instanceof TransportError)
138
- return input;
139
- if (control.timedOut) {
140
- return invocationFailure(`WebSocket invocation timed out after ${timeoutMs}ms.`, 'timeout', {
141
- cause: asError(input),
142
- });
143
- }
144
- if (parent?.aborted)
145
- return asError(parent.reason ?? input);
146
- return invocationFailure(`Failed to communicate with the server at ${url}.`, 'connect', {
147
- cause: asError(input),
148
- });
149
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrale-os/kernel-client",
3
- "version": "1.0.0-beta.26",
3
+ "version": "1.0.0-beta.27",
4
4
  "description": "Universal Client — protocol-first, transport-agnostic",
5
5
  "keywords": [
6
6
  "astrale",
@@ -65,13 +65,13 @@
65
65
  "access": "public"
66
66
  },
67
67
  "dependencies": {
68
- "@astrale-os/kernel-core": "1.0.0-beta.23",
69
- "@astrale-os/kernel-dsl": "1.0.0-beta.16",
70
- "@astrale-os/kernel-protocol": "1.0.0-beta.23",
68
+ "@astrale-os/kernel-core": "1.0.0-beta.24",
69
+ "@astrale-os/kernel-dsl": "1.0.0-beta.17",
70
+ "@astrale-os/kernel-protocol": "1.0.0-beta.24",
71
71
  "@tanstack/pacer": "0.20.1"
72
72
  },
73
73
  "devDependencies": {
74
- "@astrale-os/kernel-server": "1.0.0-beta.23",
74
+ "@astrale-os/kernel-server": "1.0.0-beta.24",
75
75
  "@astrale/typescript-config": "npm:@jsr/astrale__typescript-config@~1.1.0",
76
76
  "@types/node": "^22.0.0",
77
77
  "esbuild": "0.28.1",