@dxos/messaging 0.6.13 → 0.6.14-main.2b6a0f3
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-BZOQIXEW.mjs +691 -0
- package/dist/lib/browser/chunk-BZOQIXEW.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +256 -923
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +147 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node/chunk-WDA7H2PO.cjs +688 -0
- package/dist/lib/node/chunk-WDA7H2PO.cjs.map +7 -0
- package/dist/lib/node/index.cjs +272 -916
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +162 -0
- package/dist/lib/node/testing/index.cjs.map +7 -0
- package/dist/lib/node-esm/chunk-RAYDHHAO.mjs +683 -0
- package/dist/lib/node-esm/chunk-RAYDHHAO.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +1631 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/lib/node-esm/testing/index.mjs +146 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/messenger.blueprint-test.d.ts +2 -3
- package/dist/types/src/messenger.blueprint-test.d.ts.map +1 -1
- package/dist/types/src/messenger.d.ts.map +1 -1
- package/dist/types/src/messenger.node.test.d.ts +2 -0
- package/dist/types/src/messenger.node.test.d.ts.map +1 -0
- package/dist/types/src/signal-client/signal-client.node.test.d.ts +2 -0
- package/dist/types/src/signal-client/signal-client.node.test.d.ts.map +1 -0
- package/dist/types/src/signal-client/signal-rpc-client.node.test.d.ts +2 -0
- package/dist/types/src/signal-client/signal-rpc-client.node.test.d.ts.map +1 -0
- package/dist/types/src/signal-manager/edge-signal-manager.d.ts.map +1 -1
- package/dist/types/src/signal-manager/websocket-signal-manager.node.test.d.ts +2 -0
- package/dist/types/src/signal-manager/websocket-signal-manager.node.test.d.ts.map +1 -0
- package/dist/types/src/testing/test-builder.d.ts +3 -4
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-peer.d.ts +0 -1
- package/dist/types/src/testing/test-peer.d.ts.map +1 -1
- package/package.json +32 -21
- package/src/messenger.blueprint-test.ts +25 -33
- package/src/{messenger.test.ts → messenger.node.test.ts} +6 -19
- package/src/messenger.ts +1 -1
- package/src/signal-client/{signal-client.test.ts → signal-client.node.test.ts} +9 -8
- package/src/signal-client/{signal-rpc-client.test.ts → signal-rpc-client.node.test.ts} +8 -13
- package/src/signal-manager/edge-signal-manager.ts +16 -6
- package/src/signal-manager/{websocket-signal-manager.test.ts → websocket-signal-manager.node.test.ts} +12 -30
- package/src/testing/test-builder.ts +3 -6
- package/src/testing/test-peer.ts +5 -7
- package/dist/types/src/messenger.test.d.ts +0 -2
- package/dist/types/src/messenger.test.d.ts.map +0 -1
- package/dist/types/src/signal-client/signal-client.test.d.ts +0 -2
- package/dist/types/src/signal-client/signal-client.test.d.ts.map +0 -1
- package/dist/types/src/signal-client/signal-rpc-client.test.d.ts +0 -2
- package/dist/types/src/signal-client/signal-rpc-client.test.d.ts.map +0 -1
- package/dist/types/src/signal-manager/edge-signal-manager.test.d.ts +0 -2
- package/dist/types/src/signal-manager/edge-signal-manager.test.d.ts.map +0 -1
- package/dist/types/src/signal-manager/websocket-signal-manager.test.d.ts +0 -2
- package/dist/types/src/signal-manager/websocket-signal-manager.test.d.ts.map +0 -1
- package/src/signal-manager/edge-signal-manager.test.ts +0 -67
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var chunk_WDA7H2PO_exports = {};
|
|
20
|
+
__export(chunk_WDA7H2PO_exports, {
|
|
21
|
+
MemorySignalManager: () => MemorySignalManager,
|
|
22
|
+
MemorySignalManagerContext: () => MemorySignalManagerContext,
|
|
23
|
+
Messenger: () => Messenger,
|
|
24
|
+
PeerInfoHash: () => PeerInfoHash
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(chunk_WDA7H2PO_exports);
|
|
27
|
+
var import_async = require("@dxos/async");
|
|
28
|
+
var import_context = require("@dxos/context");
|
|
29
|
+
var import_invariant = require("@dxos/invariant");
|
|
30
|
+
var import_keys = require("@dxos/keys");
|
|
31
|
+
var import_log = require("@dxos/log");
|
|
32
|
+
var import_protocols = require("@dxos/protocols");
|
|
33
|
+
var import_proto = require("@dxos/protocols/proto");
|
|
34
|
+
var import_util = require("@dxos/util");
|
|
35
|
+
var import_tracing = require("@dxos/tracing");
|
|
36
|
+
var import_async2 = require("@dxos/async");
|
|
37
|
+
var import_context2 = require("@dxos/context");
|
|
38
|
+
var import_invariant2 = require("@dxos/invariant");
|
|
39
|
+
var import_keys2 = require("@dxos/keys");
|
|
40
|
+
var import_log2 = require("@dxos/log");
|
|
41
|
+
var import_proto2 = require("@dxos/protocols/proto");
|
|
42
|
+
var import_util2 = require("@dxos/util");
|
|
43
|
+
var MessengerMonitor = class {
|
|
44
|
+
recordMessageAckFailed() {
|
|
45
|
+
import_tracing.trace.metrics.increment("dxos.mesh.signal.messenger.failed-ack", 1);
|
|
46
|
+
}
|
|
47
|
+
recordReliableMessage(params) {
|
|
48
|
+
import_tracing.trace.metrics.increment("dxos.mesh.signal.messenger.reliable-send", 1, {
|
|
49
|
+
tags: {
|
|
50
|
+
success: params.sent,
|
|
51
|
+
attempts: params.sendAttempts
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var MESSAGE_TIMEOUT = 1e4;
|
|
57
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/messenger.ts";
|
|
58
|
+
var ReliablePayload = import_proto.schema.getCodecForType("dxos.mesh.messaging.ReliablePayload");
|
|
59
|
+
var Acknowledgement = import_proto.schema.getCodecForType("dxos.mesh.messaging.Acknowledgement");
|
|
60
|
+
var RECEIVED_MESSAGES_GC_INTERVAL = 12e4;
|
|
61
|
+
var Messenger = class {
|
|
62
|
+
constructor({ signalManager, retryDelay = 1e3 }) {
|
|
63
|
+
this._monitor = new MessengerMonitor();
|
|
64
|
+
this._listeners = new import_util.ComplexMap(({ peerId, payloadType }) => peerId + payloadType);
|
|
65
|
+
this._defaultListeners = /* @__PURE__ */ new Map();
|
|
66
|
+
this._onAckCallbacks = new import_util.ComplexMap(import_keys.PublicKey.hash);
|
|
67
|
+
this._receivedMessages = new import_util.ComplexSet(import_keys.PublicKey.hash);
|
|
68
|
+
this._toClear = new import_util.ComplexSet(import_keys.PublicKey.hash);
|
|
69
|
+
this._closed = true;
|
|
70
|
+
this._signalManager = signalManager;
|
|
71
|
+
this._retryDelay = retryDelay;
|
|
72
|
+
this.open();
|
|
73
|
+
}
|
|
74
|
+
open() {
|
|
75
|
+
if (!this._closed) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const traceId = import_keys.PublicKey.random().toHex();
|
|
79
|
+
import_log.log.trace("dxos.mesh.messenger.open", import_protocols.trace.begin({
|
|
80
|
+
id: traceId
|
|
81
|
+
}), {
|
|
82
|
+
F: __dxlog_file,
|
|
83
|
+
L: 72,
|
|
84
|
+
S: this,
|
|
85
|
+
C: (f, a) => f(...a)
|
|
86
|
+
});
|
|
87
|
+
this._ctx = new import_context.Context({
|
|
88
|
+
onError: (err) => import_log.log.catch(err, void 0, {
|
|
89
|
+
F: __dxlog_file,
|
|
90
|
+
L: 74,
|
|
91
|
+
S: this,
|
|
92
|
+
C: (f, a) => f(...a)
|
|
93
|
+
})
|
|
94
|
+
}, {
|
|
95
|
+
F: __dxlog_file,
|
|
96
|
+
L: 73
|
|
97
|
+
});
|
|
98
|
+
this._ctx.onDispose(this._signalManager.onMessage.on(async (message) => {
|
|
99
|
+
(0, import_log.log)("received message", {
|
|
100
|
+
from: message.author
|
|
101
|
+
}, {
|
|
102
|
+
F: __dxlog_file,
|
|
103
|
+
L: 78,
|
|
104
|
+
S: this,
|
|
105
|
+
C: (f, a) => f(...a)
|
|
106
|
+
});
|
|
107
|
+
await this._handleMessage(message);
|
|
108
|
+
}));
|
|
109
|
+
(0, import_async.scheduleTaskInterval)(this._ctx, async () => {
|
|
110
|
+
this._performGc();
|
|
111
|
+
}, RECEIVED_MESSAGES_GC_INTERVAL);
|
|
112
|
+
this._closed = false;
|
|
113
|
+
import_log.log.trace("dxos.mesh.messenger.open", import_protocols.trace.end({
|
|
114
|
+
id: traceId
|
|
115
|
+
}), {
|
|
116
|
+
F: __dxlog_file,
|
|
117
|
+
L: 93,
|
|
118
|
+
S: this,
|
|
119
|
+
C: (f, a) => f(...a)
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async close() {
|
|
123
|
+
if (this._closed) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
this._closed = true;
|
|
127
|
+
await this._ctx.dispose();
|
|
128
|
+
}
|
|
129
|
+
async sendMessage({ author, recipient, payload }) {
|
|
130
|
+
(0, import_invariant.invariant)(!this._closed, "Closed", {
|
|
131
|
+
F: __dxlog_file,
|
|
132
|
+
L: 105,
|
|
133
|
+
S: this,
|
|
134
|
+
A: [
|
|
135
|
+
"!this._closed",
|
|
136
|
+
"'Closed'"
|
|
137
|
+
]
|
|
138
|
+
});
|
|
139
|
+
const messageContext = this._ctx.derive();
|
|
140
|
+
const reliablePayload = {
|
|
141
|
+
messageId: import_keys.PublicKey.random(),
|
|
142
|
+
payload
|
|
143
|
+
};
|
|
144
|
+
(0, import_invariant.invariant)(!this._onAckCallbacks.has(reliablePayload.messageId), void 0, {
|
|
145
|
+
F: __dxlog_file,
|
|
146
|
+
L: 112,
|
|
147
|
+
S: this,
|
|
148
|
+
A: [
|
|
149
|
+
"!this._onAckCallbacks.has(reliablePayload.messageId!)",
|
|
150
|
+
""
|
|
151
|
+
]
|
|
152
|
+
});
|
|
153
|
+
(0, import_log.log)("send message", {
|
|
154
|
+
messageId: reliablePayload.messageId,
|
|
155
|
+
author,
|
|
156
|
+
recipient
|
|
157
|
+
}, {
|
|
158
|
+
F: __dxlog_file,
|
|
159
|
+
L: 113,
|
|
160
|
+
S: this,
|
|
161
|
+
C: (f, a) => f(...a)
|
|
162
|
+
});
|
|
163
|
+
let messageReceived;
|
|
164
|
+
let timeoutHit;
|
|
165
|
+
let sendAttempts = 0;
|
|
166
|
+
const promise = new Promise((resolve, reject) => {
|
|
167
|
+
messageReceived = resolve;
|
|
168
|
+
timeoutHit = reject;
|
|
169
|
+
});
|
|
170
|
+
(0, import_async.scheduleExponentialBackoffTaskInterval)(messageContext, async () => {
|
|
171
|
+
(0, import_log.log)("retrying message", {
|
|
172
|
+
messageId: reliablePayload.messageId
|
|
173
|
+
}, {
|
|
174
|
+
F: __dxlog_file,
|
|
175
|
+
L: 128,
|
|
176
|
+
S: this,
|
|
177
|
+
C: (f, a) => f(...a)
|
|
178
|
+
});
|
|
179
|
+
sendAttempts++;
|
|
180
|
+
await this._encodeAndSend({
|
|
181
|
+
author,
|
|
182
|
+
recipient,
|
|
183
|
+
reliablePayload
|
|
184
|
+
}).catch((err) => (0, import_log.log)("failed to send message", {
|
|
185
|
+
err
|
|
186
|
+
}, {
|
|
187
|
+
F: __dxlog_file,
|
|
188
|
+
L: 131,
|
|
189
|
+
S: this,
|
|
190
|
+
C: (f, a) => f(...a)
|
|
191
|
+
}));
|
|
192
|
+
}, this._retryDelay);
|
|
193
|
+
(0, import_async.scheduleTask)(messageContext, () => {
|
|
194
|
+
(0, import_log.log)("message not delivered", {
|
|
195
|
+
messageId: reliablePayload.messageId
|
|
196
|
+
}, {
|
|
197
|
+
F: __dxlog_file,
|
|
198
|
+
L: 140,
|
|
199
|
+
S: this,
|
|
200
|
+
C: (f, a) => f(...a)
|
|
201
|
+
});
|
|
202
|
+
this._onAckCallbacks.delete(reliablePayload.messageId);
|
|
203
|
+
timeoutHit(new import_protocols.TimeoutError("signaling message not delivered", new import_async.TimeoutError(MESSAGE_TIMEOUT, "Message not delivered")));
|
|
204
|
+
void messageContext.dispose();
|
|
205
|
+
this._monitor.recordReliableMessage({
|
|
206
|
+
sendAttempts,
|
|
207
|
+
sent: false
|
|
208
|
+
});
|
|
209
|
+
}, MESSAGE_TIMEOUT);
|
|
210
|
+
this._onAckCallbacks.set(reliablePayload.messageId, () => {
|
|
211
|
+
messageReceived();
|
|
212
|
+
this._onAckCallbacks.delete(reliablePayload.messageId);
|
|
213
|
+
void messageContext.dispose();
|
|
214
|
+
this._monitor.recordReliableMessage({
|
|
215
|
+
sendAttempts,
|
|
216
|
+
sent: true
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
await this._encodeAndSend({
|
|
220
|
+
author,
|
|
221
|
+
recipient,
|
|
222
|
+
reliablePayload
|
|
223
|
+
});
|
|
224
|
+
return promise;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Subscribes onMessage function to messages that contains payload with payloadType.
|
|
228
|
+
* @param payloadType if not specified, onMessage will be subscribed to all types of messages.
|
|
229
|
+
*/
|
|
230
|
+
async listen({ peer, payloadType, onMessage }) {
|
|
231
|
+
(0, import_invariant.invariant)(!this._closed, "Closed", {
|
|
232
|
+
F: __dxlog_file,
|
|
233
|
+
L: 178,
|
|
234
|
+
S: this,
|
|
235
|
+
A: [
|
|
236
|
+
"!this._closed",
|
|
237
|
+
"'Closed'"
|
|
238
|
+
]
|
|
239
|
+
});
|
|
240
|
+
await this._signalManager.subscribeMessages(peer);
|
|
241
|
+
let listeners;
|
|
242
|
+
(0, import_invariant.invariant)(peer.peerKey, "Peer key is required", {
|
|
243
|
+
F: __dxlog_file,
|
|
244
|
+
L: 182,
|
|
245
|
+
S: this,
|
|
246
|
+
A: [
|
|
247
|
+
"peer.peerKey",
|
|
248
|
+
"'Peer key is required'"
|
|
249
|
+
]
|
|
250
|
+
});
|
|
251
|
+
if (!payloadType) {
|
|
252
|
+
listeners = this._defaultListeners.get(peer.peerKey);
|
|
253
|
+
if (!listeners) {
|
|
254
|
+
listeners = /* @__PURE__ */ new Set();
|
|
255
|
+
this._defaultListeners.set(peer.peerKey, listeners);
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
listeners = this._listeners.get({
|
|
259
|
+
peerId: peer.peerKey,
|
|
260
|
+
payloadType
|
|
261
|
+
});
|
|
262
|
+
if (!listeners) {
|
|
263
|
+
listeners = /* @__PURE__ */ new Set();
|
|
264
|
+
this._listeners.set({
|
|
265
|
+
peerId: peer.peerKey,
|
|
266
|
+
payloadType
|
|
267
|
+
}, listeners);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
listeners.add(onMessage);
|
|
271
|
+
return {
|
|
272
|
+
unsubscribe: async () => {
|
|
273
|
+
listeners.delete(onMessage);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
async _encodeAndSend({ author, recipient, reliablePayload }) {
|
|
278
|
+
await this._signalManager.sendMessage({
|
|
279
|
+
author,
|
|
280
|
+
recipient,
|
|
281
|
+
payload: {
|
|
282
|
+
type_url: "dxos.mesh.messaging.ReliablePayload",
|
|
283
|
+
value: ReliablePayload.encode(reliablePayload, {
|
|
284
|
+
preserveAny: true
|
|
285
|
+
})
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
async _handleMessage(message) {
|
|
290
|
+
switch (message.payload.type_url) {
|
|
291
|
+
case "dxos.mesh.messaging.ReliablePayload": {
|
|
292
|
+
await this._handleReliablePayload(message);
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
case "dxos.mesh.messaging.Acknowledgement": {
|
|
296
|
+
await this._handleAcknowledgement({
|
|
297
|
+
payload: message.payload
|
|
298
|
+
});
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async _handleReliablePayload({ author, recipient, payload }) {
|
|
304
|
+
(0, import_invariant.invariant)(payload.type_url === "dxos.mesh.messaging.ReliablePayload", void 0, {
|
|
305
|
+
F: __dxlog_file,
|
|
306
|
+
L: 240,
|
|
307
|
+
S: this,
|
|
308
|
+
A: [
|
|
309
|
+
"payload.type_url === 'dxos.mesh.messaging.ReliablePayload'",
|
|
310
|
+
""
|
|
311
|
+
]
|
|
312
|
+
});
|
|
313
|
+
const reliablePayload = ReliablePayload.decode(payload.value, {
|
|
314
|
+
preserveAny: true
|
|
315
|
+
});
|
|
316
|
+
(0, import_log.log)("handling message", {
|
|
317
|
+
messageId: reliablePayload.messageId
|
|
318
|
+
}, {
|
|
319
|
+
F: __dxlog_file,
|
|
320
|
+
L: 243,
|
|
321
|
+
S: this,
|
|
322
|
+
C: (f, a) => f(...a)
|
|
323
|
+
});
|
|
324
|
+
try {
|
|
325
|
+
await this._sendAcknowledgement({
|
|
326
|
+
author,
|
|
327
|
+
recipient,
|
|
328
|
+
messageId: reliablePayload.messageId
|
|
329
|
+
});
|
|
330
|
+
} catch (err) {
|
|
331
|
+
this._monitor.recordMessageAckFailed();
|
|
332
|
+
throw err;
|
|
333
|
+
}
|
|
334
|
+
if (this._receivedMessages.has(reliablePayload.messageId)) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
this._receivedMessages.add(reliablePayload.messageId);
|
|
338
|
+
await this._callListeners({
|
|
339
|
+
author,
|
|
340
|
+
recipient,
|
|
341
|
+
payload: reliablePayload.payload
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
async _handleAcknowledgement({ payload }) {
|
|
345
|
+
(0, import_invariant.invariant)(payload.type_url === "dxos.mesh.messaging.Acknowledgement", void 0, {
|
|
346
|
+
F: __dxlog_file,
|
|
347
|
+
L: 271,
|
|
348
|
+
S: this,
|
|
349
|
+
A: [
|
|
350
|
+
"payload.type_url === 'dxos.mesh.messaging.Acknowledgement'",
|
|
351
|
+
""
|
|
352
|
+
]
|
|
353
|
+
});
|
|
354
|
+
this._onAckCallbacks.get(Acknowledgement.decode(payload.value).messageId)?.();
|
|
355
|
+
}
|
|
356
|
+
async _sendAcknowledgement({ author, recipient, messageId }) {
|
|
357
|
+
(0, import_log.log)("sending ACK", {
|
|
358
|
+
messageId,
|
|
359
|
+
from: recipient,
|
|
360
|
+
to: author
|
|
361
|
+
}, {
|
|
362
|
+
F: __dxlog_file,
|
|
363
|
+
L: 284,
|
|
364
|
+
S: this,
|
|
365
|
+
C: (f, a) => f(...a)
|
|
366
|
+
});
|
|
367
|
+
await this._signalManager.sendMessage({
|
|
368
|
+
author: recipient,
|
|
369
|
+
recipient: author,
|
|
370
|
+
payload: {
|
|
371
|
+
type_url: "dxos.mesh.messaging.Acknowledgement",
|
|
372
|
+
value: Acknowledgement.encode({
|
|
373
|
+
messageId
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
async _callListeners(message) {
|
|
379
|
+
{
|
|
380
|
+
(0, import_invariant.invariant)(message.recipient.peerKey, "Peer key is required", {
|
|
381
|
+
F: __dxlog_file,
|
|
382
|
+
L: 298,
|
|
383
|
+
S: this,
|
|
384
|
+
A: [
|
|
385
|
+
"message.recipient.peerKey",
|
|
386
|
+
"'Peer key is required'"
|
|
387
|
+
]
|
|
388
|
+
});
|
|
389
|
+
const defaultListenerMap = this._defaultListeners.get(message.recipient.peerKey);
|
|
390
|
+
if (defaultListenerMap) {
|
|
391
|
+
for (const listener of defaultListenerMap) {
|
|
392
|
+
await listener(message);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
{
|
|
397
|
+
const listenerMap = this._listeners.get({
|
|
398
|
+
peerId: message.recipient.peerKey,
|
|
399
|
+
payloadType: message.payload.type_url
|
|
400
|
+
});
|
|
401
|
+
if (listenerMap) {
|
|
402
|
+
for (const listener of listenerMap) {
|
|
403
|
+
await listener(message);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
_performGc() {
|
|
409
|
+
const start = performance.now();
|
|
410
|
+
for (const key of this._toClear.keys()) {
|
|
411
|
+
this._receivedMessages.delete(key);
|
|
412
|
+
}
|
|
413
|
+
this._toClear.clear();
|
|
414
|
+
for (const key of this._receivedMessages.keys()) {
|
|
415
|
+
this._toClear.add(key);
|
|
416
|
+
}
|
|
417
|
+
const elapsed = performance.now() - start;
|
|
418
|
+
if (elapsed > 100) {
|
|
419
|
+
import_log.log.warn("GC took too long", {
|
|
420
|
+
elapsed
|
|
421
|
+
}, {
|
|
422
|
+
F: __dxlog_file,
|
|
423
|
+
L: 333,
|
|
424
|
+
S: this,
|
|
425
|
+
C: (f, a) => f(...a)
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
var PeerInfoHash = ({ peerKey }) => peerKey;
|
|
431
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-manager/memory-signal-manager.ts";
|
|
432
|
+
var MemorySignalManagerContext = class {
|
|
433
|
+
constructor() {
|
|
434
|
+
this.swarmEvent = new import_async2.Event();
|
|
435
|
+
this.swarms = new import_util2.ComplexMap(import_keys2.PublicKey.hash);
|
|
436
|
+
this.connections = new import_util2.ComplexMap(PeerInfoHash);
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
var MemorySignalManager = class {
|
|
440
|
+
constructor(_context) {
|
|
441
|
+
this._context = _context;
|
|
442
|
+
this.statusChanged = new import_async2.Event();
|
|
443
|
+
this.swarmEvent = new import_async2.Event();
|
|
444
|
+
this.onMessage = new import_async2.Event();
|
|
445
|
+
this._joinedSwarms = new import_util2.ComplexSet(({ topic, peer }) => topic.toHex() + peer.peerKey);
|
|
446
|
+
this._freezeTrigger = new import_async2.Trigger().wake();
|
|
447
|
+
this._ctx = new import_context2.Context(void 0, {
|
|
448
|
+
F: __dxlog_file2,
|
|
449
|
+
L: 51
|
|
450
|
+
});
|
|
451
|
+
this._ctx.onDispose(this._context.swarmEvent.on((data) => this.swarmEvent.emit(data)));
|
|
452
|
+
}
|
|
453
|
+
async open() {
|
|
454
|
+
if (!this._ctx.disposed) {
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
this._ctx = new import_context2.Context(void 0, {
|
|
458
|
+
F: __dxlog_file2,
|
|
459
|
+
L: 60
|
|
460
|
+
});
|
|
461
|
+
this._ctx.onDispose(this._context.swarmEvent.on((data) => this.swarmEvent.emit(data)));
|
|
462
|
+
await Promise.all([
|
|
463
|
+
...this._joinedSwarms.values()
|
|
464
|
+
].map((value) => this.join(value)));
|
|
465
|
+
}
|
|
466
|
+
async close() {
|
|
467
|
+
if (this._ctx.disposed) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const joinedSwarmsCopy = new import_util2.ComplexSet(({ topic, peer }) => topic.toHex() + peer.peerKey, [
|
|
471
|
+
...this._joinedSwarms.values()
|
|
472
|
+
]);
|
|
473
|
+
await Promise.all([
|
|
474
|
+
...this._joinedSwarms.values()
|
|
475
|
+
].map((value) => this.leave(value)));
|
|
476
|
+
this._joinedSwarms = joinedSwarmsCopy;
|
|
477
|
+
await this._ctx.dispose();
|
|
478
|
+
}
|
|
479
|
+
getStatus() {
|
|
480
|
+
return [];
|
|
481
|
+
}
|
|
482
|
+
async join({ topic, peer }) {
|
|
483
|
+
(0, import_invariant2.invariant)(!this._ctx.disposed, "Closed", {
|
|
484
|
+
F: __dxlog_file2,
|
|
485
|
+
L: 89,
|
|
486
|
+
S: this,
|
|
487
|
+
A: [
|
|
488
|
+
"!this._ctx.disposed",
|
|
489
|
+
"'Closed'"
|
|
490
|
+
]
|
|
491
|
+
});
|
|
492
|
+
this._joinedSwarms.add({
|
|
493
|
+
topic,
|
|
494
|
+
peer
|
|
495
|
+
});
|
|
496
|
+
if (!this._context.swarms.has(topic)) {
|
|
497
|
+
this._context.swarms.set(topic, new import_util2.ComplexSet(PeerInfoHash));
|
|
498
|
+
}
|
|
499
|
+
this._context.swarms.get(topic).add(peer);
|
|
500
|
+
this._context.swarmEvent.emit({
|
|
501
|
+
topic,
|
|
502
|
+
peerAvailable: {
|
|
503
|
+
peer,
|
|
504
|
+
since: /* @__PURE__ */ new Date()
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
for (const [topic2, peers] of this._context.swarms) {
|
|
508
|
+
Array.from(peers).forEach((peer2) => {
|
|
509
|
+
this.swarmEvent.emit({
|
|
510
|
+
topic: topic2,
|
|
511
|
+
peerAvailable: {
|
|
512
|
+
peer: peer2,
|
|
513
|
+
since: /* @__PURE__ */ new Date()
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
async leave({ topic, peer }) {
|
|
520
|
+
(0, import_invariant2.invariant)(!this._ctx.disposed, "Closed", {
|
|
521
|
+
F: __dxlog_file2,
|
|
522
|
+
L: 121,
|
|
523
|
+
S: this,
|
|
524
|
+
A: [
|
|
525
|
+
"!this._ctx.disposed",
|
|
526
|
+
"'Closed'"
|
|
527
|
+
]
|
|
528
|
+
});
|
|
529
|
+
this._joinedSwarms.delete({
|
|
530
|
+
topic,
|
|
531
|
+
peer
|
|
532
|
+
});
|
|
533
|
+
if (!this._context.swarms.has(topic)) {
|
|
534
|
+
this._context.swarms.set(topic, new import_util2.ComplexSet(PeerInfoHash));
|
|
535
|
+
}
|
|
536
|
+
this._context.swarms.get(topic).delete(peer);
|
|
537
|
+
const swarmEvent = {
|
|
538
|
+
topic,
|
|
539
|
+
peerLeft: {
|
|
540
|
+
peer
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
this._context.swarmEvent.emit(swarmEvent);
|
|
544
|
+
}
|
|
545
|
+
async sendMessage({ author, recipient, payload }) {
|
|
546
|
+
(0, import_log2.log)("send message", {
|
|
547
|
+
author,
|
|
548
|
+
recipient,
|
|
549
|
+
...dec(payload)
|
|
550
|
+
}, {
|
|
551
|
+
F: __dxlog_file2,
|
|
552
|
+
L: 142,
|
|
553
|
+
S: this,
|
|
554
|
+
C: (f, a) => f(...a)
|
|
555
|
+
});
|
|
556
|
+
(0, import_invariant2.invariant)(recipient, void 0, {
|
|
557
|
+
F: __dxlog_file2,
|
|
558
|
+
L: 144,
|
|
559
|
+
S: this,
|
|
560
|
+
A: [
|
|
561
|
+
"recipient",
|
|
562
|
+
""
|
|
563
|
+
]
|
|
564
|
+
});
|
|
565
|
+
(0, import_invariant2.invariant)(!this._ctx.disposed, "Closed", {
|
|
566
|
+
F: __dxlog_file2,
|
|
567
|
+
L: 145,
|
|
568
|
+
S: this,
|
|
569
|
+
A: [
|
|
570
|
+
"!this._ctx.disposed",
|
|
571
|
+
"'Closed'"
|
|
572
|
+
]
|
|
573
|
+
});
|
|
574
|
+
await this._freezeTrigger.wait();
|
|
575
|
+
const remote = this._context.connections.get(recipient);
|
|
576
|
+
if (!remote) {
|
|
577
|
+
import_log2.log.warn("recipient is not subscribed for messages", {
|
|
578
|
+
author,
|
|
579
|
+
recipient
|
|
580
|
+
}, {
|
|
581
|
+
F: __dxlog_file2,
|
|
582
|
+
L: 151,
|
|
583
|
+
S: this,
|
|
584
|
+
C: (f, a) => f(...a)
|
|
585
|
+
});
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
if (remote._ctx.disposed) {
|
|
589
|
+
import_log2.log.warn("recipient is disposed", {
|
|
590
|
+
author,
|
|
591
|
+
recipient
|
|
592
|
+
}, {
|
|
593
|
+
F: __dxlog_file2,
|
|
594
|
+
L: 156,
|
|
595
|
+
S: this,
|
|
596
|
+
C: (f, a) => f(...a)
|
|
597
|
+
});
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
remote._freezeTrigger.wait().then(() => {
|
|
601
|
+
if (remote._ctx.disposed) {
|
|
602
|
+
import_log2.log.warn("recipient is disposed", {
|
|
603
|
+
author,
|
|
604
|
+
recipient
|
|
605
|
+
}, {
|
|
606
|
+
F: __dxlog_file2,
|
|
607
|
+
L: 164,
|
|
608
|
+
S: this,
|
|
609
|
+
C: (f, a) => f(...a)
|
|
610
|
+
});
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
(0, import_log2.log)("receive message", {
|
|
614
|
+
author,
|
|
615
|
+
recipient,
|
|
616
|
+
...dec(payload)
|
|
617
|
+
}, {
|
|
618
|
+
F: __dxlog_file2,
|
|
619
|
+
L: 168,
|
|
620
|
+
S: this,
|
|
621
|
+
C: (f, a) => f(...a)
|
|
622
|
+
});
|
|
623
|
+
remote.onMessage.emit({
|
|
624
|
+
author,
|
|
625
|
+
recipient,
|
|
626
|
+
payload
|
|
627
|
+
});
|
|
628
|
+
}).catch((err) => {
|
|
629
|
+
import_log2.log.error("error while waiting for freeze", {
|
|
630
|
+
err
|
|
631
|
+
}, {
|
|
632
|
+
F: __dxlog_file2,
|
|
633
|
+
L: 173,
|
|
634
|
+
S: this,
|
|
635
|
+
C: (f, a) => f(...a)
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
async subscribeMessages(peerInfo) {
|
|
640
|
+
(0, import_log2.log)("subscribing", {
|
|
641
|
+
peerInfo
|
|
642
|
+
}, {
|
|
643
|
+
F: __dxlog_file2,
|
|
644
|
+
L: 178,
|
|
645
|
+
S: this,
|
|
646
|
+
C: (f, a) => f(...a)
|
|
647
|
+
});
|
|
648
|
+
this._context.connections.set(peerInfo, this);
|
|
649
|
+
}
|
|
650
|
+
async unsubscribeMessages(peerInfo) {
|
|
651
|
+
(0, import_log2.log)("unsubscribing", {
|
|
652
|
+
peerInfo
|
|
653
|
+
}, {
|
|
654
|
+
F: __dxlog_file2,
|
|
655
|
+
L: 183,
|
|
656
|
+
S: this,
|
|
657
|
+
C: (f, a) => f(...a)
|
|
658
|
+
});
|
|
659
|
+
this._context.connections.delete(peerInfo);
|
|
660
|
+
}
|
|
661
|
+
freeze() {
|
|
662
|
+
this._freezeTrigger.reset();
|
|
663
|
+
}
|
|
664
|
+
unfreeze() {
|
|
665
|
+
this._freezeTrigger.wake();
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
var dec = (payload) => {
|
|
669
|
+
if (!payload.type_url.endsWith("ReliablePayload")) {
|
|
670
|
+
return {};
|
|
671
|
+
}
|
|
672
|
+
const relPayload = import_proto2.schema.getCodecForType("dxos.mesh.messaging.ReliablePayload").decode(payload.value);
|
|
673
|
+
if (typeof relPayload?.payload?.data === "object") {
|
|
674
|
+
return {
|
|
675
|
+
payload: Object.keys(relPayload?.payload?.data)[0],
|
|
676
|
+
sessionId: relPayload?.payload?.sessionId
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
return {};
|
|
680
|
+
};
|
|
681
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
682
|
+
0 && (module.exports = {
|
|
683
|
+
MemorySignalManager,
|
|
684
|
+
MemorySignalManagerContext,
|
|
685
|
+
Messenger,
|
|
686
|
+
PeerInfoHash
|
|
687
|
+
});
|
|
688
|
+
//# sourceMappingURL=chunk-WDA7H2PO.cjs.map
|