@replit/river 0.23.6 → 0.23.7

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 (65) hide show
  1. package/README.md +10 -3
  2. package/dist/{chunk-ZFN6I2GJ.js → chunk-2KC75LS4.js} +2 -2
  3. package/dist/{chunk-GX2YS7BY.js → chunk-6LCL2ZZF.js} +17 -31
  4. package/dist/chunk-6LCL2ZZF.js.map +1 -0
  5. package/dist/{chunk-7E53HHNS.js → chunk-M3TGXFWT.js} +33 -31
  6. package/dist/chunk-M3TGXFWT.js.map +1 -0
  7. package/dist/{chunk-2PSMEETT.js → chunk-MFF6IPBA.js} +24 -11
  8. package/dist/{chunk-2PSMEETT.js.map → chunk-MFF6IPBA.js.map} +1 -1
  9. package/dist/{chunk-NJJYBNYM.js → chunk-RLVU5DRX.js} +2 -2
  10. package/dist/{chunk-NB3OHLXB.js → chunk-XYFXRL7Q.js} +165 -101
  11. package/dist/chunk-XYFXRL7Q.js.map +1 -0
  12. package/dist/{connection-e257f6b7.d.ts → connection-6ce35bd5.d.ts} +1 -1
  13. package/dist/{connection-5331d77f.d.ts → connection-a3fdfa3a.d.ts} +1 -1
  14. package/dist/{index-926aea33.d.ts → index-60f03cb7.d.ts} +5 -2
  15. package/dist/logging/index.cjs +6 -73
  16. package/dist/logging/index.cjs.map +1 -1
  17. package/dist/logging/index.d.cts +1 -1
  18. package/dist/logging/index.d.ts +1 -1
  19. package/dist/logging/index.js +1 -3
  20. package/dist/router/index.cjs +47 -33
  21. package/dist/router/index.cjs.map +1 -1
  22. package/dist/router/index.d.cts +6 -6
  23. package/dist/router/index.d.ts +6 -6
  24. package/dist/router/index.js +2 -3
  25. package/dist/{services-3f99f5d2.d.ts → services-fd8a9894.d.ts} +2 -2
  26. package/dist/transport/impls/uds/client.cjs +195 -81
  27. package/dist/transport/impls/uds/client.cjs.map +1 -1
  28. package/dist/transport/impls/uds/client.d.cts +3 -3
  29. package/dist/transport/impls/uds/client.d.ts +3 -3
  30. package/dist/transport/impls/uds/client.js +5 -7
  31. package/dist/transport/impls/uds/client.js.map +1 -1
  32. package/dist/transport/impls/uds/server.cjs +170 -73
  33. package/dist/transport/impls/uds/server.cjs.map +1 -1
  34. package/dist/transport/impls/uds/server.d.cts +3 -3
  35. package/dist/transport/impls/uds/server.d.ts +3 -3
  36. package/dist/transport/impls/uds/server.js +4 -4
  37. package/dist/transport/impls/ws/client.cjs +194 -80
  38. package/dist/transport/impls/ws/client.cjs.map +1 -1
  39. package/dist/transport/impls/ws/client.d.cts +3 -3
  40. package/dist/transport/impls/ws/client.d.ts +3 -3
  41. package/dist/transport/impls/ws/client.js +6 -8
  42. package/dist/transport/impls/ws/client.js.map +1 -1
  43. package/dist/transport/impls/ws/server.cjs +170 -73
  44. package/dist/transport/impls/ws/server.cjs.map +1 -1
  45. package/dist/transport/impls/ws/server.d.cts +3 -3
  46. package/dist/transport/impls/ws/server.d.ts +3 -3
  47. package/dist/transport/impls/ws/server.js +4 -4
  48. package/dist/transport/index.cjs +219 -105
  49. package/dist/transport/index.cjs.map +1 -1
  50. package/dist/transport/index.d.cts +2 -2
  51. package/dist/transport/index.d.ts +2 -2
  52. package/dist/transport/index.js +3 -3
  53. package/dist/{transport-cbe70659.d.ts → transport-3d34f714.d.ts} +11 -6
  54. package/dist/util/testHelpers.cjs +27 -18
  55. package/dist/util/testHelpers.cjs.map +1 -1
  56. package/dist/util/testHelpers.d.cts +5 -4
  57. package/dist/util/testHelpers.d.ts +5 -4
  58. package/dist/util/testHelpers.js +5 -4
  59. package/dist/util/testHelpers.js.map +1 -1
  60. package/package.json +5 -3
  61. package/dist/chunk-7E53HHNS.js.map +0 -1
  62. package/dist/chunk-GX2YS7BY.js.map +0 -1
  63. package/dist/chunk-NB3OHLXB.js.map +0 -1
  64. /package/dist/{chunk-ZFN6I2GJ.js.map → chunk-2KC75LS4.js.map} +0 -0
  65. /package/dist/{chunk-NJJYBNYM.js.map → chunk-RLVU5DRX.js.map} +0 -0
@@ -128,7 +128,57 @@ function isAck(controlFlag) {
128
128
  }
129
129
 
130
130
  // logging/log.ts
131
- var log = void 0;
131
+ var LoggingLevels = {
132
+ debug: -1,
133
+ info: 0,
134
+ warn: 1,
135
+ error: 2
136
+ };
137
+ var cleanedLogFn = (log) => {
138
+ return (msg, metadata) => {
139
+ if (!metadata?.transportMessage) {
140
+ log(msg, metadata);
141
+ return;
142
+ }
143
+ const { payload, ...rest } = metadata.transportMessage;
144
+ metadata.transportMessage = rest;
145
+ log(msg, metadata);
146
+ };
147
+ };
148
+ var BaseLogger = class {
149
+ minLevel;
150
+ output;
151
+ constructor(output, minLevel = "info") {
152
+ this.minLevel = minLevel;
153
+ this.output = output;
154
+ }
155
+ debug(msg, metadata) {
156
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.debug) {
157
+ this.output(msg, metadata ?? {}, "debug");
158
+ }
159
+ }
160
+ info(msg, metadata) {
161
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.info) {
162
+ this.output(msg, metadata ?? {}, "info");
163
+ }
164
+ }
165
+ warn(msg, metadata) {
166
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.warn) {
167
+ this.output(msg, metadata ?? {}, "warn");
168
+ }
169
+ }
170
+ error(msg, metadata) {
171
+ if (LoggingLevels[this.minLevel] <= LoggingLevels.error) {
172
+ this.output(msg, metadata ?? {}, "error");
173
+ }
174
+ }
175
+ };
176
+ var createLogProxy = (log) => ({
177
+ debug: cleanedLogFn(log.debug.bind(log)),
178
+ info: cleanedLogFn(log.info.bind(log)),
179
+ warn: cleanedLogFn(log.warn.bind(log)),
180
+ error: cleanedLogFn(log.error.bind(log))
181
+ });
132
182
 
133
183
  // transport/events.ts
134
184
  var ProtocolError = {
@@ -171,7 +221,7 @@ var import_nanoid2 = require("nanoid");
171
221
  var import_api = require("@opentelemetry/api");
172
222
 
173
223
  // package.json
174
- var version = "0.23.6";
224
+ var version = "0.23.7";
175
225
 
176
226
  // tracing/index.ts
177
227
  function getPropagationContext(ctx) {
@@ -183,7 +233,7 @@ function getPropagationContext(ctx) {
183
233
  return tracing;
184
234
  }
185
235
  function createSessionTelemetryInfo(session, propagationCtx) {
186
- const ctx = propagationCtx ? import_api.propagation.extract(import_api.context.active(), propagationCtx) : import_api.context.active();
236
+ const parentCtx = propagationCtx ? import_api.propagation.extract(import_api.context.active(), propagationCtx) : import_api.context.active();
187
237
  const span = tracer.startSpan(
188
238
  `session ${session.id}`,
189
239
  {
@@ -194,12 +244,12 @@ function createSessionTelemetryInfo(session, propagationCtx) {
194
244
  "river.session.from": session.from
195
245
  }
196
246
  },
197
- ctx
247
+ parentCtx
198
248
  );
249
+ const ctx = import_api.trace.setSpan(parentCtx, span);
199
250
  return { span, ctx };
200
251
  }
201
- function createConnectionTelemetryInfo(connection, sessionSpan) {
202
- const ctx = import_api.trace.setSpan(import_api.context.active(), sessionSpan);
252
+ function createConnectionTelemetryInfo(connection, info) {
203
253
  const span = tracer.startSpan(
204
254
  `connection ${connection.id}`,
205
255
  {
@@ -207,10 +257,11 @@ function createConnectionTelemetryInfo(connection, sessionSpan) {
207
257
  component: "river",
208
258
  "river.connection.id": connection.id
209
259
  },
210
- links: [{ context: sessionSpan.spanContext() }]
260
+ links: [{ context: info.span.spanContext() }]
211
261
  },
212
- ctx
262
+ info.ctx
213
263
  );
264
+ const ctx = import_api.trace.setSpan(info.ctx, span);
214
265
  return { span, ctx };
215
266
  }
216
267
  var tracer = import_api.trace.getTracer("river", version);
@@ -226,6 +277,17 @@ var Connection = class {
226
277
  constructor() {
227
278
  this.id = `conn-${nanoid2(12)}`;
228
279
  }
280
+ get loggingMetadata() {
281
+ const metadata = { connId: this.id };
282
+ const spanContext = this.telemetry?.span.spanContext();
283
+ if (spanContext) {
284
+ metadata.telemetry = {
285
+ traceId: spanContext.traceId,
286
+ spanId: spanContext.spanId
287
+ };
288
+ }
289
+ return metadata;
290
+ }
229
291
  };
230
292
  var Session = class {
231
293
  codec;
@@ -271,6 +333,7 @@ var Session = class {
271
333
  * The interval for sending heartbeats.
272
334
  */
273
335
  heartbeat;
336
+ log;
274
337
  constructor(conn, from, to, options, propagationCtx) {
275
338
  this.id = `session-${nanoid2(12)}`;
276
339
  this.options = options;
@@ -285,12 +348,20 @@ var Session = class {
285
348
  );
286
349
  this.telemetry = createSessionTelemetryInfo(this, propagationCtx);
287
350
  }
351
+ bindLogger(log) {
352
+ this.log = log;
353
+ }
288
354
  get loggingMetadata() {
355
+ const spanContext = this.telemetry.span.spanContext();
289
356
  return {
290
357
  clientId: this.from,
291
358
  connectedTo: this.to,
292
359
  sessionId: this.id,
293
- connId: this.connection?.id
360
+ connId: this.connection?.id,
361
+ telemetry: {
362
+ traceId: spanContext.traceId,
363
+ spanId: spanContext.spanId
364
+ }
294
365
  };
295
366
  }
296
367
  /**
@@ -303,7 +374,7 @@ var Session = class {
303
374
  */
304
375
  send(msg) {
305
376
  const fullMsg = this.constructMsg(msg);
306
- log?.debug(`sending msg`, {
377
+ this.log?.debug(`sending msg`, {
307
378
  ...this.loggingMetadata,
308
379
  transportMessage: fullMsg
309
380
  });
@@ -311,7 +382,7 @@ var Session = class {
311
382
  const ok = this.connection.send(this.codec.toBuffer(fullMsg));
312
383
  if (ok)
313
384
  return fullMsg.id;
314
- log?.info(
385
+ this.log?.info(
315
386
  `failed to send msg to ${fullMsg.to}, connection is probably dead`,
316
387
  {
317
388
  ...this.loggingMetadata,
@@ -319,8 +390,8 @@ var Session = class {
319
390
  }
320
391
  );
321
392
  } else {
322
- log?.info(
323
- `failed to send msg to ${fullMsg.to}, connection not ready yet`,
393
+ this.log?.debug(
394
+ `buffering msg to ${fullMsg.to}, connection not ready yet`,
324
395
  { ...this.loggingMetadata, transportMessage: fullMsg }
325
396
  );
326
397
  }
@@ -331,7 +402,7 @@ var Session = class {
331
402
  const missDuration = misses * this.options.heartbeatIntervalMs;
332
403
  if (misses > this.options.heartbeatsUntilDead) {
333
404
  if (this.connection) {
334
- log?.info(
405
+ this.log?.info(
335
406
  `closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
336
407
  this.loggingMetadata
337
408
  );
@@ -355,12 +426,12 @@ var Session = class {
355
426
  this.ack = 0;
356
427
  }
357
428
  sendBufferedMessages(conn) {
358
- log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
429
+ this.log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
359
430
  ...this.loggingMetadata,
360
431
  connId: conn.id
361
432
  });
362
433
  for (const msg of this.sendBuffer) {
363
- log?.debug(`resending msg`, {
434
+ this.log?.debug(`resending msg`, {
364
435
  ...this.loggingMetadata,
365
436
  transportMessage: msg,
366
437
  connId: conn.id
@@ -372,7 +443,7 @@ var Session = class {
372
443
  code: import_api2.SpanStatusCode.ERROR,
373
444
  message: errMsg
374
445
  });
375
- log?.error(errMsg, {
446
+ this.log?.error(errMsg, {
376
447
  ...this.loggingMetadata,
377
448
  transportMessage: msg,
378
449
  connId: conn.id,
@@ -385,7 +456,7 @@ var Session = class {
385
456
  }
386
457
  updateBookkeeping(ack, seq) {
387
458
  if (seq + 1 < this.ack) {
388
- log?.error(`received stale seq ${seq} + 1 < ${this.ack}`, {
459
+ this.log?.error(`received stale seq ${seq} + 1 < ${this.ack}`, {
389
460
  ...this.loggingMetadata,
390
461
  tags: ["invariant-violation"]
391
462
  });
@@ -397,7 +468,7 @@ var Session = class {
397
468
  closeStaleConnection(conn) {
398
469
  if (this.connection === void 0 || this.connection === conn)
399
470
  return;
400
- log?.info(
471
+ this.log?.info(
401
472
  `closing old inner connection from session to ${this.to}`,
402
473
  this.loggingMetadata
403
474
  );
@@ -411,7 +482,7 @@ var Session = class {
411
482
  this.connection = newConn;
412
483
  }
413
484
  beginGrace(cb) {
414
- log?.info(
485
+ this.log?.info(
415
486
  `starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
416
487
  this.loggingMetadata
417
488
  );
@@ -645,6 +716,7 @@ var Transport = class {
645
716
  * The options for this transport.
646
717
  */
647
718
  options;
719
+ log;
648
720
  /**
649
721
  * Creates a new Transport instance.
650
722
  * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
@@ -659,6 +731,13 @@ var Transport = class {
659
731
  this.clientId = clientId;
660
732
  this.state = "open";
661
733
  }
734
+ bindLogger(fn, level) {
735
+ if (typeof fn === "function") {
736
+ this.log = createLogProxy(new BaseLogger(fn, level));
737
+ return;
738
+ }
739
+ this.log = createLogProxy(fn);
740
+ }
662
741
  /**
663
742
  * Called when a new connection is established
664
743
  * and we know the identity of the connected client.
@@ -669,13 +748,15 @@ var Transport = class {
669
748
  status: "connect",
670
749
  conn
671
750
  });
672
- conn.telemetry = createConnectionTelemetryInfo(
673
- conn,
674
- session.telemetry.span
675
- );
751
+ conn.telemetry = createConnectionTelemetryInfo(conn, session.telemetry);
676
752
  if (isReconnect) {
677
753
  session.replaceWithNewConnection(conn);
678
- log?.info(`reconnected to ${connectedTo}`, session.loggingMetadata);
754
+ this.log?.info(`reconnected to ${connectedTo}`, {
755
+ ...conn.loggingMetadata,
756
+ ...session.loggingMetadata,
757
+ clientId: this.clientId,
758
+ connectedTo
759
+ });
679
760
  }
680
761
  }
681
762
  createSession(to, conn, propagationCtx) {
@@ -686,6 +767,9 @@ var Transport = class {
686
767
  this.options,
687
768
  propagationCtx
688
769
  );
770
+ if (this.log) {
771
+ session.bindLogger(this.log);
772
+ }
689
773
  this.sessions.set(session.to, session);
690
774
  this.eventDispatcher.dispatchEvent("sessionStatus", {
691
775
  status: "connect",
@@ -697,7 +781,7 @@ var Transport = class {
697
781
  let session = this.sessions.get(to);
698
782
  let isReconnect = session !== void 0;
699
783
  if (session?.advertisedSessionId !== void 0 && sessionId !== void 0 && session.advertisedSessionId !== sessionId) {
700
- log?.info(
784
+ this.log?.info(
701
785
  `session for ${to} already exists but has a different session id (expected: ${session.advertisedSessionId}, got: ${sessionId}), creating a new one`,
702
786
  session.loggingMetadata
703
787
  );
@@ -707,7 +791,7 @@ var Transport = class {
707
791
  }
708
792
  if (!session) {
709
793
  session = this.createSession(to, conn, propagationCtx);
710
- log?.info(
794
+ this.log?.info(
711
795
  `no session for ${to}, created a new one`,
712
796
  session.loggingMetadata
713
797
  );
@@ -721,7 +805,7 @@ var Transport = class {
721
805
  session.close();
722
806
  session.telemetry.span.end();
723
807
  this.sessions.delete(session.to);
724
- log?.info(
808
+ this.log?.info(
725
809
  `session ${session.id} disconnect from ${session.to}`,
726
810
  session.loggingMetadata
727
811
  );
@@ -752,18 +836,23 @@ var Transport = class {
752
836
  * @param msg The message to parse.
753
837
  * @returns The parsed message, or null if the message is malformed or invalid.
754
838
  */
755
- parseMsg(msg) {
839
+ parseMsg(msg, conn) {
756
840
  const parsedMsg = this.codec.fromBuffer(msg);
757
841
  if (parsedMsg === null) {
758
842
  const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));
759
- log?.error(`received malformed msg, killing conn: ${decodedBuffer}`, {
760
- clientId: this.clientId
761
- });
843
+ this.log?.error(
844
+ `received malformed msg, killing conn: ${decodedBuffer}`,
845
+ {
846
+ clientId: this.clientId,
847
+ ...conn.loggingMetadata
848
+ }
849
+ );
762
850
  return null;
763
851
  }
764
852
  if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
765
- log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
853
+ this.log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
766
854
  clientId: this.clientId,
855
+ ...conn.loggingMetadata,
767
856
  validationErrors: [
768
857
  ...import_value.Value.Errors(OpaqueTransportMessageSchema, parsedMsg)
769
858
  ]
@@ -777,34 +866,41 @@ var Transport = class {
777
866
  * You generally shouldn't need to override this in downstream transport implementations.
778
867
  * @param msg The received message.
779
868
  */
780
- handleMsg(msg) {
869
+ handleMsg(msg, conn) {
781
870
  if (this.state !== "open")
782
871
  return;
783
872
  const session = this.sessions.get(msg.from);
784
873
  if (!session) {
785
- log?.error(`no existing session for ${msg.from}`, {
874
+ this.log?.error(`received message for unknown session from ${msg.from}`, {
786
875
  clientId: this.clientId,
787
876
  transportMessage: msg,
877
+ ...conn.loggingMetadata,
788
878
  tags: ["invariant-violation"]
789
879
  });
790
880
  return;
791
881
  }
792
882
  session.cancelGrace();
793
- log?.debug(`received msg`, {
883
+ this.log?.debug(`received msg`, {
794
884
  clientId: this.clientId,
795
- transportMessage: msg
885
+ transportMessage: msg,
886
+ ...conn.loggingMetadata
796
887
  });
797
888
  if (msg.seq !== session.nextExpectedSeq) {
798
889
  if (msg.seq < session.nextExpectedSeq) {
799
- log?.debug(
890
+ this.log?.debug(
800
891
  `received duplicate msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq}), discarding`,
801
- { clientId: this.clientId, transportMessage: msg }
892
+ {
893
+ clientId: this.clientId,
894
+ transportMessage: msg,
895
+ ...conn.loggingMetadata
896
+ }
802
897
  );
803
898
  } else {
804
899
  const errMsg = `received out-of-order msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq})`;
805
- log?.error(`${errMsg}, marking connection as dead`, {
900
+ this.log?.error(`${errMsg}, marking connection as dead`, {
806
901
  clientId: this.clientId,
807
902
  transportMessage: msg,
903
+ ...conn.loggingMetadata,
808
904
  tags: ["invariant-violation"]
809
905
  });
810
906
  this.protocolError(ProtocolError.MessageOrderingViolated, errMsg);
@@ -820,9 +916,10 @@ var Transport = class {
820
916
  if (!isAck(msg.controlFlags)) {
821
917
  this.eventDispatcher.dispatchEvent("message", msg);
822
918
  } else {
823
- log?.debug(`discarding msg (ack bit set)`, {
919
+ this.log?.debug(`discarding msg (ack bit set)`, {
824
920
  clientId: this.clientId,
825
- transportMessage: msg
921
+ transportMessage: msg,
922
+ ...conn.loggingMetadata
826
923
  });
827
924
  }
828
925
  }
@@ -851,7 +948,7 @@ var Transport = class {
851
948
  send(to, msg) {
852
949
  if (this.state === "destroyed") {
853
950
  const err = "transport is destroyed, cant send";
854
- log?.error(err, {
951
+ this.log?.error(err, {
855
952
  clientId: this.clientId,
856
953
  transportMessage: msg,
857
954
  tags: ["invariant-violation"]
@@ -859,7 +956,7 @@ var Transport = class {
859
956
  this.protocolError(ProtocolError.UseAfterDestroy, err);
860
957
  return void 0;
861
958
  } else if (this.state === "closed") {
862
- log?.info(`transport closed when sending, discarding`, {
959
+ this.log?.info(`transport closed when sending, discarding`, {
863
960
  clientId: this.clientId,
864
961
  transportMessage: msg
865
962
  });
@@ -890,7 +987,7 @@ var Transport = class {
890
987
  for (const session of this.sessions.values()) {
891
988
  this.deleteSession(session);
892
989
  }
893
- log?.info(`manually closed transport`, { clientId: this.clientId });
990
+ this.log?.info(`manually closed transport`, { clientId: this.clientId });
894
991
  }
895
992
  /**
896
993
  * Default destroy implementation for transports. You should override this in the downstream
@@ -902,7 +999,7 @@ var Transport = class {
902
999
  for (const session of this.sessions.values()) {
903
1000
  this.deleteSession(session);
904
1001
  }
905
- log?.info(`manually destroyed transport`, { clientId: this.clientId });
1002
+ this.log?.info(`manually destroyed transport`, { clientId: this.clientId });
906
1003
  }
907
1004
  };
908
1005
  var ClientTransport = class extends Transport {
@@ -944,9 +1041,9 @@ var ClientTransport = class extends Transport {
944
1041
  let session = void 0;
945
1042
  const handshakeTimeout = setTimeout(() => {
946
1043
  if (!session) {
947
- log?.warn(
1044
+ this.log?.warn(
948
1045
  `connection to ${to} timed out waiting for handshake, closing`,
949
- { clientId: this.clientId, connectedTo: to, connId: conn.id }
1046
+ { ...conn.loggingMetadata, clientId: this.clientId, connectedTo: to }
950
1047
  );
951
1048
  conn.close();
952
1049
  }
@@ -962,7 +1059,7 @@ var ClientTransport = class extends Transport {
962
1059
  }
963
1060
  conn.removeDataListener(handshakeHandler);
964
1061
  conn.addDataListener((data2) => {
965
- const parsed = this.parseMsg(data2);
1062
+ const parsed = this.parseMsg(data2, conn);
966
1063
  if (!parsed) {
967
1064
  conn.telemetry?.span.setStatus({
968
1065
  code: import_api3.SpanStatusCode.ERROR,
@@ -971,7 +1068,7 @@ var ClientTransport = class extends Transport {
971
1068
  conn.close();
972
1069
  return;
973
1070
  }
974
- this.handleMsg(parsed);
1071
+ this.handleMsg(parsed, conn);
975
1072
  });
976
1073
  };
977
1074
  conn.addDataListener(handshakeHandler);
@@ -979,7 +1076,8 @@ var ClientTransport = class extends Transport {
979
1076
  if (session) {
980
1077
  this.onDisconnect(conn, session);
981
1078
  }
982
- log?.info(`connection to ${to} disconnected`, {
1079
+ this.log?.info(`connection to ${to} disconnected`, {
1080
+ ...conn.loggingMetadata,
983
1081
  ...session?.loggingMetadata,
984
1082
  clientId: this.clientId,
985
1083
  connectedTo: to
@@ -994,15 +1092,19 @@ var ClientTransport = class extends Transport {
994
1092
  code: import_api3.SpanStatusCode.ERROR,
995
1093
  message: "connection error"
996
1094
  });
997
- log?.warn(`error in connection to ${to}: ${coerceErrorString(err)}`, {
998
- ...session?.loggingMetadata,
999
- clientId: this.clientId,
1000
- connectedTo: to
1001
- });
1095
+ this.log?.warn(
1096
+ `error in connection to ${to}: ${coerceErrorString(err)}`,
1097
+ {
1098
+ ...conn.loggingMetadata,
1099
+ ...session?.loggingMetadata,
1100
+ clientId: this.clientId,
1101
+ connectedTo: to
1102
+ }
1103
+ );
1002
1104
  });
1003
1105
  }
1004
1106
  receiveHandshakeResponseMessage(data, conn) {
1005
- const parsed = this.parseMsg(data);
1107
+ const parsed = this.parseMsg(data, conn);
1006
1108
  if (!parsed) {
1007
1109
  conn.telemetry?.span.setStatus({
1008
1110
  code: import_api3.SpanStatusCode.ERROR,
@@ -1019,7 +1121,8 @@ var ClientTransport = class extends Transport {
1019
1121
  code: import_api3.SpanStatusCode.ERROR,
1020
1122
  message: "invalid handshake response"
1021
1123
  });
1022
- log?.warn(`received invalid handshake resp`, {
1124
+ this.log?.warn(`received invalid handshake resp`, {
1125
+ ...conn.loggingMetadata,
1023
1126
  clientId: this.clientId,
1024
1127
  connectedTo: parsed.from,
1025
1128
  transportMessage: parsed,
@@ -1041,7 +1144,8 @@ var ClientTransport = class extends Transport {
1041
1144
  code: import_api3.SpanStatusCode.ERROR,
1042
1145
  message: "handshake rejected"
1043
1146
  });
1044
- log?.warn(`received handshake rejection`, {
1147
+ this.log?.warn(`received handshake rejection`, {
1148
+ ...conn.loggingMetadata,
1045
1149
  clientId: this.clientId,
1046
1150
  connectedTo: parsed.from,
1047
1151
  transportMessage: parsed
@@ -1052,7 +1156,8 @@ var ClientTransport = class extends Transport {
1052
1156
  );
1053
1157
  return false;
1054
1158
  }
1055
- log?.debug(`handshake from ${parsed.from} ok`, {
1159
+ this.log?.debug(`handshake from ${parsed.from} ok`, {
1160
+ ...conn.loggingMetadata,
1056
1161
  clientId: this.clientId,
1057
1162
  connectedTo: parsed.from,
1058
1163
  transportMessage: parsed
@@ -1073,7 +1178,7 @@ var ClientTransport = class extends Transport {
1073
1178
  async connect(to) {
1074
1179
  const canProceedWithConnection = () => this.state === "open";
1075
1180
  if (!canProceedWithConnection()) {
1076
- log?.info(
1181
+ this.log?.info(
1077
1182
  `transport state is no longer open, cancelling attempt to connect to ${to}`,
1078
1183
  { clientId: this.clientId, connectedTo: to }
1079
1184
  );
@@ -1084,7 +1189,7 @@ var ClientTransport = class extends Transport {
1084
1189
  const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
1085
1190
  if (!this.retryBudget.hasBudget(to)) {
1086
1191
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
1087
- log?.warn(errMsg, { clientId: this.clientId, connectedTo: to });
1192
+ this.log?.error(errMsg, { clientId: this.clientId, connectedTo: to });
1088
1193
  this.protocolError(ProtocolError.RetriesExceeded, errMsg);
1089
1194
  return;
1090
1195
  }
@@ -1093,10 +1198,13 @@ var ClientTransport = class extends Transport {
1093
1198
  if (backoffMs > 0) {
1094
1199
  sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
1095
1200
  }
1096
- log?.info(`attempting connection to ${to} (${backoffMs}ms backoff)`, {
1097
- clientId: this.clientId,
1098
- connectedTo: to
1099
- });
1201
+ this.log?.info(
1202
+ `attempting connection to ${to} (${backoffMs}ms backoff)`,
1203
+ {
1204
+ clientId: this.clientId,
1205
+ connectedTo: to
1206
+ }
1207
+ );
1100
1208
  this.retryBudget.consumeBudget(to);
1101
1209
  reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
1102
1210
  try {
@@ -1108,12 +1216,12 @@ var ClientTransport = class extends Transport {
1108
1216
  span.addEvent("connecting");
1109
1217
  const conn = await this.createNewOutgoingConnection(to);
1110
1218
  if (!canProceedWithConnection()) {
1111
- log?.info(
1219
+ this.log?.info(
1112
1220
  `transport state is no longer open, closing pre-handshake connection to ${to}`,
1113
1221
  {
1222
+ ...conn.loggingMetadata,
1114
1223
  clientId: this.clientId,
1115
- connectedTo: to,
1116
- connId: conn.id
1224
+ connectedTo: to
1117
1225
  }
1118
1226
  );
1119
1227
  conn.close();
@@ -1137,10 +1245,13 @@ var ClientTransport = class extends Transport {
1137
1245
  });
1138
1246
  this.inflightConnectionPromises.set(to, reconnectPromise);
1139
1247
  } else {
1140
- log?.info(`attempting connection to ${to} (reusing previous attempt)`, {
1141
- clientId: this.clientId,
1142
- connectedTo: to
1143
- });
1248
+ this.log?.info(
1249
+ `attempting connection to ${to} (reusing previous attempt)`,
1250
+ {
1251
+ clientId: this.clientId,
1252
+ connectedTo: to
1253
+ }
1254
+ );
1144
1255
  }
1145
1256
  try {
1146
1257
  await reconnectPromise;
@@ -1148,12 +1259,12 @@ var ClientTransport = class extends Transport {
1148
1259
  this.inflightConnectionPromises.delete(to);
1149
1260
  const errStr = coerceErrorString(error);
1150
1261
  if (!this.reconnectOnConnectionDrop || !canProceedWithConnection()) {
1151
- log?.warn(`connection to ${to} failed (${errStr})`, {
1262
+ this.log?.warn(`connection to ${to} failed (${errStr})`, {
1152
1263
  clientId: this.clientId,
1153
1264
  connectedTo: to
1154
1265
  });
1155
1266
  } else {
1156
- log?.warn(`connection to ${to} failed (${errStr}), retrying`, {
1267
+ this.log?.warn(`connection to ${to} failed (${errStr}), retrying`, {
1157
1268
  clientId: this.clientId,
1158
1269
  connectedTo: to
1159
1270
  });
@@ -1170,7 +1281,8 @@ var ClientTransport = class extends Transport {
1170
1281
  if (this.handshakeExtensions) {
1171
1282
  metadata = await this.handshakeExtensions.construct();
1172
1283
  if (!import_value.Value.Check(this.handshakeExtensions.schema, metadata)) {
1173
- log?.error(`constructed handshake metadata did not match schema`, {
1284
+ this.log?.error(`constructed handshake metadata did not match schema`, {
1285
+ ...conn.loggingMetadata,
1174
1286
  clientId: this.clientId,
1175
1287
  connectedTo: to,
1176
1288
  validationErrors: [
@@ -1197,9 +1309,11 @@ var ClientTransport = class extends Transport {
1197
1309
  metadata,
1198
1310
  getPropagationContext(session.telemetry.ctx)
1199
1311
  );
1200
- log?.debug(`sending handshake request to ${to}`, {
1312
+ this.log?.debug(`sending handshake request to ${to}`, {
1313
+ ...conn.loggingMetadata,
1201
1314
  clientId: this.clientId,
1202
- connectedTo: to
1315
+ connectedTo: to,
1316
+ transportMessage: requestMsg
1203
1317
  });
1204
1318
  conn.send(this.codec.toBuffer(requestMsg));
1205
1319
  return true;
@@ -1229,7 +1343,7 @@ var ServerTransport = class extends Transport {
1229
1343
  ...providedOptions
1230
1344
  };
1231
1345
  this.sessionHandshakeMetadata = /* @__PURE__ */ new WeakMap();
1232
- log?.info(`initiated server transport`, {
1346
+ this.log?.info(`initiated server transport`, {
1233
1347
  clientId: this.clientId,
1234
1348
  protocolVersion: PROTOCOL_VERSION
1235
1349
  });
@@ -1240,20 +1354,20 @@ var ServerTransport = class extends Transport {
1240
1354
  handleConnection(conn) {
1241
1355
  if (this.state !== "open")
1242
1356
  return;
1243
- log?.info(`new incoming connection`, {
1244
- clientId: this.clientId,
1245
- connId: conn.id
1357
+ this.log?.info(`new incoming connection`, {
1358
+ ...conn.loggingMetadata,
1359
+ clientId: this.clientId
1246
1360
  });
1247
1361
  let session = void 0;
1248
1362
  const client = () => session?.to ?? "unknown";
1249
1363
  const handshakeTimeout = setTimeout(() => {
1250
1364
  if (!session) {
1251
- log?.warn(
1365
+ this.log?.warn(
1252
1366
  `connection to ${client()} timed out waiting for handshake, closing`,
1253
1367
  {
1368
+ ...conn.loggingMetadata,
1254
1369
  clientId: this.clientId,
1255
- connectedTo: client(),
1256
- connId: conn.id
1370
+ connectedTo: client()
1257
1371
  }
1258
1372
  );
1259
1373
  conn.telemetry?.span.setStatus({
@@ -1280,12 +1394,12 @@ var ServerTransport = class extends Transport {
1280
1394
  }
1281
1395
  session = maybeSession;
1282
1396
  const dataHandler = (data2) => {
1283
- const parsed = this.parseMsg(data2);
1397
+ const parsed = this.parseMsg(data2, conn);
1284
1398
  if (!parsed) {
1285
1399
  conn.close();
1286
1400
  return;
1287
1401
  }
1288
- this.handleMsg(parsed);
1402
+ this.handleMsg(parsed, conn);
1289
1403
  };
1290
1404
  for (const data2 of buffer) {
1291
1405
  dataHandler(data2);
@@ -1300,9 +1414,9 @@ var ServerTransport = class extends Transport {
1300
1414
  conn.addCloseListener(() => {
1301
1415
  if (!session)
1302
1416
  return;
1303
- log?.info(`connection to ${client()} disconnected`, {
1304
- clientId: this.clientId,
1305
- connId: conn.id
1417
+ this.log?.info(`connection to ${client()} disconnected`, {
1418
+ ...conn.loggingMetadata,
1419
+ clientId: this.clientId
1306
1420
  });
1307
1421
  this.onDisconnect(conn, session);
1308
1422
  });
@@ -1313,9 +1427,9 @@ var ServerTransport = class extends Transport {
1313
1427
  });
1314
1428
  if (!session)
1315
1429
  return;
1316
- log?.warn(
1430
+ this.log?.warn(
1317
1431
  `connection to ${client()} got an error: ${coerceErrorString(err)}`,
1318
- { clientId: this.clientId, connId: conn.id }
1432
+ { ...conn.loggingMetadata, clientId: this.clientId }
1319
1433
  );
1320
1434
  });
1321
1435
  }
@@ -1333,9 +1447,9 @@ var ServerTransport = class extends Transport {
1333
1447
  reason
1334
1448
  });
1335
1449
  conn.send(this.codec.toBuffer(responseMsg));
1336
- log?.warn(`received malformed handshake metadata from ${from}`, {
1450
+ this.log?.warn(`received malformed handshake metadata from ${from}`, {
1451
+ ...conn.loggingMetadata,
1337
1452
  clientId: this.clientId,
1338
- connId: conn.id,
1339
1453
  validationErrors: [
1340
1454
  ...import_value.Value.Errors(this.handshakeExtensions.schema, rawMetadata)
1341
1455
  ]
@@ -1359,9 +1473,9 @@ var ServerTransport = class extends Transport {
1359
1473
  reason
1360
1474
  });
1361
1475
  conn.send(this.codec.toBuffer(responseMsg));
1362
- log?.warn(`rejected handshake from ${from}`, {
1363
- clientId: this.clientId,
1364
- connId: conn.id
1476
+ this.log?.warn(`rejected handshake from ${from}`, {
1477
+ ...conn.loggingMetadata,
1478
+ clientId: this.clientId
1365
1479
  });
1366
1480
  this.protocolError(ProtocolError.HandshakeFailed, reason);
1367
1481
  return false;
@@ -1370,7 +1484,7 @@ var ServerTransport = class extends Transport {
1370
1484
  return parsedMetadata;
1371
1485
  }
1372
1486
  async receiveHandshakeRequestMessage(data, conn) {
1373
- const parsed = this.parseMsg(data);
1487
+ const parsed = this.parseMsg(data, conn);
1374
1488
  if (!parsed) {
1375
1489
  conn.telemetry?.span.setStatus({
1376
1490
  code: import_api3.SpanStatusCode.ERROR,
@@ -1393,10 +1507,10 @@ var ServerTransport = class extends Transport {
1393
1507
  reason
1394
1508
  });
1395
1509
  conn.send(this.codec.toBuffer(responseMsg2));
1396
- log?.warn(reason, {
1510
+ this.log?.warn(reason, {
1511
+ ...conn.loggingMetadata,
1397
1512
  clientId: this.clientId,
1398
- connId: conn.id,
1399
- // safe to log metadata here as we remove the payload
1513
+ // safe to this.log metadata here as we remove the payload
1400
1514
  // before passing it to user-land
1401
1515
  transportMessage: parsed,
1402
1516
  validationErrors: [
@@ -1421,9 +1535,9 @@ var ServerTransport = class extends Transport {
1421
1535
  reason
1422
1536
  });
1423
1537
  conn.send(this.codec.toBuffer(responseMsg2));
1424
- log?.warn(
1538
+ this.log?.warn(
1425
1539
  `received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`,
1426
- { clientId: this.clientId, connId: conn.id }
1540
+ { ...conn.loggingMetadata, clientId: this.clientId }
1427
1541
  );
1428
1542
  this.protocolError(ProtocolError.HandshakeFailed, reason);
1429
1543
  return false;
@@ -1445,9 +1559,9 @@ var ServerTransport = class extends Transport {
1445
1559
  parsed.tracing
1446
1560
  );
1447
1561
  this.sessionHandshakeMetadata.set(session, parsedMetadata);
1448
- log?.debug(
1562
+ this.log?.debug(
1449
1563
  `handshake from ${parsed.from} ok, responding with handshake success`,
1450
- { clientId: this.clientId, connId: conn.id }
1564
+ conn.loggingMetadata
1451
1565
  );
1452
1566
  const responseMsg = handshakeResponseMessage(this.clientId, parsed.from, {
1453
1567
  ok: true,