@replit/river 0.23.9 → 0.23.11

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 (54) hide show
  1. package/dist/{chunk-YGW5PYGV.js → chunk-EOJMKMDO.js} +55 -73
  2. package/dist/chunk-EOJMKMDO.js.map +1 -0
  3. package/dist/{chunk-XKVIPKMR.js → chunk-HM7VDTDJ.js} +2 -2
  4. package/dist/{chunk-XUVTMFTJ.js → chunk-MEHCOYKJ.js} +2 -2
  5. package/dist/chunk-MEHCOYKJ.js.map +1 -0
  6. package/dist/{chunk-KIWWKSRI.js → chunk-T6YEMFUF.js} +2 -2
  7. package/dist/{chunk-U4CSF3SQ.js → chunk-ZSKCZYVU.js} +473 -15
  8. package/dist/chunk-ZSKCZYVU.js.map +1 -0
  9. package/dist/{connection-afa08cf1.d.ts → connection-261eee8f.d.ts} +1 -1
  10. package/dist/{connection-f5fb7805.d.ts → connection-c1eeb95d.d.ts} +1 -1
  11. package/dist/router/index.cjs +473 -14
  12. package/dist/router/index.cjs.map +1 -1
  13. package/dist/router/index.d.cts +45 -7
  14. package/dist/router/index.d.ts +45 -7
  15. package/dist/router/index.js +4 -2
  16. package/dist/{services-49b1b43d.d.ts → services-524bab79.d.ts} +2 -2
  17. package/dist/transport/impls/uds/client.cjs +52 -70
  18. package/dist/transport/impls/uds/client.cjs.map +1 -1
  19. package/dist/transport/impls/uds/client.d.cts +2 -2
  20. package/dist/transport/impls/uds/client.d.ts +2 -2
  21. package/dist/transport/impls/uds/client.js +3 -3
  22. package/dist/transport/impls/uds/server.cjs +31 -60
  23. package/dist/transport/impls/uds/server.cjs.map +1 -1
  24. package/dist/transport/impls/uds/server.d.cts +2 -2
  25. package/dist/transport/impls/uds/server.d.ts +2 -2
  26. package/dist/transport/impls/uds/server.js +3 -3
  27. package/dist/transport/impls/ws/client.cjs +55 -70
  28. package/dist/transport/impls/ws/client.cjs.map +1 -1
  29. package/dist/transport/impls/ws/client.d.cts +2 -2
  30. package/dist/transport/impls/ws/client.d.ts +2 -2
  31. package/dist/transport/impls/ws/client.js +6 -3
  32. package/dist/transport/impls/ws/client.js.map +1 -1
  33. package/dist/transport/impls/ws/server.cjs +31 -60
  34. package/dist/transport/impls/ws/server.cjs.map +1 -1
  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 +3 -3
  38. package/dist/transport/index.cjs +54 -72
  39. package/dist/transport/index.cjs.map +1 -1
  40. package/dist/transport/index.d.cts +1 -1
  41. package/dist/transport/index.d.ts +1 -1
  42. package/dist/transport/index.js +2 -2
  43. package/dist/{transport-3d0ba1da.d.ts → transport-c8f36f6d.d.ts} +15 -21
  44. package/dist/util/testHelpers.cjs +4 -21
  45. package/dist/util/testHelpers.cjs.map +1 -1
  46. package/dist/util/testHelpers.d.cts +2 -2
  47. package/dist/util/testHelpers.d.ts +2 -2
  48. package/dist/util/testHelpers.js +3 -3
  49. package/package.json +3 -3
  50. package/dist/chunk-U4CSF3SQ.js.map +0 -1
  51. package/dist/chunk-XUVTMFTJ.js.map +0 -1
  52. package/dist/chunk-YGW5PYGV.js.map +0 -1
  53. /package/dist/{chunk-XKVIPKMR.js.map → chunk-HM7VDTDJ.js.map} +0 -0
  54. /package/dist/{chunk-KIWWKSRI.js.map → chunk-T6YEMFUF.js.map} +0 -0
@@ -1,14 +1,53 @@
1
- import { A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, P as PayloadType, b as Result, R as RiverError } from '../services-49b1b43d.js';
2
- export { C as Client, E as Err, O as Ok, z as Output, j as ProcErrors, f as ProcHandler, g as ProcInit, h as ProcInput, i as ProcOutput, k as ProcType, a as Procedure, p as ProcedureMap, d as ProcedureResult, q as RPCProcedure, y as ResultUnwrapErr, x as ResultUnwrapOk, w as RiverErrorSchema, c as RiverUncaughtSchema, o as SerializedProcedureSchema, m as SerializedServerSchema, n as SerializedServiceSchema, S as Service, e as ServiceConfiguration, l as ServiceSchema, t as StreamProcedure, r as SubscriptionProcedure, v as UNCAUGHT_ERROR, U as UploadProcedure, V as ValidProcType, u as createClient, s as serializeSchema } from '../services-49b1b43d.js';
1
+ import { S as SerializedServerSchema, A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, P as PayloadType, b as Result, R as RiverError } from '../services-524bab79.js';
2
+ export { C as Client, E as Err, O as Ok, z as Output, k as ProcErrors, g as ProcHandler, h as ProcInit, i as ProcInput, j as ProcOutput, l as ProcType, a as Procedure, p as ProcedureMap, d as ProcedureResult, q as RPCProcedure, y as ResultUnwrapErr, x as ResultUnwrapOk, w as RiverErrorSchema, c as RiverUncaughtSchema, o as SerializedProcedureSchema, n as SerializedServiceSchema, e as Service, f as ServiceConfiguration, m as ServiceSchema, t as StreamProcedure, r as SubscriptionProcedure, v as UNCAUGHT_ERROR, U as UploadProcedure, V as ValidProcType, u as createClient, s as serializeSchema } from '../services-524bab79.js';
3
3
  import { Static } from '@sinclair/typebox';
4
- import { d as ServerTransport, C as Connection, f as ServerHandshakeOptions, b as ServiceContext } from '../transport-3d0ba1da.js';
5
- export { g as ParsedMetadata, h as ServiceContextWithState, i as ServiceContextWithTransportInfo, j as createClientHandshakeOptions, k as createServerHandshakeOptions } from '../transport-3d0ba1da.js';
4
+ import { d as ServerTransport, C as Connection, f as ServerHandshakeOptions, b as ServiceContext } from '../transport-c8f36f6d.js';
5
+ export { g as ParsedMetadata, h as ServiceContextWithState, i as ServiceContextWithTransportInfo, j as createClientHandshakeOptions, k as createServerHandshakeOptions } from '../transport-c8f36f6d.js';
6
6
  import { Pushable } from 'it-pushable';
7
7
  import '../index-60f03cb7.js';
8
8
  import '@sinclair/typebox/value';
9
9
  import '@opentelemetry/api';
10
10
  import '../types-3e5768ec.js';
11
11
 
12
+ interface ServerBreakage {
13
+ serviceBreakages: Record<string, ServiceBreakage>;
14
+ }
15
+ type ServiceBreakage = {
16
+ reason: 'removed';
17
+ } | {
18
+ reason: 'modified';
19
+ procedureBreakages: Record<string, ProcedureBreakage>;
20
+ };
21
+ type ProcedureBreakage = {
22
+ reason: 'removed';
23
+ } | {
24
+ reason: 'type-changed';
25
+ oldType: string;
26
+ newType: string;
27
+ } | {
28
+ reason: 'modified';
29
+ input?: PayloadBreakage;
30
+ init?: PayloadBreakage;
31
+ output?: PayloadBreakage;
32
+ };
33
+ type PayloadBreakage = {
34
+ reason: 'type-changed';
35
+ oldType: string;
36
+ newType: string;
37
+ } | {
38
+ reason: 'new-required';
39
+ } | {
40
+ reason: 'removed-required';
41
+ } | {
42
+ reason: 'field-breakage';
43
+ fieldBreakages: Record<string, PayloadBreakage>;
44
+ };
45
+ interface DiffOptions {
46
+ allowServiceRemoval?: boolean;
47
+ allowProcedureRemoval?: boolean;
48
+ }
49
+ declare function diffServerSchema(oldServer: SerializedServerSchema, newServer: SerializedServerSchema, options?: DiffOptions): ServerBreakage | null;
50
+
12
51
  /**
13
52
  * Represents a server with a set of services. Use {@link createServer} to create it.
14
53
  * @template Services - The type of services provided by the server.
@@ -16,7 +55,6 @@ import '../types-3e5768ec.js';
16
55
  interface Server<Services extends AnyServiceSchemaMap> {
17
56
  services: InstantiatedServiceSchemaMap<Services>;
18
57
  streams: Map<string, ProcStream>;
19
- close(): Promise<void>;
20
58
  }
21
59
  interface ProcStream {
22
60
  id: string;
@@ -43,6 +81,6 @@ declare function createServer<Services extends AnyServiceSchemaMap>(transport: S
43
81
  extendedContext?: Omit<ServiceContext, 'state'>;
44
82
  }>): Server<Services>;
45
83
 
46
- var version = "0.23.9";
84
+ var version = "0.23.11";
47
85
 
48
- export { PayloadType, version as RIVER_VERSION, Result, RiverError, Server, ServiceContext, createServer };
86
+ export { DiffOptions, PayloadBreakage, PayloadType, ProcedureBreakage, version as RIVER_VERSION, Result, RiverError, SerializedServerSchema, Server, ServerBreakage, ServiceBreakage, ServiceContext, createServer, diffServerSchema };
@@ -1,14 +1,53 @@
1
- import { A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, P as PayloadType, b as Result, R as RiverError } from '../services-49b1b43d.js';
2
- export { C as Client, E as Err, O as Ok, z as Output, j as ProcErrors, f as ProcHandler, g as ProcInit, h as ProcInput, i as ProcOutput, k as ProcType, a as Procedure, p as ProcedureMap, d as ProcedureResult, q as RPCProcedure, y as ResultUnwrapErr, x as ResultUnwrapOk, w as RiverErrorSchema, c as RiverUncaughtSchema, o as SerializedProcedureSchema, m as SerializedServerSchema, n as SerializedServiceSchema, S as Service, e as ServiceConfiguration, l as ServiceSchema, t as StreamProcedure, r as SubscriptionProcedure, v as UNCAUGHT_ERROR, U as UploadProcedure, V as ValidProcType, u as createClient, s as serializeSchema } from '../services-49b1b43d.js';
1
+ import { S as SerializedServerSchema, A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, P as PayloadType, b as Result, R as RiverError } from '../services-524bab79.js';
2
+ export { C as Client, E as Err, O as Ok, z as Output, k as ProcErrors, g as ProcHandler, h as ProcInit, i as ProcInput, j as ProcOutput, l as ProcType, a as Procedure, p as ProcedureMap, d as ProcedureResult, q as RPCProcedure, y as ResultUnwrapErr, x as ResultUnwrapOk, w as RiverErrorSchema, c as RiverUncaughtSchema, o as SerializedProcedureSchema, n as SerializedServiceSchema, e as Service, f as ServiceConfiguration, m as ServiceSchema, t as StreamProcedure, r as SubscriptionProcedure, v as UNCAUGHT_ERROR, U as UploadProcedure, V as ValidProcType, u as createClient, s as serializeSchema } from '../services-524bab79.js';
3
3
  import { Static } from '@sinclair/typebox';
4
- import { d as ServerTransport, C as Connection, f as ServerHandshakeOptions, b as ServiceContext } from '../transport-3d0ba1da.js';
5
- export { g as ParsedMetadata, h as ServiceContextWithState, i as ServiceContextWithTransportInfo, j as createClientHandshakeOptions, k as createServerHandshakeOptions } from '../transport-3d0ba1da.js';
4
+ import { d as ServerTransport, C as Connection, f as ServerHandshakeOptions, b as ServiceContext } from '../transport-c8f36f6d.js';
5
+ export { g as ParsedMetadata, h as ServiceContextWithState, i as ServiceContextWithTransportInfo, j as createClientHandshakeOptions, k as createServerHandshakeOptions } from '../transport-c8f36f6d.js';
6
6
  import { Pushable } from 'it-pushable';
7
7
  import '../index-60f03cb7.js';
8
8
  import '@sinclair/typebox/value';
9
9
  import '@opentelemetry/api';
10
10
  import '../types-3e5768ec.js';
11
11
 
12
+ interface ServerBreakage {
13
+ serviceBreakages: Record<string, ServiceBreakage>;
14
+ }
15
+ type ServiceBreakage = {
16
+ reason: 'removed';
17
+ } | {
18
+ reason: 'modified';
19
+ procedureBreakages: Record<string, ProcedureBreakage>;
20
+ };
21
+ type ProcedureBreakage = {
22
+ reason: 'removed';
23
+ } | {
24
+ reason: 'type-changed';
25
+ oldType: string;
26
+ newType: string;
27
+ } | {
28
+ reason: 'modified';
29
+ input?: PayloadBreakage;
30
+ init?: PayloadBreakage;
31
+ output?: PayloadBreakage;
32
+ };
33
+ type PayloadBreakage = {
34
+ reason: 'type-changed';
35
+ oldType: string;
36
+ newType: string;
37
+ } | {
38
+ reason: 'new-required';
39
+ } | {
40
+ reason: 'removed-required';
41
+ } | {
42
+ reason: 'field-breakage';
43
+ fieldBreakages: Record<string, PayloadBreakage>;
44
+ };
45
+ interface DiffOptions {
46
+ allowServiceRemoval?: boolean;
47
+ allowProcedureRemoval?: boolean;
48
+ }
49
+ declare function diffServerSchema(oldServer: SerializedServerSchema, newServer: SerializedServerSchema, options?: DiffOptions): ServerBreakage | null;
50
+
12
51
  /**
13
52
  * Represents a server with a set of services. Use {@link createServer} to create it.
14
53
  * @template Services - The type of services provided by the server.
@@ -16,7 +55,6 @@ import '../types-3e5768ec.js';
16
55
  interface Server<Services extends AnyServiceSchemaMap> {
17
56
  services: InstantiatedServiceSchemaMap<Services>;
18
57
  streams: Map<string, ProcStream>;
19
- close(): Promise<void>;
20
58
  }
21
59
  interface ProcStream {
22
60
  id: string;
@@ -43,6 +81,6 @@ declare function createServer<Services extends AnyServiceSchemaMap>(transport: S
43
81
  extendedContext?: Omit<ServiceContext, 'state'>;
44
82
  }>): Server<Services>;
45
83
 
46
- var version = "0.23.9";
84
+ var version = "0.23.11";
47
85
 
48
- export { PayloadType, version as RIVER_VERSION, Result, RiverError, Server, ServiceContext, createServer };
86
+ export { DiffOptions, PayloadBreakage, PayloadType, ProcedureBreakage, version as RIVER_VERSION, Result, RiverError, SerializedServerSchema, Server, ServerBreakage, ServiceBreakage, ServiceContext, createServer, diffServerSchema };
@@ -9,11 +9,12 @@ import {
9
9
  createClientHandshakeOptions,
10
10
  createServer,
11
11
  createServerHandshakeOptions,
12
+ diffServerSchema,
12
13
  serializeSchema
13
- } from "../chunk-U4CSF3SQ.js";
14
+ } from "../chunk-ZSKCZYVU.js";
14
15
  import {
15
16
  version
16
- } from "../chunk-XUVTMFTJ.js";
17
+ } from "../chunk-MEHCOYKJ.js";
17
18
  export {
18
19
  Err,
19
20
  Ok,
@@ -26,6 +27,7 @@ export {
26
27
  createClientHandshakeOptions,
27
28
  createServer,
28
29
  createServerHandshakeOptions,
30
+ diffServerSchema,
29
31
  serializeSchema
30
32
  };
31
33
  //# sourceMappingURL=index.js.map
@@ -1,5 +1,5 @@
1
1
  import { Static, TObject, TUnion, TString, TSchema, TNever, TLiteral } from '@sinclair/typebox';
2
- import { c as ClientTransport, C as Connection, l as ClientHandshakeOptions, i as ServiceContextWithTransportInfo } from './transport-3d0ba1da.js';
2
+ import { c as ClientTransport, C as Connection, l as ClientHandshakeOptions, i as ServiceContextWithTransportInfo } from './transport-c8f36f6d.js';
3
3
  import { Pushable } from 'it-pushable';
4
4
  import { T as TransportClientId } from './index-60f03cb7.js';
5
5
 
@@ -705,4 +705,4 @@ declare class ServiceScaffold<State extends object> {
705
705
  }>;
706
706
  }
707
707
 
708
- export { AnyServiceSchemaMap as A, Client as C, Err as E, InstantiatedServiceSchemaMap as I, Ok as O, PayloadType as P, RiverError as R, Service as S, UploadProcedure as U, ValidProcType as V, Procedure as a, Result as b, RiverUncaughtSchema as c, ProcedureResult as d, ServiceConfiguration as e, ProcHandler as f, ProcInit as g, ProcInput as h, ProcOutput as i, ProcErrors as j, ProcType as k, ServiceSchema as l, SerializedServerSchema as m, SerializedServiceSchema as n, SerializedProcedureSchema as o, ProcedureMap as p, RpcProcedure as q, SubscriptionProcedure as r, serializeSchema as s, StreamProcedure as t, createClient as u, UNCAUGHT_ERROR as v, RiverErrorSchema as w, ResultUnwrapOk as x, ResultUnwrapErr as y, Output as z };
708
+ export { AnyServiceSchemaMap as A, Client as C, Err as E, InstantiatedServiceSchemaMap as I, Ok as O, PayloadType as P, RiverError as R, SerializedServerSchema as S, UploadProcedure as U, ValidProcType as V, Procedure as a, Result as b, RiverUncaughtSchema as c, ProcedureResult as d, Service as e, ServiceConfiguration as f, ProcHandler as g, ProcInit as h, ProcInput as i, ProcOutput as j, ProcErrors as k, ProcType as l, ServiceSchema as m, SerializedServiceSchema as n, SerializedProcedureSchema as o, ProcedureMap as p, RpcProcedure as q, SubscriptionProcedure as r, serializeSchema as s, StreamProcedure as t, createClient as u, UNCAUGHT_ERROR as v, RiverErrorSchema as w, ResultUnwrapOk as x, ResultUnwrapErr as y, Output as z };
@@ -110,7 +110,7 @@ function isAck(controlFlag) {
110
110
  var import_api = require("@opentelemetry/api");
111
111
 
112
112
  // package.json
113
- var version = "0.23.9";
113
+ var version = "0.23.11";
114
114
 
115
115
  // tracing/index.ts
116
116
  function getPropagationContext(ctx) {
@@ -169,7 +169,7 @@ var Connection = class {
169
169
  get loggingMetadata() {
170
170
  const metadata = { connId: this.id };
171
171
  const spanContext = this.telemetry?.span.spanContext();
172
- if (spanContext) {
172
+ if (this.telemetry?.span.isRecording() && spanContext) {
173
173
  metadata.telemetry = {
174
174
  traceId: spanContext.traceId,
175
175
  spanId: spanContext.spanId
@@ -384,19 +384,8 @@ var Session = class {
384
384
  `starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
385
385
  this.loggingMetadata
386
386
  );
387
- this.cancelGrace({ keepHeartbeatMisses: true });
387
+ this.cancelGrace();
388
388
  this.disconnectionGrace = setTimeout(() => {
389
- if (this.connection !== void 0) {
390
- this.log?.warn(
391
- `grace period for ${this.to} elapsed while connected. not calling callback`,
392
- {
393
- ...this.loggingMetadata,
394
- connId: this.connection.id,
395
- tags: ["invariant-violation"]
396
- }
397
- );
398
- return;
399
- }
400
389
  this.log?.info(
401
390
  `grace period for ${this.to} elapsed`,
402
391
  this.loggingMetadata
@@ -405,14 +394,8 @@ var Session = class {
405
394
  }, this.options.sessionDisconnectGraceMs);
406
395
  }
407
396
  // called on reconnect of the underlying session
408
- cancelGrace({ keepHeartbeatMisses } = {
409
- keepHeartbeatMisses: false
410
- }) {
411
- if (!keepHeartbeatMisses) {
412
- this.heartbeatMisses = 0;
413
- }
414
- if (this.disconnectionGrace === void 0)
415
- return;
397
+ cancelGrace() {
398
+ this.heartbeatMisses = 0;
416
399
  clearTimeout(this.disconnectionGrace);
417
400
  this.disconnectionGrace = void 0;
418
401
  }
@@ -618,11 +601,13 @@ var createLogProxy = (log) => ({
618
601
  var ProtocolError = {
619
602
  RetriesExceeded: "conn_retry_exceeded",
620
603
  HandshakeFailed: "handshake_failed",
621
- UseAfterDestroy: "use_after_destroy",
622
604
  MessageOrderingViolated: "message_ordering_violated"
623
605
  };
624
606
  var EventDispatcher = class {
625
607
  eventListeners = {};
608
+ removeAllListeners() {
609
+ this.eventListeners = {};
610
+ }
626
611
  numberOfListeners(eventType) {
627
612
  return this.eventListeners[eventType]?.size ?? 0;
628
613
  }
@@ -641,7 +626,8 @@ var EventDispatcher = class {
641
626
  dispatchEvent(eventType, event) {
642
627
  const handlers = this.eventListeners[eventType];
643
628
  if (handlers) {
644
- for (const handler of handlers) {
629
+ const copy = [...handlers];
630
+ for (const handler of copy) {
645
631
  handler(event);
646
632
  }
647
633
  }
@@ -803,10 +789,9 @@ var defaultServerTransportOptions = {
803
789
  };
804
790
  var Transport = class {
805
791
  /**
806
- * A flag indicating whether the transport has been destroyed.
807
- * A destroyed transport will not attempt to reconnect and cannot be used again.
792
+ * The status of the transport.
808
793
  */
809
- state;
794
+ status;
810
795
  /**
811
796
  * The {@link Codec} used to encode and decode messages.
812
797
  */
@@ -848,7 +833,7 @@ var Transport = class {
848
833
  this.sessions = /* @__PURE__ */ new Map();
849
834
  this.codec = this.options.codec;
850
835
  this.clientId = clientId;
851
- this.state = "open";
836
+ this.status = "open";
852
837
  }
853
838
  bindLogger(fn, level) {
854
839
  if (typeof fn === "function") {
@@ -862,7 +847,7 @@ var Transport = class {
862
847
  * and we know the identity of the connected client.
863
848
  * @param conn The connection object.
864
849
  */
865
- onConnect(conn, connectedTo, session, isReconnect) {
850
+ onConnect(conn, session, isReconnect) {
866
851
  this.eventDispatcher.dispatchEvent("connectionStatus", {
867
852
  status: "connect",
868
853
  conn
@@ -870,13 +855,11 @@ var Transport = class {
870
855
  conn.telemetry = createConnectionTelemetryInfo(conn, session.telemetry);
871
856
  if (isReconnect) {
872
857
  session.replaceWithNewConnection(conn);
873
- this.log?.info(`reconnected to ${connectedTo}`, {
874
- ...conn.loggingMetadata,
875
- ...session.loggingMetadata,
876
- clientId: this.clientId,
877
- connectedTo
878
- });
879
858
  }
859
+ this.log?.info(`connected to ${session.to}`, {
860
+ ...conn.loggingMetadata,
861
+ ...session.loggingMetadata
862
+ });
880
863
  }
881
864
  createSession(to, conn, propagationCtx) {
882
865
  const session = new Session(
@@ -1010,7 +993,7 @@ var Transport = class {
1010
993
  * @param msg The received message.
1011
994
  */
1012
995
  handleMsg(msg, conn) {
1013
- if (this.state !== "open")
996
+ if (this.getStatus() !== "open")
1014
997
  return;
1015
998
  const session = this.sessions.get(msg.from);
1016
999
  if (!session) {
@@ -1089,21 +1072,14 @@ var Transport = class {
1089
1072
  * @returns The ID of the sent message or undefined if it wasn't sent
1090
1073
  */
1091
1074
  send(to, msg) {
1092
- if (this.state === "destroyed") {
1093
- const err = "transport is destroyed, cant send";
1075
+ if (this.getStatus() === "closed") {
1076
+ const err = "transport is closed, cant send";
1094
1077
  this.log?.error(err, {
1095
1078
  clientId: this.clientId,
1096
1079
  transportMessage: msg,
1097
1080
  tags: ["invariant-violation"]
1098
1081
  });
1099
- this.protocolError(ProtocolError.UseAfterDestroy, err);
1100
- return void 0;
1101
- } else if (this.state === "closed") {
1102
- this.log?.info(`transport closed when sending, discarding`, {
1103
- clientId: this.clientId,
1104
- transportMessage: msg
1105
- });
1106
- return void 0;
1082
+ throw new Error(err);
1107
1083
  }
1108
1084
  return this.getOrCreateSession({ to }).session.send(msg);
1109
1085
  }
@@ -1126,23 +1102,18 @@ var Transport = class {
1126
1102
  * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
1127
1103
  */
1128
1104
  close() {
1129
- this.state = "closed";
1105
+ this.status = "closed";
1130
1106
  for (const session of this.sessions.values()) {
1131
1107
  this.deleteSession({ session, closeHandshakingConnection: true });
1132
1108
  }
1109
+ this.eventDispatcher.dispatchEvent("transportStatus", {
1110
+ status: this.status
1111
+ });
1112
+ this.eventDispatcher.removeAllListeners();
1133
1113
  this.log?.info(`manually closed transport`, { clientId: this.clientId });
1134
1114
  }
1135
- /**
1136
- * Default destroy implementation for transports. You should override this in the downstream
1137
- * implementation if you need to do any additional cleanup and call super.destroy() at the end.
1138
- * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
1139
- */
1140
- destroy() {
1141
- this.state = "destroyed";
1142
- for (const session of this.sessions.values()) {
1143
- this.deleteSession({ session, closeHandshakingConnection: true });
1144
- }
1145
- this.log?.info(`manually destroyed transport`, { clientId: this.clientId });
1115
+ getStatus() {
1116
+ return this.status;
1146
1117
  }
1147
1118
  };
1148
1119
  var ClientTransport = class extends Transport {
@@ -1179,7 +1150,7 @@ var ClientTransport = class extends Transport {
1179
1150
  this.handshakeExtensions = options;
1180
1151
  }
1181
1152
  handleConnection(conn, to) {
1182
- if (this.state !== "open")
1153
+ if (this.getStatus() !== "open")
1183
1154
  return;
1184
1155
  let session = void 0;
1185
1156
  const handshakeTimeout = setTimeout(() => {
@@ -1219,12 +1190,16 @@ var ClientTransport = class extends Transport {
1219
1190
  if (session) {
1220
1191
  this.onDisconnect(conn, session);
1221
1192
  }
1222
- this.log?.info(`connection to ${to} disconnected`, {
1223
- ...conn.loggingMetadata,
1224
- ...session?.loggingMetadata,
1225
- clientId: this.clientId,
1226
- connectedTo: to
1227
- });
1193
+ const willReconnect = this.reconnectOnConnectionDrop && this.getStatus() === "open";
1194
+ this.log?.info(
1195
+ `connection to ${to} disconnected` + (willReconnect ? ", reconnecting" : ""),
1196
+ {
1197
+ ...conn.loggingMetadata,
1198
+ ...session?.loggingMetadata,
1199
+ clientId: this.clientId,
1200
+ connectedTo: to
1201
+ }
1202
+ );
1228
1203
  this.inflightConnectionPromises.delete(to);
1229
1204
  if (this.reconnectOnConnectionDrop) {
1230
1205
  void this.connect(to);
@@ -1310,8 +1285,8 @@ var ClientTransport = class extends Transport {
1310
1285
  conn,
1311
1286
  sessionId: parsed.payload.status.sessionId
1312
1287
  });
1313
- this.onConnect(conn, parsed.from, session, isReconnect);
1314
- this.retryBudget.startRestoringBudget(parsed.from);
1288
+ this.onConnect(conn, session, isReconnect);
1289
+ this.retryBudget.startRestoringBudget(session.to);
1315
1290
  return session;
1316
1291
  }
1317
1292
  /**
@@ -1319,7 +1294,14 @@ var ClientTransport = class extends Transport {
1319
1294
  * @param to The client ID of the node to connect to.
1320
1295
  */
1321
1296
  async connect(to) {
1322
- const canProceedWithConnection = () => this.state === "open";
1297
+ if (this.connections.has(to)) {
1298
+ this.log?.info(`already connected to ${to}, skipping connect attempt`, {
1299
+ clientId: this.clientId,
1300
+ connectedTo: to
1301
+ });
1302
+ return;
1303
+ }
1304
+ const canProceedWithConnection = () => this.getStatus() === "open";
1323
1305
  if (!canProceedWithConnection()) {
1324
1306
  this.log?.info(
1325
1307
  `transport state is no longer open, cancelling attempt to connect to ${to}`,
@@ -1329,8 +1311,8 @@ var ClientTransport = class extends Transport {
1329
1311
  }
1330
1312
  let reconnectPromise = this.inflightConnectionPromises.get(to);
1331
1313
  if (!reconnectPromise) {
1332
- const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
1333
1314
  if (!this.retryBudget.hasBudget(to)) {
1315
+ const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
1334
1316
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
1335
1317
  this.log?.error(errMsg, { clientId: this.clientId, connectedTo: to });
1336
1318
  this.protocolError(ProtocolError.RetriesExceeded, errMsg);
@@ -1411,7 +1393,7 @@ var ClientTransport = class extends Transport {
1411
1393
  clientId: this.clientId,
1412
1394
  connectedTo: to
1413
1395
  });
1414
- return this.connect(to);
1396
+ await this.connect(to);
1415
1397
  }
1416
1398
  }
1417
1399
  }