@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
@@ -95,8 +95,8 @@ function isAck(controlFlag) {
95
95
  return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
96
96
  }
97
97
 
98
- // logging/index.ts
99
- var log;
98
+ // logging/log.ts
99
+ var log = void 0;
100
100
 
101
101
  // transport/events.ts
102
102
  var ProtocolError = {
@@ -198,6 +198,14 @@ var Session = class {
198
198
  options.heartbeatIntervalMs
199
199
  );
200
200
  }
201
+ get loggingMetadata() {
202
+ return {
203
+ clientId: this.from,
204
+ connectedTo: this.to,
205
+ sessionId: this.id,
206
+ connId: this.connection?.debugId
207
+ };
208
+ }
201
209
  /**
202
210
  * Sends a message over the session's connection.
203
211
  * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
@@ -208,26 +216,37 @@ var Session = class {
208
216
  */
209
217
  send(msg) {
210
218
  const fullMsg = this.constructMsg(msg);
211
- log?.debug(`${this.from} -- sending ${JSON.stringify(fullMsg)}`);
219
+ log?.debug(`sending msg`, {
220
+ ...this.loggingMetadata,
221
+ fullTransportMessage: fullMsg
222
+ });
212
223
  if (this.connection) {
213
224
  const ok = this.connection.send(this.codec.toBuffer(fullMsg));
214
225
  if (ok)
215
226
  return fullMsg.id;
216
227
  log?.info(
217
- `${this.from} -- failed to send ${fullMsg.id} (seq: ${fullMsg.seq}) to ${fullMsg.to}, connection (id: ${this.connection.debugId}) is probably dead`
228
+ `failed to send msg to ${fullMsg.to}, connection is probably dead`,
229
+ {
230
+ ...this.loggingMetadata,
231
+ fullTransportMessage: fullMsg
232
+ }
218
233
  );
219
234
  } else {
220
235
  log?.info(
221
- `${this.from} -- failed to send ${fullMsg.id} (seq: ${fullMsg.seq}) to ${fullMsg.to}, connection not ready yet`
236
+ `failed to send msg to ${fullMsg.to}, connection not ready yet`,
237
+ { ...this.loggingMetadata, fullTransportMessage: fullMsg }
222
238
  );
223
239
  }
224
240
  return fullMsg.id;
225
241
  }
226
242
  sendHeartbeat() {
227
- if (this.heartbeatMisses >= this.options.heartbeatsUntilDead) {
243
+ const misses = this.heartbeatMisses;
244
+ const missDuration = misses * this.options.heartbeatIntervalMs;
245
+ if (misses > this.options.heartbeatsUntilDead) {
228
246
  if (this.connection) {
229
247
  log?.info(
230
- `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
248
+ `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
249
+ this.loggingMetadata
231
250
  );
232
251
  this.closeStaleConnection();
233
252
  }
@@ -249,26 +268,36 @@ var Session = class {
249
268
  }
250
269
  sendBufferedMessages() {
251
270
  if (!this.connection) {
252
- const msg = `${this.from} -- tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
253
- log?.error(msg);
271
+ const msg = `tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
272
+ log?.error(msg, this.loggingMetadata);
254
273
  throw new Error(msg);
255
274
  }
256
275
  log?.info(
257
- `${this.from} -- resending ${this.sendBuffer.length} buffered messages`
276
+ `resending ${this.sendBuffer.length} buffered messages`,
277
+ this.loggingMetadata
258
278
  );
259
279
  for (const msg of this.sendBuffer) {
260
- log?.debug(`${this.from} -- resending ${msg.id} (seq: ${msg.seq})`);
280
+ log?.debug(`resending msg`, {
281
+ ...this.loggingMetadata,
282
+ fullTransportMessage: msg
283
+ });
261
284
  const ok = this.connection.send(this.codec.toBuffer(msg));
262
285
  if (!ok) {
263
- 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)`;
264
- log?.error(msg2);
265
- throw new Error(msg2);
286
+ const errMsg = `failed to send buffered message to ${this.to} (if you hit this code path something is seriously wrong)`;
287
+ log?.error(errMsg, {
288
+ ...this.loggingMetadata,
289
+ fullTransportMessage: msg
290
+ });
291
+ throw new Error(errMsg);
266
292
  }
267
293
  }
268
294
  }
269
295
  updateBookkeeping(ack, seq) {
270
296
  if (seq + 1 < this.ack) {
271
- log?.error(`${this.from} -- received stale seq ${seq} + 1 < ${this.ack}`);
297
+ log?.error(
298
+ `received stale seq ${seq} + 1 < ${this.ack}`,
299
+ this.loggingMetadata
300
+ );
272
301
  return;
273
302
  }
274
303
  this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
@@ -278,7 +307,8 @@ var Session = class {
278
307
  if (this.connection === void 0 || this.connection === conn)
279
308
  return;
280
309
  log?.info(
281
- `${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.id}) to ${this.to}`
310
+ `closing old inner connection from session to ${this.to}`,
311
+ this.loggingMetadata
282
312
  );
283
313
  this.connection.close();
284
314
  this.connection = void 0;
@@ -290,7 +320,8 @@ var Session = class {
290
320
  }
291
321
  beginGrace(cb) {
292
322
  log?.info(
293
- `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.id}) to ${this.to} is closed`
323
+ `starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
324
+ this.loggingMetadata
294
325
  );
295
326
  this.disconnectionGrace = setTimeout(() => {
296
327
  this.close();
@@ -474,7 +505,8 @@ var Transport = class {
474
505
  let oldSession = this.sessions.get(connectedTo);
475
506
  if (oldSession?.advertisedSessionId && oldSession.advertisedSessionId !== advertisedSessionId) {
476
507
  log?.warn(
477
- `${this.clientId} -- connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), starting a new session`
508
+ `connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), killing old session and starting a new one`,
509
+ oldSession.loggingMetadata
478
510
  );
479
511
  this.deleteSession(oldSession);
480
512
  oldSession = void 0;
@@ -483,16 +515,18 @@ var Transport = class {
483
515
  const newSession = this.createSession(connectedTo, conn);
484
516
  newSession.advertisedSessionId = advertisedSessionId;
485
517
  log?.info(
486
- `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.id}) to ${connectedTo}`
518
+ `new connection for new session to ${connectedTo}`,
519
+ newSession.loggingMetadata
487
520
  );
488
521
  return newSession;
489
522
  }
490
- log?.info(
491
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.id}) to ${connectedTo}`
492
- );
493
523
  oldSession.replaceWithNewConnection(conn);
494
524
  oldSession.sendBufferedMessages();
495
525
  oldSession.advertisedSessionId = advertisedSessionId;
526
+ log?.info(
527
+ `new connection for existing session to ${connectedTo}`,
528
+ oldSession.loggingMetadata
529
+ );
496
530
  return oldSession;
497
531
  }
498
532
  createSession(to, conn) {
@@ -514,7 +548,8 @@ var Transport = class {
514
548
  if (!session) {
515
549
  session = this.createSession(to, conn);
516
550
  log?.info(
517
- `${this.clientId} -- no session for ${to}, created a new one (id: ${session.id})`
551
+ `no session for ${to}, created a new one`,
552
+ session.loggingMetadata
518
553
  );
519
554
  }
520
555
  return session;
@@ -523,7 +558,8 @@ var Transport = class {
523
558
  session.close();
524
559
  this.sessions.delete(session.to);
525
560
  log?.info(
526
- `${this.clientId} -- session ${session.id} disconnect from ${session.to}`
561
+ `session ${session.id} disconnect from ${session.to}`,
562
+ session.loggingMetadata
527
563
  );
528
564
  this.eventDispatcher.dispatchEvent("sessionStatus", {
529
565
  status: "disconnect",
@@ -552,17 +588,15 @@ var Transport = class {
552
588
  const parsedMsg = this.codec.fromBuffer(msg);
553
589
  if (parsedMsg === null) {
554
590
  const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));
555
- log?.error(
556
- `${this.clientId} -- received malformed msg, killing conn: ${decodedBuffer}`
557
- );
591
+ log?.error(`received malformed msg, killing conn: ${decodedBuffer}`, {
592
+ clientId: this.clientId
593
+ });
558
594
  return null;
559
595
  }
560
596
  if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
561
- log?.error(
562
- `${this.clientId} -- received invalid msg: ${JSON.stringify(
563
- parsedMsg
564
- )}`
565
- );
597
+ log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
598
+ clientId: this.clientId
599
+ });
566
600
  return null;
567
601
  }
568
602
  return parsedMsg;
@@ -577,26 +611,29 @@ var Transport = class {
577
611
  return;
578
612
  const session = this.sessions.get(msg.from);
579
613
  if (!session) {
580
- const err = `${this.clientId} -- (invariant violation) no existing session for ${msg.from}`;
581
- log?.error(err);
614
+ log?.error(`(invariant violation) no existing session for ${msg.from}`, {
615
+ clientId: this.clientId,
616
+ fullTransportMessage: msg
617
+ });
582
618
  return;
583
619
  }
584
620
  session.cancelGrace();
585
- log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
621
+ log?.debug(`received msg`, {
622
+ clientId: this.clientId,
623
+ fullTransportMessage: msg
624
+ });
586
625
  if (msg.seq !== session.nextExpectedSeq) {
587
626
  if (msg.seq < session.nextExpectedSeq) {
588
627
  log?.debug(
589
- `${this.clientId} -- received duplicate msg (got: ${msg.seq}, wanted: ${session.nextExpectedSeq}), discarding: ${JSON.stringify(
590
- msg
591
- )}`
628
+ `received duplicate msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq}), discarding`,
629
+ { clientId: this.clientId, fullTransportMessage: msg }
592
630
  );
593
631
  } else {
594
632
  const errMsg = `received out-of-order msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq})`;
595
- log?.error(
596
- `${this.clientId} -- fatal: ${errMsg}, marking connection as dead: ${JSON.stringify(
597
- msg
598
- )}`
599
- );
633
+ log?.error(`${errMsg}, marking connection as dead`, {
634
+ clientId: this.clientId,
635
+ fullTransportMessage: msg
636
+ });
600
637
  this.protocolError(ProtocolError.MessageOrderingViolated, errMsg);
601
638
  session.close();
602
639
  }
@@ -606,7 +643,10 @@ var Transport = class {
606
643
  if (!isAck(msg.controlFlags)) {
607
644
  this.eventDispatcher.dispatchEvent("message", msg);
608
645
  } else {
609
- log?.debug(`${this.clientId} -- discarding msg ${msg.id} (ack bit set)`);
646
+ log?.debug(`discarding msg (ack bit set)`, {
647
+ clientId: this.clientId,
648
+ fullTransportMessage: msg
649
+ });
610
650
  }
611
651
  }
612
652
  /**
@@ -634,15 +674,17 @@ var Transport = class {
634
674
  send(to, msg) {
635
675
  if (this.state === "destroyed") {
636
676
  const err = "transport is destroyed, cant send";
637
- log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
677
+ log?.error(err, {
678
+ clientId: this.clientId,
679
+ partialTransportMessage: msg
680
+ });
638
681
  this.protocolError(ProtocolError.UseAfterDestroy, err);
639
682
  return void 0;
640
683
  } else if (this.state === "closed") {
641
- log?.info(
642
- `${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
643
- msg
644
- )}`
645
- );
684
+ log?.info(`transport closed when sending, discarding`, {
685
+ clientId: this.clientId,
686
+ partialTransportMessage: msg
687
+ });
646
688
  return void 0;
647
689
  }
648
690
  return this.getOrCreateSession(to).send(msg);
@@ -670,7 +712,7 @@ var Transport = class {
670
712
  for (const session of this.sessions.values()) {
671
713
  this.deleteSession(session);
672
714
  }
673
- log?.info(`${this.clientId} -- manually closed transport`);
715
+ log?.info(`manually closed transport`, { clientId: this.clientId });
674
716
  }
675
717
  /**
676
718
  * Default destroy implementation for transports. You should override this in the downstream
@@ -682,24 +724,39 @@ var Transport = class {
682
724
  for (const session of this.sessions.values()) {
683
725
  this.deleteSession(session);
684
726
  }
685
- log?.info(`${this.clientId} -- manually destroyed transport`);
727
+ log?.info(`manually destroyed transport`, { clientId: this.clientId });
686
728
  }
687
729
  };
688
730
  var ServerTransport = class extends Transport {
689
731
  constructor(clientId, providedOptions) {
690
732
  super(clientId, providedOptions);
691
- log?.info(
692
- `${this.clientId} -- initiated server transport (protocol: ${PROTOCOL_VERSION})`
693
- );
733
+ log?.info(`initiated server transport`, {
734
+ clientId: this.clientId,
735
+ protocolVersion: PROTOCOL_VERSION
736
+ });
694
737
  }
695
738
  handleConnection(conn) {
696
739
  if (this.state !== "open")
697
740
  return;
698
- log?.info(
699
- `${this.clientId} -- new incoming connection (id: ${conn.debugId})`
700
- );
741
+ log?.info(`new incoming connection`, {
742
+ clientId: this.clientId,
743
+ connId: conn.debugId
744
+ });
701
745
  let session = void 0;
702
746
  const client = () => session?.to ?? "unknown";
747
+ const handshakeTimeout = setTimeout(() => {
748
+ if (!session) {
749
+ log?.warn(
750
+ `connection to ${client()} timed out waiting for handshake, closing`,
751
+ {
752
+ clientId: this.clientId,
753
+ connectedTo: client(),
754
+ connId: conn.debugId
755
+ }
756
+ );
757
+ conn.close();
758
+ }
759
+ }, this.options.sessionDisconnectGraceMs);
703
760
  const handshakeHandler = (data) => {
704
761
  const maybeSession = this.receiveHandshakeRequestMessage(data, conn);
705
762
  if (!maybeSession) {
@@ -707,6 +764,7 @@ var ServerTransport = class extends Transport {
707
764
  return;
708
765
  } else {
709
766
  session = maybeSession;
767
+ clearTimeout(handshakeTimeout);
710
768
  }
711
769
  conn.removeDataListener(handshakeHandler);
712
770
  conn.addDataListener((data2) => {
@@ -722,16 +780,18 @@ var ServerTransport = class extends Transport {
722
780
  conn.addCloseListener(() => {
723
781
  if (!session)
724
782
  return;
725
- log?.info(
726
- `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
727
- );
783
+ log?.info(`connection to ${client()} disconnected`, {
784
+ clientId: this.clientId,
785
+ connId: conn.debugId
786
+ });
728
787
  this.onDisconnect(conn, session);
729
788
  });
730
789
  conn.addErrorListener((err) => {
731
790
  if (!session)
732
791
  return;
733
792
  log?.warn(
734
- `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} got an error: ${coerceErrorString(err)}`
793
+ `connection to ${client()} got an error: ${coerceErrorString(err)}`,
794
+ { clientId: this.clientId, connId: conn.debugId }
735
795
  );
736
796
  });
737
797
  }
@@ -751,7 +811,10 @@ var ServerTransport = class extends Transport {
751
811
  reason
752
812
  });
753
813
  conn.send(this.codec.toBuffer(responseMsg2));
754
- log?.warn(`${this.clientId} -- ${reason}: ${JSON.stringify(parsed)}`);
814
+ log?.warn(`${reason}: ${JSON.stringify(parsed)}`, {
815
+ clientId: this.clientId,
816
+ connId: conn.debugId
817
+ });
755
818
  this.protocolError(
756
819
  ProtocolError.HandshakeFailed,
757
820
  "invalid handshake request"
@@ -767,14 +830,16 @@ var ServerTransport = class extends Transport {
767
830
  });
768
831
  conn.send(this.codec.toBuffer(responseMsg2));
769
832
  log?.warn(
770
- `${this.clientId} -- received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`
833
+ `received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`,
834
+ { clientId: this.clientId, connId: conn.debugId }
771
835
  );
772
836
  this.protocolError(ProtocolError.HandshakeFailed, reason);
773
837
  return false;
774
838
  }
775
839
  const session = this.getOrCreateSession(parsed.from, conn);
776
840
  log?.debug(
777
- `${this.clientId} -- handshake from ${parsed.from} ok, responding with handshake success`
841
+ `handshake from ${parsed.from} ok, responding with handshake success`,
842
+ { clientId: this.clientId, connId: conn.debugId }
778
843
  );
779
844
  const responseMsg = handshakeResponseMessage(this.clientId, parsed.from, {
780
845
  ok: true,
@@ -1,6 +1,7 @@
1
1
  import { Server, Socket } from 'node:net';
2
- import { e as ServerTransport, c as TransportClientId, f as ProvidedTransportOptions } from '../../../index-9e300e8a.js';
3
- import { U as UdsConnection } from '../../../connection-bdbd20da.js';
2
+ import { c as ServerTransport, d as ProvidedTransportOptions } from '../../../index-d412ca83.js';
3
+ import { T as TransportClientId } from '../../../index-46ed19d8.js';
4
+ import { U as UdsConnection } from '../../../connection-89918b74.js';
4
5
  import '../../../types-3e5768ec.js';
5
6
  import '@sinclair/typebox';
6
7
  import 'node:stream';
@@ -1,6 +1,7 @@
1
1
  import { Server, Socket } from 'node:net';
2
- import { e as ServerTransport, c as TransportClientId, f as ProvidedTransportOptions } from '../../../index-9e300e8a.js';
3
- import { U as UdsConnection } from '../../../connection-bdbd20da.js';
2
+ import { c as ServerTransport, d as ProvidedTransportOptions } from '../../../index-d412ca83.js';
3
+ import { T as TransportClientId } from '../../../index-46ed19d8.js';
4
+ import { U as UdsConnection } from '../../../connection-89918b74.js';
4
5
  import '../../../types-3e5768ec.js';
5
6
  import '@sinclair/typebox';
6
7
  import 'node:stream';
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  UdsConnection
3
- } from "../../../chunk-Q7OSVPZ5.js";
3
+ } from "../../../chunk-O36533OC.js";
4
4
  import {
5
5
  ServerTransport
6
- } from "../../../chunk-7WY3Z5ZN.js";
6
+ } from "../../../chunk-UNTGVPI6.js";
7
7
  import "../../../chunk-VH3NGOXQ.js";
8
- import "../../../chunk-H4BYJELI.js";
8
+ import "../../../chunk-XCQF55SQ.js";
9
9
  import "../../../chunk-GZ7HCLLM.js";
10
10
 
11
11
  // transport/impls/uds/server.ts