@astrale-os/kernel-client 1.0.0-beta.45 → 1.0.0-beta.46

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 (37) hide show
  1. package/dist/client/client.d.ts +51 -2
  2. package/dist/client/client.js +158 -13
  3. package/dist/client/level.d.ts +58 -0
  4. package/dist/client/level.js +159 -0
  5. package/dist/client/result.d.ts +4 -0
  6. package/dist/client/result.js +10 -5
  7. package/dist/errors/index.d.ts +1 -0
  8. package/dist/errors/index.js +1 -0
  9. package/dist/errors/inexpressible-error.d.ts +23 -0
  10. package/dist/errors/inexpressible-error.js +26 -0
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/transport/auto/decision.d.ts +2 -0
  14. package/dist/transport/auto/decision.js +4 -0
  15. package/dist/transport/auto/transport.d.ts +10 -1
  16. package/dist/transport/auto/transport.js +17 -0
  17. package/dist/transport/http/admission.js +3 -3
  18. package/dist/transport/http/configuration.d.ts +7 -0
  19. package/dist/transport/http/configuration.js +2 -0
  20. package/dist/transport/http/response.d.ts +10 -0
  21. package/dist/transport/http/response.js +15 -0
  22. package/dist/transport/http/transport.d.ts +4 -1
  23. package/dist/transport/http/transport.js +25 -5
  24. package/dist/transport/index.d.ts +1 -0
  25. package/dist/transport/index.js +1 -0
  26. package/dist/transport/level.d.ts +42 -0
  27. package/dist/transport/level.js +58 -0
  28. package/dist/transport/transport.d.ts +33 -0
  29. package/dist/transport/unary.d.ts +2 -1
  30. package/dist/transport/unary.js +4 -2
  31. package/dist/transport/websocket/connection.d.ts +6 -1
  32. package/dist/transport/websocket/connection.js +21 -5
  33. package/dist/transport/websocket/exchange.d.ts +3 -0
  34. package/dist/transport/websocket/exchange.js +6 -4
  35. package/dist/transport/websocket/transport.d.ts +19 -1
  36. package/dist/transport/websocket/transport.js +48 -11
  37. package/package.json +5 -5
@@ -1,3 +1,5 @@
1
+ import type { Level } from '@astrale-os/kernel-core/wire';
2
+ import * as protocol from '@astrale-os/kernel-protocol/invocation';
1
3
  import type { RequestOptions, Transport, TransportRequest, TransportResult } from '../index.js';
2
4
  /** Web-compatible WebSocket constructor used by the built-in WebSocket transport. */
3
5
  export type WebSocketConstructor = new (url: string | URL, protocols?: string | string[]) => WebSocket;
@@ -16,18 +18,24 @@ export interface WebSocketTransportOptions {
16
18
  readonly maximumActiveInvocations?: number;
17
19
  /** Maximum decoded items retained while a stream consumer is slower than the socket. */
18
20
  readonly maximumBufferedStreamItems?: number;
21
+ /**
22
+ * The wire History whose levels the transport offers, its current level first; absent, the
23
+ * Invocation History of this build.
24
+ */
25
+ readonly levels?: protocol.Levels;
19
26
  }
20
27
  /** WebSocket implementation of the universal invocation protocol. */
21
28
  export declare class WebSocketTransport implements Transport {
22
29
  readonly url: string;
23
30
  private readonly connectionUrl;
24
31
  private readonly WebSocketConstructor;
25
- private readonly timeoutMs;
32
+ readonly timeoutMs: number;
26
33
  private readonly maximumRequestBytes;
27
34
  private readonly maximumResponseBytes;
28
35
  private readonly maximumBufferedBytes;
29
36
  private readonly maximumActiveInvocations;
30
37
  private readonly maximumBufferedStreamItems;
38
+ private readonly levels;
31
39
  private readonly connections;
32
40
  private readonly connectionOwner;
33
41
  private connection;
@@ -35,8 +43,18 @@ export declare class WebSocketTransport implements Transport {
35
43
  private nextId;
36
44
  constructor(options: WebSocketTransportOptions);
37
45
  dispatch(input: TransportRequest, options?: RequestOptions): Promise<TransportResult>;
46
+ /**
47
+ * The wire level of the connection, which its subprotocol fixed for every message: it connects
48
+ * when no connection is open, within the deadline of `options`.
49
+ */
50
+ fixed(options?: RequestOptions): Promise<Level>;
38
51
  /** Close the multiplexed connection, reject active exchanges, and reject future dispatches. */
39
52
  close(): void;
53
+ /**
54
+ * What a call fails with when connecting, or writing to the connection, threw `error`: a failure
55
+ * to connect, which sent nothing, unless the connection and its exchange already judged it.
56
+ */
57
+ private failure;
40
58
  private connectionClosed;
41
59
  private acquire;
42
60
  private nextRequestId;
@@ -3,6 +3,7 @@ import * as protocol from '@astrale-os/kernel-protocol/invocation';
3
3
  import { TransportError } from '../../errors/index.js';
4
4
  import { invocationFailure, requestFailure } from '../failure.js';
5
5
  import { asError, controlledSignal, positiveInteger, requireLive } from '../index.js';
6
+ import { captureLevels, ConnectionMoved, writtenLevel } from '../level.js';
6
7
  import { WebSocketConnection } from './connection.js';
7
8
  const DEFAULT_TIMEOUT_MS = 30_000;
8
9
  const DEFAULT_MAXIMUM_REQUEST_BYTES = 16 * 1024 * 1024;
@@ -21,6 +22,7 @@ export class WebSocketTransport {
21
22
  maximumBufferedBytes;
22
23
  maximumActiveInvocations;
23
24
  maximumBufferedStreamItems;
25
+ levels;
24
26
  connections = new Set();
25
27
  connectionOwner = Object.freeze({
26
28
  closed: (connection) => this.connectionClosed(connection),
@@ -41,6 +43,7 @@ export class WebSocketTransport {
41
43
  this.maximumBufferedBytes = positiveInteger(options.maximumBufferedBytes ?? DEFAULT_MAXIMUM_BUFFERED_BYTES, 'maximumBufferedBytes');
42
44
  this.maximumActiveInvocations = positiveInteger(options.maximumActiveInvocations ?? DEFAULT_MAXIMUM_ACTIVE_INVOCATIONS, 'maximumActiveInvocations');
43
45
  this.maximumBufferedStreamItems = positiveInteger(options.maximumBufferedStreamItems ?? DEFAULT_MAXIMUM_BUFFERED_STREAM_ITEMS, 'maximumBufferedStreamItems');
46
+ this.levels = captureLevels(options.levels);
44
47
  }
45
48
  async dispatch(input, options = {}) {
46
49
  if (this.stopped) {
@@ -48,10 +51,12 @@ export class WebSocketTransport {
48
51
  }
49
52
  const timeoutMs = positiveInteger(options.timeoutMs ?? this.timeoutMs, 'timeoutMs');
50
53
  const codec = codecs.json;
54
+ const { level, ...invocationInput } = input;
55
+ const written = writtenLevel(level, this.levels);
51
56
  const request = protocol.decodeRequest({
52
57
  version: protocol.VERSION,
53
58
  requestId: this.nextRequestId(),
54
- ...input,
59
+ ...invocationInput,
55
60
  ...(options.idempotencyKey === undefined ? {} : { idempotencyKey: options.idempotencyKey }),
56
61
  });
57
62
  const control = controlledSignal(options.signal, timeoutMs);
@@ -60,25 +65,42 @@ export class WebSocketTransport {
60
65
  requireLive(control);
61
66
  const connection = await this.acquire(control, codec);
62
67
  requireLive(control);
68
+ // The subprotocol fixed the level of every message. The request was written for the level
69
+ // this Transport told: a connection made again at another level does not carry it.
70
+ if (connection.level !== written)
71
+ throw new ConnectionMoved(connection.level);
63
72
  owned = true;
64
73
  return await connection.dispatch(request, control, this.maximumBufferedStreamItems);
65
74
  }
66
75
  catch (error) {
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
- });
76
+ throw this.failure(error, control, timeoutMs);
76
77
  }
77
78
  finally {
78
79
  if (!owned)
79
80
  control.close();
80
81
  }
81
82
  }
83
+ /**
84
+ * The wire level of the connection, which its subprotocol fixed for every message: it connects
85
+ * when no connection is open, within the deadline of `options`.
86
+ */
87
+ async fixed(options = {}) {
88
+ if (this.stopped) {
89
+ throw invocationFailure('WebSocketTransport is closed.', 'closed');
90
+ }
91
+ const timeoutMs = positiveInteger(options.timeoutMs ?? this.timeoutMs, 'timeoutMs');
92
+ const control = controlledSignal(options.signal, timeoutMs);
93
+ try {
94
+ requireLive(control);
95
+ return (await this.acquire(control, codecs.json)).level;
96
+ }
97
+ catch (error) {
98
+ throw this.failure(error, control, timeoutMs);
99
+ }
100
+ finally {
101
+ control.close();
102
+ }
103
+ }
82
104
  /** Close the multiplexed connection, reject active exchanges, and reject future dispatches. */
83
105
  close() {
84
106
  if (this.stopped)
@@ -89,6 +111,21 @@ export class WebSocketTransport {
89
111
  for (const connection of this.connections)
90
112
  connection.shutdown(failure);
91
113
  }
114
+ /**
115
+ * What a call fails with when connecting, or writing to the connection, threw `error`: a failure
116
+ * to connect, which sent nothing, unless the connection and its exchange already judged it.
117
+ */
118
+ failure(error, control, timeoutMs) {
119
+ // The connection and its exchange already judged their own failures, deadline included.
120
+ if (error instanceof TransportError || error instanceof ConnectionMoved)
121
+ return error;
122
+ return requestFailure(error, control, {
123
+ phase: 'connect',
124
+ delivery: 'not-sent',
125
+ timeout: `WebSocket invocation timed out after ${timeoutMs}ms.`,
126
+ failure: `Failed to communicate with the server at ${this.url}.`,
127
+ });
128
+ }
92
129
  connectionClosed(connection) {
93
130
  this.connections.delete(connection);
94
131
  if (this.connection === connection)
@@ -97,7 +134,7 @@ export class WebSocketTransport {
97
134
  async acquire(control, codec) {
98
135
  let connection = this.connection;
99
136
  if (connection === undefined || !connection.isAvailable) {
100
- connection = new WebSocketConnection(this.connectionOwner, this.WebSocketConstructor, this.connectionUrl, codec, this.maximumRequestBytes, this.maximumResponseBytes, this.maximumBufferedBytes, this.maximumActiveInvocations);
137
+ connection = new WebSocketConnection(this.connectionOwner, this.WebSocketConstructor, this.connectionUrl, codec, this.maximumRequestBytes, this.maximumResponseBytes, this.maximumBufferedBytes, this.maximumActiveInvocations, this.levels);
101
138
  this.connection = connection;
102
139
  this.connections.add(connection);
103
140
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrale-os/kernel-client",
3
- "version": "1.0.0-beta.45",
3
+ "version": "1.0.0-beta.46",
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.42",
69
- "@astrale-os/kernel-dsl": "1.0.0-beta.30",
70
- "@astrale-os/kernel-protocol": "1.0.0-beta.42",
68
+ "@astrale-os/kernel-core": "1.0.0-beta.43",
69
+ "@astrale-os/kernel-dsl": "1.0.0-beta.31",
70
+ "@astrale-os/kernel-protocol": "1.0.0-beta.43",
71
71
  "@tanstack/pacer": "0.20.1"
72
72
  },
73
73
  "devDependencies": {
74
- "@astrale-os/kernel-server": "1.0.0-beta.42",
74
+ "@astrale-os/kernel-server": "1.0.0-beta.43",
75
75
  "@astrale/typescript-config": "npm:@jsr/astrale__typescript-config@~1.1.0",
76
76
  "@types/node": "^24.10.0",
77
77
  "esbuild": "0.28.1",