@dxos/shell 0.6.3-next.2f65b78 → 0.6.3-staging.0f23fb2
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/bundle/browser-C2u-GX_T.mjs +7 -0
- package/dist/bundle/browser-C2u-GX_T.mjs.map +1 -0
- package/dist/bundle/index-BoJU28fu.mjs +1322 -0
- package/dist/bundle/index-BoJU28fu.mjs.map +1 -0
- package/dist/bundle/index-Ckc10jMw.mjs +92927 -0
- package/dist/bundle/index-Ckc10jMw.mjs.map +1 -0
- package/dist/bundle/index-DXRmq0_o.mjs +4593 -0
- package/dist/bundle/index-DXRmq0_o.mjs.map +1 -0
- package/dist/bundle/index-DvzmUghe.mjs +112 -0
- package/dist/bundle/index-DvzmUghe.mjs.map +1 -0
- package/dist/bundle/shell.mjs +10 -88396
- package/dist/bundle/shell.mjs.map +1 -1
- package/dist/bundle/shell.umd.js +133 -101
- package/dist/bundle/shell.umd.js.map +1 -1
- package/dist/bundle/style.css +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/react.mjs +12 -8
- package/dist/lib/browser/react.mjs.map +3 -3
- package/dist/types/src/composites/Shell/Shell.d.ts +1 -1
- package/dist/types/src/composites/Shell/Shell.d.ts.map +1 -1
- package/dist/types/src/composites/Shell/Shell.js +28 -27
- package/dist/types/src/composites/Shell/Shell.js.map +1 -1
- package/dist/types/src/composites/Shell/ShellContext.d.ts +2 -1
- package/dist/types/src/composites/Shell/ShellContext.d.ts.map +1 -1
- package/dist/types/src/composites/Shell/ShellContext.js +13 -12
- package/dist/types/src/composites/Shell/ShellContext.js.map +1 -1
- package/dist/types/src/composites/Shell/memory-shell-runtime.d.ts +2 -1
- package/dist/types/src/composites/Shell/memory-shell-runtime.d.ts.map +1 -1
- package/dist/types/src/composites/Shell/memory-shell-runtime.js +2 -2
- package/dist/types/src/composites/Shell/memory-shell-runtime.js.map +1 -1
- package/dist/types/src/composites/Shell/run-shell.d.ts.map +1 -1
- package/dist/types/src/composites/Shell/run-shell.js +4 -3
- package/dist/types/src/composites/Shell/run-shell.js.map +1 -1
- package/dist/types/src/panels/IdentityPanel/useAgentHandlers.d.ts.map +1 -1
- package/dist/types/src/panels/IdentityPanel/useAgentHandlers.js +2 -1
- package/dist/types/src/panels/IdentityPanel/useAgentHandlers.js.map +1 -1
- package/package.json +24 -24
- package/src/composites/Shell/Shell.tsx +3 -3
- package/src/composites/Shell/ShellContext.tsx +2 -1
- package/src/composites/Shell/memory-shell-runtime.ts +2 -2
- package/src/composites/Shell/run-shell.tsx +2 -9
- package/src/panels/IdentityPanel/useAgentHandlers.ts +2 -1
|
@@ -0,0 +1,1322 @@
|
|
|
1
|
+
import { l as n, i as _, A as Y, f as b, E as p, P as S, B as y, F as T, w as $, C as D, R as Q, T as N, D as P, v as R, k as I, m as V, G as C, q as E, H as x, I as Z, p as z, h as j, c as q, e as ee } from "./index-Ckc10jMw.mjs";
|
|
2
|
+
import { schema as O, trace as w, RateLimitExceededError as se, TimeoutError as J } from "@dxos/protocols";
|
|
3
|
+
import { W as M } from "./browser-C2u-GX_T.mjs";
|
|
4
|
+
var l;
|
|
5
|
+
(function(e) {
|
|
6
|
+
e[e.CONNECTING = 0] = "CONNECTING", e[e.RECONNECTING = 1] = "RECONNECTING", e[e.CONNECTED = 2] = "CONNECTED", e[e.DISCONNECTED = 3] = "DISCONNECTED", e[e.ERROR = 4] = "ERROR", e[e.CLOSED = 5] = "CLOSED";
|
|
7
|
+
})(l || (l = {}));
|
|
8
|
+
var te = class {
|
|
9
|
+
recordMessageAckFailed() {
|
|
10
|
+
C.metrics.increment("dxos.mesh.signal.messenger.failed-ack", 1);
|
|
11
|
+
}
|
|
12
|
+
recordReliableMessage(e) {
|
|
13
|
+
C.metrics.increment("dxos.mesh.signal.messenger.reliable-send", 1, {
|
|
14
|
+
tags: {
|
|
15
|
+
success: e.sent,
|
|
16
|
+
attempts: e.sendAttempts
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}, G = 1e4, g = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/messenger.ts", U = O.getCodecForType("dxos.mesh.messaging.ReliablePayload"), H = O.getCodecForType("dxos.mesh.messaging.Acknowledgement"), ie = 12e4, ve = class {
|
|
21
|
+
constructor({ signalManager: e, retryDelay: s = 300 }) {
|
|
22
|
+
this._monitor = new te(), this._listeners = new E(({ peerId: i, payloadType: t }) => i.toHex() + t), this._defaultListeners = new E(S.hash), this._onAckCallbacks = new E(S.hash), this._receivedMessages = new x(S.hash), this._toClear = new x(S.hash), this._closed = !0, this._signalManager = e, this._retryDelay = s, this.open();
|
|
23
|
+
}
|
|
24
|
+
open() {
|
|
25
|
+
if (!this._closed)
|
|
26
|
+
return;
|
|
27
|
+
const e = S.random().toHex();
|
|
28
|
+
n.trace("dxos.mesh.messenger.open", w.begin({
|
|
29
|
+
id: e
|
|
30
|
+
}), {
|
|
31
|
+
F: g,
|
|
32
|
+
L: 71,
|
|
33
|
+
S: this,
|
|
34
|
+
C: (s, i) => s(...i)
|
|
35
|
+
}), this._ctx = new D({
|
|
36
|
+
onError: (s) => n.catch(s, void 0, {
|
|
37
|
+
F: g,
|
|
38
|
+
L: 73,
|
|
39
|
+
S: this,
|
|
40
|
+
C: (i, t) => i(...t)
|
|
41
|
+
})
|
|
42
|
+
}, {
|
|
43
|
+
F: g,
|
|
44
|
+
L: 72
|
|
45
|
+
}), this._ctx.onDispose(this._signalManager.onMessage.on(async (s) => {
|
|
46
|
+
n("received message", {
|
|
47
|
+
from: s.author
|
|
48
|
+
}, {
|
|
49
|
+
F: g,
|
|
50
|
+
L: 77,
|
|
51
|
+
S: this,
|
|
52
|
+
C: (i, t) => i(...t)
|
|
53
|
+
}), await this._handleMessage(s);
|
|
54
|
+
})), I(this._ctx, async () => {
|
|
55
|
+
this._performGc();
|
|
56
|
+
}, ie), this._closed = !1, n.trace("dxos.mesh.messenger.open", w.end({
|
|
57
|
+
id: e
|
|
58
|
+
}), {
|
|
59
|
+
F: g,
|
|
60
|
+
L: 92,
|
|
61
|
+
S: this,
|
|
62
|
+
C: (s, i) => s(...i)
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async close() {
|
|
66
|
+
this._closed || (this._closed = !0, await this._ctx.dispose());
|
|
67
|
+
}
|
|
68
|
+
async sendMessage({ author: e, recipient: s, payload: i }) {
|
|
69
|
+
_(!this._closed, "Closed", {
|
|
70
|
+
F: g,
|
|
71
|
+
L: 104,
|
|
72
|
+
S: this,
|
|
73
|
+
A: [
|
|
74
|
+
"!this._closed",
|
|
75
|
+
"'Closed'"
|
|
76
|
+
]
|
|
77
|
+
});
|
|
78
|
+
const t = this._ctx.derive(), a = {
|
|
79
|
+
messageId: S.random(),
|
|
80
|
+
payload: i
|
|
81
|
+
};
|
|
82
|
+
_(!this._onAckCallbacks.has(a.messageId), void 0, {
|
|
83
|
+
F: g,
|
|
84
|
+
L: 111,
|
|
85
|
+
S: this,
|
|
86
|
+
A: [
|
|
87
|
+
"!this._onAckCallbacks.has(reliablePayload.messageId!)",
|
|
88
|
+
""
|
|
89
|
+
]
|
|
90
|
+
}), n("send message", {
|
|
91
|
+
messageId: a.messageId,
|
|
92
|
+
author: e,
|
|
93
|
+
recipient: s
|
|
94
|
+
}, {
|
|
95
|
+
F: g,
|
|
96
|
+
L: 112,
|
|
97
|
+
S: this,
|
|
98
|
+
C: (f, v) => f(...v)
|
|
99
|
+
});
|
|
100
|
+
let r, o, c = 0;
|
|
101
|
+
const m = new Promise((f, v) => {
|
|
102
|
+
r = f, o = v;
|
|
103
|
+
});
|
|
104
|
+
return Z(t, async () => {
|
|
105
|
+
n("retrying message", {
|
|
106
|
+
messageId: a.messageId
|
|
107
|
+
}, {
|
|
108
|
+
F: g,
|
|
109
|
+
L: 127,
|
|
110
|
+
S: this,
|
|
111
|
+
C: (f, v) => f(...v)
|
|
112
|
+
}), c++, await this._encodeAndSend({
|
|
113
|
+
author: e,
|
|
114
|
+
recipient: s,
|
|
115
|
+
reliablePayload: a
|
|
116
|
+
}).catch((f) => n("failed to send message", {
|
|
117
|
+
err: f
|
|
118
|
+
}, {
|
|
119
|
+
F: g,
|
|
120
|
+
L: 130,
|
|
121
|
+
S: this,
|
|
122
|
+
C: (v, X) => v(...X)
|
|
123
|
+
}));
|
|
124
|
+
}, this._retryDelay), V(t, () => {
|
|
125
|
+
n("message not delivered", {
|
|
126
|
+
messageId: a.messageId
|
|
127
|
+
}, {
|
|
128
|
+
F: g,
|
|
129
|
+
L: 139,
|
|
130
|
+
S: this,
|
|
131
|
+
C: (f, v) => f(...v)
|
|
132
|
+
}), this._onAckCallbacks.delete(a.messageId), o(new J("signaling message not delivered", new z(G, "Message not delivered"))), t.dispose(), this._monitor.recordReliableMessage({
|
|
133
|
+
sendAttempts: c,
|
|
134
|
+
sent: !1
|
|
135
|
+
});
|
|
136
|
+
}, G), this._onAckCallbacks.set(a.messageId, () => {
|
|
137
|
+
r(), this._onAckCallbacks.delete(a.messageId), t.dispose(), this._monitor.recordReliableMessage({
|
|
138
|
+
sendAttempts: c,
|
|
139
|
+
sent: !0
|
|
140
|
+
});
|
|
141
|
+
}), await this._encodeAndSend({
|
|
142
|
+
author: e,
|
|
143
|
+
recipient: s,
|
|
144
|
+
reliablePayload: a
|
|
145
|
+
}), m;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Subscribes onMessage function to messages that contains payload with payloadType.
|
|
149
|
+
* @param payloadType if not specified, onMessage will be subscribed to all types of messages.
|
|
150
|
+
*/
|
|
151
|
+
async listen({ peerId: e, payloadType: s, onMessage: i }) {
|
|
152
|
+
_(!this._closed, "Closed", {
|
|
153
|
+
F: g,
|
|
154
|
+
L: 177,
|
|
155
|
+
S: this,
|
|
156
|
+
A: [
|
|
157
|
+
"!this._closed",
|
|
158
|
+
"'Closed'"
|
|
159
|
+
]
|
|
160
|
+
}), await this._signalManager.subscribeMessages(e);
|
|
161
|
+
let t;
|
|
162
|
+
return s ? (t = this._listeners.get({
|
|
163
|
+
peerId: e,
|
|
164
|
+
payloadType: s
|
|
165
|
+
}), t || (t = /* @__PURE__ */ new Set(), this._listeners.set({
|
|
166
|
+
peerId: e,
|
|
167
|
+
payloadType: s
|
|
168
|
+
}, t))) : (t = this._defaultListeners.get(e), t || (t = /* @__PURE__ */ new Set(), this._defaultListeners.set(e, t))), t.add(i), {
|
|
169
|
+
unsubscribe: async () => {
|
|
170
|
+
t.delete(i);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
async _encodeAndSend({ author: e, recipient: s, reliablePayload: i }) {
|
|
175
|
+
await this._signalManager.sendMessage({
|
|
176
|
+
author: e,
|
|
177
|
+
recipient: s,
|
|
178
|
+
payload: {
|
|
179
|
+
type_url: "dxos.mesh.messaging.ReliablePayload",
|
|
180
|
+
value: U.encode(i, {
|
|
181
|
+
preserveAny: !0
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
async _handleMessage(e) {
|
|
187
|
+
switch (e.payload.type_url) {
|
|
188
|
+
case "dxos.mesh.messaging.ReliablePayload": {
|
|
189
|
+
await this._handleReliablePayload(e);
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
case "dxos.mesh.messaging.Acknowledgement": {
|
|
193
|
+
await this._handleAcknowledgement({
|
|
194
|
+
payload: e.payload
|
|
195
|
+
});
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async _handleReliablePayload({ author: e, recipient: s, payload: i }) {
|
|
201
|
+
_(i.type_url === "dxos.mesh.messaging.ReliablePayload", void 0, {
|
|
202
|
+
F: g,
|
|
203
|
+
L: 238,
|
|
204
|
+
S: this,
|
|
205
|
+
A: [
|
|
206
|
+
"payload.type_url === 'dxos.mesh.messaging.ReliablePayload'",
|
|
207
|
+
""
|
|
208
|
+
]
|
|
209
|
+
});
|
|
210
|
+
const t = U.decode(i.value, {
|
|
211
|
+
preserveAny: !0
|
|
212
|
+
});
|
|
213
|
+
n("handling message", {
|
|
214
|
+
messageId: t.messageId
|
|
215
|
+
}, {
|
|
216
|
+
F: g,
|
|
217
|
+
L: 241,
|
|
218
|
+
S: this,
|
|
219
|
+
C: (a, r) => a(...r)
|
|
220
|
+
});
|
|
221
|
+
try {
|
|
222
|
+
await this._sendAcknowledgement({
|
|
223
|
+
author: e,
|
|
224
|
+
recipient: s,
|
|
225
|
+
messageId: t.messageId
|
|
226
|
+
});
|
|
227
|
+
} catch (a) {
|
|
228
|
+
throw this._monitor.recordMessageAckFailed(), a;
|
|
229
|
+
}
|
|
230
|
+
this._receivedMessages.has(t.messageId) || (this._receivedMessages.add(t.messageId), await this._callListeners({
|
|
231
|
+
author: e,
|
|
232
|
+
recipient: s,
|
|
233
|
+
payload: t.payload
|
|
234
|
+
}));
|
|
235
|
+
}
|
|
236
|
+
async _handleAcknowledgement({ payload: e }) {
|
|
237
|
+
var s;
|
|
238
|
+
_(e.type_url === "dxos.mesh.messaging.Acknowledgement", void 0, {
|
|
239
|
+
F: g,
|
|
240
|
+
L: 269,
|
|
241
|
+
S: this,
|
|
242
|
+
A: [
|
|
243
|
+
"payload.type_url === 'dxos.mesh.messaging.Acknowledgement'",
|
|
244
|
+
""
|
|
245
|
+
]
|
|
246
|
+
}), (s = this._onAckCallbacks.get(H.decode(e.value).messageId)) == null || s();
|
|
247
|
+
}
|
|
248
|
+
async _sendAcknowledgement({ author: e, recipient: s, messageId: i }) {
|
|
249
|
+
n("sending ACK", {
|
|
250
|
+
messageId: i,
|
|
251
|
+
from: s,
|
|
252
|
+
to: e
|
|
253
|
+
}, {
|
|
254
|
+
F: g,
|
|
255
|
+
L: 282,
|
|
256
|
+
S: this,
|
|
257
|
+
C: (t, a) => t(...a)
|
|
258
|
+
}), await this._signalManager.sendMessage({
|
|
259
|
+
author: s,
|
|
260
|
+
recipient: e,
|
|
261
|
+
payload: {
|
|
262
|
+
type_url: "dxos.mesh.messaging.Acknowledgement",
|
|
263
|
+
value: H.encode({
|
|
264
|
+
messageId: i
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
async _callListeners(e) {
|
|
270
|
+
{
|
|
271
|
+
const s = this._defaultListeners.get(e.recipient);
|
|
272
|
+
if (s)
|
|
273
|
+
for (const i of s)
|
|
274
|
+
await i(e);
|
|
275
|
+
}
|
|
276
|
+
{
|
|
277
|
+
const s = this._listeners.get({
|
|
278
|
+
peerId: e.recipient,
|
|
279
|
+
payloadType: e.payload.type_url
|
|
280
|
+
});
|
|
281
|
+
if (s)
|
|
282
|
+
for (const i of s)
|
|
283
|
+
await i(e);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
_performGc() {
|
|
287
|
+
const e = performance.now();
|
|
288
|
+
for (const i of this._toClear.keys())
|
|
289
|
+
this._receivedMessages.delete(i);
|
|
290
|
+
this._toClear.clear();
|
|
291
|
+
for (const i of this._receivedMessages.keys())
|
|
292
|
+
this._toClear.add(i);
|
|
293
|
+
const s = performance.now() - e;
|
|
294
|
+
s > 100 && n.warn("GC took too long", {
|
|
295
|
+
elapsed: s
|
|
296
|
+
}, {
|
|
297
|
+
F: g,
|
|
298
|
+
L: 330,
|
|
299
|
+
S: this,
|
|
300
|
+
C: (i, t) => i(...t)
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}, ae = class {
|
|
304
|
+
constructor() {
|
|
305
|
+
this._performance = {
|
|
306
|
+
sentMessages: 0,
|
|
307
|
+
receivedMessages: 0,
|
|
308
|
+
reconnectCounter: 0,
|
|
309
|
+
joinCounter: 0,
|
|
310
|
+
leaveCounter: 0
|
|
311
|
+
}, this._connectionStarted = /* @__PURE__ */ new Date(), this._lastStateChange = /* @__PURE__ */ new Date();
|
|
312
|
+
}
|
|
313
|
+
getRecordedTimestamps() {
|
|
314
|
+
return {
|
|
315
|
+
connectionStarted: this._connectionStarted,
|
|
316
|
+
lastStateChange: this._lastStateChange
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
recordStateChangeTime() {
|
|
320
|
+
this._lastStateChange = /* @__PURE__ */ new Date();
|
|
321
|
+
}
|
|
322
|
+
recordConnectionStartTime() {
|
|
323
|
+
this._connectionStarted = /* @__PURE__ */ new Date();
|
|
324
|
+
}
|
|
325
|
+
recordReconnect(e) {
|
|
326
|
+
this._performance.reconnectCounter++, C.metrics.increment("dxos.mesh.signal.signal-client.reconnect", 1, {
|
|
327
|
+
tags: {
|
|
328
|
+
success: e.success
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
recordJoin() {
|
|
333
|
+
this._performance.joinCounter++;
|
|
334
|
+
}
|
|
335
|
+
recordLeave() {
|
|
336
|
+
this._performance.leaveCounter++;
|
|
337
|
+
}
|
|
338
|
+
recordMessageReceived(e) {
|
|
339
|
+
this._performance.receivedMessages++, C.metrics.increment("dxos.mesh.signal.signal-client.received-total", 1, {
|
|
340
|
+
tags: A(e)
|
|
341
|
+
}), C.metrics.distribution("dxos.mesh.signal.signal-client.bytes-in", B(e), {
|
|
342
|
+
tags: A(e)
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
async recordMessageSending(e, s) {
|
|
346
|
+
this._performance.sentMessages++;
|
|
347
|
+
const i = A(e);
|
|
348
|
+
let t = !0;
|
|
349
|
+
try {
|
|
350
|
+
const a = Date.now();
|
|
351
|
+
await s();
|
|
352
|
+
const r = Date.now() - a;
|
|
353
|
+
C.metrics.distribution("dxos.mesh.signal.signal-client.send-duration", r, {
|
|
354
|
+
tags: i
|
|
355
|
+
}), C.metrics.distribution("dxos.mesh.signal.signal-client.bytes-out", B(e), {
|
|
356
|
+
tags: i
|
|
357
|
+
});
|
|
358
|
+
} catch {
|
|
359
|
+
t = !1;
|
|
360
|
+
}
|
|
361
|
+
C.metrics.increment("dxos.mesh.signal.signal-client.sent-total", 1, {
|
|
362
|
+
tags: {
|
|
363
|
+
...i,
|
|
364
|
+
success: t
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
recordStreamCloseErrors(e) {
|
|
369
|
+
C.metrics.increment("dxos.mesh.signal.signal-client.stream-close-errors", e);
|
|
370
|
+
}
|
|
371
|
+
recordReconciliation(e) {
|
|
372
|
+
C.metrics.increment("dxos.mesh.signal.signal-client.reconciliation", 1, {
|
|
373
|
+
tags: {
|
|
374
|
+
success: e.success
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
}, B = (e) => e.author.asUint8Array().length + e.recipient.asUint8Array().length + e.payload.type_url.length + e.payload.value.length, A = (e) => ({
|
|
379
|
+
peer: e.author.toHex()
|
|
380
|
+
}), W = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-client/signal-local-state.ts", ne = class {
|
|
381
|
+
constructor(e, s) {
|
|
382
|
+
this._onMessage = e, this._onSwarmEvent = s, this._swarmStreams = new E(({ topic: i, peerId: t }) => i.toHex() + t.toHex()), this._joinedTopics = new x(({ topic: i, peerId: t }) => i.toHex() + t.toHex()), this._subscribedMessages = new x(({ peerId: i }) => i.toHex()), this.messageStreams = new E((i) => i.toHex()), this.reconciled = new p();
|
|
383
|
+
}
|
|
384
|
+
async safeCloseStreams() {
|
|
385
|
+
const e = [
|
|
386
|
+
...this._swarmStreams.values()
|
|
387
|
+
].concat([
|
|
388
|
+
...this.messageStreams.values()
|
|
389
|
+
]);
|
|
390
|
+
return this._swarmStreams.clear(), this.messageStreams.clear(), {
|
|
391
|
+
failureCount: (await T(e, (i) => i.close())).length
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
join({ topic: e, peerId: s }) {
|
|
395
|
+
this._joinedTopics.add({
|
|
396
|
+
topic: e,
|
|
397
|
+
peerId: s
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
leave({ topic: e, peerId: s }) {
|
|
401
|
+
var i;
|
|
402
|
+
(i = this._swarmStreams.get({
|
|
403
|
+
topic: e,
|
|
404
|
+
peerId: s
|
|
405
|
+
})) == null || i.close(), this._swarmStreams.delete({
|
|
406
|
+
topic: e,
|
|
407
|
+
peerId: s
|
|
408
|
+
}), this._joinedTopics.delete({
|
|
409
|
+
topic: e,
|
|
410
|
+
peerId: s
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
subscribeMessages(e) {
|
|
414
|
+
this._subscribedMessages.add({
|
|
415
|
+
peerId: e
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
unsubscribeMessages(e) {
|
|
419
|
+
var s;
|
|
420
|
+
n("unsubscribing from messages", {
|
|
421
|
+
peerId: e
|
|
422
|
+
}, {
|
|
423
|
+
F: W,
|
|
424
|
+
L: 76,
|
|
425
|
+
S: this,
|
|
426
|
+
C: (i, t) => i(...t)
|
|
427
|
+
}), this._subscribedMessages.delete({
|
|
428
|
+
peerId: e
|
|
429
|
+
}), (s = this.messageStreams.get(e)) == null || s.close(), this.messageStreams.delete(e);
|
|
430
|
+
}
|
|
431
|
+
async reconcile(e, s) {
|
|
432
|
+
await this._reconcileSwarmSubscriptions(e, s), await this._reconcileMessageSubscriptions(e, s), this.reconciled.emit();
|
|
433
|
+
}
|
|
434
|
+
async _reconcileSwarmSubscriptions(e, s) {
|
|
435
|
+
var i;
|
|
436
|
+
for (const { topic: t, peerId: a } of this._swarmStreams.keys())
|
|
437
|
+
this._joinedTopics.has({
|
|
438
|
+
topic: t,
|
|
439
|
+
peerId: a
|
|
440
|
+
}) || ((i = this._swarmStreams.get({
|
|
441
|
+
topic: t,
|
|
442
|
+
peerId: a
|
|
443
|
+
})) == null || i.close(), this._swarmStreams.delete({
|
|
444
|
+
topic: t,
|
|
445
|
+
peerId: a
|
|
446
|
+
}));
|
|
447
|
+
for (const { topic: t, peerId: a } of this._joinedTopics.values()) {
|
|
448
|
+
if (this._swarmStreams.has({
|
|
449
|
+
topic: t,
|
|
450
|
+
peerId: a
|
|
451
|
+
}))
|
|
452
|
+
continue;
|
|
453
|
+
const r = await j(R(e, s.join({
|
|
454
|
+
topic: t,
|
|
455
|
+
peerId: a
|
|
456
|
+
})), 5e3);
|
|
457
|
+
r.subscribe(async (o) => {
|
|
458
|
+
this._joinedTopics.has({
|
|
459
|
+
topic: t,
|
|
460
|
+
peerId: a
|
|
461
|
+
}) && (n("swarm event", {
|
|
462
|
+
swarmEvent: o
|
|
463
|
+
}, {
|
|
464
|
+
F: W,
|
|
465
|
+
L: 112,
|
|
466
|
+
S: this,
|
|
467
|
+
C: (c, m) => c(...m)
|
|
468
|
+
}), await this._onSwarmEvent({
|
|
469
|
+
topic: t,
|
|
470
|
+
swarmEvent: o
|
|
471
|
+
}));
|
|
472
|
+
}), this._swarmStreams.set({
|
|
473
|
+
topic: t,
|
|
474
|
+
peerId: a
|
|
475
|
+
}, r);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
async _reconcileMessageSubscriptions(e, s) {
|
|
479
|
+
var i;
|
|
480
|
+
for (const t of this.messageStreams.keys())
|
|
481
|
+
this._subscribedMessages.has({
|
|
482
|
+
peerId: t
|
|
483
|
+
}) || ((i = this.messageStreams.get(t)) == null || i.close(), this.messageStreams.delete(t));
|
|
484
|
+
for (const { peerId: t } of this._subscribedMessages.values()) {
|
|
485
|
+
if (this.messageStreams.has(t))
|
|
486
|
+
continue;
|
|
487
|
+
const a = await j(R(e, s.receiveMessages(t)), 5e3);
|
|
488
|
+
a.subscribe(async (r) => {
|
|
489
|
+
if (this._subscribedMessages.has({
|
|
490
|
+
peerId: t
|
|
491
|
+
})) {
|
|
492
|
+
const o = {
|
|
493
|
+
author: S.from(r.author),
|
|
494
|
+
recipient: S.from(r.recipient),
|
|
495
|
+
payload: r.payload
|
|
496
|
+
};
|
|
497
|
+
await this._onMessage(o);
|
|
498
|
+
}
|
|
499
|
+
}), this.messageStreams.set(t, a);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}, re = class {
|
|
503
|
+
recordClientCloseFailure(e) {
|
|
504
|
+
C.metrics.increment("dxos.mesh.signal.signal-rpc-client.close-failure", 1, {
|
|
505
|
+
tags: {
|
|
506
|
+
reason: e.failureReason
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}, u = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-client/signal-rpc-client.ts", oe = 1e4, ce = class {
|
|
511
|
+
constructor({ url: e, callbacks: s = {} }) {
|
|
512
|
+
this._connectTrigger = new N(), this._closed = !1, this._closeComplete = new N(), this._monitor = new re();
|
|
513
|
+
const i = S.random().toHex();
|
|
514
|
+
n.trace("dxos.mesh.signal-rpc-client.constructor", w.begin({
|
|
515
|
+
id: i
|
|
516
|
+
}), {
|
|
517
|
+
F: u,
|
|
518
|
+
L: 60,
|
|
519
|
+
S: this,
|
|
520
|
+
C: (t, a) => t(...a)
|
|
521
|
+
}), this._url = e, this._callbacks = s, this._socket = new M(this._url), this._rpc = q({
|
|
522
|
+
requested: {
|
|
523
|
+
Signal: O.getService("dxos.mesh.signal.Signal")
|
|
524
|
+
},
|
|
525
|
+
noHandshake: !0,
|
|
526
|
+
port: {
|
|
527
|
+
send: (t) => {
|
|
528
|
+
if (!this._closed)
|
|
529
|
+
try {
|
|
530
|
+
this._socket.send(t);
|
|
531
|
+
} catch (a) {
|
|
532
|
+
n.warn("send error", a, {
|
|
533
|
+
F: u,
|
|
534
|
+
L: 79,
|
|
535
|
+
S: this,
|
|
536
|
+
C: (r, o) => r(...o)
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
subscribe: (t) => {
|
|
541
|
+
this._socket.onmessage = async (a) => {
|
|
542
|
+
typeof Blob < "u" && a.data instanceof Blob ? t(ee.from(await a.data.arrayBuffer())) : t(a.data);
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
encodingOptions: {
|
|
547
|
+
preserveAny: !0
|
|
548
|
+
}
|
|
549
|
+
}), this._socket.onopen = async () => {
|
|
550
|
+
var t, a, r, o;
|
|
551
|
+
try {
|
|
552
|
+
if (await this._rpc.open(), this._closed) {
|
|
553
|
+
await this._safeCloseRpc();
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
n(`RPC open ${this._url}`, void 0, {
|
|
557
|
+
F: u,
|
|
558
|
+
L: 104,
|
|
559
|
+
S: this,
|
|
560
|
+
C: (c, m) => c(...m)
|
|
561
|
+
}), (a = (t = this._callbacks).onConnected) == null || a.call(t), this._connectTrigger.wake(), this._keepaliveCtx = new D(void 0, {
|
|
562
|
+
F: u,
|
|
563
|
+
L: 107
|
|
564
|
+
}), I(this._keepaliveCtx, async () => {
|
|
565
|
+
var c;
|
|
566
|
+
(c = this._socket) == null || c.send("__ping__");
|
|
567
|
+
}, oe);
|
|
568
|
+
} catch (c) {
|
|
569
|
+
(o = (r = this._callbacks).onError) == null || o.call(r, c), this._socket.close(), this._closed = !0;
|
|
570
|
+
}
|
|
571
|
+
}, this._socket.onclose = async () => {
|
|
572
|
+
var t, a;
|
|
573
|
+
n(`Disconnected ${this._url}`, void 0, {
|
|
574
|
+
F: u,
|
|
575
|
+
L: 127,
|
|
576
|
+
S: this,
|
|
577
|
+
C: (r, o) => r(...o)
|
|
578
|
+
}), (a = (t = this._callbacks).onDisconnected) == null || a.call(t), this._closeComplete.wake(), await this.close();
|
|
579
|
+
}, this._socket.onerror = async (t) => {
|
|
580
|
+
var a, r;
|
|
581
|
+
if (this._closed) {
|
|
582
|
+
this._socket.close();
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
this._closed = !0, (r = (a = this._callbacks).onError) == null || r.call(a, t.error ?? new Error(t.message)), await this._safeCloseRpc(), n.warn(`Socket ${t.type ?? "unknown"} error`, {
|
|
586
|
+
message: t.message,
|
|
587
|
+
url: this._url
|
|
588
|
+
}, {
|
|
589
|
+
F: u,
|
|
590
|
+
L: 143,
|
|
591
|
+
S: this,
|
|
592
|
+
C: (o, c) => o(...c)
|
|
593
|
+
});
|
|
594
|
+
}, n.trace("dxos.mesh.signal-rpc-client.constructor", w.end({
|
|
595
|
+
id: i
|
|
596
|
+
}), {
|
|
597
|
+
F: u,
|
|
598
|
+
L: 146,
|
|
599
|
+
S: this,
|
|
600
|
+
C: (t, a) => t(...a)
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
async close() {
|
|
604
|
+
var e, s;
|
|
605
|
+
if (!this._closed) {
|
|
606
|
+
this._closed = !0, await ((e = this._keepaliveCtx) == null ? void 0 : e.dispose());
|
|
607
|
+
try {
|
|
608
|
+
await this._safeCloseRpc(), (this._socket.readyState === M.OPEN || this._socket.readyState === M.CONNECTING) && this._socket.close(), await this._closeComplete.wait({
|
|
609
|
+
timeout: 1e3
|
|
610
|
+
});
|
|
611
|
+
} catch (i) {
|
|
612
|
+
const t = i instanceof z ? "timeout" : ((s = i == null ? void 0 : i.constructor) == null ? void 0 : s.name) ?? "unknown";
|
|
613
|
+
this._monitor.recordClientCloseFailure({
|
|
614
|
+
failureReason: t
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
async join({ topic: e, peerId: s }) {
|
|
620
|
+
var t, a, r, o;
|
|
621
|
+
n("join", {
|
|
622
|
+
topic: e,
|
|
623
|
+
peerId: s,
|
|
624
|
+
metadata: (a = (t = this._callbacks) == null ? void 0 : t.getMetadata) == null ? void 0 : a.call(t)
|
|
625
|
+
}, {
|
|
626
|
+
F: u,
|
|
627
|
+
L: 172,
|
|
628
|
+
S: this,
|
|
629
|
+
C: (c, m) => c(...m)
|
|
630
|
+
}), _(!this._closed, "SignalRPCClient is closed", {
|
|
631
|
+
F: u,
|
|
632
|
+
L: 173,
|
|
633
|
+
S: this,
|
|
634
|
+
A: [
|
|
635
|
+
"!this._closed",
|
|
636
|
+
"'SignalRPCClient is closed'"
|
|
637
|
+
]
|
|
638
|
+
}), await this._connectTrigger.wait();
|
|
639
|
+
const i = this._rpc.rpc.Signal.join({
|
|
640
|
+
swarm: e.asUint8Array(),
|
|
641
|
+
peer: s.asUint8Array(),
|
|
642
|
+
metadata: (o = (r = this._callbacks) == null ? void 0 : r.getMetadata) == null ? void 0 : o.call(r)
|
|
643
|
+
});
|
|
644
|
+
return await i.waitUntilReady(), i;
|
|
645
|
+
}
|
|
646
|
+
async receiveMessages(e) {
|
|
647
|
+
n("receiveMessages", {
|
|
648
|
+
peerId: e
|
|
649
|
+
}, {
|
|
650
|
+
F: u,
|
|
651
|
+
L: 185,
|
|
652
|
+
S: this,
|
|
653
|
+
C: (i, t) => i(...t)
|
|
654
|
+
}), _(!this._closed, "SignalRPCClient is closed", {
|
|
655
|
+
F: u,
|
|
656
|
+
L: 186,
|
|
657
|
+
S: this,
|
|
658
|
+
A: [
|
|
659
|
+
"!this._closed",
|
|
660
|
+
"'SignalRPCClient is closed'"
|
|
661
|
+
]
|
|
662
|
+
}), await this._connectTrigger.wait();
|
|
663
|
+
const s = this._rpc.rpc.Signal.receiveMessages({
|
|
664
|
+
peer: e.asUint8Array()
|
|
665
|
+
});
|
|
666
|
+
return await s.waitUntilReady(), s;
|
|
667
|
+
}
|
|
668
|
+
async sendMessage({ author: e, recipient: s, payload: i }) {
|
|
669
|
+
var t, a, r, o;
|
|
670
|
+
n("sendMessage", {
|
|
671
|
+
author: e,
|
|
672
|
+
recipient: s,
|
|
673
|
+
payload: i,
|
|
674
|
+
metadata: (a = (t = this._callbacks) == null ? void 0 : t.getMetadata) == null ? void 0 : a.call(t)
|
|
675
|
+
}, {
|
|
676
|
+
F: u,
|
|
677
|
+
L: 196,
|
|
678
|
+
S: this,
|
|
679
|
+
C: (c, m) => c(...m)
|
|
680
|
+
}), _(!this._closed, "SignalRPCClient is closed", {
|
|
681
|
+
F: u,
|
|
682
|
+
L: 197,
|
|
683
|
+
S: this,
|
|
684
|
+
A: [
|
|
685
|
+
"!this._closed",
|
|
686
|
+
"'SignalRPCClient is closed'"
|
|
687
|
+
]
|
|
688
|
+
}), await this._connectTrigger.wait(), await this._rpc.rpc.Signal.sendMessage({
|
|
689
|
+
author: e.asUint8Array(),
|
|
690
|
+
recipient: s.asUint8Array(),
|
|
691
|
+
payload: i,
|
|
692
|
+
metadata: (o = (r = this._callbacks) == null ? void 0 : r.getMetadata) == null ? void 0 : o.call(r)
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
async _safeCloseRpc() {
|
|
696
|
+
try {
|
|
697
|
+
this._connectTrigger.reset(), await this._rpc.close();
|
|
698
|
+
} catch (e) {
|
|
699
|
+
n.catch(e, void 0, {
|
|
700
|
+
F: u,
|
|
701
|
+
L: 212,
|
|
702
|
+
S: this,
|
|
703
|
+
C: (s, i) => s(...i)
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}, d = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-client/signal-client.ts", K = 100, he = 5e3, le = 1e3, de = 5e3, _e = class extends Q {
|
|
708
|
+
/**
|
|
709
|
+
* @param _host Signal server websocket URL.
|
|
710
|
+
* @param onMessage called when a new message is received.
|
|
711
|
+
* @param onSwarmEvent called when a new swarm event is received.
|
|
712
|
+
* @param _getMetadata signal-message metadata provider, called for every message.
|
|
713
|
+
*/
|
|
714
|
+
constructor(e, s, i, t) {
|
|
715
|
+
if (super(), this._host = e, this._getMetadata = t, this._monitor = new ae(), this._state = l.CLOSED, this._lastReconciliationFailed = !1, this._clientReady = new N(), this._reconnectAfter = K, this._instanceId = S.random().toHex(), this.statusChanged = new p(), !this._host.startsWith("wss://") && !this._host.startsWith("ws://"))
|
|
716
|
+
throw new Error(`Signal server requires a websocket URL. Provided: ${this._host}`);
|
|
717
|
+
this.localState = new ne((a) => (this._monitor.recordMessageReceived(a), s(a)), i);
|
|
718
|
+
}
|
|
719
|
+
async _open() {
|
|
720
|
+
n.trace("dxos.mesh.signal-client.open", w.begin({
|
|
721
|
+
id: this._instanceId
|
|
722
|
+
}), {
|
|
723
|
+
F: d,
|
|
724
|
+
L: 83,
|
|
725
|
+
S: this,
|
|
726
|
+
C: (e, s) => e(...s)
|
|
727
|
+
}), ![
|
|
728
|
+
l.CONNECTED,
|
|
729
|
+
l.CONNECTING
|
|
730
|
+
].includes(this._state) && (this._setState(l.CONNECTING), this._reconcileTask = new P(this._ctx, async () => {
|
|
731
|
+
try {
|
|
732
|
+
await R(this._connectionCtx, this._clientReady.wait({
|
|
733
|
+
timeout: 5e3
|
|
734
|
+
})), _(this._state === l.CONNECTED, "Not connected to Signal Server", {
|
|
735
|
+
F: d,
|
|
736
|
+
L: 93,
|
|
737
|
+
S: this,
|
|
738
|
+
A: [
|
|
739
|
+
"this._state === SignalState.CONNECTED",
|
|
740
|
+
"'Not connected to Signal Server'"
|
|
741
|
+
]
|
|
742
|
+
}), await this.localState.reconcile(this._connectionCtx, this._client), this._monitor.recordReconciliation({
|
|
743
|
+
success: !0
|
|
744
|
+
}), this._lastReconciliationFailed = !1;
|
|
745
|
+
} catch (e) {
|
|
746
|
+
throw this._lastReconciliationFailed = !0, this._monitor.recordReconciliation({
|
|
747
|
+
success: !1
|
|
748
|
+
}), e;
|
|
749
|
+
}
|
|
750
|
+
}), I(this._ctx, async () => {
|
|
751
|
+
this._state === l.CONNECTED && this._reconcileTask.schedule();
|
|
752
|
+
}, de), this._reconnectTask = new P(this._ctx, async () => {
|
|
753
|
+
try {
|
|
754
|
+
await this._reconnect(), this._monitor.recordReconnect({
|
|
755
|
+
success: !0
|
|
756
|
+
});
|
|
757
|
+
} catch (e) {
|
|
758
|
+
throw this._monitor.recordReconnect({
|
|
759
|
+
success: !1
|
|
760
|
+
}), e;
|
|
761
|
+
}
|
|
762
|
+
}), this._createClient(), n.trace("dxos.mesh.signal-client.open", w.end({
|
|
763
|
+
id: this._instanceId
|
|
764
|
+
}), {
|
|
765
|
+
F: d,
|
|
766
|
+
L: 126,
|
|
767
|
+
S: this,
|
|
768
|
+
C: (e, s) => e(...s)
|
|
769
|
+
}));
|
|
770
|
+
}
|
|
771
|
+
async _catch(e) {
|
|
772
|
+
this._state === l.CLOSED || this._ctx.disposed || (this._state === l.CONNECTED && !this._lastReconciliationFailed && n.warn("SignalClient error:", e, {
|
|
773
|
+
F: d,
|
|
774
|
+
L: 135,
|
|
775
|
+
S: this,
|
|
776
|
+
C: (s, i) => s(...i)
|
|
777
|
+
}), this._scheduleReconcileAfterError());
|
|
778
|
+
}
|
|
779
|
+
async _close() {
|
|
780
|
+
n("closing...", void 0, {
|
|
781
|
+
F: d,
|
|
782
|
+
L: 141,
|
|
783
|
+
S: this,
|
|
784
|
+
C: (e, s) => e(...s)
|
|
785
|
+
}), ![
|
|
786
|
+
l.CLOSED
|
|
787
|
+
].includes(this._state) && (this._setState(l.CLOSED), await this._safeResetClient(), n("closed", void 0, {
|
|
788
|
+
F: d,
|
|
789
|
+
L: 149,
|
|
790
|
+
S: this,
|
|
791
|
+
C: (e, s) => e(...s)
|
|
792
|
+
}));
|
|
793
|
+
}
|
|
794
|
+
getStatus() {
|
|
795
|
+
var e;
|
|
796
|
+
return {
|
|
797
|
+
host: this._host,
|
|
798
|
+
state: this._state,
|
|
799
|
+
error: (e = this._lastError) == null ? void 0 : e.message,
|
|
800
|
+
reconnectIn: this._reconnectAfter,
|
|
801
|
+
...this._monitor.getRecordedTimestamps()
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
async join(e) {
|
|
805
|
+
var s;
|
|
806
|
+
n("joining", {
|
|
807
|
+
topic: e.topic,
|
|
808
|
+
peerId: e.peerId
|
|
809
|
+
}, {
|
|
810
|
+
F: d,
|
|
811
|
+
L: 163,
|
|
812
|
+
S: this,
|
|
813
|
+
C: (i, t) => i(...t)
|
|
814
|
+
}), this._monitor.recordJoin(), this.localState.join(e), (s = this._reconcileTask) == null || s.schedule();
|
|
815
|
+
}
|
|
816
|
+
async leave(e) {
|
|
817
|
+
n("leaving", {
|
|
818
|
+
topic: e.topic,
|
|
819
|
+
peerId: e.peerId
|
|
820
|
+
}, {
|
|
821
|
+
F: d,
|
|
822
|
+
L: 170,
|
|
823
|
+
S: this,
|
|
824
|
+
C: (s, i) => s(...i)
|
|
825
|
+
}), this._monitor.recordLeave(), this.localState.leave(e);
|
|
826
|
+
}
|
|
827
|
+
async sendMessage(e) {
|
|
828
|
+
return this._monitor.recordMessageSending(e, async () => {
|
|
829
|
+
await this._clientReady.wait(), _(this._state === l.CONNECTED, "Not connected to Signal Server", {
|
|
830
|
+
F: d,
|
|
831
|
+
L: 178,
|
|
832
|
+
S: this,
|
|
833
|
+
A: [
|
|
834
|
+
"this._state === SignalState.CONNECTED",
|
|
835
|
+
"'Not connected to Signal Server'"
|
|
836
|
+
]
|
|
837
|
+
}), await this._client.sendMessage(e);
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
async subscribeMessages(e) {
|
|
841
|
+
var s;
|
|
842
|
+
n("subscribing to messages", {
|
|
843
|
+
peerId: e
|
|
844
|
+
}, {
|
|
845
|
+
F: d,
|
|
846
|
+
L: 184,
|
|
847
|
+
S: this,
|
|
848
|
+
C: (i, t) => i(...t)
|
|
849
|
+
}), this.localState.subscribeMessages(e), (s = this._reconcileTask) == null || s.schedule();
|
|
850
|
+
}
|
|
851
|
+
async unsubscribeMessages(e) {
|
|
852
|
+
n("unsubscribing from messages", {
|
|
853
|
+
peerId: e
|
|
854
|
+
}, {
|
|
855
|
+
F: d,
|
|
856
|
+
L: 190,
|
|
857
|
+
S: this,
|
|
858
|
+
C: (s, i) => s(...i)
|
|
859
|
+
}), this.localState.unsubscribeMessages(e);
|
|
860
|
+
}
|
|
861
|
+
_scheduleReconcileAfterError() {
|
|
862
|
+
V(this._ctx, () => this._reconcileTask.schedule(), le);
|
|
863
|
+
}
|
|
864
|
+
_createClient() {
|
|
865
|
+
n("creating client", {
|
|
866
|
+
host: this._host,
|
|
867
|
+
state: this._state
|
|
868
|
+
}, {
|
|
869
|
+
F: d,
|
|
870
|
+
L: 199,
|
|
871
|
+
S: this,
|
|
872
|
+
C: (e, s) => e(...s)
|
|
873
|
+
}), _(!this._client, "Client already created", {
|
|
874
|
+
F: d,
|
|
875
|
+
L: 200,
|
|
876
|
+
S: this,
|
|
877
|
+
A: [
|
|
878
|
+
"!this._client",
|
|
879
|
+
"'Client already created'"
|
|
880
|
+
]
|
|
881
|
+
}), this._monitor.recordConnectionStartTime(), this._connectionCtx = this._ctx.derive(), this._connectionCtx.onDispose(async () => {
|
|
882
|
+
n("connection context disposed", void 0, {
|
|
883
|
+
F: d,
|
|
884
|
+
L: 207,
|
|
885
|
+
S: this,
|
|
886
|
+
C: (s, i) => s(...i)
|
|
887
|
+
});
|
|
888
|
+
const { failureCount: e } = await this.localState.safeCloseStreams();
|
|
889
|
+
this._monitor.recordStreamCloseErrors(e);
|
|
890
|
+
});
|
|
891
|
+
try {
|
|
892
|
+
const e = new ce({
|
|
893
|
+
url: this._host,
|
|
894
|
+
callbacks: {
|
|
895
|
+
onConnected: () => {
|
|
896
|
+
e === this._client && (n("socket connected", void 0, {
|
|
897
|
+
F: d,
|
|
898
|
+
L: 218,
|
|
899
|
+
S: this,
|
|
900
|
+
C: (s, i) => s(...i)
|
|
901
|
+
}), this._onConnected());
|
|
902
|
+
},
|
|
903
|
+
onDisconnected: () => {
|
|
904
|
+
e === this._client && (n("socket disconnected", {
|
|
905
|
+
state: this._state
|
|
906
|
+
}, {
|
|
907
|
+
F: d,
|
|
908
|
+
L: 227,
|
|
909
|
+
S: this,
|
|
910
|
+
C: (s, i) => s(...i)
|
|
911
|
+
}), this._state === l.ERROR ? this._setState(l.DISCONNECTED) : this._onDisconnected());
|
|
912
|
+
},
|
|
913
|
+
onError: (s) => {
|
|
914
|
+
e === this._client && (n("socket error", {
|
|
915
|
+
error: s,
|
|
916
|
+
state: this._state
|
|
917
|
+
}, {
|
|
918
|
+
F: d,
|
|
919
|
+
L: 239,
|
|
920
|
+
S: this,
|
|
921
|
+
C: (i, t) => i(...t)
|
|
922
|
+
}), this._onDisconnected({
|
|
923
|
+
error: s
|
|
924
|
+
}));
|
|
925
|
+
},
|
|
926
|
+
getMetadata: this._getMetadata
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
this._client = e;
|
|
930
|
+
} catch (e) {
|
|
931
|
+
this._client = void 0, this._onDisconnected({
|
|
932
|
+
error: e
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
async _reconnect() {
|
|
937
|
+
if (n(`reconnecting in ${this._reconnectAfter}ms`, {
|
|
938
|
+
state: this._state
|
|
939
|
+
}, {
|
|
940
|
+
F: d,
|
|
941
|
+
L: 254,
|
|
942
|
+
S: this,
|
|
943
|
+
C: (e, s) => e(...s)
|
|
944
|
+
}), this._state === l.RECONNECTING) {
|
|
945
|
+
n.info("Signal api already reconnecting.", void 0, {
|
|
946
|
+
F: d,
|
|
947
|
+
L: 257,
|
|
948
|
+
S: this,
|
|
949
|
+
C: (e, s) => e(...s)
|
|
950
|
+
});
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
this._state !== l.CLOSED && (this._setState(l.RECONNECTING), await this._safeResetClient(), await R(this._ctx, $(this._reconnectAfter)), this._createClient());
|
|
954
|
+
}
|
|
955
|
+
_onConnected() {
|
|
956
|
+
this._lastError = void 0, this._lastReconciliationFailed = !1, this._reconnectAfter = K, this._setState(l.CONNECTED), this._clientReady.wake(), this._reconcileTask.schedule();
|
|
957
|
+
}
|
|
958
|
+
_onDisconnected(e) {
|
|
959
|
+
this._updateReconnectTimeout(), this._state !== l.CLOSED && (e != null && e.error ? (this._lastError = e.error, this._setState(l.ERROR)) : this._setState(l.DISCONNECTED), this._reconnectTask.schedule());
|
|
960
|
+
}
|
|
961
|
+
_setState(e) {
|
|
962
|
+
this._state = e, this._monitor.recordStateChangeTime(), n("signal state changed", {
|
|
963
|
+
status: this.getStatus()
|
|
964
|
+
}, {
|
|
965
|
+
F: d,
|
|
966
|
+
L: 298,
|
|
967
|
+
S: this,
|
|
968
|
+
C: (s, i) => s(...i)
|
|
969
|
+
}), this.statusChanged.emit(this.getStatus());
|
|
970
|
+
}
|
|
971
|
+
_updateReconnectTimeout() {
|
|
972
|
+
this._state !== l.CONNECTED && this._state !== l.CONNECTING && (this._reconnectAfter *= 2, this._reconnectAfter = Math.min(this._reconnectAfter, he));
|
|
973
|
+
}
|
|
974
|
+
async _safeResetClient() {
|
|
975
|
+
var e, s;
|
|
976
|
+
await ((e = this._connectionCtx) == null ? void 0 : e.dispose()), this._connectionCtx = void 0, this._clientReady.reset(), await ((s = this._client) == null ? void 0 : s.close().catch(() => {
|
|
977
|
+
})), this._client = void 0;
|
|
978
|
+
}
|
|
979
|
+
}, ge = class {
|
|
980
|
+
recordRateLimitExceeded() {
|
|
981
|
+
C.metrics.increment("dxos.mesh.signal.signal-manager.rate-limit-hit", 1);
|
|
982
|
+
}
|
|
983
|
+
recordServerFailure(e) {
|
|
984
|
+
C.metrics.increment("dxos.mesh.signal.signal-manager.server-failure", 1, {
|
|
985
|
+
tags: {
|
|
986
|
+
server: e.serverName,
|
|
987
|
+
restarted: e.willRestart
|
|
988
|
+
}
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
function L(e, s, i, t) {
|
|
993
|
+
var a = arguments.length, r = a < 3 ? s : t === null ? t = Object.getOwnPropertyDescriptor(s, i) : t, o;
|
|
994
|
+
if (typeof Reflect == "object" && typeof Reflect.decorate == "function")
|
|
995
|
+
r = Reflect.decorate(e, s, i, t);
|
|
996
|
+
else
|
|
997
|
+
for (var c = e.length - 1; c >= 0; c--)
|
|
998
|
+
(o = e[c]) && (r = (a < 3 ? o(r) : a > 3 ? o(s, i, r) : o(s, i)) || r);
|
|
999
|
+
return a > 3 && r && Object.defineProperty(s, i, r), r;
|
|
1000
|
+
}
|
|
1001
|
+
var h = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-manager/websocket-signal-manager.ts", me = 5, ue = 3e3, k = class {
|
|
1002
|
+
constructor(e, s) {
|
|
1003
|
+
this._hosts = e, this._getMetadata = s, this._servers = /* @__PURE__ */ new Map(), this._monitor = new ge(), this._opened = !1, this.failureCount = /* @__PURE__ */ new Map(), this.statusChanged = new p(), this.swarmEvent = new p(), this.onMessage = new p(), this._instanceId = S.random().toHex(), n("Created WebsocketSignalManager", {
|
|
1004
|
+
hosts: this._hosts
|
|
1005
|
+
}, {
|
|
1006
|
+
F: h,
|
|
1007
|
+
L: 58,
|
|
1008
|
+
S: this,
|
|
1009
|
+
C: (i, t) => i(...t)
|
|
1010
|
+
});
|
|
1011
|
+
for (const i of this._hosts) {
|
|
1012
|
+
if (this._servers.has(i.server))
|
|
1013
|
+
continue;
|
|
1014
|
+
const t = new _e(i.server, async (a) => this.onMessage.emit(a), async (a) => this.swarmEvent.emit(a), this._getMetadata);
|
|
1015
|
+
t.statusChanged.on(() => this.statusChanged.emit(this.getStatus())), this._servers.set(i.server, t), this.failureCount.set(i.server, 0);
|
|
1016
|
+
}
|
|
1017
|
+
this._failedServersBitfield = y.zeros(this._hosts.length);
|
|
1018
|
+
}
|
|
1019
|
+
async open() {
|
|
1020
|
+
this._opened || (n("open signal manager", {
|
|
1021
|
+
hosts: this._hosts
|
|
1022
|
+
}, {
|
|
1023
|
+
F: h,
|
|
1024
|
+
L: 85,
|
|
1025
|
+
S: this,
|
|
1026
|
+
C: (e, s) => e(...s)
|
|
1027
|
+
}), n.trace("dxos.mesh.websocket-signal-manager.open", w.begin({
|
|
1028
|
+
id: this._instanceId
|
|
1029
|
+
}), {
|
|
1030
|
+
F: h,
|
|
1031
|
+
L: 86,
|
|
1032
|
+
S: this,
|
|
1033
|
+
C: (e, s) => e(...s)
|
|
1034
|
+
}), this._initContext(), await T(this._servers.values(), (e) => e.open()), this._opened = !0, n.trace("dxos.mesh.websocket-signal-manager.open", w.end({
|
|
1035
|
+
id: this._instanceId
|
|
1036
|
+
}), {
|
|
1037
|
+
F: h,
|
|
1038
|
+
L: 93,
|
|
1039
|
+
S: this,
|
|
1040
|
+
C: (e, s) => e(...s)
|
|
1041
|
+
}));
|
|
1042
|
+
}
|
|
1043
|
+
async close() {
|
|
1044
|
+
this._opened && (this._opened = !1, await this._ctx.dispose(), await T(this._servers.values(), (e) => e.close()));
|
|
1045
|
+
}
|
|
1046
|
+
async restartServer(e) {
|
|
1047
|
+
n("restarting server", {
|
|
1048
|
+
serverName: e
|
|
1049
|
+
}, {
|
|
1050
|
+
F: h,
|
|
1051
|
+
L: 107,
|
|
1052
|
+
S: this,
|
|
1053
|
+
C: (i, t) => i(...t)
|
|
1054
|
+
}), _(this._opened, "server already closed", {
|
|
1055
|
+
F: h,
|
|
1056
|
+
L: 108,
|
|
1057
|
+
S: this,
|
|
1058
|
+
A: [
|
|
1059
|
+
"this._opened",
|
|
1060
|
+
"'server already closed'"
|
|
1061
|
+
]
|
|
1062
|
+
});
|
|
1063
|
+
const s = this._servers.get(e);
|
|
1064
|
+
_(s, "server not found", {
|
|
1065
|
+
F: h,
|
|
1066
|
+
L: 111,
|
|
1067
|
+
S: this,
|
|
1068
|
+
A: [
|
|
1069
|
+
"server",
|
|
1070
|
+
"'server not found'"
|
|
1071
|
+
]
|
|
1072
|
+
}), await s.close(), await $(ue), await s.open();
|
|
1073
|
+
}
|
|
1074
|
+
getStatus() {
|
|
1075
|
+
return Array.from(this._servers.values()).map((e) => e.getStatus());
|
|
1076
|
+
}
|
|
1077
|
+
async join({ topic: e, peerId: s }) {
|
|
1078
|
+
n("join", {
|
|
1079
|
+
topic: e,
|
|
1080
|
+
peerId: s
|
|
1081
|
+
}, {
|
|
1082
|
+
F: h,
|
|
1083
|
+
L: 124,
|
|
1084
|
+
S: this,
|
|
1085
|
+
C: (i, t) => i(...t)
|
|
1086
|
+
}), _(this._opened, "Closed", {
|
|
1087
|
+
F: h,
|
|
1088
|
+
L: 125,
|
|
1089
|
+
S: this,
|
|
1090
|
+
A: [
|
|
1091
|
+
"this._opened",
|
|
1092
|
+
"'Closed'"
|
|
1093
|
+
]
|
|
1094
|
+
}), await this._forEachServer((i) => i.join({
|
|
1095
|
+
topic: e,
|
|
1096
|
+
peerId: s
|
|
1097
|
+
}));
|
|
1098
|
+
}
|
|
1099
|
+
async leave({ topic: e, peerId: s }) {
|
|
1100
|
+
n("leaving", {
|
|
1101
|
+
topic: e,
|
|
1102
|
+
peerId: s
|
|
1103
|
+
}, {
|
|
1104
|
+
F: h,
|
|
1105
|
+
L: 131,
|
|
1106
|
+
S: this,
|
|
1107
|
+
C: (i, t) => i(...t)
|
|
1108
|
+
}), _(this._opened, "Closed", {
|
|
1109
|
+
F: h,
|
|
1110
|
+
L: 132,
|
|
1111
|
+
S: this,
|
|
1112
|
+
A: [
|
|
1113
|
+
"this._opened",
|
|
1114
|
+
"'Closed'"
|
|
1115
|
+
]
|
|
1116
|
+
}), await this._forEachServer((i) => i.leave({
|
|
1117
|
+
topic: e,
|
|
1118
|
+
peerId: s
|
|
1119
|
+
}));
|
|
1120
|
+
}
|
|
1121
|
+
async sendMessage({ author: e, recipient: s, payload: i }) {
|
|
1122
|
+
n("signal", {
|
|
1123
|
+
recipient: s
|
|
1124
|
+
}, {
|
|
1125
|
+
F: h,
|
|
1126
|
+
L: 145,
|
|
1127
|
+
S: this,
|
|
1128
|
+
C: (t, a) => t(...a)
|
|
1129
|
+
}), _(this._opened, "Closed", {
|
|
1130
|
+
F: h,
|
|
1131
|
+
L: 146,
|
|
1132
|
+
S: this,
|
|
1133
|
+
A: [
|
|
1134
|
+
"this._opened",
|
|
1135
|
+
"'Closed'"
|
|
1136
|
+
]
|
|
1137
|
+
}), this._forEachServer(async (t, a, r) => {
|
|
1138
|
+
t.sendMessage({
|
|
1139
|
+
author: e,
|
|
1140
|
+
recipient: s,
|
|
1141
|
+
payload: i
|
|
1142
|
+
}).then(() => this._clearServerFailedFlag(a, r)).catch((o) => {
|
|
1143
|
+
o instanceof se ? (n.info("WSS rate limit exceeded", {
|
|
1144
|
+
err: o
|
|
1145
|
+
}, {
|
|
1146
|
+
F: h,
|
|
1147
|
+
L: 154,
|
|
1148
|
+
S: this,
|
|
1149
|
+
C: (c, m) => c(...m)
|
|
1150
|
+
}), this._monitor.recordRateLimitExceeded()) : o instanceof J || o.constructor.name === "TimeoutError" ? (n.info("WSS sendMessage timeout", {
|
|
1151
|
+
err: o
|
|
1152
|
+
}, {
|
|
1153
|
+
F: h,
|
|
1154
|
+
L: 157,
|
|
1155
|
+
S: this,
|
|
1156
|
+
C: (c, m) => c(...m)
|
|
1157
|
+
}), this.checkServerFailure(a, r)) : (n.warn(`error sending to ${a}`, {
|
|
1158
|
+
err: o
|
|
1159
|
+
}, {
|
|
1160
|
+
F: h,
|
|
1161
|
+
L: 160,
|
|
1162
|
+
S: this,
|
|
1163
|
+
C: (c, m) => c(...m)
|
|
1164
|
+
}), this.checkServerFailure(a, r));
|
|
1165
|
+
});
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
async checkServerFailure(e, s) {
|
|
1169
|
+
const i = this.failureCount.get(e) ?? 0, t = i > me;
|
|
1170
|
+
if (this._monitor.recordServerFailure({
|
|
1171
|
+
serverName: e,
|
|
1172
|
+
willRestart: t
|
|
1173
|
+
}), t) {
|
|
1174
|
+
y.get(this._failedServersBitfield, s) || (n.warn("too many failures for ws-server, restarting", {
|
|
1175
|
+
serverName: e,
|
|
1176
|
+
failureCount: i
|
|
1177
|
+
}, {
|
|
1178
|
+
F: h,
|
|
1179
|
+
L: 174,
|
|
1180
|
+
S: this,
|
|
1181
|
+
C: (a, r) => a(...r)
|
|
1182
|
+
}), y.set(this._failedServersBitfield, s, !0)), await this.restartServer(e), this.failureCount.set(e, 0);
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
this.failureCount.set(e, (this.failureCount.get(e) ?? 0) + 1);
|
|
1186
|
+
}
|
|
1187
|
+
_clearServerFailedFlag(e, s) {
|
|
1188
|
+
y.get(this._failedServersBitfield, s) && (n.info("server connection restored", {
|
|
1189
|
+
serverName: e
|
|
1190
|
+
}, {
|
|
1191
|
+
F: h,
|
|
1192
|
+
L: 187,
|
|
1193
|
+
S: this,
|
|
1194
|
+
C: (i, t) => i(...t)
|
|
1195
|
+
}), y.set(this._failedServersBitfield, s, !1), this.failureCount.set(e, 0));
|
|
1196
|
+
}
|
|
1197
|
+
async subscribeMessages(e) {
|
|
1198
|
+
n("subscribed for message stream", {
|
|
1199
|
+
peerId: e
|
|
1200
|
+
}, {
|
|
1201
|
+
F: h,
|
|
1202
|
+
L: 194,
|
|
1203
|
+
S: this,
|
|
1204
|
+
C: (s, i) => s(...i)
|
|
1205
|
+
}), _(this._opened, "Closed", {
|
|
1206
|
+
F: h,
|
|
1207
|
+
L: 195,
|
|
1208
|
+
S: this,
|
|
1209
|
+
A: [
|
|
1210
|
+
"this._opened",
|
|
1211
|
+
"'Closed'"
|
|
1212
|
+
]
|
|
1213
|
+
}), await this._forEachServer(async (s) => s.subscribeMessages(e));
|
|
1214
|
+
}
|
|
1215
|
+
async unsubscribeMessages(e) {
|
|
1216
|
+
n("subscribed for message stream", {
|
|
1217
|
+
peerId: e
|
|
1218
|
+
}, {
|
|
1219
|
+
F: h,
|
|
1220
|
+
L: 201,
|
|
1221
|
+
S: this,
|
|
1222
|
+
C: (s, i) => s(...i)
|
|
1223
|
+
}), _(this._opened, "Closed", {
|
|
1224
|
+
F: h,
|
|
1225
|
+
L: 202,
|
|
1226
|
+
S: this,
|
|
1227
|
+
A: [
|
|
1228
|
+
"this._opened",
|
|
1229
|
+
"'Closed'"
|
|
1230
|
+
]
|
|
1231
|
+
}), await this._forEachServer(async (s) => s.unsubscribeMessages(e));
|
|
1232
|
+
}
|
|
1233
|
+
_initContext() {
|
|
1234
|
+
this._ctx = new D({
|
|
1235
|
+
onError: (e) => n.catch(e, void 0, {
|
|
1236
|
+
F: h,
|
|
1237
|
+
L: 209,
|
|
1238
|
+
S: this,
|
|
1239
|
+
C: (s, i) => s(...i)
|
|
1240
|
+
})
|
|
1241
|
+
}, {
|
|
1242
|
+
F: h,
|
|
1243
|
+
L: 208
|
|
1244
|
+
});
|
|
1245
|
+
}
|
|
1246
|
+
async _forEachServer(e) {
|
|
1247
|
+
return Promise.all(Array.from(this._servers.entries()).map(([s, i], t) => e(i, s, t)));
|
|
1248
|
+
}
|
|
1249
|
+
};
|
|
1250
|
+
L([
|
|
1251
|
+
b
|
|
1252
|
+
], k.prototype, "open", null);
|
|
1253
|
+
L([
|
|
1254
|
+
b
|
|
1255
|
+
], k.prototype, "close", null);
|
|
1256
|
+
L([
|
|
1257
|
+
b
|
|
1258
|
+
], k.prototype, "join", null);
|
|
1259
|
+
L([
|
|
1260
|
+
b
|
|
1261
|
+
], k.prototype, "leave", null);
|
|
1262
|
+
L([
|
|
1263
|
+
b
|
|
1264
|
+
], k.prototype, "checkServerFailure", null);
|
|
1265
|
+
var F = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-manager/utils.ts", we = ({ identityService: e, devicesService: s, setTag: i }) => {
|
|
1266
|
+
e.queryIdentity().subscribe((t) => {
|
|
1267
|
+
var a;
|
|
1268
|
+
if (!((a = t == null ? void 0 : t.identity) != null && a.identityKey)) {
|
|
1269
|
+
n("empty response from identity service", {
|
|
1270
|
+
idqr: t
|
|
1271
|
+
}, {
|
|
1272
|
+
F,
|
|
1273
|
+
L: 21,
|
|
1274
|
+
S: void 0,
|
|
1275
|
+
C: (r, o) => r(...o)
|
|
1276
|
+
});
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
i("identityKey", t.identity.identityKey.truncate());
|
|
1280
|
+
}), s.queryDevices().subscribe((t) => {
|
|
1281
|
+
if (!t || !t.devices || t.devices.length === 0) {
|
|
1282
|
+
n("empty response from device service", {
|
|
1283
|
+
device: t
|
|
1284
|
+
}, {
|
|
1285
|
+
F,
|
|
1286
|
+
L: 30,
|
|
1287
|
+
S: void 0,
|
|
1288
|
+
C: (r, o) => r(...o)
|
|
1289
|
+
});
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
_(t, "empty response from device service", {
|
|
1293
|
+
F,
|
|
1294
|
+
L: 33,
|
|
1295
|
+
S: void 0,
|
|
1296
|
+
A: [
|
|
1297
|
+
"dqr",
|
|
1298
|
+
"'empty response from device service'"
|
|
1299
|
+
]
|
|
1300
|
+
});
|
|
1301
|
+
const a = t.devices.find((r) => r.kind === Y.CURRENT);
|
|
1302
|
+
if (!a) {
|
|
1303
|
+
n("no current device", {
|
|
1304
|
+
device: t
|
|
1305
|
+
}, {
|
|
1306
|
+
F,
|
|
1307
|
+
L: 37,
|
|
1308
|
+
S: void 0,
|
|
1309
|
+
C: (r, o) => r(...o)
|
|
1310
|
+
});
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
i("deviceKey", a.deviceKey.truncate());
|
|
1314
|
+
});
|
|
1315
|
+
};
|
|
1316
|
+
export {
|
|
1317
|
+
ve as Messenger,
|
|
1318
|
+
_e as SignalClient,
|
|
1319
|
+
k as WebsocketSignalManager,
|
|
1320
|
+
we as setIdentityTags
|
|
1321
|
+
};
|
|
1322
|
+
//# sourceMappingURL=index-BoJU28fu.mjs.map
|