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