@dxos/echo-pipeline 0.6.0 → 0.6.1-main.09a92b0

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 (42) 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 +323 -211
  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 +364 -258
  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 +13 -9
  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.d.ts.map +1 -1
  17. package/dist/types/src/automerge/echo-network-adapter.test.d.ts +2 -0
  18. package/dist/types/src/automerge/echo-network-adapter.test.d.ts.map +1 -0
  19. package/dist/types/src/automerge/echo-replicator.d.ts +5 -6
  20. package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
  21. package/dist/types/src/automerge/heads-store.d.ts +13 -0
  22. package/dist/types/src/automerge/heads-store.d.ts.map +1 -0
  23. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +35 -0
  24. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -0
  25. package/dist/types/src/automerge/mesh-echo-replicator.d.ts +2 -2
  26. package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
  27. package/dist/types/src/db-host/data-service.d.ts +4 -2
  28. package/dist/types/src/db-host/data-service.d.ts.map +1 -1
  29. package/package.json +33 -33
  30. package/src/automerge/automerge-doc-loader.ts +1 -1
  31. package/src/automerge/automerge-host.test.ts +34 -17
  32. package/src/automerge/automerge-host.ts +61 -17
  33. package/src/automerge/automerge-repo.test.ts +76 -1
  34. package/src/automerge/echo-network-adapter.test.ts +131 -0
  35. package/src/automerge/echo-network-adapter.ts +10 -6
  36. package/src/automerge/echo-replicator.ts +6 -9
  37. package/src/automerge/heads-store.ts +39 -0
  38. package/src/automerge/mesh-echo-replicator-connection.ts +130 -0
  39. package/src/automerge/mesh-echo-replicator.ts +15 -123
  40. package/src/db-host/data-service.ts +22 -2
  41. package/dist/lib/browser/chunk-HS77A4I4.mjs.map +0 -7
  42. package/dist/lib/node/chunk-Y5U7UXEL.cjs.map +0 -7
@@ -18,73 +18,78 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var node_exports = {};
20
20
  __export(node_exports, {
21
- AuthExtension: () => import_chunk_Y5U7UXEL.AuthExtension,
22
- AuthStatus: () => import_chunk_Y5U7UXEL.AuthStatus,
21
+ AuthExtension: () => import_chunk_GHBIMYZK.AuthExtension,
22
+ AuthStatus: () => import_chunk_GHBIMYZK.AuthStatus,
23
23
  AutomergeDocumentLoaderImpl: () => AutomergeDocumentLoaderImpl,
24
24
  AutomergeHost: () => AutomergeHost,
25
- CredentialRetrieverExtension: () => import_chunk_Y5U7UXEL.CredentialRetrieverExtension,
26
- CredentialServerExtension: () => import_chunk_Y5U7UXEL.CredentialServerExtension,
27
- DataServiceImpl: () => import_chunk_Y5U7UXEL.DataServiceImpl,
25
+ CredentialRetrieverExtension: () => import_chunk_GHBIMYZK.CredentialRetrieverExtension,
26
+ CredentialServerExtension: () => import_chunk_GHBIMYZK.CredentialServerExtension,
27
+ DataServiceImpl: () => import_chunk_GHBIMYZK.DataServiceImpl,
28
28
  LevelDBStorageAdapter: () => LevelDBStorageAdapter,
29
29
  LocalHostNetworkAdapter: () => LocalHostNetworkAdapter,
30
- MOCK_AUTH_PROVIDER: () => import_chunk_Y5U7UXEL.MOCK_AUTH_PROVIDER,
31
- MOCK_AUTH_VERIFIER: () => import_chunk_Y5U7UXEL.MOCK_AUTH_VERIFIER,
30
+ MOCK_AUTH_PROVIDER: () => import_chunk_GHBIMYZK.MOCK_AUTH_PROVIDER,
31
+ MOCK_AUTH_VERIFIER: () => import_chunk_GHBIMYZK.MOCK_AUTH_VERIFIER,
32
32
  MeshEchoReplicator: () => MeshEchoReplicator,
33
- MetadataStore: () => import_chunk_Y5U7UXEL.MetadataStore,
34
- Pipeline: () => import_chunk_Y5U7UXEL.Pipeline,
35
- SnapshotManager: () => import_chunk_Y5U7UXEL.SnapshotManager,
36
- SnapshotStore: () => import_chunk_Y5U7UXEL.SnapshotStore,
37
- Space: () => import_chunk_Y5U7UXEL.Space,
38
- SpaceManager: () => import_chunk_Y5U7UXEL.SpaceManager,
39
- SpaceProtocol: () => import_chunk_Y5U7UXEL.SpaceProtocol,
40
- SpaceProtocolSession: () => import_chunk_Y5U7UXEL.SpaceProtocolSession,
41
- TimeframeClock: () => import_chunk_Y5U7UXEL.TimeframeClock,
42
- codec: () => import_chunk_Y5U7UXEL.codec,
43
- createIdFromSpaceKey: () => import_chunk_Y5U7UXEL.createIdFromSpaceKey,
44
- createMappedFeedWriter: () => import_chunk_Y5U7UXEL.createMappedFeedWriter,
33
+ MetadataStore: () => import_chunk_GHBIMYZK.MetadataStore,
34
+ Pipeline: () => import_chunk_GHBIMYZK.Pipeline,
35
+ SnapshotManager: () => import_chunk_GHBIMYZK.SnapshotManager,
36
+ SnapshotStore: () => import_chunk_GHBIMYZK.SnapshotStore,
37
+ Space: () => import_chunk_GHBIMYZK.Space,
38
+ SpaceManager: () => import_chunk_GHBIMYZK.SpaceManager,
39
+ SpaceProtocol: () => import_chunk_GHBIMYZK.SpaceProtocol,
40
+ SpaceProtocolSession: () => import_chunk_GHBIMYZK.SpaceProtocolSession,
41
+ TimeframeClock: () => import_chunk_GHBIMYZK.TimeframeClock,
42
+ codec: () => import_chunk_GHBIMYZK.codec,
43
+ createIdFromSpaceKey: () => import_chunk_GHBIMYZK.createIdFromSpaceKey,
44
+ createMappedFeedWriter: () => import_chunk_GHBIMYZK.createMappedFeedWriter,
45
45
  encodingOptions: () => encodingOptions,
46
46
  getSpaceKeyFromDoc: () => getSpaceKeyFromDoc,
47
- hasInvitationExpired: () => import_chunk_Y5U7UXEL.hasInvitationExpired,
48
- mapFeedIndexesToTimeframe: () => import_chunk_Y5U7UXEL.mapFeedIndexesToTimeframe,
49
- mapTimeframeToFeedIndexes: () => import_chunk_Y5U7UXEL.mapTimeframeToFeedIndexes,
50
- startAfter: () => import_chunk_Y5U7UXEL.startAfter,
51
- valueEncoding: () => import_chunk_Y5U7UXEL.valueEncoding
47
+ hasInvitationExpired: () => import_chunk_GHBIMYZK.hasInvitationExpired,
48
+ mapFeedIndexesToTimeframe: () => import_chunk_GHBIMYZK.mapFeedIndexesToTimeframe,
49
+ mapTimeframeToFeedIndexes: () => import_chunk_GHBIMYZK.mapTimeframeToFeedIndexes,
50
+ startAfter: () => import_chunk_GHBIMYZK.startAfter,
51
+ valueEncoding: () => import_chunk_GHBIMYZK.valueEncoding
52
52
  });
53
53
  module.exports = __toCommonJS(node_exports);
54
- var import_chunk_Y5U7UXEL = require("./chunk-Y5U7UXEL.cjs");
54
+ var import_chunk_GHBIMYZK = require("./chunk-GHBIMYZK.cjs");
55
55
  var import_async = require("@dxos/async");
56
56
  var import_automerge = require("@dxos/automerge/automerge");
57
57
  var import_automerge_repo = require("@dxos/automerge/automerge-repo");
58
58
  var import_context = require("@dxos/context");
59
+ var import_invariant = require("@dxos/invariant");
59
60
  var import_keys = require("@dxos/keys");
61
+ var import_log = require("@dxos/log");
60
62
  var import_protocols = require("@dxos/protocols");
61
63
  var import_tracing = require("@dxos/tracing");
62
64
  var import_util = require("@dxos/util");
63
65
  var import_async2 = require("@dxos/async");
64
66
  var import_automerge_repo2 = require("@dxos/automerge/automerge-repo");
65
67
  var import_context2 = require("@dxos/context");
66
- var import_invariant = require("@dxos/invariant");
67
- var import_log = require("@dxos/log");
68
+ var import_invariant2 = require("@dxos/invariant");
69
+ var import_log2 = require("@dxos/log");
70
+ var import_indexing = require("@dxos/indexing");
68
71
  var import_context3 = require("@dxos/context");
69
72
  var import_async3 = require("@dxos/async");
70
73
  var import_automerge_repo3 = require("@dxos/automerge/automerge-repo");
71
74
  var import_codec_protobuf = require("@dxos/codec-protobuf");
72
- var import_invariant2 = require("@dxos/invariant");
75
+ var import_invariant3 = require("@dxos/invariant");
73
76
  var import_async4 = require("@dxos/async");
74
77
  var import_automerge_repo4 = require("@dxos/automerge/automerge-repo");
75
78
  var import_context4 = require("@dxos/context");
76
79
  var import_debug = require("@dxos/debug");
77
80
  var import_echo_protocol = require("@dxos/echo-protocol");
78
- var import_invariant3 = require("@dxos/invariant");
79
- var import_log2 = require("@dxos/log");
81
+ var import_invariant4 = require("@dxos/invariant");
82
+ var import_log3 = require("@dxos/log");
80
83
  var import_tracing2 = require("@dxos/tracing");
84
+ var import_invariant5 = require("@dxos/invariant");
85
+ var import_keys2 = require("@dxos/keys");
86
+ var import_log4 = require("@dxos/log");
87
+ var import_util2 = require("@dxos/util");
81
88
  var import_automerge_repo5 = require("@dxos/automerge/automerge-repo");
82
89
  var import_context5 = require("@dxos/context");
83
- var import_invariant4 = require("@dxos/invariant");
84
- var import_keys2 = require("@dxos/keys");
85
- var import_log3 = require("@dxos/log");
90
+ var import_invariant6 = require("@dxos/invariant");
91
+ var import_log5 = require("@dxos/log");
86
92
  var import_teleport_extension_automerge_replicator = require("@dxos/teleport-extension-automerge-replicator");
87
- var import_util2 = require("@dxos/util");
88
93
  function _ts_decorate(decorators, target, key, desc) {
89
94
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
90
95
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -117,7 +122,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
117
122
  }
118
123
  connectionEntry.writer.write(message).catch((err) => {
119
124
  if (connectionEntry.isOpen) {
120
- import_log.log.catch(err, void 0, {
125
+ import_log2.log.catch(err, void 0, {
121
126
  F: __dxlog_file,
122
127
  L: 49,
123
128
  S: this,
@@ -129,19 +134,13 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
129
134
  disconnect() {
130
135
  }
131
136
  async open() {
132
- (0, import_invariant.invariant)(this._lifecycleState === import_context2.LifecycleState.CLOSED, void 0, {
133
- F: __dxlog_file,
134
- L: 60,
135
- S: this,
136
- A: [
137
- "this._lifecycleState === LifecycleState.CLOSED",
138
- ""
139
- ]
140
- });
137
+ if (this._lifecycleState === import_context2.LifecycleState.OPEN) {
138
+ return;
139
+ }
141
140
  this._lifecycleState = import_context2.LifecycleState.OPEN;
142
- (0, import_log.log)("emit ready", void 0, {
141
+ (0, import_log2.log)("emit ready", void 0, {
143
142
  F: __dxlog_file,
144
- L: 63,
143
+ L: 65,
145
144
  S: this,
146
145
  C: (f, a) => f(...a)
147
146
  });
@@ -150,15 +149,9 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
150
149
  });
151
150
  }
152
151
  async close() {
153
- (0, import_invariant.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
154
- F: __dxlog_file,
155
- L: 71,
156
- S: this,
157
- A: [
158
- "this._lifecycleState === LifecycleState.OPEN",
159
- ""
160
- ]
161
- });
152
+ if (this._lifecycleState === import_context2.LifecycleState.CLOSED) {
153
+ return;
154
+ }
162
155
  for (const replicator of this._replicators) {
163
156
  await replicator.disconnect();
164
157
  }
@@ -171,27 +164,27 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
171
164
  });
172
165
  }
173
166
  async addReplicator(replicator) {
174
- (0, import_invariant.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
167
+ (0, import_invariant2.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
175
168
  F: __dxlog_file,
176
- L: 87,
169
+ L: 91,
177
170
  S: this,
178
171
  A: [
179
172
  "this._lifecycleState === LifecycleState.OPEN",
180
173
  ""
181
174
  ]
182
175
  });
183
- (0, import_invariant.invariant)(this.peerId, void 0, {
176
+ (0, import_invariant2.invariant)(this.peerId, void 0, {
184
177
  F: __dxlog_file,
185
- L: 88,
178
+ L: 92,
186
179
  S: this,
187
180
  A: [
188
181
  "this.peerId",
189
182
  ""
190
183
  ]
191
184
  });
192
- (0, import_invariant.invariant)(!this._replicators.has(replicator), void 0, {
185
+ (0, import_invariant2.invariant)(!this._replicators.has(replicator), void 0, {
193
186
  F: __dxlog_file,
194
- L: 89,
187
+ L: 93,
195
188
  S: this,
196
189
  A: [
197
190
  "!this._replicators.has(replicator)",
@@ -208,18 +201,18 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
208
201
  });
209
202
  }
210
203
  async removeReplicator(replicator) {
211
- (0, import_invariant.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
204
+ (0, import_invariant2.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
212
205
  F: __dxlog_file,
213
- L: 103,
206
+ L: 107,
214
207
  S: this,
215
208
  A: [
216
209
  "this._lifecycleState === LifecycleState.OPEN",
217
210
  ""
218
211
  ]
219
212
  });
220
- (0, import_invariant.invariant)(this._replicators.has(replicator), void 0, {
213
+ (0, import_invariant2.invariant)(this._replicators.has(replicator), void 0, {
221
214
  F: __dxlog_file,
222
- L: 104,
215
+ L: 108,
223
216
  S: this,
224
217
  A: [
225
218
  "this._replicators.has(replicator)",
@@ -229,25 +222,25 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
229
222
  await replicator.disconnect();
230
223
  this._replicators.delete(replicator);
231
224
  }
232
- async shouldAdvertize(peerId, params) {
225
+ async shouldAdvertise(peerId, params) {
233
226
  const connection = this._connections.get(peerId);
234
227
  if (!connection) {
235
228
  return false;
236
229
  }
237
- return connection.connection.shouldAdvertize(params);
230
+ return connection.connection.shouldAdvertise(params);
238
231
  }
239
232
  _onConnectionOpen(connection) {
240
- (0, import_log.log)("Connection opened", {
233
+ (0, import_log2.log)("Connection opened", {
241
234
  peerId: connection.peerId
242
235
  }, {
243
236
  F: __dxlog_file,
244
- L: 119,
237
+ L: 123,
245
238
  S: this,
246
239
  C: (f, a) => f(...a)
247
240
  });
248
- (0, import_invariant.invariant)(!this._connections.has(connection.peerId), void 0, {
241
+ (0, import_invariant2.invariant)(!this._connections.has(connection.peerId), void 0, {
249
242
  F: __dxlog_file,
250
- L: 120,
243
+ L: 124,
251
244
  S: this,
252
245
  A: [
253
246
  "!this._connections.has(connection.peerId as PeerId)",
@@ -274,20 +267,20 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
274
267
  }
275
268
  } catch (err) {
276
269
  if (connectionEntry.isOpen) {
277
- import_log.log.catch(err, void 0, {
270
+ import_log2.log.catch(err, void 0, {
278
271
  F: __dxlog_file,
279
- L: 139,
272
+ L: 143,
280
273
  S: this,
281
274
  C: (f, a) => f(...a)
282
275
  });
283
276
  }
284
277
  }
285
278
  });
286
- (0, import_log.log)("emit peer-candidate", {
279
+ (0, import_log2.log)("emit peer-candidate", {
287
280
  peerId: connection.peerId
288
281
  }, {
289
282
  F: __dxlog_file,
290
- L: 144,
283
+ L: 148,
291
284
  S: this,
292
285
  C: (f, a) => f(...a)
293
286
  });
@@ -298,18 +291,18 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
298
291
  * TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
299
292
  */
300
293
  _onConnectionAuthScopeChanged(connection) {
301
- (0, import_log.log)("Connection auth scope changed", {
294
+ (0, import_log2.log)("Connection auth scope changed", {
302
295
  peerId: connection.peerId
303
296
  }, {
304
297
  F: __dxlog_file,
305
- L: 153,
298
+ L: 157,
306
299
  S: this,
307
300
  C: (f, a) => f(...a)
308
301
  });
309
302
  const entry = this._connections.get(connection.peerId);
310
- (0, import_invariant.invariant)(entry, void 0, {
303
+ (0, import_invariant2.invariant)(entry, void 0, {
311
304
  F: __dxlog_file,
312
- L: 155,
305
+ L: 159,
313
306
  S: this,
314
307
  A: [
315
308
  "entry",
@@ -322,18 +315,18 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
322
315
  this._emitPeerCandidate(connection);
323
316
  }
324
317
  _onConnectionClosed(connection) {
325
- (0, import_log.log)("Connection closed", {
318
+ (0, import_log2.log)("Connection closed", {
326
319
  peerId: connection.peerId
327
320
  }, {
328
321
  F: __dxlog_file,
329
- L: 161,
322
+ L: 165,
330
323
  S: this,
331
324
  C: (f, a) => f(...a)
332
325
  });
333
326
  const entry = this._connections.get(connection.peerId);
334
- (0, import_invariant.invariant)(entry, void 0, {
327
+ (0, import_invariant2.invariant)(entry, void 0, {
335
328
  F: __dxlog_file,
336
- L: 163,
329
+ L: 167,
337
330
  S: this,
338
331
  A: [
339
332
  "entry",
@@ -344,15 +337,15 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
344
337
  this.emit("peer-disconnected", {
345
338
  peerId: connection.peerId
346
339
  });
347
- void entry.reader.cancel().catch((err) => import_log.log.catch(err, void 0, {
340
+ void entry.reader.cancel().catch((err) => import_log2.log.catch(err, void 0, {
348
341
  F: __dxlog_file,
349
- L: 168,
342
+ L: 172,
350
343
  S: this,
351
344
  C: (f, a) => f(...a)
352
345
  }));
353
- void entry.writer.abort().catch((err) => import_log.log.catch(err, void 0, {
346
+ void entry.writer.abort().catch((err) => import_log2.log.catch(err, void 0, {
354
347
  F: __dxlog_file,
355
- L: 169,
348
+ L: 173,
356
349
  S: this,
357
350
  C: (f, a) => f(...a)
358
351
  }));
@@ -382,6 +375,31 @@ var createEchoPeerMetadata = () => ({
382
375
  dxos_peerSource: "EchoNetworkAdapter"
383
376
  });
384
377
  var isEchoPeerMetadata = (metadata) => metadata?.dxos_peerSource === "EchoNetworkAdapter";
378
+ var HeadsStore = class {
379
+ constructor({ db }) {
380
+ this._db = db;
381
+ }
382
+ setHeads(documentId, heads, batch) {
383
+ batch.put(documentId, heads, {
384
+ sublevel: this._db,
385
+ keyEncoding: "utf8",
386
+ valueEncoding: import_indexing.headsEncoding
387
+ });
388
+ }
389
+ async getHeads(documentId) {
390
+ try {
391
+ return await this._db.get(documentId, {
392
+ keyEncoding: "utf8",
393
+ valueEncoding: import_indexing.headsEncoding
394
+ });
395
+ } catch (err) {
396
+ if (err.notFound) {
397
+ return void 0;
398
+ }
399
+ throw err;
400
+ }
401
+ }
402
+ };
385
403
  var LevelDBStorageAdapter = class extends import_context3.Resource {
386
404
  constructor(_params) {
387
405
  super();
@@ -503,7 +521,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo3.NetworkAdapte
503
521
  }
504
522
  send(message) {
505
523
  const peer = this._peers.get(message.targetId);
506
- (0, import_invariant2.invariant)(peer, "Peer not found.", {
524
+ (0, import_invariant3.invariant)(peer, "Peer not found.", {
507
525
  F: __dxlog_file2,
508
526
  L: 51,
509
527
  S: this,
@@ -528,7 +546,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo3.NetworkAdapte
528
546
  syncRepo({ id, syncMessage }) {
529
547
  const peerId = this._getPeerId(id);
530
548
  return new import_codec_protobuf.Stream(({ next, close }) => {
531
- (0, import_invariant2.invariant)(!this._peers.has(peerId), "Peer already connected.", {
549
+ (0, import_invariant3.invariant)(!this._peers.has(peerId), "Peer already connected.", {
532
550
  F: __dxlog_file2,
533
551
  L: 73,
534
552
  S: this,
@@ -552,7 +570,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo3.NetworkAdapte
552
570
  });
553
571
  }
554
572
  });
555
- (0, import_invariant2.invariant)(this._isConnected, void 0, {
573
+ (0, import_invariant3.invariant)(this._isConnected, void 0, {
556
574
  F: __dxlog_file2,
557
575
  L: 90,
558
576
  S: this,
@@ -568,7 +586,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo3.NetworkAdapte
568
586
  });
569
587
  }
570
588
  async sendSyncMessage({ id, syncMessage }) {
571
- (0, import_invariant2.invariant)(this._isConnected, void 0, {
589
+ (0, import_invariant3.invariant)(this._isConnected, void 0, {
572
590
  F: __dxlog_file2,
573
591
  L: 99,
574
592
  S: this,
@@ -581,7 +599,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo3.NetworkAdapte
581
599
  this.emit("message", message);
582
600
  }
583
601
  async getHostInfo() {
584
- (0, import_invariant2.invariant)(this._isConnected, void 0, {
602
+ (0, import_invariant3.invariant)(this._isConnected, void 0, {
585
603
  F: __dxlog_file2,
586
604
  L: 105,
587
605
  S: this,
@@ -590,7 +608,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo3.NetworkAdapte
590
608
  ""
591
609
  ]
592
610
  });
593
- (0, import_invariant2.invariant)(this.peerId, "Peer id not set.", {
611
+ (0, import_invariant3.invariant)(this.peerId, "Peer id not set.", {
594
612
  F: __dxlog_file2,
595
613
  L: 106,
596
614
  S: this,
@@ -618,25 +636,26 @@ function _ts_decorate2(decorators, target, key, desc) {
618
636
  return c > 3 && r && Object.defineProperty(target, key, r), r;
619
637
  }
620
638
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
621
- var AutomergeHost = class {
639
+ var AutomergeHost = class extends import_context.Resource {
622
640
  constructor({ db, indexMetadataStore }) {
623
- this._ctx = new import_context.Context(void 0, {
624
- F: __dxlog_file3,
625
- L: 71
626
- });
641
+ super();
627
642
  this._echoNetworkAdapter = new EchoNetworkAdapter({
628
643
  getContainingSpaceForDocument: this._getContainingSpaceForDocument.bind(this)
629
644
  });
645
+ this._db = db;
630
646
  this._storage = new LevelDBStorageAdapter({
631
- db,
647
+ db: db.sublevel("automerge"),
632
648
  callbacks: {
633
649
  beforeSave: async (params) => this._beforeSave(params),
634
650
  afterSave: async () => this._afterSave()
635
651
  }
636
652
  });
653
+ this._headsStore = new HeadsStore({
654
+ db: db.sublevel("heads")
655
+ });
637
656
  this._indexMetadataStore = indexMetadataStore;
638
657
  }
639
- async open() {
658
+ async _open() {
640
659
  this._peerId = `host-${import_keys.PublicKey.random().toHex()}`;
641
660
  await this._storage.open?.();
642
661
  this._clientNetwork = new LocalHostNetworkAdapter();
@@ -656,7 +675,7 @@ var AutomergeHost = class {
656
675
  await this._clientNetwork.whenConnected();
657
676
  await this._echoNetworkAdapter.whenConnected();
658
677
  }
659
- async close() {
678
+ async _close() {
660
679
  await this._storage.close?.();
661
680
  await this._clientNetwork.close();
662
681
  await this._echoNetworkAdapter.close();
@@ -668,6 +687,9 @@ var AutomergeHost = class {
668
687
  get repo() {
669
688
  return this._repo;
670
689
  }
690
+ get loadedDocsCount() {
691
+ return Object.keys(this._repo.handles).length;
692
+ }
671
693
  async addReplicator(replicator) {
672
694
  await this._echoNetworkAdapter.addReplicator(replicator);
673
695
  }
@@ -707,6 +729,56 @@ var AutomergeHost = class {
707
729
  return this._repo.create(initialValue);
708
730
  }
709
731
  }
732
+ async reIndexHeads(documentIds) {
733
+ for (const documentId of documentIds) {
734
+ import_log.log.info("reindexing heads for document", {
735
+ documentId
736
+ }, {
737
+ F: __dxlog_file3,
738
+ L: 195,
739
+ S: this,
740
+ C: (f, a) => f(...a)
741
+ });
742
+ const handle = this._repo.find(documentId);
743
+ await handle.whenReady([
744
+ "ready",
745
+ "requesting"
746
+ ]);
747
+ if (handle.inState([
748
+ "requesting"
749
+ ])) {
750
+ import_log.log.warn("document is not available locally, skipping", {
751
+ documentId
752
+ }, {
753
+ F: __dxlog_file3,
754
+ L: 199,
755
+ S: this,
756
+ C: (f, a) => f(...a)
757
+ });
758
+ continue;
759
+ }
760
+ const doc = handle.docSync();
761
+ (0, import_invariant.invariant)(doc, void 0, {
762
+ F: __dxlog_file3,
763
+ L: 204,
764
+ S: this,
765
+ A: [
766
+ "doc",
767
+ ""
768
+ ]
769
+ });
770
+ const heads = (0, import_automerge.getHeads)(doc);
771
+ const batch = this._db.batch();
772
+ this._headsStore.setHeads(documentId, heads, batch);
773
+ await batch.write();
774
+ }
775
+ import_log.log.info("done reindexing heads", void 0, {
776
+ F: __dxlog_file3,
777
+ L: 211,
778
+ S: this,
779
+ C: (f, a) => f(...a)
780
+ });
781
+ }
710
782
  // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
711
783
  // Hosts, running in the worker, don't share documents unless requested by other peers.
712
784
  // NOTE: If both peers return sharePolicy=false the replication will not happen
@@ -720,7 +792,7 @@ var AutomergeHost = class {
720
792
  }
721
793
  const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
722
794
  if (isEchoPeerMetadata(peerMetadata)) {
723
- return this._echoNetworkAdapter.shouldAdvertize(peerId, {
795
+ return this._echoNetworkAdapter.shouldAdvertise(peerId, {
724
796
  documentId
725
797
  });
726
798
  }
@@ -736,7 +808,8 @@ var AutomergeHost = class {
736
808
  return;
737
809
  }
738
810
  const spaceKey = getSpaceKeyFromDoc(doc) ?? void 0;
739
- const lastAvailableHash = (0, import_automerge.getHeads)(doc);
811
+ const heads = (0, import_automerge.getHeads)(doc);
812
+ this._headsStore.setHeads(handle.documentId, heads, batch);
740
813
  const objectIds = Object.keys(doc.objects ?? {});
741
814
  const encodedIds = objectIds.map((objectId) => import_protocols.objectPointerCodec.encode({
742
815
  documentId: handle.documentId,
@@ -745,7 +818,7 @@ var AutomergeHost = class {
745
818
  }));
746
819
  const idToLastHash = new Map(encodedIds.map((id) => [
747
820
  id,
748
- lastAvailableHash
821
+ heads
749
822
  ]));
750
823
  this._indexMetadataStore.markDirty(idToLastHash, batch);
751
824
  }
@@ -794,18 +867,30 @@ var AutomergeHost = class {
794
867
  /**
795
868
  * Flush documents to disk.
796
869
  */
797
- async flush({ states }) {
870
+ async flush({ states } = {}) {
798
871
  if (states) {
799
872
  await Promise.all(states.map(async ({ heads, documentId }) => {
800
873
  if (!heads) {
801
874
  return;
802
875
  }
803
- const handle = this.repo.handles[documentId] ?? this._repo.find(documentId);
876
+ const handle = this._repo.handles[documentId] ?? this._repo.find(documentId);
804
877
  await waitForHeads(handle, heads);
805
878
  }) ?? []);
806
879
  }
807
880
  await this._repo.flush(states?.map(({ documentId }) => documentId));
808
881
  }
882
+ async getHeads(documentId) {
883
+ const handle = this._repo.handles[documentId];
884
+ if (handle) {
885
+ const doc = handle.docSync();
886
+ if (!doc) {
887
+ return void 0;
888
+ }
889
+ return (0, import_automerge.getHeads)(doc);
890
+ } else {
891
+ return this._headsStore.getHeads(documentId);
892
+ }
893
+ }
809
894
  /**
810
895
  * Host <-> Client sync.
811
896
  */
@@ -907,7 +992,7 @@ var AutomergeDocumentLoaderImpl = class {
907
992
  }
908
993
  const existingDocHandle = await this._initDocHandle(ctx, spaceState.rootUrl);
909
994
  const doc = existingDocHandle.docSync();
910
- (0, import_invariant3.invariant)(doc, void 0, {
995
+ (0, import_invariant4.invariant)(doc, void 0, {
911
996
  F: __dxlog_file4,
912
997
  L: 84,
913
998
  S: this,
@@ -916,7 +1001,7 @@ var AutomergeDocumentLoaderImpl = class {
916
1001
  ""
917
1002
  ]
918
1003
  });
919
- (0, import_invariant3.invariant)(doc.version === import_echo_protocol.SpaceDocVersion.CURRENT, void 0, {
1004
+ (0, import_invariant4.invariant)(doc.version === import_echo_protocol.SpaceDocVersion.CURRENT, void 0, {
920
1005
  F: __dxlog_file4,
921
1006
  L: 85,
922
1007
  S: this,
@@ -937,7 +1022,7 @@ var AutomergeDocumentLoaderImpl = class {
937
1022
  let hasUrlsToLoad = false;
938
1023
  const urlsToLoad = {};
939
1024
  for (const objectId of objectIds) {
940
- (0, import_invariant3.invariant)(this._spaceRootDocHandle, void 0, {
1025
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
941
1026
  F: __dxlog_file4,
942
1027
  L: 97,
943
1028
  S: this,
@@ -950,7 +1035,7 @@ var AutomergeDocumentLoaderImpl = class {
950
1035
  continue;
951
1036
  }
952
1037
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
953
- (0, import_invariant3.invariant)(spaceRootDoc, void 0, {
1038
+ (0, import_invariant4.invariant)(spaceRootDoc, void 0, {
954
1039
  F: __dxlog_file4,
955
1040
  L: 102,
956
1041
  S: this,
@@ -962,7 +1047,7 @@ var AutomergeDocumentLoaderImpl = class {
962
1047
  const documentUrl = (spaceRootDoc.links ?? {})[objectId];
963
1048
  if (documentUrl == null) {
964
1049
  this._objectsPendingDocumentLoad.add(objectId);
965
- import_log2.log.info("loading delayed until object links are initialized", {
1050
+ import_log3.log.info("loading delayed until object links are initialized", {
966
1051
  objectId
967
1052
  }, {
968
1053
  F: __dxlog_file4,
@@ -980,7 +1065,7 @@ var AutomergeDocumentLoaderImpl = class {
980
1065
  }
981
1066
  }
982
1067
  getObjectDocumentId(objectId) {
983
- (0, import_invariant3.invariant)(this._spaceRootDocHandle, void 0, {
1068
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
984
1069
  F: __dxlog_file4,
985
1070
  L: 118,
986
1071
  S: this,
@@ -990,7 +1075,7 @@ var AutomergeDocumentLoaderImpl = class {
990
1075
  ]
991
1076
  });
992
1077
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
993
- (0, import_invariant3.invariant)(spaceRootDoc, void 0, {
1078
+ (0, import_invariant4.invariant)(spaceRootDoc, void 0, {
994
1079
  F: __dxlog_file4,
995
1080
  L: 120,
996
1081
  S: this,
@@ -1014,7 +1099,7 @@ var AutomergeDocumentLoaderImpl = class {
1014
1099
  linksAwaitingLoad.forEach(([objectId]) => this._objectsPendingDocumentLoad.delete(objectId));
1015
1100
  }
1016
1101
  getSpaceRootDocHandle() {
1017
- (0, import_invariant3.invariant)(this._spaceRootDocHandle, void 0, {
1102
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
1018
1103
  F: __dxlog_file4,
1019
1104
  L: 140,
1020
1105
  S: this,
@@ -1026,7 +1111,7 @@ var AutomergeDocumentLoaderImpl = class {
1026
1111
  return this._spaceRootDocHandle;
1027
1112
  }
1028
1113
  createDocumentForObject(objectId) {
1029
- (0, import_invariant3.invariant)(this._spaceRootDocHandle, void 0, {
1114
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
1030
1115
  F: __dxlog_file4,
1031
1116
  L: 145,
1032
1117
  S: this,
@@ -1068,7 +1153,7 @@ var AutomergeDocumentLoaderImpl = class {
1068
1153
  };
1069
1154
  const objectDocumentHandle = this._objectDocumentHandles.get(objectId);
1070
1155
  if (objectDocumentHandle != null && objectDocumentHandle.url !== automergeUrl) {
1071
- import_log2.log.warn("object already inlined in a different document, ignoring the link", {
1156
+ import_log3.log.warn("object already inlined in a different document, ignoring the link", {
1072
1157
  ...logMeta,
1073
1158
  actualDocumentUrl: objectDocumentHandle.url
1074
1159
  }, {
@@ -1080,7 +1165,7 @@ var AutomergeDocumentLoaderImpl = class {
1080
1165
  continue;
1081
1166
  }
1082
1167
  if (objectDocumentHandle?.url === automergeUrl) {
1083
- import_log2.log.warn("object document was already loaded", logMeta, {
1168
+ import_log3.log.warn("object document was already loaded", logMeta, {
1084
1169
  F: __dxlog_file4,
1085
1170
  L: 184,
1086
1171
  S: this,
@@ -1089,7 +1174,7 @@ var AutomergeDocumentLoaderImpl = class {
1089
1174
  continue;
1090
1175
  }
1091
1176
  const handle = this._repo.find(automergeUrl);
1092
- import_log2.log.debug("document loading triggered", logMeta, {
1177
+ import_log3.log.debug("document loading triggered", logMeta, {
1093
1178
  F: __dxlog_file4,
1094
1179
  L: 188,
1095
1180
  S: this,
@@ -1109,7 +1194,7 @@ var AutomergeDocumentLoaderImpl = class {
1109
1194
  break;
1110
1195
  } catch (err) {
1111
1196
  if (`${err}`.includes("Timeout")) {
1112
- import_log2.log.info("wraparound", {
1197
+ import_log3.log.info("wraparound", {
1113
1198
  id: docHandle.documentId,
1114
1199
  state: docHandle.state
1115
1200
  }, {
@@ -1138,15 +1223,13 @@ var AutomergeDocumentLoaderImpl = class {
1138
1223
  }
1139
1224
  async _createObjectOnDocumentLoad(handle, objectId) {
1140
1225
  try {
1141
- await handle.doc([
1142
- "ready"
1143
- ]);
1226
+ await handle.whenReady();
1144
1227
  const logMeta = {
1145
1228
  objectId,
1146
1229
  docUrl: handle.url
1147
1230
  };
1148
1231
  if (this.onObjectDocumentLoaded.listenerCount() === 0) {
1149
- import_log2.log.info("document loaded after all listeners were removed", logMeta, {
1232
+ import_log3.log.info("document loaded after all listeners were removed", logMeta, {
1150
1233
  F: __dxlog_file4,
1151
1234
  L: 231,
1152
1235
  S: this,
@@ -1156,7 +1239,7 @@ var AutomergeDocumentLoaderImpl = class {
1156
1239
  }
1157
1240
  const objectDocHandle = this._objectDocumentHandles.get(objectId);
1158
1241
  if (objectDocHandle?.url !== handle.url) {
1159
- import_log2.log.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1242
+ import_log3.log.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1160
1243
  F: __dxlog_file4,
1161
1244
  L: 236,
1162
1245
  S: this,
@@ -1170,7 +1253,7 @@ var AutomergeDocumentLoaderImpl = class {
1170
1253
  });
1171
1254
  } catch (err) {
1172
1255
  const shouldRetryLoading = this.onObjectDocumentLoaded.listenerCount() > 0;
1173
- import_log2.log.warn("failed to load a document", {
1256
+ import_log3.log.warn("failed to load a document", {
1174
1257
  objectId,
1175
1258
  automergeUrl: handle.url,
1176
1259
  retryLoading: shouldRetryLoading,
@@ -1195,7 +1278,121 @@ _ts_decorate3([
1195
1278
  AutomergeDocumentLoaderImpl = _ts_decorate3([
1196
1279
  import_tracing2.trace.resource()
1197
1280
  ], AutomergeDocumentLoaderImpl);
1198
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1281
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1282
+ var DEFAULT_FACTORY = (params) => new import_teleport_extension_automerge_replicator.AutomergeReplicator(...params);
1283
+ var MeshReplicatorConnection = class extends import_context5.Resource {
1284
+ constructor(_params) {
1285
+ super();
1286
+ this._params = _params;
1287
+ this.remoteDeviceKey = null;
1288
+ this._remotePeerId = null;
1289
+ this._isEnabled = false;
1290
+ let readableStreamController;
1291
+ this.readable = new ReadableStream({
1292
+ start: (controller) => {
1293
+ readableStreamController = controller;
1294
+ this._ctx.onDispose(() => controller.close());
1295
+ }
1296
+ });
1297
+ this.writable = new WritableStream({
1298
+ write: async (message, controller) => {
1299
+ (0, import_invariant6.invariant)(this._isEnabled, "Writing to a disabled connection", {
1300
+ F: __dxlog_file5,
1301
+ L: 47,
1302
+ S: this,
1303
+ A: [
1304
+ "this._isEnabled",
1305
+ "'Writing to a disabled connection'"
1306
+ ]
1307
+ });
1308
+ try {
1309
+ await this.replicatorExtension.sendSyncMessage({
1310
+ payload: import_automerge_repo5.cbor.encode(message)
1311
+ });
1312
+ } catch (err) {
1313
+ controller.error(err);
1314
+ this._disconnectIfEnabled();
1315
+ }
1316
+ }
1317
+ });
1318
+ const createAutomergeReplicator = this._params.replicatorFactory ?? DEFAULT_FACTORY;
1319
+ this.replicatorExtension = createAutomergeReplicator([
1320
+ {
1321
+ peerId: this._params.ownPeerId
1322
+ },
1323
+ {
1324
+ onStartReplication: async (info, remotePeerId) => {
1325
+ this.remoteDeviceKey = remotePeerId;
1326
+ this._remotePeerId = info.id;
1327
+ (0, import_log5.log)("onStartReplication", {
1328
+ id: info.id,
1329
+ thisPeerId: this.peerId,
1330
+ remotePeerId: remotePeerId.toHex()
1331
+ }, {
1332
+ F: __dxlog_file5,
1333
+ L: 81,
1334
+ S: this,
1335
+ C: (f, a) => f(...a)
1336
+ });
1337
+ this._params.onRemoteConnected();
1338
+ },
1339
+ onSyncMessage: async ({ payload }) => {
1340
+ if (!this._isEnabled) {
1341
+ return;
1342
+ }
1343
+ const message = import_automerge_repo5.cbor.decode(payload);
1344
+ readableStreamController.enqueue(message);
1345
+ },
1346
+ onClose: async () => {
1347
+ this._disconnectIfEnabled();
1348
+ }
1349
+ }
1350
+ ]);
1351
+ }
1352
+ _disconnectIfEnabled() {
1353
+ if (this._isEnabled) {
1354
+ this._params.onRemoteDisconnected();
1355
+ }
1356
+ }
1357
+ get peerId() {
1358
+ (0, import_invariant6.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1359
+ F: __dxlog_file5,
1360
+ L: 107,
1361
+ S: this,
1362
+ A: [
1363
+ "this._remotePeerId != null",
1364
+ "'Remote peer has not connected yet.'"
1365
+ ]
1366
+ });
1367
+ return this._remotePeerId;
1368
+ }
1369
+ async shouldAdvertise(params) {
1370
+ return this._params.shouldAdvertise(params);
1371
+ }
1372
+ /**
1373
+ * Start exchanging messages with the remote peer.
1374
+ * Call after the remote peer has connected.
1375
+ */
1376
+ enable() {
1377
+ (0, import_invariant6.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1378
+ F: __dxlog_file5,
1379
+ L: 120,
1380
+ S: this,
1381
+ A: [
1382
+ "this._remotePeerId != null",
1383
+ "'Remote peer has not connected yet.'"
1384
+ ]
1385
+ });
1386
+ this._isEnabled = true;
1387
+ }
1388
+ /**
1389
+ * Stop exchanging messages with the remote peer.
1390
+ */
1391
+ disable() {
1392
+ this._isEnabled = false;
1393
+ }
1394
+ };
1395
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1199
1396
  var MeshEchoReplicator = class {
1200
1397
  constructor() {
1201
1398
  this._connections = /* @__PURE__ */ new Set();
@@ -1214,10 +1411,10 @@ var MeshEchoReplicator = class {
1214
1411
  this._connectionsPerPeer.clear();
1215
1412
  this._context = null;
1216
1413
  }
1217
- createExtension() {
1218
- (0, import_invariant4.invariant)(this._context, void 0, {
1219
- F: __dxlog_file5,
1220
- L: 54,
1414
+ createExtension(extensionFactory) {
1415
+ (0, import_invariant5.invariant)(this._context, void 0, {
1416
+ F: __dxlog_file6,
1417
+ L: 51,
1221
1418
  S: this,
1222
1419
  A: [
1223
1420
  "this._context",
@@ -1226,18 +1423,19 @@ var MeshEchoReplicator = class {
1226
1423
  });
1227
1424
  const connection = new MeshReplicatorConnection({
1228
1425
  ownPeerId: this._context.peerId,
1426
+ replicatorFactory: extensionFactory,
1229
1427
  onRemoteConnected: async () => {
1230
- (0, import_log3.log)("onRemoteConnected", {
1428
+ (0, import_log4.log)("onRemoteConnected", {
1231
1429
  peerId: connection.peerId
1232
1430
  }, {
1233
- F: __dxlog_file5,
1234
- L: 59,
1431
+ F: __dxlog_file6,
1432
+ L: 57,
1235
1433
  S: this,
1236
1434
  C: (f, a) => f(...a)
1237
1435
  });
1238
- (0, import_invariant4.invariant)(this._context, void 0, {
1239
- F: __dxlog_file5,
1240
- L: 60,
1436
+ (0, import_invariant5.invariant)(this._context, void 0, {
1437
+ F: __dxlog_file6,
1438
+ L: 58,
1241
1439
  S: this,
1242
1440
  A: [
1243
1441
  "this._context",
@@ -1249,36 +1447,36 @@ var MeshEchoReplicator = class {
1249
1447
  } else {
1250
1448
  this._connectionsPerPeer.set(connection.peerId, connection);
1251
1449
  this._context.onConnectionOpen(connection);
1252
- await connection.enable();
1450
+ connection.enable();
1253
1451
  }
1254
1452
  },
1255
1453
  onRemoteDisconnected: async () => {
1256
- (0, import_log3.log)("onRemoteDisconnected", {
1454
+ (0, import_log4.log)("onRemoteDisconnected", {
1257
1455
  peerId: connection.peerId
1258
1456
  }, {
1259
- F: __dxlog_file5,
1260
- L: 71,
1457
+ F: __dxlog_file6,
1458
+ L: 69,
1261
1459
  S: this,
1262
1460
  C: (f, a) => f(...a)
1263
1461
  });
1264
1462
  this._context?.onConnectionClosed(connection);
1265
1463
  this._connectionsPerPeer.delete(connection.peerId);
1266
- await connection.disable();
1464
+ connection.disable();
1267
1465
  this._connections.delete(connection);
1268
1466
  },
1269
- shouldAdvertize: async (params) => {
1270
- (0, import_log3.log)("shouldAdvertize", {
1467
+ shouldAdvertise: async (params) => {
1468
+ (0, import_log4.log)("shouldAdvertise", {
1271
1469
  peerId: connection.peerId,
1272
1470
  documentId: params.documentId
1273
1471
  }, {
1274
- F: __dxlog_file5,
1275
- L: 78,
1472
+ F: __dxlog_file6,
1473
+ L: 76,
1276
1474
  S: this,
1277
1475
  C: (f, a) => f(...a)
1278
1476
  });
1279
- (0, import_invariant4.invariant)(this._context, void 0, {
1280
- F: __dxlog_file5,
1281
- L: 79,
1477
+ (0, import_invariant5.invariant)(this._context, void 0, {
1478
+ F: __dxlog_file6,
1479
+ L: 77,
1282
1480
  S: this,
1283
1481
  A: [
1284
1482
  "this._context",
@@ -1288,12 +1486,12 @@ var MeshEchoReplicator = class {
1288
1486
  try {
1289
1487
  const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
1290
1488
  if (!spaceKey) {
1291
- (0, import_log3.log)("space key not found for share policy check", {
1489
+ (0, import_log4.log)("space key not found for share policy check", {
1292
1490
  peerId: connection.peerId,
1293
1491
  documentId: params.documentId
1294
1492
  }, {
1295
- F: __dxlog_file5,
1296
- L: 83,
1493
+ F: __dxlog_file6,
1494
+ L: 81,
1297
1495
  S: this,
1298
1496
  C: (f, a) => f(...a)
1299
1497
  });
@@ -1301,19 +1499,19 @@ var MeshEchoReplicator = class {
1301
1499
  }
1302
1500
  const authorizedDevices = this._authorizedDevices.get(spaceKey);
1303
1501
  if (!connection.remoteDeviceKey) {
1304
- (0, import_log3.log)("device key not found for share policy check", {
1502
+ (0, import_log4.log)("device key not found for share policy check", {
1305
1503
  peerId: connection.peerId,
1306
1504
  documentId: params.documentId
1307
1505
  }, {
1308
- F: __dxlog_file5,
1309
- L: 93,
1506
+ F: __dxlog_file6,
1507
+ L: 91,
1310
1508
  S: this,
1311
1509
  C: (f, a) => f(...a)
1312
1510
  });
1313
1511
  return false;
1314
1512
  }
1315
1513
  const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1316
- (0, import_log3.log)("share policy check", {
1514
+ (0, import_log4.log)("share policy check", {
1317
1515
  localPeer: this._context.peerId,
1318
1516
  remotePeer: connection.peerId,
1319
1517
  documentId: params.documentId,
@@ -1321,16 +1519,16 @@ var MeshEchoReplicator = class {
1321
1519
  spaceKey,
1322
1520
  isAuthorized
1323
1521
  }, {
1324
- F: __dxlog_file5,
1325
- L: 101,
1522
+ F: __dxlog_file6,
1523
+ L: 99,
1326
1524
  S: this,
1327
1525
  C: (f, a) => f(...a)
1328
1526
  });
1329
1527
  return isAuthorized;
1330
1528
  } catch (err) {
1331
- import_log3.log.catch(err, void 0, {
1332
- F: __dxlog_file5,
1333
- L: 111,
1529
+ import_log4.log.catch(err, void 0, {
1530
+ F: __dxlog_file6,
1531
+ L: 109,
1334
1532
  S: this,
1335
1533
  C: (f, a) => f(...a)
1336
1534
  });
@@ -1342,115 +1540,23 @@ var MeshEchoReplicator = class {
1342
1540
  return connection.replicatorExtension;
1343
1541
  }
1344
1542
  authorizeDevice(spaceKey, deviceKey) {
1345
- (0, import_log3.log)("authorizeDevice", {
1543
+ (0, import_log4.log)("authorizeDevice", {
1346
1544
  spaceKey,
1347
1545
  deviceKey
1348
1546
  }, {
1349
- F: __dxlog_file5,
1350
- L: 122,
1547
+ F: __dxlog_file6,
1548
+ L: 120,
1351
1549
  S: this,
1352
1550
  C: (f, a) => f(...a)
1353
1551
  });
1354
1552
  (0, import_util2.defaultMap)(this._authorizedDevices, spaceKey, () => new import_util2.ComplexSet(import_keys2.PublicKey.hash)).add(deviceKey);
1355
1553
  for (const connection of this._connections) {
1356
1554
  if (connection.remoteDeviceKey && connection.remoteDeviceKey.equals(deviceKey)) {
1357
- this._context?.onConnectionAuthScopeChanged(connection);
1358
- }
1359
- }
1360
- }
1361
- };
1362
- var MeshReplicatorConnection = class extends import_context5.Resource {
1363
- constructor(_params) {
1364
- super();
1365
- this._params = _params;
1366
- this.remoteDeviceKey = null;
1367
- this._remotePeerId = null;
1368
- this._isEnabled = false;
1369
- let readableStreamController;
1370
- this.readable = new ReadableStream({
1371
- start: (controller) => {
1372
- readableStreamController = controller;
1373
- this._ctx.onDispose(() => controller.close());
1374
- }
1375
- });
1376
- this.writable = new WritableStream({
1377
- write: async (message, controller) => {
1378
- this.replicatorExtension.sendSyncMessage({
1379
- payload: import_automerge_repo5.cbor.encode(message)
1380
- }).catch((err) => {
1381
- controller.error(err);
1382
- });
1383
- }
1384
- });
1385
- this.replicatorExtension = new import_teleport_extension_automerge_replicator.AutomergeReplicator({
1386
- peerId: this._params.ownPeerId
1387
- }, {
1388
- onStartReplication: async (info, remotePeerId) => {
1389
- this.remoteDeviceKey = remotePeerId;
1390
- this._remotePeerId = info.id;
1391
- (0, import_log3.log)("onStartReplication", {
1392
- id: info.id,
1393
- thisPeerId: this.peerId,
1394
- remotePeerId: remotePeerId.toHex()
1395
- }, {
1396
- F: __dxlog_file5,
1397
- L: 192,
1398
- S: this,
1399
- C: (f, a) => f(...a)
1400
- });
1401
- await this._params.onRemoteConnected();
1402
- },
1403
- onSyncMessage: async ({ payload }) => {
1404
- if (!this._isEnabled) {
1405
- return;
1406
- }
1407
- const message = import_automerge_repo5.cbor.decode(payload);
1408
- readableStreamController.enqueue(message);
1409
- },
1410
- onClose: async () => {
1411
- if (!this._isEnabled) {
1412
- return;
1555
+ if (this._connectionsPerPeer.has(connection.peerId)) {
1556
+ this._context?.onConnectionAuthScopeChanged(connection);
1413
1557
  }
1414
- await this._params.onRemoteDisconnected();
1415
1558
  }
1416
- });
1417
- }
1418
- get peerId() {
1419
- (0, import_invariant4.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1420
- F: __dxlog_file5,
1421
- L: 215,
1422
- S: this,
1423
- A: [
1424
- "this._remotePeerId != null",
1425
- "'Remote peer has not connected yet.'"
1426
- ]
1427
- });
1428
- return this._remotePeerId;
1429
- }
1430
- async shouldAdvertize(params) {
1431
- return this._params.shouldAdvertize(params);
1432
- }
1433
- /**
1434
- * Start exchanging messages with the remote peer.
1435
- * Call after the remote peer has connected.
1436
- */
1437
- async enable() {
1438
- (0, import_invariant4.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1439
- F: __dxlog_file5,
1440
- L: 228,
1441
- S: this,
1442
- A: [
1443
- "this._remotePeerId != null",
1444
- "'Remote peer has not connected yet.'"
1445
- ]
1446
- });
1447
- this._isEnabled = true;
1448
- }
1449
- /**
1450
- * Stop exchanging messages with the remote peer.
1451
- */
1452
- async disable() {
1453
- this._isEnabled = false;
1559
+ }
1454
1560
  }
1455
1561
  };
1456
1562
  // Annotate the CommonJS export names for ESM import in node: