@replit/river 0.16.2 → 0.17.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 (38) hide show
  1. package/dist/{chunk-HZWCU4ZI.js → chunk-4A7FDC2C.js} +1 -1
  2. package/dist/{chunk-SKH3JYHN.js → chunk-7IQO434V.js} +1 -1
  3. package/dist/{chunk-CJGD7CNG.js → chunk-F6KWMEPR.js} +87 -88
  4. package/dist/{chunk-HKVP6XEL.js → chunk-MSAS5CVJ.js} +1 -1
  5. package/dist/{chunk-GFRAOY75.js → chunk-VH3NGOXQ.js} +8 -17
  6. package/dist/{connection-2db95d74.d.ts → connection-0767dc6b.d.ts} +1 -1
  7. package/dist/{connection-c97da681.d.ts → connection-f31edbcd.d.ts} +1 -1
  8. package/dist/{index-c9e9977f.d.ts → index-8df0bdfb.d.ts} +14 -21
  9. package/dist/{procedures-d295c1a5.d.ts → procedures-b5ddb54d.d.ts} +1 -1
  10. package/dist/router/index.cjs +2 -2
  11. package/dist/router/index.d.cts +3 -3
  12. package/dist/router/index.d.ts +3 -3
  13. package/dist/router/index.js +2 -2
  14. package/dist/transport/impls/uds/client.cjs +66 -60
  15. package/dist/transport/impls/uds/client.d.cts +2 -2
  16. package/dist/transport/impls/uds/client.d.ts +2 -2
  17. package/dist/transport/impls/uds/client.js +3 -3
  18. package/dist/transport/impls/uds/server.cjs +78 -91
  19. package/dist/transport/impls/uds/server.d.cts +2 -2
  20. package/dist/transport/impls/uds/server.d.ts +2 -2
  21. package/dist/transport/impls/uds/server.js +3 -3
  22. package/dist/transport/impls/ws/client.cjs +66 -60
  23. package/dist/transport/impls/ws/client.d.cts +2 -2
  24. package/dist/transport/impls/ws/client.d.ts +2 -2
  25. package/dist/transport/impls/ws/client.js +3 -3
  26. package/dist/transport/impls/ws/server.cjs +78 -91
  27. package/dist/transport/impls/ws/server.d.cts +2 -2
  28. package/dist/transport/impls/ws/server.d.ts +2 -2
  29. package/dist/transport/impls/ws/server.js +3 -3
  30. package/dist/transport/index.cjs +93 -106
  31. package/dist/transport/index.d.cts +1 -1
  32. package/dist/transport/index.d.ts +1 -1
  33. package/dist/transport/index.js +2 -2
  34. package/dist/util/testHelpers.cjs +46 -19
  35. package/dist/util/testHelpers.d.cts +4 -9
  36. package/dist/util/testHelpers.d.ts +4 -9
  37. package/dist/util/testHelpers.js +8 -14
  38. package/package.json +1 -1
@@ -52,18 +52,18 @@ var ControlMessageAckSchema = import_typebox.Type.Object({
52
52
  var ControlMessageCloseSchema = import_typebox.Type.Object({
53
53
  type: import_typebox.Type.Literal("CLOSE")
54
54
  });
55
- var PROTOCOL_VERSION = "v1";
55
+ 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
- instanceId: import_typebox.Type.String()
59
+ sessionId: import_typebox.Type.String()
60
60
  });
61
61
  var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
62
62
  type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
63
63
  status: import_typebox.Type.Union([
64
64
  import_typebox.Type.Object({
65
65
  ok: import_typebox.Type.Literal(true),
66
- instanceId: import_typebox.Type.String()
66
+ sessionId: import_typebox.Type.String()
67
67
  }),
68
68
  import_typebox.Type.Object({
69
69
  ok: import_typebox.Type.Literal(false),
@@ -80,7 +80,7 @@ var ControlMessagePayloadSchema = import_typebox.Type.Union([
80
80
  var OpaqueTransportMessageSchema = TransportMessageSchema(
81
81
  import_typebox.Type.Unknown()
82
82
  );
83
- function handshakeRequestMessage(from, to, instanceId) {
83
+ function handshakeRequestMessage(from, to, sessionId) {
84
84
  return {
85
85
  id: (0, import_nanoid.nanoid)(),
86
86
  from,
@@ -92,7 +92,7 @@ function handshakeRequestMessage(from, to, instanceId) {
92
92
  payload: {
93
93
  type: "HANDSHAKE_REQ",
94
94
  protocolVersion: PROTOCOL_VERSION,
95
- instanceId
95
+ sessionId
96
96
  }
97
97
  };
98
98
  }
@@ -125,7 +125,12 @@ var Session = class {
125
125
  /**
126
126
  * The unique ID of this session.
127
127
  */
128
- debugId;
128
+ id;
129
+ /**
130
+ * What the other side advertised as their session ID
131
+ * for this session.
132
+ */
133
+ advertisedSessionId;
129
134
  /**
130
135
  * Number of messages we've sent along this session (excluding handshake and acks)
131
136
  */
@@ -147,11 +152,11 @@ var Session = class {
147
152
  * The interval for sending heartbeats.
148
153
  */
149
154
  heartbeat;
150
- constructor(from, connectedTo, conn, options) {
155
+ constructor(conn, from, to, options) {
156
+ this.id = `session-${nanoid2(12)}`;
151
157
  this.options = options;
152
- this.debugId = `sess-${unsafeId()}`;
153
158
  this.from = from;
154
- this.to = connectedTo;
159
+ this.to = to;
155
160
  this.connection = conn;
156
161
  this.codec = options.codec;
157
162
  this.heartbeatMisses = 0;
@@ -191,7 +196,7 @@ var Session = class {
191
196
  log?.info(
192
197
  `${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
193
198
  );
194
- this.closeStaleConnection(this.connection);
199
+ this.closeStaleConnection();
195
200
  }
196
201
  return;
197
202
  }
@@ -222,33 +227,37 @@ var Session = class {
222
227
  log?.debug(`${this.from} -- resending ${msg.id} (seq: ${msg.seq})`);
223
228
  const ok = this.connection.send(this.codec.toBuffer(msg));
224
229
  if (!ok) {
225
- const msg2 = `${this.from} -- failed to send buffered message to ${this.to} in session (id: ${this.debugId}) (if you hit this code path something is seriously wrong)`;
230
+ const msg2 = `${this.from} -- failed to send buffered message to ${this.to} in session (id: ${this.id}) (if you hit this code path something is seriously wrong)`;
226
231
  log?.error(msg2);
227
232
  throw new Error(msg2);
228
233
  }
229
234
  }
230
235
  }
231
236
  updateBookkeeping(ack, seq) {
232
- this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq > ack);
237
+ if (seq + 1 < this.ack) {
238
+ log?.error(`${this.from} -- received stale seq ${seq} + 1 < ${this.ack}`);
239
+ return;
240
+ }
241
+ this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
233
242
  this.ack = seq + 1;
234
243
  }
235
244
  closeStaleConnection(conn) {
236
- if (!this.connection || this.connection !== conn)
245
+ if (this.connection === void 0 || this.connection === conn)
237
246
  return;
238
247
  log?.info(
239
- `${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.debugId}) to ${this.to}`
248
+ `${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.id}) to ${this.to}`
240
249
  );
241
250
  this.connection.close();
242
251
  this.connection = void 0;
243
252
  }
244
253
  replaceWithNewConnection(newConn) {
245
- this.closeStaleConnection(this.connection);
254
+ this.closeStaleConnection(newConn);
246
255
  this.cancelGrace();
247
256
  this.connection = newConn;
248
257
  }
249
258
  beginGrace(cb) {
250
259
  log?.info(
251
- `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.debugId}) to ${this.to} is closed`
260
+ `${this.from} -- starting ${this.options.sessionDisconnectGraceMs}ms grace period until session (id: ${this.id}) to ${this.to} is closed`
252
261
  );
253
262
  this.disconnectionGrace = setTimeout(() => {
254
263
  this.close();
@@ -259,11 +268,12 @@ var Session = class {
259
268
  cancelGrace() {
260
269
  this.heartbeatMisses = 0;
261
270
  clearTimeout(this.disconnectionGrace);
271
+ this.disconnectionGrace = void 0;
262
272
  }
263
273
  // closed when we want to discard the whole session
264
274
  // (i.e. shutdown or session disconnect)
265
275
  close() {
266
- this.closeStaleConnection(this.connection);
276
+ this.closeStaleConnection();
267
277
  this.cancelGrace();
268
278
  this.resetBufferedMessages();
269
279
  clearInterval(this.heartbeat);
@@ -422,9 +432,6 @@ var EventDispatcher = class {
422
432
  }
423
433
  };
424
434
 
425
- // transport/transport.ts
426
- var import_nanoid3 = require("nanoid");
427
-
428
435
  // util/stringify.ts
429
436
  function coerceErrorString(err) {
430
437
  if (err instanceof Error) {
@@ -575,13 +582,6 @@ var defaultClientTransportOptions = {
575
582
  ...defaultConnectionRetryOptions
576
583
  };
577
584
  var Transport = class {
578
- /**
579
- * Unique per instance of the transport.
580
- * This allows us to distinguish reconnects to different
581
- * transports.
582
- */
583
- instanceId = (0, import_nanoid3.nanoid)();
584
- connectedInstanceIds = /* @__PURE__ */ new Map();
585
585
  /**
586
586
  * A flag indicating whether the transport has been destroyed.
587
587
  * A destroyed transport will not attempt to reconnect and cannot be used again.
@@ -634,41 +634,41 @@ var Transport = class {
634
634
  * and we know the identity of the connected client.
635
635
  * @param conn The connection object.
636
636
  */
637
- onConnect(conn, connectedTo, instanceId) {
637
+ onConnect(conn, connectedTo, advertisedSessionId) {
638
638
  this.eventDispatcher.dispatchEvent("connectionStatus", {
639
639
  status: "connect",
640
640
  conn
641
641
  });
642
642
  let oldSession = this.sessions.get(connectedTo);
643
- const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
644
- if (oldSession && lastInstanceId !== void 0 && lastInstanceId !== instanceId) {
643
+ if (oldSession?.advertisedSessionId && oldSession.advertisedSessionId !== advertisedSessionId) {
645
644
  log?.warn(
646
- `${this.clientId} -- connection from ${connectedTo} is a different instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
645
+ `${this.clientId} -- connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), starting a new session`
647
646
  );
648
647
  oldSession.close();
649
648
  this.deleteSession(oldSession);
650
649
  oldSession = void 0;
651
650
  }
652
- this.connectedInstanceIds.set(connectedTo, instanceId);
653
651
  if (oldSession === void 0) {
654
652
  const newSession = this.createSession(connectedTo, conn);
653
+ newSession.advertisedSessionId = advertisedSessionId;
655
654
  log?.info(
656
- `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
655
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.id}) to ${connectedTo}`
657
656
  );
658
657
  return newSession;
659
658
  }
660
659
  log?.info(
661
- `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.debugId}) to ${connectedTo}`
660
+ `${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${oldSession.id}) to ${connectedTo}`
662
661
  );
663
662
  oldSession.replaceWithNewConnection(conn);
664
663
  oldSession.sendBufferedMessages();
664
+ oldSession.advertisedSessionId = advertisedSessionId;
665
665
  return oldSession;
666
666
  }
667
- createSession(connectedTo, conn) {
667
+ createSession(to, conn) {
668
668
  const session = new Session(
669
- this.clientId,
670
- connectedTo,
671
669
  conn,
670
+ this.clientId,
671
+ to,
672
672
  this.options
673
673
  );
674
674
  this.sessions.set(session.to, session);
@@ -678,10 +678,20 @@ var Transport = class {
678
678
  });
679
679
  return session;
680
680
  }
681
+ getOrCreateSession(to, conn) {
682
+ let session = this.sessions.get(to);
683
+ if (!session) {
684
+ session = this.createSession(to, conn);
685
+ log?.info(
686
+ `${this.clientId} -- no session for ${to}, created a new one (id: ${session.id})`
687
+ );
688
+ }
689
+ return session;
690
+ }
681
691
  deleteSession(session) {
682
692
  this.sessions.delete(session.to);
683
693
  log?.info(
684
- `${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
694
+ `${this.clientId} -- session ${session.id} disconnect from ${session.to}`
685
695
  );
686
696
  this.eventDispatcher.dispatchEvent("sessionStatus", {
687
697
  status: "disconnect",
@@ -751,9 +761,12 @@ var Transport = class {
751
761
  } else {
752
762
  const errMsg = `received out-of-order msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq})`;
753
763
  log?.error(
754
- `${this.clientId} -- ${errMsg}, marking connection as dead: ${JSON.stringify(msg)}`
764
+ `${this.clientId} -- fatal: ${errMsg}, marking connection as dead: ${JSON.stringify(
765
+ msg
766
+ )}`
755
767
  );
756
768
  this.protocolError(ProtocolError.MessageOrderingViolated, errMsg);
769
+ session.close();
757
770
  }
758
771
  return;
759
772
  }
@@ -800,14 +813,7 @@ var Transport = class {
800
813
  );
801
814
  return void 0;
802
815
  }
803
- let session = this.sessions.get(to);
804
- if (!session) {
805
- session = this.createSession(to, void 0);
806
- log?.info(
807
- `${this.clientId} -- no session for ${to}, created a new one (id: ${session.debugId})`
808
- );
809
- }
810
- return session.send(msg);
816
+ return this.getOrCreateSession(to).send(msg);
811
817
  }
812
818
  // control helpers
813
819
  sendCloseStream(to, streamId) {
@@ -880,12 +886,13 @@ var ClientTransport = class extends Transport {
880
886
  return;
881
887
  let session = void 0;
882
888
  const handshakeHandler = (data) => {
883
- const handshake = this.receiveHandshakeResponseMessage(data);
884
- if (!handshake) {
889
+ const maybeSession = this.receiveHandshakeResponseMessage(data, conn);
890
+ if (!maybeSession) {
885
891
  conn.close();
886
892
  return;
893
+ } else {
894
+ session = maybeSession;
887
895
  }
888
- session = this.onConnect(conn, handshake.from, handshake.instanceId);
889
896
  conn.removeDataListener(handshakeHandler);
890
897
  conn.addDataListener((data2) => {
891
898
  const parsed = this.parseMsg(data2);
@@ -915,7 +922,7 @@ var ClientTransport = class extends Transport {
915
922
  );
916
923
  });
917
924
  }
918
- receiveHandshakeResponseMessage(data) {
925
+ receiveHandshakeResponseMessage(data, conn) {
919
926
  const parsed = this.parseMsg(data);
920
927
  if (!parsed) {
921
928
  this.protocolError(
@@ -948,12 +955,14 @@ var ClientTransport = class extends Transport {
948
955
  );
949
956
  return false;
950
957
  }
951
- const instanceId = parsed.payload.status.instanceId;
952
- log?.debug(
953
- `${this.clientId} -- handshake from ${parsed.from} ok (instance: ${instanceId})`
958
+ log?.debug(`${this.clientId} -- handshake from ${parsed.from} ok`);
959
+ const session = this.onConnect(
960
+ conn,
961
+ parsed.from,
962
+ parsed.payload.status.sessionId
954
963
  );
955
964
  this.retryBudget.startRestoringBudget(parsed.from);
956
- return { instanceId, from: parsed.from };
965
+ return session;
957
966
  }
958
967
  /**
959
968
  * Manually attempts to connect to a client.
@@ -1022,11 +1031,8 @@ var ClientTransport = class extends Transport {
1022
1031
  }
1023
1032
  }
1024
1033
  sendHandshake(to, conn) {
1025
- const requestMsg = handshakeRequestMessage(
1026
- this.clientId,
1027
- to,
1028
- this.instanceId
1029
- );
1034
+ const session = this.getOrCreateSession(to, conn);
1035
+ const requestMsg = handshakeRequestMessage(this.clientId, to, session.id);
1030
1036
  log?.debug(`${this.clientId} -- sending handshake request to ${to}`);
1031
1037
  conn.send(this.codec.toBuffer(requestMsg));
1032
1038
  }
@@ -1,5 +1,5 @@
1
- import { a as ClientTransport, c as ProvidedClientTransportOptions, b as TransportClientId } from '../../../index-c9e9977f.js';
2
- import { U as UdsConnection } from '../../../connection-2db95d74.js';
1
+ import { a as ClientTransport, c as ProvidedClientTransportOptions, b as TransportClientId } from '../../../index-8df0bdfb.js';
2
+ import { U as UdsConnection } from '../../../connection-0767dc6b.js';
3
3
  import '../../../types-3e5768ec.js';
4
4
  import '@sinclair/typebox';
5
5
  import 'node:net';
@@ -1,5 +1,5 @@
1
- import { a as ClientTransport, c as ProvidedClientTransportOptions, b as TransportClientId } from '../../../index-c9e9977f.js';
2
- import { U as UdsConnection } from '../../../connection-2db95d74.js';
1
+ import { a as ClientTransport, c as ProvidedClientTransportOptions, b as TransportClientId } from '../../../index-8df0bdfb.js';
2
+ import { U as UdsConnection } from '../../../connection-0767dc6b.js';
3
3
  import '../../../types-3e5768ec.js';
4
4
  import '@sinclair/typebox';
5
5
  import 'node:net';
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  UdsConnection
3
- } from "../../../chunk-HZWCU4ZI.js";
3
+ } from "../../../chunk-4A7FDC2C.js";
4
4
  import {
5
5
  ClientTransport
6
- } from "../../../chunk-CJGD7CNG.js";
7
- import "../../../chunk-GFRAOY75.js";
6
+ } from "../../../chunk-F6KWMEPR.js";
7
+ import "../../../chunk-VH3NGOXQ.js";
8
8
  import {
9
9
  log
10
10
  } from "../../../chunk-H4BYJELI.js";