@replit/river 0.26.1 → 0.26.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 (52) hide show
  1. package/dist/{chunk-4W5LENT2.js → chunk-3JCZNGF7.js} +2 -2
  2. package/dist/{chunk-XRI2BXMM.js → chunk-BB2E5L4U.js} +25 -13
  3. package/dist/chunk-BB2E5L4U.js.map +1 -0
  4. package/dist/{chunk-6UVTCZ6K.js → chunk-JI6FFDY5.js} +5 -4
  5. package/dist/{chunk-6UVTCZ6K.js.map → chunk-JI6FFDY5.js.map} +1 -1
  6. package/dist/{chunk-UQOD22AN.js → chunk-MZELCWJK.js} +2 -2
  7. package/dist/{chunk-UQOD22AN.js.map → chunk-MZELCWJK.js.map} +1 -1
  8. package/dist/{chunk-M43R4RPL.js → chunk-OCL2FUTQ.js} +128 -45
  9. package/dist/chunk-OCL2FUTQ.js.map +1 -0
  10. package/dist/{chunk-IVNX5H6C.js → chunk-X35QRIA5.js} +54 -55
  11. package/dist/chunk-X35QRIA5.js.map +1 -0
  12. package/dist/{chunk-AYIMQWS7.js → chunk-ZY2HYJ5Y.js} +2 -2
  13. package/dist/{client-0f636b3a.d.ts → client-1894a9c9.d.ts} +2 -4
  14. package/dist/{connection-07e97a79.d.ts → connection-03ffb583.d.ts} +1 -1
  15. package/dist/{handshake-8752f79e.d.ts → handshake-154a0bb2.d.ts} +82 -39
  16. package/dist/logging/index.d.cts +1 -1
  17. package/dist/logging/index.d.ts +1 -1
  18. package/dist/{message-57296605.d.ts → message-ff78a233.d.ts} +1 -1
  19. package/dist/router/index.cjs +1 -1
  20. package/dist/router/index.cjs.map +1 -1
  21. package/dist/router/index.d.cts +9 -8
  22. package/dist/router/index.d.ts +9 -8
  23. package/dist/router/index.js +2 -2
  24. package/dist/{server-e304daec.d.ts → server-1f5eb427.d.ts} +27 -6
  25. package/dist/{services-fc99aae1.d.ts → services-491d8c32.d.ts} +3 -3
  26. package/dist/transport/impls/ws/client.cjs +153 -56
  27. package/dist/transport/impls/ws/client.cjs.map +1 -1
  28. package/dist/transport/impls/ws/client.d.cts +5 -4
  29. package/dist/transport/impls/ws/client.d.ts +5 -4
  30. package/dist/transport/impls/ws/client.js +8 -7
  31. package/dist/transport/impls/ws/client.js.map +1 -1
  32. package/dist/transport/impls/ws/server.cjs +179 -96
  33. package/dist/transport/impls/ws/server.cjs.map +1 -1
  34. package/dist/transport/impls/ws/server.d.cts +5 -4
  35. package/dist/transport/impls/ws/server.d.ts +5 -4
  36. package/dist/transport/impls/ws/server.js +5 -5
  37. package/dist/transport/index.cjs +200 -105
  38. package/dist/transport/index.cjs.map +1 -1
  39. package/dist/transport/index.d.cts +5 -4
  40. package/dist/transport/index.d.ts +5 -4
  41. package/dist/transport/index.js +5 -5
  42. package/dist/util/testHelpers.cjs +127 -44
  43. package/dist/util/testHelpers.cjs.map +1 -1
  44. package/dist/util/testHelpers.d.cts +5 -4
  45. package/dist/util/testHelpers.d.ts +5 -4
  46. package/dist/util/testHelpers.js +3 -3
  47. package/package.json +1 -1
  48. package/dist/chunk-IVNX5H6C.js.map +0 -1
  49. package/dist/chunk-M43R4RPL.js.map +0 -1
  50. package/dist/chunk-XRI2BXMM.js.map +0 -1
  51. /package/dist/{chunk-4W5LENT2.js.map → chunk-3JCZNGF7.js.map} +0 -0
  52. /package/dist/{chunk-AYIMQWS7.js.map → chunk-ZY2HYJ5Y.js.map} +0 -0
@@ -1,12 +1,13 @@
1
- import { c as TransportClientId } from '../../../message-57296605.js';
1
+ import { c as TransportClientId } from '../../../message-ff78a233.js';
2
2
  import { WebSocketServer } from 'ws';
3
- import { W as WebSocketConnection } from '../../../connection-07e97a79.js';
3
+ import { W as WebSocketConnection } from '../../../connection-03ffb583.js';
4
4
  import { W as WsLike } from '../../../wslike-e0b32dd5.js';
5
- import { S as ServerTransport } from '../../../server-e304daec.js';
6
- import { c as ProvidedServerTransportOptions } from '../../../handshake-8752f79e.js';
5
+ import { S as ServerTransport } from '../../../server-1f5eb427.js';
6
+ import { c as ProvidedServerTransportOptions } from '../../../handshake-154a0bb2.js';
7
7
  import '@sinclair/typebox/value';
8
8
  import '@sinclair/typebox';
9
9
  import '@opentelemetry/api';
10
+ import '@sinclair/typebox/errors';
10
11
  import '../../../types-3e5768ec.js';
11
12
 
12
13
  declare class WebSocketServerTransport extends ServerTransport<WebSocketConnection> {
@@ -1,12 +1,13 @@
1
- import { c as TransportClientId } from '../../../message-57296605.js';
1
+ import { c as TransportClientId } from '../../../message-ff78a233.js';
2
2
  import { WebSocketServer } from 'ws';
3
- import { W as WebSocketConnection } from '../../../connection-07e97a79.js';
3
+ import { W as WebSocketConnection } from '../../../connection-03ffb583.js';
4
4
  import { W as WsLike } from '../../../wslike-e0b32dd5.js';
5
- import { S as ServerTransport } from '../../../server-e304daec.js';
6
- import { c as ProvidedServerTransportOptions } from '../../../handshake-8752f79e.js';
5
+ import { S as ServerTransport } from '../../../server-1f5eb427.js';
6
+ import { c as ProvidedServerTransportOptions } from '../../../handshake-154a0bb2.js';
7
7
  import '@sinclair/typebox/value';
8
8
  import '@sinclair/typebox';
9
9
  import '@opentelemetry/api';
10
+ import '@sinclair/typebox/errors';
10
11
  import '../../../types-3e5768ec.js';
11
12
 
12
13
  declare class WebSocketServerTransport extends ServerTransport<WebSocketConnection> {
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  ServerTransport
3
- } from "../../../chunk-IVNX5H6C.js";
3
+ } from "../../../chunk-X35QRIA5.js";
4
4
  import {
5
5
  WebSocketConnection
6
- } from "../../../chunk-AYIMQWS7.js";
7
- import "../../../chunk-6UVTCZ6K.js";
6
+ } from "../../../chunk-ZY2HYJ5Y.js";
7
+ import "../../../chunk-JI6FFDY5.js";
8
8
  import "../../../chunk-TAH2GVTJ.js";
9
- import "../../../chunk-M43R4RPL.js";
10
- import "../../../chunk-UQOD22AN.js";
9
+ import "../../../chunk-OCL2FUTQ.js";
10
+ import "../../../chunk-MZELCWJK.js";
11
11
  import "../../../chunk-4PVU7J25.js";
12
12
 
13
13
  // transport/impls/ws/server.ts
@@ -180,6 +180,7 @@ var defaultTransportOptions = {
180
180
  sessionDisconnectGraceMs: 5e3,
181
181
  connectionTimeoutMs: 2e3,
182
182
  handshakeTimeoutMs: 1e3,
183
+ enableTransparentSessionReconnects: true,
183
184
  codec: NaiveJsonCodec
184
185
  };
185
186
  var defaultConnectionRetryOptions = {
@@ -384,6 +385,7 @@ var StateMachineState = class {
384
385
  }
385
386
  if (prop === "_handleClose") {
386
387
  return () => {
388
+ target._isConsumed = true;
387
389
  target._handleStateExit();
388
390
  target._handleClose();
389
391
  };
@@ -464,15 +466,18 @@ var IdentifiedSession = class extends CommonSession {
464
466
  }
465
467
  get loggingMetadata() {
466
468
  const spanContext = this.telemetry.span.spanContext();
467
- return {
469
+ const metadata = {
468
470
  clientId: this.from,
469
471
  connectedTo: this.to,
470
- sessionId: this.id,
471
- telemetry: {
472
+ sessionId: this.id
473
+ };
474
+ if (this.telemetry.span.isRecording()) {
475
+ metadata.telemetry = {
472
476
  traceId: spanContext.traceId,
473
477
  spanId: spanContext.spanId
474
- }
475
- };
478
+ };
479
+ }
480
+ return metadata;
476
481
  }
477
482
  constructMsg(partialMsg) {
478
483
  const msg = {
@@ -501,9 +506,32 @@ var IdentifiedSession = class extends CommonSession {
501
506
  this.telemetry.span.end();
502
507
  }
503
508
  };
509
+ var IdentifiedSessionWithGracePeriod = class extends IdentifiedSession {
510
+ graceExpiryTime;
511
+ gracePeriodTimeout;
512
+ listeners;
513
+ constructor(props) {
514
+ super(props);
515
+ this.listeners = props.listeners;
516
+ this.graceExpiryTime = props.graceExpiryTime;
517
+ this.gracePeriodTimeout = setTimeout(() => {
518
+ this.listeners.onSessionGracePeriodElapsed();
519
+ }, this.graceExpiryTime - Date.now());
520
+ }
521
+ _handleStateExit() {
522
+ super._handleStateExit();
523
+ if (this.gracePeriodTimeout) {
524
+ clearTimeout(this.gracePeriodTimeout);
525
+ this.gracePeriodTimeout = void 0;
526
+ }
527
+ }
528
+ _handleClose() {
529
+ super._handleClose();
530
+ }
531
+ };
504
532
 
505
533
  // transport/sessionStateMachine/SessionConnecting.ts
506
- var SessionConnecting = class extends IdentifiedSession {
534
+ var SessionConnecting = class extends IdentifiedSessionWithGracePeriod {
507
535
  state = "Connecting" /* Connecting */;
508
536
  connPromise;
509
537
  listeners;
@@ -531,13 +559,24 @@ var SessionConnecting = class extends IdentifiedSession {
531
559
  // close a pending connection if it resolves, ignore errors if the promise
532
560
  // ends up rejected anyways
533
561
  bestEffortClose() {
534
- void this.connPromise.then((conn) => conn.close()).catch(() => {
562
+ void this.connPromise.then((conn) => {
563
+ conn.close();
564
+ this.log?.info(
565
+ "connection eventually resolved but session has transitioned, closed connection",
566
+ {
567
+ ...this.loggingMetadata,
568
+ ...conn.loggingMetadata
569
+ }
570
+ );
571
+ }).catch(() => {
535
572
  });
536
573
  }
537
574
  _handleStateExit() {
538
575
  super._handleStateExit();
539
- clearTimeout(this.connectionTimeout);
540
- this.connectionTimeout = void 0;
576
+ if (this.connectionTimeout) {
577
+ clearTimeout(this.connectionTimeout);
578
+ this.connectionTimeout = void 0;
579
+ }
541
580
  }
542
581
  _handleClose() {
543
582
  this.bestEffortClose();
@@ -546,26 +585,13 @@ var SessionConnecting = class extends IdentifiedSession {
546
585
  };
547
586
 
548
587
  // transport/sessionStateMachine/SessionNoConnection.ts
549
- var SessionNoConnection = class extends IdentifiedSession {
588
+ var SessionNoConnection = class extends IdentifiedSessionWithGracePeriod {
550
589
  state = "NoConnection" /* NoConnection */;
551
- listeners;
552
- gracePeriodTimeout;
553
- constructor(props) {
554
- super(props);
555
- this.listeners = props.listeners;
556
- this.gracePeriodTimeout = setTimeout(() => {
557
- this.listeners.onSessionGracePeriodElapsed();
558
- }, this.options.sessionDisconnectGraceMs);
559
- }
560
590
  _handleClose() {
561
591
  super._handleClose();
562
592
  }
563
593
  _handleStateExit() {
564
594
  super._handleStateExit();
565
- if (this.gracePeriodTimeout) {
566
- clearTimeout(this.gracePeriodTimeout);
567
- this.gracePeriodTimeout = void 0;
568
- }
569
595
  }
570
596
  };
571
597
 
@@ -573,7 +599,7 @@ var SessionNoConnection = class extends IdentifiedSession {
573
599
  var import_api = require("@opentelemetry/api");
574
600
 
575
601
  // package.json
576
- var version = "0.26.1";
602
+ var version = "0.26.3";
577
603
 
578
604
  // tracing/index.ts
579
605
  function getPropagationContext(ctx) {
@@ -621,6 +647,13 @@ var SessionWaitingForHandshake = class extends CommonSession {
621
647
  this.conn.addErrorListener(this.listeners.onConnectionErrored);
622
648
  this.conn.addCloseListener(this.listeners.onConnectionClosed);
623
649
  }
650
+ get loggingMetadata() {
651
+ return {
652
+ clientId: this.from,
653
+ connId: this.conn.id,
654
+ ...this.conn.loggingMetadata
655
+ };
656
+ }
624
657
  onHandshakeData = (msg) => {
625
658
  const parsedMsg = this.parseMsg(msg);
626
659
  if (parsedMsg === null) {
@@ -632,12 +665,6 @@ var SessionWaitingForHandshake = class extends CommonSession {
632
665
  }
633
666
  this.listeners.onHandshake(parsedMsg);
634
667
  };
635
- get loggingMetadata() {
636
- return {
637
- clientId: this.from,
638
- connId: this.conn.id
639
- };
640
- }
641
668
  sendHandshake(msg) {
642
669
  return this.conn.send(this.options.codec.toBuffer(msg));
643
670
  }
@@ -654,7 +681,7 @@ var SessionWaitingForHandshake = class extends CommonSession {
654
681
  };
655
682
 
656
683
  // transport/sessionStateMachine/SessionHandshaking.ts
657
- var SessionHandshaking = class extends IdentifiedSession {
684
+ var SessionHandshaking = class extends IdentifiedSessionWithGracePeriod {
658
685
  state = "Handshaking" /* Handshaking */;
659
686
  conn;
660
687
  listeners;
@@ -670,6 +697,12 @@ var SessionHandshaking = class extends IdentifiedSession {
670
697
  this.conn.addErrorListener(this.listeners.onConnectionErrored);
671
698
  this.conn.addCloseListener(this.listeners.onConnectionClosed);
672
699
  }
700
+ get loggingMetadata() {
701
+ return {
702
+ ...super.loggingMetadata,
703
+ ...this.conn.loggingMetadata
704
+ };
705
+ }
673
706
  onHandshakeData = (msg) => {
674
707
  const parsedMsg = this.parseMsg(msg);
675
708
  if (parsedMsg === null) {
@@ -689,7 +722,10 @@ var SessionHandshaking = class extends IdentifiedSession {
689
722
  this.conn.removeDataListener(this.onHandshakeData);
690
723
  this.conn.removeErrorListener(this.listeners.onConnectionErrored);
691
724
  this.conn.removeCloseListener(this.listeners.onConnectionClosed);
692
- clearTimeout(this.handshakeTimeout);
725
+ if (this.handshakeTimeout) {
726
+ clearTimeout(this.handshakeTimeout);
727
+ this.handshakeTimeout = void 0;
728
+ }
693
729
  }
694
730
  _handleClose() {
695
731
  super._handleClose();
@@ -754,6 +790,12 @@ var SessionConnected = class extends IdentifiedSession {
754
790
  this.heartbeatMisses++;
755
791
  }, this.options.heartbeatIntervalMs);
756
792
  }
793
+ get loggingMetadata() {
794
+ return {
795
+ ...super.loggingMetadata,
796
+ ...this.conn.loggingMetadata
797
+ };
798
+ }
757
799
  startActiveHeartbeat() {
758
800
  this.isActivelyHeartbeating = true;
759
801
  }
@@ -769,8 +811,10 @@ var SessionConnected = class extends IdentifiedSession {
769
811
  }
770
812
  onMessageData = (msg) => {
771
813
  const parsedMsg = this.parseMsg(msg);
772
- if (parsedMsg === null)
814
+ if (parsedMsg === null) {
815
+ this.listeners.onInvalidMessage("could not parse message");
773
816
  return;
817
+ }
774
818
  if (parsedMsg.seq !== this.ack) {
775
819
  if (parsedMsg.seq < this.ack) {
776
820
  this.log?.debug(
@@ -827,7 +871,7 @@ var SessionConnected = class extends IdentifiedSession {
827
871
  };
828
872
 
829
873
  // transport/sessionStateMachine/SessionBackingOff.ts
830
- var SessionBackingOff = class extends IdentifiedSession {
874
+ var SessionBackingOff = class extends IdentifiedSessionWithGracePeriod {
831
875
  state = "BackingOff" /* BackingOff */;
832
876
  listeners;
833
877
  backoffTimeout;
@@ -864,6 +908,12 @@ function inheritSharedSession(session) {
864
908
  log: session.log
865
909
  };
866
910
  }
911
+ function inheritSharedSessionWithGrace(session) {
912
+ return {
913
+ ...inheritSharedSession(session),
914
+ graceExpiryTime: session.graceExpiryTime
915
+ };
916
+ }
867
917
  var SessionStateGraph = {
868
918
  entrypoints: {
869
919
  NoConnection: (to, from, listeners, options, log) => {
@@ -877,6 +927,7 @@ var SessionStateGraph = {
877
927
  to,
878
928
  seq: 0,
879
929
  ack: 0,
930
+ graceExpiryTime: Date.now() + options.sessionDisconnectGraceMs,
880
931
  sendBuffer,
881
932
  telemetry,
882
933
  options,
@@ -908,7 +959,7 @@ var SessionStateGraph = {
908
959
  transition: {
909
960
  // happy path transitions
910
961
  NoConnectionToBackingOff: (oldSession, backoffMs, listeners) => {
911
- const carriedState = inheritSharedSession(oldSession);
962
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
912
963
  oldSession._handleStateExit();
913
964
  const session = new SessionBackingOff({
914
965
  backoffMs,
@@ -925,7 +976,7 @@ var SessionStateGraph = {
925
976
  return session;
926
977
  },
927
978
  BackingOffToConnecting: (oldSession, connPromise, listeners) => {
928
- const carriedState = inheritSharedSession(oldSession);
979
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
929
980
  oldSession._handleStateExit();
930
981
  const session = new SessionConnecting({
931
982
  connPromise,
@@ -942,7 +993,7 @@ var SessionStateGraph = {
942
993
  return session;
943
994
  },
944
995
  ConnectingToHandshaking: (oldSession, conn, listeners) => {
945
- const carriedState = inheritSharedSession(oldSession);
996
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
946
997
  oldSession._handleStateExit();
947
998
  const session = new SessionHandshaking({
948
999
  conn,
@@ -1019,9 +1070,12 @@ var SessionStateGraph = {
1019
1070
  },
1020
1071
  // disconnect paths
1021
1072
  BackingOffToNoConnection: (oldSession, listeners) => {
1022
- const carriedState = inheritSharedSession(oldSession);
1073
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
1023
1074
  oldSession._handleStateExit();
1024
- const session = new SessionNoConnection({ listeners, ...carriedState });
1075
+ const session = new SessionNoConnection({
1076
+ listeners,
1077
+ ...carriedState
1078
+ });
1025
1079
  session.log?.info(
1026
1080
  `session ${session.id} transition from BackingOff to NoConnection`,
1027
1081
  {
@@ -1032,10 +1086,13 @@ var SessionStateGraph = {
1032
1086
  return session;
1033
1087
  },
1034
1088
  ConnectingToNoConnection: (oldSession, listeners) => {
1035
- const carriedState = inheritSharedSession(oldSession);
1089
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
1036
1090
  oldSession.bestEffortClose();
1037
1091
  oldSession._handleStateExit();
1038
- const session = new SessionNoConnection({ listeners, ...carriedState });
1092
+ const session = new SessionNoConnection({
1093
+ listeners,
1094
+ ...carriedState
1095
+ });
1039
1096
  session.log?.info(
1040
1097
  `session ${session.id} transition from Connecting to NoConnection`,
1041
1098
  {
@@ -1046,10 +1103,13 @@ var SessionStateGraph = {
1046
1103
  return session;
1047
1104
  },
1048
1105
  HandshakingToNoConnection: (oldSession, listeners) => {
1049
- const carriedState = inheritSharedSession(oldSession);
1106
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
1050
1107
  oldSession.conn.close();
1051
1108
  oldSession._handleStateExit();
1052
- const session = new SessionNoConnection({ listeners, ...carriedState });
1109
+ const session = new SessionNoConnection({
1110
+ listeners,
1111
+ ...carriedState
1112
+ });
1053
1113
  session.log?.info(
1054
1114
  `session ${session.id} transition from Handshaking to NoConnection`,
1055
1115
  {
@@ -1061,9 +1121,14 @@ var SessionStateGraph = {
1061
1121
  },
1062
1122
  ConnectedToNoConnection: (oldSession, listeners) => {
1063
1123
  const carriedState = inheritSharedSession(oldSession);
1124
+ const graceExpiryTime = Date.now() + oldSession.options.sessionDisconnectGraceMs;
1064
1125
  oldSession.conn.close();
1065
1126
  oldSession._handleStateExit();
1066
- const session = new SessionNoConnection({ listeners, ...carriedState });
1127
+ const session = new SessionNoConnection({
1128
+ listeners,
1129
+ graceExpiryTime,
1130
+ ...carriedState
1131
+ });
1067
1132
  session.log?.info(
1068
1133
  `session ${session.id} transition from Connected to NoConnection`,
1069
1134
  {
@@ -1080,24 +1145,42 @@ var ClientSessionStateGraph = {
1080
1145
  entrypoint: SessionStateGraph.entrypoints.NoConnection,
1081
1146
  transition: {
1082
1147
  // happy paths
1148
+ // NoConnection -> BackingOff: attempt to connect
1083
1149
  NoConnectionToBackingOff: transitions.NoConnectionToBackingOff,
1150
+ // BackingOff -> Connecting: backoff period elapsed, start connection
1084
1151
  BackingOffToConnecting: transitions.BackingOffToConnecting,
1152
+ // Connecting -> Handshaking: connection established, start handshake
1085
1153
  ConnectingToHandshaking: transitions.ConnectingToHandshaking,
1154
+ // Handshaking -> Connected: handshake complete, session ready
1086
1155
  HandshakingToConnected: transitions.HandshakingToConnected,
1087
1156
  // disconnect paths
1157
+ // BackingOff -> NoConnection: unused
1088
1158
  BackingOffToNoConnection: transitions.BackingOffToNoConnection,
1159
+ // Connecting -> NoConnection: connection failed or connection timeout
1089
1160
  ConnectingToNoConnection: transitions.ConnectingToNoConnection,
1161
+ // Handshaking -> NoConnection: connection closed or handshake timeout
1090
1162
  HandshakingToNoConnection: transitions.HandshakingToNoConnection,
1163
+ // Connected -> NoConnection: connection closed
1091
1164
  ConnectedToNoConnection: transitions.ConnectedToNoConnection
1165
+ // destroy/close paths
1166
+ // NoConnection -> x: grace period elapsed
1167
+ // BackingOff -> x: grace period elapsed
1168
+ // Connecting -> x: grace period elapsed
1169
+ // Handshaking -> x: grace period elapsed or invalid handshake message or handshake rejection
1170
+ // Connected -> x: grace period elapsed or invalid message
1092
1171
  }
1093
1172
  };
1094
1173
  var ServerSessionStateGraph = {
1095
1174
  entrypoint: SessionStateGraph.entrypoints.WaitingForHandshake,
1096
1175
  transition: {
1097
1176
  // happy paths
1177
+ // WaitingForHandshake -> Connected: handshake complete, session ready
1098
1178
  WaitingForHandshakeToConnected: transitions.WaitingForHandshakeToConnected,
1099
1179
  // disconnect paths
1180
+ // Connected -> NoConnection: connection closed
1100
1181
  ConnectedToNoConnection: transitions.ConnectedToNoConnection
1182
+ // destroy/close paths
1183
+ // WaitingForHandshake -> x: handshake timeout elapsed or invalid handshake message or handshake rejection or connection closed
1101
1184
  }
1102
1185
  };
1103
1186
 
@@ -1214,8 +1297,9 @@ var Transport = class {
1214
1297
  status: "disconnect",
1215
1298
  session
1216
1299
  });
1300
+ const to = session.to;
1217
1301
  session.close();
1218
- this.sessions.delete(session.to);
1302
+ this.sessions.delete(to);
1219
1303
  }
1220
1304
  // common listeners
1221
1305
  onSessionGracePeriodElapsed(session) {
@@ -1363,6 +1447,10 @@ var ClientTransport = class extends Transport {
1363
1447
  this.handshakeExtensions = options;
1364
1448
  }
1365
1449
  tryReconnecting(to) {
1450
+ const oldSession = this.sessions.get(to);
1451
+ if (!this.options.enableTransparentSessionReconnects && oldSession) {
1452
+ this.deleteSession(oldSession);
1453
+ }
1366
1454
  if (this.reconnectOnConnectionDrop && this.getStatus() === "open") {
1367
1455
  this.connect(to);
1368
1456
  }
@@ -1449,6 +1537,9 @@ var ClientTransport = class extends Transport {
1449
1537
  handshakingSession.loggingMetadata
1450
1538
  );
1451
1539
  this.onConnClosed(handshakingSession);
1540
+ },
1541
+ onSessionGracePeriodElapsed: () => {
1542
+ this.onSessionGracePeriodElapsed(handshakingSession);
1452
1543
  }
1453
1544
  }
1454
1545
  );
@@ -1482,14 +1573,14 @@ var ClientTransport = class extends Transport {
1482
1573
  msg.payload.status.code
1483
1574
  ) : false;
1484
1575
  const reason = `handshake failed: ${msg.payload.status.reason}`;
1576
+ const to = session.to;
1485
1577
  this.rejectHandshakeResponse(session, reason, {
1486
1578
  ...session.loggingMetadata,
1487
1579
  transportMessage: msg
1488
1580
  });
1489
1581
  if (retriable) {
1490
- this.tryReconnecting(session.to);
1582
+ this.tryReconnecting(to);
1491
1583
  } else {
1492
- this.deleteSession(session);
1493
1584
  this.protocolError({
1494
1585
  type: ProtocolError.HandshakeFailed,
1495
1586
  code: msg.payload.status.code,
@@ -1542,6 +1633,12 @@ var ClientTransport = class extends Transport {
1542
1633
  * @param to The client ID of the node to connect to.
1543
1634
  */
1544
1635
  connect(to) {
1636
+ if (this.getStatus() !== "open") {
1637
+ this.log?.info(
1638
+ `transport state is no longer open, cancelling attempt to connect to ${to}`
1639
+ );
1640
+ return;
1641
+ }
1545
1642
  let session = this.sessions.get(to);
1546
1643
  session ??= this.createUnconnectedSession(to);
1547
1644
  if (session.state !== "NoConnection" /* NoConnection */) {
@@ -1551,13 +1648,6 @@ var ClientTransport = class extends Transport {
1551
1648
  );
1552
1649
  return;
1553
1650
  }
1554
- if (this.getStatus() !== "open") {
1555
- this.log?.info(
1556
- `transport state is no longer open, cancelling attempt to connect to ${to}`,
1557
- session.loggingMetadata
1558
- );
1559
- return;
1560
- }
1561
1651
  if (!this.retryBudget.hasBudget()) {
1562
1652
  const budgetConsumed = this.retryBudget.getBudgetConsumed();
1563
1653
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
@@ -1595,6 +1685,9 @@ var ClientTransport = class extends Transport {
1595
1685
  }
1596
1686
  );
1597
1687
  this.onBackoffFinished(backingOffSession, reconnectPromise);
1688
+ },
1689
+ onSessionGracePeriodElapsed: () => {
1690
+ this.onSessionGracePeriodElapsed(backingOffSession);
1598
1691
  }
1599
1692
  }
1600
1693
  );
@@ -1626,6 +1719,9 @@ var ClientTransport = class extends Transport {
1626
1719
  connectingSession.loggingMetadata
1627
1720
  );
1628
1721
  this.onConnectingFailed(connectingSession);
1722
+ },
1723
+ onSessionGracePeriodElapsed: () => {
1724
+ this.onSessionGracePeriodElapsed(connectingSession);
1629
1725
  }
1630
1726
  }
1631
1727
  );
@@ -1848,19 +1944,58 @@ var ServerTransport = class extends Transport {
1848
1944
  return;
1849
1945
  }
1850
1946
  let oldSession = this.sessions.get(msg.from);
1851
- const parsedMetadata = await this.validateHandshakeMetadata(
1852
- session,
1853
- oldSession,
1854
- msg.payload.metadata,
1855
- msg.from
1856
- );
1857
- if (parsedMetadata === false) {
1858
- return;
1947
+ let parsedMetadata = {};
1948
+ if (this.handshakeExtensions) {
1949
+ if (!import_value3.Value.Check(this.handshakeExtensions.schema, msg.payload.metadata)) {
1950
+ this.rejectHandshakeRequest(
1951
+ session,
1952
+ msg.from,
1953
+ "received malformed handshake metadata",
1954
+ "MALFORMED_HANDSHAKE_META",
1955
+ {
1956
+ ...session.loggingMetadata,
1957
+ connectedTo: msg.from,
1958
+ validationErrors: [
1959
+ ...import_value3.Value.Errors(
1960
+ this.handshakeExtensions.schema,
1961
+ msg.payload.metadata
1962
+ )
1963
+ ]
1964
+ }
1965
+ );
1966
+ return;
1967
+ }
1968
+ const previousParsedMetadata = oldSession ? this.sessionHandshakeMetadata.get(oldSession.to) : void 0;
1969
+ const parsedMetadataOrFailureCode = await this.handshakeExtensions.validate(
1970
+ msg.payload.metadata,
1971
+ previousParsedMetadata
1972
+ );
1973
+ if (session._isConsumed) {
1974
+ return;
1975
+ }
1976
+ if (import_value3.Value.Check(
1977
+ HandshakeErrorCustomHandlerFatalResponseCodes,
1978
+ parsedMetadataOrFailureCode
1979
+ )) {
1980
+ this.rejectHandshakeRequest(
1981
+ session,
1982
+ msg.from,
1983
+ "rejected by handshake handler",
1984
+ parsedMetadataOrFailureCode,
1985
+ {
1986
+ ...session.loggingMetadata,
1987
+ connectedTo: msg.from,
1988
+ clientId: this.clientId
1989
+ }
1990
+ );
1991
+ return;
1992
+ }
1993
+ parsedMetadata = parsedMetadataOrFailureCode;
1859
1994
  }
1860
1995
  let connectCase = "new session";
1861
1996
  const clientNextExpectedSeq = msg.payload.expectedSessionState.nextExpectedSeq;
1862
1997
  const clientNextSentSeq = msg.payload.expectedSessionState.nextSentSeq ?? 0;
1863
- if (oldSession && oldSession.id === msg.payload.sessionId) {
1998
+ if (this.options.enableTransparentSessionReconnects && oldSession && oldSession.id === msg.payload.sessionId) {
1864
1999
  connectCase = "transparent reconnection";
1865
2000
  const ourNextSeq = oldSession.nextSeq();
1866
2001
  const ourAck = oldSession.ack;
@@ -1919,10 +2054,11 @@ var ServerTransport = class extends Transport {
1919
2054
  }
1920
2055
  if (!oldSession && (clientNextSentSeq > 0 || clientNextExpectedSeq > 0)) {
1921
2056
  connectCase = "unknown session";
2057
+ const rejectionMessage = this.options.enableTransparentSessionReconnects ? `client is trying to reconnect to a session the server don't know about: ${msg.payload.sessionId}` : `client is attempting a transparent reconnect to a session but the server does not support it: ${msg.payload.sessionId}`;
1922
2058
  this.rejectHandshakeRequest(
1923
2059
  session,
1924
2060
  msg.from,
1925
- `client is trying to reconnect to a session the server don't know about: ${msg.payload.sessionId}`,
2061
+ rejectionMessage,
1926
2062
  "SESSION_STATE_MISMATCH",
1927
2063
  {
1928
2064
  ...session.loggingMetadata,
@@ -1986,47 +2122,6 @@ var ServerTransport = class extends Transport {
1986
2122
  this.pendingSessions.delete(session);
1987
2123
  connectedSession.startActiveHeartbeat();
1988
2124
  }
1989
- async validateHandshakeMetadata(handshakingSession, existingSession, rawMetadata, from) {
1990
- if (!this.handshakeExtensions) {
1991
- return {};
1992
- }
1993
- if (!import_value3.Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
1994
- this.rejectHandshakeRequest(
1995
- handshakingSession,
1996
- from,
1997
- "received malformed handshake metadata",
1998
- "MALFORMED_HANDSHAKE_META",
1999
- {
2000
- ...handshakingSession.loggingMetadata,
2001
- connectedTo: from,
2002
- validationErrors: [
2003
- ...import_value3.Value.Errors(this.handshakeExtensions.schema, rawMetadata)
2004
- ]
2005
- }
2006
- );
2007
- return false;
2008
- }
2009
- const previousParsedMetadata = existingSession ? this.sessionHandshakeMetadata.get(existingSession.to) : void 0;
2010
- const parsedMetadata = await this.handshakeExtensions.validate(
2011
- rawMetadata,
2012
- previousParsedMetadata
2013
- );
2014
- if (import_value3.Value.Check(HandshakeErrorCustomHandlerFatalResponseCodes, parsedMetadata)) {
2015
- this.rejectHandshakeRequest(
2016
- handshakingSession,
2017
- from,
2018
- "rejected by handshake handler",
2019
- parsedMetadata,
2020
- {
2021
- ...handshakingSession.loggingMetadata,
2022
- connectedTo: from,
2023
- clientId: this.clientId
2024
- }
2025
- );
2026
- return false;
2027
- }
2028
- return parsedMetadata;
2029
- }
2030
2125
  };
2031
2126
 
2032
2127
  // transport/connection.ts