@fireproof/core 0.19.11-dev-dryrun → 0.19.11-dev-dryrun4

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 (37) hide show
  1. package/{chunk-MAK4D54P.js → chunk-OFGPKRCM.js} +11 -1
  2. package/chunk-OFGPKRCM.js.map +1 -0
  3. package/{chunk-XINRLWR3.js → chunk-WS3YRPIA.js} +4 -4
  4. package/{gateway-VWWKLHUI.js → gateway-5FCWPX5W.js} +3 -3
  5. package/{gateway-7OM6OSYK.js → gateway-H7UD6TNB.js} +2 -2
  6. package/index.cjs +331 -378
  7. package/index.cjs.map +1 -1
  8. package/index.d.cts +9 -5
  9. package/index.d.ts +9 -5
  10. package/index.global.js +270 -318
  11. package/index.global.js.map +1 -1
  12. package/index.js +273 -329
  13. package/index.js.map +1 -1
  14. package/{key-bag-file-DFMW6ZM6.js → key-bag-file-WADZBHYG.js} +3 -3
  15. package/{key-bag-indexdb-R2RWGSQ4.js → key-bag-indexdb-PGVAI3FJ.js} +3 -3
  16. package/{mem-filesystem-BZQZLUR6.js → mem-filesystem-YPPJV7Q2.js} +3 -3
  17. package/metafile-cjs.json +1 -1
  18. package/metafile-esm.json +1 -1
  19. package/metafile-iife.json +1 -1
  20. package/{node-filesystem-7YZR3POJ.js → node-filesystem-INX4ZTHE.js} +3 -3
  21. package/package.json +1 -1
  22. package/tests/fireproof/all-gateway.test.ts +12 -13
  23. package/tests/fireproof/utils.test.ts +19 -0
  24. package/tests/www/todo-local.html +1 -1
  25. package/tests/www/todo.html +12 -15
  26. package/{utils-AISQB3PB.js → utils-QO2HIWGI.js} +3 -3
  27. package/chunk-MAK4D54P.js.map +0 -1
  28. package/tests/fireproof/convert_uint8.py +0 -27
  29. /package/{chunk-XINRLWR3.js.map → chunk-WS3YRPIA.js.map} +0 -0
  30. /package/{gateway-VWWKLHUI.js.map → gateway-5FCWPX5W.js.map} +0 -0
  31. /package/{gateway-7OM6OSYK.js.map → gateway-H7UD6TNB.js.map} +0 -0
  32. /package/{key-bag-file-DFMW6ZM6.js.map → key-bag-file-WADZBHYG.js.map} +0 -0
  33. /package/{key-bag-indexdb-R2RWGSQ4.js.map → key-bag-indexdb-PGVAI3FJ.js.map} +0 -0
  34. /package/{mem-filesystem-BZQZLUR6.js.map → mem-filesystem-YPPJV7Q2.js.map} +0 -0
  35. /package/{node-filesystem-7YZR3POJ.js.map → node-filesystem-INX4ZTHE.js.map} +0 -0
  36. /package/tests/fireproof/cars/{bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.js → bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.ts} +0 -0
  37. /package/{utils-AISQB3PB.js.map → utils-QO2HIWGI.js.map} +0 -0
package/index.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  getFileSystem,
7
7
  getPath,
8
8
  toArrayBuffer
9
- } from "./chunk-XINRLWR3.js";
9
+ } from "./chunk-WS3YRPIA.js";
10
10
  import {
11
11
  INDEXDB_VERSION
12
12
  } from "./chunk-PB4BKL4O.js";
@@ -24,7 +24,7 @@ import {
24
24
  getName,
25
25
  getStore,
26
26
  isNotFoundError
27
- } from "./chunk-MAK4D54P.js";
27
+ } from "./chunk-OFGPKRCM.js";
28
28
 
29
29
  // src/database.ts
30
30
  import { ResolveOnce as ResolveOnce6 } from "@adviser/cement";
@@ -253,8 +253,8 @@ var UnixFSFileBuilder = class {
253
253
 
254
254
  // src/blockstore/store.ts
255
255
  import pLimit2 from "p-limit";
256
- import { format, parse } from "@ipld/dag-json";
257
- import { exception2Result, ResolveOnce as ResolveOnce3, Result as Result4 } from "@adviser/cement";
256
+ import { format as format2, parse as parse2 } from "@ipld/dag-json";
257
+ import { exception2Result, ResolveOnce as ResolveOnce3, Result as Result5 } from "@adviser/cement";
258
258
 
259
259
  // src/types.ts
260
260
  function isFalsy(value) {
@@ -668,14 +668,14 @@ var keyBagProviderFactories = new Map(
668
668
  {
669
669
  protocol: "file:",
670
670
  factory: async (url, sthis) => {
671
- const { KeyBagProviderFile } = await import("./key-bag-file-DFMW6ZM6.js");
671
+ const { KeyBagProviderFile } = await import("./key-bag-file-WADZBHYG.js");
672
672
  return new KeyBagProviderFile(url, sthis);
673
673
  }
674
674
  },
675
675
  {
676
676
  protocol: "indexdb:",
677
677
  factory: async (url, sthis) => {
678
- const { KeyBagProviderIndexDB } = await import("./key-bag-indexdb-R2RWGSQ4.js");
678
+ const { KeyBagProviderIndexDB } = await import("./key-bag-indexdb-PGVAI3FJ.js");
679
679
  return new KeyBagProviderIndexDB(url, sthis);
680
680
  }
681
681
  }
@@ -820,7 +820,7 @@ async function commit(params, t, done, opts = { noLoader: false, compact: false
820
820
  }
821
821
  async function prepareCarFiles(encoder, threshold, rootBlock, t) {
822
822
  const carFiles = [];
823
- threshold = threshold || 1e3 * 1e3;
823
+ threshold = threshold || 128e3 * 8;
824
824
  let clonedt = new CarTransaction(t.parent, { add: false, noLoader: false });
825
825
  clonedt.putSync(rootBlock.cid, rootBlock.bytes);
826
826
  let newsize = CBW.blockLength(toCIDBlock(rootBlock));
@@ -1032,7 +1032,8 @@ var Loader = class {
1032
1032
  carLog: this.carLog,
1033
1033
  carStore: fstore,
1034
1034
  WALStore: await this.WALStore(),
1035
- metaStore: await this.metaStore()
1035
+ metaStore: await this.metaStore(),
1036
+ threshold: this.ebOpts.threshold
1036
1037
  };
1037
1038
  return this.commitQueue.enqueue(async () => {
1038
1039
  await this.cacheTransaction(t);
@@ -1540,26 +1541,7 @@ var FragmentGateway = class {
1540
1541
  if (this.innerGW.subscribe) {
1541
1542
  return this.innerGW.subscribe(url, callback);
1542
1543
  } else {
1543
- let lastData = void 0;
1544
- let interval = 100;
1545
- const fetchData = async () => {
1546
- const result = await this.innerGW.get(url);
1547
- if (result.isOk()) {
1548
- const data = result.Ok();
1549
- if (!lastData || !data.every((value, index2) => lastData && value === lastData[index2])) {
1550
- lastData = data;
1551
- callback(data);
1552
- interval = 100;
1553
- } else {
1554
- interval *= 2;
1555
- }
1556
- }
1557
- timeoutId = setTimeout(fetchData, interval);
1558
- };
1559
- let timeoutId = setTimeout(fetchData, interval);
1560
- return Result3.Ok(() => {
1561
- clearTimeout(timeoutId);
1562
- });
1544
+ return Result3.Err(this.logger.Error().Url(url).Msg("subscribe not supported").AsError());
1563
1545
  }
1564
1546
  }
1565
1547
  async delete(url) {
@@ -1577,12 +1559,119 @@ var FragmentGateway = class {
1577
1559
  }
1578
1560
  };
1579
1561
 
1562
+ // src/blockstore/meta-key-helper.ts
1563
+ import { format, parse } from "@ipld/dag-json";
1564
+ import { EventBlock, decodeEventBlock } from "@web3-storage/pail/clock";
1565
+ import { CID as CID2 } from "multiformats";
1566
+ import { base64pad } from "multiformats/bases/base64";
1567
+ import { Result as Result4 } from "@adviser/cement";
1568
+ async function decodeGatewayMetaBytesToDbMeta(sthis, byteHeads) {
1569
+ const crdtEntries = JSON.parse(sthis.txt.decode(byteHeads));
1570
+ if (!crdtEntries.length) {
1571
+ sthis.logger.Debug().Str("byteHeads", new TextDecoder().decode(byteHeads)).Msg("No CRDT entries found");
1572
+ return [];
1573
+ }
1574
+ if (!crdtEntries.map) {
1575
+ sthis.logger.Debug().Str("crdtEntries", JSON.stringify(crdtEntries)).Msg("No data in CRDT entries");
1576
+ return [];
1577
+ }
1578
+ return Promise.all(
1579
+ crdtEntries.map(async (crdtEntry) => {
1580
+ const eventBlock = await decodeEventBlock(base64pad.decode(crdtEntry.data));
1581
+ const dbMeta = parse(sthis.txt.decode(eventBlock.value.data.dbMeta));
1582
+ return {
1583
+ eventCid: eventBlock.cid,
1584
+ parents: crdtEntry.parents,
1585
+ dbMeta
1586
+ };
1587
+ })
1588
+ );
1589
+ }
1590
+ async function setCryptoKeyFromGatewayMetaPayload(uri, sthis, data) {
1591
+ try {
1592
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Setting crypto key from gateway meta payload");
1593
+ const keyInfo = await decodeGatewayMetaBytesToDbMeta(sthis, data);
1594
+ if (keyInfo.length) {
1595
+ const dbMeta = keyInfo[0].dbMeta;
1596
+ if (dbMeta.key) {
1597
+ const kb = await getKeyBag(sthis);
1598
+ const keyName = getStoreKeyName(uri);
1599
+ const res = await kb.setNamedKey(keyName, dbMeta.key);
1600
+ if (res.isErr()) {
1601
+ sthis.logger.Debug().Str("keyName", keyName).Str("dbMeta.key", dbMeta.key).Msg("Failed to set named key");
1602
+ throw res.Err();
1603
+ }
1604
+ }
1605
+ sthis.logger.Debug().Str("dbMeta.key", dbMeta.key).Str("uri", uri.toString()).Msg("Set crypto key from gateway meta payload");
1606
+ return Result4.Ok(dbMeta);
1607
+ }
1608
+ sthis.logger.Debug().Str("data", new TextDecoder().decode(data)).Msg("No crypto in gateway meta payload");
1609
+ return Result4.Ok(void 0);
1610
+ } catch (error) {
1611
+ sthis.logger.Debug().Err(error).Msg("Failed to set crypto key from gateway meta payload");
1612
+ return Result4.Err(error);
1613
+ }
1614
+ }
1615
+ async function addCryptoKeyToGatewayMetaPayload(uri, sthis, body) {
1616
+ try {
1617
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Adding crypto key to gateway meta payload");
1618
+ const keyName = getStoreKeyName(uri);
1619
+ const kb = await getKeyBag(sthis);
1620
+ const res = await kb.getNamedExtractableKey(keyName, true);
1621
+ if (res.isErr()) {
1622
+ sthis.logger.Error().Str("keyName", keyName).Msg("Failed to get named extractable key");
1623
+ throw res.Err();
1624
+ }
1625
+ const keyData = await res.Ok().extract();
1626
+ const dbMetas = await decodeGatewayMetaBytesToDbMeta(sthis, body);
1627
+ const { dbMeta, parents } = dbMetas[0];
1628
+ const parentLinks = parents.map((p) => CID2.parse(p));
1629
+ dbMeta.key = keyData.keyStr;
1630
+ const events = await Promise.all([dbMeta].map((dbMeta2) => createDbMetaEventBlock(sthis, dbMeta2, parentLinks)));
1631
+ const encoded = await encodeEventsWithParents(sthis, events, parentLinks);
1632
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Added crypto key to gateway meta payload");
1633
+ return Result4.Ok(encoded);
1634
+ } catch (error) {
1635
+ sthis.logger.Error().Err(error).Msg("Failed to add crypto key to gateway meta payload");
1636
+ return Result4.Err(error);
1637
+ }
1638
+ }
1639
+ function getStoreKeyName(url) {
1640
+ const storeKeyName = [url.getParam("localName") || url.getParam("name")];
1641
+ const idx = url.getParam("index");
1642
+ if (idx) {
1643
+ storeKeyName.push(idx);
1644
+ }
1645
+ storeKeyName.push("data");
1646
+ return `@${storeKeyName.join(":")}@`;
1647
+ }
1648
+ async function createDbMetaEventBlock(sthis, dbMeta, parents) {
1649
+ const event = await EventBlock.create(
1650
+ {
1651
+ dbMeta: sthis.txt.encode(format(dbMeta))
1652
+ },
1653
+ parents
1654
+ );
1655
+ return event;
1656
+ }
1657
+ async function encodeEventsWithParents(sthis, events, parents) {
1658
+ const crdtEntries = events.map((event) => {
1659
+ const base64String = base64pad.encode(event.bytes);
1660
+ return {
1661
+ cid: event.cid.toString(),
1662
+ data: base64String,
1663
+ parents: parents.map((p) => p.toString())
1664
+ };
1665
+ });
1666
+ return sthis.txt.encode(JSON.stringify(crdtEntries));
1667
+ }
1668
+
1580
1669
  // src/blockstore/store.ts
1581
1670
  function guardVersion(url) {
1582
1671
  if (!url.hasParam("version")) {
1583
- return Result4.Err(`missing version: ${url.toString()}`);
1672
+ return Result5.Err(`missing version: ${url.toString()}`);
1584
1673
  }
1585
- return Result4.Ok(url);
1674
+ return Result5.Ok(url);
1586
1675
  }
1587
1676
  var BaseStoreImpl = class {
1588
1677
  constructor(name, url, opts, sthis, logger) {
@@ -1605,6 +1694,9 @@ var BaseStoreImpl = class {
1605
1694
  onClosed(fn) {
1606
1695
  this._onClosed.push(fn);
1607
1696
  }
1697
+ async ready() {
1698
+ return;
1699
+ }
1608
1700
  async keyedCrypto() {
1609
1701
  return keyedCryptoFactory(this._url, await this.keybag(), this.sthis);
1610
1702
  }
@@ -1650,18 +1742,87 @@ var BaseStoreImpl = class {
1650
1742
  return version;
1651
1743
  }
1652
1744
  };
1745
+ var MetaStoreImpl = class extends BaseStoreImpl {
1746
+ // remote: boolean;
1747
+ constructor(sthis, name, url, opts) {
1748
+ super(name, url, { ...opts }, sthis, ensureLogger(sthis, "MetaStoreImpl"));
1749
+ this.storeType = "meta";
1750
+ this.subscribers = /* @__PURE__ */ new Map();
1751
+ this.parents = [];
1752
+ if (
1753
+ /*this.remote && */
1754
+ opts.gateway.subscribe
1755
+ ) {
1756
+ this.onStarted(async () => {
1757
+ this.logger.Debug().Str("url", this.url().toString()).Msg("Subscribing to the gateway");
1758
+ opts.gateway.subscribe?.(this.url(), async (message) => {
1759
+ this.logger.Debug().Msg("Received message from gateway");
1760
+ const dbMetas = await decodeGatewayMetaBytesToDbMeta(this.sthis, message);
1761
+ await Promise.all(
1762
+ dbMetas.map((dbMeta) => this.loader?.taskManager?.handleEvent(dbMeta.eventCid, dbMeta.parents, dbMeta.dbMeta))
1763
+ );
1764
+ this.updateParentsFromDbMetas(dbMetas);
1765
+ });
1766
+ });
1767
+ }
1768
+ }
1769
+ updateParentsFromDbMetas(dbMetas) {
1770
+ const cids = dbMetas.map((m) => m.eventCid);
1771
+ const dbMetaParents = dbMetas.flatMap((m) => m.parents);
1772
+ const uniqueParentsMap = new Map([...this.parents, ...cids].map((p) => [p.toString(), p]));
1773
+ const dbMetaParentsSet = new Set(dbMetaParents.map((p) => p.toString()));
1774
+ this.parents = Array.from(uniqueParentsMap.values()).filter((p) => !dbMetaParentsSet.has(p.toString()));
1775
+ }
1776
+ async handleByteHeads(byteHeads) {
1777
+ return await decodeGatewayMetaBytesToDbMeta(this.sthis, byteHeads);
1778
+ }
1779
+ async load() {
1780
+ const branch = "main";
1781
+ const url = await this.gateway.buildUrl(this.url(), branch);
1782
+ if (url.isErr()) {
1783
+ throw this.logger.Error().Result("buildUrl", url).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
1784
+ }
1785
+ const bytes = await this.gateway.get(url.Ok());
1786
+ if (bytes.isErr()) {
1787
+ if (isNotFoundError(bytes)) {
1788
+ return void 0;
1789
+ }
1790
+ throw this.logger.Error().Url(url.Ok()).Result("bytes:", bytes).Msg("gateway get").AsError();
1791
+ }
1792
+ const dbMetas = await this.handleByteHeads(bytes.Ok());
1793
+ await this.loader?.handleDbMetasFromStore(dbMetas.map((m) => m.dbMeta));
1794
+ this.updateParentsFromDbMetas(dbMetas);
1795
+ return dbMetas.map((m) => m.dbMeta);
1796
+ }
1797
+ async save(meta, branch) {
1798
+ branch = branch || "main";
1799
+ this.logger.Debug().Str("branch", branch).Any("meta", meta).Msg("saving meta");
1800
+ const event = await createDbMetaEventBlock(this.sthis, meta, this.parents);
1801
+ const bytes = await encodeEventsWithParents(this.sthis, [event], this.parents);
1802
+ const url = await this.gateway.buildUrl(this.url(), branch);
1803
+ if (url.isErr()) {
1804
+ throw this.logger.Error().Err(url.Err()).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
1805
+ }
1806
+ this.parents = [event.cid];
1807
+ const res = await this.gateway.put(url.Ok(), bytes);
1808
+ if (res.isErr()) {
1809
+ throw this.logger.Error().Err(res.Err()).Msg("got error from gateway.put").AsError();
1810
+ }
1811
+ return res;
1812
+ }
1813
+ async close() {
1814
+ await this.gateway.close(this.url());
1815
+ this._onClosed.forEach((fn) => fn());
1816
+ return Result5.Ok(void 0);
1817
+ }
1818
+ async destroy() {
1819
+ return this.gateway.destroy(this.url());
1820
+ }
1821
+ };
1653
1822
  var DataStoreImpl = class extends BaseStoreImpl {
1654
1823
  // readonly tag: string = "car-base";
1655
1824
  constructor(sthis, name, url, opts) {
1656
- super(
1657
- name,
1658
- url,
1659
- {
1660
- ...opts
1661
- },
1662
- sthis,
1663
- ensureLogger(sthis, "DataStoreImpl")
1664
- );
1825
+ super(name, url, { ...opts }, sthis, ensureLogger(sthis, "DataStoreImpl"));
1665
1826
  this.storeType = "data";
1666
1827
  }
1667
1828
  async load(cid) {
@@ -1699,7 +1860,7 @@ var DataStoreImpl = class extends BaseStoreImpl {
1699
1860
  async close() {
1700
1861
  await this.gateway.close(this.url());
1701
1862
  this._onClosed.forEach((fn) => fn());
1702
- return Result4.Ok(void 0);
1863
+ return Result5.Ok(void 0);
1703
1864
  }
1704
1865
  destroy() {
1705
1866
  return this.gateway.destroy(this.url());
@@ -1707,46 +1868,43 @@ var DataStoreImpl = class extends BaseStoreImpl {
1707
1868
  };
1708
1869
  var WALStoreImpl = class extends BaseStoreImpl {
1709
1870
  constructor(loader, url, opts) {
1710
- super(
1711
- loader.name,
1712
- url,
1713
- {
1714
- ...opts
1715
- },
1716
- loader.sthis,
1717
- ensureLogger(loader.sthis, "WALStoreImpl")
1718
- );
1871
+ super(loader.name, url, { ...opts }, loader.sthis, ensureLogger(loader.sthis, "WALStoreImpl"));
1719
1872
  this.storeType = "wal";
1720
1873
  this._ready = new ResolveOnce3();
1721
1874
  this.walState = { operations: [], noLoaderOps: [], fileOperations: [] };
1722
1875
  this.processing = void 0;
1723
1876
  this.processQueue = new CommitQueue();
1724
- this.ready = async () => {
1725
- return this._ready.once(async () => {
1726
- const walState = await this.load().catch((e) => {
1727
- this.logger.Error().Any("error", e).Msg("error loading wal");
1728
- return void 0;
1729
- });
1730
- if (!walState) {
1731
- this.walState.operations = [];
1732
- this.walState.fileOperations = [];
1733
- } else {
1734
- this.walState.operations = walState.operations || [];
1735
- this.walState.fileOperations = walState.fileOperations || [];
1736
- }
1737
- });
1738
- };
1739
1877
  this.loader = loader;
1740
1878
  }
1879
+ async ready() {
1880
+ return this._ready.once(async () => {
1881
+ const walState = await this.load().catch((e) => {
1882
+ this.logger.Error().Any("error", e).Msg("error loading wal");
1883
+ return void 0;
1884
+ });
1885
+ if (!walState) {
1886
+ this.walState.operations = [];
1887
+ this.walState.fileOperations = [];
1888
+ } else {
1889
+ this.walState.operations = walState.operations || [];
1890
+ this.walState.fileOperations = walState.fileOperations || [];
1891
+ }
1892
+ });
1893
+ }
1741
1894
  async enqueue(dbMeta, opts) {
1742
1895
  await this.ready();
1743
- if (opts.noLoader) {
1896
+ if (opts.compact) {
1897
+ this.walState.operations = [];
1898
+ this.walState.noLoaderOps = [dbMeta];
1899
+ } else if (opts.noLoader) {
1744
1900
  this.walState.noLoaderOps.push(dbMeta);
1745
1901
  } else {
1746
1902
  this.walState.operations.push(dbMeta);
1747
1903
  }
1748
1904
  await this.save(this.walState);
1749
- void this.process();
1905
+ if (!opts.noLoader) {
1906
+ void this.process();
1907
+ }
1750
1908
  }
1751
1909
  async enqueueFile(fileCid, publicFile = false) {
1752
1910
  await this.ready();
@@ -1773,7 +1931,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
1773
1931
  const fileOperations = [...this.walState.fileOperations];
1774
1932
  const uploads = [];
1775
1933
  const noLoaderOps = [...this.walState.noLoaderOps];
1776
- const limit = pLimit2(5);
1934
+ const limit = pLimit2(3);
1777
1935
  if (operations.length + fileOperations.length + noLoaderOps.length === 0) return;
1778
1936
  for (const dbMeta of noLoaderOps) {
1779
1937
  const uploadP = limit(async () => {
@@ -1849,7 +2007,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
1849
2007
  throw this.logger.Error().Err(bytes.Err()).Msg("error get").AsError();
1850
2008
  }
1851
2009
  try {
1852
- return bytes && parse(this.sthis.txt.decode(bytes.Ok()));
2010
+ return bytes && parse2(this.sthis.txt.decode(bytes.Ok()));
1853
2011
  } catch (e) {
1854
2012
  throw this.logger.Error().Err(e).Msg("error parse").AsError();
1855
2013
  }
@@ -1861,7 +2019,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
1861
2019
  }
1862
2020
  let encoded;
1863
2021
  try {
1864
- encoded = format(state);
2022
+ encoded = format2(state);
1865
2023
  } catch (e) {
1866
2024
  throw this.logger.Error().Err(e).Any("state", state).Msg("error format").AsError();
1867
2025
  }
@@ -1870,263 +2028,15 @@ var WALStoreImpl = class extends BaseStoreImpl {
1870
2028
  throw this.logger.Error().Err(res.Err()).Str("filePath", filepath.Ok().toString()).Msg("error saving").AsError();
1871
2029
  }
1872
2030
  }
1873
- async close() {
1874
- await this.gateway.close(this.url());
1875
- this._onClosed.forEach((fn) => fn());
1876
- return Result4.Ok(void 0);
1877
- }
1878
- destroy() {
1879
- return this.gateway.destroy(this.url());
1880
- }
1881
- };
1882
-
1883
- // src/blockstore/store-meta.ts
1884
- import { format as format2, parse as parse2 } from "@ipld/dag-json";
1885
- import { EventBlock, decodeEventBlock } from "@web3-storage/pail/clock";
1886
- import { CID as CID2 } from "multiformats";
1887
- import { Result as Result5 } from "@adviser/cement";
1888
-
1889
- // src/runtime/index.ts
1890
- var runtime_exports = {};
1891
- __export(runtime_exports, {
1892
- FILESTORE_VERSION: () => FILESTORE_VERSION,
1893
- INDEXDB_VERSION: () => INDEXDB_VERSION,
1894
- files: () => files_exports,
1895
- getFileName: () => getFileName,
1896
- getFileSystem: () => getFileSystem,
1897
- getPath: () => getPath,
1898
- kb: () => key_bag_exports,
1899
- kc: () => keyed_crypto_exports,
1900
- mf: () => wait_pr_multiformats_exports,
1901
- runtimeFn: () => runtimeFn2,
1902
- toArrayBuffer: () => toArrayBuffer
1903
- });
1904
-
1905
- // src/runtime/wait-pr-multiformats/index.ts
1906
- var wait_pr_multiformats_exports = {};
1907
- __export(wait_pr_multiformats_exports, {
1908
- block: () => block_exports,
1909
- codec: () => codec_interface_exports
1910
- });
1911
-
1912
- // src/runtime/wait-pr-multiformats/codec-interface.ts
1913
- var codec_interface_exports = {};
1914
-
1915
- // src/runtime/index.ts
1916
- import { runtimeFn as runtimeFn2 } from "@adviser/cement";
1917
-
1918
- // src/blockstore/store-meta.ts
1919
- async function decodeGatewayMetaBytesToDbMeta(sthis, byteHeads) {
1920
- const crdtEntries = JSON.parse(sthis.txt.decode(byteHeads));
1921
- return Promise.all(
1922
- crdtEntries.map(async (crdtEntry) => {
1923
- const eventBlock = await decodeEventBlock(decodeFromBase64(crdtEntry.data));
1924
- const dbMeta = parse2(sthis.txt.decode(eventBlock.value.data.dbMeta));
1925
- return {
1926
- eventCid: eventBlock.cid,
1927
- parents: crdtEntry.parents,
1928
- dbMeta
1929
- };
1930
- })
1931
- );
1932
- }
1933
- async function setCryptoKeyFromGatewayMetaPayload(uri, sthis, data) {
1934
- try {
1935
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Setting crypto key from gateway meta payload");
1936
- const keyInfo = await decodeGatewayMetaBytesToDbMeta(sthis, data);
1937
- if (keyInfo.length) {
1938
- const dbMeta = keyInfo[0].dbMeta;
1939
- if (dbMeta.key) {
1940
- const kb = await key_bag_exports.getKeyBag(sthis);
1941
- const keyName = getStoreKeyName(uri);
1942
- const res = await kb.setNamedKey(keyName, dbMeta.key);
1943
- if (res.isErr()) {
1944
- sthis.logger.Debug().Str("keyName", keyName).Str("dbMeta.key", dbMeta.key).Msg("Failed to set named key");
1945
- throw res.Err();
1946
- }
1947
- }
1948
- sthis.logger.Debug().Str("dbMeta.key", dbMeta.key).Str("uri", uri.toString()).Msg("Set crypto key from gateway meta payload");
1949
- return Result5.Ok(dbMeta);
1950
- }
1951
- sthis.logger.Debug().Str("data", new TextDecoder().decode(data)).Msg("No crypto in gateway meta payload");
1952
- return Result5.Ok(void 0);
1953
- } catch (error) {
1954
- sthis.logger.Debug().Err(error).Msg("Failed to set crypto key from gateway meta payload");
1955
- return Result5.Err(error);
1956
- }
1957
- }
1958
- async function addCryptoKeyToGatewayMetaPayload(uri, sthis, body) {
1959
- try {
1960
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Adding crypto key to gateway meta payload");
1961
- const keyName = getStoreKeyName(uri);
1962
- const kb = await key_bag_exports.getKeyBag(sthis);
1963
- const res = await kb.getNamedExtractableKey(keyName, true);
1964
- if (res.isErr()) {
1965
- sthis.logger.Error().Str("keyName", keyName).Msg("Failed to get named extractable key");
1966
- throw res.Err();
1967
- }
1968
- const keyData = await res.Ok().extract();
1969
- const dbMetas = await decodeGatewayMetaBytesToDbMeta(sthis, body);
1970
- const { dbMeta, parents } = dbMetas[0];
1971
- const parentLinks = parents.map((p) => CID2.parse(p));
1972
- dbMeta.key = keyData.keyStr;
1973
- const events = await Promise.all([dbMeta].map((dbMeta2) => createDbMetaEventBlock(sthis, dbMeta2, parentLinks)));
1974
- const encoded = await encodeEventsWithParents(sthis, events, parentLinks);
1975
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Added crypto key to gateway meta payload");
1976
- return Result5.Ok(encoded);
1977
- } catch (error) {
1978
- sthis.logger.Error().Err(error).Msg("Failed to add crypto key to gateway meta payload");
1979
- return Result5.Err(error);
1980
- }
1981
- }
1982
- function getStoreKeyName(url) {
1983
- const storeKeyName = [url.getParam("localName") || url.getParam("name")];
1984
- const idx = url.getParam("index");
1985
- if (idx) {
1986
- storeKeyName.push(idx);
1987
- }
1988
- storeKeyName.push("data");
1989
- return `@${storeKeyName.join(":")}@`;
1990
- }
1991
- async function createDbMetaEventBlock(sthis, dbMeta, parents) {
1992
- const event = await EventBlock.create(
1993
- {
1994
- dbMeta: sthis.txt.encode(format2(dbMeta))
1995
- },
1996
- parents
1997
- );
1998
- return event;
1999
- }
2000
- async function encodeEventsWithParents(sthis, events, parents) {
2001
- const crdtEntries = events.map((event) => {
2002
- const base64String = encodeToBase64(event.bytes);
2003
- return {
2004
- cid: event.cid.toString(),
2005
- data: base64String,
2006
- parents: parents.map((p) => p.toString())
2007
- };
2008
- });
2009
- return sthis.txt.encode(JSON.stringify(crdtEntries));
2010
- }
2011
- var MetaStoreImpl = class extends BaseStoreImpl {
2012
- constructor(sthis, name, url, opts, remote) {
2013
- super(
2014
- name,
2015
- url,
2016
- {
2017
- ...opts
2018
- },
2019
- sthis,
2020
- ensureLogger(sthis, "MetaStoreImpl")
2021
- );
2022
- this.storeType = "meta";
2023
- this.subscribers = /* @__PURE__ */ new Map();
2024
- this.parents = [];
2025
- if (remote && opts.gateway.subscribe) {
2026
- this.onStarted(async () => {
2027
- this.logger.Debug().Str("url", this.url().toString()).Msg("Subscribing to the gateway");
2028
- opts.gateway.subscribe?.(this.url(), async (message) => {
2029
- this.logger.Debug().Msg("Received message from gateway");
2030
- const dbMetas = await decodeGatewayMetaBytesToDbMeta(this.sthis, message);
2031
- await Promise.all(
2032
- dbMetas.map((dbMeta) => this.loader?.taskManager?.handleEvent(dbMeta.eventCid, dbMeta.parents, dbMeta.dbMeta))
2033
- );
2034
- });
2035
- });
2036
- }
2037
- }
2038
- async handleByteHeads(byteHeads) {
2039
- return await decodeGatewayMetaBytesToDbMeta(this.sthis, byteHeads);
2040
- }
2041
- async load() {
2042
- const branch = "main";
2043
- const url = await this.gateway.buildUrl(this.url(), branch);
2044
- if (url.isErr()) {
2045
- throw this.logger.Error().Result("buildUrl", url).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
2046
- }
2047
- const bytes = await this.gateway.get(url.Ok());
2048
- if (bytes.isErr()) {
2049
- if (isNotFoundError(bytes)) {
2050
- return void 0;
2051
- }
2052
- throw this.logger.Error().Url(url.Ok()).Result("bytes:", bytes).Msg("gateway get").AsError();
2053
- }
2054
- const dbMetas = await this.handleByteHeads(bytes.Ok());
2055
- await this.loader?.handleDbMetasFromStore(dbMetas.map((m) => m.dbMeta));
2056
- const cids = dbMetas.map((m) => m.eventCid);
2057
- const uniqueParentsMap = new Map([...this.parents, ...cids].map((p) => [p.toString(), p]));
2058
- this.parents = Array.from(uniqueParentsMap.values());
2059
- return dbMetas.map((m) => m.dbMeta);
2060
- }
2061
- async save(meta, branch) {
2062
- branch = branch || "main";
2063
- this.logger.Debug().Str("branch", branch).Any("meta", meta).Msg("saving meta");
2064
- const event = await createDbMetaEventBlock(this.sthis, meta, this.parents);
2065
- const bytes = await encodeEventsWithParents(this.sthis, [event], this.parents);
2066
- const url = await this.gateway.buildUrl(this.url(), branch);
2067
- if (url.isErr()) {
2068
- throw this.logger.Error().Err(url.Err()).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
2069
- }
2070
- const res = await this.gateway.put(url.Ok(), bytes);
2071
- if (res.isErr()) {
2072
- throw this.logger.Error().Err(res.Err()).Msg("got error from gateway.put").AsError();
2073
- }
2074
- await this.loader?.handleDbMetasFromStore([meta]);
2075
- this.parents = [event.cid];
2076
- return res;
2077
- }
2078
2031
  async close() {
2079
2032
  await this.gateway.close(this.url());
2080
2033
  this._onClosed.forEach((fn) => fn());
2081
2034
  return Result5.Ok(void 0);
2082
2035
  }
2083
- async destroy() {
2036
+ destroy() {
2084
2037
  return this.gateway.destroy(this.url());
2085
2038
  }
2086
2039
  };
2087
- function encodeToBase64(bytes) {
2088
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2089
- let base64 = "";
2090
- let i;
2091
- for (i = 0; i < bytes.length - 2; i += 3) {
2092
- base64 += chars[bytes[i] >> 2];
2093
- base64 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
2094
- base64 += chars[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6];
2095
- base64 += chars[bytes[i + 2] & 63];
2096
- }
2097
- if (i < bytes.length) {
2098
- base64 += chars[bytes[i] >> 2];
2099
- if (i === bytes.length - 1) {
2100
- base64 += chars[(bytes[i] & 3) << 4];
2101
- base64 += "==";
2102
- } else {
2103
- base64 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
2104
- base64 += chars[(bytes[i + 1] & 15) << 2];
2105
- base64 += "=";
2106
- }
2107
- }
2108
- return base64;
2109
- }
2110
- function decodeFromBase64(base64) {
2111
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2112
- const bytes = new Uint8Array(base64.length * 3 / 4);
2113
- let i;
2114
- let j = 0;
2115
- for (i = 0; i < base64.length; i += 4) {
2116
- const a = chars.indexOf(base64[i]);
2117
- const b = chars.indexOf(base64[i + 1]);
2118
- const c = chars.indexOf(base64[i + 2]);
2119
- const d = chars.indexOf(base64[i + 3]);
2120
- bytes[j++] = a << 2 | b >> 4;
2121
- if (base64[i + 2] !== "=") {
2122
- bytes[j++] = (b & 15) << 4 | c >> 2;
2123
- }
2124
- if (base64[i + 3] !== "=") {
2125
- bytes[j++] = (c & 3) << 6 | d;
2126
- }
2127
- }
2128
- return bytes.slice(0, j);
2129
- }
2130
2040
 
2131
2041
  // src/blockstore/store-factory.ts
2132
2042
  function ensureIsIndex(url, isIndex) {
@@ -2291,22 +2201,22 @@ function toStoreRuntime(opts, sthis) {
2291
2201
  registerStoreProtocol({
2292
2202
  protocol: "file:",
2293
2203
  gateway: async (sthis) => {
2294
- const { FileGateway } = await import("./gateway-VWWKLHUI.js");
2204
+ const { FileGateway } = await import("./gateway-5FCWPX5W.js");
2295
2205
  return new FileGateway(sthis);
2296
2206
  },
2297
2207
  test: async (sthis) => {
2298
- const { FileTestStore } = await import("./gateway-VWWKLHUI.js");
2208
+ const { FileTestStore } = await import("./gateway-5FCWPX5W.js");
2299
2209
  return new FileTestStore(sthis);
2300
2210
  }
2301
2211
  });
2302
2212
  registerStoreProtocol({
2303
2213
  protocol: "indexdb:",
2304
2214
  gateway: async (sthis) => {
2305
- const { IndexDBGateway } = await import("./gateway-7OM6OSYK.js");
2215
+ const { IndexDBGateway } = await import("./gateway-H7UD6TNB.js");
2306
2216
  return new IndexDBGateway(sthis);
2307
2217
  },
2308
2218
  test: async (sthis) => {
2309
- const { IndexDBTestStore } = await import("./gateway-7OM6OSYK.js");
2219
+ const { IndexDBTestStore } = await import("./gateway-H7UD6TNB.js");
2310
2220
  return new IndexDBTestStore(sthis);
2311
2221
  }
2312
2222
  });
@@ -2318,7 +2228,13 @@ async function RemoteDataStore(sthis, name, url, opts) {
2318
2228
  return ds;
2319
2229
  }
2320
2230
  async function RemoteMetaStore(sthis, name, url, opts) {
2321
- const ms = new MetaStoreImpl(sthis, name, url, opts, true);
2231
+ const ms = new MetaStoreImpl(
2232
+ sthis,
2233
+ name,
2234
+ url,
2235
+ opts
2236
+ /* , true*/
2237
+ );
2322
2238
  await ms.start();
2323
2239
  return ms;
2324
2240
  }
@@ -3018,7 +2934,7 @@ function applyHeadQueue(worker, logger) {
3018
2934
  queue.sort((a, b) => b.updates ? 1 : -1);
3019
2935
  const task = queue.shift();
3020
2936
  if (!task) continue;
3021
- await worker(task.newHead, task.prevHead, task.updates !== null).catch((e) => {
2937
+ await worker(task.newHead, task.prevHead, task.updates !== void 0).catch((e) => {
3022
2938
  throw logger.Error().Err(e).Msg("int_applyHead worker error").AsError();
3023
2939
  });
3024
2940
  if (task.updates) {
@@ -3108,6 +3024,7 @@ var CRDTClock = class {
3108
3024
  this.zoomers.add(fn);
3109
3025
  }
3110
3026
  async int_applyHead(newHead, prevHead, localUpdates) {
3027
+ const noLoader = !localUpdates;
3111
3028
  const ogHead = sortClockHead(this.head);
3112
3029
  newHead = sortClockHead(newHead);
3113
3030
  if (compareClockHeads(ogHead, newHead)) {
@@ -3118,7 +3035,6 @@ var CRDTClock = class {
3118
3035
  this.setHead(newHead);
3119
3036
  return;
3120
3037
  }
3121
- const noLoader = !localUpdates;
3122
3038
  if (!this.blockstore) {
3123
3039
  throw this.logger.Error().Msg("missing blockstore").AsError();
3124
3040
  }
@@ -3237,8 +3153,7 @@ var CRDT = class {
3237
3153
  try {
3238
3154
  await Promise.all([this.blockstore.ready(), this.indexBlockstore.ready(), this.clock.ready()]);
3239
3155
  } catch (e) {
3240
- const ee = e;
3241
- throw this.logger.Error().Err(e).Msg(`CRDT is not ready: ${ee.stack}`).AsError();
3156
+ throw this.logger.Error().Err(e).Msg(`CRDT is not ready`).AsError();
3242
3157
  }
3243
3158
  });
3244
3159
  }
@@ -3341,7 +3256,7 @@ var Database = class {
3341
3256
  await this.ready();
3342
3257
  this.logger.Debug().Str("id", doc._id).Msg("put");
3343
3258
  const { _id, ...value } = doc;
3344
- const docId = _id || this.sthis.nextId().str;
3259
+ const docId = _id || this.sthis.timeOrderedNextId().str;
3345
3260
  const result = await this._writeQueue.push({
3346
3261
  id: docId,
3347
3262
  value: {
@@ -3470,9 +3385,38 @@ function makeName(fnString) {
3470
3385
  }
3471
3386
  }
3472
3387
 
3388
+ // src/runtime/index.ts
3389
+ var runtime_exports = {};
3390
+ __export(runtime_exports, {
3391
+ FILESTORE_VERSION: () => FILESTORE_VERSION,
3392
+ INDEXDB_VERSION: () => INDEXDB_VERSION,
3393
+ files: () => files_exports,
3394
+ getFileName: () => getFileName,
3395
+ getFileSystem: () => getFileSystem,
3396
+ getPath: () => getPath,
3397
+ kb: () => key_bag_exports,
3398
+ kc: () => keyed_crypto_exports,
3399
+ mf: () => wait_pr_multiformats_exports,
3400
+ runtimeFn: () => runtimeFn2,
3401
+ toArrayBuffer: () => toArrayBuffer
3402
+ });
3403
+
3404
+ // src/runtime/wait-pr-multiformats/index.ts
3405
+ var wait_pr_multiformats_exports = {};
3406
+ __export(wait_pr_multiformats_exports, {
3407
+ block: () => block_exports,
3408
+ codec: () => codec_interface_exports
3409
+ });
3410
+
3411
+ // src/runtime/wait-pr-multiformats/codec-interface.ts
3412
+ var codec_interface_exports = {};
3413
+
3414
+ // src/runtime/index.ts
3415
+ import { runtimeFn as runtimeFn2 } from "@adviser/cement";
3416
+
3473
3417
  // src/version.ts
3474
3418
  var PACKAGE_VERSION = Object.keys({
3475
- "0.19.11-dev-dryrun": "xxxx"
3419
+ "0.19.11-dev-dryrun4": "xxxx"
3476
3420
  })[0];
3477
3421
  export {
3478
3422
  CRDT,