@dxos/edge-client 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8
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/LICENSE +102 -5
- package/dist/lib/{node-esm/chunk-R6K4IIBW.mjs → neutral/chunk-L5ZHLJ4B.mjs} +57 -53
- package/dist/lib/neutral/chunk-L5ZHLJ4B.mjs.map +7 -0
- package/dist/lib/neutral/chunk-WQKMEZJR.mjs +30 -0
- package/dist/lib/neutral/chunk-WQKMEZJR.mjs.map +7 -0
- package/dist/lib/neutral/cors-proxy.mjs +7 -0
- package/dist/lib/{browser → neutral}/edge-ws-muxer.mjs +1 -1
- package/dist/lib/neutral/index.mjs +1448 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/{browser → neutral}/testing/index.mjs +6 -31
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/auth.d.ts.map +1 -1
- package/dist/types/src/base-http-client.d.ts +48 -0
- package/dist/types/src/base-http-client.d.ts.map +1 -0
- package/dist/types/src/cors-proxy.d.ts +6 -0
- package/dist/types/src/cors-proxy.d.ts.map +1 -0
- package/dist/types/src/edge-ai-http-client.d.ts +65 -0
- package/dist/types/src/edge-ai-http-client.d.ts.map +1 -0
- package/dist/types/src/edge-client.d.ts +17 -14
- package/dist/types/src/edge-client.d.ts.map +1 -1
- package/dist/types/src/edge-http-client.d.ts +81 -54
- 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 +20 -0
- package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
- package/dist/types/src/edge-ws-muxer.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/http-client.d.ts +10 -7
- package/dist/types/src/http-client.d.ts.map +1 -1
- package/dist/types/src/hub-http-client.d.ts +39 -0
- package/dist/types/src/hub-http-client.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +8 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol.d.ts +1 -1
- package/dist/types/src/protocol.d.ts.map +1 -1
- package/dist/types/src/testing/test-server.d.ts.map +1 -1
- package/dist/types/src/testing/test-utils.d.ts +3 -3
- package/dist/types/src/testing/test-utils.d.ts.map +1 -1
- package/dist/types/src/utils.d.ts +1 -1
- package/dist/types/src/utils.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +33 -32
- package/src/base-http-client.ts +243 -0
- package/src/cors-proxy.ts +38 -0
- package/src/edge-ai-http-client.ts +129 -0
- package/src/edge-client.test.ts +20 -15
- package/src/edge-client.ts +105 -44
- package/src/edge-http-client.test.ts +37 -3
- package/src/edge-http-client.ts +312 -190
- package/src/edge-ws-connection.ts +120 -6
- package/src/edge-ws-muxer.ts +49 -5
- package/src/http-client.test.ts +11 -7
- package/src/http-client.ts +18 -8
- package/src/hub-http-client.ts +118 -0
- package/src/index.ts +8 -3
- package/src/testing/test-utils.ts +7 -7
- package/dist/lib/browser/chunk-SUXH7FH6.mjs +0 -304
- package/dist/lib/browser/chunk-SUXH7FH6.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -1124
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-R6K4IIBW.mjs.map +0 -7
- package/dist/lib/node-esm/edge-ws-muxer.mjs +0 -12
- package/dist/lib/node-esm/index.mjs +0 -1125
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -186
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- /package/dist/lib/{browser/edge-ws-muxer.mjs.map → neutral/cors-proxy.mjs.map} +0 -0
- /package/dist/lib/{node-esm → neutral}/edge-ws-muxer.mjs.map +0 -0
package/LICENSE
CHANGED
|
@@ -1,8 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
Copyright (c) 2022 DXOS
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
## Abbreviation
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 DXOS
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
-
|
|
3
1
|
// src/edge-ws-muxer.ts
|
|
4
2
|
import { Trigger } from "@dxos/async";
|
|
5
3
|
import { log } from "@dxos/log";
|
|
@@ -40,38 +38,14 @@ var Protocol = class {
|
|
|
40
38
|
* Return the payload with the given type.
|
|
41
39
|
*/
|
|
42
40
|
getPayload(message, type) {
|
|
43
|
-
invariant(message.payload, void 0, {
|
|
44
|
-
F: __dxlog_file,
|
|
45
|
-
L: 40,
|
|
46
|
-
S: this,
|
|
47
|
-
A: [
|
|
48
|
-
"message.payload",
|
|
49
|
-
""
|
|
50
|
-
]
|
|
51
|
-
});
|
|
41
|
+
invariant(message.payload, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 33, S: this, A: ["message.payload", ""] });
|
|
52
42
|
const payloadTypename = this.getPayloadType(message);
|
|
53
43
|
if (type && type.typeName !== payloadTypename) {
|
|
54
44
|
throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);
|
|
55
45
|
}
|
|
56
|
-
invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, {
|
|
57
|
-
F: __dxlog_file,
|
|
58
|
-
L: 46,
|
|
59
|
-
S: this,
|
|
60
|
-
A: [
|
|
61
|
-
"bufWkt.anyIs(message.payload, type)",
|
|
62
|
-
"`Unexpected payload type: ${payloadTypename}}`"
|
|
63
|
-
]
|
|
64
|
-
});
|
|
46
|
+
invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 38, S: this, A: ["bufWkt.anyIs(message.payload, type)", "`Unexpected payload type: ${payloadTypename}}`"] });
|
|
65
47
|
const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry);
|
|
66
|
-
invariant(payload, `Empty payload: ${payloadTypename}}`, {
|
|
67
|
-
F: __dxlog_file,
|
|
68
|
-
L: 48,
|
|
69
|
-
S: this,
|
|
70
|
-
A: [
|
|
71
|
-
"payload",
|
|
72
|
-
"`Empty payload: ${payloadTypename}}`"
|
|
73
|
-
]
|
|
74
|
-
});
|
|
48
|
+
invariant(payload, `Empty payload: ${payloadTypename}}`, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 40, S: this, A: ["payload", "`Empty payload: ${payloadTypename}}`"] });
|
|
75
49
|
return payload;
|
|
76
50
|
}
|
|
77
51
|
/**
|
|
@@ -126,16 +100,13 @@ var MAX_BUFFERED_AMOUNT = CLOUDFLARE_MESSAGE_MAX_BYTES;
|
|
|
126
100
|
var BUFFER_FULL_BACKOFF_TIMEOUT = 100;
|
|
127
101
|
var WebSocketMuxer = class {
|
|
128
102
|
_ws;
|
|
129
|
-
_inMessageAccumulator;
|
|
130
|
-
_outMessageChunks;
|
|
131
|
-
_outMessageChannelByService;
|
|
103
|
+
_inMessageAccumulator = /* @__PURE__ */ new Map();
|
|
104
|
+
_outMessageChunks = /* @__PURE__ */ new Map();
|
|
105
|
+
_outMessageChannelByService = /* @__PURE__ */ new Map();
|
|
132
106
|
_sendTimeout;
|
|
133
107
|
_maxChunkLength;
|
|
134
108
|
constructor(_ws, config) {
|
|
135
109
|
this._ws = _ws;
|
|
136
|
-
this._inMessageAccumulator = /* @__PURE__ */ new Map();
|
|
137
|
-
this._outMessageChunks = /* @__PURE__ */ new Map();
|
|
138
|
-
this._outMessageChannelByService = /* @__PURE__ */ new Map();
|
|
139
110
|
this._maxChunkLength = config?.maxChunkLength ?? MAX_CHUNK_LENGTH;
|
|
140
111
|
}
|
|
141
112
|
/**
|
|
@@ -150,12 +121,7 @@ var WebSocketMuxer = class {
|
|
|
150
121
|
serviceId: message.serviceId,
|
|
151
122
|
payload: protocol.getPayloadType(message),
|
|
152
123
|
channelId
|
|
153
|
-
}, {
|
|
154
|
-
F: __dxlog_file2,
|
|
155
|
-
L: 62,
|
|
156
|
-
S: this,
|
|
157
|
-
C: (f, a) => f(...a)
|
|
158
|
-
});
|
|
124
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 44, S: this });
|
|
159
125
|
return;
|
|
160
126
|
}
|
|
161
127
|
if (channelId == null || binary.length < this._maxChunkLength) {
|
|
@@ -168,6 +134,14 @@ var WebSocketMuxer = class {
|
|
|
168
134
|
]));
|
|
169
135
|
return;
|
|
170
136
|
}
|
|
137
|
+
const chunkCount = Math.ceil(binary.length / this._maxChunkLength);
|
|
138
|
+
log("muxer sending segmented message", {
|
|
139
|
+
byteLength: binary.byteLength,
|
|
140
|
+
chunkCount,
|
|
141
|
+
channelId,
|
|
142
|
+
serviceId: message.serviceId,
|
|
143
|
+
payload: protocol.getPayloadType(message)
|
|
144
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 63, S: this });
|
|
171
145
|
const terminatorSentTrigger = new Trigger();
|
|
172
146
|
const messageChunks = [];
|
|
173
147
|
for (let i = 0; i < binary.length; i += this._maxChunkLength) {
|
|
@@ -205,28 +179,58 @@ var WebSocketMuxer = class {
|
|
|
205
179
|
this._outMessageChunks.set(channelId, messageChunks);
|
|
206
180
|
}
|
|
207
181
|
this._sendChunkedMessages();
|
|
208
|
-
|
|
182
|
+
await terminatorSentTrigger.wait();
|
|
183
|
+
log.debug("muxer segmented message send enqueued", {
|
|
184
|
+
byteLength: binary.byteLength,
|
|
185
|
+
chunkCount,
|
|
186
|
+
channelId,
|
|
187
|
+
serviceId: message.serviceId
|
|
188
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 108, S: this });
|
|
209
189
|
}
|
|
210
190
|
receiveData(data) {
|
|
211
191
|
if ((data[0] & FLAG_SEGMENT_SEQ) === 0) {
|
|
212
192
|
return buf2.fromBinary(MessageSchema2, data.slice(1));
|
|
213
193
|
}
|
|
214
194
|
const [flags, channelId, ...payload] = data;
|
|
195
|
+
const chunkPayload = Buffer.from(payload);
|
|
215
196
|
let chunkAccumulator = this._inMessageAccumulator.get(channelId);
|
|
216
197
|
if (chunkAccumulator) {
|
|
217
|
-
chunkAccumulator.push(
|
|
198
|
+
chunkAccumulator.push(chunkPayload);
|
|
218
199
|
} else {
|
|
219
200
|
chunkAccumulator = [
|
|
220
|
-
|
|
201
|
+
chunkPayload
|
|
221
202
|
];
|
|
222
203
|
this._inMessageAccumulator.set(channelId, chunkAccumulator);
|
|
204
|
+
log.debug("muxer started receiving segmented message", {
|
|
205
|
+
channelId,
|
|
206
|
+
firstChunkBytes: chunkPayload.byteLength
|
|
207
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 129, S: this });
|
|
223
208
|
}
|
|
224
209
|
if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {
|
|
225
210
|
return void 0;
|
|
226
211
|
}
|
|
227
|
-
const
|
|
212
|
+
const reassembled = Buffer.concat(chunkAccumulator);
|
|
213
|
+
const chunkCount = chunkAccumulator.length;
|
|
228
214
|
this._inMessageAccumulator.delete(channelId);
|
|
229
|
-
|
|
215
|
+
try {
|
|
216
|
+
const message = buf2.fromBinary(MessageSchema2, reassembled);
|
|
217
|
+
log("muxer reassembled segmented message", {
|
|
218
|
+
channelId,
|
|
219
|
+
chunkCount,
|
|
220
|
+
byteLength: reassembled.byteLength,
|
|
221
|
+
serviceId: message.serviceId,
|
|
222
|
+
payloadBytes: message.payload?.value?.byteLength
|
|
223
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 142, S: this });
|
|
224
|
+
return message;
|
|
225
|
+
} catch (error) {
|
|
226
|
+
log.error("muxer failed to decode reassembled message", {
|
|
227
|
+
channelId,
|
|
228
|
+
chunkCount,
|
|
229
|
+
byteLength: reassembled.byteLength,
|
|
230
|
+
cause: error instanceof Error ? error.message : String(error)
|
|
231
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 151, S: this });
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
230
234
|
}
|
|
231
235
|
destroy() {
|
|
232
236
|
if (this._sendTimeout) {
|
|
@@ -246,12 +250,7 @@ var WebSocketMuxer = class {
|
|
|
246
250
|
}
|
|
247
251
|
const send = () => {
|
|
248
252
|
if (this._ws.readyState === 2 || this._ws.readyState === 3) {
|
|
249
|
-
log.warn("send called for closed websocket", void 0, {
|
|
250
|
-
F: __dxlog_file2,
|
|
251
|
-
L: 146,
|
|
252
|
-
S: this,
|
|
253
|
-
C: (f, a) => f(...a)
|
|
254
|
-
});
|
|
253
|
+
log.warn("send called for closed websocket", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 178, S: this });
|
|
255
254
|
this._sendTimeout = void 0;
|
|
256
255
|
return;
|
|
257
256
|
}
|
|
@@ -260,6 +259,11 @@ var WebSocketMuxer = class {
|
|
|
260
259
|
for (const [channelId, messages] of this._outMessageChunks.entries()) {
|
|
261
260
|
if (this._ws.bufferedAmount != null) {
|
|
262
261
|
if (this._ws.bufferedAmount + MAX_CHUNK_LENGTH > MAX_BUFFERED_AMOUNT) {
|
|
262
|
+
log.debug("muxer send paused (websocket buffer full)", {
|
|
263
|
+
channelId,
|
|
264
|
+
bufferedAmount: this._ws.bufferedAmount,
|
|
265
|
+
pendingChannels: this._outMessageChunks.size
|
|
266
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 187, S: this });
|
|
263
267
|
timeout = BUFFER_FULL_BACKOFF_TIMEOUT;
|
|
264
268
|
break;
|
|
265
269
|
}
|
|
@@ -303,4 +307,4 @@ export {
|
|
|
303
307
|
CLOUDFLARE_RPC_MAX_BYTES,
|
|
304
308
|
WebSocketMuxer
|
|
305
309
|
};
|
|
306
|
-
//# sourceMappingURL=chunk-
|
|
310
|
+
//# sourceMappingURL=chunk-L5ZHLJ4B.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/edge-ws-muxer.ts", "../../../src/defs.ts", "../../../src/protocol.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport { Trigger } from '@dxos/async';\nimport { log } from '@dxos/log';\nimport { buf } from '@dxos/protocols/buf';\nimport { type Message, MessageSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nimport { protocol } from './defs';\n\n/**\n * 0000 0001 - message contains a part of segmented message chunk sequence.\n * The next byte defines a channel id and the rest of the message contains a part of Message proto binary.\n * Messages from different channels might interleave.\n * When the flag is NOT set the rest of the message should be interpreted as the valid Message proto binary.\n */\nconst FLAG_SEGMENT_SEQ = 1;\n/**\n * 0000 0010 - message terminates a segmented message chunk sequence.\n * All the chunks accumulated for the channel specified by the second byte can be concatenated\n * and interpreted as a valid Message proto binary.\n */\nconst FLAG_SEGMENT_SEQ_TERMINATED = 1 << 1;\n\n/**\n * https://developers.cloudflare.com/durable-objects/platform/limits/\n */\nexport const CLOUDFLARE_MESSAGE_MAX_BYTES = 1000 * 1000; // 1MB\nexport const CLOUDFLARE_RPC_MAX_BYTES = 32 * 1000 * 1000; // 32MB\n\nconst MAX_CHUNK_LENGTH = 16384;\nconst MAX_BUFFERED_AMOUNT = CLOUDFLARE_MESSAGE_MAX_BYTES;\nconst BUFFER_FULL_BACKOFF_TIMEOUT = 100;\n\nexport class WebSocketMuxer {\n private readonly _inMessageAccumulator = new Map<number, Buffer[]>();\n private readonly _outMessageChunks = new Map<number, MessageChunk[]>();\n private readonly _outMessageChannelByService = new Map<string, number>();\n\n private _sendTimeout: any | undefined;\n\n private readonly _maxChunkLength: number;\n\n constructor(\n private readonly _ws: WebSocketCompat,\n config?: { maxChunkLength: number },\n ) {\n this._maxChunkLength = config?.maxChunkLength ?? MAX_CHUNK_LENGTH;\n }\n\n /**\n * Resolves when all the message chunks get enqueued for sending.\n */\n public async send(message: Message): Promise<void> {\n const binary = buf.toBinary(MessageSchema, message);\n const channelId = this._resolveChannel(message);\n if (\n (channelId == null && binary.byteLength > CLOUDFLARE_MESSAGE_MAX_BYTES) ||\n binary.byteLength > CLOUDFLARE_RPC_MAX_BYTES\n ) {\n log.error('Large message dropped', {\n byteLength: binary.byteLength,\n serviceId: message.serviceId,\n payload: protocol.getPayloadType(message),\n channelId,\n });\n return;\n }\n\n if (channelId == null || binary.length < this._maxChunkLength) {\n const flags = Buffer.from([0]);\n this._ws.send(Buffer.concat([flags, binary]));\n return;\n }\n\n const chunkCount = Math.ceil(binary.length / this._maxChunkLength);\n log('muxer sending segmented message', {\n byteLength: binary.byteLength,\n chunkCount,\n channelId,\n serviceId: message.serviceId,\n payload: protocol.getPayloadType(message),\n });\n\n const terminatorSentTrigger = new Trigger();\n const messageChunks: MessageChunk[] = [];\n for (let i = 0; i < binary.length; i += this._maxChunkLength) {\n const chunk = binary.slice(i, i + this._maxChunkLength);\n const isLastChunk = i + this._maxChunkLength >= binary.length;\n if (isLastChunk) {\n const flags = Buffer.from([FLAG_SEGMENT_SEQ | FLAG_SEGMENT_SEQ_TERMINATED, channelId]);\n messageChunks.push({ payload: Buffer.concat([flags, chunk]), trigger: terminatorSentTrigger });\n } else {\n const flags = Buffer.from([FLAG_SEGMENT_SEQ, channelId]);\n messageChunks.push({ payload: Buffer.concat([flags, chunk]) });\n }\n }\n\n const queuedMessages = this._outMessageChunks.get(channelId);\n if (queuedMessages) {\n queuedMessages.push(...messageChunks);\n } else {\n this._outMessageChunks.set(channelId, messageChunks);\n }\n\n this._sendChunkedMessages();\n\n await terminatorSentTrigger.wait();\n log.debug('muxer segmented message send enqueued', {\n byteLength: binary.byteLength,\n chunkCount,\n channelId,\n serviceId: message.serviceId,\n });\n }\n\n public receiveData(data: Uint8Array): Message | undefined {\n if ((data[0] & FLAG_SEGMENT_SEQ) === 0) {\n return buf.fromBinary(MessageSchema, data.slice(1));\n }\n\n const [flags, channelId, ...payload] = data;\n const chunkPayload = Buffer.from(payload);\n let chunkAccumulator = this._inMessageAccumulator.get(channelId);\n if (chunkAccumulator) {\n chunkAccumulator.push(chunkPayload);\n } else {\n chunkAccumulator = [chunkPayload];\n this._inMessageAccumulator.set(channelId, chunkAccumulator);\n log.debug('muxer started receiving segmented message', {\n channelId,\n firstChunkBytes: chunkPayload.byteLength,\n });\n }\n\n if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {\n return undefined;\n }\n\n const reassembled = Buffer.concat(chunkAccumulator);\n const chunkCount = chunkAccumulator.length;\n this._inMessageAccumulator.delete(channelId);\n try {\n const message = buf.fromBinary(MessageSchema, reassembled);\n log('muxer reassembled segmented message', {\n channelId,\n chunkCount,\n byteLength: reassembled.byteLength,\n serviceId: message.serviceId,\n payloadBytes: message.payload?.value?.byteLength,\n });\n return message;\n } catch (error) {\n log.error('muxer failed to decode reassembled message', {\n channelId,\n chunkCount,\n byteLength: reassembled.byteLength,\n cause: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n public destroy(): void {\n if (this._sendTimeout) {\n clearTimeout(this._sendTimeout);\n this._sendTimeout = undefined;\n }\n for (const channelChunks of this._outMessageChunks.values()) {\n channelChunks.forEach((chunk) => chunk.trigger?.wake());\n }\n this._outMessageChunks.clear();\n this._inMessageAccumulator.clear();\n this._outMessageChannelByService.clear();\n }\n\n private _sendChunkedMessages(): void {\n if (this._sendTimeout) {\n return;\n }\n\n const send = () => {\n if (this._ws.readyState === WebSocket.CLOSING || this._ws.readyState === WebSocket.CLOSED) {\n log.warn('send called for closed websocket');\n this._sendTimeout = undefined;\n return;\n }\n\n let timeout = 0;\n const emptyChannels: number[] = [];\n for (const [channelId, messages] of this._outMessageChunks.entries()) {\n if (this._ws.bufferedAmount != null) {\n if (this._ws.bufferedAmount + MAX_CHUNK_LENGTH > MAX_BUFFERED_AMOUNT) {\n log.debug('muxer send paused (websocket buffer full)', {\n channelId,\n bufferedAmount: this._ws.bufferedAmount,\n pendingChannels: this._outMessageChunks.size,\n });\n timeout = BUFFER_FULL_BACKOFF_TIMEOUT;\n break;\n }\n }\n\n const nextMessage = messages.shift();\n if (nextMessage) {\n this._ws.send(nextMessage.payload);\n nextMessage.trigger?.wake();\n } else {\n emptyChannels.push(channelId);\n }\n }\n\n emptyChannels.forEach((channelId) => this._outMessageChunks.delete(channelId));\n\n if (this._outMessageChunks.size > 0) {\n this._sendTimeout = setTimeout(send, timeout);\n } else {\n this._sendTimeout = undefined;\n }\n };\n this._sendTimeout = setTimeout(send);\n }\n\n private _resolveChannel(message: Message): number | undefined {\n if (!message.serviceId) {\n return undefined;\n }\n let id = this._outMessageChannelByService.get(message.serviceId);\n if (!id) {\n id = this._outMessageChannelByService.size + 1;\n this._outMessageChannelByService.set(message.serviceId, id);\n }\n return id;\n }\n}\n\ntype WebSocketCompat = {\n readonly readyState: number;\n /**\n * Not available in workerd.\n */\n bufferedAmount?: number;\n send(message: (ArrayBuffer | ArrayBufferView) | string): void;\n};\n\ntype MessageChunk = {\n payload: Buffer;\n /**\n * Wakes when the payload is enqueued by WebSocket.\n */\n trigger?: Trigger;\n};\n\n/**\n * To avoid using isomorphic-ws on edge.\n */\nenum WebSocket {\n CLOSING = 2,\n CLOSED = 3,\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", "//\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 ): Message {\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"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,eAAe;AACxB,SAASC,WAAW;AACpB,SAASC,OAAAA,YAAW;AACpB,SAAuBC,iBAAAA,sBAAqB;;;ACH5C,SAASC,UAAAA,eAAc;AACvB,SAASC,oBAAoBC,qBAAqBC,yBAAyB;;;ACD3E,SAASC,iBAAiB;AAC1B,SAASC,KAAKC,cAAc;AAC5B,SAAuBC,qBAAsC;AAC7D,SAASC,qBAAqB;AAI9B,IAAA,eAAaC;;AAQX,IAAYC,WAAZ,MAAsC;;EAEtC,YAAA,OAAA;AAEIC,SAAAA,gBAA6B,IAAA,eAAA,GAAA,KAAA;;EAEjC,IAAA,eAAA;AAEAC,WAAOC,KAAuB;;kBAEnBR;;AAAiE,aAAA,IAAA,OAAA,eAAA,SAAA;QACxE,UAAY,KAAA;MACZ,CAAA;aAASS,KAAM;AAA6B,aAAA;QAC9C,MAAA,KAAA,eAAA,OAAA;MACF;IAEA;;;;;aAKQC,SAAAA,MAAkB;AACxB,cAAID,QAAaE,SAAQ,QAAKD,EAAAA,YAAAA,YAAiB,GAAA,cAAA,GAAA,IAAA,GAAA,MAAA,GAAA,CAAA,mBAAA,EAAA,EAAA,CAAA;UAC7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C;cACvCC;cACF,gBAAA,KAAA,IAAA;cACAF,iBAAUrD,KAAAA,kBAAAA;YACV,GAAA,EAAA,YAAA,YAAA,GAAAJ,eAAA,GAAA,KAAA,GAAA,KAAA,CAAA;AACF,sBAAA;AACF;UAEA;QACA;cACE,cAAc4D,SAAAA,MAAY5C;YAC1B4C,aAAY9B;AACd,eAAO,IAAA,KAAA,YAAA,OAAA;AACL+B,sBAAAA,SAAmBlD,KAAAA;QACrB,OAAA;AACF,wBAAA,KAAA,SAAA;QAEAkD;MAEA;oBACOpD,QAAY,CAAA,cAAcqD,KAAAA,kBAAML,OAAAA,SAAAA,CAAAA;AACvC,UAAA,KAAO,kBAAA,OAAA,GAAA;AACL,aAAKhD,eAAe+C,WAAAA,MAAAA,OAAAA;MACtB,OAAA;AACF,aAAA,eAAA;MACI;IACN;AAEQO,SAAAA,eAAgC,WAAsB,IAAA;;kBAEnDP,SAAAA;AACT,QAAA,CAAA,QAAA,WAAA;AACIQ,aAAK;IACT;QACEA,KAAK,KAAKxD,4BAA4ByD,IAAI,QAAG,SAAA;QAC7C,CAAA,IAAKzD;AACP,WAAA,KAAA,4BAAA,OAAA;AACA,WAAOwD,4BAAAA,IAAAA,QAAAA,WAAAA,EAAAA;IACT;AACF,WAAA;EAmBA;;",
|
|
6
|
+
"names": ["Trigger", "log", "buf", "MessageSchema", "bufWkt", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "invariant", "buf", "bufWkt", "MessageSchema", "bufferToArray", "getTypename", "types", "typeRegistry", "toJson", "message", "type", "payloadTypename", "typeName", "anyIs", "payload", "undefined", "timestamp", "Date", "toISOString", "source", "target", "serviceId", "data", "Error", "protocol", "Protocol", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "bufWkt", "AnySchema", "__dxlog_file", "MAX_CHUNK_LENGTH", "MAX_BUFFERED_AMOUNT", "CLOUDFLARE_MESSAGE_MAX_BYTES", "BUFFER_FULL_BACKOFF_TIMEOUT", "WebSocketMuxer", "_inMessageAccumulator", "_outMessageChunks", "_outMessageChannelByService", "_sendTimeout", "_maxChunkLength", "channelId", "binary", "log", "byteLength", "serviceId", "payload", "protocol", "getPayloadType", "chunkCount", "terminatorSentTrigger", "Trigger", "messageChunks", "length", "i", "isLastChunk", "Buffer", "FLAG_SEGMENT_SEQ", "flags", "chunk", "trigger", "queuedMessages", "set", "_sendChunkedMessages", "debug", "receiveData", "buf", "MessageSchema", "data", "chunkAccumulator", "push", "chunkPayload", "firstChunkBytes", "FLAG_SEGMENT_SEQ_TERMINATED", "reassembled", "delete", "fromBinary", "payloadBytes", "message", "error", "cause", "destroy", "clearTimeout", "channelChunks", "clear", "warn", "undefined", "timeout", "bufferedAmount", "pendingChannels", "nextMessage", "emptyChannels", "send", "_resolveChannel", "id", "size"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/cors-proxy.ts
|
|
2
|
+
var LEGACY_CORS_PROXY_URL = "https://cors-proxy.dxos.workers.dev";
|
|
3
|
+
var EDGE_CLIENT_TAG_HEADER = "X-DXOS-Client-Tag";
|
|
4
|
+
var remapAuthorizationForProxy = (headers) => {
|
|
5
|
+
const callerAuth = headers.get("Authorization");
|
|
6
|
+
if (callerAuth !== null) {
|
|
7
|
+
headers.delete("Authorization");
|
|
8
|
+
headers.set("X-Cors-Proxy-Authorization", callerAuth);
|
|
9
|
+
}
|
|
10
|
+
return headers;
|
|
11
|
+
};
|
|
12
|
+
var proxyFetchLegacy = (target, init = {}, clientTag) => {
|
|
13
|
+
const proxyUrl = new URL(`/${target.host}${target.pathname}${target.search}`, LEGACY_CORS_PROXY_URL);
|
|
14
|
+
if (target.protocol === "http:") {
|
|
15
|
+
proxyUrl.searchParams.set("scheme", "http");
|
|
16
|
+
}
|
|
17
|
+
const requestHeaders = remapAuthorizationForProxy(new Headers(init.headers ?? void 0));
|
|
18
|
+
if (clientTag) {
|
|
19
|
+
requestHeaders.set(EDGE_CLIENT_TAG_HEADER, clientTag);
|
|
20
|
+
}
|
|
21
|
+
return fetch(proxyUrl, {
|
|
22
|
+
...init,
|
|
23
|
+
headers: requestHeaders
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
proxyFetchLegacy
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=chunk-WQKMEZJR.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/cors-proxy.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\n// Lightweight CORS-proxy helpers — intentionally free of heavy transitive\n// dependencies so they can be bundled into workerd / browser environments\n// without pulling in protobufjs or similar node-only packages.\n\nconst LEGACY_CORS_PROXY_URL = 'https://cors-proxy.dxos.workers.dev';\n\n// Matches EDGE_CLIENT_TAG_HEADER from @dxos/protocols.\n// Duplicated here to avoid importing the heavy protocols bundle in edge environments.\nconst EDGE_CLIENT_TAG_HEADER = 'X-DXOS-Client-Tag';\n\nconst remapAuthorizationForProxy = (headers: Headers): Headers => {\n const callerAuth = headers.get('Authorization');\n if (callerAuth !== null) {\n headers.delete('Authorization');\n headers.set('X-Cors-Proxy-Authorization', callerAuth);\n }\n return headers;\n};\n\n/**\n * Fetch through the legacy standalone open proxy at `cors-proxy.dxos.workers.dev`.\n * TEMPORARY — delete when the authenticated `/proxy/*` route on edge ships.\n */\nexport const proxyFetchLegacy = (target: URL, init: RequestInit = {}, clientTag?: string): Promise<Response> => {\n const proxyUrl = new URL(`/${target.host}${target.pathname}${target.search}`, LEGACY_CORS_PROXY_URL);\n if (target.protocol === 'http:') {\n proxyUrl.searchParams.set('scheme', 'http');\n }\n const requestHeaders = remapAuthorizationForProxy(new Headers(init.headers ?? undefined));\n if (clientTag) {\n requestHeaders.set(EDGE_CLIENT_TAG_HEADER, clientTag);\n }\n return fetch(proxyUrl, { ...init, headers: requestHeaders });\n};\n"],
|
|
5
|
+
"mappings": ";AAQA,IAAMA,wBAAwB;AAI9B,IAAMC,yBAAyB;AAE/B,IAAMC,6BAA6B,CAACC,YAAAA;AAClC,QAAMC,aAAaD,QAAQE,IAAI,eAAA;AAC/B,MAAID,eAAe,MAAM;AACvBD,YAAQG,OAAO,eAAA;AACfH,YAAQI,IAAI,8BAA8BH,UAAAA;EAC5C;AACA,SAAOD;AACT;AAMO,IAAMK,mBAAmB,CAACC,QAAaC,OAAoB,CAAC,GAAGC,cAAAA;AACpE,QAAMC,WAAW,IAAIC,IAAI,IAAIJ,OAAOK,IAAI,GAAGL,OAAOM,QAAQ,GAAGN,OAAOO,MAAM,IAAIhB,qBAAAA;AAC9E,MAAIS,OAAOQ,aAAa,SAAS;AAC/BL,aAASM,aAAaX,IAAI,UAAU,MAAA;EACtC;AACA,QAAMY,iBAAiBjB,2BAA2B,IAAIkB,QAAQV,KAAKP,WAAWkB,MAAAA,CAAAA;AAC9E,MAAIV,WAAW;AACbQ,mBAAeZ,IAAIN,wBAAwBU,SAAAA;EAC7C;AACA,SAAOW,MAAMV,UAAU;IAAE,GAAGF;IAAMP,SAASgB;EAAe,CAAA;AAC5D;",
|
|
6
|
+
"names": ["LEGACY_CORS_PROXY_URL", "EDGE_CLIENT_TAG_HEADER", "remapAuthorizationForProxy", "headers", "callerAuth", "get", "delete", "set", "proxyFetchLegacy", "target", "init", "clientTag", "proxyUrl", "URL", "host", "pathname", "search", "protocol", "searchParams", "requestHeaders", "Headers", "undefined", "fetch"]
|
|
7
|
+
}
|