@dxos/edge-client 0.8.2-main.f081794 → 0.8.2-main.fbd8ed0
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-TKYUZ5ZK.mjs +302 -0
- package/dist/lib/browser/chunk-TKYUZ5ZK.mjs.map +7 -0
- package/dist/lib/browser/edge-ws-muxer.mjs +11 -0
- package/dist/lib/browser/edge-ws-muxer.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +70 -33
- package/dist/lib/browser/index.mjs.map +3 -3
- 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-ZOL3YSDR.cjs +322 -0
- package/dist/lib/node/chunk-ZOL3YSDR.cjs.map +7 -0
- package/dist/lib/node/edge-ws-muxer.cjs +33 -0
- package/dist/lib/node/edge-ws-muxer.cjs.map +7 -0
- package/dist/lib/node/index.cjs +83 -46
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +32 -21
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node-esm/chunk-25HGRGNZ.mjs +304 -0
- package/dist/lib/node-esm/chunk-25HGRGNZ.mjs.map +7 -0
- package/dist/lib/node-esm/edge-ws-muxer.mjs +12 -0
- package/dist/lib/node-esm/edge-ws-muxer.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +70 -33
- 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 +32 -20
- 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-http-client.d.ts +0 -1
- 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 +1 -0
- package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
- package/dist/types/src/edge-ws-muxer.d.ts +35 -0
- package/dist/types/src/edge-ws-muxer.d.ts.map +1 -0
- package/dist/types/src/edge-ws-muxer.test.d.ts +2 -0
- package/dist/types/src/edge-ws-muxer.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -0
- 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 +6 -2
- 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 +19 -14
- package/src/edge-client.ts +2 -2
- package/src/edge-http-client.ts +2 -2
- package/src/edge-ws-connection.ts +36 -18
- package/src/edge-ws-muxer.test.ts +55 -0
- package/src/edge-ws-muxer.ts +217 -0
- package/src/index.ts +1 -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
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// packages/core/mesh/edge-client/src/edge-ws-muxer.ts
|
|
2
|
+
import { Trigger } from "@dxos/async";
|
|
3
|
+
import { log } from "@dxos/log";
|
|
4
|
+
import { buf as buf2 } from "@dxos/protocols/buf";
|
|
5
|
+
import { MessageSchema as MessageSchema2 } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
6
|
+
|
|
7
|
+
// packages/core/mesh/edge-client/src/defs.ts
|
|
8
|
+
import { bufWkt as bufWkt2 } from "@dxos/protocols/buf";
|
|
9
|
+
import { SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
10
|
+
|
|
11
|
+
// packages/core/mesh/edge-client/src/protocol.ts
|
|
12
|
+
import { invariant } from "@dxos/invariant";
|
|
13
|
+
import { buf, bufWkt } from "@dxos/protocols/buf";
|
|
14
|
+
import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
15
|
+
import { bufferToArray } from "@dxos/util";
|
|
16
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/protocol.ts";
|
|
17
|
+
var getTypename = (typeName) => `type.googleapis.com/${typeName}`;
|
|
18
|
+
var Protocol = class {
|
|
19
|
+
constructor(types) {
|
|
20
|
+
this._typeRegistry = buf.createRegistry(...types);
|
|
21
|
+
}
|
|
22
|
+
get typeRegistry() {
|
|
23
|
+
return this._typeRegistry;
|
|
24
|
+
}
|
|
25
|
+
toJson(message) {
|
|
26
|
+
try {
|
|
27
|
+
return buf.toJson(MessageSchema, message, {
|
|
28
|
+
registry: this.typeRegistry
|
|
29
|
+
});
|
|
30
|
+
} catch (err) {
|
|
31
|
+
return {
|
|
32
|
+
type: this.getPayloadType(message)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Return the payload with the given type.
|
|
38
|
+
*/
|
|
39
|
+
getPayload(message, type) {
|
|
40
|
+
invariant(message.payload, void 0, {
|
|
41
|
+
F: __dxlog_file,
|
|
42
|
+
L: 40,
|
|
43
|
+
S: this,
|
|
44
|
+
A: [
|
|
45
|
+
"message.payload",
|
|
46
|
+
""
|
|
47
|
+
]
|
|
48
|
+
});
|
|
49
|
+
const payloadTypename = this.getPayloadType(message);
|
|
50
|
+
if (type && type.typeName !== payloadTypename) {
|
|
51
|
+
throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);
|
|
52
|
+
}
|
|
53
|
+
invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, {
|
|
54
|
+
F: __dxlog_file,
|
|
55
|
+
L: 46,
|
|
56
|
+
S: this,
|
|
57
|
+
A: [
|
|
58
|
+
"bufWkt.anyIs(message.payload, type)",
|
|
59
|
+
"`Unexpected payload type: ${payloadTypename}}`"
|
|
60
|
+
]
|
|
61
|
+
});
|
|
62
|
+
const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry);
|
|
63
|
+
invariant(payload, `Empty payload: ${payloadTypename}}`, {
|
|
64
|
+
F: __dxlog_file,
|
|
65
|
+
L: 48,
|
|
66
|
+
S: this,
|
|
67
|
+
A: [
|
|
68
|
+
"payload",
|
|
69
|
+
"`Empty payload: ${payloadTypename}}`"
|
|
70
|
+
]
|
|
71
|
+
});
|
|
72
|
+
return payload;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get the payload type.
|
|
76
|
+
*/
|
|
77
|
+
getPayloadType(message) {
|
|
78
|
+
if (!message.payload) {
|
|
79
|
+
return void 0;
|
|
80
|
+
}
|
|
81
|
+
const [, type] = message.payload.typeUrl.split("/");
|
|
82
|
+
return type;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create a packed message.
|
|
86
|
+
*/
|
|
87
|
+
createMessage(type, { source, target, payload, serviceId }) {
|
|
88
|
+
return buf.create(MessageSchema, {
|
|
89
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
90
|
+
source,
|
|
91
|
+
target,
|
|
92
|
+
serviceId,
|
|
93
|
+
payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : void 0
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var toUint8Array = async (data) => {
|
|
98
|
+
if (data instanceof Buffer) {
|
|
99
|
+
return bufferToArray(data);
|
|
100
|
+
}
|
|
101
|
+
if (data instanceof Blob) {
|
|
102
|
+
return new Uint8Array(await data.arrayBuffer());
|
|
103
|
+
}
|
|
104
|
+
throw new Error(`Unexpected datatype: ${data}`);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// packages/core/mesh/edge-client/src/defs.ts
|
|
108
|
+
var protocol = new Protocol([
|
|
109
|
+
SwarmRequestSchema,
|
|
110
|
+
SwarmResponseSchema,
|
|
111
|
+
TextMessageSchema,
|
|
112
|
+
bufWkt2.AnySchema
|
|
113
|
+
]);
|
|
114
|
+
|
|
115
|
+
// packages/core/mesh/edge-client/src/edge-ws-muxer.ts
|
|
116
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-ws-muxer.ts";
|
|
117
|
+
var FLAG_SEGMENT_SEQ = 1;
|
|
118
|
+
var FLAG_SEGMENT_SEQ_TERMINATED = 1 << 1;
|
|
119
|
+
var CLOUDFLARE_MESSAGE_MAX_BYTES = 1e3 * 1e3;
|
|
120
|
+
var CLOUDFLARE_RPC_MAX_BYTES = 32 * 1e3 * 1e3;
|
|
121
|
+
var MAX_CHUNK_LENGTH = 16384;
|
|
122
|
+
var MAX_BUFFERED_AMOUNT = CLOUDFLARE_MESSAGE_MAX_BYTES;
|
|
123
|
+
var BUFFER_FULL_BACKOFF_TIMEOUT = 100;
|
|
124
|
+
var WebSocketMuxer = class {
|
|
125
|
+
constructor(_ws, config) {
|
|
126
|
+
this._ws = _ws;
|
|
127
|
+
this._inMessageAccumulator = /* @__PURE__ */ new Map();
|
|
128
|
+
this._outMessageChunks = /* @__PURE__ */ new Map();
|
|
129
|
+
this._outMessageChannelByService = /* @__PURE__ */ new Map();
|
|
130
|
+
this._maxChunkLength = config?.maxChunkLength ?? MAX_CHUNK_LENGTH;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Resolves when all the message chunks get enqueued for sending.
|
|
134
|
+
*/
|
|
135
|
+
async send(message) {
|
|
136
|
+
const binary = buf2.toBinary(MessageSchema2, message);
|
|
137
|
+
const channelId = this._resolveChannel(message);
|
|
138
|
+
if (channelId == null && binary.byteLength > CLOUDFLARE_MESSAGE_MAX_BYTES || binary.byteLength > CLOUDFLARE_RPC_MAX_BYTES) {
|
|
139
|
+
log.error("Large message dropped", {
|
|
140
|
+
byteLength: binary.byteLength,
|
|
141
|
+
serviceId: message.serviceId,
|
|
142
|
+
payload: protocol.getPayloadType(message),
|
|
143
|
+
channelId
|
|
144
|
+
}, {
|
|
145
|
+
F: __dxlog_file2,
|
|
146
|
+
L: 62,
|
|
147
|
+
S: this,
|
|
148
|
+
C: (f, a) => f(...a)
|
|
149
|
+
});
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (channelId == null || binary.length < this._maxChunkLength) {
|
|
153
|
+
const flags = Buffer.from([
|
|
154
|
+
0
|
|
155
|
+
]);
|
|
156
|
+
this._ws.send(Buffer.concat([
|
|
157
|
+
flags,
|
|
158
|
+
binary
|
|
159
|
+
]));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const terminatorSentTrigger = new Trigger();
|
|
163
|
+
const messageChunks = [];
|
|
164
|
+
for (let i = 0; i < binary.length; i += this._maxChunkLength) {
|
|
165
|
+
const chunk = binary.slice(i, i + this._maxChunkLength);
|
|
166
|
+
const isLastChunk = i + this._maxChunkLength >= binary.length;
|
|
167
|
+
if (isLastChunk) {
|
|
168
|
+
const flags = Buffer.from([
|
|
169
|
+
FLAG_SEGMENT_SEQ | FLAG_SEGMENT_SEQ_TERMINATED,
|
|
170
|
+
channelId
|
|
171
|
+
]);
|
|
172
|
+
messageChunks.push({
|
|
173
|
+
payload: Buffer.concat([
|
|
174
|
+
flags,
|
|
175
|
+
chunk
|
|
176
|
+
]),
|
|
177
|
+
trigger: terminatorSentTrigger
|
|
178
|
+
});
|
|
179
|
+
} else {
|
|
180
|
+
const flags = Buffer.from([
|
|
181
|
+
FLAG_SEGMENT_SEQ,
|
|
182
|
+
channelId
|
|
183
|
+
]);
|
|
184
|
+
messageChunks.push({
|
|
185
|
+
payload: Buffer.concat([
|
|
186
|
+
flags,
|
|
187
|
+
chunk
|
|
188
|
+
])
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const queuedMessages = this._outMessageChunks.get(channelId);
|
|
193
|
+
if (queuedMessages) {
|
|
194
|
+
queuedMessages.push(...messageChunks);
|
|
195
|
+
} else {
|
|
196
|
+
this._outMessageChunks.set(channelId, messageChunks);
|
|
197
|
+
}
|
|
198
|
+
this._sendChunkedMessages();
|
|
199
|
+
return terminatorSentTrigger.wait();
|
|
200
|
+
}
|
|
201
|
+
receiveData(data) {
|
|
202
|
+
if ((data[0] & FLAG_SEGMENT_SEQ) === 0) {
|
|
203
|
+
return buf2.fromBinary(MessageSchema2, data.slice(1));
|
|
204
|
+
}
|
|
205
|
+
const [flags, channelId, ...payload] = data;
|
|
206
|
+
let chunkAccumulator = this._inMessageAccumulator.get(channelId);
|
|
207
|
+
if (chunkAccumulator) {
|
|
208
|
+
chunkAccumulator.push(Buffer.from(payload));
|
|
209
|
+
} else {
|
|
210
|
+
chunkAccumulator = [
|
|
211
|
+
Buffer.from(payload)
|
|
212
|
+
];
|
|
213
|
+
this._inMessageAccumulator.set(channelId, chunkAccumulator);
|
|
214
|
+
}
|
|
215
|
+
if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {
|
|
216
|
+
return void 0;
|
|
217
|
+
}
|
|
218
|
+
const message = buf2.fromBinary(MessageSchema2, Buffer.concat(chunkAccumulator));
|
|
219
|
+
this._inMessageAccumulator.delete(channelId);
|
|
220
|
+
return message;
|
|
221
|
+
}
|
|
222
|
+
destroy() {
|
|
223
|
+
if (this._sendTimeout) {
|
|
224
|
+
clearTimeout(this._sendTimeout);
|
|
225
|
+
this._sendTimeout = void 0;
|
|
226
|
+
}
|
|
227
|
+
for (const channelChunks of this._outMessageChunks.values()) {
|
|
228
|
+
channelChunks.forEach((chunk) => chunk.trigger?.wake());
|
|
229
|
+
}
|
|
230
|
+
this._outMessageChunks.clear();
|
|
231
|
+
this._inMessageAccumulator.clear();
|
|
232
|
+
this._outMessageChannelByService.clear();
|
|
233
|
+
}
|
|
234
|
+
_sendChunkedMessages() {
|
|
235
|
+
if (this._sendTimeout) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const send = () => {
|
|
239
|
+
if (this._ws.readyState === 2 || this._ws.readyState === 3) {
|
|
240
|
+
log.warn("send called for closed websocket", void 0, {
|
|
241
|
+
F: __dxlog_file2,
|
|
242
|
+
L: 146,
|
|
243
|
+
S: this,
|
|
244
|
+
C: (f, a) => f(...a)
|
|
245
|
+
});
|
|
246
|
+
this._sendTimeout = void 0;
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
let timeout = 0;
|
|
250
|
+
const emptyChannels = [];
|
|
251
|
+
for (const [channelId, messages] of this._outMessageChunks.entries()) {
|
|
252
|
+
if (this._ws.bufferedAmount != null) {
|
|
253
|
+
if (this._ws.bufferedAmount + MAX_CHUNK_LENGTH > MAX_BUFFERED_AMOUNT) {
|
|
254
|
+
timeout = BUFFER_FULL_BACKOFF_TIMEOUT;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const nextMessage = messages.shift();
|
|
259
|
+
if (nextMessage) {
|
|
260
|
+
this._ws.send(nextMessage.payload);
|
|
261
|
+
nextMessage.trigger?.wake();
|
|
262
|
+
} else {
|
|
263
|
+
emptyChannels.push(channelId);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
emptyChannels.forEach((channelId) => this._outMessageChunks.delete(channelId));
|
|
267
|
+
if (this._outMessageChunks.size > 0) {
|
|
268
|
+
this._sendTimeout = setTimeout(send, timeout);
|
|
269
|
+
} else {
|
|
270
|
+
this._sendTimeout = void 0;
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
this._sendTimeout = setTimeout(send);
|
|
274
|
+
}
|
|
275
|
+
_resolveChannel(message) {
|
|
276
|
+
if (!message.serviceId) {
|
|
277
|
+
return void 0;
|
|
278
|
+
}
|
|
279
|
+
let id = this._outMessageChannelByService.get(message.serviceId);
|
|
280
|
+
if (!id) {
|
|
281
|
+
id = this._outMessageChannelByService.size + 1;
|
|
282
|
+
this._outMessageChannelByService.set(message.serviceId, id);
|
|
283
|
+
}
|
|
284
|
+
return id;
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
var WebSocket;
|
|
288
|
+
(function(WebSocket2) {
|
|
289
|
+
WebSocket2[WebSocket2["CLOSING"] = 2] = "CLOSING";
|
|
290
|
+
WebSocket2[WebSocket2["CLOSED"] = 3] = "CLOSED";
|
|
291
|
+
})(WebSocket || (WebSocket = {}));
|
|
292
|
+
|
|
293
|
+
export {
|
|
294
|
+
getTypename,
|
|
295
|
+
Protocol,
|
|
296
|
+
toUint8Array,
|
|
297
|
+
protocol,
|
|
298
|
+
CLOUDFLARE_MESSAGE_MAX_BYTES,
|
|
299
|
+
CLOUDFLARE_RPC_MAX_BYTES,
|
|
300
|
+
WebSocketMuxer
|
|
301
|
+
};
|
|
302
|
+
//# sourceMappingURL=chunk-TKYUZ5ZK.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 { MessageSchema, type Message } 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 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 return terminatorSentTrigger.wait();\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 let chunkAccumulator = this._inMessageAccumulator.get(channelId);\n if (chunkAccumulator) {\n chunkAccumulator.push(Buffer.from(payload));\n } else {\n chunkAccumulator = [Buffer.from(payload)];\n this._inMessageAccumulator.set(channelId, chunkAccumulator);\n }\n\n if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {\n return undefined;\n }\n\n const message = buf.fromBinary(MessageSchema, Buffer.concat(chunkAccumulator));\n this._inMessageAccumulator.delete(channelId);\n return message;\n }\n\n public destroy() {\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() {\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 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 ) {\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(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EA,UAAIY,UAAU;AACd,YAAMC,gBAA0B,CAAA;AAChC,iBAAW,CAAC7C,WAAW8C,QAAAA,KAAa,KAAKxD,kBAAkByD,QAAO,GAAI;AACpE,YAAI,KAAK7D,IAAI8D,kBAAkB,MAAM;AACnC,cAAI,KAAK9D,IAAI8D,iBAAiBnE,mBAAmBC,qBAAqB;AACpE8D,sBAAU7D;AACV;UACF;QACF;AAEA,cAAMkE,cAAcH,SAASI,MAAK;AAClC,YAAID,aAAa;AACf,eAAK/D,IAAIQ,KAAKuD,YAAY3C,OAAO;AACjC2C,sBAAY3B,SAASkB,KAAAA;QACvB,OAAO;AACLK,wBAAcxB,KAAKrB,SAAAA;QACrB;MACF;AAEA6C,oBAAcN,QAAQ,CAACvC,cAAc,KAAKV,kBAAkB2C,OAAOjC,SAAAA,CAAAA;AAEnE,UAAI,KAAKV,kBAAkB6D,OAAO,GAAG;AACnC,aAAKhB,eAAeiB,WAAW1D,MAAMkD,OAAAA;MACvC,OAAO;AACL,aAAKT,eAAeH;MACtB;IACF;AACA,SAAKG,eAAeiB,WAAW1D,IAAAA;EACjC;EAEQO,gBAAgBN,SAAsC;AAC5D,QAAI,CAACA,QAAQU,WAAW;AACtB,aAAO2B;IACT;AACA,QAAIqB,KAAK,KAAK9D,4BAA4BiC,IAAI7B,QAAQU,SAAS;AAC/D,QAAI,CAACgD,IAAI;AACPA,WAAK,KAAK9D,4BAA4B4D,OAAO;AAC7C,WAAK5D,4BAA4BkC,IAAI9B,QAAQU,WAAWgD,EAAAA;IAC1D;AACA,WAAOA;EACT;AACF;;CAqBC,SACIC,YAAAA;;;GAAAA,cAAAA,YAAAA,CAAAA,EAAAA;",
|
|
6
|
+
"names": ["Trigger", "log", "buf", "MessageSchema", "bufWkt", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "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", "protocol", "Protocol", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "bufWkt", "AnySchema", "FLAG_SEGMENT_SEQ", "FLAG_SEGMENT_SEQ_TERMINATED", "CLOUDFLARE_MESSAGE_MAX_BYTES", "CLOUDFLARE_RPC_MAX_BYTES", "MAX_CHUNK_LENGTH", "MAX_BUFFERED_AMOUNT", "BUFFER_FULL_BACKOFF_TIMEOUT", "WebSocketMuxer", "constructor", "_ws", "config", "_inMessageAccumulator", "Map", "_outMessageChunks", "_outMessageChannelByService", "_maxChunkLength", "maxChunkLength", "send", "message", "binary", "buf", "toBinary", "MessageSchema", "channelId", "_resolveChannel", "byteLength", "log", "error", "serviceId", "payload", "protocol", "getPayloadType", "length", "flags", "Buffer", "from", "concat", "terminatorSentTrigger", "Trigger", "messageChunks", "i", "chunk", "slice", "isLastChunk", "push", "trigger", "queuedMessages", "get", "set", "_sendChunkedMessages", "wait", "receiveData", "data", "fromBinary", "chunkAccumulator", "undefined", "delete", "destroy", "_sendTimeout", "clearTimeout", "channelChunks", "values", "forEach", "wake", "clear", "readyState", "warn", "timeout", "emptyChannels", "messages", "entries", "bufferedAmount", "nextMessage", "shift", "size", "setTimeout", "id", "WebSocket"]
|
|
7
|
+
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
+
CLOUDFLARE_MESSAGE_MAX_BYTES,
|
|
3
|
+
CLOUDFLARE_RPC_MAX_BYTES,
|
|
2
4
|
Protocol,
|
|
5
|
+
WebSocketMuxer,
|
|
3
6
|
getTypename,
|
|
4
7
|
protocol,
|
|
5
8
|
toUint8Array
|
|
6
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-TKYUZ5ZK.mjs";
|
|
7
10
|
|
|
8
11
|
// packages/core/mesh/edge-client/src/index.ts
|
|
9
12
|
export * from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
@@ -60,6 +63,7 @@ import { scheduleTask, scheduleTaskInterval } from "@dxos/async";
|
|
|
60
63
|
import { Context, Resource } from "@dxos/context";
|
|
61
64
|
import { invariant as invariant2 } from "@dxos/invariant";
|
|
62
65
|
import { log, logInfo } from "@dxos/log";
|
|
66
|
+
import { EdgeWebsocketProtocol } from "@dxos/protocols";
|
|
63
67
|
import { buf } from "@dxos/protocols/buf";
|
|
64
68
|
import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
65
69
|
function _ts_decorate(decorators, target, key, desc) {
|
|
@@ -71,7 +75,6 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
71
75
|
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-ws-connection.ts";
|
|
72
76
|
var SIGNAL_KEEPALIVE_INTERVAL = 4e3;
|
|
73
77
|
var SIGNAL_KEEPALIVE_TIMEOUT = 12e3;
|
|
74
|
-
var CLOUDFLARE_MESSAGE_LENGTH_LIMIT = 1024 * 1024;
|
|
75
78
|
var EdgeWsConnection = class extends Resource {
|
|
76
79
|
constructor(_identity, _connectionInfo, _callbacks) {
|
|
77
80
|
super();
|
|
@@ -89,47 +92,73 @@ var EdgeWsConnection = class extends Resource {
|
|
|
89
92
|
send(message) {
|
|
90
93
|
invariant2(this._ws, void 0, {
|
|
91
94
|
F: __dxlog_file2,
|
|
92
|
-
L:
|
|
95
|
+
L: 52,
|
|
93
96
|
S: this,
|
|
94
97
|
A: [
|
|
95
98
|
"this._ws",
|
|
96
99
|
""
|
|
97
100
|
]
|
|
98
101
|
});
|
|
102
|
+
invariant2(this._wsMuxer, void 0, {
|
|
103
|
+
F: __dxlog_file2,
|
|
104
|
+
L: 53,
|
|
105
|
+
S: this,
|
|
106
|
+
A: [
|
|
107
|
+
"this._wsMuxer",
|
|
108
|
+
""
|
|
109
|
+
]
|
|
110
|
+
});
|
|
99
111
|
log("sending...", {
|
|
100
112
|
peerKey: this._identity.peerKey,
|
|
101
113
|
payload: protocol.getPayloadType(message)
|
|
102
114
|
}, {
|
|
103
115
|
F: __dxlog_file2,
|
|
104
|
-
L:
|
|
116
|
+
L: 54,
|
|
105
117
|
S: this,
|
|
106
118
|
C: (f, a) => f(...a)
|
|
107
119
|
});
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
120
|
+
if (this._ws?.protocol.includes(EdgeWebsocketProtocol.V0)) {
|
|
121
|
+
const binary = buf.toBinary(MessageSchema, message);
|
|
122
|
+
if (binary.length > CLOUDFLARE_MESSAGE_MAX_BYTES) {
|
|
123
|
+
log.error("Message dropped because it was too large (>1MB).", {
|
|
124
|
+
byteLength: binary.byteLength,
|
|
125
|
+
serviceId: message.serviceId,
|
|
126
|
+
payload: protocol.getPayloadType(message)
|
|
127
|
+
}, {
|
|
128
|
+
F: __dxlog_file2,
|
|
129
|
+
L: 58,
|
|
130
|
+
S: this,
|
|
131
|
+
C: (f, a) => f(...a)
|
|
132
|
+
});
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this._ws.send(binary);
|
|
136
|
+
} else {
|
|
137
|
+
this._wsMuxer.send(message).catch((e) => log.catch(e, void 0, {
|
|
115
138
|
F: __dxlog_file2,
|
|
116
|
-
L:
|
|
139
|
+
L: 67,
|
|
117
140
|
S: this,
|
|
118
141
|
C: (f, a) => f(...a)
|
|
119
|
-
});
|
|
120
|
-
return;
|
|
142
|
+
}));
|
|
121
143
|
}
|
|
122
|
-
this._ws.send(encoded);
|
|
123
144
|
}
|
|
124
145
|
async _open() {
|
|
146
|
+
const baseProtocols = [
|
|
147
|
+
...Object.values(EdgeWebsocketProtocol)
|
|
148
|
+
];
|
|
125
149
|
this._ws = new WebSocket(this._connectionInfo.url.toString(), this._connectionInfo.protocolHeader ? [
|
|
150
|
+
...baseProtocols,
|
|
126
151
|
this._connectionInfo.protocolHeader
|
|
127
|
-
] : [
|
|
152
|
+
] : [
|
|
153
|
+
...baseProtocols
|
|
154
|
+
]);
|
|
155
|
+
const muxer = new WebSocketMuxer(this._ws);
|
|
156
|
+
this._wsMuxer = muxer;
|
|
128
157
|
this._ws.onopen = () => {
|
|
129
158
|
if (this.isOpen) {
|
|
130
159
|
log("connected", void 0, {
|
|
131
160
|
F: __dxlog_file2,
|
|
132
|
-
L:
|
|
161
|
+
L: 84,
|
|
133
162
|
S: this,
|
|
134
163
|
C: (f, a) => f(...a)
|
|
135
164
|
});
|
|
@@ -140,7 +169,7 @@ var EdgeWsConnection = class extends Resource {
|
|
|
140
169
|
currentIdentity: this._identity
|
|
141
170
|
}, {
|
|
142
171
|
F: __dxlog_file2,
|
|
143
|
-
L:
|
|
172
|
+
L: 88,
|
|
144
173
|
S: this,
|
|
145
174
|
C: (f, a) => f(...a)
|
|
146
175
|
});
|
|
@@ -153,11 +182,12 @@ var EdgeWsConnection = class extends Resource {
|
|
|
153
182
|
reason: event.reason
|
|
154
183
|
}, {
|
|
155
184
|
F: __dxlog_file2,
|
|
156
|
-
L:
|
|
185
|
+
L: 93,
|
|
157
186
|
S: this,
|
|
158
187
|
C: (f, a) => f(...a)
|
|
159
188
|
});
|
|
160
189
|
this._callbacks.onRestartRequired();
|
|
190
|
+
muxer.destroy();
|
|
161
191
|
}
|
|
162
192
|
};
|
|
163
193
|
this._ws.onerror = (event) => {
|
|
@@ -167,7 +197,7 @@ var EdgeWsConnection = class extends Resource {
|
|
|
167
197
|
info: event.message
|
|
168
198
|
}, {
|
|
169
199
|
F: __dxlog_file2,
|
|
170
|
-
L:
|
|
200
|
+
L: 100,
|
|
171
201
|
S: this,
|
|
172
202
|
C: (f, a) => f(...a)
|
|
173
203
|
});
|
|
@@ -177,7 +207,7 @@ var EdgeWsConnection = class extends Resource {
|
|
|
177
207
|
error: event.error
|
|
178
208
|
}, {
|
|
179
209
|
F: __dxlog_file2,
|
|
180
|
-
L:
|
|
210
|
+
L: 103,
|
|
181
211
|
S: this,
|
|
182
212
|
C: (f, a) => f(...a)
|
|
183
213
|
});
|
|
@@ -189,7 +219,7 @@ var EdgeWsConnection = class extends Resource {
|
|
|
189
219
|
event: event.type
|
|
190
220
|
}, {
|
|
191
221
|
F: __dxlog_file2,
|
|
192
|
-
L:
|
|
222
|
+
L: 111,
|
|
193
223
|
S: this,
|
|
194
224
|
C: (f, a) => f(...a)
|
|
195
225
|
});
|
|
@@ -199,15 +229,18 @@ var EdgeWsConnection = class extends Resource {
|
|
|
199
229
|
this._rescheduleHeartbeatTimeout();
|
|
200
230
|
return;
|
|
201
231
|
}
|
|
202
|
-
const
|
|
203
|
-
if (this.isOpen) {
|
|
204
|
-
|
|
232
|
+
const bytes = await toUint8Array(event.data);
|
|
233
|
+
if (!this.isOpen) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const message = this._ws?.protocol?.includes(EdgeWebsocketProtocol.V0) ? buf.fromBinary(MessageSchema, bytes) : muxer.receiveData(bytes);
|
|
237
|
+
if (message) {
|
|
205
238
|
log("received", {
|
|
206
239
|
from: message.source,
|
|
207
240
|
payload: protocol.getPayloadType(message)
|
|
208
241
|
}, {
|
|
209
242
|
F: __dxlog_file2,
|
|
210
|
-
L:
|
|
243
|
+
L: 128,
|
|
211
244
|
S: this,
|
|
212
245
|
C: (f, a) => f(...a)
|
|
213
246
|
});
|
|
@@ -221,6 +254,8 @@ var EdgeWsConnection = class extends Resource {
|
|
|
221
254
|
try {
|
|
222
255
|
this._ws?.close();
|
|
223
256
|
this._ws = void 0;
|
|
257
|
+
this._wsMuxer?.destroy();
|
|
258
|
+
this._wsMuxer = void 0;
|
|
224
259
|
} catch (err) {
|
|
225
260
|
if (err instanceof Error && err.message.includes("WebSocket is closed before the connection is established.")) {
|
|
226
261
|
return;
|
|
@@ -229,7 +264,7 @@ var EdgeWsConnection = class extends Resource {
|
|
|
229
264
|
err
|
|
230
265
|
}, {
|
|
231
266
|
F: __dxlog_file2,
|
|
232
|
-
L:
|
|
267
|
+
L: 146,
|
|
233
268
|
S: this,
|
|
234
269
|
C: (f, a) => f(...a)
|
|
235
270
|
});
|
|
@@ -238,7 +273,7 @@ var EdgeWsConnection = class extends Resource {
|
|
|
238
273
|
_scheduleHeartbeats() {
|
|
239
274
|
invariant2(this._ws, void 0, {
|
|
240
275
|
F: __dxlog_file2,
|
|
241
|
-
L:
|
|
276
|
+
L: 151,
|
|
242
277
|
S: this,
|
|
243
278
|
A: [
|
|
244
279
|
"this._ws",
|
|
@@ -258,13 +293,13 @@ var EdgeWsConnection = class extends Resource {
|
|
|
258
293
|
void this._inactivityTimeoutCtx?.dispose();
|
|
259
294
|
this._inactivityTimeoutCtx = new Context(void 0, {
|
|
260
295
|
F: __dxlog_file2,
|
|
261
|
-
L:
|
|
296
|
+
L: 170
|
|
262
297
|
});
|
|
263
298
|
scheduleTask(this._inactivityTimeoutCtx, () => {
|
|
264
299
|
if (this.isOpen) {
|
|
265
300
|
log.warn("restart due to inactivity timeout", void 0, {
|
|
266
301
|
F: __dxlog_file2,
|
|
267
|
-
L:
|
|
302
|
+
L: 175,
|
|
268
303
|
S: this,
|
|
269
304
|
C: (f, a) => f(...a)
|
|
270
305
|
});
|
|
@@ -354,7 +389,7 @@ var EdgeClient = class extends Resource2 {
|
|
|
354
389
|
});
|
|
355
390
|
this._identity = identity;
|
|
356
391
|
this._closeCurrentConnection(new EdgeIdentityChangedError());
|
|
357
|
-
this._persistentLifecycle.scheduleRestart();
|
|
392
|
+
void this._persistentLifecycle.scheduleRestart();
|
|
358
393
|
}
|
|
359
394
|
}
|
|
360
395
|
onMessage(listener) {
|
|
@@ -466,7 +501,7 @@ var EdgeClient = class extends Resource2 {
|
|
|
466
501
|
onRestartRequired: () => {
|
|
467
502
|
if (this._isActive(connection)) {
|
|
468
503
|
this._closeCurrentConnection();
|
|
469
|
-
this._persistentLifecycle.scheduleRestart();
|
|
504
|
+
void this._persistentLifecycle.scheduleRestart();
|
|
470
505
|
} else {
|
|
471
506
|
log2.verbose("restart requested by inactive connection", void 0, {
|
|
472
507
|
F: __dxlog_file3,
|
|
@@ -792,7 +827,6 @@ var EdgeHttpClient = class {
|
|
|
792
827
|
async uploadFunction(pathParts, body, args) {
|
|
793
828
|
const path = [
|
|
794
829
|
"functions",
|
|
795
|
-
pathParts.spaceId,
|
|
796
830
|
...pathParts.functionId ? [
|
|
797
831
|
pathParts.functionId
|
|
798
832
|
] : []
|
|
@@ -991,11 +1025,14 @@ var encodeAuthHeader = (challenge) => {
|
|
|
991
1025
|
return `VerifiablePresentation pb;base64,${encodedChallenge}`;
|
|
992
1026
|
};
|
|
993
1027
|
export {
|
|
1028
|
+
CLOUDFLARE_MESSAGE_MAX_BYTES,
|
|
1029
|
+
CLOUDFLARE_RPC_MAX_BYTES,
|
|
994
1030
|
EdgeClient,
|
|
995
1031
|
EdgeConnectionClosedError,
|
|
996
1032
|
EdgeHttpClient,
|
|
997
1033
|
EdgeIdentityChangedError,
|
|
998
1034
|
Protocol,
|
|
1035
|
+
WebSocketMuxer,
|
|
999
1036
|
createChainEdgeIdentity,
|
|
1000
1037
|
createDeviceEdgeIdentity,
|
|
1001
1038
|
createEphemeralEdgeIdentity,
|