@replit/river 0.200.0-rc.6 → 0.200.0-rc.7

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 (68) hide show
  1. package/README.md +2 -2
  2. package/dist/{chunk-TMNVRR5R.js → chunk-3XKJOFZA.js} +193 -90
  3. package/dist/chunk-3XKJOFZA.js.map +1 -0
  4. package/dist/{chunk-IGNFE3QW.js → chunk-BZBEE2VR.js} +2 -2
  5. package/dist/chunk-BZBEE2VR.js.map +1 -0
  6. package/dist/{chunk-WE2PAPNC.js → chunk-CXPZSAU4.js} +102 -95
  7. package/dist/chunk-CXPZSAU4.js.map +1 -0
  8. package/dist/{chunk-WF3AW2CB.js → chunk-F2E7ILHW.js} +41 -45
  9. package/dist/chunk-F2E7ILHW.js.map +1 -0
  10. package/dist/{chunk-NOLXEIRP.js → chunk-U4W75CMT.js} +3 -3
  11. package/dist/chunk-U4W75CMT.js.map +1 -0
  12. package/dist/{chunk-GXRIBO3N.js → chunk-VLW5OKZG.js} +2 -2
  13. package/dist/{chunk-MXU6DJVG.js → chunk-XIJVDPYY.js} +161 -159
  14. package/dist/chunk-XIJVDPYY.js.map +1 -0
  15. package/dist/{client-f56a6da3.d.ts → client-829bf1f9.d.ts} +3 -1
  16. package/dist/{connection-6031a354.d.ts → connection-5e67d641.d.ts} +1 -1
  17. package/dist/{context-73df8978.d.ts → context-9eabf54f.d.ts} +132 -81
  18. package/dist/router/index.cjs +162 -160
  19. package/dist/router/index.cjs.map +1 -1
  20. package/dist/router/index.d.cts +7 -7
  21. package/dist/router/index.d.ts +7 -7
  22. package/dist/router/index.js +6 -6
  23. package/dist/{server-9f31d98f.d.ts → server-d82a2d9b.d.ts} +8 -3
  24. package/dist/{services-69d72cd5.d.ts → services-6a446f04.d.ts} +157 -139
  25. package/dist/transport/impls/ws/client.cjs +285 -178
  26. package/dist/transport/impls/ws/client.cjs.map +1 -1
  27. package/dist/transport/impls/ws/client.d.cts +3 -3
  28. package/dist/transport/impls/ws/client.d.ts +3 -3
  29. package/dist/transport/impls/ws/client.js +7 -7
  30. package/dist/transport/impls/ws/server.cjs +224 -128
  31. package/dist/transport/impls/ws/server.cjs.map +1 -1
  32. package/dist/transport/impls/ws/server.d.cts +3 -3
  33. package/dist/transport/impls/ws/server.d.ts +3 -3
  34. package/dist/transport/impls/ws/server.js +5 -5
  35. package/dist/transport/index.cjs +322 -218
  36. package/dist/transport/index.cjs.map +1 -1
  37. package/dist/transport/index.d.cts +3 -3
  38. package/dist/transport/index.d.ts +3 -3
  39. package/dist/transport/index.js +7 -7
  40. package/dist/util/testHelpers.cjs +227 -133
  41. package/dist/util/testHelpers.cjs.map +1 -1
  42. package/dist/util/testHelpers.d.cts +15 -12
  43. package/dist/util/testHelpers.d.ts +15 -12
  44. package/dist/util/testHelpers.js +41 -43
  45. package/dist/util/testHelpers.js.map +1 -1
  46. package/package.json +1 -1
  47. package/dist/chunk-IGNFE3QW.js.map +0 -1
  48. package/dist/chunk-MU25KVV7.js +0 -112
  49. package/dist/chunk-MU25KVV7.js.map +0 -1
  50. package/dist/chunk-MXU6DJVG.js.map +0 -1
  51. package/dist/chunk-NOLXEIRP.js.map +0 -1
  52. package/dist/chunk-TMNVRR5R.js.map +0 -1
  53. package/dist/chunk-WE2PAPNC.js.map +0 -1
  54. package/dist/chunk-WF3AW2CB.js.map +0 -1
  55. package/dist/connection-11991b13.d.ts +0 -31
  56. package/dist/transport/impls/uds/client.cjs +0 -1696
  57. package/dist/transport/impls/uds/client.cjs.map +0 -1
  58. package/dist/transport/impls/uds/client.d.cts +0 -18
  59. package/dist/transport/impls/uds/client.d.ts +0 -18
  60. package/dist/transport/impls/uds/client.js +0 -38
  61. package/dist/transport/impls/uds/client.js.map +0 -1
  62. package/dist/transport/impls/uds/server.cjs +0 -1663
  63. package/dist/transport/impls/uds/server.cjs.map +0 -1
  64. package/dist/transport/impls/uds/server.d.cts +0 -19
  65. package/dist/transport/impls/uds/server.d.ts +0 -19
  66. package/dist/transport/impls/uds/server.js +0 -33
  67. package/dist/transport/impls/uds/server.js.map +0 -1
  68. /package/dist/{chunk-GXRIBO3N.js.map → chunk-VLW5OKZG.js.map} +0 -0
@@ -212,7 +212,7 @@ var defaultTransportOptions = {
212
212
  codec: NaiveJsonCodec
213
213
  };
214
214
  var defaultConnectionRetryOptions = {
215
- baseIntervalMs: 250,
215
+ baseIntervalMs: 150,
216
216
  maxJitterMs: 200,
217
217
  maxBackoffMs: 32e3,
218
218
  attemptBudgetCapacity: 5,
@@ -229,17 +229,17 @@ var defaultServerTransportOptions = {
229
229
  // transport/rateLimit.ts
230
230
  var LeakyBucketRateLimit = class {
231
231
  budgetConsumed;
232
- intervalHandles;
232
+ intervalHandle;
233
233
  options;
234
234
  constructor(options) {
235
235
  this.options = options;
236
- this.budgetConsumed = /* @__PURE__ */ new Map();
237
- this.intervalHandles = /* @__PURE__ */ new Map();
236
+ this.budgetConsumed = 0;
238
237
  }
239
- getBackoffMs(user) {
240
- if (!this.budgetConsumed.has(user))
238
+ getBackoffMs() {
239
+ if (this.getBudgetConsumed() === 0) {
241
240
  return 0;
242
- const exponent = Math.max(0, this.getBudgetConsumed(user) - 1);
241
+ }
242
+ const exponent = Math.max(0, this.getBudgetConsumed() - 1);
243
243
  const jitter = Math.floor(Math.random() * this.options.maxJitterMs);
244
244
  const backoffMs = Math.min(
245
245
  this.options.baseIntervalMs * 2 ** exponent,
@@ -250,50 +250,46 @@ var LeakyBucketRateLimit = class {
250
250
  get totalBudgetRestoreTime() {
251
251
  return this.options.budgetRestoreIntervalMs * this.options.attemptBudgetCapacity;
252
252
  }
253
- consumeBudget(user) {
254
- this.stopLeak(user);
255
- this.budgetConsumed.set(user, this.getBudgetConsumed(user) + 1);
253
+ consumeBudget() {
254
+ this.stopLeak();
255
+ this.budgetConsumed = this.getBudgetConsumed() + 1;
256
256
  }
257
- getBudgetConsumed(user) {
258
- return this.budgetConsumed.get(user) ?? 0;
257
+ getBudgetConsumed() {
258
+ return this.budgetConsumed;
259
259
  }
260
- hasBudget(user) {
261
- return this.getBudgetConsumed(user) < this.options.attemptBudgetCapacity;
260
+ hasBudget() {
261
+ return this.getBudgetConsumed() < this.options.attemptBudgetCapacity;
262
262
  }
263
- startRestoringBudget(user) {
264
- if (this.intervalHandles.has(user)) {
263
+ startRestoringBudget() {
264
+ if (this.intervalHandle) {
265
265
  return;
266
266
  }
267
267
  const restoreBudgetForUser = () => {
268
- const currentBudget = this.budgetConsumed.get(user);
268
+ const currentBudget = this.budgetConsumed;
269
269
  if (!currentBudget) {
270
- this.stopLeak(user);
270
+ this.stopLeak();
271
271
  return;
272
272
  }
273
273
  const newBudget = currentBudget - 1;
274
274
  if (newBudget === 0) {
275
- this.budgetConsumed.delete(user);
276
275
  return;
277
276
  }
278
- this.budgetConsumed.set(user, newBudget);
277
+ this.budgetConsumed = newBudget;
279
278
  };
280
- const intervalHandle = setInterval(
279
+ this.intervalHandle = setInterval(
281
280
  restoreBudgetForUser,
282
281
  this.options.budgetRestoreIntervalMs
283
282
  );
284
- this.intervalHandles.set(user, intervalHandle);
285
283
  }
286
- stopLeak(user) {
287
- if (!this.intervalHandles.has(user)) {
284
+ stopLeak() {
285
+ if (!this.intervalHandle) {
288
286
  return;
289
287
  }
290
- clearInterval(this.intervalHandles.get(user));
291
- this.intervalHandles.delete(user);
288
+ clearInterval(this.intervalHandle);
289
+ this.intervalHandle = void 0;
292
290
  }
293
291
  close() {
294
- for (const user of this.intervalHandles.keys()) {
295
- this.stopLeak(user);
296
- }
292
+ this.stopLeak();
297
293
  }
298
294
  };
299
295
 
@@ -440,7 +436,7 @@ var CommonSession = class extends StateMachineState {
440
436
  from;
441
437
  options;
442
438
  log;
443
- constructor(from, options, log) {
439
+ constructor({ from, options, log }) {
444
440
  super();
445
441
  this.from = from;
446
442
  this.options = options;
@@ -482,8 +478,9 @@ var IdentifiedSession = class extends CommonSession {
482
478
  */
483
479
  ack;
484
480
  sendBuffer;
485
- constructor(id, from, to, seq, ack, sendBuffer, telemetry, options, protocolVersion, log) {
486
- super(from, options, log);
481
+ constructor(props) {
482
+ const { id, to, seq, ack, sendBuffer, telemetry, log, protocolVersion } = props;
483
+ super(props);
487
484
  this.id = id;
488
485
  this.to = to;
489
486
  this.seq = seq;
@@ -539,23 +536,23 @@ var SessionConnecting = class extends IdentifiedSession {
539
536
  connPromise;
540
537
  listeners;
541
538
  connectionTimeout;
542
- constructor(connPromise, listeners, ...args) {
543
- super(...args);
544
- this.connPromise = connPromise;
545
- this.listeners = listeners;
539
+ constructor(props) {
540
+ super(props);
541
+ this.connPromise = props.connPromise;
542
+ this.listeners = props.listeners;
546
543
  this.connectionTimeout = setTimeout(() => {
547
- listeners.onConnectionTimeout();
544
+ this.listeners.onConnectionTimeout();
548
545
  }, this.options.connectionTimeoutMs);
549
- connPromise.then(
546
+ this.connPromise.then(
550
547
  (conn) => {
551
548
  if (this._isConsumed)
552
549
  return;
553
- listeners.onConnectionEstablished(conn);
550
+ this.listeners.onConnectionEstablished(conn);
554
551
  },
555
552
  (err) => {
556
553
  if (this._isConsumed)
557
554
  return;
558
- listeners.onConnectionFailed(err);
555
+ this.listeners.onConnectionFailed(err);
559
556
  }
560
557
  );
561
558
  }
@@ -581,9 +578,9 @@ var SessionNoConnection = class extends IdentifiedSession {
581
578
  state = "NoConnection" /* NoConnection */;
582
579
  listeners;
583
580
  gracePeriodTimeout;
584
- constructor(listeners, ...args) {
585
- super(...args);
586
- this.listeners = listeners;
581
+ constructor(props) {
582
+ super(props);
583
+ this.listeners = props.listeners;
587
584
  this.gracePeriodTimeout = setTimeout(() => {
588
585
  this.listeners.onSessionGracePeriodElapsed();
589
586
  }, this.options.sessionDisconnectGraceMs);
@@ -604,7 +601,7 @@ var SessionNoConnection = class extends IdentifiedSession {
604
601
  var import_api = require("@opentelemetry/api");
605
602
 
606
603
  // package.json
607
- var version = "0.200.0-rc.6";
604
+ var version = "0.200.0-rc.7";
608
605
 
609
606
  // tracing/index.ts
610
607
  function getPropagationContext(ctx) {
@@ -641,16 +638,16 @@ var SessionWaitingForHandshake = class extends CommonSession {
641
638
  conn;
642
639
  listeners;
643
640
  handshakeTimeout;
644
- constructor(conn, listeners, ...args) {
645
- super(...args);
646
- this.conn = conn;
647
- this.listeners = listeners;
641
+ constructor(props) {
642
+ super(props);
643
+ this.conn = props.conn;
644
+ this.listeners = props.listeners;
648
645
  this.handshakeTimeout = setTimeout(() => {
649
- listeners.onHandshakeTimeout();
646
+ this.listeners.onHandshakeTimeout();
650
647
  }, this.options.handshakeTimeoutMs);
651
648
  this.conn.addDataListener(this.onHandshakeData);
652
- this.conn.addErrorListener(listeners.onConnectionErrored);
653
- this.conn.addCloseListener(listeners.onConnectionClosed);
649
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
650
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
654
651
  }
655
652
  onHandshakeData = (msg) => {
656
653
  const parsedMsg = this.parseMsg(msg);
@@ -687,16 +684,16 @@ var SessionHandshaking = class extends IdentifiedSession {
687
684
  conn;
688
685
  listeners;
689
686
  handshakeTimeout;
690
- constructor(conn, listeners, ...args) {
691
- super(...args);
692
- this.conn = conn;
693
- this.listeners = listeners;
687
+ constructor(props) {
688
+ super(props);
689
+ this.conn = props.conn;
690
+ this.listeners = props.listeners;
694
691
  this.handshakeTimeout = setTimeout(() => {
695
- listeners.onHandshakeTimeout();
692
+ this.listeners.onHandshakeTimeout();
696
693
  }, this.options.handshakeTimeoutMs);
697
694
  this.conn.addDataListener(this.onHandshakeData);
698
- this.conn.addErrorListener(listeners.onConnectionErrored);
699
- this.conn.addCloseListener(listeners.onConnectionClosed);
695
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
696
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
700
697
  }
701
698
  onHandshakeData = (msg) => {
702
699
  const parsedMsg = this.parseMsg(msg);
@@ -730,9 +727,7 @@ var SessionConnected = class extends IdentifiedSession {
730
727
  listeners;
731
728
  heartbeatHandle;
732
729
  heartbeatMisses = 0;
733
- get isActivelyHeartbeating() {
734
- return this.heartbeatHandle !== void 0;
735
- }
730
+ isActivelyHeartbeating;
736
731
  updateBookkeeping(ack, seq) {
737
732
  this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
738
733
  this.ack = seq + 1;
@@ -744,13 +739,13 @@ var SessionConnected = class extends IdentifiedSession {
744
739
  this.conn.send(this.options.codec.toBuffer(constructedMsg));
745
740
  return constructedMsg.id;
746
741
  }
747
- constructor(conn, listeners, ...args) {
748
- super(...args);
749
- this.conn = conn;
750
- this.listeners = listeners;
742
+ constructor(props) {
743
+ super(props);
744
+ this.conn = props.conn;
745
+ this.listeners = props.listeners;
751
746
  this.conn.addDataListener(this.onMessageData);
752
- this.conn.addCloseListener(listeners.onConnectionClosed);
753
- this.conn.addErrorListener(listeners.onConnectionErrored);
747
+ this.conn.addCloseListener(this.listeners.onConnectionClosed);
748
+ this.conn.addErrorListener(this.listeners.onConnectionErrored);
754
749
  if (this.sendBuffer.length > 0) {
755
750
  this.log?.debug(
756
751
  `sending ${this.sendBuffer.length} buffered messages`,
@@ -758,10 +753,9 @@ var SessionConnected = class extends IdentifiedSession {
758
753
  );
759
754
  }
760
755
  for (const msg of this.sendBuffer) {
761
- conn.send(this.options.codec.toBuffer(msg));
756
+ this.conn.send(this.options.codec.toBuffer(msg));
762
757
  }
763
- }
764
- startActiveHeartbeat() {
758
+ this.isActivelyHeartbeating = false;
765
759
  this.heartbeatHandle = setInterval(() => {
766
760
  const misses = this.heartbeatMisses;
767
761
  const missDuration = misses * this.options.heartbeatIntervalMs;
@@ -776,11 +770,17 @@ var SessionConnected = class extends IdentifiedSession {
776
770
  this.heartbeatHandle = void 0;
777
771
  return;
778
772
  }
779
- this.sendHeartbeat();
773
+ if (this.isActivelyHeartbeating) {
774
+ this.sendHeartbeat();
775
+ }
780
776
  this.heartbeatMisses++;
781
777
  }, this.options.heartbeatIntervalMs);
782
778
  }
779
+ startActiveHeartbeat() {
780
+ this.isActivelyHeartbeating = true;
781
+ }
783
782
  sendHeartbeat() {
783
+ this.log?.debug("sending heartbeat", this.loggingMetadata);
784
784
  this.send({
785
785
  streamId: "heartbeat",
786
786
  controlFlags: 1 /* AckBit */,
@@ -848,54 +848,78 @@ var SessionConnected = class extends IdentifiedSession {
848
848
  }
849
849
  };
850
850
 
851
+ // transport/sessionStateMachine/SessionBackingOff.ts
852
+ var SessionBackingOff = class extends IdentifiedSession {
853
+ state = "BackingOff" /* BackingOff */;
854
+ listeners;
855
+ backoffTimeout;
856
+ constructor(props) {
857
+ super(props);
858
+ this.listeners = props.listeners;
859
+ this.backoffTimeout = setTimeout(() => {
860
+ this.listeners.onBackoffFinished();
861
+ }, props.backoffMs);
862
+ }
863
+ _handleClose() {
864
+ super._handleClose();
865
+ }
866
+ _handleStateExit() {
867
+ super._handleStateExit();
868
+ if (this.backoffTimeout) {
869
+ clearTimeout(this.backoffTimeout);
870
+ this.backoffTimeout = void 0;
871
+ }
872
+ }
873
+ };
874
+
851
875
  // transport/sessionStateMachine/transitions.ts
852
876
  function inheritSharedSession(session) {
853
- return [
854
- session.id,
855
- session.from,
856
- session.to,
857
- session.seq,
858
- session.ack,
859
- session.sendBuffer,
860
- session.telemetry,
861
- session.options,
862
- session.protocolVersion,
863
- session.log
864
- ];
877
+ return {
878
+ id: session.id,
879
+ from: session.from,
880
+ to: session.to,
881
+ seq: session.seq,
882
+ ack: session.ack,
883
+ sendBuffer: session.sendBuffer,
884
+ telemetry: session.telemetry,
885
+ options: session.options,
886
+ log: session.log,
887
+ protocolVersion: session.protocolVersion
888
+ };
865
889
  }
866
890
  var SessionStateGraph = {
867
891
  entrypoints: {
868
- NoConnection(to, from, listeners, options, protocolVersion, log) {
892
+ NoConnection: (to, from, listeners, options, protocolVersion, log) => {
869
893
  const id = `session-${generateId()}`;
870
894
  const telemetry = createSessionTelemetryInfo(id, to, from);
871
895
  const sendBuffer = [];
872
- const session = new SessionNoConnection(
896
+ const session = new SessionNoConnection({
873
897
  listeners,
874
898
  id,
875
899
  from,
876
900
  to,
877
- 0,
878
- 0,
901
+ seq: 0,
902
+ ack: 0,
879
903
  sendBuffer,
880
904
  telemetry,
881
905
  options,
882
906
  protocolVersion,
883
907
  log
884
- );
908
+ });
885
909
  session.log?.info(`session ${session.id} created in NoConnection state`, {
886
910
  ...session.loggingMetadata,
887
911
  tags: ["state-transition"]
888
912
  });
889
913
  return session;
890
914
  },
891
- WaitingForHandshake(from, conn, listeners, options, log) {
892
- const session = new SessionWaitingForHandshake(
915
+ WaitingForHandshake: (from, conn, listeners, options, log) => {
916
+ const session = new SessionWaitingForHandshake({
893
917
  conn,
894
918
  listeners,
895
919
  from,
896
920
  options,
897
921
  log
898
- );
922
+ });
899
923
  session.log?.info(`session created in WaitingForHandshake state`, {
900
924
  ...session.loggingMetadata,
901
925
  tags: ["state-transition"]
@@ -907,16 +931,33 @@ var SessionStateGraph = {
907
931
  // After a session is transitioned, any usage of the old session will throw.
908
932
  transition: {
909
933
  // happy path transitions
910
- NoConnectionToConnecting(oldSession, connPromise, listeners) {
934
+ NoConnectionToBackingOff: (oldSession, backoffMs, listeners) => {
911
935
  const carriedState = inheritSharedSession(oldSession);
912
936
  oldSession._handleStateExit();
913
- const session = new SessionConnecting(
914
- connPromise,
937
+ const session = new SessionBackingOff({
938
+ backoffMs,
915
939
  listeners,
916
940
  ...carriedState
941
+ });
942
+ session.log?.info(
943
+ `session ${session.id} transition from NoConnection to BackingOff`,
944
+ {
945
+ ...session.loggingMetadata,
946
+ tags: ["state-transition"]
947
+ }
917
948
  );
949
+ return session;
950
+ },
951
+ BackingOffToConnecting: (oldSession, connPromise, listeners) => {
952
+ const carriedState = inheritSharedSession(oldSession);
953
+ oldSession._handleStateExit();
954
+ const session = new SessionConnecting({
955
+ connPromise,
956
+ listeners,
957
+ ...carriedState
958
+ });
918
959
  session.log?.info(
919
- `session ${session.id} transition from NoConnection to Connecting`,
960
+ `session ${session.id} transition from BackingOff to Connecting`,
920
961
  {
921
962
  ...session.loggingMetadata,
922
963
  tags: ["state-transition"]
@@ -924,10 +965,14 @@ var SessionStateGraph = {
924
965
  );
925
966
  return session;
926
967
  },
927
- ConnectingToHandshaking(oldSession, conn, listeners) {
968
+ ConnectingToHandshaking: (oldSession, conn, listeners) => {
928
969
  const carriedState = inheritSharedSession(oldSession);
929
970
  oldSession._handleStateExit();
930
- const session = new SessionHandshaking(conn, listeners, ...carriedState);
971
+ const session = new SessionHandshaking({
972
+ conn,
973
+ listeners,
974
+ ...carriedState
975
+ });
931
976
  session.log?.info(
932
977
  `session ${session.id} transition from Connecting to Handshaking`,
933
978
  {
@@ -937,11 +982,15 @@ var SessionStateGraph = {
937
982
  );
938
983
  return session;
939
984
  },
940
- HandshakingToConnected(oldSession, listeners) {
985
+ HandshakingToConnected: (oldSession, listeners) => {
941
986
  const carriedState = inheritSharedSession(oldSession);
942
987
  const conn = oldSession.conn;
943
988
  oldSession._handleStateExit();
944
- const session = new SessionConnected(conn, listeners, ...carriedState);
989
+ const session = new SessionConnected({
990
+ conn,
991
+ listeners,
992
+ ...carriedState
993
+ });
945
994
  session.log?.info(
946
995
  `session ${session.id} transition from Handshaking to Connected`,
947
996
  {
@@ -951,7 +1000,7 @@ var SessionStateGraph = {
951
1000
  );
952
1001
  return session;
953
1002
  },
954
- WaitingForHandshakeToConnected(pendingSession, oldSession, sessionId, to, propagationCtx, listeners, protocolVersion) {
1003
+ WaitingForHandshakeToConnected: (pendingSession, oldSession, sessionId, to, propagationCtx, listeners, protocolVersion) => {
955
1004
  const conn = pendingSession.conn;
956
1005
  const { from, options } = pendingSession;
957
1006
  const carriedState = oldSession ? (
@@ -959,22 +1008,31 @@ var SessionStateGraph = {
959
1008
  inheritSharedSession(oldSession)
960
1009
  ) : (
961
1010
  // old session does not exist, create new state
962
- [
963
- sessionId,
1011
+ {
1012
+ id: sessionId,
964
1013
  from,
965
1014
  to,
966
- 0,
967
- 0,
968
- [],
969
- createSessionTelemetryInfo(sessionId, to, from, propagationCtx),
1015
+ seq: 0,
1016
+ ack: 0,
1017
+ sendBuffer: [],
1018
+ telemetry: createSessionTelemetryInfo(
1019
+ sessionId,
1020
+ to,
1021
+ from,
1022
+ propagationCtx
1023
+ ),
970
1024
  options,
971
- protocolVersion,
972
- pendingSession.log
973
- ]
1025
+ log: pendingSession.log,
1026
+ protocolVersion
1027
+ }
974
1028
  );
975
1029
  pendingSession._handleStateExit();
976
1030
  oldSession?._handleStateExit();
977
- const session = new SessionConnected(conn, listeners, ...carriedState);
1031
+ const session = new SessionConnected({
1032
+ conn,
1033
+ listeners,
1034
+ ...carriedState
1035
+ });
978
1036
  session.log?.info(
979
1037
  `session ${session.id} transition from WaitingForHandshake to Connected`,
980
1038
  {
@@ -985,11 +1043,24 @@ var SessionStateGraph = {
985
1043
  return session;
986
1044
  },
987
1045
  // disconnect paths
988
- ConnectingToNoConnection(oldSession, listeners) {
1046
+ BackingOffToNoConnection: (oldSession, listeners) => {
1047
+ const carriedState = inheritSharedSession(oldSession);
1048
+ oldSession._handleStateExit();
1049
+ const session = new SessionNoConnection({ listeners, ...carriedState });
1050
+ session.log?.info(
1051
+ `session ${session.id} transition from BackingOff to NoConnection`,
1052
+ {
1053
+ ...session.loggingMetadata,
1054
+ tags: ["state-transition"]
1055
+ }
1056
+ );
1057
+ return session;
1058
+ },
1059
+ ConnectingToNoConnection: (oldSession, listeners) => {
989
1060
  const carriedState = inheritSharedSession(oldSession);
990
1061
  oldSession.bestEffortClose();
991
1062
  oldSession._handleStateExit();
992
- const session = new SessionNoConnection(listeners, ...carriedState);
1063
+ const session = new SessionNoConnection({ listeners, ...carriedState });
993
1064
  session.log?.info(
994
1065
  `session ${session.id} transition from Connecting to NoConnection`,
995
1066
  {
@@ -999,11 +1070,11 @@ var SessionStateGraph = {
999
1070
  );
1000
1071
  return session;
1001
1072
  },
1002
- HandshakingToNoConnection(oldSession, listeners) {
1073
+ HandshakingToNoConnection: (oldSession, listeners) => {
1003
1074
  const carriedState = inheritSharedSession(oldSession);
1004
1075
  oldSession.conn.close();
1005
1076
  oldSession._handleStateExit();
1006
- const session = new SessionNoConnection(listeners, ...carriedState);
1077
+ const session = new SessionNoConnection({ listeners, ...carriedState });
1007
1078
  session.log?.info(
1008
1079
  `session ${session.id} transition from Handshaking to NoConnection`,
1009
1080
  {
@@ -1013,11 +1084,11 @@ var SessionStateGraph = {
1013
1084
  );
1014
1085
  return session;
1015
1086
  },
1016
- ConnectedToNoConnection(oldSession, listeners) {
1087
+ ConnectedToNoConnection: (oldSession, listeners) => {
1017
1088
  const carriedState = inheritSharedSession(oldSession);
1018
1089
  oldSession.conn.close();
1019
1090
  oldSession._handleStateExit();
1020
- const session = new SessionNoConnection(listeners, ...carriedState);
1091
+ const session = new SessionNoConnection({ listeners, ...carriedState });
1021
1092
  session.log?.info(
1022
1093
  `session ${session.id} transition from Connected to NoConnection`,
1023
1094
  {
@@ -1029,6 +1100,31 @@ var SessionStateGraph = {
1029
1100
  }
1030
1101
  }
1031
1102
  };
1103
+ var transitions = SessionStateGraph.transition;
1104
+ var ClientSessionStateGraph = {
1105
+ entrypoint: SessionStateGraph.entrypoints.NoConnection,
1106
+ transition: {
1107
+ // happy paths
1108
+ NoConnectionToBackingOff: transitions.NoConnectionToBackingOff,
1109
+ BackingOffToConnecting: transitions.BackingOffToConnecting,
1110
+ ConnectingToHandshaking: transitions.ConnectingToHandshaking,
1111
+ HandshakingToConnected: transitions.HandshakingToConnected,
1112
+ // disconnect paths
1113
+ BackingOffToNoConnection: transitions.BackingOffToNoConnection,
1114
+ ConnectingToNoConnection: transitions.ConnectingToNoConnection,
1115
+ HandshakingToNoConnection: transitions.HandshakingToNoConnection,
1116
+ ConnectedToNoConnection: transitions.ConnectedToNoConnection
1117
+ }
1118
+ };
1119
+ var ServerSessionStateGraph = {
1120
+ entrypoint: SessionStateGraph.entrypoints.WaitingForHandshake,
1121
+ transition: {
1122
+ // happy paths
1123
+ WaitingForHandshakeToConnected: transitions.WaitingForHandshakeToConnected,
1124
+ // disconnect paths
1125
+ ConnectedToNoConnection: transitions.ConnectedToNoConnection
1126
+ }
1127
+ };
1032
1128
 
1033
1129
  // transport/transport.ts
1034
1130
  var Transport = class {
@@ -1208,8 +1304,10 @@ var ClientTransport = class extends Transport {
1208
1304
  * Optional handshake options for this client.
1209
1305
  */
1210
1306
  handshakeExtensions;
1307
+ sessions;
1211
1308
  constructor(clientId, providedOptions) {
1212
1309
  super(clientId, providedOptions);
1310
+ this.sessions = /* @__PURE__ */ new Map();
1213
1311
  this.options = {
1214
1312
  ...defaultClientTransportOptions,
1215
1313
  ...providedOptions
@@ -1241,7 +1339,7 @@ var ClientTransport = class extends Transport {
1241
1339
  return session.send(msg);
1242
1340
  }
1243
1341
  createUnconnectedSession(to) {
1244
- const session = SessionStateGraph.entrypoints.NoConnection(
1342
+ const session = ClientSessionStateGraph.entrypoint(
1245
1343
  to,
1246
1344
  this.clientId,
1247
1345
  {
@@ -1268,40 +1366,44 @@ var ClientTransport = class extends Transport {
1268
1366
  return noConnectionSession;
1269
1367
  }
1270
1368
  onConnectionEstablished(session, conn) {
1271
- const handshakingSession = SessionStateGraph.transition.ConnectingToHandshaking(session, conn, {
1272
- onConnectionErrored: (err) => {
1273
- const errStr = coerceErrorString(err);
1274
- this.log?.error(
1275
- `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
1276
- handshakingSession.loggingMetadata
1277
- );
1278
- },
1279
- onConnectionClosed: () => {
1280
- this.log?.warn(
1281
- `connection to ${handshakingSession.to} closed during handshake`,
1282
- handshakingSession.loggingMetadata
1283
- );
1284
- this.onConnClosed(handshakingSession);
1285
- },
1286
- onHandshake: (msg) => {
1287
- this.onHandshakeResponse(handshakingSession, msg);
1288
- },
1289
- onInvalidHandshake: (reason) => {
1290
- this.log?.error(
1291
- `invalid handshake: ${reason}`,
1292
- handshakingSession.loggingMetadata
1293
- );
1294
- this.deleteSession(session);
1295
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1296
- },
1297
- onHandshakeTimeout: () => {
1298
- this.log?.error(
1299
- `connection to ${handshakingSession.to} timed out during handshake`,
1300
- handshakingSession.loggingMetadata
1301
- );
1302
- this.onConnClosed(handshakingSession);
1369
+ const handshakingSession = ClientSessionStateGraph.transition.ConnectingToHandshaking(
1370
+ session,
1371
+ conn,
1372
+ {
1373
+ onConnectionErrored: (err) => {
1374
+ const errStr = coerceErrorString(err);
1375
+ this.log?.error(
1376
+ `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
1377
+ handshakingSession.loggingMetadata
1378
+ );
1379
+ },
1380
+ onConnectionClosed: () => {
1381
+ this.log?.warn(
1382
+ `connection to ${handshakingSession.to} closed during handshake`,
1383
+ handshakingSession.loggingMetadata
1384
+ );
1385
+ this.onConnClosed(handshakingSession);
1386
+ },
1387
+ onHandshake: (msg) => {
1388
+ this.onHandshakeResponse(handshakingSession, msg);
1389
+ },
1390
+ onInvalidHandshake: (reason) => {
1391
+ this.log?.error(
1392
+ `invalid handshake: ${reason}`,
1393
+ handshakingSession.loggingMetadata
1394
+ );
1395
+ this.deleteSession(session);
1396
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
1397
+ },
1398
+ onHandshakeTimeout: () => {
1399
+ this.log?.error(
1400
+ `connection to ${handshakingSession.to} timed out during handshake`,
1401
+ handshakingSession.loggingMetadata
1402
+ );
1403
+ this.onConnClosed(handshakingSession);
1404
+ }
1303
1405
  }
1304
- });
1406
+ );
1305
1407
  this.updateSession(handshakingSession);
1306
1408
  void this.sendHandshake(handshakingSession);
1307
1409
  return handshakingSession;
@@ -1356,7 +1458,7 @@ var ClientTransport = class extends Transport {
1356
1458
  ...session.loggingMetadata,
1357
1459
  transportMessage: msg
1358
1460
  });
1359
- const connectedSession = SessionStateGraph.transition.HandshakingToConnected(session, {
1461
+ const connectedSession = ClientSessionStateGraph.transition.HandshakingToConnected(session, {
1360
1462
  onConnectionErrored: (err) => {
1361
1463
  const errStr = coerceErrorString(err);
1362
1464
  this.log?.warn(
@@ -1378,7 +1480,7 @@ var ClientTransport = class extends Transport {
1378
1480
  }
1379
1481
  });
1380
1482
  this.updateSession(connectedSession);
1381
- this.retryBudget.startRestoringBudget(connectedSession.to);
1483
+ this.retryBudget.startRestoringBudget();
1382
1484
  }
1383
1485
  /**
1384
1486
  * Manually attempts to connect to a client.
@@ -1401,44 +1503,49 @@ var ClientTransport = class extends Transport {
1401
1503
  );
1402
1504
  return;
1403
1505
  }
1404
- if (!this.retryBudget.hasBudget(to)) {
1405
- const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
1506
+ if (!this.retryBudget.hasBudget()) {
1507
+ const budgetConsumed = this.retryBudget.getBudgetConsumed();
1406
1508
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
1407
1509
  this.log?.error(errMsg, session.loggingMetadata);
1408
1510
  this.protocolError(ProtocolError.RetriesExceeded, errMsg);
1409
1511
  return;
1410
1512
  }
1411
- let sleep = Promise.resolve();
1412
- const backoffMs = this.retryBudget.getBackoffMs(to);
1413
- if (backoffMs > 0) {
1414
- sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
1415
- }
1513
+ const backoffMs = this.retryBudget.getBackoffMs();
1416
1514
  this.log?.info(
1417
1515
  `attempting connection to ${to} (${backoffMs}ms backoff)`,
1418
1516
  session.loggingMetadata
1419
1517
  );
1420
- this.retryBudget.consumeBudget(to);
1421
- const reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
1422
- try {
1423
- span.addEvent("backoff", { backoffMs });
1424
- await sleep;
1425
- if (this.getStatus() !== "open") {
1426
- throw new Error("transport state is no longer open");
1518
+ this.retryBudget.consumeBudget();
1519
+ const backingOffSession = ClientSessionStateGraph.transition.NoConnectionToBackingOff(
1520
+ session,
1521
+ backoffMs,
1522
+ {
1523
+ onBackoffFinished: () => {
1524
+ const reconnectPromise = tracing_default.startActiveSpan(
1525
+ "connect",
1526
+ async (span) => {
1527
+ try {
1528
+ return await this.createNewOutgoingConnection(to);
1529
+ } catch (err) {
1530
+ const errStr = coerceErrorString(err);
1531
+ span.recordException(errStr);
1532
+ span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
1533
+ throw err;
1534
+ } finally {
1535
+ span.end();
1536
+ }
1537
+ }
1538
+ );
1539
+ this.onBackoffFinished(backingOffSession, reconnectPromise);
1427
1540
  }
1428
- span.addEvent("connecting");
1429
- return await this.createNewOutgoingConnection(to);
1430
- } catch (err) {
1431
- const errStr = coerceErrorString(err);
1432
- span.recordException(errStr);
1433
- span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
1434
- throw err;
1435
- } finally {
1436
- span.end();
1437
1541
  }
1438
- });
1439
- const connectingSession = SessionStateGraph.transition.NoConnectionToConnecting(
1542
+ );
1543
+ this.updateSession(backingOffSession);
1544
+ }
1545
+ onBackoffFinished(session, connPromise) {
1546
+ const connectingSession = ClientSessionStateGraph.transition.BackingOffToConnecting(
1440
1547
  session,
1441
- reconnectPromise,
1548
+ connPromise,
1442
1549
  {
1443
1550
  onConnectionEstablished: (conn) => {
1444
1551
  this.log?.debug(