@replit/river 0.18.5 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/{chunk-K7CUSLWL.js → chunk-4PFUC4QB.js} +1 -1
  2. package/dist/{chunk-VH3NGOXQ.js → chunk-D5PVGZPQ.js} +5 -3
  3. package/dist/{chunk-UABIFWM7.js → chunk-FILQOCCK.js} +219 -104
  4. package/dist/{chunk-6Q3MSICL.js → chunk-LO6MRYNI.js} +10 -2
  5. package/dist/{chunk-PUX3U2SZ.js → chunk-ZNXZ7QPT.js} +1 -1
  6. package/dist/{connection-893bd769.d.ts → connection-d625aa8d.d.ts} +1 -1
  7. package/dist/{connection-89918b74.d.ts → connection-ddc757b9.d.ts} +1 -1
  8. package/dist/index-2941de8c.d.ts +341 -0
  9. package/dist/logging/index.d.cts +2 -1
  10. package/dist/logging/index.d.ts +2 -1
  11. package/dist/router/index.cjs +11 -2
  12. package/dist/router/index.d.cts +7 -383
  13. package/dist/router/index.d.ts +7 -383
  14. package/dist/router/index.js +2 -2
  15. package/dist/services-d47ce743.d.ts +736 -0
  16. package/dist/services-ee45322f.d.ts +736 -0
  17. package/dist/transport/impls/uds/client.cjs +80 -53
  18. package/dist/transport/impls/uds/client.d.cts +5 -5
  19. package/dist/transport/impls/uds/client.d.ts +5 -5
  20. package/dist/transport/impls/uds/client.js +3 -3
  21. package/dist/transport/impls/uds/server.cjs +151 -62
  22. package/dist/transport/impls/uds/server.d.cts +4 -4
  23. package/dist/transport/impls/uds/server.d.ts +4 -4
  24. package/dist/transport/impls/uds/server.js +3 -3
  25. package/dist/transport/impls/ws/client.cjs +80 -53
  26. package/dist/transport/impls/ws/client.d.cts +3 -3
  27. package/dist/transport/impls/ws/client.d.ts +3 -3
  28. package/dist/transport/impls/ws/client.js +3 -3
  29. package/dist/transport/impls/ws/server.cjs +151 -62
  30. package/dist/transport/impls/ws/server.d.cts +4 -4
  31. package/dist/transport/impls/ws/server.d.ts +4 -4
  32. package/dist/transport/impls/ws/server.js +3 -3
  33. package/dist/transport/index.cjs +188 -71
  34. package/dist/transport/index.d.cts +295 -3
  35. package/dist/transport/index.d.ts +295 -3
  36. package/dist/transport/index.js +2 -3
  37. package/dist/util/testHelpers.cjs +410 -401
  38. package/dist/util/testHelpers.d.cts +3 -3
  39. package/dist/util/testHelpers.d.ts +3 -3
  40. package/dist/util/testHelpers.js +3 -4
  41. package/package.json +1 -1
  42. package/dist/chunk-RPIDSIQG.js +0 -0
  43. package/dist/index-46ed19d8.d.ts +0 -111
  44. package/dist/index-d412ca83.d.ts +0 -420
  45. package/dist/procedures-bfffcb0b.d.ts +0 -324
@@ -56,7 +56,8 @@ var PROTOCOL_VERSION = "v1.1";
56
56
  var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
57
57
  type: import_typebox.Type.Literal("HANDSHAKE_REQ"),
58
58
  protocolVersion: import_typebox.Type.String(),
59
- sessionId: import_typebox.Type.String()
59
+ sessionId: import_typebox.Type.String(),
60
+ metadata: import_typebox.Type.Optional(import_typebox.Type.Unknown())
60
61
  });
61
62
  var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
62
63
  type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
@@ -80,7 +81,7 @@ var ControlMessagePayloadSchema = import_typebox.Type.Union([
80
81
  var OpaqueTransportMessageSchema = TransportMessageSchema(
81
82
  import_typebox.Type.Unknown()
82
83
  );
83
- function handshakeRequestMessage(from, to, sessionId) {
84
+ function handshakeRequestMessage(from, to, sessionId, metadata) {
84
85
  return {
85
86
  id: (0, import_nanoid.nanoid)(),
86
87
  from,
@@ -92,7 +93,8 @@ function handshakeRequestMessage(from, to, sessionId) {
92
93
  payload: {
93
94
  type: "HANDSHAKE_REQ",
94
95
  protocolVersion: PROTOCOL_VERSION,
95
- sessionId
96
+ sessionId,
97
+ metadata
96
98
  }
97
99
  };
98
100
  }
@@ -131,6 +133,12 @@ var Session = class {
131
133
  * for this session.
132
134
  */
133
135
  advertisedSessionId;
136
+ /**
137
+ * The metadata for this session, as parsed from the handshake.
138
+ *
139
+ * Will only ever be populated on the server side.
140
+ */
141
+ metadata;
134
142
  /**
135
143
  * Number of messages we've sent along this session (excluding handshake and acks)
136
144
  */
@@ -233,27 +241,24 @@ var Session = class {
233
241
  this.seq = 0;
234
242
  this.ack = 0;
235
243
  }
236
- sendBufferedMessages() {
237
- if (!this.connection) {
238
- const msg = `tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
239
- log?.error(msg, this.loggingMetadata);
240
- throw new Error(msg);
241
- }
242
- log?.info(
243
- `resending ${this.sendBuffer.length} buffered messages`,
244
- this.loggingMetadata
245
- );
244
+ sendBufferedMessages(conn) {
245
+ log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
246
+ ...this.loggingMetadata,
247
+ connId: conn.debugId
248
+ });
246
249
  for (const msg of this.sendBuffer) {
247
250
  log?.debug(`resending msg`, {
248
251
  ...this.loggingMetadata,
249
- fullTransportMessage: msg
252
+ fullTransportMessage: msg,
253
+ connId: conn.debugId
250
254
  });
251
- const ok = this.connection.send(this.codec.toBuffer(msg));
255
+ const ok = conn.send(this.codec.toBuffer(msg));
252
256
  if (!ok) {
253
- const errMsg = `failed to send buffered message to ${this.to} (if you hit this code path something is seriously wrong)`;
257
+ const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;
254
258
  log?.error(errMsg, {
255
259
  ...this.loggingMetadata,
256
- fullTransportMessage: msg
260
+ fullTransportMessage: msg,
261
+ connId: conn.debugId
257
262
  });
258
263
  throw new Error(errMsg);
259
264
  }
@@ -284,6 +289,7 @@ var Session = class {
284
289
  this.closeStaleConnection(newConn);
285
290
  this.cancelGrace();
286
291
  this.connection = newConn;
292
+ this.sendBufferedMessages(newConn);
287
293
  }
288
294
  beginGrace(cb) {
289
295
  log?.info(
@@ -612,6 +618,9 @@ var defaultClientTransportOptions = {
612
618
  ...defaultTransportOptions,
613
619
  ...defaultConnectionRetryOptions
614
620
  };
621
+ var defaultServerTransportOptions = {
622
+ ...defaultTransportOptions
623
+ };
615
624
  var Transport = class {
616
625
  /**
617
626
  * A flag indicating whether the transport has been destroyed.
@@ -665,37 +674,15 @@ var Transport = class {
665
674
  * and we know the identity of the connected client.
666
675
  * @param conn The connection object.
667
676
  */
668
- onConnect(conn, connectedTo, advertisedSessionId) {
677
+ onConnect(conn, connectedTo, session, isReconnect) {
669
678
  this.eventDispatcher.dispatchEvent("connectionStatus", {
670
679
  status: "connect",
671
680
  conn
672
681
  });
673
- let oldSession = this.sessions.get(connectedTo);
674
- if (oldSession?.advertisedSessionId && oldSession.advertisedSessionId !== advertisedSessionId) {
675
- log?.warn(
676
- `connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), killing old session and starting a new one`,
677
- oldSession.loggingMetadata
678
- );
679
- this.deleteSession(oldSession);
680
- oldSession = void 0;
681
- }
682
- if (oldSession === void 0) {
683
- const newSession = this.createSession(connectedTo, conn);
684
- newSession.advertisedSessionId = advertisedSessionId;
685
- log?.info(
686
- `new connection for new session to ${connectedTo}`,
687
- newSession.loggingMetadata
688
- );
689
- return newSession;
682
+ if (isReconnect) {
683
+ session.replaceWithNewConnection(conn);
684
+ log?.info(`reconnected to ${connectedTo}`, session.loggingMetadata);
690
685
  }
691
- oldSession.replaceWithNewConnection(conn);
692
- oldSession.sendBufferedMessages();
693
- oldSession.advertisedSessionId = advertisedSessionId;
694
- log?.info(
695
- `new connection for existing session to ${connectedTo}`,
696
- oldSession.loggingMetadata
697
- );
698
- return oldSession;
699
686
  }
700
687
  createSession(to, conn) {
701
688
  const session = new Session(
@@ -711,8 +698,18 @@ var Transport = class {
711
698
  });
712
699
  return session;
713
700
  }
714
- getOrCreateSession(to, conn) {
701
+ getOrCreateSession(to, conn, sessionId) {
715
702
  let session = this.sessions.get(to);
703
+ let isReconnect = session !== void 0;
704
+ if (session?.advertisedSessionId !== void 0 && sessionId !== void 0 && session.advertisedSessionId !== sessionId) {
705
+ log?.warn(
706
+ `session for ${to} already exists but has a different session id (expected: ${session.advertisedSessionId}, got: ${sessionId}), creating a new one`,
707
+ session.loggingMetadata
708
+ );
709
+ this.deleteSession(session);
710
+ isReconnect = false;
711
+ session = void 0;
712
+ }
716
713
  if (!session) {
717
714
  session = this.createSession(to, conn);
718
715
  log?.info(
@@ -720,7 +717,10 @@ var Transport = class {
720
717
  session.loggingMetadata
721
718
  );
722
719
  }
723
- return session;
720
+ if (sessionId !== void 0) {
721
+ session.advertisedSessionId = sessionId;
722
+ }
723
+ return { session, isReconnect };
724
724
  }
725
725
  deleteSession(session) {
726
726
  session.close();
@@ -855,7 +855,7 @@ var Transport = class {
855
855
  });
856
856
  return void 0;
857
857
  }
858
- return this.getOrCreateSession(to).send(msg);
858
+ return this.getOrCreateSession(to).session.send(msg);
859
859
  }
860
860
  // control helpers
861
861
  sendCloseStream(to, streamId) {
@@ -1014,11 +1014,12 @@ var ClientTransport = class extends Transport {
1014
1014
  connectedTo: parsed.from,
1015
1015
  fullTransportMessage: parsed
1016
1016
  });
1017
- const session = this.onConnect(
1018
- conn,
1017
+ const { session, isReconnect } = this.getOrCreateSession(
1019
1018
  parsed.from,
1019
+ conn,
1020
1020
  parsed.payload.status.sessionId
1021
1021
  );
1022
+ this.onConnect(conn, parsed.from, session, isReconnect);
1022
1023
  this.retryBudget.startRestoringBudget(parsed.from);
1023
1024
  return session;
1024
1025
  }
@@ -1071,8 +1072,13 @@ var ClientTransport = class extends Transport {
1071
1072
  conn.close();
1072
1073
  throw new Error("transport state is no longer open");
1073
1074
  }
1074
- this.sendHandshake(to, conn);
1075
- return conn;
1075
+ return this.sendHandshake(to, conn).then((ok) => {
1076
+ if (!ok) {
1077
+ conn.close();
1078
+ throw new Error("failed to send handshake");
1079
+ }
1080
+ return conn;
1081
+ });
1076
1082
  });
1077
1083
  this.inflightConnectionPromises.set(to, reconnectPromise);
1078
1084
  } else {
@@ -1104,14 +1110,35 @@ var ClientTransport = class extends Transport {
1104
1110
  this.inflightConnectionPromises.delete(session.to);
1105
1111
  super.deleteSession(session);
1106
1112
  }
1107
- sendHandshake(to, conn) {
1108
- const session = this.getOrCreateSession(to, conn);
1109
- const requestMsg = handshakeRequestMessage(this.clientId, to, session.id);
1113
+ async sendHandshake(to, conn) {
1114
+ let metadata;
1115
+ if (this.options.handshake) {
1116
+ metadata = await this.options.handshake.get();
1117
+ if (!import_value.Value.Check(this.options.handshake.schema, metadata)) {
1118
+ log?.error(`handshake metadata did not match schema`, {
1119
+ clientId: this.clientId,
1120
+ connectedTo: to
1121
+ });
1122
+ this.protocolError(
1123
+ ProtocolError.HandshakeFailed,
1124
+ "handshake metadata did not match schema"
1125
+ );
1126
+ return false;
1127
+ }
1128
+ }
1129
+ const { session } = this.getOrCreateSession(to, conn);
1130
+ const requestMsg = handshakeRequestMessage(
1131
+ this.clientId,
1132
+ to,
1133
+ session.id,
1134
+ metadata
1135
+ );
1110
1136
  log?.debug(`sending handshake request to ${to}`, {
1111
1137
  clientId: this.clientId,
1112
1138
  connectedTo: to
1113
1139
  });
1114
1140
  conn.send(this.codec.toBuffer(requestMsg));
1141
+ return true;
1115
1142
  }
1116
1143
  close() {
1117
1144
  this.retryBudget.close();
@@ -1,10 +1,10 @@
1
- import { b as ClientTransport, P as ProvidedClientTransportOptions } from '../../../index-d412ca83.js';
2
- import { T as TransportClientId } from '../../../index-46ed19d8.js';
3
- import { U as UdsConnection } from '../../../connection-89918b74.js';
4
- import '../../../types-3e5768ec.js';
5
- import '@sinclair/typebox';
1
+ import { U as UdsConnection } from '../../../connection-ddc757b9.js';
2
+ import { ClientTransport, ClientTransportOptions as ProvidedClientTransportOptions } from '../../index.cjs';
3
+ import { T as TransportClientId } from '../../../index-2941de8c.js';
6
4
  import 'node:net';
7
5
  import 'node:stream';
6
+ import '../../../types-3e5768ec.js';
7
+ import '@sinclair/typebox';
8
8
 
9
9
  declare class UnixDomainSocketClientTransport extends ClientTransport<UdsConnection> {
10
10
  path: string;
@@ -1,10 +1,10 @@
1
- import { b as ClientTransport, P as ProvidedClientTransportOptions } from '../../../index-d412ca83.js';
2
- import { T as TransportClientId } from '../../../index-46ed19d8.js';
3
- import { U as UdsConnection } from '../../../connection-89918b74.js';
4
- import '../../../types-3e5768ec.js';
5
- import '@sinclair/typebox';
1
+ import { U as UdsConnection } from '../../../connection-ddc757b9.js';
2
+ import { ClientTransport, ClientTransportOptions as ProvidedClientTransportOptions } from '../../index.js';
3
+ import { T as TransportClientId } from '../../../index-2941de8c.js';
6
4
  import 'node:net';
7
5
  import 'node:stream';
6
+ import '../../../types-3e5768ec.js';
7
+ import '@sinclair/typebox';
8
8
 
9
9
  declare class UnixDomainSocketClientTransport extends ClientTransport<UdsConnection> {
10
10
  path: string;
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  UdsConnection
3
- } from "../../../chunk-PUX3U2SZ.js";
3
+ } from "../../../chunk-ZNXZ7QPT.js";
4
4
  import {
5
5
  ClientTransport
6
- } from "../../../chunk-UABIFWM7.js";
7
- import "../../../chunk-VH3NGOXQ.js";
6
+ } from "../../../chunk-FILQOCCK.js";
7
+ import "../../../chunk-D5PVGZPQ.js";
8
8
  import {
9
9
  log
10
10
  } from "../../../chunk-WER2DWCP.js";
@@ -52,7 +52,8 @@ var PROTOCOL_VERSION = "v1.1";
52
52
  var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
53
53
  type: import_typebox.Type.Literal("HANDSHAKE_REQ"),
54
54
  protocolVersion: import_typebox.Type.String(),
55
- sessionId: import_typebox.Type.String()
55
+ sessionId: import_typebox.Type.String(),
56
+ metadata: import_typebox.Type.Optional(import_typebox.Type.Unknown())
56
57
  });
57
58
  var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
58
59
  type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
@@ -164,6 +165,12 @@ var Session = class {
164
165
  * for this session.
165
166
  */
166
167
  advertisedSessionId;
168
+ /**
169
+ * The metadata for this session, as parsed from the handshake.
170
+ *
171
+ * Will only ever be populated on the server side.
172
+ */
173
+ metadata;
167
174
  /**
168
175
  * Number of messages we've sent along this session (excluding handshake and acks)
169
176
  */
@@ -266,27 +273,24 @@ var Session = class {
266
273
  this.seq = 0;
267
274
  this.ack = 0;
268
275
  }
269
- sendBufferedMessages() {
270
- if (!this.connection) {
271
- const msg = `tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
272
- log?.error(msg, this.loggingMetadata);
273
- throw new Error(msg);
274
- }
275
- log?.info(
276
- `resending ${this.sendBuffer.length} buffered messages`,
277
- this.loggingMetadata
278
- );
276
+ sendBufferedMessages(conn) {
277
+ log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
278
+ ...this.loggingMetadata,
279
+ connId: conn.debugId
280
+ });
279
281
  for (const msg of this.sendBuffer) {
280
282
  log?.debug(`resending msg`, {
281
283
  ...this.loggingMetadata,
282
- fullTransportMessage: msg
284
+ fullTransportMessage: msg,
285
+ connId: conn.debugId
283
286
  });
284
- const ok = this.connection.send(this.codec.toBuffer(msg));
287
+ const ok = conn.send(this.codec.toBuffer(msg));
285
288
  if (!ok) {
286
- const errMsg = `failed to send buffered message to ${this.to} (if you hit this code path something is seriously wrong)`;
289
+ const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;
287
290
  log?.error(errMsg, {
288
291
  ...this.loggingMetadata,
289
- fullTransportMessage: msg
292
+ fullTransportMessage: msg,
293
+ connId: conn.debugId
290
294
  });
291
295
  throw new Error(errMsg);
292
296
  }
@@ -317,6 +321,7 @@ var Session = class {
317
321
  this.closeStaleConnection(newConn);
318
322
  this.cancelGrace();
319
323
  this.connection = newConn;
324
+ this.sendBufferedMessages(newConn);
320
325
  }
321
326
  beginGrace(cb) {
322
327
  log?.info(
@@ -444,6 +449,9 @@ var defaultClientTransportOptions = {
444
449
  ...defaultTransportOptions,
445
450
  ...defaultConnectionRetryOptions
446
451
  };
452
+ var defaultServerTransportOptions = {
453
+ ...defaultTransportOptions
454
+ };
447
455
  var Transport = class {
448
456
  /**
449
457
  * A flag indicating whether the transport has been destroyed.
@@ -497,37 +505,15 @@ var Transport = class {
497
505
  * and we know the identity of the connected client.
498
506
  * @param conn The connection object.
499
507
  */
500
- onConnect(conn, connectedTo, advertisedSessionId) {
508
+ onConnect(conn, connectedTo, session, isReconnect) {
501
509
  this.eventDispatcher.dispatchEvent("connectionStatus", {
502
510
  status: "connect",
503
511
  conn
504
512
  });
505
- let oldSession = this.sessions.get(connectedTo);
506
- if (oldSession?.advertisedSessionId && oldSession.advertisedSessionId !== advertisedSessionId) {
507
- log?.warn(
508
- `connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), killing old session and starting a new one`,
509
- oldSession.loggingMetadata
510
- );
511
- this.deleteSession(oldSession);
512
- oldSession = void 0;
513
+ if (isReconnect) {
514
+ session.replaceWithNewConnection(conn);
515
+ log?.info(`reconnected to ${connectedTo}`, session.loggingMetadata);
513
516
  }
514
- if (oldSession === void 0) {
515
- const newSession = this.createSession(connectedTo, conn);
516
- newSession.advertisedSessionId = advertisedSessionId;
517
- log?.info(
518
- `new connection for new session to ${connectedTo}`,
519
- newSession.loggingMetadata
520
- );
521
- return newSession;
522
- }
523
- oldSession.replaceWithNewConnection(conn);
524
- oldSession.sendBufferedMessages();
525
- oldSession.advertisedSessionId = advertisedSessionId;
526
- log?.info(
527
- `new connection for existing session to ${connectedTo}`,
528
- oldSession.loggingMetadata
529
- );
530
- return oldSession;
531
517
  }
532
518
  createSession(to, conn) {
533
519
  const session = new Session(
@@ -543,8 +529,18 @@ var Transport = class {
543
529
  });
544
530
  return session;
545
531
  }
546
- getOrCreateSession(to, conn) {
532
+ getOrCreateSession(to, conn, sessionId) {
547
533
  let session = this.sessions.get(to);
534
+ let isReconnect = session !== void 0;
535
+ if (session?.advertisedSessionId !== void 0 && sessionId !== void 0 && session.advertisedSessionId !== sessionId) {
536
+ log?.warn(
537
+ `session for ${to} already exists but has a different session id (expected: ${session.advertisedSessionId}, got: ${sessionId}), creating a new one`,
538
+ session.loggingMetadata
539
+ );
540
+ this.deleteSession(session);
541
+ isReconnect = false;
542
+ session = void 0;
543
+ }
548
544
  if (!session) {
549
545
  session = this.createSession(to, conn);
550
546
  log?.info(
@@ -552,7 +548,10 @@ var Transport = class {
552
548
  session.loggingMetadata
553
549
  );
554
550
  }
555
- return session;
551
+ if (sessionId !== void 0) {
552
+ session.advertisedSessionId = sessionId;
553
+ }
554
+ return { session, isReconnect };
556
555
  }
557
556
  deleteSession(session) {
558
557
  session.close();
@@ -687,7 +686,7 @@ var Transport = class {
687
686
  });
688
687
  return void 0;
689
688
  }
690
- return this.getOrCreateSession(to).send(msg);
689
+ return this.getOrCreateSession(to).session.send(msg);
691
690
  }
692
691
  // control helpers
693
692
  sendCloseStream(to, streamId) {
@@ -728,8 +727,16 @@ var Transport = class {
728
727
  }
729
728
  };
730
729
  var ServerTransport = class extends Transport {
730
+ /**
731
+ * The options for this transport.
732
+ */
733
+ options;
731
734
  constructor(clientId, providedOptions) {
732
735
  super(clientId, providedOptions);
736
+ this.options = {
737
+ ...defaultServerTransportOptions,
738
+ ...providedOptions
739
+ };
733
740
  log?.info(`initiated server transport`, {
734
741
  clientId: this.clientId,
735
742
  protocolVersion: PROTOCOL_VERSION
@@ -757,24 +764,38 @@ var ServerTransport = class extends Transport {
757
764
  conn.close();
758
765
  }
759
766
  }, this.options.sessionDisconnectGraceMs);
767
+ const buffer = [];
768
+ let receivedHandshakeMessage = false;
760
769
  const handshakeHandler = (data) => {
761
- const maybeSession = this.receiveHandshakeRequestMessage(data, conn);
762
- if (!maybeSession) {
763
- conn.close();
770
+ if (receivedHandshakeMessage) {
771
+ buffer.push(data);
764
772
  return;
765
- } else {
766
- session = maybeSession;
767
- clearTimeout(handshakeTimeout);
768
773
  }
769
- conn.removeDataListener(handshakeHandler);
770
- conn.addDataListener((data2) => {
771
- const parsed = this.parseMsg(data2);
772
- if (!parsed) {
773
- conn.close();
774
- return;
774
+ receivedHandshakeMessage = true;
775
+ clearTimeout(handshakeTimeout);
776
+ void this.receiveHandshakeRequestMessage(data, conn).then(
777
+ (maybeSession) => {
778
+ if (!maybeSession) {
779
+ conn.close();
780
+ return;
781
+ }
782
+ session = maybeSession;
783
+ const dataHandler = (data2) => {
784
+ const parsed = this.parseMsg(data2);
785
+ if (!parsed) {
786
+ conn.close();
787
+ return;
788
+ }
789
+ this.handleMsg(parsed);
790
+ };
791
+ conn.removeDataListener(handshakeHandler);
792
+ conn.addDataListener(dataHandler);
793
+ for (const data2 of buffer) {
794
+ dataHandler(data2);
795
+ }
796
+ buffer.length = 0;
775
797
  }
776
- this.handleMsg(parsed);
777
- });
798
+ );
778
799
  };
779
800
  conn.addDataListener(handshakeHandler);
780
801
  conn.addCloseListener(() => {
@@ -795,7 +816,7 @@ var ServerTransport = class extends Transport {
795
816
  );
796
817
  });
797
818
  }
798
- receiveHandshakeRequestMessage(data, conn) {
819
+ async receiveHandshakeRequestMessage(data, conn) {
799
820
  const parsed = this.parseMsg(data);
800
821
  if (!parsed) {
801
822
  this.protocolError(
@@ -811,7 +832,8 @@ var ServerTransport = class extends Transport {
811
832
  reason
812
833
  });
813
834
  conn.send(this.codec.toBuffer(responseMsg2));
814
- log?.warn(`${reason}: ${JSON.stringify(parsed)}`, {
835
+ const logData = typeof parsed.payload === "object" ? { ...parsed, payload: { ...parsed.payload, metadata: "redacted" } } : { ...parsed };
836
+ log?.warn(`${reason}: ${JSON.stringify(logData)}`, {
815
837
  clientId: this.clientId,
816
838
  connId: conn.debugId
817
839
  });
@@ -836,7 +858,73 @@ var ServerTransport = class extends Transport {
836
858
  this.protocolError(ProtocolError.HandshakeFailed, reason);
837
859
  return false;
838
860
  }
839
- const session = this.getOrCreateSession(parsed.from, conn);
861
+ const { session, isReconnect } = this.getOrCreateSession(
862
+ parsed.from,
863
+ conn,
864
+ parsed.payload.sessionId
865
+ );
866
+ let handshakeMetadata;
867
+ if (this.options.handshake) {
868
+ if (!import_value.Value.Check(
869
+ this.options.handshake.requestSchema,
870
+ parsed.payload.metadata
871
+ )) {
872
+ const reason = "received malformed handshake metadata";
873
+ const responseMsg2 = handshakeResponseMessage(
874
+ this.clientId,
875
+ parsed.from,
876
+ { ok: false, reason }
877
+ );
878
+ conn.send(this.codec.toBuffer(responseMsg2));
879
+ log?.warn(`received malformed handshake metadata from ${parsed.from}`, {
880
+ clientId: this.clientId,
881
+ connId: conn.debugId
882
+ });
883
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
884
+ this.deleteSession(session);
885
+ return false;
886
+ }
887
+ const parsedMetadata = await this.options.handshake.parse(
888
+ parsed.payload.metadata,
889
+ session,
890
+ isReconnect
891
+ );
892
+ if (parsedMetadata === false) {
893
+ const reason = "rejected by server";
894
+ const responseMsg2 = handshakeResponseMessage(
895
+ this.clientId,
896
+ parsed.from,
897
+ { ok: false, reason }
898
+ );
899
+ conn.send(this.codec.toBuffer(responseMsg2));
900
+ log?.warn(`rejected handshake from ${parsed.from}`, {
901
+ clientId: this.clientId,
902
+ connId: conn.debugId
903
+ });
904
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
905
+ this.deleteSession(session);
906
+ return false;
907
+ }
908
+ if (!import_value.Value.Check(this.options.handshake.parsedSchema, parsedMetadata)) {
909
+ const reason = "failed to parse handshake metadata";
910
+ const responseMsg2 = handshakeResponseMessage(
911
+ this.clientId,
912
+ parsed.from,
913
+ { ok: false, reason }
914
+ );
915
+ conn.send(this.codec.toBuffer(responseMsg2));
916
+ log?.error(`failed to parse handshake metadata`, {
917
+ clientId: this.clientId,
918
+ connId: conn.debugId
919
+ });
920
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
921
+ this.deleteSession(session);
922
+ return false;
923
+ }
924
+ handshakeMetadata = parsedMetadata;
925
+ }
926
+ handshakeMetadata ??= {};
927
+ session.metadata = handshakeMetadata;
840
928
  log?.debug(
841
929
  `handshake from ${parsed.from} ok, responding with handshake success`,
842
930
  { clientId: this.clientId, connId: conn.debugId }
@@ -846,7 +934,8 @@ var ServerTransport = class extends Transport {
846
934
  sessionId: session.id
847
935
  });
848
936
  conn.send(this.codec.toBuffer(responseMsg));
849
- return this.onConnect(conn, parsed.from, parsed.payload.sessionId);
937
+ this.onConnect(conn, parsed.from, session, isReconnect);
938
+ return session;
850
939
  }
851
940
  };
852
941
 
@@ -1,14 +1,14 @@
1
1
  import { Server, Socket } from 'node:net';
2
- import { c as ServerTransport, d as ProvidedTransportOptions } from '../../../index-d412ca83.js';
3
- import { T as TransportClientId } from '../../../index-46ed19d8.js';
4
- import { U as UdsConnection } from '../../../connection-89918b74.js';
2
+ import { ServerTransport, ServerTransportOptions as ProvidedServerTransportOptions } from '../../index.cjs';
3
+ import { T as TransportClientId } from '../../../index-2941de8c.js';
4
+ import { U as UdsConnection } from '../../../connection-ddc757b9.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
  import 'node:stream';
8
8
 
9
9
  declare class UnixDomainSocketServerTransport extends ServerTransport<UdsConnection> {
10
10
  server: Server;
11
- constructor(server: Server, clientId: TransportClientId, providedOptions?: Partial<ProvidedTransportOptions>);
11
+ constructor(server: Server, clientId: TransportClientId, providedOptions?: Partial<ProvidedServerTransportOptions>);
12
12
  connectionHandler: (sock: Socket) => void;
13
13
  close(): void;
14
14
  }
@@ -1,14 +1,14 @@
1
1
  import { Server, Socket } from 'node:net';
2
- import { c as ServerTransport, d as ProvidedTransportOptions } from '../../../index-d412ca83.js';
3
- import { T as TransportClientId } from '../../../index-46ed19d8.js';
4
- import { U as UdsConnection } from '../../../connection-89918b74.js';
2
+ import { ServerTransport, ServerTransportOptions as ProvidedServerTransportOptions } from '../../index.js';
3
+ import { T as TransportClientId } from '../../../index-2941de8c.js';
4
+ import { U as UdsConnection } from '../../../connection-ddc757b9.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
  import 'node:stream';
8
8
 
9
9
  declare class UnixDomainSocketServerTransport extends ServerTransport<UdsConnection> {
10
10
  server: Server;
11
- constructor(server: Server, clientId: TransportClientId, providedOptions?: Partial<ProvidedTransportOptions>);
11
+ constructor(server: Server, clientId: TransportClientId, providedOptions?: Partial<ProvidedServerTransportOptions>);
12
12
  connectionHandler: (sock: Socket) => void;
13
13
  close(): void;
14
14
  }
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  UdsConnection
3
- } from "../../../chunk-PUX3U2SZ.js";
3
+ } from "../../../chunk-ZNXZ7QPT.js";
4
4
  import {
5
5
  ServerTransport
6
- } from "../../../chunk-UABIFWM7.js";
7
- import "../../../chunk-VH3NGOXQ.js";
6
+ } from "../../../chunk-FILQOCCK.js";
7
+ import "../../../chunk-D5PVGZPQ.js";
8
8
  import "../../../chunk-WER2DWCP.js";
9
9
  import "../../../chunk-GZ7HCLLM.js";
10
10