@replit/river 0.25.1 → 0.26.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 (54) hide show
  1. package/dist/{chunk-D5O3ERJU.js → chunk-5FDAIAQ5.js} +3 -3
  2. package/dist/chunk-5FDAIAQ5.js.map +1 -0
  3. package/dist/{chunk-5CNNIOAO.js → chunk-5S64PXTU.js} +102 -95
  4. package/dist/chunk-5S64PXTU.js.map +1 -0
  5. package/dist/{chunk-PCBPPTXH.js → chunk-7ETNUCOL.js} +54 -62
  6. package/dist/chunk-7ETNUCOL.js.map +1 -0
  7. package/dist/{chunk-SGSRNAWJ.js → chunk-BNNELZM4.js} +2 -2
  8. package/dist/{chunk-SGSRNAWJ.js.map → chunk-BNNELZM4.js.map} +1 -1
  9. package/dist/{chunk-SZ5NBBX7.js → chunk-CCUYKR5C.js} +14 -4
  10. package/dist/chunk-CCUYKR5C.js.map +1 -0
  11. package/dist/{chunk-YM5Y4NAT.js → chunk-JSU2KACV.js} +199 -117
  12. package/dist/chunk-JSU2KACV.js.map +1 -0
  13. package/dist/{chunk-MBMEJIPU.js → chunk-KP4UB5NW.js} +2 -2
  14. package/dist/{client-1321630c.d.ts → client-162c509c.d.ts} +4 -2
  15. package/dist/{connection-bd907ca6.d.ts → connection-6a404bb8.d.ts} +1 -1
  16. package/dist/{handshake-3772d7ca.d.ts → handshake-3342bb94.d.ts} +132 -84
  17. package/dist/logging/index.d.cts +1 -1
  18. package/dist/logging/index.d.ts +1 -1
  19. package/dist/{message-e6c560fd.d.ts → message-1a434848.d.ts} +2 -1
  20. package/dist/router/index.cjs +12 -3
  21. package/dist/router/index.cjs.map +1 -1
  22. package/dist/router/index.d.cts +8 -8
  23. package/dist/router/index.d.ts +8 -8
  24. package/dist/router/index.js +2 -2
  25. package/dist/{server-f0fd2b98.d.ts → server-1b695374.d.ts} +9 -4
  26. package/dist/{services-8d14ae16.d.ts → services-c17f7eff.d.ts} +3 -3
  27. package/dist/transport/impls/ws/client.cjs +302 -207
  28. package/dist/transport/impls/ws/client.cjs.map +1 -1
  29. package/dist/transport/impls/ws/client.d.cts +4 -4
  30. package/dist/transport/impls/ws/client.d.ts +4 -4
  31. package/dist/transport/impls/ws/client.js +5 -5
  32. package/dist/transport/impls/ws/server.cjs +253 -174
  33. package/dist/transport/impls/ws/server.cjs.map +1 -1
  34. package/dist/transport/impls/ws/server.d.cts +4 -4
  35. package/dist/transport/impls/ws/server.d.ts +4 -4
  36. package/dist/transport/impls/ws/server.js +5 -5
  37. package/dist/transport/index.cjs +351 -264
  38. package/dist/transport/index.cjs.map +1 -1
  39. package/dist/transport/index.d.cts +4 -4
  40. package/dist/transport/index.d.ts +4 -4
  41. package/dist/transport/index.js +5 -5
  42. package/dist/util/testHelpers.cjs +208 -117
  43. package/dist/util/testHelpers.cjs.map +1 -1
  44. package/dist/util/testHelpers.d.cts +6 -5
  45. package/dist/util/testHelpers.d.ts +6 -5
  46. package/dist/util/testHelpers.js +6 -3
  47. package/dist/util/testHelpers.js.map +1 -1
  48. package/package.json +14 -13
  49. package/dist/chunk-5CNNIOAO.js.map +0 -1
  50. package/dist/chunk-D5O3ERJU.js.map +0 -1
  51. package/dist/chunk-PCBPPTXH.js.map +0 -1
  52. package/dist/chunk-SZ5NBBX7.js.map +0 -1
  53. package/dist/chunk-YM5Y4NAT.js.map +0 -1
  54. /package/dist/{chunk-MBMEJIPU.js.map → chunk-KP4UB5NW.js.map} +0 -0
@@ -196,11 +196,20 @@ var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
196
196
  var HandshakeErrorRetriableResponseCodes = import_typebox.Type.Union([
197
197
  import_typebox.Type.Literal("SESSION_STATE_MISMATCH")
198
198
  ]);
199
+ var HandshakeErrorCustomHandlerFatalResponseCodes = import_typebox.Type.Union([
200
+ // The custom validation handler rejected the handler because the client is unsupported.
201
+ import_typebox.Type.Literal("REJECTED_UNSUPPORTED_CLIENT"),
202
+ // The custom validation handler rejected the handshake.
203
+ import_typebox.Type.Literal("REJECTED_BY_CUSTOM_HANDLER")
204
+ ]);
199
205
  var HandshakeErrorFatalResponseCodes = import_typebox.Type.Union([
206
+ HandshakeErrorCustomHandlerFatalResponseCodes,
207
+ // The ciient sent a handshake that doesn't comply with the extended handshake metadata.
200
208
  import_typebox.Type.Literal("MALFORMED_HANDSHAKE_META"),
209
+ // The ciient sent a handshake that doesn't comply with ControlMessageHandshakeRequestSchema.
201
210
  import_typebox.Type.Literal("MALFORMED_HANDSHAKE"),
202
- import_typebox.Type.Literal("PROTOCOL_VERSION_MISMATCH"),
203
- import_typebox.Type.Literal("REJECTED_BY_CUSTOM_HANDLER")
211
+ // The client's protocol version does not match the server's.
212
+ import_typebox.Type.Literal("PROTOCOL_VERSION_MISMATCH")
204
213
  ]);
205
214
  var HandshakeErrorResponseCodes = import_typebox.Type.Union([
206
215
  HandshakeErrorRetriableResponseCodes,
@@ -316,7 +325,7 @@ var defaultTransportOptions = {
316
325
  codec: NaiveJsonCodec
317
326
  };
318
327
  var defaultConnectionRetryOptions = {
319
- baseIntervalMs: 250,
328
+ baseIntervalMs: 150,
320
329
  maxJitterMs: 200,
321
330
  maxBackoffMs: 32e3,
322
331
  attemptBudgetCapacity: 5,
@@ -473,7 +482,7 @@ var CommonSession = class extends StateMachineState {
473
482
  from;
474
483
  options;
475
484
  log;
476
- constructor(from, options, log) {
485
+ constructor({ from, options, log }) {
477
486
  super();
478
487
  this.from = from;
479
488
  this.options = options;
@@ -514,8 +523,9 @@ var IdentifiedSession = class extends CommonSession {
514
523
  */
515
524
  ack;
516
525
  sendBuffer;
517
- constructor(id, from, to, seq, ack, sendBuffer, telemetry, options, log) {
518
- super(from, options, log);
526
+ constructor(props) {
527
+ const { id, to, seq, ack, sendBuffer, telemetry, log } = props;
528
+ super(props);
519
529
  this.id = id;
520
530
  this.to = to;
521
531
  this.seq = seq;
@@ -570,23 +580,23 @@ var SessionConnecting = class extends IdentifiedSession {
570
580
  connPromise;
571
581
  listeners;
572
582
  connectionTimeout;
573
- constructor(connPromise, listeners, ...args) {
574
- super(...args);
575
- this.connPromise = connPromise;
576
- this.listeners = listeners;
583
+ constructor(props) {
584
+ super(props);
585
+ this.connPromise = props.connPromise;
586
+ this.listeners = props.listeners;
577
587
  this.connectionTimeout = setTimeout(() => {
578
- listeners.onConnectionTimeout();
588
+ this.listeners.onConnectionTimeout();
579
589
  }, this.options.connectionTimeoutMs);
580
- connPromise.then(
590
+ this.connPromise.then(
581
591
  (conn) => {
582
592
  if (this._isConsumed)
583
593
  return;
584
- listeners.onConnectionEstablished(conn);
594
+ this.listeners.onConnectionEstablished(conn);
585
595
  },
586
596
  (err) => {
587
597
  if (this._isConsumed)
588
598
  return;
589
- listeners.onConnectionFailed(err);
599
+ this.listeners.onConnectionFailed(err);
590
600
  }
591
601
  );
592
602
  }
@@ -612,9 +622,9 @@ var SessionNoConnection = class extends IdentifiedSession {
612
622
  state = "NoConnection" /* NoConnection */;
613
623
  listeners;
614
624
  gracePeriodTimeout;
615
- constructor(listeners, ...args) {
616
- super(...args);
617
- this.listeners = listeners;
625
+ constructor(props) {
626
+ super(props);
627
+ this.listeners = props.listeners;
618
628
  this.gracePeriodTimeout = setTimeout(() => {
619
629
  this.listeners.onSessionGracePeriodElapsed();
620
630
  }, this.options.sessionDisconnectGraceMs);
@@ -635,7 +645,7 @@ var SessionNoConnection = class extends IdentifiedSession {
635
645
  var import_api = require("@opentelemetry/api");
636
646
 
637
647
  // package.json
638
- var version = "0.25.1";
648
+ var version = "0.26.0";
639
649
 
640
650
  // tracing/index.ts
641
651
  function createSessionTelemetryInfo(sessionId, to, from, propagationCtx) {
@@ -663,16 +673,16 @@ var SessionWaitingForHandshake = class extends CommonSession {
663
673
  conn;
664
674
  listeners;
665
675
  handshakeTimeout;
666
- constructor(conn, listeners, ...args) {
667
- super(...args);
668
- this.conn = conn;
669
- this.listeners = listeners;
676
+ constructor(props) {
677
+ super(props);
678
+ this.conn = props.conn;
679
+ this.listeners = props.listeners;
670
680
  this.handshakeTimeout = setTimeout(() => {
671
- listeners.onHandshakeTimeout();
681
+ this.listeners.onHandshakeTimeout();
672
682
  }, this.options.handshakeTimeoutMs);
673
683
  this.conn.addDataListener(this.onHandshakeData);
674
- this.conn.addErrorListener(listeners.onConnectionErrored);
675
- this.conn.addCloseListener(listeners.onConnectionClosed);
684
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
685
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
676
686
  }
677
687
  onHandshakeData = (msg) => {
678
688
  const parsedMsg = this.parseMsg(msg);
@@ -709,16 +719,16 @@ var SessionHandshaking = class extends IdentifiedSession {
709
719
  conn;
710
720
  listeners;
711
721
  handshakeTimeout;
712
- constructor(conn, listeners, ...args) {
713
- super(...args);
714
- this.conn = conn;
715
- this.listeners = listeners;
722
+ constructor(props) {
723
+ super(props);
724
+ this.conn = props.conn;
725
+ this.listeners = props.listeners;
716
726
  this.handshakeTimeout = setTimeout(() => {
717
- listeners.onHandshakeTimeout();
727
+ this.listeners.onHandshakeTimeout();
718
728
  }, this.options.handshakeTimeoutMs);
719
729
  this.conn.addDataListener(this.onHandshakeData);
720
- this.conn.addErrorListener(listeners.onConnectionErrored);
721
- this.conn.addCloseListener(listeners.onConnectionClosed);
730
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
731
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
722
732
  }
723
733
  onHandshakeData = (msg) => {
724
734
  const parsedMsg = this.parseMsg(msg);
@@ -750,16 +760,13 @@ var SessionConnected = class extends IdentifiedSession {
750
760
  state = "Connected" /* Connected */;
751
761
  conn;
752
762
  listeners;
753
- activeHeartbeatHandle;
754
- activeHeartbeatMisses = 0;
755
- passiveHeartbeatHandle;
756
- get isActivelyHeartbeating() {
757
- return this.activeHeartbeatHandle !== void 0;
758
- }
763
+ heartbeatHandle;
764
+ heartbeatMisses = 0;
765
+ isActivelyHeartbeating;
759
766
  updateBookkeeping(ack, seq) {
760
767
  this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
761
768
  this.ack = seq + 1;
762
- this.activeHeartbeatMisses = 0;
769
+ this.heartbeatMisses = 0;
763
770
  }
764
771
  send(msg) {
765
772
  const constructedMsg = this.constructMsg(msg);
@@ -767,13 +774,13 @@ var SessionConnected = class extends IdentifiedSession {
767
774
  this.conn.send(this.options.codec.toBuffer(constructedMsg));
768
775
  return constructedMsg.id;
769
776
  }
770
- constructor(conn, listeners, ...args) {
771
- super(...args);
772
- this.conn = conn;
773
- this.listeners = listeners;
777
+ constructor(props) {
778
+ super(props);
779
+ this.conn = props.conn;
780
+ this.listeners = props.listeners;
774
781
  this.conn.addDataListener(this.onMessageData);
775
- this.conn.addCloseListener(listeners.onConnectionClosed);
776
- this.conn.addErrorListener(listeners.onConnectionErrored);
782
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
783
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
777
784
  if (this.sendBuffer.length > 0) {
778
785
  this.log?.debug(
779
786
  `sending ${this.sendBuffer.length} buffered messages`,
@@ -781,12 +788,11 @@ var SessionConnected = class extends IdentifiedSession {
781
788
  );
782
789
  }
783
790
  for (const msg of this.sendBuffer) {
784
- conn.send(this.options.codec.toBuffer(msg));
791
+ this.conn.send(this.options.codec.toBuffer(msg));
785
792
  }
786
- }
787
- startActiveHeartbeat() {
788
- this.activeHeartbeatHandle = setInterval(() => {
789
- const misses = this.activeHeartbeatMisses;
793
+ this.isActivelyHeartbeating = false;
794
+ this.heartbeatHandle = setInterval(() => {
795
+ const misses = this.heartbeatMisses;
790
796
  const missDuration = misses * this.options.heartbeatIntervalMs;
791
797
  if (misses >= this.options.heartbeatsUntilDead) {
792
798
  this.log?.info(
@@ -795,29 +801,18 @@ var SessionConnected = class extends IdentifiedSession {
795
801
  );
796
802
  this.telemetry.span.addEvent("closing connection due to inactivity");
797
803
  this.conn.close();
798
- clearInterval(this.activeHeartbeatHandle);
799
- this.activeHeartbeatHandle = void 0;
804
+ clearInterval(this.heartbeatHandle);
805
+ this.heartbeatHandle = void 0;
800
806
  return;
801
807
  }
802
- this.sendHeartbeat();
803
- this.activeHeartbeatMisses++;
808
+ if (this.isActivelyHeartbeating) {
809
+ this.sendHeartbeat();
810
+ }
811
+ this.heartbeatMisses++;
804
812
  }, this.options.heartbeatIntervalMs);
805
813
  }
806
- waitForNextHeartbeat() {
807
- const duration = this.options.heartbeatsUntilDead * this.options.heartbeatIntervalMs;
808
- if (this.passiveHeartbeatHandle) {
809
- clearTimeout(this.passiveHeartbeatHandle);
810
- this.passiveHeartbeatHandle = void 0;
811
- }
812
- this.passiveHeartbeatHandle = setTimeout(() => {
813
- this.log?.info(
814
- `closing connection to ${this.to} due to not receiving a heartbeat in the last ${duration}ms`,
815
- this.loggingMetadata
816
- );
817
- this.telemetry.span.addEvent("closing connection due to inactivity");
818
- this.conn.close();
819
- this.passiveHeartbeatHandle = void 0;
820
- }, duration);
814
+ startActiveHeartbeat() {
815
+ this.isActivelyHeartbeating = true;
821
816
  }
822
817
  sendHeartbeat() {
823
818
  this.log?.debug("sending heartbeat", this.loggingMetadata);
@@ -872,7 +867,6 @@ var SessionConnected = class extends IdentifiedSession {
872
867
  });
873
868
  if (!this.isActivelyHeartbeating) {
874
869
  this.sendHeartbeat();
875
- this.waitForNextHeartbeat();
876
870
  }
877
871
  };
878
872
  _handleStateExit() {
@@ -880,10 +874,8 @@ var SessionConnected = class extends IdentifiedSession {
880
874
  this.conn.removeDataListener(this.onMessageData);
881
875
  this.conn.removeCloseListener(this.listeners.onConnectionClosed);
882
876
  this.conn.removeErrorListener(this.listeners.onConnectionErrored);
883
- clearInterval(this.activeHeartbeatHandle);
884
- clearTimeout(this.passiveHeartbeatHandle);
885
- this.activeHeartbeatHandle = void 0;
886
- this.passiveHeartbeatHandle = void 0;
877
+ clearInterval(this.heartbeatHandle);
878
+ this.heartbeatHandle = void 0;
887
879
  }
888
880
  _handleClose() {
889
881
  super._handleClose();
@@ -891,52 +883,76 @@ var SessionConnected = class extends IdentifiedSession {
891
883
  }
892
884
  };
893
885
 
886
+ // transport/sessionStateMachine/SessionBackingOff.ts
887
+ var SessionBackingOff = class extends IdentifiedSession {
888
+ state = "BackingOff" /* BackingOff */;
889
+ listeners;
890
+ backoffTimeout;
891
+ constructor(props) {
892
+ super(props);
893
+ this.listeners = props.listeners;
894
+ this.backoffTimeout = setTimeout(() => {
895
+ this.listeners.onBackoffFinished();
896
+ }, props.backoffMs);
897
+ }
898
+ _handleClose() {
899
+ super._handleClose();
900
+ }
901
+ _handleStateExit() {
902
+ super._handleStateExit();
903
+ if (this.backoffTimeout) {
904
+ clearTimeout(this.backoffTimeout);
905
+ this.backoffTimeout = void 0;
906
+ }
907
+ }
908
+ };
909
+
894
910
  // transport/sessionStateMachine/transitions.ts
895
911
  function inheritSharedSession(session) {
896
- return [
897
- session.id,
898
- session.from,
899
- session.to,
900
- session.seq,
901
- session.ack,
902
- session.sendBuffer,
903
- session.telemetry,
904
- session.options,
905
- session.log
906
- ];
912
+ return {
913
+ id: session.id,
914
+ from: session.from,
915
+ to: session.to,
916
+ seq: session.seq,
917
+ ack: session.ack,
918
+ sendBuffer: session.sendBuffer,
919
+ telemetry: session.telemetry,
920
+ options: session.options,
921
+ log: session.log
922
+ };
907
923
  }
908
924
  var SessionStateGraph = {
909
925
  entrypoints: {
910
- NoConnection(to, from, listeners, options, log) {
926
+ NoConnection: (to, from, listeners, options, log) => {
911
927
  const id = `session-${generateId()}`;
912
928
  const telemetry = createSessionTelemetryInfo(id, to, from);
913
929
  const sendBuffer = [];
914
- const session = new SessionNoConnection(
930
+ const session = new SessionNoConnection({
915
931
  listeners,
916
932
  id,
917
933
  from,
918
934
  to,
919
- 0,
920
- 0,
935
+ seq: 0,
936
+ ack: 0,
921
937
  sendBuffer,
922
938
  telemetry,
923
939
  options,
924
940
  log
925
- );
941
+ });
926
942
  session.log?.info(`session ${session.id} created in NoConnection state`, {
927
943
  ...session.loggingMetadata,
928
944
  tags: ["state-transition"]
929
945
  });
930
946
  return session;
931
947
  },
932
- WaitingForHandshake(from, conn, listeners, options, log) {
933
- const session = new SessionWaitingForHandshake(
948
+ WaitingForHandshake: (from, conn, listeners, options, log) => {
949
+ const session = new SessionWaitingForHandshake({
934
950
  conn,
935
951
  listeners,
936
952
  from,
937
953
  options,
938
954
  log
939
- );
955
+ });
940
956
  session.log?.info(`session created in WaitingForHandshake state`, {
941
957
  ...session.loggingMetadata,
942
958
  tags: ["state-transition"]
@@ -948,16 +964,33 @@ var SessionStateGraph = {
948
964
  // After a session is transitioned, any usage of the old session will throw.
949
965
  transition: {
950
966
  // happy path transitions
951
- NoConnectionToConnecting(oldSession, connPromise, listeners) {
967
+ NoConnectionToBackingOff: (oldSession, backoffMs, listeners) => {
952
968
  const carriedState = inheritSharedSession(oldSession);
953
969
  oldSession._handleStateExit();
954
- const session = new SessionConnecting(
955
- connPromise,
970
+ const session = new SessionBackingOff({
971
+ backoffMs,
956
972
  listeners,
957
973
  ...carriedState
974
+ });
975
+ session.log?.info(
976
+ `session ${session.id} transition from NoConnection to BackingOff`,
977
+ {
978
+ ...session.loggingMetadata,
979
+ tags: ["state-transition"]
980
+ }
958
981
  );
982
+ return session;
983
+ },
984
+ BackingOffToConnecting: (oldSession, connPromise, listeners) => {
985
+ const carriedState = inheritSharedSession(oldSession);
986
+ oldSession._handleStateExit();
987
+ const session = new SessionConnecting({
988
+ connPromise,
989
+ listeners,
990
+ ...carriedState
991
+ });
959
992
  session.log?.info(
960
- `session ${session.id} transition from NoConnection to Connecting`,
993
+ `session ${session.id} transition from BackingOff to Connecting`,
961
994
  {
962
995
  ...session.loggingMetadata,
963
996
  tags: ["state-transition"]
@@ -965,10 +998,14 @@ var SessionStateGraph = {
965
998
  );
966
999
  return session;
967
1000
  },
968
- ConnectingToHandshaking(oldSession, conn, listeners) {
1001
+ ConnectingToHandshaking: (oldSession, conn, listeners) => {
969
1002
  const carriedState = inheritSharedSession(oldSession);
970
1003
  oldSession._handleStateExit();
971
- const session = new SessionHandshaking(conn, listeners, ...carriedState);
1004
+ const session = new SessionHandshaking({
1005
+ conn,
1006
+ listeners,
1007
+ ...carriedState
1008
+ });
972
1009
  session.log?.info(
973
1010
  `session ${session.id} transition from Connecting to Handshaking`,
974
1011
  {
@@ -978,11 +1015,15 @@ var SessionStateGraph = {
978
1015
  );
979
1016
  return session;
980
1017
  },
981
- HandshakingToConnected(oldSession, listeners) {
1018
+ HandshakingToConnected: (oldSession, listeners) => {
982
1019
  const carriedState = inheritSharedSession(oldSession);
983
1020
  const conn = oldSession.conn;
984
1021
  oldSession._handleStateExit();
985
- const session = new SessionConnected(conn, listeners, ...carriedState);
1022
+ const session = new SessionConnected({
1023
+ conn,
1024
+ listeners,
1025
+ ...carriedState
1026
+ });
986
1027
  session.log?.info(
987
1028
  `session ${session.id} transition from Handshaking to Connected`,
988
1029
  {
@@ -992,7 +1033,7 @@ var SessionStateGraph = {
992
1033
  );
993
1034
  return session;
994
1035
  },
995
- WaitingForHandshakeToConnected(pendingSession, oldSession, sessionId, to, propagationCtx, listeners) {
1036
+ WaitingForHandshakeToConnected: (pendingSession, oldSession, sessionId, to, propagationCtx, listeners) => {
996
1037
  const conn = pendingSession.conn;
997
1038
  const { from, options } = pendingSession;
998
1039
  const carriedState = oldSession ? (
@@ -1000,21 +1041,30 @@ var SessionStateGraph = {
1000
1041
  inheritSharedSession(oldSession)
1001
1042
  ) : (
1002
1043
  // old session does not exist, create new state
1003
- [
1004
- sessionId,
1044
+ {
1045
+ id: sessionId,
1005
1046
  from,
1006
1047
  to,
1007
- 0,
1008
- 0,
1009
- [],
1010
- createSessionTelemetryInfo(sessionId, to, from, propagationCtx),
1048
+ seq: 0,
1049
+ ack: 0,
1050
+ sendBuffer: [],
1051
+ telemetry: createSessionTelemetryInfo(
1052
+ sessionId,
1053
+ to,
1054
+ from,
1055
+ propagationCtx
1056
+ ),
1011
1057
  options,
1012
- pendingSession.log
1013
- ]
1058
+ log: pendingSession.log
1059
+ }
1014
1060
  );
1015
1061
  pendingSession._handleStateExit();
1016
1062
  oldSession?._handleStateExit();
1017
- const session = new SessionConnected(conn, listeners, ...carriedState);
1063
+ const session = new SessionConnected({
1064
+ conn,
1065
+ listeners,
1066
+ ...carriedState
1067
+ });
1018
1068
  session.log?.info(
1019
1069
  `session ${session.id} transition from WaitingForHandshake to Connected`,
1020
1070
  {
@@ -1025,11 +1075,24 @@ var SessionStateGraph = {
1025
1075
  return session;
1026
1076
  },
1027
1077
  // disconnect paths
1028
- ConnectingToNoConnection(oldSession, listeners) {
1078
+ BackingOffToNoConnection: (oldSession, listeners) => {
1079
+ const carriedState = inheritSharedSession(oldSession);
1080
+ oldSession._handleStateExit();
1081
+ const session = new SessionNoConnection({ listeners, ...carriedState });
1082
+ session.log?.info(
1083
+ `session ${session.id} transition from BackingOff to NoConnection`,
1084
+ {
1085
+ ...session.loggingMetadata,
1086
+ tags: ["state-transition"]
1087
+ }
1088
+ );
1089
+ return session;
1090
+ },
1091
+ ConnectingToNoConnection: (oldSession, listeners) => {
1029
1092
  const carriedState = inheritSharedSession(oldSession);
1030
1093
  oldSession.bestEffortClose();
1031
1094
  oldSession._handleStateExit();
1032
- const session = new SessionNoConnection(listeners, ...carriedState);
1095
+ const session = new SessionNoConnection({ listeners, ...carriedState });
1033
1096
  session.log?.info(
1034
1097
  `session ${session.id} transition from Connecting to NoConnection`,
1035
1098
  {
@@ -1039,11 +1102,11 @@ var SessionStateGraph = {
1039
1102
  );
1040
1103
  return session;
1041
1104
  },
1042
- HandshakingToNoConnection(oldSession, listeners) {
1105
+ HandshakingToNoConnection: (oldSession, listeners) => {
1043
1106
  const carriedState = inheritSharedSession(oldSession);
1044
1107
  oldSession.conn.close();
1045
1108
  oldSession._handleStateExit();
1046
- const session = new SessionNoConnection(listeners, ...carriedState);
1109
+ const session = new SessionNoConnection({ listeners, ...carriedState });
1047
1110
  session.log?.info(
1048
1111
  `session ${session.id} transition from Handshaking to NoConnection`,
1049
1112
  {
@@ -1053,11 +1116,11 @@ var SessionStateGraph = {
1053
1116
  );
1054
1117
  return session;
1055
1118
  },
1056
- ConnectedToNoConnection(oldSession, listeners) {
1119
+ ConnectedToNoConnection: (oldSession, listeners) => {
1057
1120
  const carriedState = inheritSharedSession(oldSession);
1058
1121
  oldSession.conn.close();
1059
1122
  oldSession._handleStateExit();
1060
- const session = new SessionNoConnection(listeners, ...carriedState);
1123
+ const session = new SessionNoConnection({ listeners, ...carriedState });
1061
1124
  session.log?.info(
1062
1125
  `session ${session.id} transition from Connected to NoConnection`,
1063
1126
  {
@@ -1069,6 +1132,31 @@ var SessionStateGraph = {
1069
1132
  }
1070
1133
  }
1071
1134
  };
1135
+ var transitions = SessionStateGraph.transition;
1136
+ var ClientSessionStateGraph = {
1137
+ entrypoint: SessionStateGraph.entrypoints.NoConnection,
1138
+ transition: {
1139
+ // happy paths
1140
+ NoConnectionToBackingOff: transitions.NoConnectionToBackingOff,
1141
+ BackingOffToConnecting: transitions.BackingOffToConnecting,
1142
+ ConnectingToHandshaking: transitions.ConnectingToHandshaking,
1143
+ HandshakingToConnected: transitions.HandshakingToConnected,
1144
+ // disconnect paths
1145
+ BackingOffToNoConnection: transitions.BackingOffToNoConnection,
1146
+ ConnectingToNoConnection: transitions.ConnectingToNoConnection,
1147
+ HandshakingToNoConnection: transitions.HandshakingToNoConnection,
1148
+ ConnectedToNoConnection: transitions.ConnectedToNoConnection
1149
+ }
1150
+ };
1151
+ var ServerSessionStateGraph = {
1152
+ entrypoint: SessionStateGraph.entrypoints.WaitingForHandshake,
1153
+ transition: {
1154
+ // happy paths
1155
+ WaitingForHandshakeToConnected: transitions.WaitingForHandshakeToConnected,
1156
+ // disconnect paths
1157
+ ConnectedToNoConnection: transitions.ConnectedToNoConnection
1158
+ }
1159
+ };
1072
1160
 
1073
1161
  // transport/transport.ts
1074
1162
  var Transport = class {
@@ -1244,9 +1332,11 @@ var ServerTransport = class extends Transport {
1244
1332
  * A map of session handshake data for each session.
1245
1333
  */
1246
1334
  sessionHandshakeMetadata = /* @__PURE__ */ new Map();
1335
+ sessions = /* @__PURE__ */ new Map();
1247
1336
  pendingSessions = /* @__PURE__ */ new Set();
1248
1337
  constructor(clientId, providedOptions) {
1249
1338
  super(clientId, providedOptions);
1339
+ this.sessions = /* @__PURE__ */ new Map();
1250
1340
  this.options = {
1251
1341
  ...defaultServerTransportOptions,
1252
1342
  ...providedOptions
@@ -1297,7 +1387,7 @@ var ServerTransport = class extends Transport {
1297
1387
  clientId: this.clientId
1298
1388
  });
1299
1389
  let receivedHandshake = false;
1300
- const pendingSession = SessionStateGraph.entrypoints.WaitingForHandshake(
1390
+ const pendingSession = ServerSessionStateGraph.entrypoint(
1301
1391
  this.clientId,
1302
1392
  conn,
1303
1393
  {
@@ -1451,26 +1541,15 @@ var ServerTransport = class extends Transport {
1451
1541
  );
1452
1542
  return;
1453
1543
  }
1454
- if (oldSession.state === "Connected" /* Connected */) {
1455
- const noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(oldSession, {
1456
- onSessionGracePeriodElapsed: () => {
1457
- this.onSessionGracePeriodElapsed(noConnectionSession);
1458
- }
1459
- });
1460
- oldSession = noConnectionSession;
1461
- } else if (oldSession.state === "Handshaking" /* Handshaking */) {
1462
- const noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(oldSession, {
1463
- onSessionGracePeriodElapsed: () => {
1464
- this.onSessionGracePeriodElapsed(noConnectionSession);
1465
- }
1466
- });
1467
- oldSession = noConnectionSession;
1468
- } else if (oldSession.state === "Connecting" /* Connecting */) {
1469
- const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(oldSession, {
1470
- onSessionGracePeriodElapsed: () => {
1471
- this.onSessionGracePeriodElapsed(noConnectionSession);
1544
+ if (oldSession.state !== "NoConnection" /* NoConnection */) {
1545
+ const noConnectionSession = ServerSessionStateGraph.transition.ConnectedToNoConnection(
1546
+ oldSession,
1547
+ {
1548
+ onSessionGracePeriodElapsed: () => {
1549
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1550
+ }
1472
1551
  }
1473
- });
1552
+ );
1474
1553
  oldSession = noConnectionSession;
1475
1554
  }
1476
1555
  this.updateSession(oldSession);
@@ -1519,7 +1598,7 @@ var ServerTransport = class extends Transport {
1519
1598
  }
1520
1599
  });
1521
1600
  session.sendHandshake(responseMsg);
1522
- const connectedSession = SessionStateGraph.transition.WaitingForHandshakeToConnected(
1601
+ const connectedSession = ServerSessionStateGraph.transition.WaitingForHandshakeToConnected(
1523
1602
  session,
1524
1603
  // by this point oldSession is either no connection or we dont have an old session
1525
1604
  oldSession,
@@ -1554,43 +1633,43 @@ var ServerTransport = class extends Transport {
1554
1633
  connectedSession.startActiveHeartbeat();
1555
1634
  }
1556
1635
  async validateHandshakeMetadata(handshakingSession, existingSession, rawMetadata, from) {
1557
- let parsedMetadata = {};
1558
- if (this.handshakeExtensions) {
1559
- if (!import_value2.Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
1560
- this.rejectHandshakeRequest(
1561
- handshakingSession,
1562
- from,
1563
- "received malformed handshake metadata",
1564
- "MALFORMED_HANDSHAKE_META",
1565
- {
1566
- ...handshakingSession.loggingMetadata,
1567
- connectedTo: from,
1568
- validationErrors: [
1569
- ...import_value2.Value.Errors(this.handshakeExtensions.schema, rawMetadata)
1570
- ]
1571
- }
1572
- );
1573
- return false;
1574
- }
1575
- const previousParsedMetadata = existingSession ? this.sessionHandshakeMetadata.get(existingSession.to) : void 0;
1576
- parsedMetadata = await this.handshakeExtensions.validate(
1577
- rawMetadata,
1578
- previousParsedMetadata
1636
+ if (!this.handshakeExtensions) {
1637
+ return {};
1638
+ }
1639
+ if (!import_value2.Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
1640
+ this.rejectHandshakeRequest(
1641
+ handshakingSession,
1642
+ from,
1643
+ "received malformed handshake metadata",
1644
+ "MALFORMED_HANDSHAKE_META",
1645
+ {
1646
+ ...handshakingSession.loggingMetadata,
1647
+ connectedTo: from,
1648
+ validationErrors: [
1649
+ ...import_value2.Value.Errors(this.handshakeExtensions.schema, rawMetadata)
1650
+ ]
1651
+ }
1579
1652
  );
1580
- if (parsedMetadata === false) {
1581
- this.rejectHandshakeRequest(
1582
- handshakingSession,
1583
- from,
1584
- "rejected by handshake handler",
1585
- "REJECTED_BY_CUSTOM_HANDLER",
1586
- {
1587
- ...handshakingSession.loggingMetadata,
1588
- connectedTo: from,
1589
- clientId: this.clientId
1590
- }
1591
- );
1592
- return false;
1593
- }
1653
+ return false;
1654
+ }
1655
+ const previousParsedMetadata = existingSession ? this.sessionHandshakeMetadata.get(existingSession.to) : void 0;
1656
+ const parsedMetadata = await this.handshakeExtensions.validate(
1657
+ rawMetadata,
1658
+ previousParsedMetadata
1659
+ );
1660
+ if (import_value2.Value.Check(HandshakeErrorCustomHandlerFatalResponseCodes, parsedMetadata)) {
1661
+ this.rejectHandshakeRequest(
1662
+ handshakingSession,
1663
+ from,
1664
+ "rejected by handshake handler",
1665
+ parsedMetadata,
1666
+ {
1667
+ ...handshakingSession.loggingMetadata,
1668
+ connectedTo: from,
1669
+ clientId: this.clientId
1670
+ }
1671
+ );
1672
+ return false;
1594
1673
  }
1595
1674
  return parsedMetadata;
1596
1675
  }