@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
@@ -10,7 +10,7 @@ import {
10
10
  } from "./chunk-VH3NGOXQ.js";
11
11
  import {
12
12
  log
13
- } from "./chunk-H4BYJELI.js";
13
+ } from "./chunk-XCQF55SQ.js";
14
14
  import {
15
15
  NaiveJsonCodec
16
16
  } from "./chunk-GZ7HCLLM.js";
@@ -115,6 +115,14 @@ var Session = class {
115
115
  options.heartbeatIntervalMs
116
116
  );
117
117
  }
118
+ get loggingMetadata() {
119
+ return {
120
+ clientId: this.from,
121
+ connectedTo: this.to,
122
+ sessionId: this.id,
123
+ connId: this.connection?.debugId
124
+ };
125
+ }
118
126
  /**
119
127
  * Sends a message over the session's connection.
120
128
  * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
@@ -125,26 +133,37 @@ var Session = class {
125
133
  */
126
134
  send(msg) {
127
135
  const fullMsg = this.constructMsg(msg);
128
- log?.debug(`${this.from} -- sending ${JSON.stringify(fullMsg)}`);
136
+ log?.debug(`sending msg`, {
137
+ ...this.loggingMetadata,
138
+ fullTransportMessage: fullMsg
139
+ });
129
140
  if (this.connection) {
130
141
  const ok = this.connection.send(this.codec.toBuffer(fullMsg));
131
142
  if (ok)
132
143
  return fullMsg.id;
133
144
  log?.info(
134
- `${this.from} -- failed to send ${fullMsg.id} (seq: ${fullMsg.seq}) to ${fullMsg.to}, connection (id: ${this.connection.debugId}) is probably dead`
145
+ `failed to send msg to ${fullMsg.to}, connection is probably dead`,
146
+ {
147
+ ...this.loggingMetadata,
148
+ fullTransportMessage: fullMsg
149
+ }
135
150
  );
136
151
  } else {
137
152
  log?.info(
138
- `${this.from} -- failed to send ${fullMsg.id} (seq: ${fullMsg.seq}) to ${fullMsg.to}, connection not ready yet`
153
+ `failed to send msg to ${fullMsg.to}, connection not ready yet`,
154
+ { ...this.loggingMetadata, fullTransportMessage: fullMsg }
139
155
  );
140
156
  }
141
157
  return fullMsg.id;
142
158
  }
143
159
  sendHeartbeat() {
144
- if (this.heartbeatMisses >= this.options.heartbeatsUntilDead) {
160
+ const misses = this.heartbeatMisses;
161
+ const missDuration = misses * this.options.heartbeatIntervalMs;
162
+ if (misses > this.options.heartbeatsUntilDead) {
145
163
  if (this.connection) {
146
164
  log?.info(
147
- `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
165
+ `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
166
+ this.loggingMetadata
148
167
  );
149
168
  this.closeStaleConnection();
150
169
  }
@@ -166,26 +185,36 @@ var Session = class {
166
185
  }
167
186
  sendBufferedMessages() {
168
187
  if (!this.connection) {
169
- const msg = `${this.from} -- tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
170
- log?.error(msg);
188
+ const msg = `tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
189
+ log?.error(msg, this.loggingMetadata);
171
190
  throw new Error(msg);
172
191
  }
173
192
  log?.info(
174
- `${this.from} -- resending ${this.sendBuffer.length} buffered messages`
193
+ `resending ${this.sendBuffer.length} buffered messages`,
194
+ this.loggingMetadata
175
195
  );
176
196
  for (const msg of this.sendBuffer) {
177
- log?.debug(`${this.from} -- resending ${msg.id} (seq: ${msg.seq})`);
197
+ log?.debug(`resending msg`, {
198
+ ...this.loggingMetadata,
199
+ fullTransportMessage: msg
200
+ });
178
201
  const ok = this.connection.send(this.codec.toBuffer(msg));
179
202
  if (!ok) {
180
- 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)`;
181
- log?.error(msg2);
182
- throw new Error(msg2);
203
+ const errMsg = `failed to send buffered message to ${this.to} (if you hit this code path something is seriously wrong)`;
204
+ log?.error(errMsg, {
205
+ ...this.loggingMetadata,
206
+ fullTransportMessage: msg
207
+ });
208
+ throw new Error(errMsg);
183
209
  }
184
210
  }
185
211
  }
186
212
  updateBookkeeping(ack, seq) {
187
213
  if (seq + 1 < this.ack) {
188
- log?.error(`${this.from} -- received stale seq ${seq} + 1 < ${this.ack}`);
214
+ log?.error(
215
+ `received stale seq ${seq} + 1 < ${this.ack}`,
216
+ this.loggingMetadata
217
+ );
189
218
  return;
190
219
  }
191
220
  this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
@@ -195,7 +224,8 @@ var Session = class {
195
224
  if (this.connection === void 0 || this.connection === conn)
196
225
  return;
197
226
  log?.info(
198
- `${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.id}) to ${this.to}`
227
+ `closing old inner connection from session to ${this.to}`,
228
+ this.loggingMetadata
199
229
  );
200
230
  this.connection.close();
201
231
  this.connection = void 0;
@@ -207,7 +237,8 @@ var Session = class {
207
237
  }
208
238
  beginGrace(cb) {
209
239
  log?.info(
210
- `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.id}) to ${this.to} is closed`
240
+ `starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
241
+ this.loggingMetadata
211
242
  );
212
243
  this.disconnectionGrace = setTimeout(() => {
213
244
  this.close();
@@ -405,7 +436,8 @@ var Transport = class {
405
436
  let oldSession = this.sessions.get(connectedTo);
406
437
  if (oldSession?.advertisedSessionId && oldSession.advertisedSessionId !== advertisedSessionId) {
407
438
  log?.warn(
408
- `${this.clientId} -- connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), starting a new session`
439
+ `connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), killing old session and starting a new one`,
440
+ oldSession.loggingMetadata
409
441
  );
410
442
  this.deleteSession(oldSession);
411
443
  oldSession = void 0;
@@ -414,16 +446,18 @@ var Transport = class {
414
446
  const newSession = this.createSession(connectedTo, conn);
415
447
  newSession.advertisedSessionId = advertisedSessionId;
416
448
  log?.info(
417
- `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.id}) to ${connectedTo}`
449
+ `new connection for new session to ${connectedTo}`,
450
+ newSession.loggingMetadata
418
451
  );
419
452
  return newSession;
420
453
  }
421
- log?.info(
422
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.id}) to ${connectedTo}`
423
- );
424
454
  oldSession.replaceWithNewConnection(conn);
425
455
  oldSession.sendBufferedMessages();
426
456
  oldSession.advertisedSessionId = advertisedSessionId;
457
+ log?.info(
458
+ `new connection for existing session to ${connectedTo}`,
459
+ oldSession.loggingMetadata
460
+ );
427
461
  return oldSession;
428
462
  }
429
463
  createSession(to, conn) {
@@ -445,7 +479,8 @@ var Transport = class {
445
479
  if (!session) {
446
480
  session = this.createSession(to, conn);
447
481
  log?.info(
448
- `${this.clientId} -- no session for ${to}, created a new one (id: ${session.id})`
482
+ `no session for ${to}, created a new one`,
483
+ session.loggingMetadata
449
484
  );
450
485
  }
451
486
  return session;
@@ -454,7 +489,8 @@ var Transport = class {
454
489
  session.close();
455
490
  this.sessions.delete(session.to);
456
491
  log?.info(
457
- `${this.clientId} -- session ${session.id} disconnect from ${session.to}`
492
+ `session ${session.id} disconnect from ${session.to}`,
493
+ session.loggingMetadata
458
494
  );
459
495
  this.eventDispatcher.dispatchEvent("sessionStatus", {
460
496
  status: "disconnect",
@@ -483,17 +519,15 @@ var Transport = class {
483
519
  const parsedMsg = this.codec.fromBuffer(msg);
484
520
  if (parsedMsg === null) {
485
521
  const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));
486
- log?.error(
487
- `${this.clientId} -- received malformed msg, killing conn: ${decodedBuffer}`
488
- );
522
+ log?.error(`received malformed msg, killing conn: ${decodedBuffer}`, {
523
+ clientId: this.clientId
524
+ });
489
525
  return null;
490
526
  }
491
527
  if (!Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
492
- log?.error(
493
- `${this.clientId} -- received invalid msg: ${JSON.stringify(
494
- parsedMsg
495
- )}`
496
- );
528
+ log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
529
+ clientId: this.clientId
530
+ });
497
531
  return null;
498
532
  }
499
533
  return parsedMsg;
@@ -508,26 +542,29 @@ var Transport = class {
508
542
  return;
509
543
  const session = this.sessions.get(msg.from);
510
544
  if (!session) {
511
- const err = `${this.clientId} -- (invariant violation) no existing session for ${msg.from}`;
512
- log?.error(err);
545
+ log?.error(`(invariant violation) no existing session for ${msg.from}`, {
546
+ clientId: this.clientId,
547
+ fullTransportMessage: msg
548
+ });
513
549
  return;
514
550
  }
515
551
  session.cancelGrace();
516
- log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
552
+ log?.debug(`received msg`, {
553
+ clientId: this.clientId,
554
+ fullTransportMessage: msg
555
+ });
517
556
  if (msg.seq !== session.nextExpectedSeq) {
518
557
  if (msg.seq < session.nextExpectedSeq) {
519
558
  log?.debug(
520
- `${this.clientId} -- received duplicate msg (got: ${msg.seq}, wanted: ${session.nextExpectedSeq}), discarding: ${JSON.stringify(
521
- msg
522
- )}`
559
+ `received duplicate msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq}), discarding`,
560
+ { clientId: this.clientId, fullTransportMessage: msg }
523
561
  );
524
562
  } else {
525
563
  const errMsg = `received out-of-order msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq})`;
526
- log?.error(
527
- `${this.clientId} -- fatal: ${errMsg}, marking connection as dead: ${JSON.stringify(
528
- msg
529
- )}`
530
- );
564
+ log?.error(`${errMsg}, marking connection as dead`, {
565
+ clientId: this.clientId,
566
+ fullTransportMessage: msg
567
+ });
531
568
  this.protocolError(ProtocolError.MessageOrderingViolated, errMsg);
532
569
  session.close();
533
570
  }
@@ -537,7 +574,10 @@ var Transport = class {
537
574
  if (!isAck(msg.controlFlags)) {
538
575
  this.eventDispatcher.dispatchEvent("message", msg);
539
576
  } else {
540
- log?.debug(`${this.clientId} -- discarding msg ${msg.id} (ack bit set)`);
577
+ log?.debug(`discarding msg (ack bit set)`, {
578
+ clientId: this.clientId,
579
+ fullTransportMessage: msg
580
+ });
541
581
  }
542
582
  }
543
583
  /**
@@ -565,15 +605,17 @@ var Transport = class {
565
605
  send(to, msg) {
566
606
  if (this.state === "destroyed") {
567
607
  const err = "transport is destroyed, cant send";
568
- log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
608
+ log?.error(err, {
609
+ clientId: this.clientId,
610
+ partialTransportMessage: msg
611
+ });
569
612
  this.protocolError(ProtocolError.UseAfterDestroy, err);
570
613
  return void 0;
571
614
  } else if (this.state === "closed") {
572
- log?.info(
573
- `${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
574
- msg
575
- )}`
576
- );
615
+ log?.info(`transport closed when sending, discarding`, {
616
+ clientId: this.clientId,
617
+ partialTransportMessage: msg
618
+ });
577
619
  return void 0;
578
620
  }
579
621
  return this.getOrCreateSession(to).send(msg);
@@ -601,7 +643,7 @@ var Transport = class {
601
643
  for (const session of this.sessions.values()) {
602
644
  this.deleteSession(session);
603
645
  }
604
- log?.info(`${this.clientId} -- manually closed transport`);
646
+ log?.info(`manually closed transport`, { clientId: this.clientId });
605
647
  }
606
648
  /**
607
649
  * Default destroy implementation for transports. You should override this in the downstream
@@ -613,7 +655,7 @@ var Transport = class {
613
655
  for (const session of this.sessions.values()) {
614
656
  this.deleteSession(session);
615
657
  }
616
- log?.info(`${this.clientId} -- manually destroyed transport`);
658
+ log?.info(`manually destroyed transport`, { clientId: this.clientId });
617
659
  }
618
660
  };
619
661
  var ClientTransport = class extends Transport {
@@ -646,6 +688,15 @@ var ClientTransport = class extends Transport {
646
688
  if (this.state !== "open")
647
689
  return;
648
690
  let session = void 0;
691
+ const handshakeTimeout = setTimeout(() => {
692
+ if (!session) {
693
+ log?.warn(
694
+ `connection to ${to} timed out waiting for handshake, closing`,
695
+ { clientId: this.clientId, connectedTo: to, connId: conn.debugId }
696
+ );
697
+ conn.close();
698
+ }
699
+ }, this.options.sessionDisconnectGraceMs);
649
700
  const handshakeHandler = (data) => {
650
701
  const maybeSession = this.receiveHandshakeResponseMessage(data, conn);
651
702
  if (!maybeSession) {
@@ -653,6 +704,7 @@ var ClientTransport = class extends Transport {
653
704
  return;
654
705
  } else {
655
706
  session = maybeSession;
707
+ clearTimeout(handshakeTimeout);
656
708
  }
657
709
  conn.removeDataListener(handshakeHandler);
658
710
  conn.addDataListener((data2) => {
@@ -669,18 +721,22 @@ var ClientTransport = class extends Transport {
669
721
  if (session) {
670
722
  this.onDisconnect(conn, session);
671
723
  }
672
- log?.info(
673
- `${this.clientId} -- connection (id: ${conn.debugId}) to ${to} disconnected`
674
- );
724
+ log?.info(`connection to ${to} disconnected`, {
725
+ ...session?.loggingMetadata,
726
+ clientId: this.clientId,
727
+ connectedTo: to
728
+ });
675
729
  this.inflightConnectionPromises.delete(to);
676
730
  if (this.reconnectOnConnectionDrop) {
677
731
  void this.connect(to);
678
732
  }
679
733
  });
680
734
  conn.addErrorListener((err) => {
681
- log?.warn(
682
- `${this.clientId} -- error in connection (id: ${conn.debugId}) to ${to}: ${coerceErrorString(err)}`
683
- );
735
+ log?.warn(`error in connection to ${to}: ${coerceErrorString(err)}`, {
736
+ ...session?.loggingMetadata,
737
+ clientId: this.clientId,
738
+ connectedTo: to
739
+ });
684
740
  });
685
741
  }
686
742
  receiveHandshakeResponseMessage(data, conn) {
@@ -693,11 +749,11 @@ var ClientTransport = class extends Transport {
693
749
  return false;
694
750
  }
695
751
  if (!Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
696
- log?.warn(
697
- `${this.clientId} -- received invalid handshake resp: ${JSON.stringify(
698
- parsed
699
- )}`
700
- );
752
+ log?.warn(`received invalid handshake resp`, {
753
+ clientId: this.clientId,
754
+ connectedTo: parsed.from,
755
+ fullTransportMessage: parsed
756
+ });
701
757
  this.protocolError(
702
758
  ProtocolError.HandshakeFailed,
703
759
  "invalid handshake resp"
@@ -705,18 +761,22 @@ var ClientTransport = class extends Transport {
705
761
  return false;
706
762
  }
707
763
  if (!parsed.payload.status.ok) {
708
- log?.warn(
709
- `${this.clientId} -- received failed handshake resp: ${JSON.stringify(
710
- parsed
711
- )}`
712
- );
764
+ log?.warn(`received invalid handshake resp`, {
765
+ clientId: this.clientId,
766
+ connectedTo: parsed.from,
767
+ fullTransportMessage: parsed
768
+ });
713
769
  this.protocolError(
714
770
  ProtocolError.HandshakeFailed,
715
771
  parsed.payload.status.reason
716
772
  );
717
773
  return false;
718
774
  }
719
- log?.debug(`${this.clientId} -- handshake from ${parsed.from} ok`);
775
+ log?.debug(`handshake from ${parsed.from} ok`, {
776
+ clientId: this.clientId,
777
+ connectedTo: parsed.from,
778
+ fullTransportMessage: parsed
779
+ });
720
780
  const session = this.onConnect(
721
781
  conn,
722
782
  parsed.from,
@@ -733,7 +793,8 @@ var ClientTransport = class extends Transport {
733
793
  const canProceedWithConnection = () => this.state === "open";
734
794
  if (!canProceedWithConnection()) {
735
795
  log?.info(
736
- `${this.clientId} -- transport state is no longer open, cancelling attempt to connect to ${to}`
796
+ `transport state is no longer open, cancelling attempt to connect to ${to}`,
797
+ { clientId: this.clientId, connectedTo: to }
737
798
  );
738
799
  return;
739
800
  }
@@ -742,7 +803,7 @@ var ClientTransport = class extends Transport {
742
803
  const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
743
804
  if (!this.retryBudget.hasBudget(to)) {
744
805
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
745
- log?.warn(`${this.clientId} -- ${errMsg}`);
806
+ log?.warn(errMsg, { clientId: this.clientId, connectedTo: to });
746
807
  this.protocolError(ProtocolError.RetriesExceeded, errMsg);
747
808
  return;
748
809
  }
@@ -751,9 +812,10 @@ var ClientTransport = class extends Transport {
751
812
  if (backoffMs > 0) {
752
813
  sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
753
814
  }
754
- log?.info(
755
- `${this.clientId} -- attempting connection to ${to} (${backoffMs}ms backoff)`
756
- );
815
+ log?.info(`attempting connection to ${to} (${backoffMs}ms backoff)`, {
816
+ clientId: this.clientId,
817
+ connectedTo: to
818
+ });
757
819
  this.retryBudget.consumeBudget(to);
758
820
  reconnectPromise = sleep.then(() => {
759
821
  if (!canProceedWithConnection()) {
@@ -762,7 +824,12 @@ var ClientTransport = class extends Transport {
762
824
  }).then(() => this.createNewOutgoingConnection(to)).then((conn) => {
763
825
  if (!canProceedWithConnection()) {
764
826
  log?.info(
765
- `${this.clientId} -- transport state is no longer open, closing pre-handshake connection (id: ${conn.debugId}) to ${to}`
827
+ `transport state is no longer open, closing pre-handshake connection to ${to}`,
828
+ {
829
+ clientId: this.clientId,
830
+ connectedTo: to,
831
+ connId: conn.debugId
832
+ }
766
833
  );
767
834
  conn.close();
768
835
  throw new Error("transport state is no longer open");
@@ -772,9 +839,10 @@ var ClientTransport = class extends Transport {
772
839
  });
773
840
  this.inflightConnectionPromises.set(to, reconnectPromise);
774
841
  } else {
775
- log?.info(
776
- `${this.clientId} -- attempting connection to ${to} (reusing previous attempt)`
777
- );
842
+ log?.info(`attempting connection to ${to} (reusing previous attempt)`, {
843
+ clientId: this.clientId,
844
+ connectedTo: to
845
+ });
778
846
  }
779
847
  try {
780
848
  await reconnectPromise;
@@ -782,11 +850,15 @@ var ClientTransport = class extends Transport {
782
850
  this.inflightConnectionPromises.delete(to);
783
851
  const errStr = coerceErrorString(error);
784
852
  if (!this.reconnectOnConnectionDrop || !canProceedWithConnection()) {
785
- log?.warn(`${this.clientId} -- connection to ${to} failed (${errStr})`);
853
+ log?.warn(`connection to ${to} failed (${errStr})`, {
854
+ clientId: this.clientId,
855
+ connectedTo: to
856
+ });
786
857
  } else {
787
- log?.warn(
788
- `${this.clientId} -- connection to ${to} failed (${errStr}), retrying`
789
- );
858
+ log?.warn(`connection to ${to} failed (${errStr}), retrying`, {
859
+ clientId: this.clientId,
860
+ connectedTo: to
861
+ });
790
862
  return this.connect(to);
791
863
  }
792
864
  }
@@ -798,7 +870,10 @@ var ClientTransport = class extends Transport {
798
870
  sendHandshake(to, conn) {
799
871
  const session = this.getOrCreateSession(to, conn);
800
872
  const requestMsg = handshakeRequestMessage(this.clientId, to, session.id);
801
- log?.debug(`${this.clientId} -- sending handshake request to ${to}`);
873
+ log?.debug(`sending handshake request to ${to}`, {
874
+ clientId: this.clientId,
875
+ connectedTo: to
876
+ });
802
877
  conn.send(this.codec.toBuffer(requestMsg));
803
878
  }
804
879
  close() {
@@ -809,18 +884,33 @@ var ClientTransport = class extends Transport {
809
884
  var ServerTransport = class extends Transport {
810
885
  constructor(clientId, providedOptions) {
811
886
  super(clientId, providedOptions);
812
- log?.info(
813
- `${this.clientId} -- initiated server transport (protocol: ${PROTOCOL_VERSION})`
814
- );
887
+ log?.info(`initiated server transport`, {
888
+ clientId: this.clientId,
889
+ protocolVersion: PROTOCOL_VERSION
890
+ });
815
891
  }
816
892
  handleConnection(conn) {
817
893
  if (this.state !== "open")
818
894
  return;
819
- log?.info(
820
- `${this.clientId} -- new incoming connection (id: ${conn.debugId})`
821
- );
895
+ log?.info(`new incoming connection`, {
896
+ clientId: this.clientId,
897
+ connId: conn.debugId
898
+ });
822
899
  let session = void 0;
823
900
  const client = () => session?.to ?? "unknown";
901
+ const handshakeTimeout = setTimeout(() => {
902
+ if (!session) {
903
+ log?.warn(
904
+ `connection to ${client()} timed out waiting for handshake, closing`,
905
+ {
906
+ clientId: this.clientId,
907
+ connectedTo: client(),
908
+ connId: conn.debugId
909
+ }
910
+ );
911
+ conn.close();
912
+ }
913
+ }, this.options.sessionDisconnectGraceMs);
824
914
  const handshakeHandler = (data) => {
825
915
  const maybeSession = this.receiveHandshakeRequestMessage(data, conn);
826
916
  if (!maybeSession) {
@@ -828,6 +918,7 @@ var ServerTransport = class extends Transport {
828
918
  return;
829
919
  } else {
830
920
  session = maybeSession;
921
+ clearTimeout(handshakeTimeout);
831
922
  }
832
923
  conn.removeDataListener(handshakeHandler);
833
924
  conn.addDataListener((data2) => {
@@ -843,16 +934,18 @@ var ServerTransport = class extends Transport {
843
934
  conn.addCloseListener(() => {
844
935
  if (!session)
845
936
  return;
846
- log?.info(
847
- `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
848
- );
937
+ log?.info(`connection to ${client()} disconnected`, {
938
+ clientId: this.clientId,
939
+ connId: conn.debugId
940
+ });
849
941
  this.onDisconnect(conn, session);
850
942
  });
851
943
  conn.addErrorListener((err) => {
852
944
  if (!session)
853
945
  return;
854
946
  log?.warn(
855
- `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} got an error: ${coerceErrorString(err)}`
947
+ `connection to ${client()} got an error: ${coerceErrorString(err)}`,
948
+ { clientId: this.clientId, connId: conn.debugId }
856
949
  );
857
950
  });
858
951
  }
@@ -872,7 +965,10 @@ var ServerTransport = class extends Transport {
872
965
  reason
873
966
  });
874
967
  conn.send(this.codec.toBuffer(responseMsg2));
875
- log?.warn(`${this.clientId} -- ${reason}: ${JSON.stringify(parsed)}`);
968
+ log?.warn(`${reason}: ${JSON.stringify(parsed)}`, {
969
+ clientId: this.clientId,
970
+ connId: conn.debugId
971
+ });
876
972
  this.protocolError(
877
973
  ProtocolError.HandshakeFailed,
878
974
  "invalid handshake request"
@@ -888,14 +984,16 @@ var ServerTransport = class extends Transport {
888
984
  });
889
985
  conn.send(this.codec.toBuffer(responseMsg2));
890
986
  log?.warn(
891
- `${this.clientId} -- received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`
987
+ `received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`,
988
+ { clientId: this.clientId, connId: conn.debugId }
892
989
  );
893
990
  this.protocolError(ProtocolError.HandshakeFailed, reason);
894
991
  return false;
895
992
  }
896
993
  const session = this.getOrCreateSession(parsed.from, conn);
897
994
  log?.debug(
898
- `${this.clientId} -- handshake from ${parsed.from} ok, responding with handshake success`
995
+ `handshake from ${parsed.from} ok, responding with handshake success`,
996
+ { clientId: this.clientId, connId: conn.debugId }
899
997
  );
900
998
  const responseMsg = handshakeResponseMessage(this.clientId, parsed.from, {
901
999
  ok: true,
@@ -0,0 +1,72 @@
1
+ // logging/log.ts
2
+ var LoggingLevels = {
3
+ debug: -1,
4
+ info: 0,
5
+ warn: 1,
6
+ error: 2
7
+ };
8
+ var BaseLogger = class {
9
+ minLevel;
10
+ output;
11
+ constructor(output, minLevel = "info") {
12
+ this.minLevel = minLevel;
13
+ this.output = output;
14
+ }
15
+ debug(msg, metadata) {
16
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.debug) {
17
+ this.output(msg, metadata ?? {}, "debug");
18
+ }
19
+ }
20
+ info(msg, metadata) {
21
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.info) {
22
+ this.output(msg, metadata ?? {}, "info");
23
+ }
24
+ }
25
+ warn(msg, metadata) {
26
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.warn) {
27
+ this.output(msg, metadata ?? {}, "warn");
28
+ }
29
+ }
30
+ error(msg, metadata) {
31
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.error) {
32
+ this.output(msg, metadata ?? {}, "error");
33
+ }
34
+ }
35
+ };
36
+ var stringLogger = (msg, _ctx, level) => {
37
+ console.log(`[river:${level}] ${msg}`);
38
+ };
39
+ var colorMap = {
40
+ debug: "\x1B[34m",
41
+ info: "\x1B[32m",
42
+ warn: "\x1B[33m",
43
+ error: "\x1B[31m"
44
+ };
45
+ var coloredStringLogger = (msg, _ctx, level) => {
46
+ const color = colorMap[level ?? "info"];
47
+ console.log(`[river:${color}${level}\x1B[0m] ${msg}`);
48
+ };
49
+ var jsonLogger = (msg, ctx, level) => {
50
+ console.log(JSON.stringify({ msg, ctx, level }));
51
+ };
52
+ var log = void 0;
53
+ function bindLogger(fn, level) {
54
+ if (!fn) {
55
+ log = void 0;
56
+ return;
57
+ }
58
+ if (fn instanceof Function) {
59
+ log = new BaseLogger(fn, level);
60
+ return log;
61
+ }
62
+ log = fn;
63
+ return fn;
64
+ }
65
+
66
+ export {
67
+ stringLogger,
68
+ coloredStringLogger,
69
+ jsonLogger,
70
+ log,
71
+ bindLogger
72
+ };
@@ -1,5 +1,5 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { C as Connection } from './index-9e300e8a.js';
2
+ import { C as Connection } from './index-d412ca83.js';
3
3
 
4
4
  declare class WebSocketConnection extends Connection {
5
5
  ws: WebSocket;
@@ -1,4 +1,4 @@
1
- import { C as Connection } from './index-9e300e8a.js';
1
+ import { C as Connection } from './index-d412ca83.js';
2
2
  import { Socket } from 'node:net';
3
3
  import stream, { Transform, TransformCallback, TransformOptions } from 'node:stream';
4
4