@replit/river 0.11.0 → 0.12.0

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