@replit/river 0.25.1 → 0.25.2

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 (50) hide show
  1. package/dist/{chunk-SZ5NBBX7.js → chunk-EHD2JJBW.js} +2 -2
  2. package/dist/{chunk-SZ5NBBX7.js.map → chunk-EHD2JJBW.js.map} +1 -1
  3. package/dist/{chunk-SGSRNAWJ.js → chunk-GDHQCPWW.js} +2 -2
  4. package/dist/{chunk-5CNNIOAO.js → chunk-ONIPHO7J.js} +102 -95
  5. package/dist/chunk-ONIPHO7J.js.map +1 -0
  6. package/dist/{chunk-D5O3ERJU.js → chunk-PF74DCPQ.js} +3 -3
  7. package/dist/chunk-PF74DCPQ.js.map +1 -0
  8. package/dist/{chunk-YM5Y4NAT.js → chunk-Q3PZBJXR.js} +199 -117
  9. package/dist/chunk-Q3PZBJXR.js.map +1 -0
  10. package/dist/{chunk-PCBPPTXH.js → chunk-QXMZJ7LF.js} +17 -26
  11. package/dist/chunk-QXMZJ7LF.js.map +1 -0
  12. package/dist/{chunk-MBMEJIPU.js → chunk-ZWMWRM23.js} +2 -2
  13. package/dist/{client-1321630c.d.ts → client-edc8e2de.d.ts} +3 -1
  14. package/dist/{connection-bd907ca6.d.ts → connection-ef50ac3a.d.ts} +1 -1
  15. package/dist/{handshake-3772d7ca.d.ts → handshake-12d57161.d.ts} +131 -83
  16. package/dist/router/index.cjs +1 -1
  17. package/dist/router/index.cjs.map +1 -1
  18. package/dist/router/index.d.cts +7 -7
  19. package/dist/router/index.d.ts +7 -7
  20. package/dist/router/index.js +2 -2
  21. package/dist/{server-f0fd2b98.d.ts → server-f8228636.d.ts} +8 -3
  22. package/dist/{services-8d14ae16.d.ts → services-4b9e5232.d.ts} +2 -2
  23. package/dist/transport/impls/ws/client.cjs +291 -205
  24. package/dist/transport/impls/ws/client.cjs.map +1 -1
  25. package/dist/transport/impls/ws/client.d.cts +3 -3
  26. package/dist/transport/impls/ws/client.d.ts +3 -3
  27. package/dist/transport/impls/ws/client.js +5 -5
  28. package/dist/transport/impls/ws/server.cjs +206 -136
  29. package/dist/transport/impls/ws/server.cjs.map +1 -1
  30. package/dist/transport/impls/ws/server.d.cts +3 -3
  31. package/dist/transport/impls/ws/server.d.ts +3 -3
  32. package/dist/transport/impls/ws/server.js +5 -5
  33. package/dist/transport/index.cjs +304 -226
  34. package/dist/transport/index.cjs.map +1 -1
  35. package/dist/transport/index.d.cts +3 -3
  36. package/dist/transport/index.d.ts +3 -3
  37. package/dist/transport/index.js +5 -5
  38. package/dist/util/testHelpers.cjs +197 -115
  39. package/dist/util/testHelpers.cjs.map +1 -1
  40. package/dist/util/testHelpers.d.cts +5 -4
  41. package/dist/util/testHelpers.d.ts +5 -4
  42. package/dist/util/testHelpers.js +6 -3
  43. package/dist/util/testHelpers.js.map +1 -1
  44. package/package.json +14 -13
  45. package/dist/chunk-5CNNIOAO.js.map +0 -1
  46. package/dist/chunk-D5O3ERJU.js.map +0 -1
  47. package/dist/chunk-PCBPPTXH.js.map +0 -1
  48. package/dist/chunk-YM5Y4NAT.js.map +0 -1
  49. /package/dist/{chunk-SGSRNAWJ.js.map → chunk-GDHQCPWW.js.map} +0 -0
  50. /package/dist/{chunk-MBMEJIPU.js.map → chunk-ZWMWRM23.js.map} +0 -0
@@ -183,7 +183,7 @@ var defaultTransportOptions = {
183
183
  codec: NaiveJsonCodec
184
184
  };
185
185
  var defaultConnectionRetryOptions = {
186
- baseIntervalMs: 250,
186
+ baseIntervalMs: 150,
187
187
  maxJitterMs: 200,
188
188
  maxBackoffMs: 32e3,
189
189
  attemptBudgetCapacity: 5,
@@ -343,6 +343,7 @@ function isAck(controlFlag) {
343
343
  var import_value = require("@sinclair/typebox/value");
344
344
  var SessionState = /* @__PURE__ */ ((SessionState2) => {
345
345
  SessionState2["NoConnection"] = "NoConnection";
346
+ SessionState2["BackingOff"] = "BackingOff";
346
347
  SessionState2["Connecting"] = "Connecting";
347
348
  SessionState2["Handshaking"] = "Handshaking";
348
349
  SessionState2["Connected"] = "Connected";
@@ -400,7 +401,7 @@ var CommonSession = class extends StateMachineState {
400
401
  from;
401
402
  options;
402
403
  log;
403
- constructor(from, options, log) {
404
+ constructor({ from, options, log }) {
404
405
  super();
405
406
  this.from = from;
406
407
  this.options = options;
@@ -441,8 +442,9 @@ var IdentifiedSession = class extends CommonSession {
441
442
  */
442
443
  ack;
443
444
  sendBuffer;
444
- constructor(id, from, to, seq, ack, sendBuffer, telemetry, options, log) {
445
- super(from, options, log);
445
+ constructor(props) {
446
+ const { id, to, seq, ack, sendBuffer, telemetry, log } = props;
447
+ super(props);
446
448
  this.id = id;
447
449
  this.to = to;
448
450
  this.seq = seq;
@@ -497,23 +499,23 @@ var SessionConnecting = class extends IdentifiedSession {
497
499
  connPromise;
498
500
  listeners;
499
501
  connectionTimeout;
500
- constructor(connPromise, listeners, ...args) {
501
- super(...args);
502
- this.connPromise = connPromise;
503
- this.listeners = listeners;
502
+ constructor(props) {
503
+ super(props);
504
+ this.connPromise = props.connPromise;
505
+ this.listeners = props.listeners;
504
506
  this.connectionTimeout = setTimeout(() => {
505
- listeners.onConnectionTimeout();
507
+ this.listeners.onConnectionTimeout();
506
508
  }, this.options.connectionTimeoutMs);
507
- connPromise.then(
509
+ this.connPromise.then(
508
510
  (conn) => {
509
511
  if (this._isConsumed)
510
512
  return;
511
- listeners.onConnectionEstablished(conn);
513
+ this.listeners.onConnectionEstablished(conn);
512
514
  },
513
515
  (err) => {
514
516
  if (this._isConsumed)
515
517
  return;
516
- listeners.onConnectionFailed(err);
518
+ this.listeners.onConnectionFailed(err);
517
519
  }
518
520
  );
519
521
  }
@@ -539,9 +541,9 @@ var SessionNoConnection = class extends IdentifiedSession {
539
541
  state = "NoConnection" /* NoConnection */;
540
542
  listeners;
541
543
  gracePeriodTimeout;
542
- constructor(listeners, ...args) {
543
- super(...args);
544
- this.listeners = listeners;
544
+ constructor(props) {
545
+ super(props);
546
+ this.listeners = props.listeners;
545
547
  this.gracePeriodTimeout = setTimeout(() => {
546
548
  this.listeners.onSessionGracePeriodElapsed();
547
549
  }, this.options.sessionDisconnectGraceMs);
@@ -562,7 +564,7 @@ var SessionNoConnection = class extends IdentifiedSession {
562
564
  var import_api = require("@opentelemetry/api");
563
565
 
564
566
  // package.json
565
- var version = "0.25.1";
567
+ var version = "0.25.2";
566
568
 
567
569
  // tracing/index.ts
568
570
  function getPropagationContext(ctx) {
@@ -599,16 +601,16 @@ var SessionWaitingForHandshake = class extends CommonSession {
599
601
  conn;
600
602
  listeners;
601
603
  handshakeTimeout;
602
- constructor(conn, listeners, ...args) {
603
- super(...args);
604
- this.conn = conn;
605
- this.listeners = listeners;
604
+ constructor(props) {
605
+ super(props);
606
+ this.conn = props.conn;
607
+ this.listeners = props.listeners;
606
608
  this.handshakeTimeout = setTimeout(() => {
607
- listeners.onHandshakeTimeout();
609
+ this.listeners.onHandshakeTimeout();
608
610
  }, this.options.handshakeTimeoutMs);
609
611
  this.conn.addDataListener(this.onHandshakeData);
610
- this.conn.addErrorListener(listeners.onConnectionErrored);
611
- this.conn.addCloseListener(listeners.onConnectionClosed);
612
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
613
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
612
614
  }
613
615
  onHandshakeData = (msg) => {
614
616
  const parsedMsg = this.parseMsg(msg);
@@ -645,16 +647,16 @@ var SessionHandshaking = class extends IdentifiedSession {
645
647
  conn;
646
648
  listeners;
647
649
  handshakeTimeout;
648
- constructor(conn, listeners, ...args) {
649
- super(...args);
650
- this.conn = conn;
651
- this.listeners = listeners;
650
+ constructor(props) {
651
+ super(props);
652
+ this.conn = props.conn;
653
+ this.listeners = props.listeners;
652
654
  this.handshakeTimeout = setTimeout(() => {
653
- listeners.onHandshakeTimeout();
655
+ this.listeners.onHandshakeTimeout();
654
656
  }, this.options.handshakeTimeoutMs);
655
657
  this.conn.addDataListener(this.onHandshakeData);
656
- this.conn.addErrorListener(listeners.onConnectionErrored);
657
- this.conn.addCloseListener(listeners.onConnectionClosed);
658
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
659
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
658
660
  }
659
661
  onHandshakeData = (msg) => {
660
662
  const parsedMsg = this.parseMsg(msg);
@@ -686,16 +688,13 @@ var SessionConnected = class extends IdentifiedSession {
686
688
  state = "Connected" /* Connected */;
687
689
  conn;
688
690
  listeners;
689
- activeHeartbeatHandle;
690
- activeHeartbeatMisses = 0;
691
- passiveHeartbeatHandle;
692
- get isActivelyHeartbeating() {
693
- return this.activeHeartbeatHandle !== void 0;
694
- }
691
+ heartbeatHandle;
692
+ heartbeatMisses = 0;
693
+ isActivelyHeartbeating;
695
694
  updateBookkeeping(ack, seq) {
696
695
  this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
697
696
  this.ack = seq + 1;
698
- this.activeHeartbeatMisses = 0;
697
+ this.heartbeatMisses = 0;
699
698
  }
700
699
  send(msg) {
701
700
  const constructedMsg = this.constructMsg(msg);
@@ -703,13 +702,13 @@ var SessionConnected = class extends IdentifiedSession {
703
702
  this.conn.send(this.options.codec.toBuffer(constructedMsg));
704
703
  return constructedMsg.id;
705
704
  }
706
- constructor(conn, listeners, ...args) {
707
- super(...args);
708
- this.conn = conn;
709
- this.listeners = listeners;
705
+ constructor(props) {
706
+ super(props);
707
+ this.conn = props.conn;
708
+ this.listeners = props.listeners;
710
709
  this.conn.addDataListener(this.onMessageData);
711
- this.conn.addCloseListener(listeners.onConnectionClosed);
712
- this.conn.addErrorListener(listeners.onConnectionErrored);
710
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
711
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
713
712
  if (this.sendBuffer.length > 0) {
714
713
  this.log?.debug(
715
714
  `sending ${this.sendBuffer.length} buffered messages`,
@@ -717,12 +716,11 @@ var SessionConnected = class extends IdentifiedSession {
717
716
  );
718
717
  }
719
718
  for (const msg of this.sendBuffer) {
720
- conn.send(this.options.codec.toBuffer(msg));
719
+ this.conn.send(this.options.codec.toBuffer(msg));
721
720
  }
722
- }
723
- startActiveHeartbeat() {
724
- this.activeHeartbeatHandle = setInterval(() => {
725
- const misses = this.activeHeartbeatMisses;
721
+ this.isActivelyHeartbeating = false;
722
+ this.heartbeatHandle = setInterval(() => {
723
+ const misses = this.heartbeatMisses;
726
724
  const missDuration = misses * this.options.heartbeatIntervalMs;
727
725
  if (misses >= this.options.heartbeatsUntilDead) {
728
726
  this.log?.info(
@@ -731,29 +729,18 @@ var SessionConnected = class extends IdentifiedSession {
731
729
  );
732
730
  this.telemetry.span.addEvent("closing connection due to inactivity");
733
731
  this.conn.close();
734
- clearInterval(this.activeHeartbeatHandle);
735
- this.activeHeartbeatHandle = void 0;
732
+ clearInterval(this.heartbeatHandle);
733
+ this.heartbeatHandle = void 0;
736
734
  return;
737
735
  }
738
- this.sendHeartbeat();
739
- this.activeHeartbeatMisses++;
736
+ if (this.isActivelyHeartbeating) {
737
+ this.sendHeartbeat();
738
+ }
739
+ this.heartbeatMisses++;
740
740
  }, this.options.heartbeatIntervalMs);
741
741
  }
742
- waitForNextHeartbeat() {
743
- const duration = this.options.heartbeatsUntilDead * this.options.heartbeatIntervalMs;
744
- if (this.passiveHeartbeatHandle) {
745
- clearTimeout(this.passiveHeartbeatHandle);
746
- this.passiveHeartbeatHandle = void 0;
747
- }
748
- this.passiveHeartbeatHandle = setTimeout(() => {
749
- this.log?.info(
750
- `closing connection to ${this.to} due to not receiving a heartbeat in the last ${duration}ms`,
751
- this.loggingMetadata
752
- );
753
- this.telemetry.span.addEvent("closing connection due to inactivity");
754
- this.conn.close();
755
- this.passiveHeartbeatHandle = void 0;
756
- }, duration);
742
+ startActiveHeartbeat() {
743
+ this.isActivelyHeartbeating = true;
757
744
  }
758
745
  sendHeartbeat() {
759
746
  this.log?.debug("sending heartbeat", this.loggingMetadata);
@@ -808,7 +795,6 @@ var SessionConnected = class extends IdentifiedSession {
808
795
  });
809
796
  if (!this.isActivelyHeartbeating) {
810
797
  this.sendHeartbeat();
811
- this.waitForNextHeartbeat();
812
798
  }
813
799
  };
814
800
  _handleStateExit() {
@@ -816,10 +802,8 @@ var SessionConnected = class extends IdentifiedSession {
816
802
  this.conn.removeDataListener(this.onMessageData);
817
803
  this.conn.removeCloseListener(this.listeners.onConnectionClosed);
818
804
  this.conn.removeErrorListener(this.listeners.onConnectionErrored);
819
- clearInterval(this.activeHeartbeatHandle);
820
- clearTimeout(this.passiveHeartbeatHandle);
821
- this.activeHeartbeatHandle = void 0;
822
- this.passiveHeartbeatHandle = void 0;
805
+ clearInterval(this.heartbeatHandle);
806
+ this.heartbeatHandle = void 0;
823
807
  }
824
808
  _handleClose() {
825
809
  super._handleClose();
@@ -827,52 +811,76 @@ var SessionConnected = class extends IdentifiedSession {
827
811
  }
828
812
  };
829
813
 
814
+ // transport/sessionStateMachine/SessionBackingOff.ts
815
+ var SessionBackingOff = class extends IdentifiedSession {
816
+ state = "BackingOff" /* BackingOff */;
817
+ listeners;
818
+ backoffTimeout;
819
+ constructor(props) {
820
+ super(props);
821
+ this.listeners = props.listeners;
822
+ this.backoffTimeout = setTimeout(() => {
823
+ this.listeners.onBackoffFinished();
824
+ }, props.backoffMs);
825
+ }
826
+ _handleClose() {
827
+ super._handleClose();
828
+ }
829
+ _handleStateExit() {
830
+ super._handleStateExit();
831
+ if (this.backoffTimeout) {
832
+ clearTimeout(this.backoffTimeout);
833
+ this.backoffTimeout = void 0;
834
+ }
835
+ }
836
+ };
837
+
830
838
  // transport/sessionStateMachine/transitions.ts
831
839
  function inheritSharedSession(session) {
832
- return [
833
- session.id,
834
- session.from,
835
- session.to,
836
- session.seq,
837
- session.ack,
838
- session.sendBuffer,
839
- session.telemetry,
840
- session.options,
841
- session.log
842
- ];
840
+ return {
841
+ id: session.id,
842
+ from: session.from,
843
+ to: session.to,
844
+ seq: session.seq,
845
+ ack: session.ack,
846
+ sendBuffer: session.sendBuffer,
847
+ telemetry: session.telemetry,
848
+ options: session.options,
849
+ log: session.log
850
+ };
843
851
  }
844
852
  var SessionStateGraph = {
845
853
  entrypoints: {
846
- NoConnection(to, from, listeners, options, log) {
854
+ NoConnection: (to, from, listeners, options, log) => {
847
855
  const id = `session-${generateId()}`;
848
856
  const telemetry = createSessionTelemetryInfo(id, to, from);
849
857
  const sendBuffer = [];
850
- const session = new SessionNoConnection(
858
+ const session = new SessionNoConnection({
851
859
  listeners,
852
860
  id,
853
861
  from,
854
862
  to,
855
- 0,
856
- 0,
863
+ seq: 0,
864
+ ack: 0,
857
865
  sendBuffer,
858
866
  telemetry,
859
867
  options,
860
868
  log
861
- );
869
+ });
862
870
  session.log?.info(`session ${session.id} created in NoConnection state`, {
863
871
  ...session.loggingMetadata,
864
872
  tags: ["state-transition"]
865
873
  });
866
874
  return session;
867
875
  },
868
- WaitingForHandshake(from, conn, listeners, options, log) {
869
- const session = new SessionWaitingForHandshake(
876
+ WaitingForHandshake: (from, conn, listeners, options, log) => {
877
+ const session = new SessionWaitingForHandshake({
870
878
  conn,
871
879
  listeners,
872
880
  from,
873
881
  options,
874
882
  log
875
- );
883
+ });
876
884
  session.log?.info(`session created in WaitingForHandshake state`, {
877
885
  ...session.loggingMetadata,
878
886
  tags: ["state-transition"]
@@ -884,16 +892,33 @@ var SessionStateGraph = {
884
892
  // After a session is transitioned, any usage of the old session will throw.
885
893
  transition: {
886
894
  // happy path transitions
887
- NoConnectionToConnecting(oldSession, connPromise, listeners) {
895
+ NoConnectionToBackingOff: (oldSession, backoffMs, listeners) => {
888
896
  const carriedState = inheritSharedSession(oldSession);
889
897
  oldSession._handleStateExit();
890
- const session = new SessionConnecting(
891
- connPromise,
898
+ const session = new SessionBackingOff({
899
+ backoffMs,
892
900
  listeners,
893
901
  ...carriedState
902
+ });
903
+ session.log?.info(
904
+ `session ${session.id} transition from NoConnection to BackingOff`,
905
+ {
906
+ ...session.loggingMetadata,
907
+ tags: ["state-transition"]
908
+ }
894
909
  );
910
+ return session;
911
+ },
912
+ BackingOffToConnecting: (oldSession, connPromise, listeners) => {
913
+ const carriedState = inheritSharedSession(oldSession);
914
+ oldSession._handleStateExit();
915
+ const session = new SessionConnecting({
916
+ connPromise,
917
+ listeners,
918
+ ...carriedState
919
+ });
895
920
  session.log?.info(
896
- `session ${session.id} transition from NoConnection to Connecting`,
921
+ `session ${session.id} transition from BackingOff to Connecting`,
897
922
  {
898
923
  ...session.loggingMetadata,
899
924
  tags: ["state-transition"]
@@ -901,10 +926,14 @@ var SessionStateGraph = {
901
926
  );
902
927
  return session;
903
928
  },
904
- ConnectingToHandshaking(oldSession, conn, listeners) {
929
+ ConnectingToHandshaking: (oldSession, conn, listeners) => {
905
930
  const carriedState = inheritSharedSession(oldSession);
906
931
  oldSession._handleStateExit();
907
- const session = new SessionHandshaking(conn, listeners, ...carriedState);
932
+ const session = new SessionHandshaking({
933
+ conn,
934
+ listeners,
935
+ ...carriedState
936
+ });
908
937
  session.log?.info(
909
938
  `session ${session.id} transition from Connecting to Handshaking`,
910
939
  {
@@ -914,11 +943,15 @@ var SessionStateGraph = {
914
943
  );
915
944
  return session;
916
945
  },
917
- HandshakingToConnected(oldSession, listeners) {
946
+ HandshakingToConnected: (oldSession, listeners) => {
918
947
  const carriedState = inheritSharedSession(oldSession);
919
948
  const conn = oldSession.conn;
920
949
  oldSession._handleStateExit();
921
- const session = new SessionConnected(conn, listeners, ...carriedState);
950
+ const session = new SessionConnected({
951
+ conn,
952
+ listeners,
953
+ ...carriedState
954
+ });
922
955
  session.log?.info(
923
956
  `session ${session.id} transition from Handshaking to Connected`,
924
957
  {
@@ -928,7 +961,7 @@ var SessionStateGraph = {
928
961
  );
929
962
  return session;
930
963
  },
931
- WaitingForHandshakeToConnected(pendingSession, oldSession, sessionId, to, propagationCtx, listeners) {
964
+ WaitingForHandshakeToConnected: (pendingSession, oldSession, sessionId, to, propagationCtx, listeners) => {
932
965
  const conn = pendingSession.conn;
933
966
  const { from, options } = pendingSession;
934
967
  const carriedState = oldSession ? (
@@ -936,21 +969,30 @@ var SessionStateGraph = {
936
969
  inheritSharedSession(oldSession)
937
970
  ) : (
938
971
  // old session does not exist, create new state
939
- [
940
- sessionId,
972
+ {
973
+ id: sessionId,
941
974
  from,
942
975
  to,
943
- 0,
944
- 0,
945
- [],
946
- createSessionTelemetryInfo(sessionId, to, from, propagationCtx),
976
+ seq: 0,
977
+ ack: 0,
978
+ sendBuffer: [],
979
+ telemetry: createSessionTelemetryInfo(
980
+ sessionId,
981
+ to,
982
+ from,
983
+ propagationCtx
984
+ ),
947
985
  options,
948
- pendingSession.log
949
- ]
986
+ log: pendingSession.log
987
+ }
950
988
  );
951
989
  pendingSession._handleStateExit();
952
990
  oldSession?._handleStateExit();
953
- const session = new SessionConnected(conn, listeners, ...carriedState);
991
+ const session = new SessionConnected({
992
+ conn,
993
+ listeners,
994
+ ...carriedState
995
+ });
954
996
  session.log?.info(
955
997
  `session ${session.id} transition from WaitingForHandshake to Connected`,
956
998
  {
@@ -961,11 +1003,24 @@ var SessionStateGraph = {
961
1003
  return session;
962
1004
  },
963
1005
  // disconnect paths
964
- ConnectingToNoConnection(oldSession, listeners) {
1006
+ BackingOffToNoConnection: (oldSession, listeners) => {
1007
+ const carriedState = inheritSharedSession(oldSession);
1008
+ oldSession._handleStateExit();
1009
+ const session = new SessionNoConnection({ listeners, ...carriedState });
1010
+ session.log?.info(
1011
+ `session ${session.id} transition from BackingOff to NoConnection`,
1012
+ {
1013
+ ...session.loggingMetadata,
1014
+ tags: ["state-transition"]
1015
+ }
1016
+ );
1017
+ return session;
1018
+ },
1019
+ ConnectingToNoConnection: (oldSession, listeners) => {
965
1020
  const carriedState = inheritSharedSession(oldSession);
966
1021
  oldSession.bestEffortClose();
967
1022
  oldSession._handleStateExit();
968
- const session = new SessionNoConnection(listeners, ...carriedState);
1023
+ const session = new SessionNoConnection({ listeners, ...carriedState });
969
1024
  session.log?.info(
970
1025
  `session ${session.id} transition from Connecting to NoConnection`,
971
1026
  {
@@ -975,11 +1030,11 @@ var SessionStateGraph = {
975
1030
  );
976
1031
  return session;
977
1032
  },
978
- HandshakingToNoConnection(oldSession, listeners) {
1033
+ HandshakingToNoConnection: (oldSession, listeners) => {
979
1034
  const carriedState = inheritSharedSession(oldSession);
980
1035
  oldSession.conn.close();
981
1036
  oldSession._handleStateExit();
982
- const session = new SessionNoConnection(listeners, ...carriedState);
1037
+ const session = new SessionNoConnection({ listeners, ...carriedState });
983
1038
  session.log?.info(
984
1039
  `session ${session.id} transition from Handshaking to NoConnection`,
985
1040
  {
@@ -989,11 +1044,11 @@ var SessionStateGraph = {
989
1044
  );
990
1045
  return session;
991
1046
  },
992
- ConnectedToNoConnection(oldSession, listeners) {
1047
+ ConnectedToNoConnection: (oldSession, listeners) => {
993
1048
  const carriedState = inheritSharedSession(oldSession);
994
1049
  oldSession.conn.close();
995
1050
  oldSession._handleStateExit();
996
- const session = new SessionNoConnection(listeners, ...carriedState);
1051
+ const session = new SessionNoConnection({ listeners, ...carriedState });
997
1052
  session.log?.info(
998
1053
  `session ${session.id} transition from Connected to NoConnection`,
999
1054
  {
@@ -1005,6 +1060,31 @@ var SessionStateGraph = {
1005
1060
  }
1006
1061
  }
1007
1062
  };
1063
+ var transitions = SessionStateGraph.transition;
1064
+ var ClientSessionStateGraph = {
1065
+ entrypoint: SessionStateGraph.entrypoints.NoConnection,
1066
+ transition: {
1067
+ // happy paths
1068
+ NoConnectionToBackingOff: transitions.NoConnectionToBackingOff,
1069
+ BackingOffToConnecting: transitions.BackingOffToConnecting,
1070
+ ConnectingToHandshaking: transitions.ConnectingToHandshaking,
1071
+ HandshakingToConnected: transitions.HandshakingToConnected,
1072
+ // disconnect paths
1073
+ BackingOffToNoConnection: transitions.BackingOffToNoConnection,
1074
+ ConnectingToNoConnection: transitions.ConnectingToNoConnection,
1075
+ HandshakingToNoConnection: transitions.HandshakingToNoConnection,
1076
+ ConnectedToNoConnection: transitions.ConnectedToNoConnection
1077
+ }
1078
+ };
1079
+ var ServerSessionStateGraph = {
1080
+ entrypoint: SessionStateGraph.entrypoints.WaitingForHandshake,
1081
+ transition: {
1082
+ // happy paths
1083
+ WaitingForHandshakeToConnected: transitions.WaitingForHandshakeToConnected,
1084
+ // disconnect paths
1085
+ ConnectedToNoConnection: transitions.ConnectedToNoConnection
1086
+ }
1087
+ };
1008
1088
 
1009
1089
  // transport/transport.ts
1010
1090
  var Transport = class {
@@ -1163,17 +1243,17 @@ var import_api3 = require("@opentelemetry/api");
1163
1243
  // transport/rateLimit.ts
1164
1244
  var LeakyBucketRateLimit = class {
1165
1245
  budgetConsumed;
1166
- intervalHandles;
1246
+ intervalHandle;
1167
1247
  options;
1168
1248
  constructor(options) {
1169
1249
  this.options = options;
1170
- this.budgetConsumed = /* @__PURE__ */ new Map();
1171
- this.intervalHandles = /* @__PURE__ */ new Map();
1250
+ this.budgetConsumed = 0;
1172
1251
  }
1173
- getBackoffMs(user) {
1174
- if (!this.budgetConsumed.has(user))
1252
+ getBackoffMs() {
1253
+ if (this.getBudgetConsumed() === 0) {
1175
1254
  return 0;
1176
- const exponent = Math.max(0, this.getBudgetConsumed(user) - 1);
1255
+ }
1256
+ const exponent = Math.max(0, this.getBudgetConsumed() - 1);
1177
1257
  const jitter = Math.floor(Math.random() * this.options.maxJitterMs);
1178
1258
  const backoffMs = Math.min(
1179
1259
  this.options.baseIntervalMs * 2 ** exponent,
@@ -1184,50 +1264,46 @@ var LeakyBucketRateLimit = class {
1184
1264
  get totalBudgetRestoreTime() {
1185
1265
  return this.options.budgetRestoreIntervalMs * this.options.attemptBudgetCapacity;
1186
1266
  }
1187
- consumeBudget(user) {
1188
- this.stopLeak(user);
1189
- this.budgetConsumed.set(user, this.getBudgetConsumed(user) + 1);
1267
+ consumeBudget() {
1268
+ this.stopLeak();
1269
+ this.budgetConsumed = this.getBudgetConsumed() + 1;
1190
1270
  }
1191
- getBudgetConsumed(user) {
1192
- return this.budgetConsumed.get(user) ?? 0;
1271
+ getBudgetConsumed() {
1272
+ return this.budgetConsumed;
1193
1273
  }
1194
- hasBudget(user) {
1195
- return this.getBudgetConsumed(user) < this.options.attemptBudgetCapacity;
1274
+ hasBudget() {
1275
+ return this.getBudgetConsumed() < this.options.attemptBudgetCapacity;
1196
1276
  }
1197
- startRestoringBudget(user) {
1198
- if (this.intervalHandles.has(user)) {
1277
+ startRestoringBudget() {
1278
+ if (this.intervalHandle) {
1199
1279
  return;
1200
1280
  }
1201
1281
  const restoreBudgetForUser = () => {
1202
- const currentBudget = this.budgetConsumed.get(user);
1282
+ const currentBudget = this.budgetConsumed;
1203
1283
  if (!currentBudget) {
1204
- this.stopLeak(user);
1284
+ this.stopLeak();
1205
1285
  return;
1206
1286
  }
1207
1287
  const newBudget = currentBudget - 1;
1208
1288
  if (newBudget === 0) {
1209
- this.budgetConsumed.delete(user);
1210
1289
  return;
1211
1290
  }
1212
- this.budgetConsumed.set(user, newBudget);
1291
+ this.budgetConsumed = newBudget;
1213
1292
  };
1214
- const intervalHandle = setInterval(
1293
+ this.intervalHandle = setInterval(
1215
1294
  restoreBudgetForUser,
1216
1295
  this.options.budgetRestoreIntervalMs
1217
1296
  );
1218
- this.intervalHandles.set(user, intervalHandle);
1219
1297
  }
1220
- stopLeak(user) {
1221
- if (!this.intervalHandles.has(user)) {
1298
+ stopLeak() {
1299
+ if (!this.intervalHandle) {
1222
1300
  return;
1223
1301
  }
1224
- clearInterval(this.intervalHandles.get(user));
1225
- this.intervalHandles.delete(user);
1302
+ clearInterval(this.intervalHandle);
1303
+ this.intervalHandle = void 0;
1226
1304
  }
1227
1305
  close() {
1228
- for (const user of this.intervalHandles.keys()) {
1229
- this.stopLeak(user);
1230
- }
1306
+ this.stopLeak();
1231
1307
  }
1232
1308
  };
1233
1309
 
@@ -1258,8 +1334,10 @@ var ClientTransport = class extends Transport {
1258
1334
  * Optional handshake options for this client.
1259
1335
  */
1260
1336
  handshakeExtensions;
1337
+ sessions;
1261
1338
  constructor(clientId, providedOptions) {
1262
1339
  super(clientId, providedOptions);
1340
+ this.sessions = /* @__PURE__ */ new Map();
1263
1341
  this.options = {
1264
1342
  ...defaultClientTransportOptions,
1265
1343
  ...providedOptions
@@ -1291,7 +1369,7 @@ var ClientTransport = class extends Transport {
1291
1369
  return session.send(msg);
1292
1370
  }
1293
1371
  createUnconnectedSession(to) {
1294
- const session = SessionStateGraph.entrypoints.NoConnection(
1372
+ const session = ClientSessionStateGraph.entrypoint(
1295
1373
  to,
1296
1374
  this.clientId,
1297
1375
  {
@@ -1317,40 +1395,44 @@ var ClientTransport = class extends Transport {
1317
1395
  return noConnectionSession;
1318
1396
  }
1319
1397
  onConnectionEstablished(session, conn) {
1320
- const handshakingSession = SessionStateGraph.transition.ConnectingToHandshaking(session, conn, {
1321
- onConnectionErrored: (err) => {
1322
- const errStr = coerceErrorString(err);
1323
- this.log?.error(
1324
- `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
1325
- handshakingSession.loggingMetadata
1326
- );
1327
- },
1328
- onConnectionClosed: () => {
1329
- this.log?.warn(
1330
- `connection to ${handshakingSession.to} closed during handshake`,
1331
- handshakingSession.loggingMetadata
1332
- );
1333
- this.onConnClosed(handshakingSession);
1334
- },
1335
- onHandshake: (msg) => {
1336
- this.onHandshakeResponse(handshakingSession, msg);
1337
- },
1338
- onInvalidHandshake: (reason) => {
1339
- this.log?.error(
1340
- `invalid handshake: ${reason}`,
1341
- handshakingSession.loggingMetadata
1342
- );
1343
- this.deleteSession(session);
1344
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1345
- },
1346
- onHandshakeTimeout: () => {
1347
- this.log?.error(
1348
- `connection to ${handshakingSession.to} timed out during handshake`,
1349
- handshakingSession.loggingMetadata
1350
- );
1351
- this.onConnClosed(handshakingSession);
1398
+ const handshakingSession = ClientSessionStateGraph.transition.ConnectingToHandshaking(
1399
+ session,
1400
+ conn,
1401
+ {
1402
+ onConnectionErrored: (err) => {
1403
+ const errStr = coerceErrorString(err);
1404
+ this.log?.error(
1405
+ `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
1406
+ handshakingSession.loggingMetadata
1407
+ );
1408
+ },
1409
+ onConnectionClosed: () => {
1410
+ this.log?.warn(
1411
+ `connection to ${handshakingSession.to} closed during handshake`,
1412
+ handshakingSession.loggingMetadata
1413
+ );
1414
+ this.onConnClosed(handshakingSession);
1415
+ },
1416
+ onHandshake: (msg) => {
1417
+ this.onHandshakeResponse(handshakingSession, msg);
1418
+ },
1419
+ onInvalidHandshake: (reason) => {
1420
+ this.log?.error(
1421
+ `invalid handshake: ${reason}`,
1422
+ handshakingSession.loggingMetadata
1423
+ );
1424
+ this.deleteSession(session);
1425
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
1426
+ },
1427
+ onHandshakeTimeout: () => {
1428
+ this.log?.error(
1429
+ `connection to ${handshakingSession.to} timed out during handshake`,
1430
+ handshakingSession.loggingMetadata
1431
+ );
1432
+ this.onConnClosed(handshakingSession);
1433
+ }
1352
1434
  }
1353
- });
1435
+ );
1354
1436
  this.updateSession(handshakingSession);
1355
1437
  void this.sendHandshake(handshakingSession);
1356
1438
  return handshakingSession;
@@ -1405,7 +1487,7 @@ var ClientTransport = class extends Transport {
1405
1487
  ...session.loggingMetadata,
1406
1488
  transportMessage: msg
1407
1489
  });
1408
- const connectedSession = SessionStateGraph.transition.HandshakingToConnected(session, {
1490
+ const connectedSession = ClientSessionStateGraph.transition.HandshakingToConnected(session, {
1409
1491
  onConnectionErrored: (err) => {
1410
1492
  const errStr = coerceErrorString(err);
1411
1493
  this.log?.warn(
@@ -1427,7 +1509,7 @@ var ClientTransport = class extends Transport {
1427
1509
  }
1428
1510
  });
1429
1511
  this.updateSession(connectedSession);
1430
- this.retryBudget.startRestoringBudget(connectedSession.to);
1512
+ this.retryBudget.startRestoringBudget();
1431
1513
  }
1432
1514
  /**
1433
1515
  * Manually attempts to connect to a client.
@@ -1450,44 +1532,49 @@ var ClientTransport = class extends Transport {
1450
1532
  );
1451
1533
  return;
1452
1534
  }
1453
- if (!this.retryBudget.hasBudget(to)) {
1454
- const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
1535
+ if (!this.retryBudget.hasBudget()) {
1536
+ const budgetConsumed = this.retryBudget.getBudgetConsumed();
1455
1537
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
1456
1538
  this.log?.error(errMsg, session.loggingMetadata);
1457
1539
  this.protocolError(ProtocolError.RetriesExceeded, errMsg);
1458
1540
  return;
1459
1541
  }
1460
- let sleep = Promise.resolve();
1461
- const backoffMs = this.retryBudget.getBackoffMs(to);
1462
- if (backoffMs > 0) {
1463
- sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
1464
- }
1542
+ const backoffMs = this.retryBudget.getBackoffMs();
1465
1543
  this.log?.info(
1466
1544
  `attempting connection to ${to} (${backoffMs}ms backoff)`,
1467
1545
  session.loggingMetadata
1468
1546
  );
1469
- this.retryBudget.consumeBudget(to);
1470
- const reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
1471
- try {
1472
- span.addEvent("backoff", { backoffMs });
1473
- await sleep;
1474
- if (this.getStatus() !== "open") {
1475
- throw new Error("transport state is no longer open");
1547
+ this.retryBudget.consumeBudget();
1548
+ const backingOffSession = ClientSessionStateGraph.transition.NoConnectionToBackingOff(
1549
+ session,
1550
+ backoffMs,
1551
+ {
1552
+ onBackoffFinished: () => {
1553
+ const reconnectPromise = tracing_default.startActiveSpan(
1554
+ "connect",
1555
+ async (span) => {
1556
+ try {
1557
+ return await this.createNewOutgoingConnection(to);
1558
+ } catch (err) {
1559
+ const errStr = coerceErrorString(err);
1560
+ span.recordException(errStr);
1561
+ span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
1562
+ throw err;
1563
+ } finally {
1564
+ span.end();
1565
+ }
1566
+ }
1567
+ );
1568
+ this.onBackoffFinished(backingOffSession, reconnectPromise);
1476
1569
  }
1477
- span.addEvent("connecting");
1478
- return await this.createNewOutgoingConnection(to);
1479
- } catch (err) {
1480
- const errStr = coerceErrorString(err);
1481
- span.recordException(errStr);
1482
- span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
1483
- throw err;
1484
- } finally {
1485
- span.end();
1486
1570
  }
1487
- });
1488
- const connectingSession = SessionStateGraph.transition.NoConnectionToConnecting(
1571
+ );
1572
+ this.updateSession(backingOffSession);
1573
+ }
1574
+ onBackoffFinished(session, connPromise) {
1575
+ const connectingSession = ClientSessionStateGraph.transition.BackingOffToConnecting(
1489
1576
  session,
1490
- reconnectPromise,
1577
+ connPromise,
1491
1578
  {
1492
1579
  onConnectionEstablished: (conn) => {
1493
1580
  this.log?.debug(
@@ -1559,9 +1646,11 @@ var ServerTransport = class extends Transport {
1559
1646
  * A map of session handshake data for each session.
1560
1647
  */
1561
1648
  sessionHandshakeMetadata = /* @__PURE__ */ new Map();
1649
+ sessions = /* @__PURE__ */ new Map();
1562
1650
  pendingSessions = /* @__PURE__ */ new Set();
1563
1651
  constructor(clientId, providedOptions) {
1564
1652
  super(clientId, providedOptions);
1653
+ this.sessions = /* @__PURE__ */ new Map();
1565
1654
  this.options = {
1566
1655
  ...defaultServerTransportOptions,
1567
1656
  ...providedOptions
@@ -1612,7 +1701,7 @@ var ServerTransport = class extends Transport {
1612
1701
  clientId: this.clientId
1613
1702
  });
1614
1703
  let receivedHandshake = false;
1615
- const pendingSession = SessionStateGraph.entrypoints.WaitingForHandshake(
1704
+ const pendingSession = ServerSessionStateGraph.entrypoint(
1616
1705
  this.clientId,
1617
1706
  conn,
1618
1707
  {
@@ -1766,26 +1855,15 @@ var ServerTransport = class extends Transport {
1766
1855
  );
1767
1856
  return;
1768
1857
  }
1769
- if (oldSession.state === "Connected" /* Connected */) {
1770
- const noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(oldSession, {
1771
- onSessionGracePeriodElapsed: () => {
1772
- this.onSessionGracePeriodElapsed(noConnectionSession);
1773
- }
1774
- });
1775
- oldSession = noConnectionSession;
1776
- } else if (oldSession.state === "Handshaking" /* Handshaking */) {
1777
- const noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(oldSession, {
1778
- onSessionGracePeriodElapsed: () => {
1779
- this.onSessionGracePeriodElapsed(noConnectionSession);
1780
- }
1781
- });
1782
- oldSession = noConnectionSession;
1783
- } else if (oldSession.state === "Connecting" /* Connecting */) {
1784
- const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(oldSession, {
1785
- onSessionGracePeriodElapsed: () => {
1786
- this.onSessionGracePeriodElapsed(noConnectionSession);
1858
+ if (oldSession.state !== "NoConnection" /* NoConnection */) {
1859
+ const noConnectionSession = ServerSessionStateGraph.transition.ConnectedToNoConnection(
1860
+ oldSession,
1861
+ {
1862
+ onSessionGracePeriodElapsed: () => {
1863
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1864
+ }
1787
1865
  }
1788
- });
1866
+ );
1789
1867
  oldSession = noConnectionSession;
1790
1868
  }
1791
1869
  this.updateSession(oldSession);
@@ -1834,7 +1912,7 @@ var ServerTransport = class extends Transport {
1834
1912
  }
1835
1913
  });
1836
1914
  session.sendHandshake(responseMsg);
1837
- const connectedSession = SessionStateGraph.transition.WaitingForHandshakeToConnected(
1915
+ const connectedSession = ServerSessionStateGraph.transition.WaitingForHandshakeToConnected(
1838
1916
  session,
1839
1917
  // by this point oldSession is either no connection or we dont have an old session
1840
1918
  oldSession,