@dxos/echo-pipeline 0.6.1-main.c52e5db → 0.6.1-main.d9fdca4

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.
@@ -40,7 +40,7 @@ import { trace } from "@dxos/tracing";
40
40
  import { mapValues } from "@dxos/util";
41
41
 
42
42
  // packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts
43
- import { Trigger, synchronized } from "@dxos/async";
43
+ import { synchronized, Trigger } from "@dxos/async";
44
44
  import { NetworkAdapter } from "@dxos/automerge/automerge-repo";
45
45
  import { LifecycleState } from "@dxos/context";
46
46
  import { invariant } from "@dxos/invariant";
@@ -177,12 +177,12 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
177
177
  await replicator.disconnect();
178
178
  this._replicators.delete(replicator);
179
179
  }
180
- async shouldAdvertize(peerId, params) {
180
+ async shouldAdvertise(peerId, params) {
181
181
  const connection = this._connections.get(peerId);
182
182
  if (!connection) {
183
183
  return false;
184
184
  }
185
- return connection.connection.shouldAdvertize(params);
185
+ return connection.connection.shouldAdvertise(params);
186
186
  }
187
187
  _onConnectionOpen(connection) {
188
188
  log("Connection opened", {
@@ -656,6 +656,9 @@ var AutomergeHost = class extends Resource2 {
656
656
  get repo() {
657
657
  return this._repo;
658
658
  }
659
+ get loadedDocsCount() {
660
+ return Object.keys(this._repo.handles).length;
661
+ }
659
662
  async addReplicator(replicator) {
660
663
  await this._echoNetworkAdapter.addReplicator(replicator);
661
664
  }
@@ -701,7 +704,7 @@ var AutomergeHost = class extends Resource2 {
701
704
  documentId
702
705
  }, {
703
706
  F: __dxlog_file3,
704
- L: 191,
707
+ L: 195,
705
708
  S: this,
706
709
  C: (f, a) => f(...a)
707
710
  });
@@ -717,7 +720,7 @@ var AutomergeHost = class extends Resource2 {
717
720
  documentId
718
721
  }, {
719
722
  F: __dxlog_file3,
720
- L: 195,
723
+ L: 199,
721
724
  S: this,
722
725
  C: (f, a) => f(...a)
723
726
  });
@@ -726,7 +729,7 @@ var AutomergeHost = class extends Resource2 {
726
729
  const doc = handle.docSync();
727
730
  invariant3(doc, void 0, {
728
731
  F: __dxlog_file3,
729
- L: 200,
732
+ L: 204,
730
733
  S: this,
731
734
  A: [
732
735
  "doc",
@@ -740,7 +743,7 @@ var AutomergeHost = class extends Resource2 {
740
743
  }
741
744
  log2.info("done reindexing heads", void 0, {
742
745
  F: __dxlog_file3,
743
- L: 207,
746
+ L: 211,
744
747
  S: this,
745
748
  C: (f, a) => f(...a)
746
749
  });
@@ -758,7 +761,7 @@ var AutomergeHost = class extends Resource2 {
758
761
  }
759
762
  const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
760
763
  if (isEchoPeerMetadata(peerMetadata)) {
761
- return this._echoNetworkAdapter.shouldAdvertize(peerId, {
764
+ return this._echoNetworkAdapter.shouldAdvertise(peerId, {
762
765
  documentId
763
766
  });
764
767
  }
@@ -1256,14 +1259,134 @@ AutomergeDocumentLoaderImpl = _ts_decorate3([
1256
1259
  ], AutomergeDocumentLoaderImpl);
1257
1260
 
1258
1261
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1262
+ import { invariant as invariant6 } from "@dxos/invariant";
1263
+ import { PublicKey as PublicKey2 } from "@dxos/keys";
1264
+ import { log as log5 } from "@dxos/log";
1265
+ import { ComplexMap, ComplexSet, defaultMap } from "@dxos/util";
1266
+
1267
+ // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts
1259
1268
  import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
1260
1269
  import { Resource as Resource3 } from "@dxos/context";
1261
1270
  import { invariant as invariant5 } from "@dxos/invariant";
1262
- import { PublicKey as PublicKey2 } from "@dxos/keys";
1263
1271
  import { log as log4 } from "@dxos/log";
1264
1272
  import { AutomergeReplicator } from "@dxos/teleport-extension-automerge-replicator";
1265
- import { ComplexMap, ComplexSet, defaultMap } from "@dxos/util";
1266
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1273
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1274
+ var DEFAULT_FACTORY = (params) => new AutomergeReplicator(...params);
1275
+ var MeshReplicatorConnection = class extends Resource3 {
1276
+ constructor(_params) {
1277
+ super();
1278
+ this._params = _params;
1279
+ this.remoteDeviceKey = null;
1280
+ this._remotePeerId = null;
1281
+ this._isEnabled = false;
1282
+ let readableStreamController;
1283
+ this.readable = new ReadableStream({
1284
+ start: (controller) => {
1285
+ readableStreamController = controller;
1286
+ this._ctx.onDispose(() => controller.close());
1287
+ }
1288
+ });
1289
+ this.writable = new WritableStream({
1290
+ write: async (message, controller) => {
1291
+ invariant5(this._isEnabled, "Writing to a disabled connection", {
1292
+ F: __dxlog_file5,
1293
+ L: 47,
1294
+ S: this,
1295
+ A: [
1296
+ "this._isEnabled",
1297
+ "'Writing to a disabled connection'"
1298
+ ]
1299
+ });
1300
+ try {
1301
+ await this.replicatorExtension.sendSyncMessage({
1302
+ payload: cbor2.encode(message)
1303
+ });
1304
+ } catch (err) {
1305
+ controller.error(err);
1306
+ this._disconnectIfEnabled();
1307
+ }
1308
+ }
1309
+ });
1310
+ const createAutomergeReplicator = this._params.replicatorFactory ?? DEFAULT_FACTORY;
1311
+ this.replicatorExtension = createAutomergeReplicator([
1312
+ {
1313
+ peerId: this._params.ownPeerId
1314
+ },
1315
+ {
1316
+ onStartReplication: async (info, remotePeerId) => {
1317
+ this.remoteDeviceKey = remotePeerId;
1318
+ this._remotePeerId = info.id;
1319
+ log4("onStartReplication", {
1320
+ id: info.id,
1321
+ thisPeerId: this.peerId,
1322
+ remotePeerId: remotePeerId.toHex()
1323
+ }, {
1324
+ F: __dxlog_file5,
1325
+ L: 81,
1326
+ S: this,
1327
+ C: (f, a) => f(...a)
1328
+ });
1329
+ this._params.onRemoteConnected();
1330
+ },
1331
+ onSyncMessage: async ({ payload }) => {
1332
+ if (!this._isEnabled) {
1333
+ return;
1334
+ }
1335
+ const message = cbor2.decode(payload);
1336
+ readableStreamController.enqueue(message);
1337
+ },
1338
+ onClose: async () => {
1339
+ this._disconnectIfEnabled();
1340
+ }
1341
+ }
1342
+ ]);
1343
+ }
1344
+ _disconnectIfEnabled() {
1345
+ if (this._isEnabled) {
1346
+ this._params.onRemoteDisconnected();
1347
+ }
1348
+ }
1349
+ get peerId() {
1350
+ invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1351
+ F: __dxlog_file5,
1352
+ L: 107,
1353
+ S: this,
1354
+ A: [
1355
+ "this._remotePeerId != null",
1356
+ "'Remote peer has not connected yet.'"
1357
+ ]
1358
+ });
1359
+ return this._remotePeerId;
1360
+ }
1361
+ async shouldAdvertise(params) {
1362
+ return this._params.shouldAdvertise(params);
1363
+ }
1364
+ /**
1365
+ * Start exchanging messages with the remote peer.
1366
+ * Call after the remote peer has connected.
1367
+ */
1368
+ enable() {
1369
+ invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1370
+ F: __dxlog_file5,
1371
+ L: 120,
1372
+ S: this,
1373
+ A: [
1374
+ "this._remotePeerId != null",
1375
+ "'Remote peer has not connected yet.'"
1376
+ ]
1377
+ });
1378
+ this._isEnabled = true;
1379
+ }
1380
+ /**
1381
+ * Stop exchanging messages with the remote peer.
1382
+ */
1383
+ disable() {
1384
+ this._isEnabled = false;
1385
+ }
1386
+ };
1387
+
1388
+ // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1389
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1267
1390
  var MeshEchoReplicator = class {
1268
1391
  constructor() {
1269
1392
  this._connections = /* @__PURE__ */ new Set();
@@ -1288,10 +1411,10 @@ var MeshEchoReplicator = class {
1288
1411
  this._connectionsPerPeer.clear();
1289
1412
  this._context = null;
1290
1413
  }
1291
- createExtension() {
1292
- invariant5(this._context, void 0, {
1293
- F: __dxlog_file5,
1294
- L: 54,
1414
+ createExtension(extensionFactory) {
1415
+ invariant6(this._context, void 0, {
1416
+ F: __dxlog_file6,
1417
+ L: 51,
1295
1418
  S: this,
1296
1419
  A: [
1297
1420
  "this._context",
@@ -1300,18 +1423,19 @@ var MeshEchoReplicator = class {
1300
1423
  });
1301
1424
  const connection = new MeshReplicatorConnection({
1302
1425
  ownPeerId: this._context.peerId,
1426
+ replicatorFactory: extensionFactory,
1303
1427
  onRemoteConnected: async () => {
1304
- log4("onRemoteConnected", {
1428
+ log5("onRemoteConnected", {
1305
1429
  peerId: connection.peerId
1306
1430
  }, {
1307
- F: __dxlog_file5,
1308
- L: 59,
1431
+ F: __dxlog_file6,
1432
+ L: 57,
1309
1433
  S: this,
1310
1434
  C: (f, a) => f(...a)
1311
1435
  });
1312
- invariant5(this._context, void 0, {
1313
- F: __dxlog_file5,
1314
- L: 60,
1436
+ invariant6(this._context, void 0, {
1437
+ F: __dxlog_file6,
1438
+ L: 58,
1315
1439
  S: this,
1316
1440
  A: [
1317
1441
  "this._context",
@@ -1323,36 +1447,36 @@ var MeshEchoReplicator = class {
1323
1447
  } else {
1324
1448
  this._connectionsPerPeer.set(connection.peerId, connection);
1325
1449
  this._context.onConnectionOpen(connection);
1326
- await connection.enable();
1450
+ connection.enable();
1327
1451
  }
1328
1452
  },
1329
1453
  onRemoteDisconnected: async () => {
1330
- log4("onRemoteDisconnected", {
1454
+ log5("onRemoteDisconnected", {
1331
1455
  peerId: connection.peerId
1332
1456
  }, {
1333
- F: __dxlog_file5,
1334
- L: 71,
1457
+ F: __dxlog_file6,
1458
+ L: 69,
1335
1459
  S: this,
1336
1460
  C: (f, a) => f(...a)
1337
1461
  });
1338
1462
  this._context?.onConnectionClosed(connection);
1339
1463
  this._connectionsPerPeer.delete(connection.peerId);
1340
- await connection.disable();
1464
+ connection.disable();
1341
1465
  this._connections.delete(connection);
1342
1466
  },
1343
- shouldAdvertize: async (params) => {
1344
- log4("shouldAdvertize", {
1467
+ shouldAdvertise: async (params) => {
1468
+ log5("shouldAdvertise", {
1345
1469
  peerId: connection.peerId,
1346
1470
  documentId: params.documentId
1347
1471
  }, {
1348
- F: __dxlog_file5,
1349
- L: 78,
1472
+ F: __dxlog_file6,
1473
+ L: 76,
1350
1474
  S: this,
1351
1475
  C: (f, a) => f(...a)
1352
1476
  });
1353
- invariant5(this._context, void 0, {
1354
- F: __dxlog_file5,
1355
- L: 79,
1477
+ invariant6(this._context, void 0, {
1478
+ F: __dxlog_file6,
1479
+ L: 77,
1356
1480
  S: this,
1357
1481
  A: [
1358
1482
  "this._context",
@@ -1362,12 +1486,12 @@ var MeshEchoReplicator = class {
1362
1486
  try {
1363
1487
  const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
1364
1488
  if (!spaceKey) {
1365
- log4("space key not found for share policy check", {
1489
+ log5("space key not found for share policy check", {
1366
1490
  peerId: connection.peerId,
1367
1491
  documentId: params.documentId
1368
1492
  }, {
1369
- F: __dxlog_file5,
1370
- L: 83,
1493
+ F: __dxlog_file6,
1494
+ L: 81,
1371
1495
  S: this,
1372
1496
  C: (f, a) => f(...a)
1373
1497
  });
@@ -1375,19 +1499,19 @@ var MeshEchoReplicator = class {
1375
1499
  }
1376
1500
  const authorizedDevices = this._authorizedDevices.get(spaceKey);
1377
1501
  if (!connection.remoteDeviceKey) {
1378
- log4("device key not found for share policy check", {
1502
+ log5("device key not found for share policy check", {
1379
1503
  peerId: connection.peerId,
1380
1504
  documentId: params.documentId
1381
1505
  }, {
1382
- F: __dxlog_file5,
1383
- L: 93,
1506
+ F: __dxlog_file6,
1507
+ L: 91,
1384
1508
  S: this,
1385
1509
  C: (f, a) => f(...a)
1386
1510
  });
1387
1511
  return false;
1388
1512
  }
1389
1513
  const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1390
- log4("share policy check", {
1514
+ log5("share policy check", {
1391
1515
  localPeer: this._context.peerId,
1392
1516
  remotePeer: connection.peerId,
1393
1517
  documentId: params.documentId,
@@ -1395,16 +1519,16 @@ var MeshEchoReplicator = class {
1395
1519
  spaceKey,
1396
1520
  isAuthorized
1397
1521
  }, {
1398
- F: __dxlog_file5,
1399
- L: 101,
1522
+ F: __dxlog_file6,
1523
+ L: 99,
1400
1524
  S: this,
1401
1525
  C: (f, a) => f(...a)
1402
1526
  });
1403
1527
  return isAuthorized;
1404
1528
  } catch (err) {
1405
- log4.catch(err, void 0, {
1406
- F: __dxlog_file5,
1407
- L: 111,
1529
+ log5.catch(err, void 0, {
1530
+ F: __dxlog_file6,
1531
+ L: 109,
1408
1532
  S: this,
1409
1533
  C: (f, a) => f(...a)
1410
1534
  });
@@ -1416,115 +1540,23 @@ var MeshEchoReplicator = class {
1416
1540
  return connection.replicatorExtension;
1417
1541
  }
1418
1542
  authorizeDevice(spaceKey, deviceKey) {
1419
- log4("authorizeDevice", {
1543
+ log5("authorizeDevice", {
1420
1544
  spaceKey,
1421
1545
  deviceKey
1422
1546
  }, {
1423
- F: __dxlog_file5,
1424
- L: 122,
1547
+ F: __dxlog_file6,
1548
+ L: 120,
1425
1549
  S: this,
1426
1550
  C: (f, a) => f(...a)
1427
1551
  });
1428
1552
  defaultMap(this._authorizedDevices, spaceKey, () => new ComplexSet(PublicKey2.hash)).add(deviceKey);
1429
1553
  for (const connection of this._connections) {
1430
1554
  if (connection.remoteDeviceKey && connection.remoteDeviceKey.equals(deviceKey)) {
1431
- this._context?.onConnectionAuthScopeChanged(connection);
1432
- }
1433
- }
1434
- }
1435
- };
1436
- var MeshReplicatorConnection = class extends Resource3 {
1437
- constructor(_params) {
1438
- super();
1439
- this._params = _params;
1440
- this.remoteDeviceKey = null;
1441
- this._remotePeerId = null;
1442
- this._isEnabled = false;
1443
- let readableStreamController;
1444
- this.readable = new ReadableStream({
1445
- start: (controller) => {
1446
- readableStreamController = controller;
1447
- this._ctx.onDispose(() => controller.close());
1448
- }
1449
- });
1450
- this.writable = new WritableStream({
1451
- write: async (message, controller) => {
1452
- this.replicatorExtension.sendSyncMessage({
1453
- payload: cbor2.encode(message)
1454
- }).catch((err) => {
1455
- controller.error(err);
1456
- });
1457
- }
1458
- });
1459
- this.replicatorExtension = new AutomergeReplicator({
1460
- peerId: this._params.ownPeerId
1461
- }, {
1462
- onStartReplication: async (info, remotePeerId) => {
1463
- this.remoteDeviceKey = remotePeerId;
1464
- this._remotePeerId = info.id;
1465
- log4("onStartReplication", {
1466
- id: info.id,
1467
- thisPeerId: this.peerId,
1468
- remotePeerId: remotePeerId.toHex()
1469
- }, {
1470
- F: __dxlog_file5,
1471
- L: 192,
1472
- S: this,
1473
- C: (f, a) => f(...a)
1474
- });
1475
- await this._params.onRemoteConnected();
1476
- },
1477
- onSyncMessage: async ({ payload }) => {
1478
- if (!this._isEnabled) {
1479
- return;
1480
- }
1481
- const message = cbor2.decode(payload);
1482
- readableStreamController.enqueue(message);
1483
- },
1484
- onClose: async () => {
1485
- if (!this._isEnabled) {
1486
- return;
1555
+ if (this._connectionsPerPeer.has(connection.peerId)) {
1556
+ this._context?.onConnectionAuthScopeChanged(connection);
1487
1557
  }
1488
- await this._params.onRemoteDisconnected();
1489
1558
  }
1490
- });
1491
- }
1492
- get peerId() {
1493
- invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1494
- F: __dxlog_file5,
1495
- L: 215,
1496
- S: this,
1497
- A: [
1498
- "this._remotePeerId != null",
1499
- "'Remote peer has not connected yet.'"
1500
- ]
1501
- });
1502
- return this._remotePeerId;
1503
- }
1504
- async shouldAdvertize(params) {
1505
- return this._params.shouldAdvertize(params);
1506
- }
1507
- /**
1508
- * Start exchanging messages with the remote peer.
1509
- * Call after the remote peer has connected.
1510
- */
1511
- async enable() {
1512
- invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1513
- F: __dxlog_file5,
1514
- L: 228,
1515
- S: this,
1516
- A: [
1517
- "this._remotePeerId != null",
1518
- "'Remote peer has not connected yet.'"
1519
- ]
1520
- });
1521
- this._isEnabled = true;
1522
- }
1523
- /**
1524
- * Stop exchanging messages with the remote peer.
1525
- */
1526
- async disable() {
1527
- this._isEnabled = false;
1559
+ }
1528
1560
  }
1529
1561
  };
1530
1562
  export {