@replit/river 0.207.3 → 0.208.0

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 (52) hide show
  1. package/dist/{adapter-f2b6e211.d.ts → adapter-ChksXKVN.d.ts} +2 -2
  2. package/dist/adapter-Cuc4JtfV.d.cts +46 -0
  3. package/dist/{chunk-BO7MFCO6.js → chunk-2JNVDUMN.js} +55 -97
  4. package/dist/chunk-2JNVDUMN.js.map +1 -0
  5. package/dist/{chunk-B7REV3ZV.js → chunk-DKW3GC3M.js} +2 -2
  6. package/dist/{chunk-QGPYCXV4.js → chunk-ETZAHFGQ.js} +80 -61
  7. package/dist/chunk-ETZAHFGQ.js.map +1 -0
  8. package/dist/codec/index.d.cts +3 -3
  9. package/dist/codec/index.d.ts +3 -3
  10. package/dist/codec/index.js +2 -2
  11. package/dist/connection-BF4zg6Qv.d.cts +35 -0
  12. package/dist/{connection-06d72f2e.d.ts → connection-Donr3JRB.d.ts} +4 -4
  13. package/dist/index-C9tpZjBN.d.cts +37 -0
  14. package/dist/{index-02554794.d.ts → index-D8IOd3LG.d.ts} +2 -2
  15. package/dist/logging/index.d.cts +2 -2
  16. package/dist/logging/index.d.ts +2 -2
  17. package/dist/{message-01c3e85a.d.ts → message-Di94OL80.d.cts} +1 -1
  18. package/dist/message-Di94OL80.d.ts +108 -0
  19. package/dist/router/index.cjs +62 -43
  20. package/dist/router/index.cjs.map +1 -1
  21. package/dist/router/index.d.cts +27 -8
  22. package/dist/router/index.d.ts +27 -8
  23. package/dist/router/index.js +1 -1
  24. package/dist/testUtil/index.cjs +56 -105
  25. package/dist/testUtil/index.cjs.map +1 -1
  26. package/dist/testUtil/index.d.cts +5 -5
  27. package/dist/testUtil/index.d.ts +5 -5
  28. package/dist/testUtil/index.js +7 -14
  29. package/dist/testUtil/index.js.map +1 -1
  30. package/dist/transport/impls/ws/client.cjs +51 -80
  31. package/dist/transport/impls/ws/client.cjs.map +1 -1
  32. package/dist/transport/impls/ws/client.d.cts +6 -6
  33. package/dist/transport/impls/ws/client.d.ts +6 -6
  34. package/dist/transport/impls/ws/client.js +3 -3
  35. package/dist/transport/impls/ws/server.cjs +52 -85
  36. package/dist/transport/impls/ws/server.cjs.map +1 -1
  37. package/dist/transport/impls/ws/server.d.cts +6 -6
  38. package/dist/transport/impls/ws/server.d.ts +6 -6
  39. package/dist/transport/impls/ws/server.js +3 -3
  40. package/dist/transport/index.cjs +52 -94
  41. package/dist/transport/index.cjs.map +1 -1
  42. package/dist/transport/index.d.cts +6 -6
  43. package/dist/transport/index.d.ts +6 -6
  44. package/dist/transport/index.js +3 -3
  45. package/dist/transport-CCaWx1Rb.d.cts +1566 -0
  46. package/dist/{services-87887bc5.d.ts → transport-CZb3vdB4.d.ts} +344 -347
  47. package/dist/{wslike-e0b32dd5.d.ts → wslike-Dng9H1C7.d.cts} +1 -1
  48. package/dist/wslike-Dng9H1C7.d.ts +40 -0
  49. package/package.json +3 -3
  50. package/dist/chunk-BO7MFCO6.js.map +0 -1
  51. package/dist/chunk-QGPYCXV4.js.map +0 -1
  52. /package/dist/{chunk-B7REV3ZV.js.map → chunk-DKW3GC3M.js.map} +0 -0
@@ -1,11 +1,11 @@
1
- import { A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, c as ParsedMetadata, d as AnyProcedure, b as Connection, S as ServerHandshakeOptions, e as ServiceContext } from '../services-87887bc5.js';
2
- export { M as BaseErrorSchemaType, K as CANCEL_CODE, O as Client, X as Err, Z as ErrResult, J as INVALID_REQUEST_CODE, T as Ok, _ as OkResult, w as PayloadType, l as ProcErrors, h as ProcHandler, i as ProcInit, j as ProcRequest, k as ProcResponse, m as ProcType, D as Procedure, E as ProcedureErrorSchemaType, Q as ProcedureHandlerContext, x as ProcedureMap, R as RPCProcedure, B as Readable, L as ReaderErrorSchema, a1 as ResponseData, Y as Result, a0 as ResultUnwrapErr, $ as ResultUnwrapOk, q as SerializedProcedureSchema, v as SerializedProcedureSchemaProtocolv1, o as SerializedServerSchema, t as SerializedServerSchemaProtocolv1, p as SerializedServiceSchema, u as SerializedServiceSchemaProtocolv1, f as Service, g as ServiceConfiguration, n as ServiceSchema, z as StreamProcedure, y as SubscriptionProcedure, G as UNCAUGHT_ERROR_CODE, H as UNEXPECTED_DISCONNECT_CODE, U as UploadProcedure, V as ValidProcType, W as Writable, N as createClient, a2 as createClientHandshakeOptions, a3 as createServerHandshakeOptions, F as flattenErrorType, s as serializeSchema, r as serializeSchemaV1Compat } from '../services-87887bc5.js';
3
- import { T as TransportClientId, O as OpaqueTransportMessage } from '../message-01c3e85a.js';
1
+ import { A as AnyServiceSchemaMap, b as Connection, S as ServerHandshakeOptions, c as ServiceContext, d as ProcedureHandlerContext, e as PayloadType, I as InstantiatedServiceSchemaMap, f as ParsedMetadata, g as AnyProcedure } from '../transport-CCaWx1Rb.cjs';
2
+ export { N as BaseErrorSchemaType, L as CANCEL_CODE, Q as Client, X as Err, Z as ErrResult, K as INVALID_REQUEST_CODE, T as Ok, _ as OkResult, n as ProcErrors, j as ProcHandler, k as ProcInit, l as ProcRequest, m as ProcResponse, o as ProcType, E as Procedure, F as ProcedureErrorSchemaType, y as ProcedureMap, R as RPCProcedure, D as Readable, M as ReaderErrorSchema, a1 as ResponseData, Y as Result, a0 as ResultUnwrapErr, $ as ResultUnwrapOk, t as SerializedProcedureSchema, x as SerializedProcedureSchemaProtocolv1, q as SerializedServerSchema, v as SerializedServerSchemaProtocolv1, r as SerializedServiceSchema, w as SerializedServiceSchemaProtocolv1, h as Service, i as ServiceConfiguration, p as ServiceSchema, B as StreamProcedure, z as SubscriptionProcedure, H as UNCAUGHT_ERROR_CODE, J as UNEXPECTED_DISCONNECT_CODE, U as UploadProcedure, V as ValidProcType, W as Writable, O as createClient, a2 as createClientHandshakeOptions, a3 as createServerHandshakeOptions, G as flattenErrorType, s as serializeSchema, u as serializeSchemaV1Compat } from '../transport-CCaWx1Rb.cjs';
3
+ import { Static } from '@sinclair/typebox';
4
+ import { T as TransportClientId, O as OpaqueTransportMessage } from '../message-Di94OL80.cjs';
4
5
  import { ServerTransport } from '../transport/index.cjs';
5
- import '@sinclair/typebox';
6
6
  import '@opentelemetry/api';
7
- import '../index-02554794.js';
8
- import '../adapter-f2b6e211.js';
7
+ import '../index-C9tpZjBN.cjs';
8
+ import '../adapter-Cuc4JtfV.cjs';
9
9
 
10
10
  type StreamId = string;
11
11
  /**
@@ -33,6 +33,21 @@ interface ProcStream {
33
33
  handleMsg: (msg: OpaqueTransportMessage) => void;
34
34
  handleSessionDisconnect: () => void;
35
35
  }
36
+ interface MiddlewareContext extends Readonly<Omit<ProcedureHandlerContext<unknown>, 'cancel'>> {
37
+ readonly streamId: StreamId;
38
+ readonly procedureName: string;
39
+ readonly serviceName: string;
40
+ }
41
+ interface MiddlewareParam {
42
+ readonly ctx: MiddlewareContext;
43
+ readonly reqInit: Static<PayloadType>;
44
+ next: () => void;
45
+ }
46
+ /**
47
+ * Middleware is a function that can inspect requests as they are received.
48
+ * For now modification of the request is not supported behavior.
49
+ */
50
+ type Middleware = (param: MiddlewareParam) => void;
36
51
  /**
37
52
  * Creates a server instance that listens for incoming messages from a transport and routes them to the appropriate service and procedure.
38
53
  * The server tracks the state of each service along with open streams and the extended context object.
@@ -50,8 +65,12 @@ declare function createServer<Services extends AnyServiceSchemaMap>(transport: S
50
65
  * cascading stream errors.
51
66
  */
52
67
  maxCancelledStreamTombstonesPerSession?: number;
68
+ /**
69
+ * Middlewares run before procedure handlers allowing you to inspect requests and responses..
70
+ */
71
+ middlewares?: Array<Middleware>;
53
72
  }>): Server<Services>;
54
73
 
55
- var version = "0.207.3";
74
+ var version = "0.208.0";
56
75
 
57
- export { ParsedMetadata, version as RIVER_VERSION, Server, ServiceContext, createServer };
76
+ export { type Middleware, type MiddlewareContext, type MiddlewareParam, ParsedMetadata, PayloadType, ProcedureHandlerContext, version as RIVER_VERSION, type Server, ServiceContext, createServer };
@@ -1,11 +1,11 @@
1
- import { A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, c as ParsedMetadata, d as AnyProcedure, b as Connection, S as ServerHandshakeOptions, e as ServiceContext } from '../services-87887bc5.js';
2
- export { M as BaseErrorSchemaType, K as CANCEL_CODE, O as Client, X as Err, Z as ErrResult, J as INVALID_REQUEST_CODE, T as Ok, _ as OkResult, w as PayloadType, l as ProcErrors, h as ProcHandler, i as ProcInit, j as ProcRequest, k as ProcResponse, m as ProcType, D as Procedure, E as ProcedureErrorSchemaType, Q as ProcedureHandlerContext, x as ProcedureMap, R as RPCProcedure, B as Readable, L as ReaderErrorSchema, a1 as ResponseData, Y as Result, a0 as ResultUnwrapErr, $ as ResultUnwrapOk, q as SerializedProcedureSchema, v as SerializedProcedureSchemaProtocolv1, o as SerializedServerSchema, t as SerializedServerSchemaProtocolv1, p as SerializedServiceSchema, u as SerializedServiceSchemaProtocolv1, f as Service, g as ServiceConfiguration, n as ServiceSchema, z as StreamProcedure, y as SubscriptionProcedure, G as UNCAUGHT_ERROR_CODE, H as UNEXPECTED_DISCONNECT_CODE, U as UploadProcedure, V as ValidProcType, W as Writable, N as createClient, a2 as createClientHandshakeOptions, a3 as createServerHandshakeOptions, F as flattenErrorType, s as serializeSchema, r as serializeSchemaV1Compat } from '../services-87887bc5.js';
3
- import { T as TransportClientId, O as OpaqueTransportMessage } from '../message-01c3e85a.js';
1
+ import { A as AnyServiceSchemaMap, b as Connection, S as ServerHandshakeOptions, c as ServiceContext, d as ProcedureHandlerContext, e as PayloadType, I as InstantiatedServiceSchemaMap, f as ParsedMetadata, g as AnyProcedure } from '../transport-CZb3vdB4.js';
2
+ export { N as BaseErrorSchemaType, L as CANCEL_CODE, Q as Client, X as Err, Z as ErrResult, K as INVALID_REQUEST_CODE, T as Ok, _ as OkResult, n as ProcErrors, j as ProcHandler, k as ProcInit, l as ProcRequest, m as ProcResponse, o as ProcType, E as Procedure, F as ProcedureErrorSchemaType, y as ProcedureMap, R as RPCProcedure, D as Readable, M as ReaderErrorSchema, a1 as ResponseData, Y as Result, a0 as ResultUnwrapErr, $ as ResultUnwrapOk, t as SerializedProcedureSchema, x as SerializedProcedureSchemaProtocolv1, q as SerializedServerSchema, v as SerializedServerSchemaProtocolv1, r as SerializedServiceSchema, w as SerializedServiceSchemaProtocolv1, h as Service, i as ServiceConfiguration, p as ServiceSchema, B as StreamProcedure, z as SubscriptionProcedure, H as UNCAUGHT_ERROR_CODE, J as UNEXPECTED_DISCONNECT_CODE, U as UploadProcedure, V as ValidProcType, W as Writable, O as createClient, a2 as createClientHandshakeOptions, a3 as createServerHandshakeOptions, G as flattenErrorType, s as serializeSchema, u as serializeSchemaV1Compat } from '../transport-CZb3vdB4.js';
3
+ import { Static } from '@sinclair/typebox';
4
+ import { T as TransportClientId, O as OpaqueTransportMessage } from '../message-Di94OL80.js';
4
5
  import { ServerTransport } from '../transport/index.js';
5
- import '@sinclair/typebox';
6
6
  import '@opentelemetry/api';
7
- import '../index-02554794.js';
8
- import '../adapter-f2b6e211.js';
7
+ import '../index-D8IOd3LG.js';
8
+ import '../adapter-ChksXKVN.js';
9
9
 
10
10
  type StreamId = string;
11
11
  /**
@@ -33,6 +33,21 @@ interface ProcStream {
33
33
  handleMsg: (msg: OpaqueTransportMessage) => void;
34
34
  handleSessionDisconnect: () => void;
35
35
  }
36
+ interface MiddlewareContext extends Readonly<Omit<ProcedureHandlerContext<unknown>, 'cancel'>> {
37
+ readonly streamId: StreamId;
38
+ readonly procedureName: string;
39
+ readonly serviceName: string;
40
+ }
41
+ interface MiddlewareParam {
42
+ readonly ctx: MiddlewareContext;
43
+ readonly reqInit: Static<PayloadType>;
44
+ next: () => void;
45
+ }
46
+ /**
47
+ * Middleware is a function that can inspect requests as they are received.
48
+ * For now modification of the request is not supported behavior.
49
+ */
50
+ type Middleware = (param: MiddlewareParam) => void;
36
51
  /**
37
52
  * Creates a server instance that listens for incoming messages from a transport and routes them to the appropriate service and procedure.
38
53
  * The server tracks the state of each service along with open streams and the extended context object.
@@ -50,8 +65,12 @@ declare function createServer<Services extends AnyServiceSchemaMap>(transport: S
50
65
  * cascading stream errors.
51
66
  */
52
67
  maxCancelledStreamTombstonesPerSession?: number;
68
+ /**
69
+ * Middlewares run before procedure handlers allowing you to inspect requests and responses..
70
+ */
71
+ middlewares?: Array<Middleware>;
53
72
  }>): Server<Services>;
54
73
 
55
- var version = "0.207.3";
74
+ var version = "0.208.0";
56
75
 
57
- export { ParsedMetadata, version as RIVER_VERSION, Server, ServiceContext, createServer };
76
+ export { type Middleware, type MiddlewareContext, type MiddlewareParam, ParsedMetadata, PayloadType, ProcedureHandlerContext, version as RIVER_VERSION, type Server, ServiceContext, createServer };
@@ -16,7 +16,7 @@ import {
16
16
  serializeSchema,
17
17
  serializeSchemaV1Compat,
18
18
  version
19
- } from "../chunk-QGPYCXV4.js";
19
+ } from "../chunk-ETZAHFGQ.js";
20
20
  export {
21
21
  CANCEL_CODE,
22
22
  Err,
@@ -488,13 +488,11 @@ var SessionConnecting = class extends IdentifiedSessionWithGracePeriod {
488
488
  this.listeners = props.listeners;
489
489
  this.connPromise.then(
490
490
  (conn) => {
491
- if (this._isConsumed)
492
- return;
491
+ if (this._isConsumed) return;
493
492
  this.listeners.onConnectionEstablished(conn);
494
493
  },
495
494
  (err) => {
496
- if (this._isConsumed)
497
- return;
495
+ if (this._isConsumed) return;
498
496
  this.listeners.onConnectionFailed(err);
499
497
  }
500
498
  );
@@ -555,7 +553,7 @@ function coerceErrorString(err) {
555
553
  }
556
554
 
557
555
  // package.json
558
- var version = "0.207.3";
556
+ var version = "0.208.0";
559
557
 
560
558
  // tracing/index.ts
561
559
  function getPropagationContext(ctx) {
@@ -569,7 +567,7 @@ function getPropagationContext(ctx) {
569
567
  function createSessionTelemetryInfo(tracer, sessionId, to, from, propagationCtx) {
570
568
  const parentCtx = propagationCtx ? import_api.propagation.extract(import_api.context.active(), propagationCtx) : import_api.context.active();
571
569
  const span = tracer.startSpan(
572
- `river.session.${sessionId}`,
570
+ `river.session`,
573
571
  {
574
572
  attributes: {
575
573
  component: "river",
@@ -585,7 +583,7 @@ function createSessionTelemetryInfo(tracer, sessionId, to, from, propagationCtx)
585
583
  }
586
584
  function createConnectionTelemetryInfo(tracer, connection, info) {
587
585
  const span = tracer.startSpan(
588
- `connection ${connection.id}`,
586
+ `river.connection`,
589
587
  {
590
588
  attributes: {
591
589
  component: "river",
@@ -615,9 +613,9 @@ var SessionWaitingForHandshake = class extends CommonSession {
615
613
  this.handshakeTimeout = setTimeout(() => {
616
614
  this.listeners.onHandshakeTimeout();
617
615
  }, this.options.handshakeTimeoutMs);
618
- this.conn.addDataListener(this.onHandshakeData);
619
- this.conn.addErrorListener(this.listeners.onConnectionErrored);
620
- this.conn.addCloseListener(this.listeners.onConnectionClosed);
616
+ this.conn.setDataListener(this.onHandshakeData);
617
+ this.conn.setErrorListener(this.listeners.onConnectionErrored);
618
+ this.conn.setCloseListener(this.listeners.onConnectionClosed);
621
619
  }
622
620
  get loggingMetadata() {
623
621
  return {
@@ -641,9 +639,9 @@ var SessionWaitingForHandshake = class extends CommonSession {
641
639
  return sendMessage(this.conn, this.codec, msg);
642
640
  }
643
641
  _handleStateExit() {
644
- this.conn.removeDataListener(this.onHandshakeData);
645
- this.conn.removeErrorListener(this.listeners.onConnectionErrored);
646
- this.conn.removeCloseListener(this.listeners.onConnectionClosed);
642
+ this.conn.removeDataListener();
643
+ this.conn.removeErrorListener();
644
+ this.conn.removeCloseListener();
647
645
  clearTimeout(this.handshakeTimeout);
648
646
  this.handshakeTimeout = void 0;
649
647
  }
@@ -665,9 +663,9 @@ var SessionHandshaking = class extends IdentifiedSessionWithGracePeriod {
665
663
  this.handshakeTimeout = setTimeout(() => {
666
664
  this.listeners.onHandshakeTimeout();
667
665
  }, this.options.handshakeTimeoutMs);
668
- this.conn.addDataListener(this.onHandshakeData);
669
- this.conn.addErrorListener(this.listeners.onConnectionErrored);
670
- this.conn.addCloseListener(this.listeners.onConnectionClosed);
666
+ this.conn.setDataListener(this.onHandshakeData);
667
+ this.conn.setErrorListener(this.listeners.onConnectionErrored);
668
+ this.conn.setCloseListener(this.listeners.onConnectionClosed);
671
669
  }
672
670
  get loggingMetadata() {
673
671
  return {
@@ -691,9 +689,9 @@ var SessionHandshaking = class extends IdentifiedSessionWithGracePeriod {
691
689
  }
692
690
  _handleStateExit() {
693
691
  super._handleStateExit();
694
- this.conn.removeDataListener(this.onHandshakeData);
695
- this.conn.removeErrorListener(this.listeners.onConnectionErrored);
696
- this.conn.removeCloseListener(this.listeners.onConnectionClosed);
692
+ this.conn.removeDataListener();
693
+ this.conn.removeErrorListener();
694
+ this.conn.removeCloseListener();
697
695
  if (this.handshakeTimeout) {
698
696
  clearTimeout(this.handshakeTimeout);
699
697
  this.handshakeTimeout = void 0;
@@ -749,9 +747,9 @@ var SessionConnected = class extends IdentifiedSession {
749
747
  super(props);
750
748
  this.conn = props.conn;
751
749
  this.listeners = props.listeners;
752
- this.conn.addDataListener(this.onMessageData);
753
- this.conn.addCloseListener(this.listeners.onConnectionClosed);
754
- this.conn.addErrorListener(this.listeners.onConnectionErrored);
750
+ this.conn.setDataListener(this.onMessageData);
751
+ this.conn.setCloseListener(this.listeners.onConnectionClosed);
752
+ this.conn.setErrorListener(this.listeners.onConnectionErrored);
755
753
  }
756
754
  sendBufferedMessages() {
757
755
  if (this.sendBuffer.length > 0) {
@@ -860,9 +858,9 @@ var SessionConnected = class extends IdentifiedSession {
860
858
  };
861
859
  _handleStateExit() {
862
860
  super._handleStateExit();
863
- this.conn.removeDataListener(this.onMessageData);
864
- this.conn.removeCloseListener(this.listeners.onConnectionClosed);
865
- this.conn.removeErrorListener(this.listeners.onConnectionErrored);
861
+ this.conn.removeDataListener();
862
+ this.conn.removeCloseListener();
863
+ this.conn.removeErrorListener();
866
864
  if (this.heartbeatHandle) {
867
865
  clearInterval(this.heartbeatHandle);
868
866
  this.heartbeatHandle = void 0;
@@ -1054,8 +1052,7 @@ var Transport = class {
1054
1052
  * @param message The received message.
1055
1053
  */
1056
1054
  handleMsg(message) {
1057
- if (this.getStatus() !== "open")
1058
- return;
1055
+ if (this.getStatus() !== "open") return;
1059
1056
  this.eventDispatcher.dispatchEvent("message", message);
1060
1057
  }
1061
1058
  /**
@@ -1143,8 +1140,7 @@ var Transport = class {
1143
1140
  });
1144
1141
  }
1145
1142
  deleteSession(session, options) {
1146
- if (session._isConsumed)
1147
- return;
1143
+ if (session._isConsumed) return;
1148
1144
  const loggingMetadata = session.loggingMetadata;
1149
1145
  if (loggingMetadata.tags && options?.unhealthy) {
1150
1146
  loggingMetadata.tags.push("unhealthy-session");
@@ -1164,7 +1160,7 @@ var Transport = class {
1164
1160
  }
1165
1161
  // common listeners
1166
1162
  onSessionGracePeriodElapsed(session) {
1167
- this.log?.warn(
1163
+ this.log?.info(
1168
1164
  `session to ${session.to} grace period elapsed, closing`,
1169
1165
  session.loggingMetadata
1170
1166
  );
@@ -1521,15 +1517,6 @@ var ClientTransport = class extends Transport {
1521
1517
  });
1522
1518
  const res = connectedSession.sendBufferedMessages();
1523
1519
  if (!res.ok) {
1524
- this.log?.error(`failed to send buffered messages: ${res.reason}`, {
1525
- ...connectedSession.loggingMetadata,
1526
- transportMessage: msg
1527
- });
1528
- this.protocolError({
1529
- type: ProtocolError.MessageSendFailure,
1530
- message: res.reason
1531
- });
1532
- this.deleteSession(connectedSession, { unhealthy: true });
1533
1520
  return;
1534
1521
  }
1535
1522
  this.updateSession(connectedSession);
@@ -1731,8 +1718,7 @@ var ServerTransport = class extends Transport {
1731
1718
  super.deleteSession(session, options);
1732
1719
  }
1733
1720
  handleConnection(conn) {
1734
- if (this.getStatus() !== "open")
1735
- return;
1721
+ if (this.getStatus() !== "open") return;
1736
1722
  this.log?.info(`new incoming connection`, {
1737
1723
  ...conn.loggingMetadata,
1738
1724
  clientId: this.clientId
@@ -2076,18 +2062,6 @@ var ServerTransport = class extends Transport {
2076
2062
  );
2077
2063
  const bufferSendRes = connectedSession.sendBufferedMessages();
2078
2064
  if (!bufferSendRes.ok) {
2079
- this.log?.error(
2080
- `failed to send buffered messages: ${bufferSendRes.reason}`,
2081
- {
2082
- ...connectedSession.loggingMetadata,
2083
- transportMessage: msg
2084
- }
2085
- );
2086
- this.protocolError({
2087
- type: ProtocolError.MessageSendFailure,
2088
- message: bufferSendRes.reason
2089
- });
2090
- this.deleteSession(connectedSession, { unhealthy: true });
2091
2065
  return;
2092
2066
  }
2093
2067
  this.sessionHandshakeMetadata.set(connectedSession.to, parsedMetadata);
@@ -2118,60 +2092,44 @@ var Connection = class {
2118
2092
  }
2119
2093
  return metadata;
2120
2094
  }
2121
- // can't use event emitter because we need this to work in both node + browser
2122
- _dataListeners = /* @__PURE__ */ new Set();
2123
- _closeListeners = /* @__PURE__ */ new Set();
2124
- _errorListeners = /* @__PURE__ */ new Set();
2125
- get dataListeners() {
2126
- return [...this._dataListeners];
2127
- }
2128
- get closeListeners() {
2129
- return [...this._closeListeners];
2130
- }
2131
- get errorListeners() {
2132
- return [...this._errorListeners];
2133
- }
2095
+ dataListener;
2096
+ closeListener;
2097
+ errorListener;
2134
2098
  onData(msg) {
2135
- for (const cb of this.dataListeners) {
2136
- cb(msg);
2137
- }
2099
+ this.dataListener?.(msg);
2138
2100
  }
2139
2101
  onError(err) {
2140
- for (const cb of this.errorListeners) {
2141
- cb(err);
2142
- }
2102
+ this.errorListener?.(err);
2143
2103
  }
2144
2104
  onClose() {
2145
- for (const cb of this.closeListeners) {
2146
- cb();
2147
- }
2105
+ this.closeListener?.();
2148
2106
  this.telemetry?.span.end();
2149
2107
  }
2150
2108
  /**
2151
- * Handle adding a callback for when a message is received.
2152
- * @param msg The message that was received.
2109
+ * Set the callback for when a message is received.
2110
+ * @param cb The message handler callback.
2153
2111
  */
2154
- addDataListener(cb) {
2155
- this._dataListeners.add(cb);
2112
+ setDataListener(cb) {
2113
+ this.dataListener = cb;
2156
2114
  }
2157
- removeDataListener(cb) {
2158
- this._dataListeners.delete(cb);
2115
+ removeDataListener() {
2116
+ this.dataListener = void 0;
2159
2117
  }
2160
2118
  /**
2161
- * Handle adding a callback for when the connection is closed.
2162
- * This should also be called if an error happens and after notifying all the error listeners.
2119
+ * Set the callback for when the connection is closed.
2120
+ * This should also be called if an error happens and after notifying the error listener.
2163
2121
  * @param cb The callback to call when the connection is closed.
2164
2122
  */
2165
- addCloseListener(cb) {
2166
- this._closeListeners.add(cb);
2123
+ setCloseListener(cb) {
2124
+ this.closeListener = cb;
2167
2125
  }
2168
- removeCloseListener(cb) {
2169
- this._closeListeners.delete(cb);
2126
+ removeCloseListener() {
2127
+ this.closeListener = void 0;
2170
2128
  }
2171
2129
  /**
2172
- * Handle adding a callback for when an error is received.
2173
- * This should only be used for this.logging errors, all cleanup
2174
- * should be delegated to addCloseListener.
2130
+ * Set the callback for when an error is received.
2131
+ * This should only be used for logging errors, all cleanup
2132
+ * should be delegated to setCloseListener.
2175
2133
  *
2176
2134
  * The implementer should take care such that the implemented
2177
2135
  * connection will call both the close and error callbacks
@@ -2179,11 +2137,11 @@ var Connection = class {
2179
2137
  *
2180
2138
  * @param cb The callback to call when an error is received.
2181
2139
  */
2182
- addErrorListener(cb) {
2183
- this._errorListeners.add(cb);
2140
+ setErrorListener(cb) {
2141
+ this.errorListener = cb;
2184
2142
  }
2185
- removeErrorListener(cb) {
2186
- this._errorListeners.delete(cb);
2143
+ removeErrorListener() {
2144
+ this.errorListener = void 0;
2187
2145
  }
2188
2146
  };
2189
2147
 
@@ -2652,19 +2610,13 @@ var InMemoryConnection = class extends Connection {
2652
2610
  this.conn = pipe;
2653
2611
  this.conn.allowHalfOpen = false;
2654
2612
  this.conn.on("data", (data) => {
2655
- for (const cb of this.dataListeners) {
2656
- cb(data);
2657
- }
2613
+ this.dataListener?.(data);
2658
2614
  });
2659
2615
  this.conn.on("close", () => {
2660
- for (const cb of this.closeListeners) {
2661
- cb();
2662
- }
2616
+ this.closeListener?.();
2663
2617
  });
2664
2618
  this.conn.on("error", (err) => {
2665
- for (const cb of this.errorListeners) {
2666
- cb(err);
2667
- }
2619
+ this.errorListener?.(err);
2668
2620
  });
2669
2621
  }
2670
2622
  send(payload) {
@@ -2747,8 +2699,7 @@ function createMockTransportNetwork(opts) {
2747
2699
  },
2748
2700
  async restartServer() {
2749
2701
  for (const transport of transports) {
2750
- if (transport.clientId !== "SERVER")
2751
- continue;
2702
+ if (transport.clientId !== "SERVER") continue;
2752
2703
  transport.close();
2753
2704
  }
2754
2705
  for (const conn of Object.values(connections.get())) {