@dxos/echo-pipeline 0.6.1-main.c633aeb → 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.
Files changed (37) hide show
  1. package/dist/lib/browser/{chunk-HS77A4I4.mjs → chunk-A2LCXJVD.mjs} +16 -1
  2. package/dist/lib/browser/chunk-A2LCXJVD.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +261 -180
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +1 -1
  7. package/dist/lib/node/{chunk-Y5U7UXEL.cjs → chunk-GHBIMYZK.cjs} +19 -4
  8. package/dist/lib/node/chunk-GHBIMYZK.cjs.map +7 -0
  9. package/dist/lib/node/index.cjs +306 -229
  10. package/dist/lib/node/index.cjs.map +4 -4
  11. package/dist/lib/node/meta.json +1 -1
  12. package/dist/lib/node/testing/index.cjs +11 -11
  13. package/dist/types/src/automerge/automerge-host.d.ts +2 -0
  14. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
  15. package/dist/types/src/automerge/echo-network-adapter.d.ts +2 -2
  16. package/dist/types/src/automerge/echo-network-adapter.test.d.ts +2 -0
  17. package/dist/types/src/automerge/echo-network-adapter.test.d.ts.map +1 -0
  18. package/dist/types/src/automerge/echo-replicator.d.ts +5 -6
  19. package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
  20. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +35 -0
  21. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -0
  22. package/dist/types/src/automerge/mesh-echo-replicator.d.ts +2 -2
  23. package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
  24. package/dist/types/src/db-host/data-service.d.ts +4 -2
  25. package/dist/types/src/db-host/data-service.d.ts.map +1 -1
  26. package/package.json +33 -33
  27. package/src/automerge/automerge-doc-loader.ts +1 -1
  28. package/src/automerge/automerge-host.ts +27 -5
  29. package/src/automerge/automerge-repo.test.ts +76 -1
  30. package/src/automerge/echo-network-adapter.test.ts +131 -0
  31. package/src/automerge/echo-network-adapter.ts +4 -4
  32. package/src/automerge/echo-replicator.ts +6 -9
  33. package/src/automerge/mesh-echo-replicator-connection.ts +130 -0
  34. package/src/automerge/mesh-echo-replicator.ts +15 -123
  35. package/src/db-host/data-service.ts +22 -2
  36. package/dist/lib/browser/chunk-HS77A4I4.mjs.map +0 -7
  37. package/dist/lib/node/chunk-Y5U7UXEL.cjs.map +0 -7
@@ -25,20 +25,22 @@ import {
25
25
  mapTimeframeToFeedIndexes,
26
26
  startAfter,
27
27
  valueEncoding
28
- } from "./chunk-HS77A4I4.mjs";
28
+ } from "./chunk-A2LCXJVD.mjs";
29
29
 
30
30
  // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
31
31
  import { Event, asyncTimeout } from "@dxos/async";
32
32
  import { next as automerge, getBackend, getHeads, isAutomerge, save } from "@dxos/automerge/automerge";
33
33
  import { Repo } from "@dxos/automerge/automerge-repo";
34
34
  import { Resource as Resource2, cancelWithContext } from "@dxos/context";
35
+ import { invariant as invariant3 } from "@dxos/invariant";
35
36
  import { PublicKey } from "@dxos/keys";
37
+ import { log as log2 } from "@dxos/log";
36
38
  import { objectPointerCodec } from "@dxos/protocols";
37
39
  import { trace } from "@dxos/tracing";
38
40
  import { mapValues } from "@dxos/util";
39
41
 
40
42
  // packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts
41
- import { Trigger, synchronized } from "@dxos/async";
43
+ import { synchronized, Trigger } from "@dxos/async";
42
44
  import { NetworkAdapter } from "@dxos/automerge/automerge-repo";
43
45
  import { LifecycleState } from "@dxos/context";
44
46
  import { invariant } from "@dxos/invariant";
@@ -175,12 +177,12 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
175
177
  await replicator.disconnect();
176
178
  this._replicators.delete(replicator);
177
179
  }
178
- async shouldAdvertize(peerId, params) {
180
+ async shouldAdvertise(peerId, params) {
179
181
  const connection = this._connections.get(peerId);
180
182
  if (!connection) {
181
183
  return false;
182
184
  }
183
- return connection.connection.shouldAdvertize(params);
185
+ return connection.connection.shouldAdvertise(params);
184
186
  }
185
187
  _onConnectionOpen(connection) {
186
188
  log("Connection opened", {
@@ -602,12 +604,14 @@ function _ts_decorate2(decorators, target, key, desc) {
602
604
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
603
605
  return c > 3 && r && Object.defineProperty(target, key, r), r;
604
606
  }
607
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
605
608
  var AutomergeHost = class extends Resource2 {
606
609
  constructor({ db, indexMetadataStore }) {
607
610
  super();
608
611
  this._echoNetworkAdapter = new EchoNetworkAdapter({
609
612
  getContainingSpaceForDocument: this._getContainingSpaceForDocument.bind(this)
610
613
  });
614
+ this._db = db;
611
615
  this._storage = new LevelDBStorageAdapter({
612
616
  db: db.sublevel("automerge"),
613
617
  callbacks: {
@@ -691,6 +695,56 @@ var AutomergeHost = class extends Resource2 {
691
695
  return this._repo.create(initialValue);
692
696
  }
693
697
  }
698
+ async reIndexHeads(documentIds) {
699
+ for (const documentId of documentIds) {
700
+ log2.info("reindexing heads for document", {
701
+ documentId
702
+ }, {
703
+ F: __dxlog_file3,
704
+ L: 191,
705
+ S: this,
706
+ C: (f, a) => f(...a)
707
+ });
708
+ const handle = this._repo.find(documentId);
709
+ await handle.whenReady([
710
+ "ready",
711
+ "requesting"
712
+ ]);
713
+ if (handle.inState([
714
+ "requesting"
715
+ ])) {
716
+ log2.warn("document is not available locally, skipping", {
717
+ documentId
718
+ }, {
719
+ F: __dxlog_file3,
720
+ L: 195,
721
+ S: this,
722
+ C: (f, a) => f(...a)
723
+ });
724
+ continue;
725
+ }
726
+ const doc = handle.docSync();
727
+ invariant3(doc, void 0, {
728
+ F: __dxlog_file3,
729
+ L: 200,
730
+ S: this,
731
+ A: [
732
+ "doc",
733
+ ""
734
+ ]
735
+ });
736
+ const heads = getHeads(doc);
737
+ const batch = this._db.batch();
738
+ this._headsStore.setHeads(documentId, heads, batch);
739
+ await batch.write();
740
+ }
741
+ log2.info("done reindexing heads", void 0, {
742
+ F: __dxlog_file3,
743
+ L: 207,
744
+ S: this,
745
+ C: (f, a) => f(...a)
746
+ });
747
+ }
694
748
  // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
695
749
  // Hosts, running in the worker, don't share documents unless requested by other peers.
696
750
  // NOTE: If both peers return sharePolicy=false the replication will not happen
@@ -704,7 +758,7 @@ var AutomergeHost = class extends Resource2 {
704
758
  }
705
759
  const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
706
760
  if (isEchoPeerMetadata(peerMetadata)) {
707
- return this._echoNetworkAdapter.shouldAdvertize(peerId, {
761
+ return this._echoNetworkAdapter.shouldAdvertise(peerId, {
708
762
  documentId
709
763
  });
710
764
  }
@@ -875,8 +929,8 @@ import { interpretAsDocumentId } from "@dxos/automerge/automerge-repo";
875
929
  import { cancelWithContext as cancelWithContext2 } from "@dxos/context";
876
930
  import { warnAfterTimeout } from "@dxos/debug";
877
931
  import { SpaceDocVersion } from "@dxos/echo-protocol";
878
- import { invariant as invariant3 } from "@dxos/invariant";
879
- import { log as log2 } from "@dxos/log";
932
+ import { invariant as invariant4 } from "@dxos/invariant";
933
+ import { log as log3 } from "@dxos/log";
880
934
  import { trace as trace2 } from "@dxos/tracing";
881
935
  function _ts_decorate3(decorators, target, key, desc) {
882
936
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -888,7 +942,7 @@ function _ts_decorate3(decorators, target, key, desc) {
888
942
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
889
943
  return c > 3 && r && Object.defineProperty(target, key, r), r;
890
944
  }
891
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
945
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
892
946
  var AutomergeDocumentLoaderImpl = class {
893
947
  constructor(_spaceId, _repo, _spaceKey) {
894
948
  this._spaceId = _spaceId;
@@ -914,8 +968,8 @@ var AutomergeDocumentLoaderImpl = class {
914
968
  }
915
969
  const existingDocHandle = await this._initDocHandle(ctx, spaceState.rootUrl);
916
970
  const doc = existingDocHandle.docSync();
917
- invariant3(doc, void 0, {
918
- F: __dxlog_file3,
971
+ invariant4(doc, void 0, {
972
+ F: __dxlog_file4,
919
973
  L: 84,
920
974
  S: this,
921
975
  A: [
@@ -923,8 +977,8 @@ var AutomergeDocumentLoaderImpl = class {
923
977
  ""
924
978
  ]
925
979
  });
926
- invariant3(doc.version === SpaceDocVersion.CURRENT, void 0, {
927
- F: __dxlog_file3,
980
+ invariant4(doc.version === SpaceDocVersion.CURRENT, void 0, {
981
+ F: __dxlog_file4,
928
982
  L: 85,
929
983
  S: this,
930
984
  A: [
@@ -944,8 +998,8 @@ var AutomergeDocumentLoaderImpl = class {
944
998
  let hasUrlsToLoad = false;
945
999
  const urlsToLoad = {};
946
1000
  for (const objectId of objectIds) {
947
- invariant3(this._spaceRootDocHandle, void 0, {
948
- F: __dxlog_file3,
1001
+ invariant4(this._spaceRootDocHandle, void 0, {
1002
+ F: __dxlog_file4,
949
1003
  L: 97,
950
1004
  S: this,
951
1005
  A: [
@@ -957,8 +1011,8 @@ var AutomergeDocumentLoaderImpl = class {
957
1011
  continue;
958
1012
  }
959
1013
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
960
- invariant3(spaceRootDoc, void 0, {
961
- F: __dxlog_file3,
1014
+ invariant4(spaceRootDoc, void 0, {
1015
+ F: __dxlog_file4,
962
1016
  L: 102,
963
1017
  S: this,
964
1018
  A: [
@@ -969,10 +1023,10 @@ var AutomergeDocumentLoaderImpl = class {
969
1023
  const documentUrl = (spaceRootDoc.links ?? {})[objectId];
970
1024
  if (documentUrl == null) {
971
1025
  this._objectsPendingDocumentLoad.add(objectId);
972
- log2.info("loading delayed until object links are initialized", {
1026
+ log3.info("loading delayed until object links are initialized", {
973
1027
  objectId
974
1028
  }, {
975
- F: __dxlog_file3,
1029
+ F: __dxlog_file4,
976
1030
  L: 106,
977
1031
  S: this,
978
1032
  C: (f, a) => f(...a)
@@ -987,8 +1041,8 @@ var AutomergeDocumentLoaderImpl = class {
987
1041
  }
988
1042
  }
989
1043
  getObjectDocumentId(objectId) {
990
- invariant3(this._spaceRootDocHandle, void 0, {
991
- F: __dxlog_file3,
1044
+ invariant4(this._spaceRootDocHandle, void 0, {
1045
+ F: __dxlog_file4,
992
1046
  L: 118,
993
1047
  S: this,
994
1048
  A: [
@@ -997,8 +1051,8 @@ var AutomergeDocumentLoaderImpl = class {
997
1051
  ]
998
1052
  });
999
1053
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
1000
- invariant3(spaceRootDoc, void 0, {
1001
- F: __dxlog_file3,
1054
+ invariant4(spaceRootDoc, void 0, {
1055
+ F: __dxlog_file4,
1002
1056
  L: 120,
1003
1057
  S: this,
1004
1058
  A: [
@@ -1021,8 +1075,8 @@ var AutomergeDocumentLoaderImpl = class {
1021
1075
  linksAwaitingLoad.forEach(([objectId]) => this._objectsPendingDocumentLoad.delete(objectId));
1022
1076
  }
1023
1077
  getSpaceRootDocHandle() {
1024
- invariant3(this._spaceRootDocHandle, void 0, {
1025
- F: __dxlog_file3,
1078
+ invariant4(this._spaceRootDocHandle, void 0, {
1079
+ F: __dxlog_file4,
1026
1080
  L: 140,
1027
1081
  S: this,
1028
1082
  A: [
@@ -1033,8 +1087,8 @@ var AutomergeDocumentLoaderImpl = class {
1033
1087
  return this._spaceRootDocHandle;
1034
1088
  }
1035
1089
  createDocumentForObject(objectId) {
1036
- invariant3(this._spaceRootDocHandle, void 0, {
1037
- F: __dxlog_file3,
1090
+ invariant4(this._spaceRootDocHandle, void 0, {
1091
+ F: __dxlog_file4,
1038
1092
  L: 145,
1039
1093
  S: this,
1040
1094
  A: [
@@ -1075,11 +1129,11 @@ var AutomergeDocumentLoaderImpl = class {
1075
1129
  };
1076
1130
  const objectDocumentHandle = this._objectDocumentHandles.get(objectId);
1077
1131
  if (objectDocumentHandle != null && objectDocumentHandle.url !== automergeUrl) {
1078
- log2.warn("object already inlined in a different document, ignoring the link", {
1132
+ log3.warn("object already inlined in a different document, ignoring the link", {
1079
1133
  ...logMeta,
1080
1134
  actualDocumentUrl: objectDocumentHandle.url
1081
1135
  }, {
1082
- F: __dxlog_file3,
1136
+ F: __dxlog_file4,
1083
1137
  L: 177,
1084
1138
  S: this,
1085
1139
  C: (f, a) => f(...a)
@@ -1087,8 +1141,8 @@ var AutomergeDocumentLoaderImpl = class {
1087
1141
  continue;
1088
1142
  }
1089
1143
  if (objectDocumentHandle?.url === automergeUrl) {
1090
- log2.warn("object document was already loaded", logMeta, {
1091
- F: __dxlog_file3,
1144
+ log3.warn("object document was already loaded", logMeta, {
1145
+ F: __dxlog_file4,
1092
1146
  L: 184,
1093
1147
  S: this,
1094
1148
  C: (f, a) => f(...a)
@@ -1096,8 +1150,8 @@ var AutomergeDocumentLoaderImpl = class {
1096
1150
  continue;
1097
1151
  }
1098
1152
  const handle = this._repo.find(automergeUrl);
1099
- log2.debug("document loading triggered", logMeta, {
1100
- F: __dxlog_file3,
1153
+ log3.debug("document loading triggered", logMeta, {
1154
+ F: __dxlog_file4,
1101
1155
  L: 188,
1102
1156
  S: this,
1103
1157
  C: (f, a) => f(...a)
@@ -1116,11 +1170,11 @@ var AutomergeDocumentLoaderImpl = class {
1116
1170
  break;
1117
1171
  } catch (err) {
1118
1172
  if (`${err}`.includes("Timeout")) {
1119
- log2.info("wraparound", {
1173
+ log3.info("wraparound", {
1120
1174
  id: docHandle.documentId,
1121
1175
  state: docHandle.state
1122
1176
  }, {
1123
- F: __dxlog_file3,
1177
+ F: __dxlog_file4,
1124
1178
  L: 204,
1125
1179
  S: this,
1126
1180
  C: (f, a) => f(...a)
@@ -1145,16 +1199,14 @@ var AutomergeDocumentLoaderImpl = class {
1145
1199
  }
1146
1200
  async _createObjectOnDocumentLoad(handle, objectId) {
1147
1201
  try {
1148
- await handle.doc([
1149
- "ready"
1150
- ]);
1202
+ await handle.whenReady();
1151
1203
  const logMeta = {
1152
1204
  objectId,
1153
1205
  docUrl: handle.url
1154
1206
  };
1155
1207
  if (this.onObjectDocumentLoaded.listenerCount() === 0) {
1156
- log2.info("document loaded after all listeners were removed", logMeta, {
1157
- F: __dxlog_file3,
1208
+ log3.info("document loaded after all listeners were removed", logMeta, {
1209
+ F: __dxlog_file4,
1158
1210
  L: 231,
1159
1211
  S: this,
1160
1212
  C: (f, a) => f(...a)
@@ -1163,8 +1215,8 @@ var AutomergeDocumentLoaderImpl = class {
1163
1215
  }
1164
1216
  const objectDocHandle = this._objectDocumentHandles.get(objectId);
1165
1217
  if (objectDocHandle?.url !== handle.url) {
1166
- log2.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1167
- F: __dxlog_file3,
1218
+ log3.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1219
+ F: __dxlog_file4,
1168
1220
  L: 236,
1169
1221
  S: this,
1170
1222
  C: (f, a) => f(...a)
@@ -1177,13 +1229,13 @@ var AutomergeDocumentLoaderImpl = class {
1177
1229
  });
1178
1230
  } catch (err) {
1179
1231
  const shouldRetryLoading = this.onObjectDocumentLoaded.listenerCount() > 0;
1180
- log2.warn("failed to load a document", {
1232
+ log3.warn("failed to load a document", {
1181
1233
  objectId,
1182
1234
  automergeUrl: handle.url,
1183
1235
  retryLoading: shouldRetryLoading,
1184
1236
  err
1185
1237
  }, {
1186
- F: __dxlog_file3,
1238
+ F: __dxlog_file4,
1187
1239
  L: 242,
1188
1240
  S: this,
1189
1241
  C: (f, a) => f(...a)
@@ -1204,14 +1256,134 @@ AutomergeDocumentLoaderImpl = _ts_decorate3([
1204
1256
  ], AutomergeDocumentLoaderImpl);
1205
1257
 
1206
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
1207
1265
  import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
1208
1266
  import { Resource as Resource3 } from "@dxos/context";
1209
- import { invariant as invariant4 } from "@dxos/invariant";
1210
- import { PublicKey as PublicKey2 } from "@dxos/keys";
1211
- import { log as log3 } from "@dxos/log";
1267
+ import { invariant as invariant5 } from "@dxos/invariant";
1268
+ import { log as log4 } from "@dxos/log";
1212
1269
  import { AutomergeReplicator } from "@dxos/teleport-extension-automerge-replicator";
1213
- import { ComplexMap, ComplexSet, defaultMap } from "@dxos/util";
1214
- var __dxlog_file4 = "/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";
1215
1387
  var MeshEchoReplicator = class {
1216
1388
  constructor() {
1217
1389
  this._connections = /* @__PURE__ */ new Set();
@@ -1236,10 +1408,10 @@ var MeshEchoReplicator = class {
1236
1408
  this._connectionsPerPeer.clear();
1237
1409
  this._context = null;
1238
1410
  }
1239
- createExtension() {
1240
- invariant4(this._context, void 0, {
1241
- F: __dxlog_file4,
1242
- L: 54,
1411
+ createExtension(extensionFactory) {
1412
+ invariant6(this._context, void 0, {
1413
+ F: __dxlog_file6,
1414
+ L: 51,
1243
1415
  S: this,
1244
1416
  A: [
1245
1417
  "this._context",
@@ -1248,18 +1420,19 @@ var MeshEchoReplicator = class {
1248
1420
  });
1249
1421
  const connection = new MeshReplicatorConnection({
1250
1422
  ownPeerId: this._context.peerId,
1423
+ replicatorFactory: extensionFactory,
1251
1424
  onRemoteConnected: async () => {
1252
- log3("onRemoteConnected", {
1425
+ log5("onRemoteConnected", {
1253
1426
  peerId: connection.peerId
1254
1427
  }, {
1255
- F: __dxlog_file4,
1256
- L: 59,
1428
+ F: __dxlog_file6,
1429
+ L: 57,
1257
1430
  S: this,
1258
1431
  C: (f, a) => f(...a)
1259
1432
  });
1260
- invariant4(this._context, void 0, {
1261
- F: __dxlog_file4,
1262
- L: 60,
1433
+ invariant6(this._context, void 0, {
1434
+ F: __dxlog_file6,
1435
+ L: 58,
1263
1436
  S: this,
1264
1437
  A: [
1265
1438
  "this._context",
@@ -1271,36 +1444,36 @@ var MeshEchoReplicator = class {
1271
1444
  } else {
1272
1445
  this._connectionsPerPeer.set(connection.peerId, connection);
1273
1446
  this._context.onConnectionOpen(connection);
1274
- await connection.enable();
1447
+ connection.enable();
1275
1448
  }
1276
1449
  },
1277
1450
  onRemoteDisconnected: async () => {
1278
- log3("onRemoteDisconnected", {
1451
+ log5("onRemoteDisconnected", {
1279
1452
  peerId: connection.peerId
1280
1453
  }, {
1281
- F: __dxlog_file4,
1282
- L: 71,
1454
+ F: __dxlog_file6,
1455
+ L: 69,
1283
1456
  S: this,
1284
1457
  C: (f, a) => f(...a)
1285
1458
  });
1286
1459
  this._context?.onConnectionClosed(connection);
1287
1460
  this._connectionsPerPeer.delete(connection.peerId);
1288
- await connection.disable();
1461
+ connection.disable();
1289
1462
  this._connections.delete(connection);
1290
1463
  },
1291
- shouldAdvertize: async (params) => {
1292
- log3("shouldAdvertize", {
1464
+ shouldAdvertise: async (params) => {
1465
+ log5("shouldAdvertise", {
1293
1466
  peerId: connection.peerId,
1294
1467
  documentId: params.documentId
1295
1468
  }, {
1296
- F: __dxlog_file4,
1297
- L: 78,
1469
+ F: __dxlog_file6,
1470
+ L: 76,
1298
1471
  S: this,
1299
1472
  C: (f, a) => f(...a)
1300
1473
  });
1301
- invariant4(this._context, void 0, {
1302
- F: __dxlog_file4,
1303
- L: 79,
1474
+ invariant6(this._context, void 0, {
1475
+ F: __dxlog_file6,
1476
+ L: 77,
1304
1477
  S: this,
1305
1478
  A: [
1306
1479
  "this._context",
@@ -1310,12 +1483,12 @@ var MeshEchoReplicator = class {
1310
1483
  try {
1311
1484
  const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
1312
1485
  if (!spaceKey) {
1313
- log3("space key not found for share policy check", {
1486
+ log5("space key not found for share policy check", {
1314
1487
  peerId: connection.peerId,
1315
1488
  documentId: params.documentId
1316
1489
  }, {
1317
- F: __dxlog_file4,
1318
- L: 83,
1490
+ F: __dxlog_file6,
1491
+ L: 81,
1319
1492
  S: this,
1320
1493
  C: (f, a) => f(...a)
1321
1494
  });
@@ -1323,19 +1496,19 @@ var MeshEchoReplicator = class {
1323
1496
  }
1324
1497
  const authorizedDevices = this._authorizedDevices.get(spaceKey);
1325
1498
  if (!connection.remoteDeviceKey) {
1326
- log3("device key not found for share policy check", {
1499
+ log5("device key not found for share policy check", {
1327
1500
  peerId: connection.peerId,
1328
1501
  documentId: params.documentId
1329
1502
  }, {
1330
- F: __dxlog_file4,
1331
- L: 93,
1503
+ F: __dxlog_file6,
1504
+ L: 91,
1332
1505
  S: this,
1333
1506
  C: (f, a) => f(...a)
1334
1507
  });
1335
1508
  return false;
1336
1509
  }
1337
1510
  const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1338
- log3("share policy check", {
1511
+ log5("share policy check", {
1339
1512
  localPeer: this._context.peerId,
1340
1513
  remotePeer: connection.peerId,
1341
1514
  documentId: params.documentId,
@@ -1343,16 +1516,16 @@ var MeshEchoReplicator = class {
1343
1516
  spaceKey,
1344
1517
  isAuthorized
1345
1518
  }, {
1346
- F: __dxlog_file4,
1347
- L: 101,
1519
+ F: __dxlog_file6,
1520
+ L: 99,
1348
1521
  S: this,
1349
1522
  C: (f, a) => f(...a)
1350
1523
  });
1351
1524
  return isAuthorized;
1352
1525
  } catch (err) {
1353
- log3.catch(err, void 0, {
1354
- F: __dxlog_file4,
1355
- L: 111,
1526
+ log5.catch(err, void 0, {
1527
+ F: __dxlog_file6,
1528
+ L: 109,
1356
1529
  S: this,
1357
1530
  C: (f, a) => f(...a)
1358
1531
  });
@@ -1364,115 +1537,23 @@ var MeshEchoReplicator = class {
1364
1537
  return connection.replicatorExtension;
1365
1538
  }
1366
1539
  authorizeDevice(spaceKey, deviceKey) {
1367
- log3("authorizeDevice", {
1540
+ log5("authorizeDevice", {
1368
1541
  spaceKey,
1369
1542
  deviceKey
1370
1543
  }, {
1371
- F: __dxlog_file4,
1372
- L: 122,
1544
+ F: __dxlog_file6,
1545
+ L: 120,
1373
1546
  S: this,
1374
1547
  C: (f, a) => f(...a)
1375
1548
  });
1376
1549
  defaultMap(this._authorizedDevices, spaceKey, () => new ComplexSet(PublicKey2.hash)).add(deviceKey);
1377
1550
  for (const connection of this._connections) {
1378
1551
  if (connection.remoteDeviceKey && connection.remoteDeviceKey.equals(deviceKey)) {
1379
- this._context?.onConnectionAuthScopeChanged(connection);
1380
- }
1381
- }
1382
- }
1383
- };
1384
- var MeshReplicatorConnection = class extends Resource3 {
1385
- constructor(_params) {
1386
- super();
1387
- this._params = _params;
1388
- this.remoteDeviceKey = null;
1389
- this._remotePeerId = null;
1390
- this._isEnabled = false;
1391
- let readableStreamController;
1392
- this.readable = new ReadableStream({
1393
- start: (controller) => {
1394
- readableStreamController = controller;
1395
- this._ctx.onDispose(() => controller.close());
1396
- }
1397
- });
1398
- this.writable = new WritableStream({
1399
- write: async (message, controller) => {
1400
- this.replicatorExtension.sendSyncMessage({
1401
- payload: cbor2.encode(message)
1402
- }).catch((err) => {
1403
- controller.error(err);
1404
- });
1405
- }
1406
- });
1407
- this.replicatorExtension = new AutomergeReplicator({
1408
- peerId: this._params.ownPeerId
1409
- }, {
1410
- onStartReplication: async (info, remotePeerId) => {
1411
- this.remoteDeviceKey = remotePeerId;
1412
- this._remotePeerId = info.id;
1413
- log3("onStartReplication", {
1414
- id: info.id,
1415
- thisPeerId: this.peerId,
1416
- remotePeerId: remotePeerId.toHex()
1417
- }, {
1418
- F: __dxlog_file4,
1419
- L: 192,
1420
- S: this,
1421
- C: (f, a) => f(...a)
1422
- });
1423
- await this._params.onRemoteConnected();
1424
- },
1425
- onSyncMessage: async ({ payload }) => {
1426
- if (!this._isEnabled) {
1427
- return;
1428
- }
1429
- const message = cbor2.decode(payload);
1430
- readableStreamController.enqueue(message);
1431
- },
1432
- onClose: async () => {
1433
- if (!this._isEnabled) {
1434
- return;
1552
+ if (this._connectionsPerPeer.has(connection.peerId)) {
1553
+ this._context?.onConnectionAuthScopeChanged(connection);
1435
1554
  }
1436
- await this._params.onRemoteDisconnected();
1437
1555
  }
1438
- });
1439
- }
1440
- get peerId() {
1441
- invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1442
- F: __dxlog_file4,
1443
- L: 215,
1444
- S: this,
1445
- A: [
1446
- "this._remotePeerId != null",
1447
- "'Remote peer has not connected yet.'"
1448
- ]
1449
- });
1450
- return this._remotePeerId;
1451
- }
1452
- async shouldAdvertize(params) {
1453
- return this._params.shouldAdvertize(params);
1454
- }
1455
- /**
1456
- * Start exchanging messages with the remote peer.
1457
- * Call after the remote peer has connected.
1458
- */
1459
- async enable() {
1460
- invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1461
- F: __dxlog_file4,
1462
- L: 228,
1463
- S: this,
1464
- A: [
1465
- "this._remotePeerId != null",
1466
- "'Remote peer has not connected yet.'"
1467
- ]
1468
- });
1469
- this._isEnabled = true;
1470
- }
1471
- /**
1472
- * Stop exchanging messages with the remote peer.
1473
- */
1474
- async disable() {
1475
- this._isEnabled = false;
1556
+ }
1476
1557
  }
1477
1558
  };
1478
1559
  export {