@dxos/edge-client 0.8.4-main.ead640a → 0.8.4-main.f466a3d56e
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/{browser/chunk-IKP53CBQ.mjs → neutral/chunk-ZIQ5T3A7.mjs} +20 -83
- package/dist/lib/{browser/chunk-IKP53CBQ.mjs.map → neutral/chunk-ZIQ5T3A7.mjs.map} +2 -2
- package/dist/lib/{browser → neutral}/edge-ws-muxer.mjs +1 -1
- package/dist/lib/{browser → neutral}/index.mjs +318 -436
- 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/edge-client.d.ts +5 -2
- package/dist/types/src/edge-client.d.ts.map +1 -1
- package/dist/types/src/edge-http-client.d.ts +103 -30
- 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.map +1 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/http-client.d.ts +2 -2
- package/dist/types/src/http-client.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 +2 -2
- 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 +29 -32
- package/src/edge-client.test.ts +16 -11
- package/src/edge-client.ts +19 -3
- package/src/edge-http-client.test.ts +3 -2
- package/src/edge-http-client.ts +328 -75
- package/src/edge-ws-connection.ts +2 -1
- package/src/http-client.test.ts +3 -2
- package/src/http-client.ts +5 -1
- package/src/testing/test-utils.ts +4 -4
- 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-DR5YNW5K.mjs +0 -332
- package/dist/lib/node-esm/chunk-DR5YNW5K.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/lib/node-esm/index.mjs +0 -1357
- 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 → 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.
|
|
@@ -13,22 +13,13 @@ import { invariant } from "@dxos/invariant";
|
|
|
13
13
|
import { buf, bufWkt } from "@dxos/protocols/buf";
|
|
14
14
|
import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
15
15
|
import { bufferToArray } from "@dxos/util";
|
|
16
|
-
function _define_property(obj, key, value) {
|
|
17
|
-
if (key in obj) {
|
|
18
|
-
Object.defineProperty(obj, key, {
|
|
19
|
-
value,
|
|
20
|
-
enumerable: true,
|
|
21
|
-
configurable: true,
|
|
22
|
-
writable: true
|
|
23
|
-
});
|
|
24
|
-
} else {
|
|
25
|
-
obj[key] = value;
|
|
26
|
-
}
|
|
27
|
-
return obj;
|
|
28
|
-
}
|
|
29
16
|
var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/edge-client/src/protocol.ts";
|
|
30
17
|
var getTypename = (typeName) => `type.googleapis.com/${typeName}`;
|
|
31
18
|
var Protocol = class {
|
|
19
|
+
_typeRegistry;
|
|
20
|
+
constructor(types) {
|
|
21
|
+
this._typeRegistry = buf.createRegistry(...types);
|
|
22
|
+
}
|
|
32
23
|
get typeRegistry() {
|
|
33
24
|
return this._typeRegistry;
|
|
34
25
|
}
|
|
@@ -47,38 +38,14 @@ var Protocol = class {
|
|
|
47
38
|
* Return the payload with the given type.
|
|
48
39
|
*/
|
|
49
40
|
getPayload(message, type) {
|
|
50
|
-
invariant(message.payload, void 0, {
|
|
51
|
-
F: __dxlog_file,
|
|
52
|
-
L: 40,
|
|
53
|
-
S: this,
|
|
54
|
-
A: [
|
|
55
|
-
"message.payload",
|
|
56
|
-
""
|
|
57
|
-
]
|
|
58
|
-
});
|
|
41
|
+
invariant(message.payload, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 33, S: this, A: ["message.payload", ""] });
|
|
59
42
|
const payloadTypename = this.getPayloadType(message);
|
|
60
43
|
if (type && type.typeName !== payloadTypename) {
|
|
61
44
|
throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);
|
|
62
45
|
}
|
|
63
|
-
invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, {
|
|
64
|
-
F: __dxlog_file,
|
|
65
|
-
L: 46,
|
|
66
|
-
S: this,
|
|
67
|
-
A: [
|
|
68
|
-
"bufWkt.anyIs(message.payload, type)",
|
|
69
|
-
"`Unexpected payload type: ${payloadTypename}}`"
|
|
70
|
-
]
|
|
71
|
-
});
|
|
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}}`"] });
|
|
72
47
|
const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry);
|
|
73
|
-
invariant(payload, `Empty payload: ${payloadTypename}}`, {
|
|
74
|
-
F: __dxlog_file,
|
|
75
|
-
L: 48,
|
|
76
|
-
S: this,
|
|
77
|
-
A: [
|
|
78
|
-
"payload",
|
|
79
|
-
"`Empty payload: ${payloadTypename}}`"
|
|
80
|
-
]
|
|
81
|
-
});
|
|
48
|
+
invariant(payload, `Empty payload: ${payloadTypename}}`, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 40, S: this, A: ["payload", "`Empty payload: ${payloadTypename}}`"] });
|
|
82
49
|
return payload;
|
|
83
50
|
}
|
|
84
51
|
/**
|
|
@@ -103,10 +70,6 @@ var Protocol = class {
|
|
|
103
70
|
payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : void 0
|
|
104
71
|
});
|
|
105
72
|
}
|
|
106
|
-
constructor(types) {
|
|
107
|
-
_define_property(this, "_typeRegistry", void 0);
|
|
108
|
-
this._typeRegistry = buf.createRegistry(...types);
|
|
109
|
-
}
|
|
110
73
|
};
|
|
111
74
|
var toUint8Array = async (data) => {
|
|
112
75
|
if (data instanceof Buffer) {
|
|
@@ -127,19 +90,6 @@ var protocol = new Protocol([
|
|
|
127
90
|
]);
|
|
128
91
|
|
|
129
92
|
// src/edge-ws-muxer.ts
|
|
130
|
-
function _define_property2(obj, key, value) {
|
|
131
|
-
if (key in obj) {
|
|
132
|
-
Object.defineProperty(obj, key, {
|
|
133
|
-
value,
|
|
134
|
-
enumerable: true,
|
|
135
|
-
configurable: true,
|
|
136
|
-
writable: true
|
|
137
|
-
});
|
|
138
|
-
} else {
|
|
139
|
-
obj[key] = value;
|
|
140
|
-
}
|
|
141
|
-
return obj;
|
|
142
|
-
}
|
|
143
93
|
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/edge-client/src/edge-ws-muxer.ts";
|
|
144
94
|
var FLAG_SEGMENT_SEQ = 1;
|
|
145
95
|
var FLAG_SEGMENT_SEQ_TERMINATED = 1 << 1;
|
|
@@ -149,6 +99,16 @@ var MAX_CHUNK_LENGTH = 16384;
|
|
|
149
99
|
var MAX_BUFFERED_AMOUNT = CLOUDFLARE_MESSAGE_MAX_BYTES;
|
|
150
100
|
var BUFFER_FULL_BACKOFF_TIMEOUT = 100;
|
|
151
101
|
var WebSocketMuxer = class {
|
|
102
|
+
_ws;
|
|
103
|
+
_inMessageAccumulator = /* @__PURE__ */ new Map();
|
|
104
|
+
_outMessageChunks = /* @__PURE__ */ new Map();
|
|
105
|
+
_outMessageChannelByService = /* @__PURE__ */ new Map();
|
|
106
|
+
_sendTimeout;
|
|
107
|
+
_maxChunkLength;
|
|
108
|
+
constructor(_ws, config) {
|
|
109
|
+
this._ws = _ws;
|
|
110
|
+
this._maxChunkLength = config?.maxChunkLength ?? MAX_CHUNK_LENGTH;
|
|
111
|
+
}
|
|
152
112
|
/**
|
|
153
113
|
* Resolves when all the message chunks get enqueued for sending.
|
|
154
114
|
*/
|
|
@@ -161,12 +121,7 @@ var WebSocketMuxer = class {
|
|
|
161
121
|
serviceId: message.serviceId,
|
|
162
122
|
payload: protocol.getPayloadType(message),
|
|
163
123
|
channelId
|
|
164
|
-
}, {
|
|
165
|
-
F: __dxlog_file2,
|
|
166
|
-
L: 62,
|
|
167
|
-
S: this,
|
|
168
|
-
C: (f, a) => f(...a)
|
|
169
|
-
});
|
|
124
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 44, S: this });
|
|
170
125
|
return;
|
|
171
126
|
}
|
|
172
127
|
if (channelId == null || binary.length < this._maxChunkLength) {
|
|
@@ -257,12 +212,7 @@ var WebSocketMuxer = class {
|
|
|
257
212
|
}
|
|
258
213
|
const send = () => {
|
|
259
214
|
if (this._ws.readyState === 2 || this._ws.readyState === 3) {
|
|
260
|
-
log.warn("send called for closed websocket", void 0, {
|
|
261
|
-
F: __dxlog_file2,
|
|
262
|
-
L: 146,
|
|
263
|
-
S: this,
|
|
264
|
-
C: (f, a) => f(...a)
|
|
265
|
-
});
|
|
215
|
+
log.warn("send called for closed websocket", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 140, S: this });
|
|
266
216
|
this._sendTimeout = void 0;
|
|
267
217
|
return;
|
|
268
218
|
}
|
|
@@ -303,19 +253,6 @@ var WebSocketMuxer = class {
|
|
|
303
253
|
}
|
|
304
254
|
return id;
|
|
305
255
|
}
|
|
306
|
-
constructor(_ws, config) {
|
|
307
|
-
_define_property2(this, "_ws", void 0);
|
|
308
|
-
_define_property2(this, "_inMessageAccumulator", void 0);
|
|
309
|
-
_define_property2(this, "_outMessageChunks", void 0);
|
|
310
|
-
_define_property2(this, "_outMessageChannelByService", void 0);
|
|
311
|
-
_define_property2(this, "_sendTimeout", void 0);
|
|
312
|
-
_define_property2(this, "_maxChunkLength", void 0);
|
|
313
|
-
this._ws = _ws;
|
|
314
|
-
this._inMessageAccumulator = /* @__PURE__ */ new Map();
|
|
315
|
-
this._outMessageChunks = /* @__PURE__ */ new Map();
|
|
316
|
-
this._outMessageChannelByService = /* @__PURE__ */ new Map();
|
|
317
|
-
this._maxChunkLength = config?.maxChunkLength ?? MAX_CHUNK_LENGTH;
|
|
318
|
-
}
|
|
319
256
|
};
|
|
320
257
|
|
|
321
258
|
export {
|
|
@@ -327,4 +264,4 @@ export {
|
|
|
327
264
|
CLOUDFLARE_RPC_MAX_BYTES,
|
|
328
265
|
WebSocketMuxer
|
|
329
266
|
};
|
|
330
|
-
//# sourceMappingURL=chunk-
|
|
267
|
+
//# sourceMappingURL=chunk-ZIQ5T3A7.mjs.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/edge-ws-muxer.ts", "../../../src/defs.ts", "../../../src/protocol.ts"],
|
|
4
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 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(): 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 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
|
|
6
|
-
"names": ["Trigger", "log", "buf", "MessageSchema", "bufWkt", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "invariant", "buf", "bufWkt", "MessageSchema", "bufferToArray", "getTypename", "
|
|
5
|
+
"mappings": ";AAIA,SAASA,eAAe;AACxB,SAASC,WAAW;AACpB,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AAKpC,eAAemC,WAAAA,MAAAA,OAAAA;MACtB,OAAA;AACF,aAAA,eAAA;MACI;IACN;AAEQM,SAAAA,eAAgC,WAAsB,IAAA;;kBAEnDN,SAAAA;AACT,QAAA,CAAA,QAAA,WAAA;AACIO,aAAK;IACT;QACEA,KAAK,KAAK3C,4BAA4B4C,IAAI,QAAG,SAAA;QAC7C,CAAA,IAAK5C;AACP,WAAA,KAAA,4BAAA,OAAA;AACA,WAAO2C,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", "length", "i", "isLastChunk", "Buffer", "FLAG_SEGMENT_SEQ", "messageChunks", "flags", "chunk", "trigger", "queuedMessages", "set", "_sendChunkedMessages", "receiveData", "buf", "MessageSchema", "data", "chunkAccumulator", "push", "FLAG_SEGMENT_SEQ_TERMINATED", "message", "destroy", "clearTimeout", "channelChunks", "clear", "warn", "undefined", "timeout", "bufferedAmount", "nextMessage", "emptyChannels", "send", "_resolveChannel", "id", "size"]
|
|
7
7
|
}
|