@replit/river 0.11.0 → 0.12.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 +28 -8
- package/dist/{builder-1f26296b.d.ts → builder-c593de11.d.ts} +14 -13
- package/dist/{chunk-3JGVFWKQ.js → chunk-24O3BKZA.js} +205 -180
- package/dist/chunk-4HOR4NUO.js +40 -0
- package/dist/chunk-ANGOKBE5.js +708 -0
- package/dist/{chunk-R6H2BIMC.js → chunk-GZ7HCLLM.js} +31 -7
- package/dist/{chunk-T7M7OKPE.js → chunk-H4BYJELI.js} +5 -1
- package/dist/{chunk-GKPT5YQE.js → chunk-IIBVKYDB.js} +6 -34
- package/dist/chunk-IUDKWAOK.js +47 -0
- package/dist/chunk-WTOIOXB7.js +125 -0
- package/dist/chunk-XOTIAXAH.js +44 -0
- package/dist/codec/index.cjs +13 -7
- package/dist/codec/index.js +2 -4
- package/dist/connection-2956a1c5.d.ts +17 -0
- package/dist/connection-aaea7c88.d.ts +15 -0
- package/dist/connection-cd963ed5.d.ts +18 -0
- package/dist/index-d91775d9.d.ts +442 -0
- package/dist/logging/index.cjs +8 -3
- package/dist/logging/index.d.cts +6 -1
- package/dist/logging/index.d.ts +6 -1
- package/dist/logging/index.js +5 -3
- package/dist/messageFraming-b200ef25.d.ts +20 -0
- package/dist/router/index.cjs +249 -211
- package/dist/router/index.d.cts +6 -7
- package/dist/router/index.d.ts +6 -7
- package/dist/router/index.js +3 -3
- package/dist/transport/impls/stdio/client.cjs +891 -0
- package/dist/transport/impls/stdio/client.d.cts +27 -0
- package/dist/transport/impls/stdio/client.d.ts +27 -0
- package/dist/transport/impls/stdio/client.js +42 -0
- package/dist/transport/impls/stdio/server.cjs +861 -0
- package/dist/transport/impls/stdio/server.d.cts +25 -0
- package/dist/transport/impls/stdio/server.d.ts +25 -0
- package/dist/transport/impls/stdio/server.js +33 -0
- package/dist/transport/impls/uds/client.cjs +893 -0
- package/dist/transport/impls/uds/client.d.cts +16 -0
- package/dist/transport/impls/uds/client.d.ts +16 -0
- package/dist/transport/impls/uds/client.js +44 -0
- package/dist/transport/impls/uds/server.cjs +863 -0
- package/dist/transport/impls/uds/server.d.cts +16 -0
- package/dist/transport/impls/uds/server.d.ts +16 -0
- package/dist/transport/impls/uds/server.js +39 -0
- package/dist/transport/impls/ws/client.cjs +587 -248
- package/dist/transport/impls/ws/client.d.cts +6 -21
- package/dist/transport/impls/ws/client.d.ts +6 -21
- package/dist/transport/impls/ws/client.js +77 -7
- package/dist/transport/impls/ws/server.cjs +541 -194
- package/dist/transport/impls/ws/server.d.cts +6 -10
- package/dist/transport/impls/ws/server.d.ts +6 -10
- package/dist/transport/impls/ws/server.js +31 -8
- package/dist/transport/index.cjs +652 -129
- package/dist/transport/index.d.cts +3 -276
- package/dist/transport/index.d.ts +3 -276
- package/dist/transport/index.js +13 -10
- package/dist/util/testHelpers.cjs +40 -602
- package/dist/util/testHelpers.d.cts +18 -37
- package/dist/util/testHelpers.d.ts +18 -37
- package/dist/util/testHelpers.js +27 -47
- package/package.json +29 -14
- package/dist/chunk-5IC5XMWK.js +0 -140
- package/dist/chunk-L7D75G4K.js +0 -29
- package/dist/chunk-LQXPKF3A.js +0 -282
- package/dist/chunk-PJ2EUO7O.js +0 -63
- package/dist/chunk-WVT5QXMZ.js +0 -20
- package/dist/chunk-ZE4MX7DF.js +0 -75
- package/dist/connection-2529fc14.d.ts +0 -10
- package/dist/connection-316d6e3a.d.ts +0 -10
- package/dist/connection-8e19874c.d.ts +0 -11
- package/dist/connection-f7688cc1.d.ts +0 -11
- package/dist/transport/impls/stdio/stdio.cjs +0 -518
- package/dist/transport/impls/stdio/stdio.d.cts +0 -26
- package/dist/transport/impls/stdio/stdio.d.ts +0 -26
- package/dist/transport/impls/stdio/stdio.js +0 -70
- package/dist/transport/impls/unixsocket/client.cjs +0 -516
- package/dist/transport/impls/unixsocket/client.d.cts +0 -16
- package/dist/transport/impls/unixsocket/client.d.ts +0 -16
- package/dist/transport/impls/unixsocket/client.js +0 -67
- package/dist/transport/impls/unixsocket/server.cjs +0 -520
- package/dist/transport/impls/unixsocket/server.d.cts +0 -18
- package/dist/transport/impls/unixsocket/server.d.ts +0 -18
- package/dist/transport/impls/unixsocket/server.js +0 -73
- /package/dist/{chunk-ORAG7IAU.js → chunk-5IZ2UHWV.js} +0 -0
package/dist/transport/index.cjs
CHANGED
|
@@ -20,12 +20,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// transport/index.ts
|
|
21
21
|
var transport_exports = {};
|
|
22
22
|
__export(transport_exports, {
|
|
23
|
+
ClientTransport: () => ClientTransport,
|
|
23
24
|
Connection: () => Connection,
|
|
24
25
|
OpaqueTransportMessageSchema: () => OpaqueTransportMessageSchema,
|
|
26
|
+
ServerTransport: () => ServerTransport,
|
|
27
|
+
Session: () => Session,
|
|
25
28
|
Transport: () => Transport,
|
|
26
|
-
TransportMessageSchema: () => TransportMessageSchema
|
|
27
|
-
msg: () => msg,
|
|
28
|
-
reply: () => reply
|
|
29
|
+
TransportMessageSchema: () => TransportMessageSchema
|
|
29
30
|
});
|
|
30
31
|
module.exports = __toCommonJS(transport_exports);
|
|
31
32
|
|
|
@@ -39,43 +40,84 @@ var TransportMessageSchema = (t) => import_typebox.Type.Object({
|
|
|
39
40
|
id: import_typebox.Type.String(),
|
|
40
41
|
from: import_typebox.Type.String(),
|
|
41
42
|
to: import_typebox.Type.String(),
|
|
43
|
+
seq: import_typebox.Type.Integer(),
|
|
44
|
+
ack: import_typebox.Type.Integer(),
|
|
42
45
|
serviceName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
|
|
43
46
|
procedureName: import_typebox.Type.Optional(import_typebox.Type.Union([import_typebox.Type.String(), import_typebox.Type.Null()])),
|
|
44
47
|
streamId: import_typebox.Type.String(),
|
|
45
48
|
controlFlags: import_typebox.Type.Integer(),
|
|
46
49
|
payload: t
|
|
47
50
|
});
|
|
48
|
-
var
|
|
49
|
-
import_typebox.Type.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
);
|
|
53
|
-
var ControlMessagePayloadSchema = import_typebox.Type.Object({
|
|
51
|
+
var ControlMessageAckSchema = import_typebox.Type.Object({
|
|
52
|
+
type: import_typebox.Type.Literal("ACK")
|
|
53
|
+
});
|
|
54
|
+
var ControlMessageCloseSchema = import_typebox.Type.Object({
|
|
54
55
|
type: import_typebox.Type.Literal("CLOSE")
|
|
55
56
|
});
|
|
57
|
+
var PROTOCOL_VERSION = "v1";
|
|
58
|
+
var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
|
|
59
|
+
type: import_typebox.Type.Literal("HANDSHAKE_REQ"),
|
|
60
|
+
protocolVersion: import_typebox.Type.Literal(PROTOCOL_VERSION)
|
|
61
|
+
});
|
|
62
|
+
var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
|
|
63
|
+
type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
|
|
64
|
+
status: import_typebox.Type.Union([
|
|
65
|
+
import_typebox.Type.Object({
|
|
66
|
+
ok: import_typebox.Type.Literal(true),
|
|
67
|
+
instanceId: import_typebox.Type.String()
|
|
68
|
+
}),
|
|
69
|
+
import_typebox.Type.Object({
|
|
70
|
+
ok: import_typebox.Type.Literal(false),
|
|
71
|
+
reason: import_typebox.Type.Union([import_typebox.Type.Literal("VERSION_MISMATCH")])
|
|
72
|
+
})
|
|
73
|
+
])
|
|
74
|
+
});
|
|
75
|
+
var ControlMessagePayloadSchema = import_typebox.Type.Union([
|
|
76
|
+
ControlMessageCloseSchema,
|
|
77
|
+
ControlMessageAckSchema,
|
|
78
|
+
ControlMessageHandshakeRequestSchema,
|
|
79
|
+
ControlMessageHandshakeResponseSchema
|
|
80
|
+
]);
|
|
56
81
|
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
57
82
|
import_typebox.Type.Unknown()
|
|
58
83
|
);
|
|
59
|
-
function
|
|
84
|
+
function bootRequestMessage(from, to) {
|
|
60
85
|
return {
|
|
61
86
|
id: (0, import_nanoid.nanoid)(),
|
|
62
|
-
to,
|
|
63
87
|
from,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
88
|
+
to,
|
|
89
|
+
seq: 0,
|
|
90
|
+
ack: 0,
|
|
91
|
+
streamId: (0, import_nanoid.nanoid)(),
|
|
67
92
|
controlFlags: 0,
|
|
68
|
-
payload
|
|
93
|
+
payload: {
|
|
94
|
+
type: "HANDSHAKE_REQ",
|
|
95
|
+
protocolVersion: PROTOCOL_VERSION
|
|
96
|
+
}
|
|
69
97
|
};
|
|
70
98
|
}
|
|
71
|
-
function
|
|
99
|
+
function bootResponseMessage(from, instanceId, to, ok) {
|
|
72
100
|
return {
|
|
73
101
|
id: (0, import_nanoid.nanoid)(),
|
|
74
|
-
|
|
102
|
+
from,
|
|
103
|
+
to,
|
|
104
|
+
seq: 0,
|
|
105
|
+
ack: 0,
|
|
106
|
+
streamId: (0, import_nanoid.nanoid)(),
|
|
75
107
|
controlFlags: 0,
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
108
|
+
payload: ok ? {
|
|
109
|
+
type: "HANDSHAKE_RESP",
|
|
110
|
+
status: {
|
|
111
|
+
ok: true,
|
|
112
|
+
instanceId
|
|
113
|
+
}
|
|
114
|
+
} : {
|
|
115
|
+
type: "HANDSHAKE_RESP",
|
|
116
|
+
status: {
|
|
117
|
+
ok: false,
|
|
118
|
+
reason: "VERSION_MISMATCH"
|
|
119
|
+
}
|
|
120
|
+
}
|
|
79
121
|
};
|
|
80
122
|
}
|
|
81
123
|
function isAck(controlFlag) {
|
|
@@ -113,15 +155,280 @@ var EventDispatcher = class {
|
|
|
113
155
|
}
|
|
114
156
|
};
|
|
115
157
|
|
|
116
|
-
// transport/
|
|
158
|
+
// transport/session.ts
|
|
159
|
+
var import_nanoid2 = require("nanoid");
|
|
160
|
+
var nanoid2 = (0, import_nanoid2.customAlphabet)("1234567890abcdefghijklmnopqrstuvxyz", 6);
|
|
161
|
+
var unsafeId = () => nanoid2();
|
|
117
162
|
var Connection = class {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
163
|
+
debugId;
|
|
164
|
+
constructor() {
|
|
165
|
+
this.debugId = `conn-${unsafeId()}`;
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
var HEARTBEAT_INTERVAL_MS = 250;
|
|
169
|
+
var HEARTBEATS_TILL_DEAD = 4;
|
|
170
|
+
var SESSION_DISCONNECT_GRACE_MS = 3e3;
|
|
171
|
+
var Session = class {
|
|
172
|
+
codec;
|
|
173
|
+
/**
|
|
174
|
+
* The buffer of messages that have been sent but not yet acknowledged.
|
|
175
|
+
*/
|
|
176
|
+
sendBuffer = [];
|
|
177
|
+
/**
|
|
178
|
+
* The active connection associated with this session
|
|
179
|
+
*/
|
|
180
|
+
connection;
|
|
181
|
+
from;
|
|
182
|
+
to;
|
|
183
|
+
/**
|
|
184
|
+
* The unique ID of this session.
|
|
185
|
+
*/
|
|
186
|
+
debugId;
|
|
187
|
+
/**
|
|
188
|
+
* Number of messages we've sent along this session (excluding handshake)
|
|
189
|
+
*/
|
|
190
|
+
seq = 0;
|
|
191
|
+
/**
|
|
192
|
+
* Number of unique messages we've received this session (excluding handshake)
|
|
193
|
+
*/
|
|
194
|
+
ack = 0;
|
|
195
|
+
/**
|
|
196
|
+
* The grace period between when the inner connection is disconnected
|
|
197
|
+
* and when we should consider the entire session disconnected.
|
|
198
|
+
*/
|
|
199
|
+
disconnectionGrace;
|
|
200
|
+
/**
|
|
201
|
+
* Number of heartbeats we've sent without a response.
|
|
202
|
+
*/
|
|
203
|
+
heartbeatMisses;
|
|
204
|
+
/**
|
|
205
|
+
* The interval for sending heartbeats.
|
|
206
|
+
*/
|
|
207
|
+
heartbeat;
|
|
208
|
+
constructor(codec, from, connectedTo, conn) {
|
|
209
|
+
this.debugId = `sess-${unsafeId()}`;
|
|
210
|
+
this.from = from;
|
|
211
|
+
this.to = connectedTo;
|
|
212
|
+
this.connection = conn;
|
|
213
|
+
this.codec = codec;
|
|
214
|
+
this.heartbeatMisses = 0;
|
|
215
|
+
this.heartbeat = setInterval(
|
|
216
|
+
() => this.sendHeartbeat(),
|
|
217
|
+
HEARTBEAT_INTERVAL_MS
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Sends a message over the session's connection.
|
|
222
|
+
* If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
|
|
223
|
+
*
|
|
224
|
+
* @param msg The partial message to be sent, which will be constructed into a full message.
|
|
225
|
+
* @param skipRetry Optional. If true, the message will not be buffered for retry on failure. This should only be used for
|
|
226
|
+
* ack hearbeats, which contain information that can already be found in the other buffered messages.
|
|
227
|
+
* @returns The full transport ID of the message that was attempted to be sent.
|
|
228
|
+
*/
|
|
229
|
+
send(msg, skipRetry) {
|
|
230
|
+
const fullMsg = this.constructMsg(msg);
|
|
231
|
+
log?.debug(`${this.from} -- sending ${JSON.stringify(fullMsg)}`);
|
|
232
|
+
if (this.connection) {
|
|
233
|
+
const ok = this.connection.send(this.codec.toBuffer(fullMsg));
|
|
234
|
+
if (ok)
|
|
235
|
+
return fullMsg.id;
|
|
236
|
+
log?.info(
|
|
237
|
+
`${this.from} -- failed to send ${fullMsg.id} to ${fullMsg.to}, connection (id: ${this.connection.debugId}) is probably dead`
|
|
238
|
+
);
|
|
239
|
+
} else {
|
|
240
|
+
log?.info(
|
|
241
|
+
`${this.from} -- failed to send ${fullMsg.id} to ${fullMsg.to}, connection not ready yet`
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
if (skipRetry)
|
|
245
|
+
return fullMsg.id;
|
|
246
|
+
this.addToSendBuff(fullMsg);
|
|
247
|
+
log?.info(
|
|
248
|
+
`${this.from} -- buffering msg ${fullMsg.id} until connection is healthy again`
|
|
249
|
+
);
|
|
250
|
+
return fullMsg.id;
|
|
251
|
+
}
|
|
252
|
+
sendHeartbeat() {
|
|
253
|
+
if (this.heartbeatMisses >= HEARTBEATS_TILL_DEAD && this.connection) {
|
|
254
|
+
log?.info(
|
|
255
|
+
`${this.from} -- closing connection (id: ${this.connection.debugId}) to ${this.to} due to inactivity`
|
|
256
|
+
);
|
|
257
|
+
this.halfCloseConnection();
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
this.send(
|
|
261
|
+
{
|
|
262
|
+
streamId: "heartbeat",
|
|
263
|
+
controlFlags: 1 /* AckBit */,
|
|
264
|
+
payload: {
|
|
265
|
+
type: "ACK"
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
true
|
|
269
|
+
);
|
|
270
|
+
this.heartbeatMisses++;
|
|
271
|
+
}
|
|
272
|
+
resetBufferedMessages() {
|
|
273
|
+
this.sendBuffer = [];
|
|
274
|
+
this.seq = 0;
|
|
275
|
+
this.ack = 0;
|
|
276
|
+
}
|
|
277
|
+
sendBufferedMessages() {
|
|
278
|
+
if (!this.connection) {
|
|
279
|
+
const msg = `${this.from} -- tried sending buffered messages without a connection (if you hit this code path something is seriously wrong)`;
|
|
280
|
+
log?.error(msg);
|
|
281
|
+
throw new Error(msg);
|
|
282
|
+
}
|
|
283
|
+
for (const msg of this.sendBuffer) {
|
|
284
|
+
log?.debug(`${this.from} -- resending ${JSON.stringify(msg)}`);
|
|
285
|
+
const ok = this.connection.send(this.codec.toBuffer(msg));
|
|
286
|
+
if (!ok) {
|
|
287
|
+
const msg2 = `${this.from} -- failed to send buffered message to ${this.to} in session (id: ${this.debugId}) (if you hit this code path something is seriously wrong)`;
|
|
288
|
+
log?.error(msg2);
|
|
289
|
+
throw new Error(msg2);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
updateBookkeeping(ack, seq) {
|
|
294
|
+
this.heartbeatMisses = 0;
|
|
295
|
+
this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq > ack);
|
|
296
|
+
this.ack = seq + 1;
|
|
297
|
+
}
|
|
298
|
+
addToSendBuff(msg) {
|
|
299
|
+
this.sendBuffer.push(msg);
|
|
300
|
+
log?.debug(
|
|
301
|
+
`${this.from} -- send buff to ${this.to} now tracking ${this.sendBuffer.length} messages`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
closeStaleConnection(conn) {
|
|
305
|
+
if (!this.connection || this.connection !== conn)
|
|
306
|
+
return;
|
|
307
|
+
log?.info(
|
|
308
|
+
`${this.from} -- closing old inner connection (id: ${this.connection.debugId}) from session (id: ${this.debugId}) to ${this.to}`
|
|
309
|
+
);
|
|
310
|
+
this.connection.close();
|
|
311
|
+
this.connection = void 0;
|
|
312
|
+
}
|
|
313
|
+
replaceWithNewConnection(newConn) {
|
|
314
|
+
this.closeStaleConnection(this.connection);
|
|
315
|
+
this.cancelGrace();
|
|
316
|
+
this.connection = newConn;
|
|
317
|
+
}
|
|
318
|
+
beginGrace(cb) {
|
|
319
|
+
this.disconnectionGrace = setTimeout(() => {
|
|
320
|
+
this.resetBufferedMessages();
|
|
321
|
+
clearInterval(this.heartbeat);
|
|
322
|
+
cb();
|
|
323
|
+
}, SESSION_DISCONNECT_GRACE_MS);
|
|
324
|
+
}
|
|
325
|
+
cancelGrace() {
|
|
326
|
+
clearTimeout(this.disconnectionGrace);
|
|
327
|
+
}
|
|
328
|
+
get connected() {
|
|
329
|
+
return this.connection !== void 0;
|
|
330
|
+
}
|
|
331
|
+
get nextExpectedSeq() {
|
|
332
|
+
return this.ack;
|
|
333
|
+
}
|
|
334
|
+
constructMsg(partialMsg) {
|
|
335
|
+
const msg = {
|
|
336
|
+
...partialMsg,
|
|
337
|
+
id: unsafeId(),
|
|
338
|
+
to: this.to,
|
|
339
|
+
from: this.from,
|
|
340
|
+
seq: this.seq,
|
|
341
|
+
ack: this.ack
|
|
342
|
+
};
|
|
343
|
+
this.seq++;
|
|
344
|
+
return msg;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Closes the out-going connection but doesn't remove the listeners
|
|
348
|
+
* for incoming messages. The connection will eventually call onClose
|
|
349
|
+
* when it is ready to be cleaned up and only then will {@link connection} be set back
|
|
350
|
+
* to undefined
|
|
351
|
+
*/
|
|
352
|
+
halfCloseConnection() {
|
|
353
|
+
this.connection?.close();
|
|
354
|
+
}
|
|
355
|
+
inspectSendBuffer() {
|
|
356
|
+
return this.sendBuffer;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// codec/json.ts
|
|
361
|
+
var encoder = new TextEncoder();
|
|
362
|
+
var decoder = new TextDecoder();
|
|
363
|
+
function uint8ArrayToBase64(uint8Array) {
|
|
364
|
+
let binary = "";
|
|
365
|
+
uint8Array.forEach((byte) => {
|
|
366
|
+
binary += String.fromCharCode(byte);
|
|
367
|
+
});
|
|
368
|
+
return btoa(binary);
|
|
369
|
+
}
|
|
370
|
+
function base64ToUint8Array(base64) {
|
|
371
|
+
const binaryString = atob(base64);
|
|
372
|
+
const uint8Array = new Uint8Array(binaryString.length);
|
|
373
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
374
|
+
uint8Array[i] = binaryString.charCodeAt(i);
|
|
375
|
+
}
|
|
376
|
+
return uint8Array;
|
|
377
|
+
}
|
|
378
|
+
var NaiveJsonCodec = {
|
|
379
|
+
toBuffer: (obj) => {
|
|
380
|
+
return encoder.encode(
|
|
381
|
+
JSON.stringify(obj, function replacer(key) {
|
|
382
|
+
const val = this[key];
|
|
383
|
+
if (val instanceof Uint8Array) {
|
|
384
|
+
return { $t: uint8ArrayToBase64(val) };
|
|
385
|
+
} else {
|
|
386
|
+
return val;
|
|
387
|
+
}
|
|
388
|
+
})
|
|
389
|
+
);
|
|
390
|
+
},
|
|
391
|
+
fromBuffer: (buff) => {
|
|
392
|
+
try {
|
|
393
|
+
const parsed = JSON.parse(
|
|
394
|
+
decoder.decode(buff),
|
|
395
|
+
function reviver(_key, val) {
|
|
396
|
+
if (val?.$t) {
|
|
397
|
+
return base64ToUint8Array(val.$t);
|
|
398
|
+
} else {
|
|
399
|
+
return val;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
);
|
|
403
|
+
if (typeof parsed === "object")
|
|
404
|
+
return parsed;
|
|
405
|
+
return null;
|
|
406
|
+
} catch {
|
|
407
|
+
return null;
|
|
408
|
+
}
|
|
123
409
|
}
|
|
124
410
|
};
|
|
411
|
+
|
|
412
|
+
// transport/transport.ts
|
|
413
|
+
var import_nanoid3 = require("nanoid");
|
|
414
|
+
|
|
415
|
+
// util/stringify.ts
|
|
416
|
+
function coerceErrorString(err) {
|
|
417
|
+
if (err instanceof Error) {
|
|
418
|
+
return err.message;
|
|
419
|
+
}
|
|
420
|
+
return `[coerced to error] ${String(err)}`;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// transport/transport.ts
|
|
424
|
+
var DEFAULT_RECONNECT_JITTER_MAX_MS = 500;
|
|
425
|
+
var DEFAULT_RECONNECT_INTERVAL_MS = 250;
|
|
426
|
+
var defaultTransportOptions = {
|
|
427
|
+
retryIntervalMs: DEFAULT_RECONNECT_INTERVAL_MS,
|
|
428
|
+
retryJitterMs: DEFAULT_RECONNECT_JITTER_MAX_MS,
|
|
429
|
+
retryAttemptsMax: 5,
|
|
430
|
+
codec: NaiveJsonCodec
|
|
431
|
+
};
|
|
125
432
|
var Transport = class {
|
|
126
433
|
/**
|
|
127
434
|
* A flag indicating whether the transport has been destroyed.
|
|
@@ -137,103 +444,128 @@ var Transport = class {
|
|
|
137
444
|
*/
|
|
138
445
|
clientId;
|
|
139
446
|
/**
|
|
140
|
-
*
|
|
141
|
-
* This builds up if the WebSocket is down for a period of time.
|
|
142
|
-
*/
|
|
143
|
-
sendQueue;
|
|
144
|
-
/**
|
|
145
|
-
* The buffer of messages that have been sent but not yet acknowledged.
|
|
447
|
+
* The map of {@link Session}s managed by this transport.
|
|
146
448
|
*/
|
|
147
|
-
|
|
449
|
+
sessions;
|
|
148
450
|
/**
|
|
149
451
|
* The map of {@link Connection}s managed by this transport.
|
|
150
452
|
*/
|
|
151
|
-
connections
|
|
453
|
+
get connections() {
|
|
454
|
+
return new Map(
|
|
455
|
+
[...this.sessions].map(([client, session]) => [client, session.connection]).filter((entry) => entry[1] !== void 0)
|
|
456
|
+
);
|
|
457
|
+
}
|
|
152
458
|
/**
|
|
153
459
|
* The event dispatcher for handling events of type EventTypes.
|
|
154
460
|
*/
|
|
155
461
|
eventDispatcher;
|
|
462
|
+
/**
|
|
463
|
+
* The options for this transport.
|
|
464
|
+
*/
|
|
465
|
+
options;
|
|
156
466
|
/**
|
|
157
467
|
* Creates a new Transport instance.
|
|
158
468
|
* This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
|
|
159
469
|
* @param codec The codec used to encode and decode messages.
|
|
160
470
|
* @param clientId The client ID of this transport.
|
|
161
471
|
*/
|
|
162
|
-
constructor(
|
|
472
|
+
constructor(clientId, providedOptions) {
|
|
473
|
+
this.options = { ...defaultTransportOptions, ...providedOptions };
|
|
163
474
|
this.eventDispatcher = new EventDispatcher();
|
|
164
|
-
this.
|
|
165
|
-
this.
|
|
166
|
-
this.connections = /* @__PURE__ */ new Map();
|
|
167
|
-
this.codec = codec;
|
|
475
|
+
this.sessions = /* @__PURE__ */ new Map();
|
|
476
|
+
this.codec = this.options.codec;
|
|
168
477
|
this.clientId = clientId;
|
|
169
478
|
this.state = "open";
|
|
170
479
|
}
|
|
480
|
+
sessionByClientId(clientId) {
|
|
481
|
+
const session = this.sessions.get(clientId);
|
|
482
|
+
if (!session) {
|
|
483
|
+
const err = `${this.clientId} -- (invariant violation) no existing session for ${clientId}`;
|
|
484
|
+
log?.error(err);
|
|
485
|
+
throw new Error(err);
|
|
486
|
+
}
|
|
487
|
+
return session;
|
|
488
|
+
}
|
|
171
489
|
/**
|
|
172
|
-
*
|
|
490
|
+
* Called when a new connection is established
|
|
491
|
+
* and we know the identity of the connected client.
|
|
173
492
|
* @param conn The connection object.
|
|
174
493
|
*/
|
|
175
|
-
onConnect(conn) {
|
|
176
|
-
log?.info(`${this.clientId} -- new connection to ${conn.connectedTo}`);
|
|
177
|
-
this.connections.set(conn.connectedTo, conn);
|
|
494
|
+
onConnect(conn, connectedTo) {
|
|
178
495
|
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
179
496
|
status: "connect",
|
|
180
497
|
conn
|
|
181
498
|
});
|
|
182
|
-
const
|
|
183
|
-
if (
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
log?.warn(
|
|
190
|
-
`${this.clientId} -- tried to resend a message we received an ack for`
|
|
191
|
-
);
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
this.send(msg2);
|
|
499
|
+
const session = this.sessions.get(connectedTo);
|
|
500
|
+
if (session === void 0) {
|
|
501
|
+
const newSession = this.createSession(connectedTo, conn);
|
|
502
|
+
log?.info(
|
|
503
|
+
`${this.clientId} -- new connection (id: ${conn.debugId}) for new session (id: ${newSession.debugId}) to ${connectedTo}`
|
|
504
|
+
);
|
|
505
|
+
return newSession;
|
|
195
506
|
}
|
|
196
|
-
|
|
507
|
+
log?.info(
|
|
508
|
+
`${this.clientId} -- new connection (id: ${conn.debugId}) for existing session (id: ${session.debugId}) to ${connectedTo}`
|
|
509
|
+
);
|
|
510
|
+
session.replaceWithNewConnection(conn);
|
|
511
|
+
session.sendBufferedMessages();
|
|
512
|
+
return session;
|
|
513
|
+
}
|
|
514
|
+
createSession(connectedTo, conn) {
|
|
515
|
+
const session = new Session(
|
|
516
|
+
this.codec,
|
|
517
|
+
this.clientId,
|
|
518
|
+
connectedTo,
|
|
519
|
+
conn
|
|
520
|
+
);
|
|
521
|
+
this.sessions.set(session.to, session);
|
|
522
|
+
this.eventDispatcher.dispatchEvent("sessionStatus", {
|
|
523
|
+
status: "connect",
|
|
524
|
+
session
|
|
525
|
+
});
|
|
526
|
+
return session;
|
|
527
|
+
}
|
|
528
|
+
deleteSession(session) {
|
|
529
|
+
this.sessions.delete(session.to);
|
|
530
|
+
this.eventDispatcher.dispatchEvent("sessionStatus", {
|
|
531
|
+
status: "disconnect",
|
|
532
|
+
session
|
|
533
|
+
});
|
|
534
|
+
log?.info(
|
|
535
|
+
`${this.clientId} -- session ${session.debugId} disconnect from ${session.to}`
|
|
536
|
+
);
|
|
197
537
|
}
|
|
198
538
|
/**
|
|
199
539
|
* The downstream implementation needs to call this when a connection is closed.
|
|
200
540
|
* @param conn The connection object.
|
|
201
541
|
*/
|
|
202
|
-
onDisconnect(conn) {
|
|
203
|
-
log?.info(`${this.clientId} -- disconnect from ${conn.connectedTo}`);
|
|
204
|
-
conn.close();
|
|
205
|
-
this.connections.delete(conn.connectedTo);
|
|
542
|
+
onDisconnect(conn, connectedTo) {
|
|
206
543
|
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
207
544
|
status: "disconnect",
|
|
208
545
|
conn
|
|
209
546
|
});
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
547
|
+
if (!connectedTo)
|
|
548
|
+
return;
|
|
549
|
+
const session = this.sessionByClientId(connectedTo);
|
|
550
|
+
log?.info(
|
|
551
|
+
`${this.clientId} -- connection (id: ${conn.debugId}) disconnect from ${connectedTo}, ${SESSION_DISCONNECT_GRACE_MS}ms until session (id: ${session.debugId}) disconnect`
|
|
552
|
+
);
|
|
553
|
+
session.closeStaleConnection(conn);
|
|
554
|
+
session.beginGrace(() => this.deleteSession(session));
|
|
217
555
|
}
|
|
218
556
|
/**
|
|
219
557
|
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
220
558
|
* @param msg The message to parse.
|
|
221
559
|
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
222
560
|
*/
|
|
223
|
-
parseMsg(
|
|
224
|
-
const parsedMsg = this.codec.fromBuffer(
|
|
561
|
+
parseMsg(msg) {
|
|
562
|
+
const parsedMsg = this.codec.fromBuffer(msg);
|
|
225
563
|
if (parsedMsg === null) {
|
|
226
|
-
const decodedBuffer = new TextDecoder().decode(
|
|
564
|
+
const decodedBuffer = new TextDecoder().decode(msg);
|
|
227
565
|
log?.warn(`${this.clientId} -- received malformed msg: ${decodedBuffer}`);
|
|
228
566
|
return null;
|
|
229
567
|
}
|
|
230
|
-
if (import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
231
|
-
return {
|
|
232
|
-
...parsedMsg,
|
|
233
|
-
serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
|
|
234
|
-
procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
|
|
235
|
-
};
|
|
236
|
-
} else {
|
|
568
|
+
if (!import_value.Value.Check(OpaqueTransportMessageSchema, parsedMsg)) {
|
|
237
569
|
log?.warn(
|
|
238
570
|
`${this.clientId} -- received invalid msg: ${JSON.stringify(
|
|
239
571
|
parsedMsg
|
|
@@ -241,31 +573,36 @@ var Transport = class {
|
|
|
241
573
|
);
|
|
242
574
|
return null;
|
|
243
575
|
}
|
|
576
|
+
return {
|
|
577
|
+
...parsedMsg,
|
|
578
|
+
serviceName: parsedMsg.serviceName === null ? void 0 : parsedMsg.serviceName,
|
|
579
|
+
procedureName: parsedMsg.procedureName === null ? void 0 : parsedMsg.procedureName
|
|
580
|
+
};
|
|
244
581
|
}
|
|
245
582
|
/**
|
|
246
583
|
* Called when a message is received by this transport.
|
|
247
584
|
* You generally shouldn't need to override this in downstream transport implementations.
|
|
248
585
|
* @param msg The received message.
|
|
249
586
|
*/
|
|
250
|
-
handleMsg(
|
|
251
|
-
if (!
|
|
587
|
+
handleMsg(msg) {
|
|
588
|
+
if (!msg) {
|
|
252
589
|
return;
|
|
253
590
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
591
|
+
const session = this.sessionByClientId(msg.from);
|
|
592
|
+
session.cancelGrace();
|
|
593
|
+
log?.debug(`${this.clientId} -- received msg: ${JSON.stringify(msg)}`);
|
|
594
|
+
if (msg.seq !== session.nextExpectedSeq) {
|
|
595
|
+
log?.warn(
|
|
596
|
+
`${this.clientId} -- received out-of-order msg (got: ${msg.seq}, wanted: ${session.nextExpectedSeq}), discarding: ${JSON.stringify(
|
|
597
|
+
msg
|
|
598
|
+
)}`
|
|
599
|
+
);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
if (!isAck(msg.controlFlags)) {
|
|
603
|
+
this.eventDispatcher.dispatchEvent("message", msg);
|
|
268
604
|
}
|
|
605
|
+
session.updateBookkeeping(msg.ack, msg.seq);
|
|
269
606
|
}
|
|
270
607
|
/**
|
|
271
608
|
* Adds a listener to this transport.
|
|
@@ -287,74 +624,260 @@ var Transport = class {
|
|
|
287
624
|
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
288
625
|
* send the message.
|
|
289
626
|
* @param msg The message to send.
|
|
290
|
-
* @returns The ID of the sent message
|
|
627
|
+
* @returns The ID of the sent message or undefined if it wasn't sent
|
|
291
628
|
*/
|
|
292
|
-
send(
|
|
629
|
+
send(to, msg) {
|
|
293
630
|
if (this.state === "destroyed") {
|
|
294
631
|
const err = "transport is destroyed, cant send";
|
|
295
|
-
log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(
|
|
632
|
+
log?.error(`${this.clientId} -- ` + err + `: ${JSON.stringify(msg)}`);
|
|
296
633
|
throw new Error(err);
|
|
297
634
|
} else if (this.state === "closed") {
|
|
298
635
|
log?.info(
|
|
299
636
|
`${this.clientId} -- transport closed when sending, discarding : ${JSON.stringify(
|
|
300
|
-
|
|
637
|
+
msg
|
|
301
638
|
)}`
|
|
302
639
|
);
|
|
303
|
-
return
|
|
640
|
+
return void 0;
|
|
304
641
|
}
|
|
305
|
-
let
|
|
306
|
-
if (!
|
|
307
|
-
this.
|
|
642
|
+
let session = this.sessions.get(to);
|
|
643
|
+
if (!session) {
|
|
644
|
+
session = this.createSession(to, void 0);
|
|
645
|
+
log?.info(
|
|
646
|
+
`${this.clientId} -- no session for ${to}, created a new one (id: ${session.debugId})`
|
|
647
|
+
);
|
|
308
648
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
649
|
+
return session.send(msg);
|
|
650
|
+
}
|
|
651
|
+
// control helpers
|
|
652
|
+
sendCloseStream(to, streamId) {
|
|
653
|
+
return this.send(to, {
|
|
654
|
+
streamId,
|
|
655
|
+
controlFlags: 4 /* StreamClosedBit */,
|
|
656
|
+
payload: {
|
|
657
|
+
type: "CLOSE"
|
|
314
658
|
}
|
|
315
|
-
}
|
|
316
|
-
log?.info(
|
|
317
|
-
`${this.clientId} -- connection to ${msg2.to} not ready, attempting reconnect and queuing ${JSON.stringify(msg2)}`
|
|
318
|
-
);
|
|
319
|
-
const outstanding = this.sendQueue.get(msg2.to) || [];
|
|
320
|
-
outstanding.push(msg2.id);
|
|
321
|
-
this.sendQueue.set(msg2.to, outstanding);
|
|
322
|
-
this.createNewConnection(msg2.to);
|
|
323
|
-
return msg2.id;
|
|
659
|
+
});
|
|
324
660
|
}
|
|
325
661
|
/**
|
|
326
662
|
* Default close implementation for transports. You should override this in the downstream
|
|
327
663
|
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
328
664
|
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
329
665
|
*/
|
|
330
|
-
|
|
331
|
-
for (const
|
|
332
|
-
|
|
666
|
+
close() {
|
|
667
|
+
for (const session of this.sessions.values()) {
|
|
668
|
+
session.halfCloseConnection();
|
|
333
669
|
}
|
|
334
|
-
this.connections.clear();
|
|
335
670
|
this.state = "closed";
|
|
336
|
-
log?.info(`${this.clientId} -- closed transport`);
|
|
671
|
+
log?.info(`${this.clientId} -- manually closed transport`);
|
|
337
672
|
}
|
|
338
673
|
/**
|
|
339
674
|
* Default destroy implementation for transports. You should override this in the downstream
|
|
340
675
|
* implementation if you need to do any additional cleanup and call super.destroy() at the end.
|
|
341
676
|
* Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
|
|
342
677
|
*/
|
|
343
|
-
|
|
344
|
-
for (const
|
|
345
|
-
|
|
678
|
+
destroy() {
|
|
679
|
+
for (const session of this.sessions.values()) {
|
|
680
|
+
session.closeStaleConnection(session.connection);
|
|
346
681
|
}
|
|
347
|
-
this.connections.clear();
|
|
348
682
|
this.state = "destroyed";
|
|
349
|
-
log?.info(`${this.clientId} -- destroyed transport`);
|
|
683
|
+
log?.info(`${this.clientId} -- manually destroyed transport`);
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
var ClientTransport = class extends Transport {
|
|
687
|
+
/**
|
|
688
|
+
* The map of reconnect promises for each client ID.
|
|
689
|
+
*/
|
|
690
|
+
inflightConnectionPromises;
|
|
691
|
+
tryReconnecting = true;
|
|
692
|
+
serverInstanceIds = /* @__PURE__ */ new Map();
|
|
693
|
+
constructor(clientId, providedOptions) {
|
|
694
|
+
super(clientId, providedOptions);
|
|
695
|
+
this.inflightConnectionPromises = /* @__PURE__ */ new Map();
|
|
696
|
+
}
|
|
697
|
+
handleConnection(conn, to) {
|
|
698
|
+
const bootHandler = this.receiveWithBootSequence(conn, () => {
|
|
699
|
+
conn.removeDataListener(bootHandler);
|
|
700
|
+
conn.addDataListener((data) => this.handleMsg(this.parseMsg(data)));
|
|
701
|
+
});
|
|
702
|
+
conn.addDataListener(bootHandler);
|
|
703
|
+
conn.addCloseListener(() => {
|
|
704
|
+
this.onDisconnect(conn, to);
|
|
705
|
+
void this.connect(to);
|
|
706
|
+
});
|
|
707
|
+
conn.addErrorListener((err) => {
|
|
708
|
+
log?.warn(
|
|
709
|
+
`${this.clientId} -- error in connection (id: ${conn.debugId}) to ${to}: ${coerceErrorString(err)}`
|
|
710
|
+
);
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Manually attempts to connect to a client.
|
|
715
|
+
* @param to The client ID of the node to connect to.
|
|
716
|
+
*/
|
|
717
|
+
async connect(to, attempt = 0) {
|
|
718
|
+
if (this.state !== "open" || !this.tryReconnecting) {
|
|
719
|
+
log?.info(
|
|
720
|
+
`${this.clientId} -- transport state is no longer open, not attempting connection`
|
|
721
|
+
);
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
let reconnectPromise = this.inflightConnectionPromises.get(to);
|
|
725
|
+
if (!reconnectPromise) {
|
|
726
|
+
reconnectPromise = this.createNewOutgoingConnection(to);
|
|
727
|
+
this.inflightConnectionPromises.set(to, reconnectPromise);
|
|
728
|
+
}
|
|
729
|
+
try {
|
|
730
|
+
const conn = await reconnectPromise;
|
|
731
|
+
this.state = "open";
|
|
732
|
+
const requestMsg = bootRequestMessage(this.clientId, to);
|
|
733
|
+
log?.debug(`${this.clientId} -- sending boot handshake to ${to}`);
|
|
734
|
+
conn.send(this.codec.toBuffer(requestMsg));
|
|
735
|
+
} catch (error) {
|
|
736
|
+
const errStr = coerceErrorString(error);
|
|
737
|
+
this.inflightConnectionPromises.delete(to);
|
|
738
|
+
if (attempt >= this.options.retryAttemptsMax) {
|
|
739
|
+
const errMsg = `connection to ${to} failed after ${attempt} attempts (${errStr}), giving up`;
|
|
740
|
+
log?.error(`${this.clientId} -- ${errMsg}`);
|
|
741
|
+
throw new Error(errMsg);
|
|
742
|
+
} else {
|
|
743
|
+
const jitter = Math.floor(Math.random() * this.options.retryJitterMs);
|
|
744
|
+
const backoffMs = this.options.retryIntervalMs * 2 ** attempt + jitter;
|
|
745
|
+
log?.warn(
|
|
746
|
+
`${this.clientId} -- connection to ${to} failed (${errStr}), trying again in ${backoffMs}ms`
|
|
747
|
+
);
|
|
748
|
+
setTimeout(() => void this.connect(to, attempt + 1), backoffMs);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
receiveWithBootSequence(conn, sessionCb) {
|
|
753
|
+
const bootHandler = (data) => {
|
|
754
|
+
const parsed = this.parseMsg(data);
|
|
755
|
+
if (!parsed)
|
|
756
|
+
return;
|
|
757
|
+
if (!import_value.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
|
|
758
|
+
log?.warn(
|
|
759
|
+
`${this.clientId} -- received invalid handshake resp: ${JSON.stringify(parsed)}`
|
|
760
|
+
);
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
if (!parsed.payload.status.ok) {
|
|
764
|
+
log?.warn(
|
|
765
|
+
`${this.clientId} -- received failed handshake resp: ${JSON.stringify(
|
|
766
|
+
parsed
|
|
767
|
+
)}`
|
|
768
|
+
);
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
const oldSession = this.sessions.get(parsed.from);
|
|
772
|
+
const serverInstanceId = parsed.payload.status.instanceId;
|
|
773
|
+
const lastServerInstanceId = this.serverInstanceIds.get(parsed.from);
|
|
774
|
+
if (oldSession && lastServerInstanceId !== serverInstanceId && lastServerInstanceId !== void 0) {
|
|
775
|
+
log?.debug(
|
|
776
|
+
`${this.clientId} -- handshake from ${parsed.from} has different server instance (got: ${serverInstanceId}, last connected to: ${lastServerInstanceId}), starting a new session`
|
|
777
|
+
);
|
|
778
|
+
oldSession.resetBufferedMessages();
|
|
779
|
+
oldSession.closeStaleConnection();
|
|
780
|
+
this.deleteSession(oldSession);
|
|
781
|
+
}
|
|
782
|
+
log?.debug(
|
|
783
|
+
`${this.clientId} -- handshake from ${parsed.from} ok (server instance: ${serverInstanceId})`
|
|
784
|
+
);
|
|
785
|
+
this.serverInstanceIds.set(parsed.from, serverInstanceId);
|
|
786
|
+
sessionCb(this.onConnect(conn, parsed.from));
|
|
787
|
+
};
|
|
788
|
+
return bootHandler;
|
|
789
|
+
}
|
|
790
|
+
onDisconnect(conn, connectedTo) {
|
|
791
|
+
if (connectedTo)
|
|
792
|
+
this.inflightConnectionPromises.delete(connectedTo);
|
|
793
|
+
super.onDisconnect(conn, connectedTo);
|
|
794
|
+
}
|
|
795
|
+
};
|
|
796
|
+
var ServerTransport = class extends Transport {
|
|
797
|
+
/**
|
|
798
|
+
* Unique per instance of server transport.
|
|
799
|
+
* This allows us to distinguish reconnects to different
|
|
800
|
+
* server transports at the same reachable address and signal
|
|
801
|
+
* the appropriate session disconnect back to the client at the
|
|
802
|
+
* boot stage.
|
|
803
|
+
*/
|
|
804
|
+
instanceId = (0, import_nanoid3.nanoid)();
|
|
805
|
+
constructor(clientId, providedOptions) {
|
|
806
|
+
super(clientId, providedOptions);
|
|
807
|
+
log?.info(
|
|
808
|
+
`${this.clientId} -- initiated server transport (instance id: ${this.instanceId}, protocol: ${PROTOCOL_VERSION})`
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
handleConnection(conn) {
|
|
812
|
+
let session = void 0;
|
|
813
|
+
const client = () => session?.to ?? "unknown";
|
|
814
|
+
const bootHandler = this.receiveWithBootSequence(
|
|
815
|
+
conn,
|
|
816
|
+
(establishedSession) => {
|
|
817
|
+
session = establishedSession;
|
|
818
|
+
conn.removeDataListener(bootHandler);
|
|
819
|
+
conn.addDataListener((data) => this.handleMsg(this.parseMsg(data)));
|
|
820
|
+
}
|
|
821
|
+
);
|
|
822
|
+
conn.addDataListener(bootHandler);
|
|
823
|
+
conn.addCloseListener(() => {
|
|
824
|
+
if (!session)
|
|
825
|
+
return;
|
|
826
|
+
log?.info(
|
|
827
|
+
`${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} disconnected`
|
|
828
|
+
);
|
|
829
|
+
this.onDisconnect(conn, session.to);
|
|
830
|
+
});
|
|
831
|
+
conn.addErrorListener((err) => {
|
|
832
|
+
if (!session)
|
|
833
|
+
return;
|
|
834
|
+
log?.warn(
|
|
835
|
+
`${this.clientId} -- connection (id: ${conn.debugId}) to ${client()} got an error: ${coerceErrorString(err)}`
|
|
836
|
+
);
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
receiveWithBootSequence(conn, sessionCb) {
|
|
840
|
+
const bootHandler = (data) => {
|
|
841
|
+
const parsed = this.parseMsg(data);
|
|
842
|
+
if (!parsed)
|
|
843
|
+
return;
|
|
844
|
+
if (!import_value.Value.Check(ControlMessageHandshakeRequestSchema, parsed.payload)) {
|
|
845
|
+
const responseMsg2 = bootResponseMessage(
|
|
846
|
+
this.clientId,
|
|
847
|
+
this.instanceId,
|
|
848
|
+
parsed.from,
|
|
849
|
+
false
|
|
850
|
+
);
|
|
851
|
+
conn.send(this.codec.toBuffer(responseMsg2));
|
|
852
|
+
log?.warn(
|
|
853
|
+
`${this.clientId} -- received invalid handshake msg: ${JSON.stringify(
|
|
854
|
+
parsed
|
|
855
|
+
)}`
|
|
856
|
+
);
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
log?.debug(
|
|
860
|
+
`${this.clientId} -- handshake from ${parsed.from} ok, responding with handshake success`
|
|
861
|
+
);
|
|
862
|
+
const responseMsg = bootResponseMessage(
|
|
863
|
+
this.clientId,
|
|
864
|
+
this.instanceId,
|
|
865
|
+
parsed.from,
|
|
866
|
+
true
|
|
867
|
+
);
|
|
868
|
+
conn.send(this.codec.toBuffer(responseMsg));
|
|
869
|
+
sessionCb(this.onConnect(conn, parsed.from));
|
|
870
|
+
};
|
|
871
|
+
return bootHandler;
|
|
350
872
|
}
|
|
351
873
|
};
|
|
352
874
|
// Annotate the CommonJS export names for ESM import in node:
|
|
353
875
|
0 && (module.exports = {
|
|
876
|
+
ClientTransport,
|
|
354
877
|
Connection,
|
|
355
878
|
OpaqueTransportMessageSchema,
|
|
879
|
+
ServerTransport,
|
|
880
|
+
Session,
|
|
356
881
|
Transport,
|
|
357
|
-
TransportMessageSchema
|
|
358
|
-
msg,
|
|
359
|
-
reply
|
|
882
|
+
TransportMessageSchema
|
|
360
883
|
});
|