@replit/river 0.12.0 → 0.12.2
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-ANGOKBE5.js → chunk-CT57CKBS.js} +67 -48
- package/dist/{chunk-4HOR4NUO.js → chunk-NHSJ4RDY.js} +1 -1
- package/dist/{chunk-XOTIAXAH.js → chunk-PIF32FUL.js} +1 -1
- package/dist/{chunk-IUDKWAOK.js → chunk-R6YJHJV7.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 +57 -38
- 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 +55 -32
- 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 +57 -38
- 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 +55 -32
- 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 +63 -38
- 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 +55 -32
- 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 +71 -50
- 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
|
@@ -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"),
|
|
@@ -78,7 +79,7 @@ var ControlMessagePayloadSchema = import_typebox.Type.Union([
|
|
|
78
79
|
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
79
80
|
import_typebox.Type.Unknown()
|
|
80
81
|
);
|
|
81
|
-
function bootRequestMessage(from, to) {
|
|
82
|
+
function bootRequestMessage(from, to, instanceId) {
|
|
82
83
|
return {
|
|
83
84
|
id: (0, import_nanoid.nanoid)(),
|
|
84
85
|
from,
|
|
@@ -89,7 +90,8 @@ function bootRequestMessage(from, to) {
|
|
|
89
90
|
controlFlags: 0,
|
|
90
91
|
payload: {
|
|
91
92
|
type: "HANDSHAKE_REQ",
|
|
92
|
-
protocolVersion: PROTOCOL_VERSION
|
|
93
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
94
|
+
instanceId
|
|
93
95
|
}
|
|
94
96
|
};
|
|
95
97
|
}
|
|
@@ -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.closeStaleConnection(this.connection);
|
|
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() {
|
|
@@ -321,6 +325,7 @@ var Session = class {
|
|
|
321
325
|
*/
|
|
322
326
|
halfCloseConnection() {
|
|
323
327
|
this.connection?.close();
|
|
328
|
+
clearInterval(this.heartbeat);
|
|
324
329
|
}
|
|
325
330
|
inspectSendBuffer() {
|
|
326
331
|
return this.sendBuffer;
|
|
@@ -400,6 +405,13 @@ var defaultTransportOptions = {
|
|
|
400
405
|
codec: NaiveJsonCodec
|
|
401
406
|
};
|
|
402
407
|
var Transport = class {
|
|
408
|
+
/**
|
|
409
|
+
* Unique per instance of the transport.
|
|
410
|
+
* This allows us to distinguish reconnects to different
|
|
411
|
+
* transports.
|
|
412
|
+
*/
|
|
413
|
+
instanceId = (0, import_nanoid3.nanoid)();
|
|
414
|
+
connectedInstanceIds = /* @__PURE__ */ new Map();
|
|
403
415
|
/**
|
|
404
416
|
* A flag indicating whether the transport has been destroyed.
|
|
405
417
|
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
@@ -461,12 +473,23 @@ var Transport = class {
|
|
|
461
473
|
* and we know the identity of the connected client.
|
|
462
474
|
* @param conn The connection object.
|
|
463
475
|
*/
|
|
464
|
-
onConnect(conn, connectedTo) {
|
|
476
|
+
onConnect(conn, connectedTo, instanceId) {
|
|
465
477
|
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
466
478
|
status: "connect",
|
|
467
479
|
conn
|
|
468
480
|
});
|
|
469
|
-
|
|
481
|
+
let session = this.sessions.get(connectedTo);
|
|
482
|
+
const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
|
|
483
|
+
if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
|
|
484
|
+
log?.warn(
|
|
485
|
+
`${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
|
|
486
|
+
);
|
|
487
|
+
session.resetBufferedMessages();
|
|
488
|
+
session.closeStaleConnection(conn);
|
|
489
|
+
this.deleteSession(session);
|
|
490
|
+
session = void 0;
|
|
491
|
+
}
|
|
492
|
+
this.connectedInstanceIds.set(connectedTo, instanceId);
|
|
470
493
|
if (session === void 0) {
|
|
471
494
|
const newSession = this.createSession(connectedTo, conn);
|
|
472
495
|
log?.info(
|
|
@@ -497,13 +520,13 @@ var Transport = class {
|
|
|
497
520
|
}
|
|
498
521
|
deleteSession(session) {
|
|
499
522
|
this.sessions.delete(session.to);
|
|
523
|
+
log?.info(
|
|
524
|
+
`${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
|
|
525
|
+
);
|
|
500
526
|
this.eventDispatcher.dispatchEvent("sessionStatus", {
|
|
501
527
|
status: "disconnect",
|
|
502
528
|
session
|
|
503
529
|
});
|
|
504
|
-
log?.info(
|
|
505
|
-
`${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
|
|
506
|
-
);
|
|
507
530
|
}
|
|
508
531
|
/**
|
|
509
532
|
* The downstream implementation needs to call this when a connection is closed.
|
|
@@ -532,11 +555,13 @@ var Transport = class {
|
|
|
532
555
|
const parsedMsg = this.codec.fromBuffer(msg);
|
|
533
556
|
if (parsedMsg === null) {
|
|
534
557
|
const decodedBuffer = new TextDecoder().decode(msg);
|
|
535
|
-
log?.
|
|
558
|
+
log?.error(
|
|
559
|
+
`${this.clientId} -- received malformed msg, killing conn: ${decodedBuffer}`
|
|
560
|
+
);
|
|
536
561
|
return null;
|
|
537
562
|
}
|
|
538
563
|
if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
539
|
-
log?.
|
|
564
|
+
log?.error(
|
|
540
565
|
`${this.clientId} -- received invalid msg: ${JSON.stringify(
|
|
541
566
|
parsedMsg
|
|
542
567
|
)}`
|
|
@@ -555,9 +580,6 @@ var Transport = class {
|
|
|
555
580
|
* @param msg The received message.
|
|
556
581
|
*/
|
|
557
582
|
handleMsg(msg) {
|
|
558
|
-
if (!msg) {
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
583
|
const session = this.sessionByClientId(msg.from);
|
|
562
584
|
session.cancelGrace();
|
|
563
585
|
log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
|
|
@@ -659,7 +681,6 @@ var ClientTransport = class extends Transport {
|
|
|
659
681
|
*/
|
|
660
682
|
inflightConnectionPromises;
|
|
661
683
|
tryReconnecting = true;
|
|
662
|
-
serverInstanceIds = /* @__PURE__ */ new Map();
|
|
663
684
|
constructor(clientId, providedOptions) {
|
|
664
685
|
super(clientId, providedOptions);
|
|
665
686
|
this.inflightConnectionPromises = /* @__PURE__ */ new Map();
|
|
@@ -667,7 +688,14 @@ var ClientTransport = class extends Transport {
|
|
|
667
688
|
handleConnection(conn, to) {
|
|
668
689
|
const bootHandler = this.receiveWithBootSequence(conn, () => {
|
|
669
690
|
conn.removeDataListener(bootHandler);
|
|
670
|
-
conn.addDataListener((data) =>
|
|
691
|
+
conn.addDataListener((data) => {
|
|
692
|
+
const parsed = this.parseMsg(data);
|
|
693
|
+
if (!parsed) {
|
|
694
|
+
conn.close();
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
this.handleMsg(parsed);
|
|
698
|
+
});
|
|
671
699
|
});
|
|
672
700
|
conn.addDataListener(bootHandler);
|
|
673
701
|
conn.addCloseListener(() => {
|
|
@@ -699,7 +727,7 @@ var ClientTransport = class extends Transport {
|
|
|
699
727
|
try {
|
|
700
728
|
const conn = await reconnectPromise;
|
|
701
729
|
this.state = "open";
|
|
702
|
-
const requestMsg = bootRequestMessage(this.clientId, to);
|
|
730
|
+
const requestMsg = bootRequestMessage(this.clientId, to, this.instanceId);
|
|
703
731
|
log?.debug(`${this.clientId} -- sending boot handshake to ${to}`);
|
|
704
732
|
conn.send(this.codec.toBuffer(requestMsg));
|
|
705
733
|
} catch (error) {
|
|
@@ -722,8 +750,10 @@ var ClientTransport = class extends Transport {
|
|
|
722
750
|
receiveWithBootSequence(conn, sessionCb) {
|
|
723
751
|
const bootHandler = (data) => {
|
|
724
752
|
const parsed = this.parseMsg(data);
|
|
725
|
-
if (!parsed)
|
|
753
|
+
if (!parsed) {
|
|
754
|
+
conn.close();
|
|
726
755
|
return;
|
|
756
|
+
}
|
|
727
757
|
if (!import_value.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
|
|
728
758
|
log?.warn(
|
|
729
759
|
`${this.clientId} -- received invalid handshake resp: ${JSON.stringify(parsed)}`
|
|
@@ -738,22 +768,11 @@ var ClientTransport = class extends Transport {
|
|
|
738
768
|
);
|
|
739
769
|
return;
|
|
740
770
|
}
|
|
741
|
-
const oldSession = this.sessions.get(parsed.from);
|
|
742
771
|
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
772
|
log?.debug(
|
|
753
773
|
`${this.clientId} -- handshake from ${parsed.from} ok (server instance: ${serverInstanceId})`
|
|
754
774
|
);
|
|
755
|
-
this.
|
|
756
|
-
sessionCb(this.onConnect(conn, parsed.from));
|
|
775
|
+
sessionCb(this.onConnect(conn, parsed.from, serverInstanceId));
|
|
757
776
|
};
|
|
758
777
|
return bootHandler;
|
|
759
778
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
2
|
-
import { S as StreamConnection } from '../../../connection-
|
|
1
|
+
import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
2
|
+
import { S as StreamConnection } from '../../../connection-bf7811aa.js';
|
|
3
3
|
import '../../../types-3e5768ec.js';
|
|
4
4
|
import '@sinclair/typebox';
|
|
5
5
|
import '../../../messageFraming-b200ef25.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
2
|
-
import { S as StreamConnection } from '../../../connection-
|
|
1
|
+
import { a as ClientTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
2
|
+
import { S as StreamConnection } from '../../../connection-bf7811aa.js';
|
|
3
3
|
import '../../../types-3e5768ec.js';
|
|
4
4
|
import '@sinclair/typebox';
|
|
5
5
|
import '../../../messageFraming-b200ef25.js';
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
StreamConnection
|
|
3
|
-
} from "../../../chunk-
|
|
3
|
+
} from "../../../chunk-R6YJHJV7.js";
|
|
4
4
|
import "../../../chunk-5IZ2UHWV.js";
|
|
5
5
|
import "../../../chunk-IIBVKYDB.js";
|
|
6
6
|
import {
|
|
7
7
|
ClientTransport
|
|
8
|
-
} from "../../../chunk-
|
|
8
|
+
} from "../../../chunk-CT57CKBS.js";
|
|
9
9
|
import "../../../chunk-GZ7HCLLM.js";
|
|
10
|
-
import "../../../chunk-
|
|
10
|
+
import "../../../chunk-XFFS4UOD.js";
|
|
11
11
|
import {
|
|
12
12
|
log
|
|
13
13
|
} 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"),
|
|
@@ -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.closeStaleConnection(this.connection);
|
|
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() {
|
|
@@ -330,6 +333,7 @@ var Session = class {
|
|
|
330
333
|
*/
|
|
331
334
|
halfCloseConnection() {
|
|
332
335
|
this.connection?.close();
|
|
336
|
+
clearInterval(this.heartbeat);
|
|
333
337
|
}
|
|
334
338
|
inspectSendBuffer() {
|
|
335
339
|
return this.sendBuffer;
|
|
@@ -409,6 +413,13 @@ var defaultTransportOptions = {
|
|
|
409
413
|
codec: NaiveJsonCodec
|
|
410
414
|
};
|
|
411
415
|
var Transport = class {
|
|
416
|
+
/**
|
|
417
|
+
* Unique per instance of the transport.
|
|
418
|
+
* This allows us to distinguish reconnects to different
|
|
419
|
+
* transports.
|
|
420
|
+
*/
|
|
421
|
+
instanceId = (0, import_nanoid3.nanoid)();
|
|
422
|
+
connectedInstanceIds = /* @__PURE__ */ new Map();
|
|
412
423
|
/**
|
|
413
424
|
* A flag indicating whether the transport has been destroyed.
|
|
414
425
|
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
@@ -470,12 +481,23 @@ var Transport = class {
|
|
|
470
481
|
* and we know the identity of the connected client.
|
|
471
482
|
* @param conn The connection object.
|
|
472
483
|
*/
|
|
473
|
-
onConnect(conn, connectedTo) {
|
|
484
|
+
onConnect(conn, connectedTo, instanceId) {
|
|
474
485
|
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
475
486
|
status: "connect",
|
|
476
487
|
conn
|
|
477
488
|
});
|
|
478
|
-
|
|
489
|
+
let session = this.sessions.get(connectedTo);
|
|
490
|
+
const lastInstanceId = this.connectedInstanceIds.get(connectedTo);
|
|
491
|
+
if (session && lastInstanceId !== instanceId && lastInstanceId !== void 0) {
|
|
492
|
+
log?.warn(
|
|
493
|
+
`${this.clientId} -- handshake from ${connectedTo} has different server instance (got: ${instanceId}, last connected to: ${lastInstanceId}), starting a new session`
|
|
494
|
+
);
|
|
495
|
+
session.resetBufferedMessages();
|
|
496
|
+
session.closeStaleConnection(conn);
|
|
497
|
+
this.deleteSession(session);
|
|
498
|
+
session = void 0;
|
|
499
|
+
}
|
|
500
|
+
this.connectedInstanceIds.set(connectedTo, instanceId);
|
|
479
501
|
if (session === void 0) {
|
|
480
502
|
const newSession = this.createSession(connectedTo, conn);
|
|
481
503
|
log?.info(
|
|
@@ -506,13 +528,13 @@ var Transport = class {
|
|
|
506
528
|
}
|
|
507
529
|
deleteSession(session) {
|
|
508
530
|
this.sessions.delete(session.to);
|
|
531
|
+
log?.info(
|
|
532
|
+
`${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
|
|
533
|
+
);
|
|
509
534
|
this.eventDispatcher.dispatchEvent("sessionStatus", {
|
|
510
535
|
status: "disconnect",
|
|
511
536
|
session
|
|
512
537
|
});
|
|
513
|
-
log?.info(
|
|
514
|
-
`${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
|
|
515
|
-
);
|
|
516
538
|
}
|
|
517
539
|
/**
|
|
518
540
|
* The downstream implementation needs to call this when a connection is closed.
|
|
@@ -541,11 +563,13 @@ var Transport = class {
|
|
|
541
563
|
const parsedMsg = this.codec.fromBuffer(msg);
|
|
542
564
|
if (parsedMsg === null) {
|
|
543
565
|
const decodedBuffer = new TextDecoder().decode(msg);
|
|
544
|
-
log?.
|
|
566
|
+
log?.error(
|
|
567
|
+
`${this.clientId} -- received malformed msg, killing conn: ${decodedBuffer}`
|
|
568
|
+
);
|
|
545
569
|
return null;
|
|
546
570
|
}
|
|
547
571
|
if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
548
|
-
log?.
|
|
572
|
+
log?.error(
|
|
549
573
|
`${this.clientId} -- received invalid msg: ${JSON.stringify(
|
|
550
574
|
parsedMsg
|
|
551
575
|
)}`
|
|
@@ -564,9 +588,6 @@ var Transport = class {
|
|
|
564
588
|
* @param msg The received message.
|
|
565
589
|
*/
|
|
566
590
|
handleMsg(msg) {
|
|
567
|
-
if (!msg) {
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
591
|
const session = this.sessionByClientId(msg.from);
|
|
571
592
|
session.cancelGrace();
|
|
572
593
|
log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
|
|
@@ -663,14 +684,6 @@ var Transport = class {
|
|
|
663
684
|
}
|
|
664
685
|
};
|
|
665
686
|
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
687
|
constructor(clientId, providedOptions) {
|
|
675
688
|
super(clientId, providedOptions);
|
|
676
689
|
log?.info(
|
|
@@ -685,7 +698,14 @@ var ServerTransport = class extends Transport {
|
|
|
685
698
|
(establishedSession) => {
|
|
686
699
|
session = establishedSession;
|
|
687
700
|
conn.removeDataListener(bootHandler);
|
|
688
|
-
conn.addDataListener((data) =>
|
|
701
|
+
conn.addDataListener((data) => {
|
|
702
|
+
const parsed = this.parseMsg(data);
|
|
703
|
+
if (!parsed) {
|
|
704
|
+
conn.close();
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
this.handleMsg(parsed);
|
|
708
|
+
});
|
|
689
709
|
}
|
|
690
710
|
);
|
|
691
711
|
conn.addDataListener(bootHandler);
|
|
@@ -708,8 +728,10 @@ var ServerTransport = class extends Transport {
|
|
|
708
728
|
receiveWithBootSequence(conn, sessionCb) {
|
|
709
729
|
const bootHandler = (data) => {
|
|
710
730
|
const parsed = this.parseMsg(data);
|
|
711
|
-
if (!parsed)
|
|
731
|
+
if (!parsed) {
|
|
732
|
+
conn.close();
|
|
712
733
|
return;
|
|
734
|
+
}
|
|
713
735
|
if (!import_value.Value.Check(ControlMessageHandshakeRequestSchema, parsed.payload)) {
|
|
714
736
|
const responseMsg2 = bootResponseMessage(
|
|
715
737
|
this.clientId,
|
|
@@ -725,8 +747,9 @@ var ServerTransport = class extends Transport {
|
|
|
725
747
|
);
|
|
726
748
|
return;
|
|
727
749
|
}
|
|
750
|
+
const instanceId = parsed.payload.instanceId;
|
|
728
751
|
log?.debug(
|
|
729
|
-
`${this.clientId} -- handshake from ${parsed.from} ok, responding with handshake success`
|
|
752
|
+
`${this.clientId} -- handshake from ${parsed.from} ok (instance id: ${instanceId}), responding with handshake success`
|
|
730
753
|
);
|
|
731
754
|
const responseMsg = bootResponseMessage(
|
|
732
755
|
this.clientId,
|
|
@@ -735,7 +758,7 @@ var ServerTransport = class extends Transport {
|
|
|
735
758
|
true
|
|
736
759
|
);
|
|
737
760
|
conn.send(this.codec.toBuffer(responseMsg));
|
|
738
|
-
sessionCb(this.onConnect(conn, parsed.from));
|
|
761
|
+
sessionCb(this.onConnect(conn, parsed.from, instanceId));
|
|
739
762
|
};
|
|
740
763
|
return bootHandler;
|
|
741
764
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
2
|
-
import { S as StreamConnection } from '../../../connection-
|
|
1
|
+
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
2
|
+
import { S as StreamConnection } from '../../../connection-bf7811aa.js';
|
|
3
3
|
import '../../../types-3e5768ec.js';
|
|
4
4
|
import '@sinclair/typebox';
|
|
5
5
|
import '../../../messageFraming-b200ef25.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-
|
|
2
|
-
import { S as StreamConnection } from '../../../connection-
|
|
1
|
+
import { S as ServerTransport, b as TransportClientId, c as TransportOptions } from '../../../index-0c0a69f6.js';
|
|
2
|
+
import { S as StreamConnection } from '../../../connection-bf7811aa.js';
|
|
3
3
|
import '../../../types-3e5768ec.js';
|
|
4
4
|
import '@sinclair/typebox';
|
|
5
5
|
import '../../../messageFraming-b200ef25.js';
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
StreamConnection
|
|
3
|
-
} from "../../../chunk-
|
|
3
|
+
} from "../../../chunk-R6YJHJV7.js";
|
|
4
4
|
import "../../../chunk-5IZ2UHWV.js";
|
|
5
5
|
import "../../../chunk-IIBVKYDB.js";
|
|
6
6
|
import {
|
|
7
7
|
ServerTransport
|
|
8
|
-
} from "../../../chunk-
|
|
8
|
+
} from "../../../chunk-CT57CKBS.js";
|
|
9
9
|
import "../../../chunk-GZ7HCLLM.js";
|
|
10
|
-
import "../../../chunk-
|
|
10
|
+
import "../../../chunk-XFFS4UOD.js";
|
|
11
11
|
import "../../../chunk-H4BYJELI.js";
|
|
12
12
|
|
|
13
13
|
// transport/impls/stdio/server.ts
|