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

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", {
@@ -758,7 +758,7 @@ var AutomergeHost = class extends Resource2 {
758
758
  }
759
759
  const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
760
760
  if (isEchoPeerMetadata(peerMetadata)) {
761
- return this._echoNetworkAdapter.shouldAdvertize(peerId, {
761
+ return this._echoNetworkAdapter.shouldAdvertise(peerId, {
762
762
  documentId
763
763
  });
764
764
  }
@@ -1256,14 +1256,134 @@ AutomergeDocumentLoaderImpl = _ts_decorate3([
1256
1256
  ], AutomergeDocumentLoaderImpl);
1257
1257
 
1258
1258
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1259
+ import { invariant as invariant6 } from "@dxos/invariant";
1260
+ import { PublicKey as PublicKey2 } from "@dxos/keys";
1261
+ import { log as log5 } from "@dxos/log";
1262
+ import { ComplexMap, ComplexSet, defaultMap } from "@dxos/util";
1263
+
1264
+ // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts
1259
1265
  import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
1260
1266
  import { Resource as Resource3 } from "@dxos/context";
1261
1267
  import { invariant as invariant5 } from "@dxos/invariant";
1262
- import { PublicKey as PublicKey2 } from "@dxos/keys";
1263
1268
  import { log as log4 } from "@dxos/log";
1264
1269
  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";
1270
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1271
+ var DEFAULT_FACTORY = (params) => new AutomergeReplicator(...params);
1272
+ var MeshReplicatorConnection = class extends Resource3 {
1273
+ constructor(_params) {
1274
+ super();
1275
+ this._params = _params;
1276
+ this.remoteDeviceKey = null;
1277
+ this._remotePeerId = null;
1278
+ this._isEnabled = false;
1279
+ let readableStreamController;
1280
+ this.readable = new ReadableStream({
1281
+ start: (controller) => {
1282
+ readableStreamController = controller;
1283
+ this._ctx.onDispose(() => controller.close());
1284
+ }
1285
+ });
1286
+ this.writable = new WritableStream({
1287
+ write: async (message, controller) => {
1288
+ invariant5(this._isEnabled, "Writing to a disabled connection", {
1289
+ F: __dxlog_file5,
1290
+ L: 47,
1291
+ S: this,
1292
+ A: [
1293
+ "this._isEnabled",
1294
+ "'Writing to a disabled connection'"
1295
+ ]
1296
+ });
1297
+ try {
1298
+ await this.replicatorExtension.sendSyncMessage({
1299
+ payload: cbor2.encode(message)
1300
+ });
1301
+ } catch (err) {
1302
+ controller.error(err);
1303
+ this._disconnectIfEnabled();
1304
+ }
1305
+ }
1306
+ });
1307
+ const createAutomergeReplicator = this._params.replicatorFactory ?? DEFAULT_FACTORY;
1308
+ this.replicatorExtension = createAutomergeReplicator([
1309
+ {
1310
+ peerId: this._params.ownPeerId
1311
+ },
1312
+ {
1313
+ onStartReplication: async (info, remotePeerId) => {
1314
+ this.remoteDeviceKey = remotePeerId;
1315
+ this._remotePeerId = info.id;
1316
+ log4("onStartReplication", {
1317
+ id: info.id,
1318
+ thisPeerId: this.peerId,
1319
+ remotePeerId: remotePeerId.toHex()
1320
+ }, {
1321
+ F: __dxlog_file5,
1322
+ L: 81,
1323
+ S: this,
1324
+ C: (f, a) => f(...a)
1325
+ });
1326
+ this._params.onRemoteConnected();
1327
+ },
1328
+ onSyncMessage: async ({ payload }) => {
1329
+ if (!this._isEnabled) {
1330
+ return;
1331
+ }
1332
+ const message = cbor2.decode(payload);
1333
+ readableStreamController.enqueue(message);
1334
+ },
1335
+ onClose: async () => {
1336
+ this._disconnectIfEnabled();
1337
+ }
1338
+ }
1339
+ ]);
1340
+ }
1341
+ _disconnectIfEnabled() {
1342
+ if (this._isEnabled) {
1343
+ this._params.onRemoteDisconnected();
1344
+ }
1345
+ }
1346
+ get peerId() {
1347
+ invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1348
+ F: __dxlog_file5,
1349
+ L: 107,
1350
+ S: this,
1351
+ A: [
1352
+ "this._remotePeerId != null",
1353
+ "'Remote peer has not connected yet.'"
1354
+ ]
1355
+ });
1356
+ return this._remotePeerId;
1357
+ }
1358
+ async shouldAdvertise(params) {
1359
+ return this._params.shouldAdvertise(params);
1360
+ }
1361
+ /**
1362
+ * Start exchanging messages with the remote peer.
1363
+ * Call after the remote peer has connected.
1364
+ */
1365
+ enable() {
1366
+ invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1367
+ F: __dxlog_file5,
1368
+ L: 120,
1369
+ S: this,
1370
+ A: [
1371
+ "this._remotePeerId != null",
1372
+ "'Remote peer has not connected yet.'"
1373
+ ]
1374
+ });
1375
+ this._isEnabled = true;
1376
+ }
1377
+ /**
1378
+ * Stop exchanging messages with the remote peer.
1379
+ */
1380
+ disable() {
1381
+ this._isEnabled = false;
1382
+ }
1383
+ };
1384
+
1385
+ // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1386
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1267
1387
  var MeshEchoReplicator = class {
1268
1388
  constructor() {
1269
1389
  this._connections = /* @__PURE__ */ new Set();
@@ -1288,10 +1408,10 @@ var MeshEchoReplicator = class {
1288
1408
  this._connectionsPerPeer.clear();
1289
1409
  this._context = null;
1290
1410
  }
1291
- createExtension() {
1292
- invariant5(this._context, void 0, {
1293
- F: __dxlog_file5,
1294
- L: 54,
1411
+ createExtension(extensionFactory) {
1412
+ invariant6(this._context, void 0, {
1413
+ F: __dxlog_file6,
1414
+ L: 51,
1295
1415
  S: this,
1296
1416
  A: [
1297
1417
  "this._context",
@@ -1300,18 +1420,19 @@ var MeshEchoReplicator = class {
1300
1420
  });
1301
1421
  const connection = new MeshReplicatorConnection({
1302
1422
  ownPeerId: this._context.peerId,
1423
+ replicatorFactory: extensionFactory,
1303
1424
  onRemoteConnected: async () => {
1304
- log4("onRemoteConnected", {
1425
+ log5("onRemoteConnected", {
1305
1426
  peerId: connection.peerId
1306
1427
  }, {
1307
- F: __dxlog_file5,
1308
- L: 59,
1428
+ F: __dxlog_file6,
1429
+ L: 57,
1309
1430
  S: this,
1310
1431
  C: (f, a) => f(...a)
1311
1432
  });
1312
- invariant5(this._context, void 0, {
1313
- F: __dxlog_file5,
1314
- L: 60,
1433
+ invariant6(this._context, void 0, {
1434
+ F: __dxlog_file6,
1435
+ L: 58,
1315
1436
  S: this,
1316
1437
  A: [
1317
1438
  "this._context",
@@ -1323,36 +1444,36 @@ var MeshEchoReplicator = class {
1323
1444
  } else {
1324
1445
  this._connectionsPerPeer.set(connection.peerId, connection);
1325
1446
  this._context.onConnectionOpen(connection);
1326
- await connection.enable();
1447
+ connection.enable();
1327
1448
  }
1328
1449
  },
1329
1450
  onRemoteDisconnected: async () => {
1330
- log4("onRemoteDisconnected", {
1451
+ log5("onRemoteDisconnected", {
1331
1452
  peerId: connection.peerId
1332
1453
  }, {
1333
- F: __dxlog_file5,
1334
- L: 71,
1454
+ F: __dxlog_file6,
1455
+ L: 69,
1335
1456
  S: this,
1336
1457
  C: (f, a) => f(...a)
1337
1458
  });
1338
1459
  this._context?.onConnectionClosed(connection);
1339
1460
  this._connectionsPerPeer.delete(connection.peerId);
1340
- await connection.disable();
1461
+ connection.disable();
1341
1462
  this._connections.delete(connection);
1342
1463
  },
1343
- shouldAdvertize: async (params) => {
1344
- log4("shouldAdvertize", {
1464
+ shouldAdvertise: async (params) => {
1465
+ log5("shouldAdvertise", {
1345
1466
  peerId: connection.peerId,
1346
1467
  documentId: params.documentId
1347
1468
  }, {
1348
- F: __dxlog_file5,
1349
- L: 78,
1469
+ F: __dxlog_file6,
1470
+ L: 76,
1350
1471
  S: this,
1351
1472
  C: (f, a) => f(...a)
1352
1473
  });
1353
- invariant5(this._context, void 0, {
1354
- F: __dxlog_file5,
1355
- L: 79,
1474
+ invariant6(this._context, void 0, {
1475
+ F: __dxlog_file6,
1476
+ L: 77,
1356
1477
  S: this,
1357
1478
  A: [
1358
1479
  "this._context",
@@ -1362,12 +1483,12 @@ var MeshEchoReplicator = class {
1362
1483
  try {
1363
1484
  const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
1364
1485
  if (!spaceKey) {
1365
- log4("space key not found for share policy check", {
1486
+ log5("space key not found for share policy check", {
1366
1487
  peerId: connection.peerId,
1367
1488
  documentId: params.documentId
1368
1489
  }, {
1369
- F: __dxlog_file5,
1370
- L: 83,
1490
+ F: __dxlog_file6,
1491
+ L: 81,
1371
1492
  S: this,
1372
1493
  C: (f, a) => f(...a)
1373
1494
  });
@@ -1375,19 +1496,19 @@ var MeshEchoReplicator = class {
1375
1496
  }
1376
1497
  const authorizedDevices = this._authorizedDevices.get(spaceKey);
1377
1498
  if (!connection.remoteDeviceKey) {
1378
- log4("device key not found for share policy check", {
1499
+ log5("device key not found for share policy check", {
1379
1500
  peerId: connection.peerId,
1380
1501
  documentId: params.documentId
1381
1502
  }, {
1382
- F: __dxlog_file5,
1383
- L: 93,
1503
+ F: __dxlog_file6,
1504
+ L: 91,
1384
1505
  S: this,
1385
1506
  C: (f, a) => f(...a)
1386
1507
  });
1387
1508
  return false;
1388
1509
  }
1389
1510
  const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1390
- log4("share policy check", {
1511
+ log5("share policy check", {
1391
1512
  localPeer: this._context.peerId,
1392
1513
  remotePeer: connection.peerId,
1393
1514
  documentId: params.documentId,
@@ -1395,16 +1516,16 @@ var MeshEchoReplicator = class {
1395
1516
  spaceKey,
1396
1517
  isAuthorized
1397
1518
  }, {
1398
- F: __dxlog_file5,
1399
- L: 101,
1519
+ F: __dxlog_file6,
1520
+ L: 99,
1400
1521
  S: this,
1401
1522
  C: (f, a) => f(...a)
1402
1523
  });
1403
1524
  return isAuthorized;
1404
1525
  } catch (err) {
1405
- log4.catch(err, void 0, {
1406
- F: __dxlog_file5,
1407
- L: 111,
1526
+ log5.catch(err, void 0, {
1527
+ F: __dxlog_file6,
1528
+ L: 109,
1408
1529
  S: this,
1409
1530
  C: (f, a) => f(...a)
1410
1531
  });
@@ -1416,115 +1537,23 @@ var MeshEchoReplicator = class {
1416
1537
  return connection.replicatorExtension;
1417
1538
  }
1418
1539
  authorizeDevice(spaceKey, deviceKey) {
1419
- log4("authorizeDevice", {
1540
+ log5("authorizeDevice", {
1420
1541
  spaceKey,
1421
1542
  deviceKey
1422
1543
  }, {
1423
- F: __dxlog_file5,
1424
- L: 122,
1544
+ F: __dxlog_file6,
1545
+ L: 120,
1425
1546
  S: this,
1426
1547
  C: (f, a) => f(...a)
1427
1548
  });
1428
1549
  defaultMap(this._authorizedDevices, spaceKey, () => new ComplexSet(PublicKey2.hash)).add(deviceKey);
1429
1550
  for (const connection of this._connections) {
1430
1551
  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;
1552
+ if (this._connectionsPerPeer.has(connection.peerId)) {
1553
+ this._context?.onConnectionAuthScopeChanged(connection);
1487
1554
  }
1488
- await this._params.onRemoteDisconnected();
1489
1555
  }
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;
1556
+ }
1528
1557
  }
1529
1558
  };
1530
1559
  export {