@replit/river 0.12.1 → 0.12.3

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 (45) hide show
  1. package/dist/{chunk-QEYN2Z6O.js → chunk-2PKONGC3.js} +39 -35
  2. package/dist/{chunk-RDTTKCGV.js → chunk-6OAKPO5R.js} +1 -1
  3. package/dist/{chunk-TKINU53F.js → chunk-7J66WOIC.js} +1 -1
  4. package/dist/{chunk-55XUAPC6.js → chunk-AFLZ6INU.js} +1 -1
  5. package/dist/{chunk-M6LY25P2.js → chunk-H4QR2K3T.js} +1 -1
  6. package/dist/{connection-eb10d250.d.ts → connection-4a839b9a.d.ts} +1 -1
  7. package/dist/{connection-bf7811aa.d.ts → connection-64b9e0ff.d.ts} +1 -1
  8. package/dist/{connection-d880aa4a.d.ts → connection-a6cc48e8.d.ts} +1 -1
  9. package/dist/{index-0c0a69f6.d.ts → index-1afc5580.d.ts} +5 -9
  10. package/dist/router/index.cjs +1 -1
  11. package/dist/router/index.d.cts +1 -1
  12. package/dist/router/index.d.ts +1 -1
  13. package/dist/router/index.js +1 -1
  14. package/dist/transport/impls/stdio/client.cjs +39 -35
  15. package/dist/transport/impls/stdio/client.d.cts +2 -2
  16. package/dist/transport/impls/stdio/client.d.ts +2 -2
  17. package/dist/transport/impls/stdio/client.js +2 -2
  18. package/dist/transport/impls/stdio/server.cjs +38 -33
  19. package/dist/transport/impls/stdio/server.d.cts +2 -2
  20. package/dist/transport/impls/stdio/server.d.ts +2 -2
  21. package/dist/transport/impls/stdio/server.js +2 -2
  22. package/dist/transport/impls/uds/client.cjs +39 -35
  23. package/dist/transport/impls/uds/client.d.cts +2 -2
  24. package/dist/transport/impls/uds/client.d.ts +2 -2
  25. package/dist/transport/impls/uds/client.js +2 -2
  26. package/dist/transport/impls/uds/server.cjs +38 -33
  27. package/dist/transport/impls/uds/server.d.cts +2 -2
  28. package/dist/transport/impls/uds/server.d.ts +2 -2
  29. package/dist/transport/impls/uds/server.js +2 -2
  30. package/dist/transport/impls/ws/client.cjs +41 -36
  31. package/dist/transport/impls/ws/client.d.cts +2 -2
  32. package/dist/transport/impls/ws/client.d.ts +2 -2
  33. package/dist/transport/impls/ws/client.js +4 -3
  34. package/dist/transport/impls/ws/server.cjs +38 -33
  35. package/dist/transport/impls/ws/server.d.cts +2 -2
  36. package/dist/transport/impls/ws/server.d.ts +2 -2
  37. package/dist/transport/impls/ws/server.js +2 -2
  38. package/dist/transport/index.cjs +39 -35
  39. package/dist/transport/index.d.cts +1 -1
  40. package/dist/transport/index.d.ts +1 -1
  41. package/dist/transport/index.js +1 -1
  42. package/dist/util/testHelpers.d.cts +1 -1
  43. package/dist/util/testHelpers.d.ts +1 -1
  44. package/dist/util/testHelpers.js +1 -1
  45. package/package.json +1 -1
@@ -227,7 +227,7 @@ var Session = class {
227
227
  log?.info(
228
228
  `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
229
229
  );
230
- this.halfCloseConnection();
230
+ this.closeStaleConnection(this.connection);
231
231
  }
232
232
  return;
233
233
  }
@@ -288,17 +288,27 @@ var Session = class {
288
288
  this.cancelGrace();
289
289
  this.connection = newConn;
290
290
  }
291
+ graceCb;
291
292
  beginGrace(cb) {
293
+ this.graceCb = cb;
292
294
  this.disconnectionGrace = setTimeout(() => {
293
- this.resetBufferedMessages();
294
- clearInterval(this.heartbeat);
295
+ this.close();
295
296
  cb();
296
297
  }, SESSION_DISCONNECT_GRACE_MS);
297
298
  }
299
+ // called on reconnect of the underlying session
298
300
  cancelGrace() {
299
301
  this.heartbeatMisses = 0;
300
302
  clearTimeout(this.disconnectionGrace);
301
303
  }
304
+ // closed when we want to discard the whole session
305
+ // (i.e. shutdown or session disconnect)
306
+ close() {
307
+ this.closeStaleConnection(this.connection);
308
+ this.cancelGrace();
309
+ clearInterval(this.heartbeat);
310
+ this.resetBufferedMessages();
311
+ }
302
312
  get connected() {
303
313
  return this.connection !== void 0;
304
314
  }
@@ -317,15 +327,6 @@ var Session = class {
317
327
  this.seq++;
318
328
  return msg;
319
329
  }
320
- /**
321
- * Closes the out-going connection but doesn't remove the listeners
322
- * for incoming messages. The connection will eventually call onClose
323
- * when it is ready to be cleaned up and only then will {@link connection} be set back
324
- * to undefined
325
- */
326
- halfCloseConnection() {
327
- this.connection?.close();
328
- }
329
330
  inspectSendBuffer() {
330
331
  return this.sendBuffer;
331
332
  }
@@ -477,19 +478,18 @@ var Transport = class {
477
478
  status: "connect",
478
479
  conn
479
480
  });
480
- let session = this.sessions.get(connectedTo);
481
+ let oldSession = this.sessions.get(connectedTo);
481
482
  const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
482
- if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
483
- log?.debug(
484
- `${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
483
+ if (oldSession && lastInstanceId !== void 0 && lastInstanceId !== instanceId) {
484
+ log?.warn(
485
+ `${this.clientId} -- handshake from ${connectedTo} is a different instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
485
486
  );
486
- session.resetBufferedMessages();
487
- session.closeStaleConnection();
488
- this.deleteSession(session);
489
- session = void 0;
487
+ oldSession.close();
488
+ this.deleteSession(oldSession);
489
+ oldSession = void 0;
490
490
  }
491
491
  this.connectedInstanceIds.set(connectedTo, instanceId);
492
- if (session === void 0) {
492
+ if (oldSession === void 0) {
493
493
  const newSession = this.createSession(connectedTo, conn);
494
494
  log?.info(
495
495
  `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
@@ -497,11 +497,11 @@ var Transport = class {
497
497
  return newSession;
498
498
  }
499
499
  log?.info(
500
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
500
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.debugId}) to ${connectedTo}`
501
501
  );
502
- session.replaceWithNewConnection(conn);
503
- session.sendBufferedMessages();
504
- return session;
502
+ oldSession.replaceWithNewConnection(conn);
503
+ oldSession.sendBufferedMessages();
504
+ return oldSession;
505
505
  }
506
506
  createSession(connectedTo, conn) {
507
507
  const session = new Session(
@@ -519,24 +519,25 @@ var Transport = class {
519
519
  }
520
520
  deleteSession(session) {
521
521
  this.sessions.delete(session.to);
522
+ log?.info(
523
+ `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
524
+ );
522
525
  this.eventDispatcher.dispatchEvent("sessionStatus", {
523
526
  status: "disconnect",
524
527
  session
525
528
  });
526
- log?.info(
527
- `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
528
- );
529
529
  }
530
530
  /**
531
531
  * The downstream implementation needs to call this when a connection is closed.
532
532
  * @param conn The connection object.
533
+ * @param connectedTo The peer we are connected to.
533
534
  */
534
535
  onDisconnect(conn, connectedTo) {
535
536
  this.eventDispatcher.dispatchEvent("connectionStatus", {
536
537
  status: "disconnect",
537
538
  conn
538
539
  });
539
- if (!connectedTo)
540
+ if (this.state !== "open")
540
541
  return;
541
542
  const session = this.sessionByClientId(connectedTo);
542
543
  log?.info(
@@ -579,6 +580,8 @@ var Transport = class {
579
580
  * @param msg The received message.
580
581
  */
581
582
  handleMsg(msg) {
583
+ if (this.state !== "open")
584
+ return;
582
585
  const session = this.sessionByClientId(msg.from);
583
586
  session.cancelGrace();
584
587
  log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
@@ -655,10 +658,11 @@ var Transport = class {
655
658
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
656
659
  */
657
660
  close() {
661
+ this.state = "closed";
658
662
  for (const session of this.sessions.values()) {
659
- session.halfCloseConnection();
663
+ session.close();
664
+ this.deleteSession(session);
660
665
  }
661
- this.state = "closed";
662
666
  log?.info(`${this.clientId} -- manually closed transport`);
663
667
  }
664
668
  /**
@@ -667,10 +671,11 @@ var Transport = class {
667
671
  * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
668
672
  */
669
673
  destroy() {
674
+ this.state = "destroyed";
670
675
  for (const session of this.sessions.values()) {
671
- session.closeStaleConnection(session.connection);
676
+ session.close();
677
+ this.deleteSession(session);
672
678
  }
673
- this.state = "destroyed";
674
679
  log?.info(`${this.clientId} -- manually destroyed transport`);
675
680
  }
676
681
  };
@@ -776,8 +781,7 @@ var ClientTransport = class extends Transport {
776
781
  return bootHandler;
777
782
  }
778
783
  onDisconnect(conn, connectedTo) {
779
- if (connectedTo)
780
- this.inflightConnectionPromises.delete(connectedTo);
784
+ this.inflightConnectionPromises.delete(connectedTo);
781
785
  super.onDisconnect(conn, connectedTo);
782
786
  }
783
787
  };
@@ -863,9 +867,10 @@ var WebSocketClientTransport = class extends ClientTransport {
863
867
  resolve({ err: evt.reason });
864
868
  };
865
869
  const onError = (evt) => {
870
+ const err = evt.error;
866
871
  ws.removeEventListener("error", onError);
867
872
  ws.removeEventListener("close", onClose);
868
- resolve({ err: evt.message });
873
+ resolve({ err: err.message || err.code || "unknown error" });
869
874
  };
870
875
  ws.addEventListener("open", onOpen);
871
876
  ws.addEventListener("close", onClose);
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
3
- import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
2
+ import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
3
+ import { W as WebSocketConnection } from '../../../connection-4a839b9a.js';
4
4
  import '../../../types-3e5768ec.js';
5
5
  import '@sinclair/typebox';
6
6
 
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
3
- import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
2
+ import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
3
+ import { W as WebSocketConnection } from '../../../connection-4a839b9a.js';
4
4
  import '../../../types-3e5768ec.js';
5
5
  import '@sinclair/typebox';
6
6
 
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "../../../chunk-RDTTKCGV.js";
3
+ } from "../../../chunk-6OAKPO5R.js";
4
4
  import {
5
5
  ClientTransport
6
- } from "../../../chunk-QEYN2Z6O.js";
6
+ } from "../../../chunk-2PKONGC3.js";
7
7
  import "../../../chunk-GZ7HCLLM.js";
8
8
  import "../../../chunk-XFFS4UOD.js";
9
9
  import {
@@ -58,9 +58,10 @@ var WebSocketClientTransport = class extends ClientTransport {
58
58
  resolve({ err: evt.reason });
59
59
  };
60
60
  const onError = (evt) => {
61
+ const err = evt.error;
61
62
  ws.removeEventListener("error", onError);
62
63
  ws.removeEventListener("close", onClose);
63
- resolve({ err: evt.message });
64
+ resolve({ err: err.message || err.code || "unknown error" });
64
65
  };
65
66
  ws.addEventListener("open", onOpen);
66
67
  ws.addEventListener("close", onClose);
@@ -235,7 +235,7 @@ var Session = class {
235
235
  log?.info(
236
236
  `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
237
237
  );
238
- this.halfCloseConnection();
238
+ this.closeStaleConnection(this.connection);
239
239
  }
240
240
  return;
241
241
  }
@@ -296,17 +296,27 @@ var Session = class {
296
296
  this.cancelGrace();
297
297
  this.connection = newConn;
298
298
  }
299
+ graceCb;
299
300
  beginGrace(cb) {
301
+ this.graceCb = cb;
300
302
  this.disconnectionGrace = setTimeout(() => {
301
- this.resetBufferedMessages();
302
- clearInterval(this.heartbeat);
303
+ this.close();
303
304
  cb();
304
305
  }, SESSION_DISCONNECT_GRACE_MS);
305
306
  }
307
+ // called on reconnect of the underlying session
306
308
  cancelGrace() {
307
309
  this.heartbeatMisses = 0;
308
310
  clearTimeout(this.disconnectionGrace);
309
311
  }
312
+ // closed when we want to discard the whole session
313
+ // (i.e. shutdown or session disconnect)
314
+ close() {
315
+ this.closeStaleConnection(this.connection);
316
+ this.cancelGrace();
317
+ clearInterval(this.heartbeat);
318
+ this.resetBufferedMessages();
319
+ }
310
320
  get connected() {
311
321
  return this.connection !== void 0;
312
322
  }
@@ -325,15 +335,6 @@ var Session = class {
325
335
  this.seq++;
326
336
  return msg;
327
337
  }
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
338
  inspectSendBuffer() {
338
339
  return this.sendBuffer;
339
340
  }
@@ -485,19 +486,18 @@ var Transport = class {
485
486
  status: "connect",
486
487
  conn
487
488
  });
488
- let session = this.sessions.get(connectedTo);
489
+ let oldSession = this.sessions.get(connectedTo);
489
490
  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`
491
+ if (oldSession && lastInstanceId !== void 0 && lastInstanceId !== instanceId) {
492
+ log?.warn(
493
+ `${this.clientId} -- handshake from ${connectedTo} is a different instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
493
494
  );
494
- session.resetBufferedMessages();
495
- session.closeStaleConnection();
496
- this.deleteSession(session);
497
- session = void 0;
495
+ oldSession.close();
496
+ this.deleteSession(oldSession);
497
+ oldSession = void 0;
498
498
  }
499
499
  this.connectedInstanceIds.set(connectedTo, instanceId);
500
- if (session === void 0) {
500
+ if (oldSession === void 0) {
501
501
  const newSession = this.createSession(connectedTo, conn);
502
502
  log?.info(
503
503
  `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
@@ -505,11 +505,11 @@ var Transport = class {
505
505
  return newSession;
506
506
  }
507
507
  log?.info(
508
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
508
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.debugId}) to ${connectedTo}`
509
509
  );
510
- session.replaceWithNewConnection(conn);
511
- session.sendBufferedMessages();
512
- return session;
510
+ oldSession.replaceWithNewConnection(conn);
511
+ oldSession.sendBufferedMessages();
512
+ return oldSession;
513
513
  }
514
514
  createSession(connectedTo, conn) {
515
515
  const session = new Session(
@@ -527,24 +527,25 @@ var Transport = class {
527
527
  }
528
528
  deleteSession(session) {
529
529
  this.sessions.delete(session.to);
530
+ log?.info(
531
+ `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
532
+ );
530
533
  this.eventDispatcher.dispatchEvent("sessionStatus", {
531
534
  status: "disconnect",
532
535
  session
533
536
  });
534
- log?.info(
535
- `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
536
- );
537
537
  }
538
538
  /**
539
539
  * The downstream implementation needs to call this when a connection is closed.
540
540
  * @param conn The connection object.
541
+ * @param connectedTo The peer we are connected to.
541
542
  */
542
543
  onDisconnect(conn, connectedTo) {
543
544
  this.eventDispatcher.dispatchEvent("connectionStatus", {
544
545
  status: "disconnect",
545
546
  conn
546
547
  });
547
- if (!connectedTo)
548
+ if (this.state !== "open")
548
549
  return;
549
550
  const session = this.sessionByClientId(connectedTo);
550
551
  log?.info(
@@ -587,6 +588,8 @@ var Transport = class {
587
588
  * @param msg The received message.
588
589
  */
589
590
  handleMsg(msg) {
591
+ if (this.state !== "open")
592
+ return;
590
593
  const session = this.sessionByClientId(msg.from);
591
594
  session.cancelGrace();
592
595
  log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
@@ -663,10 +666,11 @@ var Transport = class {
663
666
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
664
667
  */
665
668
  close() {
669
+ this.state = "closed";
666
670
  for (const session of this.sessions.values()) {
667
- session.halfCloseConnection();
671
+ session.close();
672
+ this.deleteSession(session);
668
673
  }
669
- this.state = "closed";
670
674
  log?.info(`${this.clientId} -- manually closed transport`);
671
675
  }
672
676
  /**
@@ -675,10 +679,11 @@ var Transport = class {
675
679
  * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
676
680
  */
677
681
  destroy() {
682
+ this.state = "destroyed";
678
683
  for (const session of this.sessions.values()) {
679
- session.closeStaleConnection(session.connection);
684
+ session.close();
685
+ this.deleteSession(session);
680
686
  }
681
- this.state = "destroyed";
682
687
  log?.info(`${this.clientId} -- manually destroyed transport`);
683
688
  }
684
689
  };
@@ -1,7 +1,7 @@
1
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
1
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
2
2
  import { WebSocketServer } from 'ws';
3
3
  import { WebSocket } from 'isomorphic-ws';
4
- import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
4
+ import { W as WebSocketConnection } from '../../../connection-4a839b9a.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
 
@@ -1,7 +1,7 @@
1
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
1
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
2
2
  import { WebSocketServer } from 'ws';
3
3
  import { WebSocket } from 'isomorphic-ws';
4
- import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
4
+ import { W as WebSocketConnection } from '../../../connection-4a839b9a.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
 
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "../../../chunk-RDTTKCGV.js";
3
+ } from "../../../chunk-6OAKPO5R.js";
4
4
  import {
5
5
  ServerTransport
6
- } from "../../../chunk-QEYN2Z6O.js";
6
+ } from "../../../chunk-2PKONGC3.js";
7
7
  import "../../../chunk-GZ7HCLLM.js";
8
8
  import "../../../chunk-XFFS4UOD.js";
9
9
  import {
@@ -257,7 +257,7 @@ var Session = class {
257
257
  log?.info(
258
258
  `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
259
259
  );
260
- this.halfCloseConnection();
260
+ this.closeStaleConnection(this.connection);
261
261
  }
262
262
  return;
263
263
  }
@@ -318,17 +318,27 @@ var Session = class {
318
318
  this.cancelGrace();
319
319
  this.connection = newConn;
320
320
  }
321
+ graceCb;
321
322
  beginGrace(cb) {
323
+ this.graceCb = cb;
322
324
  this.disconnectionGrace = setTimeout(() => {
323
- this.resetBufferedMessages();
324
- clearInterval(this.heartbeat);
325
+ this.close();
325
326
  cb();
326
327
  }, SESSION_DISCONNECT_GRACE_MS);
327
328
  }
329
+ // called on reconnect of the underlying session
328
330
  cancelGrace() {
329
331
  this.heartbeatMisses = 0;
330
332
  clearTimeout(this.disconnectionGrace);
331
333
  }
334
+ // closed when we want to discard the whole session
335
+ // (i.e. shutdown or session disconnect)
336
+ close() {
337
+ this.closeStaleConnection(this.connection);
338
+ this.cancelGrace();
339
+ clearInterval(this.heartbeat);
340
+ this.resetBufferedMessages();
341
+ }
332
342
  get connected() {
333
343
  return this.connection !== void 0;
334
344
  }
@@ -347,15 +357,6 @@ var Session = class {
347
357
  this.seq++;
348
358
  return msg;
349
359
  }
350
- /**
351
- * Closes the out-going connection but doesn't remove the listeners
352
- * for incoming messages. The connection will eventually call onClose
353
- * when it is ready to be cleaned up and only then will {@link connection} be set back
354
- * to undefined
355
- */
356
- halfCloseConnection() {
357
- this.connection?.close();
358
- }
359
360
  inspectSendBuffer() {
360
361
  return this.sendBuffer;
361
362
  }
@@ -507,19 +508,18 @@ var Transport = class {
507
508
  status: "connect",
508
509
  conn
509
510
  });
510
- let session = this.sessions.get(connectedTo);
511
+ let oldSession = this.sessions.get(connectedTo);
511
512
  const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
512
- if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
513
- log?.debug(
514
- `${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
513
+ if (oldSession && lastInstanceId !== void 0 && lastInstanceId !== instanceId) {
514
+ log?.warn(
515
+ `${this.clientId} -- handshake from ${connectedTo} is a different instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
515
516
  );
516
- session.resetBufferedMessages();
517
- session.closeStaleConnection();
518
- this.deleteSession(session);
519
- session = void 0;
517
+ oldSession.close();
518
+ this.deleteSession(oldSession);
519
+ oldSession = void 0;
520
520
  }
521
521
  this.connectedInstanceIds.set(connectedTo, instanceId);
522
- if (session === void 0) {
522
+ if (oldSession === void 0) {
523
523
  const newSession = this.createSession(connectedTo, conn);
524
524
  log?.info(
525
525
  `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
@@ -527,11 +527,11 @@ var Transport = class {
527
527
  return newSession;
528
528
  }
529
529
  log?.info(
530
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
530
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.debugId}) to ${connectedTo}`
531
531
  );
532
- session.replaceWithNewConnection(conn);
533
- session.sendBufferedMessages();
534
- return session;
532
+ oldSession.replaceWithNewConnection(conn);
533
+ oldSession.sendBufferedMessages();
534
+ return oldSession;
535
535
  }
536
536
  createSession(connectedTo, conn) {
537
537
  const session = new Session(
@@ -549,24 +549,25 @@ var Transport = class {
549
549
  }
550
550
  deleteSession(session) {
551
551
  this.sessions.delete(session.to);
552
+ log?.info(
553
+ `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
554
+ );
552
555
  this.eventDispatcher.dispatchEvent("sessionStatus", {
553
556
  status: "disconnect",
554
557
  session
555
558
  });
556
- log?.info(
557
- `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
558
- );
559
559
  }
560
560
  /**
561
561
  * The downstream implementation needs to call this when a connection is closed.
562
562
  * @param conn The connection object.
563
+ * @param connectedTo The peer we are connected to.
563
564
  */
564
565
  onDisconnect(conn, connectedTo) {
565
566
  this.eventDispatcher.dispatchEvent("connectionStatus", {
566
567
  status: "disconnect",
567
568
  conn
568
569
  });
569
- if (!connectedTo)
570
+ if (this.state !== "open")
570
571
  return;
571
572
  const session = this.sessionByClientId(connectedTo);
572
573
  log?.info(
@@ -609,6 +610,8 @@ var Transport = class {
609
610
  * @param msg The received message.
610
611
  */
611
612
  handleMsg(msg) {
613
+ if (this.state !== "open")
614
+ return;
612
615
  const session = this.sessionByClientId(msg.from);
613
616
  session.cancelGrace();
614
617
  log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
@@ -685,10 +688,11 @@ var Transport = class {
685
688
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
686
689
  */
687
690
  close() {
691
+ this.state = "closed";
688
692
  for (const session of this.sessions.values()) {
689
- session.halfCloseConnection();
693
+ session.close();
694
+ this.deleteSession(session);
690
695
  }
691
- this.state = "closed";
692
696
  log?.info(`${this.clientId} -- manually closed transport`);
693
697
  }
694
698
  /**
@@ -697,10 +701,11 @@ var Transport = class {
697
701
  * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
698
702
  */
699
703
  destroy() {
704
+ this.state = "destroyed";
700
705
  for (const session of this.sessions.values()) {
701
- session.closeStaleConnection(session.connection);
706
+ session.close();
707
+ this.deleteSession(session);
702
708
  }
703
- this.state = "destroyed";
704
709
  log?.info(`${this.clientId} -- manually destroyed transport`);
705
710
  }
706
711
  };
@@ -806,8 +811,7 @@ var ClientTransport = class extends Transport {
806
811
  return bootHandler;
807
812
  }
808
813
  onDisconnect(conn, connectedTo) {
809
- if (connectedTo)
810
- this.inflightConnectionPromises.delete(connectedTo);
814
+ this.inflightConnectionPromises.delete(connectedTo);
811
815
  super.onDisconnect(conn, connectedTo);
812
816
  }
813
817
  };
@@ -1,3 +1,3 @@
1
- export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-0c0a69f6.js';
1
+ export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-1afc5580.js';
2
2
  import '../types-3e5768ec.js';
3
3
  import '@sinclair/typebox';
@@ -1,3 +1,3 @@
1
- export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-0c0a69f6.js';
1
+ export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-1afc5580.js';
2
2
  import '../types-3e5768ec.js';
3
3
  import '@sinclair/typebox';
@@ -5,7 +5,7 @@ import {
5
5
  ServerTransport,
6
6
  Session,
7
7
  Transport
8
- } from "../chunk-QEYN2Z6O.js";
8
+ } from "../chunk-2PKONGC3.js";
9
9
  import "../chunk-GZ7HCLLM.js";
10
10
  import {
11
11
  OpaqueTransportMessageSchema,
@@ -1,7 +1,7 @@
1
1
  import * as it_pushable from 'it-pushable';
2
2
  import WebSocket from 'isomorphic-ws';
3
3
  import http from 'node:http';
4
- import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-0c0a69f6.js';
4
+ import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-1afc5580.js';
5
5
  import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema } from '../builder-c593de11.js';
6
6
  import { Static } from '@sinclair/typebox';
7
7
  import net from 'node:net';
@@ -1,7 +1,7 @@
1
1
  import * as it_pushable from 'it-pushable';
2
2
  import WebSocket from 'isomorphic-ws';
3
3
  import http from 'node:http';
4
- import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-0c0a69f6.js';
4
+ import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-1afc5580.js';
5
5
  import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema } from '../builder-c593de11.js';
6
6
  import { Static } from '@sinclair/typebox';
7
7
  import net from 'node:net';
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  UNCAUGHT_ERROR,
3
3
  pushable
4
- } from "../chunk-55XUAPC6.js";
4
+ } from "../chunk-AFLZ6INU.js";
5
5
  import {
6
6
  coerceErrorString
7
7
  } from "../chunk-XFFS4UOD.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@replit/river",
3
3
  "description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
4
- "version": "0.12.1",
4
+ "version": "0.12.3",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": {