@dxos/echo-pipeline 0.6.1-main.c633aeb → 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.
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 +264 -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 +309 -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 +3 -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 +31 -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: {
@@ -652,6 +656,9 @@ var AutomergeHost = class extends Resource2 {
652
656
  get repo() {
653
657
  return this._repo;
654
658
  }
659
+ get loadedDocsCount() {
660
+ return Object.keys(this._repo.handles).length;
661
+ }
655
662
  async addReplicator(replicator) {
656
663
  await this._echoNetworkAdapter.addReplicator(replicator);
657
664
  }
@@ -691,6 +698,56 @@ var AutomergeHost = class extends Resource2 {
691
698
  return this._repo.create(initialValue);
692
699
  }
693
700
  }
701
+ async reIndexHeads(documentIds) {
702
+ for (const documentId of documentIds) {
703
+ log2.info("reindexing heads for document", {
704
+ documentId
705
+ }, {
706
+ F: __dxlog_file3,
707
+ L: 195,
708
+ S: this,
709
+ C: (f, a) => f(...a)
710
+ });
711
+ const handle = this._repo.find(documentId);
712
+ await handle.whenReady([
713
+ "ready",
714
+ "requesting"
715
+ ]);
716
+ if (handle.inState([
717
+ "requesting"
718
+ ])) {
719
+ log2.warn("document is not available locally, skipping", {
720
+ documentId
721
+ }, {
722
+ F: __dxlog_file3,
723
+ L: 199,
724
+ S: this,
725
+ C: (f, a) => f(...a)
726
+ });
727
+ continue;
728
+ }
729
+ const doc = handle.docSync();
730
+ invariant3(doc, void 0, {
731
+ F: __dxlog_file3,
732
+ L: 204,
733
+ S: this,
734
+ A: [
735
+ "doc",
736
+ ""
737
+ ]
738
+ });
739
+ const heads = getHeads(doc);
740
+ const batch = this._db.batch();
741
+ this._headsStore.setHeads(documentId, heads, batch);
742
+ await batch.write();
743
+ }
744
+ log2.info("done reindexing heads", void 0, {
745
+ F: __dxlog_file3,
746
+ L: 211,
747
+ S: this,
748
+ C: (f, a) => f(...a)
749
+ });
750
+ }
694
751
  // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
695
752
  // Hosts, running in the worker, don't share documents unless requested by other peers.
696
753
  // NOTE: If both peers return sharePolicy=false the replication will not happen
@@ -704,7 +761,7 @@ var AutomergeHost = class extends Resource2 {
704
761
  }
705
762
  const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
706
763
  if (isEchoPeerMetadata(peerMetadata)) {
707
- return this._echoNetworkAdapter.shouldAdvertize(peerId, {
764
+ return this._echoNetworkAdapter.shouldAdvertise(peerId, {
708
765
  documentId
709
766
  });
710
767
  }
@@ -875,8 +932,8 @@ import { interpretAsDocumentId } from "@dxos/automerge/automerge-repo";
875
932
  import { cancelWithContext as cancelWithContext2 } from "@dxos/context";
876
933
  import { warnAfterTimeout } from "@dxos/debug";
877
934
  import { SpaceDocVersion } from "@dxos/echo-protocol";
878
- import { invariant as invariant3 } from "@dxos/invariant";
879
- import { log as log2 } from "@dxos/log";
935
+ import { invariant as invariant4 } from "@dxos/invariant";
936
+ import { log as log3 } from "@dxos/log";
880
937
  import { trace as trace2 } from "@dxos/tracing";
881
938
  function _ts_decorate3(decorators, target, key, desc) {
882
939
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -888,7 +945,7 @@ function _ts_decorate3(decorators, target, key, desc) {
888
945
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
889
946
  return c > 3 && r && Object.defineProperty(target, key, r), r;
890
947
  }
891
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
948
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
892
949
  var AutomergeDocumentLoaderImpl = class {
893
950
  constructor(_spaceId, _repo, _spaceKey) {
894
951
  this._spaceId = _spaceId;
@@ -914,8 +971,8 @@ var AutomergeDocumentLoaderImpl = class {
914
971
  }
915
972
  const existingDocHandle = await this._initDocHandle(ctx, spaceState.rootUrl);
916
973
  const doc = existingDocHandle.docSync();
917
- invariant3(doc, void 0, {
918
- F: __dxlog_file3,
974
+ invariant4(doc, void 0, {
975
+ F: __dxlog_file4,
919
976
  L: 84,
920
977
  S: this,
921
978
  A: [
@@ -923,8 +980,8 @@ var AutomergeDocumentLoaderImpl = class {
923
980
  ""
924
981
  ]
925
982
  });
926
- invariant3(doc.version === SpaceDocVersion.CURRENT, void 0, {
927
- F: __dxlog_file3,
983
+ invariant4(doc.version === SpaceDocVersion.CURRENT, void 0, {
984
+ F: __dxlog_file4,
928
985
  L: 85,
929
986
  S: this,
930
987
  A: [
@@ -944,8 +1001,8 @@ var AutomergeDocumentLoaderImpl = class {
944
1001
  let hasUrlsToLoad = false;
945
1002
  const urlsToLoad = {};
946
1003
  for (const objectId of objectIds) {
947
- invariant3(this._spaceRootDocHandle, void 0, {
948
- F: __dxlog_file3,
1004
+ invariant4(this._spaceRootDocHandle, void 0, {
1005
+ F: __dxlog_file4,
949
1006
  L: 97,
950
1007
  S: this,
951
1008
  A: [
@@ -957,8 +1014,8 @@ var AutomergeDocumentLoaderImpl = class {
957
1014
  continue;
958
1015
  }
959
1016
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
960
- invariant3(spaceRootDoc, void 0, {
961
- F: __dxlog_file3,
1017
+ invariant4(spaceRootDoc, void 0, {
1018
+ F: __dxlog_file4,
962
1019
  L: 102,
963
1020
  S: this,
964
1021
  A: [
@@ -969,10 +1026,10 @@ var AutomergeDocumentLoaderImpl = class {
969
1026
  const documentUrl = (spaceRootDoc.links ?? {})[objectId];
970
1027
  if (documentUrl == null) {
971
1028
  this._objectsPendingDocumentLoad.add(objectId);
972
- log2.info("loading delayed until object links are initialized", {
1029
+ log3.info("loading delayed until object links are initialized", {
973
1030
  objectId
974
1031
  }, {
975
- F: __dxlog_file3,
1032
+ F: __dxlog_file4,
976
1033
  L: 106,
977
1034
  S: this,
978
1035
  C: (f, a) => f(...a)
@@ -987,8 +1044,8 @@ var AutomergeDocumentLoaderImpl = class {
987
1044
  }
988
1045
  }
989
1046
  getObjectDocumentId(objectId) {
990
- invariant3(this._spaceRootDocHandle, void 0, {
991
- F: __dxlog_file3,
1047
+ invariant4(this._spaceRootDocHandle, void 0, {
1048
+ F: __dxlog_file4,
992
1049
  L: 118,
993
1050
  S: this,
994
1051
  A: [
@@ -997,8 +1054,8 @@ var AutomergeDocumentLoaderImpl = class {
997
1054
  ]
998
1055
  });
999
1056
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
1000
- invariant3(spaceRootDoc, void 0, {
1001
- F: __dxlog_file3,
1057
+ invariant4(spaceRootDoc, void 0, {
1058
+ F: __dxlog_file4,
1002
1059
  L: 120,
1003
1060
  S: this,
1004
1061
  A: [
@@ -1021,8 +1078,8 @@ var AutomergeDocumentLoaderImpl = class {
1021
1078
  linksAwaitingLoad.forEach(([objectId]) => this._objectsPendingDocumentLoad.delete(objectId));
1022
1079
  }
1023
1080
  getSpaceRootDocHandle() {
1024
- invariant3(this._spaceRootDocHandle, void 0, {
1025
- F: __dxlog_file3,
1081
+ invariant4(this._spaceRootDocHandle, void 0, {
1082
+ F: __dxlog_file4,
1026
1083
  L: 140,
1027
1084
  S: this,
1028
1085
  A: [
@@ -1033,8 +1090,8 @@ var AutomergeDocumentLoaderImpl = class {
1033
1090
  return this._spaceRootDocHandle;
1034
1091
  }
1035
1092
  createDocumentForObject(objectId) {
1036
- invariant3(this._spaceRootDocHandle, void 0, {
1037
- F: __dxlog_file3,
1093
+ invariant4(this._spaceRootDocHandle, void 0, {
1094
+ F: __dxlog_file4,
1038
1095
  L: 145,
1039
1096
  S: this,
1040
1097
  A: [
@@ -1075,11 +1132,11 @@ var AutomergeDocumentLoaderImpl = class {
1075
1132
  };
1076
1133
  const objectDocumentHandle = this._objectDocumentHandles.get(objectId);
1077
1134
  if (objectDocumentHandle != null && objectDocumentHandle.url !== automergeUrl) {
1078
- log2.warn("object already inlined in a different document, ignoring the link", {
1135
+ log3.warn("object already inlined in a different document, ignoring the link", {
1079
1136
  ...logMeta,
1080
1137
  actualDocumentUrl: objectDocumentHandle.url
1081
1138
  }, {
1082
- F: __dxlog_file3,
1139
+ F: __dxlog_file4,
1083
1140
  L: 177,
1084
1141
  S: this,
1085
1142
  C: (f, a) => f(...a)
@@ -1087,8 +1144,8 @@ var AutomergeDocumentLoaderImpl = class {
1087
1144
  continue;
1088
1145
  }
1089
1146
  if (objectDocumentHandle?.url === automergeUrl) {
1090
- log2.warn("object document was already loaded", logMeta, {
1091
- F: __dxlog_file3,
1147
+ log3.warn("object document was already loaded", logMeta, {
1148
+ F: __dxlog_file4,
1092
1149
  L: 184,
1093
1150
  S: this,
1094
1151
  C: (f, a) => f(...a)
@@ -1096,8 +1153,8 @@ var AutomergeDocumentLoaderImpl = class {
1096
1153
  continue;
1097
1154
  }
1098
1155
  const handle = this._repo.find(automergeUrl);
1099
- log2.debug("document loading triggered", logMeta, {
1100
- F: __dxlog_file3,
1156
+ log3.debug("document loading triggered", logMeta, {
1157
+ F: __dxlog_file4,
1101
1158
  L: 188,
1102
1159
  S: this,
1103
1160
  C: (f, a) => f(...a)
@@ -1116,11 +1173,11 @@ var AutomergeDocumentLoaderImpl = class {
1116
1173
  break;
1117
1174
  } catch (err) {
1118
1175
  if (`${err}`.includes("Timeout")) {
1119
- log2.info("wraparound", {
1176
+ log3.info("wraparound", {
1120
1177
  id: docHandle.documentId,
1121
1178
  state: docHandle.state
1122
1179
  }, {
1123
- F: __dxlog_file3,
1180
+ F: __dxlog_file4,
1124
1181
  L: 204,
1125
1182
  S: this,
1126
1183
  C: (f, a) => f(...a)
@@ -1145,16 +1202,14 @@ var AutomergeDocumentLoaderImpl = class {
1145
1202
  }
1146
1203
  async _createObjectOnDocumentLoad(handle, objectId) {
1147
1204
  try {
1148
- await handle.doc([
1149
- "ready"
1150
- ]);
1205
+ await handle.whenReady();
1151
1206
  const logMeta = {
1152
1207
  objectId,
1153
1208
  docUrl: handle.url
1154
1209
  };
1155
1210
  if (this.onObjectDocumentLoaded.listenerCount() === 0) {
1156
- log2.info("document loaded after all listeners were removed", logMeta, {
1157
- F: __dxlog_file3,
1211
+ log3.info("document loaded after all listeners were removed", logMeta, {
1212
+ F: __dxlog_file4,
1158
1213
  L: 231,
1159
1214
  S: this,
1160
1215
  C: (f, a) => f(...a)
@@ -1163,8 +1218,8 @@ var AutomergeDocumentLoaderImpl = class {
1163
1218
  }
1164
1219
  const objectDocHandle = this._objectDocumentHandles.get(objectId);
1165
1220
  if (objectDocHandle?.url !== handle.url) {
1166
- log2.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1167
- F: __dxlog_file3,
1221
+ log3.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1222
+ F: __dxlog_file4,
1168
1223
  L: 236,
1169
1224
  S: this,
1170
1225
  C: (f, a) => f(...a)
@@ -1177,13 +1232,13 @@ var AutomergeDocumentLoaderImpl = class {
1177
1232
  });
1178
1233
  } catch (err) {
1179
1234
  const shouldRetryLoading = this.onObjectDocumentLoaded.listenerCount() > 0;
1180
- log2.warn("failed to load a document", {
1235
+ log3.warn("failed to load a document", {
1181
1236
  objectId,
1182
1237
  automergeUrl: handle.url,
1183
1238
  retryLoading: shouldRetryLoading,
1184
1239
  err
1185
1240
  }, {
1186
- F: __dxlog_file3,
1241
+ F: __dxlog_file4,
1187
1242
  L: 242,
1188
1243
  S: this,
1189
1244
  C: (f, a) => f(...a)
@@ -1204,14 +1259,134 @@ AutomergeDocumentLoaderImpl = _ts_decorate3([
1204
1259
  ], AutomergeDocumentLoaderImpl);
1205
1260
 
1206
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
1207
1268
  import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
1208
1269
  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";
1270
+ import { invariant as invariant5 } from "@dxos/invariant";
1271
+ import { log as log4 } from "@dxos/log";
1212
1272
  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";
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";
1215
1390
  var MeshEchoReplicator = class {
1216
1391
  constructor() {
1217
1392
  this._connections = /* @__PURE__ */ new Set();
@@ -1236,10 +1411,10 @@ var MeshEchoReplicator = class {
1236
1411
  this._connectionsPerPeer.clear();
1237
1412
  this._context = null;
1238
1413
  }
1239
- createExtension() {
1240
- invariant4(this._context, void 0, {
1241
- F: __dxlog_file4,
1242
- L: 54,
1414
+ createExtension(extensionFactory) {
1415
+ invariant6(this._context, void 0, {
1416
+ F: __dxlog_file6,
1417
+ L: 51,
1243
1418
  S: this,
1244
1419
  A: [
1245
1420
  "this._context",
@@ -1248,18 +1423,19 @@ var MeshEchoReplicator = class {
1248
1423
  });
1249
1424
  const connection = new MeshReplicatorConnection({
1250
1425
  ownPeerId: this._context.peerId,
1426
+ replicatorFactory: extensionFactory,
1251
1427
  onRemoteConnected: async () => {
1252
- log3("onRemoteConnected", {
1428
+ log5("onRemoteConnected", {
1253
1429
  peerId: connection.peerId
1254
1430
  }, {
1255
- F: __dxlog_file4,
1256
- L: 59,
1431
+ F: __dxlog_file6,
1432
+ L: 57,
1257
1433
  S: this,
1258
1434
  C: (f, a) => f(...a)
1259
1435
  });
1260
- invariant4(this._context, void 0, {
1261
- F: __dxlog_file4,
1262
- L: 60,
1436
+ invariant6(this._context, void 0, {
1437
+ F: __dxlog_file6,
1438
+ L: 58,
1263
1439
  S: this,
1264
1440
  A: [
1265
1441
  "this._context",
@@ -1271,36 +1447,36 @@ var MeshEchoReplicator = class {
1271
1447
  } else {
1272
1448
  this._connectionsPerPeer.set(connection.peerId, connection);
1273
1449
  this._context.onConnectionOpen(connection);
1274
- await connection.enable();
1450
+ connection.enable();
1275
1451
  }
1276
1452
  },
1277
1453
  onRemoteDisconnected: async () => {
1278
- log3("onRemoteDisconnected", {
1454
+ log5("onRemoteDisconnected", {
1279
1455
  peerId: connection.peerId
1280
1456
  }, {
1281
- F: __dxlog_file4,
1282
- L: 71,
1457
+ F: __dxlog_file6,
1458
+ L: 69,
1283
1459
  S: this,
1284
1460
  C: (f, a) => f(...a)
1285
1461
  });
1286
1462
  this._context?.onConnectionClosed(connection);
1287
1463
  this._connectionsPerPeer.delete(connection.peerId);
1288
- await connection.disable();
1464
+ connection.disable();
1289
1465
  this._connections.delete(connection);
1290
1466
  },
1291
- shouldAdvertize: async (params) => {
1292
- log3("shouldAdvertize", {
1467
+ shouldAdvertise: async (params) => {
1468
+ log5("shouldAdvertise", {
1293
1469
  peerId: connection.peerId,
1294
1470
  documentId: params.documentId
1295
1471
  }, {
1296
- F: __dxlog_file4,
1297
- L: 78,
1472
+ F: __dxlog_file6,
1473
+ L: 76,
1298
1474
  S: this,
1299
1475
  C: (f, a) => f(...a)
1300
1476
  });
1301
- invariant4(this._context, void 0, {
1302
- F: __dxlog_file4,
1303
- L: 79,
1477
+ invariant6(this._context, void 0, {
1478
+ F: __dxlog_file6,
1479
+ L: 77,
1304
1480
  S: this,
1305
1481
  A: [
1306
1482
  "this._context",
@@ -1310,12 +1486,12 @@ var MeshEchoReplicator = class {
1310
1486
  try {
1311
1487
  const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
1312
1488
  if (!spaceKey) {
1313
- log3("space key not found for share policy check", {
1489
+ log5("space key not found for share policy check", {
1314
1490
  peerId: connection.peerId,
1315
1491
  documentId: params.documentId
1316
1492
  }, {
1317
- F: __dxlog_file4,
1318
- L: 83,
1493
+ F: __dxlog_file6,
1494
+ L: 81,
1319
1495
  S: this,
1320
1496
  C: (f, a) => f(...a)
1321
1497
  });
@@ -1323,19 +1499,19 @@ var MeshEchoReplicator = class {
1323
1499
  }
1324
1500
  const authorizedDevices = this._authorizedDevices.get(spaceKey);
1325
1501
  if (!connection.remoteDeviceKey) {
1326
- log3("device key not found for share policy check", {
1502
+ log5("device key not found for share policy check", {
1327
1503
  peerId: connection.peerId,
1328
1504
  documentId: params.documentId
1329
1505
  }, {
1330
- F: __dxlog_file4,
1331
- L: 93,
1506
+ F: __dxlog_file6,
1507
+ L: 91,
1332
1508
  S: this,
1333
1509
  C: (f, a) => f(...a)
1334
1510
  });
1335
1511
  return false;
1336
1512
  }
1337
1513
  const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1338
- log3("share policy check", {
1514
+ log5("share policy check", {
1339
1515
  localPeer: this._context.peerId,
1340
1516
  remotePeer: connection.peerId,
1341
1517
  documentId: params.documentId,
@@ -1343,16 +1519,16 @@ var MeshEchoReplicator = class {
1343
1519
  spaceKey,
1344
1520
  isAuthorized
1345
1521
  }, {
1346
- F: __dxlog_file4,
1347
- L: 101,
1522
+ F: __dxlog_file6,
1523
+ L: 99,
1348
1524
  S: this,
1349
1525
  C: (f, a) => f(...a)
1350
1526
  });
1351
1527
  return isAuthorized;
1352
1528
  } catch (err) {
1353
- log3.catch(err, void 0, {
1354
- F: __dxlog_file4,
1355
- L: 111,
1529
+ log5.catch(err, void 0, {
1530
+ F: __dxlog_file6,
1531
+ L: 109,
1356
1532
  S: this,
1357
1533
  C: (f, a) => f(...a)
1358
1534
  });
@@ -1364,115 +1540,23 @@ var MeshEchoReplicator = class {
1364
1540
  return connection.replicatorExtension;
1365
1541
  }
1366
1542
  authorizeDevice(spaceKey, deviceKey) {
1367
- log3("authorizeDevice", {
1543
+ log5("authorizeDevice", {
1368
1544
  spaceKey,
1369
1545
  deviceKey
1370
1546
  }, {
1371
- F: __dxlog_file4,
1372
- L: 122,
1547
+ F: __dxlog_file6,
1548
+ L: 120,
1373
1549
  S: this,
1374
1550
  C: (f, a) => f(...a)
1375
1551
  });
1376
1552
  defaultMap(this._authorizedDevices, spaceKey, () => new ComplexSet(PublicKey2.hash)).add(deviceKey);
1377
1553
  for (const connection of this._connections) {
1378
1554
  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;
1555
+ if (this._connectionsPerPeer.has(connection.peerId)) {
1556
+ this._context?.onConnectionAuthScopeChanged(connection);
1435
1557
  }
1436
- await this._params.onRemoteDisconnected();
1437
1558
  }
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;
1559
+ }
1476
1560
  }
1477
1561
  };
1478
1562
  export {