@dxos/echo-pipeline 0.6.13-main.ed424a1 → 0.6.13

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