@replit/river 0.11.0 → 0.12.1

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 (82) hide show
  1. package/README.md +28 -8
  2. package/dist/{builder-1f26296b.d.ts → builder-c593de11.d.ts} +14 -13
  3. package/dist/{chunk-3JGVFWKQ.js → chunk-55XUAPC6.js} +205 -180
  4. package/dist/{chunk-R6H2BIMC.js → chunk-GZ7HCLLM.js} +31 -7
  5. package/dist/{chunk-T7M7OKPE.js → chunk-H4BYJELI.js} +5 -1
  6. package/dist/{chunk-GKPT5YQE.js → chunk-IIBVKYDB.js} +6 -34
  7. package/dist/chunk-M6LY25P2.js +47 -0
  8. package/dist/chunk-QEYN2Z6O.js +726 -0
  9. package/dist/chunk-RDTTKCGV.js +40 -0
  10. package/dist/chunk-TKINU53F.js +44 -0
  11. package/dist/chunk-XFFS4UOD.js +127 -0
  12. package/dist/codec/index.cjs +13 -7
  13. package/dist/codec/index.js +2 -4
  14. package/dist/connection-bf7811aa.d.ts +17 -0
  15. package/dist/connection-d880aa4a.d.ts +18 -0
  16. package/dist/connection-eb10d250.d.ts +15 -0
  17. package/dist/index-0c0a69f6.d.ts +440 -0
  18. package/dist/logging/index.cjs +8 -3
  19. package/dist/logging/index.d.cts +6 -1
  20. package/dist/logging/index.d.ts +6 -1
  21. package/dist/logging/index.js +5 -3
  22. package/dist/messageFraming-b200ef25.d.ts +20 -0
  23. package/dist/router/index.cjs +250 -211
  24. package/dist/router/index.d.cts +6 -7
  25. package/dist/router/index.d.ts +6 -7
  26. package/dist/router/index.js +3 -3
  27. package/dist/transport/impls/stdio/client.cjs +909 -0
  28. package/dist/transport/impls/stdio/client.d.cts +27 -0
  29. package/dist/transport/impls/stdio/client.d.ts +27 -0
  30. package/dist/transport/impls/stdio/client.js +42 -0
  31. package/dist/transport/impls/stdio/server.cjs +883 -0
  32. package/dist/transport/impls/stdio/server.d.cts +25 -0
  33. package/dist/transport/impls/stdio/server.d.ts +25 -0
  34. package/dist/transport/impls/stdio/server.js +33 -0
  35. package/dist/transport/impls/uds/client.cjs +911 -0
  36. package/dist/transport/impls/uds/client.d.cts +16 -0
  37. package/dist/transport/impls/uds/client.d.ts +16 -0
  38. package/dist/transport/impls/uds/client.js +44 -0
  39. package/dist/transport/impls/uds/server.cjs +885 -0
  40. package/dist/transport/impls/uds/server.d.cts +16 -0
  41. package/dist/transport/impls/uds/server.d.ts +16 -0
  42. package/dist/transport/impls/uds/server.js +39 -0
  43. package/dist/transport/impls/ws/client.cjs +612 -249
  44. package/dist/transport/impls/ws/client.d.cts +6 -21
  45. package/dist/transport/impls/ws/client.d.ts +6 -21
  46. package/dist/transport/impls/ws/client.js +83 -7
  47. package/dist/transport/impls/ws/server.cjs +565 -196
  48. package/dist/transport/impls/ws/server.d.cts +6 -10
  49. package/dist/transport/impls/ws/server.d.ts +6 -10
  50. package/dist/transport/impls/ws/server.js +31 -8
  51. package/dist/transport/index.cjs +673 -130
  52. package/dist/transport/index.d.cts +3 -276
  53. package/dist/transport/index.d.ts +3 -276
  54. package/dist/transport/index.js +13 -10
  55. package/dist/util/testHelpers.cjs +40 -602
  56. package/dist/util/testHelpers.d.cts +18 -37
  57. package/dist/util/testHelpers.d.ts +18 -37
  58. package/dist/util/testHelpers.js +27 -47
  59. package/package.json +29 -14
  60. package/dist/chunk-5IC5XMWK.js +0 -140
  61. package/dist/chunk-L7D75G4K.js +0 -29
  62. package/dist/chunk-LQXPKF3A.js +0 -282
  63. package/dist/chunk-PJ2EUO7O.js +0 -63
  64. package/dist/chunk-WVT5QXMZ.js +0 -20
  65. package/dist/chunk-ZE4MX7DF.js +0 -75
  66. package/dist/connection-2529fc14.d.ts +0 -10
  67. package/dist/connection-316d6e3a.d.ts +0 -10
  68. package/dist/connection-8e19874c.d.ts +0 -11
  69. package/dist/connection-f7688cc1.d.ts +0 -11
  70. package/dist/transport/impls/stdio/stdio.cjs +0 -518
  71. package/dist/transport/impls/stdio/stdio.d.cts +0 -26
  72. package/dist/transport/impls/stdio/stdio.d.ts +0 -26
  73. package/dist/transport/impls/stdio/stdio.js +0 -70
  74. package/dist/transport/impls/unixsocket/client.cjs +0 -516
  75. package/dist/transport/impls/unixsocket/client.d.cts +0 -16
  76. package/dist/transport/impls/unixsocket/client.d.ts +0 -16
  77. package/dist/transport/impls/unixsocket/client.js +0 -67
  78. package/dist/transport/impls/unixsocket/server.cjs +0 -520
  79. package/dist/transport/impls/unixsocket/server.d.cts +0 -18
  80. package/dist/transport/impls/unixsocket/server.d.ts +0 -18
  81. package/dist/transport/impls/unixsocket/server.js +0 -73
  82. /package/dist/{chunk-ORAG7IAU.js → chunk-5IZ2UHWV.js} +0 -0
@@ -34,31 +34,62 @@ var TransportMessageSchema = (t) => import_typebox.Type.Object({
34
34
  id: import_typebox.Type.String(),
35
35
  from: import_typebox.Type.String(),
36
36
  to: import_typebox.Type.String(),
37
+ seq: import_typebox.Type.Integer(),
38
+ ack: import_typebox.Type.Integer(),
37
39
  serviceName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
38
40
  procedureName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
39
41
  streamId: import_typebox.Type.String(),
40
42
  controlFlags: import_typebox.Type.Integer(),
41
43
  payload: t
42
44
  });
43
- var TransportAckSchema = TransportMessageSchema(
44
- import_typebox.Type.Object({
45
- ack: import_typebox.Type.String()
46
- })
47
- );
48
- var ControlMessagePayloadSchema = import_typebox.Type.Object({
45
+ var ControlMessageAckSchema = import_typebox.Type.Object({
46
+ type: import_typebox.Type.Literal("ACK")
47
+ });
48
+ var ControlMessageCloseSchema = import_typebox.Type.Object({
49
49
  type: import_typebox.Type.Literal("CLOSE")
50
50
  });
51
+ var PROTOCOL_VERSION = "v1";
52
+ var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
53
+ type: import_typebox.Type.Literal("HANDSHAKE_REQ"),
54
+ protocolVersion: import_typebox.Type.Literal(PROTOCOL_VERSION),
55
+ instanceId: import_typebox.Type.String()
56
+ });
57
+ var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
58
+ type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
59
+ status: import_typebox.Type.Union([
60
+ import_typebox.Type.Object({
61
+ ok: import_typebox.Type.Literal(true),
62
+ instanceId: import_typebox.Type.String()
63
+ }),
64
+ import_typebox.Type.Object({
65
+ ok: import_typebox.Type.Literal(false),
66
+ reason: import_typebox.Type.Union([import_typebox.Type.Literal("VERSION_MISMATCH")])
67
+ })
68
+ ])
69
+ });
70
+ var ControlMessagePayloadSchema = import_typebox.Type.Union([
71
+ ControlMessageCloseSchema,
72
+ ControlMessageAckSchema,
73
+ ControlMessageHandshakeRequestSchema,
74
+ ControlMessageHandshakeResponseSchema
75
+ ]);
51
76
  var OpaqueTransportMessageSchema = TransportMessageSchema(
52
77
  import_typebox.Type.Unknown()
53
78
  );
54
- function reply(msg, response) {
79
+ function bootRequestMessage(from, to, instanceId) {
55
80
  return {
56
81
  id: (0, import_nanoid.nanoid)(),
57
- streamId: msg.streamId,
82
+ from,
83
+ to,
84
+ seq: 0,
85
+ ack: 0,
86
+ streamId: (0, import_nanoid.nanoid)(),
58
87
  controlFlags: 0,
59
- to: msg.from,
60
- from: msg.to,
61
- payload: response
88
+ payload: {
89
+ type: "HANDSHAKE_REQ",
90
+ protocolVersion: PROTOCOL_VERSION,
91
+ instanceId
92
+ }
62
93
  };
63
94
  }
64
95
  function isAck(controlFlag) {
@@ -96,16 +127,290 @@ var EventDispatcher = class {
96
127
  }
97
128
  };
98
129
 
99
- // transport/transport.ts
130
+ // transport/session.ts
131
+ var import_nanoid2 = require("nanoid");
132
+ var nanoid2 = (0, import_nanoid2.customAlphabet)("1234567890abcdefghijklmnopqrstuvxyz", 6);
133
+ var unsafeId = () => nanoid2();
100
134
  var Connection = class {
101
- connectedTo;
102
- transport;
103
- constructor(transport, connectedTo) {
104
- this.connectedTo = connectedTo;
105
- this.transport = transport;
135
+ debugId;
136
+ constructor() {
137
+ this.debugId = `conn-${unsafeId()}`;
138
+ }
139
+ };
140
+ var HEARTBEAT_INTERVAL_MS = 1e3;
141
+ var HEARTBEATS_TILL_DEAD = 2;
142
+ var SESSION_DISCONNECT_GRACE_MS = 5e3;
143
+ var Session = class {
144
+ codec;
145
+ /**
146
+ * The buffer of messages that have been sent but not yet acknowledged.
147
+ */
148
+ sendBuffer = [];
149
+ /**
150
+ * The active connection associated with this session
151
+ */
152
+ connection;
153
+ from;
154
+ to;
155
+ /**
156
+ * The unique ID of this session.
157
+ */
158
+ debugId;
159
+ /**
160
+ * Number of messages we've sent along this session (excluding handshake)
161
+ */
162
+ seq = 0;
163
+ /**
164
+ * Number of unique messages we've received this session (excluding handshake)
165
+ */
166
+ ack = 0;
167
+ /**
168
+ * The grace period between when the inner connection is disconnected
169
+ * and when we should consider the entire session disconnected.
170
+ */
171
+ disconnectionGrace;
172
+ /**
173
+ * Number of heartbeats we've sent without a response.
174
+ */
175
+ heartbeatMisses;
176
+ /**
177
+ * The interval for sending heartbeats.
178
+ */
179
+ heartbeat;
180
+ constructor(codec, from, connectedTo, conn) {
181
+ this.debugId = `sess-${unsafeId()}`;
182
+ this.from = from;
183
+ this.to = connectedTo;
184
+ this.connection = conn;
185
+ this.codec = codec;
186
+ this.heartbeatMisses = 0;
187
+ this.heartbeat = setInterval(
188
+ () => this.sendHeartbeat(),
189
+ HEARTBEAT_INTERVAL_MS
190
+ );
191
+ }
192
+ /**
193
+ * Sends a message over the session's connection.
194
+ * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
195
+ *
196
+ * @param msg The partial message to be sent, which will be constructed into a full message.
197
+ * @param skipRetry Optional. If true, the message will not be buffered for retry on failure. This should only be used for
198
+ * ack hearbeats, which contain information that can already be found in the other buffered messages.
199
+ * @returns The full transport ID of the message that was attempted to be sent.
200
+ */
201
+ send(msg, skipRetry) {
202
+ const fullMsg = this.constructMsg(msg);
203
+ log?.debug(`${this.from} -- sending ${JSON.stringify(fullMsg)}`);
204
+ if (this.connection) {
205
+ const ok = this.connection.send(this.codec.toBuffer(fullMsg));
206
+ if (ok)
207
+ return fullMsg.id;
208
+ log?.info(
209
+ `${this.from} -- failed to send ${fullMsg.id} to ${fullMsg.to}, connection (id: ${this.connection.debugId}) is probably dead`
210
+ );
211
+ } else {
212
+ log?.info(
213
+ `${this.from} -- failed to send ${fullMsg.id} to ${fullMsg.to}, connection not ready yet`
214
+ );
215
+ }
216
+ if (skipRetry)
217
+ return fullMsg.id;
218
+ this.addToSendBuff(fullMsg);
219
+ log?.info(
220
+ `${this.from} -- buffering msg ${fullMsg.id} until connection is healthy again`
221
+ );
222
+ return fullMsg.id;
223
+ }
224
+ sendHeartbeat() {
225
+ if (this.heartbeatMisses >= HEARTBEATS_TILL_DEAD) {
226
+ if (this.connection) {
227
+ log?.info(
228
+ `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
229
+ );
230
+ this.halfCloseConnection();
231
+ }
232
+ return;
233
+ }
234
+ this.send(
235
+ {
236
+ streamId: "heartbeat",
237
+ controlFlags: 1 /* AckBit */,
238
+ payload: {
239
+ type: "ACK"
240
+ }
241
+ },
242
+ true
243
+ );
244
+ this.heartbeatMisses++;
245
+ }
246
+ resetBufferedMessages() {
247
+ this.sendBuffer = [];
248
+ this.seq = 0;
249
+ this.ack = 0;
250
+ }
251
+ sendBufferedMessages() {
252
+ if (!this.connection) {
253
+ const msg = `${this.from} -- tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
254
+ log?.error(msg);
255
+ throw new Error(msg);
256
+ }
257
+ for (const msg of this.sendBuffer) {
258
+ log?.debug(`${this.from} -- resending ${JSON.stringify(msg)}`);
259
+ const ok = this.connection.send(this.codec.toBuffer(msg));
260
+ if (!ok) {
261
+ const msg2 = `${this.from} -- failed to send buffered message to ${this.to} in session (id: ${this.debugId}) (if you hit this code path something is seriously wrong)`;
262
+ log?.error(msg2);
263
+ throw new Error(msg2);
264
+ }
265
+ }
266
+ }
267
+ updateBookkeeping(ack, seq) {
268
+ this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq > ack);
269
+ this.ack = seq + 1;
270
+ }
271
+ addToSendBuff(msg) {
272
+ this.sendBuffer.push(msg);
273
+ log?.debug(
274
+ `${this.from} -- send buff to ${this.to} now tracking ${this.sendBuffer.length} messages`
275
+ );
276
+ }
277
+ closeStaleConnection(conn) {
278
+ if (!this.connection || this.connection !== conn)
279
+ return;
280
+ log?.info(
281
+ `${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.debugId}) to ${this.to}`
282
+ );
283
+ this.connection.close();
284
+ this.connection = void 0;
285
+ }
286
+ replaceWithNewConnection(newConn) {
287
+ this.closeStaleConnection(this.connection);
288
+ this.cancelGrace();
289
+ this.connection = newConn;
290
+ }
291
+ beginGrace(cb) {
292
+ this.disconnectionGrace = setTimeout(() => {
293
+ this.resetBufferedMessages();
294
+ clearInterval(this.heartbeat);
295
+ cb();
296
+ }, SESSION_DISCONNECT_GRACE_MS);
297
+ }
298
+ cancelGrace() {
299
+ this.heartbeatMisses = 0;
300
+ clearTimeout(this.disconnectionGrace);
301
+ }
302
+ get connected() {
303
+ return this.connection !== void 0;
304
+ }
305
+ get nextExpectedSeq() {
306
+ return this.ack;
307
+ }
308
+ constructMsg(partialMsg) {
309
+ const msg = {
310
+ ...partialMsg,
311
+ id: unsafeId(),
312
+ to: this.to,
313
+ from: this.from,
314
+ seq: this.seq,
315
+ ack: this.ack
316
+ };
317
+ this.seq++;
318
+ return msg;
319
+ }
320
+ /**
321
+ * Closes the out-going connection but doesn't remove the listeners
322
+ * for incoming messages. The connection will eventually call onClose
323
+ * when it is ready to be cleaned up and only then will {@link connection} be set back
324
+ * to undefined
325
+ */
326
+ halfCloseConnection() {
327
+ this.connection?.close();
328
+ }
329
+ inspectSendBuffer() {
330
+ return this.sendBuffer;
106
331
  }
107
332
  };
333
+
334
+ // codec/json.ts
335
+ var encoder = new TextEncoder();
336
+ var decoder = new TextDecoder();
337
+ function uint8ArrayToBase64(uint8Array) {
338
+ let binary = "";
339
+ uint8Array.forEach((byte) => {
340
+ binary += String.fromCharCode(byte);
341
+ });
342
+ return btoa(binary);
343
+ }
344
+ function base64ToUint8Array(base64) {
345
+ const binaryString = atob(base64);
346
+ const uint8Array = new Uint8Array(binaryString.length);
347
+ for (let i = 0; i < binaryString.length; i++) {
348
+ uint8Array[i] = binaryString.charCodeAt(i);
349
+ }
350
+ return uint8Array;
351
+ }
352
+ var NaiveJsonCodec = {
353
+ toBuffer: (obj) => {
354
+ return encoder.encode(
355
+ JSON.stringify(obj, function replacer(key) {
356
+ const val = this[key];
357
+ if (val instanceof Uint8Array) {
358
+ return { $t: uint8ArrayToBase64(val) };
359
+ } else {
360
+ return val;
361
+ }
362
+ })
363
+ );
364
+ },
365
+ fromBuffer: (buff) => {
366
+ try {
367
+ const parsed = JSON.parse(
368
+ decoder.decode(buff),
369
+ function reviver(_key, val) {
370
+ if (val?.$t) {
371
+ return base64ToUint8Array(val.$t);
372
+ } else {
373
+ return val;
374
+ }
375
+ }
376
+ );
377
+ if (typeof parsed === "object")
378
+ return parsed;
379
+ return null;
380
+ } catch {
381
+ return null;
382
+ }
383
+ }
384
+ };
385
+
386
+ // transport/transport.ts
387
+ var import_nanoid3 = require("nanoid");
388
+
389
+ // util/stringify.ts
390
+ function coerceErrorString(err) {
391
+ if (err instanceof Error) {
392
+ return err.message;
393
+ }
394
+ return `[coerced to error] ${String(err)}`;
395
+ }
396
+
397
+ // transport/transport.ts
398
+ var DEFAULT_RECONNECT_JITTER_MAX_MS = 500;
399
+ var DEFAULT_RECONNECT_INTERVAL_MS = 250;
400
+ var defaultTransportOptions = {
401
+ retryIntervalMs: DEFAULT_RECONNECT_INTERVAL_MS,
402
+ retryJitterMs: DEFAULT_RECONNECT_JITTER_MAX_MS,
403
+ retryAttemptsMax: 5,
404
+ codec: NaiveJsonCodec
405
+ };
108
406
  var Transport = class {
407
+ /**
408
+ * Unique per instance of the transport.
409
+ * This allows us to distinguish reconnects to different
410
+ * transports.
411
+ */
412
+ instanceId = (0, import_nanoid3.nanoid)();
413
+ connectedInstanceIds = /* @__PURE__ */ new Map();
109
414
  /**
110
415
  * A flag indicating whether the transport has been destroyed.
111
416
  * A destroyed transport will not attempt to reconnect and cannot be used again.
@@ -120,83 +425,125 @@ var Transport = class {
120
425
  */
121
426
  clientId;
122
427
  /**
123
- * An array of message IDs that are waiting to be sent over the WebSocket connection.
124
- * This builds up if the WebSocket is down for a period of time.
125
- */
126
- sendQueue;
127
- /**
128
- * The buffer of messages that have been sent but not yet acknowledged.
428
+ * The map of {@link Session}s managed by this transport.
129
429
  */
130
- sendBuffer;
430
+ sessions;
131
431
  /**
132
432
  * The map of {@link Connection}s managed by this transport.
133
433
  */
134
- connections;
434
+ get connections() {
435
+ return new Map(
436
+ [...this.sessions].map(([client, session]) => [client, session.connection]).filter((entry) => entry[1] !== void 0)
437
+ );
438
+ }
135
439
  /**
136
440
  * The event dispatcher for handling events of type EventTypes.
137
441
  */
138
442
  eventDispatcher;
443
+ /**
444
+ * The options for this transport.
445
+ */
446
+ options;
139
447
  /**
140
448
  * Creates a new Transport instance.
141
449
  * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
142
450
  * @param codec The codec used to encode and decode messages.
143
451
  * @param clientId The client ID of this transport.
144
452
  */
145
- constructor(codec, clientId) {
453
+ constructor(clientId, providedOptions) {
454
+ this.options = { ...defaultTransportOptions, ...providedOptions };
146
455
  this.eventDispatcher = new EventDispatcher();
147
- this.sendBuffer = /* @__PURE__ */ new Map();
148
- this.sendQueue = /* @__PURE__ */ new Map();
149
- this.connections = /* @__PURE__ */ new Map();
150
- this.codec = codec;
456
+ this.sessions = /* @__PURE__ */ new Map();
457
+ this.codec = this.options.codec;
151
458
  this.clientId = clientId;
152
459
  this.state = "open";
153
460
  }
461
+ sessionByClientId(clientId) {
462
+ const session = this.sessions.get(clientId);
463
+ if (!session) {
464
+ const err = `${this.clientId} -- (invariant violation) no existing session for ${clientId}`;
465
+ log?.error(err);
466
+ throw new Error(err);
467
+ }
468
+ return session;
469
+ }
154
470
  /**
155
- * The downstream implementation needs to call this when a new connection is established.
471
+ * Called when a new connection is established
472
+ * and we know the identity of the connected client.
156
473
  * @param conn The connection object.
157
474
  */
158
- onConnect(conn) {
159
- log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
160
- this.connections.set(conn.connectedTo, conn);
475
+ onConnect(conn, connectedTo, instanceId) {
161
476
  this.eventDispatcher.dispatchEvent("connectionStatus", {
162
477
  status: "connect",
163
478
  conn
164
479
  });
165
- const outstanding = this.sendQueue.get(conn.connectedTo);
166
- if (!outstanding) {
167
- return;
480
+ let session = this.sessions.get(connectedTo);
481
+ const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
482
+ if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
483
+ log?.debug(
484
+ `${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
485
+ );
486
+ session.resetBufferedMessages();
487
+ session.closeStaleConnection();
488
+ this.deleteSession(session);
489
+ session = void 0;
168
490
  }
169
- for (const id of outstanding) {
170
- const msg = this.sendBuffer.get(id);
171
- if (!msg) {
172
- log?.warn(
173
- `${this.clientId} -- tried to resend a message we received an ack for`
174
- );
175
- continue;
176
- }
177
- this.send(msg);
491
+ this.connectedInstanceIds.set(connectedTo, instanceId);
492
+ if (session === void 0) {
493
+ const newSession = this.createSession(connectedTo, conn);
494
+ log?.info(
495
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
496
+ );
497
+ return newSession;
178
498
  }
179
- this.sendQueue.delete(conn.connectedTo);
499
+ log?.info(
500
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
501
+ );
502
+ session.replaceWithNewConnection(conn);
503
+ session.sendBufferedMessages();
504
+ return session;
505
+ }
506
+ createSession(connectedTo, conn) {
507
+ const session = new Session(
508
+ this.codec,
509
+ this.clientId,
510
+ connectedTo,
511
+ conn
512
+ );
513
+ this.sessions.set(session.to, session);
514
+ this.eventDispatcher.dispatchEvent("sessionStatus", {
515
+ status: "connect",
516
+ session
517
+ });
518
+ return session;
519
+ }
520
+ deleteSession(session) {
521
+ this.sessions.delete(session.to);
522
+ this.eventDispatcher.dispatchEvent("sessionStatus", {
523
+ status: "disconnect",
524
+ session
525
+ });
526
+ log?.info(
527
+ `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
528
+ );
180
529
  }
181
530
  /**
182
531
  * The downstream implementation needs to call this when a connection is closed.
183
532
  * @param conn The connection object.
184
533
  */
185
- onDisconnect(conn) {
186
- log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
187
- conn.close();
188
- this.connections.delete(conn.connectedTo);
534
+ onDisconnect(conn, connectedTo) {
189
535
  this.eventDispatcher.dispatchEvent("connectionStatus", {
190
536
  status: "disconnect",
191
537
  conn
192
538
  });
193
- }
194
- /**
195
- * Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
196
- * @param msg The message to handle.
197
- */
198
- onMessage(msg) {
199
- return this.handleMsg(this.parseMsg(msg));
539
+ if (!connectedTo)
540
+ return;
541
+ const session = this.sessionByClientId(connectedTo);
542
+ log?.info(
543
+ `${this.clientId} -- connection (id: ${conn.debugId}) disconnect from ${connectedTo}, ${SESSION_DISCONNECT_GRACE_MS}ms until session (id: ${session.debugId}) disconnect`
544
+ );
545
+ session.closeStaleConnection(conn);
546
+ session.beginGrace(() => this.deleteSession(session));
200
547
  }
201
548
  /**
202
549
  * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
@@ -207,23 +554,24 @@ var Transport = class {
207
554
  const parsedMsg = this.codec.fromBuffer(msg);
208
555
  if (parsedMsg === null) {
209
556
  const decodedBuffer = new TextDecoder().decode(msg);
210
- log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
557
+ log?.error(
558
+ `${this.clientId} -- received malformed msg, killing conn: ${decodedBuffer}`
559
+ );
211
560
  return null;
212
561
  }
213
- if (import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
214
- return {
215
- ...parsedMsg,
216
- serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
217
- procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
218
- };
219
- } else {
220
- log?.warn(
562
+ if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
563
+ log?.error(
221
564
  `${this.clientId} -- received invalid msg: ${JSON.stringify(
222
565
  parsedMsg
223
566
  )}`
224
567
  );
225
568
  return null;
226
569
  }
570
+ return {
571
+ ...parsedMsg,
572
+ serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
573
+ procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
574
+ };
227
575
  }
228
576
  /**
229
577
  * Called when a message is received by this transport.
@@ -231,24 +579,21 @@ var Transport = class {
231
579
  * @param msg The received message.
232
580
  */
233
581
  handleMsg(msg) {
234
- if (!msg) {
582
+ const session = this.sessionByClientId(msg.from);
583
+ session.cancelGrace();
584
+ log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
585
+ if (msg.seq !== session.nextExpectedSeq) {
586
+ log?.warn(
587
+ `${this.clientId} -- received out-of-order msg (got: ${msg.seq}, wanted: ${session.nextExpectedSeq}), discarding: ${JSON.stringify(
588
+ msg
589
+ )}`
590
+ );
235
591
  return;
236
592
  }
237
- if (isAck(msg.controlFlags) && import_value.Value.Check(TransportAckSchema, msg)) {
238
- log?.debug(`${this.clientId} -- received ack: ${JSON.stringify(msg)}`);
239
- if (this.sendBuffer.has(msg.payload.ack)) {
240
- this.sendBuffer.delete(msg.payload.ack);
241
- }
242
- } else {
243
- log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
593
+ if (!isAck(msg.controlFlags)) {
244
594
  this.eventDispatcher.dispatchEvent("message", msg);
245
- if (!isAck(msg.controlFlags)) {
246
- const ackMsg = reply(msg, { ack: msg.id });
247
- ackMsg.controlFlags = 1 /* AckBit */;
248
- ackMsg.from = this.clientId;
249
- this.send(ackMsg);
250
- }
251
595
  }
596
+ session.updateBookkeeping(msg.ack, msg.seq);
252
597
  }
253
598
  /**
254
599
  * Adds a listener to this transport.
@@ -270,9 +615,9 @@ var Transport = class {
270
615
  * Sends a message over this transport, delegating to the appropriate connection to actually
271
616
  * send the message.
272
617
  * @param msg The message to send.
273
- * @returns The ID of the sent message.
618
+ * @returns The ID of the sent message or undefined if it wasn't sent
274
619
  */
275
- send(msg) {
620
+ send(to, msg) {
276
621
  if (this.state === "destroyed") {
277
622
  const err = "transport is destroyed, cant send";
278
623
  log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
@@ -283,110 +628,179 @@ var Transport = class {
283
628
  msg
284
629
  )}`
285
630
  );
286
- return msg.id;
631
+ return void 0;
287
632
  }
288
- let conn = this.connections.get(msg.to);
289
- if (!isAck(msg.controlFlags)) {
290
- this.sendBuffer.set(msg.id, msg);
633
+ let session = this.sessions.get(to);
634
+ if (!session) {
635
+ session = this.createSession(to, void 0);
636
+ log?.info(
637
+ `${this.clientId} -- no session for ${to}, created a new one (id: ${session.debugId})`
638
+ );
291
639
  }
292
- if (conn) {
293
- log?.debug(`${this.clientId} -- sending ${JSON.stringify(msg)}`);
294
- const ok = conn.send(this.codec.toBuffer(msg));
295
- if (ok) {
296
- return msg.id;
640
+ return session.send(msg);
641
+ }
642
+ // control helpers
643
+ sendCloseStream(to, streamId) {
644
+ return this.send(to, {
645
+ streamId,
646
+ controlFlags: 4 /* StreamClosedBit */,
647
+ payload: {
648
+ type: "CLOSE"
297
649
  }
298
- }
299
- log?.info(
300
- `${this.clientId} -- connection to ${msg.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg)}`
301
- );
302
- const outstanding = this.sendQueue.get(msg.to) || [];
303
- outstanding.push(msg.id);
304
- this.sendQueue.set(msg.to, outstanding);
305
- this.createNewConnection(msg.to);
306
- return msg.id;
650
+ });
307
651
  }
308
652
  /**
309
653
  * Default close implementation for transports. You should override this in the downstream
310
654
  * implementation if you need to do any additional cleanup and call super.close() at the end.
311
655
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
312
656
  */
313
- async close() {
314
- for (const conn of this.connections.values()) {
315
- conn.close();
657
+ close() {
658
+ for (const session of this.sessions.values()) {
659
+ session.halfCloseConnection();
316
660
  }
317
- this.connections.clear();
318
661
  this.state = "closed";
319
- log?.info(`${this.clientId} -- closed transport`);
662
+ log?.info(`${this.clientId} -- manually closed transport`);
320
663
  }
321
664
  /**
322
665
  * Default destroy implementation for transports. You should override this in the downstream
323
666
  * implementation if you need to do any additional cleanup and call super.destroy() at the end.
324
667
  * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
325
668
  */
326
- async destroy() {
327
- for (const conn of this.connections.values()) {
328
- conn.close();
669
+ destroy() {
670
+ for (const session of this.sessions.values()) {
671
+ session.closeStaleConnection(session.connection);
329
672
  }
330
- this.connections.clear();
331
673
  this.state = "destroyed";
332
- log?.info(`${this.clientId} -- destroyed transport`);
674
+ log?.info(`${this.clientId} -- manually destroyed transport`);
333
675
  }
334
676
  };
335
-
336
- // codec/json.ts
337
- var encoder = new TextEncoder();
338
- var decoder = new TextDecoder();
339
- function uint8ArrayToBase64(uint8Array) {
340
- let binary = "";
341
- uint8Array.forEach((byte) => {
342
- binary += String.fromCharCode(byte);
343
- });
344
- return btoa(binary);
345
- }
346
- function base64ToUint8Array(base64) {
347
- const binaryString = atob(base64);
348
- const uint8Array = new Uint8Array(binaryString.length);
349
- for (let i = 0; i < binaryString.length; i++) {
350
- uint8Array[i] = binaryString.charCodeAt(i);
677
+ var ClientTransport = class extends Transport {
678
+ /**
679
+ * The map of reconnect promises for each client ID.
680
+ */
681
+ inflightConnectionPromises;
682
+ tryReconnecting = true;
683
+ constructor(clientId, providedOptions) {
684
+ super(clientId, providedOptions);
685
+ this.inflightConnectionPromises = /* @__PURE__ */ new Map();
351
686
  }
352
- return uint8Array;
353
- }
354
- var NaiveJsonCodec = {
355
- toBuffer: (obj) => {
356
- return encoder.encode(
357
- JSON.stringify(obj, function replacer(key) {
358
- let val = this[key];
359
- if (val instanceof Uint8Array) {
360
- return { $t: uint8ArrayToBase64(val) };
361
- } else {
362
- return val;
363
- }
364
- })
365
- );
366
- },
367
- fromBuffer: (buff) => {
368
- try {
369
- return JSON.parse(decoder.decode(buff), function reviver(_key, val) {
370
- if (val?.$t) {
371
- return base64ToUint8Array(val.$t);
372
- } else {
373
- return val;
687
+ handleConnection(conn, to) {
688
+ const bootHandler = this.receiveWithBootSequence(conn, () => {
689
+ conn.removeDataListener(bootHandler);
690
+ conn.addDataListener((data) => {
691
+ const parsed = this.parseMsg(data);
692
+ if (!parsed) {
693
+ conn.close();
694
+ return;
374
695
  }
696
+ this.handleMsg(parsed);
375
697
  });
376
- } catch {
377
- return null;
698
+ });
699
+ conn.addDataListener(bootHandler);
700
+ conn.addCloseListener(() => {
701
+ this.onDisconnect(conn, to);
702
+ void this.connect(to);
703
+ });
704
+ conn.addErrorListener((err) => {
705
+ log?.warn(
706
+ `${this.clientId} -- error in connection (id: ${conn.debugId}) to ${to}: ${coerceErrorString(err)}`
707
+ );
708
+ });
709
+ }
710
+ /**
711
+ * Manually attempts to connect to a client.
712
+ * @param to The client ID of the node to connect to.
713
+ */
714
+ async connect(to, attempt = 0) {
715
+ if (this.state !== "open" || !this.tryReconnecting) {
716
+ log?.info(
717
+ `${this.clientId} -- transport state is no longer open, not attempting connection`
718
+ );
719
+ return;
720
+ }
721
+ let reconnectPromise = this.inflightConnectionPromises.get(to);
722
+ if (!reconnectPromise) {
723
+ reconnectPromise = this.createNewOutgoingConnection(to);
724
+ this.inflightConnectionPromises.set(to, reconnectPromise);
725
+ }
726
+ try {
727
+ const conn = await reconnectPromise;
728
+ this.state = "open";
729
+ const requestMsg = bootRequestMessage(this.clientId, to, this.instanceId);
730
+ log?.debug(`${this.clientId} -- sending boot handshake to ${to}`);
731
+ conn.send(this.codec.toBuffer(requestMsg));
732
+ } catch (error) {
733
+ const errStr = coerceErrorString(error);
734
+ this.inflightConnectionPromises.delete(to);
735
+ if (attempt >= this.options.retryAttemptsMax) {
736
+ const errMsg = `connection to ${to} failed after ${attempt} attempts (${errStr}), giving up`;
737
+ log?.error(`${this.clientId} -- ${errMsg}`);
738
+ throw new Error(errMsg);
739
+ } else {
740
+ const jitter = Math.floor(Math.random() * this.options.retryJitterMs);
741
+ const backoffMs = this.options.retryIntervalMs * 2 ** attempt + jitter;
742
+ log?.warn(
743
+ `${this.clientId} -- connection to ${to} failed (${errStr}), trying again in ${backoffMs}ms`
744
+ );
745
+ setTimeout(() => void this.connect(to, attempt + 1), backoffMs);
746
+ }
378
747
  }
379
748
  }
749
+ receiveWithBootSequence(conn, sessionCb) {
750
+ const bootHandler = (data) => {
751
+ const parsed = this.parseMsg(data);
752
+ if (!parsed) {
753
+ conn.close();
754
+ return;
755
+ }
756
+ if (!import_value.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
757
+ log?.warn(
758
+ `${this.clientId} -- received invalid handshake resp: ${JSON.stringify(parsed)}`
759
+ );
760
+ return;
761
+ }
762
+ if (!parsed.payload.status.ok) {
763
+ log?.warn(
764
+ `${this.clientId} -- received failed handshake resp: ${JSON.stringify(
765
+ parsed
766
+ )}`
767
+ );
768
+ return;
769
+ }
770
+ const serverInstanceId = parsed.payload.status.instanceId;
771
+ log?.debug(
772
+ `${this.clientId} -- handshake from ${parsed.from} ok (server instance: ${serverInstanceId})`
773
+ );
774
+ sessionCb(this.onConnect(conn, parsed.from, serverInstanceId));
775
+ };
776
+ return bootHandler;
777
+ }
778
+ onDisconnect(conn, connectedTo) {
779
+ if (connectedTo)
780
+ this.inflightConnectionPromises.delete(connectedTo);
781
+ super.onDisconnect(conn, connectedTo);
782
+ }
380
783
  };
381
784
 
382
785
  // transport/impls/ws/connection.ts
383
786
  var WebSocketConnection = class extends Connection {
384
787
  ws;
385
- constructor(transport, connectedTo, ws) {
386
- super(transport, connectedTo);
788
+ constructor(ws) {
789
+ super();
387
790
  this.ws = ws;
388
- ws.binaryType = "arraybuffer";
389
- this.ws.onmessage = (msg) => transport.onMessage(msg.data);
791
+ this.ws.binaryType = "arraybuffer";
792
+ }
793
+ addDataListener(cb) {
794
+ this.ws.onmessage = (msg) => cb(msg.data);
795
+ }
796
+ removeDataListener() {
797
+ this.ws.onmessage = null;
798
+ }
799
+ addCloseListener(cb) {
800
+ this.ws.onclose = cb;
801
+ }
802
+ addErrorListener(cb) {
803
+ this.ws.onerror = (err) => cb(new Error(err.message));
390
804
  }
391
805
  send(payload) {
392
806
  if (this.ws.readyState === this.ws.OPEN) {
@@ -396,26 +810,18 @@ var WebSocketConnection = class extends Connection {
396
810
  return false;
397
811
  }
398
812
  }
399
- async close() {
813
+ close() {
400
814
  this.ws.close();
401
815
  }
402
816
  };
403
817
 
404
818
  // transport/impls/ws/client.ts
405
- var defaultOptions = {
406
- retryIntervalMs: 250,
407
- retryAttemptsMax: 5,
408
- codec: NaiveJsonCodec
409
- };
410
- var WebSocketClientTransport = class extends Transport {
819
+ var WebSocketClientTransport = class extends ClientTransport {
411
820
  /**
412
821
  * A function that returns a Promise that resolves to a WebSocket instance.
413
822
  */
414
823
  wsGetter;
415
- options;
416
824
  serverId;
417
- reconnectPromises;
418
- tryReconnecting = true;
419
825
  /**
420
826
  * Creates a new WebSocketClientTransport instance.
421
827
  * @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
@@ -424,105 +830,62 @@ var WebSocketClientTransport = class extends Transport {
424
830
  * @param providedOptions An optional object containing configuration options for the transport.
425
831
  */
426
832
  constructor(wsGetter, sessionId, serverId, providedOptions) {
427
- const options = { ...defaultOptions, ...providedOptions };
428
- super(options.codec, sessionId);
833
+ super(sessionId, providedOptions);
429
834
  this.wsGetter = wsGetter;
430
835
  this.serverId = serverId;
431
- this.options = options;
432
- this.reconnectPromises = /* @__PURE__ */ new Map();
433
- this.createNewConnection(this.serverId);
836
+ void this.connect(this.serverId);
434
837
  }
435
838
  reopen() {
436
839
  if (this.state === "destroyed") {
437
840
  throw new Error("cant reopen a destroyed connection");
438
841
  }
439
842
  this.state = "open";
440
- this.createNewConnection(this.serverId);
843
+ void this.connect(this.serverId);
441
844
  }
442
- async createNewConnection(to, attempt = 0) {
443
- if (this.state === "destroyed") {
444
- throw new Error("cant reopen a destroyed connection");
445
- }
446
- let reconnectPromise = this.reconnectPromises.get(to);
447
- if (!reconnectPromise) {
448
- if (!this.tryReconnecting) {
449
- log?.info(
450
- `${this.clientId} -- tryReconnecting is false, not attempting reconnect`
451
- );
452
- return;
453
- }
454
- reconnectPromise = new Promise(async (resolve) => {
455
- log?.info(`${this.clientId} -- establishing a new websocket to ${to}`);
456
- try {
457
- const ws = await this.wsGetter(to);
458
- if (ws.readyState === ws.OPEN) {
459
- return resolve({ ws });
460
- }
461
- if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) {
462
- return resolve({ err: "ws is closing or closed" });
463
- }
464
- const onOpen = () => {
465
- ws.removeEventListener("open", onOpen);
466
- resolve({ ws });
467
- };
468
- const onClose = (evt) => {
469
- ws.removeEventListener("close", onClose);
470
- resolve({ err: evt.reason });
471
- };
472
- ws.addEventListener("open", onOpen);
473
- ws.addEventListener("close", onClose);
474
- } catch (e) {
475
- const reason = e instanceof Error ? e.message : "unknown reason";
476
- return resolve({ err: `couldn't get a new websocket: ${reason}` });
845
+ async createNewOutgoingConnection(to) {
846
+ const wsRes = await new Promise((resolve) => {
847
+ log?.info(`${this.clientId} -- establishing a new websocket to ${to}`);
848
+ this.wsGetter(to).then((ws) => {
849
+ if (ws.readyState === ws.OPEN) {
850
+ resolve({ ws });
851
+ return;
477
852
  }
853
+ if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) {
854
+ resolve({ err: "ws is closing or closed" });
855
+ return;
856
+ }
857
+ const onOpen = () => {
858
+ ws.removeEventListener("open", onOpen);
859
+ resolve({ ws });
860
+ };
861
+ const onClose = (evt) => {
862
+ ws.removeEventListener("close", onClose);
863
+ resolve({ err: evt.reason });
864
+ };
865
+ const onError = (evt) => {
866
+ ws.removeEventListener("error", onError);
867
+ ws.removeEventListener("close", onClose);
868
+ resolve({ err: evt.message });
869
+ };
870
+ ws.addEventListener("open", onOpen);
871
+ ws.addEventListener("close", onClose);
872
+ ws.addEventListener("error", onError);
873
+ }).catch((e) => {
874
+ const reason = e instanceof Error ? e.message : "unknown reason";
875
+ resolve({ err: `couldn't get a new websocket: ${reason}` });
478
876
  });
479
- this.reconnectPromises.set(to, reconnectPromise);
480
- }
481
- const res = await reconnectPromise;
482
- if (this.state !== "open") {
483
- this.reconnectPromises.delete(to);
484
- if ("ws" in res) {
485
- res.ws.close();
486
- }
487
- return;
488
- }
489
- if ("ws" in res && res.ws.readyState === res.ws.OPEN) {
490
- if (res.ws === this.connections.get(to)?.ws) {
491
- return;
492
- }
493
- log?.info(`${this.clientId} -- websocket ok`);
494
- const conn = new WebSocketConnection(this, to, res.ws);
495
- this.onConnect(conn);
496
- res.ws.onclose = () => {
497
- this.reconnectPromises.delete(to);
498
- this.onDisconnect(conn);
499
- };
500
- this.state = "open";
501
- return;
502
- }
503
- this.reconnectPromises.delete(to);
504
- if (attempt >= this.options.retryAttemptsMax) {
505
- throw new Error(
506
- `${this.clientId} -- websocket to ${to} failed after ${attempt} attempts, giving up`
877
+ });
878
+ if ("ws" in wsRes) {
879
+ const conn = new WebSocketConnection(wsRes.ws);
880
+ log?.info(
881
+ `${this.clientId} -- websocket (id: ${conn.debugId}) to ${to} ok`
507
882
  );
883
+ this.handleConnection(conn, to);
884
+ return conn;
508
885
  } else {
509
- log?.warn(
510
- `${this.clientId} -- websocket to ${to} failed, trying again in ${this.options.retryIntervalMs * attempt}ms`
511
- );
512
- setTimeout(
513
- () => this.createNewConnection(to, attempt + 1),
514
- this.options.retryIntervalMs * attempt
515
- );
886
+ throw new Error(wsRes.err);
516
887
  }
517
888
  }
518
- async close() {
519
- super.close();
520
- this.reconnectPromises.clear();
521
- }
522
- async destroy() {
523
- super.destroy();
524
- this.reconnectPromises.clear();
525
- }
526
889
  };
527
890
  // Annotate the CommonJS export names for ESM import in node:
528
891
  0 && (module.exports = {