@replit/river 0.18.5 → 0.19.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-VH3NGOXQ.js → chunk-D5PVGZPQ.js} +5 -3
- package/dist/{chunk-K7CUSLWL.js → chunk-JH275HID.js} +1 -1
- package/dist/{chunk-WER2DWCP.js → chunk-NBE3D667.js} +0 -4
- package/dist/{chunk-6Q3MSICL.js → chunk-SR4DBLJ6.js} +11 -3
- package/dist/{chunk-UABIFWM7.js → chunk-YFPVQTWL.js} +220 -105
- package/dist/{chunk-PUX3U2SZ.js → chunk-ZWPEZS27.js} +1 -1
- package/dist/{connection-893bd769.d.ts → connection-aa0ea000.d.ts} +1 -1
- package/dist/{connection-89918b74.d.ts → connection-cfec12e6.d.ts} +1 -1
- package/dist/index-e2513701.d.ts +342 -0
- package/dist/logging/index.cjs +0 -4
- package/dist/logging/index.d.cts +2 -1
- package/dist/logging/index.d.ts +2 -1
- package/dist/logging/index.js +1 -1
- package/dist/router/index.cjs +11 -2
- package/dist/router/index.d.cts +7 -383
- package/dist/router/index.d.ts +7 -383
- package/dist/router/index.js +3 -3
- package/dist/services-4bba42d8.d.ts +736 -0
- package/dist/services-5fc5712d.d.ts +736 -0
- package/dist/transport/impls/uds/client.cjs +80 -53
- package/dist/transport/impls/uds/client.d.cts +5 -5
- package/dist/transport/impls/uds/client.d.ts +5 -5
- package/dist/transport/impls/uds/client.js +4 -4
- package/dist/transport/impls/uds/server.cjs +151 -62
- 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 +4 -4
- package/dist/transport/impls/ws/client.cjs +80 -53
- package/dist/transport/impls/ws/client.d.cts +3 -3
- package/dist/transport/impls/ws/client.d.ts +3 -3
- package/dist/transport/impls/ws/client.js +4 -4
- package/dist/transport/impls/ws/server.cjs +151 -62
- 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 +4 -4
- package/dist/transport/index.cjs +188 -71
- package/dist/transport/index.d.cts +295 -3
- package/dist/transport/index.d.ts +295 -3
- package/dist/transport/index.js +3 -4
- package/dist/util/testHelpers.cjs +410 -401
- package/dist/util/testHelpers.d.cts +3 -3
- package/dist/util/testHelpers.d.ts +3 -3
- package/dist/util/testHelpers.js +4 -5
- package/package.json +1 -1
- package/dist/chunk-RPIDSIQG.js +0 -0
- package/dist/index-46ed19d8.d.ts +0 -111
- package/dist/index-d412ca83.d.ts +0 -420
- package/dist/procedures-bfffcb0b.d.ts +0 -324
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
UdsConnection
|
|
3
|
-
} from "../../../chunk-
|
|
3
|
+
} from "../../../chunk-ZWPEZS27.js";
|
|
4
4
|
import {
|
|
5
5
|
ServerTransport
|
|
6
|
-
} from "../../../chunk-
|
|
7
|
-
import "../../../chunk-
|
|
8
|
-
import "../../../chunk-
|
|
6
|
+
} from "../../../chunk-YFPVQTWL.js";
|
|
7
|
+
import "../../../chunk-D5PVGZPQ.js";
|
|
8
|
+
import "../../../chunk-NBE3D667.js";
|
|
9
9
|
import "../../../chunk-GZ7HCLLM.js";
|
|
10
10
|
|
|
11
11
|
// transport/impls/uds/server.ts
|
|
@@ -52,7 +52,8 @@ var PROTOCOL_VERSION = "v1.1";
|
|
|
52
52
|
var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
|
|
53
53
|
type: import_typebox.Type.Literal("HANDSHAKE_REQ"),
|
|
54
54
|
protocolVersion: import_typebox.Type.String(),
|
|
55
|
-
sessionId: import_typebox.Type.String()
|
|
55
|
+
sessionId: import_typebox.Type.String(),
|
|
56
|
+
metadata: import_typebox.Type.Optional(import_typebox.Type.Unknown())
|
|
56
57
|
});
|
|
57
58
|
var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
|
|
58
59
|
type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
|
|
@@ -76,7 +77,7 @@ var ControlMessagePayloadSchema = import_typebox.Type.Union([
|
|
|
76
77
|
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
77
78
|
import_typebox.Type.Unknown()
|
|
78
79
|
);
|
|
79
|
-
function handshakeRequestMessage(from, to, sessionId) {
|
|
80
|
+
function handshakeRequestMessage(from, to, sessionId, metadata) {
|
|
80
81
|
return {
|
|
81
82
|
id: (0, import_nanoid.nanoid)(),
|
|
82
83
|
from,
|
|
@@ -88,7 +89,8 @@ function handshakeRequestMessage(from, to, sessionId) {
|
|
|
88
89
|
payload: {
|
|
89
90
|
type: "HANDSHAKE_REQ",
|
|
90
91
|
protocolVersion: PROTOCOL_VERSION,
|
|
91
|
-
sessionId
|
|
92
|
+
sessionId,
|
|
93
|
+
metadata
|
|
92
94
|
}
|
|
93
95
|
};
|
|
94
96
|
}
|
|
@@ -165,6 +167,12 @@ var Session = class {
|
|
|
165
167
|
* for this session.
|
|
166
168
|
*/
|
|
167
169
|
advertisedSessionId;
|
|
170
|
+
/**
|
|
171
|
+
* The metadata for this session, as parsed from the handshake.
|
|
172
|
+
*
|
|
173
|
+
* Will only ever be populated on the server side.
|
|
174
|
+
*/
|
|
175
|
+
metadata;
|
|
168
176
|
/**
|
|
169
177
|
* Number of messages we've sent along this session (excluding handshake and acks)
|
|
170
178
|
*/
|
|
@@ -267,27 +275,24 @@ var Session = class {
|
|
|
267
275
|
this.seq = 0;
|
|
268
276
|
this.ack = 0;
|
|
269
277
|
}
|
|
270
|
-
sendBufferedMessages() {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
276
|
-
log?.info(
|
|
277
|
-
`resending ${this.sendBuffer.length} buffered messages`,
|
|
278
|
-
this.loggingMetadata
|
|
279
|
-
);
|
|
278
|
+
sendBufferedMessages(conn) {
|
|
279
|
+
log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
|
|
280
|
+
...this.loggingMetadata,
|
|
281
|
+
connId: conn.debugId
|
|
282
|
+
});
|
|
280
283
|
for (const msg of this.sendBuffer) {
|
|
281
284
|
log?.debug(`resending msg`, {
|
|
282
285
|
...this.loggingMetadata,
|
|
283
|
-
fullTransportMessage: msg
|
|
286
|
+
fullTransportMessage: msg,
|
|
287
|
+
connId: conn.debugId
|
|
284
288
|
});
|
|
285
|
-
const ok =
|
|
289
|
+
const ok = conn.send(this.codec.toBuffer(msg));
|
|
286
290
|
if (!ok) {
|
|
287
|
-
const errMsg = `failed to send buffered message to ${this.to} (
|
|
291
|
+
const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;
|
|
288
292
|
log?.error(errMsg, {
|
|
289
293
|
...this.loggingMetadata,
|
|
290
|
-
fullTransportMessage: msg
|
|
294
|
+
fullTransportMessage: msg,
|
|
295
|
+
connId: conn.debugId
|
|
291
296
|
});
|
|
292
297
|
throw new Error(errMsg);
|
|
293
298
|
}
|
|
@@ -318,6 +323,7 @@ var Session = class {
|
|
|
318
323
|
this.closeStaleConnection(newConn);
|
|
319
324
|
this.cancelGrace();
|
|
320
325
|
this.connection = newConn;
|
|
326
|
+
this.sendBufferedMessages(newConn);
|
|
321
327
|
}
|
|
322
328
|
beginGrace(cb) {
|
|
323
329
|
log?.info(
|
|
@@ -516,6 +522,9 @@ var defaultClientTransportOptions = {
|
|
|
516
522
|
...defaultTransportOptions,
|
|
517
523
|
...defaultConnectionRetryOptions
|
|
518
524
|
};
|
|
525
|
+
var defaultServerTransportOptions = {
|
|
526
|
+
...defaultTransportOptions
|
|
527
|
+
};
|
|
519
528
|
var Transport = class {
|
|
520
529
|
/**
|
|
521
530
|
* A flag indicating whether the transport has been destroyed.
|
|
@@ -569,37 +578,15 @@ var Transport = class {
|
|
|
569
578
|
* and we know the identity of the connected client.
|
|
570
579
|
* @param conn The connection object.
|
|
571
580
|
*/
|
|
572
|
-
onConnect(conn, connectedTo,
|
|
581
|
+
onConnect(conn, connectedTo, session, isReconnect) {
|
|
573
582
|
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
574
583
|
status: "connect",
|
|
575
584
|
conn
|
|
576
585
|
});
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
log?.
|
|
580
|
-
`connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), killing old session and starting a new one`,
|
|
581
|
-
oldSession.loggingMetadata
|
|
582
|
-
);
|
|
583
|
-
this.deleteSession(oldSession);
|
|
584
|
-
oldSession = void 0;
|
|
585
|
-
}
|
|
586
|
-
if (oldSession === void 0) {
|
|
587
|
-
const newSession = this.createSession(connectedTo, conn);
|
|
588
|
-
newSession.advertisedSessionId = advertisedSessionId;
|
|
589
|
-
log?.info(
|
|
590
|
-
`new connection for new session to ${connectedTo}`,
|
|
591
|
-
newSession.loggingMetadata
|
|
592
|
-
);
|
|
593
|
-
return newSession;
|
|
586
|
+
if (isReconnect) {
|
|
587
|
+
session.replaceWithNewConnection(conn);
|
|
588
|
+
log?.info(`reconnected to ${connectedTo}`, session.loggingMetadata);
|
|
594
589
|
}
|
|
595
|
-
oldSession.replaceWithNewConnection(conn);
|
|
596
|
-
oldSession.sendBufferedMessages();
|
|
597
|
-
oldSession.advertisedSessionId = advertisedSessionId;
|
|
598
|
-
log?.info(
|
|
599
|
-
`new connection for existing session to ${connectedTo}`,
|
|
600
|
-
oldSession.loggingMetadata
|
|
601
|
-
);
|
|
602
|
-
return oldSession;
|
|
603
590
|
}
|
|
604
591
|
createSession(to, conn) {
|
|
605
592
|
const session = new Session(
|
|
@@ -615,8 +602,18 @@ var Transport = class {
|
|
|
615
602
|
});
|
|
616
603
|
return session;
|
|
617
604
|
}
|
|
618
|
-
getOrCreateSession(to, conn) {
|
|
605
|
+
getOrCreateSession(to, conn, sessionId) {
|
|
619
606
|
let session = this.sessions.get(to);
|
|
607
|
+
let isReconnect = session !== void 0;
|
|
608
|
+
if (session?.advertisedSessionId !== void 0 && sessionId !== void 0 && session.advertisedSessionId !== sessionId) {
|
|
609
|
+
log?.warn(
|
|
610
|
+
`session for ${to} already exists but has a different session id (expected: ${session.advertisedSessionId}, got: ${sessionId}), creating a new one`,
|
|
611
|
+
session.loggingMetadata
|
|
612
|
+
);
|
|
613
|
+
this.deleteSession(session);
|
|
614
|
+
isReconnect = false;
|
|
615
|
+
session = void 0;
|
|
616
|
+
}
|
|
620
617
|
if (!session) {
|
|
621
618
|
session = this.createSession(to, conn);
|
|
622
619
|
log?.info(
|
|
@@ -624,7 +621,10 @@ var Transport = class {
|
|
|
624
621
|
session.loggingMetadata
|
|
625
622
|
);
|
|
626
623
|
}
|
|
627
|
-
|
|
624
|
+
if (sessionId !== void 0) {
|
|
625
|
+
session.advertisedSessionId = sessionId;
|
|
626
|
+
}
|
|
627
|
+
return { session, isReconnect };
|
|
628
628
|
}
|
|
629
629
|
deleteSession(session) {
|
|
630
630
|
session.close();
|
|
@@ -759,7 +759,7 @@ var Transport = class {
|
|
|
759
759
|
});
|
|
760
760
|
return void 0;
|
|
761
761
|
}
|
|
762
|
-
return this.getOrCreateSession(to).send(msg);
|
|
762
|
+
return this.getOrCreateSession(to).session.send(msg);
|
|
763
763
|
}
|
|
764
764
|
// control helpers
|
|
765
765
|
sendCloseStream(to, streamId) {
|
|
@@ -918,11 +918,12 @@ var ClientTransport = class extends Transport {
|
|
|
918
918
|
connectedTo: parsed.from,
|
|
919
919
|
fullTransportMessage: parsed
|
|
920
920
|
});
|
|
921
|
-
const session = this.
|
|
922
|
-
conn,
|
|
921
|
+
const { session, isReconnect } = this.getOrCreateSession(
|
|
923
922
|
parsed.from,
|
|
923
|
+
conn,
|
|
924
924
|
parsed.payload.status.sessionId
|
|
925
925
|
);
|
|
926
|
+
this.onConnect(conn, parsed.from, session, isReconnect);
|
|
926
927
|
this.retryBudget.startRestoringBudget(parsed.from);
|
|
927
928
|
return session;
|
|
928
929
|
}
|
|
@@ -975,8 +976,13 @@ var ClientTransport = class extends Transport {
|
|
|
975
976
|
conn.close();
|
|
976
977
|
throw new Error("transport state is no longer open");
|
|
977
978
|
}
|
|
978
|
-
this.sendHandshake(to, conn)
|
|
979
|
-
|
|
979
|
+
return this.sendHandshake(to, conn).then((ok) => {
|
|
980
|
+
if (!ok) {
|
|
981
|
+
conn.close();
|
|
982
|
+
throw new Error("failed to send handshake");
|
|
983
|
+
}
|
|
984
|
+
return conn;
|
|
985
|
+
});
|
|
980
986
|
});
|
|
981
987
|
this.inflightConnectionPromises.set(to, reconnectPromise);
|
|
982
988
|
} else {
|
|
@@ -1008,14 +1014,35 @@ var ClientTransport = class extends Transport {
|
|
|
1008
1014
|
this.inflightConnectionPromises.delete(session.to);
|
|
1009
1015
|
super.deleteSession(session);
|
|
1010
1016
|
}
|
|
1011
|
-
sendHandshake(to, conn) {
|
|
1012
|
-
|
|
1013
|
-
|
|
1017
|
+
async sendHandshake(to, conn) {
|
|
1018
|
+
let metadata;
|
|
1019
|
+
if (this.options.handshake) {
|
|
1020
|
+
metadata = await this.options.handshake.get();
|
|
1021
|
+
if (!import_value.Value.Check(this.options.handshake.schema, metadata)) {
|
|
1022
|
+
log?.error(`handshake metadata did not match schema`, {
|
|
1023
|
+
clientId: this.clientId,
|
|
1024
|
+
connectedTo: to
|
|
1025
|
+
});
|
|
1026
|
+
this.protocolError(
|
|
1027
|
+
ProtocolError.HandshakeFailed,
|
|
1028
|
+
"handshake metadata did not match schema"
|
|
1029
|
+
);
|
|
1030
|
+
return false;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
const { session } = this.getOrCreateSession(to, conn);
|
|
1034
|
+
const requestMsg = handshakeRequestMessage(
|
|
1035
|
+
this.clientId,
|
|
1036
|
+
to,
|
|
1037
|
+
session.id,
|
|
1038
|
+
metadata
|
|
1039
|
+
);
|
|
1014
1040
|
log?.debug(`sending handshake request to ${to}`, {
|
|
1015
1041
|
clientId: this.clientId,
|
|
1016
1042
|
connectedTo: to
|
|
1017
1043
|
});
|
|
1018
1044
|
conn.send(this.codec.toBuffer(requestMsg));
|
|
1045
|
+
return true;
|
|
1019
1046
|
}
|
|
1020
1047
|
close() {
|
|
1021
1048
|
this.retryBudget.close();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
|
-
import {
|
|
3
|
-
import { T as TransportClientId } from '../../../index-
|
|
4
|
-
import { W as WebSocketConnection } from '../../../connection-
|
|
2
|
+
import { ClientTransport, ClientTransportOptions as ProvidedClientTransportOptions } from '../../index.cjs';
|
|
3
|
+
import { T as TransportClientId } from '../../../index-e2513701.js';
|
|
4
|
+
import { W as WebSocketConnection } from '../../../connection-aa0ea000.js';
|
|
5
5
|
import '../../../types-3e5768ec.js';
|
|
6
6
|
import '@sinclair/typebox';
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
|
-
import {
|
|
3
|
-
import { T as TransportClientId } from '../../../index-
|
|
4
|
-
import { W as WebSocketConnection } from '../../../connection-
|
|
2
|
+
import { ClientTransport, ClientTransportOptions as ProvidedClientTransportOptions } from '../../index.js';
|
|
3
|
+
import { T as TransportClientId } from '../../../index-e2513701.js';
|
|
4
|
+
import { W as WebSocketConnection } from '../../../connection-aa0ea000.js';
|
|
5
5
|
import '../../../types-3e5768ec.js';
|
|
6
6
|
import '@sinclair/typebox';
|
|
7
7
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
WebSocketConnection
|
|
3
|
-
} from "../../../chunk-
|
|
3
|
+
} from "../../../chunk-JH275HID.js";
|
|
4
4
|
import {
|
|
5
5
|
ClientTransport
|
|
6
|
-
} from "../../../chunk-
|
|
7
|
-
import "../../../chunk-
|
|
6
|
+
} from "../../../chunk-YFPVQTWL.js";
|
|
7
|
+
import "../../../chunk-D5PVGZPQ.js";
|
|
8
8
|
import {
|
|
9
9
|
log
|
|
10
|
-
} from "../../../chunk-
|
|
10
|
+
} from "../../../chunk-NBE3D667.js";
|
|
11
11
|
import "../../../chunk-GZ7HCLLM.js";
|
|
12
12
|
|
|
13
13
|
// transport/impls/ws/client.ts
|
|
@@ -52,7 +52,8 @@ var PROTOCOL_VERSION = "v1.1";
|
|
|
52
52
|
var ControlMessageHandshakeRequestSchema = import_typebox.Type.Object({
|
|
53
53
|
type: import_typebox.Type.Literal("HANDSHAKE_REQ"),
|
|
54
54
|
protocolVersion: import_typebox.Type.String(),
|
|
55
|
-
sessionId: import_typebox.Type.String()
|
|
55
|
+
sessionId: import_typebox.Type.String(),
|
|
56
|
+
metadata: import_typebox.Type.Optional(import_typebox.Type.Unknown())
|
|
56
57
|
});
|
|
57
58
|
var ControlMessageHandshakeResponseSchema = import_typebox.Type.Object({
|
|
58
59
|
type: import_typebox.Type.Literal("HANDSHAKE_RESP"),
|
|
@@ -164,6 +165,12 @@ var Session = class {
|
|
|
164
165
|
* for this session.
|
|
165
166
|
*/
|
|
166
167
|
advertisedSessionId;
|
|
168
|
+
/**
|
|
169
|
+
* The metadata for this session, as parsed from the handshake.
|
|
170
|
+
*
|
|
171
|
+
* Will only ever be populated on the server side.
|
|
172
|
+
*/
|
|
173
|
+
metadata;
|
|
167
174
|
/**
|
|
168
175
|
* Number of messages we've sent along this session (excluding handshake and acks)
|
|
169
176
|
*/
|
|
@@ -266,27 +273,24 @@ var Session = class {
|
|
|
266
273
|
this.seq = 0;
|
|
267
274
|
this.ack = 0;
|
|
268
275
|
}
|
|
269
|
-
sendBufferedMessages() {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
log?.info(
|
|
276
|
-
`resending ${this.sendBuffer.length} buffered messages`,
|
|
277
|
-
this.loggingMetadata
|
|
278
|
-
);
|
|
276
|
+
sendBufferedMessages(conn) {
|
|
277
|
+
log?.info(`resending ${this.sendBuffer.length} buffered messages`, {
|
|
278
|
+
...this.loggingMetadata,
|
|
279
|
+
connId: conn.debugId
|
|
280
|
+
});
|
|
279
281
|
for (const msg of this.sendBuffer) {
|
|
280
282
|
log?.debug(`resending msg`, {
|
|
281
283
|
...this.loggingMetadata,
|
|
282
|
-
fullTransportMessage: msg
|
|
284
|
+
fullTransportMessage: msg,
|
|
285
|
+
connId: conn.debugId
|
|
283
286
|
});
|
|
284
|
-
const ok =
|
|
287
|
+
const ok = conn.send(this.codec.toBuffer(msg));
|
|
285
288
|
if (!ok) {
|
|
286
|
-
const errMsg = `failed to send buffered message to ${this.to} (
|
|
289
|
+
const errMsg = `failed to send buffered message to ${this.to} (sus, this is a fresh connection)`;
|
|
287
290
|
log?.error(errMsg, {
|
|
288
291
|
...this.loggingMetadata,
|
|
289
|
-
fullTransportMessage: msg
|
|
292
|
+
fullTransportMessage: msg,
|
|
293
|
+
connId: conn.debugId
|
|
290
294
|
});
|
|
291
295
|
throw new Error(errMsg);
|
|
292
296
|
}
|
|
@@ -317,6 +321,7 @@ var Session = class {
|
|
|
317
321
|
this.closeStaleConnection(newConn);
|
|
318
322
|
this.cancelGrace();
|
|
319
323
|
this.connection = newConn;
|
|
324
|
+
this.sendBufferedMessages(newConn);
|
|
320
325
|
}
|
|
321
326
|
beginGrace(cb) {
|
|
322
327
|
log?.info(
|
|
@@ -444,6 +449,9 @@ var defaultClientTransportOptions = {
|
|
|
444
449
|
...defaultTransportOptions,
|
|
445
450
|
...defaultConnectionRetryOptions
|
|
446
451
|
};
|
|
452
|
+
var defaultServerTransportOptions = {
|
|
453
|
+
...defaultTransportOptions
|
|
454
|
+
};
|
|
447
455
|
var Transport = class {
|
|
448
456
|
/**
|
|
449
457
|
* A flag indicating whether the transport has been destroyed.
|
|
@@ -497,37 +505,15 @@ var Transport = class {
|
|
|
497
505
|
* and we know the identity of the connected client.
|
|
498
506
|
* @param conn The connection object.
|
|
499
507
|
*/
|
|
500
|
-
onConnect(conn, connectedTo,
|
|
508
|
+
onConnect(conn, connectedTo, session, isReconnect) {
|
|
501
509
|
this.eventDispatcher.dispatchEvent("connectionStatus", {
|
|
502
510
|
status: "connect",
|
|
503
511
|
conn
|
|
504
512
|
});
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
log?.
|
|
508
|
-
`connection from ${connectedTo} is a different session (id: ${advertisedSessionId}, last connected to: ${oldSession.advertisedSessionId}), killing old session and starting a new one`,
|
|
509
|
-
oldSession.loggingMetadata
|
|
510
|
-
);
|
|
511
|
-
this.deleteSession(oldSession);
|
|
512
|
-
oldSession = void 0;
|
|
513
|
+
if (isReconnect) {
|
|
514
|
+
session.replaceWithNewConnection(conn);
|
|
515
|
+
log?.info(`reconnected to ${connectedTo}`, session.loggingMetadata);
|
|
513
516
|
}
|
|
514
|
-
if (oldSession === void 0) {
|
|
515
|
-
const newSession = this.createSession(connectedTo, conn);
|
|
516
|
-
newSession.advertisedSessionId = advertisedSessionId;
|
|
517
|
-
log?.info(
|
|
518
|
-
`new connection for new session to ${connectedTo}`,
|
|
519
|
-
newSession.loggingMetadata
|
|
520
|
-
);
|
|
521
|
-
return newSession;
|
|
522
|
-
}
|
|
523
|
-
oldSession.replaceWithNewConnection(conn);
|
|
524
|
-
oldSession.sendBufferedMessages();
|
|
525
|
-
oldSession.advertisedSessionId = advertisedSessionId;
|
|
526
|
-
log?.info(
|
|
527
|
-
`new connection for existing session to ${connectedTo}`,
|
|
528
|
-
oldSession.loggingMetadata
|
|
529
|
-
);
|
|
530
|
-
return oldSession;
|
|
531
517
|
}
|
|
532
518
|
createSession(to, conn) {
|
|
533
519
|
const session = new Session(
|
|
@@ -543,8 +529,18 @@ var Transport = class {
|
|
|
543
529
|
});
|
|
544
530
|
return session;
|
|
545
531
|
}
|
|
546
|
-
getOrCreateSession(to, conn) {
|
|
532
|
+
getOrCreateSession(to, conn, sessionId) {
|
|
547
533
|
let session = this.sessions.get(to);
|
|
534
|
+
let isReconnect = session !== void 0;
|
|
535
|
+
if (session?.advertisedSessionId !== void 0 && sessionId !== void 0 && session.advertisedSessionId !== sessionId) {
|
|
536
|
+
log?.warn(
|
|
537
|
+
`session for ${to} already exists but has a different session id (expected: ${session.advertisedSessionId}, got: ${sessionId}), creating a new one`,
|
|
538
|
+
session.loggingMetadata
|
|
539
|
+
);
|
|
540
|
+
this.deleteSession(session);
|
|
541
|
+
isReconnect = false;
|
|
542
|
+
session = void 0;
|
|
543
|
+
}
|
|
548
544
|
if (!session) {
|
|
549
545
|
session = this.createSession(to, conn);
|
|
550
546
|
log?.info(
|
|
@@ -552,7 +548,10 @@ var Transport = class {
|
|
|
552
548
|
session.loggingMetadata
|
|
553
549
|
);
|
|
554
550
|
}
|
|
555
|
-
|
|
551
|
+
if (sessionId !== void 0) {
|
|
552
|
+
session.advertisedSessionId = sessionId;
|
|
553
|
+
}
|
|
554
|
+
return { session, isReconnect };
|
|
556
555
|
}
|
|
557
556
|
deleteSession(session) {
|
|
558
557
|
session.close();
|
|
@@ -687,7 +686,7 @@ var Transport = class {
|
|
|
687
686
|
});
|
|
688
687
|
return void 0;
|
|
689
688
|
}
|
|
690
|
-
return this.getOrCreateSession(to).send(msg);
|
|
689
|
+
return this.getOrCreateSession(to).session.send(msg);
|
|
691
690
|
}
|
|
692
691
|
// control helpers
|
|
693
692
|
sendCloseStream(to, streamId) {
|
|
@@ -728,8 +727,16 @@ var Transport = class {
|
|
|
728
727
|
}
|
|
729
728
|
};
|
|
730
729
|
var ServerTransport = class extends Transport {
|
|
730
|
+
/**
|
|
731
|
+
* The options for this transport.
|
|
732
|
+
*/
|
|
733
|
+
options;
|
|
731
734
|
constructor(clientId, providedOptions) {
|
|
732
735
|
super(clientId, providedOptions);
|
|
736
|
+
this.options = {
|
|
737
|
+
...defaultServerTransportOptions,
|
|
738
|
+
...providedOptions
|
|
739
|
+
};
|
|
733
740
|
log?.info(`initiated server transport`, {
|
|
734
741
|
clientId: this.clientId,
|
|
735
742
|
protocolVersion: PROTOCOL_VERSION
|
|
@@ -757,24 +764,38 @@ var ServerTransport = class extends Transport {
|
|
|
757
764
|
conn.close();
|
|
758
765
|
}
|
|
759
766
|
}, this.options.sessionDisconnectGraceMs);
|
|
767
|
+
const buffer = [];
|
|
768
|
+
let receivedHandshakeMessage = false;
|
|
760
769
|
const handshakeHandler = (data) => {
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
conn.close();
|
|
770
|
+
if (receivedHandshakeMessage) {
|
|
771
|
+
buffer.push(data);
|
|
764
772
|
return;
|
|
765
|
-
} else {
|
|
766
|
-
session = maybeSession;
|
|
767
|
-
clearTimeout(handshakeTimeout);
|
|
768
773
|
}
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
774
|
+
receivedHandshakeMessage = true;
|
|
775
|
+
clearTimeout(handshakeTimeout);
|
|
776
|
+
void this.receiveHandshakeRequestMessage(data, conn).then(
|
|
777
|
+
(maybeSession) => {
|
|
778
|
+
if (!maybeSession) {
|
|
779
|
+
conn.close();
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
session = maybeSession;
|
|
783
|
+
const dataHandler = (data2) => {
|
|
784
|
+
const parsed = this.parseMsg(data2);
|
|
785
|
+
if (!parsed) {
|
|
786
|
+
conn.close();
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
this.handleMsg(parsed);
|
|
790
|
+
};
|
|
791
|
+
conn.removeDataListener(handshakeHandler);
|
|
792
|
+
conn.addDataListener(dataHandler);
|
|
793
|
+
for (const data2 of buffer) {
|
|
794
|
+
dataHandler(data2);
|
|
795
|
+
}
|
|
796
|
+
buffer.length = 0;
|
|
775
797
|
}
|
|
776
|
-
|
|
777
|
-
});
|
|
798
|
+
);
|
|
778
799
|
};
|
|
779
800
|
conn.addDataListener(handshakeHandler);
|
|
780
801
|
conn.addCloseListener(() => {
|
|
@@ -795,7 +816,7 @@ var ServerTransport = class extends Transport {
|
|
|
795
816
|
);
|
|
796
817
|
});
|
|
797
818
|
}
|
|
798
|
-
receiveHandshakeRequestMessage(data, conn) {
|
|
819
|
+
async receiveHandshakeRequestMessage(data, conn) {
|
|
799
820
|
const parsed = this.parseMsg(data);
|
|
800
821
|
if (!parsed) {
|
|
801
822
|
this.protocolError(
|
|
@@ -811,7 +832,8 @@ var ServerTransport = class extends Transport {
|
|
|
811
832
|
reason
|
|
812
833
|
});
|
|
813
834
|
conn.send(this.codec.toBuffer(responseMsg2));
|
|
814
|
-
|
|
835
|
+
const logData = typeof parsed.payload === "object" ? { ...parsed, payload: { ...parsed.payload, metadata: "redacted" } } : { ...parsed };
|
|
836
|
+
log?.warn(`${reason}: ${JSON.stringify(logData)}`, {
|
|
815
837
|
clientId: this.clientId,
|
|
816
838
|
connId: conn.debugId
|
|
817
839
|
});
|
|
@@ -836,7 +858,73 @@ var ServerTransport = class extends Transport {
|
|
|
836
858
|
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
837
859
|
return false;
|
|
838
860
|
}
|
|
839
|
-
const session = this.getOrCreateSession(
|
|
861
|
+
const { session, isReconnect } = this.getOrCreateSession(
|
|
862
|
+
parsed.from,
|
|
863
|
+
conn,
|
|
864
|
+
parsed.payload.sessionId
|
|
865
|
+
);
|
|
866
|
+
let handshakeMetadata;
|
|
867
|
+
if (this.options.handshake) {
|
|
868
|
+
if (!import_value.Value.Check(
|
|
869
|
+
this.options.handshake.requestSchema,
|
|
870
|
+
parsed.payload.metadata
|
|
871
|
+
)) {
|
|
872
|
+
const reason = "received malformed handshake metadata";
|
|
873
|
+
const responseMsg2 = handshakeResponseMessage(
|
|
874
|
+
this.clientId,
|
|
875
|
+
parsed.from,
|
|
876
|
+
{ ok: false, reason }
|
|
877
|
+
);
|
|
878
|
+
conn.send(this.codec.toBuffer(responseMsg2));
|
|
879
|
+
log?.warn(`received malformed handshake metadata from ${parsed.from}`, {
|
|
880
|
+
clientId: this.clientId,
|
|
881
|
+
connId: conn.debugId
|
|
882
|
+
});
|
|
883
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
884
|
+
this.deleteSession(session);
|
|
885
|
+
return false;
|
|
886
|
+
}
|
|
887
|
+
const parsedMetadata = await this.options.handshake.parse(
|
|
888
|
+
parsed.payload.metadata,
|
|
889
|
+
session,
|
|
890
|
+
isReconnect
|
|
891
|
+
);
|
|
892
|
+
if (parsedMetadata === false) {
|
|
893
|
+
const reason = "rejected by server";
|
|
894
|
+
const responseMsg2 = handshakeResponseMessage(
|
|
895
|
+
this.clientId,
|
|
896
|
+
parsed.from,
|
|
897
|
+
{ ok: false, reason }
|
|
898
|
+
);
|
|
899
|
+
conn.send(this.codec.toBuffer(responseMsg2));
|
|
900
|
+
log?.warn(`rejected handshake from ${parsed.from}`, {
|
|
901
|
+
clientId: this.clientId,
|
|
902
|
+
connId: conn.debugId
|
|
903
|
+
});
|
|
904
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
905
|
+
this.deleteSession(session);
|
|
906
|
+
return false;
|
|
907
|
+
}
|
|
908
|
+
if (!import_value.Value.Check(this.options.handshake.parsedSchema, parsedMetadata)) {
|
|
909
|
+
const reason = "failed to parse handshake metadata";
|
|
910
|
+
const responseMsg2 = handshakeResponseMessage(
|
|
911
|
+
this.clientId,
|
|
912
|
+
parsed.from,
|
|
913
|
+
{ ok: false, reason }
|
|
914
|
+
);
|
|
915
|
+
conn.send(this.codec.toBuffer(responseMsg2));
|
|
916
|
+
log?.error(`failed to parse handshake metadata`, {
|
|
917
|
+
clientId: this.clientId,
|
|
918
|
+
connId: conn.debugId
|
|
919
|
+
});
|
|
920
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
921
|
+
this.deleteSession(session);
|
|
922
|
+
return false;
|
|
923
|
+
}
|
|
924
|
+
handshakeMetadata = parsedMetadata;
|
|
925
|
+
}
|
|
926
|
+
handshakeMetadata ??= {};
|
|
927
|
+
session.metadata = handshakeMetadata;
|
|
840
928
|
log?.debug(
|
|
841
929
|
`handshake from ${parsed.from} ok, responding with handshake success`,
|
|
842
930
|
{ clientId: this.clientId, connId: conn.debugId }
|
|
@@ -846,7 +934,8 @@ var ServerTransport = class extends Transport {
|
|
|
846
934
|
sessionId: session.id
|
|
847
935
|
});
|
|
848
936
|
conn.send(this.codec.toBuffer(responseMsg));
|
|
849
|
-
|
|
937
|
+
this.onConnect(conn, parsed.from, session, isReconnect);
|
|
938
|
+
return session;
|
|
850
939
|
}
|
|
851
940
|
};
|
|
852
941
|
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { T as TransportClientId } from '../../../index-
|
|
2
|
-
import {
|
|
1
|
+
import { T as TransportClientId } from '../../../index-e2513701.js';
|
|
2
|
+
import { ServerTransport, ServerTransportOptions as ProvidedServerTransportOptions } from '../../index.cjs';
|
|
3
3
|
import { WebSocketServer } from 'ws';
|
|
4
4
|
import { WebSocket } from 'isomorphic-ws';
|
|
5
|
-
import { W as WebSocketConnection } from '../../../connection-
|
|
5
|
+
import { W as WebSocketConnection } from '../../../connection-aa0ea000.js';
|
|
6
6
|
import '@sinclair/typebox';
|
|
7
7
|
import '../../../types-3e5768ec.js';
|
|
8
8
|
|
|
9
9
|
declare class WebSocketServerTransport extends ServerTransport<WebSocketConnection> {
|
|
10
10
|
wss: WebSocketServer;
|
|
11
|
-
constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?:
|
|
11
|
+
constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?: ProvidedServerTransportOptions);
|
|
12
12
|
connectionHandler: (ws: WebSocket) => void;
|
|
13
13
|
close(): void;
|
|
14
14
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { T as TransportClientId } from '../../../index-
|
|
2
|
-
import {
|
|
1
|
+
import { T as TransportClientId } from '../../../index-e2513701.js';
|
|
2
|
+
import { ServerTransport, ServerTransportOptions as ProvidedServerTransportOptions } from '../../index.js';
|
|
3
3
|
import { WebSocketServer } from 'ws';
|
|
4
4
|
import { WebSocket } from 'isomorphic-ws';
|
|
5
|
-
import { W as WebSocketConnection } from '../../../connection-
|
|
5
|
+
import { W as WebSocketConnection } from '../../../connection-aa0ea000.js';
|
|
6
6
|
import '@sinclair/typebox';
|
|
7
7
|
import '../../../types-3e5768ec.js';
|
|
8
8
|
|
|
9
9
|
declare class WebSocketServerTransport extends ServerTransport<WebSocketConnection> {
|
|
10
10
|
wss: WebSocketServer;
|
|
11
|
-
constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?:
|
|
11
|
+
constructor(wss: WebSocketServer, clientId: TransportClientId, providedOptions?: ProvidedServerTransportOptions);
|
|
12
12
|
connectionHandler: (ws: WebSocket) => void;
|
|
13
13
|
close(): void;
|
|
14
14
|
}
|