@replit/river 0.10.13 → 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 (83) 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-IIBVKYDB.js +62 -0
  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-3MQETIGZ.js +0 -80
  61. package/dist/chunk-5IC5XMWK.js +0 -140
  62. package/dist/chunk-L7D75G4K.js +0 -29
  63. package/dist/chunk-LQXPKF3A.js +0 -282
  64. package/dist/chunk-PJ2EUO7O.js +0 -63
  65. package/dist/chunk-WVT5QXMZ.js +0 -20
  66. package/dist/chunk-ZE4MX7DF.js +0 -75
  67. package/dist/connection-2529fc14.d.ts +0 -10
  68. package/dist/connection-316d6e3a.d.ts +0 -10
  69. package/dist/connection-8e19874c.d.ts +0 -11
  70. package/dist/connection-f7688cc1.d.ts +0 -11
  71. package/dist/transport/impls/stdio/stdio.cjs +0 -508
  72. package/dist/transport/impls/stdio/stdio.d.cts +0 -26
  73. package/dist/transport/impls/stdio/stdio.d.ts +0 -26
  74. package/dist/transport/impls/stdio/stdio.js +0 -70
  75. package/dist/transport/impls/unixsocket/client.cjs +0 -506
  76. package/dist/transport/impls/unixsocket/client.d.cts +0 -16
  77. package/dist/transport/impls/unixsocket/client.d.ts +0 -16
  78. package/dist/transport/impls/unixsocket/client.js +0 -67
  79. package/dist/transport/impls/unixsocket/server.cjs +0 -510
  80. package/dist/transport/impls/unixsocket/server.d.cts +0 -18
  81. package/dist/transport/impls/unixsocket/server.d.ts +0 -18
  82. package/dist/transport/impls/unixsocket/server.js +0 -73
  83. /package/dist/{chunk-ORAG7IAU.js → chunk-5IZ2UHWV.js} +0 -0
@@ -0,0 +1,708 @@
1
+ import {
2
+ NaiveJsonCodec
3
+ } from "./chunk-GZ7HCLLM.js";
4
+ import {
5
+ ControlMessageHandshakeRequestSchema,
6
+ ControlMessageHandshakeResponseSchema,
7
+ OpaqueTransportMessageSchema,
8
+ PROTOCOL_VERSION,
9
+ bootRequestMessage,
10
+ bootResponseMessage,
11
+ coerceErrorString,
12
+ isAck
13
+ } from "./chunk-WTOIOXB7.js";
14
+ import {
15
+ log
16
+ } from "./chunk-H4BYJELI.js";
17
+
18
+ // transport/events.ts
19
+ var EventDispatcher = class {
20
+ eventListeners = {};
21
+ numberOfListeners(eventType) {
22
+ return this.eventListeners[eventType]?.size ?? 0;
23
+ }
24
+ addEventListener(eventType, handler) {
25
+ if (!this.eventListeners[eventType]) {
26
+ this.eventListeners[eventType] = /* @__PURE__ */ new Set();
27
+ }
28
+ this.eventListeners[eventType]?.add(handler);
29
+ }
30
+ removeEventListener(eventType, handler) {
31
+ const handlers = this.eventListeners[eventType];
32
+ if (handlers) {
33
+ this.eventListeners[eventType]?.delete(handler);
34
+ }
35
+ }
36
+ dispatchEvent(eventType, event) {
37
+ const handlers = this.eventListeners[eventType];
38
+ if (handlers) {
39
+ for (const handler of handlers) {
40
+ handler(event);
41
+ }
42
+ }
43
+ }
44
+ };
45
+
46
+ // transport/session.ts
47
+ import { customAlphabet } from "nanoid";
48
+ var nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvxyz", 6);
49
+ var unsafeId = () => nanoid();
50
+ var Connection = class {
51
+ debugId;
52
+ constructor() {
53
+ this.debugId = `conn-${unsafeId()}`;
54
+ }
55
+ };
56
+ var HEARTBEAT_INTERVAL_MS = 250;
57
+ var HEARTBEATS_TILL_DEAD = 4;
58
+ var SESSION_DISCONNECT_GRACE_MS = 3e3;
59
+ var Session = class {
60
+ codec;
61
+ /**
62
+ * The buffer of messages that have been sent but not yet acknowledged.
63
+ */
64
+ sendBuffer = [];
65
+ /**
66
+ * The active connection associated with this session
67
+ */
68
+ connection;
69
+ from;
70
+ to;
71
+ /**
72
+ * The unique ID of this session.
73
+ */
74
+ debugId;
75
+ /**
76
+ * Number of messages we've sent along this session (excluding handshake)
77
+ */
78
+ seq = 0;
79
+ /**
80
+ * Number of unique messages we've received this session (excluding handshake)
81
+ */
82
+ ack = 0;
83
+ /**
84
+ * The grace period between when the inner connection is disconnected
85
+ * and when we should consider the entire session disconnected.
86
+ */
87
+ disconnectionGrace;
88
+ /**
89
+ * Number of heartbeats we've sent without a response.
90
+ */
91
+ heartbeatMisses;
92
+ /**
93
+ * The interval for sending heartbeats.
94
+ */
95
+ heartbeat;
96
+ constructor(codec, from, connectedTo, conn) {
97
+ this.debugId = `sess-${unsafeId()}`;
98
+ this.from = from;
99
+ this.to = connectedTo;
100
+ this.connection = conn;
101
+ this.codec = codec;
102
+ this.heartbeatMisses = 0;
103
+ this.heartbeat = setInterval(
104
+ () => this.sendHeartbeat(),
105
+ HEARTBEAT_INTERVAL_MS
106
+ );
107
+ }
108
+ /**
109
+ * Sends a message over the session's connection.
110
+ * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
111
+ *
112
+ * @param msg The partial message to be sent, which will be constructed into a full message.
113
+ * @param skipRetry Optional. If true, the message will not be buffered for retry on failure. This should only be used for
114
+ * ack hearbeats, which contain information that can already be found in the other buffered messages.
115
+ * @returns The full transport ID of the message that was attempted to be sent.
116
+ */
117
+ send(msg, skipRetry) {
118
+ const fullMsg = this.constructMsg(msg);
119
+ log?.debug(`${this.from} -- sending ${JSON.stringify(fullMsg)}`);
120
+ if (this.connection) {
121
+ const ok = this.connection.send(this.codec.toBuffer(fullMsg));
122
+ if (ok)
123
+ return fullMsg.id;
124
+ log?.info(
125
+ `${this.from} -- failed to send ${fullMsg.id} to ${fullMsg.to}, connection (id: ${this.connection.debugId}) is probably dead`
126
+ );
127
+ } else {
128
+ log?.info(
129
+ `${this.from} -- failed to send ${fullMsg.id} to ${fullMsg.to}, connection not ready yet`
130
+ );
131
+ }
132
+ if (skipRetry)
133
+ return fullMsg.id;
134
+ this.addToSendBuff(fullMsg);
135
+ log?.info(
136
+ `${this.from} -- buffering msg ${fullMsg.id} until connection is healthy again`
137
+ );
138
+ return fullMsg.id;
139
+ }
140
+ sendHeartbeat() {
141
+ if (this.heartbeatMisses >= HEARTBEATS_TILL_DEAD && this.connection) {
142
+ log?.info(
143
+ `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
144
+ );
145
+ this.halfCloseConnection();
146
+ return;
147
+ }
148
+ this.send(
149
+ {
150
+ streamId: "heartbeat",
151
+ controlFlags: 1 /* AckBit */,
152
+ payload: {
153
+ type: "ACK"
154
+ }
155
+ },
156
+ true
157
+ );
158
+ this.heartbeatMisses++;
159
+ }
160
+ resetBufferedMessages() {
161
+ this.sendBuffer = [];
162
+ this.seq = 0;
163
+ this.ack = 0;
164
+ }
165
+ sendBufferedMessages() {
166
+ if (!this.connection) {
167
+ const msg = `${this.from} -- tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
168
+ log?.error(msg);
169
+ throw new Error(msg);
170
+ }
171
+ for (const msg of this.sendBuffer) {
172
+ log?.debug(`${this.from} -- resending ${JSON.stringify(msg)}`);
173
+ const ok = this.connection.send(this.codec.toBuffer(msg));
174
+ if (!ok) {
175
+ 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)`;
176
+ log?.error(msg2);
177
+ throw new Error(msg2);
178
+ }
179
+ }
180
+ }
181
+ updateBookkeeping(ack, seq) {
182
+ this.heartbeatMisses = 0;
183
+ this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq > ack);
184
+ this.ack = seq + 1;
185
+ }
186
+ addToSendBuff(msg) {
187
+ this.sendBuffer.push(msg);
188
+ log?.debug(
189
+ `${this.from} -- send buff to ${this.to} now tracking ${this.sendBuffer.length} messages`
190
+ );
191
+ }
192
+ closeStaleConnection(conn) {
193
+ if (!this.connection || this.connection !== conn)
194
+ return;
195
+ log?.info(
196
+ `${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.debugId}) to ${this.to}`
197
+ );
198
+ this.connection.close();
199
+ this.connection = void 0;
200
+ }
201
+ replaceWithNewConnection(newConn) {
202
+ this.closeStaleConnection(this.connection);
203
+ this.cancelGrace();
204
+ this.connection = newConn;
205
+ }
206
+ beginGrace(cb) {
207
+ this.disconnectionGrace = setTimeout(() => {
208
+ this.resetBufferedMessages();
209
+ clearInterval(this.heartbeat);
210
+ cb();
211
+ }, SESSION_DISCONNECT_GRACE_MS);
212
+ }
213
+ cancelGrace() {
214
+ clearTimeout(this.disconnectionGrace);
215
+ }
216
+ get connected() {
217
+ return this.connection !== void 0;
218
+ }
219
+ get nextExpectedSeq() {
220
+ return this.ack;
221
+ }
222
+ constructMsg(partialMsg) {
223
+ const msg = {
224
+ ...partialMsg,
225
+ id: unsafeId(),
226
+ to: this.to,
227
+ from: this.from,
228
+ seq: this.seq,
229
+ ack: this.ack
230
+ };
231
+ this.seq++;
232
+ return msg;
233
+ }
234
+ /**
235
+ * Closes the out-going connection but doesn't remove the listeners
236
+ * for incoming messages. The connection will eventually call onClose
237
+ * when it is ready to be cleaned up and only then will {@link connection} be set back
238
+ * to undefined
239
+ */
240
+ halfCloseConnection() {
241
+ this.connection?.close();
242
+ }
243
+ inspectSendBuffer() {
244
+ return this.sendBuffer;
245
+ }
246
+ };
247
+
248
+ // transport/transport.ts
249
+ import { Value } from "@sinclair/typebox/value";
250
+ import { nanoid as nanoid2 } from "nanoid";
251
+ var DEFAULT_RECONNECT_JITTER_MAX_MS = 500;
252
+ var DEFAULT_RECONNECT_INTERVAL_MS = 250;
253
+ var defaultTransportOptions = {
254
+ retryIntervalMs: DEFAULT_RECONNECT_INTERVAL_MS,
255
+ retryJitterMs: DEFAULT_RECONNECT_JITTER_MAX_MS,
256
+ retryAttemptsMax: 5,
257
+ codec: NaiveJsonCodec
258
+ };
259
+ var Transport = class {
260
+ /**
261
+ * A flag indicating whether the transport has been destroyed.
262
+ * A destroyed transport will not attempt to reconnect and cannot be used again.
263
+ */
264
+ state;
265
+ /**
266
+ * The {@link Codec} used to encode and decode messages.
267
+ */
268
+ codec;
269
+ /**
270
+ * The client ID of this transport.
271
+ */
272
+ clientId;
273
+ /**
274
+ * The map of {@link Session}s managed by this transport.
275
+ */
276
+ sessions;
277
+ /**
278
+ * The map of {@link Connection}s managed by this transport.
279
+ */
280
+ get connections() {
281
+ return new Map(
282
+ [...this.sessions].map(([client, session]) => [client, session.connection]).filter((entry) => entry[1] !== void 0)
283
+ );
284
+ }
285
+ /**
286
+ * The event dispatcher for handling events of type EventTypes.
287
+ */
288
+ eventDispatcher;
289
+ /**
290
+ * The options for this transport.
291
+ */
292
+ options;
293
+ /**
294
+ * Creates a new Transport instance.
295
+ * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
296
+ * @param codec The codec used to encode and decode messages.
297
+ * @param clientId The client ID of this transport.
298
+ */
299
+ constructor(clientId, providedOptions) {
300
+ this.options = { ...defaultTransportOptions, ...providedOptions };
301
+ this.eventDispatcher = new EventDispatcher();
302
+ this.sessions = /* @__PURE__ */ new Map();
303
+ this.codec = this.options.codec;
304
+ this.clientId = clientId;
305
+ this.state = "open";
306
+ }
307
+ sessionByClientId(clientId) {
308
+ const session = this.sessions.get(clientId);
309
+ if (!session) {
310
+ const err = `${this.clientId} -- (invariant violation) no existing session for ${clientId}`;
311
+ log?.error(err);
312
+ throw new Error(err);
313
+ }
314
+ return session;
315
+ }
316
+ /**
317
+ * Called when a new connection is established
318
+ * and we know the identity of the connected client.
319
+ * @param conn The connection object.
320
+ */
321
+ onConnect(conn, connectedTo) {
322
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
323
+ status: "connect",
324
+ conn
325
+ });
326
+ const session = this.sessions.get(connectedTo);
327
+ if (session === void 0) {
328
+ const newSession = this.createSession(connectedTo, conn);
329
+ log?.info(
330
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
331
+ );
332
+ return newSession;
333
+ }
334
+ log?.info(
335
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
336
+ );
337
+ session.replaceWithNewConnection(conn);
338
+ session.sendBufferedMessages();
339
+ return session;
340
+ }
341
+ createSession(connectedTo, conn) {
342
+ const session = new Session(
343
+ this.codec,
344
+ this.clientId,
345
+ connectedTo,
346
+ conn
347
+ );
348
+ this.sessions.set(session.to, session);
349
+ this.eventDispatcher.dispatchEvent("sessionStatus", {
350
+ status: "connect",
351
+ session
352
+ });
353
+ return session;
354
+ }
355
+ deleteSession(session) {
356
+ this.sessions.delete(session.to);
357
+ this.eventDispatcher.dispatchEvent("sessionStatus", {
358
+ status: "disconnect",
359
+ session
360
+ });
361
+ log?.info(
362
+ `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
363
+ );
364
+ }
365
+ /**
366
+ * The downstream implementation needs to call this when a connection is closed.
367
+ * @param conn The connection object.
368
+ */
369
+ onDisconnect(conn, connectedTo) {
370
+ this.eventDispatcher.dispatchEvent("connectionStatus", {
371
+ status: "disconnect",
372
+ conn
373
+ });
374
+ if (!connectedTo)
375
+ return;
376
+ const session = this.sessionByClientId(connectedTo);
377
+ log?.info(
378
+ `${this.clientId} -- connection (id: ${conn.debugId}) disconnect from ${connectedTo}, ${SESSION_DISCONNECT_GRACE_MS}ms until session (id: ${session.debugId}) disconnect`
379
+ );
380
+ session.closeStaleConnection(conn);
381
+ session.beginGrace(() => this.deleteSession(session));
382
+ }
383
+ /**
384
+ * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
385
+ * @param msg The message to parse.
386
+ * @returns The parsed message, or null if the message is malformed or invalid.
387
+ */
388
+ parseMsg(msg) {
389
+ const parsedMsg = this.codec.fromBuffer(msg);
390
+ if (parsedMsg === null) {
391
+ const decodedBuffer = new TextDecoder().decode(msg);
392
+ log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
393
+ return null;
394
+ }
395
+ if (!Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
396
+ log?.warn(
397
+ `${this.clientId} -- received invalid msg: ${JSON.stringify(
398
+ parsedMsg
399
+ )}`
400
+ );
401
+ return null;
402
+ }
403
+ return {
404
+ ...parsedMsg,
405
+ serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
406
+ procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
407
+ };
408
+ }
409
+ /**
410
+ * Called when a message is received by this transport.
411
+ * You generally shouldn't need to override this in downstream transport implementations.
412
+ * @param msg The received message.
413
+ */
414
+ handleMsg(msg) {
415
+ if (!msg) {
416
+ return;
417
+ }
418
+ const session = this.sessionByClientId(msg.from);
419
+ session.cancelGrace();
420
+ log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
421
+ if (msg.seq !== session.nextExpectedSeq) {
422
+ log?.warn(
423
+ `${this.clientId} -- received out-of-order msg (got: ${msg.seq}, wanted: ${session.nextExpectedSeq}), discarding: ${JSON.stringify(
424
+ msg
425
+ )}`
426
+ );
427
+ return;
428
+ }
429
+ if (!isAck(msg.controlFlags)) {
430
+ this.eventDispatcher.dispatchEvent("message", msg);
431
+ }
432
+ session.updateBookkeeping(msg.ack, msg.seq);
433
+ }
434
+ /**
435
+ * Adds a listener to this transport.
436
+ * @param the type of event to listen for
437
+ * @param handler The message handler to add.
438
+ */
439
+ addEventListener(type, handler) {
440
+ this.eventDispatcher.addEventListener(type, handler);
441
+ }
442
+ /**
443
+ * Removes a listener from this transport.
444
+ * @param the type of event to unlisten on
445
+ * @param handler The message handler to remove.
446
+ */
447
+ removeEventListener(type, handler) {
448
+ this.eventDispatcher.removeEventListener(type, handler);
449
+ }
450
+ /**
451
+ * Sends a message over this transport, delegating to the appropriate connection to actually
452
+ * send the message.
453
+ * @param msg The message to send.
454
+ * @returns The ID of the sent message or undefined if it wasn't sent
455
+ */
456
+ send(to, msg) {
457
+ if (this.state === "destroyed") {
458
+ const err = "transport is destroyed, cant send";
459
+ log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
460
+ throw new Error(err);
461
+ } else if (this.state === "closed") {
462
+ log?.info(
463
+ `${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
464
+ msg
465
+ )}`
466
+ );
467
+ return void 0;
468
+ }
469
+ let session = this.sessions.get(to);
470
+ if (!session) {
471
+ session = this.createSession(to, void 0);
472
+ log?.info(
473
+ `${this.clientId} -- no session for ${to}, created a new one (id: ${session.debugId})`
474
+ );
475
+ }
476
+ return session.send(msg);
477
+ }
478
+ // control helpers
479
+ sendCloseStream(to, streamId) {
480
+ return this.send(to, {
481
+ streamId,
482
+ controlFlags: 4 /* StreamClosedBit */,
483
+ payload: {
484
+ type: "CLOSE"
485
+ }
486
+ });
487
+ }
488
+ /**
489
+ * Default close implementation for transports. You should override this in the downstream
490
+ * implementation if you need to do any additional cleanup and call super.close() at the end.
491
+ * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
492
+ */
493
+ close() {
494
+ for (const session of this.sessions.values()) {
495
+ session.halfCloseConnection();
496
+ }
497
+ this.state = "closed";
498
+ log?.info(`${this.clientId} -- manually closed transport`);
499
+ }
500
+ /**
501
+ * Default destroy implementation for transports. You should override this in the downstream
502
+ * implementation if you need to do any additional cleanup and call super.destroy() at the end.
503
+ * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
504
+ */
505
+ destroy() {
506
+ for (const session of this.sessions.values()) {
507
+ session.closeStaleConnection(session.connection);
508
+ }
509
+ this.state = "destroyed";
510
+ log?.info(`${this.clientId} -- manually destroyed transport`);
511
+ }
512
+ };
513
+ var ClientTransport = class extends Transport {
514
+ /**
515
+ * The map of reconnect promises for each client ID.
516
+ */
517
+ inflightConnectionPromises;
518
+ tryReconnecting = true;
519
+ serverInstanceIds = /* @__PURE__ */ new Map();
520
+ constructor(clientId, providedOptions) {
521
+ super(clientId, providedOptions);
522
+ this.inflightConnectionPromises = /* @__PURE__ */ new Map();
523
+ }
524
+ handleConnection(conn, to) {
525
+ const bootHandler = this.receiveWithBootSequence(conn, () => {
526
+ conn.removeDataListener(bootHandler);
527
+ conn.addDataListener((data) => this.handleMsg(this.parseMsg(data)));
528
+ });
529
+ conn.addDataListener(bootHandler);
530
+ conn.addCloseListener(() => {
531
+ this.onDisconnect(conn, to);
532
+ void this.connect(to);
533
+ });
534
+ conn.addErrorListener((err) => {
535
+ log?.warn(
536
+ `${this.clientId} -- error in connection (id: ${conn.debugId}) to ${to}: ${coerceErrorString(err)}`
537
+ );
538
+ });
539
+ }
540
+ /**
541
+ * Manually attempts to connect to a client.
542
+ * @param to The client ID of the node to connect to.
543
+ */
544
+ async connect(to, attempt = 0) {
545
+ if (this.state !== "open" || !this.tryReconnecting) {
546
+ log?.info(
547
+ `${this.clientId} -- transport state is no longer open, not attempting connection`
548
+ );
549
+ return;
550
+ }
551
+ let reconnectPromise = this.inflightConnectionPromises.get(to);
552
+ if (!reconnectPromise) {
553
+ reconnectPromise = this.createNewOutgoingConnection(to);
554
+ this.inflightConnectionPromises.set(to, reconnectPromise);
555
+ }
556
+ try {
557
+ const conn = await reconnectPromise;
558
+ this.state = "open";
559
+ const requestMsg = bootRequestMessage(this.clientId, to);
560
+ log?.debug(`${this.clientId} -- sending boot handshake to ${to}`);
561
+ conn.send(this.codec.toBuffer(requestMsg));
562
+ } catch (error) {
563
+ const errStr = coerceErrorString(error);
564
+ this.inflightConnectionPromises.delete(to);
565
+ if (attempt >= this.options.retryAttemptsMax) {
566
+ const errMsg = `connection to ${to} failed after ${attempt} attempts (${errStr}), giving up`;
567
+ log?.error(`${this.clientId} -- ${errMsg}`);
568
+ throw new Error(errMsg);
569
+ } else {
570
+ const jitter = Math.floor(Math.random() * this.options.retryJitterMs);
571
+ const backoffMs = this.options.retryIntervalMs * 2 ** attempt + jitter;
572
+ log?.warn(
573
+ `${this.clientId} -- connection to ${to} failed (${errStr}), trying again in ${backoffMs}ms`
574
+ );
575
+ setTimeout(() => void this.connect(to, attempt + 1), backoffMs);
576
+ }
577
+ }
578
+ }
579
+ receiveWithBootSequence(conn, sessionCb) {
580
+ const bootHandler = (data) => {
581
+ const parsed = this.parseMsg(data);
582
+ if (!parsed)
583
+ return;
584
+ if (!Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
585
+ log?.warn(
586
+ `${this.clientId} -- received invalid handshake resp: ${JSON.stringify(parsed)}`
587
+ );
588
+ return;
589
+ }
590
+ if (!parsed.payload.status.ok) {
591
+ log?.warn(
592
+ `${this.clientId} -- received failed handshake resp: ${JSON.stringify(
593
+ parsed
594
+ )}`
595
+ );
596
+ return;
597
+ }
598
+ const oldSession = this.sessions.get(parsed.from);
599
+ const serverInstanceId = parsed.payload.status.instanceId;
600
+ const lastServerInstanceId = this.serverInstanceIds.get(parsed.from);
601
+ if (oldSession && lastServerInstanceId !== serverInstanceId && lastServerInstanceId !== void 0) {
602
+ log?.debug(
603
+ `${this.clientId} -- handshake from ${parsed.from} has different server instance (got: ${serverInstanceId}, last connected to: ${lastServerInstanceId}), starting a new session`
604
+ );
605
+ oldSession.resetBufferedMessages();
606
+ oldSession.closeStaleConnection();
607
+ this.deleteSession(oldSession);
608
+ }
609
+ log?.debug(
610
+ `${this.clientId} -- handshake from ${parsed.from} ok (server instance: ${serverInstanceId})`
611
+ );
612
+ this.serverInstanceIds.set(parsed.from, serverInstanceId);
613
+ sessionCb(this.onConnect(conn, parsed.from));
614
+ };
615
+ return bootHandler;
616
+ }
617
+ onDisconnect(conn, connectedTo) {
618
+ if (connectedTo)
619
+ this.inflightConnectionPromises.delete(connectedTo);
620
+ super.onDisconnect(conn, connectedTo);
621
+ }
622
+ };
623
+ var ServerTransport = class extends Transport {
624
+ /**
625
+ * Unique per instance of server transport.
626
+ * This allows us to distinguish reconnects to different
627
+ * server transports at the same reachable address and signal
628
+ * the appropriate session disconnect back to the client at the
629
+ * boot stage.
630
+ */
631
+ instanceId = nanoid2();
632
+ constructor(clientId, providedOptions) {
633
+ super(clientId, providedOptions);
634
+ log?.info(
635
+ `${this.clientId} -- initiated server transport (instance id: ${this.instanceId}, protocol: ${PROTOCOL_VERSION})`
636
+ );
637
+ }
638
+ handleConnection(conn) {
639
+ let session = void 0;
640
+ const client = () => session?.to ?? "unknown";
641
+ const bootHandler = this.receiveWithBootSequence(
642
+ conn,
643
+ (establishedSession) => {
644
+ session = establishedSession;
645
+ conn.removeDataListener(bootHandler);
646
+ conn.addDataListener((data) => this.handleMsg(this.parseMsg(data)));
647
+ }
648
+ );
649
+ conn.addDataListener(bootHandler);
650
+ conn.addCloseListener(() => {
651
+ if (!session)
652
+ return;
653
+ log?.info(
654
+ `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
655
+ );
656
+ this.onDisconnect(conn, session.to);
657
+ });
658
+ conn.addErrorListener((err) => {
659
+ if (!session)
660
+ return;
661
+ log?.warn(
662
+ `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} got an error: ${coerceErrorString(err)}`
663
+ );
664
+ });
665
+ }
666
+ receiveWithBootSequence(conn, sessionCb) {
667
+ const bootHandler = (data) => {
668
+ const parsed = this.parseMsg(data);
669
+ if (!parsed)
670
+ return;
671
+ if (!Value.Check(ControlMessageHandshakeRequestSchema, parsed.payload)) {
672
+ const responseMsg2 = bootResponseMessage(
673
+ this.clientId,
674
+ this.instanceId,
675
+ parsed.from,
676
+ false
677
+ );
678
+ conn.send(this.codec.toBuffer(responseMsg2));
679
+ log?.warn(
680
+ `${this.clientId} -- received invalid handshake msg: ${JSON.stringify(
681
+ parsed
682
+ )}`
683
+ );
684
+ return;
685
+ }
686
+ log?.debug(
687
+ `${this.clientId} -- handshake from ${parsed.from} ok, responding with handshake success`
688
+ );
689
+ const responseMsg = bootResponseMessage(
690
+ this.clientId,
691
+ this.instanceId,
692
+ parsed.from,
693
+ true
694
+ );
695
+ conn.send(this.codec.toBuffer(responseMsg));
696
+ sessionCb(this.onConnect(conn, parsed.from));
697
+ };
698
+ return bootHandler;
699
+ }
700
+ };
701
+
702
+ export {
703
+ Connection,
704
+ Session,
705
+ Transport,
706
+ ClientTransport,
707
+ ServerTransport
708
+ };