@replit/river 0.26.0 → 0.26.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 (52) hide show
  1. package/dist/{chunk-KP4UB5NW.js → chunk-AWCUCZY4.js} +2 -2
  2. package/dist/{chunk-5S64PXTU.js → chunk-IV27BICV.js} +36 -9
  3. package/dist/chunk-IV27BICV.js.map +1 -0
  4. package/dist/{chunk-5FDAIAQ5.js → chunk-M5X4JTU3.js} +5 -5
  5. package/dist/chunk-M5X4JTU3.js.map +1 -0
  6. package/dist/{chunk-CCUYKR5C.js → chunk-M75K5TJS.js} +2 -2
  7. package/dist/{chunk-CCUYKR5C.js.map → chunk-M75K5TJS.js.map} +1 -1
  8. package/dist/{chunk-BNNELZM4.js → chunk-MREEJE3X.js} +2 -2
  9. package/dist/{chunk-JSU2KACV.js → chunk-NC54BC47.js} +98 -35
  10. package/dist/chunk-NC54BC47.js.map +1 -0
  11. package/dist/{chunk-7ETNUCOL.js → chunk-YQABPD3C.js} +29 -14
  12. package/dist/chunk-YQABPD3C.js.map +1 -0
  13. package/dist/{client-162c509c.d.ts → client-654098be.d.ts} +2 -4
  14. package/dist/{connection-6a404bb8.d.ts → connection-bc2454dc.d.ts} +1 -1
  15. package/dist/{handshake-3342bb94.d.ts → handshake-1a86f06d.d.ts} +60 -42
  16. package/dist/logging/index.d.cts +1 -1
  17. package/dist/logging/index.d.ts +1 -1
  18. package/dist/{message-1a434848.d.ts → message-57296605.d.ts} +2 -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 +8 -8
  22. package/dist/router/index.d.ts +8 -8
  23. package/dist/router/index.js +2 -2
  24. package/dist/{server-1b695374.d.ts → server-9a6b5a8e.d.ts} +12 -4
  25. package/dist/{services-c17f7eff.d.ts → services-7daa60a0.d.ts} +3 -3
  26. package/dist/transport/impls/ws/client.cjs +131 -41
  27. package/dist/transport/impls/ws/client.cjs.map +1 -1
  28. package/dist/transport/impls/ws/client.d.cts +4 -4
  29. package/dist/transport/impls/ws/client.d.ts +4 -4
  30. package/dist/transport/impls/ws/client.js +5 -5
  31. package/dist/transport/impls/ws/server.cjs +124 -46
  32. package/dist/transport/impls/ws/server.cjs.map +1 -1
  33. package/dist/transport/impls/ws/server.d.cts +4 -4
  34. package/dist/transport/impls/ws/server.d.ts +4 -4
  35. package/dist/transport/impls/ws/server.js +5 -5
  36. package/dist/transport/index.cjs +156 -51
  37. package/dist/transport/index.cjs.map +1 -1
  38. package/dist/transport/index.d.cts +4 -4
  39. package/dist/transport/index.d.ts +4 -4
  40. package/dist/transport/index.js +5 -5
  41. package/dist/util/testHelpers.cjs +97 -34
  42. package/dist/util/testHelpers.cjs.map +1 -1
  43. package/dist/util/testHelpers.d.cts +4 -4
  44. package/dist/util/testHelpers.d.ts +4 -4
  45. package/dist/util/testHelpers.js +3 -3
  46. package/package.json +1 -1
  47. package/dist/chunk-5FDAIAQ5.js.map +0 -1
  48. package/dist/chunk-5S64PXTU.js.map +0 -1
  49. package/dist/chunk-7ETNUCOL.js.map +0 -1
  50. package/dist/chunk-JSU2KACV.js.map +0 -1
  51. /package/dist/{chunk-KP4UB5NW.js.map → chunk-AWCUCZY4.js.map} +0 -0
  52. /package/dist/{chunk-BNNELZM4.js.map → chunk-MREEJE3X.js.map} +0 -0
@@ -1,9 +1,9 @@
1
- import { c as TransportClientId } from '../../../message-1a434848.js';
1
+ import { c as TransportClientId } from '../../../message-57296605.js';
2
2
  import { WebSocketServer } from 'ws';
3
- import { W as WebSocketConnection } from '../../../connection-6a404bb8.js';
3
+ import { W as WebSocketConnection } from '../../../connection-bc2454dc.js';
4
4
  import { W as WsLike } from '../../../wslike-e0b32dd5.js';
5
- import { S as ServerTransport } from '../../../server-1b695374.js';
6
- import { c as ProvidedServerTransportOptions } from '../../../handshake-3342bb94.js';
5
+ import { S as ServerTransport } from '../../../server-9a6b5a8e.js';
6
+ import { c as ProvidedServerTransportOptions } from '../../../handshake-1a86f06d.js';
7
7
  import '@sinclair/typebox/value';
8
8
  import '@sinclair/typebox';
9
9
  import '@opentelemetry/api';
@@ -1,9 +1,9 @@
1
- import { c as TransportClientId } from '../../../message-1a434848.js';
1
+ import { c as TransportClientId } from '../../../message-57296605.js';
2
2
  import { WebSocketServer } from 'ws';
3
- import { W as WebSocketConnection } from '../../../connection-6a404bb8.js';
3
+ import { W as WebSocketConnection } from '../../../connection-bc2454dc.js';
4
4
  import { W as WsLike } from '../../../wslike-e0b32dd5.js';
5
- import { S as ServerTransport } from '../../../server-1b695374.js';
6
- import { c as ProvidedServerTransportOptions } from '../../../handshake-3342bb94.js';
5
+ import { S as ServerTransport } from '../../../server-9a6b5a8e.js';
6
+ import { c as ProvidedServerTransportOptions } from '../../../handshake-1a86f06d.js';
7
7
  import '@sinclair/typebox/value';
8
8
  import '@sinclair/typebox';
9
9
  import '@opentelemetry/api';
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  ServerTransport
3
- } from "../../../chunk-7ETNUCOL.js";
3
+ } from "../../../chunk-YQABPD3C.js";
4
4
  import {
5
5
  WebSocketConnection
6
- } from "../../../chunk-KP4UB5NW.js";
7
- import "../../../chunk-5FDAIAQ5.js";
6
+ } from "../../../chunk-AWCUCZY4.js";
7
+ import "../../../chunk-M5X4JTU3.js";
8
8
  import "../../../chunk-TAH2GVTJ.js";
9
- import "../../../chunk-JSU2KACV.js";
10
- import "../../../chunk-CCUYKR5C.js";
9
+ import "../../../chunk-NC54BC47.js";
10
+ import "../../../chunk-M75K5TJS.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 = {
@@ -501,9 +502,32 @@ var IdentifiedSession = class extends CommonSession {
501
502
  this.telemetry.span.end();
502
503
  }
503
504
  };
505
+ var IdentifiedSessionWithGracePeriod = class extends IdentifiedSession {
506
+ graceExpiryTime;
507
+ gracePeriodTimeout;
508
+ listeners;
509
+ constructor(props) {
510
+ super(props);
511
+ this.listeners = props.listeners;
512
+ this.graceExpiryTime = props.graceExpiryTime;
513
+ this.gracePeriodTimeout = setTimeout(() => {
514
+ this.listeners.onSessionGracePeriodElapsed();
515
+ }, this.graceExpiryTime - Date.now());
516
+ }
517
+ _handleStateExit() {
518
+ super._handleStateExit();
519
+ if (this.gracePeriodTimeout) {
520
+ clearTimeout(this.gracePeriodTimeout);
521
+ this.gracePeriodTimeout = void 0;
522
+ }
523
+ }
524
+ _handleClose() {
525
+ super._handleClose();
526
+ }
527
+ };
504
528
 
505
529
  // transport/sessionStateMachine/SessionConnecting.ts
506
- var SessionConnecting = class extends IdentifiedSession {
530
+ var SessionConnecting = class extends IdentifiedSessionWithGracePeriod {
507
531
  state = "Connecting" /* Connecting */;
508
532
  connPromise;
509
533
  listeners;
@@ -536,8 +560,10 @@ var SessionConnecting = class extends IdentifiedSession {
536
560
  }
537
561
  _handleStateExit() {
538
562
  super._handleStateExit();
539
- clearTimeout(this.connectionTimeout);
540
- this.connectionTimeout = void 0;
563
+ if (this.connectionTimeout) {
564
+ clearTimeout(this.connectionTimeout);
565
+ this.connectionTimeout = void 0;
566
+ }
541
567
  }
542
568
  _handleClose() {
543
569
  this.bestEffortClose();
@@ -546,26 +572,13 @@ var SessionConnecting = class extends IdentifiedSession {
546
572
  };
547
573
 
548
574
  // transport/sessionStateMachine/SessionNoConnection.ts
549
- var SessionNoConnection = class extends IdentifiedSession {
575
+ var SessionNoConnection = class extends IdentifiedSessionWithGracePeriod {
550
576
  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
577
  _handleClose() {
561
578
  super._handleClose();
562
579
  }
563
580
  _handleStateExit() {
564
581
  super._handleStateExit();
565
- if (this.gracePeriodTimeout) {
566
- clearTimeout(this.gracePeriodTimeout);
567
- this.gracePeriodTimeout = void 0;
568
- }
569
582
  }
570
583
  };
571
584
 
@@ -573,7 +586,7 @@ var SessionNoConnection = class extends IdentifiedSession {
573
586
  var import_api = require("@opentelemetry/api");
574
587
 
575
588
  // package.json
576
- var version = "0.26.0";
589
+ var version = "0.26.2";
577
590
 
578
591
  // tracing/index.ts
579
592
  function getPropagationContext(ctx) {
@@ -624,7 +637,10 @@ var SessionWaitingForHandshake = class extends CommonSession {
624
637
  onHandshakeData = (msg) => {
625
638
  const parsedMsg = this.parseMsg(msg);
626
639
  if (parsedMsg === null) {
627
- this.listeners.onInvalidHandshake("could not parse message");
640
+ this.listeners.onInvalidHandshake(
641
+ "could not parse message",
642
+ "MALFORMED_HANDSHAKE"
643
+ );
628
644
  return;
629
645
  }
630
646
  this.listeners.onHandshake(parsedMsg);
@@ -651,7 +667,7 @@ var SessionWaitingForHandshake = class extends CommonSession {
651
667
  };
652
668
 
653
669
  // transport/sessionStateMachine/SessionHandshaking.ts
654
- var SessionHandshaking = class extends IdentifiedSession {
670
+ var SessionHandshaking = class extends IdentifiedSessionWithGracePeriod {
655
671
  state = "Handshaking" /* Handshaking */;
656
672
  conn;
657
673
  listeners;
@@ -670,7 +686,10 @@ var SessionHandshaking = class extends IdentifiedSession {
670
686
  onHandshakeData = (msg) => {
671
687
  const parsedMsg = this.parseMsg(msg);
672
688
  if (parsedMsg === null) {
673
- this.listeners.onInvalidHandshake("could not parse message");
689
+ this.listeners.onInvalidHandshake(
690
+ "could not parse message",
691
+ "MALFORMED_HANDSHAKE"
692
+ );
674
693
  return;
675
694
  }
676
695
  this.listeners.onHandshake(parsedMsg);
@@ -683,7 +702,10 @@ var SessionHandshaking = class extends IdentifiedSession {
683
702
  this.conn.removeDataListener(this.onHandshakeData);
684
703
  this.conn.removeErrorListener(this.listeners.onConnectionErrored);
685
704
  this.conn.removeCloseListener(this.listeners.onConnectionClosed);
686
- clearTimeout(this.handshakeTimeout);
705
+ if (this.handshakeTimeout) {
706
+ clearTimeout(this.handshakeTimeout);
707
+ this.handshakeTimeout = void 0;
708
+ }
687
709
  }
688
710
  _handleClose() {
689
711
  super._handleClose();
@@ -763,8 +785,10 @@ var SessionConnected = class extends IdentifiedSession {
763
785
  }
764
786
  onMessageData = (msg) => {
765
787
  const parsedMsg = this.parseMsg(msg);
766
- if (parsedMsg === null)
788
+ if (parsedMsg === null) {
789
+ this.listeners.onInvalidMessage("could not parse message");
767
790
  return;
791
+ }
768
792
  if (parsedMsg.seq !== this.ack) {
769
793
  if (parsedMsg.seq < this.ack) {
770
794
  this.log?.debug(
@@ -821,7 +845,7 @@ var SessionConnected = class extends IdentifiedSession {
821
845
  };
822
846
 
823
847
  // transport/sessionStateMachine/SessionBackingOff.ts
824
- var SessionBackingOff = class extends IdentifiedSession {
848
+ var SessionBackingOff = class extends IdentifiedSessionWithGracePeriod {
825
849
  state = "BackingOff" /* BackingOff */;
826
850
  listeners;
827
851
  backoffTimeout;
@@ -858,6 +882,12 @@ function inheritSharedSession(session) {
858
882
  log: session.log
859
883
  };
860
884
  }
885
+ function inheritSharedSessionWithGrace(session) {
886
+ return {
887
+ ...inheritSharedSession(session),
888
+ graceExpiryTime: session.graceExpiryTime
889
+ };
890
+ }
861
891
  var SessionStateGraph = {
862
892
  entrypoints: {
863
893
  NoConnection: (to, from, listeners, options, log) => {
@@ -871,6 +901,7 @@ var SessionStateGraph = {
871
901
  to,
872
902
  seq: 0,
873
903
  ack: 0,
904
+ graceExpiryTime: Date.now() + options.sessionDisconnectGraceMs,
874
905
  sendBuffer,
875
906
  telemetry,
876
907
  options,
@@ -902,7 +933,7 @@ var SessionStateGraph = {
902
933
  transition: {
903
934
  // happy path transitions
904
935
  NoConnectionToBackingOff: (oldSession, backoffMs, listeners) => {
905
- const carriedState = inheritSharedSession(oldSession);
936
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
906
937
  oldSession._handleStateExit();
907
938
  const session = new SessionBackingOff({
908
939
  backoffMs,
@@ -919,7 +950,7 @@ var SessionStateGraph = {
919
950
  return session;
920
951
  },
921
952
  BackingOffToConnecting: (oldSession, connPromise, listeners) => {
922
- const carriedState = inheritSharedSession(oldSession);
953
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
923
954
  oldSession._handleStateExit();
924
955
  const session = new SessionConnecting({
925
956
  connPromise,
@@ -936,7 +967,7 @@ var SessionStateGraph = {
936
967
  return session;
937
968
  },
938
969
  ConnectingToHandshaking: (oldSession, conn, listeners) => {
939
- const carriedState = inheritSharedSession(oldSession);
970
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
940
971
  oldSession._handleStateExit();
941
972
  const session = new SessionHandshaking({
942
973
  conn,
@@ -1013,9 +1044,12 @@ var SessionStateGraph = {
1013
1044
  },
1014
1045
  // disconnect paths
1015
1046
  BackingOffToNoConnection: (oldSession, listeners) => {
1016
- const carriedState = inheritSharedSession(oldSession);
1047
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
1017
1048
  oldSession._handleStateExit();
1018
- const session = new SessionNoConnection({ listeners, ...carriedState });
1049
+ const session = new SessionNoConnection({
1050
+ listeners,
1051
+ ...carriedState
1052
+ });
1019
1053
  session.log?.info(
1020
1054
  `session ${session.id} transition from BackingOff to NoConnection`,
1021
1055
  {
@@ -1026,10 +1060,13 @@ var SessionStateGraph = {
1026
1060
  return session;
1027
1061
  },
1028
1062
  ConnectingToNoConnection: (oldSession, listeners) => {
1029
- const carriedState = inheritSharedSession(oldSession);
1063
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
1030
1064
  oldSession.bestEffortClose();
1031
1065
  oldSession._handleStateExit();
1032
- const session = new SessionNoConnection({ listeners, ...carriedState });
1066
+ const session = new SessionNoConnection({
1067
+ listeners,
1068
+ ...carriedState
1069
+ });
1033
1070
  session.log?.info(
1034
1071
  `session ${session.id} transition from Connecting to NoConnection`,
1035
1072
  {
@@ -1040,10 +1077,13 @@ var SessionStateGraph = {
1040
1077
  return session;
1041
1078
  },
1042
1079
  HandshakingToNoConnection: (oldSession, listeners) => {
1043
- const carriedState = inheritSharedSession(oldSession);
1080
+ const carriedState = inheritSharedSessionWithGrace(oldSession);
1044
1081
  oldSession.conn.close();
1045
1082
  oldSession._handleStateExit();
1046
- const session = new SessionNoConnection({ listeners, ...carriedState });
1083
+ const session = new SessionNoConnection({
1084
+ listeners,
1085
+ ...carriedState
1086
+ });
1047
1087
  session.log?.info(
1048
1088
  `session ${session.id} transition from Handshaking to NoConnection`,
1049
1089
  {
@@ -1055,9 +1095,14 @@ var SessionStateGraph = {
1055
1095
  },
1056
1096
  ConnectedToNoConnection: (oldSession, listeners) => {
1057
1097
  const carriedState = inheritSharedSession(oldSession);
1098
+ const graceExpiryTime = Date.now() + oldSession.options.sessionDisconnectGraceMs;
1058
1099
  oldSession.conn.close();
1059
1100
  oldSession._handleStateExit();
1060
- const session = new SessionNoConnection({ listeners, ...carriedState });
1101
+ const session = new SessionNoConnection({
1102
+ listeners,
1103
+ graceExpiryTime,
1104
+ ...carriedState
1105
+ });
1061
1106
  session.log?.info(
1062
1107
  `session ${session.id} transition from Connected to NoConnection`,
1063
1108
  {
@@ -1074,24 +1119,42 @@ var ClientSessionStateGraph = {
1074
1119
  entrypoint: SessionStateGraph.entrypoints.NoConnection,
1075
1120
  transition: {
1076
1121
  // happy paths
1122
+ // NoConnection -> BackingOff: attempt to connect
1077
1123
  NoConnectionToBackingOff: transitions.NoConnectionToBackingOff,
1124
+ // BackingOff -> Connecting: backoff period elapsed, start connection
1078
1125
  BackingOffToConnecting: transitions.BackingOffToConnecting,
1126
+ // Connecting -> Handshaking: connection established, start handshake
1079
1127
  ConnectingToHandshaking: transitions.ConnectingToHandshaking,
1128
+ // Handshaking -> Connected: handshake complete, session ready
1080
1129
  HandshakingToConnected: transitions.HandshakingToConnected,
1081
1130
  // disconnect paths
1131
+ // BackingOff -> NoConnection: unused
1082
1132
  BackingOffToNoConnection: transitions.BackingOffToNoConnection,
1133
+ // Connecting -> NoConnection: connection failed or connection timeout
1083
1134
  ConnectingToNoConnection: transitions.ConnectingToNoConnection,
1135
+ // Handshaking -> NoConnection: connection closed or handshake timeout
1084
1136
  HandshakingToNoConnection: transitions.HandshakingToNoConnection,
1137
+ // Connected -> NoConnection: connection closed
1085
1138
  ConnectedToNoConnection: transitions.ConnectedToNoConnection
1139
+ // destroy/close paths
1140
+ // NoConnection -> x: grace period elapsed
1141
+ // BackingOff -> x: grace period elapsed
1142
+ // Connecting -> x: grace period elapsed
1143
+ // Handshaking -> x: grace period elapsed or invalid handshake message or handshake rejection
1144
+ // Connected -> x: grace period elapsed or invalid message
1086
1145
  }
1087
1146
  };
1088
1147
  var ServerSessionStateGraph = {
1089
1148
  entrypoint: SessionStateGraph.entrypoints.WaitingForHandshake,
1090
1149
  transition: {
1091
1150
  // happy paths
1151
+ // WaitingForHandshake -> Connected: handshake complete, session ready
1092
1152
  WaitingForHandshakeToConnected: transitions.WaitingForHandshakeToConnected,
1093
1153
  // disconnect paths
1154
+ // Connected -> NoConnection: connection closed
1094
1155
  ConnectedToNoConnection: transitions.ConnectedToNoConnection
1156
+ // destroy/close paths
1157
+ // WaitingForHandshake -> x: handshake timeout elapsed or invalid handshake message or handshake rejection or connection closed
1095
1158
  }
1096
1159
  };
1097
1160
 
@@ -1160,8 +1223,8 @@ var Transport = class {
1160
1223
  removeEventListener(type, handler) {
1161
1224
  this.eventDispatcher.removeEventListener(type, handler);
1162
1225
  }
1163
- protocolError(type, message) {
1164
- this.eventDispatcher.dispatchEvent("protocolError", { type, message });
1226
+ protocolError(message) {
1227
+ this.eventDispatcher.dispatchEvent("protocolError", message);
1165
1228
  }
1166
1229
  /**
1167
1230
  * Default close implementation for transports. You should override this in the downstream
@@ -1357,6 +1420,10 @@ var ClientTransport = class extends Transport {
1357
1420
  this.handshakeExtensions = options;
1358
1421
  }
1359
1422
  tryReconnecting(to) {
1423
+ const oldSession = this.sessions.get(to);
1424
+ if (!this.options.enableTransparentSessionReconnects && oldSession) {
1425
+ this.deleteSession(oldSession);
1426
+ }
1360
1427
  if (this.reconnectOnConnectionDrop && this.getStatus() === "open") {
1361
1428
  this.connect(to);
1362
1429
  }
@@ -1425,13 +1492,17 @@ var ClientTransport = class extends Transport {
1425
1492
  onHandshake: (msg) => {
1426
1493
  this.onHandshakeResponse(handshakingSession, msg);
1427
1494
  },
1428
- onInvalidHandshake: (reason) => {
1495
+ onInvalidHandshake: (reason, code) => {
1429
1496
  this.log?.error(
1430
1497
  `invalid handshake: ${reason}`,
1431
1498
  handshakingSession.loggingMetadata
1432
1499
  );
1433
1500
  this.deleteSession(session);
1434
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1501
+ this.protocolError({
1502
+ type: ProtocolError.HandshakeFailed,
1503
+ code,
1504
+ message: reason
1505
+ });
1435
1506
  },
1436
1507
  onHandshakeTimeout: () => {
1437
1508
  this.log?.error(
@@ -1439,6 +1510,9 @@ var ClientTransport = class extends Transport {
1439
1510
  handshakingSession.loggingMetadata
1440
1511
  );
1441
1512
  this.onConnClosed(handshakingSession);
1513
+ },
1514
+ onSessionGracePeriodElapsed: () => {
1515
+ this.onSessionGracePeriodElapsed(handshakingSession);
1442
1516
  }
1443
1517
  }
1444
1518
  );
@@ -1480,7 +1554,11 @@ var ClientTransport = class extends Transport {
1480
1554
  this.tryReconnecting(session.to);
1481
1555
  } else {
1482
1556
  this.deleteSession(session);
1483
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1557
+ this.protocolError({
1558
+ type: ProtocolError.HandshakeFailed,
1559
+ code: msg.payload.status.code,
1560
+ message: reason
1561
+ });
1484
1562
  }
1485
1563
  return;
1486
1564
  }
@@ -1514,7 +1592,10 @@ var ClientTransport = class extends Transport {
1514
1592
  onMessage: (msg2) => this.handleMsg(msg2),
1515
1593
  onInvalidMessage: (reason) => {
1516
1594
  this.deleteSession(connectedSession);
1517
- this.protocolError(ProtocolError.MessageOrderingViolated, reason);
1595
+ this.protocolError({
1596
+ type: ProtocolError.MessageOrderingViolated,
1597
+ message: reason
1598
+ });
1518
1599
  }
1519
1600
  });
1520
1601
  this.updateSession(connectedSession);
@@ -1545,7 +1626,10 @@ var ClientTransport = class extends Transport {
1545
1626
  const budgetConsumed = this.retryBudget.getBudgetConsumed();
1546
1627
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
1547
1628
  this.log?.error(errMsg, session.loggingMetadata);
1548
- this.protocolError(ProtocolError.RetriesExceeded, errMsg);
1629
+ this.protocolError({
1630
+ type: ProtocolError.RetriesExceeded,
1631
+ message: errMsg
1632
+ });
1549
1633
  return;
1550
1634
  }
1551
1635
  const backoffMs = this.retryBudget.getBackoffMs();
@@ -1575,6 +1659,9 @@ var ClientTransport = class extends Transport {
1575
1659
  }
1576
1660
  );
1577
1661
  this.onBackoffFinished(backingOffSession, reconnectPromise);
1662
+ },
1663
+ onSessionGracePeriodElapsed: () => {
1664
+ this.onSessionGracePeriodElapsed(backingOffSession);
1578
1665
  }
1579
1666
  }
1580
1667
  );
@@ -1606,6 +1693,9 @@ var ClientTransport = class extends Transport {
1606
1693
  connectingSession.loggingMetadata
1607
1694
  );
1608
1695
  this.onConnectingFailed(connectingSession);
1696
+ },
1697
+ onSessionGracePeriodElapsed: () => {
1698
+ this.onSessionGracePeriodElapsed(connectingSession);
1609
1699
  }
1610
1700
  }
1611
1701
  );
@@ -1752,13 +1842,17 @@ var ServerTransport = class extends Transport {
1752
1842
  receivedHandshake = true;
1753
1843
  void this.onHandshakeRequest(pendingSession, msg);
1754
1844
  },
1755
- onInvalidHandshake: (reason) => {
1845
+ onInvalidHandshake: (reason, code) => {
1756
1846
  this.log?.error(
1757
1847
  `invalid handshake: ${reason}`,
1758
1848
  pendingSession.loggingMetadata
1759
1849
  );
1760
1850
  this.deletePendingSession(pendingSession);
1761
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1851
+ this.protocolError({
1852
+ type: ProtocolError.HandshakeFailed,
1853
+ code,
1854
+ message: reason
1855
+ });
1762
1856
  }
1763
1857
  },
1764
1858
  this.options,
@@ -1783,7 +1877,11 @@ var ServerTransport = class extends Transport {
1783
1877
  }
1784
1878
  })
1785
1879
  );
1786
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1880
+ this.protocolError({
1881
+ type: ProtocolError.HandshakeFailed,
1882
+ code,
1883
+ message: reason
1884
+ });
1787
1885
  this.deletePendingSession(session);
1788
1886
  }
1789
1887
  async onHandshakeRequest(session, msg) {
@@ -1832,7 +1930,7 @@ var ServerTransport = class extends Transport {
1832
1930
  let connectCase = "new session";
1833
1931
  const clientNextExpectedSeq = msg.payload.expectedSessionState.nextExpectedSeq;
1834
1932
  const clientNextSentSeq = msg.payload.expectedSessionState.nextSentSeq ?? 0;
1835
- if (oldSession && oldSession.id === msg.payload.sessionId) {
1933
+ if (this.options.enableTransparentSessionReconnects && oldSession && oldSession.id === msg.payload.sessionId) {
1836
1934
  connectCase = "transparent reconnection";
1837
1935
  const ourNextSeq = oldSession.nextSeq();
1838
1936
  const ourAck = oldSession.ack;
@@ -1891,10 +1989,11 @@ var ServerTransport = class extends Transport {
1891
1989
  }
1892
1990
  if (!oldSession && (clientNextSentSeq > 0 || clientNextExpectedSeq > 0)) {
1893
1991
  connectCase = "unknown session";
1992
+ 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}`;
1894
1993
  this.rejectHandshakeRequest(
1895
1994
  session,
1896
1995
  msg.from,
1897
- `client is trying to reconnect to a session the server don't know about: ${msg.payload.sessionId}`,
1996
+ rejectionMessage,
1898
1997
  "SESSION_STATE_MISMATCH",
1899
1998
  {
1900
1999
  ...session.loggingMetadata,
@@ -1945,7 +2044,10 @@ var ServerTransport = class extends Transport {
1945
2044
  },
1946
2045
  onMessage: (msg2) => this.handleMsg(msg2),
1947
2046
  onInvalidMessage: (reason) => {
1948
- this.protocolError(ProtocolError.MessageOrderingViolated, reason);
2047
+ this.protocolError({
2048
+ type: ProtocolError.MessageOrderingViolated,
2049
+ message: reason
2050
+ });
1949
2051
  this.deleteSession(connectedSession);
1950
2052
  }
1951
2053
  }
@@ -1976,16 +2078,19 @@ var ServerTransport = class extends Transport {
1976
2078
  return false;
1977
2079
  }
1978
2080
  const previousParsedMetadata = existingSession ? this.sessionHandshakeMetadata.get(existingSession.to) : void 0;
1979
- const parsedMetadata = await this.handshakeExtensions.validate(
2081
+ const parsedMetadataOrFailureCode = await this.handshakeExtensions.validate(
1980
2082
  rawMetadata,
1981
2083
  previousParsedMetadata
1982
2084
  );
1983
- if (import_value3.Value.Check(HandshakeErrorCustomHandlerFatalResponseCodes, parsedMetadata)) {
2085
+ if (import_value3.Value.Check(
2086
+ HandshakeErrorCustomHandlerFatalResponseCodes,
2087
+ parsedMetadataOrFailureCode
2088
+ )) {
1984
2089
  this.rejectHandshakeRequest(
1985
2090
  handshakingSession,
1986
2091
  from,
1987
2092
  "rejected by handshake handler",
1988
- parsedMetadata,
2093
+ parsedMetadataOrFailureCode,
1989
2094
  {
1990
2095
  ...handshakingSession.loggingMetadata,
1991
2096
  connectedTo: from,
@@ -1994,7 +2099,7 @@ var ServerTransport = class extends Transport {
1994
2099
  );
1995
2100
  return false;
1996
2101
  }
1997
- return parsedMetadata;
2102
+ return parsedMetadataOrFailureCode;
1998
2103
  }
1999
2104
  };
2000
2105