@replit/river 0.200.0-rc.2 → 0.200.0-rc.3
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/README.md +21 -20
- package/dist/{chunk-S5RL45KH.js → chunk-3CCOX55A.js} +76 -42
- package/dist/chunk-3CCOX55A.js.map +1 -0
- package/dist/chunk-GWYJZFCW.js +653 -0
- package/dist/chunk-GWYJZFCW.js.map +1 -0
- package/dist/chunk-O4O2E6DJ.js +277 -0
- package/dist/chunk-O4O2E6DJ.js.map +1 -0
- package/dist/chunk-QXLXDJD5.js +382 -0
- package/dist/chunk-QXLXDJD5.js.map +1 -0
- package/dist/{chunk-4VNY34QG.js → chunk-UFMDEG44.js} +24 -18
- package/dist/chunk-UFMDEG44.js.map +1 -0
- package/dist/{chunk-7CKIN3JT.js → chunk-UIYGPURD.js} +31 -484
- package/dist/chunk-UIYGPURD.js.map +1 -0
- package/dist/{chunk-QMM35C3H.js → chunk-VXYHC666.js} +1 -1
- package/dist/chunk-VXYHC666.js.map +1 -0
- package/dist/chunk-WSCAA7VY.js +50 -0
- package/dist/chunk-WSCAA7VY.js.map +1 -0
- package/dist/chunk-ZQVKFLAB.js +399 -0
- package/dist/chunk-ZQVKFLAB.js.map +1 -0
- package/dist/client-a84783be.d.ts +49 -0
- package/dist/{connection-f900e390.d.ts → connection-320fb130.d.ts} +1 -5
- package/dist/connection-d0b488e6.d.ts +11 -0
- package/dist/context-c9668e95.d.ts +527 -0
- package/dist/logging/index.cjs.map +1 -1
- package/dist/logging/index.d.cts +1 -1
- package/dist/logging/index.d.ts +1 -1
- package/dist/logging/index.js +1 -1
- package/dist/{index-10ebd26a.d.ts → message-fd349b27.d.ts} +31 -31
- package/dist/router/index.cjs +83 -496
- package/dist/router/index.cjs.map +1 -1
- package/dist/router/index.d.cts +11 -46
- package/dist/router/index.d.ts +11 -46
- package/dist/router/index.js +2 -4
- package/dist/server-dbad597e.d.ts +42 -0
- package/dist/{services-970f97bb.d.ts → services-690e5553.d.ts} +5 -602
- package/dist/transport/impls/uds/client.cjs +1240 -1237
- package/dist/transport/impls/uds/client.cjs.map +1 -1
- package/dist/transport/impls/uds/client.d.cts +4 -3
- package/dist/transport/impls/uds/client.d.ts +4 -3
- package/dist/transport/impls/uds/client.js +7 -13
- package/dist/transport/impls/uds/client.js.map +1 -1
- package/dist/transport/impls/uds/server.cjs +1302 -1168
- package/dist/transport/impls/uds/server.cjs.map +1 -1
- package/dist/transport/impls/uds/server.d.cts +4 -4
- package/dist/transport/impls/uds/server.d.ts +4 -4
- package/dist/transport/impls/uds/server.js +6 -6
- package/dist/transport/impls/ws/client.cjs +981 -986
- package/dist/transport/impls/ws/client.cjs.map +1 -1
- package/dist/transport/impls/ws/client.d.cts +6 -5
- package/dist/transport/impls/ws/client.d.ts +6 -5
- package/dist/transport/impls/ws/client.js +6 -7
- package/dist/transport/impls/ws/client.js.map +1 -1
- package/dist/transport/impls/ws/server.cjs +1183 -1064
- package/dist/transport/impls/ws/server.cjs.map +1 -1
- package/dist/transport/impls/ws/server.d.cts +4 -4
- package/dist/transport/impls/ws/server.d.ts +4 -4
- package/dist/transport/impls/ws/server.js +6 -6
- package/dist/transport/index.cjs +1435 -1377
- package/dist/transport/index.cjs.map +1 -1
- package/dist/transport/index.d.cts +4 -26
- package/dist/transport/index.d.ts +4 -26
- package/dist/transport/index.js +9 -9
- package/dist/util/testHelpers.cjs +738 -302
- package/dist/util/testHelpers.cjs.map +1 -1
- package/dist/util/testHelpers.d.cts +9 -4
- package/dist/util/testHelpers.d.ts +9 -4
- package/dist/util/testHelpers.js +32 -8
- package/dist/util/testHelpers.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-47TFNAY2.js +0 -476
- package/dist/chunk-47TFNAY2.js.map +0 -1
- package/dist/chunk-4VNY34QG.js.map +0 -1
- package/dist/chunk-7CKIN3JT.js.map +0 -1
- package/dist/chunk-CZP4LK3F.js +0 -335
- package/dist/chunk-CZP4LK3F.js.map +0 -1
- package/dist/chunk-DJCW3SKT.js +0 -59
- package/dist/chunk-DJCW3SKT.js.map +0 -1
- package/dist/chunk-NQWDT6GS.js +0 -347
- package/dist/chunk-NQWDT6GS.js.map +0 -1
- package/dist/chunk-ONUXWVRC.js +0 -492
- package/dist/chunk-ONUXWVRC.js.map +0 -1
- package/dist/chunk-QMM35C3H.js.map +0 -1
- package/dist/chunk-S5RL45KH.js.map +0 -1
- package/dist/connection-3f117047.d.ts +0 -17
|
@@ -34,13 +34,16 @@ __export(testHelpers_exports, {
|
|
|
34
34
|
asClientStream: () => asClientStream,
|
|
35
35
|
asClientSubscription: () => asClientSubscription,
|
|
36
36
|
asClientUpload: () => asClientUpload,
|
|
37
|
+
closeAllConnections: () => closeAllConnections,
|
|
37
38
|
createDummyTransportMessage: () => createDummyTransportMessage,
|
|
38
39
|
createLocalWebSocketClient: () => createLocalWebSocketClient,
|
|
39
40
|
createWebSocketServer: () => createWebSocketServer,
|
|
40
41
|
dummySession: () => dummySession,
|
|
41
42
|
getIteratorFromStream: () => getIteratorFromStream,
|
|
43
|
+
getTransportConnections: () => getTransportConnections,
|
|
42
44
|
getUnixSocketPath: () => getUnixSocketPath,
|
|
43
45
|
iterNext: () => iterNext,
|
|
46
|
+
numberOfConnections: () => numberOfConnections,
|
|
44
47
|
onUdsServeReady: () => onUdsServeReady,
|
|
45
48
|
onWsServerReady: () => onWsServerReady,
|
|
46
49
|
payloadToTransportMessage: () => payloadToTransportMessage,
|
|
@@ -376,23 +379,119 @@ var InputReaderErrorSchema = import_typebox2.Type.Object({
|
|
|
376
379
|
message: import_typebox2.Type.String()
|
|
377
380
|
});
|
|
378
381
|
|
|
382
|
+
// transport/message.ts
|
|
383
|
+
var import_typebox3 = require("@sinclair/typebox");
|
|
384
|
+
|
|
385
|
+
// transport/id.ts
|
|
386
|
+
var import_nanoid = require("nanoid");
|
|
387
|
+
var alphabet = (0, import_nanoid.customAlphabet)(
|
|
388
|
+
"1234567890abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ"
|
|
389
|
+
);
|
|
390
|
+
var generateId = () => alphabet(12);
|
|
391
|
+
|
|
392
|
+
// transport/message.ts
|
|
393
|
+
var TransportMessageSchema = (t) => import_typebox3.Type.Object({
|
|
394
|
+
id: import_typebox3.Type.String(),
|
|
395
|
+
from: import_typebox3.Type.String(),
|
|
396
|
+
to: import_typebox3.Type.String(),
|
|
397
|
+
seq: import_typebox3.Type.Integer(),
|
|
398
|
+
ack: import_typebox3.Type.Integer(),
|
|
399
|
+
serviceName: import_typebox3.Type.Optional(import_typebox3.Type.String()),
|
|
400
|
+
procedureName: import_typebox3.Type.Optional(import_typebox3.Type.String()),
|
|
401
|
+
streamId: import_typebox3.Type.String(),
|
|
402
|
+
controlFlags: import_typebox3.Type.Integer(),
|
|
403
|
+
tracing: import_typebox3.Type.Optional(
|
|
404
|
+
import_typebox3.Type.Object({
|
|
405
|
+
traceparent: import_typebox3.Type.String(),
|
|
406
|
+
tracestate: import_typebox3.Type.String()
|
|
407
|
+
})
|
|
408
|
+
),
|
|
409
|
+
payload: t
|
|
410
|
+
});
|
|
411
|
+
var ControlMessageAckSchema = import_typebox3.Type.Object({
|
|
412
|
+
type: import_typebox3.Type.Literal("ACK")
|
|
413
|
+
});
|
|
414
|
+
var ControlMessageCloseSchema = import_typebox3.Type.Object({
|
|
415
|
+
type: import_typebox3.Type.Literal("CLOSE")
|
|
416
|
+
});
|
|
417
|
+
var ControlMessageHandshakeRequestSchema = import_typebox3.Type.Object({
|
|
418
|
+
type: import_typebox3.Type.Literal("HANDSHAKE_REQ"),
|
|
419
|
+
protocolVersion: import_typebox3.Type.String(),
|
|
420
|
+
sessionId: import_typebox3.Type.String(),
|
|
421
|
+
/**
|
|
422
|
+
* Specifies what the server's expected session state (from the pov of the client). This can be
|
|
423
|
+
* used by the server to know whether this is a new or a reestablished connection, and whether it
|
|
424
|
+
* is compatible with what it already has.
|
|
425
|
+
*/
|
|
426
|
+
expectedSessionState: import_typebox3.Type.Object({
|
|
427
|
+
// what the client expects the server to send next
|
|
428
|
+
nextExpectedSeq: import_typebox3.Type.Integer(),
|
|
429
|
+
// TODO: remove optional once we know all servers
|
|
430
|
+
// are nextSentSeq here
|
|
431
|
+
// what the server expects the client to send next
|
|
432
|
+
nextSentSeq: import_typebox3.Type.Optional(import_typebox3.Type.Integer())
|
|
433
|
+
}),
|
|
434
|
+
metadata: import_typebox3.Type.Optional(import_typebox3.Type.Unknown())
|
|
435
|
+
});
|
|
436
|
+
var HandshakeErrorRetriableResponseCodes = import_typebox3.Type.Union([
|
|
437
|
+
import_typebox3.Type.Literal("SESSION_STATE_MISMATCH")
|
|
438
|
+
]);
|
|
439
|
+
var HandshakeErrorFatalResponseCodes = import_typebox3.Type.Union([
|
|
440
|
+
import_typebox3.Type.Literal("MALFORMED_HANDSHAKE_META"),
|
|
441
|
+
import_typebox3.Type.Literal("MALFORMED_HANDSHAKE"),
|
|
442
|
+
import_typebox3.Type.Literal("PROTOCOL_VERSION_MISMATCH"),
|
|
443
|
+
import_typebox3.Type.Literal("REJECTED_BY_CUSTOM_HANDLER")
|
|
444
|
+
]);
|
|
445
|
+
var HandshakeErrorResponseCodes = import_typebox3.Type.Union([
|
|
446
|
+
HandshakeErrorRetriableResponseCodes,
|
|
447
|
+
HandshakeErrorFatalResponseCodes
|
|
448
|
+
]);
|
|
449
|
+
var ControlMessageHandshakeResponseSchema = import_typebox3.Type.Object({
|
|
450
|
+
type: import_typebox3.Type.Literal("HANDSHAKE_RESP"),
|
|
451
|
+
status: import_typebox3.Type.Union([
|
|
452
|
+
import_typebox3.Type.Object({
|
|
453
|
+
ok: import_typebox3.Type.Literal(true),
|
|
454
|
+
sessionId: import_typebox3.Type.String()
|
|
455
|
+
}),
|
|
456
|
+
import_typebox3.Type.Object({
|
|
457
|
+
ok: import_typebox3.Type.Literal(false),
|
|
458
|
+
reason: import_typebox3.Type.String(),
|
|
459
|
+
// TODO: remove optional once we know all servers
|
|
460
|
+
// are sending code here
|
|
461
|
+
code: import_typebox3.Type.Optional(HandshakeErrorResponseCodes)
|
|
462
|
+
})
|
|
463
|
+
])
|
|
464
|
+
});
|
|
465
|
+
var ControlMessagePayloadSchema = import_typebox3.Type.Union([
|
|
466
|
+
ControlMessageCloseSchema,
|
|
467
|
+
ControlMessageAckSchema,
|
|
468
|
+
ControlMessageHandshakeRequestSchema,
|
|
469
|
+
ControlMessageHandshakeResponseSchema
|
|
470
|
+
]);
|
|
471
|
+
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
472
|
+
import_typebox3.Type.Unknown()
|
|
473
|
+
);
|
|
474
|
+
function isAck(controlFlag) {
|
|
475
|
+
return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
|
|
476
|
+
}
|
|
477
|
+
|
|
379
478
|
// tracing/index.ts
|
|
380
479
|
var import_api = require("@opentelemetry/api");
|
|
381
480
|
|
|
382
481
|
// package.json
|
|
383
|
-
var version = "0.200.0-rc.
|
|
482
|
+
var version = "0.200.0-rc.3";
|
|
384
483
|
|
|
385
484
|
// tracing/index.ts
|
|
386
|
-
function createSessionTelemetryInfo(
|
|
485
|
+
function createSessionTelemetryInfo(sessionId, to, from, propagationCtx) {
|
|
387
486
|
const parentCtx = propagationCtx ? import_api.propagation.extract(import_api.context.active(), propagationCtx) : import_api.context.active();
|
|
388
487
|
const span = tracer.startSpan(
|
|
389
|
-
`session ${
|
|
488
|
+
`session ${sessionId}`,
|
|
390
489
|
{
|
|
391
490
|
attributes: {
|
|
392
491
|
component: "river",
|
|
393
|
-
"river.session.id":
|
|
394
|
-
"river.session.to":
|
|
395
|
-
"river.session.from":
|
|
492
|
+
"river.session.id": sessionId,
|
|
493
|
+
"river.session.to": to,
|
|
494
|
+
"river.session.from": from
|
|
396
495
|
}
|
|
397
496
|
},
|
|
398
497
|
parentCtx
|
|
@@ -413,298 +512,6 @@ function coerceErrorString(err) {
|
|
|
413
512
|
// util/testHelpers.ts
|
|
414
513
|
var import_nanoid2 = require("nanoid");
|
|
415
514
|
|
|
416
|
-
// transport/session.ts
|
|
417
|
-
var import_nanoid = require("nanoid");
|
|
418
|
-
var import_api2 = require("@opentelemetry/api");
|
|
419
|
-
var nanoid = (0, import_nanoid.customAlphabet)("1234567890abcdefghijklmnopqrstuvxyz", 6);
|
|
420
|
-
var unsafeId = () => nanoid();
|
|
421
|
-
var Session = class {
|
|
422
|
-
codec;
|
|
423
|
-
options;
|
|
424
|
-
telemetry;
|
|
425
|
-
/**
|
|
426
|
-
* The buffer of messages that have been sent but not yet acknowledged.
|
|
427
|
-
*/
|
|
428
|
-
sendBuffer = [];
|
|
429
|
-
/**
|
|
430
|
-
* The active connection associated with this session
|
|
431
|
-
*/
|
|
432
|
-
connection;
|
|
433
|
-
/**
|
|
434
|
-
* A connection that is currently undergoing handshaking. Used to distinguish between the active
|
|
435
|
-
* connection, but still be able to close it if needed.
|
|
436
|
-
*/
|
|
437
|
-
handshakingConnection;
|
|
438
|
-
from;
|
|
439
|
-
to;
|
|
440
|
-
/**
|
|
441
|
-
* The unique ID of this session.
|
|
442
|
-
*/
|
|
443
|
-
id;
|
|
444
|
-
/**
|
|
445
|
-
* What the other side advertised as their session ID
|
|
446
|
-
* for this session.
|
|
447
|
-
*/
|
|
448
|
-
advertisedSessionId;
|
|
449
|
-
/**
|
|
450
|
-
* Number of messages we've sent along this session (excluding handshake and acks)
|
|
451
|
-
*/
|
|
452
|
-
seq = 0;
|
|
453
|
-
/**
|
|
454
|
-
* Number of unique messages we've received this session (excluding handshake and acks)
|
|
455
|
-
*/
|
|
456
|
-
ack = 0;
|
|
457
|
-
/**
|
|
458
|
-
* The grace period between when the inner connection is disconnected
|
|
459
|
-
* and when we should consider the entire session disconnected.
|
|
460
|
-
*/
|
|
461
|
-
disconnectionGrace;
|
|
462
|
-
/**
|
|
463
|
-
* Number of heartbeats we've sent without a response.
|
|
464
|
-
*/
|
|
465
|
-
heartbeatMisses;
|
|
466
|
-
/**
|
|
467
|
-
* The interval for sending heartbeats.
|
|
468
|
-
*/
|
|
469
|
-
heartbeat;
|
|
470
|
-
log;
|
|
471
|
-
constructor(conn, from, to, options, propagationCtx) {
|
|
472
|
-
this.id = `session-${nanoid(12)}`;
|
|
473
|
-
this.options = options;
|
|
474
|
-
this.from = from;
|
|
475
|
-
this.to = to;
|
|
476
|
-
this.connection = conn;
|
|
477
|
-
this.codec = options.codec;
|
|
478
|
-
this.heartbeatMisses = 0;
|
|
479
|
-
this.heartbeat = setInterval(
|
|
480
|
-
() => this.sendHeartbeat(),
|
|
481
|
-
options.heartbeatIntervalMs
|
|
482
|
-
);
|
|
483
|
-
this.telemetry = createSessionTelemetryInfo(this, propagationCtx);
|
|
484
|
-
}
|
|
485
|
-
bindLogger(log) {
|
|
486
|
-
this.log = log;
|
|
487
|
-
}
|
|
488
|
-
get loggingMetadata() {
|
|
489
|
-
const spanContext = this.telemetry.span.spanContext();
|
|
490
|
-
return {
|
|
491
|
-
clientId: this.from,
|
|
492
|
-
connectedTo: this.to,
|
|
493
|
-
sessionId: this.id,
|
|
494
|
-
connId: this.connection?.id,
|
|
495
|
-
telemetry: {
|
|
496
|
-
traceId: spanContext.traceId,
|
|
497
|
-
spanId: spanContext.spanId
|
|
498
|
-
}
|
|
499
|
-
};
|
|
500
|
-
}
|
|
501
|
-
/**
|
|
502
|
-
* Sends a message over the session's connection.
|
|
503
|
-
* If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
|
|
504
|
-
*
|
|
505
|
-
* @param msg The partial message to be sent, which will be constructed into a full message.
|
|
506
|
-
* @param addToSendBuff Whether to add the message to the send buffer for retry.
|
|
507
|
-
* @returns The full transport ID of the message that was attempted to be sent.
|
|
508
|
-
*/
|
|
509
|
-
send(msg) {
|
|
510
|
-
const fullMsg = this.constructMsg(msg);
|
|
511
|
-
this.log?.debug(`sending msg`, {
|
|
512
|
-
...this.loggingMetadata,
|
|
513
|
-
transportMessage: fullMsg
|
|
514
|
-
});
|
|
515
|
-
if (this.connection) {
|
|
516
|
-
const ok = this.connection.send(this.codec.toBuffer(fullMsg));
|
|
517
|
-
if (ok)
|
|
518
|
-
return fullMsg.id;
|
|
519
|
-
this.log?.info(
|
|
520
|
-
`failed to send msg to ${fullMsg.to}, connection is probably dead`,
|
|
521
|
-
{
|
|
522
|
-
...this.loggingMetadata,
|
|
523
|
-
transportMessage: fullMsg
|
|
524
|
-
}
|
|
525
|
-
);
|
|
526
|
-
} else {
|
|
527
|
-
this.log?.debug(
|
|
528
|
-
`buffering msg to ${fullMsg.to}, connection not ready yet`,
|
|
529
|
-
{ ...this.loggingMetadata, transportMessage: fullMsg }
|
|
530
|
-
);
|
|
531
|
-
}
|
|
532
|
-
return fullMsg.id;
|
|
533
|
-
}
|
|
534
|
-
sendHeartbeat() {
|
|
535
|
-
const misses = this.heartbeatMisses;
|
|
536
|
-
const missDuration = misses * this.options.heartbeatIntervalMs;
|
|
537
|
-
if (misses > this.options.heartbeatsUntilDead) {
|
|
538
|
-
if (this.connection) {
|
|
539
|
-
this.log?.info(
|
|
540
|
-
`closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
|
|
541
|
-
this.loggingMetadata
|
|
542
|
-
);
|
|
543
|
-
this.telemetry.span.addEvent("closing connection due to inactivity");
|
|
544
|
-
this.closeStaleConnection();
|
|
545
|
-
}
|
|
546
|
-
return;
|
|
547
|
-
}
|
|
548
|
-
this.send({
|
|
549
|
-
streamId: "heartbeat",
|
|
550
|
-
controlFlags: 1 /* AckBit */,
|
|
551
|
-
payload: {
|
|
552
|
-
type: "ACK"
|
|
553
|
-
}
|
|
554
|
-
});
|
|
555
|
-
this.heartbeatMisses++;
|
|
556
|
-
}
|
|
557
|
-
resetBufferedMessages() {
|
|
558
|
-
this.sendBuffer = [];
|
|
559
|
-
this.seq = 0;
|
|
560
|
-
this.ack = 0;
|
|
561
|
-
}
|
|
562
|
-
sendBufferedMessages(conn) {
|
|
563
|
-
this.log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
|
|
564
|
-
...this.loggingMetadata,
|
|
565
|
-
connId: conn.id
|
|
566
|
-
});
|
|
567
|
-
for (const msg of this.sendBuffer) {
|
|
568
|
-
this.log?.debug(`resending msg`, {
|
|
569
|
-
...this.loggingMetadata,
|
|
570
|
-
transportMessage: msg,
|
|
571
|
-
connId: conn.id
|
|
572
|
-
});
|
|
573
|
-
const ok = conn.send(this.codec.toBuffer(msg));
|
|
574
|
-
if (!ok) {
|
|
575
|
-
const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;
|
|
576
|
-
conn.telemetry?.span.setStatus({
|
|
577
|
-
code: import_api2.SpanStatusCode.ERROR,
|
|
578
|
-
message: errMsg
|
|
579
|
-
});
|
|
580
|
-
this.log?.error(errMsg, {
|
|
581
|
-
...this.loggingMetadata,
|
|
582
|
-
transportMessage: msg,
|
|
583
|
-
connId: conn.id,
|
|
584
|
-
tags: ["invariant-violation"]
|
|
585
|
-
});
|
|
586
|
-
conn.close();
|
|
587
|
-
return;
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
updateBookkeeping(ack, seq) {
|
|
592
|
-
if (seq + 1 < this.ack) {
|
|
593
|
-
this.log?.error(`received stale seq ${seq} + 1 < ${this.ack}`, {
|
|
594
|
-
...this.loggingMetadata,
|
|
595
|
-
tags: ["invariant-violation"]
|
|
596
|
-
});
|
|
597
|
-
return;
|
|
598
|
-
}
|
|
599
|
-
this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
|
|
600
|
-
this.ack = seq + 1;
|
|
601
|
-
}
|
|
602
|
-
closeStaleConnection(conn) {
|
|
603
|
-
if (this.connection === void 0 || this.connection === conn)
|
|
604
|
-
return;
|
|
605
|
-
this.log?.info(
|
|
606
|
-
`closing old inner connection from session to ${this.to}`,
|
|
607
|
-
this.loggingMetadata
|
|
608
|
-
);
|
|
609
|
-
this.connection.close();
|
|
610
|
-
this.connection = void 0;
|
|
611
|
-
}
|
|
612
|
-
replaceWithNewConnection(newConn, isTransparentReconnect) {
|
|
613
|
-
this.closeStaleConnection(newConn);
|
|
614
|
-
this.cancelGrace();
|
|
615
|
-
if (isTransparentReconnect) {
|
|
616
|
-
this.sendBufferedMessages(newConn);
|
|
617
|
-
}
|
|
618
|
-
this.connection = newConn;
|
|
619
|
-
this.handshakingConnection = void 0;
|
|
620
|
-
}
|
|
621
|
-
replaceWithNewHandshakingConnection(newConn) {
|
|
622
|
-
this.handshakingConnection = newConn;
|
|
623
|
-
}
|
|
624
|
-
beginGrace(cb) {
|
|
625
|
-
this.log?.info(
|
|
626
|
-
`starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
|
|
627
|
-
this.loggingMetadata
|
|
628
|
-
);
|
|
629
|
-
this.cancelGrace();
|
|
630
|
-
this.disconnectionGrace = setTimeout(() => {
|
|
631
|
-
this.log?.info(
|
|
632
|
-
`grace period for ${this.to} elapsed`,
|
|
633
|
-
this.loggingMetadata
|
|
634
|
-
);
|
|
635
|
-
cb();
|
|
636
|
-
}, this.options.sessionDisconnectGraceMs);
|
|
637
|
-
}
|
|
638
|
-
// called on reconnect of the underlying session
|
|
639
|
-
cancelGrace() {
|
|
640
|
-
this.heartbeatMisses = 0;
|
|
641
|
-
clearTimeout(this.disconnectionGrace);
|
|
642
|
-
this.disconnectionGrace = void 0;
|
|
643
|
-
}
|
|
644
|
-
/**
|
|
645
|
-
* Used to close the handshaking connection, if set.
|
|
646
|
-
*/
|
|
647
|
-
closeHandshakingConnection(expectedHandshakingConn) {
|
|
648
|
-
if (this.handshakingConnection === void 0)
|
|
649
|
-
return;
|
|
650
|
-
if (expectedHandshakingConn !== void 0 && this.handshakingConnection === expectedHandshakingConn) {
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
this.handshakingConnection.close();
|
|
654
|
-
this.handshakingConnection = void 0;
|
|
655
|
-
}
|
|
656
|
-
// closed when we want to discard the whole session
|
|
657
|
-
// (i.e. shutdown or session disconnect)
|
|
658
|
-
close() {
|
|
659
|
-
this.closeStaleConnection();
|
|
660
|
-
this.cancelGrace();
|
|
661
|
-
this.resetBufferedMessages();
|
|
662
|
-
clearInterval(this.heartbeat);
|
|
663
|
-
}
|
|
664
|
-
get connected() {
|
|
665
|
-
return this.connection !== void 0;
|
|
666
|
-
}
|
|
667
|
-
get nextExpectedAck() {
|
|
668
|
-
return this.seq;
|
|
669
|
-
}
|
|
670
|
-
get nextExpectedSeq() {
|
|
671
|
-
return this.ack;
|
|
672
|
-
}
|
|
673
|
-
/**
|
|
674
|
-
* Check that the peer's next expected seq number matches something that is in our send buffer
|
|
675
|
-
* _or_ matches our actual next seq.
|
|
676
|
-
*/
|
|
677
|
-
nextExpectedSeqInRange(nextExpectedSeq) {
|
|
678
|
-
for (const msg of this.sendBuffer) {
|
|
679
|
-
if (nextExpectedSeq === msg.seq) {
|
|
680
|
-
return true;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
return nextExpectedSeq === this.seq;
|
|
684
|
-
}
|
|
685
|
-
// This is only used in tests to make the session misbehave.
|
|
686
|
-
/* @internal */
|
|
687
|
-
advanceAckForTesting(by) {
|
|
688
|
-
this.ack += by;
|
|
689
|
-
}
|
|
690
|
-
constructMsg(partialMsg) {
|
|
691
|
-
const msg = {
|
|
692
|
-
...partialMsg,
|
|
693
|
-
id: unsafeId(),
|
|
694
|
-
to: this.to,
|
|
695
|
-
from: this.from,
|
|
696
|
-
seq: this.seq,
|
|
697
|
-
ack: this.ack
|
|
698
|
-
};
|
|
699
|
-
this.seq++;
|
|
700
|
-
this.sendBuffer.push(msg);
|
|
701
|
-
return msg;
|
|
702
|
-
}
|
|
703
|
-
inspectSendBuffer() {
|
|
704
|
-
return this.sendBuffer;
|
|
705
|
-
}
|
|
706
|
-
};
|
|
707
|
-
|
|
708
515
|
// codec/json.ts
|
|
709
516
|
var encoder = new TextEncoder();
|
|
710
517
|
var decoder = new TextDecoder();
|
|
@@ -762,6 +569,8 @@ var defaultTransportOptions = {
|
|
|
762
569
|
heartbeatIntervalMs: 1e3,
|
|
763
570
|
heartbeatsUntilDead: 2,
|
|
764
571
|
sessionDisconnectGraceMs: 5e3,
|
|
572
|
+
connectionTimeoutMs: 2e3,
|
|
573
|
+
handshakeTimeoutMs: 1e3,
|
|
765
574
|
codec: NaiveJsonCodec
|
|
766
575
|
};
|
|
767
576
|
var defaultConnectionRetryOptions = {
|
|
@@ -779,6 +588,609 @@ var defaultServerTransportOptions = {
|
|
|
779
588
|
...defaultTransportOptions
|
|
780
589
|
};
|
|
781
590
|
|
|
591
|
+
// transport/sessionStateMachine/common.ts
|
|
592
|
+
var import_value = require("@sinclair/typebox/value");
|
|
593
|
+
var ERR_CONSUMED = `session state has been consumed and is no longer valid`;
|
|
594
|
+
var StateMachineState = class {
|
|
595
|
+
/*
|
|
596
|
+
* Whether this state has been consumed
|
|
597
|
+
* and we've moved on to another state
|
|
598
|
+
*/
|
|
599
|
+
_isConsumed;
|
|
600
|
+
close() {
|
|
601
|
+
this._handleClose();
|
|
602
|
+
}
|
|
603
|
+
constructor() {
|
|
604
|
+
this._isConsumed = false;
|
|
605
|
+
return new Proxy(this, {
|
|
606
|
+
get(target, prop) {
|
|
607
|
+
if (prop === "_isConsumed" || prop === "id" || prop === "state") {
|
|
608
|
+
return Reflect.get(target, prop);
|
|
609
|
+
}
|
|
610
|
+
if (prop === "_handleStateExit") {
|
|
611
|
+
return () => {
|
|
612
|
+
target._isConsumed = true;
|
|
613
|
+
target._handleStateExit();
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
if (prop === "_handleClose") {
|
|
617
|
+
return () => {
|
|
618
|
+
target._handleStateExit();
|
|
619
|
+
target._handleClose();
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
if (target._isConsumed) {
|
|
623
|
+
throw new Error(
|
|
624
|
+
`${ERR_CONSUMED}: getting ${prop.toString()} on consumed state`
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
return Reflect.get(target, prop);
|
|
628
|
+
},
|
|
629
|
+
set(target, prop, value) {
|
|
630
|
+
if (target._isConsumed) {
|
|
631
|
+
throw new Error(
|
|
632
|
+
`${ERR_CONSUMED}: setting ${prop.toString()} on consumed state`
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
return Reflect.set(target, prop, value);
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
var CommonSession = class extends StateMachineState {
|
|
641
|
+
from;
|
|
642
|
+
options;
|
|
643
|
+
log;
|
|
644
|
+
constructor(from, options, log) {
|
|
645
|
+
super();
|
|
646
|
+
this.from = from;
|
|
647
|
+
this.options = options;
|
|
648
|
+
this.log = log;
|
|
649
|
+
}
|
|
650
|
+
parseMsg(msg) {
|
|
651
|
+
const parsedMsg = this.options.codec.fromBuffer(msg);
|
|
652
|
+
if (parsedMsg === null) {
|
|
653
|
+
const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));
|
|
654
|
+
this.log?.error(
|
|
655
|
+
`received malformed msg: ${decodedBuffer}`,
|
|
656
|
+
this.loggingMetadata
|
|
657
|
+
);
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
661
|
+
this.log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
|
|
662
|
+
...this.loggingMetadata,
|
|
663
|
+
validationErrors: [
|
|
664
|
+
...import_value.Value.Errors(OpaqueTransportMessageSchema, parsedMsg)
|
|
665
|
+
]
|
|
666
|
+
});
|
|
667
|
+
return null;
|
|
668
|
+
}
|
|
669
|
+
return parsedMsg;
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
var IdentifiedSession = class extends CommonSession {
|
|
673
|
+
id;
|
|
674
|
+
telemetry;
|
|
675
|
+
to;
|
|
676
|
+
/**
|
|
677
|
+
* Index of the message we will send next (excluding handshake)
|
|
678
|
+
*/
|
|
679
|
+
seq;
|
|
680
|
+
/**
|
|
681
|
+
* Number of unique messages we've received this session (excluding handshake)
|
|
682
|
+
*/
|
|
683
|
+
ack;
|
|
684
|
+
sendBuffer;
|
|
685
|
+
constructor(id, from, to, seq, ack, sendBuffer, telemetry, options, log) {
|
|
686
|
+
super(from, options, log);
|
|
687
|
+
this.id = id;
|
|
688
|
+
this.to = to;
|
|
689
|
+
this.seq = seq;
|
|
690
|
+
this.ack = ack;
|
|
691
|
+
this.sendBuffer = sendBuffer;
|
|
692
|
+
this.telemetry = telemetry;
|
|
693
|
+
this.log = log;
|
|
694
|
+
}
|
|
695
|
+
get loggingMetadata() {
|
|
696
|
+
const spanContext = this.telemetry.span.spanContext();
|
|
697
|
+
return {
|
|
698
|
+
clientId: this.from,
|
|
699
|
+
connectedTo: this.to,
|
|
700
|
+
sessionId: this.id,
|
|
701
|
+
telemetry: {
|
|
702
|
+
traceId: spanContext.traceId,
|
|
703
|
+
spanId: spanContext.spanId
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
constructMsg(partialMsg) {
|
|
708
|
+
const msg = {
|
|
709
|
+
...partialMsg,
|
|
710
|
+
id: generateId(),
|
|
711
|
+
to: this.to,
|
|
712
|
+
from: this.from,
|
|
713
|
+
seq: this.seq,
|
|
714
|
+
ack: this.ack
|
|
715
|
+
};
|
|
716
|
+
this.seq++;
|
|
717
|
+
return msg;
|
|
718
|
+
}
|
|
719
|
+
nextSeq() {
|
|
720
|
+
return this.sendBuffer.length > 0 ? this.sendBuffer[0].seq : this.seq;
|
|
721
|
+
}
|
|
722
|
+
send(msg) {
|
|
723
|
+
const constructedMsg = this.constructMsg(msg);
|
|
724
|
+
this.sendBuffer.push(constructedMsg);
|
|
725
|
+
return constructedMsg.id;
|
|
726
|
+
}
|
|
727
|
+
_handleStateExit() {
|
|
728
|
+
}
|
|
729
|
+
_handleClose() {
|
|
730
|
+
this.sendBuffer.length = 0;
|
|
731
|
+
this.telemetry.span.end();
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
// transport/sessionStateMachine/SessionConnecting.ts
|
|
736
|
+
var SessionConnecting = class extends IdentifiedSession {
|
|
737
|
+
state = "Connecting" /* Connecting */;
|
|
738
|
+
connPromise;
|
|
739
|
+
listeners;
|
|
740
|
+
connectionTimeout;
|
|
741
|
+
constructor(connPromise, listeners, ...args) {
|
|
742
|
+
super(...args);
|
|
743
|
+
this.connPromise = connPromise;
|
|
744
|
+
this.listeners = listeners;
|
|
745
|
+
this.connectionTimeout = setTimeout(() => {
|
|
746
|
+
listeners.onConnectionTimeout();
|
|
747
|
+
}, this.options.connectionTimeoutMs);
|
|
748
|
+
connPromise.then(
|
|
749
|
+
(conn) => {
|
|
750
|
+
if (this._isConsumed)
|
|
751
|
+
return;
|
|
752
|
+
listeners.onConnectionEstablished(conn);
|
|
753
|
+
},
|
|
754
|
+
(err) => {
|
|
755
|
+
if (this._isConsumed)
|
|
756
|
+
return;
|
|
757
|
+
listeners.onConnectionFailed(err);
|
|
758
|
+
}
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
// close a pending connection if it resolves, ignore errors if the promise
|
|
762
|
+
// ends up rejected anyways
|
|
763
|
+
bestEffortClose() {
|
|
764
|
+
void this.connPromise.then((conn) => conn.close()).catch(() => {
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
_handleStateExit() {
|
|
768
|
+
super._handleStateExit();
|
|
769
|
+
clearTimeout(this.connectionTimeout);
|
|
770
|
+
this.connectionTimeout = void 0;
|
|
771
|
+
}
|
|
772
|
+
_handleClose() {
|
|
773
|
+
this.bestEffortClose();
|
|
774
|
+
super._handleClose();
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
// transport/sessionStateMachine/SessionNoConnection.ts
|
|
779
|
+
var SessionNoConnection = class extends IdentifiedSession {
|
|
780
|
+
state = "NoConnection" /* NoConnection */;
|
|
781
|
+
listeners;
|
|
782
|
+
gracePeriodTimeout;
|
|
783
|
+
constructor(listeners, ...args) {
|
|
784
|
+
super(...args);
|
|
785
|
+
this.listeners = listeners;
|
|
786
|
+
this.gracePeriodTimeout = setTimeout(() => {
|
|
787
|
+
this.listeners.onSessionGracePeriodElapsed();
|
|
788
|
+
}, this.options.sessionDisconnectGraceMs);
|
|
789
|
+
}
|
|
790
|
+
_handleClose() {
|
|
791
|
+
super._handleClose();
|
|
792
|
+
}
|
|
793
|
+
_handleStateExit() {
|
|
794
|
+
super._handleStateExit();
|
|
795
|
+
if (this.gracePeriodTimeout) {
|
|
796
|
+
clearTimeout(this.gracePeriodTimeout);
|
|
797
|
+
this.gracePeriodTimeout = void 0;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
|
|
802
|
+
// transport/sessionStateMachine/SessionWaitingForHandshake.ts
|
|
803
|
+
var SessionWaitingForHandshake = class extends CommonSession {
|
|
804
|
+
state = "WaitingForHandshake" /* WaitingForHandshake */;
|
|
805
|
+
conn;
|
|
806
|
+
listeners;
|
|
807
|
+
handshakeTimeout;
|
|
808
|
+
constructor(conn, listeners, ...args) {
|
|
809
|
+
super(...args);
|
|
810
|
+
this.conn = conn;
|
|
811
|
+
this.listeners = listeners;
|
|
812
|
+
this.handshakeTimeout = setTimeout(() => {
|
|
813
|
+
listeners.onHandshakeTimeout();
|
|
814
|
+
}, this.options.handshakeTimeoutMs);
|
|
815
|
+
this.conn.addDataListener(this.onHandshakeData);
|
|
816
|
+
this.conn.addErrorListener(listeners.onConnectionErrored);
|
|
817
|
+
this.conn.addCloseListener(listeners.onConnectionClosed);
|
|
818
|
+
}
|
|
819
|
+
onHandshakeData = (msg) => {
|
|
820
|
+
const parsedMsg = this.parseMsg(msg);
|
|
821
|
+
if (parsedMsg === null) {
|
|
822
|
+
this.listeners.onInvalidHandshake("could not parse message");
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
this.listeners.onHandshake(parsedMsg);
|
|
826
|
+
};
|
|
827
|
+
get loggingMetadata() {
|
|
828
|
+
return {
|
|
829
|
+
clientId: this.from,
|
|
830
|
+
connId: this.conn.id
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
sendHandshake(msg) {
|
|
834
|
+
return this.conn.send(this.options.codec.toBuffer(msg));
|
|
835
|
+
}
|
|
836
|
+
_handleStateExit() {
|
|
837
|
+
this.conn.removeDataListener(this.onHandshakeData);
|
|
838
|
+
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
|
|
839
|
+
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
|
|
840
|
+
clearTimeout(this.handshakeTimeout);
|
|
841
|
+
this.handshakeTimeout = void 0;
|
|
842
|
+
}
|
|
843
|
+
_handleClose() {
|
|
844
|
+
this.conn.close();
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
// transport/sessionStateMachine/SessionHandshaking.ts
|
|
849
|
+
var SessionHandshaking = class extends IdentifiedSession {
|
|
850
|
+
state = "Handshaking" /* Handshaking */;
|
|
851
|
+
conn;
|
|
852
|
+
listeners;
|
|
853
|
+
handshakeTimeout;
|
|
854
|
+
constructor(conn, listeners, ...args) {
|
|
855
|
+
super(...args);
|
|
856
|
+
this.conn = conn;
|
|
857
|
+
this.listeners = listeners;
|
|
858
|
+
this.handshakeTimeout = setTimeout(() => {
|
|
859
|
+
listeners.onHandshakeTimeout();
|
|
860
|
+
}, this.options.handshakeTimeoutMs);
|
|
861
|
+
this.conn.addDataListener(this.onHandshakeData);
|
|
862
|
+
this.conn.addErrorListener(listeners.onConnectionErrored);
|
|
863
|
+
this.conn.addCloseListener(listeners.onConnectionClosed);
|
|
864
|
+
}
|
|
865
|
+
onHandshakeData = (msg) => {
|
|
866
|
+
const parsedMsg = this.parseMsg(msg);
|
|
867
|
+
if (parsedMsg === null) {
|
|
868
|
+
this.listeners.onInvalidHandshake("could not parse message");
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
this.listeners.onHandshake(parsedMsg);
|
|
872
|
+
};
|
|
873
|
+
sendHandshake(msg) {
|
|
874
|
+
return this.conn.send(this.options.codec.toBuffer(msg));
|
|
875
|
+
}
|
|
876
|
+
_handleStateExit() {
|
|
877
|
+
super._handleStateExit();
|
|
878
|
+
this.conn.removeDataListener(this.onHandshakeData);
|
|
879
|
+
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
|
|
880
|
+
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
|
|
881
|
+
clearTimeout(this.handshakeTimeout);
|
|
882
|
+
}
|
|
883
|
+
_handleClose() {
|
|
884
|
+
super._handleClose();
|
|
885
|
+
this.conn.close();
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
|
|
889
|
+
// transport/sessionStateMachine/SessionConnected.ts
|
|
890
|
+
var import_api2 = require("@opentelemetry/api");
|
|
891
|
+
var SessionConnected = class extends IdentifiedSession {
|
|
892
|
+
state = "Connected" /* Connected */;
|
|
893
|
+
conn;
|
|
894
|
+
listeners;
|
|
895
|
+
heartbeatHandle;
|
|
896
|
+
heartbeatMisses = 0;
|
|
897
|
+
get isActivelyHeartbeating() {
|
|
898
|
+
return this.heartbeatHandle !== void 0;
|
|
899
|
+
}
|
|
900
|
+
updateBookkeeping(ack, seq) {
|
|
901
|
+
this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
|
|
902
|
+
this.ack = seq + 1;
|
|
903
|
+
this.heartbeatMisses = 0;
|
|
904
|
+
}
|
|
905
|
+
send(msg) {
|
|
906
|
+
const constructedMsg = this.constructMsg(msg);
|
|
907
|
+
this.sendBuffer.push(constructedMsg);
|
|
908
|
+
this.conn.send(this.options.codec.toBuffer(constructedMsg));
|
|
909
|
+
return constructedMsg.id;
|
|
910
|
+
}
|
|
911
|
+
constructor(conn, listeners, ...args) {
|
|
912
|
+
super(...args);
|
|
913
|
+
this.conn = conn;
|
|
914
|
+
this.listeners = listeners;
|
|
915
|
+
this.conn.addDataListener(this.onMessageData);
|
|
916
|
+
this.conn.addCloseListener(listeners.onConnectionClosed);
|
|
917
|
+
this.conn.addErrorListener(listeners.onConnectionErrored);
|
|
918
|
+
if (this.sendBuffer.length > 0) {
|
|
919
|
+
this.log?.debug(
|
|
920
|
+
`sending ${this.sendBuffer.length} buffered messages`,
|
|
921
|
+
this.loggingMetadata
|
|
922
|
+
);
|
|
923
|
+
}
|
|
924
|
+
for (const msg of this.sendBuffer) {
|
|
925
|
+
conn.send(this.options.codec.toBuffer(msg));
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
startActiveHeartbeat() {
|
|
929
|
+
this.heartbeatHandle = setInterval(() => {
|
|
930
|
+
const misses = this.heartbeatMisses;
|
|
931
|
+
const missDuration = misses * this.options.heartbeatIntervalMs;
|
|
932
|
+
if (misses >= this.options.heartbeatsUntilDead) {
|
|
933
|
+
this.log?.info(
|
|
934
|
+
`closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
|
|
935
|
+
this.loggingMetadata
|
|
936
|
+
);
|
|
937
|
+
this.telemetry.span.addEvent("closing connection due to inactivity");
|
|
938
|
+
this.conn.close();
|
|
939
|
+
clearInterval(this.heartbeatHandle);
|
|
940
|
+
this.heartbeatHandle = void 0;
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
this.sendHeartbeat();
|
|
944
|
+
this.heartbeatMisses++;
|
|
945
|
+
}, this.options.heartbeatIntervalMs);
|
|
946
|
+
}
|
|
947
|
+
sendHeartbeat() {
|
|
948
|
+
this.send({
|
|
949
|
+
streamId: "heartbeat",
|
|
950
|
+
controlFlags: 1 /* AckBit */,
|
|
951
|
+
payload: {
|
|
952
|
+
type: "ACK"
|
|
953
|
+
}
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
onMessageData = (msg) => {
|
|
957
|
+
const parsedMsg = this.parseMsg(msg);
|
|
958
|
+
if (parsedMsg === null)
|
|
959
|
+
return;
|
|
960
|
+
if (parsedMsg.seq !== this.ack) {
|
|
961
|
+
if (parsedMsg.seq < this.ack) {
|
|
962
|
+
this.log?.debug(
|
|
963
|
+
`received duplicate msg (got seq: ${parsedMsg.seq}, wanted seq: ${this.ack}), discarding`,
|
|
964
|
+
{
|
|
965
|
+
...this.loggingMetadata,
|
|
966
|
+
transportMessage: parsedMsg
|
|
967
|
+
}
|
|
968
|
+
);
|
|
969
|
+
} else {
|
|
970
|
+
const reason = `received out-of-order msg (got seq: ${parsedMsg.seq}, wanted seq: ${this.ack})`;
|
|
971
|
+
this.log?.error(reason, {
|
|
972
|
+
...this.loggingMetadata,
|
|
973
|
+
transportMessage: parsedMsg,
|
|
974
|
+
tags: ["invariant-violation"]
|
|
975
|
+
});
|
|
976
|
+
this.telemetry.span.setStatus({
|
|
977
|
+
code: import_api2.SpanStatusCode.ERROR,
|
|
978
|
+
message: reason
|
|
979
|
+
});
|
|
980
|
+
this.listeners.onInvalidMessage(reason);
|
|
981
|
+
}
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
this.log?.debug(`received msg`, {
|
|
985
|
+
...this.loggingMetadata,
|
|
986
|
+
transportMessage: parsedMsg
|
|
987
|
+
});
|
|
988
|
+
this.updateBookkeeping(parsedMsg.ack, parsedMsg.seq);
|
|
989
|
+
if (!isAck(parsedMsg.controlFlags)) {
|
|
990
|
+
this.listeners.onMessage(parsedMsg);
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
this.log?.debug(`discarding msg (ack bit set)`, {
|
|
994
|
+
...this.loggingMetadata,
|
|
995
|
+
transportMessage: parsedMsg
|
|
996
|
+
});
|
|
997
|
+
if (!this.isActivelyHeartbeating) {
|
|
998
|
+
this.sendHeartbeat();
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
_handleStateExit() {
|
|
1002
|
+
super._handleStateExit();
|
|
1003
|
+
this.conn.removeDataListener(this.onMessageData);
|
|
1004
|
+
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
|
|
1005
|
+
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
|
|
1006
|
+
clearInterval(this.heartbeatHandle);
|
|
1007
|
+
this.heartbeatHandle = void 0;
|
|
1008
|
+
}
|
|
1009
|
+
_handleClose() {
|
|
1010
|
+
super._handleClose();
|
|
1011
|
+
this.conn.close();
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
|
|
1015
|
+
// transport/sessionStateMachine/transitions.ts
|
|
1016
|
+
function inheritSharedSession(session) {
|
|
1017
|
+
return [
|
|
1018
|
+
session.id,
|
|
1019
|
+
session.from,
|
|
1020
|
+
session.to,
|
|
1021
|
+
session.seq,
|
|
1022
|
+
session.ack,
|
|
1023
|
+
session.sendBuffer,
|
|
1024
|
+
session.telemetry,
|
|
1025
|
+
session.options,
|
|
1026
|
+
session.log
|
|
1027
|
+
];
|
|
1028
|
+
}
|
|
1029
|
+
var SessionStateGraph = {
|
|
1030
|
+
entrypoints: {
|
|
1031
|
+
NoConnection(to, from, listeners, options, log) {
|
|
1032
|
+
const id = `session-${generateId()}`;
|
|
1033
|
+
const telemetry = createSessionTelemetryInfo(id, to, from);
|
|
1034
|
+
const sendBuffer = [];
|
|
1035
|
+
const session = new SessionNoConnection(
|
|
1036
|
+
listeners,
|
|
1037
|
+
id,
|
|
1038
|
+
from,
|
|
1039
|
+
to,
|
|
1040
|
+
0,
|
|
1041
|
+
0,
|
|
1042
|
+
sendBuffer,
|
|
1043
|
+
telemetry,
|
|
1044
|
+
options,
|
|
1045
|
+
log
|
|
1046
|
+
);
|
|
1047
|
+
session.log?.info(`session ${session.id} created in NoConnection state`, {
|
|
1048
|
+
...session.loggingMetadata,
|
|
1049
|
+
tags: ["state-transition"]
|
|
1050
|
+
});
|
|
1051
|
+
return session;
|
|
1052
|
+
},
|
|
1053
|
+
WaitingForHandshake(from, conn, listeners, options, log) {
|
|
1054
|
+
const session = new SessionWaitingForHandshake(
|
|
1055
|
+
conn,
|
|
1056
|
+
listeners,
|
|
1057
|
+
from,
|
|
1058
|
+
options,
|
|
1059
|
+
log
|
|
1060
|
+
);
|
|
1061
|
+
session.log?.info(`session created in WaitingForHandshake state`, {
|
|
1062
|
+
...session.loggingMetadata,
|
|
1063
|
+
tags: ["state-transition"]
|
|
1064
|
+
});
|
|
1065
|
+
return session;
|
|
1066
|
+
}
|
|
1067
|
+
},
|
|
1068
|
+
// All of the transitions 'move'/'consume' the old session and return a new one.
|
|
1069
|
+
// After a session is transitioned, any usage of the old session will throw.
|
|
1070
|
+
transition: {
|
|
1071
|
+
// happy path transitions
|
|
1072
|
+
NoConnectionToConnecting(oldSession, connPromise, listeners) {
|
|
1073
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
1074
|
+
oldSession._handleStateExit();
|
|
1075
|
+
const session = new SessionConnecting(
|
|
1076
|
+
connPromise,
|
|
1077
|
+
listeners,
|
|
1078
|
+
...carriedState
|
|
1079
|
+
);
|
|
1080
|
+
session.log?.info(
|
|
1081
|
+
`session ${session.id} transition from NoConnection to Connecting`,
|
|
1082
|
+
{
|
|
1083
|
+
...session.loggingMetadata,
|
|
1084
|
+
tags: ["state-transition"]
|
|
1085
|
+
}
|
|
1086
|
+
);
|
|
1087
|
+
return session;
|
|
1088
|
+
},
|
|
1089
|
+
ConnectingToHandshaking(oldSession, conn, listeners) {
|
|
1090
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
1091
|
+
oldSession._handleStateExit();
|
|
1092
|
+
const session = new SessionHandshaking(conn, listeners, ...carriedState);
|
|
1093
|
+
session.log?.info(
|
|
1094
|
+
`session ${session.id} transition from Connecting to Handshaking`,
|
|
1095
|
+
{
|
|
1096
|
+
...session.loggingMetadata,
|
|
1097
|
+
tags: ["state-transition"]
|
|
1098
|
+
}
|
|
1099
|
+
);
|
|
1100
|
+
return session;
|
|
1101
|
+
},
|
|
1102
|
+
HandshakingToConnected(oldSession, listeners) {
|
|
1103
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
1104
|
+
const conn = oldSession.conn;
|
|
1105
|
+
oldSession._handleStateExit();
|
|
1106
|
+
const session = new SessionConnected(conn, listeners, ...carriedState);
|
|
1107
|
+
session.log?.info(
|
|
1108
|
+
`session ${session.id} transition from Handshaking to Connected`,
|
|
1109
|
+
{
|
|
1110
|
+
...session.loggingMetadata,
|
|
1111
|
+
tags: ["state-transition"]
|
|
1112
|
+
}
|
|
1113
|
+
);
|
|
1114
|
+
return session;
|
|
1115
|
+
},
|
|
1116
|
+
WaitingForHandshakeToConnected(pendingSession, oldSession, sessionId, to, propagationCtx, listeners) {
|
|
1117
|
+
const conn = pendingSession.conn;
|
|
1118
|
+
const { from, options } = pendingSession;
|
|
1119
|
+
const carriedState = oldSession ? (
|
|
1120
|
+
// old session exists, inherit state
|
|
1121
|
+
inheritSharedSession(oldSession)
|
|
1122
|
+
) : (
|
|
1123
|
+
// old session does not exist, create new state
|
|
1124
|
+
[
|
|
1125
|
+
sessionId,
|
|
1126
|
+
from,
|
|
1127
|
+
to,
|
|
1128
|
+
0,
|
|
1129
|
+
0,
|
|
1130
|
+
[],
|
|
1131
|
+
createSessionTelemetryInfo(sessionId, to, from, propagationCtx),
|
|
1132
|
+
options,
|
|
1133
|
+
pendingSession.log
|
|
1134
|
+
]
|
|
1135
|
+
);
|
|
1136
|
+
pendingSession._handleStateExit();
|
|
1137
|
+
oldSession?._handleStateExit();
|
|
1138
|
+
const session = new SessionConnected(conn, listeners, ...carriedState);
|
|
1139
|
+
session.log?.info(
|
|
1140
|
+
`session ${session.id} transition from WaitingForHandshake to Connected`,
|
|
1141
|
+
{
|
|
1142
|
+
...session.loggingMetadata,
|
|
1143
|
+
tags: ["state-transition"]
|
|
1144
|
+
}
|
|
1145
|
+
);
|
|
1146
|
+
return session;
|
|
1147
|
+
},
|
|
1148
|
+
// disconnect paths
|
|
1149
|
+
ConnectingToNoConnection(oldSession, listeners) {
|
|
1150
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
1151
|
+
oldSession.bestEffortClose();
|
|
1152
|
+
oldSession._handleStateExit();
|
|
1153
|
+
const session = new SessionNoConnection(listeners, ...carriedState);
|
|
1154
|
+
session.log?.info(
|
|
1155
|
+
`session ${session.id} transition from Connecting to NoConnection`,
|
|
1156
|
+
{
|
|
1157
|
+
...session.loggingMetadata,
|
|
1158
|
+
tags: ["state-transition"]
|
|
1159
|
+
}
|
|
1160
|
+
);
|
|
1161
|
+
return session;
|
|
1162
|
+
},
|
|
1163
|
+
HandshakingToNoConnection(oldSession, listeners) {
|
|
1164
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
1165
|
+
oldSession.conn.close();
|
|
1166
|
+
oldSession._handleStateExit();
|
|
1167
|
+
const session = new SessionNoConnection(listeners, ...carriedState);
|
|
1168
|
+
session.log?.info(
|
|
1169
|
+
`session ${session.id} transition from Handshaking to NoConnection`,
|
|
1170
|
+
{
|
|
1171
|
+
...session.loggingMetadata,
|
|
1172
|
+
tags: ["state-transition"]
|
|
1173
|
+
}
|
|
1174
|
+
);
|
|
1175
|
+
return session;
|
|
1176
|
+
},
|
|
1177
|
+
ConnectedToNoConnection(oldSession, listeners) {
|
|
1178
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
1179
|
+
oldSession.conn.close();
|
|
1180
|
+
oldSession._handleStateExit();
|
|
1181
|
+
const session = new SessionNoConnection(listeners, ...carriedState);
|
|
1182
|
+
session.log?.info(
|
|
1183
|
+
`session ${session.id} transition from Connected to NoConnection`,
|
|
1184
|
+
{
|
|
1185
|
+
...session.loggingMetadata,
|
|
1186
|
+
tags: ["state-transition"]
|
|
1187
|
+
}
|
|
1188
|
+
);
|
|
1189
|
+
return session;
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
|
|
782
1194
|
// util/testHelpers.ts
|
|
783
1195
|
function createLocalWebSocketClient(port) {
|
|
784
1196
|
const sock = new import_ws.default(`ws://localhost:${port}`);
|
|
@@ -847,18 +1259,22 @@ function catchProcError(err) {
|
|
|
847
1259
|
}
|
|
848
1260
|
var testingSessionOptions = defaultTransportOptions;
|
|
849
1261
|
function dummySession() {
|
|
850
|
-
return
|
|
851
|
-
void 0,
|
|
1262
|
+
return SessionStateGraph.entrypoints.NoConnection(
|
|
852
1263
|
"client",
|
|
853
1264
|
"server",
|
|
1265
|
+
{
|
|
1266
|
+
onSessionGracePeriodElapsed: () => {
|
|
1267
|
+
}
|
|
1268
|
+
},
|
|
854
1269
|
testingSessionOptions
|
|
855
1270
|
);
|
|
856
1271
|
}
|
|
857
1272
|
function dummyCtx(state, session, extendedContext) {
|
|
858
1273
|
return {
|
|
859
1274
|
...extendedContext,
|
|
860
|
-
from: session.from,
|
|
861
1275
|
state,
|
|
1276
|
+
sessionId: session.id,
|
|
1277
|
+
from: session.from,
|
|
862
1278
|
metadata: {},
|
|
863
1279
|
abortController: new AbortController(),
|
|
864
1280
|
clientAbortSignal: new AbortController().signal,
|
|
@@ -936,21 +1352,41 @@ function asClientUpload(state, proc, init, extendedContext, session = dummySessi
|
|
|
936
1352
|
return [inputPipe.writer, () => result];
|
|
937
1353
|
}
|
|
938
1354
|
var getUnixSocketPath = () => {
|
|
939
|
-
return
|
|
1355
|
+
return `/tmp/${(0, import_nanoid2.nanoid)()}.sock`;
|
|
940
1356
|
};
|
|
1357
|
+
function getTransportConnections(transport) {
|
|
1358
|
+
const connections = [];
|
|
1359
|
+
for (const session of transport.sessions.values()) {
|
|
1360
|
+
if (session.state === "Connected" /* Connected */) {
|
|
1361
|
+
connections.push(session.conn);
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
return connections;
|
|
1365
|
+
}
|
|
1366
|
+
function numberOfConnections(transport) {
|
|
1367
|
+
return getTransportConnections(transport).length;
|
|
1368
|
+
}
|
|
1369
|
+
function closeAllConnections(transport) {
|
|
1370
|
+
for (const conn of getTransportConnections(transport)) {
|
|
1371
|
+
conn.close();
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
941
1374
|
// Annotate the CommonJS export names for ESM import in node:
|
|
942
1375
|
0 && (module.exports = {
|
|
943
1376
|
asClientRpc,
|
|
944
1377
|
asClientStream,
|
|
945
1378
|
asClientSubscription,
|
|
946
1379
|
asClientUpload,
|
|
1380
|
+
closeAllConnections,
|
|
947
1381
|
createDummyTransportMessage,
|
|
948
1382
|
createLocalWebSocketClient,
|
|
949
1383
|
createWebSocketServer,
|
|
950
1384
|
dummySession,
|
|
951
1385
|
getIteratorFromStream,
|
|
1386
|
+
getTransportConnections,
|
|
952
1387
|
getUnixSocketPath,
|
|
953
1388
|
iterNext,
|
|
1389
|
+
numberOfConnections,
|
|
954
1390
|
onUdsServeReady,
|
|
955
1391
|
onWsServerReady,
|
|
956
1392
|
payloadToTransportMessage,
|