@dxos/echo-pipeline 0.6.3-main.9e4e207 → 0.6.3-next.2f65b78

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 (62) hide show
  1. package/dist/lib/browser/{chunk-PEE7MYCZ.mjs → chunk-UJQ5VS5V.mjs} +1790 -3268
  2. package/dist/lib/browser/chunk-UJQ5VS5V.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1049 -12
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +2 -224
  7. package/dist/lib/browser/testing/index.mjs.map +4 -4
  8. package/dist/lib/node/{chunk-RLTLWNAY.cjs → chunk-RH6TDRML.cjs} +783 -2233
  9. package/dist/lib/node/chunk-RH6TDRML.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +1056 -37
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +13 -238
  14. package/dist/lib/node/testing/index.cjs.map +4 -4
  15. package/dist/types/src/automerge/automerge-host.d.ts +2 -29
  16. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
  17. package/dist/types/src/automerge/echo-network-adapter.d.ts +2 -9
  18. package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
  19. package/dist/types/src/automerge/echo-replicator.d.ts +0 -7
  20. package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
  21. package/dist/types/src/automerge/heads-store.d.ts +1 -1
  22. package/dist/types/src/automerge/heads-store.d.ts.map +1 -1
  23. package/dist/types/src/automerge/index.d.ts +0 -2
  24. package/dist/types/src/automerge/index.d.ts.map +1 -1
  25. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +1 -3
  26. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -1
  27. package/dist/types/src/automerge/mesh-echo-replicator.d.ts +2 -2
  28. package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
  29. package/dist/types/src/db-host/data-service.d.ts +1 -2
  30. package/dist/types/src/db-host/data-service.d.ts.map +1 -1
  31. package/dist/types/src/testing/index.d.ts +0 -1
  32. package/dist/types/src/testing/index.d.ts.map +1 -1
  33. package/package.json +33 -33
  34. package/src/automerge/automerge-host.test.ts +14 -76
  35. package/src/automerge/automerge-host.ts +30 -217
  36. package/src/automerge/automerge-repo.test.ts +1 -2
  37. package/src/automerge/echo-network-adapter.test.ts +1 -5
  38. package/src/automerge/echo-network-adapter.ts +4 -69
  39. package/src/automerge/echo-replicator.ts +0 -9
  40. package/src/automerge/heads-store.ts +9 -6
  41. package/src/automerge/index.ts +0 -2
  42. package/src/automerge/mesh-echo-replicator-connection.ts +1 -6
  43. package/src/automerge/mesh-echo-replicator.ts +7 -28
  44. package/src/db-host/data-service.ts +12 -26
  45. package/src/testing/index.ts +0 -1
  46. package/dist/lib/browser/chunk-PEE7MYCZ.mjs.map +0 -7
  47. package/dist/lib/node/chunk-RLTLWNAY.cjs.map +0 -7
  48. package/dist/types/src/automerge/collection-synchronizer.d.ts +0 -61
  49. package/dist/types/src/automerge/collection-synchronizer.d.ts.map +0 -1
  50. package/dist/types/src/automerge/collection-synchronizer.test.d.ts +0 -2
  51. package/dist/types/src/automerge/collection-synchronizer.test.d.ts.map +0 -1
  52. package/dist/types/src/automerge/network-protocol.d.ts +0 -31
  53. package/dist/types/src/automerge/network-protocol.d.ts.map +0 -1
  54. package/dist/types/src/automerge/space-collection.d.ts +0 -4
  55. package/dist/types/src/automerge/space-collection.d.ts.map +0 -1
  56. package/dist/types/src/testing/test-replicator.d.ts +0 -46
  57. package/dist/types/src/testing/test-replicator.d.ts.map +0 -1
  58. package/src/automerge/collection-synchronizer.test.ts +0 -91
  59. package/src/automerge/collection-synchronizer.ts +0 -204
  60. package/src/automerge/network-protocol.ts +0 -45
  61. package/src/automerge/space-collection.ts +0 -14
  62. package/src/testing/test-replicator.ts +0 -194
@@ -26,19 +26,16 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var chunk_RLTLWNAY_exports = {};
30
- __export(chunk_RLTLWNAY_exports, {
29
+ var chunk_RH6TDRML_exports = {};
30
+ __export(chunk_RH6TDRML_exports, {
31
31
  AuthExtension: () => AuthExtension,
32
32
  AuthStatus: () => AuthStatus,
33
- AutomergeHost: () => AutomergeHost,
34
33
  CredentialRetrieverExtension: () => CredentialRetrieverExtension,
35
34
  CredentialServerExtension: () => CredentialServerExtension,
36
35
  DataServiceImpl: () => DataServiceImpl,
37
36
  DocumentsSynchronizer: () => DocumentsSynchronizer,
38
- LevelDBStorageAdapter: () => LevelDBStorageAdapter,
39
37
  MOCK_AUTH_PROVIDER: () => MOCK_AUTH_PROVIDER,
40
38
  MOCK_AUTH_VERIFIER: () => MOCK_AUTH_VERIFIER,
41
- MeshEchoReplicator: () => MeshEchoReplicator,
42
39
  MetadataStore: () => MetadataStore,
43
40
  Pipeline: () => Pipeline,
44
41
  SnapshotManager: () => SnapshotManager,
@@ -51,18 +48,13 @@ __export(chunk_RLTLWNAY_exports, {
51
48
  codec: () => codec,
52
49
  createIdFromSpaceKey: () => createIdFromSpaceKey,
53
50
  createMappedFeedWriter: () => createMappedFeedWriter,
54
- deriveCollectionIdFromSpaceId: () => deriveCollectionIdFromSpaceId,
55
- diffCollectionState: () => diffCollectionState,
56
- encodingOptions: () => encodingOptions,
57
- getSpaceIdFromCollectionId: () => getSpaceIdFromCollectionId,
58
- getSpaceKeyFromDoc: () => getSpaceKeyFromDoc,
59
51
  hasInvitationExpired: () => hasInvitationExpired,
60
52
  mapFeedIndexesToTimeframe: () => mapFeedIndexesToTimeframe,
61
53
  mapTimeframeToFeedIndexes: () => mapTimeframeToFeedIndexes,
62
54
  startAfter: () => startAfter,
63
55
  valueEncoding: () => valueEncoding
64
56
  });
65
- module.exports = __toCommonJS(chunk_RLTLWNAY_exports);
57
+ module.exports = __toCommonJS(chunk_RH6TDRML_exports);
66
58
  var import_hypercore = require("@dxos/hypercore");
67
59
  var import_protocols = require("@dxos/protocols");
68
60
  var import_invariant = require("@dxos/invariant");
@@ -77,111 +69,77 @@ var import_automerge = require("@dxos/automerge/automerge");
77
69
  var import_context2 = require("@dxos/context");
78
70
  var import_invariant2 = require("@dxos/invariant");
79
71
  var import_log = require("@dxos/log");
72
+ var import_codec_protobuf = require("@dxos/codec-protobuf");
73
+ var import_invariant3 = require("@dxos/invariant");
74
+ var import_log2 = require("@dxos/log");
75
+ var import_crc_32 = __toESM(require("crc-32"));
80
76
  var import_async2 = require("@dxos/async");
81
- var import_automerge2 = require("@dxos/automerge/automerge");
82
77
  var import_context3 = require("@dxos/context");
83
- var import_util = require("@dxos/util");
84
- var import_context4 = require("@dxos/context");
85
- var import_async3 = require("@dxos/async");
86
- var import_automerge3 = require("@dxos/automerge/automerge");
87
- var import_automerge_repo = require("@dxos/automerge/automerge-repo");
88
- var import_context5 = require("@dxos/context");
89
- var import_invariant3 = require("@dxos/invariant");
78
+ var import_invariant4 = require("@dxos/invariant");
90
79
  var import_keys2 = require("@dxos/keys");
91
- var import_log2 = require("@dxos/log");
80
+ var import_log3 = require("@dxos/log");
92
81
  var import_protocols4 = require("@dxos/protocols");
93
- var import_tracing = require("@dxos/tracing");
94
- var import_util2 = require("@dxos/util");
82
+ var import_services = require("@dxos/protocols/proto/dxos/client/services");
83
+ var import_util = require("@dxos/util");
84
+ var import_async3 = require("@dxos/async");
85
+ var import_debug = require("@dxos/debug");
86
+ var import_log4 = require("@dxos/log");
87
+ var import_timeframe = require("@dxos/timeframe");
95
88
  var import_async4 = require("@dxos/async");
96
- var import_automerge_repo2 = require("@dxos/automerge/automerge-repo");
97
- var import_context6 = require("@dxos/context");
98
- var import_invariant4 = require("@dxos/invariant");
99
- var import_log3 = require("@dxos/log");
100
- var import_util3 = require("@dxos/util");
101
- var import_indexing = require("@dxos/indexing");
89
+ var import_context4 = require("@dxos/context");
90
+ var import_debug2 = require("@dxos/debug");
91
+ var import_feed_store = require("@dxos/feed-store");
102
92
  var import_invariant5 = require("@dxos/invariant");
103
93
  var import_keys3 = require("@dxos/keys");
94
+ var import_log5 = require("@dxos/log");
95
+ var import_timeframe2 = require("@dxos/timeframe");
96
+ var import_util2 = require("@dxos/util");
97
+ var import_invariant6 = require("@dxos/invariant");
98
+ var import_log6 = require("@dxos/log");
104
99
  var import_async5 = require("@dxos/async");
105
- var import_context7 = require("@dxos/context");
100
+ var import_context5 = require("@dxos/context");
106
101
  var import_crypto2 = require("@dxos/crypto");
107
- var import_invariant6 = require("@dxos/invariant");
108
- var import_log4 = require("@dxos/log");
102
+ var import_invariant7 = require("@dxos/invariant");
103
+ var import_log7 = require("@dxos/log");
109
104
  var import_protocols5 = require("@dxos/protocols");
110
105
  var import_teleport = require("@dxos/teleport");
111
106
  var import_async6 = require("@dxos/async");
112
- var import_debug = require("@dxos/debug");
113
- var import_log5 = require("@dxos/log");
114
- var import_timeframe = require("@dxos/timeframe");
115
- var import_async7 = require("@dxos/async");
116
- var import_context8 = require("@dxos/context");
117
- var import_debug2 = require("@dxos/debug");
118
- var import_feed_store = require("@dxos/feed-store");
119
- var import_invariant7 = require("@dxos/invariant");
120
- var import_keys4 = require("@dxos/keys");
121
- var import_log6 = require("@dxos/log");
122
- var import_timeframe2 = require("@dxos/timeframe");
123
- var import_util4 = require("@dxos/util");
124
- var import_invariant8 = require("@dxos/invariant");
125
- var import_log7 = require("@dxos/log");
126
- var import_async8 = require("@dxos/async");
127
- var import_context9 = require("@dxos/context");
107
+ var import_context6 = require("@dxos/context");
128
108
  var import_crypto3 = require("@dxos/crypto");
129
- var import_invariant9 = require("@dxos/invariant");
130
- var import_keys5 = require("@dxos/keys");
109
+ var import_invariant8 = require("@dxos/invariant");
110
+ var import_keys4 = require("@dxos/keys");
131
111
  var import_log8 = require("@dxos/log");
132
112
  var import_credentials = require("@dxos/protocols/proto/dxos/halo/credentials");
133
- var import_tracing2 = require("@dxos/tracing");
134
- var import_util5 = require("@dxos/util");
135
- var import_async9 = require("@dxos/async");
136
- var import_context10 = require("@dxos/context");
113
+ var import_tracing = require("@dxos/tracing");
114
+ var import_util3 = require("@dxos/util");
115
+ var import_async7 = require("@dxos/async");
116
+ var import_context7 = require("@dxos/context");
137
117
  var import_credentials2 = require("@dxos/credentials");
138
- var import_keys6 = require("@dxos/keys");
118
+ var import_keys5 = require("@dxos/keys");
139
119
  var import_log9 = require("@dxos/log");
140
120
  var import_credentials3 = require("@dxos/protocols/proto/dxos/halo/credentials");
141
121
  var import_timeframe3 = require("@dxos/timeframe");
142
- var import_tracing3 = require("@dxos/tracing");
143
- var import_util6 = require("@dxos/util");
144
- var import_async10 = require("@dxos/async");
145
- var import_context11 = require("@dxos/context");
122
+ var import_tracing2 = require("@dxos/tracing");
123
+ var import_util4 = require("@dxos/util");
124
+ var import_async8 = require("@dxos/async");
125
+ var import_context8 = require("@dxos/context");
146
126
  var import_protocols6 = require("@dxos/protocols");
147
127
  var import_teleport2 = require("@dxos/teleport");
148
128
  var import_crypto4 = require("@dxos/crypto");
149
- var import_keys7 = require("@dxos/keys");
129
+ var import_keys6 = require("@dxos/keys");
150
130
  var import_log10 = require("@dxos/log");
151
131
  var import_network_manager = require("@dxos/network-manager");
152
132
  var import_teleport3 = require("@dxos/teleport");
153
133
  var import_teleport_extension_object_sync = require("@dxos/teleport-extension-object-sync");
154
134
  var import_teleport_extension_replicator = require("@dxos/teleport-extension-replicator");
155
- var import_tracing4 = require("@dxos/tracing");
156
- var import_util7 = require("@dxos/util");
157
- var import_async11 = require("@dxos/async");
135
+ var import_tracing3 = require("@dxos/tracing");
136
+ var import_util5 = require("@dxos/util");
137
+ var import_async9 = require("@dxos/async");
158
138
  var import_debug3 = require("@dxos/debug");
159
- var import_keys8 = require("@dxos/keys");
139
+ var import_keys7 = require("@dxos/keys");
160
140
  var import_log11 = require("@dxos/log");
161
141
  var import_protocols7 = require("@dxos/protocols");
162
- var import_util8 = require("@dxos/util");
163
- var import_codec_protobuf = require("@dxos/codec-protobuf");
164
- var import_invariant10 = require("@dxos/invariant");
165
- var import_keys9 = require("@dxos/keys");
166
- var import_log12 = require("@dxos/log");
167
- var import_invariant11 = require("@dxos/invariant");
168
- var import_keys10 = require("@dxos/keys");
169
- var import_log13 = require("@dxos/log");
170
- var import_util9 = require("@dxos/util");
171
- var import_automerge_repo3 = require("@dxos/automerge/automerge-repo");
172
- var import_context12 = require("@dxos/context");
173
- var import_invariant12 = require("@dxos/invariant");
174
- var import_log14 = require("@dxos/log");
175
- var import_teleport_extension_automerge_replicator = require("@dxos/teleport-extension-automerge-replicator");
176
- var import_crc_32 = __toESM(require("crc-32"));
177
- var import_async12 = require("@dxos/async");
178
- var import_context13 = require("@dxos/context");
179
- var import_invariant13 = require("@dxos/invariant");
180
- var import_keys11 = require("@dxos/keys");
181
- var import_log15 = require("@dxos/log");
182
- var import_protocols8 = require("@dxos/protocols");
183
- var import_services = require("@dxos/protocols/proto/dxos/client/services");
184
- var import_util10 = require("@dxos/util");
142
+ var import_util6 = require("@dxos/util");
185
143
  var codec = import_protocols.schema.getCodecForType("dxos.echo.feed.FeedMessage");
186
144
  var valueEncoding = (0, import_hypercore.createCodecEncoding)(codec);
187
145
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/common/feeds.ts";
@@ -413,241 +371,92 @@ var DocumentsSynchronizer = class extends import_context2.Resource {
413
371
  });
414
372
  }
415
373
  };
416
- var MIN_QUERY_INTERVAL = 5e3;
417
- var POLL_INTERVAL = 3e4;
418
- var CollectionSynchronizer = class extends import_context3.Resource {
374
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service.ts";
375
+ var DataServiceImpl = class {
419
376
  constructor(params) {
420
- super();
421
- this._perCollectionStates = /* @__PURE__ */ new Map();
422
- this._connectedPeers = /* @__PURE__ */ new Set();
423
- this.remoteStateUpdated = new import_async2.Event();
424
- this._sendCollectionState = params.sendCollectionState;
425
- this._queryCollectionState = params.queryCollectionState;
426
- this._shouldSyncCollection = params.shouldSyncCollection;
377
+ this._subscriptions = /* @__PURE__ */ new Map();
378
+ this._automergeHost = params.automergeHost;
379
+ this._updateIndexes = params.updateIndexes;
427
380
  }
428
- async _open(ctx) {
429
- (0, import_async2.scheduleTaskInterval)(this._ctx, async () => {
430
- for (const collectionId of this._perCollectionStates.keys()) {
431
- this.refreshCollection(collectionId);
432
- await (0, import_async2.asyncReturn)();
433
- }
434
- }, POLL_INTERVAL);
435
- }
436
- getRegisteredCollectionIds() {
437
- return [
438
- ...this._perCollectionStates.keys()
439
- ];
440
- }
441
- getLocalCollectionState(collectionId) {
442
- return this._getPerCollectionState(collectionId).localState;
443
- }
444
- setLocalCollectionState(collectionId, state) {
445
- this._getPerCollectionState(collectionId).localState = state;
446
- queueMicrotask(async () => {
447
- if (!this._ctx.disposed) {
448
- this._refreshInterestedPeers(collectionId);
449
- this.refreshCollection(collectionId);
450
- }
381
+ subscribe(request) {
382
+ return new import_codec_protobuf.Stream(({ next, ready }) => {
383
+ const synchronizer = new DocumentsSynchronizer({
384
+ repo: this._automergeHost.repo,
385
+ sendUpdates: (updates) => next(updates)
386
+ });
387
+ synchronizer.open().then(() => {
388
+ this._subscriptions.set(request.subscriptionId, synchronizer);
389
+ ready();
390
+ }).catch((err) => import_log2.log.catch(err, void 0, {
391
+ F: __dxlog_file3,
392
+ L: 62,
393
+ S: this,
394
+ C: (f, a) => f(...a)
395
+ }));
396
+ return () => synchronizer.close();
451
397
  });
452
398
  }
453
- getRemoteCollectionStates(collectionId) {
454
- return this._getPerCollectionState(collectionId).remoteStates;
455
- }
456
- refreshCollection(collectionId) {
457
- let scheduleAnotherRefresh = false;
458
- const state = this._getPerCollectionState(collectionId);
459
- for (const peerId of this._connectedPeers) {
460
- if (state.interestedPeers.has(peerId)) {
461
- const lastQueried = state.lastQueried.get(peerId) ?? 0;
462
- if (Date.now() - lastQueried > MIN_QUERY_INTERVAL) {
463
- state.lastQueried.set(peerId, Date.now());
464
- this._queryCollectionState(collectionId, peerId);
465
- } else {
466
- scheduleAnotherRefresh = true;
467
- }
468
- }
469
- }
470
- if (scheduleAnotherRefresh) {
471
- (0, import_async2.scheduleTask)(this._ctx, () => this.refreshCollection(collectionId), MIN_QUERY_INTERVAL);
472
- }
473
- }
474
- /**
475
- * Callback when a connection to a peer is established.
476
- */
477
- onConnectionOpen(peerId) {
478
- this._connectedPeers.add(peerId);
479
- queueMicrotask(async () => {
480
- if (this._ctx.disposed) {
481
- return;
482
- }
483
- for (const [collectionId, state] of this._perCollectionStates.entries()) {
484
- if (this._shouldSyncCollection(collectionId, peerId)) {
485
- state.interestedPeers.add(peerId);
486
- state.lastQueried.set(peerId, Date.now());
487
- this._queryCollectionState(collectionId, peerId);
488
- }
489
- }
399
+ async updateSubscription(request) {
400
+ const synchronizer = this._subscriptions.get(request.subscriptionId);
401
+ (0, import_invariant3.invariant)(synchronizer, "Subscription not found", {
402
+ F: __dxlog_file3,
403
+ L: 69,
404
+ S: this,
405
+ A: [
406
+ "synchronizer",
407
+ "'Subscription not found'"
408
+ ]
490
409
  });
491
- }
492
- /**
493
- * Callback when a connection to a peer is closed.
494
- */
495
- onConnectionClosed(peerId) {
496
- this._connectedPeers.delete(peerId);
497
- for (const perCollectionState of this._perCollectionStates.values()) {
498
- perCollectionState.remoteStates.delete(peerId);
499
- }
500
- }
501
- /**
502
- * Callback when a peer queries the state of a collection.
503
- */
504
- onCollectionStateQueried(collectionId, peerId) {
505
- const perCollectionState = this._getPerCollectionState(collectionId);
506
- if (perCollectionState.localState) {
507
- this._sendCollectionState(collectionId, peerId, perCollectionState.localState);
410
+ if (request.addIds?.length) {
411
+ await synchronizer.addDocuments(request.addIds);
508
412
  }
509
- }
510
- /**
511
- * Callback when a peer sends the state of a collection.
512
- */
513
- onRemoteStateReceived(collectionId, peerId, state) {
514
- const perCollectionState = this._getPerCollectionState(collectionId);
515
- perCollectionState.remoteStates.set(peerId, state);
516
- this.remoteStateUpdated.emit({
517
- peerId,
518
- collectionId
519
- });
520
- }
521
- _getPerCollectionState(collectionId) {
522
- return (0, import_util.defaultMap)(this._perCollectionStates, collectionId, () => ({
523
- localState: void 0,
524
- remoteStates: /* @__PURE__ */ new Map(),
525
- interestedPeers: /* @__PURE__ */ new Set(),
526
- lastQueried: /* @__PURE__ */ new Map()
527
- }));
528
- }
529
- _refreshInterestedPeers(collectionId) {
530
- for (const peerId of this._connectedPeers) {
531
- if (this._shouldSyncCollection(collectionId, peerId)) {
532
- this._getPerCollectionState(collectionId).interestedPeers.add(peerId);
533
- } else {
534
- this._getPerCollectionState(collectionId).interestedPeers.delete(peerId);
535
- }
413
+ if (request.removeIds?.length) {
414
+ await synchronizer.removeDocuments(request.removeIds);
536
415
  }
537
416
  }
538
- };
539
- var diffCollectionState = (local, remote) => {
540
- const allDocuments = /* @__PURE__ */ new Set([
541
- ...Object.keys(local.documents),
542
- ...Object.keys(remote.documents)
543
- ]);
544
- const different = [];
545
- for (const documentId of allDocuments) {
546
- if (!local.documents[documentId] || !remote.documents[documentId] || !import_automerge2.next.equals(local.documents[documentId], remote.documents[documentId])) {
547
- different.push(documentId);
417
+ async update(request) {
418
+ if (!request.updates) {
419
+ return;
548
420
  }
421
+ const synchronizer = this._subscriptions.get(request.subscriptionId);
422
+ (0, import_invariant3.invariant)(synchronizer, "Subscription not found", {
423
+ F: __dxlog_file3,
424
+ L: 84,
425
+ S: this,
426
+ A: [
427
+ "synchronizer",
428
+ "'Subscription not found'"
429
+ ]
430
+ });
431
+ synchronizer.update(request.updates);
549
432
  }
550
- return {
551
- different
552
- };
553
- };
554
- var LevelDBStorageAdapter = class extends import_context4.Resource {
555
- constructor(_params) {
556
- super();
557
- this._params = _params;
433
+ async flush(request) {
434
+ await this._automergeHost.flush(request);
558
435
  }
559
- async load(keyArray) {
560
- try {
561
- if (this._lifecycleState !== import_context4.LifecycleState.OPEN) {
562
- return void 0;
563
- }
564
- return await this._params.db.get(keyArray, {
565
- ...encodingOptions
566
- });
567
- } catch (err) {
568
- if (isLevelDbNotFoundError(err)) {
569
- return void 0;
436
+ async getDocumentHeads(request) {
437
+ const entries = await Promise.all(request.documentIds?.map(async (documentId) => {
438
+ const heads = await this._automergeHost.getHeads(documentId);
439
+ return {
440
+ documentId,
441
+ heads
442
+ };
443
+ }) ?? []);
444
+ return {
445
+ heads: {
446
+ entries
570
447
  }
571
- throw err;
572
- }
573
- }
574
- async save(keyArray, binary) {
575
- if (this._lifecycleState !== import_context4.LifecycleState.OPEN) {
576
- return void 0;
577
- }
578
- const batch = this._params.db.batch();
579
- await this._params.callbacks?.beforeSave?.({
580
- path: keyArray,
581
- batch
582
- });
583
- batch.put(keyArray, Buffer.from(binary), {
584
- ...encodingOptions
585
- });
586
- await batch.write();
587
- await this._params.callbacks?.afterSave?.(keyArray);
448
+ };
588
449
  }
589
- async remove(keyArray) {
590
- if (this._lifecycleState !== import_context4.LifecycleState.OPEN) {
591
- return void 0;
592
- }
593
- await this._params.db.del(keyArray, {
594
- ...encodingOptions
595
- });
450
+ async waitUntilHeadsReplicated(request, options) {
451
+ await this._automergeHost.waitUntilHeadsReplicated(request.heads);
596
452
  }
597
- async loadRange(keyPrefix) {
598
- if (this._lifecycleState !== import_context4.LifecycleState.OPEN) {
599
- return [];
600
- }
601
- const result = [];
602
- for await (const [key, value] of this._params.db.iterator({
603
- gte: keyPrefix,
604
- lte: [
605
- ...keyPrefix,
606
- "\uFFFF"
607
- ],
608
- ...encodingOptions
609
- })) {
610
- result.push({
611
- key,
612
- data: value
613
- });
614
- }
615
- return result;
453
+ async reIndexHeads(request, options) {
454
+ await this._automergeHost.reIndexHeads(request.documentIds ?? []);
616
455
  }
617
- async removeRange(keyPrefix) {
618
- if (this._lifecycleState !== import_context4.LifecycleState.OPEN) {
619
- return void 0;
620
- }
621
- const batch = this._params.db.batch();
622
- for await (const [key] of this._params.db.iterator({
623
- gte: keyPrefix,
624
- lte: [
625
- ...keyPrefix,
626
- "\uFFFF"
627
- ],
628
- ...encodingOptions
629
- })) {
630
- batch.del(key, {
631
- ...encodingOptions
632
- });
633
- }
634
- await batch.write();
456
+ async updateIndexes() {
457
+ await this._updateIndexes();
635
458
  }
636
459
  };
637
- var keyEncoder = {
638
- encode: (key) => Buffer.from(key.map((k) => k.replaceAll("%", "%25").replaceAll("-", "%2D")).join("-")),
639
- decode: (key) => Buffer.from(key).toString().split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%")),
640
- format: "buffer"
641
- };
642
- var encodingOptions = {
643
- keyEncoding: keyEncoder,
644
- valueEncoding: "buffer"
645
- };
646
- var isLevelDbNotFoundError = (err) => err.code === "LEVEL_NOT_FOUND";
647
- var MESSAGE_TYPE_COLLECTION_QUERY = "collection-query";
648
- var isCollectionQueryMessage = (message) => message.type === MESSAGE_TYPE_COLLECTION_QUERY;
649
- var MESSAGE_TYPE_COLLECTION_STATE = "collection-state";
650
- var isCollectionStateMessage = (message) => message.type === MESSAGE_TYPE_COLLECTION_STATE;
651
460
  function _ts_decorate(decorators, target, key, desc) {
652
461
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
653
462
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -658,1049 +467,446 @@ function _ts_decorate(decorators, target, key, desc) {
658
467
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
659
468
  return c > 3 && r && Object.defineProperty(target, key, r), r;
660
469
  }
661
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
662
- var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
663
- constructor(_params) {
664
- super();
665
- this._params = _params;
666
- this._replicators = /* @__PURE__ */ new Set();
667
- this._connections = /* @__PURE__ */ new Map();
668
- this._lifecycleState = import_context6.LifecycleState.CLOSED;
669
- this._connected = new import_async4.Trigger();
670
- }
671
- connect(peerId, peerMetadata) {
672
- this.peerId = peerId;
673
- this.peerMetadata = peerMetadata;
674
- this._connected.wake();
675
- }
676
- send(message) {
677
- const connectionEntry = this._connections.get(message.targetId);
678
- if (!connectionEntry) {
679
- throw new Error("Connection not found.");
680
- }
681
- connectionEntry.writer.write(message).catch((err) => {
682
- if (connectionEntry.isOpen) {
683
- import_log3.log.catch(err, void 0, {
684
- F: __dxlog_file3,
685
- L: 63,
686
- S: this,
687
- C: (f, a) => f(...a)
688
- });
689
- }
470
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/metadata/metadata-store.ts";
471
+ var EXPIRED_INVITATION_CLEANUP_INTERVAL = 60 * 60 * 1e3;
472
+ var emptyEchoMetadata = () => ({
473
+ version: import_protocols4.STORAGE_VERSION,
474
+ spaces: [],
475
+ created: /* @__PURE__ */ new Date(),
476
+ updated: /* @__PURE__ */ new Date()
477
+ });
478
+ var emptyLargeSpaceMetadata = () => ({});
479
+ var EchoMetadata = import_protocols4.schema.getCodecForType("dxos.echo.metadata.EchoMetadata");
480
+ var LargeSpaceMetadata = import_protocols4.schema.getCodecForType("dxos.echo.metadata.LargeSpaceMetadata");
481
+ var MetadataStore = class {
482
+ constructor(directory) {
483
+ this._metadata = emptyEchoMetadata();
484
+ this._spaceLargeMetadata = new import_util.ComplexMap(import_keys2.PublicKey.hash);
485
+ this._metadataFile = void 0;
486
+ this.update = new import_async2.Event();
487
+ this._invitationCleanupCtx = new import_context3.Context(void 0, {
488
+ F: __dxlog_file4,
489
+ L: 53
690
490
  });
491
+ this._directory = directory;
691
492
  }
692
- disconnect() {
693
- }
694
- async open() {
695
- if (this._lifecycleState === import_context6.LifecycleState.OPEN) {
696
- return;
697
- }
698
- this._lifecycleState = import_context6.LifecycleState.OPEN;
699
- (0, import_log3.log)("emit ready", void 0, {
700
- F: __dxlog_file3,
701
- L: 79,
702
- S: this,
703
- C: (f, a) => f(...a)
704
- });
705
- this.emit("ready", {
706
- network: this
707
- });
493
+ get metadata() {
494
+ return this._metadata;
708
495
  }
709
- async close() {
710
- if (this._lifecycleState === import_context6.LifecycleState.CLOSED) {
711
- return this;
712
- }
713
- for (const replicator of this._replicators) {
714
- await replicator.disconnect();
715
- }
716
- this._replicators.clear();
717
- this._lifecycleState = import_context6.LifecycleState.CLOSED;
496
+ get version() {
497
+ return this._metadata.version ?? 0;
718
498
  }
719
- async whenConnected() {
720
- await this._connected.wait({
721
- timeout: 1e4
722
- });
499
+ /**
500
+ * Returns a list of currently saved spaces. The list and objects in it can be modified addSpace and
501
+ * addSpaceFeed functions.
502
+ */
503
+ get spaces() {
504
+ return this._metadata.spaces ?? [];
723
505
  }
724
- async addReplicator(replicator) {
725
- (0, import_invariant4.invariant)(this._lifecycleState === import_context6.LifecycleState.OPEN, void 0, {
726
- F: __dxlog_file3,
727
- L: 105,
728
- S: this,
729
- A: [
730
- "this._lifecycleState === LifecycleState.OPEN",
731
- ""
732
- ]
733
- });
734
- (0, import_invariant4.invariant)(this.peerId, void 0, {
735
- F: __dxlog_file3,
736
- L: 106,
737
- S: this,
738
- A: [
739
- "this.peerId",
740
- ""
741
- ]
742
- });
743
- (0, import_invariant4.invariant)(!this._replicators.has(replicator), void 0, {
744
- F: __dxlog_file3,
745
- L: 107,
746
- S: this,
747
- A: [
748
- "!this._replicators.has(replicator)",
749
- ""
750
- ]
751
- });
752
- this._replicators.add(replicator);
753
- await replicator.connect({
754
- peerId: this.peerId,
755
- onConnectionOpen: this._onConnectionOpen.bind(this),
756
- onConnectionClosed: this._onConnectionClosed.bind(this),
757
- onConnectionAuthScopeChanged: this._onConnectionAuthScopeChanged.bind(this),
758
- getContainingSpaceForDocument: this._params.getContainingSpaceForDocument
759
- });
760
- }
761
- async removeReplicator(replicator) {
762
- (0, import_invariant4.invariant)(this._lifecycleState === import_context6.LifecycleState.OPEN, void 0, {
763
- F: __dxlog_file3,
764
- L: 121,
765
- S: this,
766
- A: [
767
- "this._lifecycleState === LifecycleState.OPEN",
768
- ""
769
- ]
770
- });
771
- (0, import_invariant4.invariant)(this._replicators.has(replicator), void 0, {
772
- F: __dxlog_file3,
773
- L: 122,
774
- S: this,
775
- A: [
776
- "this._replicators.has(replicator)",
777
- ""
778
- ]
779
- });
780
- await replicator.disconnect();
781
- this._replicators.delete(replicator);
782
- }
783
- async shouldAdvertise(peerId, params) {
784
- const connection = this._connections.get(peerId);
785
- if (!connection) {
786
- return false;
787
- }
788
- return connection.connection.shouldAdvertise(params);
789
- }
790
- shouldSyncCollection(peerId, params) {
791
- const connection = this._connections.get(peerId);
792
- if (!connection) {
793
- return false;
794
- }
795
- return connection.connection.shouldSyncCollection(params);
796
- }
797
- queryCollectionState(collectionId, targetId) {
798
- const message = {
799
- type: "collection-query",
800
- senderId: this.peerId,
801
- targetId,
802
- collectionId
803
- };
804
- this.send(message);
805
- }
806
- sendCollectionState(collectionId, targetId, state) {
807
- const message = {
808
- type: "collection-state",
809
- senderId: this.peerId,
810
- targetId,
811
- collectionId,
812
- state
813
- };
814
- this.send(message);
815
- }
816
- // TODO(dmaretskyi): Remove.
817
- getPeersInterestedInCollection(collectionId) {
818
- return Array.from(this._connections.values()).map((connection) => {
819
- return connection.connection.shouldSyncCollection({
820
- collectionId
821
- }) ? connection.connection.peerId : null;
822
- }).filter(import_util3.nonNullable);
823
- }
824
- _onConnectionOpen(connection) {
825
- (0, import_log3.log)("Connection opened", {
826
- peerId: connection.peerId
827
- }, {
828
- F: __dxlog_file3,
829
- L: 178,
830
- S: this,
831
- C: (f, a) => f(...a)
832
- });
833
- (0, import_invariant4.invariant)(!this._connections.has(connection.peerId), void 0, {
834
- F: __dxlog_file3,
835
- L: 179,
836
- S: this,
837
- A: [
838
- "!this._connections.has(connection.peerId as PeerId)",
839
- ""
840
- ]
841
- });
842
- const reader = connection.readable.getReader();
843
- const writer = connection.writable.getWriter();
844
- const connectionEntry = {
845
- connection,
846
- reader,
847
- writer,
848
- isOpen: true
849
- };
850
- this._connections.set(connection.peerId, connectionEntry);
851
- queueMicrotask(async () => {
852
- try {
853
- while (true) {
854
- const { done, value } = await reader.read();
855
- if (done) {
856
- break;
857
- }
858
- this._onMessage(value);
859
- }
860
- } catch (err) {
861
- if (connectionEntry.isOpen) {
862
- import_log3.log.catch(err, void 0, {
863
- F: __dxlog_file3,
864
- L: 198,
865
- S: this,
866
- C: (f, a) => f(...a)
867
- });
868
- }
506
+ async _readFile(file, codec2) {
507
+ try {
508
+ const { size: fileLength } = await file.stat();
509
+ if (fileLength < 8) {
510
+ return;
869
511
  }
870
- });
871
- (0, import_log3.log)("emit peer-candidate", {
872
- peerId: connection.peerId
873
- }, {
874
- F: __dxlog_file3,
875
- L: 203,
876
- S: this,
877
- C: (f, a) => f(...a)
878
- });
879
- this._emitPeerCandidate(connection);
880
- }
881
- _onMessage(message) {
882
- if (isCollectionQueryMessage(message)) {
883
- this._params.onCollectionStateQueried(message.collectionId, message.senderId);
884
- } else if (isCollectionStateMessage(message)) {
885
- this._params.onCollectionStateReceived(message.collectionId, message.senderId, message.state);
886
- } else {
887
- this.emit("message", message);
512
+ const dataSize = fromBytesInt32(await file.read(0, 4));
513
+ const checksum = fromBytesInt32(await file.read(4, 4));
514
+ (0, import_log3.log)("loaded", {
515
+ size: dataSize,
516
+ checksum,
517
+ name: file.filename
518
+ }, {
519
+ F: __dxlog_file4,
520
+ L: 89,
521
+ S: this,
522
+ C: (f, a) => f(...a)
523
+ });
524
+ if (fileLength < dataSize + 8) {
525
+ throw new import_protocols4.DataCorruptionError("Metadata size is smaller than expected.", {
526
+ fileLength,
527
+ dataSize
528
+ });
529
+ }
530
+ const data = await file.read(8, dataSize);
531
+ const calculatedChecksum = import_crc_32.default.buf(data);
532
+ if (calculatedChecksum !== checksum) {
533
+ throw new import_protocols4.DataCorruptionError("Metadata checksum is invalid.");
534
+ }
535
+ return codec2.decode(data);
536
+ } finally {
537
+ await file.close();
888
538
  }
889
539
  }
890
540
  /**
891
- * Trigger doc-synchronizer shared documents set recalculation. Happens on peer-candidate.
892
- * TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
541
+ * @internal
893
542
  */
894
- _onConnectionAuthScopeChanged(connection) {
895
- (0, import_log3.log)("Connection auth scope changed", {
896
- peerId: connection.peerId
897
- }, {
898
- F: __dxlog_file3,
899
- L: 222,
900
- S: this,
901
- C: (f, a) => f(...a)
902
- });
903
- const entry = this._connections.get(connection.peerId);
904
- (0, import_invariant4.invariant)(entry, void 0, {
905
- F: __dxlog_file3,
906
- L: 224,
907
- S: this,
908
- A: [
909
- "entry",
910
- ""
911
- ]
912
- });
913
- this.emit("peer-disconnected", {
914
- peerId: connection.peerId
915
- });
916
- this._emitPeerCandidate(connection);
917
- }
918
- _onConnectionClosed(connection) {
919
- (0, import_log3.log)("Connection closed", {
920
- peerId: connection.peerId
543
+ async _writeFile(file, codec2, data) {
544
+ const encoded = (0, import_util.arrayToBuffer)(codec2.encode(data));
545
+ const checksum = import_crc_32.default.buf(encoded);
546
+ const result = Buffer.alloc(8 + encoded.length);
547
+ result.writeInt32LE(encoded.length, 0);
548
+ result.writeInt32LE(checksum, 4);
549
+ encoded.copy(result, 8);
550
+ await file.write(0, result);
551
+ (0, import_log3.log)("saved", {
552
+ size: encoded.length,
553
+ checksum
921
554
  }, {
922
- F: __dxlog_file3,
923
- L: 230,
924
- S: this,
925
- C: (f, a) => f(...a)
926
- });
927
- const entry = this._connections.get(connection.peerId);
928
- (0, import_invariant4.invariant)(entry, void 0, {
929
- F: __dxlog_file3,
930
- L: 232,
931
- S: this,
932
- A: [
933
- "entry",
934
- ""
935
- ]
936
- });
937
- entry.isOpen = false;
938
- this.emit("peer-disconnected", {
939
- peerId: connection.peerId
940
- });
941
- void entry.reader.cancel().catch((err) => import_log3.log.catch(err, void 0, {
942
- F: __dxlog_file3,
943
- L: 237,
944
- S: this,
945
- C: (f, a) => f(...a)
946
- }));
947
- void entry.writer.abort().catch((err) => import_log3.log.catch(err, void 0, {
948
- F: __dxlog_file3,
949
- L: 238,
555
+ F: __dxlog_file4,
556
+ L: 124,
950
557
  S: this,
951
558
  C: (f, a) => f(...a)
952
- }));
953
- this._connections.delete(connection.peerId);
954
- }
955
- _emitPeerCandidate(connection) {
956
- this.emit("peer-candidate", {
957
- peerId: connection.peerId,
958
- peerMetadata: createEchoPeerMetadata()
959
- });
960
- }
961
- };
962
- _ts_decorate([
963
- import_async4.synchronized
964
- ], EchoNetworkAdapter.prototype, "open", null);
965
- _ts_decorate([
966
- import_async4.synchronized
967
- ], EchoNetworkAdapter.prototype, "close", null);
968
- _ts_decorate([
969
- import_async4.synchronized
970
- ], EchoNetworkAdapter.prototype, "addReplicator", null);
971
- _ts_decorate([
972
- import_async4.synchronized
973
- ], EchoNetworkAdapter.prototype, "removeReplicator", null);
974
- var createEchoPeerMetadata = () => ({
975
- // TODO(dmaretskyi): Refactor this.
976
- dxos_peerSource: "EchoNetworkAdapter"
977
- });
978
- var isEchoPeerMetadata = (metadata) => metadata?.dxos_peerSource === "EchoNetworkAdapter";
979
- var HeadsStore = class {
980
- constructor({ db }) {
981
- this._db = db;
982
- }
983
- setHeads(documentId, heads, batch) {
984
- batch.put(documentId, heads, {
985
- sublevel: this._db,
986
- keyEncoding: "utf8",
987
- valueEncoding: import_indexing.headsEncoding
988
- });
989
- }
990
- // TODO(dmaretskyi): Make batched.
991
- async getHeads(documentIds) {
992
- return this._db.getMany(documentIds, {
993
- keyEncoding: "utf8",
994
- valueEncoding: import_indexing.headsEncoding
995
- });
996
- }
997
- };
998
- function _ts_decorate2(decorators, target, key, desc) {
999
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1000
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1001
- r = Reflect.decorate(decorators, target, key, desc);
1002
- else
1003
- for (var i = decorators.length - 1; i >= 0; i--)
1004
- if (d = decorators[i])
1005
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1006
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1007
- }
1008
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
1009
- var AutomergeHost = class extends import_context5.Resource {
1010
- constructor({ db, indexMetadataStore }) {
1011
- super();
1012
- this._echoNetworkAdapter = new EchoNetworkAdapter({
1013
- getContainingSpaceForDocument: this._getContainingSpaceForDocument.bind(this),
1014
- onCollectionStateQueried: this._onCollectionStateQueried.bind(this),
1015
- onCollectionStateReceived: this._onCollectionStateReceived.bind(this)
1016
- });
1017
- this._collectionSynchronizer = new CollectionSynchronizer({
1018
- queryCollectionState: this._queryCollectionState.bind(this),
1019
- sendCollectionState: this._sendCollectionState.bind(this),
1020
- shouldSyncCollection: this._shouldSyncCollection.bind(this)
1021
- });
1022
- this._db = db;
1023
- this._storage = new LevelDBStorageAdapter({
1024
- db: db.sublevel("automerge"),
1025
- callbacks: {
1026
- beforeSave: async (params) => this._beforeSave(params),
1027
- afterSave: async (key) => this._afterSave(key)
1028
- }
1029
- });
1030
- this._headsStore = new HeadsStore({
1031
- db: db.sublevel("heads")
1032
- });
1033
- this._indexMetadataStore = indexMetadataStore;
1034
- }
1035
- async _open() {
1036
- this._peerId = `host-${import_keys2.PublicKey.random().toHex()}`;
1037
- await this._storage.open?.();
1038
- this._repo = new import_automerge_repo.Repo({
1039
- peerId: this._peerId,
1040
- sharePolicy: this._sharePolicy.bind(this),
1041
- storage: this._storage,
1042
- network: [
1043
- // Upstream swarm.
1044
- this._echoNetworkAdapter
1045
- ]
1046
- });
1047
- import_async3.Event.wrap(this._echoNetworkAdapter, "peer-candidate").on(this._ctx, (e) => this._onPeerConnected(e.peerId));
1048
- import_async3.Event.wrap(this._echoNetworkAdapter, "peer-disconnected").on(this._ctx, (e) => this._onPeerDisconnected(e.peerId));
1049
- this._collectionSynchronizer.remoteStateUpdated.on(this._ctx, ({ collectionId, peerId }) => {
1050
- this._onRemoteCollectionStateUpdated(collectionId, peerId);
1051
559
  });
1052
- await this._echoNetworkAdapter.open();
1053
- await this._collectionSynchronizer.open();
1054
- await this._echoNetworkAdapter.open();
1055
- await this._echoNetworkAdapter.whenConnected();
1056
- }
1057
- async _close() {
1058
- await this._collectionSynchronizer.close();
1059
- await this._storage.close?.();
1060
- await this._echoNetworkAdapter.close();
1061
- await this._ctx.dispose();
1062
- }
1063
- /**
1064
- * @deprecated To be abstracted away.
1065
- */
1066
- get repo() {
1067
- return this._repo;
1068
560
  }
1069
- get peerId() {
1070
- return this._peerId;
1071
- }
1072
- get loadedDocsCount() {
1073
- return Object.keys(this._repo.handles).length;
1074
- }
1075
- async addReplicator(replicator) {
1076
- await this._echoNetworkAdapter.addReplicator(replicator);
1077
- }
1078
- async removeReplicator(replicator) {
1079
- await this._echoNetworkAdapter.removeReplicator(replicator);
1080
- }
1081
- /**
1082
- * Loads the document handle from the repo and waits for it to be ready.
1083
- */
1084
- async loadDoc(ctx, documentId, opts) {
1085
- let handle;
1086
- if (typeof documentId === "string") {
1087
- handle = this._repo.handles[documentId];
1088
- }
1089
- if (!handle) {
1090
- handle = this._repo.find(documentId);
1091
- }
1092
- if (!handle.isReady()) {
1093
- if (!opts?.timeout) {
1094
- await (0, import_context5.cancelWithContext)(ctx, handle.whenReady());
1095
- } else {
1096
- await (0, import_context5.cancelWithContext)(ctx, (0, import_async3.asyncTimeout)(handle.whenReady(), opts.timeout));
1097
- }
1098
- }
1099
- return handle;
561
+ async close() {
562
+ await this._invitationCleanupCtx.dispose();
563
+ await this.flush();
564
+ await this._metadataFile?.close();
565
+ this._metadataFile = void 0;
566
+ this._metadata = emptyEchoMetadata();
567
+ this._spaceLargeMetadata.clear();
1100
568
  }
1101
569
  /**
1102
- * Create new persisted document.
570
+ * Loads metadata from persistent storage.
1103
571
  */
1104
- createDoc(initialValue, opts) {
1105
- if (opts?.preserveHistory) {
1106
- if (!(0, import_automerge3.isAutomerge)(initialValue)) {
1107
- throw new TypeError("Initial value must be an Automerge document");
1108
- }
1109
- return this._repo.import((0, import_automerge3.save)(initialValue));
1110
- } else {
1111
- return this._repo.create(initialValue);
1112
- }
1113
- }
1114
- async waitUntilHeadsReplicated(heads) {
1115
- const entries = heads.entries;
1116
- if (!entries?.length) {
1117
- return;
572
+ async load() {
573
+ if (!this._metadataFile || this._metadataFile.closed) {
574
+ this._metadataFile = this._directory.getOrCreateFile("EchoMetadata");
1118
575
  }
1119
- const documentIds = entries.map((entry) => entry.documentId);
1120
- const documentHeads = await this.getHeads(documentIds);
1121
- const headsToWait = entries.filter((entry, index) => {
1122
- const targetHeads = entry.heads;
1123
- if (!targetHeads || targetHeads.length === 0) {
1124
- return false;
576
+ try {
577
+ const metadata = await this._readFile(this._metadataFile, EchoMetadata);
578
+ if (metadata) {
579
+ this._metadata = metadata;
1125
580
  }
1126
- const currentHeads = documentHeads[index];
1127
- return !(currentHeads !== null && (0, import_automerge3.equals)(currentHeads, targetHeads));
1128
- });
1129
- if (headsToWait.length > 0) {
1130
- await Promise.all(headsToWait.map(async (entry, index) => {
1131
- const handle = await this.loadDoc(import_context5.Context.default(void 0, {
1132
- F: __dxlog_file4,
1133
- L: 223
1134
- }), entry.documentId);
1135
- await waitForHeads(handle, entry.heads);
1136
- }));
1137
- }
1138
- await this._repo.flush(heads.entries?.map((entry) => entry.documentId) ?? []);
1139
- }
1140
- async reIndexHeads(documentIds) {
1141
- for (const documentId of documentIds) {
1142
- import_log2.log.info("re-indexing heads for document", {
1143
- documentId
581
+ this._metadata.spaces?.forEach((space) => {
582
+ space.state ??= import_services.SpaceState.ACTIVE;
583
+ });
584
+ } catch (err) {
585
+ import_log3.log.error("failed to load metadata", {
586
+ err
1144
587
  }, {
1145
588
  F: __dxlog_file4,
1146
- L: 235,
589
+ L: 156,
1147
590
  S: this,
1148
591
  C: (f, a) => f(...a)
1149
592
  });
1150
- const handle = this._repo.find(documentId);
1151
- await handle.whenReady([
1152
- "ready",
1153
- "requesting"
1154
- ]);
1155
- if (handle.inState([
1156
- "requesting"
1157
- ])) {
1158
- import_log2.log.warn("document is not available locally, skipping", {
1159
- documentId
593
+ this._metadata = emptyEchoMetadata();
594
+ }
595
+ await (0, import_util.forEachAsync)([
596
+ this._metadata.identity?.haloSpace.key,
597
+ ...this._metadata.spaces?.map((space) => space.key) ?? []
598
+ ].filter(import_util.isNotNullOrUndefined), async (key) => {
599
+ try {
600
+ await this._loadSpaceLargeMetadata(key);
601
+ } catch (err) {
602
+ import_log3.log.error("failed to load space large metadata", {
603
+ err
1160
604
  }, {
1161
605
  F: __dxlog_file4,
1162
- L: 239,
606
+ L: 168,
1163
607
  S: this,
1164
608
  C: (f, a) => f(...a)
1165
609
  });
1166
- continue;
1167
610
  }
1168
- const doc = handle.docSync();
1169
- (0, import_invariant3.invariant)(doc, void 0, {
611
+ });
612
+ (0, import_async2.scheduleTaskInterval)(this._invitationCleanupCtx, async () => {
613
+ for (const invitation of this._metadata.invitations ?? []) {
614
+ if (hasInvitationExpired(invitation) || isLegacyInvitationFormat(invitation)) {
615
+ await this.removeInvitation(invitation.invitationId);
616
+ }
617
+ }
618
+ }, EXPIRED_INVITATION_CLEANUP_INTERVAL);
619
+ }
620
+ async _save() {
621
+ const data = {
622
+ ...this._metadata,
623
+ version: import_protocols4.STORAGE_VERSION,
624
+ created: this._metadata.created ?? /* @__PURE__ */ new Date(),
625
+ updated: /* @__PURE__ */ new Date()
626
+ };
627
+ this.update.emit(data);
628
+ const file = this._directory.getOrCreateFile("EchoMetadata");
629
+ await this._writeFile(file, EchoMetadata, data);
630
+ }
631
+ async _loadSpaceLargeMetadata(key) {
632
+ const file = this._directory.getOrCreateFile(`space_${key.toHex()}_large`);
633
+ try {
634
+ const metadata = await this._readFile(file, LargeSpaceMetadata);
635
+ if (metadata) {
636
+ this._spaceLargeMetadata.set(key, metadata);
637
+ }
638
+ } catch (err) {
639
+ import_log3.log.error("failed to load space large metadata", {
640
+ err
641
+ }, {
1170
642
  F: __dxlog_file4,
1171
- L: 244,
643
+ L: 210,
1172
644
  S: this,
1173
- A: [
1174
- "doc",
1175
- ""
1176
- ]
645
+ C: (f, a) => f(...a)
1177
646
  });
1178
- const heads = (0, import_automerge3.getHeads)(doc);
1179
- const batch = this._db.batch();
1180
- this._headsStore.setHeads(documentId, heads, batch);
1181
- await batch.write();
1182
647
  }
1183
- import_log2.log.info("done re-indexing heads", void 0, {
1184
- F: __dxlog_file4,
1185
- L: 251,
1186
- S: this,
1187
- C: (f, a) => f(...a)
1188
- });
1189
648
  }
1190
- // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
1191
- // Hosts, running in the worker, don't share documents unless requested by other peers.
1192
- // NOTE: If both peers return sharePolicy=false the replication will not happen
1193
- // https://github.com/automerge/automerge-repo/pull/292
1194
- async _sharePolicy(peerId, documentId) {
1195
- if (peerId.startsWith("client-")) {
1196
- return false;
1197
- }
1198
- if (!documentId) {
1199
- return false;
1200
- }
1201
- const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
1202
- if (isEchoPeerMetadata(peerMetadata)) {
1203
- return this._echoNetworkAdapter.shouldAdvertise(peerId, {
1204
- documentId
1205
- });
1206
- }
1207
- return false;
649
+ async _saveSpaceLargeMetadata(key) {
650
+ const data = this._getLargeSpaceMetadata(key);
651
+ const file = this._directory.getOrCreateFile(`space_${key.toHex()}_large`);
652
+ await this._writeFile(file, LargeSpaceMetadata, data);
1208
653
  }
1209
- async _beforeSave({ path, batch }) {
1210
- const handle = this._repo.handles[path[0]];
1211
- if (!handle) {
1212
- return;
1213
- }
1214
- const doc = handle.docSync();
1215
- if (!doc) {
1216
- return;
654
+ async flush() {
655
+ await this._directory.flush();
656
+ }
657
+ _getSpace(spaceKey) {
658
+ if (this._metadata.identity?.haloSpace.key.equals(spaceKey)) {
659
+ return this._metadata.identity.haloSpace;
1217
660
  }
1218
- const heads = (0, import_automerge3.getHeads)(doc);
1219
- this._headsStore.setHeads(handle.documentId, heads, batch);
1220
- const spaceKey = getSpaceKeyFromDoc(doc) ?? void 0;
1221
- const objectIds = Object.keys(doc.objects ?? {});
1222
- const encodedIds = objectIds.map((objectId) => import_protocols4.objectPointerCodec.encode({
1223
- documentId: handle.documentId,
1224
- objectId,
1225
- spaceKey
1226
- }));
1227
- const idToLastHash = new Map(encodedIds.map((id) => [
1228
- id,
1229
- heads
1230
- ]));
1231
- this._indexMetadataStore.markDirty(idToLastHash, batch);
661
+ const space = this.spaces.find((space2) => space2.key === spaceKey);
662
+ (0, import_invariant4.invariant)(space, "Space not found", {
663
+ F: __dxlog_file4,
664
+ L: 232,
665
+ S: this,
666
+ A: [
667
+ "space",
668
+ "'Space not found'"
669
+ ]
670
+ });
671
+ return space;
1232
672
  }
1233
- _shouldSyncCollection(collectionId, peerId) {
1234
- const peerMetadata = this._repo.peerMetadataByPeerId[peerId];
1235
- if (isEchoPeerMetadata(peerMetadata)) {
1236
- return this._echoNetworkAdapter.shouldSyncCollection(peerId, {
1237
- collectionId
1238
- });
673
+ _getLargeSpaceMetadata(key) {
674
+ let entry = this._spaceLargeMetadata.get(key);
675
+ if (entry) {
676
+ return entry;
1239
677
  }
1240
- return false;
678
+ entry = emptyLargeSpaceMetadata();
679
+ this._spaceLargeMetadata.set(key, entry);
680
+ return entry;
1241
681
  }
1242
682
  /**
1243
- * Called by AutomergeStorageAdapter after levelDB batch commit.
683
+ * Clears storage - doesn't work for now.
1244
684
  */
1245
- async _afterSave(path) {
1246
- this._indexMetadataStore.notifyMarkedDirty();
1247
- const documentId = path[0];
1248
- const document = this._repo.handles[documentId]?.docSync();
1249
- if (document) {
1250
- const heads = (0, import_automerge3.getHeads)(document);
1251
- this._onHeadsChanged(documentId, heads);
1252
- }
685
+ async clear() {
686
+ (0, import_log3.log)("clearing all metadata", void 0, {
687
+ F: __dxlog_file4,
688
+ L: 251,
689
+ S: this,
690
+ C: (f, a) => f(...a)
691
+ });
692
+ await this._directory.delete();
693
+ this._metadata = emptyEchoMetadata();
1253
694
  }
1254
- _automergeDocs() {
1255
- return (0, import_util2.mapValues)(this._repo.handles, (handle) => ({
1256
- state: handle.state,
1257
- hasDoc: !!handle.docSync(),
1258
- heads: handle.docSync() ? import_automerge3.next.getHeads(handle.docSync()) : null,
1259
- data: handle.docSync() && (0, import_util2.mapValues)(handle.docSync(), (value, key) => {
1260
- try {
1261
- switch (key) {
1262
- case "access":
1263
- case "links":
1264
- return value;
1265
- case "objects":
1266
- return Object.keys(value);
1267
- default:
1268
- return `${value}`;
1269
- }
1270
- } catch (err) {
1271
- return `${err}`;
1272
- }
1273
- })
1274
- }));
695
+ getIdentityRecord() {
696
+ return this._metadata.identity;
697
+ }
698
+ async setIdentityRecord(record) {
699
+ (0, import_invariant4.invariant)(!this._metadata.identity, "Cannot overwrite existing identity in metadata", {
700
+ F: __dxlog_file4,
701
+ L: 261,
702
+ S: this,
703
+ A: [
704
+ "!this._metadata.identity",
705
+ "'Cannot overwrite existing identity in metadata'"
706
+ ]
707
+ });
708
+ this._metadata.identity = record;
709
+ await this._save();
710
+ await this.flush();
1275
711
  }
1276
- _automergePeers() {
1277
- return this._repo.peers;
712
+ getInvitations() {
713
+ return this._metadata.invitations ?? [];
1278
714
  }
1279
- async _getContainingSpaceForDocument(documentId) {
1280
- const doc = this._repo.handles[documentId]?.docSync();
1281
- if (!doc) {
1282
- return null;
1283
- }
1284
- const spaceKeyHex = getSpaceKeyFromDoc(doc);
1285
- if (!spaceKeyHex) {
1286
- return null;
715
+ async addInvitation(invitation) {
716
+ if (this._metadata.invitations?.find((i) => i.invitationId === invitation.invitationId)) {
717
+ return;
1287
718
  }
1288
- return import_keys2.PublicKey.from(spaceKeyHex);
719
+ (this._metadata.invitations ??= []).push(invitation);
720
+ await this._save();
721
+ await this.flush();
1289
722
  }
1290
- /**
1291
- * Flush documents to disk.
1292
- */
1293
- async flush({ documentIds } = {}) {
1294
- await this._repo.flush(documentIds);
723
+ async removeInvitation(invitationId) {
724
+ this._metadata.invitations = (this._metadata.invitations ?? []).filter((i) => i.invitationId !== invitationId);
725
+ await this._save();
726
+ await this.flush();
1295
727
  }
1296
- async getHeads(documentIds) {
1297
- const result = [];
1298
- const storeRequestIds = [];
1299
- const storeResultIndices = [];
1300
- for (const documentId of documentIds) {
1301
- const doc = this._repo.handles[documentId]?.docSync();
1302
- if (doc) {
1303
- result.push((0, import_automerge3.getHeads)(doc));
1304
- } else {
1305
- storeRequestIds.push(documentId);
1306
- storeResultIndices.push(result.length);
1307
- result.push(void 0);
1308
- }
1309
- }
1310
- if (storeRequestIds.length > 0) {
1311
- const storedHeads = await this._headsStore.getHeads(storeRequestIds);
1312
- for (let i = 0; i < storedHeads.length; i++) {
1313
- result[storeResultIndices[i]] = storedHeads[i];
1314
- }
1315
- }
1316
- return result;
728
+ async addSpace(record) {
729
+ (0, import_invariant4.invariant)(!(this._metadata.spaces ?? []).find((space) => space.key === record.key), "Cannot overwrite existing space in metadata", {
730
+ F: __dxlog_file4,
731
+ L: 289,
732
+ S: this,
733
+ A: [
734
+ "!(this._metadata.spaces ?? []).find((space) => space.key === record.key)",
735
+ "'Cannot overwrite existing space in metadata'"
736
+ ]
737
+ });
738
+ (this._metadata.spaces ??= []).push(record);
739
+ await this._save();
740
+ await this.flush();
741
+ }
742
+ async setSpaceDataLatestTimeframe(spaceKey, timeframe) {
743
+ this._getSpace(spaceKey).dataTimeframe = timeframe;
744
+ await this._save();
1317
745
  }
1318
- //
1319
- // Collection sync.
1320
- //
1321
- getLocalCollectionState(collectionId) {
1322
- return this._collectionSynchronizer.getLocalCollectionState(collectionId);
746
+ async setSpaceControlLatestTimeframe(spaceKey, timeframe) {
747
+ this._getSpace(spaceKey).controlTimeframe = timeframe;
748
+ await this._save();
749
+ await this.flush();
1323
750
  }
1324
- getRemoteCollectionStates(collectionId) {
1325
- return this._collectionSynchronizer.getRemoteCollectionStates(collectionId);
751
+ async setCache(spaceKey, cache) {
752
+ this._getSpace(spaceKey).cache = cache;
753
+ await this._save();
1326
754
  }
1327
- refreshCollection(collectionId) {
1328
- this._collectionSynchronizer.refreshCollection(collectionId);
755
+ async setWritableFeedKeys(spaceKey, controlFeedKey, dataFeedKey) {
756
+ const space = this._getSpace(spaceKey);
757
+ space.controlFeedKey = controlFeedKey;
758
+ space.dataFeedKey = dataFeedKey;
759
+ await this._save();
760
+ await this.flush();
1329
761
  }
1330
- async getCollectionSyncState(collectionId) {
1331
- const result = {
1332
- peers: []
1333
- };
1334
- const localState = this.getLocalCollectionState(collectionId);
1335
- const remoteState = this.getRemoteCollectionStates(collectionId);
1336
- if (!localState) {
1337
- return result;
1338
- }
1339
- for (const [peerId, state] of remoteState) {
1340
- const diff = diffCollectionState(localState, state);
1341
- result.peers.push({
1342
- peerId,
1343
- differentDocuments: diff.different.length
1344
- });
1345
- }
1346
- return result;
762
+ async setSpaceState(spaceKey, state) {
763
+ this._getSpace(spaceKey).state = state;
764
+ await this._save();
765
+ await this.flush();
766
+ }
767
+ getSpaceControlPipelineSnapshot(spaceKey) {
768
+ return this._getLargeSpaceMetadata(spaceKey).controlPipelineSnapshot;
769
+ }
770
+ async setSpaceControlPipelineSnapshot(spaceKey, snapshot) {
771
+ this._getLargeSpaceMetadata(spaceKey).controlPipelineSnapshot = snapshot;
772
+ await this._saveSpaceLargeMetadata(spaceKey);
773
+ await this.flush();
774
+ }
775
+ };
776
+ _ts_decorate([
777
+ import_async2.synchronized
778
+ ], MetadataStore.prototype, "load", null);
779
+ _ts_decorate([
780
+ import_async2.synchronized
781
+ ], MetadataStore.prototype, "_save", null);
782
+ _ts_decorate([
783
+ import_async2.synchronized
784
+ ], MetadataStore.prototype, "_saveSpaceLargeMetadata", null);
785
+ var fromBytesInt32 = (buf) => buf.readInt32LE(0);
786
+ var hasInvitationExpired = (invitation) => {
787
+ return Boolean(invitation.created && invitation.lifetime && invitation.lifetime !== 0 && invitation.created.getTime() + invitation.lifetime * 1e3 < Date.now());
788
+ };
789
+ var isLegacyInvitationFormat = (invitation) => {
790
+ return invitation.type === import_services.Invitation.Type.MULTIUSE;
791
+ };
792
+ function _ts_decorate2(decorators, target, key, desc) {
793
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
794
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
795
+ r = Reflect.decorate(decorators, target, key, desc);
796
+ else
797
+ for (var i = decorators.length - 1; i >= 0; i--)
798
+ if (d = decorators[i])
799
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
800
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
801
+ }
802
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/pipeline/timeframe-clock.ts";
803
+ var mapTimeframeToFeedIndexes = (timeframe) => timeframe.frames().map(([feedKey, index]) => ({
804
+ feedKey,
805
+ index
806
+ }));
807
+ var mapFeedIndexesToTimeframe = (indexes) => new import_timeframe.Timeframe(indexes.map(({ feedKey, index }) => [
808
+ feedKey,
809
+ index
810
+ ]));
811
+ var startAfter = (timeframe) => timeframe.frames().map(([feedKey, index]) => ({
812
+ feedKey,
813
+ index: index + 1
814
+ }));
815
+ var TimeframeClock = class {
816
+ constructor(_timeframe = new import_timeframe.Timeframe()) {
817
+ this._timeframe = _timeframe;
818
+ this.update = new import_async3.Event();
819
+ this._pendingTimeframe = _timeframe;
1347
820
  }
1348
821
  /**
1349
- * Update the local collection state based on the locally stored document heads.
822
+ * Timeframe that was processed by ECHO.
1350
823
  */
1351
- async updateLocalCollectionState(collectionId, documentIds) {
1352
- const heads = await this.getHeads(documentIds);
1353
- const documents = Object.fromEntries(heads.map((heads2, index) => [
1354
- documentIds[index],
1355
- heads2 ?? []
1356
- ]));
1357
- this._collectionSynchronizer.setLocalCollectionState(collectionId, {
1358
- documents
1359
- });
1360
- }
1361
- _onCollectionStateQueried(collectionId, peerId) {
1362
- this._collectionSynchronizer.onCollectionStateQueried(collectionId, peerId);
824
+ get timeframe() {
825
+ return this._timeframe;
1363
826
  }
1364
- _onCollectionStateReceived(collectionId, peerId, state) {
1365
- this._collectionSynchronizer.onRemoteStateReceived(collectionId, peerId, decodeCollectionState(state));
827
+ /**
828
+ * Timeframe that is currently being processed by ECHO.
829
+ * Will be equal to `timeframe` after the processing is complete.
830
+ */
831
+ get pendingTimeframe() {
832
+ return this._pendingTimeframe;
1366
833
  }
1367
- _queryCollectionState(collectionId, peerId) {
1368
- this._echoNetworkAdapter.queryCollectionState(collectionId, peerId);
834
+ setTimeframe(timeframe) {
835
+ this._timeframe = timeframe;
836
+ this._pendingTimeframe = timeframe;
837
+ this.update.emit(this._timeframe);
1369
838
  }
1370
- _sendCollectionState(collectionId, peerId, state) {
1371
- this._echoNetworkAdapter.sendCollectionState(collectionId, peerId, encodeCollectionState(state));
839
+ updatePendingTimeframe(key, seq) {
840
+ this._pendingTimeframe = import_timeframe.Timeframe.merge(this._pendingTimeframe, new import_timeframe.Timeframe([
841
+ [
842
+ key,
843
+ seq
844
+ ]
845
+ ]));
1372
846
  }
1373
- _onPeerConnected(peerId) {
1374
- this._collectionSynchronizer.onConnectionOpen(peerId);
847
+ updateTimeframe() {
848
+ this._timeframe = this._pendingTimeframe;
849
+ this.update.emit(this._timeframe);
1375
850
  }
1376
- _onPeerDisconnected(peerId) {
1377
- this._collectionSynchronizer.onConnectionClosed(peerId);
851
+ hasGaps(timeframe) {
852
+ const gaps = import_timeframe.Timeframe.dependencies(timeframe, this._timeframe);
853
+ return !gaps.isEmpty();
1378
854
  }
1379
- _onRemoteCollectionStateUpdated(collectionId, peerId) {
1380
- const localState = this._collectionSynchronizer.getLocalCollectionState(collectionId);
1381
- const remoteState = this._collectionSynchronizer.getRemoteCollectionStates(collectionId).get(peerId);
1382
- if (!localState || !remoteState) {
1383
- return;
1384
- }
1385
- const { different } = diffCollectionState(localState, remoteState);
1386
- if (different.length === 0) {
1387
- return;
1388
- }
1389
- import_log2.log.info("replication documents after collection sync", {
1390
- count: different.length
855
+ async waitUntilReached(target) {
856
+ (0, import_log4.log)("waitUntilReached", {
857
+ target,
858
+ current: this._timeframe
1391
859
  }, {
1392
- F: __dxlog_file4,
1393
- L: 486,
860
+ F: __dxlog_file5,
861
+ L: 70,
1394
862
  S: this,
1395
863
  C: (f, a) => f(...a)
1396
864
  });
1397
- for (const documentId of different) {
1398
- this._repo.find(documentId);
1399
- }
1400
- }
1401
- _onHeadsChanged(documentId, heads) {
1402
- for (const collectionId of this._collectionSynchronizer.getRegisteredCollectionIds()) {
1403
- const state = this._collectionSynchronizer.getLocalCollectionState(collectionId);
1404
- if (state?.documents[documentId]) {
1405
- const newState = structuredClone(state);
1406
- newState.documents[documentId] = heads;
1407
- this._collectionSynchronizer.setLocalCollectionState(collectionId, newState);
1408
- }
1409
- }
865
+ await this.update.waitForCondition(() => {
866
+ (0, import_log4.log)("check if reached", {
867
+ target,
868
+ current: this._timeframe,
869
+ deps: import_timeframe.Timeframe.dependencies(target, this._timeframe)
870
+ }, {
871
+ F: __dxlog_file5,
872
+ L: 72,
873
+ S: this,
874
+ C: (f, a) => f(...a)
875
+ });
876
+ return import_timeframe.Timeframe.dependencies(target, this._timeframe).isEmpty();
877
+ });
1410
878
  }
1411
879
  };
1412
880
  _ts_decorate2([
1413
- import_tracing.trace.info()
1414
- ], AutomergeHost.prototype, "_peerId", void 0);
1415
- _ts_decorate2([
1416
- import_tracing.trace.info({
1417
- depth: null
1418
- })
1419
- ], AutomergeHost.prototype, "_automergeDocs", null);
1420
- _ts_decorate2([
1421
- import_tracing.trace.info({
1422
- depth: null
1423
- })
1424
- ], AutomergeHost.prototype, "_automergePeers", null);
1425
- _ts_decorate2([
1426
- import_tracing.trace.span({
1427
- showInBrowserTimeline: true
1428
- })
1429
- ], AutomergeHost.prototype, "flush", null);
1430
- AutomergeHost = _ts_decorate2([
1431
- import_tracing.trace.resource()
1432
- ], AutomergeHost);
1433
- var getSpaceKeyFromDoc = (doc) => {
1434
- const rawSpaceKey = doc.access?.spaceKey ?? doc.experimental_spaceKey;
1435
- if (rawSpaceKey == null) {
1436
- return null;
1437
- }
1438
- return String(rawSpaceKey);
1439
- };
1440
- var waitForHeads = async (handle, heads) => {
1441
- const unavailableHeads = new Set(heads);
1442
- await handle.whenReady();
1443
- await import_async3.Event.wrap(handle, "change").waitForCondition(() => {
1444
- for (const changeHash of unavailableHeads.values()) {
1445
- if (changeIsPresentInDoc(handle.docSync(), changeHash)) {
1446
- unavailableHeads.delete(changeHash);
881
+ (0, import_debug.timed)(5e3)
882
+ ], TimeframeClock.prototype, "waitUntilReached", null);
883
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/pipeline/message-selector.ts";
884
+ var createMessageSelector = (timeframeClock) => {
885
+ return (messages) => {
886
+ for (let i = 0; i < messages.length; i++) {
887
+ const { data: { timeframe } } = messages[i];
888
+ (0, import_invariant6.invariant)(timeframe, void 0, {
889
+ F: __dxlog_file6,
890
+ L: 25,
891
+ S: void 0,
892
+ A: [
893
+ "timeframe",
894
+ ""
895
+ ]
896
+ });
897
+ if (!timeframeClock.hasGaps(timeframe)) {
898
+ return i;
1447
899
  }
1448
900
  }
1449
- return unavailableHeads.size === 0;
1450
- });
1451
- };
1452
- var changeIsPresentInDoc = (doc, changeHash) => {
1453
- return !!(0, import_automerge3.getBackend)(doc).getChangeByHash(changeHash);
1454
- };
1455
- var decodeCollectionState = (state) => {
1456
- (0, import_invariant3.invariant)(typeof state === "object" && state !== null, "Invalid state", {
1457
- F: __dxlog_file4,
1458
- L: 539,
1459
- S: void 0,
1460
- A: [
1461
- "typeof state === 'object' && state !== null",
1462
- "'Invalid state'"
1463
- ]
1464
- });
1465
- return state;
1466
- };
1467
- var encodeCollectionState = (state) => {
1468
- return state;
1469
- };
1470
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1471
- var deriveCollectionIdFromSpaceId = (spaceId) => `space:${spaceId}`;
1472
- var getSpaceIdFromCollectionId = (collectionId) => {
1473
- const spaceId = collectionId.replace(/^space:/, "");
1474
- (0, import_invariant5.invariant)(import_keys3.SpaceId.isValid(spaceId), void 0, {
1475
- F: __dxlog_file5,
1476
- L: 12,
1477
- S: void 0,
1478
- A: [
1479
- "SpaceId.isValid(spaceId)",
1480
- ""
1481
- ]
1482
- });
1483
- return spaceId;
1484
- };
1485
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/auth.ts";
1486
- var AuthExtension = class extends import_teleport.RpcExtension {
1487
- constructor(_authParams) {
1488
- super({
1489
- requested: {
1490
- AuthService: import_protocols5.schema.getService("dxos.mesh.teleport.auth.AuthService")
1491
- },
1492
- exposed: {
1493
- AuthService: import_protocols5.schema.getService("dxos.mesh.teleport.auth.AuthService")
1494
- },
1495
- timeout: 60 * 1e3
1496
- });
1497
- this._authParams = _authParams;
1498
- this._ctx = new import_context7.Context({
1499
- onError: (err) => {
1500
- import_log4.log.catch(err, void 0, {
1501
- F: __dxlog_file6,
1502
- L: 28,
1503
- S: this,
1504
- C: (f, a) => f(...a)
1505
- });
1506
- }
1507
- }, {
901
+ (0, import_log6.log)("Skipping...", void 0, {
1508
902
  F: __dxlog_file6,
1509
- L: 26
1510
- });
1511
- }
1512
- async getHandlers() {
1513
- return {
1514
- AuthService: {
1515
- authenticate: async ({ challenge }) => {
1516
- try {
1517
- const credential = await this._authParams.provider(challenge);
1518
- if (!credential) {
1519
- throw new Error("auth rejected");
1520
- }
1521
- return {
1522
- credential
1523
- };
1524
- } catch (err) {
1525
- import_log4.log.error("failed to generate auth credentials", err, {
1526
- F: __dxlog_file6,
1527
- L: 55,
1528
- S: this,
1529
- C: (f, a) => f(...a)
1530
- });
1531
- throw new Error("auth rejected");
1532
- }
1533
- }
1534
- }
1535
- };
1536
- }
1537
- async onOpen(context) {
1538
- await super.onOpen(context);
1539
- (0, import_async5.scheduleTask)(this._ctx, async () => {
1540
- try {
1541
- const challenge = (0, import_crypto2.randomBytes)(32);
1542
- const { credential } = await this.rpc.AuthService.authenticate({
1543
- challenge
1544
- });
1545
- (0, import_invariant6.invariant)(credential?.length > 0, "invalid credential", {
1546
- F: __dxlog_file6,
1547
- L: 69,
1548
- S: this,
1549
- A: [
1550
- "credential?.length > 0",
1551
- "'invalid credential'"
1552
- ]
1553
- });
1554
- const success = await this._authParams.verifier(challenge, credential);
1555
- (0, import_invariant6.invariant)(success, "credential not verified", {
1556
- F: __dxlog_file6,
1557
- L: 71,
1558
- S: this,
1559
- A: [
1560
- "success",
1561
- "'credential not verified'"
1562
- ]
1563
- });
1564
- (0, import_async5.runInContext)(this._ctx, () => this._authParams.onAuthSuccess());
1565
- } catch (err) {
1566
- (0, import_log4.log)("auth failed", err, {
1567
- F: __dxlog_file6,
1568
- L: 74,
1569
- S: this,
1570
- C: (f, a) => f(...a)
1571
- });
1572
- this.close();
1573
- this._authParams.onAuthFailure();
1574
- }
1575
- });
1576
- }
1577
- async onClose() {
1578
- await this._ctx.dispose();
1579
- await super.onClose();
1580
- }
1581
- async onAbort() {
1582
- await this._ctx.dispose();
1583
- await super.onAbort();
1584
- }
1585
- };
1586
- function _ts_decorate3(decorators, target, key, desc) {
1587
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1588
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1589
- r = Reflect.decorate(decorators, target, key, desc);
1590
- else
1591
- for (var i = decorators.length - 1; i >= 0; i--)
1592
- if (d = decorators[i])
1593
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1594
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1595
- }
1596
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/pipeline/timeframe-clock.ts";
1597
- var mapTimeframeToFeedIndexes = (timeframe) => timeframe.frames().map(([feedKey, index]) => ({
1598
- feedKey,
1599
- index
1600
- }));
1601
- var mapFeedIndexesToTimeframe = (indexes) => new import_timeframe.Timeframe(indexes.map(({ feedKey, index }) => [
1602
- feedKey,
1603
- index
1604
- ]));
1605
- var startAfter = (timeframe) => timeframe.frames().map(([feedKey, index]) => ({
1606
- feedKey,
1607
- index: index + 1
1608
- }));
1609
- var TimeframeClock = class {
1610
- constructor(_timeframe = new import_timeframe.Timeframe()) {
1611
- this._timeframe = _timeframe;
1612
- this.update = new import_async6.Event();
1613
- this._pendingTimeframe = _timeframe;
1614
- }
1615
- /**
1616
- * Timeframe that was processed by ECHO.
1617
- */
1618
- get timeframe() {
1619
- return this._timeframe;
1620
- }
1621
- /**
1622
- * Timeframe that is currently being processed by ECHO.
1623
- * Will be equal to `timeframe` after the processing is complete.
1624
- */
1625
- get pendingTimeframe() {
1626
- return this._pendingTimeframe;
1627
- }
1628
- setTimeframe(timeframe) {
1629
- this._timeframe = timeframe;
1630
- this._pendingTimeframe = timeframe;
1631
- this.update.emit(this._timeframe);
1632
- }
1633
- updatePendingTimeframe(key, seq) {
1634
- this._pendingTimeframe = import_timeframe.Timeframe.merge(this._pendingTimeframe, new import_timeframe.Timeframe([
1635
- [
1636
- key,
1637
- seq
1638
- ]
1639
- ]));
1640
- }
1641
- updateTimeframe() {
1642
- this._timeframe = this._pendingTimeframe;
1643
- this.update.emit(this._timeframe);
1644
- }
1645
- hasGaps(timeframe) {
1646
- const gaps = import_timeframe.Timeframe.dependencies(timeframe, this._timeframe);
1647
- return !gaps.isEmpty();
1648
- }
1649
- async waitUntilReached(target) {
1650
- (0, import_log5.log)("waitUntilReached", {
1651
- target,
1652
- current: this._timeframe
1653
- }, {
1654
- F: __dxlog_file7,
1655
- L: 70,
1656
- S: this,
1657
- C: (f, a) => f(...a)
1658
- });
1659
- await this.update.waitForCondition(() => {
1660
- (0, import_log5.log)("check if reached", {
1661
- target,
1662
- current: this._timeframe,
1663
- deps: import_timeframe.Timeframe.dependencies(target, this._timeframe)
1664
- }, {
1665
- F: __dxlog_file7,
1666
- L: 72,
1667
- S: this,
1668
- C: (f, a) => f(...a)
1669
- });
1670
- return import_timeframe.Timeframe.dependencies(target, this._timeframe).isEmpty();
1671
- });
1672
- }
1673
- };
1674
- _ts_decorate3([
1675
- (0, import_debug.timed)(5e3)
1676
- ], TimeframeClock.prototype, "waitUntilReached", null);
1677
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/pipeline/message-selector.ts";
1678
- var createMessageSelector = (timeframeClock) => {
1679
- return (messages) => {
1680
- for (let i = 0; i < messages.length; i++) {
1681
- const { data: { timeframe } } = messages[i];
1682
- (0, import_invariant8.invariant)(timeframe, void 0, {
1683
- F: __dxlog_file8,
1684
- L: 25,
1685
- S: void 0,
1686
- A: [
1687
- "timeframe",
1688
- ""
1689
- ]
1690
- });
1691
- if (!timeframeClock.hasGaps(timeframe)) {
1692
- return i;
1693
- }
1694
- }
1695
- (0, import_log7.log)("Skipping...", void 0, {
1696
- F: __dxlog_file8,
1697
903
  L: 33,
1698
904
  S: void 0,
1699
905
  C: (f, a) => f(...a)
1700
906
  });
1701
907
  };
1702
908
  };
1703
- function _ts_decorate4(decorators, target, key, desc) {
909
+ function _ts_decorate3(decorators, target, key, desc) {
1704
910
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1705
911
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1706
912
  r = Reflect.decorate(decorators, target, key, desc);
@@ -1710,17 +916,17 @@ function _ts_decorate4(decorators, target, key, desc) {
1710
916
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1711
917
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1712
918
  }
1713
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/pipeline/pipeline.ts";
919
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/pipeline/pipeline.ts";
1714
920
  var PipelineState = class {
1715
921
  constructor(_feeds, _timeframeClock) {
1716
922
  this._feeds = _feeds;
1717
923
  this._timeframeClock = _timeframeClock;
1718
- this._ctx = new import_context8.Context(void 0, {
1719
- F: __dxlog_file9,
924
+ this._ctx = new import_context4.Context(void 0, {
925
+ F: __dxlog_file7,
1720
926
  L: 41
1721
927
  });
1722
928
  this.timeframeUpdate = this._timeframeClock.update;
1723
- this.stalled = new import_async7.Event();
929
+ this.stalled = new import_async4.Event();
1724
930
  this._startTimeframe = new import_timeframe2.Timeframe();
1725
931
  this._reachedTarget = false;
1726
932
  }
@@ -1766,16 +972,16 @@ var PipelineState = class {
1766
972
  *
1767
973
  * @param timeout Timeout in milliseconds to specify the maximum wait time.
1768
974
  */
1769
- async waitUntilReachedTargetTimeframe({ ctx = new import_context8.Context(void 0, {
1770
- F: __dxlog_file9,
975
+ async waitUntilReachedTargetTimeframe({ ctx = new import_context4.Context(void 0, {
976
+ F: __dxlog_file7,
1771
977
  L: 129
1772
978
  }), timeout, breakOnStall = true } = {}) {
1773
- (0, import_log6.log)("waitUntilReachedTargetTimeframe", {
979
+ (0, import_log5.log)("waitUntilReachedTargetTimeframe", {
1774
980
  timeout,
1775
981
  current: this.timeframe,
1776
982
  target: this.targetTimeframe
1777
983
  }, {
1778
- F: __dxlog_file9,
984
+ F: __dxlog_file7,
1779
985
  L: 133,
1780
986
  S: this,
1781
987
  C: (f, a) => f(...a)
@@ -1791,23 +997,23 @@ var PipelineState = class {
1791
997
  let done = false;
1792
998
  if (timeout) {
1793
999
  return Promise.race([
1794
- (0, import_context8.rejectOnDispose)(ctx),
1795
- (0, import_context8.rejectOnDispose)(this._ctx),
1000
+ (0, import_context4.rejectOnDispose)(ctx),
1001
+ (0, import_context4.rejectOnDispose)(this._ctx),
1796
1002
  this._reachedTargetPromise.then(() => {
1797
1003
  done = true;
1798
1004
  this._reachedTarget = true;
1799
1005
  }),
1800
- (0, import_async7.sleepWithContext)(this._ctx, timeout).then(() => {
1006
+ (0, import_async4.sleepWithContext)(this._ctx, timeout).then(() => {
1801
1007
  if (done) {
1802
1008
  return;
1803
1009
  }
1804
- import_log6.log.warn("waitUntilReachedTargetTimeframe timed out", {
1010
+ import_log5.log.warn("waitUntilReachedTargetTimeframe timed out", {
1805
1011
  timeout,
1806
1012
  current: this.timeframe,
1807
1013
  target: this.targetTimeframe,
1808
1014
  dependencies: import_timeframe2.Timeframe.dependencies(this.targetTimeframe, this.timeframe)
1809
1015
  }, {
1810
- F: __dxlog_file9,
1016
+ F: __dxlog_file7,
1811
1017
  L: 161,
1812
1018
  S: this,
1813
1019
  C: (f, a) => f(...a)
@@ -1822,11 +1028,11 @@ var PipelineState = class {
1822
1028
  var Pipeline = class {
1823
1029
  constructor() {
1824
1030
  this._timeframeClock = new TimeframeClock(new import_timeframe2.Timeframe());
1825
- this._feeds = new import_util4.ComplexMap(import_keys4.PublicKey.hash);
1031
+ this._feeds = new import_util2.ComplexMap(import_keys3.PublicKey.hash);
1826
1032
  this._state = new PipelineState(this._feeds, this._timeframeClock);
1827
- this._processingTrigger = new import_async7.Trigger().wake();
1828
- this._pauseTrigger = new import_async7.Trigger().wake();
1829
- this._downloads = new import_util4.ComplexMap((value) => import_keys4.PublicKey.hash(value.key));
1033
+ this._processingTrigger = new import_async4.Trigger().wake();
1034
+ this._pauseTrigger = new import_async4.Trigger().wake();
1035
+ this._downloads = new import_util2.ComplexMap((value) => import_keys3.PublicKey.hash(value.key));
1830
1036
  this._isStopping = false;
1831
1037
  this._isStarted = false;
1832
1038
  this._isBeingConsumed = false;
@@ -1836,8 +1042,8 @@ var Pipeline = class {
1836
1042
  return this._state;
1837
1043
  }
1838
1044
  get writer() {
1839
- (0, import_invariant7.invariant)(this._writer, "Writer not set.", {
1840
- F: __dxlog_file9,
1045
+ (0, import_invariant5.invariant)(this._writer, "Writer not set.", {
1046
+ F: __dxlog_file7,
1841
1047
  L: 243,
1842
1048
  S: this,
1843
1049
  A: [
@@ -1865,8 +1071,8 @@ var Pipeline = class {
1865
1071
  }
1866
1072
  }
1867
1073
  setWriteFeed(feed) {
1868
- (0, import_invariant7.invariant)(!this._writer, "Writer already set.", {
1869
- F: __dxlog_file9,
1074
+ (0, import_invariant5.invariant)(!this._writer, "Writer already set.", {
1075
+ F: __dxlog_file7,
1870
1076
  L: 270,
1871
1077
  S: this,
1872
1078
  A: [
@@ -1874,8 +1080,8 @@ var Pipeline = class {
1874
1080
  "'Writer already set.'"
1875
1081
  ]
1876
1082
  });
1877
- (0, import_invariant7.invariant)(feed.properties.writable, "Feed must be writable.", {
1878
- F: __dxlog_file9,
1083
+ (0, import_invariant5.invariant)(feed.properties.writable, "Feed must be writable.", {
1084
+ F: __dxlog_file7,
1879
1085
  L: 271,
1880
1086
  S: this,
1881
1087
  A: [
@@ -1889,8 +1095,8 @@ var Pipeline = class {
1889
1095
  }), feed.createFeedWriter());
1890
1096
  }
1891
1097
  async start() {
1892
- (0, import_invariant7.invariant)(!this._isStarted, "Pipeline is already started.", {
1893
- F: __dxlog_file9,
1098
+ (0, import_invariant5.invariant)(!this._isStarted, "Pipeline is already started.", {
1099
+ F: __dxlog_file7,
1894
1100
  L: 284,
1895
1101
  S: this,
1896
1102
  A: [
@@ -1898,8 +1104,8 @@ var Pipeline = class {
1898
1104
  "'Pipeline is already started.'"
1899
1105
  ]
1900
1106
  });
1901
- (0, import_log6.log)("starting...", void 0, {
1902
- F: __dxlog_file9,
1107
+ (0, import_log5.log)("starting...", void 0, {
1108
+ F: __dxlog_file7,
1903
1109
  L: 285,
1904
1110
  S: this,
1905
1111
  C: (f, a) => f(...a)
@@ -1907,8 +1113,8 @@ var Pipeline = class {
1907
1113
  await this._initIterator();
1908
1114
  await this._feedSetIterator.open();
1909
1115
  this._isStarted = true;
1910
- (0, import_log6.log)("started", void 0, {
1911
- F: __dxlog_file9,
1116
+ (0, import_log5.log)("started", void 0, {
1117
+ F: __dxlog_file7,
1912
1118
  L: 289,
1913
1119
  S: this,
1914
1120
  C: (f, a) => f(...a)
@@ -1920,8 +1126,8 @@ var Pipeline = class {
1920
1126
  }
1921
1127
  }
1922
1128
  async stop() {
1923
- (0, import_log6.log)("stopping...", void 0, {
1924
- F: __dxlog_file9,
1129
+ (0, import_log5.log)("stopping...", void 0, {
1130
+ F: __dxlog_file7,
1925
1131
  L: 300,
1926
1132
  S: this,
1927
1133
  C: (f, a) => f(...a)
@@ -1934,15 +1140,15 @@ var Pipeline = class {
1934
1140
  await this._feedSetIterator?.close();
1935
1141
  await this._processingTrigger.wait();
1936
1142
  await this._state._ctx.dispose();
1937
- this._state._ctx = new import_context8.Context(void 0, {
1938
- F: __dxlog_file9,
1143
+ this._state._ctx = new import_context4.Context(void 0, {
1144
+ F: __dxlog_file7,
1939
1145
  L: 309
1940
1146
  });
1941
1147
  this._state._reachedTargetPromise = void 0;
1942
1148
  this._state._reachedTarget = false;
1943
1149
  this._isStarted = false;
1944
- (0, import_log6.log)("stopped", void 0, {
1945
- F: __dxlog_file9,
1150
+ (0, import_log5.log)("stopped", void 0, {
1151
+ F: __dxlog_file7,
1946
1152
  L: 313,
1947
1153
  S: this,
1948
1154
  C: (f, a) => f(...a)
@@ -1953,8 +1159,8 @@ var Pipeline = class {
1953
1159
  * The pipeline will start processing messages AFTER this timeframe.
1954
1160
  */
1955
1161
  async setCursor(timeframe) {
1956
- (0, import_invariant7.invariant)(!this._isStarted || this._isPaused, "Invalid state.", {
1957
- F: __dxlog_file9,
1162
+ (0, import_invariant5.invariant)(!this._isStarted || this._isPaused, "Invalid state.", {
1163
+ F: __dxlog_file7,
1958
1164
  L: 322,
1959
1165
  S: this,
1960
1166
  A: [
@@ -1982,8 +1188,8 @@ var Pipeline = class {
1982
1188
  this._isPaused = true;
1983
1189
  }
1984
1190
  async unpause() {
1985
- (0, import_invariant7.invariant)(this._isPaused, "Pipeline is not paused.", {
1986
- F: __dxlog_file9,
1191
+ (0, import_invariant5.invariant)(this._isPaused, "Pipeline is not paused.", {
1192
+ F: __dxlog_file7,
1987
1193
  L: 351,
1988
1194
  S: this,
1989
1195
  A: [
@@ -2002,8 +1208,8 @@ var Pipeline = class {
2002
1208
  * Updates the timeframe clock after the message has bee processed.
2003
1209
  */
2004
1210
  async *consume() {
2005
- (0, import_invariant7.invariant)(!this._isBeingConsumed, "Pipeline is already being consumed.", {
2006
- F: __dxlog_file9,
1211
+ (0, import_invariant5.invariant)(!this._isBeingConsumed, "Pipeline is already being consumed.", {
1212
+ F: __dxlog_file7,
2007
1213
  L: 366,
2008
1214
  S: this,
2009
1215
  A: [
@@ -2012,8 +1218,8 @@ var Pipeline = class {
2012
1218
  ]
2013
1219
  });
2014
1220
  this._isBeingConsumed = true;
2015
- (0, import_invariant7.invariant)(this._feedSetIterator, "Iterator not initialized.", {
2016
- F: __dxlog_file9,
1221
+ (0, import_invariant5.invariant)(this._feedSetIterator, "Iterator not initialized.", {
1222
+ F: __dxlog_file7,
2017
1223
  L: 369,
2018
1224
  S: this,
2019
1225
  A: [
@@ -2026,8 +1232,8 @@ var Pipeline = class {
2026
1232
  while (!this._isStopping) {
2027
1233
  await this._pauseTrigger.wait();
2028
1234
  if (lastFeedSetIterator !== this._feedSetIterator) {
2029
- (0, import_invariant7.invariant)(this._feedSetIterator, "Iterator not initialized.", {
2030
- F: __dxlog_file9,
1235
+ (0, import_invariant5.invariant)(this._feedSetIterator, "Iterator not initialized.", {
1236
+ F: __dxlog_file7,
2031
1237
  L: 378,
2032
1238
  S: this,
2033
1239
  A: [
@@ -2042,7 +1248,7 @@ var Pipeline = class {
2042
1248
  if (!done) {
2043
1249
  const block = value ?? (0, import_debug2.failUndefined)();
2044
1250
  this._processingTrigger.reset();
2045
- this._timeframeClock.updatePendingTimeframe(import_keys4.PublicKey.from(block.feedKey), block.seq);
1251
+ this._timeframeClock.updatePendingTimeframe(import_keys3.PublicKey.from(block.feedKey), block.seq);
2046
1252
  yield block;
2047
1253
  this._processingTrigger.wake();
2048
1254
  this._timeframeClock.updateTimeframe();
@@ -2057,12 +1263,12 @@ var Pipeline = class {
2057
1263
  }
2058
1264
  const timeframe = this._state._startTimeframe;
2059
1265
  const seq = timeframe.get(feed.key) ?? -1;
2060
- (0, import_log6.log)("download", {
1266
+ (0, import_log5.log)("download", {
2061
1267
  feed: feed.key.truncate(),
2062
1268
  seq,
2063
1269
  length: feed.length
2064
1270
  }, {
2065
- F: __dxlog_file9,
1271
+ F: __dxlog_file7,
2066
1272
  L: 407,
2067
1273
  S: this,
2068
1274
  C: (f, a) => f(...a)
@@ -2073,10 +1279,10 @@ var Pipeline = class {
2073
1279
  }, (err, data) => {
2074
1280
  if (err) {
2075
1281
  } else {
2076
- import_log6.log.info("downloaded", {
1282
+ import_log5.log.info("downloaded", {
2077
1283
  data
2078
1284
  }, {
2079
- F: __dxlog_file9,
1285
+ F: __dxlog_file7,
2080
1286
  L: 412,
2081
1287
  S: this,
2082
1288
  C: (f, a) => f(...a)
@@ -2091,8 +1297,8 @@ var Pipeline = class {
2091
1297
  stallTimeout: 1e3
2092
1298
  });
2093
1299
  this._feedSetIterator.stalled.on((iterator) => {
2094
- import_log6.log.warn(`Stalled after ${iterator.options.stallTimeout}ms with ${iterator.size} feeds.`, void 0, {
2095
- F: __dxlog_file9,
1300
+ import_log5.log.warn(`Stalled after ${iterator.options.stallTimeout}ms with ${iterator.size} feeds.`, void 0, {
1301
+ F: __dxlog_file7,
2096
1302
  L: 426,
2097
1303
  S: this,
2098
1304
  C: (f, a) => f(...a)
@@ -2104,22 +1310,123 @@ var Pipeline = class {
2104
1310
  }
2105
1311
  }
2106
1312
  };
2107
- _ts_decorate4([
2108
- import_async7.synchronized
1313
+ _ts_decorate3([
1314
+ import_async4.synchronized
2109
1315
  ], Pipeline.prototype, "start", null);
2110
- _ts_decorate4([
2111
- import_async7.synchronized
1316
+ _ts_decorate3([
1317
+ import_async4.synchronized
2112
1318
  ], Pipeline.prototype, "stop", null);
2113
- _ts_decorate4([
2114
- import_async7.synchronized
1319
+ _ts_decorate3([
1320
+ import_async4.synchronized
2115
1321
  ], Pipeline.prototype, "setCursor", null);
2116
- _ts_decorate4([
2117
- import_async7.synchronized
1322
+ _ts_decorate3([
1323
+ import_async4.synchronized
2118
1324
  ], Pipeline.prototype, "pause", null);
2119
- _ts_decorate4([
2120
- import_async7.synchronized
1325
+ _ts_decorate3([
1326
+ import_async4.synchronized
2121
1327
  ], Pipeline.prototype, "unpause", null);
2122
- function _ts_decorate5(decorators, target, key, desc) {
1328
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/auth.ts";
1329
+ var AuthExtension = class extends import_teleport.RpcExtension {
1330
+ constructor(_authParams) {
1331
+ super({
1332
+ requested: {
1333
+ AuthService: import_protocols5.schema.getService("dxos.mesh.teleport.auth.AuthService")
1334
+ },
1335
+ exposed: {
1336
+ AuthService: import_protocols5.schema.getService("dxos.mesh.teleport.auth.AuthService")
1337
+ },
1338
+ timeout: 60 * 1e3
1339
+ });
1340
+ this._authParams = _authParams;
1341
+ this._ctx = new import_context5.Context({
1342
+ onError: (err) => {
1343
+ import_log7.log.catch(err, void 0, {
1344
+ F: __dxlog_file8,
1345
+ L: 28,
1346
+ S: this,
1347
+ C: (f, a) => f(...a)
1348
+ });
1349
+ }
1350
+ }, {
1351
+ F: __dxlog_file8,
1352
+ L: 26
1353
+ });
1354
+ }
1355
+ async getHandlers() {
1356
+ return {
1357
+ AuthService: {
1358
+ authenticate: async ({ challenge }) => {
1359
+ try {
1360
+ const credential = await this._authParams.provider(challenge);
1361
+ if (!credential) {
1362
+ throw new Error("auth rejected");
1363
+ }
1364
+ return {
1365
+ credential
1366
+ };
1367
+ } catch (err) {
1368
+ import_log7.log.error("failed to generate auth credentials", err, {
1369
+ F: __dxlog_file8,
1370
+ L: 55,
1371
+ S: this,
1372
+ C: (f, a) => f(...a)
1373
+ });
1374
+ throw new Error("auth rejected");
1375
+ }
1376
+ }
1377
+ }
1378
+ };
1379
+ }
1380
+ async onOpen(context) {
1381
+ await super.onOpen(context);
1382
+ (0, import_async5.scheduleTask)(this._ctx, async () => {
1383
+ try {
1384
+ const challenge = (0, import_crypto2.randomBytes)(32);
1385
+ const { credential } = await this.rpc.AuthService.authenticate({
1386
+ challenge
1387
+ });
1388
+ (0, import_invariant7.invariant)(credential?.length > 0, "invalid credential", {
1389
+ F: __dxlog_file8,
1390
+ L: 69,
1391
+ S: this,
1392
+ A: [
1393
+ "credential?.length > 0",
1394
+ "'invalid credential'"
1395
+ ]
1396
+ });
1397
+ const success = await this._authParams.verifier(challenge, credential);
1398
+ (0, import_invariant7.invariant)(success, "credential not verified", {
1399
+ F: __dxlog_file8,
1400
+ L: 71,
1401
+ S: this,
1402
+ A: [
1403
+ "success",
1404
+ "'credential not verified'"
1405
+ ]
1406
+ });
1407
+ (0, import_async5.runInContext)(this._ctx, () => this._authParams.onAuthSuccess());
1408
+ } catch (err) {
1409
+ (0, import_log7.log)("auth failed", err, {
1410
+ F: __dxlog_file8,
1411
+ L: 74,
1412
+ S: this,
1413
+ C: (f, a) => f(...a)
1414
+ });
1415
+ this.close();
1416
+ this._authParams.onAuthFailure();
1417
+ }
1418
+ });
1419
+ }
1420
+ async onClose() {
1421
+ await this._ctx.dispose();
1422
+ await super.onClose();
1423
+ }
1424
+ async onAbort() {
1425
+ await this._ctx.dispose();
1426
+ await super.onAbort();
1427
+ }
1428
+ };
1429
+ function _ts_decorate4(decorators, target, key, desc) {
2123
1430
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2124
1431
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2125
1432
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2129,22 +1436,22 @@ function _ts_decorate5(decorators, target, key, desc) {
2129
1436
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2130
1437
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2131
1438
  }
2132
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/control-pipeline.ts";
1439
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/control-pipeline.ts";
2133
1440
  var TIMEFRAME_SAVE_DEBOUNCE_INTERVAL = 500;
2134
1441
  var CONTROL_PIPELINE_SNAPSHOT_DELAY = 1e4;
2135
1442
  var USE_SNAPSHOTS = true;
2136
1443
  var ControlPipeline = class {
2137
1444
  constructor({ spaceKey, genesisFeed, feedProvider, metadataStore }) {
2138
- this._ctx = new import_context10.Context(void 0, {
2139
- F: __dxlog_file10,
1445
+ this._ctx = new import_context7.Context(void 0, {
1446
+ F: __dxlog_file9,
2140
1447
  L: 47
2141
1448
  });
2142
1449
  this._lastTimeframeSaveTime = Date.now();
2143
- this.onFeedAdmitted = new import_util6.Callback();
2144
- this._usage = new import_tracing3.TimeUsageCounter();
2145
- this._mutations = new import_tracing3.TimeSeriesCounter();
2146
- this._snapshotTask = new import_async9.DeferredTask(this._ctx, async () => {
2147
- await (0, import_async9.sleepWithContext)(this._ctx, CONTROL_PIPELINE_SNAPSHOT_DELAY);
1450
+ this.onFeedAdmitted = new import_util4.Callback();
1451
+ this._usage = new import_tracing2.TimeUsageCounter();
1452
+ this._mutations = new import_tracing2.TimeSeriesCounter();
1453
+ this._snapshotTask = new import_async7.DeferredTask(this._ctx, async () => {
1454
+ await (0, import_async7.sleepWithContext)(this._ctx, CONTROL_PIPELINE_SNAPSHOT_DELAY);
2148
1455
  await this._saveSnapshot();
2149
1456
  });
2150
1457
  this._spaceKey = spaceKey;
@@ -2156,7 +1463,7 @@ var ControlPipeline = class {
2156
1463
  (0, import_log9.log)("feed admitted", {
2157
1464
  key: info.key
2158
1465
  }, {
2159
- F: __dxlog_file10,
1466
+ F: __dxlog_file9,
2160
1467
  L: 82,
2161
1468
  S: this,
2162
1469
  C: (f, a) => f(...a)
@@ -2170,7 +1477,7 @@ var ControlPipeline = class {
2170
1477
  }
2171
1478
  } catch (err) {
2172
1479
  import_log9.log.catch(err, void 0, {
2173
- F: __dxlog_file10,
1480
+ F: __dxlog_file9,
2174
1481
  L: 93,
2175
1482
  S: this,
2176
1483
  C: (f, a) => f(...a)
@@ -2202,7 +1509,7 @@ var ControlPipeline = class {
2202
1509
  present: !!snapshot,
2203
1510
  tf: snapshot?.timeframe
2204
1511
  }, {
2205
- F: __dxlog_file10,
1512
+ F: __dxlog_file9,
2206
1513
  L: 123,
2207
1514
  S: this,
2208
1515
  C: (f, a) => f(...a)
@@ -2211,20 +1518,20 @@ var ControlPipeline = class {
2211
1518
  await this._processSnapshot(snapshot);
2212
1519
  }
2213
1520
  (0, import_log9.log)("starting...", void 0, {
2214
- F: __dxlog_file10,
1521
+ F: __dxlog_file9,
2215
1522
  L: 128,
2216
1523
  S: this,
2217
1524
  C: (f, a) => f(...a)
2218
1525
  });
2219
1526
  setTimeout(async () => {
2220
- void this._consumePipeline(new import_context10.Context(void 0, {
2221
- F: __dxlog_file10,
1527
+ void this._consumePipeline(new import_context7.Context(void 0, {
1528
+ F: __dxlog_file9,
2222
1529
  L: 130
2223
1530
  }));
2224
1531
  });
2225
1532
  await this._pipeline.start();
2226
1533
  (0, import_log9.log)("started", void 0, {
2227
- F: __dxlog_file10,
1534
+ F: __dxlog_file9,
2228
1535
  L: 134,
2229
1536
  S: this,
2230
1537
  C: (f, a) => f(...a)
@@ -2241,7 +1548,7 @@ var ControlPipeline = class {
2241
1548
  import_log9.log.warn("credential processing failed from snapshot", {
2242
1549
  message
2243
1550
  }, {
2244
- F: __dxlog_file10,
1551
+ F: __dxlog_file9,
2245
1552
  L: 147,
2246
1553
  S: this,
2247
1554
  C: (f, a) => f(...a)
@@ -2263,7 +1570,7 @@ var ControlPipeline = class {
2263
1570
  key: this._spaceKey,
2264
1571
  snapshot
2265
1572
  }, {
2266
- F: __dxlog_file10,
1573
+ F: __dxlog_file9,
2267
1574
  L: 163,
2268
1575
  S: this,
2269
1576
  C: (f, a) => f(...a)
@@ -2278,7 +1585,7 @@ var ControlPipeline = class {
2278
1585
  await this._processMessage(ctx, msg);
2279
1586
  } catch (err) {
2280
1587
  import_log9.log.catch(err, void 0, {
2281
- F: __dxlog_file10,
1588
+ F: __dxlog_file9,
2282
1589
  L: 176,
2283
1590
  S: this,
2284
1591
  C: (f, a) => f(...a)
@@ -2292,22 +1599,22 @@ var ControlPipeline = class {
2292
1599
  key: msg.feedKey,
2293
1600
  seq: msg.seq
2294
1601
  }, {
2295
- F: __dxlog_file10,
1602
+ F: __dxlog_file9,
2296
1603
  L: 186,
2297
1604
  S: this,
2298
1605
  C: (f, a) => f(...a)
2299
1606
  });
2300
1607
  if (msg.data.payload.credential) {
2301
- const timer = import_util6.tracer.mark("dxos.echo.pipeline.control");
1608
+ const timer = import_util4.tracer.mark("dxos.echo.pipeline.control");
2302
1609
  const result = await this._spaceStateMachine.process(msg.data.payload.credential.credential, {
2303
- sourceFeed: import_keys6.PublicKey.from(msg.feedKey)
1610
+ sourceFeed: import_keys5.PublicKey.from(msg.feedKey)
2304
1611
  });
2305
1612
  timer.end();
2306
1613
  if (!result) {
2307
1614
  import_log9.log.warn("processing failed", {
2308
1615
  msg
2309
1616
  }, {
2310
- F: __dxlog_file10,
1617
+ F: __dxlog_file9,
2311
1618
  L: 195,
2312
1619
  S: this,
2313
1620
  C: (f, a) => f(...a)
@@ -2326,7 +1633,7 @@ var ControlPipeline = class {
2326
1633
  }
2327
1634
  async stop() {
2328
1635
  (0, import_log9.log)("stopping...", void 0, {
2329
- F: __dxlog_file10,
1636
+ F: __dxlog_file9,
2330
1637
  L: 215,
2331
1638
  S: this,
2332
1639
  C: (f, a) => f(...a)
@@ -2335,7 +1642,7 @@ var ControlPipeline = class {
2335
1642
  await this._pipeline.stop();
2336
1643
  await this._saveTargetTimeframe(this._pipeline.state.timeframe);
2337
1644
  (0, import_log9.log)("stopped", void 0, {
2338
- F: __dxlog_file10,
1645
+ F: __dxlog_file9,
2339
1646
  L: 219,
2340
1647
  S: this,
2341
1648
  C: (f, a) => f(...a)
@@ -2348,7 +1655,7 @@ var ControlPipeline = class {
2348
1655
  this._targetTimeframe = newTimeframe;
2349
1656
  } catch (err) {
2350
1657
  (0, import_log9.log)(err, void 0, {
2351
- F: __dxlog_file10,
1658
+ F: __dxlog_file9,
2352
1659
  L: 228,
2353
1660
  S: this,
2354
1661
  C: (f, a) => f(...a)
@@ -2356,28 +1663,28 @@ var ControlPipeline = class {
2356
1663
  }
2357
1664
  }
2358
1665
  };
2359
- _ts_decorate5([
2360
- import_tracing3.trace.metricsCounter()
1666
+ _ts_decorate4([
1667
+ import_tracing2.trace.metricsCounter()
2361
1668
  ], ControlPipeline.prototype, "_usage", void 0);
2362
- _ts_decorate5([
2363
- import_tracing3.trace.metricsCounter()
1669
+ _ts_decorate4([
1670
+ import_tracing2.trace.metricsCounter()
2364
1671
  ], ControlPipeline.prototype, "_mutations", void 0);
2365
- _ts_decorate5([
2366
- import_tracing3.trace.span({
1672
+ _ts_decorate4([
1673
+ import_tracing2.trace.span({
2367
1674
  showInBrowserTimeline: true
2368
1675
  })
2369
1676
  ], ControlPipeline.prototype, "start", null);
2370
- _ts_decorate5([
2371
- import_tracing3.trace.span()
1677
+ _ts_decorate4([
1678
+ import_tracing2.trace.span()
2372
1679
  ], ControlPipeline.prototype, "_consumePipeline", null);
2373
- _ts_decorate5([
2374
- import_tracing3.trace.span()
1680
+ _ts_decorate4([
1681
+ import_tracing2.trace.span()
2375
1682
  ], ControlPipeline.prototype, "_processMessage", null);
2376
- ControlPipeline = _ts_decorate5([
2377
- import_tracing3.trace.resource(),
2378
- (0, import_async9.trackLeaks)("start", "stop")
1683
+ ControlPipeline = _ts_decorate4([
1684
+ import_tracing2.trace.resource(),
1685
+ (0, import_async7.trackLeaks)("start", "stop")
2379
1686
  ], ControlPipeline);
2380
- function _ts_decorate6(decorators, target, key, desc) {
1687
+ function _ts_decorate5(decorators, target, key, desc) {
2381
1688
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2382
1689
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2383
1690
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2387,15 +1694,15 @@ function _ts_decorate6(decorators, target, key, desc) {
2387
1694
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2388
1695
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2389
1696
  }
2390
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/space.ts";
2391
- var Space = class extends import_context9.Resource {
1697
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/space.ts";
1698
+ var Space = class extends import_context6.Resource {
2392
1699
  constructor(params) {
2393
1700
  super();
2394
- this._addFeedMutex = new import_async8.Mutex();
2395
- this.onCredentialProcessed = new import_util5.Callback();
2396
- this.stateUpdate = new import_async8.Event();
2397
- (0, import_invariant9.invariant)(params.spaceKey && params.feedProvider, void 0, {
2398
- F: __dxlog_file11,
1701
+ this._addFeedMutex = new import_async6.Mutex();
1702
+ this.onCredentialProcessed = new import_util3.Callback();
1703
+ this.stateUpdate = new import_async6.Event();
1704
+ (0, import_invariant8.invariant)(params.spaceKey && params.feedProvider, void 0, {
1705
+ F: __dxlog_file10,
2399
1706
  L: 78,
2400
1707
  S: this,
2401
1708
  A: [
@@ -2429,7 +1736,7 @@ var Space = class extends import_context9.Resource {
2429
1736
  (0, import_log8.log)("onCredentialProcessed", {
2430
1737
  credential
2431
1738
  }, {
2432
- F: __dxlog_file11,
1739
+ F: __dxlog_file10,
2433
1740
  L: 106,
2434
1741
  S: this,
2435
1742
  C: (f, a) => f(...a)
@@ -2440,7 +1747,7 @@ var Space = class extends import_context9.Resource {
2440
1747
  (0, import_log8.log)("onDelegatedInvitation", {
2441
1748
  invitation
2442
1749
  }, {
2443
- F: __dxlog_file11,
1750
+ F: __dxlog_file10,
2444
1751
  L: 110,
2445
1752
  S: this,
2446
1753
  C: (f, a) => f(...a)
@@ -2451,7 +1758,7 @@ var Space = class extends import_context9.Resource {
2451
1758
  (0, import_log8.log)("onDelegatedInvitationRemoved", {
2452
1759
  invitation
2453
1760
  }, {
2454
- F: __dxlog_file11,
1761
+ F: __dxlog_file10,
2455
1762
  L: 114,
2456
1763
  S: this,
2457
1764
  C: (f, a) => f(...a)
@@ -2465,7 +1772,7 @@ var Space = class extends import_context9.Resource {
2465
1772
  m.role
2466
1773
  ])
2467
1774
  }), {
2468
- F: __dxlog_file11,
1775
+ F: __dxlog_file10,
2469
1776
  L: 118,
2470
1777
  S: this,
2471
1778
  C: (f, a) => f(...a)
@@ -2482,7 +1789,7 @@ var Space = class extends import_context9.Resource {
2482
1789
  return this._key;
2483
1790
  }
2484
1791
  get isOpen() {
2485
- return this._lifecycleState === import_context9.LifecycleState.OPEN;
1792
+ return this._lifecycleState === import_context6.LifecycleState.OPEN;
2486
1793
  }
2487
1794
  get genesisFeedKey() {
2488
1795
  return this._genesisFeedKey;
@@ -2506,8 +1813,8 @@ var Space = class extends import_context9.Resource {
2506
1813
  return this._snapshotManager;
2507
1814
  }
2508
1815
  async setControlFeed(feed) {
2509
- (0, import_invariant9.invariant)(!this._controlFeed, "Control feed already set.", {
2510
- F: __dxlog_file11,
1816
+ (0, import_invariant8.invariant)(!this._controlFeed, "Control feed already set.", {
1817
+ F: __dxlog_file10,
2511
1818
  L: 171,
2512
1819
  S: this,
2513
1820
  A: [
@@ -2520,8 +1827,8 @@ var Space = class extends import_context9.Resource {
2520
1827
  return this;
2521
1828
  }
2522
1829
  async setDataFeed(feed) {
2523
- (0, import_invariant9.invariant)(!this._dataFeed, "Data feed already set.", {
2524
- F: __dxlog_file11,
1830
+ (0, import_invariant8.invariant)(!this._dataFeed, "Data feed already set.", {
1831
+ F: __dxlog_file10,
2525
1832
  L: 178,
2526
1833
  S: this,
2527
1834
  A: [
@@ -2540,7 +1847,7 @@ var Space = class extends import_context9.Resource {
2540
1847
  }
2541
1848
  async _open(ctx) {
2542
1849
  (0, import_log8.log)("opening...", void 0, {
2543
- F: __dxlog_file11,
1850
+ F: __dxlog_file10,
2544
1851
  L: 192,
2545
1852
  S: this,
2546
1853
  C: (f, a) => f(...a)
@@ -2548,7 +1855,7 @@ var Space = class extends import_context9.Resource {
2548
1855
  await this._controlPipeline.start();
2549
1856
  await this.protocol.start();
2550
1857
  (0, import_log8.log)("opened", void 0, {
2551
- F: __dxlog_file11,
1858
+ F: __dxlog_file10,
2552
1859
  L: 198,
2553
1860
  S: this,
2554
1861
  C: (f, a) => f(...a)
@@ -2558,7 +1865,7 @@ var Space = class extends import_context9.Resource {
2558
1865
  (0, import_log8.log)("closing...", {
2559
1866
  key: this._key
2560
1867
  }, {
2561
- F: __dxlog_file11,
1868
+ F: __dxlog_file10,
2562
1869
  L: 203,
2563
1870
  S: this,
2564
1871
  C: (f, a) => f(...a)
@@ -2566,50 +1873,50 @@ var Space = class extends import_context9.Resource {
2566
1873
  await this.protocol.stop();
2567
1874
  await this._controlPipeline.stop();
2568
1875
  (0, import_log8.log)("closed", void 0, {
2569
- F: __dxlog_file11,
1876
+ F: __dxlog_file10,
2570
1877
  L: 209,
2571
1878
  S: this,
2572
1879
  C: (f, a) => f(...a)
2573
1880
  });
2574
1881
  }
2575
1882
  };
2576
- _ts_decorate6([
2577
- import_tracing2.trace.info()
1883
+ _ts_decorate5([
1884
+ import_tracing.trace.info()
2578
1885
  ], Space.prototype, "protocol", void 0);
2579
- _ts_decorate6([
2580
- import_tracing2.trace.info()
1886
+ _ts_decorate5([
1887
+ import_tracing.trace.info()
2581
1888
  ], Space.prototype, "_controlPipeline", void 0);
2582
- _ts_decorate6([
1889
+ _ts_decorate5([
2583
1890
  import_log8.logInfo,
2584
- import_tracing2.trace.info()
1891
+ import_tracing.trace.info()
2585
1892
  ], Space.prototype, "id", null);
2586
- _ts_decorate6([
1893
+ _ts_decorate5([
2587
1894
  import_log8.logInfo,
2588
- import_tracing2.trace.info()
1895
+ import_tracing.trace.info()
2589
1896
  ], Space.prototype, "key", null);
2590
- _ts_decorate6([
2591
- import_tracing2.trace.span()
1897
+ _ts_decorate5([
1898
+ import_tracing.trace.span()
2592
1899
  ], Space.prototype, "_open", null);
2593
- _ts_decorate6([
2594
- import_async8.synchronized
1900
+ _ts_decorate5([
1901
+ import_async6.synchronized
2595
1902
  ], Space.prototype, "_close", null);
2596
- Space = _ts_decorate6([
2597
- (0, import_async8.trackLeaks)("open", "close"),
2598
- import_tracing2.trace.resource()
1903
+ Space = _ts_decorate5([
1904
+ (0, import_async6.trackLeaks)("open", "close"),
1905
+ import_tracing.trace.resource()
2599
1906
  ], Space);
2600
- var SPACE_IDS_CACHE = new import_util5.ComplexMap(import_keys5.PublicKey.hash);
1907
+ var SPACE_IDS_CACHE = new import_util3.ComplexMap(import_keys4.PublicKey.hash);
2601
1908
  var createIdFromSpaceKey = async (spaceKey) => {
2602
1909
  const cachedValue = SPACE_IDS_CACHE.get(spaceKey);
2603
1910
  if (cachedValue !== void 0) {
2604
1911
  return cachedValue;
2605
1912
  }
2606
1913
  const digest = await import_crypto3.subtleCrypto.digest("SHA-256", spaceKey.asUint8Array());
2607
- const bytes = new Uint8Array(digest).slice(0, import_keys5.SpaceId.byteLength);
2608
- const spaceId = import_keys5.SpaceId.encode(bytes);
1914
+ const bytes = new Uint8Array(digest).slice(0, import_keys4.SpaceId.byteLength);
1915
+ const spaceId = import_keys4.SpaceId.encode(bytes);
2609
1916
  SPACE_IDS_CACHE.set(spaceKey, spaceId);
2610
1917
  return spaceId;
2611
1918
  };
2612
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/admission-discovery-extension.ts";
1919
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/admission-discovery-extension.ts";
2613
1920
  var CredentialRetrieverExtension = class extends import_teleport2.RpcExtension {
2614
1921
  constructor(_request, _onResult) {
2615
1922
  super({
@@ -2619,8 +1926,8 @@ var CredentialRetrieverExtension = class extends import_teleport2.RpcExtension {
2619
1926
  });
2620
1927
  this._request = _request;
2621
1928
  this._onResult = _onResult;
2622
- this._ctx = new import_context11.Context(void 0, {
2623
- F: __dxlog_file12,
1929
+ this._ctx = new import_context8.Context(void 0, {
1930
+ F: __dxlog_file11,
2624
1931
  L: 25
2625
1932
  });
2626
1933
  }
@@ -2629,7 +1936,7 @@ var CredentialRetrieverExtension = class extends import_teleport2.RpcExtension {
2629
1936
  }
2630
1937
  async onOpen(context) {
2631
1938
  await super.onOpen(context);
2632
- (0, import_async10.scheduleTask)(this._ctx, async () => {
1939
+ (0, import_async8.scheduleTask)(this._ctx, async () => {
2633
1940
  try {
2634
1941
  const result = await this.rpc.AdmissionDiscoveryService.getAdmissionCredential(this._request);
2635
1942
  this._onResult.wake(result.admissionCredential);
@@ -2670,7 +1977,7 @@ var CredentialServerExtension = class extends import_teleport2.RpcExtension {
2670
1977
  };
2671
1978
  }
2672
1979
  };
2673
- function _ts_decorate7(decorators, target, key, desc) {
1980
+ function _ts_decorate6(decorators, target, key, desc) {
2674
1981
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2675
1982
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2676
1983
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2680,13 +1987,13 @@ function _ts_decorate7(decorators, target, key, desc) {
2680
1987
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2681
1988
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2682
1989
  }
2683
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/space-protocol.ts";
1990
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/space-protocol.ts";
2684
1991
  var MOCK_AUTH_PROVIDER = async (nonce) => Buffer.from("mock");
2685
1992
  var MOCK_AUTH_VERIFIER = async (nonce, credential) => true;
2686
1993
  var SpaceProtocol = class {
2687
1994
  constructor({ topic, swarmIdentity, networkManager, onSessionAuth, onAuthFailure, blobStore }) {
2688
1995
  this._feeds = /* @__PURE__ */ new Set();
2689
- this._sessions = new import_util7.ComplexMap(import_keys7.PublicKey.hash);
1996
+ this._sessions = new import_util5.ComplexMap(import_keys6.PublicKey.hash);
2690
1997
  this._topology = new import_network_manager.MMSTTopology({
2691
1998
  originateConnections: 4,
2692
1999
  maxPeers: 10,
@@ -2700,7 +2007,7 @@ var SpaceProtocol = class {
2700
2007
  this.blobSync = new import_teleport_extension_object_sync.BlobSync({
2701
2008
  blobStore
2702
2009
  });
2703
- this._topic = import_crypto4.subtleCrypto.digest("SHA-256", topic.asBuffer()).then(import_crypto4.discoveryKey).then(import_keys7.PublicKey.from);
2010
+ this._topic = import_crypto4.subtleCrypto.digest("SHA-256", topic.asBuffer()).then(import_crypto4.discoveryKey).then(import_keys6.PublicKey.from);
2704
2011
  }
2705
2012
  get sessions() {
2706
2013
  return this._sessions;
@@ -2716,7 +2023,7 @@ var SpaceProtocol = class {
2716
2023
  (0, import_log10.log)("addFeed", {
2717
2024
  key: feed.key
2718
2025
  }, {
2719
- F: __dxlog_file13,
2026
+ F: __dxlog_file12,
2720
2027
  L: 109,
2721
2028
  S: this,
2722
2029
  C: (f, a) => f(...a)
@@ -2734,7 +2041,7 @@ var SpaceProtocol = class {
2734
2041
  const credentials = await this._swarmIdentity.credentialProvider(Buffer.from(""));
2735
2042
  await this.blobSync.open();
2736
2043
  (0, import_log10.log)("starting...", void 0, {
2737
- F: __dxlog_file13,
2044
+ F: __dxlog_file12,
2738
2045
  L: 128,
2739
2046
  S: this,
2740
2047
  C: (f, a) => f(...a)
@@ -2748,7 +2055,7 @@ var SpaceProtocol = class {
2748
2055
  label: `swarm ${topic.truncate()} for space ${this._spaceKey.truncate()}`
2749
2056
  });
2750
2057
  (0, import_log10.log)("started", void 0, {
2751
- F: __dxlog_file13,
2058
+ F: __dxlog_file12,
2752
2059
  L: 138,
2753
2060
  S: this,
2754
2061
  C: (f, a) => f(...a)
@@ -2761,14 +2068,14 @@ var SpaceProtocol = class {
2761
2068
  await this.blobSync.close();
2762
2069
  if (this._connection) {
2763
2070
  (0, import_log10.log)("stopping...", void 0, {
2764
- F: __dxlog_file13,
2071
+ F: __dxlog_file12,
2765
2072
  L: 149,
2766
2073
  S: this,
2767
2074
  C: (f, a) => f(...a)
2768
2075
  });
2769
2076
  await this._connection.close();
2770
2077
  (0, import_log10.log)("stopped", void 0, {
2771
- F: __dxlog_file13,
2078
+ F: __dxlog_file12,
2772
2079
  L: 151,
2773
2080
  S: this,
2774
2081
  C: (f, a) => f(...a)
@@ -2792,18 +2099,18 @@ var SpaceProtocol = class {
2792
2099
  };
2793
2100
  }
2794
2101
  };
2795
- _ts_decorate7([
2102
+ _ts_decorate6([
2796
2103
  import_log10.logInfo,
2797
- import_tracing4.trace.info()
2104
+ import_tracing3.trace.info()
2798
2105
  ], SpaceProtocol.prototype, "_topic", void 0);
2799
- _ts_decorate7([
2800
- import_tracing4.trace.info()
2106
+ _ts_decorate6([
2107
+ import_tracing3.trace.info()
2801
2108
  ], SpaceProtocol.prototype, "_spaceKey", void 0);
2802
- _ts_decorate7([
2109
+ _ts_decorate6([
2803
2110
  import_log10.logInfo
2804
2111
  ], SpaceProtocol.prototype, "_ownPeerKey", null);
2805
- SpaceProtocol = _ts_decorate7([
2806
- import_tracing4.trace.resource()
2112
+ SpaceProtocol = _ts_decorate6([
2113
+ import_tracing3.trace.resource()
2807
2114
  ], SpaceProtocol);
2808
2115
  var AuthStatus;
2809
2116
  (function(AuthStatus2) {
@@ -2841,7 +2148,7 @@ var SpaceProtocolSession = class {
2841
2148
  verifier: this._swarmIdentity.credentialAuthenticator,
2842
2149
  onAuthSuccess: () => {
2843
2150
  (0, import_log10.log)("Peer authenticated", void 0, {
2844
- F: __dxlog_file13,
2151
+ F: __dxlog_file12,
2845
2152
  L: 248,
2846
2153
  S: this,
2847
2154
  C: (f, a) => f(...a)
@@ -2859,7 +2166,7 @@ var SpaceProtocolSession = class {
2859
2166
  }
2860
2167
  async close() {
2861
2168
  (0, import_log10.log)("close", void 0, {
2862
- F: __dxlog_file13,
2169
+ F: __dxlog_file12,
2863
2170
  L: 264,
2864
2171
  S: this,
2865
2172
  C: (f, a) => f(...a)
@@ -2870,466 +2177,49 @@ var SpaceProtocolSession = class {
2870
2177
  await this._teleport.abort();
2871
2178
  }
2872
2179
  };
2873
- _ts_decorate7([
2180
+ _ts_decorate6([
2874
2181
  import_log10.logInfo
2875
2182
  ], SpaceProtocolSession.prototype, "_wireParams", void 0);
2876
- _ts_decorate7([
2183
+ _ts_decorate6([
2877
2184
  import_log10.logInfo
2878
2185
  ], SpaceProtocolSession.prototype, "authStatus", null);
2879
- var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
2880
- var DEFAULT_FACTORY = (params) => new import_teleport_extension_automerge_replicator.AutomergeReplicator(...params);
2881
- var MeshReplicatorConnection = class extends import_context12.Resource {
2882
- constructor(_params) {
2883
- super();
2884
- this._params = _params;
2885
- this.remoteDeviceKey = null;
2886
- this._remotePeerId = null;
2887
- this._isEnabled = false;
2888
- let readableStreamController;
2889
- this.readable = new ReadableStream({
2890
- start: (controller) => {
2891
- readableStreamController = controller;
2892
- this._ctx.onDispose(() => controller.close());
2893
- }
2894
- });
2895
- this.writable = new WritableStream({
2896
- write: async (message, controller) => {
2897
- (0, import_invariant12.invariant)(this._isEnabled, "Writing to a disabled connection", {
2898
- F: __dxlog_file14,
2899
- L: 48,
2900
- S: this,
2901
- A: [
2902
- "this._isEnabled",
2903
- "'Writing to a disabled connection'"
2904
- ]
2905
- });
2906
- try {
2907
- await this.replicatorExtension.sendSyncMessage({
2908
- payload: import_automerge_repo3.cbor.encode(message)
2909
- });
2910
- } catch (err) {
2911
- controller.error(err);
2912
- this._disconnectIfEnabled();
2913
- }
2914
- }
2915
- });
2916
- const createAutomergeReplicator = this._params.replicatorFactory ?? DEFAULT_FACTORY;
2917
- this.replicatorExtension = createAutomergeReplicator([
2918
- {
2919
- peerId: this._params.ownPeerId
2920
- },
2921
- {
2922
- onStartReplication: async (info, remotePeerId) => {
2923
- this.remoteDeviceKey = remotePeerId;
2924
- this._remotePeerId = info.id;
2925
- (0, import_log14.log)("onStartReplication", {
2926
- id: info.id,
2927
- thisPeerId: this.peerId,
2928
- remotePeerId: remotePeerId.toHex()
2929
- }, {
2930
- F: __dxlog_file14,
2931
- L: 82,
2932
- S: this,
2933
- C: (f, a) => f(...a)
2934
- });
2935
- this._params.onRemoteConnected();
2936
- },
2937
- onSyncMessage: async ({ payload }) => {
2938
- if (!this._isEnabled) {
2939
- return;
2940
- }
2941
- const message = import_automerge_repo3.cbor.decode(payload);
2942
- readableStreamController.enqueue(message);
2943
- },
2944
- onClose: async () => {
2945
- this._disconnectIfEnabled();
2946
- }
2947
- }
2948
- ]);
2949
- }
2950
- _disconnectIfEnabled() {
2951
- if (this._isEnabled) {
2952
- this._params.onRemoteDisconnected();
2953
- }
2186
+ function _ts_decorate7(decorators, target, key, desc) {
2187
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2188
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2189
+ r = Reflect.decorate(decorators, target, key, desc);
2190
+ else
2191
+ for (var i = decorators.length - 1; i >= 0; i--)
2192
+ if (d = decorators[i])
2193
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2194
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2195
+ }
2196
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/space-manager.ts";
2197
+ var SpaceManager = class {
2198
+ constructor({ feedStore, networkManager, metadataStore, snapshotStore, blobStore }) {
2199
+ this._spaces = new import_util6.ComplexMap(import_keys7.PublicKey.hash);
2200
+ this._instanceId = import_keys7.PublicKey.random().toHex();
2201
+ this._feedStore = feedStore;
2202
+ this._networkManager = networkManager;
2203
+ this._metadataStore = metadataStore;
2204
+ this._snapshotStore = snapshotStore;
2205
+ this._blobStore = blobStore;
2954
2206
  }
2955
- get peerId() {
2956
- (0, import_invariant12.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
2957
- F: __dxlog_file14,
2958
- L: 108,
2959
- S: this,
2960
- A: [
2961
- "this._remotePeerId != null",
2962
- "'Remote peer has not connected yet.'"
2963
- ]
2964
- });
2965
- return this._remotePeerId;
2207
+ // TODO(burdon): Remove.
2208
+ get spaces() {
2209
+ return this._spaces;
2966
2210
  }
2967
- async shouldAdvertise(params) {
2968
- return this._params.shouldAdvertise(params);
2211
+ async open() {
2969
2212
  }
2970
- shouldSyncCollection(params) {
2971
- return this._params.shouldSyncCollection(params);
2972
- }
2973
- /**
2974
- * Start exchanging messages with the remote peer.
2975
- * Call after the remote peer has connected.
2976
- */
2977
- enable() {
2978
- (0, import_invariant12.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
2979
- F: __dxlog_file14,
2980
- L: 125,
2981
- S: this,
2982
- A: [
2983
- "this._remotePeerId != null",
2984
- "'Remote peer has not connected yet.'"
2985
- ]
2986
- });
2987
- this._isEnabled = true;
2988
- }
2989
- /**
2990
- * Stop exchanging messages with the remote peer.
2991
- */
2992
- disable() {
2993
- this._isEnabled = false;
2994
- }
2995
- };
2996
- var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
2997
- var MeshEchoReplicator = class {
2998
- constructor() {
2999
- this._connections = /* @__PURE__ */ new Set();
3000
- this._connectionsPerPeer = /* @__PURE__ */ new Map();
3001
- this._authorizedDevices = /* @__PURE__ */ new Map();
3002
- this._context = null;
3003
- }
3004
- async connect(context) {
3005
- this._context = context;
3006
- }
3007
- async disconnect() {
3008
- for (const connection of this._connectionsPerPeer.values()) {
3009
- this._context?.onConnectionClosed(connection);
3010
- }
3011
- for (const connection of this._connections) {
3012
- await connection.close();
3013
- }
3014
- this._connections.clear();
3015
- this._connectionsPerPeer.clear();
3016
- this._context = null;
3017
- }
3018
- createExtension(extensionFactory) {
3019
- (0, import_invariant11.invariant)(this._context, void 0, {
3020
- F: __dxlog_file15,
3021
- L: 56,
3022
- S: this,
3023
- A: [
3024
- "this._context",
3025
- ""
3026
- ]
3027
- });
3028
- const connection = new MeshReplicatorConnection({
3029
- ownPeerId: this._context.peerId,
3030
- replicatorFactory: extensionFactory,
3031
- onRemoteConnected: async () => {
3032
- (0, import_log13.log)("onRemoteConnected", {
3033
- peerId: connection.peerId
3034
- }, {
3035
- F: __dxlog_file15,
3036
- L: 62,
3037
- S: this,
3038
- C: (f, a) => f(...a)
3039
- });
3040
- (0, import_invariant11.invariant)(this._context, void 0, {
3041
- F: __dxlog_file15,
3042
- L: 63,
3043
- S: this,
3044
- A: [
3045
- "this._context",
3046
- ""
3047
- ]
3048
- });
3049
- if (this._connectionsPerPeer.has(connection.peerId)) {
3050
- this._context.onConnectionAuthScopeChanged(connection);
3051
- } else {
3052
- this._connectionsPerPeer.set(connection.peerId, connection);
3053
- this._context.onConnectionOpen(connection);
3054
- connection.enable();
3055
- }
3056
- },
3057
- onRemoteDisconnected: async () => {
3058
- (0, import_log13.log)("onRemoteDisconnected", {
3059
- peerId: connection.peerId
3060
- }, {
3061
- F: __dxlog_file15,
3062
- L: 74,
3063
- S: this,
3064
- C: (f, a) => f(...a)
3065
- });
3066
- this._context?.onConnectionClosed(connection);
3067
- this._connectionsPerPeer.delete(connection.peerId);
3068
- connection.disable();
3069
- this._connections.delete(connection);
3070
- },
3071
- shouldAdvertise: async (params) => {
3072
- (0, import_log13.log)("shouldAdvertise", {
3073
- peerId: connection.peerId,
3074
- documentId: params.documentId
3075
- }, {
3076
- F: __dxlog_file15,
3077
- L: 81,
3078
- S: this,
3079
- C: (f, a) => f(...a)
3080
- });
3081
- (0, import_invariant11.invariant)(this._context, void 0, {
3082
- F: __dxlog_file15,
3083
- L: 82,
3084
- S: this,
3085
- A: [
3086
- "this._context",
3087
- ""
3088
- ]
3089
- });
3090
- try {
3091
- const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
3092
- if (!spaceKey) {
3093
- (0, import_log13.log)("space key not found for share policy check", {
3094
- peerId: connection.peerId,
3095
- documentId: params.documentId
3096
- }, {
3097
- F: __dxlog_file15,
3098
- L: 86,
3099
- S: this,
3100
- C: (f, a) => f(...a)
3101
- });
3102
- return false;
3103
- }
3104
- const spaceId = await createIdFromSpaceKey(spaceKey);
3105
- const authorizedDevices = this._authorizedDevices.get(spaceId);
3106
- if (!connection.remoteDeviceKey) {
3107
- (0, import_log13.log)("device key not found for share policy check", {
3108
- peerId: connection.peerId,
3109
- documentId: params.documentId
3110
- }, {
3111
- F: __dxlog_file15,
3112
- L: 98,
3113
- S: this,
3114
- C: (f, a) => f(...a)
3115
- });
3116
- return false;
3117
- }
3118
- const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
3119
- (0, import_log13.log)("share policy check", {
3120
- localPeer: this._context.peerId,
3121
- remotePeer: connection.peerId,
3122
- documentId: params.documentId,
3123
- deviceKey: connection.remoteDeviceKey,
3124
- spaceKey,
3125
- isAuthorized
3126
- }, {
3127
- F: __dxlog_file15,
3128
- L: 106,
3129
- S: this,
3130
- C: (f, a) => f(...a)
3131
- });
3132
- return isAuthorized;
3133
- } catch (err) {
3134
- import_log13.log.catch(err, void 0, {
3135
- F: __dxlog_file15,
3136
- L: 116,
3137
- S: this,
3138
- C: (f, a) => f(...a)
3139
- });
3140
- return false;
3141
- }
3142
- },
3143
- shouldSyncCollection: ({ collectionId }) => {
3144
- const spaceId = getSpaceIdFromCollectionId(collectionId);
3145
- const authorizedDevices = this._authorizedDevices.get(spaceId);
3146
- if (!connection.remoteDeviceKey) {
3147
- (0, import_log13.log)("device key not found for collection sync check", {
3148
- peerId: connection.peerId,
3149
- collectionId
3150
- }, {
3151
- F: __dxlog_file15,
3152
- L: 126,
3153
- S: this,
3154
- C: (f, a) => f(...a)
3155
- });
3156
- return false;
3157
- }
3158
- const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
3159
- return isAuthorized;
3160
- }
3161
- });
3162
- this._connections.add(connection);
3163
- return connection.replicatorExtension;
3164
- }
3165
- async authorizeDevice(spaceKey, deviceKey) {
3166
- (0, import_log13.log)("authorizeDevice", {
3167
- spaceKey,
3168
- deviceKey
3169
- }, {
3170
- F: __dxlog_file15,
3171
- L: 143,
3172
- S: this,
3173
- C: (f, a) => f(...a)
3174
- });
3175
- const spaceId = await createIdFromSpaceKey(spaceKey);
3176
- (0, import_util9.defaultMap)(this._authorizedDevices, spaceId, () => new import_util9.ComplexSet(import_keys10.PublicKey.hash)).add(deviceKey);
3177
- for (const connection of this._connections) {
3178
- if (connection.remoteDeviceKey && connection.remoteDeviceKey.equals(deviceKey)) {
3179
- if (this._connectionsPerPeer.has(connection.peerId)) {
3180
- this._context?.onConnectionAuthScopeChanged(connection);
3181
- }
3182
- }
3183
- }
3184
- }
3185
- };
3186
- var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service.ts";
3187
- var DataServiceImpl = class {
3188
- constructor(params) {
3189
- this._subscriptions = /* @__PURE__ */ new Map();
3190
- this._automergeHost = params.automergeHost;
3191
- this._updateIndexes = params.updateIndexes;
3192
- }
3193
- subscribe(request) {
3194
- return new import_codec_protobuf.Stream(({ next, ready }) => {
3195
- const synchronizer = new DocumentsSynchronizer({
3196
- repo: this._automergeHost.repo,
3197
- sendUpdates: (updates) => next(updates)
3198
- });
3199
- synchronizer.open().then(() => {
3200
- this._subscriptions.set(request.subscriptionId, synchronizer);
3201
- ready();
3202
- }).catch((err) => import_log12.log.catch(err, void 0, {
3203
- F: __dxlog_file16,
3204
- L: 64,
3205
- S: this,
3206
- C: (f, a) => f(...a)
3207
- }));
3208
- return () => synchronizer.close();
3209
- });
3210
- }
3211
- async updateSubscription(request) {
3212
- const synchronizer = this._subscriptions.get(request.subscriptionId);
3213
- (0, import_invariant10.invariant)(synchronizer, "Subscription not found", {
3214
- F: __dxlog_file16,
3215
- L: 71,
3216
- S: this,
3217
- A: [
3218
- "synchronizer",
3219
- "'Subscription not found'"
3220
- ]
3221
- });
3222
- if (request.addIds?.length) {
3223
- await synchronizer.addDocuments(request.addIds);
3224
- }
3225
- if (request.removeIds?.length) {
3226
- await synchronizer.removeDocuments(request.removeIds);
3227
- }
3228
- }
3229
- async update(request) {
3230
- if (!request.updates) {
3231
- return;
3232
- }
3233
- const synchronizer = this._subscriptions.get(request.subscriptionId);
3234
- (0, import_invariant10.invariant)(synchronizer, "Subscription not found", {
3235
- F: __dxlog_file16,
3236
- L: 86,
3237
- S: this,
3238
- A: [
3239
- "synchronizer",
3240
- "'Subscription not found'"
3241
- ]
3242
- });
3243
- synchronizer.update(request.updates);
3244
- }
3245
- async flush(request) {
3246
- await this._automergeHost.flush(request);
3247
- }
3248
- async getDocumentHeads(request) {
3249
- const documentIds = request.documentIds;
3250
- if (!documentIds) {
3251
- return {
3252
- heads: {
3253
- entries: []
3254
- }
3255
- };
3256
- }
3257
- const heads = await this._automergeHost.getHeads(documentIds);
3258
- return {
3259
- heads: {
3260
- entries: heads.map((heads2, idx) => ({
3261
- documentId: documentIds[idx],
3262
- heads: heads2
3263
- }))
3264
- }
3265
- };
3266
- }
3267
- async waitUntilHeadsReplicated(request, options) {
3268
- await this._automergeHost.waitUntilHeadsReplicated(request.heads);
3269
- }
3270
- async reIndexHeads(request, options) {
3271
- await this._automergeHost.reIndexHeads(request.documentIds ?? []);
3272
- }
3273
- async updateIndexes() {
3274
- await this._updateIndexes();
3275
- }
3276
- async getSpaceSyncState(request, options) {
3277
- (0, import_invariant10.invariant)(import_keys9.SpaceId.isValid(request.spaceId), void 0, {
3278
- F: __dxlog_file16,
3279
- L: 127,
3280
- S: this,
3281
- A: [
3282
- "SpaceId.isValid(request.spaceId)",
3283
- ""
3284
- ]
3285
- });
3286
- const collectionId = deriveCollectionIdFromSpaceId(request.spaceId);
3287
- const state = await this._automergeHost.getCollectionSyncState(collectionId);
3288
- return {
3289
- peers: state.peers.map((peer) => ({
3290
- peerId: peer.peerId,
3291
- documentsToReconcile: peer.differentDocuments
3292
- }))
3293
- };
3294
- }
3295
- };
3296
- function _ts_decorate8(decorators, target, key, desc) {
3297
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3298
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
3299
- r = Reflect.decorate(decorators, target, key, desc);
3300
- else
3301
- for (var i = decorators.length - 1; i >= 0; i--)
3302
- if (d = decorators[i])
3303
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3304
- return c > 3 && r && Object.defineProperty(target, key, r), r;
3305
- }
3306
- var __dxlog_file17 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/space-manager.ts";
3307
- var SpaceManager = class {
3308
- constructor({ feedStore, networkManager, metadataStore, snapshotStore, blobStore }) {
3309
- this._spaces = new import_util8.ComplexMap(import_keys8.PublicKey.hash);
3310
- this._instanceId = import_keys8.PublicKey.random().toHex();
3311
- this._feedStore = feedStore;
3312
- this._networkManager = networkManager;
3313
- this._metadataStore = metadataStore;
3314
- this._snapshotStore = snapshotStore;
3315
- this._blobStore = blobStore;
3316
- }
3317
- // TODO(burdon): Remove.
3318
- get spaces() {
3319
- return this._spaces;
3320
- }
3321
- async open() {
3322
- }
3323
- async close() {
3324
- await Promise.all([
3325
- ...this._spaces.values()
3326
- ].map((space) => space.close()));
2213
+ async close() {
2214
+ await Promise.all([
2215
+ ...this._spaces.values()
2216
+ ].map((space) => space.close()));
3327
2217
  }
3328
2218
  async constructSpace({ metadata, swarmIdentity, onAuthorizedConnection, onAuthFailure, onDelegatedInvitationStatusChange, onMemberRolesChanged, memberKey }) {
3329
2219
  import_log11.log.trace("dxos.echo.space-manager.construct-space", import_protocols7.trace.begin({
3330
2220
  id: this._instanceId
3331
2221
  }), {
3332
- F: __dxlog_file17,
2222
+ F: __dxlog_file13,
3333
2223
  L: 103,
3334
2224
  S: this,
3335
2225
  C: (f, a) => f(...a)
@@ -3337,7 +2227,7 @@ var SpaceManager = class {
3337
2227
  (0, import_log11.log)("constructing space...", {
3338
2228
  spaceKey: metadata.genesisFeedKey
3339
2229
  }, {
3340
- F: __dxlog_file17,
2230
+ F: __dxlog_file13,
3341
2231
  L: 104,
3342
2232
  S: this,
3343
2233
  C: (f, a) => f(...a)
@@ -3370,7 +2260,7 @@ var SpaceManager = class {
3370
2260
  import_log11.log.trace("dxos.echo.space-manager.construct-space", import_protocols7.trace.end({
3371
2261
  id: this._instanceId
3372
2262
  }), {
3373
- F: __dxlog_file17,
2263
+ F: __dxlog_file13,
3374
2264
  L: 135,
3375
2265
  S: this,
3376
2266
  C: (f, a) => f(...a)
@@ -3382,7 +2272,7 @@ var SpaceManager = class {
3382
2272
  import_log11.log.trace(traceKey, import_protocols7.trace.begin({
3383
2273
  id: this._instanceId
3384
2274
  }), {
3385
- F: __dxlog_file17,
2275
+ F: __dxlog_file13,
3386
2276
  L: 141,
3387
2277
  S: this,
3388
2278
  C: (f, a) => f(...a)
@@ -3390,12 +2280,12 @@ var SpaceManager = class {
3390
2280
  (0, import_log11.log)("requesting space admission credential...", {
3391
2281
  spaceKey: params.spaceKey
3392
2282
  }, {
3393
- F: __dxlog_file17,
2283
+ F: __dxlog_file13,
3394
2284
  L: 142,
3395
2285
  S: this,
3396
2286
  C: (f, a) => f(...a)
3397
2287
  });
3398
- const onCredentialResolved = new import_async11.Trigger();
2288
+ const onCredentialResolved = new import_async9.Trigger();
3399
2289
  const protocol = new SpaceProtocol({
3400
2290
  topic: params.spaceKey,
3401
2291
  swarmIdentity: params.swarmIdentity,
@@ -3417,7 +2307,7 @@ var SpaceManager = class {
3417
2307
  import_log11.log.trace(traceKey, import_protocols7.trace.end({
3418
2308
  id: this._instanceId
3419
2309
  }), {
3420
- F: __dxlog_file17,
2310
+ F: __dxlog_file13,
3421
2311
  L: 165,
3422
2312
  S: this,
3423
2313
  C: (f, a) => f(...a)
@@ -3428,7 +2318,7 @@ var SpaceManager = class {
3428
2318
  id: this._instanceId,
3429
2319
  error: err
3430
2320
  }), {
3431
- F: __dxlog_file17,
2321
+ F: __dxlog_file13,
3432
2322
  L: 168,
3433
2323
  S: this,
3434
2324
  C: (f, a) => f(...a)
@@ -3439,360 +2329,25 @@ var SpaceManager = class {
3439
2329
  }
3440
2330
  }
3441
2331
  };
3442
- _ts_decorate8([
3443
- import_async11.synchronized
2332
+ _ts_decorate7([
2333
+ import_async9.synchronized
3444
2334
  ], SpaceManager.prototype, "open", null);
3445
- _ts_decorate8([
3446
- import_async11.synchronized
2335
+ _ts_decorate7([
2336
+ import_async9.synchronized
3447
2337
  ], SpaceManager.prototype, "close", null);
3448
- SpaceManager = _ts_decorate8([
3449
- (0, import_async11.trackLeaks)("open", "close")
2338
+ SpaceManager = _ts_decorate7([
2339
+ (0, import_async9.trackLeaks)("open", "close")
3450
2340
  ], SpaceManager);
3451
- function _ts_decorate9(decorators, target, key, desc) {
3452
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3453
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
3454
- r = Reflect.decorate(decorators, target, key, desc);
3455
- else
3456
- for (var i = decorators.length - 1; i >= 0; i--)
3457
- if (d = decorators[i])
3458
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3459
- return c > 3 && r && Object.defineProperty(target, key, r), r;
3460
- }
3461
- var __dxlog_file18 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/metadata/metadata-store.ts";
3462
- var EXPIRED_INVITATION_CLEANUP_INTERVAL = 60 * 60 * 1e3;
3463
- var emptyEchoMetadata = () => ({
3464
- version: import_protocols8.STORAGE_VERSION,
3465
- spaces: [],
3466
- created: /* @__PURE__ */ new Date(),
3467
- updated: /* @__PURE__ */ new Date()
3468
- });
3469
- var emptyLargeSpaceMetadata = () => ({});
3470
- var EchoMetadata = import_protocols8.schema.getCodecForType("dxos.echo.metadata.EchoMetadata");
3471
- var LargeSpaceMetadata = import_protocols8.schema.getCodecForType("dxos.echo.metadata.LargeSpaceMetadata");
3472
- var MetadataStore = class {
3473
- constructor(directory) {
3474
- this._metadata = emptyEchoMetadata();
3475
- this._spaceLargeMetadata = new import_util10.ComplexMap(import_keys11.PublicKey.hash);
3476
- this._metadataFile = void 0;
3477
- this.update = new import_async12.Event();
3478
- this._invitationCleanupCtx = new import_context13.Context(void 0, {
3479
- F: __dxlog_file18,
3480
- L: 53
3481
- });
3482
- this._directory = directory;
3483
- }
3484
- get metadata() {
3485
- return this._metadata;
3486
- }
3487
- get version() {
3488
- return this._metadata.version ?? 0;
3489
- }
3490
- /**
3491
- * Returns a list of currently saved spaces. The list and objects in it can be modified addSpace and
3492
- * addSpaceFeed functions.
3493
- */
3494
- get spaces() {
3495
- return this._metadata.spaces ?? [];
3496
- }
3497
- async _readFile(file, codec2) {
3498
- try {
3499
- const { size: fileLength } = await file.stat();
3500
- if (fileLength < 8) {
3501
- return;
3502
- }
3503
- const dataSize = fromBytesInt32(await file.read(0, 4));
3504
- const checksum = fromBytesInt32(await file.read(4, 4));
3505
- (0, import_log15.log)("loaded", {
3506
- size: dataSize,
3507
- checksum,
3508
- name: file.filename
3509
- }, {
3510
- F: __dxlog_file18,
3511
- L: 89,
3512
- S: this,
3513
- C: (f, a) => f(...a)
3514
- });
3515
- if (fileLength < dataSize + 8) {
3516
- throw new import_protocols8.DataCorruptionError("Metadata size is smaller than expected.", {
3517
- fileLength,
3518
- dataSize
3519
- });
3520
- }
3521
- const data = await file.read(8, dataSize);
3522
- const calculatedChecksum = import_crc_32.default.buf(data);
3523
- if (calculatedChecksum !== checksum) {
3524
- throw new import_protocols8.DataCorruptionError("Metadata checksum is invalid.");
3525
- }
3526
- return codec2.decode(data);
3527
- } finally {
3528
- await file.close();
3529
- }
3530
- }
3531
- /**
3532
- * @internal
3533
- */
3534
- async _writeFile(file, codec2, data) {
3535
- const encoded = (0, import_util10.arrayToBuffer)(codec2.encode(data));
3536
- const checksum = import_crc_32.default.buf(encoded);
3537
- const result = Buffer.alloc(8 + encoded.length);
3538
- result.writeInt32LE(encoded.length, 0);
3539
- result.writeInt32LE(checksum, 4);
3540
- encoded.copy(result, 8);
3541
- await file.write(0, result);
3542
- (0, import_log15.log)("saved", {
3543
- size: encoded.length,
3544
- checksum
3545
- }, {
3546
- F: __dxlog_file18,
3547
- L: 124,
3548
- S: this,
3549
- C: (f, a) => f(...a)
3550
- });
3551
- }
3552
- async close() {
3553
- await this._invitationCleanupCtx.dispose();
3554
- await this.flush();
3555
- await this._metadataFile?.close();
3556
- this._metadataFile = void 0;
3557
- this._metadata = emptyEchoMetadata();
3558
- this._spaceLargeMetadata.clear();
3559
- }
3560
- /**
3561
- * Loads metadata from persistent storage.
3562
- */
3563
- async load() {
3564
- if (!this._metadataFile || this._metadataFile.closed) {
3565
- this._metadataFile = this._directory.getOrCreateFile("EchoMetadata");
3566
- }
3567
- try {
3568
- const metadata = await this._readFile(this._metadataFile, EchoMetadata);
3569
- if (metadata) {
3570
- this._metadata = metadata;
3571
- }
3572
- this._metadata.spaces?.forEach((space) => {
3573
- space.state ??= import_services.SpaceState.ACTIVE;
3574
- });
3575
- } catch (err) {
3576
- import_log15.log.error("failed to load metadata", {
3577
- err
3578
- }, {
3579
- F: __dxlog_file18,
3580
- L: 156,
3581
- S: this,
3582
- C: (f, a) => f(...a)
3583
- });
3584
- this._metadata = emptyEchoMetadata();
3585
- }
3586
- await (0, import_util10.forEachAsync)([
3587
- this._metadata.identity?.haloSpace.key,
3588
- ...this._metadata.spaces?.map((space) => space.key) ?? []
3589
- ].filter(import_util10.isNotNullOrUndefined), async (key) => {
3590
- try {
3591
- await this._loadSpaceLargeMetadata(key);
3592
- } catch (err) {
3593
- import_log15.log.error("failed to load space large metadata", {
3594
- err
3595
- }, {
3596
- F: __dxlog_file18,
3597
- L: 168,
3598
- S: this,
3599
- C: (f, a) => f(...a)
3600
- });
3601
- }
3602
- });
3603
- (0, import_async12.scheduleTaskInterval)(this._invitationCleanupCtx, async () => {
3604
- for (const invitation of this._metadata.invitations ?? []) {
3605
- if (hasInvitationExpired(invitation) || isLegacyInvitationFormat(invitation)) {
3606
- await this.removeInvitation(invitation.invitationId);
3607
- }
3608
- }
3609
- }, EXPIRED_INVITATION_CLEANUP_INTERVAL);
3610
- }
3611
- async _save() {
3612
- const data = {
3613
- ...this._metadata,
3614
- version: import_protocols8.STORAGE_VERSION,
3615
- created: this._metadata.created ?? /* @__PURE__ */ new Date(),
3616
- updated: /* @__PURE__ */ new Date()
3617
- };
3618
- this.update.emit(data);
3619
- const file = this._directory.getOrCreateFile("EchoMetadata");
3620
- await this._writeFile(file, EchoMetadata, data);
3621
- }
3622
- async _loadSpaceLargeMetadata(key) {
3623
- const file = this._directory.getOrCreateFile(`space_${key.toHex()}_large`);
3624
- try {
3625
- const metadata = await this._readFile(file, LargeSpaceMetadata);
3626
- if (metadata) {
3627
- this._spaceLargeMetadata.set(key, metadata);
3628
- }
3629
- } catch (err) {
3630
- import_log15.log.error("failed to load space large metadata", {
3631
- err
3632
- }, {
3633
- F: __dxlog_file18,
3634
- L: 210,
3635
- S: this,
3636
- C: (f, a) => f(...a)
3637
- });
3638
- }
3639
- }
3640
- async _saveSpaceLargeMetadata(key) {
3641
- const data = this._getLargeSpaceMetadata(key);
3642
- const file = this._directory.getOrCreateFile(`space_${key.toHex()}_large`);
3643
- await this._writeFile(file, LargeSpaceMetadata, data);
3644
- }
3645
- async flush() {
3646
- await this._directory.flush();
3647
- }
3648
- _getSpace(spaceKey) {
3649
- if (this._metadata.identity?.haloSpace.key.equals(spaceKey)) {
3650
- return this._metadata.identity.haloSpace;
3651
- }
3652
- const space = this.spaces.find((space2) => space2.key === spaceKey);
3653
- (0, import_invariant13.invariant)(space, "Space not found", {
3654
- F: __dxlog_file18,
3655
- L: 232,
3656
- S: this,
3657
- A: [
3658
- "space",
3659
- "'Space not found'"
3660
- ]
3661
- });
3662
- return space;
3663
- }
3664
- _getLargeSpaceMetadata(key) {
3665
- let entry = this._spaceLargeMetadata.get(key);
3666
- if (entry) {
3667
- return entry;
3668
- }
3669
- entry = emptyLargeSpaceMetadata();
3670
- this._spaceLargeMetadata.set(key, entry);
3671
- return entry;
3672
- }
3673
- /**
3674
- * Clears storage - doesn't work for now.
3675
- */
3676
- async clear() {
3677
- (0, import_log15.log)("clearing all metadata", void 0, {
3678
- F: __dxlog_file18,
3679
- L: 251,
3680
- S: this,
3681
- C: (f, a) => f(...a)
3682
- });
3683
- await this._directory.delete();
3684
- this._metadata = emptyEchoMetadata();
3685
- }
3686
- getIdentityRecord() {
3687
- return this._metadata.identity;
3688
- }
3689
- async setIdentityRecord(record) {
3690
- (0, import_invariant13.invariant)(!this._metadata.identity, "Cannot overwrite existing identity in metadata", {
3691
- F: __dxlog_file18,
3692
- L: 261,
3693
- S: this,
3694
- A: [
3695
- "!this._metadata.identity",
3696
- "'Cannot overwrite existing identity in metadata'"
3697
- ]
3698
- });
3699
- this._metadata.identity = record;
3700
- await this._save();
3701
- await this.flush();
3702
- }
3703
- getInvitations() {
3704
- return this._metadata.invitations ?? [];
3705
- }
3706
- async addInvitation(invitation) {
3707
- if (this._metadata.invitations?.find((i) => i.invitationId === invitation.invitationId)) {
3708
- return;
3709
- }
3710
- (this._metadata.invitations ??= []).push(invitation);
3711
- await this._save();
3712
- await this.flush();
3713
- }
3714
- async removeInvitation(invitationId) {
3715
- this._metadata.invitations = (this._metadata.invitations ?? []).filter((i) => i.invitationId !== invitationId);
3716
- await this._save();
3717
- await this.flush();
3718
- }
3719
- async addSpace(record) {
3720
- (0, import_invariant13.invariant)(!(this._metadata.spaces ?? []).find((space) => space.key === record.key), "Cannot overwrite existing space in metadata", {
3721
- F: __dxlog_file18,
3722
- L: 289,
3723
- S: this,
3724
- A: [
3725
- "!(this._metadata.spaces ?? []).find((space) => space.key === record.key)",
3726
- "'Cannot overwrite existing space in metadata'"
3727
- ]
3728
- });
3729
- (this._metadata.spaces ??= []).push(record);
3730
- await this._save();
3731
- await this.flush();
3732
- }
3733
- async setSpaceDataLatestTimeframe(spaceKey, timeframe) {
3734
- this._getSpace(spaceKey).dataTimeframe = timeframe;
3735
- await this._save();
3736
- }
3737
- async setSpaceControlLatestTimeframe(spaceKey, timeframe) {
3738
- this._getSpace(spaceKey).controlTimeframe = timeframe;
3739
- await this._save();
3740
- await this.flush();
3741
- }
3742
- async setCache(spaceKey, cache) {
3743
- this._getSpace(spaceKey).cache = cache;
3744
- await this._save();
3745
- }
3746
- async setWritableFeedKeys(spaceKey, controlFeedKey, dataFeedKey) {
3747
- const space = this._getSpace(spaceKey);
3748
- space.controlFeedKey = controlFeedKey;
3749
- space.dataFeedKey = dataFeedKey;
3750
- await this._save();
3751
- await this.flush();
3752
- }
3753
- async setSpaceState(spaceKey, state) {
3754
- this._getSpace(spaceKey).state = state;
3755
- await this._save();
3756
- await this.flush();
3757
- }
3758
- getSpaceControlPipelineSnapshot(spaceKey) {
3759
- return this._getLargeSpaceMetadata(spaceKey).controlPipelineSnapshot;
3760
- }
3761
- async setSpaceControlPipelineSnapshot(spaceKey, snapshot) {
3762
- this._getLargeSpaceMetadata(spaceKey).controlPipelineSnapshot = snapshot;
3763
- await this._saveSpaceLargeMetadata(spaceKey);
3764
- await this.flush();
3765
- }
3766
- };
3767
- _ts_decorate9([
3768
- import_async12.synchronized
3769
- ], MetadataStore.prototype, "load", null);
3770
- _ts_decorate9([
3771
- import_async12.synchronized
3772
- ], MetadataStore.prototype, "_save", null);
3773
- _ts_decorate9([
3774
- import_async12.synchronized
3775
- ], MetadataStore.prototype, "_saveSpaceLargeMetadata", null);
3776
- var fromBytesInt32 = (buf) => buf.readInt32LE(0);
3777
- var hasInvitationExpired = (invitation) => {
3778
- return Boolean(invitation.created && invitation.lifetime && invitation.lifetime !== 0 && invitation.created.getTime() + invitation.lifetime * 1e3 < Date.now());
3779
- };
3780
- var isLegacyInvitationFormat = (invitation) => {
3781
- return invitation.type === import_services.Invitation.Type.MULTIUSE;
3782
- };
3783
2341
  // Annotate the CommonJS export names for ESM import in node:
3784
2342
  0 && (module.exports = {
3785
2343
  AuthExtension,
3786
2344
  AuthStatus,
3787
- AutomergeHost,
3788
2345
  CredentialRetrieverExtension,
3789
2346
  CredentialServerExtension,
3790
2347
  DataServiceImpl,
3791
2348
  DocumentsSynchronizer,
3792
- LevelDBStorageAdapter,
3793
2349
  MOCK_AUTH_PROVIDER,
3794
2350
  MOCK_AUTH_VERIFIER,
3795
- MeshEchoReplicator,
3796
2351
  MetadataStore,
3797
2352
  Pipeline,
3798
2353
  SnapshotManager,
@@ -3805,15 +2360,10 @@ var isLegacyInvitationFormat = (invitation) => {
3805
2360
  codec,
3806
2361
  createIdFromSpaceKey,
3807
2362
  createMappedFeedWriter,
3808
- deriveCollectionIdFromSpaceId,
3809
- diffCollectionState,
3810
- encodingOptions,
3811
- getSpaceIdFromCollectionId,
3812
- getSpaceKeyFromDoc,
3813
2363
  hasInvitationExpired,
3814
2364
  mapFeedIndexesToTimeframe,
3815
2365
  mapTimeframeToFeedIndexes,
3816
2366
  startAfter,
3817
2367
  valueEncoding
3818
2368
  });
3819
- //# sourceMappingURL=chunk-RLTLWNAY.cjs.map
2369
+ //# sourceMappingURL=chunk-RH6TDRML.cjs.map