@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
@@ -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"),
@@ -76,7 +77,7 @@ var ControlMessagePayloadSchema = import_typebox.Type.Union([
76
77
  var OpaqueTransportMessageSchema = TransportMessageSchema(
77
78
  import_typebox.Type.Unknown()
78
79
  );
79
- function handshakeRequestMessage(from, to, sessionId) {
80
+ function handshakeRequestMessage(from, to, sessionId, metadata) {
80
81
  return {
81
82
  id: (0, import_nanoid.nanoid)(),
82
83
  from,
@@ -88,7 +89,8 @@ function handshakeRequestMessage(from, to, sessionId) {
88
89
  payload: {
89
90
  type: "HANDSHAKE_REQ",
90
91
  protocolVersion: PROTOCOL_VERSION,
91
- sessionId
92
+ sessionId,
93
+ metadata
92
94
  }
93
95
  };
94
96
  }
@@ -165,6 +167,12 @@ var Session = class {
165
167
  * for this session.
166
168
  */
167
169
  advertisedSessionId;
170
+ /**
171
+ * The metadata for this session, as parsed from the handshake.
172
+ *
173
+ * Will only ever be populated on the server side.
174
+ */
175
+ metadata;
168
176
  /**
169
177
  * Number of messages we've sent along this session (excluding handshake and acks)
170
178
  */
@@ -267,27 +275,24 @@ var Session = class {
267
275
  this.seq = 0;
268
276
  this.ack = 0;
269
277
  }
270
- sendBufferedMessages() {
271
- if (!this.connection) {
272
- const msg = `tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
273
- log?.error(msg, this.loggingMetadata);
274
- throw new Error(msg);
275
- }
276
- log?.info(
277
- `resending ${this.sendBuffer.length} buffered messages`,
278
- this.loggingMetadata
279
- );
278
+ sendBufferedMessages(conn) {
279
+ log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
280
+ ...this.loggingMetadata,
281
+ connId: conn.debugId
282
+ });
280
283
  for (const msg of this.sendBuffer) {
281
284
  log?.debug(`resending msg`, {
282
285
  ...this.loggingMetadata,
283
- fullTransportMessage: msg
286
+ fullTransportMessage: msg,
287
+ connId: conn.debugId
284
288
  });
285
- const ok = this.connection.send(this.codec.toBuffer(msg));
289
+ const ok = conn.send(this.codec.toBuffer(msg));
286
290
  if (!ok) {
287
- const errMsg = `failed to send buffered message to ${this.to} (if you hit this code path something is seriously wrong)`;
291
+ const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;
288
292
  log?.error(errMsg, {
289
293
  ...this.loggingMetadata,
290
- fullTransportMessage: msg
294
+ fullTransportMessage: msg,
295
+ connId: conn.debugId
291
296
  });
292
297
  throw new Error(errMsg);
293
298
  }
@@ -318,6 +323,7 @@ var Session = class {
318
323
  this.closeStaleConnection(newConn);
319
324
  this.cancelGrace();
320
325
  this.connection = newConn;
326
+ this.sendBufferedMessages(newConn);
321
327
  }
322
328
  beginGrace(cb) {
323
329
  log?.info(
@@ -516,6 +522,9 @@ var defaultClientTransportOptions = {
516
522
  ...defaultTransportOptions,
517
523
  ...defaultConnectionRetryOptions
518
524
  };
525
+ var defaultServerTransportOptions = {
526
+ ...defaultTransportOptions
527
+ };
519
528
  var Transport = class {
520
529
  /**
521
530
  * A flag indicating whether the transport has been destroyed.
@@ -569,37 +578,15 @@ var Transport = class {
569
578
  * and we know the identity of the connected client.
570
579
  * @param conn The connection object.
571
580
  */
572
- onConnect(conn, connectedTo, advertisedSessionId) {
581
+ onConnect(conn, connectedTo, session, isReconnect) {
573
582
  this.eventDispatcher.dispatchEvent("connectionStatus", {
574
583
  status: "connect",
575
584
  conn
576
585
  });
577
- let oldSession = this.sessions.get(connectedTo);
578
- if (oldSession?.advertisedSessionId && oldSession.advertisedSessionId !== advertisedSessionId) {
579
- log?.warn(
580
- `connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), killing old session and starting a new one`,
581
- oldSession.loggingMetadata
582
- );
583
- this.deleteSession(oldSession);
584
- oldSession = void 0;
585
- }
586
- if (oldSession === void 0) {
587
- const newSession = this.createSession(connectedTo, conn);
588
- newSession.advertisedSessionId = advertisedSessionId;
589
- log?.info(
590
- `new connection for new session to ${connectedTo}`,
591
- newSession.loggingMetadata
592
- );
593
- return newSession;
586
+ if (isReconnect) {
587
+ session.replaceWithNewConnection(conn);
588
+ log?.info(`reconnected to ${connectedTo}`, session.loggingMetadata);
594
589
  }
595
- oldSession.replaceWithNewConnection(conn);
596
- oldSession.sendBufferedMessages();
597
- oldSession.advertisedSessionId = advertisedSessionId;
598
- log?.info(
599
- `new connection for existing session to ${connectedTo}`,
600
- oldSession.loggingMetadata
601
- );
602
- return oldSession;
603
590
  }
604
591
  createSession(to, conn) {
605
592
  const session = new Session(
@@ -615,8 +602,18 @@ var Transport = class {
615
602
  });
616
603
  return session;
617
604
  }
618
- getOrCreateSession(to, conn) {
605
+ getOrCreateSession(to, conn, sessionId) {
619
606
  let session = this.sessions.get(to);
607
+ let isReconnect = session !== void 0;
608
+ if (session?.advertisedSessionId !== void 0 && sessionId !== void 0 && session.advertisedSessionId !== sessionId) {
609
+ log?.warn(
610
+ `session for ${to} already exists but has a different session id (expected: ${session.advertisedSessionId}, got: ${sessionId}), creating a new one`,
611
+ session.loggingMetadata
612
+ );
613
+ this.deleteSession(session);
614
+ isReconnect = false;
615
+ session = void 0;
616
+ }
620
617
  if (!session) {
621
618
  session = this.createSession(to, conn);
622
619
  log?.info(
@@ -624,7 +621,10 @@ var Transport = class {
624
621
  session.loggingMetadata
625
622
  );
626
623
  }
627
- return session;
624
+ if (sessionId !== void 0) {
625
+ session.advertisedSessionId = sessionId;
626
+ }
627
+ return { session, isReconnect };
628
628
  }
629
629
  deleteSession(session) {
630
630
  session.close();
@@ -759,7 +759,7 @@ var Transport = class {
759
759
  });
760
760
  return void 0;
761
761
  }
762
- return this.getOrCreateSession(to).send(msg);
762
+ return this.getOrCreateSession(to).session.send(msg);
763
763
  }
764
764
  // control helpers
765
765
  sendCloseStream(to, streamId) {
@@ -918,11 +918,12 @@ var ClientTransport = class extends Transport {
918
918
  connectedTo: parsed.from,
919
919
  fullTransportMessage: parsed
920
920
  });
921
- const session = this.onConnect(
922
- conn,
921
+ const { session, isReconnect } = this.getOrCreateSession(
923
922
  parsed.from,
923
+ conn,
924
924
  parsed.payload.status.sessionId
925
925
  );
926
+ this.onConnect(conn, parsed.from, session, isReconnect);
926
927
  this.retryBudget.startRestoringBudget(parsed.from);
927
928
  return session;
928
929
  }
@@ -975,8 +976,13 @@ var ClientTransport = class extends Transport {
975
976
  conn.close();
976
977
  throw new Error("transport state is no longer open");
977
978
  }
978
- this.sendHandshake(to, conn);
979
- return conn;
979
+ return this.sendHandshake(to, conn).then((ok) => {
980
+ if (!ok) {
981
+ conn.close();
982
+ throw new Error("failed to send handshake");
983
+ }
984
+ return conn;
985
+ });
980
986
  });
981
987
  this.inflightConnectionPromises.set(to, reconnectPromise);
982
988
  } else {
@@ -1008,14 +1014,35 @@ var ClientTransport = class extends Transport {
1008
1014
  this.inflightConnectionPromises.delete(session.to);
1009
1015
  super.deleteSession(session);
1010
1016
  }
1011
- sendHandshake(to, conn) {
1012
- const session = this.getOrCreateSession(to, conn);
1013
- const requestMsg = handshakeRequestMessage(this.clientId, to, session.id);
1017
+ async sendHandshake(to, conn) {
1018
+ let metadata;
1019
+ if (this.options.handshake) {
1020
+ metadata = await this.options.handshake.get();
1021
+ if (!import_value.Value.Check(this.options.handshake.schema, metadata)) {
1022
+ log?.error(`handshake metadata did not match schema`, {
1023
+ clientId: this.clientId,
1024
+ connectedTo: to
1025
+ });
1026
+ this.protocolError(
1027
+ ProtocolError.HandshakeFailed,
1028
+ "handshake metadata did not match schema"
1029
+ );
1030
+ return false;
1031
+ }
1032
+ }
1033
+ const { session } = this.getOrCreateSession(to, conn);
1034
+ const requestMsg = handshakeRequestMessage(
1035
+ this.clientId,
1036
+ to,
1037
+ session.id,
1038
+ metadata
1039
+ );
1014
1040
  log?.debug(`sending handshake request to ${to}`, {
1015
1041
  clientId: this.clientId,
1016
1042
  connectedTo: to
1017
1043
  });
1018
1044
  conn.send(this.codec.toBuffer(requestMsg));
1045
+ return true;
1019
1046
  }
1020
1047
  close() {
1021
1048
  this.retryBudget.close();
@@ -1,7 +1,7 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { b as ClientTransport, P as ProvidedClientTransportOptions } from '../../../index-d412ca83.js';
3
- import { T as TransportClientId } from '../../../index-46ed19d8.js';
4
- import { W as WebSocketConnection } from '../../../connection-893bd769.js';
2
+ import { ClientTransport, ClientTransportOptions as ProvidedClientTransportOptions } from '../../index.cjs';
3
+ import { T as TransportClientId } from '../../../index-2941de8c.js';
4
+ import { W as WebSocketConnection } from '../../../connection-d625aa8d.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
 
@@ -1,7 +1,7 @@
1
1
  import WebSocket from 'isomorphic-ws';
2
- import { b as ClientTransport, P as ProvidedClientTransportOptions } from '../../../index-d412ca83.js';
3
- import { T as TransportClientId } from '../../../index-46ed19d8.js';
4
- import { W as WebSocketConnection } from '../../../connection-893bd769.js';
2
+ import { ClientTransport, ClientTransportOptions as ProvidedClientTransportOptions } from '../../index.js';
3
+ import { T as TransportClientId } from '../../../index-2941de8c.js';
4
+ import { W as WebSocketConnection } from '../../../connection-d625aa8d.js';
5
5
  import '../../../types-3e5768ec.js';
6
6
  import '@sinclair/typebox';
7
7
 
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "../../../chunk-K7CUSLWL.js";
3
+ } from "../../../chunk-4PFUC4QB.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
- import { T as TransportClientId } from '../../../index-46ed19d8.js';
2
- import { c as ServerTransport, d as ProvidedTransportOptions } from '../../../index-d412ca83.js';
1
+ import { T as TransportClientId } from '../../../index-2941de8c.js';
2
+ import { ServerTransport, ServerTransportOptions as ProvidedServerTransportOptions } from '../../index.cjs';
3
3
  import { WebSocketServer } from 'ws';
4
4
  import { WebSocket } from 'isomorphic-ws';
5
- import { W as WebSocketConnection } from '../../../connection-893bd769.js';
5
+ import { W as WebSocketConnection } from '../../../connection-d625aa8d.js';
6
6
  import '@sinclair/typebox';
7
7
  import '../../../types-3e5768ec.js';
8
8
 
9
9
  declare class WebSocketServerTransport extends ServerTransport<WebSocketConnection> {
10
10
  wss: WebSocketServer;
11
- constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?: ProvidedTransportOptions);
11
+ constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?: ProvidedServerTransportOptions);
12
12
  connectionHandler: (ws: WebSocket) => void;
13
13
  close(): void;
14
14
  }
@@ -1,14 +1,14 @@
1
- import { T as TransportClientId } from '../../../index-46ed19d8.js';
2
- import { c as ServerTransport, d as ProvidedTransportOptions } from '../../../index-d412ca83.js';
1
+ import { T as TransportClientId } from '../../../index-2941de8c.js';
2
+ import { ServerTransport, ServerTransportOptions as ProvidedServerTransportOptions } from '../../index.js';
3
3
  import { WebSocketServer } from 'ws';
4
4
  import { WebSocket } from 'isomorphic-ws';
5
- import { W as WebSocketConnection } from '../../../connection-893bd769.js';
5
+ import { W as WebSocketConnection } from '../../../connection-d625aa8d.js';
6
6
  import '@sinclair/typebox';
7
7
  import '../../../types-3e5768ec.js';
8
8
 
9
9
  declare class WebSocketServerTransport extends ServerTransport<WebSocketConnection> {
10
10
  wss: WebSocketServer;
11
- constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?: ProvidedTransportOptions);
11
+ constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?: ProvidedServerTransportOptions);
12
12
  connectionHandler: (ws: WebSocket) => void;
13
13
  close(): void;
14
14
  }
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WebSocketConnection
3
- } from "../../../chunk-K7CUSLWL.js";
3
+ } from "../../../chunk-4PFUC4QB.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