@dxos/messaging 0.1.58-main.c4d3e15 → 0.1.58-main.ca7a0bc

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.
@@ -41,7 +41,6 @@ module.exports = __toCommonJS(src_exports);
41
41
  // packages/core/mesh/messaging/src/messenger.ts
42
42
  var import_async = require("@dxos/async");
43
43
  var import_context = require("@dxos/context");
44
- var import_errors = require("@dxos/errors");
45
44
  var import_invariant = require("@dxos/invariant");
46
45
  var import_keys = require("@dxos/keys");
47
46
  var import_log = require("@dxos/log");
@@ -57,6 +56,7 @@ var ReliablePayload = import_protocols.schema.getCodecForType("dxos.mesh.messagi
57
56
  var Acknowledgement = import_protocols.schema.getCodecForType("dxos.mesh.messaging.Acknowledgement");
58
57
  var ERROR_LIMIT = 5;
59
58
  var NETWORK_REBOOT_DELAY = 3e3;
59
+ var RECEIVED_MESSAGES_GC_INTERVAL = 12e4;
60
60
  var Messenger = class {
61
61
  constructor({ signalManager, retryDelay = 300 }) {
62
62
  // { peerId, payloadType } => listeners set
@@ -64,7 +64,11 @@ var Messenger = class {
64
64
  // peerId => listeners set
65
65
  this._defaultListeners = new import_util.ComplexMap(import_keys.PublicKey.hash);
66
66
  this._onAckCallbacks = new import_util.ComplexMap(import_keys.PublicKey.hash);
67
- this._receivedMessages = new import_util.ComplexSet((key) => key.toHex());
67
+ this._receivedMessages = new import_util.ComplexSet(import_keys.PublicKey.hash);
68
+ /**
69
+ * Keys scheduled to be cleared from _receivedMessages on the next iteration.
70
+ */
71
+ this._toClear = new import_util.ComplexSet(import_keys.PublicKey.hash);
68
72
  this._closed = true;
69
73
  this._errorCount = 0;
70
74
  this._rebooting = false;
@@ -81,14 +85,14 @@ var Messenger = class {
81
85
  id: traceId
82
86
  }), {
83
87
  F: __dxlog_file,
84
- L: 68,
88
+ L: 74,
85
89
  S: this,
86
90
  C: (f, a) => f(...a)
87
91
  });
88
92
  this._ctx = new import_context.Context({
89
93
  onError: (err) => import_log.log.catch(err, void 0, {
90
94
  F: __dxlog_file,
91
- L: 70,
95
+ L: 76,
92
96
  S: this,
93
97
  C: (f, a) => f(...a)
94
98
  })
@@ -98,18 +102,21 @@ var Messenger = class {
98
102
  from: message.author
99
103
  }, {
100
104
  F: __dxlog_file,
101
- L: 74,
105
+ L: 80,
102
106
  S: this,
103
107
  C: (f, a) => f(...a)
104
108
  });
105
109
  await this._handleMessage(message);
106
110
  }));
111
+ (0, import_async.scheduleTaskInterval)(this._ctx, async () => {
112
+ this._performGc();
113
+ }, RECEIVED_MESSAGES_GC_INTERVAL);
107
114
  this._closed = false;
108
115
  import_log.log.trace("dxos.mesh.messenger.open", import_protocols.trace.end({
109
116
  id: traceId
110
117
  }), {
111
118
  F: __dxlog_file,
112
- L: 79,
119
+ L: 95,
113
120
  S: this,
114
121
  C: (f, a) => f(...a)
115
122
  });
@@ -124,7 +131,7 @@ var Messenger = class {
124
131
  async sendMessage({ author, recipient, payload }) {
125
132
  (0, import_invariant.invariant)(!this._closed, "Closed", {
126
133
  F: __dxlog_file,
127
- L: 91,
134
+ L: 107,
128
135
  S: this,
129
136
  A: [
130
137
  "!this._closed",
@@ -138,7 +145,7 @@ var Messenger = class {
138
145
  };
139
146
  (0, import_invariant.invariant)(!this._onAckCallbacks.has(reliablePayload.messageId), void 0, {
140
147
  F: __dxlog_file,
141
- L: 98,
148
+ L: 114,
142
149
  S: this,
143
150
  A: [
144
151
  "!this._onAckCallbacks.has(reliablePayload.messageId!)",
@@ -151,7 +158,7 @@ var Messenger = class {
151
158
  recipient
152
159
  }, {
153
160
  F: __dxlog_file,
154
- L: 99,
161
+ L: 115,
155
162
  S: this,
156
163
  C: (f, a) => f(...a)
157
164
  });
@@ -166,7 +173,7 @@ var Messenger = class {
166
173
  messageId: reliablePayload.messageId
167
174
  }, {
168
175
  F: __dxlog_file,
169
- L: 113,
176
+ L: 129,
170
177
  S: this,
171
178
  C: (f, a) => f(...a)
172
179
  });
@@ -178,7 +185,7 @@ var Messenger = class {
178
185
  err
179
186
  }, {
180
187
  F: __dxlog_file,
181
- L: 115,
188
+ L: 131,
182
189
  S: this,
183
190
  C: (f, a) => f(...a)
184
191
  }));
@@ -188,13 +195,13 @@ var Messenger = class {
188
195
  messageId: reliablePayload.messageId
189
196
  }, {
190
197
  F: __dxlog_file,
191
- L: 124,
198
+ L: 140,
192
199
  S: this,
193
200
  C: (f, a) => f(...a)
194
201
  });
195
202
  this._onAckCallbacks.delete(reliablePayload.messageId);
196
203
  await this.errorLimitCheck();
197
- timeoutHit(new import_errors.TimeoutError("signaling message not delivered", new import_async.TimeoutError(MESSAGE_TIMEOUT, "Message not delivered")));
204
+ timeoutHit(new import_protocols.TimeoutError("signaling message not delivered", new import_async.TimeoutError(MESSAGE_TIMEOUT, "Message not delivered")));
198
205
  void messageContext.dispose();
199
206
  }, MESSAGE_TIMEOUT);
200
207
  this._onAckCallbacks.set(reliablePayload.messageId, () => {
@@ -212,7 +219,7 @@ var Messenger = class {
212
219
  async errorLimitCheck() {
213
220
  (0, import_log.log)(`checking error limit ${this._errorCount}`, void 0, {
214
221
  F: __dxlog_file,
215
- L: 149,
222
+ L: 165,
216
223
  S: this,
217
224
  C: (f, a) => f(...a)
218
225
  });
@@ -227,7 +234,7 @@ var Messenger = class {
227
234
  this._rebooting = true;
228
235
  (0, import_log.log)("rebooting Messenger/SignalManager", void 0, {
229
236
  F: __dxlog_file,
230
- L: 161,
237
+ L: 177,
231
238
  S: this,
232
239
  C: (f, a) => f(...a)
233
240
  });
@@ -235,14 +242,14 @@ var Messenger = class {
235
242
  await this._signalManager.close();
236
243
  (0, import_log.log)("pausing", void 0, {
237
244
  F: __dxlog_file,
238
- L: 164,
245
+ L: 180,
239
246
  S: this,
240
247
  C: (f, a) => f(...a)
241
248
  });
242
249
  await new Promise((resolve) => setTimeout(resolve, NETWORK_REBOOT_DELAY));
243
250
  (0, import_log.log)("done pausing", void 0, {
244
251
  F: __dxlog_file,
245
- L: 166,
252
+ L: 182,
246
253
  S: this,
247
254
  C: (f, a) => f(...a)
248
255
  });
@@ -250,7 +257,7 @@ var Messenger = class {
250
257
  await this._signalManager.open();
251
258
  (0, import_log.log)("done rebooting", void 0, {
252
259
  F: __dxlog_file,
253
- L: 169,
260
+ L: 185,
254
261
  S: this,
255
262
  C: (f, a) => f(...a)
256
263
  });
@@ -264,7 +271,7 @@ var Messenger = class {
264
271
  async listen({ peerId, payloadType, onMessage }) {
265
272
  (0, import_invariant.invariant)(!this._closed, "Closed", {
266
273
  F: __dxlog_file,
267
- L: 188,
274
+ L: 204,
268
275
  S: this,
269
276
  A: [
270
277
  "!this._closed",
@@ -328,7 +335,7 @@ var Messenger = class {
328
335
  async _handleReliablePayload({ author, recipient, payload }) {
329
336
  (0, import_invariant.invariant)(payload.type_url === "dxos.mesh.messaging.ReliablePayload", void 0, {
330
337
  F: __dxlog_file,
331
- L: 249,
338
+ L: 265,
332
339
  S: this,
333
340
  A: [
334
341
  "payload.type_url === 'dxos.mesh.messaging.ReliablePayload'",
@@ -342,7 +349,7 @@ var Messenger = class {
342
349
  messageId: reliablePayload.messageId
343
350
  }, {
344
351
  F: __dxlog_file,
345
- L: 252,
352
+ L: 268,
346
353
  S: this,
347
354
  C: (f, a) => f(...a)
348
355
  });
@@ -364,7 +371,7 @@ var Messenger = class {
364
371
  async _handleAcknowledgement({ payload }) {
365
372
  (0, import_invariant.invariant)(payload.type_url === "dxos.mesh.messaging.Acknowledgement", void 0, {
366
373
  F: __dxlog_file,
367
- L: 274,
374
+ L: 290,
368
375
  S: this,
369
376
  A: [
370
377
  "payload.type_url === 'dxos.mesh.messaging.Acknowledgement'",
@@ -380,7 +387,7 @@ var Messenger = class {
380
387
  to: author
381
388
  }, {
382
389
  F: __dxlog_file,
383
- L: 287,
390
+ L: 303,
384
391
  S: this,
385
392
  C: (f, a) => f(...a)
386
393
  });
@@ -416,6 +423,27 @@ var Messenger = class {
416
423
  }
417
424
  }
418
425
  }
426
+ _performGc() {
427
+ const start = performance.now();
428
+ for (const key of this._toClear.keys()) {
429
+ this._receivedMessages.delete(key);
430
+ }
431
+ this._toClear.clear();
432
+ for (const key of this._receivedMessages.keys()) {
433
+ this._toClear.add(key);
434
+ }
435
+ const elapsed = performance.now() - start;
436
+ if (elapsed > 100) {
437
+ import_log.log.warn("GC took too long", {
438
+ elapsed
439
+ }, {
440
+ F: __dxlog_file,
441
+ L: 351,
442
+ S: this,
443
+ C: (f, a) => f(...a)
444
+ });
445
+ }
446
+ }
419
447
  };
420
448
 
421
449
  // packages/core/mesh/messaging/src/signal-client/signal-client.ts
@@ -1150,7 +1178,6 @@ var MemorySignalManagerContext = class {
1150
1178
  }
1151
1179
  };
1152
1180
  var MemorySignalManager = class {
1153
- // prettier-ignore
1154
1181
  constructor(_context) {
1155
1182
  this._context = _context;
1156
1183
  this.statusChanged = new import_async4.Event();
@@ -1191,7 +1218,7 @@ var MemorySignalManager = class {
1191
1218
  async join({ topic, peerId }) {
1192
1219
  (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1193
1220
  F: __dxlog_file4,
1194
- L: 104,
1221
+ L: 101,
1195
1222
  S: this,
1196
1223
  A: [
1197
1224
  "!this._ctx.disposed",
@@ -1232,7 +1259,7 @@ var MemorySignalManager = class {
1232
1259
  async leave({ topic, peerId }) {
1233
1260
  (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1234
1261
  F: __dxlog_file4,
1235
- L: 140,
1262
+ L: 137,
1236
1263
  S: this,
1237
1264
  A: [
1238
1265
  "!this._ctx.disposed",
@@ -1264,13 +1291,13 @@ var MemorySignalManager = class {
1264
1291
  ...dec(payload)
1265
1292
  }, {
1266
1293
  F: __dxlog_file4,
1267
- L: 160,
1294
+ L: 157,
1268
1295
  S: this,
1269
1296
  C: (f, a) => f(...a)
1270
1297
  });
1271
1298
  (0, import_invariant4.invariant)(recipient, void 0, {
1272
1299
  F: __dxlog_file4,
1273
- L: 162,
1300
+ L: 159,
1274
1301
  S: this,
1275
1302
  A: [
1276
1303
  "recipient",
@@ -1279,7 +1306,7 @@ var MemorySignalManager = class {
1279
1306
  });
1280
1307
  (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1281
1308
  F: __dxlog_file4,
1282
- L: 163,
1309
+ L: 160,
1283
1310
  S: this,
1284
1311
  A: [
1285
1312
  "!this._ctx.disposed",
@@ -1294,7 +1321,7 @@ var MemorySignalManager = class {
1294
1321
  recipient
1295
1322
  }, {
1296
1323
  F: __dxlog_file4,
1297
- L: 169,
1324
+ L: 166,
1298
1325
  S: this,
1299
1326
  C: (f, a) => f(...a)
1300
1327
  });
@@ -1306,7 +1333,7 @@ var MemorySignalManager = class {
1306
1333
  recipient
1307
1334
  }, {
1308
1335
  F: __dxlog_file4,
1309
- L: 174,
1336
+ L: 171,
1310
1337
  S: this,
1311
1338
  C: (f, a) => f(...a)
1312
1339
  });
@@ -1319,7 +1346,7 @@ var MemorySignalManager = class {
1319
1346
  recipient
1320
1347
  }, {
1321
1348
  F: __dxlog_file4,
1322
- L: 182,
1349
+ L: 179,
1323
1350
  S: this,
1324
1351
  C: (f, a) => f(...a)
1325
1352
  });
@@ -1331,7 +1358,7 @@ var MemorySignalManager = class {
1331
1358
  ...dec(payload)
1332
1359
  }, {
1333
1360
  F: __dxlog_file4,
1334
- L: 186,
1361
+ L: 183,
1335
1362
  S: this,
1336
1363
  C: (f, a) => f(...a)
1337
1364
  });
@@ -1345,7 +1372,7 @@ var MemorySignalManager = class {
1345
1372
  err
1346
1373
  }, {
1347
1374
  F: __dxlog_file4,
1348
- L: 191,
1375
+ L: 188,
1349
1376
  S: this,
1350
1377
  C: (f, a) => f(...a)
1351
1378
  });
@@ -1356,7 +1383,7 @@ var MemorySignalManager = class {
1356
1383
  peerId
1357
1384
  }, {
1358
1385
  F: __dxlog_file4,
1359
- L: 196,
1386
+ L: 193,
1360
1387
  S: this,
1361
1388
  C: (f, a) => f(...a)
1362
1389
  });
@@ -1367,7 +1394,7 @@ var MemorySignalManager = class {
1367
1394
  peerId
1368
1395
  }, {
1369
1396
  F: __dxlog_file4,
1370
- L: 201,
1397
+ L: 198,
1371
1398
  S: this,
1372
1399
  C: (f, a) => f(...a)
1373
1400
  });
@@ -1413,7 +1440,6 @@ function _ts_decorate(decorators, target, key, desc) {
1413
1440
  }
1414
1441
  var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-manager/websocket-signal-manager.ts";
1415
1442
  var WebsocketSignalManager = class {
1416
- // prettier-ignore
1417
1443
  constructor(_hosts) {
1418
1444
  this._hosts = _hosts;
1419
1445
  this._servers = /* @__PURE__ */ new Map();
@@ -1427,7 +1453,7 @@ var WebsocketSignalManager = class {
1427
1453
  hosts: this._hosts
1428
1454
  }, {
1429
1455
  F: __dxlog_file5,
1430
- L: 46,
1456
+ L: 43,
1431
1457
  S: this,
1432
1458
  C: (f, a) => f(...a)
1433
1459
  });
@@ -1449,7 +1475,7 @@ var WebsocketSignalManager = class {
1449
1475
  hosts: this._hosts
1450
1476
  }, {
1451
1477
  F: __dxlog_file5,
1452
- L: 68,
1478
+ L: 65,
1453
1479
  S: this,
1454
1480
  C: (f, a) => f(...a)
1455
1481
  });
@@ -1457,7 +1483,7 @@ var WebsocketSignalManager = class {
1457
1483
  id: this._instanceId
1458
1484
  }), {
1459
1485
  F: __dxlog_file5,
1460
- L: 69,
1486
+ L: 66,
1461
1487
  S: this,
1462
1488
  C: (f, a) => f(...a)
1463
1489
  });
@@ -1470,7 +1496,7 @@ var WebsocketSignalManager = class {
1470
1496
  id: this._instanceId
1471
1497
  }), {
1472
1498
  F: __dxlog_file5,
1473
- L: 76,
1499
+ L: 73,
1474
1500
  S: this,
1475
1501
  C: (f, a) => f(...a)
1476
1502
  });
@@ -1492,13 +1518,13 @@ var WebsocketSignalManager = class {
1492
1518
  peerId
1493
1519
  }, {
1494
1520
  F: __dxlog_file5,
1495
- L: 96,
1521
+ L: 93,
1496
1522
  S: this,
1497
1523
  C: (f, a) => f(...a)
1498
1524
  });
1499
1525
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1500
1526
  F: __dxlog_file5,
1501
- L: 97,
1527
+ L: 94,
1502
1528
  S: this,
1503
1529
  A: [
1504
1530
  "this._opened",
@@ -1516,13 +1542,13 @@ var WebsocketSignalManager = class {
1516
1542
  peerId
1517
1543
  }, {
1518
1544
  F: __dxlog_file5,
1519
- L: 103,
1545
+ L: 100,
1520
1546
  S: this,
1521
1547
  C: (f, a) => f(...a)
1522
1548
  });
1523
1549
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1524
1550
  F: __dxlog_file5,
1525
- L: 104,
1551
+ L: 101,
1526
1552
  S: this,
1527
1553
  A: [
1528
1554
  "this._opened",
@@ -1537,13 +1563,13 @@ var WebsocketSignalManager = class {
1537
1563
  async sendMessage({ author, recipient, payload }) {
1538
1564
  (0, import_log5.log)(`Signal ${recipient}`, void 0, {
1539
1565
  F: __dxlog_file5,
1540
- L: 118,
1566
+ L: 115,
1541
1567
  S: this,
1542
1568
  C: (f, a) => f(...a)
1543
1569
  });
1544
1570
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1545
1571
  F: __dxlog_file5,
1546
- L: 119,
1572
+ L: 116,
1547
1573
  S: this,
1548
1574
  A: [
1549
1575
  "this._opened",
@@ -1557,7 +1583,7 @@ var WebsocketSignalManager = class {
1557
1583
  payload
1558
1584
  }).catch((err) => (0, import_log5.log)(err, void 0, {
1559
1585
  F: __dxlog_file5,
1560
- L: 122,
1586
+ L: 119,
1561
1587
  S: this,
1562
1588
  C: (f, a) => f(...a)
1563
1589
  }));
@@ -1566,13 +1592,13 @@ var WebsocketSignalManager = class {
1566
1592
  async subscribeMessages(peerId) {
1567
1593
  (0, import_log5.log)(`Subscribed for message stream peerId=${peerId}`, void 0, {
1568
1594
  F: __dxlog_file5,
1569
- L: 127,
1595
+ L: 124,
1570
1596
  S: this,
1571
1597
  C: (f, a) => f(...a)
1572
1598
  });
1573
1599
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1574
1600
  F: __dxlog_file5,
1575
- L: 128,
1601
+ L: 125,
1576
1602
  S: this,
1577
1603
  A: [
1578
1604
  "this._opened",
@@ -1584,13 +1610,13 @@ var WebsocketSignalManager = class {
1584
1610
  async unsubscribeMessages(peerId) {
1585
1611
  (0, import_log5.log)(`Subscribed for message stream peerId=${peerId}`, void 0, {
1586
1612
  F: __dxlog_file5,
1587
- L: 134,
1613
+ L: 131,
1588
1614
  S: this,
1589
1615
  C: (f, a) => f(...a)
1590
1616
  });
1591
1617
  (0, import_invariant5.invariant)(this._opened, "Closed", {
1592
1618
  F: __dxlog_file5,
1593
- L: 135,
1619
+ L: 132,
1594
1620
  S: this,
1595
1621
  A: [
1596
1622
  "this._opened",
@@ -1603,7 +1629,7 @@ var WebsocketSignalManager = class {
1603
1629
  this._ctx = new import_context4.Context({
1604
1630
  onError: (err) => import_log5.log.catch(err, void 0, {
1605
1631
  F: __dxlog_file5,
1606
- L: 142,
1632
+ L: 139,
1607
1633
  S: this,
1608
1634
  C: (f, a) => f(...a)
1609
1635
  })