@replit/river 0.12.2 → 0.12.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 (45) hide show
  1. package/dist/{chunk-CT57CKBS.js → chunk-2PKONGC3.js} +34 -31
  2. package/dist/{chunk-NHSJ4RDY.js → chunk-6OAKPO5R.js} +1 -1
  3. package/dist/{chunk-PIF32FUL.js → chunk-7J66WOIC.js} +1 -1
  4. package/dist/{chunk-55XUAPC6.js → chunk-AFLZ6INU.js} +1 -1
  5. package/dist/{chunk-R6YJHJV7.js → chunk-H4QR2K3T.js} +1 -1
  6. package/dist/{connection-eb10d250.d.ts → connection-4a839b9a.d.ts} +1 -1
  7. package/dist/{connection-bf7811aa.d.ts → connection-64b9e0ff.d.ts} +1 -1
  8. package/dist/{connection-d880aa4a.d.ts → connection-a6cc48e8.d.ts} +1 -1
  9. package/dist/{index-0c0a69f6.d.ts → index-1afc5580.d.ts} +5 -9
  10. package/dist/router/index.cjs +1 -1
  11. package/dist/router/index.d.cts +1 -1
  12. package/dist/router/index.d.ts +1 -1
  13. package/dist/router/index.js +1 -1
  14. package/dist/transport/impls/stdio/client.cjs +34 -31
  15. package/dist/transport/impls/stdio/client.d.cts +2 -2
  16. package/dist/transport/impls/stdio/client.d.ts +2 -2
  17. package/dist/transport/impls/stdio/client.js +2 -2
  18. package/dist/transport/impls/stdio/server.cjs +33 -29
  19. package/dist/transport/impls/stdio/server.d.cts +2 -2
  20. package/dist/transport/impls/stdio/server.d.ts +2 -2
  21. package/dist/transport/impls/stdio/server.js +2 -2
  22. package/dist/transport/impls/uds/client.cjs +34 -31
  23. package/dist/transport/impls/uds/client.d.cts +2 -2
  24. package/dist/transport/impls/uds/client.d.ts +2 -2
  25. package/dist/transport/impls/uds/client.js +2 -2
  26. package/dist/transport/impls/uds/server.cjs +33 -29
  27. package/dist/transport/impls/uds/server.d.cts +2 -2
  28. package/dist/transport/impls/uds/server.d.ts +2 -2
  29. package/dist/transport/impls/uds/server.js +2 -2
  30. package/dist/transport/impls/ws/client.cjs +36 -32
  31. package/dist/transport/impls/ws/client.d.cts +2 -2
  32. package/dist/transport/impls/ws/client.d.ts +2 -2
  33. package/dist/transport/impls/ws/client.js +4 -3
  34. package/dist/transport/impls/ws/server.cjs +33 -29
  35. package/dist/transport/impls/ws/server.d.cts +2 -2
  36. package/dist/transport/impls/ws/server.d.ts +2 -2
  37. package/dist/transport/impls/ws/server.js +2 -2
  38. package/dist/transport/index.cjs +34 -31
  39. package/dist/transport/index.d.cts +1 -1
  40. package/dist/transport/index.d.ts +1 -1
  41. package/dist/transport/index.js +1 -1
  42. package/dist/util/testHelpers.d.cts +1 -1
  43. package/dist/util/testHelpers.d.ts +1 -1
  44. package/dist/util/testHelpers.js +1 -1
  45. package/package.json +1 -1
@@ -296,17 +296,27 @@ var Session = class {
296
296
  this.cancelGrace();
297
297
  this.connection = newConn;
298
298
  }
299
+ graceCb;
299
300
  beginGrace(cb) {
301
+ this.graceCb = cb;
300
302
  this.disconnectionGrace = setTimeout(() => {
301
- this.resetBufferedMessages();
302
- clearInterval(this.heartbeat);
303
+ this.close();
303
304
  cb();
304
305
  }, SESSION_DISCONNECT_GRACE_MS);
305
306
  }
307
+ // called on reconnect of the underlying session
306
308
  cancelGrace() {
307
309
  this.heartbeatMisses = 0;
308
310
  clearTimeout(this.disconnectionGrace);
309
311
  }
312
+ // closed when we want to discard the whole session
313
+ // (i.e. shutdown or session disconnect)
314
+ close() {
315
+ this.closeStaleConnection(this.connection);
316
+ this.cancelGrace();
317
+ clearInterval(this.heartbeat);
318
+ this.resetBufferedMessages();
319
+ }
310
320
  get connected() {
311
321
  return this.connection !== void 0;
312
322
  }
@@ -325,16 +335,6 @@ var Session = class {
325
335
  this.seq++;
326
336
  return msg;
327
337
  }
328
- /**
329
- * Closes the out-going connection but doesn't remove the listeners
330
- * for incoming messages. The connection will eventually call onClose
331
- * when it is ready to be cleaned up and only then will {@link connection} be set back
332
- * to undefined
333
- */
334
- halfCloseConnection() {
335
- this.connection?.close();
336
- clearInterval(this.heartbeat);
337
- }
338
338
  inspectSendBuffer() {
339
339
  return this.sendBuffer;
340
340
  }
@@ -486,19 +486,18 @@ var Transport = class {
486
486
  status: "connect",
487
487
  conn
488
488
  });
489
- let session = this.sessions.get(connectedTo);
489
+ let oldSession = this.sessions.get(connectedTo);
490
490
  const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
491
- if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
491
+ if (oldSession && lastInstanceId !== void 0 && lastInstanceId !== instanceId) {
492
492
  log?.warn(
493
- `${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
493
+ `${this.clientId} -- handshake from ${connectedTo} is a different instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
494
494
  );
495
- session.resetBufferedMessages();
496
- session.closeStaleConnection(conn);
497
- this.deleteSession(session);
498
- session = void 0;
495
+ oldSession.close();
496
+ this.deleteSession(oldSession);
497
+ oldSession = void 0;
499
498
  }
500
499
  this.connectedInstanceIds.set(connectedTo, instanceId);
501
- if (session === void 0) {
500
+ if (oldSession === void 0) {
502
501
  const newSession = this.createSession(connectedTo, conn);
503
502
  log?.info(
504
503
  `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
@@ -506,11 +505,11 @@ var Transport = class {
506
505
  return newSession;
507
506
  }
508
507
  log?.info(
509
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
508
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.debugId}) to ${connectedTo}`
510
509
  );
511
- session.replaceWithNewConnection(conn);
512
- session.sendBufferedMessages();
513
- return session;
510
+ oldSession.replaceWithNewConnection(conn);
511
+ oldSession.sendBufferedMessages();
512
+ return oldSession;
514
513
  }
515
514
  createSession(connectedTo, conn) {
516
515
  const session = new Session(
@@ -539,13 +538,14 @@ var Transport = class {
539
538
  /**
540
539
  * The downstream implementation needs to call this when a connection is closed.
541
540
  * @param conn The connection object.
541
+ * @param connectedTo The peer we are connected to.
542
542
  */
543
543
  onDisconnect(conn, connectedTo) {
544
544
  this.eventDispatcher.dispatchEvent("connectionStatus", {
545
545
  status: "disconnect",
546
546
  conn
547
547
  });
548
- if (!connectedTo)
548
+ if (this.state !== "open")
549
549
  return;
550
550
  const session = this.sessionByClientId(connectedTo);
551
551
  log?.info(
@@ -588,6 +588,8 @@ var Transport = class {
588
588
  * @param msg The received message.
589
589
  */
590
590
  handleMsg(msg) {
591
+ if (this.state !== "open")
592
+ return;
591
593
  const session = this.sessionByClientId(msg.from);
592
594
  session.cancelGrace();
593
595
  log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
@@ -664,10 +666,11 @@ var Transport = class {
664
666
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
665
667
  */
666
668
  close() {
669
+ this.state = "closed";
667
670
  for (const session of this.sessions.values()) {
668
- session.halfCloseConnection();
671
+ session.close();
672
+ this.deleteSession(session);
669
673
  }
670
- this.state = "closed";
671
674
  log?.info(`${this.clientId} -- manually closed transport`);
672
675
  }
673
676
  /**
@@ -676,10 +679,11 @@ var Transport = class {
676
679
  * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
677
680
  */
678
681
  destroy() {
682
+ this.state = "destroyed";
679
683
  for (const session of this.sessions.values()) {
680
- session.closeStaleConnection(session.connection);
684
+ session.close();
685
+ this.deleteSession(session);
681
686
  }
682
- this.state = "destroyed";
683
687
  log?.info(`${this.clientId} -- manually destroyed transport`);
684
688
  }
685
689
  };
@@ -1,7 +1,7 @@
1
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
1
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
2
2
  import { WebSocketServer } from 'ws';
3
3
  import { WebSocket } from 'isomorphic-ws';
4
- import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
4
+ import { W as WebSocketConnection } from '../../../connection-4a839b9a.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-0c0a69f6.js';
1
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
2
2
  import { WebSocketServer } from 'ws';
3
3
  import { WebSocket } from 'isomorphic-ws';
4
- import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
4
+ import { W as WebSocketConnection } from '../../../connection-4a839b9a.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
 
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "../../../chunk-NHSJ4RDY.js";
3
+ } from "../../../chunk-6OAKPO5R.js";
4
4
  import {
5
5
  ServerTransport
6
- } from "../../../chunk-CT57CKBS.js";
6
+ } from "../../../chunk-2PKONGC3.js";
7
7
  import "../../../chunk-GZ7HCLLM.js";
8
8
  import "../../../chunk-XFFS4UOD.js";
9
9
  import {
@@ -318,17 +318,27 @@ var Session = class {
318
318
  this.cancelGrace();
319
319
  this.connection = newConn;
320
320
  }
321
+ graceCb;
321
322
  beginGrace(cb) {
323
+ this.graceCb = cb;
322
324
  this.disconnectionGrace = setTimeout(() => {
323
- this.resetBufferedMessages();
324
- clearInterval(this.heartbeat);
325
+ this.close();
325
326
  cb();
326
327
  }, SESSION_DISCONNECT_GRACE_MS);
327
328
  }
329
+ // called on reconnect of the underlying session
328
330
  cancelGrace() {
329
331
  this.heartbeatMisses = 0;
330
332
  clearTimeout(this.disconnectionGrace);
331
333
  }
334
+ // closed when we want to discard the whole session
335
+ // (i.e. shutdown or session disconnect)
336
+ close() {
337
+ this.closeStaleConnection(this.connection);
338
+ this.cancelGrace();
339
+ clearInterval(this.heartbeat);
340
+ this.resetBufferedMessages();
341
+ }
332
342
  get connected() {
333
343
  return this.connection !== void 0;
334
344
  }
@@ -347,16 +357,6 @@ var Session = class {
347
357
  this.seq++;
348
358
  return msg;
349
359
  }
350
- /**
351
- * Closes the out-going connection but doesn't remove the listeners
352
- * for incoming messages. The connection will eventually call onClose
353
- * when it is ready to be cleaned up and only then will {@link connection} be set back
354
- * to undefined
355
- */
356
- halfCloseConnection() {
357
- this.connection?.close();
358
- clearInterval(this.heartbeat);
359
- }
360
360
  inspectSendBuffer() {
361
361
  return this.sendBuffer;
362
362
  }
@@ -508,19 +508,18 @@ var Transport = class {
508
508
  status: "connect",
509
509
  conn
510
510
  });
511
- let session = this.sessions.get(connectedTo);
511
+ let oldSession = this.sessions.get(connectedTo);
512
512
  const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
513
- if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
513
+ if (oldSession && lastInstanceId !== void 0 && lastInstanceId !== instanceId) {
514
514
  log?.warn(
515
- `${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
515
+ `${this.clientId} -- handshake from ${connectedTo} is a different instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
516
516
  );
517
- session.resetBufferedMessages();
518
- session.closeStaleConnection(conn);
519
- this.deleteSession(session);
520
- session = void 0;
517
+ oldSession.close();
518
+ this.deleteSession(oldSession);
519
+ oldSession = void 0;
521
520
  }
522
521
  this.connectedInstanceIds.set(connectedTo, instanceId);
523
- if (session === void 0) {
522
+ if (oldSession === void 0) {
524
523
  const newSession = this.createSession(connectedTo, conn);
525
524
  log?.info(
526
525
  `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
@@ -528,11 +527,11 @@ var Transport = class {
528
527
  return newSession;
529
528
  }
530
529
  log?.info(
531
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
530
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.debugId}) to ${connectedTo}`
532
531
  );
533
- session.replaceWithNewConnection(conn);
534
- session.sendBufferedMessages();
535
- return session;
532
+ oldSession.replaceWithNewConnection(conn);
533
+ oldSession.sendBufferedMessages();
534
+ return oldSession;
536
535
  }
537
536
  createSession(connectedTo, conn) {
538
537
  const session = new Session(
@@ -561,13 +560,14 @@ var Transport = class {
561
560
  /**
562
561
  * The downstream implementation needs to call this when a connection is closed.
563
562
  * @param conn The connection object.
563
+ * @param connectedTo The peer we are connected to.
564
564
  */
565
565
  onDisconnect(conn, connectedTo) {
566
566
  this.eventDispatcher.dispatchEvent("connectionStatus", {
567
567
  status: "disconnect",
568
568
  conn
569
569
  });
570
- if (!connectedTo)
570
+ if (this.state !== "open")
571
571
  return;
572
572
  const session = this.sessionByClientId(connectedTo);
573
573
  log?.info(
@@ -610,6 +610,8 @@ var Transport = class {
610
610
  * @param msg The received message.
611
611
  */
612
612
  handleMsg(msg) {
613
+ if (this.state !== "open")
614
+ return;
613
615
  const session = this.sessionByClientId(msg.from);
614
616
  session.cancelGrace();
615
617
  log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
@@ -686,10 +688,11 @@ var Transport = class {
686
688
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
687
689
  */
688
690
  close() {
691
+ this.state = "closed";
689
692
  for (const session of this.sessions.values()) {
690
- session.halfCloseConnection();
693
+ session.close();
694
+ this.deleteSession(session);
691
695
  }
692
- this.state = "closed";
693
696
  log?.info(`${this.clientId} -- manually closed transport`);
694
697
  }
695
698
  /**
@@ -698,10 +701,11 @@ var Transport = class {
698
701
  * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
699
702
  */
700
703
  destroy() {
704
+ this.state = "destroyed";
701
705
  for (const session of this.sessions.values()) {
702
- session.closeStaleConnection(session.connection);
706
+ session.close();
707
+ this.deleteSession(session);
703
708
  }
704
- this.state = "destroyed";
705
709
  log?.info(`${this.clientId} -- manually destroyed transport`);
706
710
  }
707
711
  };
@@ -807,8 +811,7 @@ var ClientTransport = class extends Transport {
807
811
  return bootHandler;
808
812
  }
809
813
  onDisconnect(conn, connectedTo) {
810
- if (connectedTo)
811
- this.inflightConnectionPromises.delete(connectedTo);
814
+ this.inflightConnectionPromises.delete(connectedTo);
812
815
  super.onDisconnect(conn, connectedTo);
813
816
  }
814
817
  };
@@ -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-0c0a69f6.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-1afc5580.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-0c0a69f6.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-1afc5580.js';
2
2
  import '../types-3e5768ec.js';
3
3
  import '@sinclair/typebox';
@@ -5,7 +5,7 @@ import {
5
5
  ServerTransport,
6
6
  Session,
7
7
  Transport
8
- } from "../chunk-CT57CKBS.js";
8
+ } from "../chunk-2PKONGC3.js";
9
9
  import "../chunk-GZ7HCLLM.js";
10
10
  import {
11
11
  OpaqueTransportMessageSchema,
@@ -1,7 +1,7 @@
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-0c0a69f6.js';
4
+ import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-1afc5580.js';
5
5
  import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema } from '../builder-c593de11.js';
6
6
  import { Static } from '@sinclair/typebox';
7
7
  import net from 'node:net';
@@ -1,7 +1,7 @@
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-0c0a69f6.js';
4
+ import { P as PartialTransportMessage, T as Transport, C as Connection, O as OpaqueTransportMessage } from '../index-1afc5580.js';
5
5
  import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema } from '../builder-c593de11.js';
6
6
  import { Static } from '@sinclair/typebox';
7
7
  import net from 'node:net';
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  UNCAUGHT_ERROR,
3
3
  pushable
4
- } from "../chunk-55XUAPC6.js";
4
+ } from "../chunk-AFLZ6INU.js";
5
5
  import {
6
6
  coerceErrorString
7
7
  } from "../chunk-XFFS4UOD.js";
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.12.2",
4
+ "version": "0.12.3",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": {