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

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.cjs CHANGED
@@ -243,6 +243,16 @@ var init_utils = __esm({
243
243
  bin
244
244
  };
245
245
  }
246
+ timeOrderedNextId(now) {
247
+ now = typeof now === "number" ? now : (/* @__PURE__ */ new Date()).getTime();
248
+ const t = (281474976710656 + now).toString(16).replace(/^1/, "");
249
+ const bin = this.crypto.randomBytes(10);
250
+ bin[1] = bin[1] & 240 | (bin[1] | 8 && 11);
251
+ const hex = Array.from(bin).map((i) => i.toString(16).padStart(2, "0")).join("");
252
+ return {
253
+ str: `${t.slice(0, 8)}-${t.slice(8)}-7${hex.slice(0, 3)}-${hex.slice(3, 7)}-${hex.slice(7, 19)}`
254
+ };
255
+ }
246
256
  start() {
247
257
  return Promise.resolve();
248
258
  }
@@ -571,30 +581,21 @@ var init_version = __esm({
571
581
  }
572
582
  });
573
583
 
574
- // src/runtime/gateways/indexdb/version.ts
575
- var INDEXDB_VERSION;
576
- var init_version2 = __esm({
577
- "src/runtime/gateways/indexdb/version.ts"() {
578
- "use strict";
579
- INDEXDB_VERSION = "v0.19-indexdb";
580
- }
581
- });
582
-
583
584
  // src/runtime/gateways/file/gateway.ts
584
585
  var gateway_exports = {};
585
586
  __export(gateway_exports, {
586
587
  FileGateway: () => FileGateway,
587
588
  FileTestStore: () => FileTestStore
588
589
  });
589
- var import_cement12, versionFiles, FileGateway, FileTestStore;
590
+ var import_cement11, versionFiles, FileGateway, FileTestStore;
590
591
  var init_gateway = __esm({
591
592
  "src/runtime/gateways/file/gateway.ts"() {
592
593
  "use strict";
593
594
  init_version();
594
- import_cement12 = require("@adviser/cement");
595
+ import_cement11 = require("@adviser/cement");
595
596
  init_utils();
596
597
  init_utils2();
597
- versionFiles = new import_cement12.KeyedResolvOnce();
598
+ versionFiles = new import_cement11.KeyedResolvOnce();
598
599
  FileGateway = class {
599
600
  get fs() {
600
601
  if (!this._fs) throw this.logger.Error().Msg("fs not initialized").AsError();
@@ -624,7 +625,7 @@ var init_gateway = __esm({
624
625
  });
625
626
  }
626
627
  start(baseURL) {
627
- return (0, import_cement12.exception2Result)(async () => {
628
+ return (0, import_cement11.exception2Result)(async () => {
628
629
  this._fs = await getFileSystem(baseURL);
629
630
  await this.fs.start();
630
631
  const url = baseURL.build();
@@ -639,10 +640,10 @@ var init_gateway = __esm({
639
640
  });
640
641
  }
641
642
  async buildUrl(baseUrl, key) {
642
- return import_cement12.Result.Ok(baseUrl.build().setParam("key", key).URI());
643
+ return import_cement11.Result.Ok(baseUrl.build().setParam("key", key).URI());
643
644
  }
644
645
  async close() {
645
- return import_cement12.Result.Ok(void 0);
646
+ return import_cement11.Result.Ok(void 0);
646
647
  }
647
648
  // abstract buildUrl(baseUrl: URL, key: string): Promise<Result<URL>>;
648
649
  getFilePath(url) {
@@ -651,7 +652,7 @@ var init_gateway = __esm({
651
652
  return this.sthis.pathOps.join(getPath(url, this.sthis), getFileName(url, this.sthis));
652
653
  }
653
654
  async put(url, body) {
654
- return (0, import_cement12.exception2Result)(async () => {
655
+ return (0, import_cement11.exception2Result)(async () => {
655
656
  const file = await this.getFilePath(url);
656
657
  this.logger.Debug().Str("url", url.toString()).Str("file", file).Msg("put");
657
658
  await this.fs.writefile(file, body);
@@ -663,17 +664,17 @@ var init_gateway = __esm({
663
664
  try {
664
665
  const res = await this.fs.readfile(file);
665
666
  this.logger.Debug().Url(url.asURL()).Str("file", file).Msg("get");
666
- return import_cement12.Result.Ok(new Uint8Array(res));
667
+ return import_cement11.Result.Ok(new Uint8Array(res));
667
668
  } catch (e) {
668
669
  if (isNotFoundError(e)) {
669
- return import_cement12.Result.Err(new NotFoundError(`file not found: ${file}`));
670
+ return import_cement11.Result.Err(new NotFoundError(`file not found: ${file}`));
670
671
  }
671
- return import_cement12.Result.Err(e);
672
+ return import_cement11.Result.Err(e);
672
673
  }
673
674
  });
674
675
  }
675
676
  async delete(url) {
676
- return (0, import_cement12.exception2Result)(async () => {
677
+ return (0, import_cement11.exception2Result)(async () => {
677
678
  await this.fs.unlink(this.getFilePath(url));
678
679
  });
679
680
  }
@@ -699,7 +700,7 @@ var init_gateway = __esm({
699
700
  }
700
701
  }
701
702
  }
702
- return import_cement12.Result.Ok(void 0);
703
+ return import_cement11.Result.Ok(void 0);
703
704
  }
704
705
  };
705
706
  FileTestStore = class {
@@ -719,6 +720,15 @@ var init_gateway = __esm({
719
720
  }
720
721
  });
721
722
 
723
+ // src/runtime/gateways/indexdb/version.ts
724
+ var INDEXDB_VERSION;
725
+ var init_version2 = __esm({
726
+ "src/runtime/gateways/indexdb/version.ts"() {
727
+ "use strict";
728
+ INDEXDB_VERSION = "v0.19-indexdb";
729
+ }
730
+ });
731
+
722
732
  // src/runtime/gateways/indexdb/gateway.ts
723
733
  var gateway_exports2 = {};
724
734
  __export(gateway_exports2, {
@@ -779,15 +789,15 @@ function getIndexDBName(iurl, sthis) {
779
789
  dbName
780
790
  };
781
791
  }
782
- var import_idb2, import_cement13, onceIndexDB, IndexDBGateway, IndexDBTestStore;
792
+ var import_idb2, import_cement12, onceIndexDB, IndexDBGateway, IndexDBTestStore;
783
793
  var init_gateway2 = __esm({
784
794
  "src/runtime/gateways/indexdb/gateway.ts"() {
785
795
  "use strict";
786
796
  import_idb2 = require("idb");
787
- import_cement13 = require("@adviser/cement");
797
+ import_cement12 = require("@adviser/cement");
788
798
  init_version2();
789
799
  init_utils();
790
- onceIndexDB = new import_cement13.KeyedResolvOnce();
800
+ onceIndexDB = new import_cement12.KeyedResolvOnce();
791
801
  IndexDBGateway = class {
792
802
  constructor(sthis) {
793
803
  this._db = {};
@@ -795,7 +805,7 @@ var init_gateway2 = __esm({
795
805
  this.sthis = sthis;
796
806
  }
797
807
  async start(baseURL) {
798
- return (0, import_cement13.exception2Result)(async () => {
808
+ return (0, import_cement12.exception2Result)(async () => {
799
809
  this.logger.Debug().Url(baseURL).Msg("starting");
800
810
  await this.sthis.start();
801
811
  const ic = await connectIdb(baseURL, this.sthis);
@@ -805,10 +815,10 @@ var init_gateway2 = __esm({
805
815
  });
806
816
  }
807
817
  async close() {
808
- return import_cement13.Result.Ok(void 0);
818
+ return import_cement12.Result.Ok(void 0);
809
819
  }
810
820
  async destroy(baseUrl) {
811
- return (0, import_cement13.exception2Result)(async () => {
821
+ return (0, import_cement12.exception2Result)(async () => {
812
822
  const type = getStore(baseUrl, this.sthis, joinDBName).name;
813
823
  const idb = this._db;
814
824
  const trans = idb.transaction(type, "readwrite");
@@ -824,7 +834,7 @@ var init_gateway2 = __esm({
824
834
  });
825
835
  }
826
836
  buildUrl(baseUrl, key) {
827
- return Promise.resolve(import_cement13.Result.Ok(baseUrl.build().setParam("key", key).URI()));
837
+ return Promise.resolve(import_cement12.Result.Ok(baseUrl.build().setParam("key", key).URI()));
828
838
  }
829
839
  async get(url) {
830
840
  return exceptionWrapper(async () => {
@@ -835,13 +845,13 @@ var init_gateway2 = __esm({
835
845
  const bytes = await tx.objectStore(store).get(sanitzeKey(key));
836
846
  await tx.done;
837
847
  if (!bytes) {
838
- return import_cement13.Result.Err(new NotFoundError(`missing ${key}`));
848
+ return import_cement12.Result.Err(new NotFoundError(`missing ${key}`));
839
849
  }
840
- return import_cement13.Result.Ok(bytes);
850
+ return import_cement12.Result.Ok(bytes);
841
851
  });
842
852
  }
843
853
  async put(url, value) {
844
- return (0, import_cement13.exception2Result)(async () => {
854
+ return (0, import_cement12.exception2Result)(async () => {
845
855
  const key = getKey(url, this.logger);
846
856
  const store = getStore(url, this.sthis, joinDBName).name;
847
857
  this.logger.Debug().Url(url).Str("key", key).Str("store", store).Msg("putting");
@@ -851,14 +861,14 @@ var init_gateway2 = __esm({
851
861
  });
852
862
  }
853
863
  async delete(url) {
854
- return (0, import_cement13.exception2Result)(async () => {
864
+ return (0, import_cement12.exception2Result)(async () => {
855
865
  const key = getKey(url, this.logger);
856
866
  const store = getStore(url, this.sthis, joinDBName).name;
857
867
  this.logger.Debug().Url(url).Str("key", key).Str("store", store).Msg("deleting");
858
868
  const tx = this._db.transaction([store], "readwrite");
859
869
  await tx.objectStore(store).delete(sanitzeKey(key));
860
870
  await tx.done;
861
- return import_cement13.Result.Ok(void 0);
871
+ return import_cement12.Result.Ok(void 0);
862
872
  });
863
873
  }
864
874
  };
@@ -914,7 +924,7 @@ __export(src_exports, {
914
924
  module.exports = __toCommonJS(src_exports);
915
925
 
916
926
  // src/database.ts
917
- var import_cement17 = require("@adviser/cement");
927
+ var import_cement16 = require("@adviser/cement");
918
928
 
919
929
  // src/write-queue.ts
920
930
  function writeQueue(worker, payload = Infinity, unbounded = false) {
@@ -957,7 +967,7 @@ function writeQueue(worker, payload = Infinity, unbounded = false) {
957
967
  }
958
968
 
959
969
  // src/crdt.ts
960
- var import_cement16 = require("@adviser/cement");
970
+ var import_cement15 = require("@adviser/cement");
961
971
 
962
972
  // src/runtime/wait-pr-multiformats/block.ts
963
973
  var block_exports = {};
@@ -1065,7 +1075,7 @@ function toCIDBlock(block) {
1065
1075
  }
1066
1076
 
1067
1077
  // src/blockstore/store-factory.ts
1068
- var import_cement14 = require("@adviser/cement");
1078
+ var import_cement13 = require("@adviser/cement");
1069
1079
 
1070
1080
  // src/runtime/files.ts
1071
1081
  var files_exports = {};
@@ -1140,8 +1150,8 @@ var UnixFSFileBuilder = class {
1140
1150
 
1141
1151
  // src/blockstore/store.ts
1142
1152
  var import_p_limit2 = __toESM(require("p-limit"), 1);
1143
- var import_dag_json = require("@ipld/dag-json");
1144
- var import_cement9 = require("@adviser/cement");
1153
+ var import_dag_json2 = require("@ipld/dag-json");
1154
+ var import_cement10 = require("@adviser/cement");
1145
1155
 
1146
1156
  // src/types.ts
1147
1157
  function isFalsy(value) {
@@ -1704,7 +1714,7 @@ async function commit(params, t, done, opts = { noLoader: false, compact: false
1704
1714
  }
1705
1715
  async function prepareCarFiles(encoder, threshold, rootBlock, t) {
1706
1716
  const carFiles = [];
1707
- threshold = threshold || 1e3 * 1e3;
1717
+ threshold = threshold || 128e3 * 8;
1708
1718
  let clonedt = new CarTransaction(t.parent, { add: false, noLoader: false });
1709
1719
  clonedt.putSync(rootBlock.cid, rootBlock.bytes);
1710
1720
  let newsize = CBW.blockLength(toCIDBlock(rootBlock));
@@ -1917,7 +1927,8 @@ var Loader = class {
1917
1927
  carLog: this.carLog,
1918
1928
  carStore: fstore,
1919
1929
  WALStore: await this.WALStore(),
1920
- metaStore: await this.metaStore()
1930
+ metaStore: await this.metaStore(),
1931
+ threshold: this.ebOpts.threshold
1921
1932
  };
1922
1933
  return this.commitQueue.enqueue(async () => {
1923
1934
  await this.cacheTransaction(t);
@@ -2427,26 +2438,7 @@ var FragmentGateway = class {
2427
2438
  if (this.innerGW.subscribe) {
2428
2439
  return this.innerGW.subscribe(url, callback);
2429
2440
  } else {
2430
- let lastData = void 0;
2431
- let interval = 100;
2432
- const fetchData = async () => {
2433
- const result = await this.innerGW.get(url);
2434
- if (result.isOk()) {
2435
- const data = result.Ok();
2436
- if (!lastData || !data.every((value, index2) => lastData && value === lastData[index2])) {
2437
- lastData = data;
2438
- callback(data);
2439
- interval = 100;
2440
- } else {
2441
- interval *= 2;
2442
- }
2443
- }
2444
- timeoutId = setTimeout(fetchData, interval);
2445
- };
2446
- let timeoutId = setTimeout(fetchData, interval);
2447
- return import_cement8.Result.Ok(() => {
2448
- clearTimeout(timeoutId);
2449
- });
2441
+ return import_cement8.Result.Err(this.logger.Error().Url(url).Msg("subscribe not supported").AsError());
2450
2442
  }
2451
2443
  }
2452
2444
  async delete(url) {
@@ -2464,12 +2456,119 @@ var FragmentGateway = class {
2464
2456
  }
2465
2457
  };
2466
2458
 
2459
+ // src/blockstore/meta-key-helper.ts
2460
+ var import_dag_json = require("@ipld/dag-json");
2461
+ var import_clock = require("@web3-storage/pail/clock");
2462
+ var import_multiformats2 = require("multiformats");
2463
+ var import_base64 = require("multiformats/bases/base64");
2464
+ var import_cement9 = require("@adviser/cement");
2465
+ async function decodeGatewayMetaBytesToDbMeta(sthis, byteHeads) {
2466
+ const crdtEntries = JSON.parse(sthis.txt.decode(byteHeads));
2467
+ if (!crdtEntries.length) {
2468
+ sthis.logger.Debug().Str("byteHeads", new TextDecoder().decode(byteHeads)).Msg("No CRDT entries found");
2469
+ return [];
2470
+ }
2471
+ if (!crdtEntries.map) {
2472
+ sthis.logger.Debug().Str("crdtEntries", JSON.stringify(crdtEntries)).Msg("No data in CRDT entries");
2473
+ return [];
2474
+ }
2475
+ return Promise.all(
2476
+ crdtEntries.map(async (crdtEntry) => {
2477
+ const eventBlock = await (0, import_clock.decodeEventBlock)(import_base64.base64pad.decode(crdtEntry.data));
2478
+ const dbMeta = (0, import_dag_json.parse)(sthis.txt.decode(eventBlock.value.data.dbMeta));
2479
+ return {
2480
+ eventCid: eventBlock.cid,
2481
+ parents: crdtEntry.parents,
2482
+ dbMeta
2483
+ };
2484
+ })
2485
+ );
2486
+ }
2487
+ async function setCryptoKeyFromGatewayMetaPayload(uri, sthis, data) {
2488
+ try {
2489
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Setting crypto key from gateway meta payload");
2490
+ const keyInfo = await decodeGatewayMetaBytesToDbMeta(sthis, data);
2491
+ if (keyInfo.length) {
2492
+ const dbMeta = keyInfo[0].dbMeta;
2493
+ if (dbMeta.key) {
2494
+ const kb = await getKeyBag(sthis);
2495
+ const keyName = getStoreKeyName(uri);
2496
+ const res = await kb.setNamedKey(keyName, dbMeta.key);
2497
+ if (res.isErr()) {
2498
+ sthis.logger.Debug().Str("keyName", keyName).Str("dbMeta.key", dbMeta.key).Msg("Failed to set named key");
2499
+ throw res.Err();
2500
+ }
2501
+ }
2502
+ sthis.logger.Debug().Str("dbMeta.key", dbMeta.key).Str("uri", uri.toString()).Msg("Set crypto key from gateway meta payload");
2503
+ return import_cement9.Result.Ok(dbMeta);
2504
+ }
2505
+ sthis.logger.Debug().Str("data", new TextDecoder().decode(data)).Msg("No crypto in gateway meta payload");
2506
+ return import_cement9.Result.Ok(void 0);
2507
+ } catch (error) {
2508
+ sthis.logger.Debug().Err(error).Msg("Failed to set crypto key from gateway meta payload");
2509
+ return import_cement9.Result.Err(error);
2510
+ }
2511
+ }
2512
+ async function addCryptoKeyToGatewayMetaPayload(uri, sthis, body) {
2513
+ try {
2514
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Adding crypto key to gateway meta payload");
2515
+ const keyName = getStoreKeyName(uri);
2516
+ const kb = await getKeyBag(sthis);
2517
+ const res = await kb.getNamedExtractableKey(keyName, true);
2518
+ if (res.isErr()) {
2519
+ sthis.logger.Error().Str("keyName", keyName).Msg("Failed to get named extractable key");
2520
+ throw res.Err();
2521
+ }
2522
+ const keyData = await res.Ok().extract();
2523
+ const dbMetas = await decodeGatewayMetaBytesToDbMeta(sthis, body);
2524
+ const { dbMeta, parents } = dbMetas[0];
2525
+ const parentLinks = parents.map((p) => import_multiformats2.CID.parse(p));
2526
+ dbMeta.key = keyData.keyStr;
2527
+ const events = await Promise.all([dbMeta].map((dbMeta2) => createDbMetaEventBlock(sthis, dbMeta2, parentLinks)));
2528
+ const encoded = await encodeEventsWithParents(sthis, events, parentLinks);
2529
+ sthis.logger.Debug().Str("uri", uri.toString()).Msg("Added crypto key to gateway meta payload");
2530
+ return import_cement9.Result.Ok(encoded);
2531
+ } catch (error) {
2532
+ sthis.logger.Error().Err(error).Msg("Failed to add crypto key to gateway meta payload");
2533
+ return import_cement9.Result.Err(error);
2534
+ }
2535
+ }
2536
+ function getStoreKeyName(url) {
2537
+ const storeKeyName = [url.getParam("localName") || url.getParam("name")];
2538
+ const idx = url.getParam("index");
2539
+ if (idx) {
2540
+ storeKeyName.push(idx);
2541
+ }
2542
+ storeKeyName.push("data");
2543
+ return `@${storeKeyName.join(":")}@`;
2544
+ }
2545
+ async function createDbMetaEventBlock(sthis, dbMeta, parents) {
2546
+ const event = await import_clock.EventBlock.create(
2547
+ {
2548
+ dbMeta: sthis.txt.encode((0, import_dag_json.format)(dbMeta))
2549
+ },
2550
+ parents
2551
+ );
2552
+ return event;
2553
+ }
2554
+ async function encodeEventsWithParents(sthis, events, parents) {
2555
+ const crdtEntries = events.map((event) => {
2556
+ const base64String = import_base64.base64pad.encode(event.bytes);
2557
+ return {
2558
+ cid: event.cid.toString(),
2559
+ data: base64String,
2560
+ parents: parents.map((p) => p.toString())
2561
+ };
2562
+ });
2563
+ return sthis.txt.encode(JSON.stringify(crdtEntries));
2564
+ }
2565
+
2467
2566
  // src/blockstore/store.ts
2468
2567
  function guardVersion(url) {
2469
2568
  if (!url.hasParam("version")) {
2470
- return import_cement9.Result.Err(`missing version: ${url.toString()}`);
2569
+ return import_cement10.Result.Err(`missing version: ${url.toString()}`);
2471
2570
  }
2472
- return import_cement9.Result.Ok(url);
2571
+ return import_cement10.Result.Ok(url);
2473
2572
  }
2474
2573
  var BaseStoreImpl = class {
2475
2574
  constructor(name, url, opts, sthis, logger) {
@@ -2492,6 +2591,9 @@ var BaseStoreImpl = class {
2492
2591
  onClosed(fn) {
2493
2592
  this._onClosed.push(fn);
2494
2593
  }
2594
+ async ready() {
2595
+ return;
2596
+ }
2495
2597
  async keyedCrypto() {
2496
2598
  return keyedCryptoFactory(this._url, await this.keybag(), this.sthis);
2497
2599
  }
@@ -2526,7 +2628,7 @@ var BaseStoreImpl = class {
2526
2628
  }
2527
2629
  if (this.ready) {
2528
2630
  const fn = this.ready.bind(this);
2529
- const ready = await (0, import_cement9.exception2Result)(fn);
2631
+ const ready = await (0, import_cement10.exception2Result)(fn);
2530
2632
  if (ready.isErr()) {
2531
2633
  await this.close();
2532
2634
  return ready;
@@ -2537,18 +2639,87 @@ var BaseStoreImpl = class {
2537
2639
  return version;
2538
2640
  }
2539
2641
  };
2642
+ var MetaStoreImpl = class extends BaseStoreImpl {
2643
+ // remote: boolean;
2644
+ constructor(sthis, name, url, opts) {
2645
+ super(name, url, { ...opts }, sthis, ensureLogger(sthis, "MetaStoreImpl"));
2646
+ this.storeType = "meta";
2647
+ this.subscribers = /* @__PURE__ */ new Map();
2648
+ this.parents = [];
2649
+ if (
2650
+ /*this.remote && */
2651
+ opts.gateway.subscribe
2652
+ ) {
2653
+ this.onStarted(async () => {
2654
+ this.logger.Debug().Str("url", this.url().toString()).Msg("Subscribing to the gateway");
2655
+ opts.gateway.subscribe?.(this.url(), async (message) => {
2656
+ this.logger.Debug().Msg("Received message from gateway");
2657
+ const dbMetas = await decodeGatewayMetaBytesToDbMeta(this.sthis, message);
2658
+ await Promise.all(
2659
+ dbMetas.map((dbMeta) => this.loader?.taskManager?.handleEvent(dbMeta.eventCid, dbMeta.parents, dbMeta.dbMeta))
2660
+ );
2661
+ this.updateParentsFromDbMetas(dbMetas);
2662
+ });
2663
+ });
2664
+ }
2665
+ }
2666
+ updateParentsFromDbMetas(dbMetas) {
2667
+ const cids = dbMetas.map((m) => m.eventCid);
2668
+ const dbMetaParents = dbMetas.flatMap((m) => m.parents);
2669
+ const uniqueParentsMap = new Map([...this.parents, ...cids].map((p) => [p.toString(), p]));
2670
+ const dbMetaParentsSet = new Set(dbMetaParents.map((p) => p.toString()));
2671
+ this.parents = Array.from(uniqueParentsMap.values()).filter((p) => !dbMetaParentsSet.has(p.toString()));
2672
+ }
2673
+ async handleByteHeads(byteHeads) {
2674
+ return await decodeGatewayMetaBytesToDbMeta(this.sthis, byteHeads);
2675
+ }
2676
+ async load() {
2677
+ const branch = "main";
2678
+ const url = await this.gateway.buildUrl(this.url(), branch);
2679
+ if (url.isErr()) {
2680
+ throw this.logger.Error().Result("buildUrl", url).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
2681
+ }
2682
+ const bytes = await this.gateway.get(url.Ok());
2683
+ if (bytes.isErr()) {
2684
+ if (isNotFoundError(bytes)) {
2685
+ return void 0;
2686
+ }
2687
+ throw this.logger.Error().Url(url.Ok()).Result("bytes:", bytes).Msg("gateway get").AsError();
2688
+ }
2689
+ const dbMetas = await this.handleByteHeads(bytes.Ok());
2690
+ await this.loader?.handleDbMetasFromStore(dbMetas.map((m) => m.dbMeta));
2691
+ this.updateParentsFromDbMetas(dbMetas);
2692
+ return dbMetas.map((m) => m.dbMeta);
2693
+ }
2694
+ async save(meta, branch) {
2695
+ branch = branch || "main";
2696
+ this.logger.Debug().Str("branch", branch).Any("meta", meta).Msg("saving meta");
2697
+ const event = await createDbMetaEventBlock(this.sthis, meta, this.parents);
2698
+ const bytes = await encodeEventsWithParents(this.sthis, [event], this.parents);
2699
+ const url = await this.gateway.buildUrl(this.url(), branch);
2700
+ if (url.isErr()) {
2701
+ throw this.logger.Error().Err(url.Err()).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
2702
+ }
2703
+ this.parents = [event.cid];
2704
+ const res = await this.gateway.put(url.Ok(), bytes);
2705
+ if (res.isErr()) {
2706
+ throw this.logger.Error().Err(res.Err()).Msg("got error from gateway.put").AsError();
2707
+ }
2708
+ return res;
2709
+ }
2710
+ async close() {
2711
+ await this.gateway.close(this.url());
2712
+ this._onClosed.forEach((fn) => fn());
2713
+ return import_cement10.Result.Ok(void 0);
2714
+ }
2715
+ async destroy() {
2716
+ return this.gateway.destroy(this.url());
2717
+ }
2718
+ };
2540
2719
  var DataStoreImpl = class extends BaseStoreImpl {
2541
2720
  // readonly tag: string = "car-base";
2542
2721
  constructor(sthis, name, url, opts) {
2543
- super(
2544
- name,
2545
- url,
2546
- {
2547
- ...opts
2548
- },
2549
- sthis,
2550
- ensureLogger(sthis, "DataStoreImpl")
2551
- );
2722
+ super(name, url, { ...opts }, sthis, ensureLogger(sthis, "DataStoreImpl"));
2552
2723
  this.storeType = "data";
2553
2724
  }
2554
2725
  async load(cid) {
@@ -2586,7 +2757,7 @@ var DataStoreImpl = class extends BaseStoreImpl {
2586
2757
  async close() {
2587
2758
  await this.gateway.close(this.url());
2588
2759
  this._onClosed.forEach((fn) => fn());
2589
- return import_cement9.Result.Ok(void 0);
2760
+ return import_cement10.Result.Ok(void 0);
2590
2761
  }
2591
2762
  destroy() {
2592
2763
  return this.gateway.destroy(this.url());
@@ -2594,46 +2765,43 @@ var DataStoreImpl = class extends BaseStoreImpl {
2594
2765
  };
2595
2766
  var WALStoreImpl = class extends BaseStoreImpl {
2596
2767
  constructor(loader, url, opts) {
2597
- super(
2598
- loader.name,
2599
- url,
2600
- {
2601
- ...opts
2602
- },
2603
- loader.sthis,
2604
- ensureLogger(loader.sthis, "WALStoreImpl")
2605
- );
2768
+ super(loader.name, url, { ...opts }, loader.sthis, ensureLogger(loader.sthis, "WALStoreImpl"));
2606
2769
  this.storeType = "wal";
2607
- this._ready = new import_cement9.ResolveOnce();
2770
+ this._ready = new import_cement10.ResolveOnce();
2608
2771
  this.walState = { operations: [], noLoaderOps: [], fileOperations: [] };
2609
2772
  this.processing = void 0;
2610
2773
  this.processQueue = new CommitQueue();
2611
- this.ready = async () => {
2612
- return this._ready.once(async () => {
2613
- const walState = await this.load().catch((e) => {
2614
- this.logger.Error().Any("error", e).Msg("error loading wal");
2615
- return void 0;
2616
- });
2617
- if (!walState) {
2618
- this.walState.operations = [];
2619
- this.walState.fileOperations = [];
2620
- } else {
2621
- this.walState.operations = walState.operations || [];
2622
- this.walState.fileOperations = walState.fileOperations || [];
2623
- }
2624
- });
2625
- };
2626
2774
  this.loader = loader;
2627
2775
  }
2776
+ async ready() {
2777
+ return this._ready.once(async () => {
2778
+ const walState = await this.load().catch((e) => {
2779
+ this.logger.Error().Any("error", e).Msg("error loading wal");
2780
+ return void 0;
2781
+ });
2782
+ if (!walState) {
2783
+ this.walState.operations = [];
2784
+ this.walState.fileOperations = [];
2785
+ } else {
2786
+ this.walState.operations = walState.operations || [];
2787
+ this.walState.fileOperations = walState.fileOperations || [];
2788
+ }
2789
+ });
2790
+ }
2628
2791
  async enqueue(dbMeta, opts) {
2629
2792
  await this.ready();
2630
- if (opts.noLoader) {
2793
+ if (opts.compact) {
2794
+ this.walState.operations = [];
2795
+ this.walState.noLoaderOps = [dbMeta];
2796
+ } else if (opts.noLoader) {
2631
2797
  this.walState.noLoaderOps.push(dbMeta);
2632
2798
  } else {
2633
2799
  this.walState.operations.push(dbMeta);
2634
2800
  }
2635
2801
  await this.save(this.walState);
2636
- void this.process();
2802
+ if (!opts.noLoader) {
2803
+ void this.process();
2804
+ }
2637
2805
  }
2638
2806
  async enqueueFile(fileCid, publicFile = false) {
2639
2807
  await this.ready();
@@ -2660,7 +2828,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
2660
2828
  const fileOperations = [...this.walState.fileOperations];
2661
2829
  const uploads = [];
2662
2830
  const noLoaderOps = [...this.walState.noLoaderOps];
2663
- const limit = (0, import_p_limit2.default)(5);
2831
+ const limit = (0, import_p_limit2.default)(3);
2664
2832
  if (operations.length + fileOperations.length + noLoaderOps.length === 0) return;
2665
2833
  for (const dbMeta of noLoaderOps) {
2666
2834
  const uploadP = limit(async () => {
@@ -2736,7 +2904,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
2736
2904
  throw this.logger.Error().Err(bytes.Err()).Msg("error get").AsError();
2737
2905
  }
2738
2906
  try {
2739
- return bytes && (0, import_dag_json.parse)(this.sthis.txt.decode(bytes.Ok()));
2907
+ return bytes && (0, import_dag_json2.parse)(this.sthis.txt.decode(bytes.Ok()));
2740
2908
  } catch (e) {
2741
2909
  throw this.logger.Error().Err(e).Msg("error parse").AsError();
2742
2910
  }
@@ -2748,7 +2916,7 @@ var WALStoreImpl = class extends BaseStoreImpl {
2748
2916
  }
2749
2917
  let encoded;
2750
2918
  try {
2751
- encoded = (0, import_dag_json.format)(state);
2919
+ encoded = (0, import_dag_json2.format)(state);
2752
2920
  } catch (e) {
2753
2921
  throw this.logger.Error().Err(e).Any("state", state).Msg("error format").AsError();
2754
2922
  }
@@ -2760,265 +2928,13 @@ var WALStoreImpl = class extends BaseStoreImpl {
2760
2928
  async close() {
2761
2929
  await this.gateway.close(this.url());
2762
2930
  this._onClosed.forEach((fn) => fn());
2763
- return import_cement9.Result.Ok(void 0);
2931
+ return import_cement10.Result.Ok(void 0);
2764
2932
  }
2765
2933
  destroy() {
2766
2934
  return this.gateway.destroy(this.url());
2767
2935
  }
2768
2936
  };
2769
2937
 
2770
- // src/blockstore/store-meta.ts
2771
- var import_dag_json2 = require("@ipld/dag-json");
2772
- var import_clock = require("@web3-storage/pail/clock");
2773
- var import_multiformats2 = require("multiformats");
2774
- var import_cement11 = require("@adviser/cement");
2775
- init_utils();
2776
-
2777
- // src/runtime/index.ts
2778
- var runtime_exports = {};
2779
- __export(runtime_exports, {
2780
- FILESTORE_VERSION: () => FILESTORE_VERSION,
2781
- INDEXDB_VERSION: () => INDEXDB_VERSION,
2782
- files: () => files_exports,
2783
- getFileName: () => getFileName,
2784
- getFileSystem: () => getFileSystem,
2785
- getPath: () => getPath,
2786
- kb: () => key_bag_exports,
2787
- kc: () => keyed_crypto_exports,
2788
- mf: () => wait_pr_multiformats_exports,
2789
- runtimeFn: () => import_cement10.runtimeFn,
2790
- toArrayBuffer: () => toArrayBuffer
2791
- });
2792
- init_utils2();
2793
-
2794
- // src/runtime/wait-pr-multiformats/index.ts
2795
- var wait_pr_multiformats_exports = {};
2796
- __export(wait_pr_multiformats_exports, {
2797
- block: () => block_exports,
2798
- codec: () => codec_interface_exports
2799
- });
2800
-
2801
- // src/runtime/wait-pr-multiformats/codec-interface.ts
2802
- var codec_interface_exports = {};
2803
-
2804
- // src/runtime/index.ts
2805
- var import_cement10 = require("@adviser/cement");
2806
- init_version();
2807
- init_version2();
2808
-
2809
- // src/blockstore/store-meta.ts
2810
- async function decodeGatewayMetaBytesToDbMeta(sthis, byteHeads) {
2811
- const crdtEntries = JSON.parse(sthis.txt.decode(byteHeads));
2812
- return Promise.all(
2813
- crdtEntries.map(async (crdtEntry) => {
2814
- const eventBlock = await (0, import_clock.decodeEventBlock)(decodeFromBase64(crdtEntry.data));
2815
- const dbMeta = (0, import_dag_json2.parse)(sthis.txt.decode(eventBlock.value.data.dbMeta));
2816
- return {
2817
- eventCid: eventBlock.cid,
2818
- parents: crdtEntry.parents,
2819
- dbMeta
2820
- };
2821
- })
2822
- );
2823
- }
2824
- async function setCryptoKeyFromGatewayMetaPayload(uri, sthis, data) {
2825
- try {
2826
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Setting crypto key from gateway meta payload");
2827
- const keyInfo = await decodeGatewayMetaBytesToDbMeta(sthis, data);
2828
- if (keyInfo.length) {
2829
- const dbMeta = keyInfo[0].dbMeta;
2830
- if (dbMeta.key) {
2831
- const kb = await key_bag_exports.getKeyBag(sthis);
2832
- const keyName = getStoreKeyName(uri);
2833
- const res = await kb.setNamedKey(keyName, dbMeta.key);
2834
- if (res.isErr()) {
2835
- sthis.logger.Debug().Str("keyName", keyName).Str("dbMeta.key", dbMeta.key).Msg("Failed to set named key");
2836
- throw res.Err();
2837
- }
2838
- }
2839
- sthis.logger.Debug().Str("dbMeta.key", dbMeta.key).Str("uri", uri.toString()).Msg("Set crypto key from gateway meta payload");
2840
- return import_cement11.Result.Ok(dbMeta);
2841
- }
2842
- sthis.logger.Debug().Str("data", new TextDecoder().decode(data)).Msg("No crypto in gateway meta payload");
2843
- return import_cement11.Result.Ok(void 0);
2844
- } catch (error) {
2845
- sthis.logger.Debug().Err(error).Msg("Failed to set crypto key from gateway meta payload");
2846
- return import_cement11.Result.Err(error);
2847
- }
2848
- }
2849
- async function addCryptoKeyToGatewayMetaPayload(uri, sthis, body) {
2850
- try {
2851
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Adding crypto key to gateway meta payload");
2852
- const keyName = getStoreKeyName(uri);
2853
- const kb = await key_bag_exports.getKeyBag(sthis);
2854
- const res = await kb.getNamedExtractableKey(keyName, true);
2855
- if (res.isErr()) {
2856
- sthis.logger.Error().Str("keyName", keyName).Msg("Failed to get named extractable key");
2857
- throw res.Err();
2858
- }
2859
- const keyData = await res.Ok().extract();
2860
- const dbMetas = await decodeGatewayMetaBytesToDbMeta(sthis, body);
2861
- const { dbMeta, parents } = dbMetas[0];
2862
- const parentLinks = parents.map((p) => import_multiformats2.CID.parse(p));
2863
- dbMeta.key = keyData.keyStr;
2864
- const events = await Promise.all([dbMeta].map((dbMeta2) => createDbMetaEventBlock(sthis, dbMeta2, parentLinks)));
2865
- const encoded = await encodeEventsWithParents(sthis, events, parentLinks);
2866
- sthis.logger.Debug().Str("uri", uri.toString()).Msg("Added crypto key to gateway meta payload");
2867
- return import_cement11.Result.Ok(encoded);
2868
- } catch (error) {
2869
- sthis.logger.Error().Err(error).Msg("Failed to add crypto key to gateway meta payload");
2870
- return import_cement11.Result.Err(error);
2871
- }
2872
- }
2873
- function getStoreKeyName(url) {
2874
- const storeKeyName = [url.getParam("localName") || url.getParam("name")];
2875
- const idx = url.getParam("index");
2876
- if (idx) {
2877
- storeKeyName.push(idx);
2878
- }
2879
- storeKeyName.push("data");
2880
- return `@${storeKeyName.join(":")}@`;
2881
- }
2882
- async function createDbMetaEventBlock(sthis, dbMeta, parents) {
2883
- const event = await import_clock.EventBlock.create(
2884
- {
2885
- dbMeta: sthis.txt.encode((0, import_dag_json2.format)(dbMeta))
2886
- },
2887
- parents
2888
- );
2889
- return event;
2890
- }
2891
- async function encodeEventsWithParents(sthis, events, parents) {
2892
- const crdtEntries = events.map((event) => {
2893
- const base64String = encodeToBase64(event.bytes);
2894
- return {
2895
- cid: event.cid.toString(),
2896
- data: base64String,
2897
- parents: parents.map((p) => p.toString())
2898
- };
2899
- });
2900
- return sthis.txt.encode(JSON.stringify(crdtEntries));
2901
- }
2902
- var MetaStoreImpl = class extends BaseStoreImpl {
2903
- constructor(sthis, name, url, opts, remote) {
2904
- super(
2905
- name,
2906
- url,
2907
- {
2908
- ...opts
2909
- },
2910
- sthis,
2911
- ensureLogger(sthis, "MetaStoreImpl")
2912
- );
2913
- this.storeType = "meta";
2914
- this.subscribers = /* @__PURE__ */ new Map();
2915
- this.parents = [];
2916
- if (remote && opts.gateway.subscribe) {
2917
- this.onStarted(async () => {
2918
- this.logger.Debug().Str("url", this.url().toString()).Msg("Subscribing to the gateway");
2919
- opts.gateway.subscribe?.(this.url(), async (message) => {
2920
- this.logger.Debug().Msg("Received message from gateway");
2921
- const dbMetas = await decodeGatewayMetaBytesToDbMeta(this.sthis, message);
2922
- await Promise.all(
2923
- dbMetas.map((dbMeta) => this.loader?.taskManager?.handleEvent(dbMeta.eventCid, dbMeta.parents, dbMeta.dbMeta))
2924
- );
2925
- });
2926
- });
2927
- }
2928
- }
2929
- async handleByteHeads(byteHeads) {
2930
- return await decodeGatewayMetaBytesToDbMeta(this.sthis, byteHeads);
2931
- }
2932
- async load() {
2933
- const branch = "main";
2934
- const url = await this.gateway.buildUrl(this.url(), branch);
2935
- if (url.isErr()) {
2936
- throw this.logger.Error().Result("buildUrl", url).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
2937
- }
2938
- const bytes = await this.gateway.get(url.Ok());
2939
- if (bytes.isErr()) {
2940
- if (isNotFoundError(bytes)) {
2941
- return void 0;
2942
- }
2943
- throw this.logger.Error().Url(url.Ok()).Result("bytes:", bytes).Msg("gateway get").AsError();
2944
- }
2945
- const dbMetas = await this.handleByteHeads(bytes.Ok());
2946
- await this.loader?.handleDbMetasFromStore(dbMetas.map((m) => m.dbMeta));
2947
- const cids = dbMetas.map((m) => m.eventCid);
2948
- const uniqueParentsMap = new Map([...this.parents, ...cids].map((p) => [p.toString(), p]));
2949
- this.parents = Array.from(uniqueParentsMap.values());
2950
- return dbMetas.map((m) => m.dbMeta);
2951
- }
2952
- async save(meta, branch) {
2953
- branch = branch || "main";
2954
- this.logger.Debug().Str("branch", branch).Any("meta", meta).Msg("saving meta");
2955
- const event = await createDbMetaEventBlock(this.sthis, meta, this.parents);
2956
- const bytes = await encodeEventsWithParents(this.sthis, [event], this.parents);
2957
- const url = await this.gateway.buildUrl(this.url(), branch);
2958
- if (url.isErr()) {
2959
- throw this.logger.Error().Err(url.Err()).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError();
2960
- }
2961
- const res = await this.gateway.put(url.Ok(), bytes);
2962
- if (res.isErr()) {
2963
- throw this.logger.Error().Err(res.Err()).Msg("got error from gateway.put").AsError();
2964
- }
2965
- await this.loader?.handleDbMetasFromStore([meta]);
2966
- this.parents = [event.cid];
2967
- return res;
2968
- }
2969
- async close() {
2970
- await this.gateway.close(this.url());
2971
- this._onClosed.forEach((fn) => fn());
2972
- return import_cement11.Result.Ok(void 0);
2973
- }
2974
- async destroy() {
2975
- return this.gateway.destroy(this.url());
2976
- }
2977
- };
2978
- function encodeToBase64(bytes) {
2979
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2980
- let base64 = "";
2981
- let i;
2982
- for (i = 0; i < bytes.length - 2; i += 3) {
2983
- base64 += chars[bytes[i] >> 2];
2984
- base64 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
2985
- base64 += chars[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6];
2986
- base64 += chars[bytes[i + 2] & 63];
2987
- }
2988
- if (i < bytes.length) {
2989
- base64 += chars[bytes[i] >> 2];
2990
- if (i === bytes.length - 1) {
2991
- base64 += chars[(bytes[i] & 3) << 4];
2992
- base64 += "==";
2993
- } else {
2994
- base64 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
2995
- base64 += chars[(bytes[i + 1] & 15) << 2];
2996
- base64 += "=";
2997
- }
2998
- }
2999
- return base64;
3000
- }
3001
- function decodeFromBase64(base64) {
3002
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3003
- const bytes = new Uint8Array(base64.length * 3 / 4);
3004
- let i;
3005
- let j = 0;
3006
- for (i = 0; i < base64.length; i += 4) {
3007
- const a = chars.indexOf(base64[i]);
3008
- const b = chars.indexOf(base64[i + 1]);
3009
- const c = chars.indexOf(base64[i + 2]);
3010
- const d = chars.indexOf(base64[i + 3]);
3011
- bytes[j++] = a << 2 | b >> 4;
3012
- if (base64[i + 2] !== "=") {
3013
- bytes[j++] = (b & 15) << 4 | c >> 2;
3014
- }
3015
- if (base64[i + 3] !== "=") {
3016
- bytes[j++] = (c & 3) << 6 | d;
3017
- }
3018
- }
3019
- return bytes.slice(0, j);
3020
- }
3021
-
3022
2938
  // src/blockstore/store-factory.ts
3023
2939
  init_utils();
3024
2940
  function ensureIsIndex(url, isIndex) {
@@ -3039,15 +2955,15 @@ function buildURL(optURL, loader) {
3039
2955
  const obuItem = Array.from(storeFactory.values()).find((items) => items.overrideBaseURL);
3040
2956
  let obuUrl;
3041
2957
  if (obuItem && obuItem.overrideBaseURL) {
3042
- obuUrl = import_cement14.URI.from(obuItem.overrideBaseURL);
2958
+ obuUrl = import_cement13.URI.from(obuItem.overrideBaseURL);
3043
2959
  }
3044
2960
  const ret = ensureIsIndex(
3045
- import_cement14.URI.from(optURL || obuUrl || dataDir(loader.sthis, loader.name, storeOpts.stores?.base)),
2961
+ import_cement13.URI.from(optURL || obuUrl || dataDir(loader.sthis, loader.name, storeOpts.stores?.base)),
3046
2962
  storeOpts.isIndex
3047
2963
  );
3048
2964
  return ret;
3049
2965
  }
3050
- var onceGateway = new import_cement14.KeyedResolvOnce();
2966
+ var onceGateway = new import_cement13.KeyedResolvOnce();
3051
2967
  async function getGatewayFromURL(url, sthis) {
3052
2968
  return onceGateway.get(url.toString()).once(async () => {
3053
2969
  const item = storeFactory.get(url.protocol);
@@ -3089,7 +3005,7 @@ function registerStoreProtocol(item) {
3089
3005
  storeFactory.delete(protocol);
3090
3006
  };
3091
3007
  }
3092
- var onceDataStoreFactory = new import_cement14.KeyedResolvOnce();
3008
+ var onceDataStoreFactory = new import_cement13.KeyedResolvOnce();
3093
3009
  async function dataStoreFactory(loader) {
3094
3010
  const url = ensureName(loader.name, buildURL(loader.ebOpts.store.stores?.data, loader)).build().setParam("store", "data").URI();
3095
3011
  const sthis = ensureSuperLog(loader.sthis, "dataStoreFactory", { url: url.toString() });
@@ -3107,7 +3023,7 @@ async function dataStoreFactory(loader) {
3107
3023
  return store;
3108
3024
  });
3109
3025
  }
3110
- var onceMetaStoreFactory = new import_cement14.KeyedResolvOnce();
3026
+ var onceMetaStoreFactory = new import_cement13.KeyedResolvOnce();
3111
3027
  async function metaStoreFactory(loader) {
3112
3028
  const url = ensureName(loader.name, buildURL(loader.ebOpts.store.stores?.meta, loader)).build().setParam("store", "meta").URI();
3113
3029
  const sthis = ensureSuperLog(loader.sthis, "metaStoreFactory", { url: () => url.toString() });
@@ -3126,7 +3042,7 @@ async function metaStoreFactory(loader) {
3126
3042
  return store;
3127
3043
  });
3128
3044
  }
3129
- var onceRemoteWalFactory = new import_cement14.KeyedResolvOnce();
3045
+ var onceRemoteWalFactory = new import_cement13.KeyedResolvOnce();
3130
3046
  async function remoteWalFactory(loader) {
3131
3047
  const url = ensureName(loader.name, buildURL(loader.ebOpts.store.stores?.wal, loader)).build().setParam("store", "wal").URI();
3132
3048
  const sthis = ensureSuperLog(loader.sthis, "remoteWalFactory", { url: url.toString() });
@@ -3210,7 +3126,13 @@ async function RemoteDataStore(sthis, name, url, opts) {
3210
3126
  return ds;
3211
3127
  }
3212
3128
  async function RemoteMetaStore(sthis, name, url, opts) {
3213
- const ms = new MetaStoreImpl(sthis, name, url, opts, true);
3129
+ const ms = new MetaStoreImpl(
3130
+ sthis,
3131
+ name,
3132
+ url,
3133
+ opts
3134
+ /* , true*/
3135
+ );
3214
3136
  await ms.start();
3215
3137
  return ms;
3216
3138
  }
@@ -3570,7 +3492,7 @@ var import_sha26 = require("multiformats/hashes/sha2");
3570
3492
  var codec2 = __toESM(require("@ipld/dag-cbor"), 1);
3571
3493
  var import_charwise = __toESM(require("charwise"), 1);
3572
3494
  var DbIndex = __toESM(require("prolly-trees/db-index"), 1);
3573
- var import_utils17 = require("prolly-trees/utils");
3495
+ var import_utils16 = require("prolly-trees/utils");
3574
3496
  var import_cache = require("prolly-trees/cache");
3575
3497
  var IndexTree = class {
3576
3498
  };
@@ -3578,17 +3500,17 @@ function refCompare(aRef, bRef) {
3578
3500
  if (Number.isNaN(aRef)) return -1;
3579
3501
  if (Number.isNaN(bRef)) throw new Error("ref may not be Infinity or NaN");
3580
3502
  if (aRef === Infinity) return 1;
3581
- return (0, import_utils17.simpleCompare)(aRef, bRef);
3503
+ return (0, import_utils16.simpleCompare)(aRef, bRef);
3582
3504
  }
3583
3505
  function compare(a, b) {
3584
3506
  const [aKey, aRef] = a;
3585
3507
  const [bKey, bRef] = b;
3586
- const comp = (0, import_utils17.simpleCompare)(aKey, bKey);
3508
+ const comp = (0, import_utils16.simpleCompare)(aKey, bKey);
3587
3509
  if (comp !== 0) return comp;
3588
3510
  return refCompare(aRef, bRef);
3589
3511
  }
3590
- var byKeyOpts = { cache: import_cache.nocache, chunker: (0, import_utils17.bf)(30), codec: codec2, hasher: import_sha26.sha256, compare };
3591
- var byIdOpts = { cache: import_cache.nocache, chunker: (0, import_utils17.bf)(30), codec: codec2, hasher: import_sha26.sha256, compare: import_utils17.simpleCompare };
3512
+ var byKeyOpts = { cache: import_cache.nocache, chunker: (0, import_utils16.bf)(30), codec: codec2, hasher: import_sha26.sha256, compare };
3513
+ var byIdOpts = { cache: import_cache.nocache, chunker: (0, import_utils16.bf)(30), codec: codec2, hasher: import_sha26.sha256, compare: import_utils16.simpleCompare };
3592
3514
  function indexEntriesForChanges(changes, mapFn) {
3593
3515
  const indexEntries = [];
3594
3516
  changes.forEach(({ id: key, value, del }) => {
@@ -3896,7 +3818,7 @@ var Index = class {
3896
3818
  // src/crdt-clock.ts
3897
3819
  var import_clock3 = require("@web3-storage/pail/clock");
3898
3820
  var import_crdt2 = require("@web3-storage/pail/crdt");
3899
- var import_cement15 = require("@adviser/cement");
3821
+ var import_cement14 = require("@adviser/cement");
3900
3822
 
3901
3823
  // src/apply-head-queue.ts
3902
3824
  function applyHeadQueue(worker, logger) {
@@ -3911,7 +3833,7 @@ function applyHeadQueue(worker, logger) {
3911
3833
  queue.sort((a, b) => b.updates ? 1 : -1);
3912
3834
  const task = queue.shift();
3913
3835
  if (!task) continue;
3914
- await worker(task.newHead, task.prevHead, task.updates !== null).catch((e) => {
3836
+ await worker(task.newHead, task.prevHead, task.updates !== void 0).catch((e) => {
3915
3837
  throw logger.Error().Err(e).Msg("int_applyHead worker error").AsError();
3916
3838
  });
3917
3839
  if (task.updates) {
@@ -3954,7 +3876,7 @@ var CRDTClock = class {
3954
3876
  this.zoomers = /* @__PURE__ */ new Set();
3955
3877
  this.watchers = /* @__PURE__ */ new Set();
3956
3878
  this.emptyWatchers = /* @__PURE__ */ new Set();
3957
- this._ready = new import_cement15.ResolveOnce();
3879
+ this._ready = new import_cement14.ResolveOnce();
3958
3880
  this.blockstore = blockstore;
3959
3881
  this.logger = ensureLogger(blockstore.sthis, "CRDTClock");
3960
3882
  this.applyHeadQueue = applyHeadQueue(this.int_applyHead.bind(this), this.logger);
@@ -4002,6 +3924,7 @@ var CRDTClock = class {
4002
3924
  this.zoomers.add(fn);
4003
3925
  }
4004
3926
  async int_applyHead(newHead, prevHead, localUpdates) {
3927
+ const noLoader = !localUpdates;
4005
3928
  const ogHead = sortClockHead(this.head);
4006
3929
  newHead = sortClockHead(newHead);
4007
3930
  if (compareClockHeads(ogHead, newHead)) {
@@ -4012,7 +3935,6 @@ var CRDTClock = class {
4012
3935
  this.setHead(newHead);
4013
3936
  return;
4014
3937
  }
4015
- const noLoader = !localUpdates;
4016
3938
  if (!this.blockstore) {
4017
3939
  throw this.logger.Error().Msg("missing blockstore").AsError();
4018
3940
  }
@@ -4066,7 +3988,7 @@ init_utils();
4066
3988
  var CRDT = class {
4067
3989
  constructor(sthis, name, opts = {}) {
4068
3990
  this.indexers = /* @__PURE__ */ new Map();
4069
- this.onceReady = new import_cement16.ResolveOnce();
3991
+ this.onceReady = new import_cement15.ResolveOnce();
4070
3992
  this.sthis = sthis;
4071
3993
  this.name = name;
4072
3994
  this.logger = ensureLogger(sthis, "CRDT");
@@ -4132,8 +4054,7 @@ var CRDT = class {
4132
4054
  try {
4133
4055
  await Promise.all([this.blockstore.ready(), this.indexBlockstore.ready(), this.clock.ready()]);
4134
4056
  } catch (e) {
4135
- const ee = e;
4136
- throw this.logger.Error().Err(e).Msg(`CRDT is not ready: ${ee.stack}`).AsError();
4057
+ throw this.logger.Error().Err(e).Msg(`CRDT is not ready`).AsError();
4137
4058
  }
4138
4059
  });
4139
4060
  }
@@ -4188,7 +4109,7 @@ var Database = class {
4188
4109
  this._listening = false;
4189
4110
  this._listeners = /* @__PURE__ */ new Set();
4190
4111
  this._noupdate_listeners = /* @__PURE__ */ new Set();
4191
- this._ready = new import_cement17.ResolveOnce();
4112
+ this._ready = new import_cement16.ResolveOnce();
4192
4113
  this.name = name;
4193
4114
  this.opts = opts || this.opts;
4194
4115
  this.sthis = ensureSuperThis(this.opts);
@@ -4237,7 +4158,7 @@ var Database = class {
4237
4158
  await this.ready();
4238
4159
  this.logger.Debug().Str("id", doc._id).Msg("put");
4239
4160
  const { _id, ...value } = doc;
4240
- const docId = _id || this.sthis.nextId().str;
4161
+ const docId = _id || this.sthis.timeOrderedNextId().str;
4241
4162
  const result = await this._writeQueue.push({
4242
4163
  id: docId,
4243
4164
  value: {
@@ -4366,11 +4287,43 @@ function makeName(fnString) {
4366
4287
  }
4367
4288
  }
4368
4289
 
4290
+ // src/runtime/index.ts
4291
+ var runtime_exports = {};
4292
+ __export(runtime_exports, {
4293
+ FILESTORE_VERSION: () => FILESTORE_VERSION,
4294
+ INDEXDB_VERSION: () => INDEXDB_VERSION,
4295
+ files: () => files_exports,
4296
+ getFileName: () => getFileName,
4297
+ getFileSystem: () => getFileSystem,
4298
+ getPath: () => getPath,
4299
+ kb: () => key_bag_exports,
4300
+ kc: () => keyed_crypto_exports,
4301
+ mf: () => wait_pr_multiformats_exports,
4302
+ runtimeFn: () => import_cement17.runtimeFn,
4303
+ toArrayBuffer: () => toArrayBuffer
4304
+ });
4305
+ init_utils2();
4306
+
4307
+ // src/runtime/wait-pr-multiformats/index.ts
4308
+ var wait_pr_multiformats_exports = {};
4309
+ __export(wait_pr_multiformats_exports, {
4310
+ block: () => block_exports,
4311
+ codec: () => codec_interface_exports
4312
+ });
4313
+
4314
+ // src/runtime/wait-pr-multiformats/codec-interface.ts
4315
+ var codec_interface_exports = {};
4316
+
4317
+ // src/runtime/index.ts
4318
+ var import_cement17 = require("@adviser/cement");
4319
+ init_version();
4320
+ init_version2();
4321
+
4369
4322
  // src/index.ts
4370
4323
  init_utils();
4371
4324
 
4372
4325
  // src/version.ts
4373
4326
  var PACKAGE_VERSION = Object.keys({
4374
- "0.19.11-dev-dryrun": "xxxx"
4327
+ "0.19.11-dev-dryrun2": "xxxx"
4375
4328
  })[0];
4376
4329
  //# sourceMappingURL=index.cjs.map