@powerhousedao/connect 1.0.8 → 1.0.9-dev.0

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 (29) hide show
  1. package/dist/.env +0 -5
  2. package/dist/assets/{app-D3TxLTK-.css → app-D9QJBr8i.css} +19 -1
  3. package/dist/assets/{app-Bw1Ba-jV.js → app-Uu65do7x.js} +342 -384
  4. package/dist/assets/{app-loader-CjrEwupY.css → app-loader-Bnp0H-wa.css} +16 -0
  5. package/dist/assets/{app-loader-KTD3Q6e9.js → app-loader-BnxLpX27.js} +445 -320
  6. package/dist/assets/{ccip-D3HujWHr.js → ccip-CpD5P3bc.js} +3 -3
  7. package/dist/assets/{content-D3TuBhK9.js → content-DV_bRXbH.js} +3 -3
  8. package/dist/assets/{index-DpasqVlD.js → index-B4m3ucR9.js} +3 -3
  9. package/dist/assets/{index-DsNVpRhT.js → index-Ch_NXi_f.js} +3 -3
  10. package/dist/assets/{index-yFk8X8m1.js → index-yrCJMRn6.js} +4 -4
  11. package/dist/assets/{main.CzEw2R-H.js → main.CpORyZoE.js} +1 -1
  12. package/dist/external-packages.js +5 -0
  13. package/dist/hmr.js +4 -1
  14. package/dist/index.html +1 -4
  15. package/dist/modules/@powerhousedao/reactor-browser/{chunk-POMUCSTC.js → chunk-6MBHOHAA.js} +74 -20
  16. package/dist/modules/@powerhousedao/reactor-browser/{chunk-4LZZ55AN.js → chunk-IC6B3767.js} +1 -1
  17. package/dist/modules/@powerhousedao/reactor-browser/{chunk-YOX3ZAET.js → chunk-N5UNGAA6.js} +256 -183
  18. package/dist/modules/@powerhousedao/reactor-browser/{chunk-P46ZMPJ3.js → chunk-OYYVE7RP.js} +1 -1
  19. package/dist/modules/@powerhousedao/reactor-browser/{chunk-6AXML2S3.js → chunk-UDKYG6I4.js} +1 -1
  20. package/dist/modules/@powerhousedao/reactor-browser/context/index.js +2 -2
  21. package/dist/modules/@powerhousedao/reactor-browser/context/read-mode.js +2 -2
  22. package/dist/modules/@powerhousedao/reactor-browser/hooks/index.js +3 -3
  23. package/dist/modules/@powerhousedao/reactor-browser/hooks/useDriveActions.js +2 -2
  24. package/dist/modules/@powerhousedao/reactor-browser/hooks/useDriveActionsWithUiNodes.js +3 -3
  25. package/dist/modules/@powerhousedao/reactor-browser/index.js +5 -5
  26. package/dist/modules/@powerhousedao/reactor-browser/reactor.js +2 -2
  27. package/dist/swEnv.js +0 -3
  28. package/dist/vite-envs.sh +1 -28
  29. package/package.json +9 -9
@@ -688,30 +688,79 @@ var require_lib = __commonJS({
688
688
  }
689
689
  });
690
690
 
691
+ // ../../packages/document-drive/dist/src/cache/util.js
692
+ var trimResultingState = (document) => {
693
+ const global2 = document.operations.global.map((e) => {
694
+ delete e.resultingState;
695
+ return e;
696
+ });
697
+ const local = document.operations.local.map((e) => {
698
+ delete e.resultingState;
699
+ return e;
700
+ });
701
+ return { ...document, operations: { global: global2, local } };
702
+ };
703
+
691
704
  // ../../packages/document-drive/dist/src/cache/memory.js
692
705
  var InMemoryCache = class {
693
- cache = /* @__PURE__ */ new Map();
694
- async setDocument(drive, id, document) {
695
- const global2 = document.operations.global.map((e) => {
696
- delete e.resultingState;
697
- return e;
698
- });
699
- const local = document.operations.local.map((e) => {
700
- delete e.resultingState;
701
- return e;
702
- });
703
- const doc = { ...document, operations: { global: global2, local } };
704
- if (!this.cache.has(drive)) {
705
- this.cache.set(drive, /* @__PURE__ */ new Map());
706
+ idTodocument = /* @__PURE__ */ new Map();
707
+ idToDrive = /* @__PURE__ */ new Map();
708
+ slugToDriveId = /* @__PURE__ */ new Map();
709
+ clear() {
710
+ this.idTodocument.clear();
711
+ this.idToDrive.clear();
712
+ this.slugToDriveId.clear();
713
+ }
714
+ /////////////////////////////////////////////////////////////////////////////
715
+ // ICache
716
+ /////////////////////////////////////////////////////////////////////////////
717
+ async setDocument(documentId, document) {
718
+ const doc = trimResultingState(document);
719
+ this.idTodocument.set(documentId, doc);
720
+ }
721
+ async getDocument(documentId) {
722
+ return this.idTodocument.get(documentId);
723
+ }
724
+ async deleteDocument(documentId) {
725
+ return this.idTodocument.delete(documentId);
726
+ }
727
+ async setDrive(driveId, drive) {
728
+ const doc = trimResultingState(drive);
729
+ this.idToDrive.set(driveId, doc);
730
+ }
731
+ async getDrive(driveId) {
732
+ return this.idToDrive.get(driveId);
733
+ }
734
+ async deleteDrive(driveId) {
735
+ const drive = this.idToDrive.get(driveId);
736
+ if (!drive) {
737
+ return false;
706
738
  }
707
- this.cache.get(drive)?.set(id, doc);
708
- return true;
739
+ const slug = drive.state.global.slug;
740
+ if (slug) {
741
+ this.slugToDriveId.delete(slug);
742
+ }
743
+ return this.idToDrive.delete(driveId);
709
744
  }
710
- async deleteDocument(drive, id) {
711
- return this.cache.get(drive)?.delete(id) ?? false;
745
+ async setDriveBySlug(slug, drive) {
746
+ const driveId = drive.state.global.id;
747
+ this.slugToDriveId.set(slug, driveId);
748
+ this.setDrive(driveId, drive);
749
+ }
750
+ async getDriveBySlug(slug) {
751
+ const driveId = this.slugToDriveId.get(slug);
752
+ if (!driveId) {
753
+ return void 0;
754
+ }
755
+ return this.getDrive(driveId);
712
756
  }
713
- async getDocument(drive, id) {
714
- return this.cache.get(drive)?.get(id);
757
+ async deleteDriveBySlug(slug) {
758
+ const driveId = this.slugToDriveId.get(slug);
759
+ if (!driveId) {
760
+ return false;
761
+ }
762
+ this.slugToDriveId.delete(slug);
763
+ return this.deleteDrive(driveId);
715
764
  }
716
765
  };
717
766
  var memory_default = InMemoryCache;
@@ -1652,19 +1701,16 @@ var ReadDocumentNotFoundError = class extends ReadDriveError {
1652
1701
  // ../../packages/document-drive/dist/src/storage/memory.js
1653
1702
  var MemoryStorage = class {
1654
1703
  documents;
1655
- drives;
1656
1704
  driveManifests;
1657
- slugToDriveId = {};
1658
1705
  constructor() {
1659
1706
  this.documents = {};
1660
- this.drives = {};
1661
1707
  this.driveManifests = {};
1662
1708
  }
1663
1709
  ////////////////////////////////
1664
1710
  // IDocumentStorage
1665
1711
  ////////////////////////////////
1666
1712
  exists(documentId) {
1667
- return Promise.resolve(!!this.documents[documentId]);
1713
+ return Promise.resolve(!!this.documents[documentId] || !!this.documents[`drive/${documentId}`]);
1668
1714
  }
1669
1715
  create(documentId, document) {
1670
1716
  this.documents[documentId] = document;
@@ -1673,10 +1719,52 @@ var MemoryStorage = class {
1673
1719
  get(documentId) {
1674
1720
  const document = this.documents[documentId];
1675
1721
  if (!document) {
1722
+ const drive = this.documents[`drive/${documentId}`];
1723
+ if (drive) {
1724
+ return Promise.resolve(drive);
1725
+ }
1676
1726
  throw new Error(`Document with id ${documentId} not found`);
1677
1727
  }
1678
1728
  return Promise.resolve(document);
1679
1729
  }
1730
+ async delete(documentId) {
1731
+ const drives = await this.getDrives();
1732
+ for (const driveId of drives) {
1733
+ if (driveId === documentId)
1734
+ continue;
1735
+ await this.removeChild(driveId, documentId);
1736
+ }
1737
+ delete this.driveManifests[documentId];
1738
+ if (this.documents[documentId]) {
1739
+ delete this.documents[documentId];
1740
+ return Promise.resolve(true);
1741
+ }
1742
+ return Promise.resolve(false);
1743
+ }
1744
+ async addChild(parentId, childId) {
1745
+ if (parentId === childId) {
1746
+ throw new Error("Cannot associate a document with itself");
1747
+ }
1748
+ const children = await this.getChildren(childId);
1749
+ if (children.includes(parentId)) {
1750
+ throw new Error("Cannot associate a document with its child");
1751
+ }
1752
+ const manifest = this.getManifest(parentId);
1753
+ manifest.documentIds.add(childId);
1754
+ this.updateDriveManifest(parentId, manifest);
1755
+ }
1756
+ async removeChild(parentId, childId) {
1757
+ const manifest = this.getManifest(parentId);
1758
+ if (manifest.documentIds.delete(childId)) {
1759
+ this.updateDriveManifest(parentId, manifest);
1760
+ return true;
1761
+ }
1762
+ return false;
1763
+ }
1764
+ async getChildren(parentId) {
1765
+ const manifest = this.getManifest(parentId);
1766
+ return [...manifest.documentIds];
1767
+ }
1680
1768
  ////////////////////////////////
1681
1769
  // IDriveStorage
1682
1770
  ////////////////////////////////
@@ -1684,27 +1772,19 @@ var MemoryStorage = class {
1684
1772
  return this.exists(id);
1685
1773
  }
1686
1774
  getDocuments(drive) {
1687
- const manifest = this.getDriveManifest(drive);
1775
+ const manifest = this.getManifest(drive);
1688
1776
  return Promise.resolve([...manifest.documentIds]);
1689
1777
  }
1690
1778
  getDocument(driveId, id) {
1691
1779
  return this.get(id);
1692
1780
  }
1693
- async saveDocument(drive, id, document) {
1694
- this.documents[id] = document;
1695
- const manifest = this.getDriveManifest(drive);
1696
- manifest.documentIds.add(id);
1697
- this.updateDriveManifest(drive, manifest);
1698
- }
1699
1781
  async clearStorage() {
1700
1782
  this.documents = {};
1701
- this.drives = {};
1702
1783
  this.driveManifests = {};
1703
- this.slugToDriveId = {};
1704
1784
  }
1705
1785
  async createDocument(drive, id, document) {
1706
1786
  await this.create(id, document);
1707
- const manifest = this.getDriveManifest(drive);
1787
+ const manifest = this.getManifest(drive);
1708
1788
  manifest.documentIds.add(id);
1709
1789
  this.updateDriveManifest(drive, manifest);
1710
1790
  }
@@ -1721,59 +1801,60 @@ var MemoryStorage = class {
1721
1801
  };
1722
1802
  }
1723
1803
  async deleteDocument(drive, id) {
1724
- const drives = await this.getDrives();
1725
- for (const driveId of drives) {
1726
- const manifest = this.getDriveManifest(driveId);
1727
- if (manifest.documentIds.has(id)) {
1728
- manifest.documentIds.delete(id);
1729
- this.updateDriveManifest(driveId, manifest);
1730
- }
1731
- }
1732
- delete this.documents[id];
1804
+ this.delete(id);
1733
1805
  }
1734
1806
  async getDrives() {
1735
- return Object.keys(this.drives);
1807
+ return Object.keys(this.driveManifests);
1736
1808
  }
1737
1809
  async getDrive(id) {
1738
- const drive = this.drives[id];
1810
+ const drive = this.documents[`drive/${id}`];
1739
1811
  if (!drive) {
1740
1812
  throw new DriveNotFoundError(id);
1741
1813
  }
1742
1814
  return drive;
1743
1815
  }
1744
1816
  async getDriveBySlug(slug) {
1745
- const driveId = this.slugToDriveId[slug];
1746
- if (!driveId) {
1747
- throw new Error(`Drive with slug ${slug} not found`);
1817
+ for (const driveId of Object.keys(this.driveManifests)) {
1818
+ const drive = this.documents[`drive/${driveId}`];
1819
+ if (drive.initialState.state.global.slug === slug) {
1820
+ return drive;
1821
+ }
1748
1822
  }
1749
- return this.getDrive(driveId);
1823
+ throw new Error(`Drive with slug ${slug} not found`);
1750
1824
  }
1751
1825
  async createDrive(id, drive) {
1752
- this.drives[id] = drive;
1753
- this.updateDriveManifest(id, { documentIds: /* @__PURE__ */ new Set() });
1754
- const { slug } = drive.initialState.state.global;
1826
+ const slug = drive.initialState.state.global.slug;
1755
1827
  if (slug) {
1756
- this.slugToDriveId[slug] = id;
1828
+ let existingDrive;
1829
+ try {
1830
+ existingDrive = await this.getDriveBySlug(slug);
1831
+ } catch {
1832
+ }
1833
+ if (existingDrive) {
1834
+ throw new Error(`Drive with slug ${slug} already exists`);
1835
+ }
1757
1836
  }
1837
+ await this.create(`drive/${id}`, drive);
1838
+ this.updateDriveManifest(id, { documentIds: /* @__PURE__ */ new Set() });
1758
1839
  }
1759
1840
  async addDriveOperations(id, operations, header) {
1760
1841
  const drive = await this.getDrive(id);
1761
1842
  const mergedOperations = mergeOperations(drive.operations, operations);
1762
- this.drives[id] = {
1843
+ this.documents[`drive/${id}`] = {
1763
1844
  ...drive,
1764
1845
  ...header,
1765
1846
  operations: mergedOperations
1766
1847
  };
1767
1848
  }
1768
1849
  async deleteDrive(id) {
1769
- const manifest = this.getDriveManifest(id);
1850
+ const manifest = this.getManifest(id);
1770
1851
  const drives = await this.getDrives();
1771
1852
  await Promise.all([...manifest.documentIds].map((docId) => {
1772
1853
  for (const driveId of drives) {
1773
1854
  if (driveId === id) {
1774
1855
  continue;
1775
1856
  }
1776
- const manifest2 = this.getDriveManifest(driveId);
1857
+ const manifest2 = this.getManifest(driveId);
1777
1858
  if (manifest2.documentIds.has(docId)) {
1778
1859
  return;
1779
1860
  }
@@ -1781,24 +1862,18 @@ var MemoryStorage = class {
1781
1862
  delete this.documents[docId];
1782
1863
  }));
1783
1864
  delete this.driveManifests[id];
1784
- delete this.drives[id];
1785
- for (const [slug, driveId] of Object.entries(this.slugToDriveId)) {
1786
- if (driveId === id) {
1787
- delete this.slugToDriveId[slug];
1788
- }
1789
- }
1865
+ delete this.documents[id];
1790
1866
  }
1791
1867
  async getSynchronizationUnitsRevision(units) {
1792
1868
  const results = await Promise.allSettled(units.map(async (unit) => {
1793
1869
  try {
1794
- const document = await (unit.documentId ? this.getDocument(unit.driveId, unit.documentId) : this.getDrive(unit.driveId));
1870
+ const document = await this.get(unit.documentId);
1795
1871
  if (!document) {
1796
1872
  return void 0;
1797
1873
  }
1798
1874
  const operation = document.operations[unit.scope].at(-1);
1799
1875
  if (operation) {
1800
1876
  return {
1801
- driveId: unit.driveId,
1802
1877
  documentId: unit.documentId,
1803
1878
  scope: unit.scope,
1804
1879
  branch: unit.branch,
@@ -1820,7 +1895,7 @@ var MemoryStorage = class {
1820
1895
  ////////////////////////////////
1821
1896
  // Private
1822
1897
  ////////////////////////////////
1823
- getDriveManifest(driveId) {
1898
+ getManifest(driveId) {
1824
1899
  if (!this.driveManifests[driveId]) {
1825
1900
  this.driveManifests[driveId] = { documentIds: /* @__PURE__ */ new Set() };
1826
1901
  }
@@ -11851,7 +11926,7 @@ var PullResponderTransmitter = class _PullResponderTransmitter {
11851
11926
  const syncUnits = await this.manager.getListenerSyncUnitIds(driveId, listenerId);
11852
11927
  let success = true;
11853
11928
  for (const revision of revisions) {
11854
- const syncUnit = syncUnits.find((s) => s.scope === revision.scope && s.branch === revision.branch && s.driveId === revision.driveId && s.documentId == revision.documentId);
11929
+ const syncUnit = syncUnits.find((s) => s.scope === revision.scope && s.branch === revision.branch && s.documentId == revision.documentId);
11855
11930
  if (!syncUnit) {
11856
11931
  this.logger.warn("Unknown sync unit was acknowledged", revision);
11857
11932
  success = false;
@@ -12088,6 +12163,80 @@ var PullResponderTransmitter = class _PullResponderTransmitter {
12088
12163
  }
12089
12164
  };
12090
12165
 
12166
+ // ../../packages/document-drive/dist/src/server/listener/transmitter/switchboard-push.js
12167
+ var import_json_stringify_deterministic = __toESM(require_lib(), 1);
12168
+ var SYNC_OPS_BATCH_LIMIT = 10;
12169
+ var SwitchboardPushTransmitter = class {
12170
+ targetURL;
12171
+ logger = childLogger([
12172
+ "SwitchboardPushTransmitter",
12173
+ Math.floor(Math.random() * 999).toString()
12174
+ ]);
12175
+ constructor(targetURL) {
12176
+ this.targetURL = targetURL;
12177
+ }
12178
+ async transmit(strands, source) {
12179
+ if (source.type === "trigger" && source.trigger.data?.url === this.targetURL) {
12180
+ this.logger.verbose(`Cutting trigger loop from ${this.targetURL}.`);
12181
+ return strands.map((strand) => ({
12182
+ driveId: strand.driveId,
12183
+ documentId: strand.documentId,
12184
+ scope: strand.scope,
12185
+ branch: strand.branch,
12186
+ status: "SUCCESS",
12187
+ revision: strand.operations.at(-1)?.index ?? -1
12188
+ }));
12189
+ }
12190
+ const culledStrands = [];
12191
+ let opsCounter = 0;
12192
+ for (let s = 0; opsCounter <= SYNC_OPS_BATCH_LIMIT && s < strands.length; s++) {
12193
+ const currentStrand = strands.at(s);
12194
+ if (!currentStrand) {
12195
+ break;
12196
+ }
12197
+ const newOps = Math.min(SYNC_OPS_BATCH_LIMIT - opsCounter, currentStrand.operations.length);
12198
+ culledStrands.push({
12199
+ ...currentStrand,
12200
+ operations: currentStrand.operations.slice(0, newOps)
12201
+ });
12202
+ opsCounter += newOps;
12203
+ }
12204
+ this.logger.verbose(` Total update: [${strands.map((s) => s.operations.length).join(", ")}] operations`);
12205
+ this.logger.verbose(`Culled update: [${culledStrands.map((s) => s.operations.length).join(", ")}] operations`);
12206
+ try {
12207
+ const { pushUpdates } = await requestGraphql(this.targetURL, gql`
12208
+ mutation pushUpdates($strands: [InputStrandUpdate!]) {
12209
+ pushUpdates(strands: $strands) {
12210
+ driveId
12211
+ documentId
12212
+ scope
12213
+ branch
12214
+ status
12215
+ revision
12216
+ error
12217
+ }
12218
+ }
12219
+ `, {
12220
+ strands: culledStrands.map((strand) => ({
12221
+ ...strand,
12222
+ operations: strand.operations.map((op) => ({
12223
+ ...op,
12224
+ input: (0, import_json_stringify_deterministic.default)(op.input)
12225
+ }))
12226
+ }))
12227
+ });
12228
+ if (!pushUpdates) {
12229
+ throw new Error("Couldn't update listener revision");
12230
+ }
12231
+ return pushUpdates;
12232
+ } catch (e) {
12233
+ this.logger.error(e);
12234
+ throw e;
12235
+ }
12236
+ return [];
12237
+ }
12238
+ };
12239
+
12091
12240
  // ../../packages/document-drive/dist/src/server/types.js
12092
12241
  var TransmitterType;
12093
12242
  (function(TransmitterType2) {
@@ -12185,17 +12334,6 @@ var BaseDocumentDriveServer = class {
12185
12334
  taskQueueMethod: options?.taskQueueMethod === void 0 ? RunAsap.runAsap : options.taskQueueMethod
12186
12335
  };
12187
12336
  this.defaultDrivesManager = new DefaultDrivesManager(this, this.defaultDrivesManagerDelegate, options);
12188
- this.storage.setStorageDelegate?.({
12189
- getCachedOperations: async (drive, id) => {
12190
- try {
12191
- const document = await this.cache.getDocument(drive, id);
12192
- return document?.operations;
12193
- } catch (error) {
12194
- logger.error(error);
12195
- return void 0;
12196
- }
12197
- }
12198
- });
12199
12337
  this.initializePromise = this._initialize();
12200
12338
  }
12201
12339
  // workaround for testing the ephemeral listeners -- we don't have DI in place yet
@@ -12339,6 +12477,26 @@ var BaseDocumentDriveServer = class {
12339
12477
  if (this.shouldSyncRemoteDrive(drive)) {
12340
12478
  await this.startSyncRemoteDrive(driveId);
12341
12479
  }
12480
+ for (const zodListener of drive.state.local.listeners) {
12481
+ if (zodListener.callInfo?.transmitterType === "SwitchboardPush") {
12482
+ const transmitter = new SwitchboardPushTransmitter(zodListener.callInfo?.data ?? "");
12483
+ this.listenerManager.setListener(driveId, {
12484
+ block: zodListener.block,
12485
+ driveId: drive.state.global.id,
12486
+ filter: {
12487
+ branch: zodListener.filter?.branch ?? [],
12488
+ documentId: zodListener.filter?.documentId ?? [],
12489
+ documentType: zodListener.filter?.documentType ?? [],
12490
+ scope: zodListener.filter?.scope ?? []
12491
+ },
12492
+ listenerId: zodListener.listenerId,
12493
+ callInfo: zodListener.callInfo,
12494
+ system: zodListener.system,
12495
+ label: zodListener.label ?? "",
12496
+ transmitter
12497
+ });
12498
+ }
12499
+ }
12342
12500
  }
12343
12501
  // Delegate synchronization methods to synchronizationManager
12344
12502
  getSynchronizationUnits(driveId, documentId, scope, branch, documentType2) {
@@ -12380,7 +12538,7 @@ var BaseDocumentDriveServer = class {
12380
12538
  };
12381
12539
  await this.storage.createDrive(id, document);
12382
12540
  if (input.global.slug) {
12383
- await this.cache.deleteDocument("drives-slug", input.global.slug);
12541
+ await this.cache.deleteDriveBySlug(input.global.slug);
12384
12542
  }
12385
12543
  await this._initializeDrive(id);
12386
12544
  this.eventEmitter.emit("driveAdded", document);
@@ -12412,7 +12570,7 @@ var BaseDocumentDriveServer = class {
12412
12570
  const result = await Promise.allSettled([
12413
12571
  this.stopSyncRemoteDrive(driveId),
12414
12572
  this.listenerManager.removeDrive(driveId),
12415
- this.cache.deleteDocument("drives", driveId),
12573
+ this.cache.deleteDrive(driveId),
12416
12574
  this.storage.deleteDrive(driveId)
12417
12575
  ]);
12418
12576
  result.forEach((r) => {
@@ -12427,7 +12585,7 @@ var BaseDocumentDriveServer = class {
12427
12585
  async getDrive(driveId, options) {
12428
12586
  let document;
12429
12587
  try {
12430
- const cachedDocument = await this.cache.getDocument("drives", driveId);
12588
+ const cachedDocument = await this.cache.getDrive(driveId);
12431
12589
  if (cachedDocument && isDocumentDrive(cachedDocument)) {
12432
12590
  document = cachedDocument;
12433
12591
  if (isAtRevision(document, options?.revisions)) {
@@ -12443,16 +12601,16 @@ var BaseDocumentDriveServer = class {
12443
12601
  throw new Error(`Document with id ${driveId} is not a Document Drive`);
12444
12602
  } else {
12445
12603
  if (!options?.revisions) {
12446
- this.cache.setDocument("drives", driveId, result).catch(logger.error);
12604
+ this.cache.setDrive(driveId, result).catch(logger.error);
12447
12605
  }
12448
12606
  return result;
12449
12607
  }
12450
12608
  }
12451
12609
  async getDriveBySlug(slug, options) {
12452
12610
  try {
12453
- const document2 = await this.cache.getDocument("drives-slug", slug);
12454
- if (document2 && isDocumentDrive(document2)) {
12455
- return document2;
12611
+ const drive = await this.cache.getDriveBySlug(slug);
12612
+ if (drive) {
12613
+ return drive;
12456
12614
  }
12457
12615
  } catch (e) {
12458
12616
  logger.error("Error getting drive from cache", e);
@@ -12462,14 +12620,14 @@ var BaseDocumentDriveServer = class {
12462
12620
  if (!isDocumentDrive(document)) {
12463
12621
  throw new Error(`Document with slug ${slug} is not a Document Drive`);
12464
12622
  } else {
12465
- this.cache.setDocument("drives-slug", slug, document).catch(logger.error);
12623
+ this.cache.setDriveBySlug(slug, document).catch(logger.error);
12466
12624
  return document;
12467
12625
  }
12468
12626
  }
12469
12627
  async getDocument(driveId, documentId, options) {
12470
12628
  let cachedDocument;
12471
12629
  try {
12472
- cachedDocument = await this.cache.getDocument(driveId, documentId);
12630
+ cachedDocument = await this.cache.getDocument(documentId);
12473
12631
  if (cachedDocument && isAtRevision(cachedDocument, options?.revisions)) {
12474
12632
  return cachedDocument;
12475
12633
  }
@@ -12479,7 +12637,7 @@ var BaseDocumentDriveServer = class {
12479
12637
  const documentStorage = cachedDocument ?? await this.storage.getDocument(driveId, documentId);
12480
12638
  const document = this._buildDocument(documentStorage, options);
12481
12639
  if (!options?.revisions) {
12482
- this.cache.setDocument(driveId, documentId, document).catch(logger.error);
12640
+ this.cache.setDocument(documentId, document).catch(logger.error);
12483
12641
  }
12484
12642
  return document;
12485
12643
  }
@@ -12536,7 +12694,7 @@ var BaseDocumentDriveServer = class {
12536
12694
  } catch (error) {
12537
12695
  logger.warn("Error deleting document", error);
12538
12696
  }
12539
- await this.cache.deleteDocument(driveId, documentId);
12697
+ await this.cache.deleteDocument(documentId);
12540
12698
  return this.storage.deleteDocument(driveId, documentId);
12541
12699
  }
12542
12700
  async _processOperations(driveId, documentId, documentStorage, operations) {
@@ -12824,7 +12982,7 @@ var BaseDocumentDriveServer = class {
12824
12982
  };
12825
12983
  });
12826
12984
  if (document) {
12827
- this.cache.setDocument(driveId, documentId, document).catch(logger.error);
12985
+ this.cache.setDocument(documentId, document).catch(logger.error);
12828
12986
  }
12829
12987
  const { scopes, branches } = operationsApplied.reduce((acc, operation) => {
12830
12988
  if (!acc.scopes.includes(operation.scope)) {
@@ -12986,7 +13144,7 @@ var BaseDocumentDriveServer = class {
12986
13144
  if (!document || !isDocumentDrive(document)) {
12987
13145
  throw error ?? new Error("Invalid Document Drive document");
12988
13146
  }
12989
- this.cache.setDocument("drives", driveId, document).catch(logger.error);
13147
+ this.cache.setDrive(driveId, document).catch(logger.error);
12990
13148
  const lastOperation = operationsApplied.filter((op) => op.scope === "global").slice().pop();
12991
13149
  if (lastOperation) {
12992
13150
  const newOp = operationsApplied.find((appliedOp) => !operations.find((o) => o.id === appliedOp.id && o.index === appliedOp.index && o.skip === appliedOp.skip && o.hash === appliedOp.hash));
@@ -12995,7 +13153,6 @@ var BaseDocumentDriveServer = class {
12995
13153
  this.listenerManager.updateSynchronizationRevisions(driveId, [
12996
13154
  {
12997
13155
  syncId: "0",
12998
- driveId,
12999
13156
  documentId: "",
13000
13157
  scope: "global",
13001
13158
  branch: "main",
@@ -13306,7 +13463,7 @@ var ListenerManager = class _ListenerManager {
13306
13463
  this.logger.verbose(`Abandoning push for sync unit ${syncUnit.syncId}: already up-to-date (${unitState.listenerRev} >= ${syncUnit.revision})`);
13307
13464
  return;
13308
13465
  } else {
13309
- this.logger.verbose(`Listener out-of-date for sync unit (${syncUnit.driveId}, ${syncUnit.scope}, ${syncUnit.documentId}): ${unitState?.listenerRev} < ${syncUnit.revision}`);
13466
+ this.logger.verbose(`Listener out-of-date for sync unit (${syncUnit.scope}, ${syncUnit.documentId}): ${unitState?.listenerRev} < ${syncUnit.revision}`);
13310
13467
  }
13311
13468
  const opData = [];
13312
13469
  try {
@@ -13461,11 +13618,11 @@ var ListenerManager = class _ListenerManager {
13461
13618
  if (entry && entry.listenerRev >= syncUnit.revision) {
13462
13619
  return;
13463
13620
  }
13464
- const { documentId, driveId: driveId2, scope, branch } = syncUnit;
13621
+ const { documentId, scope, branch } = syncUnit;
13465
13622
  try {
13466
13623
  const operations = await this.syncManager.getOperationData(
13467
13624
  // DEAL WITH INVALID SYNC ID ERROR
13468
- driveId2,
13625
+ driveId,
13469
13626
  syncUnit.syncId,
13470
13627
  {
13471
13628
  since: options?.since,
@@ -13478,7 +13635,7 @@ var ListenerManager = class _ListenerManager {
13478
13635
  }
13479
13636
  operationsCount += operations.length;
13480
13637
  strands.push({
13481
- driveId: driveId2,
13638
+ driveId,
13482
13639
  documentId,
13483
13640
  scope,
13484
13641
  branch,
@@ -13520,80 +13677,6 @@ var ListenerManager = class _ListenerManager {
13520
13677
  }
13521
13678
  };
13522
13679
 
13523
- // ../../packages/document-drive/dist/src/server/listener/transmitter/switchboard-push.js
13524
- var import_json_stringify_deterministic = __toESM(require_lib(), 1);
13525
- var SYNC_OPS_BATCH_LIMIT = 10;
13526
- var SwitchboardPushTransmitter = class {
13527
- targetURL;
13528
- logger = childLogger([
13529
- "SwitchboardPushTransmitter",
13530
- Math.floor(Math.random() * 999).toString()
13531
- ]);
13532
- constructor(targetURL) {
13533
- this.targetURL = targetURL;
13534
- }
13535
- async transmit(strands, source) {
13536
- if (source.type === "trigger" && source.trigger.data?.url === this.targetURL) {
13537
- this.logger.verbose(`Cutting trigger loop from ${this.targetURL}.`);
13538
- return strands.map((strand) => ({
13539
- driveId: strand.driveId,
13540
- documentId: strand.documentId,
13541
- scope: strand.scope,
13542
- branch: strand.branch,
13543
- status: "SUCCESS",
13544
- revision: strand.operations.at(-1)?.index ?? -1
13545
- }));
13546
- }
13547
- const culledStrands = [];
13548
- let opsCounter = 0;
13549
- for (let s = 0; opsCounter <= SYNC_OPS_BATCH_LIMIT && s < strands.length; s++) {
13550
- const currentStrand = strands.at(s);
13551
- if (!currentStrand) {
13552
- break;
13553
- }
13554
- const newOps = Math.min(SYNC_OPS_BATCH_LIMIT - opsCounter, currentStrand.operations.length);
13555
- culledStrands.push({
13556
- ...currentStrand,
13557
- operations: currentStrand.operations.slice(0, newOps)
13558
- });
13559
- opsCounter += newOps;
13560
- }
13561
- this.logger.verbose(` Total update: [${strands.map((s) => s.operations.length).join(", ")}] operations`);
13562
- this.logger.verbose(`Culled update: [${culledStrands.map((s) => s.operations.length).join(", ")}] operations`);
13563
- try {
13564
- const { pushUpdates } = await requestGraphql(this.targetURL, gql`
13565
- mutation pushUpdates($strands: [InputStrandUpdate!]) {
13566
- pushUpdates(strands: $strands) {
13567
- driveId
13568
- documentId
13569
- scope
13570
- branch
13571
- status
13572
- revision
13573
- error
13574
- }
13575
- }
13576
- `, {
13577
- strands: culledStrands.map((strand) => ({
13578
- ...strand,
13579
- operations: strand.operations.map((op) => ({
13580
- ...op,
13581
- input: (0, import_json_stringify_deterministic.default)(op.input)
13582
- }))
13583
- }))
13584
- });
13585
- if (!pushUpdates) {
13586
- throw new Error("Couldn't update listener revision");
13587
- }
13588
- return pushUpdates;
13589
- } catch (e) {
13590
- this.logger.error(e);
13591
- throw e;
13592
- }
13593
- return [];
13594
- }
13595
- };
13596
-
13597
13680
  // ../../packages/document-drive/dist/src/server/listener/transmitter/factory.js
13598
13681
  var TransmitterFactory = class {
13599
13682
  listenerManager;
@@ -13639,26 +13722,18 @@ var SynchronizationManager = class {
13639
13722
  async getSynchronizationUnitsRevision(driveId, syncUnitsQuery) {
13640
13723
  const drive = await this.getDrive(driveId);
13641
13724
  const revisions = await this.storage.getSynchronizationUnitsRevision(syncUnitsQuery);
13642
- const synchronizationUnits = syncUnitsQuery.map((s) => ({
13725
+ return syncUnitsQuery.map((s) => ({
13643
13726
  ...s,
13644
13727
  lastUpdated: drive.created,
13645
- revision: -1
13728
+ revision: revisions.find((r) => r.documentId === s.documentId && r.scope === s.scope && r.branch === s.branch)?.revision ?? -1
13646
13729
  }));
13647
- for (const revision of revisions) {
13648
- const syncUnit = synchronizationUnits.find((s) => revision.driveId === s.driveId && revision.documentId === s.documentId && revision.scope === s.scope && revision.branch === s.branch);
13649
- if (syncUnit) {
13650
- syncUnit.revision = revision.revision;
13651
- syncUnit.lastUpdated = revision.lastUpdated;
13652
- }
13653
- }
13654
- return synchronizationUnits;
13655
13730
  }
13656
13731
  async getSynchronizationUnitsIds(driveId, documentId, scope, branch, documentType2) {
13657
13732
  const drive = await this.getDrive(driveId);
13658
13733
  const nodes = drive.state.global.nodes.filter((node) => isFileNode(node) && (!documentId?.length || documentId.includes(node.id) || documentId.includes("*")) && (!documentType2?.length || documentType2.includes(node.documentType) || documentType2.includes("*")));
13659
13734
  if ((!documentId || documentId.includes("*") || documentId.includes("")) && (!documentType2?.length || documentType2.includes("powerhouse/document-drive") || documentType2.includes("*"))) {
13660
13735
  nodes.unshift({
13661
- id: "",
13736
+ id: drive.state.global.id,
13662
13737
  documentType: "powerhouse/document-drive",
13663
13738
  synchronizationUnits: [
13664
13739
  {
@@ -13700,7 +13775,6 @@ var SynchronizationManager = class {
13700
13775
  syncId,
13701
13776
  scope: syncUnit.scope,
13702
13777
  branch: syncUnit.branch,
13703
- driveId,
13704
13778
  documentId: node.id,
13705
13779
  documentType: node.documentType
13706
13780
  };
@@ -13718,7 +13792,6 @@ var SynchronizationManager = class {
13718
13792
  syncId,
13719
13793
  scope,
13720
13794
  branch,
13721
- driveId,
13722
13795
  documentId,
13723
13796
  documentType: documentType2,
13724
13797
  lastUpdated: lastOperation.timestamp ?? document.lastModified,
@@ -13747,7 +13820,7 @@ var SynchronizationManager = class {
13747
13820
  }
13748
13821
  async getDrive(driveId) {
13749
13822
  try {
13750
- const cachedDocument = await this.cache.getDocument("drives", driveId);
13823
+ const cachedDocument = await this.cache.getDrive(driveId);
13751
13824
  if (cachedDocument && isDocumentDrive(cachedDocument)) {
13752
13825
  return cachedDocument;
13753
13826
  }
@@ -13763,7 +13836,7 @@ var SynchronizationManager = class {
13763
13836
  }
13764
13837
  async getDocument(driveId, documentId) {
13765
13838
  try {
13766
- const cachedDocument = await this.cache.getDocument(driveId, documentId);
13839
+ const cachedDocument = await this.cache.getDocument(documentId);
13767
13840
  if (cachedDocument) {
13768
13841
  return cachedDocument;
13769
13842
  }