@replit/river 0.23.18 → 0.24.0
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 +17 -16
- package/dist/{chunk-AVL32IMG.js → chunk-AASMR3CQ.js} +20 -16
- package/dist/chunk-AASMR3CQ.js.map +1 -0
- package/dist/chunk-JA57I7MG.js +653 -0
- package/dist/chunk-JA57I7MG.js.map +1 -0
- package/dist/chunk-KX5PQRVN.js +382 -0
- package/dist/chunk-KX5PQRVN.js.map +1 -0
- package/dist/{chunk-EV5HW4IC.js → chunk-KYYB4DUR.js} +65 -53
- package/dist/chunk-KYYB4DUR.js.map +1 -0
- package/dist/chunk-NLQPPDOT.js +399 -0
- package/dist/chunk-NLQPPDOT.js.map +1 -0
- package/dist/{chunk-R2HAS3GM.js → chunk-PJGGC3LV.js} +55 -41
- package/dist/chunk-PJGGC3LV.js.map +1 -0
- package/dist/{chunk-7MJYOL32.js → chunk-RXJLI2OP.js} +15 -23
- package/dist/chunk-RXJLI2OP.js.map +1 -0
- package/dist/{chunk-6LCL2ZZF.js → chunk-TAH2GVTJ.js} +1 -1
- package/dist/chunk-TAH2GVTJ.js.map +1 -0
- package/dist/chunk-ZAT3R4CU.js +277 -0
- package/dist/chunk-ZAT3R4CU.js.map +1 -0
- package/dist/{client-5776a6bb.d.ts → client-ba0d3315.d.ts} +12 -15
- package/dist/{connection-bd35d442.d.ts → connection-c3a96d09.d.ts} +1 -5
- package/dist/connection-d33e3246.d.ts +11 -0
- package/dist/{handshake-a947c234.d.ts → handshake-cdead82a.d.ts} +148 -183
- 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-ea74cdbb.d.ts → message-e6c560fd.d.ts} +2 -2
- package/dist/router/index.cjs +104 -63
- package/dist/router/index.cjs.map +1 -1
- package/dist/router/index.d.cts +11 -10
- package/dist/router/index.d.ts +11 -10
- package/dist/router/index.js +2 -2
- package/dist/server-2ef5e6ec.d.ts +42 -0
- package/dist/{services-38b3f758.d.ts → services-e1417b33.d.ts} +3 -3
- package/dist/transport/impls/uds/client.cjs +1246 -1230
- package/dist/transport/impls/uds/client.cjs.map +1 -1
- package/dist/transport/impls/uds/client.d.cts +4 -4
- package/dist/transport/impls/uds/client.d.ts +4 -4
- 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 +1298 -1151
- 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 +976 -965
- package/dist/transport/impls/ws/client.cjs.map +1 -1
- package/dist/transport/impls/ws/client.d.cts +4 -4
- package/dist/transport/impls/ws/client.d.ts +4 -4
- 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 +1182 -1047
- 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 +1433 -1360
- package/dist/transport/index.cjs.map +1 -1
- package/dist/transport/index.d.cts +4 -4
- package/dist/transport/index.d.ts +4 -4
- package/dist/transport/index.js +9 -9
- package/dist/util/testHelpers.cjs +743 -310
- package/dist/util/testHelpers.cjs.map +1 -1
- package/dist/util/testHelpers.d.cts +9 -6
- package/dist/util/testHelpers.d.ts +9 -6
- package/dist/util/testHelpers.js +33 -10
- package/dist/util/testHelpers.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-6LCL2ZZF.js.map +0 -1
- package/dist/chunk-7MJYOL32.js.map +0 -1
- package/dist/chunk-AVL32IMG.js.map +0 -1
- package/dist/chunk-DPKOJQWF.js +0 -476
- package/dist/chunk-DPKOJQWF.js.map +0 -1
- package/dist/chunk-EV5HW4IC.js.map +0 -1
- package/dist/chunk-J6N6H2WU.js +0 -476
- package/dist/chunk-J6N6H2WU.js.map +0 -1
- package/dist/chunk-MW5JXLHY.js +0 -348
- package/dist/chunk-MW5JXLHY.js.map +0 -1
- package/dist/chunk-R2HAS3GM.js.map +0 -1
- package/dist/chunk-RJOWZIWB.js +0 -335
- package/dist/chunk-RJOWZIWB.js.map +0 -1
- package/dist/connection-df85db7e.d.ts +0 -17
- package/dist/server-53cd5b7e.d.ts +0 -24
package/dist/transport/index.cjs
CHANGED
|
@@ -25,18 +25,189 @@ __export(transport_exports, {
|
|
|
25
25
|
OpaqueTransportMessageSchema: () => OpaqueTransportMessageSchema,
|
|
26
26
|
ProtocolError: () => ProtocolError,
|
|
27
27
|
ServerTransport: () => ServerTransport,
|
|
28
|
-
|
|
28
|
+
SessionState: () => SessionState,
|
|
29
29
|
Transport: () => Transport,
|
|
30
30
|
TransportMessageSchema: () => TransportMessageSchema
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(transport_exports);
|
|
33
33
|
|
|
34
|
-
//
|
|
35
|
-
var
|
|
34
|
+
// logging/log.ts
|
|
35
|
+
var LoggingLevels = {
|
|
36
|
+
debug: -1,
|
|
37
|
+
info: 0,
|
|
38
|
+
warn: 1,
|
|
39
|
+
error: 2
|
|
40
|
+
};
|
|
41
|
+
var cleanedLogFn = (log) => {
|
|
42
|
+
return (msg, metadata) => {
|
|
43
|
+
if (!metadata?.transportMessage) {
|
|
44
|
+
log(msg, metadata);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const { payload, ...rest } = metadata.transportMessage;
|
|
48
|
+
metadata.transportMessage = rest;
|
|
49
|
+
log(msg, metadata);
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
var BaseLogger = class {
|
|
53
|
+
minLevel;
|
|
54
|
+
output;
|
|
55
|
+
constructor(output, minLevel = "info") {
|
|
56
|
+
this.minLevel = minLevel;
|
|
57
|
+
this.output = output;
|
|
58
|
+
}
|
|
59
|
+
debug(msg, metadata) {
|
|
60
|
+
if (LoggingLevels[this.minLevel] <= LoggingLevels.debug) {
|
|
61
|
+
this.output(msg, metadata ?? {}, "debug");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
info(msg, metadata) {
|
|
65
|
+
if (LoggingLevels[this.minLevel] <= LoggingLevels.info) {
|
|
66
|
+
this.output(msg, metadata ?? {}, "info");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
warn(msg, metadata) {
|
|
70
|
+
if (LoggingLevels[this.minLevel] <= LoggingLevels.warn) {
|
|
71
|
+
this.output(msg, metadata ?? {}, "warn");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
error(msg, metadata) {
|
|
75
|
+
if (LoggingLevels[this.minLevel] <= LoggingLevels.error) {
|
|
76
|
+
this.output(msg, metadata ?? {}, "error");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var createLogProxy = (log) => ({
|
|
81
|
+
debug: cleanedLogFn(log.debug.bind(log)),
|
|
82
|
+
info: cleanedLogFn(log.info.bind(log)),
|
|
83
|
+
warn: cleanedLogFn(log.warn.bind(log)),
|
|
84
|
+
error: cleanedLogFn(log.error.bind(log))
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// transport/events.ts
|
|
88
|
+
var ProtocolError = {
|
|
89
|
+
RetriesExceeded: "conn_retry_exceeded",
|
|
90
|
+
HandshakeFailed: "handshake_failed",
|
|
91
|
+
MessageOrderingViolated: "message_ordering_violated"
|
|
92
|
+
};
|
|
93
|
+
var EventDispatcher = class {
|
|
94
|
+
eventListeners = {};
|
|
95
|
+
removeAllListeners() {
|
|
96
|
+
this.eventListeners = {};
|
|
97
|
+
}
|
|
98
|
+
numberOfListeners(eventType) {
|
|
99
|
+
return this.eventListeners[eventType]?.size ?? 0;
|
|
100
|
+
}
|
|
101
|
+
addEventListener(eventType, handler) {
|
|
102
|
+
if (!this.eventListeners[eventType]) {
|
|
103
|
+
this.eventListeners[eventType] = /* @__PURE__ */ new Set();
|
|
104
|
+
}
|
|
105
|
+
this.eventListeners[eventType]?.add(handler);
|
|
106
|
+
}
|
|
107
|
+
removeEventListener(eventType, handler) {
|
|
108
|
+
const handlers = this.eventListeners[eventType];
|
|
109
|
+
if (handlers) {
|
|
110
|
+
this.eventListeners[eventType]?.delete(handler);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
dispatchEvent(eventType, event) {
|
|
114
|
+
const handlers = this.eventListeners[eventType];
|
|
115
|
+
if (handlers) {
|
|
116
|
+
const copy = [...handlers];
|
|
117
|
+
for (const handler of copy) {
|
|
118
|
+
handler(event);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// codec/json.ts
|
|
125
|
+
var encoder = new TextEncoder();
|
|
126
|
+
var decoder = new TextDecoder();
|
|
127
|
+
function uint8ArrayToBase64(uint8Array) {
|
|
128
|
+
let binary = "";
|
|
129
|
+
uint8Array.forEach((byte) => {
|
|
130
|
+
binary += String.fromCharCode(byte);
|
|
131
|
+
});
|
|
132
|
+
return btoa(binary);
|
|
133
|
+
}
|
|
134
|
+
function base64ToUint8Array(base64) {
|
|
135
|
+
const binaryString = atob(base64);
|
|
136
|
+
const uint8Array = new Uint8Array(binaryString.length);
|
|
137
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
138
|
+
uint8Array[i] = binaryString.charCodeAt(i);
|
|
139
|
+
}
|
|
140
|
+
return uint8Array;
|
|
141
|
+
}
|
|
142
|
+
var NaiveJsonCodec = {
|
|
143
|
+
toBuffer: (obj) => {
|
|
144
|
+
return encoder.encode(
|
|
145
|
+
JSON.stringify(obj, function replacer(key) {
|
|
146
|
+
const val = this[key];
|
|
147
|
+
if (val instanceof Uint8Array) {
|
|
148
|
+
return { $t: uint8ArrayToBase64(val) };
|
|
149
|
+
} else {
|
|
150
|
+
return val;
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
);
|
|
154
|
+
},
|
|
155
|
+
fromBuffer: (buff) => {
|
|
156
|
+
try {
|
|
157
|
+
const parsed = JSON.parse(
|
|
158
|
+
decoder.decode(buff),
|
|
159
|
+
function reviver(_key, val) {
|
|
160
|
+
if (val?.$t) {
|
|
161
|
+
return base64ToUint8Array(val.$t);
|
|
162
|
+
} else {
|
|
163
|
+
return val;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
);
|
|
167
|
+
if (typeof parsed === "object")
|
|
168
|
+
return parsed;
|
|
169
|
+
return null;
|
|
170
|
+
} catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// transport/options.ts
|
|
177
|
+
var defaultTransportOptions = {
|
|
178
|
+
heartbeatIntervalMs: 1e3,
|
|
179
|
+
heartbeatsUntilDead: 2,
|
|
180
|
+
sessionDisconnectGraceMs: 5e3,
|
|
181
|
+
connectionTimeoutMs: 2e3,
|
|
182
|
+
handshakeTimeoutMs: 1e3,
|
|
183
|
+
codec: NaiveJsonCodec
|
|
184
|
+
};
|
|
185
|
+
var defaultConnectionRetryOptions = {
|
|
186
|
+
baseIntervalMs: 250,
|
|
187
|
+
maxJitterMs: 200,
|
|
188
|
+
maxBackoffMs: 32e3,
|
|
189
|
+
attemptBudgetCapacity: 5,
|
|
190
|
+
budgetRestoreIntervalMs: 200
|
|
191
|
+
};
|
|
192
|
+
var defaultClientTransportOptions = {
|
|
193
|
+
...defaultTransportOptions,
|
|
194
|
+
...defaultConnectionRetryOptions
|
|
195
|
+
};
|
|
196
|
+
var defaultServerTransportOptions = {
|
|
197
|
+
...defaultTransportOptions
|
|
198
|
+
};
|
|
36
199
|
|
|
37
200
|
// transport/message.ts
|
|
38
201
|
var import_typebox = require("@sinclair/typebox");
|
|
202
|
+
|
|
203
|
+
// transport/id.ts
|
|
39
204
|
var import_nanoid = require("nanoid");
|
|
205
|
+
var alphabet = (0, import_nanoid.customAlphabet)(
|
|
206
|
+
"1234567890abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ"
|
|
207
|
+
);
|
|
208
|
+
var generateId = () => alphabet(12);
|
|
209
|
+
|
|
210
|
+
// transport/message.ts
|
|
40
211
|
var TransportMessageSchema = (t) => import_typebox.Type.Object({
|
|
41
212
|
id: import_typebox.Type.String(),
|
|
42
213
|
from: import_typebox.Type.String(),
|
|
@@ -71,18 +242,29 @@ var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
|
|
|
71
242
|
* used by the server to know whether this is a new or a reestablished connection, and whether it
|
|
72
243
|
* is compatible with what it already has.
|
|
73
244
|
*/
|
|
74
|
-
expectedSessionState: import_typebox.Type.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
})
|
|
83
|
-
),
|
|
245
|
+
expectedSessionState: import_typebox.Type.Object({
|
|
246
|
+
// what the client expects the server to send next
|
|
247
|
+
nextExpectedSeq: import_typebox.Type.Integer(),
|
|
248
|
+
// TODO: remove optional once we know all servers
|
|
249
|
+
// are nextSentSeq here
|
|
250
|
+
// what the server expects the client to send next
|
|
251
|
+
nextSentSeq: import_typebox.Type.Optional(import_typebox.Type.Integer())
|
|
252
|
+
}),
|
|
84
253
|
metadata: import_typebox.Type.Optional(import_typebox.Type.Unknown())
|
|
85
254
|
});
|
|
255
|
+
var HandshakeErrorRetriableResponseCodes = import_typebox.Type.Union([
|
|
256
|
+
import_typebox.Type.Literal("SESSION_STATE_MISMATCH")
|
|
257
|
+
]);
|
|
258
|
+
var HandshakeErrorFatalResponseCodes = import_typebox.Type.Union([
|
|
259
|
+
import_typebox.Type.Literal("MALFORMED_HANDSHAKE_META"),
|
|
260
|
+
import_typebox.Type.Literal("MALFORMED_HANDSHAKE"),
|
|
261
|
+
import_typebox.Type.Literal("PROTOCOL_VERSION_MISMATCH"),
|
|
262
|
+
import_typebox.Type.Literal("REJECTED_BY_CUSTOM_HANDLER")
|
|
263
|
+
]);
|
|
264
|
+
var HandshakeErrorResponseCodes = import_typebox.Type.Union([
|
|
265
|
+
HandshakeErrorRetriableResponseCodes,
|
|
266
|
+
HandshakeErrorFatalResponseCodes
|
|
267
|
+
]);
|
|
86
268
|
var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
|
|
87
269
|
type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
|
|
88
270
|
status: import_typebox.Type.Union([
|
|
@@ -92,7 +274,10 @@ var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
|
|
|
92
274
|
}),
|
|
93
275
|
import_typebox.Type.Object({
|
|
94
276
|
ok: import_typebox.Type.Literal(false),
|
|
95
|
-
reason: import_typebox.Type.String()
|
|
277
|
+
reason: import_typebox.Type.String(),
|
|
278
|
+
// TODO: remove optional once we know all servers
|
|
279
|
+
// are sending code here
|
|
280
|
+
code: import_typebox.Type.Optional(HandshakeErrorResponseCodes)
|
|
96
281
|
})
|
|
97
282
|
])
|
|
98
283
|
});
|
|
@@ -114,12 +299,12 @@ function handshakeRequestMessage({
|
|
|
114
299
|
tracing
|
|
115
300
|
}) {
|
|
116
301
|
return {
|
|
117
|
-
id: (
|
|
302
|
+
id: generateId(),
|
|
118
303
|
from,
|
|
119
304
|
to,
|
|
120
305
|
seq: 0,
|
|
121
306
|
ack: 0,
|
|
122
|
-
streamId: (
|
|
307
|
+
streamId: generateId(),
|
|
123
308
|
controlFlags: 0,
|
|
124
309
|
tracing,
|
|
125
310
|
payload: {
|
|
@@ -131,19 +316,18 @@ function handshakeRequestMessage({
|
|
|
131
316
|
}
|
|
132
317
|
};
|
|
133
318
|
}
|
|
134
|
-
var SESSION_STATE_MISMATCH = "session state mismatch";
|
|
135
319
|
function handshakeResponseMessage({
|
|
136
320
|
from,
|
|
137
321
|
to,
|
|
138
322
|
status
|
|
139
323
|
}) {
|
|
140
324
|
return {
|
|
141
|
-
id: (
|
|
325
|
+
id: generateId(),
|
|
142
326
|
from,
|
|
143
327
|
to,
|
|
144
328
|
seq: 0,
|
|
145
329
|
ack: 0,
|
|
146
|
-
streamId: (
|
|
330
|
+
streamId: generateId(),
|
|
147
331
|
controlFlags: 0,
|
|
148
332
|
payload: {
|
|
149
333
|
type: "HANDSHAKE_RESP",
|
|
@@ -155,104 +339,230 @@ function isAck(controlFlag) {
|
|
|
155
339
|
return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
|
|
156
340
|
}
|
|
157
341
|
|
|
158
|
-
//
|
|
159
|
-
var
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
minLevel;
|
|
178
|
-
output;
|
|
179
|
-
constructor(output, minLevel = "info") {
|
|
180
|
-
this.minLevel = minLevel;
|
|
181
|
-
this.output = output;
|
|
342
|
+
// transport/sessionStateMachine/common.ts
|
|
343
|
+
var import_value = require("@sinclair/typebox/value");
|
|
344
|
+
var SessionState = /* @__PURE__ */ ((SessionState2) => {
|
|
345
|
+
SessionState2["NoConnection"] = "NoConnection";
|
|
346
|
+
SessionState2["Connecting"] = "Connecting";
|
|
347
|
+
SessionState2["Handshaking"] = "Handshaking";
|
|
348
|
+
SessionState2["Connected"] = "Connected";
|
|
349
|
+
SessionState2["WaitingForHandshake"] = "WaitingForHandshake";
|
|
350
|
+
return SessionState2;
|
|
351
|
+
})(SessionState || {});
|
|
352
|
+
var ERR_CONSUMED = `session state has been consumed and is no longer valid`;
|
|
353
|
+
var StateMachineState = class {
|
|
354
|
+
/*
|
|
355
|
+
* Whether this state has been consumed
|
|
356
|
+
* and we've moved on to another state
|
|
357
|
+
*/
|
|
358
|
+
_isConsumed;
|
|
359
|
+
close() {
|
|
360
|
+
this._handleClose();
|
|
182
361
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
362
|
+
constructor() {
|
|
363
|
+
this._isConsumed = false;
|
|
364
|
+
return new Proxy(this, {
|
|
365
|
+
get(target, prop) {
|
|
366
|
+
if (prop === "_isConsumed" || prop === "id" || prop === "state") {
|
|
367
|
+
return Reflect.get(target, prop);
|
|
368
|
+
}
|
|
369
|
+
if (prop === "_handleStateExit") {
|
|
370
|
+
return () => {
|
|
371
|
+
target._isConsumed = true;
|
|
372
|
+
target._handleStateExit();
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
if (prop === "_handleClose") {
|
|
376
|
+
return () => {
|
|
377
|
+
target._handleStateExit();
|
|
378
|
+
target._handleClose();
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
if (target._isConsumed) {
|
|
382
|
+
throw new Error(
|
|
383
|
+
`${ERR_CONSUMED}: getting ${prop.toString()} on consumed state`
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
return Reflect.get(target, prop);
|
|
387
|
+
},
|
|
388
|
+
set(target, prop, value) {
|
|
389
|
+
if (target._isConsumed) {
|
|
390
|
+
throw new Error(
|
|
391
|
+
`${ERR_CONSUMED}: setting ${prop.toString()} on consumed state`
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
return Reflect.set(target, prop, value);
|
|
395
|
+
}
|
|
396
|
+
});
|
|
187
397
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
398
|
+
};
|
|
399
|
+
var CommonSession = class extends StateMachineState {
|
|
400
|
+
from;
|
|
401
|
+
options;
|
|
402
|
+
log;
|
|
403
|
+
constructor(from, options, log) {
|
|
404
|
+
super();
|
|
405
|
+
this.from = from;
|
|
406
|
+
this.options = options;
|
|
407
|
+
this.log = log;
|
|
192
408
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
409
|
+
parseMsg(msg) {
|
|
410
|
+
const parsedMsg = this.options.codec.fromBuffer(msg);
|
|
411
|
+
if (parsedMsg === null) {
|
|
412
|
+
const decodedBuffer = new TextDecoder().decode(Buffer.from(msg));
|
|
413
|
+
this.log?.error(
|
|
414
|
+
`received malformed msg: ${decodedBuffer}`,
|
|
415
|
+
this.loggingMetadata
|
|
416
|
+
);
|
|
417
|
+
return null;
|
|
196
418
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
419
|
+
if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
420
|
+
this.log?.error(`received invalid msg: ${JSON.stringify(parsedMsg)}`, {
|
|
421
|
+
...this.loggingMetadata,
|
|
422
|
+
validationErrors: [
|
|
423
|
+
...import_value.Value.Errors(OpaqueTransportMessageSchema, parsedMsg)
|
|
424
|
+
]
|
|
425
|
+
});
|
|
426
|
+
return null;
|
|
201
427
|
}
|
|
428
|
+
return parsedMsg;
|
|
202
429
|
}
|
|
203
430
|
};
|
|
204
|
-
var
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
this.
|
|
431
|
+
var IdentifiedSession = class extends CommonSession {
|
|
432
|
+
id;
|
|
433
|
+
telemetry;
|
|
434
|
+
to;
|
|
435
|
+
/**
|
|
436
|
+
* Index of the message we will send next (excluding handshake)
|
|
437
|
+
*/
|
|
438
|
+
seq;
|
|
439
|
+
/**
|
|
440
|
+
* Number of unique messages we've received this session (excluding handshake)
|
|
441
|
+
*/
|
|
442
|
+
ack;
|
|
443
|
+
sendBuffer;
|
|
444
|
+
constructor(id, from, to, seq, ack, sendBuffer, telemetry, options, log) {
|
|
445
|
+
super(from, options, log);
|
|
446
|
+
this.id = id;
|
|
447
|
+
this.to = to;
|
|
448
|
+
this.seq = seq;
|
|
449
|
+
this.ack = ack;
|
|
450
|
+
this.sendBuffer = sendBuffer;
|
|
451
|
+
this.telemetry = telemetry;
|
|
452
|
+
this.log = log;
|
|
221
453
|
}
|
|
222
|
-
|
|
223
|
-
|
|
454
|
+
get loggingMetadata() {
|
|
455
|
+
const spanContext = this.telemetry.span.spanContext();
|
|
456
|
+
return {
|
|
457
|
+
clientId: this.from,
|
|
458
|
+
connectedTo: this.to,
|
|
459
|
+
sessionId: this.id,
|
|
460
|
+
telemetry: {
|
|
461
|
+
traceId: spanContext.traceId,
|
|
462
|
+
spanId: spanContext.spanId
|
|
463
|
+
}
|
|
464
|
+
};
|
|
224
465
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
466
|
+
constructMsg(partialMsg) {
|
|
467
|
+
const msg = {
|
|
468
|
+
...partialMsg,
|
|
469
|
+
id: generateId(),
|
|
470
|
+
to: this.to,
|
|
471
|
+
from: this.from,
|
|
472
|
+
seq: this.seq,
|
|
473
|
+
ack: this.ack
|
|
474
|
+
};
|
|
475
|
+
this.seq++;
|
|
476
|
+
return msg;
|
|
230
477
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (handlers) {
|
|
234
|
-
this.eventListeners[eventType]?.delete(handler);
|
|
235
|
-
}
|
|
478
|
+
nextSeq() {
|
|
479
|
+
return this.sendBuffer.length > 0 ? this.sendBuffer[0].seq : this.seq;
|
|
236
480
|
}
|
|
237
|
-
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
481
|
+
send(msg) {
|
|
482
|
+
const constructedMsg = this.constructMsg(msg);
|
|
483
|
+
this.sendBuffer.push(constructedMsg);
|
|
484
|
+
return constructedMsg.id;
|
|
485
|
+
}
|
|
486
|
+
_handleStateExit() {
|
|
487
|
+
}
|
|
488
|
+
_handleClose() {
|
|
489
|
+
this.sendBuffer.length = 0;
|
|
490
|
+
this.telemetry.span.end();
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// transport/sessionStateMachine/SessionConnecting.ts
|
|
495
|
+
var SessionConnecting = class extends IdentifiedSession {
|
|
496
|
+
state = "Connecting" /* Connecting */;
|
|
497
|
+
connPromise;
|
|
498
|
+
listeners;
|
|
499
|
+
connectionTimeout;
|
|
500
|
+
constructor(connPromise, listeners, ...args) {
|
|
501
|
+
super(...args);
|
|
502
|
+
this.connPromise = connPromise;
|
|
503
|
+
this.listeners = listeners;
|
|
504
|
+
this.connectionTimeout = setTimeout(() => {
|
|
505
|
+
listeners.onConnectionTimeout();
|
|
506
|
+
}, this.options.connectionTimeoutMs);
|
|
507
|
+
connPromise.then(
|
|
508
|
+
(conn) => {
|
|
509
|
+
if (this._isConsumed)
|
|
510
|
+
return;
|
|
511
|
+
listeners.onConnectionEstablished(conn);
|
|
512
|
+
},
|
|
513
|
+
(err) => {
|
|
514
|
+
if (this._isConsumed)
|
|
515
|
+
return;
|
|
516
|
+
listeners.onConnectionFailed(err);
|
|
243
517
|
}
|
|
244
|
-
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
// close a pending connection if it resolves, ignore errors if the promise
|
|
521
|
+
// ends up rejected anyways
|
|
522
|
+
bestEffortClose() {
|
|
523
|
+
void this.connPromise.then((conn) => conn.close()).catch(() => {
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
_handleStateExit() {
|
|
527
|
+
super._handleStateExit();
|
|
528
|
+
clearTimeout(this.connectionTimeout);
|
|
529
|
+
this.connectionTimeout = void 0;
|
|
530
|
+
}
|
|
531
|
+
_handleClose() {
|
|
532
|
+
this.bestEffortClose();
|
|
533
|
+
super._handleClose();
|
|
245
534
|
}
|
|
246
535
|
};
|
|
247
536
|
|
|
248
|
-
// transport/
|
|
249
|
-
var
|
|
537
|
+
// transport/sessionStateMachine/SessionNoConnection.ts
|
|
538
|
+
var SessionNoConnection = class extends IdentifiedSession {
|
|
539
|
+
state = "NoConnection" /* NoConnection */;
|
|
540
|
+
listeners;
|
|
541
|
+
gracePeriodTimeout;
|
|
542
|
+
constructor(listeners, ...args) {
|
|
543
|
+
super(...args);
|
|
544
|
+
this.listeners = listeners;
|
|
545
|
+
this.gracePeriodTimeout = setTimeout(() => {
|
|
546
|
+
this.listeners.onSessionGracePeriodElapsed();
|
|
547
|
+
}, this.options.sessionDisconnectGraceMs);
|
|
548
|
+
}
|
|
549
|
+
_handleClose() {
|
|
550
|
+
super._handleClose();
|
|
551
|
+
}
|
|
552
|
+
_handleStateExit() {
|
|
553
|
+
super._handleStateExit();
|
|
554
|
+
if (this.gracePeriodTimeout) {
|
|
555
|
+
clearTimeout(this.gracePeriodTimeout);
|
|
556
|
+
this.gracePeriodTimeout = void 0;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
};
|
|
250
560
|
|
|
251
561
|
// tracing/index.ts
|
|
252
562
|
var import_api = require("@opentelemetry/api");
|
|
253
563
|
|
|
254
564
|
// package.json
|
|
255
|
-
var version = "0.
|
|
565
|
+
var version = "0.24.0";
|
|
256
566
|
|
|
257
567
|
// tracing/index.ts
|
|
258
568
|
function getPropagationContext(ctx) {
|
|
@@ -263,16 +573,16 @@ function getPropagationContext(ctx) {
|
|
|
263
573
|
import_api.propagation.inject(ctx, tracing);
|
|
264
574
|
return tracing;
|
|
265
575
|
}
|
|
266
|
-
function createSessionTelemetryInfo(
|
|
576
|
+
function createSessionTelemetryInfo(sessionId, to, from, propagationCtx) {
|
|
267
577
|
const parentCtx = propagationCtx ? import_api.propagation.extract(import_api.context.active(), propagationCtx) : import_api.context.active();
|
|
268
578
|
const span = tracer.startSpan(
|
|
269
|
-
`session ${
|
|
579
|
+
`session ${sessionId}`,
|
|
270
580
|
{
|
|
271
581
|
attributes: {
|
|
272
582
|
component: "river",
|
|
273
|
-
"river.session.id":
|
|
274
|
-
"river.session.to":
|
|
275
|
-
"river.session.from":
|
|
583
|
+
"river.session.id": sessionId,
|
|
584
|
+
"river.session.to": to,
|
|
585
|
+
"river.session.from": from
|
|
276
586
|
}
|
|
277
587
|
},
|
|
278
588
|
parentCtx
|
|
@@ -280,173 +590,155 @@ function createSessionTelemetryInfo(session, propagationCtx) {
|
|
|
280
590
|
const ctx = import_api.trace.setSpan(parentCtx, span);
|
|
281
591
|
return { span, ctx };
|
|
282
592
|
}
|
|
283
|
-
function createConnectionTelemetryInfo(connection, info) {
|
|
284
|
-
const span = tracer.startSpan(
|
|
285
|
-
`connection ${connection.id}`,
|
|
286
|
-
{
|
|
287
|
-
attributes: {
|
|
288
|
-
component: "river",
|
|
289
|
-
"river.connection.id": connection.id
|
|
290
|
-
},
|
|
291
|
-
links: [{ context: info.span.spanContext() }]
|
|
292
|
-
},
|
|
293
|
-
info.ctx
|
|
294
|
-
);
|
|
295
|
-
const ctx = import_api.trace.setSpan(info.ctx, span);
|
|
296
|
-
return { span, ctx };
|
|
297
|
-
}
|
|
298
593
|
var tracer = import_api.trace.getTracer("river", version);
|
|
299
594
|
var tracing_default = tracer;
|
|
300
595
|
|
|
301
|
-
// transport/
|
|
302
|
-
var
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
this.
|
|
596
|
+
// transport/sessionStateMachine/SessionWaitingForHandshake.ts
|
|
597
|
+
var SessionWaitingForHandshake = class extends CommonSession {
|
|
598
|
+
state = "WaitingForHandshake" /* WaitingForHandshake */;
|
|
599
|
+
conn;
|
|
600
|
+
listeners;
|
|
601
|
+
handshakeTimeout;
|
|
602
|
+
constructor(conn, listeners, ...args) {
|
|
603
|
+
super(...args);
|
|
604
|
+
this.conn = conn;
|
|
605
|
+
this.listeners = listeners;
|
|
606
|
+
this.handshakeTimeout = setTimeout(() => {
|
|
607
|
+
listeners.onHandshakeTimeout();
|
|
608
|
+
}, this.options.handshakeTimeoutMs);
|
|
609
|
+
this.conn.addDataListener(this.onHandshakeData);
|
|
610
|
+
this.conn.addErrorListener(listeners.onConnectionErrored);
|
|
611
|
+
this.conn.addCloseListener(listeners.onConnectionClosed);
|
|
310
612
|
}
|
|
311
|
-
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
traceId: spanContext.traceId,
|
|
317
|
-
spanId: spanContext.spanId
|
|
318
|
-
};
|
|
613
|
+
onHandshakeData = (msg) => {
|
|
614
|
+
const parsedMsg = this.parseMsg(msg);
|
|
615
|
+
if (parsedMsg === null) {
|
|
616
|
+
this.listeners.onInvalidHandshake("could not parse message");
|
|
617
|
+
return;
|
|
319
618
|
}
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
var Session = class {
|
|
324
|
-
codec;
|
|
325
|
-
options;
|
|
326
|
-
telemetry;
|
|
327
|
-
/**
|
|
328
|
-
* The buffer of messages that have been sent but not yet acknowledged.
|
|
329
|
-
*/
|
|
330
|
-
sendBuffer = [];
|
|
331
|
-
/**
|
|
332
|
-
* The active connection associated with this session
|
|
333
|
-
*/
|
|
334
|
-
connection;
|
|
335
|
-
/**
|
|
336
|
-
* A connection that is currently undergoing handshaking. Used to distinguish between the active
|
|
337
|
-
* connection, but still be able to close it if needed.
|
|
338
|
-
*/
|
|
339
|
-
handshakingConnection;
|
|
340
|
-
from;
|
|
341
|
-
to;
|
|
342
|
-
/**
|
|
343
|
-
* The unique ID of this session.
|
|
344
|
-
*/
|
|
345
|
-
id;
|
|
346
|
-
/**
|
|
347
|
-
* What the other side advertised as their session ID
|
|
348
|
-
* for this session.
|
|
349
|
-
*/
|
|
350
|
-
advertisedSessionId;
|
|
351
|
-
/**
|
|
352
|
-
* Number of messages we've sent along this session (excluding handshake and acks)
|
|
353
|
-
*/
|
|
354
|
-
seq = 0;
|
|
355
|
-
/**
|
|
356
|
-
* Number of unique messages we've received this session (excluding handshake and acks)
|
|
357
|
-
*/
|
|
358
|
-
ack = 0;
|
|
359
|
-
/**
|
|
360
|
-
* The grace period between when the inner connection is disconnected
|
|
361
|
-
* and when we should consider the entire session disconnected.
|
|
362
|
-
*/
|
|
363
|
-
disconnectionGrace;
|
|
364
|
-
/**
|
|
365
|
-
* Number of heartbeats we've sent without a response.
|
|
366
|
-
*/
|
|
367
|
-
heartbeatMisses;
|
|
368
|
-
/**
|
|
369
|
-
* The interval for sending heartbeats.
|
|
370
|
-
*/
|
|
371
|
-
heartbeat;
|
|
372
|
-
log;
|
|
373
|
-
constructor(conn, from, to, options, propagationCtx) {
|
|
374
|
-
this.id = `session-${nanoid2(12)}`;
|
|
375
|
-
this.options = options;
|
|
376
|
-
this.from = from;
|
|
377
|
-
this.to = to;
|
|
378
|
-
this.connection = conn;
|
|
379
|
-
this.codec = options.codec;
|
|
380
|
-
this.heartbeatMisses = 0;
|
|
381
|
-
this.heartbeat = setInterval(
|
|
382
|
-
() => this.sendHeartbeat(),
|
|
383
|
-
options.heartbeatIntervalMs
|
|
384
|
-
);
|
|
385
|
-
this.telemetry = createSessionTelemetryInfo(this, propagationCtx);
|
|
386
|
-
}
|
|
387
|
-
bindLogger(log) {
|
|
388
|
-
this.log = log;
|
|
389
|
-
}
|
|
619
|
+
this.listeners.onHandshake(parsedMsg);
|
|
620
|
+
};
|
|
390
621
|
get loggingMetadata() {
|
|
391
|
-
const spanContext = this.telemetry.span.spanContext();
|
|
392
622
|
return {
|
|
393
623
|
clientId: this.from,
|
|
394
|
-
|
|
395
|
-
sessionId: this.id,
|
|
396
|
-
connId: this.connection?.id,
|
|
397
|
-
telemetry: {
|
|
398
|
-
traceId: spanContext.traceId,
|
|
399
|
-
spanId: spanContext.spanId
|
|
400
|
-
}
|
|
624
|
+
connId: this.conn.id
|
|
401
625
|
};
|
|
402
626
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
627
|
+
sendHandshake(msg) {
|
|
628
|
+
return this.conn.send(this.options.codec.toBuffer(msg));
|
|
629
|
+
}
|
|
630
|
+
_handleStateExit() {
|
|
631
|
+
this.conn.removeDataListener(this.onHandshakeData);
|
|
632
|
+
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
|
|
633
|
+
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
|
|
634
|
+
clearTimeout(this.handshakeTimeout);
|
|
635
|
+
this.handshakeTimeout = void 0;
|
|
636
|
+
}
|
|
637
|
+
_handleClose() {
|
|
638
|
+
this.conn.close();
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// transport/sessionStateMachine/SessionHandshaking.ts
|
|
643
|
+
var SessionHandshaking = class extends IdentifiedSession {
|
|
644
|
+
state = "Handshaking" /* Handshaking */;
|
|
645
|
+
conn;
|
|
646
|
+
listeners;
|
|
647
|
+
handshakeTimeout;
|
|
648
|
+
constructor(conn, listeners, ...args) {
|
|
649
|
+
super(...args);
|
|
650
|
+
this.conn = conn;
|
|
651
|
+
this.listeners = listeners;
|
|
652
|
+
this.handshakeTimeout = setTimeout(() => {
|
|
653
|
+
listeners.onHandshakeTimeout();
|
|
654
|
+
}, this.options.handshakeTimeoutMs);
|
|
655
|
+
this.conn.addDataListener(this.onHandshakeData);
|
|
656
|
+
this.conn.addErrorListener(listeners.onConnectionErrored);
|
|
657
|
+
this.conn.addCloseListener(listeners.onConnectionClosed);
|
|
658
|
+
}
|
|
659
|
+
onHandshakeData = (msg) => {
|
|
660
|
+
const parsedMsg = this.parseMsg(msg);
|
|
661
|
+
if (parsedMsg === null) {
|
|
662
|
+
this.listeners.onInvalidHandshake("could not parse message");
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
this.listeners.onHandshake(parsedMsg);
|
|
666
|
+
};
|
|
667
|
+
sendHandshake(msg) {
|
|
668
|
+
return this.conn.send(this.options.codec.toBuffer(msg));
|
|
669
|
+
}
|
|
670
|
+
_handleStateExit() {
|
|
671
|
+
super._handleStateExit();
|
|
672
|
+
this.conn.removeDataListener(this.onHandshakeData);
|
|
673
|
+
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
|
|
674
|
+
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
|
|
675
|
+
clearTimeout(this.handshakeTimeout);
|
|
676
|
+
}
|
|
677
|
+
_handleClose() {
|
|
678
|
+
super._handleClose();
|
|
679
|
+
this.conn.close();
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
// transport/sessionStateMachine/SessionConnected.ts
|
|
684
|
+
var import_api2 = require("@opentelemetry/api");
|
|
685
|
+
var SessionConnected = class extends IdentifiedSession {
|
|
686
|
+
state = "Connected" /* Connected */;
|
|
687
|
+
conn;
|
|
688
|
+
listeners;
|
|
689
|
+
heartbeatHandle;
|
|
690
|
+
heartbeatMisses = 0;
|
|
691
|
+
get isActivelyHeartbeating() {
|
|
692
|
+
return this.heartbeatHandle !== void 0;
|
|
693
|
+
}
|
|
694
|
+
updateBookkeeping(ack, seq) {
|
|
695
|
+
this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
|
|
696
|
+
this.ack = seq + 1;
|
|
697
|
+
this.heartbeatMisses = 0;
|
|
698
|
+
}
|
|
411
699
|
send(msg) {
|
|
412
|
-
const
|
|
413
|
-
this.
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
transportMessage: fullMsg
|
|
426
|
-
}
|
|
427
|
-
);
|
|
428
|
-
} else {
|
|
700
|
+
const constructedMsg = this.constructMsg(msg);
|
|
701
|
+
this.sendBuffer.push(constructedMsg);
|
|
702
|
+
this.conn.send(this.options.codec.toBuffer(constructedMsg));
|
|
703
|
+
return constructedMsg.id;
|
|
704
|
+
}
|
|
705
|
+
constructor(conn, listeners, ...args) {
|
|
706
|
+
super(...args);
|
|
707
|
+
this.conn = conn;
|
|
708
|
+
this.listeners = listeners;
|
|
709
|
+
this.conn.addDataListener(this.onMessageData);
|
|
710
|
+
this.conn.addCloseListener(listeners.onConnectionClosed);
|
|
711
|
+
this.conn.addErrorListener(listeners.onConnectionErrored);
|
|
712
|
+
if (this.sendBuffer.length > 0) {
|
|
429
713
|
this.log?.debug(
|
|
430
|
-
`
|
|
431
|
-
|
|
714
|
+
`sending ${this.sendBuffer.length} buffered messages`,
|
|
715
|
+
this.loggingMetadata
|
|
432
716
|
);
|
|
433
717
|
}
|
|
434
|
-
|
|
718
|
+
for (const msg of this.sendBuffer) {
|
|
719
|
+
conn.send(this.options.codec.toBuffer(msg));
|
|
720
|
+
}
|
|
435
721
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
if (this.
|
|
722
|
+
startActiveHeartbeat() {
|
|
723
|
+
this.heartbeatHandle = setInterval(() => {
|
|
724
|
+
const misses = this.heartbeatMisses;
|
|
725
|
+
const missDuration = misses * this.options.heartbeatIntervalMs;
|
|
726
|
+
if (misses >= this.options.heartbeatsUntilDead) {
|
|
441
727
|
this.log?.info(
|
|
442
728
|
`closing connection to ${this.to} due to inactivity (missed ${misses} heartbeats which is ${missDuration}ms)`,
|
|
443
729
|
this.loggingMetadata
|
|
444
730
|
);
|
|
445
731
|
this.telemetry.span.addEvent("closing connection due to inactivity");
|
|
446
|
-
this.
|
|
732
|
+
this.conn.close();
|
|
733
|
+
clearInterval(this.heartbeatHandle);
|
|
734
|
+
this.heartbeatHandle = void 0;
|
|
735
|
+
return;
|
|
447
736
|
}
|
|
448
|
-
|
|
449
|
-
|
|
737
|
+
this.sendHeartbeat();
|
|
738
|
+
this.heartbeatMisses++;
|
|
739
|
+
}, this.options.heartbeatIntervalMs);
|
|
740
|
+
}
|
|
741
|
+
sendHeartbeat() {
|
|
450
742
|
this.send({
|
|
451
743
|
streamId: "heartbeat",
|
|
452
744
|
controlFlags: 1 /* AckBit */,
|
|
@@ -454,586 +746,293 @@ var Session = class {
|
|
|
454
746
|
type: "ACK"
|
|
455
747
|
}
|
|
456
748
|
});
|
|
457
|
-
this.heartbeatMisses++;
|
|
458
|
-
}
|
|
459
|
-
resetBufferedMessages() {
|
|
460
|
-
this.sendBuffer = [];
|
|
461
|
-
this.seq = 0;
|
|
462
|
-
this.ack = 0;
|
|
463
749
|
}
|
|
464
|
-
|
|
465
|
-
this.
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
message: errMsg
|
|
481
|
-
});
|
|
482
|
-
this.log?.error(errMsg, {
|
|
750
|
+
onMessageData = (msg) => {
|
|
751
|
+
const parsedMsg = this.parseMsg(msg);
|
|
752
|
+
if (parsedMsg === null)
|
|
753
|
+
return;
|
|
754
|
+
if (parsedMsg.seq !== this.ack) {
|
|
755
|
+
if (parsedMsg.seq < this.ack) {
|
|
756
|
+
this.log?.debug(
|
|
757
|
+
`received duplicate msg (got seq: ${parsedMsg.seq}, wanted seq: ${this.ack}), discarding`,
|
|
758
|
+
{
|
|
759
|
+
...this.loggingMetadata,
|
|
760
|
+
transportMessage: parsedMsg
|
|
761
|
+
}
|
|
762
|
+
);
|
|
763
|
+
} else {
|
|
764
|
+
const reason = `received out-of-order msg (got seq: ${parsedMsg.seq}, wanted seq: ${this.ack})`;
|
|
765
|
+
this.log?.error(reason, {
|
|
483
766
|
...this.loggingMetadata,
|
|
484
|
-
transportMessage:
|
|
485
|
-
connId: conn.id,
|
|
767
|
+
transportMessage: parsedMsg,
|
|
486
768
|
tags: ["invariant-violation"]
|
|
487
769
|
});
|
|
488
|
-
|
|
489
|
-
|
|
770
|
+
this.telemetry.span.setStatus({
|
|
771
|
+
code: import_api2.SpanStatusCode.ERROR,
|
|
772
|
+
message: reason
|
|
773
|
+
});
|
|
774
|
+
this.listeners.onInvalidMessage(reason);
|
|
490
775
|
}
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
updateBookkeeping(ack, seq) {
|
|
494
|
-
if (seq + 1 < this.ack) {
|
|
495
|
-
this.log?.error(`received stale seq ${seq} + 1 < ${this.ack}`, {
|
|
496
|
-
...this.loggingMetadata,
|
|
497
|
-
tags: ["invariant-violation"]
|
|
498
|
-
});
|
|
499
776
|
return;
|
|
500
777
|
}
|
|
501
|
-
this.
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
`closing old inner connection from session to ${this.to}`,
|
|
509
|
-
this.loggingMetadata
|
|
510
|
-
);
|
|
511
|
-
this.connection.close();
|
|
512
|
-
this.connection = void 0;
|
|
513
|
-
}
|
|
514
|
-
replaceWithNewConnection(newConn, isTransparentReconnect) {
|
|
515
|
-
this.closeStaleConnection(newConn);
|
|
516
|
-
this.cancelGrace();
|
|
517
|
-
if (isTransparentReconnect) {
|
|
518
|
-
this.sendBufferedMessages(newConn);
|
|
519
|
-
}
|
|
520
|
-
this.connection = newConn;
|
|
521
|
-
this.handshakingConnection = void 0;
|
|
522
|
-
}
|
|
523
|
-
replaceWithNewHandshakingConnection(newConn) {
|
|
524
|
-
this.handshakingConnection = newConn;
|
|
525
|
-
}
|
|
526
|
-
beginGrace(cb) {
|
|
527
|
-
this.log?.info(
|
|
528
|
-
`starting ${this.options.sessionDisconnectGraceMs}ms grace period until session to ${this.to} is closed`,
|
|
529
|
-
this.loggingMetadata
|
|
530
|
-
);
|
|
531
|
-
this.cancelGrace();
|
|
532
|
-
this.disconnectionGrace = setTimeout(() => {
|
|
533
|
-
this.log?.info(
|
|
534
|
-
`grace period for ${this.to} elapsed`,
|
|
535
|
-
this.loggingMetadata
|
|
536
|
-
);
|
|
537
|
-
cb();
|
|
538
|
-
}, this.options.sessionDisconnectGraceMs);
|
|
539
|
-
}
|
|
540
|
-
// called on reconnect of the underlying session
|
|
541
|
-
cancelGrace() {
|
|
542
|
-
this.heartbeatMisses = 0;
|
|
543
|
-
clearTimeout(this.disconnectionGrace);
|
|
544
|
-
this.disconnectionGrace = void 0;
|
|
545
|
-
}
|
|
546
|
-
/**
|
|
547
|
-
* Used to close the handshaking connection, if set.
|
|
548
|
-
*/
|
|
549
|
-
closeHandshakingConnection(expectedHandshakingConn) {
|
|
550
|
-
if (this.handshakingConnection === void 0)
|
|
551
|
-
return;
|
|
552
|
-
if (expectedHandshakingConn !== void 0 && this.handshakingConnection === expectedHandshakingConn) {
|
|
778
|
+
this.log?.debug(`received msg`, {
|
|
779
|
+
...this.loggingMetadata,
|
|
780
|
+
transportMessage: parsedMsg
|
|
781
|
+
});
|
|
782
|
+
this.updateBookkeeping(parsedMsg.ack, parsedMsg.seq);
|
|
783
|
+
if (!isAck(parsedMsg.controlFlags)) {
|
|
784
|
+
this.listeners.onMessage(parsedMsg);
|
|
553
785
|
return;
|
|
554
786
|
}
|
|
555
|
-
this.
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
this.closeStaleConnection();
|
|
562
|
-
this.cancelGrace();
|
|
563
|
-
this.resetBufferedMessages();
|
|
564
|
-
clearInterval(this.heartbeat);
|
|
565
|
-
}
|
|
566
|
-
get connected() {
|
|
567
|
-
return this.connection !== void 0;
|
|
568
|
-
}
|
|
569
|
-
get nextExpectedAck() {
|
|
570
|
-
return this.seq;
|
|
571
|
-
}
|
|
572
|
-
get nextExpectedSeq() {
|
|
573
|
-
return this.ack;
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* Check that the peer's next expected seq number matches something that is in our send buffer
|
|
577
|
-
* _or_ matches our actual next seq.
|
|
578
|
-
*/
|
|
579
|
-
nextExpectedSeqInRange(nextExpectedSeq) {
|
|
580
|
-
for (const msg of this.sendBuffer) {
|
|
581
|
-
if (nextExpectedSeq === msg.seq) {
|
|
582
|
-
return true;
|
|
583
|
-
}
|
|
787
|
+
this.log?.debug(`discarding msg (ack bit set)`, {
|
|
788
|
+
...this.loggingMetadata,
|
|
789
|
+
transportMessage: parsedMsg
|
|
790
|
+
});
|
|
791
|
+
if (!this.isActivelyHeartbeating) {
|
|
792
|
+
this.sendHeartbeat();
|
|
584
793
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
this.
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
from: this.from,
|
|
598
|
-
seq: this.seq,
|
|
599
|
-
ack: this.ack
|
|
600
|
-
};
|
|
601
|
-
this.seq++;
|
|
602
|
-
this.sendBuffer.push(msg);
|
|
603
|
-
return msg;
|
|
604
|
-
}
|
|
605
|
-
inspectSendBuffer() {
|
|
606
|
-
return this.sendBuffer;
|
|
794
|
+
};
|
|
795
|
+
_handleStateExit() {
|
|
796
|
+
super._handleStateExit();
|
|
797
|
+
this.conn.removeDataListener(this.onMessageData);
|
|
798
|
+
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
|
|
799
|
+
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
|
|
800
|
+
clearInterval(this.heartbeatHandle);
|
|
801
|
+
this.heartbeatHandle = void 0;
|
|
802
|
+
}
|
|
803
|
+
_handleClose() {
|
|
804
|
+
super._handleClose();
|
|
805
|
+
this.conn.close();
|
|
607
806
|
}
|
|
608
807
|
};
|
|
609
808
|
|
|
610
|
-
// transport/
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
function base64ToUint8Array(base64) {
|
|
624
|
-
const binaryString = atob(base64);
|
|
625
|
-
const uint8Array = new Uint8Array(binaryString.length);
|
|
626
|
-
for (let i = 0; i < binaryString.length; i++) {
|
|
627
|
-
uint8Array[i] = binaryString.charCodeAt(i);
|
|
628
|
-
}
|
|
629
|
-
return uint8Array;
|
|
809
|
+
// transport/sessionStateMachine/transitions.ts
|
|
810
|
+
function inheritSharedSession(session) {
|
|
811
|
+
return [
|
|
812
|
+
session.id,
|
|
813
|
+
session.from,
|
|
814
|
+
session.to,
|
|
815
|
+
session.seq,
|
|
816
|
+
session.ack,
|
|
817
|
+
session.sendBuffer,
|
|
818
|
+
session.telemetry,
|
|
819
|
+
session.options,
|
|
820
|
+
session.log
|
|
821
|
+
];
|
|
630
822
|
}
|
|
631
|
-
var
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
823
|
+
var SessionStateGraph = {
|
|
824
|
+
entrypoints: {
|
|
825
|
+
NoConnection(to, from, listeners, options, log) {
|
|
826
|
+
const id = `session-${generateId()}`;
|
|
827
|
+
const telemetry = createSessionTelemetryInfo(id, to, from);
|
|
828
|
+
const sendBuffer = [];
|
|
829
|
+
const session = new SessionNoConnection(
|
|
830
|
+
listeners,
|
|
831
|
+
id,
|
|
832
|
+
from,
|
|
833
|
+
to,
|
|
834
|
+
0,
|
|
835
|
+
0,
|
|
836
|
+
sendBuffer,
|
|
837
|
+
telemetry,
|
|
838
|
+
options,
|
|
839
|
+
log
|
|
840
|
+
);
|
|
841
|
+
session.log?.info(`session ${session.id} created in NoConnection state`, {
|
|
842
|
+
...session.loggingMetadata,
|
|
843
|
+
tags: ["state-transition"]
|
|
844
|
+
});
|
|
845
|
+
return session;
|
|
846
|
+
},
|
|
847
|
+
WaitingForHandshake(from, conn, listeners, options, log) {
|
|
848
|
+
const session = new SessionWaitingForHandshake(
|
|
849
|
+
conn,
|
|
850
|
+
listeners,
|
|
851
|
+
from,
|
|
852
|
+
options,
|
|
853
|
+
log
|
|
854
|
+
);
|
|
855
|
+
session.log?.info(`session created in WaitingForHandshake state`, {
|
|
856
|
+
...session.loggingMetadata,
|
|
857
|
+
tags: ["state-transition"]
|
|
858
|
+
});
|
|
859
|
+
return session;
|
|
860
|
+
}
|
|
643
861
|
},
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
862
|
+
// All of the transitions 'move'/'consume' the old session and return a new one.
|
|
863
|
+
// After a session is transitioned, any usage of the old session will throw.
|
|
864
|
+
transition: {
|
|
865
|
+
// happy path transitions
|
|
866
|
+
NoConnectionToConnecting(oldSession, connPromise, listeners) {
|
|
867
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
868
|
+
oldSession._handleStateExit();
|
|
869
|
+
const session = new SessionConnecting(
|
|
870
|
+
connPromise,
|
|
871
|
+
listeners,
|
|
872
|
+
...carriedState
|
|
873
|
+
);
|
|
874
|
+
session.log?.info(
|
|
875
|
+
`session ${session.id} transition from NoConnection to Connecting`,
|
|
876
|
+
{
|
|
877
|
+
...session.loggingMetadata,
|
|
878
|
+
tags: ["state-transition"]
|
|
654
879
|
}
|
|
655
880
|
);
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
// transport/options.ts
|
|
666
|
-
var defaultTransportOptions = {
|
|
667
|
-
heartbeatIntervalMs: 1e3,
|
|
668
|
-
heartbeatsUntilDead: 2,
|
|
669
|
-
sessionDisconnectGraceMs: 5e3,
|
|
670
|
-
handshakeTimeoutMs: 5e3,
|
|
671
|
-
codec: NaiveJsonCodec
|
|
672
|
-
};
|
|
673
|
-
var defaultConnectionRetryOptions = {
|
|
674
|
-
baseIntervalMs: 250,
|
|
675
|
-
maxJitterMs: 200,
|
|
676
|
-
maxBackoffMs: 32e3,
|
|
677
|
-
attemptBudgetCapacity: 5,
|
|
678
|
-
budgetRestoreIntervalMs: 200
|
|
679
|
-
};
|
|
680
|
-
var defaultClientTransportOptions = {
|
|
681
|
-
...defaultTransportOptions,
|
|
682
|
-
...defaultConnectionRetryOptions
|
|
683
|
-
};
|
|
684
|
-
var defaultServerTransportOptions = {
|
|
685
|
-
...defaultTransportOptions
|
|
686
|
-
};
|
|
687
|
-
|
|
688
|
-
// transport/transport.ts
|
|
689
|
-
var Transport = class {
|
|
690
|
-
/**
|
|
691
|
-
* The status of the transport.
|
|
692
|
-
*/
|
|
693
|
-
status;
|
|
694
|
-
/**
|
|
695
|
-
* The {@link Codec} used to encode and decode messages.
|
|
696
|
-
*/
|
|
697
|
-
codec;
|
|
698
|
-
/**
|
|
699
|
-
* The client ID of this transport.
|
|
700
|
-
*/
|
|
701
|
-
clientId;
|
|
702
|
-
/**
|
|
703
|
-
* The map of {@link Session}s managed by this transport.
|
|
704
|
-
*/
|
|
705
|
-
sessions;
|
|
706
|
-
/**
|
|
707
|
-
* The map of {@link Connection}s managed by this transport.
|
|
708
|
-
*/
|
|
709
|
-
get connections() {
|
|
710
|
-
return new Map(
|
|
711
|
-
[...this.sessions].map(([client, session]) => [client, session.connection]).filter((entry) => entry[1] !== void 0)
|
|
712
|
-
);
|
|
713
|
-
}
|
|
714
|
-
/**
|
|
715
|
-
* The event dispatcher for handling events of type EventTypes.
|
|
716
|
-
*/
|
|
717
|
-
eventDispatcher;
|
|
718
|
-
/**
|
|
719
|
-
* The options for this transport.
|
|
720
|
-
*/
|
|
721
|
-
options;
|
|
722
|
-
log;
|
|
723
|
-
/**
|
|
724
|
-
* Creates a new Transport instance.
|
|
725
|
-
* This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
|
|
726
|
-
* @param codec The codec used to encode and decode messages.
|
|
727
|
-
* @param clientId The client ID of this transport.
|
|
728
|
-
*/
|
|
729
|
-
constructor(clientId, providedOptions) {
|
|
730
|
-
this.options = { ...defaultTransportOptions, ...providedOptions };
|
|
731
|
-
this.eventDispatcher = new EventDispatcher();
|
|
732
|
-
this.sessions = /* @__PURE__ */ new Map();
|
|
733
|
-
this.codec = this.options.codec;
|
|
734
|
-
this.clientId = clientId;
|
|
735
|
-
this.status = "open";
|
|
736
|
-
}
|
|
737
|
-
bindLogger(fn, level) {
|
|
738
|
-
if (typeof fn === "function") {
|
|
739
|
-
this.log = createLogProxy(new BaseLogger(fn, level));
|
|
740
|
-
return;
|
|
741
|
-
}
|
|
742
|
-
this.log = createLogProxy(fn);
|
|
743
|
-
}
|
|
744
|
-
/**
|
|
745
|
-
* Called when a new connection is established
|
|
746
|
-
* and we know the identity of the connected client.
|
|
747
|
-
* @param conn The connection object.
|
|
748
|
-
*/
|
|
749
|
-
onConnect(conn, session, isTransparentReconnect) {
|
|
750
|
-
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
751
|
-
status: "connect",
|
|
752
|
-
conn
|
|
753
|
-
});
|
|
754
|
-
conn.telemetry = createConnectionTelemetryInfo(conn, session.telemetry);
|
|
755
|
-
session.replaceWithNewConnection(conn, isTransparentReconnect);
|
|
756
|
-
this.log?.info(`connected to ${session.to}`, {
|
|
757
|
-
...conn.loggingMetadata,
|
|
758
|
-
...session.loggingMetadata
|
|
759
|
-
});
|
|
760
|
-
}
|
|
761
|
-
createSession(to, conn, propagationCtx) {
|
|
762
|
-
const session = new Session(
|
|
763
|
-
conn,
|
|
764
|
-
this.clientId,
|
|
765
|
-
to,
|
|
766
|
-
this.options,
|
|
767
|
-
propagationCtx
|
|
768
|
-
);
|
|
769
|
-
if (this.log) {
|
|
770
|
-
session.bindLogger(this.log);
|
|
771
|
-
}
|
|
772
|
-
const currentSession = this.sessions.get(session.to);
|
|
773
|
-
if (currentSession) {
|
|
774
|
-
this.log?.warn(
|
|
775
|
-
`session ${session.id} from ${session.to} surreptitiously replacing ${currentSession.id}`,
|
|
881
|
+
return session;
|
|
882
|
+
},
|
|
883
|
+
ConnectingToHandshaking(oldSession, conn, listeners) {
|
|
884
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
885
|
+
oldSession._handleStateExit();
|
|
886
|
+
const session = new SessionHandshaking(conn, listeners, ...carriedState);
|
|
887
|
+
session.log?.info(
|
|
888
|
+
`session ${session.id} transition from Connecting to Handshaking`,
|
|
776
889
|
{
|
|
777
|
-
...
|
|
778
|
-
tags: ["
|
|
890
|
+
...session.loggingMetadata,
|
|
891
|
+
tags: ["state-transition"]
|
|
779
892
|
}
|
|
780
893
|
);
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
to,
|
|
795
|
-
conn,
|
|
796
|
-
sessionId,
|
|
797
|
-
propagationCtx
|
|
798
|
-
}) {
|
|
799
|
-
let session = this.sessions.get(to);
|
|
800
|
-
if (session !== void 0) {
|
|
801
|
-
this.log?.info(
|
|
802
|
-
`session for ${to} already exists, replacing it with a new session as requested`,
|
|
803
|
-
session.loggingMetadata
|
|
894
|
+
return session;
|
|
895
|
+
},
|
|
896
|
+
HandshakingToConnected(oldSession, listeners) {
|
|
897
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
898
|
+
const conn = oldSession.conn;
|
|
899
|
+
oldSession._handleStateExit();
|
|
900
|
+
const session = new SessionConnected(conn, listeners, ...carriedState);
|
|
901
|
+
session.log?.info(
|
|
902
|
+
`session ${session.id} transition from Handshaking to Connected`,
|
|
903
|
+
{
|
|
904
|
+
...session.loggingMetadata,
|
|
905
|
+
tags: ["state-transition"]
|
|
906
|
+
}
|
|
804
907
|
);
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
session.advertisedSessionId !== sessionId
|
|
827
|
-
) {
|
|
828
|
-
return false;
|
|
829
|
-
}
|
|
830
|
-
this.log?.info(
|
|
831
|
-
`reused existing session for ${to}`,
|
|
832
|
-
session.loggingMetadata
|
|
833
|
-
);
|
|
834
|
-
return session;
|
|
835
|
-
}
|
|
836
|
-
getOrCreateSession({
|
|
837
|
-
to,
|
|
838
|
-
conn,
|
|
839
|
-
handshakingConn,
|
|
840
|
-
sessionId,
|
|
841
|
-
propagationCtx
|
|
842
|
-
}) {
|
|
843
|
-
let session = this.sessions.get(to);
|
|
844
|
-
const isReconnect = session !== void 0;
|
|
845
|
-
let isTransparentReconnect = isReconnect;
|
|
846
|
-
if (session?.advertisedSessionId !== void 0 && sessionId !== void 0 && session.advertisedSessionId !== sessionId) {
|
|
847
|
-
this.log?.info(
|
|
848
|
-
`session for ${to} already exists but has a different session id (expected: ${session.advertisedSessionId}, got: ${sessionId}), creating a new one`,
|
|
849
|
-
session.loggingMetadata
|
|
908
|
+
return session;
|
|
909
|
+
},
|
|
910
|
+
WaitingForHandshakeToConnected(pendingSession, oldSession, sessionId, to, propagationCtx, listeners) {
|
|
911
|
+
const conn = pendingSession.conn;
|
|
912
|
+
const { from, options } = pendingSession;
|
|
913
|
+
const carriedState = oldSession ? (
|
|
914
|
+
// old session exists, inherit state
|
|
915
|
+
inheritSharedSession(oldSession)
|
|
916
|
+
) : (
|
|
917
|
+
// old session does not exist, create new state
|
|
918
|
+
[
|
|
919
|
+
sessionId,
|
|
920
|
+
from,
|
|
921
|
+
to,
|
|
922
|
+
0,
|
|
923
|
+
0,
|
|
924
|
+
[],
|
|
925
|
+
createSessionTelemetryInfo(sessionId, to, from, propagationCtx),
|
|
926
|
+
options,
|
|
927
|
+
pendingSession.log
|
|
928
|
+
]
|
|
850
929
|
);
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
930
|
+
pendingSession._handleStateExit();
|
|
931
|
+
oldSession?._handleStateExit();
|
|
932
|
+
const session = new SessionConnected(conn, listeners, ...carriedState);
|
|
933
|
+
session.log?.info(
|
|
934
|
+
`session ${session.id} transition from WaitingForHandshake to Connected`,
|
|
935
|
+
{
|
|
936
|
+
...session.loggingMetadata,
|
|
937
|
+
tags: ["state-transition"]
|
|
938
|
+
}
|
|
939
|
+
);
|
|
940
|
+
return session;
|
|
941
|
+
},
|
|
942
|
+
// disconnect paths
|
|
943
|
+
ConnectingToNoConnection(oldSession, listeners) {
|
|
944
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
945
|
+
oldSession.bestEffortClose();
|
|
946
|
+
oldSession._handleStateExit();
|
|
947
|
+
const session = new SessionNoConnection(listeners, ...carriedState);
|
|
948
|
+
session.log?.info(
|
|
949
|
+
`session ${session.id} transition from Connecting to NoConnection`,
|
|
950
|
+
{
|
|
951
|
+
...session.loggingMetadata,
|
|
952
|
+
tags: ["state-transition"]
|
|
953
|
+
}
|
|
954
|
+
);
|
|
955
|
+
return session;
|
|
956
|
+
},
|
|
957
|
+
HandshakingToNoConnection(oldSession, listeners) {
|
|
958
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
959
|
+
oldSession.conn.close();
|
|
960
|
+
oldSession._handleStateExit();
|
|
961
|
+
const session = new SessionNoConnection(listeners, ...carriedState);
|
|
962
|
+
session.log?.info(
|
|
963
|
+
`session ${session.id} transition from Handshaking to NoConnection`,
|
|
964
|
+
{
|
|
965
|
+
...session.loggingMetadata,
|
|
966
|
+
tags: ["state-transition"]
|
|
967
|
+
}
|
|
864
968
|
);
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
deleteSession({
|
|
875
|
-
session,
|
|
876
|
-
closeHandshakingConnection,
|
|
877
|
-
handshakingConn
|
|
878
|
-
}) {
|
|
879
|
-
if (closeHandshakingConnection) {
|
|
880
|
-
session.closeHandshakingConnection(handshakingConn);
|
|
881
|
-
}
|
|
882
|
-
session.close();
|
|
883
|
-
session.telemetry.span.end();
|
|
884
|
-
const currentSession = this.sessions.get(session.to);
|
|
885
|
-
if (currentSession && currentSession.id !== session.id) {
|
|
886
|
-
this.log?.warn(
|
|
887
|
-
`session ${session.id} disconnect from ${session.to}, mismatch with ${currentSession.id}`,
|
|
969
|
+
return session;
|
|
970
|
+
},
|
|
971
|
+
ConnectedToNoConnection(oldSession, listeners) {
|
|
972
|
+
const carriedState = inheritSharedSession(oldSession);
|
|
973
|
+
oldSession.conn.close();
|
|
974
|
+
oldSession._handleStateExit();
|
|
975
|
+
const session = new SessionNoConnection(listeners, ...carriedState);
|
|
976
|
+
session.log?.info(
|
|
977
|
+
`session ${session.id} transition from Connected to NoConnection`,
|
|
888
978
|
{
|
|
889
979
|
...session.loggingMetadata,
|
|
890
|
-
tags: ["
|
|
980
|
+
tags: ["state-transition"]
|
|
891
981
|
}
|
|
892
982
|
);
|
|
893
|
-
return;
|
|
983
|
+
return session;
|
|
894
984
|
}
|
|
895
|
-
this.sessions.delete(session.to);
|
|
896
|
-
this.log?.info(
|
|
897
|
-
`session ${session.id} disconnect from ${session.to}`,
|
|
898
|
-
session.loggingMetadata
|
|
899
|
-
);
|
|
900
|
-
this.eventDispatcher.dispatchEvent("sessionStatus", {
|
|
901
|
-
status: "disconnect",
|
|
902
|
-
session
|
|
903
|
-
});
|
|
904
985
|
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
// transport/transport.ts
|
|
989
|
+
var Transport = class {
|
|
905
990
|
/**
|
|
906
|
-
* The
|
|
907
|
-
* @param conn The connection object.
|
|
908
|
-
* @param connectedTo The peer we are connected to.
|
|
991
|
+
* The status of the transport.
|
|
909
992
|
*/
|
|
910
|
-
|
|
911
|
-
if (session.connection !== void 0 && session.connection.id !== conn.id) {
|
|
912
|
-
session.telemetry.span.addEvent("onDisconnect race");
|
|
913
|
-
this.log?.warn("onDisconnect race", {
|
|
914
|
-
clientId: this.clientId,
|
|
915
|
-
...session.loggingMetadata,
|
|
916
|
-
...conn.loggingMetadata,
|
|
917
|
-
tags: ["invariant-violation"]
|
|
918
|
-
});
|
|
919
|
-
return;
|
|
920
|
-
}
|
|
921
|
-
conn.telemetry?.span.end();
|
|
922
|
-
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
923
|
-
status: "disconnect",
|
|
924
|
-
conn
|
|
925
|
-
});
|
|
926
|
-
session.connection = void 0;
|
|
927
|
-
session.beginGrace(() => {
|
|
928
|
-
if (session.connection !== void 0) {
|
|
929
|
-
session.telemetry.span.addEvent("session grace period race");
|
|
930
|
-
this.log?.warn("session grace period race", {
|
|
931
|
-
clientId: this.clientId,
|
|
932
|
-
...session.loggingMetadata,
|
|
933
|
-
...conn.loggingMetadata,
|
|
934
|
-
tags: ["invariant-violation"]
|
|
935
|
-
});
|
|
936
|
-
return;
|
|
937
|
-
}
|
|
938
|
-
session.telemetry.span.addEvent("session grace period expired");
|
|
939
|
-
this.deleteSession({
|
|
940
|
-
session,
|
|
941
|
-
closeHandshakingConnection: true,
|
|
942
|
-
handshakingConn: conn
|
|
943
|
-
});
|
|
944
|
-
});
|
|
945
|
-
}
|
|
993
|
+
status;
|
|
946
994
|
/**
|
|
947
|
-
*
|
|
948
|
-
* @param msg The message to parse.
|
|
949
|
-
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
995
|
+
* The client ID of this transport.
|
|
950
996
|
*/
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
997
|
+
clientId;
|
|
998
|
+
/**
|
|
999
|
+
* The event dispatcher for handling events of type EventTypes.
|
|
1000
|
+
*/
|
|
1001
|
+
eventDispatcher;
|
|
1002
|
+
/**
|
|
1003
|
+
* The options for this transport.
|
|
1004
|
+
*/
|
|
1005
|
+
options;
|
|
1006
|
+
log;
|
|
1007
|
+
sessions;
|
|
1008
|
+
/**
|
|
1009
|
+
* Creates a new Transport instance.
|
|
1010
|
+
* @param codec The codec used to encode and decode messages.
|
|
1011
|
+
* @param clientId The client ID of this transport.
|
|
1012
|
+
*/
|
|
1013
|
+
constructor(clientId, providedOptions) {
|
|
1014
|
+
this.options = { ...defaultTransportOptions, ...providedOptions };
|
|
1015
|
+
this.eventDispatcher = new EventDispatcher();
|
|
1016
|
+
this.clientId = clientId;
|
|
1017
|
+
this.status = "open";
|
|
1018
|
+
this.sessions = /* @__PURE__ */ new Map();
|
|
1019
|
+
}
|
|
1020
|
+
bindLogger(fn, level) {
|
|
1021
|
+
if (typeof fn === "function") {
|
|
1022
|
+
this.log = createLogProxy(new BaseLogger(fn, level));
|
|
1023
|
+
return;
|
|
973
1024
|
}
|
|
974
|
-
|
|
1025
|
+
this.log = createLogProxy(fn);
|
|
975
1026
|
}
|
|
976
1027
|
/**
|
|
977
1028
|
* Called when a message is received by this transport.
|
|
978
1029
|
* You generally shouldn't need to override this in downstream transport implementations.
|
|
979
1030
|
* @param msg The received message.
|
|
980
1031
|
*/
|
|
981
|
-
handleMsg(msg
|
|
1032
|
+
handleMsg(msg) {
|
|
982
1033
|
if (this.getStatus() !== "open")
|
|
983
1034
|
return;
|
|
984
|
-
|
|
985
|
-
if (!session) {
|
|
986
|
-
this.log?.error(`received message for unknown session from ${msg.from}`, {
|
|
987
|
-
clientId: this.clientId,
|
|
988
|
-
transportMessage: msg,
|
|
989
|
-
...conn.loggingMetadata,
|
|
990
|
-
tags: ["invariant-violation"]
|
|
991
|
-
});
|
|
992
|
-
return;
|
|
993
|
-
}
|
|
994
|
-
session.cancelGrace();
|
|
995
|
-
this.log?.debug(`received msg`, {
|
|
996
|
-
clientId: this.clientId,
|
|
997
|
-
transportMessage: msg,
|
|
998
|
-
...conn.loggingMetadata
|
|
999
|
-
});
|
|
1000
|
-
if (msg.seq !== session.nextExpectedSeq) {
|
|
1001
|
-
if (msg.seq < session.nextExpectedSeq) {
|
|
1002
|
-
this.log?.debug(
|
|
1003
|
-
`received duplicate msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq}), discarding`,
|
|
1004
|
-
{
|
|
1005
|
-
clientId: this.clientId,
|
|
1006
|
-
transportMessage: msg,
|
|
1007
|
-
...conn.loggingMetadata
|
|
1008
|
-
}
|
|
1009
|
-
);
|
|
1010
|
-
} else {
|
|
1011
|
-
const errMsg = `received out-of-order msg (got seq: ${msg.seq}, wanted seq: ${session.nextExpectedSeq})`;
|
|
1012
|
-
this.log?.error(`${errMsg}, marking connection as dead`, {
|
|
1013
|
-
clientId: this.clientId,
|
|
1014
|
-
transportMessage: msg,
|
|
1015
|
-
...conn.loggingMetadata,
|
|
1016
|
-
tags: ["invariant-violation"]
|
|
1017
|
-
});
|
|
1018
|
-
this.protocolError(ProtocolError.MessageOrderingViolated, errMsg);
|
|
1019
|
-
session.telemetry.span.setStatus({
|
|
1020
|
-
code: import_api3.SpanStatusCode.ERROR,
|
|
1021
|
-
message: "message order violated"
|
|
1022
|
-
});
|
|
1023
|
-
this.deleteSession({ session, closeHandshakingConnection: true });
|
|
1024
|
-
}
|
|
1025
|
-
return;
|
|
1026
|
-
}
|
|
1027
|
-
session.updateBookkeeping(msg.ack, msg.seq);
|
|
1028
|
-
if (!isAck(msg.controlFlags)) {
|
|
1029
|
-
this.eventDispatcher.dispatchEvent("message", msg);
|
|
1030
|
-
} else {
|
|
1031
|
-
this.log?.debug(`discarding msg (ack bit set)`, {
|
|
1032
|
-
clientId: this.clientId,
|
|
1033
|
-
transportMessage: msg,
|
|
1034
|
-
...conn.loggingMetadata
|
|
1035
|
-
});
|
|
1036
|
-
}
|
|
1035
|
+
this.eventDispatcher.dispatchEvent("message", msg);
|
|
1037
1036
|
}
|
|
1038
1037
|
/**
|
|
1039
1038
|
* Adds a listener to this transport.
|
|
@@ -1051,34 +1050,6 @@ var Transport = class {
|
|
|
1051
1050
|
removeEventListener(type, handler) {
|
|
1052
1051
|
this.eventDispatcher.removeEventListener(type, handler);
|
|
1053
1052
|
}
|
|
1054
|
-
/**
|
|
1055
|
-
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
1056
|
-
* send the message.
|
|
1057
|
-
* @param msg The message to send.
|
|
1058
|
-
* @returns The ID of the sent message or undefined if it wasn't sent
|
|
1059
|
-
*/
|
|
1060
|
-
send(to, msg) {
|
|
1061
|
-
if (this.getStatus() === "closed") {
|
|
1062
|
-
const err = "transport is closed, cant send";
|
|
1063
|
-
this.log?.error(err, {
|
|
1064
|
-
clientId: this.clientId,
|
|
1065
|
-
transportMessage: msg,
|
|
1066
|
-
tags: ["invariant-violation"]
|
|
1067
|
-
});
|
|
1068
|
-
throw new Error(err);
|
|
1069
|
-
}
|
|
1070
|
-
return this.getOrCreateSession({ to }).session.send(msg);
|
|
1071
|
-
}
|
|
1072
|
-
// control helpers
|
|
1073
|
-
sendCloseStream(to, streamId) {
|
|
1074
|
-
return this.send(to, {
|
|
1075
|
-
streamId,
|
|
1076
|
-
controlFlags: 4 /* StreamClosedBit */,
|
|
1077
|
-
payload: {
|
|
1078
|
-
type: "CLOSE"
|
|
1079
|
-
}
|
|
1080
|
-
});
|
|
1081
|
-
}
|
|
1082
1053
|
protocolError(type, message) {
|
|
1083
1054
|
this.eventDispatcher.dispatchEvent("protocolError", { type, message });
|
|
1084
1055
|
}
|
|
@@ -1090,7 +1061,7 @@ var Transport = class {
|
|
|
1090
1061
|
close() {
|
|
1091
1062
|
this.status = "closed";
|
|
1092
1063
|
for (const session of this.sessions.values()) {
|
|
1093
|
-
this.deleteSession(
|
|
1064
|
+
this.deleteSession(session);
|
|
1094
1065
|
}
|
|
1095
1066
|
this.eventDispatcher.dispatchEvent("transportStatus", {
|
|
1096
1067
|
status: this.status
|
|
@@ -1101,10 +1072,72 @@ var Transport = class {
|
|
|
1101
1072
|
getStatus() {
|
|
1102
1073
|
return this.status;
|
|
1103
1074
|
}
|
|
1075
|
+
updateSession(session) {
|
|
1076
|
+
const activeSession = this.sessions.get(session.to);
|
|
1077
|
+
if (activeSession && activeSession.id !== session.id) {
|
|
1078
|
+
const msg = `attempt to transition active session for ${session.to} but active session (${activeSession.id}) is different from handle (${session.id})`;
|
|
1079
|
+
throw new Error(msg);
|
|
1080
|
+
}
|
|
1081
|
+
this.sessions.set(session.to, session);
|
|
1082
|
+
if (!activeSession) {
|
|
1083
|
+
this.eventDispatcher.dispatchEvent("sessionStatus", {
|
|
1084
|
+
status: "connect",
|
|
1085
|
+
session
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
this.eventDispatcher.dispatchEvent("sessionTransition", {
|
|
1089
|
+
state: session.state,
|
|
1090
|
+
session
|
|
1091
|
+
});
|
|
1092
|
+
return session;
|
|
1093
|
+
}
|
|
1094
|
+
// state transitions
|
|
1095
|
+
deleteSession(session) {
|
|
1096
|
+
session.log?.info(`closing session ${session.id}`, session.loggingMetadata);
|
|
1097
|
+
this.eventDispatcher.dispatchEvent("sessionStatus", {
|
|
1098
|
+
status: "disconnect",
|
|
1099
|
+
session
|
|
1100
|
+
});
|
|
1101
|
+
session.close();
|
|
1102
|
+
this.sessions.delete(session.to);
|
|
1103
|
+
}
|
|
1104
|
+
// common listeners
|
|
1105
|
+
onSessionGracePeriodElapsed(session) {
|
|
1106
|
+
this.log?.warn(
|
|
1107
|
+
`session to ${session.to} grace period elapsed, closing`,
|
|
1108
|
+
session.loggingMetadata
|
|
1109
|
+
);
|
|
1110
|
+
this.deleteSession(session);
|
|
1111
|
+
}
|
|
1112
|
+
onConnectingFailed(session) {
|
|
1113
|
+
const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(session, {
|
|
1114
|
+
onSessionGracePeriodElapsed: () => {
|
|
1115
|
+
this.onSessionGracePeriodElapsed(noConnectionSession);
|
|
1116
|
+
}
|
|
1117
|
+
});
|
|
1118
|
+
return this.updateSession(noConnectionSession);
|
|
1119
|
+
}
|
|
1120
|
+
onConnClosed(session) {
|
|
1121
|
+
let noConnectionSession;
|
|
1122
|
+
if (session.state === "Handshaking" /* Handshaking */) {
|
|
1123
|
+
noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(session, {
|
|
1124
|
+
onSessionGracePeriodElapsed: () => {
|
|
1125
|
+
this.onSessionGracePeriodElapsed(noConnectionSession);
|
|
1126
|
+
}
|
|
1127
|
+
});
|
|
1128
|
+
} else {
|
|
1129
|
+
noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(session, {
|
|
1130
|
+
onSessionGracePeriodElapsed: () => {
|
|
1131
|
+
this.onSessionGracePeriodElapsed(noConnectionSession);
|
|
1132
|
+
}
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
return this.updateSession(noConnectionSession);
|
|
1136
|
+
}
|
|
1104
1137
|
};
|
|
1105
1138
|
|
|
1106
1139
|
// transport/client.ts
|
|
1107
|
-
var
|
|
1140
|
+
var import_api3 = require("@opentelemetry/api");
|
|
1108
1141
|
|
|
1109
1142
|
// transport/rateLimit.ts
|
|
1110
1143
|
var LeakyBucketRateLimit = class {
|
|
@@ -1192,10 +1225,6 @@ var ClientTransport = class extends Transport {
|
|
|
1192
1225
|
* The options for this transport.
|
|
1193
1226
|
*/
|
|
1194
1227
|
options;
|
|
1195
|
-
/**
|
|
1196
|
-
* The map of reconnect promises for each client ID.
|
|
1197
|
-
*/
|
|
1198
|
-
inflightConnectionPromises;
|
|
1199
1228
|
retryBudget;
|
|
1200
1229
|
/**
|
|
1201
1230
|
* A flag indicating whether the transport should automatically reconnect
|
|
@@ -1214,352 +1243,278 @@ var ClientTransport = class extends Transport {
|
|
|
1214
1243
|
...defaultClientTransportOptions,
|
|
1215
1244
|
...providedOptions
|
|
1216
1245
|
};
|
|
1217
|
-
this.inflightConnectionPromises = /* @__PURE__ */ new Map();
|
|
1218
1246
|
this.retryBudget = new LeakyBucketRateLimit(this.options);
|
|
1219
1247
|
}
|
|
1220
1248
|
extendHandshake(options) {
|
|
1221
1249
|
this.handshakeExtensions = options;
|
|
1222
1250
|
}
|
|
1223
|
-
|
|
1224
|
-
if (this.getStatus()
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
}, this.options.handshakeTimeoutMs);
|
|
1236
|
-
const handshakeHandler = (data) => {
|
|
1237
|
-
const maybeSession = this.receiveHandshakeResponseMessage(data, conn);
|
|
1238
|
-
clearTimeout(handshakeTimeout);
|
|
1239
|
-
if (!maybeSession) {
|
|
1240
|
-
conn.close();
|
|
1241
|
-
return;
|
|
1242
|
-
} else {
|
|
1243
|
-
session = maybeSession;
|
|
1244
|
-
}
|
|
1245
|
-
conn.removeDataListener(handshakeHandler);
|
|
1246
|
-
conn.addDataListener((data2) => {
|
|
1247
|
-
const parsed = this.parseMsg(data2, conn);
|
|
1248
|
-
if (!parsed) {
|
|
1249
|
-
conn.telemetry?.span.setStatus({
|
|
1250
|
-
code: import_api4.SpanStatusCode.ERROR,
|
|
1251
|
-
message: "message parse failure"
|
|
1252
|
-
});
|
|
1253
|
-
conn.close();
|
|
1254
|
-
return;
|
|
1255
|
-
}
|
|
1256
|
-
this.handleMsg(parsed, conn);
|
|
1251
|
+
tryReconnecting(to) {
|
|
1252
|
+
if (this.reconnectOnConnectionDrop && this.getStatus() === "open") {
|
|
1253
|
+
this.connect(to);
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
send(to, msg) {
|
|
1257
|
+
if (this.getStatus() === "closed") {
|
|
1258
|
+
const err = "transport is closed, cant send";
|
|
1259
|
+
this.log?.error(err, {
|
|
1260
|
+
clientId: this.clientId,
|
|
1261
|
+
transportMessage: msg,
|
|
1262
|
+
tags: ["invariant-violation"]
|
|
1257
1263
|
});
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1264
|
+
throw new Error(err);
|
|
1265
|
+
}
|
|
1266
|
+
let session = this.sessions.get(to);
|
|
1267
|
+
if (!session) {
|
|
1268
|
+
session = this.createUnconnectedSession(to);
|
|
1269
|
+
}
|
|
1270
|
+
return session.send(msg);
|
|
1271
|
+
}
|
|
1272
|
+
createUnconnectedSession(to) {
|
|
1273
|
+
const session = SessionStateGraph.entrypoints.NoConnection(
|
|
1274
|
+
to,
|
|
1275
|
+
this.clientId,
|
|
1276
|
+
{
|
|
1277
|
+
onSessionGracePeriodElapsed: () => {
|
|
1278
|
+
this.onSessionGracePeriodElapsed(session);
|
|
1272
1279
|
}
|
|
1273
|
-
|
|
1274
|
-
this.
|
|
1275
|
-
|
|
1276
|
-
|
|
1280
|
+
},
|
|
1281
|
+
this.options,
|
|
1282
|
+
this.log
|
|
1283
|
+
);
|
|
1284
|
+
this.updateSession(session);
|
|
1285
|
+
return session;
|
|
1286
|
+
}
|
|
1287
|
+
// listeners
|
|
1288
|
+
onConnectingFailed(session) {
|
|
1289
|
+
const noConnectionSession = super.onConnectingFailed(session);
|
|
1290
|
+
this.tryReconnecting(noConnectionSession.to);
|
|
1291
|
+
return noConnectionSession;
|
|
1292
|
+
}
|
|
1293
|
+
onConnClosed(session) {
|
|
1294
|
+
const noConnectionSession = super.onConnClosed(session);
|
|
1295
|
+
this.tryReconnecting(noConnectionSession.to);
|
|
1296
|
+
return noConnectionSession;
|
|
1297
|
+
}
|
|
1298
|
+
onConnectionEstablished(session, conn) {
|
|
1299
|
+
const handshakingSession = SessionStateGraph.transition.ConnectingToHandshaking(session, conn, {
|
|
1300
|
+
onConnectionErrored: (err) => {
|
|
1301
|
+
const errStr = coerceErrorString(err);
|
|
1302
|
+
this.log?.error(
|
|
1303
|
+
`connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
|
|
1304
|
+
handshakingSession.loggingMetadata
|
|
1305
|
+
);
|
|
1306
|
+
},
|
|
1307
|
+
onConnectionClosed: () => {
|
|
1308
|
+
this.log?.warn(
|
|
1309
|
+
`connection to ${handshakingSession.to} closed during handshake`,
|
|
1310
|
+
handshakingSession.loggingMetadata
|
|
1311
|
+
);
|
|
1312
|
+
this.onConnClosed(handshakingSession);
|
|
1313
|
+
},
|
|
1314
|
+
onHandshake: (msg) => {
|
|
1315
|
+
this.onHandshakeResponse(handshakingSession, msg);
|
|
1316
|
+
},
|
|
1317
|
+
onInvalidHandshake: (reason) => {
|
|
1318
|
+
this.log?.error(
|
|
1319
|
+
`invalid handshake: ${reason}`,
|
|
1320
|
+
handshakingSession.loggingMetadata
|
|
1321
|
+
);
|
|
1322
|
+
this.deleteSession(session);
|
|
1323
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
1324
|
+
},
|
|
1325
|
+
onHandshakeTimeout: () => {
|
|
1326
|
+
this.log?.error(
|
|
1327
|
+
`connection to ${handshakingSession.to} timed out during handshake`,
|
|
1328
|
+
handshakingSession.loggingMetadata
|
|
1329
|
+
);
|
|
1330
|
+
this.onConnClosed(handshakingSession);
|
|
1277
1331
|
}
|
|
1278
1332
|
});
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
...conn.loggingMetadata,
|
|
1288
|
-
...session?.loggingMetadata,
|
|
1289
|
-
clientId: this.clientId,
|
|
1290
|
-
connectedTo: to
|
|
1291
|
-
}
|
|
1292
|
-
);
|
|
1333
|
+
this.updateSession(handshakingSession);
|
|
1334
|
+
void this.sendHandshake(handshakingSession);
|
|
1335
|
+
return handshakingSession;
|
|
1336
|
+
}
|
|
1337
|
+
rejectHandshakeResponse(session, reason, metadata) {
|
|
1338
|
+
session.conn.telemetry?.span.setStatus({
|
|
1339
|
+
code: import_api3.SpanStatusCode.ERROR,
|
|
1340
|
+
message: reason
|
|
1293
1341
|
});
|
|
1342
|
+
this.log?.warn(reason, metadata);
|
|
1343
|
+
this.deleteSession(session);
|
|
1294
1344
|
}
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
});
|
|
1302
|
-
this.protocolError(
|
|
1303
|
-
ProtocolError.HandshakeFailed,
|
|
1304
|
-
"received non-transport message"
|
|
1305
|
-
);
|
|
1306
|
-
return false;
|
|
1307
|
-
}
|
|
1308
|
-
if (!import_value2.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
|
|
1309
|
-
conn.telemetry?.span.setStatus({
|
|
1310
|
-
code: import_api4.SpanStatusCode.ERROR,
|
|
1311
|
-
message: "invalid handshake response"
|
|
1312
|
-
});
|
|
1313
|
-
this.log?.warn(`received invalid handshake resp`, {
|
|
1314
|
-
...conn.loggingMetadata,
|
|
1315
|
-
clientId: this.clientId,
|
|
1316
|
-
connectedTo: parsed.from,
|
|
1317
|
-
transportMessage: parsed,
|
|
1345
|
+
onHandshakeResponse(session, msg) {
|
|
1346
|
+
if (!import_value2.Value.Check(ControlMessageHandshakeResponseSchema, msg.payload)) {
|
|
1347
|
+
const reason = `received invalid handshake response`;
|
|
1348
|
+
this.rejectHandshakeResponse(session, reason, {
|
|
1349
|
+
...session.loggingMetadata,
|
|
1350
|
+
transportMessage: msg,
|
|
1318
1351
|
validationErrors: [
|
|
1319
|
-
...import_value2.Value.Errors(
|
|
1320
|
-
ControlMessageHandshakeResponseSchema,
|
|
1321
|
-
parsed.payload
|
|
1322
|
-
)
|
|
1352
|
+
...import_value2.Value.Errors(ControlMessageHandshakeResponseSchema, msg.payload)
|
|
1323
1353
|
]
|
|
1324
1354
|
});
|
|
1325
|
-
|
|
1326
|
-
ProtocolError.HandshakeFailed,
|
|
1327
|
-
"invalid handshake resp"
|
|
1328
|
-
);
|
|
1329
|
-
return false;
|
|
1355
|
+
return;
|
|
1330
1356
|
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
});
|
|
1357
|
+
if (!msg.payload.status.ok) {
|
|
1358
|
+
const retriable = msg.payload.status.code ? import_value2.Value.Check(
|
|
1359
|
+
HandshakeErrorRetriableResponseCodes,
|
|
1360
|
+
msg.payload.status.code
|
|
1361
|
+
) : false;
|
|
1362
|
+
const reason = `handshake failed: ${msg.payload.status.reason}`;
|
|
1363
|
+
this.rejectHandshakeResponse(session, reason, {
|
|
1364
|
+
...session.loggingMetadata,
|
|
1365
|
+
transportMessage: msg
|
|
1366
|
+
});
|
|
1367
|
+
if (retriable) {
|
|
1368
|
+
this.tryReconnecting(session.to);
|
|
1344
1369
|
} else {
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
message: "handshake rejected"
|
|
1348
|
-
});
|
|
1370
|
+
this.deleteSession(session);
|
|
1371
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
1349
1372
|
}
|
|
1350
|
-
|
|
1351
|
-
`received handshake rejection: ${parsed.payload.status.reason}`,
|
|
1352
|
-
{
|
|
1353
|
-
...conn.loggingMetadata,
|
|
1354
|
-
clientId: this.clientId,
|
|
1355
|
-
connectedTo: parsed.from,
|
|
1356
|
-
transportMessage: parsed
|
|
1357
|
-
}
|
|
1358
|
-
);
|
|
1359
|
-
this.protocolError(
|
|
1360
|
-
ProtocolError.HandshakeFailed,
|
|
1361
|
-
parsed.payload.status.reason
|
|
1362
|
-
);
|
|
1363
|
-
return false;
|
|
1373
|
+
return;
|
|
1364
1374
|
}
|
|
1365
|
-
if (
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
conn.telemetry?.span.setStatus({
|
|
1371
|
-
code: import_api4.SpanStatusCode.ERROR,
|
|
1372
|
-
message: "session id mismatch"
|
|
1373
|
-
});
|
|
1374
|
-
this.log?.warn(`handshake from ${parsed.from} session id mismatch`, {
|
|
1375
|
-
...conn.loggingMetadata,
|
|
1376
|
-
clientId: this.clientId,
|
|
1377
|
-
connectedTo: parsed.from,
|
|
1378
|
-
transportMessage: parsed
|
|
1375
|
+
if (msg.payload.status.sessionId !== session.id) {
|
|
1376
|
+
const reason = `session id mismatch: expected ${session.id}, got ${msg.payload.status.sessionId}`;
|
|
1377
|
+
this.rejectHandshakeResponse(session, reason, {
|
|
1378
|
+
...session.loggingMetadata,
|
|
1379
|
+
transportMessage: msg
|
|
1379
1380
|
});
|
|
1380
|
-
|
|
1381
|
-
return false;
|
|
1381
|
+
return;
|
|
1382
1382
|
}
|
|
1383
|
-
this.log?.
|
|
1384
|
-
...
|
|
1385
|
-
|
|
1386
|
-
connectedTo: parsed.from,
|
|
1387
|
-
transportMessage: parsed
|
|
1383
|
+
this.log?.info(`handshake from ${msg.from} ok`, {
|
|
1384
|
+
...session.loggingMetadata,
|
|
1385
|
+
transportMessage: msg
|
|
1388
1386
|
});
|
|
1389
|
-
const
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1387
|
+
const connectedSession = SessionStateGraph.transition.HandshakingToConnected(session, {
|
|
1388
|
+
onConnectionErrored: (err) => {
|
|
1389
|
+
const errStr = coerceErrorString(err);
|
|
1390
|
+
this.log?.warn(
|
|
1391
|
+
`connection to ${connectedSession.to} errored: ${errStr}`,
|
|
1392
|
+
connectedSession.loggingMetadata
|
|
1393
|
+
);
|
|
1394
|
+
},
|
|
1395
|
+
onConnectionClosed: () => {
|
|
1396
|
+
this.log?.info(
|
|
1397
|
+
`connection to ${connectedSession.to} closed`,
|
|
1398
|
+
connectedSession.loggingMetadata
|
|
1399
|
+
);
|
|
1400
|
+
this.onConnClosed(connectedSession);
|
|
1401
|
+
},
|
|
1402
|
+
onMessage: (msg2) => this.handleMsg(msg2),
|
|
1403
|
+
onInvalidMessage: (reason) => {
|
|
1404
|
+
this.deleteSession(connectedSession);
|
|
1405
|
+
this.protocolError(ProtocolError.MessageOrderingViolated, reason);
|
|
1406
|
+
}
|
|
1393
1407
|
});
|
|
1394
|
-
this.
|
|
1395
|
-
this.retryBudget.startRestoringBudget(
|
|
1396
|
-
return session;
|
|
1408
|
+
this.updateSession(connectedSession);
|
|
1409
|
+
this.retryBudget.startRestoringBudget(connectedSession.to);
|
|
1397
1410
|
}
|
|
1398
1411
|
/**
|
|
1399
1412
|
* Manually attempts to connect to a client.
|
|
1400
1413
|
* @param to The client ID of the node to connect to.
|
|
1401
1414
|
*/
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1415
|
+
connect(to) {
|
|
1416
|
+
let session = this.sessions.get(to);
|
|
1417
|
+
session ??= this.createUnconnectedSession(to);
|
|
1418
|
+
if (session.state !== "NoConnection" /* NoConnection */) {
|
|
1419
|
+
this.log?.debug(
|
|
1420
|
+
`session to ${to} has state ${session.state}, skipping connect attempt`,
|
|
1421
|
+
session.loggingMetadata
|
|
1422
|
+
);
|
|
1408
1423
|
return;
|
|
1409
1424
|
}
|
|
1410
|
-
|
|
1411
|
-
if (!canProceedWithConnection()) {
|
|
1425
|
+
if (this.getStatus() !== "open") {
|
|
1412
1426
|
this.log?.info(
|
|
1413
1427
|
`transport state is no longer open, cancelling attempt to connect to ${to}`,
|
|
1414
|
-
|
|
1428
|
+
session.loggingMetadata
|
|
1415
1429
|
);
|
|
1416
1430
|
return;
|
|
1417
1431
|
}
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
this.protocolError(ProtocolError.RetriesExceeded, errMsg);
|
|
1425
|
-
return;
|
|
1426
|
-
}
|
|
1427
|
-
let sleep = Promise.resolve();
|
|
1428
|
-
const backoffMs = this.retryBudget.getBackoffMs(to);
|
|
1429
|
-
if (backoffMs > 0) {
|
|
1430
|
-
sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
|
|
1431
|
-
}
|
|
1432
|
-
this.log?.info(
|
|
1433
|
-
`attempting connection to ${to} (${backoffMs}ms backoff)`,
|
|
1434
|
-
{
|
|
1435
|
-
clientId: this.clientId,
|
|
1436
|
-
connectedTo: to
|
|
1437
|
-
}
|
|
1438
|
-
);
|
|
1439
|
-
this.retryBudget.consumeBudget(to);
|
|
1440
|
-
reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
|
|
1441
|
-
try {
|
|
1442
|
-
span.addEvent("backoff", { backoffMs });
|
|
1443
|
-
await sleep;
|
|
1444
|
-
if (!canProceedWithConnection()) {
|
|
1445
|
-
throw new Error("transport state is no longer open");
|
|
1446
|
-
}
|
|
1447
|
-
span.addEvent("connecting");
|
|
1448
|
-
const conn = await this.createNewOutgoingConnection(to);
|
|
1449
|
-
if (!canProceedWithConnection()) {
|
|
1450
|
-
this.log?.info(
|
|
1451
|
-
`transport state is no longer open, closing pre-handshake connection to ${to}`,
|
|
1452
|
-
{
|
|
1453
|
-
...conn.loggingMetadata,
|
|
1454
|
-
clientId: this.clientId,
|
|
1455
|
-
connectedTo: to
|
|
1456
|
-
}
|
|
1457
|
-
);
|
|
1458
|
-
conn.close();
|
|
1459
|
-
throw new Error("transport state is no longer open");
|
|
1460
|
-
}
|
|
1461
|
-
span.addEvent("sending handshake");
|
|
1462
|
-
const ok = await this.sendHandshake(to, conn);
|
|
1463
|
-
if (!ok) {
|
|
1464
|
-
conn.close();
|
|
1465
|
-
throw new Error("failed to send handshake");
|
|
1466
|
-
}
|
|
1467
|
-
return conn;
|
|
1468
|
-
} catch (err) {
|
|
1469
|
-
const errStr = coerceErrorString(err);
|
|
1470
|
-
span.recordException(errStr);
|
|
1471
|
-
span.setStatus({ code: import_api4.SpanStatusCode.ERROR });
|
|
1472
|
-
throw err;
|
|
1473
|
-
} finally {
|
|
1474
|
-
span.end();
|
|
1475
|
-
}
|
|
1476
|
-
});
|
|
1477
|
-
this.inflightConnectionPromises.set(to, reconnectPromise);
|
|
1478
|
-
} else {
|
|
1479
|
-
this.log?.info(
|
|
1480
|
-
`attempting connection to ${to} (reusing previous attempt)`,
|
|
1481
|
-
{
|
|
1482
|
-
clientId: this.clientId,
|
|
1483
|
-
connectedTo: to
|
|
1484
|
-
}
|
|
1485
|
-
);
|
|
1432
|
+
if (!this.retryBudget.hasBudget(to)) {
|
|
1433
|
+
const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
|
|
1434
|
+
const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
|
|
1435
|
+
this.log?.error(errMsg, session.loggingMetadata);
|
|
1436
|
+
this.protocolError(ProtocolError.RetriesExceeded, errMsg);
|
|
1437
|
+
return;
|
|
1486
1438
|
}
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
const errStr = coerceErrorString(error);
|
|
1492
|
-
if (!this.reconnectOnConnectionDrop || !canProceedWithConnection()) {
|
|
1493
|
-
this.log?.warn(`connection to ${to} failed (${errStr})`, {
|
|
1494
|
-
clientId: this.clientId,
|
|
1495
|
-
connectedTo: to
|
|
1496
|
-
});
|
|
1497
|
-
} else {
|
|
1498
|
-
this.log?.warn(`connection to ${to} failed (${errStr}), retrying`, {
|
|
1499
|
-
clientId: this.clientId,
|
|
1500
|
-
connectedTo: to
|
|
1501
|
-
});
|
|
1502
|
-
await this.connect(to);
|
|
1503
|
-
}
|
|
1439
|
+
let sleep = Promise.resolve();
|
|
1440
|
+
const backoffMs = this.retryBudget.getBackoffMs(to);
|
|
1441
|
+
if (backoffMs > 0) {
|
|
1442
|
+
sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
|
|
1504
1443
|
}
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1444
|
+
this.log?.info(
|
|
1445
|
+
`attempting connection to ${to} (${backoffMs}ms backoff)`,
|
|
1446
|
+
session.loggingMetadata
|
|
1447
|
+
);
|
|
1448
|
+
this.retryBudget.consumeBudget(to);
|
|
1449
|
+
const reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
|
|
1450
|
+
try {
|
|
1451
|
+
span.addEvent("backoff", { backoffMs });
|
|
1452
|
+
await sleep;
|
|
1453
|
+
if (this.getStatus() !== "open") {
|
|
1454
|
+
throw new Error("transport state is no longer open");
|
|
1455
|
+
}
|
|
1456
|
+
span.addEvent("connecting");
|
|
1457
|
+
return await this.createNewOutgoingConnection(to);
|
|
1458
|
+
} catch (err) {
|
|
1459
|
+
const errStr = coerceErrorString(err);
|
|
1460
|
+
span.recordException(errStr);
|
|
1461
|
+
span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
|
|
1462
|
+
throw err;
|
|
1463
|
+
} finally {
|
|
1464
|
+
span.end();
|
|
1465
|
+
}
|
|
1516
1466
|
});
|
|
1467
|
+
const connectingSession = SessionStateGraph.transition.NoConnectionToConnecting(
|
|
1468
|
+
session,
|
|
1469
|
+
reconnectPromise,
|
|
1470
|
+
{
|
|
1471
|
+
onConnectionEstablished: (conn) => {
|
|
1472
|
+
this.log?.debug(
|
|
1473
|
+
`connection to ${connectingSession.to} established`,
|
|
1474
|
+
connectingSession.loggingMetadata
|
|
1475
|
+
);
|
|
1476
|
+
this.onConnectionEstablished(connectingSession, conn);
|
|
1477
|
+
},
|
|
1478
|
+
onConnectionFailed: (error) => {
|
|
1479
|
+
const errStr = coerceErrorString(error);
|
|
1480
|
+
this.log?.error(
|
|
1481
|
+
`error connecting to ${connectingSession.to}: ${errStr}`,
|
|
1482
|
+
connectingSession.loggingMetadata
|
|
1483
|
+
);
|
|
1484
|
+
this.onConnectingFailed(connectingSession);
|
|
1485
|
+
},
|
|
1486
|
+
onConnectionTimeout: () => {
|
|
1487
|
+
this.log?.error(
|
|
1488
|
+
`connection to ${connectingSession.to} timed out`,
|
|
1489
|
+
connectingSession.loggingMetadata
|
|
1490
|
+
);
|
|
1491
|
+
this.onConnectingFailed(connectingSession);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
);
|
|
1495
|
+
this.updateSession(connectingSession);
|
|
1517
1496
|
}
|
|
1518
|
-
async sendHandshake(
|
|
1497
|
+
async sendHandshake(session) {
|
|
1519
1498
|
let metadata = void 0;
|
|
1520
1499
|
if (this.handshakeExtensions) {
|
|
1521
1500
|
metadata = await this.handshakeExtensions.construct();
|
|
1522
|
-
if (!import_value2.Value.Check(this.handshakeExtensions.schema, metadata)) {
|
|
1523
|
-
this.log?.error(`constructed handshake metadata did not match schema`, {
|
|
1524
|
-
...conn.loggingMetadata,
|
|
1525
|
-
clientId: this.clientId,
|
|
1526
|
-
connectedTo: to,
|
|
1527
|
-
validationErrors: [
|
|
1528
|
-
...import_value2.Value.Errors(this.handshakeExtensions.schema, metadata)
|
|
1529
|
-
],
|
|
1530
|
-
tags: ["invariant-violation"]
|
|
1531
|
-
});
|
|
1532
|
-
this.protocolError(
|
|
1533
|
-
ProtocolError.HandshakeFailed,
|
|
1534
|
-
"handshake metadata did not match schema"
|
|
1535
|
-
);
|
|
1536
|
-
conn.telemetry?.span.setStatus({
|
|
1537
|
-
code: import_api4.SpanStatusCode.ERROR,
|
|
1538
|
-
message: "handshake meta mismatch"
|
|
1539
|
-
});
|
|
1540
|
-
return false;
|
|
1541
|
-
}
|
|
1542
1501
|
}
|
|
1543
|
-
const { session } = this.getOrCreateSession({ to, handshakingConn: conn });
|
|
1544
1502
|
const requestMsg = handshakeRequestMessage({
|
|
1545
1503
|
from: this.clientId,
|
|
1546
|
-
to,
|
|
1504
|
+
to: session.to,
|
|
1547
1505
|
sessionId: session.id,
|
|
1548
1506
|
expectedSessionState: {
|
|
1549
|
-
|
|
1550
|
-
|
|
1507
|
+
nextExpectedSeq: session.ack,
|
|
1508
|
+
nextSentSeq: session.nextSeq()
|
|
1551
1509
|
},
|
|
1552
1510
|
metadata,
|
|
1553
1511
|
tracing: getPropagationContext(session.telemetry.ctx)
|
|
1554
1512
|
});
|
|
1555
|
-
this.log?.debug(`sending handshake request to ${to}`, {
|
|
1556
|
-
...
|
|
1557
|
-
clientId: this.clientId,
|
|
1558
|
-
connectedTo: to,
|
|
1513
|
+
this.log?.debug(`sending handshake request to ${session.to}`, {
|
|
1514
|
+
...session.loggingMetadata,
|
|
1559
1515
|
transportMessage: requestMsg
|
|
1560
1516
|
});
|
|
1561
|
-
|
|
1562
|
-
return true;
|
|
1517
|
+
session.sendHandshake(requestMsg);
|
|
1563
1518
|
}
|
|
1564
1519
|
close() {
|
|
1565
1520
|
this.retryBudget.close();
|
|
@@ -1568,7 +1523,7 @@ var ClientTransport = class extends Transport {
|
|
|
1568
1523
|
};
|
|
1569
1524
|
|
|
1570
1525
|
// transport/server.ts
|
|
1571
|
-
var
|
|
1526
|
+
var import_api4 = require("@opentelemetry/api");
|
|
1572
1527
|
var import_value3 = require("@sinclair/typebox/value");
|
|
1573
1528
|
var ServerTransport = class extends Transport {
|
|
1574
1529
|
/**
|
|
@@ -1582,14 +1537,14 @@ var ServerTransport = class extends Transport {
|
|
|
1582
1537
|
/**
|
|
1583
1538
|
* A map of session handshake data for each session.
|
|
1584
1539
|
*/
|
|
1585
|
-
sessionHandshakeMetadata;
|
|
1540
|
+
sessionHandshakeMetadata = /* @__PURE__ */ new Map();
|
|
1541
|
+
pendingSessions = /* @__PURE__ */ new Set();
|
|
1586
1542
|
constructor(clientId, providedOptions) {
|
|
1587
1543
|
super(clientId, providedOptions);
|
|
1588
1544
|
this.options = {
|
|
1589
1545
|
...defaultServerTransportOptions,
|
|
1590
1546
|
...providedOptions
|
|
1591
1547
|
};
|
|
1592
|
-
this.sessionHandshakeMetadata = /* @__PURE__ */ new WeakMap();
|
|
1593
1548
|
this.log?.info(`initiated server transport`, {
|
|
1594
1549
|
clientId: this.clientId,
|
|
1595
1550
|
protocolVersion: PROTOCOL_VERSION
|
|
@@ -1598,6 +1553,36 @@ var ServerTransport = class extends Transport {
|
|
|
1598
1553
|
extendHandshake(options) {
|
|
1599
1554
|
this.handshakeExtensions = options;
|
|
1600
1555
|
}
|
|
1556
|
+
send(to, msg) {
|
|
1557
|
+
if (this.getStatus() === "closed") {
|
|
1558
|
+
const err = "transport is closed, cant send";
|
|
1559
|
+
this.log?.error(err, {
|
|
1560
|
+
clientId: this.clientId,
|
|
1561
|
+
transportMessage: msg,
|
|
1562
|
+
tags: ["invariant-violation"]
|
|
1563
|
+
});
|
|
1564
|
+
throw new Error(err);
|
|
1565
|
+
}
|
|
1566
|
+
const session = this.sessions.get(to);
|
|
1567
|
+
if (!session) {
|
|
1568
|
+
const err = `session to ${to} does not exist`;
|
|
1569
|
+
this.log?.error(err, {
|
|
1570
|
+
clientId: this.clientId,
|
|
1571
|
+
transportMessage: msg,
|
|
1572
|
+
tags: ["invariant-violation"]
|
|
1573
|
+
});
|
|
1574
|
+
throw new Error(err);
|
|
1575
|
+
}
|
|
1576
|
+
return session.send(msg);
|
|
1577
|
+
}
|
|
1578
|
+
deletePendingSession(pendingSession) {
|
|
1579
|
+
pendingSession.close();
|
|
1580
|
+
this.pendingSessions.delete(pendingSession);
|
|
1581
|
+
}
|
|
1582
|
+
deleteSession(session) {
|
|
1583
|
+
this.sessionHandshakeMetadata.delete(session.to);
|
|
1584
|
+
super.deleteSession(session);
|
|
1585
|
+
}
|
|
1601
1586
|
handleConnection(conn) {
|
|
1602
1587
|
if (this.getStatus() !== "open")
|
|
1603
1588
|
return;
|
|
@@ -1605,281 +1590,369 @@ var ServerTransport = class extends Transport {
|
|
|
1605
1590
|
...conn.loggingMetadata,
|
|
1606
1591
|
clientId: this.clientId
|
|
1607
1592
|
});
|
|
1608
|
-
let
|
|
1609
|
-
const
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1593
|
+
let receivedHandshake = false;
|
|
1594
|
+
const pendingSession = SessionStateGraph.entrypoints.WaitingForHandshake(
|
|
1595
|
+
this.clientId,
|
|
1596
|
+
conn,
|
|
1597
|
+
{
|
|
1598
|
+
onConnectionClosed: () => {
|
|
1599
|
+
this.log?.warn(
|
|
1600
|
+
`connection from unknown closed before handshake finished`,
|
|
1601
|
+
pendingSession.loggingMetadata
|
|
1602
|
+
);
|
|
1603
|
+
this.deletePendingSession(pendingSession);
|
|
1604
|
+
},
|
|
1605
|
+
onConnectionErrored: (err) => {
|
|
1606
|
+
const errorString = coerceErrorString(err);
|
|
1607
|
+
this.log?.warn(
|
|
1608
|
+
`connection from unknown errored before handshake finished: ${errorString}`,
|
|
1609
|
+
pendingSession.loggingMetadata
|
|
1610
|
+
);
|
|
1611
|
+
this.deletePendingSession(pendingSession);
|
|
1612
|
+
},
|
|
1613
|
+
onHandshakeTimeout: () => {
|
|
1614
|
+
this.log?.warn(
|
|
1615
|
+
`connection from unknown timed out before handshake finished`,
|
|
1616
|
+
pendingSession.loggingMetadata
|
|
1617
|
+
);
|
|
1618
|
+
this.deletePendingSession(pendingSession);
|
|
1619
|
+
},
|
|
1620
|
+
onHandshake: (msg) => {
|
|
1621
|
+
if (receivedHandshake) {
|
|
1622
|
+
this.log?.error(
|
|
1623
|
+
`received multiple handshake messages from pending session`,
|
|
1624
|
+
{
|
|
1625
|
+
...pendingSession.loggingMetadata,
|
|
1626
|
+
connectedTo: msg.from,
|
|
1627
|
+
transportMessage: msg
|
|
1628
|
+
}
|
|
1629
|
+
);
|
|
1630
|
+
this.deletePendingSession(pendingSession);
|
|
1640
1631
|
return;
|
|
1641
1632
|
}
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
dataHandler(data2);
|
|
1653
|
-
}
|
|
1654
|
-
conn.removeDataListener(handshakeHandler);
|
|
1655
|
-
conn.addDataListener(dataHandler);
|
|
1656
|
-
buffer.length = 0;
|
|
1633
|
+
receivedHandshake = true;
|
|
1634
|
+
void this.onHandshakeRequest(pendingSession, msg);
|
|
1635
|
+
},
|
|
1636
|
+
onInvalidHandshake: (reason) => {
|
|
1637
|
+
this.log?.error(
|
|
1638
|
+
`invalid handshake: ${reason}`,
|
|
1639
|
+
pendingSession.loggingMetadata
|
|
1640
|
+
);
|
|
1641
|
+
this.deletePendingSession(pendingSession);
|
|
1642
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
1657
1643
|
}
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
return;
|
|
1664
|
-
this.log?.info(`connection to ${client()} disconnected`, {
|
|
1665
|
-
...conn.loggingMetadata,
|
|
1666
|
-
clientId: this.clientId
|
|
1667
|
-
});
|
|
1668
|
-
this.onDisconnect(conn, session);
|
|
1669
|
-
});
|
|
1670
|
-
conn.addErrorListener((err) => {
|
|
1671
|
-
conn.telemetry?.span.setStatus({
|
|
1672
|
-
code: import_api5.SpanStatusCode.ERROR,
|
|
1673
|
-
message: "connection error"
|
|
1674
|
-
});
|
|
1675
|
-
if (!session)
|
|
1676
|
-
return;
|
|
1677
|
-
this.log?.warn(
|
|
1678
|
-
`connection to ${client()} got an error: ${coerceErrorString(err)}`,
|
|
1679
|
-
{ ...conn.loggingMetadata, clientId: this.clientId }
|
|
1680
|
-
);
|
|
1681
|
-
});
|
|
1682
|
-
}
|
|
1683
|
-
async validateHandshakeMetadata(conn, session, rawMetadata, from) {
|
|
1684
|
-
let parsedMetadata = {};
|
|
1685
|
-
if (this.handshakeExtensions) {
|
|
1686
|
-
if (!import_value3.Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
|
|
1687
|
-
conn.telemetry?.span.setStatus({
|
|
1688
|
-
code: import_api5.SpanStatusCode.ERROR,
|
|
1689
|
-
message: "malformed handshake meta"
|
|
1690
|
-
});
|
|
1691
|
-
const reason = "received malformed handshake metadata";
|
|
1692
|
-
const responseMsg = handshakeResponseMessage({
|
|
1693
|
-
from: this.clientId,
|
|
1694
|
-
to: from,
|
|
1695
|
-
status: {
|
|
1696
|
-
ok: false,
|
|
1697
|
-
reason
|
|
1698
|
-
}
|
|
1699
|
-
});
|
|
1700
|
-
conn.send(this.codec.toBuffer(responseMsg));
|
|
1701
|
-
this.log?.warn(`received malformed handshake metadata from ${from}`, {
|
|
1702
|
-
...conn.loggingMetadata,
|
|
1703
|
-
clientId: this.clientId,
|
|
1704
|
-
validationErrors: [
|
|
1705
|
-
...import_value3.Value.Errors(this.handshakeExtensions.schema, rawMetadata)
|
|
1706
|
-
]
|
|
1707
|
-
});
|
|
1708
|
-
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
1709
|
-
return false;
|
|
1710
|
-
}
|
|
1711
|
-
const previousParsedMetadata = session ? this.sessionHandshakeMetadata.get(session) : void 0;
|
|
1712
|
-
parsedMetadata = await this.handshakeExtensions.validate(
|
|
1713
|
-
rawMetadata,
|
|
1714
|
-
previousParsedMetadata
|
|
1715
|
-
);
|
|
1716
|
-
if (parsedMetadata === false) {
|
|
1717
|
-
const reason = "rejected by handshake handler";
|
|
1718
|
-
conn.telemetry?.span.setStatus({
|
|
1719
|
-
code: import_api5.SpanStatusCode.ERROR,
|
|
1720
|
-
message: reason
|
|
1721
|
-
});
|
|
1722
|
-
const responseMsg = handshakeResponseMessage({
|
|
1723
|
-
from: this.clientId,
|
|
1724
|
-
to: from,
|
|
1725
|
-
status: {
|
|
1726
|
-
ok: false,
|
|
1727
|
-
reason
|
|
1728
|
-
}
|
|
1729
|
-
});
|
|
1730
|
-
conn.send(this.codec.toBuffer(responseMsg));
|
|
1731
|
-
this.log?.warn(`rejected handshake from ${from}`, {
|
|
1732
|
-
...conn.loggingMetadata,
|
|
1733
|
-
clientId: this.clientId
|
|
1734
|
-
});
|
|
1735
|
-
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
1736
|
-
return false;
|
|
1737
|
-
}
|
|
1738
|
-
}
|
|
1739
|
-
return parsedMetadata;
|
|
1644
|
+
},
|
|
1645
|
+
this.options,
|
|
1646
|
+
this.log
|
|
1647
|
+
);
|
|
1648
|
+
this.pendingSessions.add(pendingSession);
|
|
1740
1649
|
}
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
ProtocolError.HandshakeFailed,
|
|
1750
|
-
"received non-transport message"
|
|
1751
|
-
);
|
|
1752
|
-
return false;
|
|
1753
|
-
}
|
|
1754
|
-
if (!import_value3.Value.Check(ControlMessageHandshakeRequestSchema, parsed.payload)) {
|
|
1755
|
-
conn.telemetry?.span.setStatus({
|
|
1756
|
-
code: import_api5.SpanStatusCode.ERROR,
|
|
1757
|
-
message: "invalid handshake request"
|
|
1758
|
-
});
|
|
1759
|
-
const reason = "received invalid handshake msg";
|
|
1760
|
-
const responseMsg2 = handshakeResponseMessage({
|
|
1650
|
+
rejectHandshakeRequest(session, to, reason, code, metadata) {
|
|
1651
|
+
session.conn.telemetry?.span.setStatus({
|
|
1652
|
+
code: import_api4.SpanStatusCode.ERROR,
|
|
1653
|
+
message: reason
|
|
1654
|
+
});
|
|
1655
|
+
this.log?.warn(reason, metadata);
|
|
1656
|
+
session.sendHandshake(
|
|
1657
|
+
handshakeResponseMessage({
|
|
1761
1658
|
from: this.clientId,
|
|
1762
|
-
to
|
|
1659
|
+
to,
|
|
1763
1660
|
status: {
|
|
1764
1661
|
ok: false,
|
|
1662
|
+
code,
|
|
1765
1663
|
reason
|
|
1766
1664
|
}
|
|
1767
|
-
})
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1665
|
+
})
|
|
1666
|
+
);
|
|
1667
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
1668
|
+
this.deletePendingSession(session);
|
|
1669
|
+
}
|
|
1670
|
+
async onHandshakeRequest(session, msg) {
|
|
1671
|
+
if (!import_value3.Value.Check(ControlMessageHandshakeRequestSchema, msg.payload)) {
|
|
1672
|
+
this.rejectHandshakeRequest(
|
|
1673
|
+
session,
|
|
1674
|
+
msg.from,
|
|
1675
|
+
"received invalid handshake request",
|
|
1676
|
+
"MALFORMED_HANDSHAKE",
|
|
1677
|
+
{
|
|
1678
|
+
...session.loggingMetadata,
|
|
1679
|
+
transportMessage: msg,
|
|
1680
|
+
connectedTo: msg.from,
|
|
1681
|
+
validationErrors: [
|
|
1682
|
+
...import_value3.Value.Errors(ControlMessageHandshakeRequestSchema, msg.payload)
|
|
1683
|
+
]
|
|
1684
|
+
}
|
|
1782
1685
|
);
|
|
1783
|
-
return
|
|
1686
|
+
return;
|
|
1784
1687
|
}
|
|
1785
|
-
const gotVersion =
|
|
1688
|
+
const gotVersion = msg.payload.protocolVersion;
|
|
1786
1689
|
if (gotVersion !== PROTOCOL_VERSION) {
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
ok: false,
|
|
1797
|
-
reason
|
|
1690
|
+
this.rejectHandshakeRequest(
|
|
1691
|
+
session,
|
|
1692
|
+
msg.from,
|
|
1693
|
+
`expected protocol version ${PROTOCOL_VERSION}, got ${gotVersion}`,
|
|
1694
|
+
"PROTOCOL_VERSION_MISMATCH",
|
|
1695
|
+
{
|
|
1696
|
+
...session.loggingMetadata,
|
|
1697
|
+
connectedTo: msg.from,
|
|
1698
|
+
transportMessage: msg
|
|
1798
1699
|
}
|
|
1799
|
-
});
|
|
1800
|
-
conn.send(this.codec.toBuffer(responseMsg2));
|
|
1801
|
-
this.log?.warn(
|
|
1802
|
-
`received handshake msg with incompatible protocol version (got: ${gotVersion}, expected: ${PROTOCOL_VERSION})`,
|
|
1803
|
-
{ ...conn.loggingMetadata, clientId: this.clientId }
|
|
1804
1700
|
);
|
|
1805
|
-
|
|
1806
|
-
return false;
|
|
1701
|
+
return;
|
|
1807
1702
|
}
|
|
1808
|
-
|
|
1703
|
+
let oldSession = this.sessions.get(msg.from);
|
|
1809
1704
|
const parsedMetadata = await this.validateHandshakeMetadata(
|
|
1810
|
-
|
|
1705
|
+
session,
|
|
1811
1706
|
oldSession,
|
|
1812
|
-
|
|
1813
|
-
|
|
1707
|
+
msg.payload.metadata,
|
|
1708
|
+
msg.from
|
|
1814
1709
|
);
|
|
1815
1710
|
if (parsedMetadata === false) {
|
|
1816
|
-
return
|
|
1711
|
+
return;
|
|
1817
1712
|
}
|
|
1818
|
-
let session;
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1713
|
+
let connectCase = "new session";
|
|
1714
|
+
if (oldSession && oldSession.id === msg.payload.sessionId) {
|
|
1715
|
+
connectCase = "transparent reconnection";
|
|
1716
|
+
const clientNextExpectedSeq = msg.payload.expectedSessionState.nextExpectedSeq;
|
|
1717
|
+
const clientNextSentSeq = msg.payload.expectedSessionState.nextSentSeq ?? 0;
|
|
1718
|
+
const ourNextSeq = oldSession.nextSeq();
|
|
1719
|
+
const ourAck = oldSession.ack;
|
|
1720
|
+
if (clientNextSentSeq > ourAck) {
|
|
1721
|
+
this.rejectHandshakeRequest(
|
|
1722
|
+
session,
|
|
1723
|
+
msg.from,
|
|
1724
|
+
`client is in the future: server wanted next message to be ${ourAck} but client would have sent ${clientNextSentSeq}`,
|
|
1725
|
+
"SESSION_STATE_MISMATCH",
|
|
1726
|
+
{
|
|
1727
|
+
...session.loggingMetadata,
|
|
1728
|
+
connectedTo: msg.from,
|
|
1729
|
+
transportMessage: msg
|
|
1730
|
+
}
|
|
1731
|
+
);
|
|
1732
|
+
return;
|
|
1733
|
+
}
|
|
1734
|
+
if (ourNextSeq > clientNextExpectedSeq) {
|
|
1735
|
+
this.rejectHandshakeRequest(
|
|
1736
|
+
session,
|
|
1737
|
+
msg.from,
|
|
1738
|
+
`server is in the future: client wanted next message to be ${clientNextExpectedSeq} but server would have sent ${ourNextSeq}`,
|
|
1739
|
+
"SESSION_STATE_MISMATCH",
|
|
1740
|
+
{
|
|
1741
|
+
...session.loggingMetadata,
|
|
1742
|
+
connectedTo: msg.from,
|
|
1743
|
+
transportMessage: msg
|
|
1744
|
+
}
|
|
1745
|
+
);
|
|
1746
|
+
return;
|
|
1747
|
+
}
|
|
1748
|
+
if (oldSession.state === "Connected" /* Connected */) {
|
|
1749
|
+
const noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(oldSession, {
|
|
1750
|
+
onSessionGracePeriodElapsed: () => {
|
|
1751
|
+
this.onSessionGracePeriodElapsed(noConnectionSession);
|
|
1752
|
+
}
|
|
1837
1753
|
});
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
ok: false,
|
|
1844
|
-
reason
|
|
1754
|
+
oldSession = noConnectionSession;
|
|
1755
|
+
} else if (oldSession.state === "Handshaking" /* Handshaking */) {
|
|
1756
|
+
const noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(oldSession, {
|
|
1757
|
+
onSessionGracePeriodElapsed: () => {
|
|
1758
|
+
this.onSessionGracePeriodElapsed(noConnectionSession);
|
|
1845
1759
|
}
|
|
1846
1760
|
});
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1761
|
+
oldSession = noConnectionSession;
|
|
1762
|
+
} else if (oldSession.state === "Connecting" /* Connecting */) {
|
|
1763
|
+
const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(oldSession, {
|
|
1764
|
+
onSessionGracePeriodElapsed: () => {
|
|
1765
|
+
this.onSessionGracePeriodElapsed(noConnectionSession);
|
|
1766
|
+
}
|
|
1767
|
+
});
|
|
1768
|
+
oldSession = noConnectionSession;
|
|
1854
1769
|
}
|
|
1855
|
-
|
|
1856
|
-
|
|
1770
|
+
this.updateSession(oldSession);
|
|
1771
|
+
} else if (oldSession) {
|
|
1772
|
+
connectCase = "hard reconnection";
|
|
1773
|
+
this.deleteSession(oldSession);
|
|
1774
|
+
oldSession = void 0;
|
|
1857
1775
|
} else {
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1776
|
+
connectCase = "unknown session";
|
|
1777
|
+
const clientNextExpectedSeq = msg.payload.expectedSessionState.nextExpectedSeq;
|
|
1778
|
+
const clientNextSentSeq = msg.payload.expectedSessionState.nextSentSeq ?? 0;
|
|
1779
|
+
if (clientNextSentSeq > 0 || clientNextExpectedSeq > 0) {
|
|
1780
|
+
this.rejectHandshakeRequest(
|
|
1781
|
+
session,
|
|
1782
|
+
msg.from,
|
|
1783
|
+
`client is trying to reconnect to a session the server don't know about: ${msg.payload.sessionId}`,
|
|
1784
|
+
"SESSION_STATE_MISMATCH",
|
|
1785
|
+
{
|
|
1786
|
+
...session.loggingMetadata,
|
|
1787
|
+
connectedTo: msg.from,
|
|
1788
|
+
transportMessage: msg
|
|
1789
|
+
}
|
|
1790
|
+
);
|
|
1791
|
+
return;
|
|
1792
|
+
}
|
|
1866
1793
|
}
|
|
1867
|
-
|
|
1868
|
-
this.log?.
|
|
1869
|
-
`handshake from ${
|
|
1870
|
-
|
|
1794
|
+
const sessionId = msg.payload.sessionId;
|
|
1795
|
+
this.log?.info(
|
|
1796
|
+
`handshake from ${msg.from} ok (${connectCase}), responding with handshake success`,
|
|
1797
|
+
{
|
|
1798
|
+
...session.loggingMetadata,
|
|
1799
|
+
connectedTo: msg.from
|
|
1800
|
+
}
|
|
1871
1801
|
);
|
|
1872
1802
|
const responseMsg = handshakeResponseMessage({
|
|
1873
1803
|
from: this.clientId,
|
|
1874
|
-
to:
|
|
1804
|
+
to: msg.from,
|
|
1875
1805
|
status: {
|
|
1876
1806
|
ok: true,
|
|
1877
|
-
sessionId
|
|
1807
|
+
sessionId
|
|
1878
1808
|
}
|
|
1879
1809
|
});
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1810
|
+
session.sendHandshake(responseMsg);
|
|
1811
|
+
const connectedSession = SessionStateGraph.transition.WaitingForHandshakeToConnected(
|
|
1812
|
+
session,
|
|
1813
|
+
// by this point oldSession is either no connection or we dont have an old session
|
|
1814
|
+
oldSession,
|
|
1815
|
+
sessionId,
|
|
1816
|
+
msg.from,
|
|
1817
|
+
msg.tracing,
|
|
1818
|
+
{
|
|
1819
|
+
onConnectionErrored: (err) => {
|
|
1820
|
+
const errStr = coerceErrorString(err);
|
|
1821
|
+
this.log?.warn(
|
|
1822
|
+
`connection to ${connectedSession.to} errored: ${errStr}`,
|
|
1823
|
+
connectedSession.loggingMetadata
|
|
1824
|
+
);
|
|
1825
|
+
},
|
|
1826
|
+
onConnectionClosed: () => {
|
|
1827
|
+
this.log?.info(
|
|
1828
|
+
`connection to ${connectedSession.to} closed`,
|
|
1829
|
+
connectedSession.loggingMetadata
|
|
1830
|
+
);
|
|
1831
|
+
this.onConnClosed(connectedSession);
|
|
1832
|
+
},
|
|
1833
|
+
onMessage: (msg2) => this.handleMsg(msg2),
|
|
1834
|
+
onInvalidMessage: (reason) => {
|
|
1835
|
+
this.protocolError(ProtocolError.MessageOrderingViolated, reason);
|
|
1836
|
+
this.deleteSession(connectedSession);
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
);
|
|
1840
|
+
this.sessionHandshakeMetadata.set(connectedSession.to, parsedMetadata);
|
|
1841
|
+
this.updateSession(connectedSession);
|
|
1842
|
+
this.pendingSessions.delete(session);
|
|
1843
|
+
connectedSession.startActiveHeartbeat();
|
|
1844
|
+
}
|
|
1845
|
+
async validateHandshakeMetadata(handshakingSession, existingSession, rawMetadata, from) {
|
|
1846
|
+
let parsedMetadata = {};
|
|
1847
|
+
if (this.handshakeExtensions) {
|
|
1848
|
+
if (!import_value3.Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
|
|
1849
|
+
this.rejectHandshakeRequest(
|
|
1850
|
+
handshakingSession,
|
|
1851
|
+
from,
|
|
1852
|
+
"received malformed handshake metadata",
|
|
1853
|
+
"MALFORMED_HANDSHAKE_META",
|
|
1854
|
+
{
|
|
1855
|
+
...handshakingSession.loggingMetadata,
|
|
1856
|
+
connectedTo: from,
|
|
1857
|
+
validationErrors: [
|
|
1858
|
+
...import_value3.Value.Errors(this.handshakeExtensions.schema, rawMetadata)
|
|
1859
|
+
]
|
|
1860
|
+
}
|
|
1861
|
+
);
|
|
1862
|
+
return false;
|
|
1863
|
+
}
|
|
1864
|
+
const previousParsedMetadata = existingSession ? this.sessionHandshakeMetadata.get(existingSession.to) : void 0;
|
|
1865
|
+
parsedMetadata = await this.handshakeExtensions.validate(
|
|
1866
|
+
rawMetadata,
|
|
1867
|
+
previousParsedMetadata
|
|
1868
|
+
);
|
|
1869
|
+
if (parsedMetadata === false) {
|
|
1870
|
+
this.rejectHandshakeRequest(
|
|
1871
|
+
handshakingSession,
|
|
1872
|
+
from,
|
|
1873
|
+
"rejected by handshake handler",
|
|
1874
|
+
"REJECTED_BY_CUSTOM_HANDLER",
|
|
1875
|
+
{
|
|
1876
|
+
...handshakingSession.loggingMetadata,
|
|
1877
|
+
connectedTo: from,
|
|
1878
|
+
clientId: this.clientId
|
|
1879
|
+
}
|
|
1880
|
+
);
|
|
1881
|
+
return false;
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
return parsedMetadata;
|
|
1885
|
+
}
|
|
1886
|
+
};
|
|
1887
|
+
|
|
1888
|
+
// transport/connection.ts
|
|
1889
|
+
var Connection = class {
|
|
1890
|
+
id;
|
|
1891
|
+
telemetry;
|
|
1892
|
+
constructor() {
|
|
1893
|
+
this.id = `conn-${generateId()}`;
|
|
1894
|
+
}
|
|
1895
|
+
get loggingMetadata() {
|
|
1896
|
+
const metadata = { connId: this.id };
|
|
1897
|
+
const spanContext = this.telemetry?.span.spanContext();
|
|
1898
|
+
if (this.telemetry?.span.isRecording() && spanContext) {
|
|
1899
|
+
metadata.telemetry = {
|
|
1900
|
+
traceId: spanContext.traceId,
|
|
1901
|
+
spanId: spanContext.spanId
|
|
1902
|
+
};
|
|
1903
|
+
}
|
|
1904
|
+
return metadata;
|
|
1905
|
+
}
|
|
1906
|
+
// can't use event emitter because we need this to work in both node + browser
|
|
1907
|
+
_dataListeners = /* @__PURE__ */ new Set();
|
|
1908
|
+
_closeListeners = /* @__PURE__ */ new Set();
|
|
1909
|
+
_errorListeners = /* @__PURE__ */ new Set();
|
|
1910
|
+
get dataListeners() {
|
|
1911
|
+
return [...this._dataListeners];
|
|
1912
|
+
}
|
|
1913
|
+
get closeListeners() {
|
|
1914
|
+
return [...this._closeListeners];
|
|
1915
|
+
}
|
|
1916
|
+
get errorListeners() {
|
|
1917
|
+
return [...this._errorListeners];
|
|
1918
|
+
}
|
|
1919
|
+
/**
|
|
1920
|
+
* Handle adding a callback for when a message is received.
|
|
1921
|
+
* @param msg The message that was received.
|
|
1922
|
+
*/
|
|
1923
|
+
addDataListener(cb) {
|
|
1924
|
+
this._dataListeners.add(cb);
|
|
1925
|
+
}
|
|
1926
|
+
removeDataListener(cb) {
|
|
1927
|
+
this._dataListeners.delete(cb);
|
|
1928
|
+
}
|
|
1929
|
+
/**
|
|
1930
|
+
* Handle adding a callback for when the connection is closed.
|
|
1931
|
+
* This should also be called if an error happens and after notifying all the error listeners.
|
|
1932
|
+
* @param cb The callback to call when the connection is closed.
|
|
1933
|
+
*/
|
|
1934
|
+
addCloseListener(cb) {
|
|
1935
|
+
this._closeListeners.add(cb);
|
|
1936
|
+
}
|
|
1937
|
+
removeCloseListener(cb) {
|
|
1938
|
+
this._closeListeners.delete(cb);
|
|
1939
|
+
}
|
|
1940
|
+
/**
|
|
1941
|
+
* Handle adding a callback for when an error is received.
|
|
1942
|
+
* This should only be used for this.logging errors, all cleanup
|
|
1943
|
+
* should be delegated to addCloseListener.
|
|
1944
|
+
*
|
|
1945
|
+
* The implementer should take care such that the implemented
|
|
1946
|
+
* connection will call both the close and error callbacks
|
|
1947
|
+
* on an error.
|
|
1948
|
+
*
|
|
1949
|
+
* @param cb The callback to call when an error is received.
|
|
1950
|
+
*/
|
|
1951
|
+
addErrorListener(cb) {
|
|
1952
|
+
this._errorListeners.add(cb);
|
|
1953
|
+
}
|
|
1954
|
+
removeErrorListener(cb) {
|
|
1955
|
+
this._errorListeners.delete(cb);
|
|
1883
1956
|
}
|
|
1884
1957
|
};
|
|
1885
1958
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1889,7 +1962,7 @@ var ServerTransport = class extends Transport {
|
|
|
1889
1962
|
OpaqueTransportMessageSchema,
|
|
1890
1963
|
ProtocolError,
|
|
1891
1964
|
ServerTransport,
|
|
1892
|
-
|
|
1965
|
+
SessionState,
|
|
1893
1966
|
Transport,
|
|
1894
1967
|
TransportMessageSchema
|
|
1895
1968
|
});
|