@dxos/edge-client 0.8.2-main.12df754 → 0.8.2-main.30e4dbb

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.
Files changed (58) hide show
  1. package/dist/lib/browser/chunk-TKYUZ5ZK.mjs +302 -0
  2. package/dist/lib/browser/chunk-TKYUZ5ZK.mjs.map +7 -0
  3. package/dist/lib/browser/edge-ws-muxer.mjs +11 -0
  4. package/dist/lib/browser/edge-ws-muxer.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +70 -33
  6. package/dist/lib/browser/index.mjs.map +3 -3
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +32 -20
  9. package/dist/lib/browser/testing/index.mjs.map +3 -3
  10. package/dist/lib/node/chunk-ZOL3YSDR.cjs +322 -0
  11. package/dist/lib/node/chunk-ZOL3YSDR.cjs.map +7 -0
  12. package/dist/lib/node/edge-ws-muxer.cjs +33 -0
  13. package/dist/lib/node/edge-ws-muxer.cjs.map +7 -0
  14. package/dist/lib/node/index.cjs +83 -46
  15. package/dist/lib/node/index.cjs.map +3 -3
  16. package/dist/lib/node/meta.json +1 -1
  17. package/dist/lib/node/testing/index.cjs +32 -21
  18. package/dist/lib/node/testing/index.cjs.map +3 -3
  19. package/dist/lib/node-esm/chunk-25HGRGNZ.mjs +304 -0
  20. package/dist/lib/node-esm/chunk-25HGRGNZ.mjs.map +7 -0
  21. package/dist/lib/node-esm/edge-ws-muxer.mjs +12 -0
  22. package/dist/lib/node-esm/edge-ws-muxer.mjs.map +7 -0
  23. package/dist/lib/node-esm/index.mjs +70 -33
  24. package/dist/lib/node-esm/index.mjs.map +3 -3
  25. package/dist/lib/node-esm/meta.json +1 -1
  26. package/dist/lib/node-esm/testing/index.mjs +32 -20
  27. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  28. package/dist/types/src/auth.d.ts.map +1 -1
  29. package/dist/types/src/edge-http-client.d.ts +0 -1
  30. package/dist/types/src/edge-http-client.d.ts.map +1 -1
  31. package/dist/types/src/edge-identity.d.ts.map +1 -1
  32. package/dist/types/src/edge-ws-connection.d.ts +1 -0
  33. package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
  34. package/dist/types/src/edge-ws-muxer.d.ts +35 -0
  35. package/dist/types/src/edge-ws-muxer.d.ts.map +1 -0
  36. package/dist/types/src/edge-ws-muxer.test.d.ts +2 -0
  37. package/dist/types/src/edge-ws-muxer.test.d.ts.map +1 -0
  38. package/dist/types/src/index.d.ts +1 -0
  39. package/dist/types/src/index.d.ts.map +1 -1
  40. package/dist/types/src/protocol.d.ts.map +1 -1
  41. package/dist/types/src/testing/test-utils.d.ts +6 -2
  42. package/dist/types/src/testing/test-utils.d.ts.map +1 -1
  43. package/dist/types/src/utils.d.ts.map +1 -1
  44. package/dist/types/tsconfig.tsbuildinfo +1 -1
  45. package/package.json +19 -14
  46. package/src/edge-client.ts +2 -2
  47. package/src/edge-http-client.ts +2 -2
  48. package/src/edge-ws-connection.ts +36 -18
  49. package/src/edge-ws-muxer.test.ts +55 -0
  50. package/src/edge-ws-muxer.ts +217 -0
  51. package/src/index.ts +1 -0
  52. package/src/testing/test-utils.ts +33 -26
  53. package/dist/lib/browser/chunk-ZWJXA37R.mjs +0 -113
  54. package/dist/lib/browser/chunk-ZWJXA37R.mjs.map +0 -7
  55. package/dist/lib/node/chunk-ANV2HBEH.cjs +0 -136
  56. package/dist/lib/node/chunk-ANV2HBEH.cjs.map +0 -7
  57. package/dist/lib/node-esm/chunk-HNVT57AU.mjs +0 -115
  58. package/dist/lib/node-esm/chunk-HNVT57AU.mjs.map +0 -7
@@ -8,21 +8,18 @@ import { scheduleTask, scheduleTaskInterval } from '@dxos/async';
8
8
  import { Context, Resource } from '@dxos/context';
9
9
  import { invariant } from '@dxos/invariant';
10
10
  import { log, logInfo } from '@dxos/log';
11
+ import { EdgeWebsocketProtocol } from '@dxos/protocols';
11
12
  import { buf } from '@dxos/protocols/buf';
12
13
  import { MessageSchema, type Message } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
13
14
 
14
15
  import { protocol } from './defs';
15
16
  import { type EdgeIdentity } from './edge-identity';
17
+ import { CLOUDFLARE_MESSAGE_MAX_BYTES, WebSocketMuxer } from './edge-ws-muxer';
16
18
  import { toUint8Array } from './protocol';
17
19
 
18
20
  const SIGNAL_KEEPALIVE_INTERVAL = 4_000;
19
21
  const SIGNAL_KEEPALIVE_TIMEOUT = 12_000;
20
22
 
21
- /**
22
- * 1MB websocket message limit: https://developers.cloudflare.com/durable-objects/platform/limits/
23
- */
24
- const CLOUDFLARE_MESSAGE_LENGTH_LIMIT = 1024 * 1024;
25
-
26
23
  export type EdgeWsConnectionCallbacks = {
27
24
  onConnected: () => void;
28
25
  onMessage: (message: Message) => void;
@@ -32,6 +29,7 @@ export type EdgeWsConnectionCallbacks = {
32
29
  export class EdgeWsConnection extends Resource {
33
30
  private _inactivityTimeoutCtx: Context | undefined;
34
31
  private _ws: WebSocket | undefined;
32
+ private _wsMuxer: WebSocketMuxer | undefined;
35
33
 
36
34
  constructor(
37
35
  private readonly _identity: EdgeIdentity,
@@ -52,24 +50,34 @@ export class EdgeWsConnection extends Resource {
52
50
 
53
51
  public send(message: Message) {
54
52
  invariant(this._ws);
53
+ invariant(this._wsMuxer);
55
54
  log('sending...', { peerKey: this._identity.peerKey, payload: protocol.getPayloadType(message) });
56
- const encoded = buf.toBinary(MessageSchema, message);
57
- if (encoded.byteLength >= CLOUDFLARE_MESSAGE_LENGTH_LIMIT) {
58
- log.error('edge message dropped due to websocket message limit', {
59
- byteLength: encoded.byteLength,
60
- serviceId: message.serviceId,
61
- payload: protocol.getPayloadType(message),
62
- });
63
- return;
55
+ if (this._ws?.protocol.includes(EdgeWebsocketProtocol.V0)) {
56
+ const binary = buf.toBinary(MessageSchema, message);
57
+ if (binary.length > CLOUDFLARE_MESSAGE_MAX_BYTES) {
58
+ log.error('Message dropped because it was too large (>1MB).', {
59
+ byteLength: binary.byteLength,
60
+ serviceId: message.serviceId,
61
+ payload: protocol.getPayloadType(message),
62
+ });
63
+ return;
64
+ }
65
+ this._ws.send(binary);
66
+ } else {
67
+ this._wsMuxer.send(message).catch((e) => log.catch(e));
64
68
  }
65
- this._ws.send(encoded);
66
69
  }
67
70
 
68
71
  protected override async _open() {
72
+ const baseProtocols = [...Object.values(EdgeWebsocketProtocol)];
69
73
  this._ws = new WebSocket(
70
74
  this._connectionInfo.url.toString(),
71
- this._connectionInfo.protocolHeader ? [this._connectionInfo.protocolHeader] : [],
75
+ this._connectionInfo.protocolHeader
76
+ ? [...baseProtocols, this._connectionInfo.protocolHeader]
77
+ : [...baseProtocols],
72
78
  );
79
+ const muxer = new WebSocketMuxer(this._ws);
80
+ this._wsMuxer = muxer;
73
81
 
74
82
  this._ws.onopen = () => {
75
83
  if (this.isOpen) {
@@ -84,6 +92,7 @@ export class EdgeWsConnection extends Resource {
84
92
  if (this.isOpen) {
85
93
  log.warn('disconnected while being open', { code: event.code, reason: event.reason });
86
94
  this._callbacks.onRestartRequired();
95
+ muxer.destroy();
87
96
  }
88
97
  };
89
98
  this._ws.onerror = (event) => {
@@ -106,9 +115,16 @@ export class EdgeWsConnection extends Resource {
106
115
  this._rescheduleHeartbeatTimeout();
107
116
  return;
108
117
  }
109
- const data = await toUint8Array(event.data);
110
- if (this.isOpen) {
111
- const message = buf.fromBinary(MessageSchema, data);
118
+ const bytes = await toUint8Array(event.data);
119
+ if (!this.isOpen) {
120
+ return;
121
+ }
122
+
123
+ const message = this._ws?.protocol?.includes(EdgeWebsocketProtocol.V0)
124
+ ? buf.fromBinary(MessageSchema, bytes)
125
+ : muxer.receiveData(bytes);
126
+
127
+ if (message) {
112
128
  log('received', { from: message.source, payload: protocol.getPayloadType(message) });
113
129
  this._callbacks.onMessage(message);
114
130
  }
@@ -121,6 +137,8 @@ export class EdgeWsConnection extends Resource {
121
137
  try {
122
138
  this._ws?.close();
123
139
  this._ws = undefined;
140
+ this._wsMuxer?.destroy();
141
+ this._wsMuxer = undefined;
124
142
  } catch (err) {
125
143
  if (err instanceof Error && err.message.includes('WebSocket is closed before the connection is established.')) {
126
144
  return;
@@ -0,0 +1,55 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { describe, expect, test } from 'vitest';
6
+
7
+ import { bufWkt } from '@dxos/protocols/buf';
8
+ import { TextMessageSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
9
+
10
+ import { protocol } from './defs';
11
+ import type { EdgeIdentity } from './edge-identity';
12
+ import { WebSocketMuxer } from './edge-ws-muxer';
13
+
14
+ const MAX_CHUNK_LENGTH = 16;
15
+
16
+ describe('WebSocketMuxerTest', () => {
17
+ test('basic message reassembly', async () => {
18
+ const { muxer: muxer1, sentMessages } = await createMuxer();
19
+ const { muxer: muxer2 } = await createMuxer();
20
+ const content = 'A'.repeat(MAX_CHUNK_LENGTH);
21
+ await muxer1.send(textMessage(content));
22
+
23
+ expect(sentMessages.length).toBeGreaterThan(1);
24
+ for (const chunk of sentMessages.slice(0, -1)) {
25
+ expect(muxer2.receiveData(chunk)).toBeUndefined();
26
+ }
27
+
28
+ const reassembledMessage = muxer2.receiveData(sentMessages.at(-1)!)!;
29
+ expect(reassembledMessage).toBeDefined();
30
+
31
+ const decoded = bufWkt.anyUnpack(reassembledMessage.payload!, TextMessageSchema);
32
+ expect(decoded?.message).toStrictEqual(content);
33
+ });
34
+ });
35
+
36
+ const textMessage = (message: string, source?: EdgeIdentity) =>
37
+ protocol.createMessage(TextMessageSchema, {
38
+ source: source && { peerKey: source.peerKey, identityKey: source.identityKey },
39
+ serviceId: 'test-service',
40
+ payload: { message },
41
+ });
42
+
43
+ const createMuxer = async () => {
44
+ const sentMessages: Uint8Array[] = [];
45
+ const muxer = new WebSocketMuxer(
46
+ {
47
+ readyState: 1,
48
+ send: (message: string) => {
49
+ sentMessages.push(Buffer.from(message));
50
+ },
51
+ },
52
+ { maxChunkLength: MAX_CHUNK_LENGTH },
53
+ );
54
+ return { muxer, sentMessages };
55
+ };
@@ -0,0 +1,217 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Trigger } from '@dxos/async';
6
+ import { log } from '@dxos/log';
7
+ import { buf } from '@dxos/protocols/buf';
8
+ import { MessageSchema, type Message } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
9
+
10
+ import { protocol } from './defs';
11
+
12
+ /**
13
+ * 0000 0001 - message contains a part of segmented message chunk sequence.
14
+ * The next byte defines a channel id and the rest of the message contains a part of Message proto binary.
15
+ * Messages from different channels might interleave.
16
+ * When the flag is NOT set the rest of the message should be interpreted as the valid Message proto binary.
17
+ */
18
+ const FLAG_SEGMENT_SEQ = 1;
19
+ /**
20
+ * 0000 0010 - message terminates a segmented message chunk sequence.
21
+ * All the chunks accumulated for the channel specified by the second byte can be concatenated
22
+ * and interpreted as a valid Message proto binary.
23
+ */
24
+ const FLAG_SEGMENT_SEQ_TERMINATED = 1 << 1;
25
+
26
+ /**
27
+ * https://developers.cloudflare.com/durable-objects/platform/limits/
28
+ */
29
+ export const CLOUDFLARE_MESSAGE_MAX_BYTES = 1000 * 1000; // 1MB
30
+ export const CLOUDFLARE_RPC_MAX_BYTES = 32 * 1000 * 1000; // 32MB
31
+
32
+ const MAX_CHUNK_LENGTH = 16384;
33
+ const MAX_BUFFERED_AMOUNT = CLOUDFLARE_MESSAGE_MAX_BYTES;
34
+ const BUFFER_FULL_BACKOFF_TIMEOUT = 100;
35
+
36
+ export class WebSocketMuxer {
37
+ private readonly _inMessageAccumulator = new Map<number, Buffer[]>();
38
+ private readonly _outMessageChunks = new Map<number, MessageChunk[]>();
39
+ private readonly _outMessageChannelByService = new Map<string, number>();
40
+
41
+ private _sendTimeout: any | undefined;
42
+
43
+ private readonly _maxChunkLength: number;
44
+
45
+ constructor(
46
+ private readonly _ws: WebSocketCompat,
47
+ config?: { maxChunkLength: number },
48
+ ) {
49
+ this._maxChunkLength = config?.maxChunkLength ?? MAX_CHUNK_LENGTH;
50
+ }
51
+
52
+ /**
53
+ * Resolves when all the message chunks get enqueued for sending.
54
+ */
55
+ public async send(message: Message): Promise<void> {
56
+ const binary = buf.toBinary(MessageSchema, message);
57
+ const channelId = this._resolveChannel(message);
58
+ if (
59
+ (channelId == null && binary.byteLength > CLOUDFLARE_MESSAGE_MAX_BYTES) ||
60
+ binary.byteLength > CLOUDFLARE_RPC_MAX_BYTES
61
+ ) {
62
+ log.error('Large message dropped', {
63
+ byteLength: binary.byteLength,
64
+ serviceId: message.serviceId,
65
+ payload: protocol.getPayloadType(message),
66
+ channelId,
67
+ });
68
+ return;
69
+ }
70
+
71
+ if (channelId == null || binary.length < this._maxChunkLength) {
72
+ const flags = Buffer.from([0]);
73
+ this._ws.send(Buffer.concat([flags, binary]));
74
+ return;
75
+ }
76
+
77
+ const terminatorSentTrigger = new Trigger();
78
+ const messageChunks: MessageChunk[] = [];
79
+ for (let i = 0; i < binary.length; i += this._maxChunkLength) {
80
+ const chunk = binary.slice(i, i + this._maxChunkLength);
81
+ const isLastChunk = i + this._maxChunkLength >= binary.length;
82
+ if (isLastChunk) {
83
+ const flags = Buffer.from([FLAG_SEGMENT_SEQ | FLAG_SEGMENT_SEQ_TERMINATED, channelId]);
84
+ messageChunks.push({ payload: Buffer.concat([flags, chunk]), trigger: terminatorSentTrigger });
85
+ } else {
86
+ const flags = Buffer.from([FLAG_SEGMENT_SEQ, channelId]);
87
+ messageChunks.push({ payload: Buffer.concat([flags, chunk]) });
88
+ }
89
+ }
90
+
91
+ const queuedMessages = this._outMessageChunks.get(channelId);
92
+ if (queuedMessages) {
93
+ queuedMessages.push(...messageChunks);
94
+ } else {
95
+ this._outMessageChunks.set(channelId, messageChunks);
96
+ }
97
+
98
+ this._sendChunkedMessages();
99
+
100
+ return terminatorSentTrigger.wait();
101
+ }
102
+
103
+ public receiveData(data: Uint8Array): Message | undefined {
104
+ if ((data[0] & FLAG_SEGMENT_SEQ) === 0) {
105
+ return buf.fromBinary(MessageSchema, data.slice(1));
106
+ }
107
+
108
+ const [flags, channelId, ...payload] = data;
109
+ let chunkAccumulator = this._inMessageAccumulator.get(channelId);
110
+ if (chunkAccumulator) {
111
+ chunkAccumulator.push(Buffer.from(payload));
112
+ } else {
113
+ chunkAccumulator = [Buffer.from(payload)];
114
+ this._inMessageAccumulator.set(channelId, chunkAccumulator);
115
+ }
116
+
117
+ if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {
118
+ return undefined;
119
+ }
120
+
121
+ const message = buf.fromBinary(MessageSchema, Buffer.concat(chunkAccumulator));
122
+ this._inMessageAccumulator.delete(channelId);
123
+ return message;
124
+ }
125
+
126
+ public destroy() {
127
+ if (this._sendTimeout) {
128
+ clearTimeout(this._sendTimeout);
129
+ this._sendTimeout = undefined;
130
+ }
131
+ for (const channelChunks of this._outMessageChunks.values()) {
132
+ channelChunks.forEach((chunk) => chunk.trigger?.wake());
133
+ }
134
+ this._outMessageChunks.clear();
135
+ this._inMessageAccumulator.clear();
136
+ this._outMessageChannelByService.clear();
137
+ }
138
+
139
+ private _sendChunkedMessages() {
140
+ if (this._sendTimeout) {
141
+ return;
142
+ }
143
+
144
+ const send = () => {
145
+ if (this._ws.readyState === WebSocket.CLOSING || this._ws.readyState === WebSocket.CLOSED) {
146
+ log.warn('send called for closed websocket');
147
+ this._sendTimeout = undefined;
148
+ return;
149
+ }
150
+
151
+ let timeout = 0;
152
+ const emptyChannels: number[] = [];
153
+ for (const [channelId, messages] of this._outMessageChunks.entries()) {
154
+ if (this._ws.bufferedAmount != null) {
155
+ if (this._ws.bufferedAmount + MAX_CHUNK_LENGTH > MAX_BUFFERED_AMOUNT) {
156
+ timeout = BUFFER_FULL_BACKOFF_TIMEOUT;
157
+ break;
158
+ }
159
+ }
160
+
161
+ const nextMessage = messages.shift();
162
+ if (nextMessage) {
163
+ this._ws.send(nextMessage.payload);
164
+ nextMessage.trigger?.wake();
165
+ } else {
166
+ emptyChannels.push(channelId);
167
+ }
168
+ }
169
+
170
+ emptyChannels.forEach((channelId) => this._outMessageChunks.delete(channelId));
171
+
172
+ if (this._outMessageChunks.size > 0) {
173
+ this._sendTimeout = setTimeout(send, timeout);
174
+ } else {
175
+ this._sendTimeout = undefined;
176
+ }
177
+ };
178
+ this._sendTimeout = setTimeout(send);
179
+ }
180
+
181
+ private _resolveChannel(message: Message): number | undefined {
182
+ if (!message.serviceId) {
183
+ return undefined;
184
+ }
185
+ let id = this._outMessageChannelByService.get(message.serviceId);
186
+ if (!id) {
187
+ id = this._outMessageChannelByService.size + 1;
188
+ this._outMessageChannelByService.set(message.serviceId, id);
189
+ }
190
+ return id;
191
+ }
192
+ }
193
+
194
+ type WebSocketCompat = {
195
+ readonly readyState: number;
196
+ /**
197
+ * Not available in workerd.
198
+ */
199
+ bufferedAmount?: number;
200
+ send(message: (ArrayBuffer | ArrayBufferView) | string): void;
201
+ };
202
+
203
+ type MessageChunk = {
204
+ payload: Buffer;
205
+ /**
206
+ * Wakes when the payload is enqueued by WebSocket.
207
+ */
208
+ trigger?: Trigger;
209
+ };
210
+
211
+ /**
212
+ * To avoid using isomorphic-ws on edge.
213
+ */
214
+ enum WebSocket {
215
+ CLOSING = 2,
216
+ CLOSED = 3,
217
+ }
package/src/index.ts CHANGED
@@ -11,3 +11,4 @@ export * from './errors';
11
11
  export * from './auth';
12
12
  export * from './edge-http-client';
13
13
  export * from './edge-identity';
14
+ export * from './edge-ws-muxer';
@@ -6,10 +6,12 @@ import WebSocket from 'isomorphic-ws';
6
6
 
7
7
  import { Trigger } from '@dxos/async';
8
8
  import { log } from '@dxos/log';
9
+ import { EdgeWebsocketProtocol } from '@dxos/protocols';
9
10
  import { buf } from '@dxos/protocols/buf';
10
11
  import { MessageSchema, TextMessageSchema, type Message } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
11
12
 
12
13
  import { protocol } from '../defs';
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 server = new WebSocket.Server({ port, verifyClient: createConnectionDelayHandler(params) });
26
+ const wsServer = new WebSocket.Server({
27
+ port,
28
+ verifyClient: createConnectionDelayHandler(params),
29
+ handleProtocols: () => [EdgeWebsocketProtocol.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
- server.on('connection', (ws) => {
34
- connection = ws;
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 { request, requestPayload } = await decodeRequest(params, data);
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://localhost:${port}`,
64
- cleanup: () => server.close(),
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(buf.toBinary(MessageSchema, msg));
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: () => WebSocket) => {
103
+ const createResponseSender = (connection: () => WebSocketMuxer) => {
93
104
  return (request: Message, responsePayload: Uint8Array) => {
94
105
  const recipient = request.source!;
95
- connection().send(
96
- buf.toBinary(
97
- MessageSchema,
98
- buf.create(MessageSchema, {
99
- source: {
100
- identityKey: recipient.identityKey,
101
- peerKey: recipient.peerKey,
102
- },
103
- serviceId: request.serviceId!,
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 decodeRequest = async (params: TestEdgeWsServerParams | undefined, data: any) => {
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
- }