@replit/river 0.23.18 → 0.24.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 (84) hide show
  1. package/README.md +17 -16
  2. package/dist/{chunk-AVL32IMG.js → chunk-AASMR3CQ.js} +20 -16
  3. package/dist/chunk-AASMR3CQ.js.map +1 -0
  4. package/dist/chunk-JA57I7MG.js +653 -0
  5. package/dist/chunk-JA57I7MG.js.map +1 -0
  6. package/dist/chunk-KX5PQRVN.js +382 -0
  7. package/dist/chunk-KX5PQRVN.js.map +1 -0
  8. package/dist/{chunk-EV5HW4IC.js → chunk-KYYB4DUR.js} +65 -53
  9. package/dist/chunk-KYYB4DUR.js.map +1 -0
  10. package/dist/chunk-NLQPPDOT.js +399 -0
  11. package/dist/chunk-NLQPPDOT.js.map +1 -0
  12. package/dist/{chunk-R2HAS3GM.js → chunk-PJGGC3LV.js} +55 -41
  13. package/dist/chunk-PJGGC3LV.js.map +1 -0
  14. package/dist/{chunk-7MJYOL32.js → chunk-RXJLI2OP.js} +15 -23
  15. package/dist/chunk-RXJLI2OP.js.map +1 -0
  16. package/dist/{chunk-6LCL2ZZF.js → chunk-TAH2GVTJ.js} +1 -1
  17. package/dist/chunk-TAH2GVTJ.js.map +1 -0
  18. package/dist/chunk-ZAT3R4CU.js +277 -0
  19. package/dist/chunk-ZAT3R4CU.js.map +1 -0
  20. package/dist/{client-5776a6bb.d.ts → client-ba0d3315.d.ts} +12 -15
  21. package/dist/{connection-bd35d442.d.ts → connection-c3a96d09.d.ts} +1 -5
  22. package/dist/connection-d33e3246.d.ts +11 -0
  23. package/dist/{handshake-a947c234.d.ts → handshake-cdead82a.d.ts} +148 -183
  24. package/dist/logging/index.cjs.map +1 -1
  25. package/dist/logging/index.d.cts +1 -1
  26. package/dist/logging/index.d.ts +1 -1
  27. package/dist/logging/index.js +1 -1
  28. package/dist/{index-ea74cdbb.d.ts → message-e6c560fd.d.ts} +2 -2
  29. package/dist/router/index.cjs +104 -63
  30. package/dist/router/index.cjs.map +1 -1
  31. package/dist/router/index.d.cts +11 -10
  32. package/dist/router/index.d.ts +11 -10
  33. package/dist/router/index.js +2 -2
  34. package/dist/server-2ef5e6ec.d.ts +42 -0
  35. package/dist/{services-38b3f758.d.ts → services-e1417b33.d.ts} +3 -3
  36. package/dist/transport/impls/uds/client.cjs +1246 -1230
  37. package/dist/transport/impls/uds/client.cjs.map +1 -1
  38. package/dist/transport/impls/uds/client.d.cts +4 -4
  39. package/dist/transport/impls/uds/client.d.ts +4 -4
  40. package/dist/transport/impls/uds/client.js +7 -13
  41. package/dist/transport/impls/uds/client.js.map +1 -1
  42. package/dist/transport/impls/uds/server.cjs +1298 -1151
  43. package/dist/transport/impls/uds/server.cjs.map +1 -1
  44. package/dist/transport/impls/uds/server.d.cts +4 -4
  45. package/dist/transport/impls/uds/server.d.ts +4 -4
  46. package/dist/transport/impls/uds/server.js +6 -6
  47. package/dist/transport/impls/ws/client.cjs +976 -965
  48. package/dist/transport/impls/ws/client.cjs.map +1 -1
  49. package/dist/transport/impls/ws/client.d.cts +4 -4
  50. package/dist/transport/impls/ws/client.d.ts +4 -4
  51. package/dist/transport/impls/ws/client.js +6 -7
  52. package/dist/transport/impls/ws/client.js.map +1 -1
  53. package/dist/transport/impls/ws/server.cjs +1182 -1047
  54. package/dist/transport/impls/ws/server.cjs.map +1 -1
  55. package/dist/transport/impls/ws/server.d.cts +4 -4
  56. package/dist/transport/impls/ws/server.d.ts +4 -4
  57. package/dist/transport/impls/ws/server.js +6 -6
  58. package/dist/transport/index.cjs +1433 -1360
  59. package/dist/transport/index.cjs.map +1 -1
  60. package/dist/transport/index.d.cts +4 -4
  61. package/dist/transport/index.d.ts +4 -4
  62. package/dist/transport/index.js +9 -9
  63. package/dist/util/testHelpers.cjs +743 -310
  64. package/dist/util/testHelpers.cjs.map +1 -1
  65. package/dist/util/testHelpers.d.cts +9 -6
  66. package/dist/util/testHelpers.d.ts +9 -6
  67. package/dist/util/testHelpers.js +33 -10
  68. package/dist/util/testHelpers.js.map +1 -1
  69. package/package.json +1 -1
  70. package/dist/chunk-6LCL2ZZF.js.map +0 -1
  71. package/dist/chunk-7MJYOL32.js.map +0 -1
  72. package/dist/chunk-AVL32IMG.js.map +0 -1
  73. package/dist/chunk-DPKOJQWF.js +0 -476
  74. package/dist/chunk-DPKOJQWF.js.map +0 -1
  75. package/dist/chunk-EV5HW4IC.js.map +0 -1
  76. package/dist/chunk-J6N6H2WU.js +0 -476
  77. package/dist/chunk-J6N6H2WU.js.map +0 -1
  78. package/dist/chunk-MW5JXLHY.js +0 -348
  79. package/dist/chunk-MW5JXLHY.js.map +0 -1
  80. package/dist/chunk-R2HAS3GM.js.map +0 -1
  81. package/dist/chunk-RJOWZIWB.js +0 -335
  82. package/dist/chunk-RJOWZIWB.js.map +0 -1
  83. package/dist/connection-df85db7e.d.ts +0 -17
  84. package/dist/server-53cd5b7e.d.ts +0 -24
@@ -25,18 +25,189 @@ __export(transport_exports, {
25
25
  OpaqueTransportMessageSchema: () => OpaqueTransportMessageSchema,
26
26
  ProtocolError: () => ProtocolError,
27
27
  ServerTransport: () => ServerTransport,
28
- Session: () => Session,
28
+ SessionState: () => SessionState,
29
29
  Transport: () => Transport,
30
30
  TransportMessageSchema: () => TransportMessageSchema
31
31
  });
32
32
  module.exports = __toCommonJS(transport_exports);
33
33
 
34
- // transport/transport.ts
35
- var import_value = require("@sinclair/typebox/value");
34
+ // logging/log.ts
35
+ var LoggingLevels = {
36
+ debug: -1,
37
+ info: 0,
38
+ warn: 1,
39
+ error: 2
40
+ };
41
+ var cleanedLogFn = (log) => {
42
+ return (msg, metadata) => {
43
+ if (!metadata?.transportMessage) {
44
+ log(msg, metadata);
45
+ return;
46
+ }
47
+ const { payload, ...rest } = metadata.transportMessage;
48
+ metadata.transportMessage = rest;
49
+ log(msg, metadata);
50
+ };
51
+ };
52
+ var BaseLogger = class {
53
+ minLevel;
54
+ output;
55
+ constructor(output, minLevel = "info") {
56
+ this.minLevel = minLevel;
57
+ this.output = output;
58
+ }
59
+ debug(msg, metadata) {
60
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.debug) {
61
+ this.output(msg, metadata ?? {}, "debug");
62
+ }
63
+ }
64
+ info(msg, metadata) {
65
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.info) {
66
+ this.output(msg, metadata ?? {}, "info");
67
+ }
68
+ }
69
+ warn(msg, metadata) {
70
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.warn) {
71
+ this.output(msg, metadata ?? {}, "warn");
72
+ }
73
+ }
74
+ error(msg, metadata) {
75
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.error) {
76
+ this.output(msg, metadata ?? {}, "error");
77
+ }
78
+ }
79
+ };
80
+ var createLogProxy = (log) => ({
81
+ debug: cleanedLogFn(log.debug.bind(log)),
82
+ info: cleanedLogFn(log.info.bind(log)),
83
+ warn: cleanedLogFn(log.warn.bind(log)),
84
+ error: cleanedLogFn(log.error.bind(log))
85
+ });
86
+
87
+ // transport/events.ts
88
+ var ProtocolError = {
89
+ RetriesExceeded: "conn_retry_exceeded",
90
+ HandshakeFailed: "handshake_failed",
91
+ MessageOrderingViolated: "message_ordering_violated"
92
+ };
93
+ var EventDispatcher = class {
94
+ eventListeners = {};
95
+ removeAllListeners() {
96
+ this.eventListeners = {};
97
+ }
98
+ numberOfListeners(eventType) {
99
+ return this.eventListeners[eventType]?.size ?? 0;
100
+ }
101
+ addEventListener(eventType, handler) {
102
+ if (!this.eventListeners[eventType]) {
103
+ this.eventListeners[eventType] = /* @__PURE__ */ new Set();
104
+ }
105
+ this.eventListeners[eventType]?.add(handler);
106
+ }
107
+ removeEventListener(eventType, handler) {
108
+ const handlers = this.eventListeners[eventType];
109
+ if (handlers) {
110
+ this.eventListeners[eventType]?.delete(handler);
111
+ }
112
+ }
113
+ dispatchEvent(eventType, event) {
114
+ const handlers = this.eventListeners[eventType];
115
+ if (handlers) {
116
+ const copy = [...handlers];
117
+ for (const handler of copy) {
118
+ handler(event);
119
+ }
120
+ }
121
+ }
122
+ };
123
+
124
+ // codec/json.ts
125
+ var encoder = new TextEncoder();
126
+ var decoder = new TextDecoder();
127
+ function uint8ArrayToBase64(uint8Array) {
128
+ let binary = "";
129
+ uint8Array.forEach((byte) => {
130
+ binary += String.fromCharCode(byte);
131
+ });
132
+ return btoa(binary);
133
+ }
134
+ function base64ToUint8Array(base64) {
135
+ const binaryString = atob(base64);
136
+ const uint8Array = new Uint8Array(binaryString.length);
137
+ for (let i = 0; i < binaryString.length; i++) {
138
+ uint8Array[i] = binaryString.charCodeAt(i);
139
+ }
140
+ return uint8Array;
141
+ }
142
+ var NaiveJsonCodec = {
143
+ toBuffer: (obj) => {
144
+ return encoder.encode(
145
+ JSON.stringify(obj, function replacer(key) {
146
+ const val = this[key];
147
+ if (val instanceof Uint8Array) {
148
+ return { $t: uint8ArrayToBase64(val) };
149
+ } else {
150
+ return val;
151
+ }
152
+ })
153
+ );
154
+ },
155
+ fromBuffer: (buff) => {
156
+ try {
157
+ const parsed = JSON.parse(
158
+ decoder.decode(buff),
159
+ function reviver(_key, val) {
160
+ if (val?.$t) {
161
+ return base64ToUint8Array(val.$t);
162
+ } else {
163
+ return val;
164
+ }
165
+ }
166
+ );
167
+ if (typeof parsed === "object")
168
+ return parsed;
169
+ return null;
170
+ } catch {
171
+ return null;
172
+ }
173
+ }
174
+ };
175
+
176
+ // transport/options.ts
177
+ var defaultTransportOptions = {
178
+ heartbeatIntervalMs: 1e3,
179
+ heartbeatsUntilDead: 2,
180
+ sessionDisconnectGraceMs: 5e3,
181
+ connectionTimeoutMs: 2e3,
182
+ handshakeTimeoutMs: 1e3,
183
+ codec: NaiveJsonCodec
184
+ };
185
+ var defaultConnectionRetryOptions = {
186
+ baseIntervalMs: 250,
187
+ maxJitterMs: 200,
188
+ maxBackoffMs: 32e3,
189
+ attemptBudgetCapacity: 5,
190
+ budgetRestoreIntervalMs: 200
191
+ };
192
+ var defaultClientTransportOptions = {
193
+ ...defaultTransportOptions,
194
+ ...defaultConnectionRetryOptions
195
+ };
196
+ var defaultServerTransportOptions = {
197
+ ...defaultTransportOptions
198
+ };
36
199
 
37
200
  // transport/message.ts
38
201
  var import_typebox = require("@sinclair/typebox");
202
+
203
+ // transport/id.ts
39
204
  var import_nanoid = require("nanoid");
205
+ var alphabet = (0, import_nanoid.customAlphabet)(
206
+ "1234567890abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ"
207
+ );
208
+ var generateId = () => alphabet(12);
209
+
210
+ // transport/message.ts
40
211
  var TransportMessageSchema = (t) => import_typebox.Type.Object({
41
212
  id: import_typebox.Type.String(),
42
213
  from: import_typebox.Type.String(),
@@ -71,18 +242,29 @@ var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
71
242
  * used by the server to know whether this is a new or a reestablished connection, and whether it
72
243
  * is compatible with what it already has.
73
244
  */
74
- expectedSessionState: import_typebox.Type.Optional(
75
- import_typebox.Type.Object({
76
- /**
77
- * reconnect is set to true if the client explicitly wants to reestablish an existing
78
- * connection.
79
- */
80
- reconnect: import_typebox.Type.Boolean(),
81
- nextExpectedSeq: import_typebox.Type.Integer()
82
- })
83
- ),
245
+ expectedSessionState: import_typebox.Type.Object({
246
+ // what the client expects the server to send next
247
+ nextExpectedSeq: import_typebox.Type.Integer(),
248
+ // TODO: remove optional once we know all servers
249
+ // are nextSentSeq here
250
+ // what the server expects the client to send next
251
+ nextSentSeq: import_typebox.Type.Optional(import_typebox.Type.Integer())
252
+ }),
84
253
  metadata: import_typebox.Type.Optional(import_typebox.Type.Unknown())
85
254
  });
255
+ var HandshakeErrorRetriableResponseCodes = import_typebox.Type.Union([
256
+ import_typebox.Type.Literal("SESSION_STATE_MISMATCH")
257
+ ]);
258
+ var HandshakeErrorFatalResponseCodes = import_typebox.Type.Union([
259
+ import_typebox.Type.Literal("MALFORMED_HANDSHAKE_META"),
260
+ import_typebox.Type.Literal("MALFORMED_HANDSHAKE"),
261
+ import_typebox.Type.Literal("PROTOCOL_VERSION_MISMATCH"),
262
+ import_typebox.Type.Literal("REJECTED_BY_CUSTOM_HANDLER")
263
+ ]);
264
+ var HandshakeErrorResponseCodes = import_typebox.Type.Union([
265
+ HandshakeErrorRetriableResponseCodes,
266
+ HandshakeErrorFatalResponseCodes
267
+ ]);
86
268
  var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
87
269
  type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
88
270
  status: import_typebox.Type.Union([
@@ -92,7 +274,10 @@ var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
92
274
  }),
93
275
  import_typebox.Type.Object({
94
276
  ok: import_typebox.Type.Literal(false),
95
- reason: import_typebox.Type.String()
277
+ reason: import_typebox.Type.String(),
278
+ // TODO: remove optional once we know all servers
279
+ // are sending code here
280
+ code: import_typebox.Type.Optional(HandshakeErrorResponseCodes)
96
281
  })
97
282
  ])
98
283
  });
@@ -114,12 +299,12 @@ function handshakeRequestMessage({
114
299
  tracing
115
300
  }) {
116
301
  return {
117
- id: (0, import_nanoid.nanoid)(),
302
+ id: generateId(),
118
303
  from,
119
304
  to,
120
305
  seq: 0,
121
306
  ack: 0,
122
- streamId: (0, import_nanoid.nanoid)(),
307
+ streamId: generateId(),
123
308
  controlFlags: 0,
124
309
  tracing,
125
310
  payload: {
@@ -131,19 +316,18 @@ function handshakeRequestMessage({
131
316
  }
132
317
  };
133
318
  }
134
- var SESSION_STATE_MISMATCH = "session state mismatch";
135
319
  function handshakeResponseMessage({
136
320
  from,
137
321
  to,
138
322
  status
139
323
  }) {
140
324
  return {
141
- id: (0, import_nanoid.nanoid)(),
325
+ id: generateId(),
142
326
  from,
143
327
  to,
144
328
  seq: 0,
145
329
  ack: 0,
146
- streamId: (0, import_nanoid.nanoid)(),
330
+ streamId: generateId(),
147
331
  controlFlags: 0,
148
332
  payload: {
149
333
  type: "HANDSHAKE_RESP",
@@ -155,104 +339,230 @@ function isAck(controlFlag) {
155
339
  return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
156
340
  }
157
341
 
158
- // logging/log.ts
159
- var LoggingLevels = {
160
- debug: -1,
161
- info: 0,
162
- warn: 1,
163
- error: 2
164
- };
165
- var cleanedLogFn = (log) => {
166
- return (msg, metadata) => {
167
- if (!metadata?.transportMessage) {
168
- log(msg, metadata);
169
- return;
170
- }
171
- const { payload, ...rest } = metadata.transportMessage;
172
- metadata.transportMessage = rest;
173
- log(msg, metadata);
174
- };
175
- };
176
- var BaseLogger = class {
177
- minLevel;
178
- output;
179
- constructor(output, minLevel = "info") {
180
- this.minLevel = minLevel;
181
- this.output = output;
342
+ // transport/sessionStateMachine/common.ts
343
+ var import_value = require("@sinclair/typebox/value");
344
+ var SessionState = /* @__PURE__ */ ((SessionState2) => {
345
+ SessionState2["NoConnection"] = "NoConnection";
346
+ SessionState2["Connecting"] = "Connecting";
347
+ SessionState2["Handshaking"] = "Handshaking";
348
+ SessionState2["Connected"] = "Connected";
349
+ SessionState2["WaitingForHandshake"] = "WaitingForHandshake";
350
+ return SessionState2;
351
+ })(SessionState || {});
352
+ var ERR_CONSUMED = `session state has been consumed and is no longer valid`;
353
+ var StateMachineState = class {
354
+ /*
355
+ * Whether this state has been consumed
356
+ * and we've moved on to another state
357
+ */
358
+ _isConsumed;
359
+ close() {
360
+ this._handleClose();
182
361
  }
183
- debug(msg, metadata) {
184
- if (LoggingLevels[this.minLevel] <= LoggingLevels.debug) {
185
- this.output(msg, metadata ?? {}, "debug");
186
- }
362
+ constructor() {
363
+ this._isConsumed = false;
364
+ return new Proxy(this, {
365
+ get(target, prop) {
366
+ if (prop === "_isConsumed" || prop === "id" || prop === "state") {
367
+ return Reflect.get(target, prop);
368
+ }
369
+ if (prop === "_handleStateExit") {
370
+ return () => {
371
+ target._isConsumed = true;
372
+ target._handleStateExit();
373
+ };
374
+ }
375
+ if (prop === "_handleClose") {
376
+ return () => {
377
+ target._handleStateExit();
378
+ target._handleClose();
379
+ };
380
+ }
381
+ if (target._isConsumed) {
382
+ throw new Error(
383
+ `${ERR_CONSUMED}: getting ${prop.toString()} on consumed state`
384
+ );
385
+ }
386
+ return Reflect.get(target, prop);
387
+ },
388
+ set(target, prop, value) {
389
+ if (target._isConsumed) {
390
+ throw new Error(
391
+ `${ERR_CONSUMED}: setting ${prop.toString()} on consumed state`
392
+ );
393
+ }
394
+ return Reflect.set(target, prop, value);
395
+ }
396
+ });
187
397
  }
188
- info(msg, metadata) {
189
- if (LoggingLevels[this.minLevel] <= LoggingLevels.info) {
190
- this.output(msg, metadata ?? {}, "info");
191
- }
398
+ };
399
+ var CommonSession = class extends StateMachineState {
400
+ from;
401
+ options;
402
+ log;
403
+ constructor(from, options, log) {
404
+ super();
405
+ this.from = from;
406
+ this.options = options;
407
+ this.log = log;
192
408
  }
193
- warn(msg, metadata) {
194
- if (LoggingLevels[this.minLevel] <= LoggingLevels.warn) {
195
- this.output(msg, metadata ?? {}, "warn");
409
+ parseMsg(msg) {
410
+ const parsedMsg = this.options.codec.fromBuffer(msg);
411
+ if (parsedMsg === null) {
412
+ const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));
413
+ this.log?.error(
414
+ `received malformed msg: ${decodedBuffer}`,
415
+ this.loggingMetadata
416
+ );
417
+ return null;
196
418
  }
197
- }
198
- error(msg, metadata) {
199
- if (LoggingLevels[this.minLevel] <= LoggingLevels.error) {
200
- this.output(msg, metadata ?? {}, "error");
419
+ if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
420
+ this.log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
421
+ ...this.loggingMetadata,
422
+ validationErrors: [
423
+ ...import_value.Value.Errors(OpaqueTransportMessageSchema, parsedMsg)
424
+ ]
425
+ });
426
+ return null;
201
427
  }
428
+ return parsedMsg;
202
429
  }
203
430
  };
204
- var createLogProxy = (log) => ({
205
- debug: cleanedLogFn(log.debug.bind(log)),
206
- info: cleanedLogFn(log.info.bind(log)),
207
- warn: cleanedLogFn(log.warn.bind(log)),
208
- error: cleanedLogFn(log.error.bind(log))
209
- });
210
-
211
- // transport/events.ts
212
- var ProtocolError = {
213
- RetriesExceeded: "conn_retry_exceeded",
214
- HandshakeFailed: "handshake_failed",
215
- MessageOrderingViolated: "message_ordering_violated"
216
- };
217
- var EventDispatcher = class {
218
- eventListeners = {};
219
- removeAllListeners() {
220
- this.eventListeners = {};
431
+ var IdentifiedSession = class extends CommonSession {
432
+ id;
433
+ telemetry;
434
+ to;
435
+ /**
436
+ * Index of the message we will send next (excluding handshake)
437
+ */
438
+ seq;
439
+ /**
440
+ * Number of unique messages we've received this session (excluding handshake)
441
+ */
442
+ ack;
443
+ sendBuffer;
444
+ constructor(id, from, to, seq, ack, sendBuffer, telemetry, options, log) {
445
+ super(from, options, log);
446
+ this.id = id;
447
+ this.to = to;
448
+ this.seq = seq;
449
+ this.ack = ack;
450
+ this.sendBuffer = sendBuffer;
451
+ this.telemetry = telemetry;
452
+ this.log = log;
221
453
  }
222
- numberOfListeners(eventType) {
223
- return this.eventListeners[eventType]?.size ?? 0;
454
+ get loggingMetadata() {
455
+ const spanContext = this.telemetry.span.spanContext();
456
+ return {
457
+ clientId: this.from,
458
+ connectedTo: this.to,
459
+ sessionId: this.id,
460
+ telemetry: {
461
+ traceId: spanContext.traceId,
462
+ spanId: spanContext.spanId
463
+ }
464
+ };
224
465
  }
225
- addEventListener(eventType, handler) {
226
- if (!this.eventListeners[eventType]) {
227
- this.eventListeners[eventType] = /* @__PURE__ */ new Set();
228
- }
229
- this.eventListeners[eventType]?.add(handler);
466
+ constructMsg(partialMsg) {
467
+ const msg = {
468
+ ...partialMsg,
469
+ id: generateId(),
470
+ to: this.to,
471
+ from: this.from,
472
+ seq: this.seq,
473
+ ack: this.ack
474
+ };
475
+ this.seq++;
476
+ return msg;
230
477
  }
231
- removeEventListener(eventType, handler) {
232
- const handlers = this.eventListeners[eventType];
233
- if (handlers) {
234
- this.eventListeners[eventType]?.delete(handler);
235
- }
478
+ nextSeq() {
479
+ return this.sendBuffer.length > 0 ? this.sendBuffer[0].seq : this.seq;
236
480
  }
237
- dispatchEvent(eventType, event) {
238
- const handlers = this.eventListeners[eventType];
239
- if (handlers) {
240
- const copy = [...handlers];
241
- for (const handler of copy) {
242
- handler(event);
481
+ send(msg) {
482
+ const constructedMsg = this.constructMsg(msg);
483
+ this.sendBuffer.push(constructedMsg);
484
+ return constructedMsg.id;
485
+ }
486
+ _handleStateExit() {
487
+ }
488
+ _handleClose() {
489
+ this.sendBuffer.length = 0;
490
+ this.telemetry.span.end();
491
+ }
492
+ };
493
+
494
+ // transport/sessionStateMachine/SessionConnecting.ts
495
+ var SessionConnecting = class extends IdentifiedSession {
496
+ state = "Connecting" /* Connecting */;
497
+ connPromise;
498
+ listeners;
499
+ connectionTimeout;
500
+ constructor(connPromise, listeners, ...args) {
501
+ super(...args);
502
+ this.connPromise = connPromise;
503
+ this.listeners = listeners;
504
+ this.connectionTimeout = setTimeout(() => {
505
+ listeners.onConnectionTimeout();
506
+ }, this.options.connectionTimeoutMs);
507
+ connPromise.then(
508
+ (conn) => {
509
+ if (this._isConsumed)
510
+ return;
511
+ listeners.onConnectionEstablished(conn);
512
+ },
513
+ (err) => {
514
+ if (this._isConsumed)
515
+ return;
516
+ listeners.onConnectionFailed(err);
243
517
  }
244
- }
518
+ );
519
+ }
520
+ // close a pending connection if it resolves, ignore errors if the promise
521
+ // ends up rejected anyways
522
+ bestEffortClose() {
523
+ void this.connPromise.then((conn) => conn.close()).catch(() => {
524
+ });
525
+ }
526
+ _handleStateExit() {
527
+ super._handleStateExit();
528
+ clearTimeout(this.connectionTimeout);
529
+ this.connectionTimeout = void 0;
530
+ }
531
+ _handleClose() {
532
+ this.bestEffortClose();
533
+ super._handleClose();
245
534
  }
246
535
  };
247
536
 
248
- // transport/session.ts
249
- var import_nanoid2 = require("nanoid");
537
+ // transport/sessionStateMachine/SessionNoConnection.ts
538
+ var SessionNoConnection = class extends IdentifiedSession {
539
+ state = "NoConnection" /* NoConnection */;
540
+ listeners;
541
+ gracePeriodTimeout;
542
+ constructor(listeners, ...args) {
543
+ super(...args);
544
+ this.listeners = listeners;
545
+ this.gracePeriodTimeout = setTimeout(() => {
546
+ this.listeners.onSessionGracePeriodElapsed();
547
+ }, this.options.sessionDisconnectGraceMs);
548
+ }
549
+ _handleClose() {
550
+ super._handleClose();
551
+ }
552
+ _handleStateExit() {
553
+ super._handleStateExit();
554
+ if (this.gracePeriodTimeout) {
555
+ clearTimeout(this.gracePeriodTimeout);
556
+ this.gracePeriodTimeout = void 0;
557
+ }
558
+ }
559
+ };
250
560
 
251
561
  // tracing/index.ts
252
562
  var import_api = require("@opentelemetry/api");
253
563
 
254
564
  // package.json
255
- var version = "0.23.18";
565
+ var version = "0.24.0";
256
566
 
257
567
  // tracing/index.ts
258
568
  function getPropagationContext(ctx) {
@@ -263,16 +573,16 @@ function getPropagationContext(ctx) {
263
573
  import_api.propagation.inject(ctx, tracing);
264
574
  return tracing;
265
575
  }
266
- function createSessionTelemetryInfo(session, propagationCtx) {
576
+ function createSessionTelemetryInfo(sessionId, to, from, propagationCtx) {
267
577
  const parentCtx = propagationCtx ? import_api.propagation.extract(import_api.context.active(), propagationCtx) : import_api.context.active();
268
578
  const span = tracer.startSpan(
269
- `session ${session.id}`,
579
+ `session ${sessionId}`,
270
580
  {
271
581
  attributes: {
272
582
  component: "river",
273
- "river.session.id": session.id,
274
- "river.session.to": session.to,
275
- "river.session.from": session.from
583
+ "river.session.id": sessionId,
584
+ "river.session.to": to,
585
+ "river.session.from": from
276
586
  }
277
587
  },
278
588
  parentCtx
@@ -280,173 +590,155 @@ function createSessionTelemetryInfo(session, propagationCtx) {
280
590
  const ctx = import_api.trace.setSpan(parentCtx, span);
281
591
  return { span, ctx };
282
592
  }
283
- function createConnectionTelemetryInfo(connection, info) {
284
- const span = tracer.startSpan(
285
- `connection ${connection.id}`,
286
- {
287
- attributes: {
288
- component: "river",
289
- "river.connection.id": connection.id
290
- },
291
- links: [{ context: info.span.spanContext() }]
292
- },
293
- info.ctx
294
- );
295
- const ctx = import_api.trace.setSpan(info.ctx, span);
296
- return { span, ctx };
297
- }
298
593
  var tracer = import_api.trace.getTracer("river", version);
299
594
  var tracing_default = tracer;
300
595
 
301
- // transport/session.ts
302
- var import_api2 = require("@opentelemetry/api");
303
- var nanoid2 = (0, import_nanoid2.customAlphabet)("1234567890abcdefghijklmnopqrstuvxyz", 6);
304
- var unsafeId = () => nanoid2();
305
- var Connection = class {
306
- id;
307
- telemetry;
308
- constructor() {
309
- this.id = `conn-${nanoid2(12)}`;
596
+ // transport/sessionStateMachine/SessionWaitingForHandshake.ts
597
+ var SessionWaitingForHandshake = class extends CommonSession {
598
+ state = "WaitingForHandshake" /* WaitingForHandshake */;
599
+ conn;
600
+ listeners;
601
+ handshakeTimeout;
602
+ constructor(conn, listeners, ...args) {
603
+ super(...args);
604
+ this.conn = conn;
605
+ this.listeners = listeners;
606
+ this.handshakeTimeout = setTimeout(() => {
607
+ listeners.onHandshakeTimeout();
608
+ }, this.options.handshakeTimeoutMs);
609
+ this.conn.addDataListener(this.onHandshakeData);
610
+ this.conn.addErrorListener(listeners.onConnectionErrored);
611
+ this.conn.addCloseListener(listeners.onConnectionClosed);
310
612
  }
311
- get loggingMetadata() {
312
- const metadata = { connId: this.id };
313
- const spanContext = this.telemetry?.span.spanContext();
314
- if (this.telemetry?.span.isRecording() && spanContext) {
315
- metadata.telemetry = {
316
- traceId: spanContext.traceId,
317
- spanId: spanContext.spanId
318
- };
613
+ onHandshakeData = (msg) => {
614
+ const parsedMsg = this.parseMsg(msg);
615
+ if (parsedMsg === null) {
616
+ this.listeners.onInvalidHandshake("could not parse message");
617
+ return;
319
618
  }
320
- return metadata;
321
- }
322
- };
323
- var Session = class {
324
- codec;
325
- options;
326
- telemetry;
327
- /**
328
- * The buffer of messages that have been sent but not yet acknowledged.
329
- */
330
- sendBuffer = [];
331
- /**
332
- * The active connection associated with this session
333
- */
334
- connection;
335
- /**
336
- * A connection that is currently undergoing handshaking. Used to distinguish between the active
337
- * connection, but still be able to close it if needed.
338
- */
339
- handshakingConnection;
340
- from;
341
- to;
342
- /**
343
- * The unique ID of this session.
344
- */
345
- id;
346
- /**
347
- * What the other side advertised as their session ID
348
- * for this session.
349
- */
350
- advertisedSessionId;
351
- /**
352
- * Number of messages we've sent along this session (excluding handshake and acks)
353
- */
354
- seq = 0;
355
- /**
356
- * Number of unique messages we've received this session (excluding handshake and acks)
357
- */
358
- ack = 0;
359
- /**
360
- * The grace period between when the inner connection is disconnected
361
- * and when we should consider the entire session disconnected.
362
- */
363
- disconnectionGrace;
364
- /**
365
- * Number of heartbeats we've sent without a response.
366
- */
367
- heartbeatMisses;
368
- /**
369
- * The interval for sending heartbeats.
370
- */
371
- heartbeat;
372
- log;
373
- constructor(conn, from, to, options, propagationCtx) {
374
- this.id = `session-${nanoid2(12)}`;
375
- this.options = options;
376
- this.from = from;
377
- this.to = to;
378
- this.connection = conn;
379
- this.codec = options.codec;
380
- this.heartbeatMisses = 0;
381
- this.heartbeat = setInterval(
382
- () => this.sendHeartbeat(),
383
- options.heartbeatIntervalMs
384
- );
385
- this.telemetry = createSessionTelemetryInfo(this, propagationCtx);
386
- }
387
- bindLogger(log) {
388
- this.log = log;
389
- }
619
+ this.listeners.onHandshake(parsedMsg);
620
+ };
390
621
  get loggingMetadata() {
391
- const spanContext = this.telemetry.span.spanContext();
392
622
  return {
393
623
  clientId: this.from,
394
- connectedTo: this.to,
395
- sessionId: this.id,
396
- connId: this.connection?.id,
397
- telemetry: {
398
- traceId: spanContext.traceId,
399
- spanId: spanContext.spanId
400
- }
624
+ connId: this.conn.id
401
625
  };
402
626
  }
403
- /**
404
- * Sends a message over the session's connection.
405
- * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
406
- *
407
- * @param msg The partial message to be sent, which will be constructed into a full message.
408
- * @param addToSendBuff Whether to add the message to the send buffer for retry.
409
- * @returns The full transport ID of the message that was attempted to be sent.
410
- */
627
+ sendHandshake(msg) {
628
+ return this.conn.send(this.options.codec.toBuffer(msg));
629
+ }
630
+ _handleStateExit() {
631
+ this.conn.removeDataListener(this.onHandshakeData);
632
+ this.conn.removeErrorListener(this.listeners.onConnectionErrored);
633
+ this.conn.removeCloseListener(this.listeners.onConnectionClosed);
634
+ clearTimeout(this.handshakeTimeout);
635
+ this.handshakeTimeout = void 0;
636
+ }
637
+ _handleClose() {
638
+ this.conn.close();
639
+ }
640
+ };
641
+
642
+ // transport/sessionStateMachine/SessionHandshaking.ts
643
+ var SessionHandshaking = class extends IdentifiedSession {
644
+ state = "Handshaking" /* Handshaking */;
645
+ conn;
646
+ listeners;
647
+ handshakeTimeout;
648
+ constructor(conn, listeners, ...args) {
649
+ super(...args);
650
+ this.conn = conn;
651
+ this.listeners = listeners;
652
+ this.handshakeTimeout = setTimeout(() => {
653
+ listeners.onHandshakeTimeout();
654
+ }, this.options.handshakeTimeoutMs);
655
+ this.conn.addDataListener(this.onHandshakeData);
656
+ this.conn.addErrorListener(listeners.onConnectionErrored);
657
+ this.conn.addCloseListener(listeners.onConnectionClosed);
658
+ }
659
+ onHandshakeData = (msg) => {
660
+ const parsedMsg = this.parseMsg(msg);
661
+ if (parsedMsg === null) {
662
+ this.listeners.onInvalidHandshake("could not parse message");
663
+ return;
664
+ }
665
+ this.listeners.onHandshake(parsedMsg);
666
+ };
667
+ sendHandshake(msg) {
668
+ return this.conn.send(this.options.codec.toBuffer(msg));
669
+ }
670
+ _handleStateExit() {
671
+ super._handleStateExit();
672
+ this.conn.removeDataListener(this.onHandshakeData);
673
+ this.conn.removeErrorListener(this.listeners.onConnectionErrored);
674
+ this.conn.removeCloseListener(this.listeners.onConnectionClosed);
675
+ clearTimeout(this.handshakeTimeout);
676
+ }
677
+ _handleClose() {
678
+ super._handleClose();
679
+ this.conn.close();
680
+ }
681
+ };
682
+
683
+ // transport/sessionStateMachine/SessionConnected.ts
684
+ var import_api2 = require("@opentelemetry/api");
685
+ var SessionConnected = class extends IdentifiedSession {
686
+ state = "Connected" /* Connected */;
687
+ conn;
688
+ listeners;
689
+ heartbeatHandle;
690
+ heartbeatMisses = 0;
691
+ get isActivelyHeartbeating() {
692
+ return this.heartbeatHandle !== void 0;
693
+ }
694
+ updateBookkeeping(ack, seq) {
695
+ this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
696
+ this.ack = seq + 1;
697
+ this.heartbeatMisses = 0;
698
+ }
411
699
  send(msg) {
412
- const fullMsg = this.constructMsg(msg);
413
- this.log?.debug(`sending msg`, {
414
- ...this.loggingMetadata,
415
- transportMessage: fullMsg
416
- });
417
- if (this.connection) {
418
- const ok = this.connection.send(this.codec.toBuffer(fullMsg));
419
- if (ok)
420
- return fullMsg.id;
421
- this.log?.info(
422
- `failed to send msg to ${fullMsg.to}, connection is probably dead`,
423
- {
424
- ...this.loggingMetadata,
425
- transportMessage: fullMsg
426
- }
427
- );
428
- } else {
700
+ const constructedMsg = this.constructMsg(msg);
701
+ this.sendBuffer.push(constructedMsg);
702
+ this.conn.send(this.options.codec.toBuffer(constructedMsg));
703
+ return constructedMsg.id;
704
+ }
705
+ constructor(conn, listeners, ...args) {
706
+ super(...args);
707
+ this.conn = conn;
708
+ this.listeners = listeners;
709
+ this.conn.addDataListener(this.onMessageData);
710
+ this.conn.addCloseListener(listeners.onConnectionClosed);
711
+ this.conn.addErrorListener(listeners.onConnectionErrored);
712
+ if (this.sendBuffer.length > 0) {
429
713
  this.log?.debug(
430
- `buffering msg to ${fullMsg.to}, connection not ready yet`,
431
- { ...this.loggingMetadata, transportMessage: fullMsg }
714
+ `sending ${this.sendBuffer.length} buffered messages`,
715
+ this.loggingMetadata
432
716
  );
433
717
  }
434
- return fullMsg.id;
718
+ for (const msg of this.sendBuffer) {
719
+ conn.send(this.options.codec.toBuffer(msg));
720
+ }
435
721
  }
436
- sendHeartbeat() {
437
- const misses = this.heartbeatMisses;
438
- const missDuration = misses * this.options.heartbeatIntervalMs;
439
- if (misses > this.options.heartbeatsUntilDead) {
440
- if (this.connection) {
722
+ startActiveHeartbeat() {
723
+ this.heartbeatHandle = setInterval(() => {
724
+ const misses = this.heartbeatMisses;
725
+ const missDuration = misses * this.options.heartbeatIntervalMs;
726
+ if (misses >= this.options.heartbeatsUntilDead) {
441
727
  this.log?.info(
442
728
  `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
443
729
  this.loggingMetadata
444
730
  );
445
731
  this.telemetry.span.addEvent("closing connection due to inactivity");
446
- this.closeStaleConnection();
732
+ this.conn.close();
733
+ clearInterval(this.heartbeatHandle);
734
+ this.heartbeatHandle = void 0;
735
+ return;
447
736
  }
448
- return;
449
- }
737
+ this.sendHeartbeat();
738
+ this.heartbeatMisses++;
739
+ }, this.options.heartbeatIntervalMs);
740
+ }
741
+ sendHeartbeat() {
450
742
  this.send({
451
743
  streamId: "heartbeat",
452
744
  controlFlags: 1 /* AckBit */,
@@ -454,586 +746,293 @@ var Session = class {
454
746
  type: "ACK"
455
747
  }
456
748
  });
457
- this.heartbeatMisses++;
458
- }
459
- resetBufferedMessages() {
460
- this.sendBuffer = [];
461
- this.seq = 0;
462
- this.ack = 0;
463
749
  }
464
- sendBufferedMessages(conn) {
465
- this.log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
466
- ...this.loggingMetadata,
467
- connId: conn.id
468
- });
469
- for (const msg of this.sendBuffer) {
470
- this.log?.debug(`resending msg`, {
471
- ...this.loggingMetadata,
472
- transportMessage: msg,
473
- connId: conn.id
474
- });
475
- const ok = conn.send(this.codec.toBuffer(msg));
476
- if (!ok) {
477
- const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;
478
- conn.telemetry?.span.setStatus({
479
- code: import_api2.SpanStatusCode.ERROR,
480
- message: errMsg
481
- });
482
- this.log?.error(errMsg, {
750
+ onMessageData = (msg) => {
751
+ const parsedMsg = this.parseMsg(msg);
752
+ if (parsedMsg === null)
753
+ return;
754
+ if (parsedMsg.seq !== this.ack) {
755
+ if (parsedMsg.seq < this.ack) {
756
+ this.log?.debug(
757
+ `received duplicate msg (got seq: ${parsedMsg.seq}, wanted seq: ${this.ack}), discarding`,
758
+ {
759
+ ...this.loggingMetadata,
760
+ transportMessage: parsedMsg
761
+ }
762
+ );
763
+ } else {
764
+ const reason = `received out-of-order msg (got seq: ${parsedMsg.seq}, wanted seq: ${this.ack})`;
765
+ this.log?.error(reason, {
483
766
  ...this.loggingMetadata,
484
- transportMessage: msg,
485
- connId: conn.id,
767
+ transportMessage: parsedMsg,
486
768
  tags: ["invariant-violation"]
487
769
  });
488
- conn.close();
489
- return;
770
+ this.telemetry.span.setStatus({
771
+ code: import_api2.SpanStatusCode.ERROR,
772
+ message: reason
773
+ });
774
+ this.listeners.onInvalidMessage(reason);
490
775
  }
491
- }
492
- }
493
- updateBookkeeping(ack, seq) {
494
- if (seq + 1 < this.ack) {
495
- this.log?.error(`received stale seq ${seq} + 1 < ${this.ack}`, {
496
- ...this.loggingMetadata,
497
- tags: ["invariant-violation"]
498
- });
499
776
  return;
500
777
  }
501
- this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
502
- this.ack = seq + 1;
503
- }
504
- closeStaleConnection(conn) {
505
- if (this.connection === void 0 || this.connection === conn)
506
- return;
507
- this.log?.info(
508
- `closing old inner connection from session to ${this.to}`,
509
- this.loggingMetadata
510
- );
511
- this.connection.close();
512
- this.connection = void 0;
513
- }
514
- replaceWithNewConnection(newConn, isTransparentReconnect) {
515
- this.closeStaleConnection(newConn);
516
- this.cancelGrace();
517
- if (isTransparentReconnect) {
518
- this.sendBufferedMessages(newConn);
519
- }
520
- this.connection = newConn;
521
- this.handshakingConnection = void 0;
522
- }
523
- replaceWithNewHandshakingConnection(newConn) {
524
- this.handshakingConnection = newConn;
525
- }
526
- beginGrace(cb) {
527
- this.log?.info(
528
- `starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
529
- this.loggingMetadata
530
- );
531
- this.cancelGrace();
532
- this.disconnectionGrace = setTimeout(() => {
533
- this.log?.info(
534
- `grace period for ${this.to} elapsed`,
535
- this.loggingMetadata
536
- );
537
- cb();
538
- }, this.options.sessionDisconnectGraceMs);
539
- }
540
- // called on reconnect of the underlying session
541
- cancelGrace() {
542
- this.heartbeatMisses = 0;
543
- clearTimeout(this.disconnectionGrace);
544
- this.disconnectionGrace = void 0;
545
- }
546
- /**
547
- * Used to close the handshaking connection, if set.
548
- */
549
- closeHandshakingConnection(expectedHandshakingConn) {
550
- if (this.handshakingConnection === void 0)
551
- return;
552
- if (expectedHandshakingConn !== void 0 && this.handshakingConnection === expectedHandshakingConn) {
778
+ this.log?.debug(`received msg`, {
779
+ ...this.loggingMetadata,
780
+ transportMessage: parsedMsg
781
+ });
782
+ this.updateBookkeeping(parsedMsg.ack, parsedMsg.seq);
783
+ if (!isAck(parsedMsg.controlFlags)) {
784
+ this.listeners.onMessage(parsedMsg);
553
785
  return;
554
786
  }
555
- this.handshakingConnection.close();
556
- this.handshakingConnection = void 0;
557
- }
558
- // closed when we want to discard the whole session
559
- // (i.e. shutdown or session disconnect)
560
- close() {
561
- this.closeStaleConnection();
562
- this.cancelGrace();
563
- this.resetBufferedMessages();
564
- clearInterval(this.heartbeat);
565
- }
566
- get connected() {
567
- return this.connection !== void 0;
568
- }
569
- get nextExpectedAck() {
570
- return this.seq;
571
- }
572
- get nextExpectedSeq() {
573
- return this.ack;
574
- }
575
- /**
576
- * Check that the peer's next expected seq number matches something that is in our send buffer
577
- * _or_ matches our actual next seq.
578
- */
579
- nextExpectedSeqInRange(nextExpectedSeq) {
580
- for (const msg of this.sendBuffer) {
581
- if (nextExpectedSeq === msg.seq) {
582
- return true;
583
- }
787
+ this.log?.debug(`discarding msg (ack bit set)`, {
788
+ ...this.loggingMetadata,
789
+ transportMessage: parsedMsg
790
+ });
791
+ if (!this.isActivelyHeartbeating) {
792
+ this.sendHeartbeat();
584
793
  }
585
- return nextExpectedSeq === this.seq;
586
- }
587
- // This is only used in tests to make the session misbehave.
588
- /* @internal */
589
- advanceAckForTesting(by) {
590
- this.ack += by;
591
- }
592
- constructMsg(partialMsg) {
593
- const msg = {
594
- ...partialMsg,
595
- id: unsafeId(),
596
- to: this.to,
597
- from: this.from,
598
- seq: this.seq,
599
- ack: this.ack
600
- };
601
- this.seq++;
602
- this.sendBuffer.push(msg);
603
- return msg;
604
- }
605
- inspectSendBuffer() {
606
- return this.sendBuffer;
794
+ };
795
+ _handleStateExit() {
796
+ super._handleStateExit();
797
+ this.conn.removeDataListener(this.onMessageData);
798
+ this.conn.removeCloseListener(this.listeners.onConnectionClosed);
799
+ this.conn.removeErrorListener(this.listeners.onConnectionErrored);
800
+ clearInterval(this.heartbeatHandle);
801
+ this.heartbeatHandle = void 0;
802
+ }
803
+ _handleClose() {
804
+ super._handleClose();
805
+ this.conn.close();
607
806
  }
608
807
  };
609
808
 
610
- // transport/transport.ts
611
- var import_api3 = require("@opentelemetry/api");
612
-
613
- // codec/json.ts
614
- var encoder = new TextEncoder();
615
- var decoder = new TextDecoder();
616
- function uint8ArrayToBase64(uint8Array) {
617
- let binary = "";
618
- uint8Array.forEach((byte) => {
619
- binary += String.fromCharCode(byte);
620
- });
621
- return btoa(binary);
622
- }
623
- function base64ToUint8Array(base64) {
624
- const binaryString = atob(base64);
625
- const uint8Array = new Uint8Array(binaryString.length);
626
- for (let i = 0; i < binaryString.length; i++) {
627
- uint8Array[i] = binaryString.charCodeAt(i);
628
- }
629
- return uint8Array;
809
+ // transport/sessionStateMachine/transitions.ts
810
+ function inheritSharedSession(session) {
811
+ return [
812
+ session.id,
813
+ session.from,
814
+ session.to,
815
+ session.seq,
816
+ session.ack,
817
+ session.sendBuffer,
818
+ session.telemetry,
819
+ session.options,
820
+ session.log
821
+ ];
630
822
  }
631
- var NaiveJsonCodec = {
632
- toBuffer: (obj) => {
633
- return encoder.encode(
634
- JSON.stringify(obj, function replacer(key) {
635
- const val = this[key];
636
- if (val instanceof Uint8Array) {
637
- return { $t: uint8ArrayToBase64(val) };
638
- } else {
639
- return val;
640
- }
641
- })
642
- );
823
+ var SessionStateGraph = {
824
+ entrypoints: {
825
+ NoConnection(to, from, listeners, options, log) {
826
+ const id = `session-${generateId()}`;
827
+ const telemetry = createSessionTelemetryInfo(id, to, from);
828
+ const sendBuffer = [];
829
+ const session = new SessionNoConnection(
830
+ listeners,
831
+ id,
832
+ from,
833
+ to,
834
+ 0,
835
+ 0,
836
+ sendBuffer,
837
+ telemetry,
838
+ options,
839
+ log
840
+ );
841
+ session.log?.info(`session ${session.id} created in NoConnection state`, {
842
+ ...session.loggingMetadata,
843
+ tags: ["state-transition"]
844
+ });
845
+ return session;
846
+ },
847
+ WaitingForHandshake(from, conn, listeners, options, log) {
848
+ const session = new SessionWaitingForHandshake(
849
+ conn,
850
+ listeners,
851
+ from,
852
+ options,
853
+ log
854
+ );
855
+ session.log?.info(`session created in WaitingForHandshake state`, {
856
+ ...session.loggingMetadata,
857
+ tags: ["state-transition"]
858
+ });
859
+ return session;
860
+ }
643
861
  },
644
- fromBuffer: (buff) => {
645
- try {
646
- const parsed = JSON.parse(
647
- decoder.decode(buff),
648
- function reviver(_key, val) {
649
- if (val?.$t) {
650
- return base64ToUint8Array(val.$t);
651
- } else {
652
- return val;
653
- }
862
+ // All of the transitions 'move'/'consume' the old session and return a new one.
863
+ // After a session is transitioned, any usage of the old session will throw.
864
+ transition: {
865
+ // happy path transitions
866
+ NoConnectionToConnecting(oldSession, connPromise, listeners) {
867
+ const carriedState = inheritSharedSession(oldSession);
868
+ oldSession._handleStateExit();
869
+ const session = new SessionConnecting(
870
+ connPromise,
871
+ listeners,
872
+ ...carriedState
873
+ );
874
+ session.log?.info(
875
+ `session ${session.id} transition from NoConnection to Connecting`,
876
+ {
877
+ ...session.loggingMetadata,
878
+ tags: ["state-transition"]
654
879
  }
655
880
  );
656
- if (typeof parsed === "object")
657
- return parsed;
658
- return null;
659
- } catch {
660
- return null;
661
- }
662
- }
663
- };
664
-
665
- // transport/options.ts
666
- var defaultTransportOptions = {
667
- heartbeatIntervalMs: 1e3,
668
- heartbeatsUntilDead: 2,
669
- sessionDisconnectGraceMs: 5e3,
670
- handshakeTimeoutMs: 5e3,
671
- codec: NaiveJsonCodec
672
- };
673
- var defaultConnectionRetryOptions = {
674
- baseIntervalMs: 250,
675
- maxJitterMs: 200,
676
- maxBackoffMs: 32e3,
677
- attemptBudgetCapacity: 5,
678
- budgetRestoreIntervalMs: 200
679
- };
680
- var defaultClientTransportOptions = {
681
- ...defaultTransportOptions,
682
- ...defaultConnectionRetryOptions
683
- };
684
- var defaultServerTransportOptions = {
685
- ...defaultTransportOptions
686
- };
687
-
688
- // transport/transport.ts
689
- var Transport = class {
690
- /**
691
- * The status of the transport.
692
- */
693
- status;
694
- /**
695
- * The {@link Codec} used to encode and decode messages.
696
- */
697
- codec;
698
- /**
699
- * The client ID of this transport.
700
- */
701
- clientId;
702
- /**
703
- * The map of {@link Session}s managed by this transport.
704
- */
705
- sessions;
706
- /**
707
- * The map of {@link Connection}s managed by this transport.
708
- */
709
- get connections() {
710
- return new Map(
711
- [...this.sessions].map(([client, session]) => [client, session.connection]).filter((entry) => entry[1] !== void 0)
712
- );
713
- }
714
- /**
715
- * The event dispatcher for handling events of type EventTypes.
716
- */
717
- eventDispatcher;
718
- /**
719
- * The options for this transport.
720
- */
721
- options;
722
- log;
723
- /**
724
- * Creates a new Transport instance.
725
- * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
726
- * @param codec The codec used to encode and decode messages.
727
- * @param clientId The client ID of this transport.
728
- */
729
- constructor(clientId, providedOptions) {
730
- this.options = { ...defaultTransportOptions, ...providedOptions };
731
- this.eventDispatcher = new EventDispatcher();
732
- this.sessions = /* @__PURE__ */ new Map();
733
- this.codec = this.options.codec;
734
- this.clientId = clientId;
735
- this.status = "open";
736
- }
737
- bindLogger(fn, level) {
738
- if (typeof fn === "function") {
739
- this.log = createLogProxy(new BaseLogger(fn, level));
740
- return;
741
- }
742
- this.log = createLogProxy(fn);
743
- }
744
- /**
745
- * Called when a new connection is established
746
- * and we know the identity of the connected client.
747
- * @param conn The connection object.
748
- */
749
- onConnect(conn, session, isTransparentReconnect) {
750
- this.eventDispatcher.dispatchEvent("connectionStatus", {
751
- status: "connect",
752
- conn
753
- });
754
- conn.telemetry = createConnectionTelemetryInfo(conn, session.telemetry);
755
- session.replaceWithNewConnection(conn, isTransparentReconnect);
756
- this.log?.info(`connected to ${session.to}`, {
757
- ...conn.loggingMetadata,
758
- ...session.loggingMetadata
759
- });
760
- }
761
- createSession(to, conn, propagationCtx) {
762
- const session = new Session(
763
- conn,
764
- this.clientId,
765
- to,
766
- this.options,
767
- propagationCtx
768
- );
769
- if (this.log) {
770
- session.bindLogger(this.log);
771
- }
772
- const currentSession = this.sessions.get(session.to);
773
- if (currentSession) {
774
- this.log?.warn(
775
- `session ${session.id} from ${session.to} surreptitiously replacing ${currentSession.id}`,
881
+ return session;
882
+ },
883
+ ConnectingToHandshaking(oldSession, conn, listeners) {
884
+ const carriedState = inheritSharedSession(oldSession);
885
+ oldSession._handleStateExit();
886
+ const session = new SessionHandshaking(conn, listeners, ...carriedState);
887
+ session.log?.info(
888
+ `session ${session.id} transition from Connecting to Handshaking`,
776
889
  {
777
- ...currentSession.loggingMetadata,
778
- tags: ["invariant-violation"]
890
+ ...session.loggingMetadata,
891
+ tags: ["state-transition"]
779
892
  }
780
893
  );
781
- this.deleteSession({
782
- session: currentSession,
783
- closeHandshakingConnection: false
784
- });
785
- }
786
- this.sessions.set(session.to, session);
787
- this.eventDispatcher.dispatchEvent("sessionStatus", {
788
- status: "connect",
789
- session
790
- });
791
- return session;
792
- }
793
- createNewSession({
794
- to,
795
- conn,
796
- sessionId,
797
- propagationCtx
798
- }) {
799
- let session = this.sessions.get(to);
800
- if (session !== void 0) {
801
- this.log?.info(
802
- `session for ${to} already exists, replacing it with a new session as requested`,
803
- session.loggingMetadata
894
+ return session;
895
+ },
896
+ HandshakingToConnected(oldSession, listeners) {
897
+ const carriedState = inheritSharedSession(oldSession);
898
+ const conn = oldSession.conn;
899
+ oldSession._handleStateExit();
900
+ const session = new SessionConnected(conn, listeners, ...carriedState);
901
+ session.log?.info(
902
+ `session ${session.id} transition from Handshaking to Connected`,
903
+ {
904
+ ...session.loggingMetadata,
905
+ tags: ["state-transition"]
906
+ }
804
907
  );
805
- this.deleteSession({
806
- session,
807
- closeHandshakingConnection: false
808
- });
809
- session = void 0;
810
- }
811
- session = this.createSession(to, conn, propagationCtx);
812
- session.advertisedSessionId = sessionId;
813
- this.log?.info(`created new session for ${to}`, session.loggingMetadata);
814
- return session;
815
- }
816
- getExistingSession({
817
- to,
818
- sessionId,
819
- nextExpectedSeq
820
- }) {
821
- const session = this.sessions.get(to);
822
- if (
823
- // reject this request if there was no previous session to replace
824
- session === void 0 || // or if both parties do not agree about the next expected sequence number
825
- !session.nextExpectedSeqInRange(nextExpectedSeq) || // or if both parties do not agree on the advertised session id
826
- session.advertisedSessionId !== sessionId
827
- ) {
828
- return false;
829
- }
830
- this.log?.info(
831
- `reused existing session for ${to}`,
832
- session.loggingMetadata
833
- );
834
- return session;
835
- }
836
- getOrCreateSession({
837
- to,
838
- conn,
839
- handshakingConn,
840
- sessionId,
841
- propagationCtx
842
- }) {
843
- let session = this.sessions.get(to);
844
- const isReconnect = session !== void 0;
845
- let isTransparentReconnect = isReconnect;
846
- if (session?.advertisedSessionId !== void 0 && sessionId !== void 0 && session.advertisedSessionId !== sessionId) {
847
- this.log?.info(
848
- `session for ${to} already exists but has a different session id (expected: ${session.advertisedSessionId}, got: ${sessionId}), creating a new one`,
849
- session.loggingMetadata
908
+ return session;
909
+ },
910
+ WaitingForHandshakeToConnected(pendingSession, oldSession, sessionId, to, propagationCtx, listeners) {
911
+ const conn = pendingSession.conn;
912
+ const { from, options } = pendingSession;
913
+ const carriedState = oldSession ? (
914
+ // old session exists, inherit state
915
+ inheritSharedSession(oldSession)
916
+ ) : (
917
+ // old session does not exist, create new state
918
+ [
919
+ sessionId,
920
+ from,
921
+ to,
922
+ 0,
923
+ 0,
924
+ [],
925
+ createSessionTelemetryInfo(sessionId, to, from, propagationCtx),
926
+ options,
927
+ pendingSession.log
928
+ ]
850
929
  );
851
- this.deleteSession({
852
- session,
853
- closeHandshakingConnection: handshakingConn !== void 0,
854
- handshakingConn
855
- });
856
- isTransparentReconnect = false;
857
- session = void 0;
858
- }
859
- if (!session) {
860
- session = this.createSession(to, conn, propagationCtx);
861
- this.log?.info(
862
- `no session for ${to}, created a new one`,
863
- session.loggingMetadata
930
+ pendingSession._handleStateExit();
931
+ oldSession?._handleStateExit();
932
+ const session = new SessionConnected(conn, listeners, ...carriedState);
933
+ session.log?.info(
934
+ `session ${session.id} transition from WaitingForHandshake to Connected`,
935
+ {
936
+ ...session.loggingMetadata,
937
+ tags: ["state-transition"]
938
+ }
939
+ );
940
+ return session;
941
+ },
942
+ // disconnect paths
943
+ ConnectingToNoConnection(oldSession, listeners) {
944
+ const carriedState = inheritSharedSession(oldSession);
945
+ oldSession.bestEffortClose();
946
+ oldSession._handleStateExit();
947
+ const session = new SessionNoConnection(listeners, ...carriedState);
948
+ session.log?.info(
949
+ `session ${session.id} transition from Connecting to NoConnection`,
950
+ {
951
+ ...session.loggingMetadata,
952
+ tags: ["state-transition"]
953
+ }
954
+ );
955
+ return session;
956
+ },
957
+ HandshakingToNoConnection(oldSession, listeners) {
958
+ const carriedState = inheritSharedSession(oldSession);
959
+ oldSession.conn.close();
960
+ oldSession._handleStateExit();
961
+ const session = new SessionNoConnection(listeners, ...carriedState);
962
+ session.log?.info(
963
+ `session ${session.id} transition from Handshaking to NoConnection`,
964
+ {
965
+ ...session.loggingMetadata,
966
+ tags: ["state-transition"]
967
+ }
864
968
  );
865
- }
866
- if (sessionId !== void 0) {
867
- session.advertisedSessionId = sessionId;
868
- }
869
- if (handshakingConn !== void 0) {
870
- session.replaceWithNewHandshakingConnection(handshakingConn);
871
- }
872
- return { session, isReconnect, isTransparentReconnect };
873
- }
874
- deleteSession({
875
- session,
876
- closeHandshakingConnection,
877
- handshakingConn
878
- }) {
879
- if (closeHandshakingConnection) {
880
- session.closeHandshakingConnection(handshakingConn);
881
- }
882
- session.close();
883
- session.telemetry.span.end();
884
- const currentSession = this.sessions.get(session.to);
885
- if (currentSession && currentSession.id !== session.id) {
886
- this.log?.warn(
887
- `session ${session.id} disconnect from ${session.to}, mismatch with ${currentSession.id}`,
969
+ return session;
970
+ },
971
+ ConnectedToNoConnection(oldSession, listeners) {
972
+ const carriedState = inheritSharedSession(oldSession);
973
+ oldSession.conn.close();
974
+ oldSession._handleStateExit();
975
+ const session = new SessionNoConnection(listeners, ...carriedState);
976
+ session.log?.info(
977
+ `session ${session.id} transition from Connected to NoConnection`,
888
978
  {
889
979
  ...session.loggingMetadata,
890
- tags: ["invariant-violation"]
980
+ tags: ["state-transition"]
891
981
  }
892
982
  );
893
- return;
983
+ return session;
894
984
  }
895
- this.sessions.delete(session.to);
896
- this.log?.info(
897
- `session ${session.id} disconnect from ${session.to}`,
898
- session.loggingMetadata
899
- );
900
- this.eventDispatcher.dispatchEvent("sessionStatus", {
901
- status: "disconnect",
902
- session
903
- });
904
985
  }
986
+ };
987
+
988
+ // transport/transport.ts
989
+ var Transport = class {
905
990
  /**
906
- * The downstream implementation needs to call this when a connection is closed.
907
- * @param conn The connection object.
908
- * @param connectedTo The peer we are connected to.
991
+ * The status of the transport.
909
992
  */
910
- onDisconnect(conn, session) {
911
- if (session.connection !== void 0 && session.connection.id !== conn.id) {
912
- session.telemetry.span.addEvent("onDisconnect race");
913
- this.log?.warn("onDisconnect race", {
914
- clientId: this.clientId,
915
- ...session.loggingMetadata,
916
- ...conn.loggingMetadata,
917
- tags: ["invariant-violation"]
918
- });
919
- return;
920
- }
921
- conn.telemetry?.span.end();
922
- this.eventDispatcher.dispatchEvent("connectionStatus", {
923
- status: "disconnect",
924
- conn
925
- });
926
- session.connection = void 0;
927
- session.beginGrace(() => {
928
- if (session.connection !== void 0) {
929
- session.telemetry.span.addEvent("session grace period race");
930
- this.log?.warn("session grace period race", {
931
- clientId: this.clientId,
932
- ...session.loggingMetadata,
933
- ...conn.loggingMetadata,
934
- tags: ["invariant-violation"]
935
- });
936
- return;
937
- }
938
- session.telemetry.span.addEvent("session grace period expired");
939
- this.deleteSession({
940
- session,
941
- closeHandshakingConnection: true,
942
- handshakingConn: conn
943
- });
944
- });
945
- }
993
+ status;
946
994
  /**
947
- * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
948
- * @param msg The message to parse.
949
- * @returns The parsed message, or null if the message is malformed or invalid.
995
+ * The client ID of this transport.
950
996
  */
951
- parseMsg(msg, conn) {
952
- const parsedMsg = this.codec.fromBuffer(msg);
953
- if (parsedMsg === null) {
954
- const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));
955
- this.log?.error(
956
- `received malformed msg, killing conn: ${decodedBuffer}`,
957
- {
958
- clientId: this.clientId,
959
- ...conn.loggingMetadata
960
- }
961
- );
962
- return null;
963
- }
964
- if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
965
- this.log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
966
- clientId: this.clientId,
967
- ...conn.loggingMetadata,
968
- validationErrors: [
969
- ...import_value.Value.Errors(OpaqueTransportMessageSchema, parsedMsg)
970
- ]
971
- });
972
- return null;
997
+ clientId;
998
+ /**
999
+ * The event dispatcher for handling events of type EventTypes.
1000
+ */
1001
+ eventDispatcher;
1002
+ /**
1003
+ * The options for this transport.
1004
+ */
1005
+ options;
1006
+ log;
1007
+ sessions;
1008
+ /**
1009
+ * Creates a new Transport instance.
1010
+ * @param codec The codec used to encode and decode messages.
1011
+ * @param clientId The client ID of this transport.
1012
+ */
1013
+ constructor(clientId, providedOptions) {
1014
+ this.options = { ...defaultTransportOptions, ...providedOptions };
1015
+ this.eventDispatcher = new EventDispatcher();
1016
+ this.clientId = clientId;
1017
+ this.status = "open";
1018
+ this.sessions = /* @__PURE__ */ new Map();
1019
+ }
1020
+ bindLogger(fn, level) {
1021
+ if (typeof fn === "function") {
1022
+ this.log = createLogProxy(new BaseLogger(fn, level));
1023
+ return;
973
1024
  }
974
- return parsedMsg;
1025
+ this.log = createLogProxy(fn);
975
1026
  }
976
1027
  /**
977
1028
  * Called when a message is received by this transport.
978
1029
  * You generally shouldn't need to override this in downstream transport implementations.
979
1030
  * @param msg The received message.
980
1031
  */
981
- handleMsg(msg, conn) {
1032
+ handleMsg(msg) {
982
1033
  if (this.getStatus() !== "open")
983
1034
  return;
984
- const session = this.sessions.get(msg.from);
985
- if (!session) {
986
- this.log?.error(`received message for unknown session from ${msg.from}`, {
987
- clientId: this.clientId,
988
- transportMessage: msg,
989
- ...conn.loggingMetadata,
990
- tags: ["invariant-violation"]
991
- });
992
- return;
993
- }
994
- session.cancelGrace();
995
- this.log?.debug(`received msg`, {
996
- clientId: this.clientId,
997
- transportMessage: msg,
998
- ...conn.loggingMetadata
999
- });
1000
- if (msg.seq !== session.nextExpectedSeq) {
1001
- if (msg.seq < session.nextExpectedSeq) {
1002
- this.log?.debug(
1003
- `received duplicate msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq}), discarding`,
1004
- {
1005
- clientId: this.clientId,
1006
- transportMessage: msg,
1007
- ...conn.loggingMetadata
1008
- }
1009
- );
1010
- } else {
1011
- const errMsg = `received out-of-order msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq})`;
1012
- this.log?.error(`${errMsg}, marking connection as dead`, {
1013
- clientId: this.clientId,
1014
- transportMessage: msg,
1015
- ...conn.loggingMetadata,
1016
- tags: ["invariant-violation"]
1017
- });
1018
- this.protocolError(ProtocolError.MessageOrderingViolated, errMsg);
1019
- session.telemetry.span.setStatus({
1020
- code: import_api3.SpanStatusCode.ERROR,
1021
- message: "message order violated"
1022
- });
1023
- this.deleteSession({ session, closeHandshakingConnection: true });
1024
- }
1025
- return;
1026
- }
1027
- session.updateBookkeeping(msg.ack, msg.seq);
1028
- if (!isAck(msg.controlFlags)) {
1029
- this.eventDispatcher.dispatchEvent("message", msg);
1030
- } else {
1031
- this.log?.debug(`discarding msg (ack bit set)`, {
1032
- clientId: this.clientId,
1033
- transportMessage: msg,
1034
- ...conn.loggingMetadata
1035
- });
1036
- }
1035
+ this.eventDispatcher.dispatchEvent("message", msg);
1037
1036
  }
1038
1037
  /**
1039
1038
  * Adds a listener to this transport.
@@ -1051,34 +1050,6 @@ var Transport = class {
1051
1050
  removeEventListener(type, handler) {
1052
1051
  this.eventDispatcher.removeEventListener(type, handler);
1053
1052
  }
1054
- /**
1055
- * Sends a message over this transport, delegating to the appropriate connection to actually
1056
- * send the message.
1057
- * @param msg The message to send.
1058
- * @returns The ID of the sent message or undefined if it wasn't sent
1059
- */
1060
- send(to, msg) {
1061
- if (this.getStatus() === "closed") {
1062
- const err = "transport is closed, cant send";
1063
- this.log?.error(err, {
1064
- clientId: this.clientId,
1065
- transportMessage: msg,
1066
- tags: ["invariant-violation"]
1067
- });
1068
- throw new Error(err);
1069
- }
1070
- return this.getOrCreateSession({ to }).session.send(msg);
1071
- }
1072
- // control helpers
1073
- sendCloseStream(to, streamId) {
1074
- return this.send(to, {
1075
- streamId,
1076
- controlFlags: 4 /* StreamClosedBit */,
1077
- payload: {
1078
- type: "CLOSE"
1079
- }
1080
- });
1081
- }
1082
1053
  protocolError(type, message) {
1083
1054
  this.eventDispatcher.dispatchEvent("protocolError", { type, message });
1084
1055
  }
@@ -1090,7 +1061,7 @@ var Transport = class {
1090
1061
  close() {
1091
1062
  this.status = "closed";
1092
1063
  for (const session of this.sessions.values()) {
1093
- this.deleteSession({ session, closeHandshakingConnection: true });
1064
+ this.deleteSession(session);
1094
1065
  }
1095
1066
  this.eventDispatcher.dispatchEvent("transportStatus", {
1096
1067
  status: this.status
@@ -1101,10 +1072,72 @@ var Transport = class {
1101
1072
  getStatus() {
1102
1073
  return this.status;
1103
1074
  }
1075
+ updateSession(session) {
1076
+ const activeSession = this.sessions.get(session.to);
1077
+ if (activeSession && activeSession.id !== session.id) {
1078
+ const msg = `attempt to transition active session for ${session.to} but active session (${activeSession.id}) is different from handle (${session.id})`;
1079
+ throw new Error(msg);
1080
+ }
1081
+ this.sessions.set(session.to, session);
1082
+ if (!activeSession) {
1083
+ this.eventDispatcher.dispatchEvent("sessionStatus", {
1084
+ status: "connect",
1085
+ session
1086
+ });
1087
+ }
1088
+ this.eventDispatcher.dispatchEvent("sessionTransition", {
1089
+ state: session.state,
1090
+ session
1091
+ });
1092
+ return session;
1093
+ }
1094
+ // state transitions
1095
+ deleteSession(session) {
1096
+ session.log?.info(`closing session ${session.id}`, session.loggingMetadata);
1097
+ this.eventDispatcher.dispatchEvent("sessionStatus", {
1098
+ status: "disconnect",
1099
+ session
1100
+ });
1101
+ session.close();
1102
+ this.sessions.delete(session.to);
1103
+ }
1104
+ // common listeners
1105
+ onSessionGracePeriodElapsed(session) {
1106
+ this.log?.warn(
1107
+ `session to ${session.to} grace period elapsed, closing`,
1108
+ session.loggingMetadata
1109
+ );
1110
+ this.deleteSession(session);
1111
+ }
1112
+ onConnectingFailed(session) {
1113
+ const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(session, {
1114
+ onSessionGracePeriodElapsed: () => {
1115
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1116
+ }
1117
+ });
1118
+ return this.updateSession(noConnectionSession);
1119
+ }
1120
+ onConnClosed(session) {
1121
+ let noConnectionSession;
1122
+ if (session.state === "Handshaking" /* Handshaking */) {
1123
+ noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(session, {
1124
+ onSessionGracePeriodElapsed: () => {
1125
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1126
+ }
1127
+ });
1128
+ } else {
1129
+ noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(session, {
1130
+ onSessionGracePeriodElapsed: () => {
1131
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1132
+ }
1133
+ });
1134
+ }
1135
+ return this.updateSession(noConnectionSession);
1136
+ }
1104
1137
  };
1105
1138
 
1106
1139
  // transport/client.ts
1107
- var import_api4 = require("@opentelemetry/api");
1140
+ var import_api3 = require("@opentelemetry/api");
1108
1141
 
1109
1142
  // transport/rateLimit.ts
1110
1143
  var LeakyBucketRateLimit = class {
@@ -1192,10 +1225,6 @@ var ClientTransport = class extends Transport {
1192
1225
  * The options for this transport.
1193
1226
  */
1194
1227
  options;
1195
- /**
1196
- * The map of reconnect promises for each client ID.
1197
- */
1198
- inflightConnectionPromises;
1199
1228
  retryBudget;
1200
1229
  /**
1201
1230
  * A flag indicating whether the transport should automatically reconnect
@@ -1214,352 +1243,278 @@ var ClientTransport = class extends Transport {
1214
1243
  ...defaultClientTransportOptions,
1215
1244
  ...providedOptions
1216
1245
  };
1217
- this.inflightConnectionPromises = /* @__PURE__ */ new Map();
1218
1246
  this.retryBudget = new LeakyBucketRateLimit(this.options);
1219
1247
  }
1220
1248
  extendHandshake(options) {
1221
1249
  this.handshakeExtensions = options;
1222
1250
  }
1223
- handleConnection(conn, to) {
1224
- if (this.getStatus() !== "open")
1225
- return;
1226
- let session = void 0;
1227
- const handshakeTimeout = setTimeout(() => {
1228
- if (session)
1229
- return;
1230
- this.log?.warn(
1231
- `connection to ${to} timed out waiting for handshake, closing`,
1232
- { ...conn.loggingMetadata, clientId: this.clientId, connectedTo: to }
1233
- );
1234
- conn.close();
1235
- }, this.options.handshakeTimeoutMs);
1236
- const handshakeHandler = (data) => {
1237
- const maybeSession = this.receiveHandshakeResponseMessage(data, conn);
1238
- clearTimeout(handshakeTimeout);
1239
- if (!maybeSession) {
1240
- conn.close();
1241
- return;
1242
- } else {
1243
- session = maybeSession;
1244
- }
1245
- conn.removeDataListener(handshakeHandler);
1246
- conn.addDataListener((data2) => {
1247
- const parsed = this.parseMsg(data2, conn);
1248
- if (!parsed) {
1249
- conn.telemetry?.span.setStatus({
1250
- code: import_api4.SpanStatusCode.ERROR,
1251
- message: "message parse failure"
1252
- });
1253
- conn.close();
1254
- return;
1255
- }
1256
- this.handleMsg(parsed, conn);
1251
+ tryReconnecting(to) {
1252
+ if (this.reconnectOnConnectionDrop && this.getStatus() === "open") {
1253
+ this.connect(to);
1254
+ }
1255
+ }
1256
+ send(to, msg) {
1257
+ if (this.getStatus() === "closed") {
1258
+ const err = "transport is closed, cant send";
1259
+ this.log?.error(err, {
1260
+ clientId: this.clientId,
1261
+ transportMessage: msg,
1262
+ tags: ["invariant-violation"]
1257
1263
  });
1258
- };
1259
- conn.addDataListener(handshakeHandler);
1260
- conn.addCloseListener(() => {
1261
- if (session) {
1262
- this.onDisconnect(conn, session);
1263
- }
1264
- const willReconnect = this.reconnectOnConnectionDrop && this.getStatus() === "open";
1265
- this.log?.info(
1266
- `connection to ${to} disconnected` + (willReconnect ? ", reconnecting" : ""),
1267
- {
1268
- ...conn.loggingMetadata,
1269
- ...session?.loggingMetadata,
1270
- clientId: this.clientId,
1271
- connectedTo: to
1264
+ throw new Error(err);
1265
+ }
1266
+ let session = this.sessions.get(to);
1267
+ if (!session) {
1268
+ session = this.createUnconnectedSession(to);
1269
+ }
1270
+ return session.send(msg);
1271
+ }
1272
+ createUnconnectedSession(to) {
1273
+ const session = SessionStateGraph.entrypoints.NoConnection(
1274
+ to,
1275
+ this.clientId,
1276
+ {
1277
+ onSessionGracePeriodElapsed: () => {
1278
+ this.onSessionGracePeriodElapsed(session);
1272
1279
  }
1273
- );
1274
- this.inflightConnectionPromises.delete(to);
1275
- if (this.reconnectOnConnectionDrop) {
1276
- void this.connect(to);
1280
+ },
1281
+ this.options,
1282
+ this.log
1283
+ );
1284
+ this.updateSession(session);
1285
+ return session;
1286
+ }
1287
+ // listeners
1288
+ onConnectingFailed(session) {
1289
+ const noConnectionSession = super.onConnectingFailed(session);
1290
+ this.tryReconnecting(noConnectionSession.to);
1291
+ return noConnectionSession;
1292
+ }
1293
+ onConnClosed(session) {
1294
+ const noConnectionSession = super.onConnClosed(session);
1295
+ this.tryReconnecting(noConnectionSession.to);
1296
+ return noConnectionSession;
1297
+ }
1298
+ onConnectionEstablished(session, conn) {
1299
+ const handshakingSession = SessionStateGraph.transition.ConnectingToHandshaking(session, conn, {
1300
+ onConnectionErrored: (err) => {
1301
+ const errStr = coerceErrorString(err);
1302
+ this.log?.error(
1303
+ `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
1304
+ handshakingSession.loggingMetadata
1305
+ );
1306
+ },
1307
+ onConnectionClosed: () => {
1308
+ this.log?.warn(
1309
+ `connection to ${handshakingSession.to} closed during handshake`,
1310
+ handshakingSession.loggingMetadata
1311
+ );
1312
+ this.onConnClosed(handshakingSession);
1313
+ },
1314
+ onHandshake: (msg) => {
1315
+ this.onHandshakeResponse(handshakingSession, msg);
1316
+ },
1317
+ onInvalidHandshake: (reason) => {
1318
+ this.log?.error(
1319
+ `invalid handshake: ${reason}`,
1320
+ handshakingSession.loggingMetadata
1321
+ );
1322
+ this.deleteSession(session);
1323
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
1324
+ },
1325
+ onHandshakeTimeout: () => {
1326
+ this.log?.error(
1327
+ `connection to ${handshakingSession.to} timed out during handshake`,
1328
+ handshakingSession.loggingMetadata
1329
+ );
1330
+ this.onConnClosed(handshakingSession);
1277
1331
  }
1278
1332
  });
1279
- conn.addErrorListener((err) => {
1280
- conn.telemetry?.span.setStatus({
1281
- code: import_api4.SpanStatusCode.ERROR,
1282
- message: "connection error"
1283
- });
1284
- this.log?.warn(
1285
- `error in connection to ${to}: ${coerceErrorString(err)}`,
1286
- {
1287
- ...conn.loggingMetadata,
1288
- ...session?.loggingMetadata,
1289
- clientId: this.clientId,
1290
- connectedTo: to
1291
- }
1292
- );
1333
+ this.updateSession(handshakingSession);
1334
+ void this.sendHandshake(handshakingSession);
1335
+ return handshakingSession;
1336
+ }
1337
+ rejectHandshakeResponse(session, reason, metadata) {
1338
+ session.conn.telemetry?.span.setStatus({
1339
+ code: import_api3.SpanStatusCode.ERROR,
1340
+ message: reason
1293
1341
  });
1342
+ this.log?.warn(reason, metadata);
1343
+ this.deleteSession(session);
1294
1344
  }
1295
- receiveHandshakeResponseMessage(data, conn) {
1296
- const parsed = this.parseMsg(data, conn);
1297
- if (!parsed) {
1298
- conn.telemetry?.span.setStatus({
1299
- code: import_api4.SpanStatusCode.ERROR,
1300
- message: "non-transport message"
1301
- });
1302
- this.protocolError(
1303
- ProtocolError.HandshakeFailed,
1304
- "received non-transport message"
1305
- );
1306
- return false;
1307
- }
1308
- if (!import_value2.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
1309
- conn.telemetry?.span.setStatus({
1310
- code: import_api4.SpanStatusCode.ERROR,
1311
- message: "invalid handshake response"
1312
- });
1313
- this.log?.warn(`received invalid handshake resp`, {
1314
- ...conn.loggingMetadata,
1315
- clientId: this.clientId,
1316
- connectedTo: parsed.from,
1317
- transportMessage: parsed,
1345
+ onHandshakeResponse(session, msg) {
1346
+ if (!import_value2.Value.Check(ControlMessageHandshakeResponseSchema, msg.payload)) {
1347
+ const reason = `received invalid handshake response`;
1348
+ this.rejectHandshakeResponse(session, reason, {
1349
+ ...session.loggingMetadata,
1350
+ transportMessage: msg,
1318
1351
  validationErrors: [
1319
- ...import_value2.Value.Errors(
1320
- ControlMessageHandshakeResponseSchema,
1321
- parsed.payload
1322
- )
1352
+ ...import_value2.Value.Errors(ControlMessageHandshakeResponseSchema, msg.payload)
1323
1353
  ]
1324
1354
  });
1325
- this.protocolError(
1326
- ProtocolError.HandshakeFailed,
1327
- "invalid handshake resp"
1328
- );
1329
- return false;
1355
+ return;
1330
1356
  }
1331
- const previousSession = this.sessions.get(parsed.from);
1332
- if (!parsed.payload.status.ok) {
1333
- if (parsed.payload.status.reason === SESSION_STATE_MISMATCH) {
1334
- if (previousSession) {
1335
- this.deleteSession({
1336
- session: previousSession,
1337
- closeHandshakingConnection: true
1338
- });
1339
- }
1340
- conn.telemetry?.span.setStatus({
1341
- code: import_api4.SpanStatusCode.ERROR,
1342
- message: parsed.payload.status.reason
1343
- });
1357
+ if (!msg.payload.status.ok) {
1358
+ const retriable = msg.payload.status.code ? import_value2.Value.Check(
1359
+ HandshakeErrorRetriableResponseCodes,
1360
+ msg.payload.status.code
1361
+ ) : false;
1362
+ const reason = `handshake failed: ${msg.payload.status.reason}`;
1363
+ this.rejectHandshakeResponse(session, reason, {
1364
+ ...session.loggingMetadata,
1365
+ transportMessage: msg
1366
+ });
1367
+ if (retriable) {
1368
+ this.tryReconnecting(session.to);
1344
1369
  } else {
1345
- conn.telemetry?.span.setStatus({
1346
- code: import_api4.SpanStatusCode.ERROR,
1347
- message: "handshake rejected"
1348
- });
1370
+ this.deleteSession(session);
1371
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
1349
1372
  }
1350
- this.log?.warn(
1351
- `received handshake rejection: ${parsed.payload.status.reason}`,
1352
- {
1353
- ...conn.loggingMetadata,
1354
- clientId: this.clientId,
1355
- connectedTo: parsed.from,
1356
- transportMessage: parsed
1357
- }
1358
- );
1359
- this.protocolError(
1360
- ProtocolError.HandshakeFailed,
1361
- parsed.payload.status.reason
1362
- );
1363
- return false;
1373
+ return;
1364
1374
  }
1365
- if (previousSession?.advertisedSessionId && previousSession.advertisedSessionId !== parsed.payload.status.sessionId) {
1366
- this.deleteSession({
1367
- session: previousSession,
1368
- closeHandshakingConnection: true
1369
- });
1370
- conn.telemetry?.span.setStatus({
1371
- code: import_api4.SpanStatusCode.ERROR,
1372
- message: "session id mismatch"
1373
- });
1374
- this.log?.warn(`handshake from ${parsed.from} session id mismatch`, {
1375
- ...conn.loggingMetadata,
1376
- clientId: this.clientId,
1377
- connectedTo: parsed.from,
1378
- transportMessage: parsed
1375
+ if (msg.payload.status.sessionId !== session.id) {
1376
+ const reason = `session id mismatch: expected ${session.id}, got ${msg.payload.status.sessionId}`;
1377
+ this.rejectHandshakeResponse(session, reason, {
1378
+ ...session.loggingMetadata,
1379
+ transportMessage: msg
1379
1380
  });
1380
- this.protocolError(ProtocolError.HandshakeFailed, "session id mismatch");
1381
- return false;
1381
+ return;
1382
1382
  }
1383
- this.log?.debug(`handshake from ${parsed.from} ok`, {
1384
- ...conn.loggingMetadata,
1385
- clientId: this.clientId,
1386
- connectedTo: parsed.from,
1387
- transportMessage: parsed
1383
+ this.log?.info(`handshake from ${msg.from} ok`, {
1384
+ ...session.loggingMetadata,
1385
+ transportMessage: msg
1388
1386
  });
1389
- const { session, isTransparentReconnect } = this.getOrCreateSession({
1390
- to: parsed.from,
1391
- conn,
1392
- sessionId: parsed.payload.status.sessionId
1387
+ const connectedSession = SessionStateGraph.transition.HandshakingToConnected(session, {
1388
+ onConnectionErrored: (err) => {
1389
+ const errStr = coerceErrorString(err);
1390
+ this.log?.warn(
1391
+ `connection to ${connectedSession.to} errored: ${errStr}`,
1392
+ connectedSession.loggingMetadata
1393
+ );
1394
+ },
1395
+ onConnectionClosed: () => {
1396
+ this.log?.info(
1397
+ `connection to ${connectedSession.to} closed`,
1398
+ connectedSession.loggingMetadata
1399
+ );
1400
+ this.onConnClosed(connectedSession);
1401
+ },
1402
+ onMessage: (msg2) => this.handleMsg(msg2),
1403
+ onInvalidMessage: (reason) => {
1404
+ this.deleteSession(connectedSession);
1405
+ this.protocolError(ProtocolError.MessageOrderingViolated, reason);
1406
+ }
1393
1407
  });
1394
- this.onConnect(conn, session, isTransparentReconnect);
1395
- this.retryBudget.startRestoringBudget(session.to);
1396
- return session;
1408
+ this.updateSession(connectedSession);
1409
+ this.retryBudget.startRestoringBudget(connectedSession.to);
1397
1410
  }
1398
1411
  /**
1399
1412
  * Manually attempts to connect to a client.
1400
1413
  * @param to The client ID of the node to connect to.
1401
1414
  */
1402
- async connect(to) {
1403
- if (this.connections.has(to)) {
1404
- this.log?.info(`already connected to ${to}, skipping connect attempt`, {
1405
- clientId: this.clientId,
1406
- connectedTo: to
1407
- });
1415
+ connect(to) {
1416
+ let session = this.sessions.get(to);
1417
+ session ??= this.createUnconnectedSession(to);
1418
+ if (session.state !== "NoConnection" /* NoConnection */) {
1419
+ this.log?.debug(
1420
+ `session to ${to} has state ${session.state}, skipping connect attempt`,
1421
+ session.loggingMetadata
1422
+ );
1408
1423
  return;
1409
1424
  }
1410
- const canProceedWithConnection = () => this.getStatus() === "open";
1411
- if (!canProceedWithConnection()) {
1425
+ if (this.getStatus() !== "open") {
1412
1426
  this.log?.info(
1413
1427
  `transport state is no longer open, cancelling attempt to connect to ${to}`,
1414
- { clientId: this.clientId, connectedTo: to }
1428
+ session.loggingMetadata
1415
1429
  );
1416
1430
  return;
1417
1431
  }
1418
- let reconnectPromise = this.inflightConnectionPromises.get(to);
1419
- if (!reconnectPromise) {
1420
- if (!this.retryBudget.hasBudget(to)) {
1421
- const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
1422
- const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
1423
- this.log?.error(errMsg, { clientId: this.clientId, connectedTo: to });
1424
- this.protocolError(ProtocolError.RetriesExceeded, errMsg);
1425
- return;
1426
- }
1427
- let sleep = Promise.resolve();
1428
- const backoffMs = this.retryBudget.getBackoffMs(to);
1429
- if (backoffMs > 0) {
1430
- sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
1431
- }
1432
- this.log?.info(
1433
- `attempting connection to ${to} (${backoffMs}ms backoff)`,
1434
- {
1435
- clientId: this.clientId,
1436
- connectedTo: to
1437
- }
1438
- );
1439
- this.retryBudget.consumeBudget(to);
1440
- reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
1441
- try {
1442
- span.addEvent("backoff", { backoffMs });
1443
- await sleep;
1444
- if (!canProceedWithConnection()) {
1445
- throw new Error("transport state is no longer open");
1446
- }
1447
- span.addEvent("connecting");
1448
- const conn = await this.createNewOutgoingConnection(to);
1449
- if (!canProceedWithConnection()) {
1450
- this.log?.info(
1451
- `transport state is no longer open, closing pre-handshake connection to ${to}`,
1452
- {
1453
- ...conn.loggingMetadata,
1454
- clientId: this.clientId,
1455
- connectedTo: to
1456
- }
1457
- );
1458
- conn.close();
1459
- throw new Error("transport state is no longer open");
1460
- }
1461
- span.addEvent("sending handshake");
1462
- const ok = await this.sendHandshake(to, conn);
1463
- if (!ok) {
1464
- conn.close();
1465
- throw new Error("failed to send handshake");
1466
- }
1467
- return conn;
1468
- } catch (err) {
1469
- const errStr = coerceErrorString(err);
1470
- span.recordException(errStr);
1471
- span.setStatus({ code: import_api4.SpanStatusCode.ERROR });
1472
- throw err;
1473
- } finally {
1474
- span.end();
1475
- }
1476
- });
1477
- this.inflightConnectionPromises.set(to, reconnectPromise);
1478
- } else {
1479
- this.log?.info(
1480
- `attempting connection to ${to} (reusing previous attempt)`,
1481
- {
1482
- clientId: this.clientId,
1483
- connectedTo: to
1484
- }
1485
- );
1432
+ if (!this.retryBudget.hasBudget(to)) {
1433
+ const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
1434
+ const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
1435
+ this.log?.error(errMsg, session.loggingMetadata);
1436
+ this.protocolError(ProtocolError.RetriesExceeded, errMsg);
1437
+ return;
1486
1438
  }
1487
- try {
1488
- await reconnectPromise;
1489
- } catch (error) {
1490
- this.inflightConnectionPromises.delete(to);
1491
- const errStr = coerceErrorString(error);
1492
- if (!this.reconnectOnConnectionDrop || !canProceedWithConnection()) {
1493
- this.log?.warn(`connection to ${to} failed (${errStr})`, {
1494
- clientId: this.clientId,
1495
- connectedTo: to
1496
- });
1497
- } else {
1498
- this.log?.warn(`connection to ${to} failed (${errStr}), retrying`, {
1499
- clientId: this.clientId,
1500
- connectedTo: to
1501
- });
1502
- await this.connect(to);
1503
- }
1439
+ let sleep = Promise.resolve();
1440
+ const backoffMs = this.retryBudget.getBackoffMs(to);
1441
+ if (backoffMs > 0) {
1442
+ sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
1504
1443
  }
1505
- }
1506
- deleteSession({
1507
- session,
1508
- closeHandshakingConnection,
1509
- handshakingConn
1510
- }) {
1511
- this.inflightConnectionPromises.delete(session.to);
1512
- super.deleteSession({
1513
- session,
1514
- closeHandshakingConnection,
1515
- handshakingConn
1444
+ this.log?.info(
1445
+ `attempting connection to ${to} (${backoffMs}ms backoff)`,
1446
+ session.loggingMetadata
1447
+ );
1448
+ this.retryBudget.consumeBudget(to);
1449
+ const reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
1450
+ try {
1451
+ span.addEvent("backoff", { backoffMs });
1452
+ await sleep;
1453
+ if (this.getStatus() !== "open") {
1454
+ throw new Error("transport state is no longer open");
1455
+ }
1456
+ span.addEvent("connecting");
1457
+ return await this.createNewOutgoingConnection(to);
1458
+ } catch (err) {
1459
+ const errStr = coerceErrorString(err);
1460
+ span.recordException(errStr);
1461
+ span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
1462
+ throw err;
1463
+ } finally {
1464
+ span.end();
1465
+ }
1516
1466
  });
1467
+ const connectingSession = SessionStateGraph.transition.NoConnectionToConnecting(
1468
+ session,
1469
+ reconnectPromise,
1470
+ {
1471
+ onConnectionEstablished: (conn) => {
1472
+ this.log?.debug(
1473
+ `connection to ${connectingSession.to} established`,
1474
+ connectingSession.loggingMetadata
1475
+ );
1476
+ this.onConnectionEstablished(connectingSession, conn);
1477
+ },
1478
+ onConnectionFailed: (error) => {
1479
+ const errStr = coerceErrorString(error);
1480
+ this.log?.error(
1481
+ `error connecting to ${connectingSession.to}: ${errStr}`,
1482
+ connectingSession.loggingMetadata
1483
+ );
1484
+ this.onConnectingFailed(connectingSession);
1485
+ },
1486
+ onConnectionTimeout: () => {
1487
+ this.log?.error(
1488
+ `connection to ${connectingSession.to} timed out`,
1489
+ connectingSession.loggingMetadata
1490
+ );
1491
+ this.onConnectingFailed(connectingSession);
1492
+ }
1493
+ }
1494
+ );
1495
+ this.updateSession(connectingSession);
1517
1496
  }
1518
- async sendHandshake(to, conn) {
1497
+ async sendHandshake(session) {
1519
1498
  let metadata = void 0;
1520
1499
  if (this.handshakeExtensions) {
1521
1500
  metadata = await this.handshakeExtensions.construct();
1522
- if (!import_value2.Value.Check(this.handshakeExtensions.schema, metadata)) {
1523
- this.log?.error(`constructed handshake metadata did not match schema`, {
1524
- ...conn.loggingMetadata,
1525
- clientId: this.clientId,
1526
- connectedTo: to,
1527
- validationErrors: [
1528
- ...import_value2.Value.Errors(this.handshakeExtensions.schema, metadata)
1529
- ],
1530
- tags: ["invariant-violation"]
1531
- });
1532
- this.protocolError(
1533
- ProtocolError.HandshakeFailed,
1534
- "handshake metadata did not match schema"
1535
- );
1536
- conn.telemetry?.span.setStatus({
1537
- code: import_api4.SpanStatusCode.ERROR,
1538
- message: "handshake meta mismatch"
1539
- });
1540
- return false;
1541
- }
1542
1501
  }
1543
- const { session } = this.getOrCreateSession({ to, handshakingConn: conn });
1544
1502
  const requestMsg = handshakeRequestMessage({
1545
1503
  from: this.clientId,
1546
- to,
1504
+ to: session.to,
1547
1505
  sessionId: session.id,
1548
1506
  expectedSessionState: {
1549
- reconnect: session.advertisedSessionId !== void 0,
1550
- nextExpectedSeq: session.nextExpectedSeq
1507
+ nextExpectedSeq: session.ack,
1508
+ nextSentSeq: session.nextSeq()
1551
1509
  },
1552
1510
  metadata,
1553
1511
  tracing: getPropagationContext(session.telemetry.ctx)
1554
1512
  });
1555
- this.log?.debug(`sending handshake request to ${to}`, {
1556
- ...conn.loggingMetadata,
1557
- clientId: this.clientId,
1558
- connectedTo: to,
1513
+ this.log?.debug(`sending handshake request to ${session.to}`, {
1514
+ ...session.loggingMetadata,
1559
1515
  transportMessage: requestMsg
1560
1516
  });
1561
- conn.send(this.codec.toBuffer(requestMsg));
1562
- return true;
1517
+ session.sendHandshake(requestMsg);
1563
1518
  }
1564
1519
  close() {
1565
1520
  this.retryBudget.close();
@@ -1568,7 +1523,7 @@ var ClientTransport = class extends Transport {
1568
1523
  };
1569
1524
 
1570
1525
  // transport/server.ts
1571
- var import_api5 = require("@opentelemetry/api");
1526
+ var import_api4 = require("@opentelemetry/api");
1572
1527
  var import_value3 = require("@sinclair/typebox/value");
1573
1528
  var ServerTransport = class extends Transport {
1574
1529
  /**
@@ -1582,14 +1537,14 @@ var ServerTransport = class extends Transport {
1582
1537
  /**
1583
1538
  * A map of session handshake data for each session.
1584
1539
  */
1585
- sessionHandshakeMetadata;
1540
+ sessionHandshakeMetadata = /* @__PURE__ */ new Map();
1541
+ pendingSessions = /* @__PURE__ */ new Set();
1586
1542
  constructor(clientId, providedOptions) {
1587
1543
  super(clientId, providedOptions);
1588
1544
  this.options = {
1589
1545
  ...defaultServerTransportOptions,
1590
1546
  ...providedOptions
1591
1547
  };
1592
- this.sessionHandshakeMetadata = /* @__PURE__ */ new WeakMap();
1593
1548
  this.log?.info(`initiated server transport`, {
1594
1549
  clientId: this.clientId,
1595
1550
  protocolVersion: PROTOCOL_VERSION
@@ -1598,6 +1553,36 @@ var ServerTransport = class extends Transport {
1598
1553
  extendHandshake(options) {
1599
1554
  this.handshakeExtensions = options;
1600
1555
  }
1556
+ send(to, msg) {
1557
+ if (this.getStatus() === "closed") {
1558
+ const err = "transport is closed, cant send";
1559
+ this.log?.error(err, {
1560
+ clientId: this.clientId,
1561
+ transportMessage: msg,
1562
+ tags: ["invariant-violation"]
1563
+ });
1564
+ throw new Error(err);
1565
+ }
1566
+ const session = this.sessions.get(to);
1567
+ if (!session) {
1568
+ const err = `session to ${to} does not exist`;
1569
+ this.log?.error(err, {
1570
+ clientId: this.clientId,
1571
+ transportMessage: msg,
1572
+ tags: ["invariant-violation"]
1573
+ });
1574
+ throw new Error(err);
1575
+ }
1576
+ return session.send(msg);
1577
+ }
1578
+ deletePendingSession(pendingSession) {
1579
+ pendingSession.close();
1580
+ this.pendingSessions.delete(pendingSession);
1581
+ }
1582
+ deleteSession(session) {
1583
+ this.sessionHandshakeMetadata.delete(session.to);
1584
+ super.deleteSession(session);
1585
+ }
1601
1586
  handleConnection(conn) {
1602
1587
  if (this.getStatus() !== "open")
1603
1588
  return;
@@ -1605,281 +1590,369 @@ var ServerTransport = class extends Transport {
1605
1590
  ...conn.loggingMetadata,
1606
1591
  clientId: this.clientId
1607
1592
  });
1608
- let session = void 0;
1609
- const client = () => session?.to ?? "unknown";
1610
- const handshakeTimeout = setTimeout(() => {
1611
- if (!session) {
1612
- this.log?.warn(
1613
- `connection to ${client()} timed out waiting for handshake, closing`,
1614
- {
1615
- ...conn.loggingMetadata,
1616
- clientId: this.clientId,
1617
- connectedTo: client()
1618
- }
1619
- );
1620
- conn.telemetry?.span.setStatus({
1621
- code: import_api5.SpanStatusCode.ERROR,
1622
- message: "handshake timeout"
1623
- });
1624
- conn.close();
1625
- }
1626
- }, this.options.handshakeTimeoutMs);
1627
- const buffer = [];
1628
- let receivedHandshakeMessage = false;
1629
- const handshakeHandler = (data) => {
1630
- if (receivedHandshakeMessage) {
1631
- buffer.push(data);
1632
- return;
1633
- }
1634
- receivedHandshakeMessage = true;
1635
- clearTimeout(handshakeTimeout);
1636
- void this.receiveHandshakeRequestMessage(data, conn).then(
1637
- (maybeSession) => {
1638
- if (!maybeSession) {
1639
- conn.close();
1593
+ let receivedHandshake = false;
1594
+ const pendingSession = SessionStateGraph.entrypoints.WaitingForHandshake(
1595
+ this.clientId,
1596
+ conn,
1597
+ {
1598
+ onConnectionClosed: () => {
1599
+ this.log?.warn(
1600
+ `connection from unknown closed before handshake finished`,
1601
+ pendingSession.loggingMetadata
1602
+ );
1603
+ this.deletePendingSession(pendingSession);
1604
+ },
1605
+ onConnectionErrored: (err) => {
1606
+ const errorString = coerceErrorString(err);
1607
+ this.log?.warn(
1608
+ `connection from unknown errored before handshake finished: ${errorString}`,
1609
+ pendingSession.loggingMetadata
1610
+ );
1611
+ this.deletePendingSession(pendingSession);
1612
+ },
1613
+ onHandshakeTimeout: () => {
1614
+ this.log?.warn(
1615
+ `connection from unknown timed out before handshake finished`,
1616
+ pendingSession.loggingMetadata
1617
+ );
1618
+ this.deletePendingSession(pendingSession);
1619
+ },
1620
+ onHandshake: (msg) => {
1621
+ if (receivedHandshake) {
1622
+ this.log?.error(
1623
+ `received multiple handshake messages from pending session`,
1624
+ {
1625
+ ...pendingSession.loggingMetadata,
1626
+ connectedTo: msg.from,
1627
+ transportMessage: msg
1628
+ }
1629
+ );
1630
+ this.deletePendingSession(pendingSession);
1640
1631
  return;
1641
1632
  }
1642
- session = maybeSession;
1643
- const dataHandler = (data2) => {
1644
- const parsed = this.parseMsg(data2, conn);
1645
- if (!parsed) {
1646
- conn.close();
1647
- return;
1648
- }
1649
- this.handleMsg(parsed, conn);
1650
- };
1651
- for (const data2 of buffer) {
1652
- dataHandler(data2);
1653
- }
1654
- conn.removeDataListener(handshakeHandler);
1655
- conn.addDataListener(dataHandler);
1656
- buffer.length = 0;
1633
+ receivedHandshake = true;
1634
+ void this.onHandshakeRequest(pendingSession, msg);
1635
+ },
1636
+ onInvalidHandshake: (reason) => {
1637
+ this.log?.error(
1638
+ `invalid handshake: ${reason}`,
1639
+ pendingSession.loggingMetadata
1640
+ );
1641
+ this.deletePendingSession(pendingSession);
1642
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
1657
1643
  }
1658
- );
1659
- };
1660
- conn.addDataListener(handshakeHandler);
1661
- conn.addCloseListener(() => {
1662
- if (!session)
1663
- return;
1664
- this.log?.info(`connection to ${client()} disconnected`, {
1665
- ...conn.loggingMetadata,
1666
- clientId: this.clientId
1667
- });
1668
- this.onDisconnect(conn, session);
1669
- });
1670
- conn.addErrorListener((err) => {
1671
- conn.telemetry?.span.setStatus({
1672
- code: import_api5.SpanStatusCode.ERROR,
1673
- message: "connection error"
1674
- });
1675
- if (!session)
1676
- return;
1677
- this.log?.warn(
1678
- `connection to ${client()} got an error: ${coerceErrorString(err)}`,
1679
- { ...conn.loggingMetadata, clientId: this.clientId }
1680
- );
1681
- });
1682
- }
1683
- async validateHandshakeMetadata(conn, session, rawMetadata, from) {
1684
- let parsedMetadata = {};
1685
- if (this.handshakeExtensions) {
1686
- if (!import_value3.Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
1687
- conn.telemetry?.span.setStatus({
1688
- code: import_api5.SpanStatusCode.ERROR,
1689
- message: "malformed handshake meta"
1690
- });
1691
- const reason = "received malformed handshake metadata";
1692
- const responseMsg = handshakeResponseMessage({
1693
- from: this.clientId,
1694
- to: from,
1695
- status: {
1696
- ok: false,
1697
- reason
1698
- }
1699
- });
1700
- conn.send(this.codec.toBuffer(responseMsg));
1701
- this.log?.warn(`received malformed handshake metadata from ${from}`, {
1702
- ...conn.loggingMetadata,
1703
- clientId: this.clientId,
1704
- validationErrors: [
1705
- ...import_value3.Value.Errors(this.handshakeExtensions.schema, rawMetadata)
1706
- ]
1707
- });
1708
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1709
- return false;
1710
- }
1711
- const previousParsedMetadata = session ? this.sessionHandshakeMetadata.get(session) : void 0;
1712
- parsedMetadata = await this.handshakeExtensions.validate(
1713
- rawMetadata,
1714
- previousParsedMetadata
1715
- );
1716
- if (parsedMetadata === false) {
1717
- const reason = "rejected by handshake handler";
1718
- conn.telemetry?.span.setStatus({
1719
- code: import_api5.SpanStatusCode.ERROR,
1720
- message: reason
1721
- });
1722
- const responseMsg = handshakeResponseMessage({
1723
- from: this.clientId,
1724
- to: from,
1725
- status: {
1726
- ok: false,
1727
- reason
1728
- }
1729
- });
1730
- conn.send(this.codec.toBuffer(responseMsg));
1731
- this.log?.warn(`rejected handshake from ${from}`, {
1732
- ...conn.loggingMetadata,
1733
- clientId: this.clientId
1734
- });
1735
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1736
- return false;
1737
- }
1738
- }
1739
- return parsedMetadata;
1644
+ },
1645
+ this.options,
1646
+ this.log
1647
+ );
1648
+ this.pendingSessions.add(pendingSession);
1740
1649
  }
1741
- async receiveHandshakeRequestMessage(data, conn) {
1742
- const parsed = this.parseMsg(data, conn);
1743
- if (!parsed) {
1744
- conn.telemetry?.span.setStatus({
1745
- code: import_api5.SpanStatusCode.ERROR,
1746
- message: "non-transport message"
1747
- });
1748
- this.protocolError(
1749
- ProtocolError.HandshakeFailed,
1750
- "received non-transport message"
1751
- );
1752
- return false;
1753
- }
1754
- if (!import_value3.Value.Check(ControlMessageHandshakeRequestSchema, parsed.payload)) {
1755
- conn.telemetry?.span.setStatus({
1756
- code: import_api5.SpanStatusCode.ERROR,
1757
- message: "invalid handshake request"
1758
- });
1759
- const reason = "received invalid handshake msg";
1760
- const responseMsg2 = handshakeResponseMessage({
1650
+ rejectHandshakeRequest(session, to, reason, code, metadata) {
1651
+ session.conn.telemetry?.span.setStatus({
1652
+ code: import_api4.SpanStatusCode.ERROR,
1653
+ message: reason
1654
+ });
1655
+ this.log?.warn(reason, metadata);
1656
+ session.sendHandshake(
1657
+ handshakeResponseMessage({
1761
1658
  from: this.clientId,
1762
- to: parsed.from,
1659
+ to,
1763
1660
  status: {
1764
1661
  ok: false,
1662
+ code,
1765
1663
  reason
1766
1664
  }
1767
- });
1768
- conn.send(this.codec.toBuffer(responseMsg2));
1769
- this.log?.warn(reason, {
1770
- ...conn.loggingMetadata,
1771
- clientId: this.clientId,
1772
- // safe to this.log metadata here as we remove the payload
1773
- // before passing it to user-land
1774
- transportMessage: parsed,
1775
- validationErrors: [
1776
- ...import_value3.Value.Errors(ControlMessageHandshakeRequestSchema, parsed.payload)
1777
- ]
1778
- });
1779
- this.protocolError(
1780
- ProtocolError.HandshakeFailed,
1781
- "invalid handshake request"
1665
+ })
1666
+ );
1667
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
1668
+ this.deletePendingSession(session);
1669
+ }
1670
+ async onHandshakeRequest(session, msg) {
1671
+ if (!import_value3.Value.Check(ControlMessageHandshakeRequestSchema, msg.payload)) {
1672
+ this.rejectHandshakeRequest(
1673
+ session,
1674
+ msg.from,
1675
+ "received invalid handshake request",
1676
+ "MALFORMED_HANDSHAKE",
1677
+ {
1678
+ ...session.loggingMetadata,
1679
+ transportMessage: msg,
1680
+ connectedTo: msg.from,
1681
+ validationErrors: [
1682
+ ...import_value3.Value.Errors(ControlMessageHandshakeRequestSchema, msg.payload)
1683
+ ]
1684
+ }
1782
1685
  );
1783
- return false;
1686
+ return;
1784
1687
  }
1785
- const gotVersion = parsed.payload.protocolVersion;
1688
+ const gotVersion = msg.payload.protocolVersion;
1786
1689
  if (gotVersion !== PROTOCOL_VERSION) {
1787
- conn.telemetry?.span.setStatus({
1788
- code: import_api5.SpanStatusCode.ERROR,
1789
- message: "incorrect protocol version"
1790
- });
1791
- const reason = `incorrect version (got: ${gotVersion} wanted ${PROTOCOL_VERSION})`;
1792
- const responseMsg2 = handshakeResponseMessage({
1793
- from: this.clientId,
1794
- to: parsed.from,
1795
- status: {
1796
- ok: false,
1797
- reason
1690
+ this.rejectHandshakeRequest(
1691
+ session,
1692
+ msg.from,
1693
+ `expected protocol version ${PROTOCOL_VERSION}, got ${gotVersion}`,
1694
+ "PROTOCOL_VERSION_MISMATCH",
1695
+ {
1696
+ ...session.loggingMetadata,
1697
+ connectedTo: msg.from,
1698
+ transportMessage: msg
1798
1699
  }
1799
- });
1800
- conn.send(this.codec.toBuffer(responseMsg2));
1801
- this.log?.warn(
1802
- `received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`,
1803
- { ...conn.loggingMetadata, clientId: this.clientId }
1804
1700
  );
1805
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1806
- return false;
1701
+ return;
1807
1702
  }
1808
- const oldSession = this.sessions.get(parsed.from);
1703
+ let oldSession = this.sessions.get(msg.from);
1809
1704
  const parsedMetadata = await this.validateHandshakeMetadata(
1810
- conn,
1705
+ session,
1811
1706
  oldSession,
1812
- parsed.payload.metadata,
1813
- parsed.from
1707
+ msg.payload.metadata,
1708
+ msg.from
1814
1709
  );
1815
1710
  if (parsedMetadata === false) {
1816
- return false;
1711
+ return;
1817
1712
  }
1818
- let session;
1819
- let isTransparentReconnect;
1820
- if (!parsed.payload.expectedSessionState) {
1821
- ({ session, isTransparentReconnect } = this.getOrCreateSession({
1822
- to: parsed.from,
1823
- conn,
1824
- sessionId: parsed.payload.sessionId,
1825
- propagationCtx: parsed.tracing
1826
- }));
1827
- } else if (parsed.payload.expectedSessionState.reconnect) {
1828
- const existingSession = this.getExistingSession({
1829
- to: parsed.from,
1830
- sessionId: parsed.payload.sessionId,
1831
- nextExpectedSeq: parsed.payload.expectedSessionState.nextExpectedSeq
1832
- });
1833
- if (existingSession === false) {
1834
- conn.telemetry?.span.setStatus({
1835
- code: import_api5.SpanStatusCode.ERROR,
1836
- message: SESSION_STATE_MISMATCH
1713
+ let connectCase = "new session";
1714
+ if (oldSession && oldSession.id === msg.payload.sessionId) {
1715
+ connectCase = "transparent reconnection";
1716
+ const clientNextExpectedSeq = msg.payload.expectedSessionState.nextExpectedSeq;
1717
+ const clientNextSentSeq = msg.payload.expectedSessionState.nextSentSeq ?? 0;
1718
+ const ourNextSeq = oldSession.nextSeq();
1719
+ const ourAck = oldSession.ack;
1720
+ if (clientNextSentSeq > ourAck) {
1721
+ this.rejectHandshakeRequest(
1722
+ session,
1723
+ msg.from,
1724
+ `client is in the future: server wanted next message to be ${ourAck} but client would have sent ${clientNextSentSeq}`,
1725
+ "SESSION_STATE_MISMATCH",
1726
+ {
1727
+ ...session.loggingMetadata,
1728
+ connectedTo: msg.from,
1729
+ transportMessage: msg
1730
+ }
1731
+ );
1732
+ return;
1733
+ }
1734
+ if (ourNextSeq > clientNextExpectedSeq) {
1735
+ this.rejectHandshakeRequest(
1736
+ session,
1737
+ msg.from,
1738
+ `server is in the future: client wanted next message to be ${clientNextExpectedSeq} but server would have sent ${ourNextSeq}`,
1739
+ "SESSION_STATE_MISMATCH",
1740
+ {
1741
+ ...session.loggingMetadata,
1742
+ connectedTo: msg.from,
1743
+ transportMessage: msg
1744
+ }
1745
+ );
1746
+ return;
1747
+ }
1748
+ if (oldSession.state === "Connected" /* Connected */) {
1749
+ const noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(oldSession, {
1750
+ onSessionGracePeriodElapsed: () => {
1751
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1752
+ }
1837
1753
  });
1838
- const reason = SESSION_STATE_MISMATCH;
1839
- const responseMsg2 = handshakeResponseMessage({
1840
- from: this.clientId,
1841
- to: parsed.from,
1842
- status: {
1843
- ok: false,
1844
- reason
1754
+ oldSession = noConnectionSession;
1755
+ } else if (oldSession.state === "Handshaking" /* Handshaking */) {
1756
+ const noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(oldSession, {
1757
+ onSessionGracePeriodElapsed: () => {
1758
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1845
1759
  }
1846
1760
  });
1847
- conn.send(this.codec.toBuffer(responseMsg2));
1848
- this.log?.warn(
1849
- `'received handshake msg with incompatible existing session state: ${parsed.payload.sessionId}`,
1850
- { ...conn.loggingMetadata, clientId: this.clientId }
1851
- );
1852
- this.protocolError(ProtocolError.HandshakeFailed, reason);
1853
- return false;
1761
+ oldSession = noConnectionSession;
1762
+ } else if (oldSession.state === "Connecting" /* Connecting */) {
1763
+ const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(oldSession, {
1764
+ onSessionGracePeriodElapsed: () => {
1765
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1766
+ }
1767
+ });
1768
+ oldSession = noConnectionSession;
1854
1769
  }
1855
- session = existingSession;
1856
- isTransparentReconnect = false;
1770
+ this.updateSession(oldSession);
1771
+ } else if (oldSession) {
1772
+ connectCase = "hard reconnection";
1773
+ this.deleteSession(oldSession);
1774
+ oldSession = void 0;
1857
1775
  } else {
1858
- const createdSession = this.createNewSession({
1859
- to: parsed.from,
1860
- conn,
1861
- sessionId: parsed.payload.sessionId,
1862
- propagationCtx: parsed.tracing
1863
- });
1864
- session = createdSession;
1865
- isTransparentReconnect = false;
1776
+ connectCase = "unknown session";
1777
+ const clientNextExpectedSeq = msg.payload.expectedSessionState.nextExpectedSeq;
1778
+ const clientNextSentSeq = msg.payload.expectedSessionState.nextSentSeq ?? 0;
1779
+ if (clientNextSentSeq > 0 || clientNextExpectedSeq > 0) {
1780
+ this.rejectHandshakeRequest(
1781
+ session,
1782
+ msg.from,
1783
+ `client is trying to reconnect to a session the server don't know about: ${msg.payload.sessionId}`,
1784
+ "SESSION_STATE_MISMATCH",
1785
+ {
1786
+ ...session.loggingMetadata,
1787
+ connectedTo: msg.from,
1788
+ transportMessage: msg
1789
+ }
1790
+ );
1791
+ return;
1792
+ }
1866
1793
  }
1867
- this.sessionHandshakeMetadata.set(session, parsedMetadata);
1868
- this.log?.debug(
1869
- `handshake from ${parsed.from} ok, responding with handshake success`,
1870
- conn.loggingMetadata
1794
+ const sessionId = msg.payload.sessionId;
1795
+ this.log?.info(
1796
+ `handshake from ${msg.from} ok (${connectCase}), responding with handshake success`,
1797
+ {
1798
+ ...session.loggingMetadata,
1799
+ connectedTo: msg.from
1800
+ }
1871
1801
  );
1872
1802
  const responseMsg = handshakeResponseMessage({
1873
1803
  from: this.clientId,
1874
- to: parsed.from,
1804
+ to: msg.from,
1875
1805
  status: {
1876
1806
  ok: true,
1877
- sessionId: session.id
1807
+ sessionId
1878
1808
  }
1879
1809
  });
1880
- conn.send(this.codec.toBuffer(responseMsg));
1881
- this.onConnect(conn, session, isTransparentReconnect);
1882
- return session;
1810
+ session.sendHandshake(responseMsg);
1811
+ const connectedSession = SessionStateGraph.transition.WaitingForHandshakeToConnected(
1812
+ session,
1813
+ // by this point oldSession is either no connection or we dont have an old session
1814
+ oldSession,
1815
+ sessionId,
1816
+ msg.from,
1817
+ msg.tracing,
1818
+ {
1819
+ onConnectionErrored: (err) => {
1820
+ const errStr = coerceErrorString(err);
1821
+ this.log?.warn(
1822
+ `connection to ${connectedSession.to} errored: ${errStr}`,
1823
+ connectedSession.loggingMetadata
1824
+ );
1825
+ },
1826
+ onConnectionClosed: () => {
1827
+ this.log?.info(
1828
+ `connection to ${connectedSession.to} closed`,
1829
+ connectedSession.loggingMetadata
1830
+ );
1831
+ this.onConnClosed(connectedSession);
1832
+ },
1833
+ onMessage: (msg2) => this.handleMsg(msg2),
1834
+ onInvalidMessage: (reason) => {
1835
+ this.protocolError(ProtocolError.MessageOrderingViolated, reason);
1836
+ this.deleteSession(connectedSession);
1837
+ }
1838
+ }
1839
+ );
1840
+ this.sessionHandshakeMetadata.set(connectedSession.to, parsedMetadata);
1841
+ this.updateSession(connectedSession);
1842
+ this.pendingSessions.delete(session);
1843
+ connectedSession.startActiveHeartbeat();
1844
+ }
1845
+ async validateHandshakeMetadata(handshakingSession, existingSession, rawMetadata, from) {
1846
+ let parsedMetadata = {};
1847
+ if (this.handshakeExtensions) {
1848
+ if (!import_value3.Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
1849
+ this.rejectHandshakeRequest(
1850
+ handshakingSession,
1851
+ from,
1852
+ "received malformed handshake metadata",
1853
+ "MALFORMED_HANDSHAKE_META",
1854
+ {
1855
+ ...handshakingSession.loggingMetadata,
1856
+ connectedTo: from,
1857
+ validationErrors: [
1858
+ ...import_value3.Value.Errors(this.handshakeExtensions.schema, rawMetadata)
1859
+ ]
1860
+ }
1861
+ );
1862
+ return false;
1863
+ }
1864
+ const previousParsedMetadata = existingSession ? this.sessionHandshakeMetadata.get(existingSession.to) : void 0;
1865
+ parsedMetadata = await this.handshakeExtensions.validate(
1866
+ rawMetadata,
1867
+ previousParsedMetadata
1868
+ );
1869
+ if (parsedMetadata === false) {
1870
+ this.rejectHandshakeRequest(
1871
+ handshakingSession,
1872
+ from,
1873
+ "rejected by handshake handler",
1874
+ "REJECTED_BY_CUSTOM_HANDLER",
1875
+ {
1876
+ ...handshakingSession.loggingMetadata,
1877
+ connectedTo: from,
1878
+ clientId: this.clientId
1879
+ }
1880
+ );
1881
+ return false;
1882
+ }
1883
+ }
1884
+ return parsedMetadata;
1885
+ }
1886
+ };
1887
+
1888
+ // transport/connection.ts
1889
+ var Connection = class {
1890
+ id;
1891
+ telemetry;
1892
+ constructor() {
1893
+ this.id = `conn-${generateId()}`;
1894
+ }
1895
+ get loggingMetadata() {
1896
+ const metadata = { connId: this.id };
1897
+ const spanContext = this.telemetry?.span.spanContext();
1898
+ if (this.telemetry?.span.isRecording() && spanContext) {
1899
+ metadata.telemetry = {
1900
+ traceId: spanContext.traceId,
1901
+ spanId: spanContext.spanId
1902
+ };
1903
+ }
1904
+ return metadata;
1905
+ }
1906
+ // can't use event emitter because we need this to work in both node + browser
1907
+ _dataListeners = /* @__PURE__ */ new Set();
1908
+ _closeListeners = /* @__PURE__ */ new Set();
1909
+ _errorListeners = /* @__PURE__ */ new Set();
1910
+ get dataListeners() {
1911
+ return [...this._dataListeners];
1912
+ }
1913
+ get closeListeners() {
1914
+ return [...this._closeListeners];
1915
+ }
1916
+ get errorListeners() {
1917
+ return [...this._errorListeners];
1918
+ }
1919
+ /**
1920
+ * Handle adding a callback for when a message is received.
1921
+ * @param msg The message that was received.
1922
+ */
1923
+ addDataListener(cb) {
1924
+ this._dataListeners.add(cb);
1925
+ }
1926
+ removeDataListener(cb) {
1927
+ this._dataListeners.delete(cb);
1928
+ }
1929
+ /**
1930
+ * Handle adding a callback for when the connection is closed.
1931
+ * This should also be called if an error happens and after notifying all the error listeners.
1932
+ * @param cb The callback to call when the connection is closed.
1933
+ */
1934
+ addCloseListener(cb) {
1935
+ this._closeListeners.add(cb);
1936
+ }
1937
+ removeCloseListener(cb) {
1938
+ this._closeListeners.delete(cb);
1939
+ }
1940
+ /**
1941
+ * Handle adding a callback for when an error is received.
1942
+ * This should only be used for this.logging errors, all cleanup
1943
+ * should be delegated to addCloseListener.
1944
+ *
1945
+ * The implementer should take care such that the implemented
1946
+ * connection will call both the close and error callbacks
1947
+ * on an error.
1948
+ *
1949
+ * @param cb The callback to call when an error is received.
1950
+ */
1951
+ addErrorListener(cb) {
1952
+ this._errorListeners.add(cb);
1953
+ }
1954
+ removeErrorListener(cb) {
1955
+ this._errorListeners.delete(cb);
1883
1956
  }
1884
1957
  };
1885
1958
  // Annotate the CommonJS export names for ESM import in node:
@@ -1889,7 +1962,7 @@ var ServerTransport = class extends Transport {
1889
1962
  OpaqueTransportMessageSchema,
1890
1963
  ProtocolError,
1891
1964
  ServerTransport,
1892
- Session,
1965
+ SessionState,
1893
1966
  Transport,
1894
1967
  TransportMessageSchema
1895
1968
  });