@replit/river 0.12.0 → 0.12.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.
- package/dist/{chunk-24O3BKZA.js → chunk-55XUAPC6.js} +1 -1
- package/dist/{chunk-IUDKWAOK.js → chunk-M6LY25P2.js} +1 -1
- package/dist/{chunk-ANGOKBE5.js → chunk-QEYN2Z6O.js} +63 -45
- package/dist/{chunk-4HOR4NUO.js → chunk-RDTTKCGV.js} +1 -1
- package/dist/{chunk-XOTIAXAH.js → chunk-TKINU53F.js} +1 -1
- package/dist/{chunk-WTOIOXB7.js → chunk-XFFS4UOD.js} +5 -3
- package/dist/{connection-2956a1c5.d.ts → connection-bf7811aa.d.ts} +1 -1
- package/dist/{connection-cd963ed5.d.ts → connection-d880aa4a.d.ts} +1 -1
- package/dist/{connection-aaea7c88.d.ts → connection-eb10d250.d.ts} +1 -1
- package/dist/{index-d91775d9.d.ts → index-0c0a69f6.d.ts} +9 -11
- package/dist/router/index.cjs +2 -1
- package/dist/router/index.d.cts +1 -1
- package/dist/router/index.d.ts +1 -1
- package/dist/router/index.js +2 -2
- package/dist/transport/impls/stdio/client.cjs +53 -35
- package/dist/transport/impls/stdio/client.d.cts +2 -2
- package/dist/transport/impls/stdio/client.d.ts +2 -2
- package/dist/transport/impls/stdio/client.js +3 -3
- package/dist/transport/impls/stdio/server.cjs +51 -29
- package/dist/transport/impls/stdio/server.d.cts +2 -2
- package/dist/transport/impls/stdio/server.d.ts +2 -2
- package/dist/transport/impls/stdio/server.js +3 -3
- package/dist/transport/impls/uds/client.cjs +53 -35
- package/dist/transport/impls/uds/client.d.cts +2 -2
- package/dist/transport/impls/uds/client.d.ts +2 -2
- package/dist/transport/impls/uds/client.js +3 -3
- package/dist/transport/impls/uds/server.cjs +51 -29
- package/dist/transport/impls/uds/server.d.cts +2 -2
- package/dist/transport/impls/uds/server.d.ts +2 -2
- package/dist/transport/impls/uds/server.js +3 -3
- package/dist/transport/impls/ws/client.cjs +59 -35
- package/dist/transport/impls/ws/client.d.cts +2 -2
- package/dist/transport/impls/ws/client.d.ts +2 -2
- package/dist/transport/impls/ws/client.js +9 -3
- package/dist/transport/impls/ws/server.cjs +51 -29
- package/dist/transport/impls/ws/server.d.cts +2 -2
- package/dist/transport/impls/ws/server.d.ts +2 -2
- package/dist/transport/impls/ws/server.js +3 -3
- package/dist/transport/index.cjs +67 -47
- package/dist/transport/index.d.cts +1 -1
- package/dist/transport/index.d.ts +1 -1
- package/dist/transport/index.js +2 -2
- package/dist/util/testHelpers.d.cts +1 -1
- package/dist/util/testHelpers.d.ts +1 -1
- package/dist/util/testHelpers.js +2 -2
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Server, Socket } from 'node:net';
|
|
2
|
-
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
3
|
-
import { U as UdsConnection } from '../../../connection-
|
|
2
|
+
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
3
|
+
import { U as UdsConnection } from '../../../connection-d880aa4a.js';
|
|
4
4
|
import '../../../types-3e5768ec.js';
|
|
5
5
|
import '@sinclair/typebox';
|
|
6
6
|
import '../../../messageFraming-b200ef25.js';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
UdsConnection
|
|
3
|
-
} from "../../../chunk-
|
|
3
|
+
} from "../../../chunk-TKINU53F.js";
|
|
4
4
|
import "../../../chunk-IIBVKYDB.js";
|
|
5
5
|
import {
|
|
6
6
|
ServerTransport
|
|
7
|
-
} from "../../../chunk-
|
|
7
|
+
} from "../../../chunk-QEYN2Z6O.js";
|
|
8
8
|
import "../../../chunk-GZ7HCLLM.js";
|
|
9
|
-
import "../../../chunk-
|
|
9
|
+
import "../../../chunk-XFFS4UOD.js";
|
|
10
10
|
import {
|
|
11
11
|
log
|
|
12
12
|
} from "../../../chunk-H4BYJELI.js";
|
|
@@ -51,7 +51,8 @@ var ControlMessageCloseSchema = import_typebox.Type.Object({
|
|
|
51
51
|
var PROTOCOL_VERSION = "v1";
|
|
52
52
|
var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
|
|
53
53
|
type: import_typebox.Type.Literal("HANDSHAKE_REQ"),
|
|
54
|
-
protocolVersion: import_typebox.Type.Literal(PROTOCOL_VERSION)
|
|
54
|
+
protocolVersion: import_typebox.Type.Literal(PROTOCOL_VERSION),
|
|
55
|
+
instanceId: import_typebox.Type.String()
|
|
55
56
|
});
|
|
56
57
|
var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
|
|
57
58
|
type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
|
|
@@ -75,7 +76,7 @@ var ControlMessagePayloadSchema = import_typebox.Type.Union([
|
|
|
75
76
|
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
76
77
|
import_typebox.Type.Unknown()
|
|
77
78
|
);
|
|
78
|
-
function bootRequestMessage(from, to) {
|
|
79
|
+
function bootRequestMessage(from, to, instanceId) {
|
|
79
80
|
return {
|
|
80
81
|
id: (0, import_nanoid.nanoid)(),
|
|
81
82
|
from,
|
|
@@ -86,7 +87,8 @@ function bootRequestMessage(from, to) {
|
|
|
86
87
|
controlFlags: 0,
|
|
87
88
|
payload: {
|
|
88
89
|
type: "HANDSHAKE_REQ",
|
|
89
|
-
protocolVersion: PROTOCOL_VERSION
|
|
90
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
91
|
+
instanceId
|
|
90
92
|
}
|
|
91
93
|
};
|
|
92
94
|
}
|
|
@@ -135,9 +137,9 @@ var Connection = class {
|
|
|
135
137
|
this.debugId = `conn-${unsafeId()}`;
|
|
136
138
|
}
|
|
137
139
|
};
|
|
138
|
-
var HEARTBEAT_INTERVAL_MS =
|
|
139
|
-
var HEARTBEATS_TILL_DEAD =
|
|
140
|
-
var SESSION_DISCONNECT_GRACE_MS =
|
|
140
|
+
var HEARTBEAT_INTERVAL_MS = 1e3;
|
|
141
|
+
var HEARTBEATS_TILL_DEAD = 2;
|
|
142
|
+
var SESSION_DISCONNECT_GRACE_MS = 5e3;
|
|
141
143
|
var Session = class {
|
|
142
144
|
codec;
|
|
143
145
|
/**
|
|
@@ -220,11 +222,13 @@ var Session = class {
|
|
|
220
222
|
return fullMsg.id;
|
|
221
223
|
}
|
|
222
224
|
sendHeartbeat() {
|
|
223
|
-
if (this.heartbeatMisses >= HEARTBEATS_TILL_DEAD
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
225
|
+
if (this.heartbeatMisses >= HEARTBEATS_TILL_DEAD) {
|
|
226
|
+
if (this.connection) {
|
|
227
|
+
log?.info(
|
|
228
|
+
`${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
|
|
229
|
+
);
|
|
230
|
+
this.halfCloseConnection();
|
|
231
|
+
}
|
|
228
232
|
return;
|
|
229
233
|
}
|
|
230
234
|
this.send(
|
|
@@ -261,7 +265,6 @@ var Session = class {
|
|
|
261
265
|
}
|
|
262
266
|
}
|
|
263
267
|
updateBookkeeping(ack, seq) {
|
|
264
|
-
this.heartbeatMisses = 0;
|
|
265
268
|
this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq > ack);
|
|
266
269
|
this.ack = seq + 1;
|
|
267
270
|
}
|
|
@@ -293,6 +296,7 @@ var Session = class {
|
|
|
293
296
|
}, SESSION_DISCONNECT_GRACE_MS);
|
|
294
297
|
}
|
|
295
298
|
cancelGrace() {
|
|
299
|
+
this.heartbeatMisses = 0;
|
|
296
300
|
clearTimeout(this.disconnectionGrace);
|
|
297
301
|
}
|
|
298
302
|
get connected() {
|
|
@@ -400,6 +404,13 @@ var defaultTransportOptions = {
|
|
|
400
404
|
codec: NaiveJsonCodec
|
|
401
405
|
};
|
|
402
406
|
var Transport = class {
|
|
407
|
+
/**
|
|
408
|
+
* Unique per instance of the transport.
|
|
409
|
+
* This allows us to distinguish reconnects to different
|
|
410
|
+
* transports.
|
|
411
|
+
*/
|
|
412
|
+
instanceId = (0, import_nanoid3.nanoid)();
|
|
413
|
+
connectedInstanceIds = /* @__PURE__ */ new Map();
|
|
403
414
|
/**
|
|
404
415
|
* A flag indicating whether the transport has been destroyed.
|
|
405
416
|
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
@@ -461,12 +472,23 @@ var Transport = class {
|
|
|
461
472
|
* and we know the identity of the connected client.
|
|
462
473
|
* @param conn The connection object.
|
|
463
474
|
*/
|
|
464
|
-
onConnect(conn, connectedTo) {
|
|
475
|
+
onConnect(conn, connectedTo, instanceId) {
|
|
465
476
|
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
466
477
|
status: "connect",
|
|
467
478
|
conn
|
|
468
479
|
});
|
|
469
|
-
|
|
480
|
+
let session = this.sessions.get(connectedTo);
|
|
481
|
+
const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
|
|
482
|
+
if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
|
|
483
|
+
log?.debug(
|
|
484
|
+
`${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
|
|
485
|
+
);
|
|
486
|
+
session.resetBufferedMessages();
|
|
487
|
+
session.closeStaleConnection();
|
|
488
|
+
this.deleteSession(session);
|
|
489
|
+
session = void 0;
|
|
490
|
+
}
|
|
491
|
+
this.connectedInstanceIds.set(connectedTo, instanceId);
|
|
470
492
|
if (session === void 0) {
|
|
471
493
|
const newSession = this.createSession(connectedTo, conn);
|
|
472
494
|
log?.info(
|
|
@@ -532,11 +554,13 @@ var Transport = class {
|
|
|
532
554
|
const parsedMsg = this.codec.fromBuffer(msg);
|
|
533
555
|
if (parsedMsg === null) {
|
|
534
556
|
const decodedBuffer = new TextDecoder().decode(msg);
|
|
535
|
-
log?.
|
|
557
|
+
log?.error(
|
|
558
|
+
`${this.clientId} -- received malformed msg, killing conn: ${decodedBuffer}`
|
|
559
|
+
);
|
|
536
560
|
return null;
|
|
537
561
|
}
|
|
538
562
|
if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
539
|
-
log?.
|
|
563
|
+
log?.error(
|
|
540
564
|
`${this.clientId} -- received invalid msg: ${JSON.stringify(
|
|
541
565
|
parsedMsg
|
|
542
566
|
)}`
|
|
@@ -555,9 +579,6 @@ var Transport = class {
|
|
|
555
579
|
* @param msg The received message.
|
|
556
580
|
*/
|
|
557
581
|
handleMsg(msg) {
|
|
558
|
-
if (!msg) {
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
582
|
const session = this.sessionByClientId(msg.from);
|
|
562
583
|
session.cancelGrace();
|
|
563
584
|
log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
|
|
@@ -659,7 +680,6 @@ var ClientTransport = class extends Transport {
|
|
|
659
680
|
*/
|
|
660
681
|
inflightConnectionPromises;
|
|
661
682
|
tryReconnecting = true;
|
|
662
|
-
serverInstanceIds = /* @__PURE__ */ new Map();
|
|
663
683
|
constructor(clientId, providedOptions) {
|
|
664
684
|
super(clientId, providedOptions);
|
|
665
685
|
this.inflightConnectionPromises = /* @__PURE__ */ new Map();
|
|
@@ -667,7 +687,14 @@ var ClientTransport = class extends Transport {
|
|
|
667
687
|
handleConnection(conn, to) {
|
|
668
688
|
const bootHandler = this.receiveWithBootSequence(conn, () => {
|
|
669
689
|
conn.removeDataListener(bootHandler);
|
|
670
|
-
conn.addDataListener((data) =>
|
|
690
|
+
conn.addDataListener((data) => {
|
|
691
|
+
const parsed = this.parseMsg(data);
|
|
692
|
+
if (!parsed) {
|
|
693
|
+
conn.close();
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
this.handleMsg(parsed);
|
|
697
|
+
});
|
|
671
698
|
});
|
|
672
699
|
conn.addDataListener(bootHandler);
|
|
673
700
|
conn.addCloseListener(() => {
|
|
@@ -699,7 +726,7 @@ var ClientTransport = class extends Transport {
|
|
|
699
726
|
try {
|
|
700
727
|
const conn = await reconnectPromise;
|
|
701
728
|
this.state = "open";
|
|
702
|
-
const requestMsg = bootRequestMessage(this.clientId, to);
|
|
729
|
+
const requestMsg = bootRequestMessage(this.clientId, to, this.instanceId);
|
|
703
730
|
log?.debug(`${this.clientId} -- sending boot handshake to ${to}`);
|
|
704
731
|
conn.send(this.codec.toBuffer(requestMsg));
|
|
705
732
|
} catch (error) {
|
|
@@ -722,8 +749,10 @@ var ClientTransport = class extends Transport {
|
|
|
722
749
|
receiveWithBootSequence(conn, sessionCb) {
|
|
723
750
|
const bootHandler = (data) => {
|
|
724
751
|
const parsed = this.parseMsg(data);
|
|
725
|
-
if (!parsed)
|
|
752
|
+
if (!parsed) {
|
|
753
|
+
conn.close();
|
|
726
754
|
return;
|
|
755
|
+
}
|
|
727
756
|
if (!import_value.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
|
|
728
757
|
log?.warn(
|
|
729
758
|
`${this.clientId} -- received invalid handshake resp: ${JSON.stringify(parsed)}`
|
|
@@ -738,22 +767,11 @@ var ClientTransport = class extends Transport {
|
|
|
738
767
|
);
|
|
739
768
|
return;
|
|
740
769
|
}
|
|
741
|
-
const oldSession = this.sessions.get(parsed.from);
|
|
742
770
|
const serverInstanceId = parsed.payload.status.instanceId;
|
|
743
|
-
const lastServerInstanceId = this.serverInstanceIds.get(parsed.from);
|
|
744
|
-
if (oldSession && lastServerInstanceId !== serverInstanceId && lastServerInstanceId !== void 0) {
|
|
745
|
-
log?.debug(
|
|
746
|
-
`${this.clientId} -- handshake from ${parsed.from} has different server instance (got: ${serverInstanceId}, last connected to: ${lastServerInstanceId}), starting a new session`
|
|
747
|
-
);
|
|
748
|
-
oldSession.resetBufferedMessages();
|
|
749
|
-
oldSession.closeStaleConnection();
|
|
750
|
-
this.deleteSession(oldSession);
|
|
751
|
-
}
|
|
752
771
|
log?.debug(
|
|
753
772
|
`${this.clientId} -- handshake from ${parsed.from} ok (server instance: ${serverInstanceId})`
|
|
754
773
|
);
|
|
755
|
-
this.
|
|
756
|
-
sessionCb(this.onConnect(conn, parsed.from));
|
|
774
|
+
sessionCb(this.onConnect(conn, parsed.from, serverInstanceId));
|
|
757
775
|
};
|
|
758
776
|
return bootHandler;
|
|
759
777
|
}
|
|
@@ -844,8 +862,14 @@ var WebSocketClientTransport = class extends ClientTransport {
|
|
|
844
862
|
ws.removeEventListener("close", onClose);
|
|
845
863
|
resolve({ err: evt.reason });
|
|
846
864
|
};
|
|
865
|
+
const onError = (evt) => {
|
|
866
|
+
ws.removeEventListener("error", onError);
|
|
867
|
+
ws.removeEventListener("close", onClose);
|
|
868
|
+
resolve({ err: evt.message });
|
|
869
|
+
};
|
|
847
870
|
ws.addEventListener("open", onOpen);
|
|
848
871
|
ws.addEventListener("close", onClose);
|
|
872
|
+
ws.addEventListener("error", onError);
|
|
849
873
|
}).catch((e) => {
|
|
850
874
|
const reason = e instanceof Error ? e.message : "unknown reason";
|
|
851
875
|
resolve({ err: `couldn't get a new websocket: ${reason}` });
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
|
-
import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
3
|
-
import { W as WebSocketConnection } from '../../../connection-
|
|
2
|
+
import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
3
|
+
import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
|
|
4
4
|
import '../../../types-3e5768ec.js';
|
|
5
5
|
import '@sinclair/typebox';
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
|
-
import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
3
|
-
import { W as WebSocketConnection } from '../../../connection-
|
|
2
|
+
import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
3
|
+
import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
|
|
4
4
|
import '../../../types-3e5768ec.js';
|
|
5
5
|
import '@sinclair/typebox';
|
|
6
6
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
WebSocketConnection
|
|
3
|
-
} from "../../../chunk-
|
|
3
|
+
} from "../../../chunk-RDTTKCGV.js";
|
|
4
4
|
import {
|
|
5
5
|
ClientTransport
|
|
6
|
-
} from "../../../chunk-
|
|
6
|
+
} from "../../../chunk-QEYN2Z6O.js";
|
|
7
7
|
import "../../../chunk-GZ7HCLLM.js";
|
|
8
|
-
import "../../../chunk-
|
|
8
|
+
import "../../../chunk-XFFS4UOD.js";
|
|
9
9
|
import {
|
|
10
10
|
log
|
|
11
11
|
} from "../../../chunk-H4BYJELI.js";
|
|
@@ -57,8 +57,14 @@ var WebSocketClientTransport = class extends ClientTransport {
|
|
|
57
57
|
ws.removeEventListener("close", onClose);
|
|
58
58
|
resolve({ err: evt.reason });
|
|
59
59
|
};
|
|
60
|
+
const onError = (evt) => {
|
|
61
|
+
ws.removeEventListener("error", onError);
|
|
62
|
+
ws.removeEventListener("close", onClose);
|
|
63
|
+
resolve({ err: evt.message });
|
|
64
|
+
};
|
|
60
65
|
ws.addEventListener("open", onOpen);
|
|
61
66
|
ws.addEventListener("close", onClose);
|
|
67
|
+
ws.addEventListener("error", onError);
|
|
62
68
|
}).catch((e) => {
|
|
63
69
|
const reason = e instanceof Error ? e.message : "unknown reason";
|
|
64
70
|
resolve({ err: `couldn't get a new websocket: ${reason}` });
|
|
@@ -54,7 +54,8 @@ var ControlMessageCloseSchema = import_typebox.Type.Object({
|
|
|
54
54
|
var PROTOCOL_VERSION = "v1";
|
|
55
55
|
var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
|
|
56
56
|
type: import_typebox.Type.Literal("HANDSHAKE_REQ"),
|
|
57
|
-
protocolVersion: import_typebox.Type.Literal(PROTOCOL_VERSION)
|
|
57
|
+
protocolVersion: import_typebox.Type.Literal(PROTOCOL_VERSION),
|
|
58
|
+
instanceId: import_typebox.Type.String()
|
|
58
59
|
});
|
|
59
60
|
var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
|
|
60
61
|
type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
|
|
@@ -144,9 +145,9 @@ var Connection = class {
|
|
|
144
145
|
this.debugId = `conn-${unsafeId()}`;
|
|
145
146
|
}
|
|
146
147
|
};
|
|
147
|
-
var HEARTBEAT_INTERVAL_MS =
|
|
148
|
-
var HEARTBEATS_TILL_DEAD =
|
|
149
|
-
var SESSION_DISCONNECT_GRACE_MS =
|
|
148
|
+
var HEARTBEAT_INTERVAL_MS = 1e3;
|
|
149
|
+
var HEARTBEATS_TILL_DEAD = 2;
|
|
150
|
+
var SESSION_DISCONNECT_GRACE_MS = 5e3;
|
|
150
151
|
var Session = class {
|
|
151
152
|
codec;
|
|
152
153
|
/**
|
|
@@ -229,11 +230,13 @@ var Session = class {
|
|
|
229
230
|
return fullMsg.id;
|
|
230
231
|
}
|
|
231
232
|
sendHeartbeat() {
|
|
232
|
-
if (this.heartbeatMisses >= HEARTBEATS_TILL_DEAD
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
233
|
+
if (this.heartbeatMisses >= HEARTBEATS_TILL_DEAD) {
|
|
234
|
+
if (this.connection) {
|
|
235
|
+
log?.info(
|
|
236
|
+
`${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
|
|
237
|
+
);
|
|
238
|
+
this.halfCloseConnection();
|
|
239
|
+
}
|
|
237
240
|
return;
|
|
238
241
|
}
|
|
239
242
|
this.send(
|
|
@@ -270,7 +273,6 @@ var Session = class {
|
|
|
270
273
|
}
|
|
271
274
|
}
|
|
272
275
|
updateBookkeeping(ack, seq) {
|
|
273
|
-
this.heartbeatMisses = 0;
|
|
274
276
|
this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq > ack);
|
|
275
277
|
this.ack = seq + 1;
|
|
276
278
|
}
|
|
@@ -302,6 +304,7 @@ var Session = class {
|
|
|
302
304
|
}, SESSION_DISCONNECT_GRACE_MS);
|
|
303
305
|
}
|
|
304
306
|
cancelGrace() {
|
|
307
|
+
this.heartbeatMisses = 0;
|
|
305
308
|
clearTimeout(this.disconnectionGrace);
|
|
306
309
|
}
|
|
307
310
|
get connected() {
|
|
@@ -409,6 +412,13 @@ var defaultTransportOptions = {
|
|
|
409
412
|
codec: NaiveJsonCodec
|
|
410
413
|
};
|
|
411
414
|
var Transport = class {
|
|
415
|
+
/**
|
|
416
|
+
* Unique per instance of the transport.
|
|
417
|
+
* This allows us to distinguish reconnects to different
|
|
418
|
+
* transports.
|
|
419
|
+
*/
|
|
420
|
+
instanceId = (0, import_nanoid3.nanoid)();
|
|
421
|
+
connectedInstanceIds = /* @__PURE__ */ new Map();
|
|
412
422
|
/**
|
|
413
423
|
* A flag indicating whether the transport has been destroyed.
|
|
414
424
|
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
@@ -470,12 +480,23 @@ var Transport = class {
|
|
|
470
480
|
* and we know the identity of the connected client.
|
|
471
481
|
* @param conn The connection object.
|
|
472
482
|
*/
|
|
473
|
-
onConnect(conn, connectedTo) {
|
|
483
|
+
onConnect(conn, connectedTo, instanceId) {
|
|
474
484
|
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
475
485
|
status: "connect",
|
|
476
486
|
conn
|
|
477
487
|
});
|
|
478
|
-
|
|
488
|
+
let session = this.sessions.get(connectedTo);
|
|
489
|
+
const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
|
|
490
|
+
if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
|
|
491
|
+
log?.debug(
|
|
492
|
+
`${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
|
|
493
|
+
);
|
|
494
|
+
session.resetBufferedMessages();
|
|
495
|
+
session.closeStaleConnection();
|
|
496
|
+
this.deleteSession(session);
|
|
497
|
+
session = void 0;
|
|
498
|
+
}
|
|
499
|
+
this.connectedInstanceIds.set(connectedTo, instanceId);
|
|
479
500
|
if (session === void 0) {
|
|
480
501
|
const newSession = this.createSession(connectedTo, conn);
|
|
481
502
|
log?.info(
|
|
@@ -541,11 +562,13 @@ var Transport = class {
|
|
|
541
562
|
const parsedMsg = this.codec.fromBuffer(msg);
|
|
542
563
|
if (parsedMsg === null) {
|
|
543
564
|
const decodedBuffer = new TextDecoder().decode(msg);
|
|
544
|
-
log?.
|
|
565
|
+
log?.error(
|
|
566
|
+
`${this.clientId} -- received malformed msg, killing conn: ${decodedBuffer}`
|
|
567
|
+
);
|
|
545
568
|
return null;
|
|
546
569
|
}
|
|
547
570
|
if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
548
|
-
log?.
|
|
571
|
+
log?.error(
|
|
549
572
|
`${this.clientId} -- received invalid msg: ${JSON.stringify(
|
|
550
573
|
parsedMsg
|
|
551
574
|
)}`
|
|
@@ -564,9 +587,6 @@ var Transport = class {
|
|
|
564
587
|
* @param msg The received message.
|
|
565
588
|
*/
|
|
566
589
|
handleMsg(msg) {
|
|
567
|
-
if (!msg) {
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
590
|
const session = this.sessionByClientId(msg.from);
|
|
571
591
|
session.cancelGrace();
|
|
572
592
|
log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
|
|
@@ -663,14 +683,6 @@ var Transport = class {
|
|
|
663
683
|
}
|
|
664
684
|
};
|
|
665
685
|
var ServerTransport = class extends Transport {
|
|
666
|
-
/**
|
|
667
|
-
* Unique per instance of server transport.
|
|
668
|
-
* This allows us to distinguish reconnects to different
|
|
669
|
-
* server transports at the same reachable address and signal
|
|
670
|
-
* the appropriate session disconnect back to the client at the
|
|
671
|
-
* boot stage.
|
|
672
|
-
*/
|
|
673
|
-
instanceId = (0, import_nanoid3.nanoid)();
|
|
674
686
|
constructor(clientId, providedOptions) {
|
|
675
687
|
super(clientId, providedOptions);
|
|
676
688
|
log?.info(
|
|
@@ -685,7 +697,14 @@ var ServerTransport = class extends Transport {
|
|
|
685
697
|
(establishedSession) => {
|
|
686
698
|
session = establishedSession;
|
|
687
699
|
conn.removeDataListener(bootHandler);
|
|
688
|
-
conn.addDataListener((data) =>
|
|
700
|
+
conn.addDataListener((data) => {
|
|
701
|
+
const parsed = this.parseMsg(data);
|
|
702
|
+
if (!parsed) {
|
|
703
|
+
conn.close();
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
this.handleMsg(parsed);
|
|
707
|
+
});
|
|
689
708
|
}
|
|
690
709
|
);
|
|
691
710
|
conn.addDataListener(bootHandler);
|
|
@@ -708,8 +727,10 @@ var ServerTransport = class extends Transport {
|
|
|
708
727
|
receiveWithBootSequence(conn, sessionCb) {
|
|
709
728
|
const bootHandler = (data) => {
|
|
710
729
|
const parsed = this.parseMsg(data);
|
|
711
|
-
if (!parsed)
|
|
730
|
+
if (!parsed) {
|
|
731
|
+
conn.close();
|
|
712
732
|
return;
|
|
733
|
+
}
|
|
713
734
|
if (!import_value.Value.Check(ControlMessageHandshakeRequestSchema, parsed.payload)) {
|
|
714
735
|
const responseMsg2 = bootResponseMessage(
|
|
715
736
|
this.clientId,
|
|
@@ -725,8 +746,9 @@ var ServerTransport = class extends Transport {
|
|
|
725
746
|
);
|
|
726
747
|
return;
|
|
727
748
|
}
|
|
749
|
+
const instanceId = parsed.payload.instanceId;
|
|
728
750
|
log?.debug(
|
|
729
|
-
`${this.clientId} -- handshake from ${parsed.from} ok, responding with handshake success`
|
|
751
|
+
`${this.clientId} -- handshake from ${parsed.from} ok (instance id: ${instanceId}), responding with handshake success`
|
|
730
752
|
);
|
|
731
753
|
const responseMsg = bootResponseMessage(
|
|
732
754
|
this.clientId,
|
|
@@ -735,7 +757,7 @@ var ServerTransport = class extends Transport {
|
|
|
735
757
|
true
|
|
736
758
|
);
|
|
737
759
|
conn.send(this.codec.toBuffer(responseMsg));
|
|
738
|
-
sessionCb(this.onConnect(conn, parsed.from));
|
|
760
|
+
sessionCb(this.onConnect(conn, parsed.from, instanceId));
|
|
739
761
|
};
|
|
740
762
|
return bootHandler;
|
|
741
763
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
1
|
+
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
2
2
|
import { WebSocketServer } from 'ws';
|
|
3
3
|
import { WebSocket } from 'isomorphic-ws';
|
|
4
|
-
import { W as WebSocketConnection } from '../../../connection-
|
|
4
|
+
import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
|
|
5
5
|
import '../../../types-3e5768ec.js';
|
|
6
6
|
import '@sinclair/typebox';
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
1
|
+
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
2
2
|
import { WebSocketServer } from 'ws';
|
|
3
3
|
import { WebSocket } from 'isomorphic-ws';
|
|
4
|
-
import { W as WebSocketConnection } from '../../../connection-
|
|
4
|
+
import { W as WebSocketConnection } from '../../../connection-eb10d250.js';
|
|
5
5
|
import '../../../types-3e5768ec.js';
|
|
6
6
|
import '@sinclair/typebox';
|
|
7
7
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
WebSocketConnection
|
|
3
|
-
} from "../../../chunk-
|
|
3
|
+
} from "../../../chunk-RDTTKCGV.js";
|
|
4
4
|
import {
|
|
5
5
|
ServerTransport
|
|
6
|
-
} from "../../../chunk-
|
|
6
|
+
} from "../../../chunk-QEYN2Z6O.js";
|
|
7
7
|
import "../../../chunk-GZ7HCLLM.js";
|
|
8
|
-
import "../../../chunk-
|
|
8
|
+
import "../../../chunk-XFFS4UOD.js";
|
|
9
9
|
import {
|
|
10
10
|
log
|
|
11
11
|
} from "../../../chunk-H4BYJELI.js";
|