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