@dxos/edge-client 0.8.2-main.12df754 → 0.8.2-main.36232bc
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/lib/browser/chunk-XS3TKGM4.mjs +545 -0
- package/dist/lib/browser/chunk-XS3TKGM4.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +60 -282
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +32 -20
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/chunk-ZURVCY7K.cjs +577 -0
- package/dist/lib/node/chunk-ZURVCY7K.cjs.map +7 -0
- package/dist/lib/node/index.cjs +50 -281
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +31 -21
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node-esm/chunk-HNRMNQPG.mjs +547 -0
- package/dist/lib/node-esm/chunk-HNRMNQPG.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +60 -282
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +32 -20
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/edge-ws-connection.d.ts +5 -0
- package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
- package/dist/types/src/edge-ws-muxer.d.ts +23 -0
- package/dist/types/src/edge-ws-muxer.d.ts.map +1 -0
- package/dist/types/src/testing/test-utils.d.ts +6 -2
- package/dist/types/src/testing/test-utils.d.ts.map +1 -1
- package/package.json +14 -14
- package/src/edge-client.ts +2 -2
- package/src/edge-ws-connection.ts +38 -15
- package/src/edge-ws-muxer.ts +187 -0
- package/src/testing/test-utils.ts +33 -26
- package/dist/lib/browser/chunk-ZWJXA37R.mjs +0 -113
- package/dist/lib/browser/chunk-ZWJXA37R.mjs.map +0 -7
- package/dist/lib/node/chunk-ANV2HBEH.cjs +0 -136
- package/dist/lib/node/chunk-ANV2HBEH.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-HNVT57AU.mjs +0 -115
- package/dist/lib/node-esm/chunk-HNVT57AU.mjs.map +0 -7
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
2
|
import { Trigger } from '@dxos/async';
|
|
3
3
|
import { type Message } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
|
|
4
|
+
import { WebSocketMuxer } from '../edge-ws-muxer';
|
|
4
5
|
export declare const DEFAULT_PORT = 8080;
|
|
5
6
|
type TestEdgeWsServerParams = {
|
|
6
7
|
admitConnection?: Trigger;
|
|
@@ -13,9 +14,12 @@ export declare const createTestEdgeWsServer: (port?: number, params?: TestEdgeWs
|
|
|
13
14
|
messageSourceLog: any[];
|
|
14
15
|
endpoint: string;
|
|
15
16
|
cleanup: () => void;
|
|
16
|
-
currentConnection: () =>
|
|
17
|
+
currentConnection: () => {
|
|
18
|
+
ws: WebSocket;
|
|
19
|
+
muxer: WebSocketMuxer;
|
|
20
|
+
} | undefined;
|
|
17
21
|
sendResponseMessage: (request: Message, responsePayload: Uint8Array) => void;
|
|
18
|
-
sendMessage: (msg: Message) => void
|
|
22
|
+
sendMessage: (msg: Message) => Promise<void>;
|
|
19
23
|
closeConnection: () => Promise<void>;
|
|
20
24
|
}>;
|
|
21
25
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-utils.d.ts","sourceRoot":"","sources":["../../../../src/testing/test-utils.ts"],"names":[],"mappings":"AAIA,OAAO,SAAS,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGtC,OAAO,EAAoC,KAAK,OAAO,EAAE,MAAM,4CAA4C,CAAC;
|
|
1
|
+
{"version":3,"file":"test-utils.d.ts","sourceRoot":"","sources":["../../../../src/testing/test-utils.ts"],"names":[],"mappings":"AAIA,OAAO,SAAS,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGtC,OAAO,EAAoC,KAAK,OAAO,EAAE,MAAM,4CAA4C,CAAC;AAI5G,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC,KAAK,sBAAsB,GAAG;IAC5B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK,GAAG,CAAC;IAC9C,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;CACpE,CAAC;AAEF,eAAO,MAAM,sBAAsB,2BAAwC,sBAAsB;;;;;;;YAOzE,SAAS;eAAS,cAAc;;mCAwErC,OAAO,mBAAmB,UAAU;uBA1BhC,OAAO;;EAS7B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/edge-client",
|
|
3
|
-
"version": "0.8.2-main.
|
|
3
|
+
"version": "0.8.2-main.36232bc",
|
|
4
4
|
"description": "EDGE Client",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -36,21 +36,21 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"isomorphic-ws": "^5.0.0",
|
|
38
38
|
"ws": "^8.14.2",
|
|
39
|
-
"@dxos/async": "0.8.2-main.
|
|
40
|
-
"@dxos/
|
|
41
|
-
"@dxos/credentials": "0.8.2-main.
|
|
42
|
-
"@dxos/
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/
|
|
47
|
-
"@dxos/log": "0.8.2-main.
|
|
48
|
-
"@dxos/node-std": "0.8.2-main.
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
39
|
+
"@dxos/async": "0.8.2-main.36232bc",
|
|
40
|
+
"@dxos/context": "0.8.2-main.36232bc",
|
|
41
|
+
"@dxos/credentials": "0.8.2-main.36232bc",
|
|
42
|
+
"@dxos/debug": "0.8.2-main.36232bc",
|
|
43
|
+
"@dxos/keyring": "0.8.2-main.36232bc",
|
|
44
|
+
"@dxos/keys": "0.8.2-main.36232bc",
|
|
45
|
+
"@dxos/invariant": "0.8.2-main.36232bc",
|
|
46
|
+
"@dxos/crypto": "0.8.2-main.36232bc",
|
|
47
|
+
"@dxos/log": "0.8.2-main.36232bc",
|
|
48
|
+
"@dxos/node-std": "0.8.2-main.36232bc",
|
|
49
|
+
"@dxos/util": "0.8.2-main.36232bc",
|
|
50
|
+
"@dxos/protocols": "0.8.2-main.36232bc"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@dxos/test-utils": "0.8.2-main.
|
|
53
|
+
"@dxos/test-utils": "0.8.2-main.36232bc"
|
|
54
54
|
},
|
|
55
55
|
"publishConfig": {
|
|
56
56
|
"access": "public"
|
package/src/edge-client.ts
CHANGED
|
@@ -99,7 +99,7 @@ export class EdgeClient extends Resource implements EdgeConnection {
|
|
|
99
99
|
log('Edge identity changed', { identity, oldIdentity: this._identity });
|
|
100
100
|
this._identity = identity;
|
|
101
101
|
this._closeCurrentConnection(new EdgeIdentityChangedError());
|
|
102
|
-
this._persistentLifecycle.scheduleRestart();
|
|
102
|
+
void this._persistentLifecycle.scheduleRestart();
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
|
|
@@ -176,7 +176,7 @@ export class EdgeClient extends Resource implements EdgeConnection {
|
|
|
176
176
|
onRestartRequired: () => {
|
|
177
177
|
if (this._isActive(connection)) {
|
|
178
178
|
this._closeCurrentConnection();
|
|
179
|
-
this._persistentLifecycle.scheduleRestart();
|
|
179
|
+
void this._persistentLifecycle.scheduleRestart();
|
|
180
180
|
} else {
|
|
181
181
|
log.verbose('restart requested by inactive connection');
|
|
182
182
|
}
|
|
@@ -13,15 +13,17 @@ import { MessageSchema, type Message } from '@dxos/protocols/buf/dxos/edge/messe
|
|
|
13
13
|
|
|
14
14
|
import { protocol } from './defs';
|
|
15
15
|
import { type EdgeIdentity } from './edge-identity';
|
|
16
|
+
import { CLOUDFLARE_MESSAGE_LENGTH_LIMIT, WebSocketMuxer } from './edge-ws-muxer';
|
|
16
17
|
import { toUint8Array } from './protocol';
|
|
17
18
|
|
|
18
19
|
const SIGNAL_KEEPALIVE_INTERVAL = 4_000;
|
|
19
20
|
const SIGNAL_KEEPALIVE_TIMEOUT = 12_000;
|
|
20
21
|
|
|
22
|
+
const EDGE_WEBSOCKET_PROTOCOL_V0 = 'edge-ws-v0';
|
|
21
23
|
/**
|
|
22
|
-
*
|
|
24
|
+
* Supports message segmentation and muxing.
|
|
23
25
|
*/
|
|
24
|
-
const
|
|
26
|
+
export const EDGE_WEBSOCKET_PROTOCOL_V1 = 'edge-ws-v1';
|
|
25
27
|
|
|
26
28
|
export type EdgeWsConnectionCallbacks = {
|
|
27
29
|
onConnected: () => void;
|
|
@@ -32,6 +34,7 @@ export type EdgeWsConnectionCallbacks = {
|
|
|
32
34
|
export class EdgeWsConnection extends Resource {
|
|
33
35
|
private _inactivityTimeoutCtx: Context | undefined;
|
|
34
36
|
private _ws: WebSocket | undefined;
|
|
37
|
+
private _wsMuxer: WebSocketMuxer | undefined;
|
|
35
38
|
|
|
36
39
|
constructor(
|
|
37
40
|
private readonly _identity: EdgeIdentity,
|
|
@@ -52,24 +55,34 @@ export class EdgeWsConnection extends Resource {
|
|
|
52
55
|
|
|
53
56
|
public send(message: Message) {
|
|
54
57
|
invariant(this._ws);
|
|
58
|
+
invariant(this._wsMuxer);
|
|
55
59
|
log('sending...', { peerKey: this._identity.peerKey, payload: protocol.getPayloadType(message) });
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
if (this._ws?.protocol.includes(EDGE_WEBSOCKET_PROTOCOL_V0)) {
|
|
61
|
+
const binary = buf.toBinary(MessageSchema, message);
|
|
62
|
+
if (binary.length > CLOUDFLARE_MESSAGE_LENGTH_LIMIT) {
|
|
63
|
+
log.error('Message dropped because it was too large (>1MB).', {
|
|
64
|
+
byteLength: binary.byteLength,
|
|
65
|
+
serviceId: message.serviceId,
|
|
66
|
+
payload: protocol.getPayloadType(message),
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this._ws.send(binary);
|
|
71
|
+
} else {
|
|
72
|
+
this._wsMuxer.send(message).catch((e) => log.catch(e));
|
|
64
73
|
}
|
|
65
|
-
this._ws.send(encoded);
|
|
66
74
|
}
|
|
67
75
|
|
|
68
76
|
protected override async _open() {
|
|
77
|
+
const baseProtocols = [EDGE_WEBSOCKET_PROTOCOL_V0, EDGE_WEBSOCKET_PROTOCOL_V1];
|
|
69
78
|
this._ws = new WebSocket(
|
|
70
79
|
this._connectionInfo.url.toString(),
|
|
71
|
-
this._connectionInfo.protocolHeader
|
|
80
|
+
this._connectionInfo.protocolHeader
|
|
81
|
+
? [...baseProtocols, this._connectionInfo.protocolHeader]
|
|
82
|
+
: [...baseProtocols],
|
|
72
83
|
);
|
|
84
|
+
const muxer = new WebSocketMuxer(this._ws);
|
|
85
|
+
this._wsMuxer = muxer;
|
|
73
86
|
|
|
74
87
|
this._ws.onopen = () => {
|
|
75
88
|
if (this.isOpen) {
|
|
@@ -84,6 +97,7 @@ export class EdgeWsConnection extends Resource {
|
|
|
84
97
|
if (this.isOpen) {
|
|
85
98
|
log.warn('disconnected while being open', { code: event.code, reason: event.reason });
|
|
86
99
|
this._callbacks.onRestartRequired();
|
|
100
|
+
muxer.destroy();
|
|
87
101
|
}
|
|
88
102
|
};
|
|
89
103
|
this._ws.onerror = (event) => {
|
|
@@ -106,9 +120,16 @@ export class EdgeWsConnection extends Resource {
|
|
|
106
120
|
this._rescheduleHeartbeatTimeout();
|
|
107
121
|
return;
|
|
108
122
|
}
|
|
109
|
-
const
|
|
110
|
-
if (this.isOpen) {
|
|
111
|
-
|
|
123
|
+
const bytes = await toUint8Array(event.data);
|
|
124
|
+
if (!this.isOpen) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const message = this._ws?.protocol?.includes(EDGE_WEBSOCKET_PROTOCOL_V0)
|
|
129
|
+
? buf.fromBinary(MessageSchema, bytes)
|
|
130
|
+
: muxer.receiveData(bytes);
|
|
131
|
+
|
|
132
|
+
if (message) {
|
|
112
133
|
log('received', { from: message.source, payload: protocol.getPayloadType(message) });
|
|
113
134
|
this._callbacks.onMessage(message);
|
|
114
135
|
}
|
|
@@ -121,6 +142,8 @@ export class EdgeWsConnection extends Resource {
|
|
|
121
142
|
try {
|
|
122
143
|
this._ws?.close();
|
|
123
144
|
this._ws = undefined;
|
|
145
|
+
this._wsMuxer?.destroy();
|
|
146
|
+
this._wsMuxer = undefined;
|
|
124
147
|
} catch (err) {
|
|
125
148
|
if (err instanceof Error && err.message.includes('WebSocket is closed before the connection is established.')) {
|
|
126
149
|
return;
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
import WebSocket from 'isomorphic-ws';
|
|
5
|
+
|
|
6
|
+
import { Trigger } from '@dxos/async';
|
|
7
|
+
import { log } from '@dxos/log';
|
|
8
|
+
import { buf } from '@dxos/protocols/buf';
|
|
9
|
+
import { MessageSchema, type Message } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
|
|
10
|
+
|
|
11
|
+
import { protocol } from './defs';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 0000 0001 - message contains a part of segmented message chunk sequence.
|
|
15
|
+
* The next byte defines a channel id and the rest of the message contains a part of Message proto binary.
|
|
16
|
+
* Messages from different channels might interleave.
|
|
17
|
+
* When the flag is NOT set the rest of the message should be interpreted as the valid Message proto binary.
|
|
18
|
+
*/
|
|
19
|
+
const FLAG_SEGMENT_SEQ = 1;
|
|
20
|
+
/**
|
|
21
|
+
* 0000 0010 - message terminates a segmented message chunk sequence.
|
|
22
|
+
* All the chunks accumulated for the channel specified by the second byte can be concatenated
|
|
23
|
+
* and interpreted as a valid Message proto binary.
|
|
24
|
+
*/
|
|
25
|
+
const FLAG_SEGMENT_SEQ_TERMINATED = 1 << 1;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 1MB websocket message limit: https://developers.cloudflare.com/durable-objects/platform/limits/
|
|
29
|
+
*/
|
|
30
|
+
export const CLOUDFLARE_MESSAGE_LENGTH_LIMIT = 1024 * 1024;
|
|
31
|
+
|
|
32
|
+
const MAX_CHUNK_LENGTH = 16384;
|
|
33
|
+
const MAX_BUFFERED_AMOUNT = CLOUDFLARE_MESSAGE_LENGTH_LIMIT;
|
|
34
|
+
const BUFFER_FULL_BACKOFF_TIMEOUT = 100;
|
|
35
|
+
|
|
36
|
+
export class WebSocketMuxer {
|
|
37
|
+
private readonly _incomingMessageAccumulator = new Map<number, Buffer[]>();
|
|
38
|
+
private readonly _outgoingMessageChunks = new Map<number, MessageChunk[]>();
|
|
39
|
+
private readonly _serviceToChannel = new Map<string, number>();
|
|
40
|
+
|
|
41
|
+
private _sendTimeout: any | undefined;
|
|
42
|
+
|
|
43
|
+
constructor(private readonly _ws: WebSocket) {}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Resolves when all the message chunks get enqueued for sending.
|
|
47
|
+
*/
|
|
48
|
+
public async send(message: Message): Promise<void> {
|
|
49
|
+
const binary = buf.toBinary(MessageSchema, message);
|
|
50
|
+
const channelId = this._resolveChannel(message);
|
|
51
|
+
if (channelId == null && binary.length > CLOUDFLARE_MESSAGE_LENGTH_LIMIT) {
|
|
52
|
+
log.error('Large message dropped because channel resolution failed.', {
|
|
53
|
+
byteLength: binary.byteLength,
|
|
54
|
+
serviceId: message.serviceId,
|
|
55
|
+
payload: protocol.getPayloadType(message),
|
|
56
|
+
});
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (channelId == null || binary.length < MAX_CHUNK_LENGTH) {
|
|
61
|
+
const flags = Buffer.from([0]);
|
|
62
|
+
this._ws.send(Buffer.concat([flags, binary]));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const terminatorSentTrigger = new Trigger();
|
|
67
|
+
const messageChunks: MessageChunk[] = [];
|
|
68
|
+
for (let i = 0; i < binary.length; i += MAX_CHUNK_LENGTH) {
|
|
69
|
+
const chunk = binary.slice(i, i + MAX_CHUNK_LENGTH);
|
|
70
|
+
const isLastChunk = i + MAX_CHUNK_LENGTH < binary.length;
|
|
71
|
+
if (isLastChunk) {
|
|
72
|
+
const flags = Buffer.from([FLAG_SEGMENT_SEQ | FLAG_SEGMENT_SEQ_TERMINATED, channelId]);
|
|
73
|
+
messageChunks.push({ payload: Buffer.concat([flags, chunk]), trigger: terminatorSentTrigger });
|
|
74
|
+
} else {
|
|
75
|
+
const flags = Buffer.from([FLAG_SEGMENT_SEQ]);
|
|
76
|
+
messageChunks.push({ payload: Buffer.concat([flags, chunk]) });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const queuedMessages = this._outgoingMessageChunks.get(channelId);
|
|
81
|
+
if (queuedMessages) {
|
|
82
|
+
queuedMessages.push(...messageChunks);
|
|
83
|
+
} else {
|
|
84
|
+
this._outgoingMessageChunks.set(channelId, messageChunks);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this._sendChunkedMessages();
|
|
88
|
+
|
|
89
|
+
return terminatorSentTrigger.wait();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public receiveData(data: Uint8Array): Message | undefined {
|
|
93
|
+
if ((data[0] & FLAG_SEGMENT_SEQ) === 0) {
|
|
94
|
+
return buf.fromBinary(MessageSchema, data.slice(1));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const [flags, channelId, ...payload] = data;
|
|
98
|
+
let chunkAccumulator = this._incomingMessageAccumulator.get(channelId);
|
|
99
|
+
if (chunkAccumulator) {
|
|
100
|
+
chunkAccumulator.push(Buffer.from(payload));
|
|
101
|
+
} else {
|
|
102
|
+
chunkAccumulator = [Buffer.from(payload)];
|
|
103
|
+
this._incomingMessageAccumulator.set(channelId, chunkAccumulator);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const message = buf.fromBinary(MessageSchema, Buffer.concat(chunkAccumulator));
|
|
111
|
+
this._incomingMessageAccumulator.delete(channelId);
|
|
112
|
+
return message;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public destroy() {
|
|
116
|
+
if (this._sendTimeout) {
|
|
117
|
+
clearTimeout(this._sendTimeout);
|
|
118
|
+
this._sendTimeout = undefined;
|
|
119
|
+
}
|
|
120
|
+
for (const channelChunks of this._outgoingMessageChunks.values()) {
|
|
121
|
+
channelChunks.forEach((chunk) => chunk.trigger?.wake());
|
|
122
|
+
}
|
|
123
|
+
this._outgoingMessageChunks.clear();
|
|
124
|
+
this._incomingMessageAccumulator.clear();
|
|
125
|
+
this._serviceToChannel.clear();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private _sendChunkedMessages() {
|
|
129
|
+
if (this._sendTimeout) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const send = () => {
|
|
134
|
+
if (this._ws.readyState === WebSocket.CLOSING || this._ws.readyState === WebSocket.CLOSED) {
|
|
135
|
+
log.warn('send called for closed websocket');
|
|
136
|
+
this._sendTimeout = undefined;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let timeout = 0;
|
|
141
|
+
const emptyChannels: number[] = [];
|
|
142
|
+
for (const [channelId, messages] of this._outgoingMessageChunks.entries()) {
|
|
143
|
+
if (this._ws.bufferedAmount + MAX_CHUNK_LENGTH > MAX_BUFFERED_AMOUNT) {
|
|
144
|
+
timeout = BUFFER_FULL_BACKOFF_TIMEOUT;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const nextMessage = messages.shift();
|
|
149
|
+
if (nextMessage) {
|
|
150
|
+
this._ws.send(nextMessage.payload);
|
|
151
|
+
nextMessage.trigger?.wake();
|
|
152
|
+
} else {
|
|
153
|
+
emptyChannels.push(channelId);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
emptyChannels.forEach((channelId) => this._outgoingMessageChunks.delete(channelId));
|
|
158
|
+
|
|
159
|
+
if (this._outgoingMessageChunks.size > 0) {
|
|
160
|
+
this._sendTimeout = setTimeout(send, timeout);
|
|
161
|
+
} else {
|
|
162
|
+
this._sendTimeout = undefined;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
this._sendTimeout = setTimeout(send);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private _resolveChannel(message: Message): number | undefined {
|
|
169
|
+
if (!message.serviceId) {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
let id = this._serviceToChannel.get(message.serviceId);
|
|
173
|
+
if (!id) {
|
|
174
|
+
id = this._serviceToChannel.size + 1;
|
|
175
|
+
this._serviceToChannel.set(message.serviceId, id);
|
|
176
|
+
}
|
|
177
|
+
return id;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
type MessageChunk = {
|
|
182
|
+
payload: Buffer;
|
|
183
|
+
/**
|
|
184
|
+
* Wakes when the payload is enqueued by WebSocket.
|
|
185
|
+
*/
|
|
186
|
+
trigger?: Trigger;
|
|
187
|
+
};
|
|
@@ -10,6 +10,8 @@ import { buf } from '@dxos/protocols/buf';
|
|
|
10
10
|
import { MessageSchema, TextMessageSchema, type Message } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
|
|
11
11
|
|
|
12
12
|
import { protocol } from '../defs';
|
|
13
|
+
import { EDGE_WEBSOCKET_PROTOCOL_V1 } from '../edge-ws-connection';
|
|
14
|
+
import { WebSocketMuxer } from '../edge-ws-muxer';
|
|
13
15
|
import { toUint8Array } from '../protocol';
|
|
14
16
|
|
|
15
17
|
export const DEFAULT_PORT = 8080;
|
|
@@ -21,24 +23,33 @@ type TestEdgeWsServerParams = {
|
|
|
21
23
|
};
|
|
22
24
|
|
|
23
25
|
export const createTestEdgeWsServer = async (port = DEFAULT_PORT, params?: TestEdgeWsServerParams) => {
|
|
24
|
-
const
|
|
26
|
+
const wsServer = new WebSocket.Server({
|
|
27
|
+
port,
|
|
28
|
+
verifyClient: createConnectionDelayHandler(params),
|
|
29
|
+
handleProtocols: () => [EDGE_WEBSOCKET_PROTOCOL_V1],
|
|
30
|
+
});
|
|
25
31
|
|
|
26
|
-
let connection: WebSocket | undefined;
|
|
32
|
+
let connection: { ws: WebSocket; muxer: WebSocketMuxer } | undefined;
|
|
27
33
|
|
|
28
34
|
const messageSink: any[] = [];
|
|
29
35
|
const messageSourceLog: any[] = [];
|
|
30
36
|
const closeTrigger = new Trigger();
|
|
31
|
-
const sendResponseMessage = createResponseSender(() => connection
|
|
37
|
+
const sendResponseMessage = createResponseSender(() => connection!.muxer);
|
|
32
38
|
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
wsServer.on('connection', (ws) => {
|
|
40
|
+
const muxer = new WebSocketMuxer(ws);
|
|
41
|
+
connection = { ws, muxer };
|
|
35
42
|
ws.on('error', (err) => log.catch(err));
|
|
36
43
|
ws.on('message', async (data) => {
|
|
37
44
|
if (String(data) === '__ping__') {
|
|
38
45
|
ws.send('__pong__');
|
|
39
46
|
return;
|
|
40
47
|
}
|
|
41
|
-
const
|
|
48
|
+
const message = muxer.receiveData(await toUint8Array(data));
|
|
49
|
+
if (!message) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const { request, requestPayload } = await decodePayload(message, params);
|
|
42
53
|
messageSourceLog.push(request.source);
|
|
43
54
|
if (params?.messageHandler) {
|
|
44
55
|
const responsePayload = await params.messageHandler(requestPayload);
|
|
@@ -57,19 +68,19 @@ export const createTestEdgeWsServer = async (port = DEFAULT_PORT, params?: TestE
|
|
|
57
68
|
});
|
|
58
69
|
|
|
59
70
|
return {
|
|
60
|
-
server,
|
|
71
|
+
server: wsServer,
|
|
61
72
|
messageSink,
|
|
62
73
|
messageSourceLog,
|
|
63
|
-
endpoint: `ws://
|
|
64
|
-
cleanup: () =>
|
|
74
|
+
endpoint: `ws://127.0.0.1:${port}`,
|
|
75
|
+
cleanup: () => wsServer.close(),
|
|
65
76
|
currentConnection: () => connection,
|
|
66
77
|
sendResponseMessage,
|
|
67
78
|
sendMessage: (msg: Message) => {
|
|
68
|
-
connection!.send(
|
|
79
|
+
return connection!.muxer.send(msg);
|
|
69
80
|
},
|
|
70
81
|
closeConnection: () => {
|
|
71
82
|
closeTrigger.reset();
|
|
72
|
-
connection!.close(1011);
|
|
83
|
+
connection!.ws.close(1011);
|
|
73
84
|
return closeTrigger.wait();
|
|
74
85
|
},
|
|
75
86
|
};
|
|
@@ -89,27 +100,23 @@ const createConnectionDelayHandler = (params: TestEdgeWsServerParams | undefined
|
|
|
89
100
|
};
|
|
90
101
|
};
|
|
91
102
|
|
|
92
|
-
const createResponseSender = (connection: () =>
|
|
103
|
+
const createResponseSender = (connection: () => WebSocketMuxer) => {
|
|
93
104
|
return (request: Message, responsePayload: Uint8Array) => {
|
|
94
105
|
const recipient = request.source!;
|
|
95
|
-
connection().send(
|
|
96
|
-
buf.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
payload: { value: responsePayload },
|
|
105
|
-
}),
|
|
106
|
-
),
|
|
106
|
+
void connection().send(
|
|
107
|
+
buf.create(MessageSchema, {
|
|
108
|
+
source: {
|
|
109
|
+
identityKey: recipient.identityKey,
|
|
110
|
+
peerKey: recipient.peerKey,
|
|
111
|
+
},
|
|
112
|
+
serviceId: request.serviceId!,
|
|
113
|
+
payload: { value: responsePayload },
|
|
114
|
+
}),
|
|
107
115
|
);
|
|
108
116
|
};
|
|
109
117
|
};
|
|
110
118
|
|
|
111
|
-
const
|
|
112
|
-
const request = buf.fromBinary(MessageSchema, await toUint8Array(data));
|
|
119
|
+
const decodePayload = async (request: Message, params: TestEdgeWsServerParams | undefined) => {
|
|
113
120
|
const requestPayload = params?.payloadDecoder
|
|
114
121
|
? params.payloadDecoder(request.payload!.value!)
|
|
115
122
|
: protocol.getPayload(request, TextMessageSchema);
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
// packages/core/mesh/edge-client/src/protocol.ts
|
|
2
|
-
import { invariant } from "@dxos/invariant";
|
|
3
|
-
import { buf, bufWkt } from "@dxos/protocols/buf";
|
|
4
|
-
import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
5
|
-
import { bufferToArray } from "@dxos/util";
|
|
6
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/protocol.ts";
|
|
7
|
-
var getTypename = (typeName) => `type.googleapis.com/${typeName}`;
|
|
8
|
-
var Protocol = class {
|
|
9
|
-
constructor(types) {
|
|
10
|
-
this._typeRegistry = buf.createRegistry(...types);
|
|
11
|
-
}
|
|
12
|
-
get typeRegistry() {
|
|
13
|
-
return this._typeRegistry;
|
|
14
|
-
}
|
|
15
|
-
toJson(message) {
|
|
16
|
-
try {
|
|
17
|
-
return buf.toJson(MessageSchema, message, {
|
|
18
|
-
registry: this.typeRegistry
|
|
19
|
-
});
|
|
20
|
-
} catch (err) {
|
|
21
|
-
return {
|
|
22
|
-
type: this.getPayloadType(message)
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Return the payload with the given type.
|
|
28
|
-
*/
|
|
29
|
-
getPayload(message, type) {
|
|
30
|
-
invariant(message.payload, void 0, {
|
|
31
|
-
F: __dxlog_file,
|
|
32
|
-
L: 40,
|
|
33
|
-
S: this,
|
|
34
|
-
A: [
|
|
35
|
-
"message.payload",
|
|
36
|
-
""
|
|
37
|
-
]
|
|
38
|
-
});
|
|
39
|
-
const payloadTypename = this.getPayloadType(message);
|
|
40
|
-
if (type && type.typeName !== payloadTypename) {
|
|
41
|
-
throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);
|
|
42
|
-
}
|
|
43
|
-
invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, {
|
|
44
|
-
F: __dxlog_file,
|
|
45
|
-
L: 46,
|
|
46
|
-
S: this,
|
|
47
|
-
A: [
|
|
48
|
-
"bufWkt.anyIs(message.payload, type)",
|
|
49
|
-
"`Unexpected payload type: ${payloadTypename}}`"
|
|
50
|
-
]
|
|
51
|
-
});
|
|
52
|
-
const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry);
|
|
53
|
-
invariant(payload, `Empty payload: ${payloadTypename}}`, {
|
|
54
|
-
F: __dxlog_file,
|
|
55
|
-
L: 48,
|
|
56
|
-
S: this,
|
|
57
|
-
A: [
|
|
58
|
-
"payload",
|
|
59
|
-
"`Empty payload: ${payloadTypename}}`"
|
|
60
|
-
]
|
|
61
|
-
});
|
|
62
|
-
return payload;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Get the payload type.
|
|
66
|
-
*/
|
|
67
|
-
getPayloadType(message) {
|
|
68
|
-
if (!message.payload) {
|
|
69
|
-
return void 0;
|
|
70
|
-
}
|
|
71
|
-
const [, type] = message.payload.typeUrl.split("/");
|
|
72
|
-
return type;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Create a packed message.
|
|
76
|
-
*/
|
|
77
|
-
createMessage(type, { source, target, payload, serviceId }) {
|
|
78
|
-
return buf.create(MessageSchema, {
|
|
79
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
80
|
-
source,
|
|
81
|
-
target,
|
|
82
|
-
serviceId,
|
|
83
|
-
payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : void 0
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
var toUint8Array = async (data) => {
|
|
88
|
-
if (data instanceof Buffer) {
|
|
89
|
-
return bufferToArray(data);
|
|
90
|
-
}
|
|
91
|
-
if (data instanceof Blob) {
|
|
92
|
-
return new Uint8Array(await data.arrayBuffer());
|
|
93
|
-
}
|
|
94
|
-
throw new Error(`Unexpected datatype: ${data}`);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// packages/core/mesh/edge-client/src/defs.ts
|
|
98
|
-
import { bufWkt as bufWkt2 } from "@dxos/protocols/buf";
|
|
99
|
-
import { SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
100
|
-
var protocol = new Protocol([
|
|
101
|
-
SwarmRequestSchema,
|
|
102
|
-
SwarmResponseSchema,
|
|
103
|
-
TextMessageSchema,
|
|
104
|
-
bufWkt2.AnySchema
|
|
105
|
-
]);
|
|
106
|
-
|
|
107
|
-
export {
|
|
108
|
-
getTypename,
|
|
109
|
-
Protocol,
|
|
110
|
-
toUint8Array,
|
|
111
|
-
protocol
|
|
112
|
-
};
|
|
113
|
-
//# sourceMappingURL=chunk-ZWJXA37R.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/protocol.ts", "../../../src/defs.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\nimport { buf, bufWkt } from '@dxos/protocols/buf';\nimport { type Message, MessageSchema, type PeerSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\nimport { bufferToArray } from '@dxos/util';\n\nexport type PeerData = buf.MessageInitShape<typeof PeerSchema>;\n\nexport const getTypename = (typeName: string) => `type.googleapis.com/${typeName}`;\n\n/**\n * NOTE: The type registry should be extended with all message types.\n */\nexport class Protocol {\n private readonly _typeRegistry: buf.Registry;\n\n constructor(types: buf.DescMessage[]) {\n this._typeRegistry = buf.createRegistry(...types);\n }\n\n get typeRegistry(): buf.Registry {\n return this._typeRegistry;\n }\n\n toJson(message: Message): any {\n try {\n return buf.toJson(MessageSchema, message, { registry: this.typeRegistry });\n } catch (err) {\n return { type: this.getPayloadType(message) };\n }\n }\n\n /**\n * Return the payload with the given type.\n */\n getPayload<Desc extends buf.DescMessage>(message: Message, type: Desc): buf.MessageShape<Desc> {\n invariant(message.payload);\n const payloadTypename = this.getPayloadType(message);\n if (type && type.typeName !== payloadTypename) {\n throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);\n }\n\n invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`);\n const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry) as buf.MessageShape<Desc>;\n invariant(payload, `Empty payload: ${payloadTypename}}`);\n return payload;\n }\n\n /**\n * Get the payload type.\n */\n getPayloadType(message: Message): string | undefined {\n if (!message.payload) {\n return undefined;\n }\n\n const [, type] = message.payload.typeUrl.split('/');\n return type;\n }\n\n /**\n * Create a packed message.\n */\n createMessage<Desc extends buf.DescMessage>(\n type: Desc,\n {\n source,\n target,\n payload,\n serviceId,\n }: {\n source?: PeerData;\n target?: PeerData[];\n payload?: buf.MessageInitShape<Desc>;\n serviceId?: string;\n },\n ) {\n return buf.create(MessageSchema, {\n timestamp: new Date().toISOString(),\n source,\n target,\n serviceId,\n payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : undefined,\n });\n }\n}\n\n/**\n * Convert websocket data to Uint8Array.\n */\nexport const toUint8Array = async (data: any): Promise<Uint8Array> => {\n // Node.\n if (data instanceof Buffer) {\n return bufferToArray(data);\n }\n\n // Browser.\n if (data instanceof Blob) {\n return new Uint8Array(await (data as Blob).arrayBuffer());\n }\n\n throw new Error(`Unexpected datatype: ${data}`);\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { bufWkt } from '@dxos/protocols/buf';\nimport { SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nimport { Protocol } from './protocol';\n\nexport const protocol = new Protocol([SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema, bufWkt.AnySchema]);\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,iBAAiB;AAC1B,SAASC,KAAKC,cAAc;AAC5B,SAAuBC,qBAAsC;AAC7D,SAASC,qBAAqB;;AAIvB,IAAMC,cAAc,CAACC,aAAqB,uBAAuBA,QAAAA;AAKjE,IAAMC,WAAN,MAAMA;EAGXC,YAAYC,OAA0B;AACpC,SAAKC,gBAAgBT,IAAIU,eAAc,GAAIF,KAAAA;EAC7C;EAEA,IAAIG,eAA6B;AAC/B,WAAO,KAAKF;EACd;EAEAG,OAAOC,SAAuB;AAC5B,QAAI;AACF,aAAOb,IAAIY,OAAOV,eAAeW,SAAS;QAAEC,UAAU,KAAKH;MAAa,CAAA;IAC1E,SAASI,KAAK;AACZ,aAAO;QAAEC,MAAM,KAAKC,eAAeJ,OAAAA;MAAS;IAC9C;EACF;;;;EAKAK,WAAyCL,SAAkBG,MAAoC;AAC7FjB,cAAUc,QAAQM,SAAO,QAAA;;;;;;;;;AACzB,UAAMC,kBAAkB,KAAKH,eAAeJ,OAAAA;AAC5C,QAAIG,QAAQA,KAAKX,aAAae,iBAAiB;AAC7C,YAAM,IAAIC,MAAM,4BAA4BD,eAAAA,cAA6BJ,KAAKX,QAAQ,EAAE;IAC1F;AAEAN,cAAUE,OAAOqB,MAAMT,QAAQM,SAASH,IAAAA,GAAO,4BAA4BI,eAAAA,KAAkB;;;;;;;;;AAC7F,UAAMD,UAAUlB,OAAOsB,UAAUV,QAAQM,SAAS,KAAKR,YAAY;AACnEZ,cAAUoB,SAAS,kBAAkBC,eAAAA,KAAkB;;;;;;;;;AACvD,WAAOD;EACT;;;;EAKAF,eAAeJ,SAAsC;AACnD,QAAI,CAACA,QAAQM,SAAS;AACpB,aAAOK;IACT;AAEA,UAAM,CAAA,EAAGR,IAAAA,IAAQH,QAAQM,QAAQM,QAAQC,MAAM,GAAA;AAC/C,WAAOV;EACT;;;;EAKAW,cACEX,MACA,EACEY,QACAC,QACAV,SACAW,UAAS,GAOX;AACA,WAAO9B,IAAI+B,OAAO7B,eAAe;MAC/B8B,YAAW,oBAAIC,KAAAA,GAAOC,YAAW;MACjCN;MACAC;MACAC;MACAX,SAASA,UAAUlB,OAAOkC,QAAQnB,MAAMhB,IAAI+B,OAAOf,MAAMG,OAAAA,CAAAA,IAAYK;IACvE,CAAA;EACF;AACF;AAKO,IAAMY,eAAe,OAAOC,SAAAA;AAEjC,MAAIA,gBAAgBC,QAAQ;AAC1B,WAAOnC,cAAckC,IAAAA;EACvB;AAGA,MAAIA,gBAAgBE,MAAM;AACxB,WAAO,IAAIC,WAAW,MAAOH,KAAcI,YAAW,CAAA;EACxD;AAEA,QAAM,IAAIpB,MAAM,wBAAwBgB,IAAAA,EAAM;AAChD;;;ACrGA,SAASK,UAAAA,eAAc;AACvB,SAASC,oBAAoBC,qBAAqBC,yBAAyB;AAIpE,IAAMC,WAAW,IAAIC,SAAS;EAACC;EAAoBC;EAAqBC;EAAmBC,QAAOC;CAAU;",
|
|
6
|
-
"names": ["invariant", "buf", "bufWkt", "MessageSchema", "bufferToArray", "getTypename", "typeName", "Protocol", "constructor", "types", "_typeRegistry", "createRegistry", "typeRegistry", "toJson", "message", "registry", "err", "type", "getPayloadType", "getPayload", "payload", "payloadTypename", "Error", "anyIs", "anyUnpack", "undefined", "typeUrl", "split", "createMessage", "source", "target", "serviceId", "create", "timestamp", "Date", "toISOString", "anyPack", "toUint8Array", "data", "Buffer", "Blob", "Uint8Array", "arrayBuffer", "bufWkt", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "protocol", "Protocol", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "bufWkt", "AnySchema"]
|
|
7
|
-
}
|