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