@dxos/echo-pipeline 0.6.11 → 0.6.12-main.15a606f

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 (106) hide show
  1. package/dist/lib/browser/chunk-YTSGJIY7.mjs +2036 -0
  2. package/dist/lib/browser/chunk-YTSGJIY7.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +3401 -17
  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 +3 -4
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/{chunk-7HHYCGUR.cjs → chunk-6SSC2M4R.cjs} +65 -37
  9. package/dist/lib/node/chunk-6SSC2M4R.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +3378 -35
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +11 -12
  14. package/dist/lib/node/testing/index.cjs.map +3 -3
  15. package/dist/lib/{browser/chunk-UKXIJW43.mjs → node-esm/chunk-ASOERPR6.mjs} +52 -26
  16. package/dist/lib/node-esm/chunk-ASOERPR6.mjs.map +7 -0
  17. package/dist/lib/{browser/chunk-MPWFDDQK.mjs → node-esm/index.mjs} +1623 -318
  18. package/dist/lib/node-esm/index.mjs.map +7 -0
  19. package/dist/lib/node-esm/meta.json +1 -0
  20. package/dist/lib/node-esm/testing/index.mjs +551 -0
  21. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  22. package/dist/types/src/automerge/automerge-host.d.ts +24 -1
  23. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
  24. package/dist/types/src/automerge/collection-synchronizer.d.ts +2 -0
  25. package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
  26. package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
  27. package/dist/types/src/automerge/space-collection.d.ts +3 -2
  28. package/dist/types/src/automerge/space-collection.d.ts.map +1 -1
  29. package/dist/types/src/db-host/automerge-metrics.d.ts +11 -0
  30. package/dist/types/src/db-host/automerge-metrics.d.ts.map +1 -0
  31. package/dist/types/src/db-host/data-service.d.ts +3 -2
  32. package/dist/types/src/db-host/data-service.d.ts.map +1 -1
  33. package/dist/types/src/db-host/database-root.d.ts +20 -0
  34. package/dist/types/src/db-host/database-root.d.ts.map +1 -0
  35. package/dist/types/src/db-host/documents-iterator.d.ts +7 -0
  36. package/dist/types/src/db-host/documents-iterator.d.ts.map +1 -0
  37. package/dist/types/src/db-host/echo-host.d.ts +73 -0
  38. package/dist/types/src/db-host/echo-host.d.ts.map +1 -0
  39. package/dist/types/src/db-host/index.d.ts +5 -0
  40. package/dist/types/src/db-host/index.d.ts.map +1 -1
  41. package/dist/types/src/db-host/migration.d.ts +8 -0
  42. package/dist/types/src/db-host/migration.d.ts.map +1 -0
  43. package/dist/types/src/db-host/query-service.d.ts +25 -0
  44. package/dist/types/src/db-host/query-service.d.ts.map +1 -0
  45. package/dist/types/src/db-host/query-state.d.ts +41 -0
  46. package/dist/types/src/db-host/query-state.d.ts.map +1 -0
  47. package/dist/types/src/db-host/space-state-manager.d.ts +23 -0
  48. package/dist/types/src/db-host/space-state-manager.d.ts.map +1 -0
  49. package/dist/types/src/edge/echo-edge-replicator.d.ts +23 -0
  50. package/dist/types/src/edge/echo-edge-replicator.d.ts.map +1 -0
  51. package/dist/types/src/edge/index.d.ts +2 -0
  52. package/dist/types/src/edge/index.d.ts.map +1 -0
  53. package/dist/types/src/index.d.ts +1 -0
  54. package/dist/types/src/index.d.ts.map +1 -1
  55. package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
  56. package/package.json +40 -50
  57. package/src/automerge/automerge-host.test.ts +8 -9
  58. package/src/automerge/automerge-host.ts +42 -6
  59. package/src/automerge/automerge-repo.test.ts +18 -16
  60. package/src/automerge/collection-synchronizer.test.ts +10 -5
  61. package/src/automerge/collection-synchronizer.ts +17 -6
  62. package/src/automerge/echo-data-monitor.test.ts +1 -3
  63. package/src/automerge/echo-network-adapter.test.ts +4 -3
  64. package/src/automerge/mesh-echo-replicator.ts +2 -1
  65. package/src/automerge/space-collection.ts +3 -2
  66. package/src/automerge/storage-adapter.test.ts +2 -3
  67. package/src/db-host/automerge-metrics.ts +38 -0
  68. package/src/db-host/data-service.ts +29 -14
  69. package/src/db-host/database-root.ts +86 -0
  70. package/src/db-host/documents-iterator.ts +73 -0
  71. package/src/db-host/documents-synchronizer.test.ts +2 -2
  72. package/src/db-host/echo-host.ts +257 -0
  73. package/src/db-host/index.ts +6 -1
  74. package/src/db-host/migration.ts +57 -0
  75. package/src/db-host/query-service.ts +208 -0
  76. package/src/db-host/query-state.ts +194 -0
  77. package/src/db-host/space-state-manager.ts +90 -0
  78. package/src/edge/echo-edge-replicator.ts +286 -0
  79. package/src/edge/index.ts +5 -0
  80. package/src/index.ts +1 -0
  81. package/src/pipeline/pipeline-stress.test.ts +44 -47
  82. package/src/pipeline/pipeline.test.ts +3 -4
  83. package/src/space/control-pipeline.test.ts +2 -3
  84. package/src/space/control-pipeline.ts +10 -1
  85. package/src/space/replication.browser.test.ts +2 -8
  86. package/src/space/space-manager.browser.test.ts +6 -5
  87. package/src/space/space-protocol.browser.test.ts +29 -34
  88. package/src/space/space-protocol.test.ts +29 -27
  89. package/src/space/space.test.ts +28 -11
  90. package/src/testing/test-agent-builder.ts +2 -2
  91. package/dist/lib/browser/chunk-MPWFDDQK.mjs.map +0 -7
  92. package/dist/lib/browser/chunk-UKXIJW43.mjs.map +0 -7
  93. package/dist/lib/browser/chunk-XPCF2V5U.mjs +0 -31
  94. package/dist/lib/browser/chunk-XPCF2V5U.mjs.map +0 -7
  95. package/dist/lib/browser/light.mjs +0 -32
  96. package/dist/lib/browser/light.mjs.map +0 -7
  97. package/dist/lib/node/chunk-5DH4KR2S.cjs +0 -2148
  98. package/dist/lib/node/chunk-5DH4KR2S.cjs.map +0 -7
  99. package/dist/lib/node/chunk-7HHYCGUR.cjs.map +0 -7
  100. package/dist/lib/node/chunk-DZVH7HDD.cjs +0 -43
  101. package/dist/lib/node/chunk-DZVH7HDD.cjs.map +0 -7
  102. package/dist/lib/node/light.cjs +0 -52
  103. package/dist/lib/node/light.cjs.map +0 -7
  104. package/dist/types/src/light.d.ts +0 -4
  105. package/dist/types/src/light.d.ts.map +0 -1
  106. package/src/light.ts +0 -7
@@ -1,8 +1,34 @@
1
- import "@dxos/node-std/globals";
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
  import {
3
- Buffer,
4
- createIdFromSpaceKey
5
- } from "./chunk-XPCF2V5U.mjs";
3
+ AuthExtension,
4
+ AuthStatus,
5
+ CredentialRetrieverExtension,
6
+ CredentialServerExtension,
7
+ MOCK_AUTH_PROVIDER,
8
+ MOCK_AUTH_VERIFIER,
9
+ MetadataStore,
10
+ Pipeline,
11
+ Space,
12
+ SpaceManager,
13
+ SpaceProtocol,
14
+ SpaceProtocolSession,
15
+ TimeframeClock,
16
+ codec,
17
+ createIdFromSpaceKey,
18
+ createMappedFeedWriter,
19
+ hasInvitationExpired,
20
+ mapFeedIndexesToTimeframe,
21
+ mapTimeframeToFeedIndexes,
22
+ startAfter,
23
+ valueEncoding
24
+ } from "./chunk-ASOERPR6.mjs";
25
+
26
+ // packages/core/echo/echo-pipeline/src/db-host/data-service.ts
27
+ import { UpdateScheduler as UpdateScheduler2 } from "@dxos/async";
28
+ import { Stream } from "@dxos/codec-protobuf/stream";
29
+ import { invariant as invariant7 } from "@dxos/invariant";
30
+ import { SpaceId as SpaceId2 } from "@dxos/keys";
31
+ import { log as log7 } from "@dxos/log";
6
32
 
7
33
  // packages/core/echo/echo-pipeline/src/db-host/documents-synchronizer.ts
8
34
  import { UpdateScheduler } from "@dxos/async";
@@ -170,11 +196,24 @@ var DocumentsSynchronizer = class extends Resource {
170
196
  }
171
197
  };
172
198
 
199
+ // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
200
+ import { Event as Event2, asyncTimeout } from "@dxos/async";
201
+ import { getBackend, getHeads, isAutomerge, equals as headsEquals, save } from "@dxos/automerge/automerge";
202
+ import { Repo } from "@dxos/automerge/automerge-repo";
203
+ import { Context, Resource as Resource4, cancelWithContext } from "@dxos/context";
204
+ import { invariant as invariant3 } from "@dxos/invariant";
205
+ import { PublicKey } from "@dxos/keys";
206
+ import { log as log4 } from "@dxos/log";
207
+ import { objectPointerCodec } from "@dxos/protocols";
208
+ import { trace } from "@dxos/tracing";
209
+
173
210
  // packages/core/echo/echo-pipeline/src/automerge/collection-synchronizer.ts
174
211
  import { asyncReturn, Event, scheduleTask, scheduleTaskInterval } from "@dxos/async";
175
212
  import { next as am } from "@dxos/automerge/automerge";
176
213
  import { Resource as Resource2 } from "@dxos/context";
214
+ import { log as log2 } from "@dxos/log";
177
215
  import { defaultMap } from "@dxos/util";
216
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/collection-synchronizer.ts";
178
217
  var MIN_QUERY_INTERVAL = 5e3;
179
218
  var POLL_INTERVAL = 3e4;
180
219
  var CollectionSynchronizer = class extends Resource2 {
@@ -207,6 +246,15 @@ var CollectionSynchronizer = class extends Resource2 {
207
246
  return this._getPerCollectionState(collectionId).localState;
208
247
  }
209
248
  setLocalCollectionState(collectionId, state) {
249
+ log2("setLocalCollectionState", {
250
+ collectionId,
251
+ state
252
+ }, {
253
+ F: __dxlog_file2,
254
+ L: 68,
255
+ S: this,
256
+ C: (f, a) => f(...a)
257
+ });
210
258
  this._getPerCollectionState(collectionId).localState = state;
211
259
  queueMicrotask(async () => {
212
260
  if (!this._ctx.disposed) {
@@ -276,6 +324,16 @@ var CollectionSynchronizer = class extends Resource2 {
276
324
  * Callback when a peer sends the state of a collection.
277
325
  */
278
326
  onRemoteStateReceived(collectionId, peerId, state) {
327
+ log2("onRemoteStateReceived", {
328
+ collectionId,
329
+ peerId,
330
+ state
331
+ }, {
332
+ F: __dxlog_file2,
333
+ L: 148,
334
+ S: this,
335
+ C: (f, a) => f(...a)
336
+ });
279
337
  validateCollectionState(state);
280
338
  const perCollectionState = this._getPerCollectionState(collectionId);
281
339
  perCollectionState.remoteStates.set(peerId, state);
@@ -307,13 +365,21 @@ var diffCollectionState = (local, remote) => {
307
365
  ...Object.keys(local.documents),
308
366
  ...Object.keys(remote.documents)
309
367
  ]);
368
+ const missingOnRemote = [];
369
+ const missingOnLocal = [];
310
370
  const different = [];
311
371
  for (const documentId of allDocuments) {
312
- if (!local.documents[documentId] || !remote.documents[documentId] || !am.equals(local.documents[documentId], remote.documents[documentId])) {
372
+ if (!local.documents[documentId]) {
373
+ missingOnLocal.push(documentId);
374
+ } else if (!remote.documents[documentId]) {
375
+ missingOnRemote.push(documentId);
376
+ } else if (!am.equals(local.documents[documentId], remote.documents[documentId])) {
313
377
  different.push(documentId);
314
378
  }
315
379
  }
316
380
  return {
381
+ missingOnRemote,
382
+ missingOnLocal,
317
383
  different
318
384
  };
319
385
  };
@@ -331,129 +397,12 @@ var isValidDocumentId = (documentId) => {
331
397
  return typeof documentId === "string" && !documentId.includes(":");
332
398
  };
333
399
 
334
- // packages/core/echo/echo-pipeline/src/automerge/leveldb-storage-adapter.ts
335
- import { LifecycleState, Resource as Resource3 } from "@dxos/context";
336
- var LevelDBStorageAdapter = class extends Resource3 {
337
- constructor(_params) {
338
- super();
339
- this._params = _params;
340
- }
341
- async load(keyArray) {
342
- try {
343
- if (this._lifecycleState !== LifecycleState.OPEN) {
344
- return void 0;
345
- }
346
- const startMs = Date.now();
347
- const chunk = await this._params.db.get(keyArray, {
348
- ...encodingOptions
349
- });
350
- this._params.monitor?.recordBytesLoaded(chunk.byteLength);
351
- this._params.monitor?.recordLoadDuration(Date.now() - startMs);
352
- return chunk;
353
- } catch (err) {
354
- if (isLevelDbNotFoundError(err)) {
355
- return void 0;
356
- }
357
- throw err;
358
- }
359
- }
360
- async save(keyArray, binary) {
361
- if (this._lifecycleState !== LifecycleState.OPEN) {
362
- return void 0;
363
- }
364
- const startMs = Date.now();
365
- const batch = this._params.db.batch();
366
- await this._params.callbacks?.beforeSave?.({
367
- path: keyArray,
368
- batch
369
- });
370
- batch.put(keyArray, Buffer.from(binary), {
371
- ...encodingOptions
372
- });
373
- await batch.write();
374
- this._params.monitor?.recordBytesStored(binary.byteLength);
375
- await this._params.callbacks?.afterSave?.(keyArray);
376
- this._params.monitor?.recordStoreDuration(Date.now() - startMs);
377
- }
378
- async remove(keyArray) {
379
- if (this._lifecycleState !== LifecycleState.OPEN) {
380
- return void 0;
381
- }
382
- await this._params.db.del(keyArray, {
383
- ...encodingOptions
384
- });
385
- }
386
- async loadRange(keyPrefix) {
387
- if (this._lifecycleState !== LifecycleState.OPEN) {
388
- return [];
389
- }
390
- const startMs = Date.now();
391
- const result = [];
392
- for await (const [key, value] of this._params.db.iterator({
393
- gte: keyPrefix,
394
- lte: [
395
- ...keyPrefix,
396
- "\uFFFF"
397
- ],
398
- ...encodingOptions
399
- })) {
400
- result.push({
401
- key,
402
- data: value
403
- });
404
- this._params.monitor?.recordBytesLoaded(value.byteLength);
405
- }
406
- this._params.monitor?.recordLoadDuration(Date.now() - startMs);
407
- return result;
408
- }
409
- async removeRange(keyPrefix) {
410
- if (this._lifecycleState !== LifecycleState.OPEN) {
411
- return void 0;
412
- }
413
- const batch = this._params.db.batch();
414
- for await (const [key] of this._params.db.iterator({
415
- gte: keyPrefix,
416
- lte: [
417
- ...keyPrefix,
418
- "\uFFFF"
419
- ],
420
- ...encodingOptions
421
- })) {
422
- batch.del(key, {
423
- ...encodingOptions
424
- });
425
- }
426
- await batch.write();
427
- }
428
- };
429
- var keyEncoder = {
430
- encode: (key) => Buffer.from(key.map((k) => k.replaceAll("%", "%25").replaceAll("-", "%2D")).join("-")),
431
- decode: (key) => Buffer.from(key).toString().split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%")),
432
- format: "buffer"
433
- };
434
- var encodingOptions = {
435
- keyEncoding: keyEncoder,
436
- valueEncoding: "buffer"
437
- };
438
- var isLevelDbNotFoundError = (err) => err.code === "LEVEL_NOT_FOUND";
439
-
440
- // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
441
- import { Event as Event2, asyncTimeout } from "@dxos/async";
442
- import { getBackend, getHeads, isAutomerge, equals as headsEquals, save } from "@dxos/automerge/automerge";
443
- import { Repo } from "@dxos/automerge/automerge-repo";
444
- import { Context, Resource as Resource4, cancelWithContext } from "@dxos/context";
445
- import { invariant as invariant3 } from "@dxos/invariant";
446
- import { PublicKey } from "@dxos/keys";
447
- import { log as log3 } from "@dxos/log";
448
- import { objectPointerCodec } from "@dxos/protocols";
449
- import { trace } from "@dxos/tracing";
450
-
451
400
  // packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts
452
401
  import { synchronized, Trigger } from "@dxos/async";
453
402
  import { NetworkAdapter } from "@dxos/automerge/automerge-repo";
454
- import { LifecycleState as LifecycleState2 } from "@dxos/context";
403
+ import { LifecycleState } from "@dxos/context";
455
404
  import { invariant as invariant2 } from "@dxos/invariant";
456
- import { log as log2 } from "@dxos/log";
405
+ import { log as log3 } from "@dxos/log";
457
406
  import { nonNullable } from "@dxos/util";
458
407
 
459
408
  // packages/core/echo/echo-pipeline/src/automerge/network-protocol.ts
@@ -468,14 +417,14 @@ function _ts_decorate(decorators, target, key, desc) {
468
417
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
469
418
  return c > 3 && r && Object.defineProperty(target, key, r), r;
470
419
  }
471
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
420
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
472
421
  var EchoNetworkAdapter = class extends NetworkAdapter {
473
422
  constructor(_params) {
474
423
  super();
475
424
  this._params = _params;
476
425
  this._replicators = /* @__PURE__ */ new Set();
477
426
  this._connections = /* @__PURE__ */ new Map();
478
- this._lifecycleState = LifecycleState2.CLOSED;
427
+ this._lifecycleState = LifecycleState.CLOSED;
479
428
  this._connected = new Trigger();
480
429
  }
481
430
  connect(peerId, peerMetadata) {
@@ -489,12 +438,12 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
489
438
  disconnect() {
490
439
  }
491
440
  async open() {
492
- if (this._lifecycleState === LifecycleState2.OPEN) {
441
+ if (this._lifecycleState === LifecycleState.OPEN) {
493
442
  return;
494
443
  }
495
- this._lifecycleState = LifecycleState2.OPEN;
496
- log2("emit ready", void 0, {
497
- F: __dxlog_file2,
444
+ this._lifecycleState = LifecycleState.OPEN;
445
+ log3("emit ready", void 0, {
446
+ F: __dxlog_file3,
498
447
  L: 81,
499
448
  S: this,
500
449
  C: (f, a) => f(...a)
@@ -504,14 +453,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
504
453
  });
505
454
  }
506
455
  async close() {
507
- if (this._lifecycleState === LifecycleState2.CLOSED) {
456
+ if (this._lifecycleState === LifecycleState.CLOSED) {
508
457
  return this;
509
458
  }
510
459
  for (const replicator of this._replicators) {
511
460
  await replicator.disconnect();
512
461
  }
513
462
  this._replicators.clear();
514
- this._lifecycleState = LifecycleState2.CLOSED;
463
+ this._lifecycleState = LifecycleState.CLOSED;
515
464
  }
516
465
  async whenConnected() {
517
466
  await this._connected.wait({
@@ -519,8 +468,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
519
468
  });
520
469
  }
521
470
  async addReplicator(replicator) {
522
- invariant2(this._lifecycleState === LifecycleState2.OPEN, void 0, {
523
- F: __dxlog_file2,
471
+ invariant2(this._lifecycleState === LifecycleState.OPEN, void 0, {
472
+ F: __dxlog_file3,
524
473
  L: 107,
525
474
  S: this,
526
475
  A: [
@@ -529,7 +478,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
529
478
  ]
530
479
  });
531
480
  invariant2(this.peerId, void 0, {
532
- F: __dxlog_file2,
481
+ F: __dxlog_file3,
533
482
  L: 108,
534
483
  S: this,
535
484
  A: [
@@ -538,7 +487,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
538
487
  ]
539
488
  });
540
489
  invariant2(!this._replicators.has(replicator), void 0, {
541
- F: __dxlog_file2,
490
+ F: __dxlog_file3,
542
491
  L: 109,
543
492
  S: this,
544
493
  A: [
@@ -561,8 +510,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
561
510
  });
562
511
  }
563
512
  async removeReplicator(replicator) {
564
- invariant2(this._lifecycleState === LifecycleState2.OPEN, void 0, {
565
- F: __dxlog_file2,
513
+ invariant2(this._lifecycleState === LifecycleState.OPEN, void 0, {
514
+ F: __dxlog_file3,
566
515
  L: 128,
567
516
  S: this,
568
517
  A: [
@@ -571,7 +520,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
571
520
  ]
572
521
  });
573
522
  invariant2(this._replicators.has(replicator), void 0, {
574
- F: __dxlog_file2,
523
+ F: __dxlog_file3,
575
524
  L: 129,
576
525
  S: this,
577
526
  A: [
@@ -626,8 +575,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
626
575
  this._params.monitor?.recordMessageSent(message, durationMs);
627
576
  }).catch((err) => {
628
577
  if (connectionEntry.isOpen) {
629
- log2.catch(err, void 0, {
630
- F: __dxlog_file2,
578
+ log3.catch(err, void 0, {
579
+ F: __dxlog_file3,
631
580
  L: 189,
632
581
  S: this,
633
582
  C: (f, a) => f(...a)
@@ -645,16 +594,16 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
645
594
  }).filter(nonNullable);
646
595
  }
647
596
  _onConnectionOpen(connection) {
648
- log2("Connection opened", {
597
+ log3("Connection opened", {
649
598
  peerId: connection.peerId
650
599
  }, {
651
- F: __dxlog_file2,
600
+ F: __dxlog_file3,
652
601
  L: 207,
653
602
  S: this,
654
603
  C: (f, a) => f(...a)
655
604
  });
656
605
  invariant2(!this._connections.has(connection.peerId), void 0, {
657
- F: __dxlog_file2,
606
+ F: __dxlog_file3,
658
607
  L: 208,
659
608
  S: this,
660
609
  A: [
@@ -682,8 +631,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
682
631
  }
683
632
  } catch (err) {
684
633
  if (connectionEntry.isOpen) {
685
- log2.catch(err, void 0, {
686
- F: __dxlog_file2,
634
+ log3.catch(err, void 0, {
635
+ F: __dxlog_file3,
687
636
  L: 227,
688
637
  S: this,
689
638
  C: (f, a) => f(...a)
@@ -691,10 +640,10 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
691
640
  }
692
641
  }
693
642
  });
694
- log2("emit peer-candidate", {
643
+ log3("emit peer-candidate", {
695
644
  peerId: connection.peerId
696
645
  }, {
697
- F: __dxlog_file2,
646
+ F: __dxlog_file3,
698
647
  L: 232,
699
648
  S: this,
700
649
  C: (f, a) => f(...a)
@@ -717,17 +666,17 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
717
666
  * TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
718
667
  */
719
668
  _onConnectionAuthScopeChanged(connection) {
720
- log2("Connection auth scope changed", {
669
+ log3("Connection auth scope changed", {
721
670
  peerId: connection.peerId
722
671
  }, {
723
- F: __dxlog_file2,
672
+ F: __dxlog_file3,
724
673
  L: 253,
725
674
  S: this,
726
675
  C: (f, a) => f(...a)
727
676
  });
728
677
  const entry = this._connections.get(connection.peerId);
729
678
  invariant2(entry, void 0, {
730
- F: __dxlog_file2,
679
+ F: __dxlog_file3,
731
680
  L: 255,
732
681
  S: this,
733
682
  A: [
@@ -741,17 +690,17 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
741
690
  this._emitPeerCandidate(connection);
742
691
  }
743
692
  _onConnectionClosed(connection) {
744
- log2("Connection closed", {
693
+ log3("Connection closed", {
745
694
  peerId: connection.peerId
746
695
  }, {
747
- F: __dxlog_file2,
696
+ F: __dxlog_file3,
748
697
  L: 261,
749
698
  S: this,
750
699
  C: (f, a) => f(...a)
751
700
  });
752
701
  const entry = this._connections.get(connection.peerId);
753
702
  invariant2(entry, void 0, {
754
- F: __dxlog_file2,
703
+ F: __dxlog_file3,
755
704
  L: 263,
756
705
  S: this,
757
706
  A: [
@@ -764,14 +713,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
764
713
  peerId: connection.peerId
765
714
  });
766
715
  this._params.monitor?.recordPeerDisconnected(connection.peerId);
767
- void entry.reader.cancel().catch((err) => log2.catch(err, void 0, {
768
- F: __dxlog_file2,
716
+ void entry.reader.cancel().catch((err) => log3.catch(err, void 0, {
717
+ F: __dxlog_file3,
769
718
  L: 269,
770
719
  S: this,
771
720
  C: (f, a) => f(...a)
772
721
  }));
773
- void entry.writer.abort().catch((err) => log2.catch(err, void 0, {
774
- F: __dxlog_file2,
722
+ void entry.writer.abort().catch((err) => log3.catch(err, void 0, {
723
+ F: __dxlog_file3,
775
724
  L: 270,
776
725
  S: this,
777
726
  C: (f, a) => f(...a)
@@ -825,50 +774,157 @@ var HeadsStore = class {
825
774
  }
826
775
  };
827
776
 
828
- // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
829
- function _ts_decorate2(decorators, target, key, desc) {
830
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
831
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
832
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
833
- return c > 3 && r && Object.defineProperty(target, key, r), r;
834
- }
835
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
836
- var AutomergeHost = class extends Resource4 {
837
- constructor({ db, indexMetadataStore, dataMonitor }) {
777
+ // packages/core/echo/echo-pipeline/src/automerge/leveldb-storage-adapter.ts
778
+ import { LifecycleState as LifecycleState2, Resource as Resource3 } from "@dxos/context";
779
+ var LevelDBStorageAdapter = class extends Resource3 {
780
+ constructor(_params) {
838
781
  super();
839
- this._collectionSynchronizer = new CollectionSynchronizer({
840
- queryCollectionState: this._queryCollectionState.bind(this),
841
- sendCollectionState: this._sendCollectionState.bind(this),
842
- shouldSyncCollection: this._shouldSyncCollection.bind(this)
843
- });
844
- this._db = db;
845
- this._storage = new LevelDBStorageAdapter({
846
- db: db.sublevel("automerge"),
847
- callbacks: {
848
- beforeSave: async (params) => this._beforeSave(params),
849
- afterSave: async (key) => this._afterSave(key)
850
- },
851
- monitor: dataMonitor
782
+ this._params = _params;
783
+ }
784
+ async load(keyArray) {
785
+ try {
786
+ if (this._lifecycleState !== LifecycleState2.OPEN) {
787
+ return void 0;
788
+ }
789
+ const startMs = Date.now();
790
+ const chunk = await this._params.db.get(keyArray, {
791
+ ...encodingOptions
792
+ });
793
+ this._params.monitor?.recordBytesLoaded(chunk.byteLength);
794
+ this._params.monitor?.recordLoadDuration(Date.now() - startMs);
795
+ return chunk;
796
+ } catch (err) {
797
+ if (isLevelDbNotFoundError(err)) {
798
+ return void 0;
799
+ }
800
+ throw err;
801
+ }
802
+ }
803
+ async save(keyArray, binary) {
804
+ if (this._lifecycleState !== LifecycleState2.OPEN) {
805
+ return void 0;
806
+ }
807
+ const startMs = Date.now();
808
+ const batch = this._params.db.batch();
809
+ await this._params.callbacks?.beforeSave?.({
810
+ path: keyArray,
811
+ batch
852
812
  });
853
- this._echoNetworkAdapter = new EchoNetworkAdapter({
854
- getContainingSpaceForDocument: this._getContainingSpaceForDocument.bind(this),
855
- isDocumentInRemoteCollection: this._isDocumentInRemoteCollection.bind(this),
856
- onCollectionStateQueried: this._onCollectionStateQueried.bind(this),
857
- onCollectionStateReceived: this._onCollectionStateReceived.bind(this),
858
- monitor: dataMonitor
813
+ batch.put(keyArray, Buffer.from(binary), {
814
+ ...encodingOptions
859
815
  });
860
- this._headsStore = new HeadsStore({
861
- db: db.sublevel("heads")
816
+ await batch.write();
817
+ this._params.monitor?.recordBytesStored(binary.byteLength);
818
+ await this._params.callbacks?.afterSave?.(keyArray);
819
+ this._params.monitor?.recordStoreDuration(Date.now() - startMs);
820
+ }
821
+ async remove(keyArray) {
822
+ if (this._lifecycleState !== LifecycleState2.OPEN) {
823
+ return void 0;
824
+ }
825
+ await this._params.db.del(keyArray, {
826
+ ...encodingOptions
862
827
  });
863
- this._indexMetadataStore = indexMetadataStore;
864
828
  }
865
- async _open() {
866
- this._peerId = `host-${PublicKey.random().toHex()}`;
867
- await this._storage.open?.();
868
- this._repo = new Repo({
869
- peerId: this._peerId,
870
- sharePolicy: this._sharePolicy.bind(this),
871
- storage: this._storage,
829
+ async loadRange(keyPrefix) {
830
+ if (this._lifecycleState !== LifecycleState2.OPEN) {
831
+ return [];
832
+ }
833
+ const startMs = Date.now();
834
+ const result = [];
835
+ for await (const [key, value] of this._params.db.iterator({
836
+ gte: keyPrefix,
837
+ lte: [
838
+ ...keyPrefix,
839
+ "\uFFFF"
840
+ ],
841
+ ...encodingOptions
842
+ })) {
843
+ result.push({
844
+ key,
845
+ data: value
846
+ });
847
+ this._params.monitor?.recordBytesLoaded(value.byteLength);
848
+ }
849
+ this._params.monitor?.recordLoadDuration(Date.now() - startMs);
850
+ return result;
851
+ }
852
+ async removeRange(keyPrefix) {
853
+ if (this._lifecycleState !== LifecycleState2.OPEN) {
854
+ return void 0;
855
+ }
856
+ const batch = this._params.db.batch();
857
+ for await (const [key] of this._params.db.iterator({
858
+ gte: keyPrefix,
859
+ lte: [
860
+ ...keyPrefix,
861
+ "\uFFFF"
862
+ ],
863
+ ...encodingOptions
864
+ })) {
865
+ batch.del(key, {
866
+ ...encodingOptions
867
+ });
868
+ }
869
+ await batch.write();
870
+ }
871
+ };
872
+ var keyEncoder = {
873
+ encode: (key) => Buffer.from(key.map((k) => k.replaceAll("%", "%25").replaceAll("-", "%2D")).join("-")),
874
+ decode: (key) => Buffer.from(key).toString().split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%")),
875
+ format: "buffer"
876
+ };
877
+ var encodingOptions = {
878
+ keyEncoding: keyEncoder,
879
+ valueEncoding: "buffer"
880
+ };
881
+ var isLevelDbNotFoundError = (err) => err.code === "LEVEL_NOT_FOUND";
882
+
883
+ // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
884
+ function _ts_decorate2(decorators, target, key, desc) {
885
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
886
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
887
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
888
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
889
+ }
890
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
891
+ var AutomergeHost = class extends Resource4 {
892
+ constructor({ db, indexMetadataStore, dataMonitor }) {
893
+ super();
894
+ this._collectionSynchronizer = new CollectionSynchronizer({
895
+ queryCollectionState: this._queryCollectionState.bind(this),
896
+ sendCollectionState: this._sendCollectionState.bind(this),
897
+ shouldSyncCollection: this._shouldSyncCollection.bind(this)
898
+ });
899
+ this.collectionStateUpdated = new Event2();
900
+ this._db = db;
901
+ this._storage = new LevelDBStorageAdapter({
902
+ db: db.sublevel("automerge"),
903
+ callbacks: {
904
+ beforeSave: async (params) => this._beforeSave(params),
905
+ afterSave: async (key) => this._afterSave(key)
906
+ },
907
+ monitor: dataMonitor
908
+ });
909
+ this._echoNetworkAdapter = new EchoNetworkAdapter({
910
+ getContainingSpaceForDocument: this._getContainingSpaceForDocument.bind(this),
911
+ isDocumentInRemoteCollection: this._isDocumentInRemoteCollection.bind(this),
912
+ onCollectionStateQueried: this._onCollectionStateQueried.bind(this),
913
+ onCollectionStateReceived: this._onCollectionStateReceived.bind(this),
914
+ monitor: dataMonitor
915
+ });
916
+ this._headsStore = new HeadsStore({
917
+ db: db.sublevel("heads")
918
+ });
919
+ this._indexMetadataStore = indexMetadataStore;
920
+ }
921
+ async _open() {
922
+ this._peerId = `host-${PublicKey.random().toHex()}`;
923
+ await this._storage.open?.();
924
+ this._repo = new Repo({
925
+ peerId: this._peerId,
926
+ sharePolicy: this._sharePolicy.bind(this),
927
+ storage: this._storage,
872
928
  network: [
873
929
  // Upstream swarm.
874
930
  this._echoNetworkAdapter
@@ -878,6 +934,9 @@ var AutomergeHost = class extends Resource4 {
878
934
  Event2.wrap(this._echoNetworkAdapter, "peer-disconnected").on(this._ctx, (e) => this._onPeerDisconnected(e.peerId));
879
935
  this._collectionSynchronizer.remoteStateUpdated.on(this._ctx, ({ collectionId, peerId }) => {
880
936
  this._onRemoteCollectionStateUpdated(collectionId, peerId);
937
+ this.collectionStateUpdated.emit({
938
+ collectionId
939
+ });
881
940
  });
882
941
  await this._echoNetworkAdapter.open();
883
942
  await this._collectionSynchronizer.open();
@@ -959,8 +1018,8 @@ var AutomergeHost = class extends Resource4 {
959
1018
  if (headsToWait.length > 0) {
960
1019
  await Promise.all(headsToWait.map(async (entry, index) => {
961
1020
  const handle = await this.loadDoc(Context.default(void 0, {
962
- F: __dxlog_file3,
963
- L: 227
1021
+ F: __dxlog_file4,
1022
+ L: 230
964
1023
  }), entry.documentId);
965
1024
  await waitForHeads(handle, entry.heads);
966
1025
  }));
@@ -969,11 +1028,11 @@ var AutomergeHost = class extends Resource4 {
969
1028
  }
970
1029
  async reIndexHeads(documentIds) {
971
1030
  for (const documentId of documentIds) {
972
- log3.info("re-indexing heads for document", {
1031
+ log4.info("re-indexing heads for document", {
973
1032
  documentId
974
1033
  }, {
975
- F: __dxlog_file3,
976
- L: 239,
1034
+ F: __dxlog_file4,
1035
+ L: 242,
977
1036
  S: this,
978
1037
  C: (f, a) => f(...a)
979
1038
  });
@@ -985,11 +1044,11 @@ var AutomergeHost = class extends Resource4 {
985
1044
  if (handle.inState([
986
1045
  "requesting"
987
1046
  ])) {
988
- log3.warn("document is not available locally, skipping", {
1047
+ log4.warn("document is not available locally, skipping", {
989
1048
  documentId
990
1049
  }, {
991
- F: __dxlog_file3,
992
- L: 243,
1050
+ F: __dxlog_file4,
1051
+ L: 246,
993
1052
  S: this,
994
1053
  C: (f, a) => f(...a)
995
1054
  });
@@ -997,8 +1056,8 @@ var AutomergeHost = class extends Resource4 {
997
1056
  }
998
1057
  const doc = handle.docSync();
999
1058
  invariant3(doc, void 0, {
1000
- F: __dxlog_file3,
1001
- L: 248,
1059
+ F: __dxlog_file4,
1060
+ L: 251,
1002
1061
  S: this,
1003
1062
  A: [
1004
1063
  "doc",
@@ -1010,9 +1069,9 @@ var AutomergeHost = class extends Resource4 {
1010
1069
  this._headsStore.setHeads(documentId, heads, batch);
1011
1070
  await batch.write();
1012
1071
  }
1013
- log3.info("done re-indexing heads", void 0, {
1014
- F: __dxlog_file3,
1015
- L: 255,
1072
+ log4.info("done re-indexing heads", void 0, {
1073
+ F: __dxlog_file4,
1074
+ L: 258,
1016
1075
  S: this,
1017
1076
  C: (f, a) => f(...a)
1018
1077
  });
@@ -1158,7 +1217,11 @@ var AutomergeHost = class extends Resource4 {
1158
1217
  const diff = diffCollectionState(localState, state);
1159
1218
  result.peers.push({
1160
1219
  peerId,
1161
- differentDocuments: diff.different.length
1220
+ missingOnRemote: diff.missingOnRemote.length,
1221
+ missingOnLocal: diff.missingOnLocal.length,
1222
+ differentDocuments: diff.different.length,
1223
+ localDocumentCount: Object.keys(localState.documents).length,
1224
+ remoteDocumentCount: Object.keys(state.documents).length
1162
1225
  });
1163
1226
  }
1164
1227
  return result;
@@ -1200,31 +1263,43 @@ var AutomergeHost = class extends Resource4 {
1200
1263
  if (!localState || !remoteState) {
1201
1264
  return;
1202
1265
  }
1203
- const { different } = diffCollectionState(localState, remoteState);
1204
- if (different.length === 0) {
1266
+ const { different, missingOnLocal, missingOnRemote } = diffCollectionState(localState, remoteState);
1267
+ const toReplicate = [
1268
+ ...missingOnLocal,
1269
+ ...missingOnRemote,
1270
+ ...different
1271
+ ];
1272
+ if (toReplicate.length === 0) {
1205
1273
  return;
1206
1274
  }
1207
- log3.info("replication documents after collection sync", {
1208
- count: different.length
1275
+ log4.info("replication documents after collection sync", {
1276
+ count: toReplicate.length
1209
1277
  }, {
1210
- F: __dxlog_file3,
1211
- L: 475,
1278
+ F: __dxlog_file4,
1279
+ L: 483,
1212
1280
  S: this,
1213
1281
  C: (f, a) => f(...a)
1214
1282
  });
1215
- for (const documentId of different) {
1283
+ for (const documentId of toReplicate) {
1216
1284
  this._repo.find(documentId);
1217
1285
  }
1218
1286
  }
1219
1287
  _onHeadsChanged(documentId, heads) {
1288
+ const collectionsChanged = /* @__PURE__ */ new Set();
1220
1289
  for (const collectionId of this._collectionSynchronizer.getRegisteredCollectionIds()) {
1221
1290
  const state = this._collectionSynchronizer.getLocalCollectionState(collectionId);
1222
1291
  if (state?.documents[documentId]) {
1223
1292
  const newState = structuredClone(state);
1224
1293
  newState.documents[documentId] = heads;
1225
1294
  this._collectionSynchronizer.setLocalCollectionState(collectionId, newState);
1295
+ collectionsChanged.add(collectionId);
1226
1296
  }
1227
1297
  }
1298
+ for (const collectionId of collectionsChanged) {
1299
+ this.collectionStateUpdated.emit({
1300
+ collectionId
1301
+ });
1302
+ }
1228
1303
  }
1229
1304
  };
1230
1305
  _ts_decorate2([
@@ -1267,8 +1342,8 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1267
1342
  };
1268
1343
  var decodeCollectionState = (state) => {
1269
1344
  invariant3(typeof state === "object" && state !== null, "Invalid state", {
1270
- F: __dxlog_file3,
1271
- L: 528,
1345
+ F: __dxlog_file4,
1346
+ L: 541,
1272
1347
  S: void 0,
1273
1348
  A: [
1274
1349
  "typeof state === 'object' && state !== null",
@@ -1281,37 +1356,18 @@ var encodeCollectionState = (state) => {
1281
1356
  return state;
1282
1357
  };
1283
1358
 
1284
- // packages/core/echo/echo-pipeline/src/automerge/space-collection.ts
1285
- import { invariant as invariant4 } from "@dxos/invariant";
1286
- import { SpaceId } from "@dxos/keys";
1287
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1288
- var deriveCollectionIdFromSpaceId = (spaceId) => `space:${spaceId}`;
1289
- var getSpaceIdFromCollectionId = (collectionId) => {
1290
- const spaceId = collectionId.replace(/^space:/, "");
1291
- invariant4(SpaceId.isValid(spaceId), void 0, {
1292
- F: __dxlog_file4,
1293
- L: 12,
1294
- S: void 0,
1295
- A: [
1296
- "SpaceId.isValid(spaceId)",
1297
- ""
1298
- ]
1299
- });
1300
- return spaceId;
1301
- };
1302
-
1303
1359
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1304
1360
  import { invariant as invariant6 } from "@dxos/invariant";
1305
1361
  import { PublicKey as PublicKey2 } from "@dxos/keys";
1306
- import { log as log5 } from "@dxos/log";
1362
+ import { log as log6 } from "@dxos/log";
1307
1363
  import { ComplexSet, defaultMap as defaultMap2 } from "@dxos/util";
1308
1364
 
1309
1365
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts
1310
1366
  import * as A2 from "@dxos/automerge/automerge";
1311
1367
  import { cbor } from "@dxos/automerge/automerge-repo";
1312
1368
  import { Resource as Resource5 } from "@dxos/context";
1313
- import { invariant as invariant5 } from "@dxos/invariant";
1314
- import { log as log4 } from "@dxos/log";
1369
+ import { invariant as invariant4 } from "@dxos/invariant";
1370
+ import { log as log5 } from "@dxos/log";
1315
1371
  import { AutomergeReplicator } from "@dxos/teleport-extension-automerge-replicator";
1316
1372
  var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1317
1373
  var DEFAULT_FACTORY = (params) => new AutomergeReplicator(...params);
@@ -1331,7 +1387,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1331
1387
  });
1332
1388
  this.writable = new WritableStream({
1333
1389
  write: async (message, controller) => {
1334
- invariant5(this._isEnabled, "Writing to a disabled connection", {
1390
+ invariant4(this._isEnabled, "Writing to a disabled connection", {
1335
1391
  F: __dxlog_file5,
1336
1392
  L: 49,
1337
1393
  S: this,
@@ -1360,7 +1416,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1360
1416
  onStartReplication: async (info, remotePeerId) => {
1361
1417
  this.remoteDeviceKey = remotePeerId;
1362
1418
  this._remotePeerId = info.id;
1363
- log4("onStartReplication", {
1419
+ log5("onStartReplication", {
1364
1420
  id: info.id,
1365
1421
  thisPeerId: this.peerId,
1366
1422
  remotePeerId: remotePeerId.toHex()
@@ -1391,7 +1447,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1391
1447
  }
1392
1448
  }
1393
1449
  get peerId() {
1394
- invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1450
+ invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1395
1451
  F: __dxlog_file5,
1396
1452
  L: 110,
1397
1453
  S: this,
@@ -1413,7 +1469,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1413
1469
  * Call after the remote peer has connected.
1414
1470
  */
1415
1471
  enable() {
1416
- invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1472
+ invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1417
1473
  F: __dxlog_file5,
1418
1474
  L: 127,
1419
1475
  S: this,
@@ -1432,7 +1488,7 @@ var MeshReplicatorConnection = class extends Resource5 {
1432
1488
  }
1433
1489
  };
1434
1490
  var logSendSync = (message) => {
1435
- log4("sendSyncMessage", () => {
1491
+ log5("sendSyncMessage", () => {
1436
1492
  const decodedSyncMessage = message.type === "sync" && message.data ? A2.decodeSyncMessage(message.data) : void 0;
1437
1493
  return {
1438
1494
  sync: decodedSyncMessage && {
@@ -1452,8 +1508,27 @@ var logSendSync = (message) => {
1452
1508
  });
1453
1509
  };
1454
1510
 
1511
+ // packages/core/echo/echo-pipeline/src/automerge/space-collection.ts
1512
+ import { invariant as invariant5 } from "@dxos/invariant";
1513
+ import { SpaceId } from "@dxos/keys";
1514
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1515
+ var deriveCollectionIdFromSpaceId = (spaceId) => `space:${spaceId}`;
1516
+ var getSpaceIdFromCollectionId = (collectionId) => {
1517
+ const spaceId = collectionId.replace(/^space:/, "");
1518
+ invariant5(SpaceId.isValid(spaceId), void 0, {
1519
+ F: __dxlog_file6,
1520
+ L: 13,
1521
+ S: void 0,
1522
+ A: [
1523
+ "SpaceId.isValid(spaceId)",
1524
+ ""
1525
+ ]
1526
+ });
1527
+ return spaceId;
1528
+ };
1529
+
1455
1530
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1456
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1531
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1457
1532
  var MeshEchoReplicator = class {
1458
1533
  constructor() {
1459
1534
  this._connections = /* @__PURE__ */ new Set();
@@ -1483,8 +1558,8 @@ var MeshEchoReplicator = class {
1483
1558
  }
1484
1559
  createExtension(extensionFactory) {
1485
1560
  invariant6(this._context, void 0, {
1486
- F: __dxlog_file6,
1487
- L: 56,
1561
+ F: __dxlog_file7,
1562
+ L: 57,
1488
1563
  S: this,
1489
1564
  A: [
1490
1565
  "this._context",
@@ -1495,17 +1570,17 @@ var MeshEchoReplicator = class {
1495
1570
  ownPeerId: this._context.peerId,
1496
1571
  replicatorFactory: extensionFactory,
1497
1572
  onRemoteConnected: async () => {
1498
- log5("onRemoteConnected", {
1573
+ log6("onRemoteConnected", {
1499
1574
  peerId: connection.peerId
1500
1575
  }, {
1501
- F: __dxlog_file6,
1502
- L: 62,
1576
+ F: __dxlog_file7,
1577
+ L: 63,
1503
1578
  S: this,
1504
1579
  C: (f, a) => f(...a)
1505
1580
  });
1506
1581
  invariant6(this._context, void 0, {
1507
- F: __dxlog_file6,
1508
- L: 63,
1582
+ F: __dxlog_file7,
1583
+ L: 64,
1509
1584
  S: this,
1510
1585
  A: [
1511
1586
  "this._context",
@@ -1521,11 +1596,11 @@ var MeshEchoReplicator = class {
1521
1596
  }
1522
1597
  },
1523
1598
  onRemoteDisconnected: async () => {
1524
- log5("onRemoteDisconnected", {
1599
+ log6("onRemoteDisconnected", {
1525
1600
  peerId: connection.peerId
1526
1601
  }, {
1527
- F: __dxlog_file6,
1528
- L: 74,
1602
+ F: __dxlog_file7,
1603
+ L: 75,
1529
1604
  S: this,
1530
1605
  C: (f, a) => f(...a)
1531
1606
  });
@@ -1535,18 +1610,18 @@ var MeshEchoReplicator = class {
1535
1610
  this._connections.delete(connection);
1536
1611
  },
1537
1612
  shouldAdvertise: async (params) => {
1538
- log5("shouldAdvertise", {
1613
+ log6("shouldAdvertise", {
1539
1614
  peerId: connection.peerId,
1540
1615
  documentId: params.documentId
1541
1616
  }, {
1542
- F: __dxlog_file6,
1543
- L: 81,
1617
+ F: __dxlog_file7,
1618
+ L: 82,
1544
1619
  S: this,
1545
1620
  C: (f, a) => f(...a)
1546
1621
  });
1547
1622
  invariant6(this._context, void 0, {
1548
- F: __dxlog_file6,
1549
- L: 82,
1623
+ F: __dxlog_file7,
1624
+ L: 83,
1550
1625
  S: this,
1551
1626
  A: [
1552
1627
  "this._context",
@@ -1560,13 +1635,13 @@ var MeshEchoReplicator = class {
1560
1635
  documentId: params.documentId,
1561
1636
  peerId: connection.peerId
1562
1637
  });
1563
- log5("document not found locally for share policy check, accepting the remote document", {
1638
+ log6("document not found locally for share policy check, accepting the remote document", {
1564
1639
  peerId: connection.peerId,
1565
1640
  documentId: params.documentId,
1566
1641
  remoteDocumentExists
1567
1642
  }, {
1568
- F: __dxlog_file6,
1569
- L: 90,
1643
+ F: __dxlog_file7,
1644
+ L: 91,
1570
1645
  S: this,
1571
1646
  C: (f, a) => f(...a)
1572
1647
  });
@@ -1575,19 +1650,19 @@ var MeshEchoReplicator = class {
1575
1650
  const spaceId = await createIdFromSpaceKey(spaceKey);
1576
1651
  const authorizedDevices = this._authorizedDevices.get(spaceId);
1577
1652
  if (!connection.remoteDeviceKey) {
1578
- log5("device key not found for share policy check", {
1653
+ log6("device key not found for share policy check", {
1579
1654
  peerId: connection.peerId,
1580
1655
  documentId: params.documentId
1581
1656
  }, {
1582
- F: __dxlog_file6,
1583
- L: 106,
1657
+ F: __dxlog_file7,
1658
+ L: 107,
1584
1659
  S: this,
1585
1660
  C: (f, a) => f(...a)
1586
1661
  });
1587
1662
  return false;
1588
1663
  }
1589
1664
  const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1590
- log5("share policy check", {
1665
+ log6("share policy check", {
1591
1666
  localPeer: this._context.peerId,
1592
1667
  remotePeer: connection.peerId,
1593
1668
  documentId: params.documentId,
@@ -1595,16 +1670,16 @@ var MeshEchoReplicator = class {
1595
1670
  spaceKey,
1596
1671
  isAuthorized
1597
1672
  }, {
1598
- F: __dxlog_file6,
1599
- L: 114,
1673
+ F: __dxlog_file7,
1674
+ L: 115,
1600
1675
  S: this,
1601
1676
  C: (f, a) => f(...a)
1602
1677
  });
1603
1678
  return isAuthorized;
1604
1679
  } catch (err) {
1605
- log5.catch(err, void 0, {
1606
- F: __dxlog_file6,
1607
- L: 124,
1680
+ log6.catch(err, void 0, {
1681
+ F: __dxlog_file7,
1682
+ L: 125,
1608
1683
  S: this,
1609
1684
  C: (f, a) => f(...a)
1610
1685
  });
@@ -1615,12 +1690,12 @@ var MeshEchoReplicator = class {
1615
1690
  const spaceId = getSpaceIdFromCollectionId(collectionId);
1616
1691
  const authorizedDevices = this._authorizedDevices.get(spaceId);
1617
1692
  if (!connection.remoteDeviceKey) {
1618
- log5("device key not found for collection sync check", {
1693
+ log6("device key not found for collection sync check", {
1619
1694
  peerId: connection.peerId,
1620
1695
  collectionId
1621
1696
  }, {
1622
- F: __dxlog_file6,
1623
- L: 134,
1697
+ F: __dxlog_file7,
1698
+ L: 135,
1624
1699
  S: this,
1625
1700
  C: (f, a) => f(...a)
1626
1701
  });
@@ -1634,12 +1709,12 @@ var MeshEchoReplicator = class {
1634
1709
  return connection.replicatorExtension;
1635
1710
  }
1636
1711
  async authorizeDevice(spaceKey, deviceKey) {
1637
- log5("authorizeDevice", {
1712
+ log6("authorizeDevice", {
1638
1713
  spaceKey,
1639
1714
  deviceKey
1640
1715
  }, {
1641
- F: __dxlog_file6,
1642
- L: 151,
1716
+ F: __dxlog_file7,
1717
+ L: 152,
1643
1718
  S: this,
1644
1719
  C: (f, a) => f(...a)
1645
1720
  });
@@ -2017,11 +2092,7 @@ var getByteCount = (message) => {
2017
2092
  };
2018
2093
 
2019
2094
  // packages/core/echo/echo-pipeline/src/db-host/data-service.ts
2020
- import { Stream } from "@dxos/codec-protobuf";
2021
- import { invariant as invariant7 } from "@dxos/invariant";
2022
- import { SpaceId as SpaceId2 } from "@dxos/keys";
2023
- import { log as log6 } from "@dxos/log";
2024
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service.ts";
2095
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service.ts";
2025
2096
  var DataServiceImpl = class {
2026
2097
  constructor(params) {
2027
2098
  /**
@@ -2041,9 +2112,9 @@ var DataServiceImpl = class {
2041
2112
  synchronizer.open().then(() => {
2042
2113
  this._subscriptions.set(request.subscriptionId, synchronizer);
2043
2114
  ready();
2044
- }).catch((err) => log6.catch(err, void 0, {
2045
- F: __dxlog_file7,
2046
- L: 64,
2115
+ }).catch((err) => log7.catch(err, void 0, {
2116
+ F: __dxlog_file8,
2117
+ L: 66,
2047
2118
  S: this,
2048
2119
  C: (f, a) => f(...a)
2049
2120
  }));
@@ -2053,8 +2124,8 @@ var DataServiceImpl = class {
2053
2124
  async updateSubscription(request) {
2054
2125
  const synchronizer = this._subscriptions.get(request.subscriptionId);
2055
2126
  invariant7(synchronizer, "Subscription not found", {
2056
- F: __dxlog_file7,
2057
- L: 71,
2127
+ F: __dxlog_file8,
2128
+ L: 73,
2058
2129
  S: this,
2059
2130
  A: [
2060
2131
  "synchronizer",
@@ -2074,8 +2145,8 @@ var DataServiceImpl = class {
2074
2145
  }
2075
2146
  const synchronizer = this._subscriptions.get(request.subscriptionId);
2076
2147
  invariant7(synchronizer, "Subscription not found", {
2077
- F: __dxlog_file7,
2078
- L: 86,
2148
+ F: __dxlog_file8,
2149
+ L: 88,
2079
2150
  S: this,
2080
2151
  A: [
2081
2152
  "synchronizer",
@@ -2115,38 +2186,1272 @@ var DataServiceImpl = class {
2115
2186
  async updateIndexes() {
2116
2187
  await this._updateIndexes();
2117
2188
  }
2118
- async getSpaceSyncState(request, options) {
2119
- invariant7(SpaceId2.isValid(request.spaceId), void 0, {
2120
- F: __dxlog_file7,
2121
- L: 127,
2122
- S: this,
2123
- A: [
2124
- "SpaceId.isValid(request.spaceId)",
2125
- ""
2126
- ]
2189
+ subscribeSpaceSyncState(request) {
2190
+ return new Stream(({ ctx, next, ready }) => {
2191
+ invariant7(SpaceId2.isValid(request.spaceId), void 0, {
2192
+ F: __dxlog_file8,
2193
+ L: 127,
2194
+ S: this,
2195
+ A: [
2196
+ "SpaceId.isValid(request.spaceId)",
2197
+ ""
2198
+ ]
2199
+ });
2200
+ const collectionId = deriveCollectionIdFromSpaceId(request.spaceId);
2201
+ const scheduler = new UpdateScheduler2(ctx, async () => {
2202
+ const state = await this._automergeHost.getCollectionSyncState(collectionId);
2203
+ next({
2204
+ peers: state.peers.map((peer) => ({
2205
+ peerId: peer.peerId,
2206
+ missingOnRemote: peer.missingOnRemote,
2207
+ missingOnLocal: peer.missingOnLocal,
2208
+ differentDocuments: peer.differentDocuments,
2209
+ localDocumentCount: peer.localDocumentCount,
2210
+ remoteDocumentCount: peer.remoteDocumentCount
2211
+ }))
2212
+ });
2213
+ });
2214
+ this._automergeHost.collectionStateUpdated.on(ctx, (e) => {
2215
+ if (e.collectionId === collectionId) {
2216
+ scheduler.trigger();
2217
+ }
2218
+ });
2219
+ scheduler.trigger();
2127
2220
  });
2128
- const collectionId = deriveCollectionIdFromSpaceId(request.spaceId);
2129
- const state = await this._automergeHost.getCollectionSyncState(collectionId);
2221
+ }
2222
+ };
2223
+
2224
+ // packages/core/echo/echo-pipeline/src/db-host/echo-host.ts
2225
+ import { LifecycleState as LifecycleState4, Resource as Resource9 } from "@dxos/context";
2226
+ import { todo } from "@dxos/debug";
2227
+ import { createIdFromSpaceKey as createIdFromSpaceKey3, SpaceDocVersion as SpaceDocVersion3 } from "@dxos/echo-protocol";
2228
+ import { IndexMetadataStore, IndexStore, Indexer } from "@dxos/indexing";
2229
+ import { invariant as invariant11 } from "@dxos/invariant";
2230
+ import { IndexKind } from "@dxos/protocols/proto/dxos/echo/indexing";
2231
+ import { trace as trace5 } from "@dxos/tracing";
2232
+
2233
+ // packages/core/echo/echo-pipeline/src/db-host/documents-iterator.ts
2234
+ import * as A3 from "@dxos/automerge/automerge";
2235
+ import { Context as Context2 } from "@dxos/context";
2236
+ import { SpaceDocVersion } from "@dxos/echo-protocol";
2237
+ import { invariant as invariant8 } from "@dxos/invariant";
2238
+ import { log as log8 } from "@dxos/log";
2239
+ import { ObjectPointerVersion, objectPointerCodec as objectPointerCodec2 } from "@dxos/protocols";
2240
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/documents-iterator.ts";
2241
+ var LOG_VIEW_OPERATION_THRESHOLD = 300;
2242
+ var createSelectedDocumentsIterator = (automergeHost) => (
2243
+ /**
2244
+ * Get object data blobs from Automerge Repo by ids.
2245
+ */
2246
+ // TODO(mykola): Unload automerge handles after usage.
2247
+ async function* loadDocuments(objects) {
2248
+ for (const [id, heads] of objects.entries()) {
2249
+ try {
2250
+ const { documentId, objectId } = objectPointerCodec2.decode(id);
2251
+ const handle = await automergeHost.loadDoc(Context2.default(void 0, {
2252
+ F: __dxlog_file9,
2253
+ L: 30
2254
+ }), documentId);
2255
+ let doc = handle.docSync();
2256
+ invariant8(doc, void 0, {
2257
+ F: __dxlog_file9,
2258
+ L: 33,
2259
+ S: this,
2260
+ A: [
2261
+ "doc",
2262
+ ""
2263
+ ]
2264
+ });
2265
+ const currentHeads = A3.getHeads(doc);
2266
+ if (!A3.equals(currentHeads, heads)) {
2267
+ const begin = Date.now();
2268
+ doc = A3.view(doc, heads);
2269
+ const end = Date.now();
2270
+ if (end - begin > LOG_VIEW_OPERATION_THRESHOLD) {
2271
+ log8.info("Checking out document version is taking too long", {
2272
+ duration: end - begin,
2273
+ requestedHeads: heads,
2274
+ originalHeads: currentHeads
2275
+ }, {
2276
+ F: __dxlog_file9,
2277
+ L: 44,
2278
+ S: this,
2279
+ C: (f, a) => f(...a)
2280
+ });
2281
+ }
2282
+ }
2283
+ if (doc.version !== SpaceDocVersion.CURRENT) {
2284
+ continue;
2285
+ }
2286
+ if (!doc.objects?.[objectId]) {
2287
+ continue;
2288
+ }
2289
+ let newId = id;
2290
+ if (objectPointerCodec2.getVersion(id) === ObjectPointerVersion.V0) {
2291
+ const spaceKey = getSpaceKeyFromDoc(doc) ?? void 0;
2292
+ newId = objectPointerCodec2.encode({
2293
+ documentId,
2294
+ objectId,
2295
+ spaceKey
2296
+ });
2297
+ }
2298
+ yield [
2299
+ {
2300
+ id: newId,
2301
+ object: doc.objects[objectId],
2302
+ heads
2303
+ }
2304
+ ];
2305
+ } catch (error) {
2306
+ log8.error("Error loading document", {
2307
+ heads,
2308
+ id,
2309
+ error
2310
+ }, {
2311
+ F: __dxlog_file9,
2312
+ L: 70,
2313
+ S: this,
2314
+ C: (f, a) => f(...a)
2315
+ });
2316
+ }
2317
+ }
2318
+ }
2319
+ );
2320
+
2321
+ // packages/core/echo/echo-pipeline/src/db-host/query-service.ts
2322
+ import { DeferredTask } from "@dxos/async";
2323
+ import { getHeads as getHeads3 } from "@dxos/automerge/automerge";
2324
+ import { Stream as Stream2 } from "@dxos/codec-protobuf";
2325
+ import { Context as Context4, Resource as Resource7 } from "@dxos/context";
2326
+ import { log as log9 } from "@dxos/log";
2327
+ import { objectPointerCodec as objectPointerCodec4 } from "@dxos/protocols";
2328
+ import { trace as trace4 } from "@dxos/tracing";
2329
+
2330
+ // packages/core/echo/echo-pipeline/src/db-host/query-state.ts
2331
+ import { Context as Context3, LifecycleState as LifecycleState3, Resource as Resource6 } from "@dxos/context";
2332
+ import { createIdFromSpaceKey as createIdFromSpaceKey2 } from "@dxos/echo-protocol";
2333
+ import { invariant as invariant9 } from "@dxos/invariant";
2334
+ import { PublicKey as PublicKey3 } from "@dxos/keys";
2335
+ import { objectPointerCodec as objectPointerCodec3 } from "@dxos/protocols";
2336
+ import { trace as trace3 } from "@dxos/tracing";
2337
+ import { nonNullable as nonNullable2 } from "@dxos/util";
2338
+ function _ts_decorate4(decorators, target, key, desc) {
2339
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2340
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
2341
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2342
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2343
+ }
2344
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/query-state.ts";
2345
+ var QueryState = class extends Resource6 {
2346
+ get active() {
2347
+ return this._lifecycleState === LifecycleState3.OPEN;
2348
+ }
2349
+ constructor(_params) {
2350
+ super();
2351
+ this._params = _params;
2352
+ this._results = [];
2353
+ this._firstRun = true;
2354
+ this.metrics = {
2355
+ objectsReturned: 0,
2356
+ objectsReturnedFromIndex: 0,
2357
+ documentsLoaded: 0,
2358
+ executionTime: 0,
2359
+ indexQueryTime: 0,
2360
+ documentLoadTime: 0
2361
+ };
2362
+ this.filter = _params.request.filter;
2363
+ }
2364
+ getResults() {
2365
+ return this._results;
2366
+ }
2367
+ // https://github.com/open-telemetry/semantic-conventions/blob/main/docs/attributes-registry/db.md#generic-database-attributes
2368
+ async execQuery() {
2369
+ const filter = this._params.request.filter;
2370
+ const beginQuery = performance.now();
2371
+ const hits = await this._params.indexer.execQuery(filterToIndexQuery(filter));
2372
+ if (this._firstRun) {
2373
+ this.metrics.indexQueryTime = performance.now() - beginQuery;
2374
+ }
2375
+ const beginFilter = performance.now();
2376
+ const results = (await Promise.all(hits.map(async (result) => {
2377
+ if (this._firstRun) {
2378
+ this.metrics.objectsReturnedFromIndex++;
2379
+ }
2380
+ const { objectId, documentId, spaceKey: spaceKeyInIndex } = objectPointerCodec3.decode(result.id);
2381
+ let spaceKey;
2382
+ if (spaceKeyInIndex !== void 0) {
2383
+ spaceKey = spaceKeyInIndex;
2384
+ } else {
2385
+ if (this._firstRun) {
2386
+ this.metrics.documentsLoaded++;
2387
+ }
2388
+ const handle = await this._params.automergeHost.loadDoc(Context3.default(void 0, {
2389
+ F: __dxlog_file10,
2390
+ L: 110
2391
+ }), documentId);
2392
+ if (this._ctx.disposed) {
2393
+ return;
2394
+ }
2395
+ spaceKey = getSpaceKeyFromDoc(handle.docSync());
2396
+ }
2397
+ if (!spaceKey) {
2398
+ return;
2399
+ }
2400
+ if (this._params.request.filter.options?.spaces?.length && !this._params.request.filter.options.spaces.some((key) => key.equals(spaceKey))) {
2401
+ return;
2402
+ }
2403
+ if (this._firstRun) {
2404
+ this.metrics.objectsReturned++;
2405
+ }
2406
+ return {
2407
+ id: objectId,
2408
+ documentId,
2409
+ spaceId: await createIdFromSpaceKey2(PublicKey3.from(spaceKey)),
2410
+ spaceKey: PublicKey3.from(spaceKey),
2411
+ rank: result.rank
2412
+ };
2413
+ }))).filter(nonNullable2);
2414
+ if (this._firstRun) {
2415
+ this.metrics.documentLoadTime = performance.now() - beginFilter;
2416
+ }
2417
+ if (this._ctx.disposed) {
2418
+ return {
2419
+ changed: false
2420
+ };
2421
+ }
2422
+ const areResultsUnchanged = !this._firstRun && this._results.length === results.length && this._results.every((oldResult) => results.some((result) => result.id === oldResult.id)) && results.every((result) => this._results.some((oldResult) => oldResult.id === result.id));
2423
+ if (this._firstRun) {
2424
+ this.metrics.executionTime = performance.now() - beginQuery;
2425
+ }
2426
+ this._firstRun = false;
2427
+ if (areResultsUnchanged) {
2428
+ return {
2429
+ changed: false
2430
+ };
2431
+ }
2432
+ this._results = results;
2433
+ return {
2434
+ changed: true
2435
+ };
2436
+ }
2437
+ };
2438
+ _ts_decorate4([
2439
+ trace3.info({
2440
+ depth: null
2441
+ })
2442
+ ], QueryState.prototype, "filter", void 0);
2443
+ _ts_decorate4([
2444
+ trace3.info()
2445
+ ], QueryState.prototype, "metrics", void 0);
2446
+ _ts_decorate4([
2447
+ trace3.info()
2448
+ ], QueryState.prototype, "active", null);
2449
+ _ts_decorate4([
2450
+ trace3.span({
2451
+ showInBrowserTimeline: true,
2452
+ op: "db.query",
2453
+ attributes: {
2454
+ "db.system": "echo"
2455
+ }
2456
+ })
2457
+ ], QueryState.prototype, "execQuery", null);
2458
+ QueryState = _ts_decorate4([
2459
+ trace3.resource()
2460
+ ], QueryState);
2461
+ var filterToIndexQuery = (filter) => {
2462
+ invariant9(!(filter.type && (filter.or ?? []).length > 0), "Cannot mix type and or filters.", {
2463
+ F: __dxlog_file10,
2464
+ L: 175,
2465
+ S: void 0,
2466
+ A: [
2467
+ "!(filter.type && (filter.or ?? []).length > 0)",
2468
+ "'Cannot mix type and or filters.'"
2469
+ ]
2470
+ });
2471
+ invariant9((filter.or ?? []).every((subFilter) => !(subFilter.type && (subFilter.or ?? []).length > 0)), "Cannot mix type and or filters.", {
2472
+ F: __dxlog_file10,
2473
+ L: 176,
2474
+ S: void 0,
2475
+ A: [
2476
+ "(filter.or ?? []).every((subFilter) => !(subFilter.type && (subFilter.or ?? []).length > 0))",
2477
+ "'Cannot mix type and or filters.'"
2478
+ ]
2479
+ });
2480
+ if (filter.type || (filter.or ?? []).length > 0 && (filter.or ?? []).every((subFilter) => !subFilter.not && subFilter.type)) {
2481
+ return {
2482
+ typenames: filter.type?.objectId ? [
2483
+ filter.type.objectId
2484
+ ] : (filter.or ?? []).map((f) => f.type?.objectId).filter(nonNullable2),
2485
+ inverted: filter.not
2486
+ };
2487
+ } else {
2130
2488
  return {
2131
- peers: state.peers.map((peer) => ({
2132
- peerId: peer.peerId,
2133
- documentsToReconcile: peer.differentDocuments
2134
- }))
2489
+ typenames: []
2135
2490
  };
2136
2491
  }
2137
2492
  };
2138
2493
 
2139
- export {
2140
- DocumentsSynchronizer,
2141
- diffCollectionState,
2142
- LevelDBStorageAdapter,
2143
- encodingOptions,
2494
+ // packages/core/echo/echo-pipeline/src/db-host/query-service.ts
2495
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/query-service.ts";
2496
+ var QueryServiceImpl = class extends Resource7 {
2497
+ // TODO(burdon): OK for options, but not params. Pass separately and type readonly here.
2498
+ constructor(_params) {
2499
+ super();
2500
+ this._params = _params;
2501
+ this._queries = /* @__PURE__ */ new Set();
2502
+ this._updateQueries = new DeferredTask(this._ctx, async () => {
2503
+ await Promise.all(Array.from(this._queries).map(async (query) => {
2504
+ try {
2505
+ const { changed } = await query.state.execQuery();
2506
+ if (changed) {
2507
+ query.sendResults(query.state.getResults());
2508
+ }
2509
+ } catch (err) {
2510
+ log9.catch(err, void 0, {
2511
+ F: __dxlog_file11,
2512
+ L: 52,
2513
+ S: this,
2514
+ C: (f, a) => f(...a)
2515
+ });
2516
+ }
2517
+ }));
2518
+ });
2519
+ trace4.diagnostic({
2520
+ id: "active-queries",
2521
+ name: "Active Queries",
2522
+ fetch: () => {
2523
+ return Array.from(this._queries).map((query) => {
2524
+ return {
2525
+ filter: JSON.stringify(query.state.filter),
2526
+ metrics: query.state.metrics
2527
+ };
2528
+ });
2529
+ }
2530
+ });
2531
+ }
2532
+ async _open() {
2533
+ this._params.indexer.updated.on(this._ctx, () => this._updateQueries.schedule());
2534
+ }
2535
+ async _close() {
2536
+ await Promise.all(Array.from(this._queries).map((query) => query.close()));
2537
+ }
2538
+ async setConfig(config) {
2539
+ if (this._params.indexer.initialized) {
2540
+ log9.warn("Indexer already initialized.", void 0, {
2541
+ F: __dxlog_file11,
2542
+ L: 86,
2543
+ S: this,
2544
+ C: (f, a) => f(...a)
2545
+ });
2546
+ return;
2547
+ }
2548
+ this._params.indexer.setConfig(config);
2549
+ }
2550
+ execQuery(request) {
2551
+ return new Stream2(({ next, close, ctx }) => {
2552
+ const query = {
2553
+ state: new QueryState({
2554
+ indexer: this._params.indexer,
2555
+ automergeHost: this._params.automergeHost,
2556
+ request
2557
+ }),
2558
+ sendResults: (results) => {
2559
+ if (ctx.disposed) {
2560
+ return;
2561
+ }
2562
+ next({
2563
+ queryId: request.queryId,
2564
+ results
2565
+ });
2566
+ },
2567
+ close: async () => {
2568
+ close();
2569
+ await query.state.close();
2570
+ this._queries.delete(query);
2571
+ }
2572
+ };
2573
+ this._queries.add(query);
2574
+ queueMicrotask(async () => {
2575
+ await query.state.open();
2576
+ try {
2577
+ const { changed } = await query.state.execQuery();
2578
+ if (changed) {
2579
+ query.sendResults(query.state.getResults());
2580
+ }
2581
+ } catch (error) {
2582
+ log9.catch(error, void 0, {
2583
+ F: __dxlog_file11,
2584
+ L: 123,
2585
+ S: this,
2586
+ C: (f, a) => f(...a)
2587
+ });
2588
+ }
2589
+ });
2590
+ return query.close;
2591
+ });
2592
+ }
2593
+ /**
2594
+ * Re-index all loaded documents.
2595
+ */
2596
+ async reindex() {
2597
+ log9.info("Reindexing all documents...", void 0, {
2598
+ F: __dxlog_file11,
2599
+ L: 135,
2600
+ S: this,
2601
+ C: (f, a) => f(...a)
2602
+ });
2603
+ const iterator = createDocumentsIterator(this._params.automergeHost);
2604
+ const ids = /* @__PURE__ */ new Map();
2605
+ for await (const documents of iterator()) {
2606
+ for (const { id, heads } of documents) {
2607
+ ids.set(id, heads);
2608
+ }
2609
+ if (ids.size % 100 === 0) {
2610
+ log9.info("Collected documents...", {
2611
+ count: ids.size
2612
+ }, {
2613
+ F: __dxlog_file11,
2614
+ L: 143,
2615
+ S: this,
2616
+ C: (f, a) => f(...a)
2617
+ });
2618
+ }
2619
+ }
2620
+ log9.info("Marking all documents as dirty...", {
2621
+ count: ids.size
2622
+ }, {
2623
+ F: __dxlog_file11,
2624
+ L: 147,
2625
+ S: this,
2626
+ C: (f, a) => f(...a)
2627
+ });
2628
+ await this._params.indexer.reindex(ids);
2629
+ }
2630
+ };
2631
+ var createDocumentsIterator = (automergeHost) => (
2632
+ /**
2633
+ * Recursively get all object data blobs from loaded documents from Automerge Repo.
2634
+ */
2635
+ // TODO(mykola): Unload automerge handles after usage.
2636
+ async function* getAllDocuments() {
2637
+ const visited = /* @__PURE__ */ new Set();
2638
+ async function* getObjectsFromHandle(handle) {
2639
+ if (visited.has(handle.documentId)) {
2640
+ return;
2641
+ }
2642
+ const doc = handle.docSync();
2643
+ const spaceKey = getSpaceKeyFromDoc(doc) ?? void 0;
2644
+ if (doc.objects) {
2645
+ yield Object.entries(doc.objects).map(([objectId, object]) => {
2646
+ return {
2647
+ id: objectPointerCodec4.encode({
2648
+ documentId: handle.documentId,
2649
+ objectId,
2650
+ spaceKey
2651
+ }),
2652
+ object,
2653
+ heads: getHeads3(doc)
2654
+ };
2655
+ });
2656
+ }
2657
+ if (doc.links) {
2658
+ for (const id of Object.values(doc.links)) {
2659
+ if (visited.has(id)) {
2660
+ continue;
2661
+ }
2662
+ const linkHandle = await automergeHost.loadDoc(Context4.default(void 0, {
2663
+ F: __dxlog_file11,
2664
+ L: 188
2665
+ }), id);
2666
+ for await (const result of getObjectsFromHandle(linkHandle)) {
2667
+ yield result;
2668
+ }
2669
+ }
2670
+ }
2671
+ visited.add(handle.documentId);
2672
+ }
2673
+ for (const handle of Object.values(automergeHost.repo.handles)) {
2674
+ if (visited.has(handle.documentId)) {
2675
+ continue;
2676
+ }
2677
+ for await (const result of getObjectsFromHandle(handle)) {
2678
+ yield result;
2679
+ }
2680
+ visited.add(handle.documentId);
2681
+ }
2682
+ }
2683
+ );
2684
+
2685
+ // packages/core/echo/echo-pipeline/src/db-host/space-state-manager.ts
2686
+ import isEqual from "lodash.isequal";
2687
+ import { Event as Event3, UpdateScheduler as UpdateScheduler3 } from "@dxos/async";
2688
+ import { interpretAsDocumentId } from "@dxos/automerge/automerge-repo";
2689
+ import { Resource as Resource8, Context as Context5 } from "@dxos/context";
2690
+
2691
+ // packages/core/echo/echo-pipeline/src/db-host/database-root.ts
2692
+ import { SpaceDocVersion as SpaceDocVersion2 } from "@dxos/echo-protocol";
2693
+ import { invariant as invariant10 } from "@dxos/invariant";
2694
+
2695
+ // packages/core/echo/echo-pipeline/src/db-host/automerge-metrics.ts
2696
+ import * as A4 from "@dxos/automerge/automerge";
2697
+ import { log as log10 } from "@dxos/log";
2698
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/automerge-metrics.ts";
2699
+ var measureDocMetrics = (doc) => {
2700
+ const snapshot = A4.save(doc);
2701
+ const start = Date.now();
2702
+ const temp = A4.load(snapshot);
2703
+ const end = Date.now();
2704
+ A4.free(temp);
2705
+ const getAllChangesStart = Date.now();
2706
+ const mutationCount = A4.getAllChanges(doc).length;
2707
+ const getAllChangesEnd = Date.now();
2708
+ if (getAllChangesEnd - getAllChangesStart > 300) {
2709
+ log10.warn("getAllChanges took too long", {
2710
+ elapsed: getAllChangesEnd - getAllChangesStart
2711
+ }, {
2712
+ F: __dxlog_file12,
2713
+ L: 30,
2714
+ S: void 0,
2715
+ C: (f, a) => f(...a)
2716
+ });
2717
+ }
2718
+ return {
2719
+ compressedByteSize: snapshot.byteLength,
2720
+ loadTime: end - start,
2721
+ mutationCount
2722
+ };
2723
+ };
2724
+
2725
+ // packages/core/echo/echo-pipeline/src/db-host/database-root.ts
2726
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/database-root.ts";
2727
+ var DatabaseRoot = class {
2728
+ constructor(_rootHandle) {
2729
+ this._rootHandle = _rootHandle;
2730
+ }
2731
+ get documentId() {
2732
+ return this._rootHandle.documentId;
2733
+ }
2734
+ get url() {
2735
+ return this._rootHandle.url;
2736
+ }
2737
+ get isLoaded() {
2738
+ return !!this._rootHandle.docSync();
2739
+ }
2740
+ get handle() {
2741
+ return this._rootHandle;
2742
+ }
2743
+ docSync() {
2744
+ return this._rootHandle.docSync();
2745
+ }
2746
+ getVersion() {
2747
+ const doc = this.docSync();
2748
+ if (!doc) {
2749
+ return null;
2750
+ }
2751
+ return doc.version ?? SpaceDocVersion2.LEGACY;
2752
+ }
2753
+ getSpaceKey() {
2754
+ const doc = this.docSync();
2755
+ if (!doc) {
2756
+ return null;
2757
+ }
2758
+ return getSpaceKeyFromDoc(doc);
2759
+ }
2760
+ getInlineObjectCount() {
2761
+ const doc = this.docSync();
2762
+ if (!doc) {
2763
+ return null;
2764
+ }
2765
+ return Object.keys(doc.objects ?? {}).length;
2766
+ }
2767
+ getLinkedObjectCount() {
2768
+ const doc = this.docSync();
2769
+ if (!doc) {
2770
+ return null;
2771
+ }
2772
+ return Object.keys(doc.links ?? {}).length;
2773
+ }
2774
+ getAllLinkedDocuments() {
2775
+ const doc = this.docSync();
2776
+ invariant10(doc, void 0, {
2777
+ F: __dxlog_file13,
2778
+ L: 74,
2779
+ S: this,
2780
+ A: [
2781
+ "doc",
2782
+ ""
2783
+ ]
2784
+ });
2785
+ return Object.values(doc.links ?? {});
2786
+ }
2787
+ measureMetrics() {
2788
+ const doc = this.docSync();
2789
+ if (!doc) {
2790
+ return null;
2791
+ }
2792
+ return measureDocMetrics(doc);
2793
+ }
2794
+ };
2795
+
2796
+ // packages/core/echo/echo-pipeline/src/db-host/space-state-manager.ts
2797
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/space-state-manager.ts";
2798
+ var SpaceStateManager = class extends Resource8 {
2799
+ constructor() {
2800
+ super(...arguments);
2801
+ this._roots = /* @__PURE__ */ new Map();
2802
+ this._rootBySpace = /* @__PURE__ */ new Map();
2803
+ this._perRootContext = /* @__PURE__ */ new Map();
2804
+ this._lastSpaceDocumentList = /* @__PURE__ */ new Map();
2805
+ this.spaceDocumentListUpdated = new Event3();
2806
+ }
2807
+ async _close(ctx) {
2808
+ for (const [_, rootCtx] of this._perRootContext) {
2809
+ await rootCtx.dispose();
2810
+ }
2811
+ this._roots.clear();
2812
+ }
2813
+ get roots() {
2814
+ return this._roots;
2815
+ }
2816
+ getRootByDocumentId(documentId) {
2817
+ return this._roots.get(documentId);
2818
+ }
2819
+ async assignRootToSpace(spaceId, handle) {
2820
+ let root;
2821
+ if (this._roots.has(handle.documentId)) {
2822
+ root = this._roots.get(handle.documentId);
2823
+ } else {
2824
+ root = new DatabaseRoot(handle);
2825
+ this._roots.set(handle.documentId, root);
2826
+ }
2827
+ if (this._rootBySpace.get(spaceId) === root.handle.documentId) {
2828
+ return root;
2829
+ }
2830
+ const prevRootId = this._rootBySpace.get(spaceId);
2831
+ if (prevRootId) {
2832
+ void this._perRootContext.get(prevRootId)?.dispose();
2833
+ this._perRootContext.delete(prevRootId);
2834
+ }
2835
+ this._rootBySpace.set(spaceId, root.handle.documentId);
2836
+ const ctx = new Context5(void 0, {
2837
+ F: __dxlog_file14,
2838
+ L: 58
2839
+ });
2840
+ this._perRootContext.set(root.handle.documentId, ctx);
2841
+ await root.handle.whenReady();
2842
+ const documentListCheckScheduler = new UpdateScheduler3(ctx, async () => {
2843
+ const documentIds = [
2844
+ root.documentId,
2845
+ ...root.getAllLinkedDocuments().map((url) => interpretAsDocumentId(url))
2846
+ ];
2847
+ if (!isEqual(documentIds, this._lastSpaceDocumentList.get(spaceId))) {
2848
+ this._lastSpaceDocumentList.set(spaceId, documentIds);
2849
+ this.spaceDocumentListUpdated.emit(new SpaceDocumentListUpdatedEvent(spaceId, documentIds));
2850
+ }
2851
+ }, {
2852
+ maxFrequency: 50
2853
+ });
2854
+ const triggerCheckOnChange = () => documentListCheckScheduler.trigger();
2855
+ root.handle.addListener("change", triggerCheckOnChange);
2856
+ ctx.onDispose(() => root.handle.removeListener("change", triggerCheckOnChange));
2857
+ documentListCheckScheduler.trigger();
2858
+ return root;
2859
+ }
2860
+ };
2861
+ var SpaceDocumentListUpdatedEvent = class {
2862
+ constructor(spaceId, documentIds) {
2863
+ this.spaceId = spaceId;
2864
+ this.documentIds = documentIds;
2865
+ }
2866
+ };
2867
+
2868
+ // packages/core/echo/echo-pipeline/src/db-host/echo-host.ts
2869
+ var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/echo-host.ts";
2870
+ var INDEXER_CONFIG = {
2871
+ enabled: true,
2872
+ indexes: [
2873
+ {
2874
+ kind: IndexKind.Kind.SCHEMA_MATCH
2875
+ }
2876
+ ]
2877
+ };
2878
+ var EchoHost = class extends Resource9 {
2879
+ constructor({ kv }) {
2880
+ super();
2881
+ this._spaceStateManager = new SpaceStateManager();
2882
+ this._indexMetadataStore = new IndexMetadataStore({
2883
+ db: kv.sublevel("index-metadata")
2884
+ });
2885
+ this._echoDataMonitor = new EchoDataMonitor();
2886
+ this._automergeHost = new AutomergeHost({
2887
+ db: kv,
2888
+ dataMonitor: this._echoDataMonitor,
2889
+ indexMetadataStore: this._indexMetadataStore
2890
+ });
2891
+ this._indexer = new Indexer({
2892
+ db: kv,
2893
+ indexStore: new IndexStore({
2894
+ db: kv.sublevel("index-storage")
2895
+ }),
2896
+ metadataStore: this._indexMetadataStore,
2897
+ loadDocuments: createSelectedDocumentsIterator(this._automergeHost),
2898
+ indexCooldownTime: process.env.NODE_ENV === "test" ? 0 : void 0
2899
+ });
2900
+ this._indexer.setConfig(INDEXER_CONFIG);
2901
+ this._queryService = new QueryServiceImpl({
2902
+ automergeHost: this._automergeHost,
2903
+ indexer: this._indexer
2904
+ });
2905
+ this._dataService = new DataServiceImpl({
2906
+ automergeHost: this._automergeHost,
2907
+ updateIndexes: async () => {
2908
+ await this._indexer.updateIndexes();
2909
+ }
2910
+ });
2911
+ trace5.diagnostic({
2912
+ id: "echo-stats",
2913
+ name: "Echo Stats",
2914
+ fetch: async () => {
2915
+ return {
2916
+ dataStats: this._echoDataMonitor.computeStats(),
2917
+ loadedDocsCount: this._automergeHost.loadedDocsCount
2918
+ };
2919
+ }
2920
+ });
2921
+ trace5.diagnostic({
2922
+ id: "database-roots",
2923
+ name: "Database Roots",
2924
+ fetch: async () => {
2925
+ return Array.from(this._spaceStateManager.roots.values()).map((root) => ({
2926
+ url: root.url,
2927
+ isLoaded: root.isLoaded,
2928
+ spaceKey: root.getSpaceKey(),
2929
+ inlineObjects: root.getInlineObjectCount(),
2930
+ linkedObjects: root.getLinkedObjectCount()
2931
+ }));
2932
+ }
2933
+ });
2934
+ trace5.diagnostic({
2935
+ id: "database-root-metrics",
2936
+ name: "Database Roots (with metrics)",
2937
+ fetch: async () => {
2938
+ return Array.from(this._spaceStateManager.roots.values()).map((root) => ({
2939
+ url: root.url,
2940
+ isLoaded: root.isLoaded,
2941
+ spaceKey: root.getSpaceKey(),
2942
+ inlineObjects: root.getInlineObjectCount(),
2943
+ linkedObjects: root.getLinkedObjectCount(),
2944
+ ...root.measureMetrics() ?? {}
2945
+ }));
2946
+ }
2947
+ });
2948
+ }
2949
+ get queryService() {
2950
+ return this._queryService;
2951
+ }
2952
+ get dataService() {
2953
+ return this._dataService;
2954
+ }
2955
+ /**
2956
+ * @deprecated To be abstracted away.
2957
+ */
2958
+ get automergeRepo() {
2959
+ return this._automergeHost.repo;
2960
+ }
2961
+ get roots() {
2962
+ return this._spaceStateManager.roots;
2963
+ }
2964
+ async _open(ctx) {
2965
+ await this._automergeHost.open();
2966
+ await this._indexer.open(ctx);
2967
+ await this._queryService.open(ctx);
2968
+ await this._spaceStateManager.open(ctx);
2969
+ this._spaceStateManager.spaceDocumentListUpdated.on(this._ctx, (e) => {
2970
+ void this._automergeHost.updateLocalCollectionState(deriveCollectionIdFromSpaceId(e.spaceId), e.documentIds);
2971
+ });
2972
+ }
2973
+ async _close(ctx) {
2974
+ await this._spaceStateManager.close();
2975
+ await this._queryService.close(ctx);
2976
+ await this._indexer.close(ctx);
2977
+ await this._automergeHost.close();
2978
+ }
2979
+ /**
2980
+ * Flush all pending writes to the underlying storage.
2981
+ */
2982
+ async flush() {
2983
+ await this._automergeHost.repo.flush();
2984
+ }
2985
+ /**
2986
+ * Perform any pending index updates.
2987
+ */
2988
+ async updateIndexes() {
2989
+ await this._indexer.updateIndexes();
2990
+ }
2991
+ /**
2992
+ * Loads the document handle from the repo and waits for it to be ready.
2993
+ */
2994
+ async loadDoc(ctx, documentId, opts) {
2995
+ return await this._automergeHost.loadDoc(ctx, documentId, opts);
2996
+ }
2997
+ /**
2998
+ * Create new persisted document.
2999
+ */
3000
+ createDoc(initialValue, opts) {
3001
+ return this._automergeHost.createDoc(initialValue, opts);
3002
+ }
3003
+ /**
3004
+ * Create new space root.
3005
+ */
3006
+ async createSpaceRoot(spaceKey) {
3007
+ invariant11(this._lifecycleState === LifecycleState4.OPEN, void 0, {
3008
+ F: __dxlog_file15,
3009
+ L: 206,
3010
+ S: this,
3011
+ A: [
3012
+ "this._lifecycleState === LifecycleState.OPEN",
3013
+ ""
3014
+ ]
3015
+ });
3016
+ const spaceId = await createIdFromSpaceKey3(spaceKey);
3017
+ const automergeRoot = this._automergeHost.createDoc({
3018
+ version: SpaceDocVersion3.CURRENT,
3019
+ access: {
3020
+ spaceKey: spaceKey.toHex()
3021
+ }
3022
+ });
3023
+ await this._automergeHost.flush({
3024
+ documentIds: [
3025
+ automergeRoot.documentId
3026
+ ]
3027
+ });
3028
+ return await this.openSpaceRoot(spaceId, automergeRoot.url);
3029
+ }
3030
+ // TODO(dmaretskyi): Change to document id.
3031
+ async openSpaceRoot(spaceId, automergeUrl) {
3032
+ invariant11(this._lifecycleState === LifecycleState4.OPEN, void 0, {
3033
+ F: __dxlog_file15,
3034
+ L: 221,
3035
+ S: this,
3036
+ A: [
3037
+ "this._lifecycleState === LifecycleState.OPEN",
3038
+ ""
3039
+ ]
3040
+ });
3041
+ const handle = this._automergeHost.repo.find(automergeUrl);
3042
+ return this._spaceStateManager.assignRootToSpace(spaceId, handle);
3043
+ }
3044
+ // TODO(dmaretskyi): Change to document id.
3045
+ async closeSpaceRoot(automergeUrl) {
3046
+ todo();
3047
+ }
3048
+ /**
3049
+ * Install data replicator.
3050
+ */
3051
+ async addReplicator(replicator) {
3052
+ await this._automergeHost.addReplicator(replicator);
3053
+ }
3054
+ /**
3055
+ * Remove data replicator.
3056
+ */
3057
+ async removeReplicator(replicator) {
3058
+ await this._automergeHost.removeReplicator(replicator);
3059
+ }
3060
+ async getSpaceSyncState(spaceId) {
3061
+ const collectionId = deriveCollectionIdFromSpaceId(spaceId);
3062
+ return this._automergeHost.getCollectionSyncState(collectionId);
3063
+ }
3064
+ };
3065
+
3066
+ // packages/core/echo/echo-pipeline/src/db-host/migration.ts
3067
+ import { convertLegacyReference } from "@dxos/echo-protocol";
3068
+ import { decodeReference, encodeReference, isLegacyReference, LEGACY_TYPE_PROPERTIES, Reference, SpaceDocVersion as SpaceDocVersion4 } from "@dxos/echo-protocol";
3069
+ import { TYPE_PROPERTIES } from "@dxos/echo-schema";
3070
+ import { deepMapValuesAsync } from "@dxos/util";
3071
+ var convertLegacyReferences = async (doc) => {
3072
+ const newDoc = await deepMapValuesAsync(doc, async (value, recurse) => {
3073
+ if (isLegacyReference(value)) {
3074
+ return convertLegacyReference(value);
3075
+ }
3076
+ return recurse(value);
3077
+ });
3078
+ newDoc.version = SpaceDocVersion4.CURRENT;
3079
+ return newDoc;
3080
+ };
3081
+ var convertLegacySpaceRootDoc = async (root) => {
3082
+ const newDoc = await convertLegacyReferences(root);
3083
+ const properties = findInlineObjectOfType(newDoc, LEGACY_TYPE_PROPERTIES);
3084
+ if (properties) {
3085
+ const [_, obj] = properties;
3086
+ obj.system.type = encodeReference(Reference.fromLegacyTypename(TYPE_PROPERTIES));
3087
+ }
3088
+ return newDoc;
3089
+ };
3090
+ var findInlineObjectOfType = (spaceDoc, typename) => {
3091
+ for (const id in spaceDoc.objects ?? {}) {
3092
+ const obj = spaceDoc.objects[id];
3093
+ if (obj.system.type && decodeReference(obj.system.type).objectId === typename) {
3094
+ return [
3095
+ id,
3096
+ obj
3097
+ ];
3098
+ }
3099
+ }
3100
+ return void 0;
3101
+ };
3102
+
3103
+ // packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts
3104
+ import { Mutex, Trigger as Trigger2 } from "@dxos/async";
3105
+ import * as A5 from "@dxos/automerge/automerge";
3106
+ import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
3107
+ import { Context as Context6, Resource as Resource10 } from "@dxos/context";
3108
+ import { randomUUID } from "@dxos/crypto";
3109
+ import { invariant as invariant12 } from "@dxos/invariant";
3110
+ import { log as log11 } from "@dxos/log";
3111
+ import { EdgeService } from "@dxos/protocols";
3112
+ import { buf } from "@dxos/protocols/buf";
3113
+ import { MessageSchema as RouterMessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
3114
+ import { bufferToArray } from "@dxos/util";
3115
+ function _using_ctx() {
3116
+ var _disposeSuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed) {
3117
+ var err = new Error();
3118
+ err.name = "SuppressedError";
3119
+ err.suppressed = suppressed;
3120
+ err.error = error;
3121
+ return err;
3122
+ }, empty = {}, stack = [];
3123
+ function using(isAwait, value) {
3124
+ if (value != null) {
3125
+ if (Object(value) !== value) {
3126
+ throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
3127
+ }
3128
+ if (isAwait) {
3129
+ var dispose = value[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")];
3130
+ }
3131
+ if (dispose == null) {
3132
+ dispose = value[Symbol.dispose || Symbol.for("Symbol.dispose")];
3133
+ }
3134
+ if (typeof dispose !== "function") {
3135
+ throw new TypeError(`Property [Symbol.dispose] is not a function.`);
3136
+ }
3137
+ stack.push({
3138
+ v: value,
3139
+ d: dispose,
3140
+ a: isAwait
3141
+ });
3142
+ } else if (isAwait) {
3143
+ stack.push({
3144
+ d: value,
3145
+ a: isAwait
3146
+ });
3147
+ }
3148
+ return value;
3149
+ }
3150
+ return {
3151
+ e: empty,
3152
+ u: using.bind(null, false),
3153
+ a: using.bind(null, true),
3154
+ d: function() {
3155
+ var error = this.e;
3156
+ function next() {
3157
+ while (resource = stack.pop()) {
3158
+ try {
3159
+ var resource, disposalResult = resource.d && resource.d.call(resource.v);
3160
+ if (resource.a) {
3161
+ return Promise.resolve(disposalResult).then(next, err);
3162
+ }
3163
+ } catch (e) {
3164
+ return err(e);
3165
+ }
3166
+ }
3167
+ if (error !== empty) throw error;
3168
+ }
3169
+ function err(e) {
3170
+ error = error !== empty ? new _disposeSuppressedError(error, e) : e;
3171
+ return next();
3172
+ }
3173
+ return next();
3174
+ }
3175
+ };
3176
+ }
3177
+ var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts";
3178
+ var EchoEdgeReplicator = class {
3179
+ constructor({ edgeConnection, disableSharePolicy }) {
3180
+ this._mutex = new Mutex();
3181
+ this._ctx = void 0;
3182
+ this._context = null;
3183
+ this._connectedSpaces = /* @__PURE__ */ new Set();
3184
+ this._connections = /* @__PURE__ */ new Map();
3185
+ this._sharePolicyEnabled = true;
3186
+ this._edgeConnection = edgeConnection;
3187
+ this._sharePolicyEnabled = !disableSharePolicy;
3188
+ }
3189
+ async connect(context) {
3190
+ log11.info("connect", {
3191
+ peerId: context.peerId
3192
+ }, {
3193
+ F: __dxlog_file16,
3194
+ L: 53,
3195
+ S: this,
3196
+ C: (f, a) => f(...a)
3197
+ });
3198
+ this._context = context;
3199
+ this._ctx = Context6.default(void 0, {
3200
+ F: __dxlog_file16,
3201
+ L: 56
3202
+ });
3203
+ this._edgeConnection.reconnect.on(this._ctx, async () => {
3204
+ try {
3205
+ var _usingCtx = _using_ctx();
3206
+ const _guard = _usingCtx.u(await this._mutex.acquire());
3207
+ const spaces = [
3208
+ ...this._connectedSpaces
3209
+ ];
3210
+ for (const connection of this._connections.values()) {
3211
+ await connection.close();
3212
+ }
3213
+ this._connections.clear();
3214
+ if (this._context !== null) {
3215
+ for (const spaceId of spaces) {
3216
+ await this._openConnection(spaceId);
3217
+ }
3218
+ }
3219
+ } catch (_) {
3220
+ _usingCtx.e = _;
3221
+ } finally {
3222
+ _usingCtx.d();
3223
+ }
3224
+ });
3225
+ for (const spaceId of this._connectedSpaces) {
3226
+ await this._openConnection(spaceId);
3227
+ }
3228
+ }
3229
+ async disconnect() {
3230
+ try {
3231
+ var _usingCtx = _using_ctx();
3232
+ const _guard = _usingCtx.u(await this._mutex.acquire());
3233
+ for (const connection of this._connections.values()) {
3234
+ await connection.close();
3235
+ }
3236
+ this._connections.clear();
3237
+ } catch (_) {
3238
+ _usingCtx.e = _;
3239
+ } finally {
3240
+ _usingCtx.d();
3241
+ }
3242
+ }
3243
+ async connectToSpace(spaceId) {
3244
+ try {
3245
+ var _usingCtx = _using_ctx();
3246
+ const _guard = _usingCtx.u(await this._mutex.acquire());
3247
+ this._connectedSpaces.add(spaceId);
3248
+ if (this._context !== null) {
3249
+ await this._openConnection(spaceId);
3250
+ }
3251
+ } catch (_) {
3252
+ _usingCtx.e = _;
3253
+ } finally {
3254
+ _usingCtx.d();
3255
+ }
3256
+ }
3257
+ async disconnectFromSpace(spaceId) {
3258
+ try {
3259
+ var _usingCtx = _using_ctx();
3260
+ const _guard = _usingCtx.u(await this._mutex.acquire());
3261
+ this._connectedSpaces.delete(spaceId);
3262
+ const connection = this._connections.get(spaceId);
3263
+ if (connection) {
3264
+ await connection.close();
3265
+ this._connections.delete(spaceId);
3266
+ }
3267
+ } catch (_) {
3268
+ _usingCtx.e = _;
3269
+ } finally {
3270
+ _usingCtx.d();
3271
+ }
3272
+ }
3273
+ async _openConnection(spaceId) {
3274
+ invariant12(this._context, void 0, {
3275
+ F: __dxlog_file16,
3276
+ L: 111,
3277
+ S: this,
3278
+ A: [
3279
+ "this._context",
3280
+ ""
3281
+ ]
3282
+ });
3283
+ const connection = new EdgeReplicatorConnection({
3284
+ edgeConnection: this._edgeConnection,
3285
+ ownPeerId: this._context.peerId,
3286
+ spaceId,
3287
+ context: this._context,
3288
+ sharedPolicyEnabled: this._sharePolicyEnabled,
3289
+ onRemoteConnected: async () => {
3290
+ this._context?.onConnectionOpen(connection);
3291
+ },
3292
+ onRemoteDisconnected: async () => {
3293
+ this._context?.onConnectionClosed(connection);
3294
+ }
3295
+ });
3296
+ this._connections.set(spaceId, connection);
3297
+ await connection.open();
3298
+ }
3299
+ };
3300
+ var EdgeReplicatorConnection = class extends Resource10 {
3301
+ constructor({ edgeConnection, ownPeerId, spaceId, context, sharedPolicyEnabled, onRemoteConnected, onRemoteDisconnected }) {
3302
+ super();
3303
+ this._remotePeerId = null;
3304
+ this._streamStarted = new Trigger2();
3305
+ this._edgeConnection = edgeConnection;
3306
+ this._ownPeerId = ownPeerId;
3307
+ this._spaceId = spaceId;
3308
+ this._context = context;
3309
+ this._remotePeerId = `${EdgeService.AUTOMERGE_REPLICATOR}:${spaceId}-${randomUUID()}`;
3310
+ this._targetServiceId = `${EdgeService.AUTOMERGE_REPLICATOR}:${spaceId}`;
3311
+ this._sharedPolicyEnabled = sharedPolicyEnabled;
3312
+ this._onRemoteConnected = onRemoteConnected;
3313
+ this._onRemoteDisconnected = onRemoteDisconnected;
3314
+ this.readable = new ReadableStream({
3315
+ start: (controller) => {
3316
+ this._readableStreamController = controller;
3317
+ this._ctx.onDispose(() => controller.close());
3318
+ this._streamStarted.wake();
3319
+ }
3320
+ });
3321
+ this.writable = new WritableStream({
3322
+ write: async (message, controller) => {
3323
+ await this._sendMessage(message);
3324
+ }
3325
+ });
3326
+ }
3327
+ async _open(ctx) {
3328
+ this._ctx.onDispose(this._edgeConnection.addListener((msg) => {
3329
+ this._onMessage(msg);
3330
+ }));
3331
+ await this._onRemoteConnected();
3332
+ }
3333
+ async _close() {
3334
+ await this._onRemoteDisconnected();
3335
+ }
3336
+ get peerId() {
3337
+ invariant12(this._remotePeerId, "Not connected", {
3338
+ F: __dxlog_file16,
3339
+ L: 214,
3340
+ S: this,
3341
+ A: [
3342
+ "this._remotePeerId",
3343
+ "'Not connected'"
3344
+ ]
3345
+ });
3346
+ return this._remotePeerId;
3347
+ }
3348
+ async shouldAdvertise(params) {
3349
+ if (!this._sharedPolicyEnabled) {
3350
+ return true;
3351
+ }
3352
+ const spaceId = await this._context.getContainingSpaceIdForDocument(params.documentId);
3353
+ if (!spaceId) {
3354
+ return true;
3355
+ }
3356
+ return spaceId === this._spaceId;
3357
+ }
3358
+ shouldSyncCollection(params) {
3359
+ if (!this._sharedPolicyEnabled) {
3360
+ return true;
3361
+ }
3362
+ const spaceId = getSpaceIdFromCollectionId(params.collectionId);
3363
+ return spaceId === this._spaceId;
3364
+ }
3365
+ _onMessage(message) {
3366
+ if (message.serviceId !== this._targetServiceId) {
3367
+ return;
3368
+ }
3369
+ const payload = cbor2.decode(message.payload.value);
3370
+ log11("recv", () => {
3371
+ const decodedData = payload.type === "sync" && payload.data ? A5.decodeSyncMessage(payload.data) : payload.type === "collection-state" ? payload.state : payload;
3372
+ return {
3373
+ from: message.serviceId,
3374
+ type: payload.type,
3375
+ decodedData
3376
+ };
3377
+ }, {
3378
+ F: __dxlog_file16,
3379
+ L: 245,
3380
+ S: this,
3381
+ C: (f, a) => f(...a)
3382
+ });
3383
+ payload.senderId = this._remotePeerId;
3384
+ this._processMessage(payload);
3385
+ }
3386
+ _processMessage(message) {
3387
+ this._readableStreamController.enqueue(message);
3388
+ }
3389
+ async _sendMessage(message) {
3390
+ message.targetId = this._targetServiceId;
3391
+ log11("send", {
3392
+ type: message.type,
3393
+ senderId: message.senderId,
3394
+ targetId: message.targetId,
3395
+ documentId: message.documentId
3396
+ }, {
3397
+ F: __dxlog_file16,
3398
+ L: 267,
3399
+ S: this,
3400
+ C: (f, a) => f(...a)
3401
+ });
3402
+ const encoded = cbor2.encode(message);
3403
+ await this._edgeConnection.send(buf.create(RouterMessageSchema, {
3404
+ serviceId: this._targetServiceId,
3405
+ source: {
3406
+ identityKey: this._edgeConnection.identityKey,
3407
+ peerKey: this._edgeConnection.peerKey
3408
+ },
3409
+ payload: {
3410
+ value: bufferToArray(encoded)
3411
+ }
3412
+ }));
3413
+ }
3414
+ };
3415
+ export {
3416
+ AuthExtension,
3417
+ AuthStatus,
2144
3418
  AutomergeHost,
2145
- getSpaceKeyFromDoc,
3419
+ CredentialRetrieverExtension,
3420
+ CredentialServerExtension,
3421
+ DataServiceImpl,
3422
+ DatabaseRoot,
3423
+ DocumentsSynchronizer,
3424
+ EchoDataMonitor,
3425
+ EchoEdgeReplicator,
3426
+ EchoHost,
3427
+ LevelDBStorageAdapter,
3428
+ MOCK_AUTH_PROVIDER,
3429
+ MOCK_AUTH_VERIFIER,
3430
+ MeshEchoReplicator,
3431
+ MetadataStore,
3432
+ Pipeline,
3433
+ QueryServiceImpl,
3434
+ QueryState,
3435
+ Space,
3436
+ SpaceManager,
3437
+ SpaceProtocol,
3438
+ SpaceProtocolSession,
3439
+ TimeframeClock,
3440
+ codec,
3441
+ convertLegacyReferences,
3442
+ convertLegacySpaceRootDoc,
3443
+ createIdFromSpaceKey,
3444
+ createMappedFeedWriter,
2146
3445
  deriveCollectionIdFromSpaceId,
3446
+ diffCollectionState,
3447
+ encodingOptions,
3448
+ findInlineObjectOfType,
2147
3449
  getSpaceIdFromCollectionId,
2148
- MeshEchoReplicator,
2149
- EchoDataMonitor,
2150
- DataServiceImpl
3450
+ getSpaceKeyFromDoc,
3451
+ hasInvitationExpired,
3452
+ mapFeedIndexesToTimeframe,
3453
+ mapTimeframeToFeedIndexes,
3454
+ startAfter,
3455
+ valueEncoding
2151
3456
  };
2152
- //# sourceMappingURL=chunk-MPWFDDQK.mjs.map
3457
+ //# sourceMappingURL=index.mjs.map