@replit/river 0.13.1 → 0.13.3

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 (38) hide show
  1. package/dist/{builder-eef3b061.d.ts → builder-d649690f.d.ts} +1 -1
  2. package/dist/{chunk-Q7AWJYDQ.js → chunk-5V6B7TM3.js} +1 -1
  3. package/dist/{chunk-JXKTY3GQ.js → chunk-67M47YPQ.js} +1 -1
  4. package/dist/{chunk-JXO2SCQB.js → chunk-FIX2F6VT.js} +1 -1
  5. package/dist/{chunk-SCG5S2EC.js → chunk-GFRAOY75.js} +1 -1
  6. package/dist/{chunk-LDUFHGZU.js → chunk-MCTB3NYK.js} +17 -11
  7. package/dist/{connection-d052d027.d.ts → connection-2b55cc7f.d.ts} +1 -1
  8. package/dist/{connection-03e650c8.d.ts → connection-6222fd0b.d.ts} +1 -1
  9. package/dist/{index-9aa0aabb.d.ts → index-1c1493df.d.ts} +2 -2
  10. package/dist/router/index.cjs +1 -1
  11. package/dist/router/index.d.cts +3 -3
  12. package/dist/router/index.d.ts +3 -3
  13. package/dist/router/index.js +2 -2
  14. package/dist/transport/impls/uds/client.cjs +16 -10
  15. package/dist/transport/impls/uds/client.d.cts +2 -2
  16. package/dist/transport/impls/uds/client.d.ts +2 -2
  17. package/dist/transport/impls/uds/client.js +3 -3
  18. package/dist/transport/impls/uds/server.cjs +6 -6
  19. package/dist/transport/impls/uds/server.d.cts +2 -2
  20. package/dist/transport/impls/uds/server.d.ts +2 -2
  21. package/dist/transport/impls/uds/server.js +3 -3
  22. package/dist/transport/impls/ws/client.cjs +16 -10
  23. package/dist/transport/impls/ws/client.d.cts +2 -2
  24. package/dist/transport/impls/ws/client.d.ts +2 -2
  25. package/dist/transport/impls/ws/client.js +3 -3
  26. package/dist/transport/impls/ws/server.cjs +6 -6
  27. package/dist/transport/impls/ws/server.d.cts +2 -2
  28. package/dist/transport/impls/ws/server.d.ts +2 -2
  29. package/dist/transport/impls/ws/server.js +3 -3
  30. package/dist/transport/index.cjs +17 -11
  31. package/dist/transport/index.d.cts +1 -1
  32. package/dist/transport/index.d.ts +1 -1
  33. package/dist/transport/index.js +2 -2
  34. package/dist/util/testHelpers.cjs +4 -1
  35. package/dist/util/testHelpers.d.cts +2 -2
  36. package/dist/util/testHelpers.d.ts +2 -2
  37. package/dist/util/testHelpers.js +3 -3
  38. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  import { TObject, TUnion, TString, TSchema, TNever, TLiteral, Static } from '@sinclair/typebox';
2
2
  import { Pushable } from 'it-pushable';
3
- import { b as TransportClientId, d as Session, C as Connection } from './index-9aa0aabb.js';
3
+ import { b as TransportClientId, d as Session, C as Connection } from './index-1c1493df.js';
4
4
 
5
5
  /**
6
6
  * The context for services/procedures. This is used only on
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Connection
3
- } from "./chunk-LDUFHGZU.js";
3
+ } from "./chunk-MCTB3NYK.js";
4
4
 
5
5
  // transport/impls/ws/connection.ts
6
6
  var WebSocketConnection = class extends Connection {
@@ -3,7 +3,7 @@ import {
3
3
  coerceErrorString,
4
4
  isStreamClose,
5
5
  isStreamOpen
6
- } from "./chunk-SCG5S2EC.js";
6
+ } from "./chunk-GFRAOY75.js";
7
7
  import {
8
8
  log
9
9
  } from "./chunk-H4BYJELI.js";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Connection
3
- } from "./chunk-LDUFHGZU.js";
3
+ } from "./chunk-MCTB3NYK.js";
4
4
 
5
5
  // transport/transforms/messageFraming.ts
6
6
  import { Transform } from "node:stream";
@@ -106,7 +106,7 @@ function isStreamClose(controlFlag) {
106
106
  // util/stringify.ts
107
107
  function coerceErrorString(err) {
108
108
  if (err instanceof Error) {
109
- return err.message;
109
+ return err.message || "unknown reason";
110
110
  }
111
111
  return `[coerced to error] ${String(err)}`;
112
112
  }
@@ -7,7 +7,7 @@ import {
7
7
  handshakeRequestMessage,
8
8
  handshakeResponseMessage,
9
9
  isAck
10
- } from "./chunk-SCG5S2EC.js";
10
+ } from "./chunk-GFRAOY75.js";
11
11
  import {
12
12
  log
13
13
  } from "./chunk-H4BYJELI.js";
@@ -213,6 +213,9 @@ var Session = class {
213
213
  this.connection = newConn;
214
214
  }
215
215
  beginGrace(cb) {
216
+ log?.info(
217
+ `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.debugId}) to ${this.to} is closed`
218
+ );
216
219
  this.disconnectionGrace = setTimeout(() => {
217
220
  this.close();
218
221
  cb();
@@ -384,14 +387,11 @@ var Transport = class {
384
387
  * @param conn The connection object.
385
388
  * @param connectedTo The peer we are connected to.
386
389
  */
387
- onDisconnect(conn, connectedTo) {
390
+ onDisconnect(conn, session) {
388
391
  this.eventDispatcher.dispatchEvent("connectionStatus", {
389
392
  status: "disconnect",
390
393
  conn
391
394
  });
392
- const session = this.sessions.get(connectedTo);
393
- if (!session)
394
- return;
395
395
  session.connection = void 0;
396
396
  session.beginGrace(() => this.deleteSession(session));
397
397
  }
@@ -549,13 +549,14 @@ var ClientTransport = class extends Transport {
549
549
  this.inflightConnectionPromises = /* @__PURE__ */ new Map();
550
550
  }
551
551
  handleConnection(conn, to) {
552
+ let session = void 0;
552
553
  const handshakeHandler = (data) => {
553
554
  const handshake = this.receiveHandshakeResponseMessage(data);
554
555
  if (!handshake) {
555
556
  conn.close();
556
557
  return;
557
558
  }
558
- this.onConnect(conn, handshake.from, handshake.instanceId);
559
+ session = this.onConnect(conn, handshake.from, handshake.instanceId);
559
560
  conn.removeDataListener(handshakeHandler);
560
561
  conn.addDataListener((data2) => {
561
562
  const parsed = this.parseMsg(data2);
@@ -568,7 +569,9 @@ var ClientTransport = class extends Transport {
568
569
  };
569
570
  conn.addDataListener(handshakeHandler);
570
571
  conn.addCloseListener(() => {
571
- this.onDisconnect(conn, to);
572
+ if (session) {
573
+ this.onDisconnect(conn, session);
574
+ }
572
575
  void this.connect(to);
573
576
  });
574
577
  conn.addErrorListener((err) => {
@@ -625,6 +628,9 @@ var ClientTransport = class extends Transport {
625
628
  } catch (error) {
626
629
  const errStr = coerceErrorString(error);
627
630
  this.inflightConnectionPromises.delete(to);
631
+ const shouldRetry = this.state === "open" && this.tryReconnecting;
632
+ if (!shouldRetry)
633
+ return;
628
634
  if (attempt >= this.options.retryAttemptsMax) {
629
635
  const errMsg = `connection to ${to} failed after ${attempt} attempts (${errStr}), giving up`;
630
636
  log?.error(`${this.clientId} -- ${errMsg}`);
@@ -648,9 +654,9 @@ var ClientTransport = class extends Transport {
648
654
  log?.debug(`${this.clientId} -- sending handshake request to ${to}`);
649
655
  conn.send(this.codec.toBuffer(requestMsg));
650
656
  }
651
- onDisconnect(conn, connectedTo) {
652
- this.inflightConnectionPromises.delete(connectedTo);
653
- super.onDisconnect(conn, connectedTo);
657
+ onDisconnect(conn, session) {
658
+ this.inflightConnectionPromises.delete(session.to);
659
+ super.onDisconnect(conn, session);
654
660
  }
655
661
  };
656
662
  var ServerTransport = class extends Transport {
@@ -687,7 +693,7 @@ var ServerTransport = class extends Transport {
687
693
  log?.info(
688
694
  `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
689
695
  );
690
- this.onDisconnect(conn, session.to);
696
+ this.onDisconnect(conn, session);
691
697
  });
692
698
  conn.addErrorListener((err) => {
693
699
  if (!session)
@@ -1,5 +1,5 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { C as Connection } from './index-9aa0aabb.js';
2
+ import { C as Connection } from './index-1c1493df.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-9aa0aabb.js';
1
+ import { C as Connection } from './index-1c1493df.js';
2
2
  import { Socket } from 'node:net';
3
3
  import stream, { Transform, TransformCallback, TransformOptions } from 'node:stream';
4
4
 
@@ -336,7 +336,7 @@ declare abstract class Transport<ConnType extends Connection> {
336
336
  * @param conn The connection object.
337
337
  * @param connectedTo The peer we are connected to.
338
338
  */
339
- protected onDisconnect(conn: ConnType, connectedTo: TransportClientId): void;
339
+ protected onDisconnect(conn: ConnType, session: Session<ConnType>): void;
340
340
  /**
341
341
  * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
342
342
  * @param msg The message to parse.
@@ -410,7 +410,7 @@ declare abstract class ClientTransport<ConnType extends Connection> extends Tran
410
410
  */
411
411
  connect(to: TransportClientId, attempt?: number): Promise<void>;
412
412
  protected sendHandshake(to: TransportClientId, conn: ConnType): void;
413
- protected onDisconnect(conn: ConnType, connectedTo: string): void;
413
+ protected onDisconnect(conn: ConnType, session: Session<ConnType>): void;
414
414
  }
415
415
  declare abstract class ServerTransport<ConnType extends Connection> extends Transport<ConnType> {
416
416
  constructor(clientId: TransportClientId, providedOptions?: Partial<TransportOptions>);
@@ -793,7 +793,7 @@ var import_value = require("@sinclair/typebox/value");
793
793
  // util/stringify.ts
794
794
  function coerceErrorString(err) {
795
795
  if (err instanceof Error) {
796
- return err.message;
796
+ return err.message || "unknown reason";
797
797
  }
798
798
  return `[coerced to error] ${String(err)}`;
799
799
  }
@@ -1,6 +1,6 @@
1
- import { A as AnyService, P as PayloadType, b as Result, R as RiverError, S as ServiceContext, d as ProcType, e as ProcInput, f as ProcOutput, g as ProcErrors, h as ProcHasInit, i as ProcInit } from '../builder-eef3b061.js';
2
- export { E as Err, O as Ok, m as ProcHandler, k as ProcListing, a as Procedure, p as RiverErrorSchema, c as RiverUncaughtSchema, l as Service, j as ServiceBuilder, n as ServiceContextWithState, o as ServiceContextWithTransportInfo, U as UNCAUGHT_ERROR, V as ValidProcType, s as serializeService } from '../builder-eef3b061.js';
3
- import { S as ServerTransport, C as Connection, a as ClientTransport } from '../index-9aa0aabb.js';
1
+ import { A as AnyService, P as PayloadType, b as Result, R as RiverError, S as ServiceContext, d as ProcType, e as ProcInput, f as ProcOutput, g as ProcErrors, h as ProcHasInit, i as ProcInit } from '../builder-d649690f.js';
2
+ export { E as Err, O as Ok, m as ProcHandler, k as ProcListing, a as Procedure, p as RiverErrorSchema, c as RiverUncaughtSchema, l as Service, j as ServiceBuilder, n as ServiceContextWithState, o as ServiceContextWithTransportInfo, U as UNCAUGHT_ERROR, V as ValidProcType, s as serializeService } from '../builder-d649690f.js';
3
+ import { S as ServerTransport, C as Connection, a as ClientTransport } from '../index-1c1493df.js';
4
4
  import { Pushable } from 'it-pushable';
5
5
  import { Static } from '@sinclair/typebox';
6
6
  import '../types-3e5768ec.js';
@@ -1,6 +1,6 @@
1
- import { A as AnyService, P as PayloadType, b as Result, R as RiverError, S as ServiceContext, d as ProcType, e as ProcInput, f as ProcOutput, g as ProcErrors, h as ProcHasInit, i as ProcInit } from '../builder-eef3b061.js';
2
- export { E as Err, O as Ok, m as ProcHandler, k as ProcListing, a as Procedure, p as RiverErrorSchema, c as RiverUncaughtSchema, l as Service, j as ServiceBuilder, n as ServiceContextWithState, o as ServiceContextWithTransportInfo, U as UNCAUGHT_ERROR, V as ValidProcType, s as serializeService } from '../builder-eef3b061.js';
3
- import { S as ServerTransport, C as Connection, a as ClientTransport } from '../index-9aa0aabb.js';
1
+ import { A as AnyService, P as PayloadType, b as Result, R as RiverError, S as ServiceContext, d as ProcType, e as ProcInput, f as ProcOutput, g as ProcErrors, h as ProcHasInit, i as ProcInit } from '../builder-d649690f.js';
2
+ export { E as Err, O as Ok, m as ProcHandler, k as ProcListing, a as Procedure, p as RiverErrorSchema, c as RiverUncaughtSchema, l as Service, j as ServiceBuilder, n as ServiceContextWithState, o as ServiceContextWithTransportInfo, U as UNCAUGHT_ERROR, V as ValidProcType, s as serializeService } from '../builder-d649690f.js';
3
+ import { S as ServerTransport, C as Connection, a as ClientTransport } from '../index-1c1493df.js';
4
4
  import { Pushable } from 'it-pushable';
5
5
  import { Static } from '@sinclair/typebox';
6
6
  import '../types-3e5768ec.js';
@@ -8,8 +8,8 @@ import {
8
8
  createClient,
9
9
  createServer,
10
10
  serializeService
11
- } from "../chunk-JXKTY3GQ.js";
12
- import "../chunk-SCG5S2EC.js";
11
+ } from "../chunk-67M47YPQ.js";
12
+ import "../chunk-GFRAOY75.js";
13
13
  import "../chunk-H4BYJELI.js";
14
14
  export {
15
15
  Err,
@@ -321,6 +321,9 @@ var Session = class {
321
321
  this.connection = newConn;
322
322
  }
323
323
  beginGrace(cb) {
324
+ log?.info(
325
+ `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.debugId}) to ${this.to} is closed`
326
+ );
324
327
  this.disconnectionGrace = setTimeout(() => {
325
328
  this.close();
326
329
  cb();
@@ -492,7 +495,7 @@ var import_nanoid3 = require("nanoid");
492
495
  // util/stringify.ts
493
496
  function coerceErrorString(err) {
494
497
  if (err instanceof Error) {
495
- return err.message;
498
+ return err.message || "unknown reason";
496
499
  }
497
500
  return `[coerced to error] ${String(err)}`;
498
501
  }
@@ -625,14 +628,11 @@ var Transport = class {
625
628
  * @param conn The connection object.
626
629
  * @param connectedTo The peer we are connected to.
627
630
  */
628
- onDisconnect(conn, connectedTo) {
631
+ onDisconnect(conn, session) {
629
632
  this.eventDispatcher.dispatchEvent("connectionStatus", {
630
633
  status: "disconnect",
631
634
  conn
632
635
  });
633
- const session = this.sessions.get(connectedTo);
634
- if (!session)
635
- return;
636
636
  session.connection = void 0;
637
637
  session.beginGrace(() => this.deleteSession(session));
638
638
  }
@@ -790,13 +790,14 @@ var ClientTransport = class extends Transport {
790
790
  this.inflightConnectionPromises = /* @__PURE__ */ new Map();
791
791
  }
792
792
  handleConnection(conn, to) {
793
+ let session = void 0;
793
794
  const handshakeHandler = (data) => {
794
795
  const handshake = this.receiveHandshakeResponseMessage(data);
795
796
  if (!handshake) {
796
797
  conn.close();
797
798
  return;
798
799
  }
799
- this.onConnect(conn, handshake.from, handshake.instanceId);
800
+ session = this.onConnect(conn, handshake.from, handshake.instanceId);
800
801
  conn.removeDataListener(handshakeHandler);
801
802
  conn.addDataListener((data2) => {
802
803
  const parsed = this.parseMsg(data2);
@@ -809,7 +810,9 @@ var ClientTransport = class extends Transport {
809
810
  };
810
811
  conn.addDataListener(handshakeHandler);
811
812
  conn.addCloseListener(() => {
812
- this.onDisconnect(conn, to);
813
+ if (session) {
814
+ this.onDisconnect(conn, session);
815
+ }
813
816
  void this.connect(to);
814
817
  });
815
818
  conn.addErrorListener((err) => {
@@ -866,6 +869,9 @@ var ClientTransport = class extends Transport {
866
869
  } catch (error) {
867
870
  const errStr = coerceErrorString(error);
868
871
  this.inflightConnectionPromises.delete(to);
872
+ const shouldRetry = this.state === "open" && this.tryReconnecting;
873
+ if (!shouldRetry)
874
+ return;
869
875
  if (attempt >= this.options.retryAttemptsMax) {
870
876
  const errMsg = `connection to ${to} failed after ${attempt} attempts (${errStr}), giving up`;
871
877
  log?.error(`${this.clientId} -- ${errMsg}`);
@@ -889,9 +895,9 @@ var ClientTransport = class extends Transport {
889
895
  log?.debug(`${this.clientId} -- sending handshake request to ${to}`);
890
896
  conn.send(this.codec.toBuffer(requestMsg));
891
897
  }
892
- onDisconnect(conn, connectedTo) {
893
- this.inflightConnectionPromises.delete(connectedTo);
894
- super.onDisconnect(conn, connectedTo);
898
+ onDisconnect(conn, session) {
899
+ this.inflightConnectionPromises.delete(session.to);
900
+ super.onDisconnect(conn, session);
895
901
  }
896
902
  };
897
903
 
@@ -1,5 +1,5 @@
1
- import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-9aa0aabb.js';
2
- import { U as UdsConnection } from '../../../connection-03e650c8.js';
1
+ import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1c1493df.js';
2
+ import { U as UdsConnection } from '../../../connection-6222fd0b.js';
3
3
  import '../../../types-3e5768ec.js';
4
4
  import '@sinclair/typebox';
5
5
  import 'node:net';
@@ -1,5 +1,5 @@
1
- import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-9aa0aabb.js';
2
- import { U as UdsConnection } from '../../../connection-03e650c8.js';
1
+ import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1c1493df.js';
2
+ import { U as UdsConnection } from '../../../connection-6222fd0b.js';
3
3
  import '../../../types-3e5768ec.js';
4
4
  import '@sinclair/typebox';
5
5
  import 'node:net';
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  UdsConnection
3
- } from "../../../chunk-JXO2SCQB.js";
3
+ } from "../../../chunk-FIX2F6VT.js";
4
4
  import {
5
5
  ClientTransport
6
- } from "../../../chunk-LDUFHGZU.js";
7
- import "../../../chunk-SCG5S2EC.js";
6
+ } from "../../../chunk-MCTB3NYK.js";
7
+ import "../../../chunk-GFRAOY75.js";
8
8
  import {
9
9
  log
10
10
  } from "../../../chunk-H4BYJELI.js";
@@ -359,6 +359,9 @@ var Session = class {
359
359
  this.connection = newConn;
360
360
  }
361
361
  beginGrace(cb) {
362
+ log?.info(
363
+ `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.debugId}) to ${this.to} is closed`
364
+ );
362
365
  this.disconnectionGrace = setTimeout(() => {
363
366
  this.close();
364
367
  cb();
@@ -406,7 +409,7 @@ var import_nanoid3 = require("nanoid");
406
409
  // util/stringify.ts
407
410
  function coerceErrorString(err) {
408
411
  if (err instanceof Error) {
409
- return err.message;
412
+ return err.message || "unknown reason";
410
413
  }
411
414
  return `[coerced to error] ${String(err)}`;
412
415
  }
@@ -539,14 +542,11 @@ var Transport = class {
539
542
  * @param conn The connection object.
540
543
  * @param connectedTo The peer we are connected to.
541
544
  */
542
- onDisconnect(conn, connectedTo) {
545
+ onDisconnect(conn, session) {
543
546
  this.eventDispatcher.dispatchEvent("connectionStatus", {
544
547
  status: "disconnect",
545
548
  conn
546
549
  });
547
- const session = this.sessions.get(connectedTo);
548
- if (!session)
549
- return;
550
550
  session.connection = void 0;
551
551
  session.beginGrace(() => this.deleteSession(session));
552
552
  }
@@ -725,7 +725,7 @@ var ServerTransport = class extends Transport {
725
725
  log?.info(
726
726
  `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
727
727
  );
728
- this.onDisconnect(conn, session.to);
728
+ this.onDisconnect(conn, session);
729
729
  });
730
730
  conn.addErrorListener((err) => {
731
731
  if (!session)
@@ -1,6 +1,6 @@
1
1
  import { Server, Socket } from 'node:net';
2
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-9aa0aabb.js';
3
- import { U as UdsConnection } from '../../../connection-03e650c8.js';
2
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1c1493df.js';
3
+ import { U as UdsConnection } from '../../../connection-6222fd0b.js';
4
4
  import '../../../types-3e5768ec.js';
5
5
  import '@sinclair/typebox';
6
6
  import 'node:stream';
@@ -1,6 +1,6 @@
1
1
  import { Server, Socket } from 'node:net';
2
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-9aa0aabb.js';
3
- import { U as UdsConnection } from '../../../connection-03e650c8.js';
2
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1c1493df.js';
3
+ import { U as UdsConnection } from '../../../connection-6222fd0b.js';
4
4
  import '../../../types-3e5768ec.js';
5
5
  import '@sinclair/typebox';
6
6
  import 'node:stream';
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  UdsConnection
3
- } from "../../../chunk-JXO2SCQB.js";
3
+ } from "../../../chunk-FIX2F6VT.js";
4
4
  import {
5
5
  ServerTransport
6
- } from "../../../chunk-LDUFHGZU.js";
7
- import "../../../chunk-SCG5S2EC.js";
6
+ } from "../../../chunk-MCTB3NYK.js";
7
+ import "../../../chunk-GFRAOY75.js";
8
8
  import {
9
9
  log
10
10
  } from "../../../chunk-H4BYJELI.js";
@@ -351,6 +351,9 @@ var Session = class {
351
351
  this.connection = newConn;
352
352
  }
353
353
  beginGrace(cb) {
354
+ log?.info(
355
+ `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.debugId}) to ${this.to} is closed`
356
+ );
354
357
  this.disconnectionGrace = setTimeout(() => {
355
358
  this.close();
356
359
  cb();
@@ -398,7 +401,7 @@ var import_nanoid3 = require("nanoid");
398
401
  // util/stringify.ts
399
402
  function coerceErrorString(err) {
400
403
  if (err instanceof Error) {
401
- return err.message;
404
+ return err.message || "unknown reason";
402
405
  }
403
406
  return `[coerced to error] ${String(err)}`;
404
407
  }
@@ -531,14 +534,11 @@ var Transport = class {
531
534
  * @param conn The connection object.
532
535
  * @param connectedTo The peer we are connected to.
533
536
  */
534
- onDisconnect(conn, connectedTo) {
537
+ onDisconnect(conn, session) {
535
538
  this.eventDispatcher.dispatchEvent("connectionStatus", {
536
539
  status: "disconnect",
537
540
  conn
538
541
  });
539
- const session = this.sessions.get(connectedTo);
540
- if (!session)
541
- return;
542
542
  session.connection = void 0;
543
543
  session.beginGrace(() => this.deleteSession(session));
544
544
  }
@@ -696,13 +696,14 @@ var ClientTransport = class extends Transport {
696
696
  this.inflightConnectionPromises = /* @__PURE__ */ new Map();
697
697
  }
698
698
  handleConnection(conn, to) {
699
+ let session = void 0;
699
700
  const handshakeHandler = (data) => {
700
701
  const handshake = this.receiveHandshakeResponseMessage(data);
701
702
  if (!handshake) {
702
703
  conn.close();
703
704
  return;
704
705
  }
705
- this.onConnect(conn, handshake.from, handshake.instanceId);
706
+ session = this.onConnect(conn, handshake.from, handshake.instanceId);
706
707
  conn.removeDataListener(handshakeHandler);
707
708
  conn.addDataListener((data2) => {
708
709
  const parsed = this.parseMsg(data2);
@@ -715,7 +716,9 @@ var ClientTransport = class extends Transport {
715
716
  };
716
717
  conn.addDataListener(handshakeHandler);
717
718
  conn.addCloseListener(() => {
718
- this.onDisconnect(conn, to);
719
+ if (session) {
720
+ this.onDisconnect(conn, session);
721
+ }
719
722
  void this.connect(to);
720
723
  });
721
724
  conn.addErrorListener((err) => {
@@ -772,6 +775,9 @@ var ClientTransport = class extends Transport {
772
775
  } catch (error) {
773
776
  const errStr = coerceErrorString(error);
774
777
  this.inflightConnectionPromises.delete(to);
778
+ const shouldRetry = this.state === "open" && this.tryReconnecting;
779
+ if (!shouldRetry)
780
+ return;
775
781
  if (attempt >= this.options.retryAttemptsMax) {
776
782
  const errMsg = `connection to ${to} failed after ${attempt} attempts (${errStr}), giving up`;
777
783
  log?.error(`${this.clientId} -- ${errMsg}`);
@@ -795,9 +801,9 @@ var ClientTransport = class extends Transport {
795
801
  log?.debug(`${this.clientId} -- sending handshake request to ${to}`);
796
802
  conn.send(this.codec.toBuffer(requestMsg));
797
803
  }
798
- onDisconnect(conn, connectedTo) {
799
- this.inflightConnectionPromises.delete(connectedTo);
800
- super.onDisconnect(conn, connectedTo);
804
+ onDisconnect(conn, session) {
805
+ this.inflightConnectionPromises.delete(session.to);
806
+ super.onDisconnect(conn, session);
801
807
  }
802
808
  };
803
809
 
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-9aa0aabb.js';
3
- import { W as WebSocketConnection } from '../../../connection-d052d027.js';
2
+ import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1c1493df.js';
3
+ import { W as WebSocketConnection } from '../../../connection-2b55cc7f.js';
4
4
  import '../../../types-3e5768ec.js';
5
5
  import '@sinclair/typebox';
6
6
 
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-9aa0aabb.js';
3
- import { W as WebSocketConnection } from '../../../connection-d052d027.js';
2
+ import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1c1493df.js';
3
+ import { W as WebSocketConnection } from '../../../connection-2b55cc7f.js';
4
4
  import '../../../types-3e5768ec.js';
5
5
  import '@sinclair/typebox';
6
6
 
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "../../../chunk-Q7AWJYDQ.js";
3
+ } from "../../../chunk-5V6B7TM3.js";
4
4
  import {
5
5
  ClientTransport
6
- } from "../../../chunk-LDUFHGZU.js";
7
- import "../../../chunk-SCG5S2EC.js";
6
+ } from "../../../chunk-MCTB3NYK.js";
7
+ import "../../../chunk-GFRAOY75.js";
8
8
  import {
9
9
  log
10
10
  } from "../../../chunk-H4BYJELI.js";
@@ -359,6 +359,9 @@ var Session = class {
359
359
  this.connection = newConn;
360
360
  }
361
361
  beginGrace(cb) {
362
+ log?.info(
363
+ `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.debugId}) to ${this.to} is closed`
364
+ );
362
365
  this.disconnectionGrace = setTimeout(() => {
363
366
  this.close();
364
367
  cb();
@@ -406,7 +409,7 @@ var import_nanoid3 = require("nanoid");
406
409
  // util/stringify.ts
407
410
  function coerceErrorString(err) {
408
411
  if (err instanceof Error) {
409
- return err.message;
412
+ return err.message || "unknown reason";
410
413
  }
411
414
  return `[coerced to error] ${String(err)}`;
412
415
  }
@@ -539,14 +542,11 @@ var Transport = class {
539
542
  * @param conn The connection object.
540
543
  * @param connectedTo The peer we are connected to.
541
544
  */
542
- onDisconnect(conn, connectedTo) {
545
+ onDisconnect(conn, session) {
543
546
  this.eventDispatcher.dispatchEvent("connectionStatus", {
544
547
  status: "disconnect",
545
548
  conn
546
549
  });
547
- const session = this.sessions.get(connectedTo);
548
- if (!session)
549
- return;
550
550
  session.connection = void 0;
551
551
  session.beginGrace(() => this.deleteSession(session));
552
552
  }
@@ -725,7 +725,7 @@ var ServerTransport = class extends Transport {
725
725
  log?.info(
726
726
  `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
727
727
  );
728
- this.onDisconnect(conn, session.to);
728
+ this.onDisconnect(conn, session);
729
729
  });
730
730
  conn.addErrorListener((err) => {
731
731
  if (!session)
@@ -1,7 +1,7 @@
1
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-9aa0aabb.js';
1
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1c1493df.js';
2
2
  import { WebSocketServer } from 'ws';
3
3
  import { WebSocket } from 'isomorphic-ws';
4
- import { W as WebSocketConnection } from '../../../connection-d052d027.js';
4
+ import { W as WebSocketConnection } from '../../../connection-2b55cc7f.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
 
@@ -1,7 +1,7 @@
1
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-9aa0aabb.js';
1
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1c1493df.js';
2
2
  import { WebSocketServer } from 'ws';
3
3
  import { WebSocket } from 'isomorphic-ws';
4
- import { W as WebSocketConnection } from '../../../connection-d052d027.js';
4
+ import { W as WebSocketConnection } from '../../../connection-2b55cc7f.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
 
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "../../../chunk-Q7AWJYDQ.js";
3
+ } from "../../../chunk-5V6B7TM3.js";
4
4
  import {
5
5
  ServerTransport
6
- } from "../../../chunk-LDUFHGZU.js";
7
- import "../../../chunk-SCG5S2EC.js";
6
+ } from "../../../chunk-MCTB3NYK.js";
7
+ import "../../../chunk-GFRAOY75.js";
8
8
  import {
9
9
  log
10
10
  } from "../../../chunk-H4BYJELI.js";
@@ -381,6 +381,9 @@ var Session = class {
381
381
  this.connection = newConn;
382
382
  }
383
383
  beginGrace(cb) {
384
+ log?.info(
385
+ `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.debugId}) to ${this.to} is closed`
386
+ );
384
387
  this.disconnectionGrace = setTimeout(() => {
385
388
  this.close();
386
389
  cb();
@@ -428,7 +431,7 @@ var import_nanoid3 = require("nanoid");
428
431
  // util/stringify.ts
429
432
  function coerceErrorString(err) {
430
433
  if (err instanceof Error) {
431
- return err.message;
434
+ return err.message || "unknown reason";
432
435
  }
433
436
  return `[coerced to error] ${String(err)}`;
434
437
  }
@@ -561,14 +564,11 @@ var Transport = class {
561
564
  * @param conn The connection object.
562
565
  * @param connectedTo The peer we are connected to.
563
566
  */
564
- onDisconnect(conn, connectedTo) {
567
+ onDisconnect(conn, session) {
565
568
  this.eventDispatcher.dispatchEvent("connectionStatus", {
566
569
  status: "disconnect",
567
570
  conn
568
571
  });
569
- const session = this.sessions.get(connectedTo);
570
- if (!session)
571
- return;
572
572
  session.connection = void 0;
573
573
  session.beginGrace(() => this.deleteSession(session));
574
574
  }
@@ -726,13 +726,14 @@ var ClientTransport = class extends Transport {
726
726
  this.inflightConnectionPromises = /* @__PURE__ */ new Map();
727
727
  }
728
728
  handleConnection(conn, to) {
729
+ let session = void 0;
729
730
  const handshakeHandler = (data) => {
730
731
  const handshake = this.receiveHandshakeResponseMessage(data);
731
732
  if (!handshake) {
732
733
  conn.close();
733
734
  return;
734
735
  }
735
- this.onConnect(conn, handshake.from, handshake.instanceId);
736
+ session = this.onConnect(conn, handshake.from, handshake.instanceId);
736
737
  conn.removeDataListener(handshakeHandler);
737
738
  conn.addDataListener((data2) => {
738
739
  const parsed = this.parseMsg(data2);
@@ -745,7 +746,9 @@ var ClientTransport = class extends Transport {
745
746
  };
746
747
  conn.addDataListener(handshakeHandler);
747
748
  conn.addCloseListener(() => {
748
- this.onDisconnect(conn, to);
749
+ if (session) {
750
+ this.onDisconnect(conn, session);
751
+ }
749
752
  void this.connect(to);
750
753
  });
751
754
  conn.addErrorListener((err) => {
@@ -802,6 +805,9 @@ var ClientTransport = class extends Transport {
802
805
  } catch (error) {
803
806
  const errStr = coerceErrorString(error);
804
807
  this.inflightConnectionPromises.delete(to);
808
+ const shouldRetry = this.state === "open" && this.tryReconnecting;
809
+ if (!shouldRetry)
810
+ return;
805
811
  if (attempt >= this.options.retryAttemptsMax) {
806
812
  const errMsg = `connection to ${to} failed after ${attempt} attempts (${errStr}), giving up`;
807
813
  log?.error(`${this.clientId} -- ${errMsg}`);
@@ -825,9 +831,9 @@ var ClientTransport = class extends Transport {
825
831
  log?.debug(`${this.clientId} -- sending handshake request to ${to}`);
826
832
  conn.send(this.codec.toBuffer(requestMsg));
827
833
  }
828
- onDisconnect(conn, connectedTo) {
829
- this.inflightConnectionPromises.delete(connectedTo);
830
- super.onDisconnect(conn, connectedTo);
834
+ onDisconnect(conn, session) {
835
+ this.inflightConnectionPromises.delete(session.to);
836
+ super.onDisconnect(conn, session);
831
837
  }
832
838
  };
833
839
  var ServerTransport = class extends Transport {
@@ -864,7 +870,7 @@ var ServerTransport = class extends Transport {
864
870
  log?.info(
865
871
  `${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
866
872
  );
867
- this.onDisconnect(conn, session.to);
873
+ this.onDisconnect(conn, session);
868
874
  });
869
875
  conn.addErrorListener((err) => {
870
876
  if (!session)
@@ -1,3 +1,3 @@
1
- export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-9aa0aabb.js';
1
+ export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-1c1493df.js';
2
2
  import '../types-3e5768ec.js';
3
3
  import '@sinclair/typebox';
@@ -1,3 +1,3 @@
1
- export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-9aa0aabb.js';
1
+ export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-1c1493df.js';
2
2
  import '../types-3e5768ec.js';
3
3
  import '@sinclair/typebox';
@@ -5,11 +5,11 @@ import {
5
5
  ServerTransport,
6
6
  Session,
7
7
  Transport
8
- } from "../chunk-LDUFHGZU.js";
8
+ } from "../chunk-MCTB3NYK.js";
9
9
  import {
10
10
  OpaqueTransportMessageSchema,
11
11
  TransportMessageSchema
12
- } from "../chunk-SCG5S2EC.js";
12
+ } from "../chunk-GFRAOY75.js";
13
13
  import "../chunk-H4BYJELI.js";
14
14
  import "../chunk-GZ7HCLLM.js";
15
15
  export {
@@ -269,6 +269,9 @@ var Session = class {
269
269
  this.connection = newConn;
270
270
  }
271
271
  beginGrace(cb) {
272
+ log?.info(
273
+ `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.debugId}) to ${this.to} is closed`
274
+ );
272
275
  this.disconnectionGrace = setTimeout(() => {
273
276
  this.close();
274
277
  cb();
@@ -313,7 +316,7 @@ var Session = class {
313
316
  // util/stringify.ts
314
317
  function coerceErrorString(err) {
315
318
  if (err instanceof Error) {
316
- return err.message;
319
+ return err.message || "unknown reason";
317
320
  }
318
321
  return `[coerced to error] ${String(err)}`;
319
322
  }
@@ -1,8 +1,8 @@
1
1
  import * as it_pushable from 'it-pushable';
2
2
  import WebSocket from 'isomorphic-ws';
3
3
  import http from 'node:http';
4
- import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-9aa0aabb.js';
5
- import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema } from '../builder-eef3b061.js';
4
+ import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-1c1493df.js';
5
+ import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema } from '../builder-d649690f.js';
6
6
  import { Static } from '@sinclair/typebox';
7
7
  import net from 'node:net';
8
8
  import '../types-3e5768ec.js';
@@ -1,8 +1,8 @@
1
1
  import * as it_pushable from 'it-pushable';
2
2
  import WebSocket from 'isomorphic-ws';
3
3
  import http from 'node:http';
4
- import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-9aa0aabb.js';
5
- import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema } from '../builder-eef3b061.js';
4
+ import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-1c1493df.js';
5
+ import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema } from '../builder-d649690f.js';
6
6
  import { Static } from '@sinclair/typebox';
7
7
  import net from 'node:net';
8
8
  import '../types-3e5768ec.js';
@@ -1,15 +1,15 @@
1
1
  import {
2
2
  UNCAUGHT_ERROR,
3
3
  pushable
4
- } from "../chunk-JXKTY3GQ.js";
4
+ } from "../chunk-67M47YPQ.js";
5
5
  import "../chunk-5IZ2UHWV.js";
6
6
  import {
7
7
  Session,
8
8
  defaultSessionOptions
9
- } from "../chunk-LDUFHGZU.js";
9
+ } from "../chunk-MCTB3NYK.js";
10
10
  import {
11
11
  coerceErrorString
12
- } from "../chunk-SCG5S2EC.js";
12
+ } from "../chunk-GFRAOY75.js";
13
13
  import "../chunk-H4BYJELI.js";
14
14
  import "../chunk-GZ7HCLLM.js";
15
15
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@replit/river",
3
3
  "description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
4
- "version": "0.13.1",
4
+ "version": "0.13.3",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": {