@replit/river 0.12.2 → 0.12.4

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 +38 -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 +9 -4
  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
@@ -204,17 +204,27 @@ var Session = class {
204
204
  this.cancelGrace();
205
205
  this.connection = newConn;
206
206
  }
207
+ graceCb;
207
208
  beginGrace(cb) {
209
+ this.graceCb = cb;
208
210
  this.disconnectionGrace = setTimeout(() => {
209
- this.resetBufferedMessages();
210
- clearInterval(this.heartbeat);
211
+ this.close();
211
212
  cb();
212
213
  }, SESSION_DISCONNECT_GRACE_MS);
213
214
  }
215
+ // called on reconnect of the underlying session
214
216
  cancelGrace() {
215
217
  this.heartbeatMisses = 0;
216
218
  clearTimeout(this.disconnectionGrace);
217
219
  }
220
+ // closed when we want to discard the whole session
221
+ // (i.e. shutdown or session disconnect)
222
+ close() {
223
+ this.closeStaleConnection(this.connection);
224
+ this.cancelGrace();
225
+ clearInterval(this.heartbeat);
226
+ this.resetBufferedMessages();
227
+ }
218
228
  get connected() {
219
229
  return this.connection !== void 0;
220
230
  }
@@ -233,16 +243,6 @@ var Session = class {
233
243
  this.seq++;
234
244
  return msg;
235
245
  }
236
- /**
237
- * Closes the out-going connection but doesn't remove the listeners
238
- * for incoming messages. The connection will eventually call onClose
239
- * when it is ready to be cleaned up and only then will {@link connection} be set back
240
- * to undefined
241
- */
242
- halfCloseConnection() {
243
- this.connection?.close();
244
- clearInterval(this.heartbeat);
245
- }
246
246
  inspectSendBuffer() {
247
247
  return this.sendBuffer;
248
248
  }
@@ -333,19 +333,18 @@ var Transport = class {
333
333
  status: "connect",
334
334
  conn
335
335
  });
336
- let session = this.sessions.get(connectedTo);
336
+ let oldSession = this.sessions.get(connectedTo);
337
337
  const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
338
- if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
338
+ if (oldSession && lastInstanceId !== void 0 && lastInstanceId !== instanceId) {
339
339
  log?.warn(
340
- `${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
340
+ `${this.clientId} -- handshake from ${connectedTo} is a different instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
341
341
  );
342
- session.resetBufferedMessages();
343
- session.closeStaleConnection(conn);
344
- this.deleteSession(session);
345
- session = void 0;
342
+ oldSession.close();
343
+ this.deleteSession(oldSession);
344
+ oldSession = void 0;
346
345
  }
347
346
  this.connectedInstanceIds.set(connectedTo, instanceId);
348
- if (session === void 0) {
347
+ if (oldSession === void 0) {
349
348
  const newSession = this.createSession(connectedTo, conn);
350
349
  log?.info(
351
350
  `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
@@ -353,11 +352,11 @@ var Transport = class {
353
352
  return newSession;
354
353
  }
355
354
  log?.info(
356
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
355
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.debugId}) to ${connectedTo}`
357
356
  );
358
- session.replaceWithNewConnection(conn);
359
- session.sendBufferedMessages();
360
- return session;
357
+ oldSession.replaceWithNewConnection(conn);
358
+ oldSession.sendBufferedMessages();
359
+ return oldSession;
361
360
  }
362
361
  createSession(connectedTo, conn) {
363
362
  const session = new Session(
@@ -386,13 +385,14 @@ var Transport = class {
386
385
  /**
387
386
  * The downstream implementation needs to call this when a connection is closed.
388
387
  * @param conn The connection object.
388
+ * @param connectedTo The peer we are connected to.
389
389
  */
390
390
  onDisconnect(conn, connectedTo) {
391
391
  this.eventDispatcher.dispatchEvent("connectionStatus", {
392
392
  status: "disconnect",
393
393
  conn
394
394
  });
395
- if (!connectedTo)
395
+ if (this.state !== "open")
396
396
  return;
397
397
  const session = this.sessionByClientId(connectedTo);
398
398
  log?.info(
@@ -435,6 +435,8 @@ var Transport = class {
435
435
  * @param msg The received message.
436
436
  */
437
437
  handleMsg(msg) {
438
+ if (this.state !== "open")
439
+ return;
438
440
  const session = this.sessionByClientId(msg.from);
439
441
  session.cancelGrace();
440
442
  log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
@@ -511,10 +513,11 @@ var Transport = class {
511
513
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
512
514
  */
513
515
  close() {
516
+ this.state = "closed";
514
517
  for (const session of this.sessions.values()) {
515
- session.halfCloseConnection();
518
+ session.close();
519
+ this.deleteSession(session);
516
520
  }
517
- this.state = "closed";
518
521
  log?.info(`${this.clientId} -- manually closed transport`);
519
522
  }
520
523
  /**
@@ -523,10 +526,11 @@ var Transport = class {
523
526
  * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
524
527
  */
525
528
  destroy() {
529
+ this.state = "destroyed";
526
530
  for (const session of this.sessions.values()) {
527
- session.closeStaleConnection(session.connection);
531
+ session.close();
532
+ this.deleteSession(session);
528
533
  }
529
- this.state = "destroyed";
530
534
  log?.info(`${this.clientId} -- manually destroyed transport`);
531
535
  }
532
536
  };
@@ -632,8 +636,7 @@ var ClientTransport = class extends Transport {
632
636
  return bootHandler;
633
637
  }
634
638
  onDisconnect(conn, connectedTo) {
635
- if (connectedTo)
636
- this.inflightConnectionPromises.delete(connectedTo);
639
+ this.inflightConnectionPromises.delete(connectedTo);
637
640
  super.onDisconnect(conn, connectedTo);
638
641
  }
639
642
  };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Connection
3
- } from "./chunk-CT57CKBS.js";
3
+ } from "./chunk-2PKONGC3.js";
4
4
 
5
5
  // transport/impls/ws/connection.ts
6
6
  var WebSocketConnection = class extends Connection {
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-IIBVKYDB.js";
4
4
  import {
5
5
  Connection
6
- } from "./chunk-CT57CKBS.js";
6
+ } from "./chunk-2PKONGC3.js";
7
7
 
8
8
  // transport/impls/uds/connection.ts
9
9
  var UdsConnection = class extends Connection {
@@ -756,7 +756,7 @@ var RiverServer = class {
756
756
  }
757
757
  const disconnectedClientId = evt.session.to;
758
758
  log?.info(
759
- `${this.transport.clientId} -- got unexpected disconnect from ${disconnectedClientId}, cleaning up streams`
759
+ `${this.transport.clientId} -- got session disconnect from ${disconnectedClientId}, cleaning up streams`
760
760
  );
761
761
  const streamsFromThisClient = this.clientStreams.get(disconnectedClientId);
762
762
  if (!streamsFromThisClient) {
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-IIBVKYDB.js";
4
4
  import {
5
5
  Connection
6
- } from "./chunk-CT57CKBS.js";
6
+ } from "./chunk-2PKONGC3.js";
7
7
 
8
8
  // transport/impls/stdio/connection.ts
9
9
  var StreamConnection = class extends Connection {
@@ -1,5 +1,5 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { C as Connection } from './index-0c0a69f6.js';
2
+ import { C as Connection } from './index-1afc5580.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-0c0a69f6.js';
1
+ import { C as Connection } from './index-1afc5580.js';
2
2
  import { U as Uint32LengthPrefixFraming } from './messageFraming-b200ef25.js';
3
3
 
4
4
  declare class StreamConnection extends Connection {
@@ -1,4 +1,4 @@
1
- import { C as Connection } from './index-0c0a69f6.js';
1
+ import { C as Connection } from './index-1afc5580.js';
2
2
  import { Socket } from 'node:net';
3
3
  import { U as Uint32LengthPrefixFraming } from './messageFraming-b200ef25.js';
4
4
 
@@ -214,18 +214,13 @@ declare class Session<ConnType extends Connection> {
214
214
  addToSendBuff(msg: TransportMessage): void;
215
215
  closeStaleConnection(conn?: ConnType): void;
216
216
  replaceWithNewConnection(newConn: ConnType): void;
217
+ graceCb?: () => unknown;
217
218
  beginGrace(cb: () => void): void;
218
219
  cancelGrace(): void;
220
+ close(): void;
219
221
  get connected(): boolean;
220
222
  get nextExpectedSeq(): number;
221
223
  constructMsg<Payload extends Record<string, unknown>>(partialMsg: PartialTransportMessage<Payload>): TransportMessage<Payload>;
222
- /**
223
- * Closes the out-going connection but doesn't remove the listeners
224
- * for incoming messages. The connection will eventually call onClose
225
- * when it is ready to be cleaned up and only then will {@link connection} be set back
226
- * to undefined
227
- */
228
- halfCloseConnection(): void;
229
224
  inspectSendBuffer(): ReadonlyArray<OpaqueTransportMessage>;
230
225
  }
231
226
 
@@ -359,8 +354,9 @@ declare abstract class Transport<ConnType extends Connection> {
359
354
  /**
360
355
  * The downstream implementation needs to call this when a connection is closed.
361
356
  * @param conn The connection object.
357
+ * @param connectedTo The peer we are connected to.
362
358
  */
363
- onDisconnect(conn: ConnType, connectedTo: TransportClientId | undefined): void;
359
+ onDisconnect(conn: ConnType, connectedTo: TransportClientId): void;
364
360
  /**
365
361
  * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
366
362
  * @param msg The message to parse.
@@ -429,7 +425,7 @@ declare abstract class ClientTransport<ConnType extends Connection> extends Tran
429
425
  */
430
426
  connect(to: TransportClientId, attempt?: number): Promise<void>;
431
427
  receiveWithBootSequence(conn: ConnType, sessionCb: (sess: Session<ConnType>) => void): (data: Uint8Array) => void;
432
- onDisconnect(conn: ConnType, connectedTo: string | undefined): void;
428
+ onDisconnect(conn: ConnType, connectedTo: string): void;
433
429
  }
434
430
  declare abstract class ServerTransport<ConnType extends Connection> extends Transport<ConnType> {
435
431
  constructor(clientId: TransportClientId, providedOptions?: Partial<TransportOptions>);
@@ -853,7 +853,7 @@ var RiverServer = class {
853
853
  }
854
854
  const disconnectedClientId = evt.session.to;
855
855
  log?.info(
856
- `${this.transport.clientId} -- got unexpected disconnect from ${disconnectedClientId}, cleaning up streams`
856
+ `${this.transport.clientId} -- got session disconnect from ${disconnectedClientId}, cleaning up streams`
857
857
  );
858
858
  const streamsFromThisClient = this.clientStreams.get(disconnectedClientId);
859
859
  if (!streamsFromThisClient) {
@@ -1,6 +1,6 @@
1
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-c593de11.js';
2
2
  export { E as Err, O as Ok, m as ProcHandler, k as ProcListing, a as Procedure, o as RiverErrorSchema, c as RiverUncaughtSchema, l as Service, j as ServiceBuilder, n as ServiceContextWithState, U as UNCAUGHT_ERROR, V as ValidProcType, s as serializeService } from '../builder-c593de11.js';
3
- import { T as Transport, C as Connection, b as TransportClientId } from '../index-0c0a69f6.js';
3
+ import { T as Transport, C as Connection, b as TransportClientId } from '../index-1afc5580.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
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-c593de11.js';
2
2
  export { E as Err, O as Ok, m as ProcHandler, k as ProcListing, a as Procedure, o as RiverErrorSchema, c as RiverUncaughtSchema, l as Service, j as ServiceBuilder, n as ServiceContextWithState, U as UNCAUGHT_ERROR, V as ValidProcType, s as serializeService } from '../builder-c593de11.js';
3
- import { T as Transport, C as Connection, b as TransportClientId } from '../index-0c0a69f6.js';
3
+ import { T as Transport, C as Connection, b as TransportClientId } from '../index-1afc5580.js';
4
4
  import { Pushable } from 'it-pushable';
5
5
  import { Static } from '@sinclair/typebox';
6
6
  import '../types-3e5768ec.js';
@@ -8,7 +8,7 @@ import {
8
8
  createClient,
9
9
  createServer,
10
10
  serializeService
11
- } from "../chunk-55XUAPC6.js";
11
+ } from "../chunk-AFLZ6INU.js";
12
12
  import "../chunk-XFFS4UOD.js";
13
13
  import "../chunk-H4BYJELI.js";
14
14
  export {
@@ -288,17 +288,27 @@ var Session = class {
288
288
  this.cancelGrace();
289
289
  this.connection = newConn;
290
290
  }
291
+ graceCb;
291
292
  beginGrace(cb) {
293
+ this.graceCb = cb;
292
294
  this.disconnectionGrace = setTimeout(() => {
293
- this.resetBufferedMessages();
294
- clearInterval(this.heartbeat);
295
+ this.close();
295
296
  cb();
296
297
  }, SESSION_DISCONNECT_GRACE_MS);
297
298
  }
299
+ // called on reconnect of the underlying session
298
300
  cancelGrace() {
299
301
  this.heartbeatMisses = 0;
300
302
  clearTimeout(this.disconnectionGrace);
301
303
  }
304
+ // closed when we want to discard the whole session
305
+ // (i.e. shutdown or session disconnect)
306
+ close() {
307
+ this.closeStaleConnection(this.connection);
308
+ this.cancelGrace();
309
+ clearInterval(this.heartbeat);
310
+ this.resetBufferedMessages();
311
+ }
302
312
  get connected() {
303
313
  return this.connection !== void 0;
304
314
  }
@@ -317,16 +327,6 @@ var Session = class {
317
327
  this.seq++;
318
328
  return msg;
319
329
  }
320
- /**
321
- * Closes the out-going connection but doesn't remove the listeners
322
- * for incoming messages. The connection will eventually call onClose
323
- * when it is ready to be cleaned up and only then will {@link connection} be set back
324
- * to undefined
325
- */
326
- halfCloseConnection() {
327
- this.connection?.close();
328
- clearInterval(this.heartbeat);
329
- }
330
330
  inspectSendBuffer() {
331
331
  return this.sendBuffer;
332
332
  }
@@ -478,19 +478,18 @@ var Transport = class {
478
478
  status: "connect",
479
479
  conn
480
480
  });
481
- let session = this.sessions.get(connectedTo);
481
+ let oldSession = this.sessions.get(connectedTo);
482
482
  const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
483
- if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
483
+ if (oldSession && lastInstanceId !== void 0 && lastInstanceId !== instanceId) {
484
484
  log?.warn(
485
- `${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
485
+ `${this.clientId} -- handshake from ${connectedTo} is a different instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
486
486
  );
487
- session.resetBufferedMessages();
488
- session.closeStaleConnection(conn);
489
- this.deleteSession(session);
490
- session = void 0;
487
+ oldSession.close();
488
+ this.deleteSession(oldSession);
489
+ oldSession = void 0;
491
490
  }
492
491
  this.connectedInstanceIds.set(connectedTo, instanceId);
493
- if (session === void 0) {
492
+ if (oldSession === void 0) {
494
493
  const newSession = this.createSession(connectedTo, conn);
495
494
  log?.info(
496
495
  `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
@@ -498,11 +497,11 @@ var Transport = class {
498
497
  return newSession;
499
498
  }
500
499
  log?.info(
501
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
500
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.debugId}) to ${connectedTo}`
502
501
  );
503
- session.replaceWithNewConnection(conn);
504
- session.sendBufferedMessages();
505
- return session;
502
+ oldSession.replaceWithNewConnection(conn);
503
+ oldSession.sendBufferedMessages();
504
+ return oldSession;
506
505
  }
507
506
  createSession(connectedTo, conn) {
508
507
  const session = new Session(
@@ -531,13 +530,14 @@ var Transport = class {
531
530
  /**
532
531
  * The downstream implementation needs to call this when a connection is closed.
533
532
  * @param conn The connection object.
533
+ * @param connectedTo The peer we are connected to.
534
534
  */
535
535
  onDisconnect(conn, connectedTo) {
536
536
  this.eventDispatcher.dispatchEvent("connectionStatus", {
537
537
  status: "disconnect",
538
538
  conn
539
539
  });
540
- if (!connectedTo)
540
+ if (this.state !== "open")
541
541
  return;
542
542
  const session = this.sessionByClientId(connectedTo);
543
543
  log?.info(
@@ -580,6 +580,8 @@ var Transport = class {
580
580
  * @param msg The received message.
581
581
  */
582
582
  handleMsg(msg) {
583
+ if (this.state !== "open")
584
+ return;
583
585
  const session = this.sessionByClientId(msg.from);
584
586
  session.cancelGrace();
585
587
  log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
@@ -656,10 +658,11 @@ var Transport = class {
656
658
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
657
659
  */
658
660
  close() {
661
+ this.state = "closed";
659
662
  for (const session of this.sessions.values()) {
660
- session.halfCloseConnection();
663
+ session.close();
664
+ this.deleteSession(session);
661
665
  }
662
- this.state = "closed";
663
666
  log?.info(`${this.clientId} -- manually closed transport`);
664
667
  }
665
668
  /**
@@ -668,10 +671,11 @@ var Transport = class {
668
671
  * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
669
672
  */
670
673
  destroy() {
674
+ this.state = "destroyed";
671
675
  for (const session of this.sessions.values()) {
672
- session.closeStaleConnection(session.connection);
676
+ session.close();
677
+ this.deleteSession(session);
673
678
  }
674
- this.state = "destroyed";
675
679
  log?.info(`${this.clientId} -- manually destroyed transport`);
676
680
  }
677
681
  };
@@ -777,8 +781,7 @@ var ClientTransport = class extends Transport {
777
781
  return bootHandler;
778
782
  }
779
783
  onDisconnect(conn, connectedTo) {
780
- if (connectedTo)
781
- this.inflightConnectionPromises.delete(connectedTo);
784
+ this.inflightConnectionPromises.delete(connectedTo);
782
785
  super.onDisconnect(conn, connectedTo);
783
786
  }
784
787
  };
@@ -1,5 +1,5 @@
1
- import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
2
- import { S as StreamConnection } from '../../../connection-bf7811aa.js';
1
+ import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
2
+ import { S as StreamConnection } from '../../../connection-64b9e0ff.js';
3
3
  import '../../../types-3e5768ec.js';
4
4
  import '@sinclair/typebox';
5
5
  import '../../../messageFraming-b200ef25.js';
@@ -1,5 +1,5 @@
1
- import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
2
- import { S as StreamConnection } from '../../../connection-bf7811aa.js';
1
+ import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
2
+ import { S as StreamConnection } from '../../../connection-64b9e0ff.js';
3
3
  import '../../../types-3e5768ec.js';
4
4
  import '@sinclair/typebox';
5
5
  import '../../../messageFraming-b200ef25.js';
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  StreamConnection
3
- } from "../../../chunk-R6YJHJV7.js";
3
+ } from "../../../chunk-H4QR2K3T.js";
4
4
  import "../../../chunk-5IZ2UHWV.js";
5
5
  import "../../../chunk-IIBVKYDB.js";
6
6
  import {
7
7
  ClientTransport
8
- } from "../../../chunk-CT57CKBS.js";
8
+ } from "../../../chunk-2PKONGC3.js";
9
9
  import "../../../chunk-GZ7HCLLM.js";
10
10
  import "../../../chunk-XFFS4UOD.js";
11
11
  import {
@@ -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,5 +1,5 @@
1
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
2
- import { S as StreamConnection } from '../../../connection-bf7811aa.js';
1
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
2
+ import { S as StreamConnection } from '../../../connection-64b9e0ff.js';
3
3
  import '../../../types-3e5768ec.js';
4
4
  import '@sinclair/typebox';
5
5
  import '../../../messageFraming-b200ef25.js';
@@ -1,5 +1,5 @@
1
- import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
2
- import { S as StreamConnection } from '../../../connection-bf7811aa.js';
1
+ import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-1afc5580.js';
2
+ import { S as StreamConnection } from '../../../connection-64b9e0ff.js';
3
3
  import '../../../types-3e5768ec.js';
4
4
  import '@sinclair/typebox';
5
5
  import '../../../messageFraming-b200ef25.js';
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  StreamConnection
3
- } from "../../../chunk-R6YJHJV7.js";
3
+ } from "../../../chunk-H4QR2K3T.js";
4
4
  import "../../../chunk-5IZ2UHWV.js";
5
5
  import "../../../chunk-IIBVKYDB.js";
6
6
  import {
7
7
  ServerTransport
8
- } from "../../../chunk-CT57CKBS.js";
8
+ } from "../../../chunk-2PKONGC3.js";
9
9
  import "../../../chunk-GZ7HCLLM.js";
10
10
  import "../../../chunk-XFFS4UOD.js";
11
11
  import "../../../chunk-H4BYJELI.js";