@replit/river 0.17.4 → 0.18.1

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 (45) hide show
  1. package/README.md +4 -3
  2. package/dist/{chunk-F3LFO3GU.js → chunk-5YDJDYVB.js} +1 -1
  3. package/dist/{chunk-Q7OSVPZ5.js → chunk-O36533OC.js} +1 -1
  4. package/dist/{chunk-4C2OXQJB.js → chunk-QBGGJSQM.js} +62 -65
  5. package/dist/{chunk-7WY3Z5ZN.js → chunk-UNTGVPI6.js} +193 -95
  6. package/dist/chunk-XCQF55SQ.js +72 -0
  7. package/dist/{connection-c4a17403.d.ts → connection-893bd769.d.ts} +1 -1
  8. package/dist/{connection-bdbd20da.d.ts → connection-89918b74.d.ts} +1 -1
  9. package/dist/index-46ed19d8.d.ts +111 -0
  10. package/dist/{index-9e300e8a.d.ts → index-d412ca83.d.ts} +4 -86
  11. package/dist/logging/index.cjs +63 -27
  12. package/dist/logging/index.d.cts +2 -34
  13. package/dist/logging/index.d.ts +2 -34
  14. package/dist/logging/index.js +7 -7
  15. package/dist/{procedures-1c0d2eee.d.ts → procedures-85e52b9c.d.ts} +4 -3
  16. package/dist/router/index.cjs +63 -66
  17. package/dist/router/index.d.cts +43 -42
  18. package/dist/router/index.d.ts +43 -42
  19. package/dist/router/index.js +2 -2
  20. package/dist/transport/impls/uds/client.cjs +162 -84
  21. package/dist/transport/impls/uds/client.d.cts +3 -2
  22. package/dist/transport/impls/uds/client.d.ts +3 -2
  23. package/dist/transport/impls/uds/client.js +7 -4
  24. package/dist/transport/impls/uds/server.cjs +130 -65
  25. package/dist/transport/impls/uds/server.d.cts +3 -2
  26. package/dist/transport/impls/uds/server.d.ts +3 -2
  27. package/dist/transport/impls/uds/server.js +3 -3
  28. package/dist/transport/impls/ws/client.cjs +166 -87
  29. package/dist/transport/impls/ws/client.d.cts +3 -2
  30. package/dist/transport/impls/ws/client.d.ts +3 -2
  31. package/dist/transport/impls/ws/client.js +11 -7
  32. package/dist/transport/impls/ws/server.cjs +130 -65
  33. package/dist/transport/impls/ws/server.d.cts +4 -3
  34. package/dist/transport/impls/ws/server.d.ts +4 -3
  35. package/dist/transport/impls/ws/server.js +3 -3
  36. package/dist/transport/index.cjs +194 -96
  37. package/dist/transport/index.d.cts +2 -1
  38. package/dist/transport/index.d.ts +2 -1
  39. package/dist/transport/index.js +2 -2
  40. package/dist/util/testHelpers.cjs +48 -17
  41. package/dist/util/testHelpers.d.cts +4 -3
  42. package/dist/util/testHelpers.d.ts +4 -3
  43. package/dist/util/testHelpers.js +3 -3
  44. package/package.json +1 -1
  45. package/dist/chunk-H4BYJELI.js +0 -37
@@ -118,8 +118,8 @@ function isAck(controlFlag) {
118
118
  return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
119
119
  }
120
120
 
121
- // logging/index.ts
122
- var log;
121
+ // logging/log.ts
122
+ var log = void 0;
123
123
 
124
124
  // transport/events.ts
125
125
  var ProtocolError = {
@@ -221,6 +221,14 @@ var Session = class {
221
221
  options.heartbeatIntervalMs
222
222
  );
223
223
  }
224
+ get loggingMetadata() {
225
+ return {
226
+ clientId: this.from,
227
+ connectedTo: this.to,
228
+ sessionId: this.id,
229
+ connId: this.connection?.debugId
230
+ };
231
+ }
224
232
  /**
225
233
  * Sends a message over the session's connection.
226
234
  * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
@@ -231,26 +239,37 @@ var Session = class {
231
239
  */
232
240
  send(msg) {
233
241
  const fullMsg = this.constructMsg(msg);
234
- log?.debug(`${this.from} -- sending ${JSON.stringify(fullMsg)}`);
242
+ log?.debug(`sending msg`, {
243
+ ...this.loggingMetadata,
244
+ fullTransportMessage: fullMsg
245
+ });
235
246
  if (this.connection) {
236
247
  const ok = this.connection.send(this.codec.toBuffer(fullMsg));
237
248
  if (ok)
238
249
  return fullMsg.id;
239
250
  log?.info(
240
- `${this.from} -- failed to send ${fullMsg.id} (seq: ${fullMsg.seq}) to ${fullMsg.to}, connection (id: ${this.connection.debugId}) is probably dead`
251
+ `failed to send msg to ${fullMsg.to}, connection is probably dead`,
252
+ {
253
+ ...this.loggingMetadata,
254
+ fullTransportMessage: fullMsg
255
+ }
241
256
  );
242
257
  } else {
243
258
  log?.info(
244
- `${this.from} -- failed to send ${fullMsg.id} (seq: ${fullMsg.seq}) to ${fullMsg.to}, connection not ready yet`
259
+ `failed to send msg to ${fullMsg.to}, connection not ready yet`,
260
+ { ...this.loggingMetadata, fullTransportMessage: fullMsg }
245
261
  );
246
262
  }
247
263
  return fullMsg.id;
248
264
  }
249
265
  sendHeartbeat() {
250
- if (this.heartbeatMisses >= this.options.heartbeatsUntilDead) {
266
+ const misses = this.heartbeatMisses;
267
+ const missDuration = misses * this.options.heartbeatIntervalMs;
268
+ if (misses > this.options.heartbeatsUntilDead) {
251
269
  if (this.connection) {
252
270
  log?.info(
253
- `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
271
+ `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
272
+ this.loggingMetadata
254
273
  );
255
274
  this.closeStaleConnection();
256
275
  }
@@ -272,26 +291,36 @@ var Session = class {
272
291
  }
273
292
  sendBufferedMessages() {
274
293
  if (!this.connection) {
275
- const msg = `${this.from} -- tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
276
- log?.error(msg);
294
+ const msg = `tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
295
+ log?.error(msg, this.loggingMetadata);
277
296
  throw new Error(msg);
278
297
  }
279
298
  log?.info(
280
- `${this.from} -- resending ${this.sendBuffer.length} buffered messages`
299
+ `resending ${this.sendBuffer.length} buffered messages`,
300
+ this.loggingMetadata
281
301
  );
282
302
  for (const msg of this.sendBuffer) {
283
- log?.debug(`${this.from} -- resending ${msg.id} (seq: ${msg.seq})`);
303
+ log?.debug(`resending msg`, {
304
+ ...this.loggingMetadata,
305
+ fullTransportMessage: msg
306
+ });
284
307
  const ok = this.connection.send(this.codec.toBuffer(msg));
285
308
  if (!ok) {
286
- const msg2 = `${this.from} -- failed to send buffered message to ${this.to} in session (id: ${this.id}) (if you hit this code path something is seriously wrong)`;
287
- log?.error(msg2);
288
- throw new Error(msg2);
309
+ const errMsg = `failed to send buffered message to ${this.to} (if you hit this code path something is seriously wrong)`;
310
+ log?.error(errMsg, {
311
+ ...this.loggingMetadata,
312
+ fullTransportMessage: msg
313
+ });
314
+ throw new Error(errMsg);
289
315
  }
290
316
  }
291
317
  }
292
318
  updateBookkeeping(ack, seq) {
293
319
  if (seq + 1 < this.ack) {
294
- log?.error(`${this.from} -- received stale seq ${seq} + 1 < ${this.ack}`);
320
+ log?.error(
321
+ `received stale seq ${seq} + 1 < ${this.ack}`,
322
+ this.loggingMetadata
323
+ );
295
324
  return;
296
325
  }
297
326
  this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
@@ -301,7 +330,8 @@ var Session = class {
301
330
  if (this.connection === void 0 || this.connection === conn)
302
331
  return;
303
332
  log?.info(
304
- `${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.id}) to ${this.to}`
333
+ `closing old inner connection from session to ${this.to}`,
334
+ this.loggingMetadata
305
335
  );
306
336
  this.connection.close();
307
337
  this.connection = void 0;
@@ -313,7 +343,8 @@ var Session = class {
313
343
  }
314
344
  beginGrace(cb) {
315
345
  log?.info(
316
- `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.id}) to ${this.to} is closed`
346
+ `starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
347
+ this.loggingMetadata
317
348
  );
318
349
  this.disconnectionGrace = setTimeout(() => {
319
350
  this.close();
@@ -568,7 +599,8 @@ var Transport = class {
568
599
  let oldSession = this.sessions.get(connectedTo);
569
600
  if (oldSession?.advertisedSessionId && oldSession.advertisedSessionId !== advertisedSessionId) {
570
601
  log?.warn(
571
- `${this.clientId} -- connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), starting a new session`
602
+ `connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), killing old session and starting a new one`,
603
+ oldSession.loggingMetadata
572
604
  );
573
605
  this.deleteSession(oldSession);
574
606
  oldSession = void 0;
@@ -577,16 +609,18 @@ var Transport = class {
577
609
  const newSession = this.createSession(connectedTo, conn);
578
610
  newSession.advertisedSessionId = advertisedSessionId;
579
611
  log?.info(
580
- `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.id}) to ${connectedTo}`
612
+ `new connection for new session to ${connectedTo}`,
613
+ newSession.loggingMetadata
581
614
  );
582
615
  return newSession;
583
616
  }
584
- log?.info(
585
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.id}) to ${connectedTo}`
586
- );
587
617
  oldSession.replaceWithNewConnection(conn);
588
618
  oldSession.sendBufferedMessages();
589
619
  oldSession.advertisedSessionId = advertisedSessionId;
620
+ log?.info(
621
+ `new connection for existing session to ${connectedTo}`,
622
+ oldSession.loggingMetadata
623
+ );
590
624
  return oldSession;
591
625
  }
592
626
  createSession(to, conn) {
@@ -608,7 +642,8 @@ var Transport = class {
608
642
  if (!session) {
609
643
  session = this.createSession(to, conn);
610
644
  log?.info(
611
- `${this.clientId} -- no session for ${to}, created a new one (id: ${session.id})`
645
+ `no session for ${to}, created a new one`,
646
+ session.loggingMetadata
612
647
  );
613
648
  }
614
649
  return session;
@@ -617,7 +652,8 @@ var Transport = class {
617
652
  session.close();
618
653
  this.sessions.delete(session.to);
619
654
  log?.info(
620
- `${this.clientId} -- session ${session.id} disconnect from ${session.to}`
655
+ `session ${session.id} disconnect from ${session.to}`,
656
+ session.loggingMetadata
621
657
  );
622
658
  this.eventDispatcher.dispatchEvent("sessionStatus", {
623
659
  status: "disconnect",
@@ -646,17 +682,15 @@ var Transport = class {
646
682
  const parsedMsg = this.codec.fromBuffer(msg);
647
683
  if (parsedMsg === null) {
648
684
  const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));
649
- log?.error(
650
- `${this.clientId} -- received malformed msg, killing conn: ${decodedBuffer}`
651
- );
685
+ log?.error(`received malformed msg, killing conn: ${decodedBuffer}`, {
686
+ clientId: this.clientId
687
+ });
652
688
  return null;
653
689
  }
654
690
  if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
655
- log?.error(
656
- `${this.clientId} -- received invalid msg: ${JSON.stringify(
657
- parsedMsg
658
- )}`
659
- );
691
+ log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
692
+ clientId: this.clientId
693
+ });
660
694
  return null;
661
695
  }
662
696
  return parsedMsg;
@@ -671,26 +705,29 @@ var Transport = class {
671
705
  return;
672
706
  const session = this.sessions.get(msg.from);
673
707
  if (!session) {
674
- const err = `${this.clientId} -- (invariant violation) no existing session for ${msg.from}`;
675
- log?.error(err);
708
+ log?.error(`(invariant violation) no existing session for ${msg.from}`, {
709
+ clientId: this.clientId,
710
+ fullTransportMessage: msg
711
+ });
676
712
  return;
677
713
  }
678
714
  session.cancelGrace();
679
- log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
715
+ log?.debug(`received msg`, {
716
+ clientId: this.clientId,
717
+ fullTransportMessage: msg
718
+ });
680
719
  if (msg.seq !== session.nextExpectedSeq) {
681
720
  if (msg.seq < session.nextExpectedSeq) {
682
721
  log?.debug(
683
- `${this.clientId} -- received duplicate msg (got: ${msg.seq}, wanted: ${session.nextExpectedSeq}), discarding: ${JSON.stringify(
684
- msg
685
- )}`
722
+ `received duplicate msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq}), discarding`,
723
+ { clientId: this.clientId, fullTransportMessage: msg }
686
724
  );
687
725
  } else {
688
726
  const errMsg = `received out-of-order msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq})`;
689
- log?.error(
690
- `${this.clientId} -- fatal: ${errMsg}, marking connection as dead: ${JSON.stringify(
691
- msg
692
- )}`
693
- );
727
+ log?.error(`${errMsg}, marking connection as dead`, {
728
+ clientId: this.clientId,
729
+ fullTransportMessage: msg
730
+ });
694
731
  this.protocolError(ProtocolError.MessageOrderingViolated, errMsg);
695
732
  session.close();
696
733
  }
@@ -700,7 +737,10 @@ var Transport = class {
700
737
  if (!isAck(msg.controlFlags)) {
701
738
  this.eventDispatcher.dispatchEvent("message", msg);
702
739
  } else {
703
- log?.debug(`${this.clientId} -- discarding msg ${msg.id} (ack bit set)`);
740
+ log?.debug(`discarding msg (ack bit set)`, {
741
+ clientId: this.clientId,
742
+ fullTransportMessage: msg
743
+ });
704
744
  }
705
745
  }
706
746
  /**
@@ -728,15 +768,17 @@ var Transport = class {
728
768
  send(to, msg) {
729
769
  if (this.state === "destroyed") {
730
770
  const err = "transport is destroyed, cant send";
731
- log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
771
+ log?.error(err, {
772
+ clientId: this.clientId,
773
+ partialTransportMessage: msg
774
+ });
732
775
  this.protocolError(ProtocolError.UseAfterDestroy, err);
733
776
  return void 0;
734
777
  } else if (this.state === "closed") {
735
- log?.info(
736
- `${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
737
- msg
738
- )}`
739
- );
778
+ log?.info(`transport closed when sending, discarding`, {
779
+ clientId: this.clientId,
780
+ partialTransportMessage: msg
781
+ });
740
782
  return void 0;
741
783
  }
742
784
  return this.getOrCreateSession(to).send(msg);
@@ -764,7 +806,7 @@ var Transport = class {
764
806
  for (const session of this.sessions.values()) {
765
807
  this.deleteSession(session);
766
808
  }
767
- log?.info(`${this.clientId} -- manually closed transport`);
809
+ log?.info(`manually closed transport`, { clientId: this.clientId });
768
810
  }
769
811
  /**
770
812
  * Default destroy implementation for transports. You should override this in the downstream
@@ -776,7 +818,7 @@ var Transport = class {
776
818
  for (const session of this.sessions.values()) {
777
819
  this.deleteSession(session);
778
820
  }
779
- log?.info(`${this.clientId} -- manually destroyed transport`);
821
+ log?.info(`manually destroyed transport`, { clientId: this.clientId });
780
822
  }
781
823
  };
782
824
  var ClientTransport = class extends Transport {
@@ -809,6 +851,15 @@ var ClientTransport = class extends Transport {
809
851
  if (this.state !== "open")
810
852
  return;
811
853
  let session = void 0;
854
+ const handshakeTimeout = setTimeout(() => {
855
+ if (!session) {
856
+ log?.warn(
857
+ `connection to ${to} timed out waiting for handshake, closing`,
858
+ { clientId: this.clientId, connectedTo: to, connId: conn.debugId }
859
+ );
860
+ conn.close();
861
+ }
862
+ }, this.options.sessionDisconnectGraceMs);
812
863
  const handshakeHandler = (data) => {
813
864
  const maybeSession = this.receiveHandshakeResponseMessage(data, conn);
814
865
  if (!maybeSession) {
@@ -816,6 +867,7 @@ var ClientTransport = class extends Transport {
816
867
  return;
817
868
  } else {
818
869
  session = maybeSession;
870
+ clearTimeout(handshakeTimeout);
819
871
  }
820
872
  conn.removeDataListener(handshakeHandler);
821
873
  conn.addDataListener((data2) => {
@@ -832,18 +884,22 @@ var ClientTransport = class extends Transport {
832
884
  if (session) {
833
885
  this.onDisconnect(conn, session);
834
886
  }
835
- log?.info(
836
- `${this.clientId} -- connection (id: ${conn.debugId}) to ${to} disconnected`
837
- );
887
+ log?.info(`connection to ${to} disconnected`, {
888
+ ...session?.loggingMetadata,
889
+ clientId: this.clientId,
890
+ connectedTo: to
891
+ });
838
892
  this.inflightConnectionPromises.delete(to);
839
893
  if (this.reconnectOnConnectionDrop) {
840
894
  void this.connect(to);
841
895
  }
842
896
  });
843
897
  conn.addErrorListener((err) => {
844
- log?.warn(
845
- `${this.clientId} -- error in connection (id: ${conn.debugId}) to ${to}: ${coerceErrorString(err)}`
846
- );
898
+ log?.warn(`error in connection to ${to}: ${coerceErrorString(err)}`, {
899
+ ...session?.loggingMetadata,
900
+ clientId: this.clientId,
901
+ connectedTo: to
902
+ });
847
903
  });
848
904
  }
849
905
  receiveHandshakeResponseMessage(data, conn) {
@@ -856,11 +912,11 @@ var ClientTransport = class extends Transport {
856
912
  return false;
857
913
  }
858
914
  if (!import_value.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
859
- log?.warn(
860
- `${this.clientId} -- received invalid handshake resp: ${JSON.stringify(
861
- parsed
862
- )}`
863
- );
915
+ log?.warn(`received invalid handshake resp`, {
916
+ clientId: this.clientId,
917
+ connectedTo: parsed.from,
918
+ fullTransportMessage: parsed
919
+ });
864
920
  this.protocolError(
865
921
  ProtocolError.HandshakeFailed,
866
922
  "invalid handshake resp"
@@ -868,18 +924,22 @@ var ClientTransport = class extends Transport {
868
924
  return false;
869
925
  }
870
926
  if (!parsed.payload.status.ok) {
871
- log?.warn(
872
- `${this.clientId} -- received failed handshake resp: ${JSON.stringify(
873
- parsed
874
- )}`
875
- );
927
+ log?.warn(`received invalid handshake resp`, {
928
+ clientId: this.clientId,
929
+ connectedTo: parsed.from,
930
+ fullTransportMessage: parsed
931
+ });
876
932
  this.protocolError(
877
933
  ProtocolError.HandshakeFailed,
878
934
  parsed.payload.status.reason
879
935
  );
880
936
  return false;
881
937
  }
882
- log?.debug(`${this.clientId} -- handshake from ${parsed.from} ok`);
938
+ log?.debug(`handshake from ${parsed.from} ok`, {
939
+ clientId: this.clientId,
940
+ connectedTo: parsed.from,
941
+ fullTransportMessage: parsed
942
+ });
883
943
  const session = this.onConnect(
884
944
  conn,
885
945
  parsed.from,
@@ -896,7 +956,8 @@ var ClientTransport = class extends Transport {
896
956
  const canProceedWithConnection = () => this.state === "open";
897
957
  if (!canProceedWithConnection()) {
898
958
  log?.info(
899
- `${this.clientId} -- transport state is no longer open, cancelling attempt to connect to ${to}`
959
+ `transport state is no longer open, cancelling attempt to connect to ${to}`,
960
+ { clientId: this.clientId, connectedTo: to }
900
961
  );
901
962
  return;
902
963
  }
@@ -905,7 +966,7 @@ var ClientTransport = class extends Transport {
905
966
  const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
906
967
  if (!this.retryBudget.hasBudget(to)) {
907
968
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
908
- log?.warn(`${this.clientId} -- ${errMsg}`);
969
+ log?.warn(errMsg, { clientId: this.clientId, connectedTo: to });
909
970
  this.protocolError(ProtocolError.RetriesExceeded, errMsg);
910
971
  return;
911
972
  }
@@ -914,9 +975,10 @@ var ClientTransport = class extends Transport {
914
975
  if (backoffMs > 0) {
915
976
  sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
916
977
  }
917
- log?.info(
918
- `${this.clientId} -- attempting connection to ${to} (${backoffMs}ms backoff)`
919
- );
978
+ log?.info(`attempting connection to ${to} (${backoffMs}ms backoff)`, {
979
+ clientId: this.clientId,
980
+ connectedTo: to
981
+ });
920
982
  this.retryBudget.consumeBudget(to);
921
983
  reconnectPromise = sleep.then(() => {
922
984
  if (!canProceedWithConnection()) {
@@ -925,7 +987,12 @@ var ClientTransport = class extends Transport {
925
987
  }).then(() => this.createNewOutgoingConnection(to)).then((conn) => {
926
988
  if (!canProceedWithConnection()) {
927
989
  log?.info(
928
- `${this.clientId} -- transport state is no longer open, closing pre-handshake connection (id: ${conn.debugId}) to ${to}`
990
+ `transport state is no longer open, closing pre-handshake connection to ${to}`,
991
+ {
992
+ clientId: this.clientId,
993
+ connectedTo: to,
994
+ connId: conn.debugId
995
+ }
929
996
  );
930
997
  conn.close();
931
998
  throw new Error("transport state is no longer open");
@@ -935,9 +1002,10 @@ var ClientTransport = class extends Transport {
935
1002
  });
936
1003
  this.inflightConnectionPromises.set(to, reconnectPromise);
937
1004
  } else {
938
- log?.info(
939
- `${this.clientId} -- attempting connection to ${to} (reusing previous attempt)`
940
- );
1005
+ log?.info(`attempting connection to ${to} (reusing previous attempt)`, {
1006
+ clientId: this.clientId,
1007
+ connectedTo: to
1008
+ });
941
1009
  }
942
1010
  try {
943
1011
  await reconnectPromise;
@@ -945,11 +1013,15 @@ var ClientTransport = class extends Transport {
945
1013
  this.inflightConnectionPromises.delete(to);
946
1014
  const errStr = coerceErrorString(error);
947
1015
  if (!this.reconnectOnConnectionDrop || !canProceedWithConnection()) {
948
- log?.warn(`${this.clientId} -- connection to ${to} failed (${errStr})`);
1016
+ log?.warn(`connection to ${to} failed (${errStr})`, {
1017
+ clientId: this.clientId,
1018
+ connectedTo: to
1019
+ });
949
1020
  } else {
950
- log?.warn(
951
- `${this.clientId} -- connection to ${to} failed (${errStr}), retrying`
952
- );
1021
+ log?.warn(`connection to ${to} failed (${errStr}), retrying`, {
1022
+ clientId: this.clientId,
1023
+ connectedTo: to
1024
+ });
953
1025
  return this.connect(to);
954
1026
  }
955
1027
  }
@@ -961,7 +1033,10 @@ var ClientTransport = class extends Transport {
961
1033
  sendHandshake(to, conn) {
962
1034
  const session = this.getOrCreateSession(to, conn);
963
1035
  const requestMsg = handshakeRequestMessage(this.clientId, to, session.id);
964
- log?.debug(`${this.clientId} -- sending handshake request to ${to}`);
1036
+ log?.debug(`sending handshake request to ${to}`, {
1037
+ clientId: this.clientId,
1038
+ connectedTo: to
1039
+ });
965
1040
  conn.send(this.codec.toBuffer(requestMsg));
966
1041
  }
967
1042
  close() {
@@ -972,18 +1047,33 @@ var ClientTransport = class extends Transport {
972
1047
  var ServerTransport = class extends Transport {
973
1048
  constructor(clientId, providedOptions) {
974
1049
  super(clientId, providedOptions);
975
- log?.info(
976
- `${this.clientId} -- initiated server transport (protocol: ${PROTOCOL_VERSION})`
977
- );
1050
+ log?.info(`initiated server transport`, {
1051
+ clientId: this.clientId,
1052
+ protocolVersion: PROTOCOL_VERSION
1053
+ });
978
1054
  }
979
1055
  handleConnection(conn) {
980
1056
  if (this.state !== "open")
981
1057
  return;
982
- log?.info(
983
- `${this.clientId} -- new incoming connection (id: ${conn.debugId})`
984
- );
1058
+ log?.info(`new incoming connection`, {
1059
+ clientId: this.clientId,
1060
+ connId: conn.debugId
1061
+ });
985
1062
  let session = void 0;
986
1063
  const client = () => session?.to ?? "unknown";
1064
+ const handshakeTimeout = setTimeout(() => {
1065
+ if (!session) {
1066
+ log?.warn(
1067
+ `connection to ${client()} timed out waiting for handshake, closing`,
1068
+ {
1069
+ clientId: this.clientId,
1070
+ connectedTo: client(),
1071
+ connId: conn.debugId
1072
+ }
1073
+ );
1074
+ conn.close();
1075
+ }
1076
+ }, this.options.sessionDisconnectGraceMs);
987
1077
  const handshakeHandler = (data) => {
988
1078
  const maybeSession = this.receiveHandshakeRequestMessage(data, conn);
989
1079
  if (!maybeSession) {
@@ -991,6 +1081,7 @@ var ServerTransport = class extends Transport {
991
1081
  return;
992
1082
  } else {
993
1083
  session = maybeSession;
1084
+ clearTimeout(handshakeTimeout);
994
1085
  }
995
1086
  conn.removeDataListener(handshakeHandler);
996
1087
  conn.addDataListener((data2) => {
@@ -1006,16 +1097,18 @@ var ServerTransport = class extends Transport {
1006
1097
  conn.addCloseListener(() => {
1007
1098
  if (!session)
1008
1099
  return;
1009
- log?.info(
1010
- `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
1011
- );
1100
+ log?.info(`connection to ${client()} disconnected`, {
1101
+ clientId: this.clientId,
1102
+ connId: conn.debugId
1103
+ });
1012
1104
  this.onDisconnect(conn, session);
1013
1105
  });
1014
1106
  conn.addErrorListener((err) => {
1015
1107
  if (!session)
1016
1108
  return;
1017
1109
  log?.warn(
1018
- `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} got an error: ${coerceErrorString(err)}`
1110
+ `connection to ${client()} got an error: ${coerceErrorString(err)}`,
1111
+ { clientId: this.clientId, connId: conn.debugId }
1019
1112
  );
1020
1113
  });
1021
1114
  }
@@ -1035,7 +1128,10 @@ var ServerTransport = class extends Transport {
1035
1128
  reason
1036
1129
  });
1037
1130
  conn.send(this.codec.toBuffer(responseMsg2));
1038
- log?.warn(`${this.clientId} -- ${reason}: ${JSON.stringify(parsed)}`);
1131
+ log?.warn(`${reason}: ${JSON.stringify(parsed)}`, {
1132
+ clientId: this.clientId,
1133
+ connId: conn.debugId
1134
+ });
1039
1135
  this.protocolError(
1040
1136
  ProtocolError.HandshakeFailed,
1041
1137
  "invalid handshake request"
@@ -1051,14 +1147,16 @@ var ServerTransport = class extends Transport {
1051
1147
  });
1052
1148
  conn.send(this.codec.toBuffer(responseMsg2));
1053
1149
  log?.warn(
1054
- `${this.clientId} -- received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`
1150
+ `received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`,
1151
+ { clientId: this.clientId, connId: conn.debugId }
1055
1152
  );
1056
1153
  this.protocolError(ProtocolError.HandshakeFailed, reason);
1057
1154
  return false;
1058
1155
  }
1059
1156
  const session = this.getOrCreateSession(parsed.from, conn);
1060
1157
  log?.debug(
1061
- `${this.clientId} -- handshake from ${parsed.from} ok, responding with handshake success`
1158
+ `handshake from ${parsed.from} ok, responding with handshake success`,
1159
+ { clientId: this.clientId, connId: conn.debugId }
1062
1160
  );
1063
1161
  const responseMsg = handshakeResponseMessage(this.clientId, parsed.from, {
1064
1162
  ok: true,
@@ -1,3 +1,4 @@
1
- export { b as ClientTransport, d as ClientTransportOptions, C as Connection, n as EventHandler, E as EventMap, m as EventTypes, O as OpaqueTransportMessage, i as OpaqueTransportMessageSchema, o as ProtocolError, p as ProtocolErrorType, e as ServerTransport, a as Session, T as Transport, c as TransportClientId, j as TransportMessage, h as TransportMessageSchema, f as TransportOptions, g as TransportStatus, l as isStreamClose, k as isStreamOpen } from '../index-9e300e8a.js';
1
+ export { b as ClientTransport, P as ClientTransportOptions, C as Connection, g as EventHandler, E as EventMap, f as EventTypes, h as ProtocolError, i as ProtocolErrorType, c as ServerTransport, a as Session, T as Transport, d as TransportOptions, e as TransportStatus } from '../index-d412ca83.js';
2
+ export { O as OpaqueTransportMessage, c as OpaqueTransportMessageSchema, T as TransportClientId, a as TransportMessage, b as TransportMessageSchema, d as isStreamClose, i as isStreamOpen } from '../index-46ed19d8.js';
2
3
  import '../types-3e5768ec.js';
3
4
  import '@sinclair/typebox';
@@ -1,3 +1,4 @@
1
- export { b as ClientTransport, d as ClientTransportOptions, C as Connection, n as EventHandler, E as EventMap, m as EventTypes, O as OpaqueTransportMessage, i as OpaqueTransportMessageSchema, o as ProtocolError, p as ProtocolErrorType, e as ServerTransport, a as Session, T as Transport, c as TransportClientId, j as TransportMessage, h as TransportMessageSchema, f as TransportOptions, g as TransportStatus, l as isStreamClose, k as isStreamOpen } from '../index-9e300e8a.js';
1
+ export { b as ClientTransport, P as ClientTransportOptions, C as Connection, g as EventHandler, E as EventMap, f as EventTypes, h as ProtocolError, i as ProtocolErrorType, c as ServerTransport, a as Session, T as Transport, d as TransportOptions, e as TransportStatus } from '../index-d412ca83.js';
2
+ export { O as OpaqueTransportMessage, c as OpaqueTransportMessageSchema, T as TransportClientId, a as TransportMessage, b as TransportMessageSchema, d as isStreamClose, i as isStreamOpen } from '../index-46ed19d8.js';
2
3
  import '../types-3e5768ec.js';
3
4
  import '@sinclair/typebox';
@@ -6,12 +6,12 @@ import {
6
6
  ServerTransport,
7
7
  Session,
8
8
  Transport
9
- } from "../chunk-7WY3Z5ZN.js";
9
+ } from "../chunk-UNTGVPI6.js";
10
10
  import {
11
11
  OpaqueTransportMessageSchema,
12
12
  TransportMessageSchema
13
13
  } from "../chunk-VH3NGOXQ.js";
14
- import "../chunk-H4BYJELI.js";
14
+ import "../chunk-XCQF55SQ.js";
15
15
  import "../chunk-GZ7HCLLM.js";
16
16
  export {
17
17
  ClientTransport,