@dxos/edge-client 0.8.2-main.fbd8ed0 → 0.8.2-staging.7ac8446
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-ZWJXA37R.mjs +113 -0
- package/dist/lib/browser/chunk-ZWJXA37R.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +50 -93
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +20 -32
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/chunk-ANV2HBEH.cjs +136 -0
- package/dist/lib/node/chunk-ANV2HBEH.cjs.map +7 -0
- package/dist/lib/node/index.cjs +62 -105
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +21 -32
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node-esm/chunk-HNVT57AU.mjs +115 -0
- package/dist/lib/node-esm/chunk-HNVT57AU.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +50 -93
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +20 -32
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/auth.d.ts.map +1 -1
- package/dist/types/src/edge-client.d.ts +2 -7
- package/dist/types/src/edge-client.d.ts.map +1 -1
- package/dist/types/src/edge-http-client.d.ts +1 -0
- package/dist/types/src/edge-http-client.d.ts.map +1 -1
- package/dist/types/src/edge-identity.d.ts.map +1 -1
- package/dist/types/src/edge-ws-connection.d.ts +0 -1
- package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol.d.ts.map +1 -1
- package/dist/types/src/testing/test-utils.d.ts +2 -6
- package/dist/types/src/testing/test-utils.d.ts.map +1 -1
- package/dist/types/src/utils.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +14 -19
- package/src/edge-client.test.ts +4 -5
- package/src/edge-client.ts +8 -16
- package/src/edge-http-client.ts +2 -2
- package/src/edge-ws-connection.ts +18 -36
- package/src/index.ts +0 -1
- package/src/testing/test-utils.ts +26 -33
- package/dist/lib/browser/chunk-TKYUZ5ZK.mjs +0 -302
- package/dist/lib/browser/chunk-TKYUZ5ZK.mjs.map +0 -7
- package/dist/lib/browser/edge-ws-muxer.mjs +0 -11
- package/dist/lib/browser/edge-ws-muxer.mjs.map +0 -7
- package/dist/lib/node/chunk-ZOL3YSDR.cjs +0 -322
- package/dist/lib/node/chunk-ZOL3YSDR.cjs.map +0 -7
- package/dist/lib/node/edge-ws-muxer.cjs +0 -33
- package/dist/lib/node/edge-ws-muxer.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-25HGRGNZ.mjs +0 -304
- package/dist/lib/node-esm/chunk-25HGRGNZ.mjs.map +0 -7
- package/dist/lib/node-esm/edge-ws-muxer.mjs +0 -12
- package/dist/lib/node-esm/edge-ws-muxer.mjs.map +0 -7
- package/dist/types/src/edge-ws-muxer.d.ts +0 -35
- package/dist/types/src/edge-ws-muxer.d.ts.map +0 -1
- package/dist/types/src/edge-ws-muxer.test.d.ts +0 -2
- package/dist/types/src/edge-ws-muxer.test.d.ts.map +0 -1
- package/src/edge-ws-muxer.test.ts +0 -55
- package/src/edge-ws-muxer.ts +0 -217
package/src/edge-ws-muxer.ts
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
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
|
-
}
|