@fireproof/core 0.17.5 → 0.17.6

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 (35) hide show
  1. package/README.md +1 -0
  2. package/dist/browser/fireproof.cjs +7 -3
  3. package/dist/browser/fireproof.cjs.map +1 -1
  4. package/dist/browser/fireproof.d.cts +1 -0
  5. package/dist/browser/fireproof.d.ts +1 -0
  6. package/dist/browser/fireproof.global.js +99 -18
  7. package/dist/browser/fireproof.global.js.map +1 -1
  8. package/dist/browser/fireproof.js +7 -3
  9. package/dist/browser/fireproof.js.map +1 -1
  10. package/dist/browser/metafile-cjs.json +1 -1
  11. package/dist/browser/metafile-esm.json +1 -1
  12. package/dist/browser/metafile-iife.json +1 -1
  13. package/dist/memory/fireproof.cjs +7 -3
  14. package/dist/memory/fireproof.cjs.map +1 -1
  15. package/dist/memory/fireproof.d.cts +1 -0
  16. package/dist/memory/fireproof.d.ts +1 -0
  17. package/dist/memory/fireproof.global.js +99 -18
  18. package/dist/memory/fireproof.global.js.map +1 -1
  19. package/dist/memory/fireproof.js +7 -3
  20. package/dist/memory/fireproof.js.map +1 -1
  21. package/dist/memory/metafile-cjs.json +1 -1
  22. package/dist/memory/metafile-esm.json +1 -1
  23. package/dist/memory/metafile-iife.json +1 -1
  24. package/dist/node/fireproof.cjs +7 -3
  25. package/dist/node/fireproof.cjs.map +1 -1
  26. package/dist/node/fireproof.d.cts +1 -0
  27. package/dist/node/fireproof.d.ts +1 -0
  28. package/dist/node/fireproof.global.js +99 -18
  29. package/dist/node/fireproof.global.js.map +1 -1
  30. package/dist/node/fireproof.js +7 -3
  31. package/dist/node/fireproof.js.map +1 -1
  32. package/dist/node/metafile-cjs.json +1 -1
  33. package/dist/node/metafile-esm.json +1 -1
  34. package/dist/node/metafile-iife.json +1 -1
  35. package/package.json +2 -2
@@ -9968,7 +9968,7 @@ You can use close({ resize: true }) to resize header`);
9968
9968
  }
9969
9969
  return bytes;
9970
9970
  }
9971
- var PACKAGE_VERSION = "0.17.4";
9971
+ var PACKAGE_VERSION = "0.17.5";
9972
9972
  var match = PACKAGE_VERSION.match(/^([^.]*\.[^.]*)/);
9973
9973
  if (!match)
9974
9974
  throw new Error("invalid version: " + PACKAGE_VERSION);
@@ -10038,6 +10038,7 @@ You can use close({ resize: true }) to resize header`);
10038
10038
  key;
10039
10039
  keyId;
10040
10040
  seenCompacted = /* @__PURE__ */ new Set();
10041
+ processedCars = /* @__PURE__ */ new Set();
10041
10042
  writing = Promise.resolve();
10042
10043
  getBlockCache = /* @__PURE__ */ new Map();
10043
10044
  seenMeta = /* @__PURE__ */ new Set();
@@ -10126,7 +10127,7 @@ You can use close({ resize: true }) to resize header`);
10126
10127
  // can these skip the queue? or have a file queue?
10127
10128
  async _commitInternalFiles(t, done, opts = { noLoader: false, compact: false }) {
10128
10129
  await this.ready;
10129
- const { files: roots } = this.makeFileCarHeader(done, this.carLog, !!opts.compact);
10130
+ const { files: roots } = this.makeFileCarHeader(done);
10130
10131
  const { cid, bytes } = await this.prepareCarFile(roots[0], t, !!opts.public);
10131
10132
  await this.fileStore.save({ cid, bytes });
10132
10133
  await this.remoteWAL.enqueueFile(cid, !!opts.public);
@@ -10146,7 +10147,10 @@ You can use close({ resize: true }) to resize header`);
10146
10147
  }
10147
10148
  }
10148
10149
  }
10149
- async cacheCarReader(reader) {
10150
+ async cacheCarReader(carCidStr, reader) {
10151
+ if (this.processedCars.has(carCidStr))
10152
+ return;
10153
+ this.processedCars.add(carCidStr);
10150
10154
  for await (const block of reader.blocks()) {
10151
10155
  const sBlock = block.cid.toString();
10152
10156
  if (!this.getBlockCache.has(sBlock)) {
@@ -10176,7 +10180,7 @@ You can use close({ resize: true }) to resize header`);
10176
10180
  const theKey = isPublic ? null : await this._getKey();
10177
10181
  return theKey && this.ebOpts.crypto ? await encryptedEncodeCarFile(this.ebOpts.crypto, theKey, root2, t) : await encodeCarFile([root2], t);
10178
10182
  }
10179
- makeFileCarHeader(result, cars, compact = false) {
10183
+ makeFileCarHeader(result) {
10180
10184
  const files = [];
10181
10185
  for (const [, meta] of Object.entries(result.files)) {
10182
10186
  if (meta && typeof meta === "object" && "cid" in meta && meta !== null) {
@@ -10211,10 +10215,27 @@ You can use close({ resize: true }) to resize header`);
10211
10215
  // await this.remoteWAL!.enqueue(dbMeta, { public: false })
10212
10216
  // }
10213
10217
  // }
10214
- async *entries() {
10218
+ async *entries(cache22 = true) {
10215
10219
  await this.ready;
10216
- for (const [, block] of this.getBlockCache) {
10217
- yield block;
10220
+ if (cache22) {
10221
+ for (const [, block] of this.getBlockCache) {
10222
+ yield block;
10223
+ }
10224
+ } else {
10225
+ for (const [, block] of this.getBlockCache) {
10226
+ yield block;
10227
+ }
10228
+ for (const cid of this.carLog) {
10229
+ const reader = await this.loadCar(cid);
10230
+ if (!reader)
10231
+ throw new Error(`missing car reader ${cid.toString()}`);
10232
+ for await (const block of reader.blocks()) {
10233
+ const sCid = block.cid.toString();
10234
+ if (!this.getBlockCache.has(sCid)) {
10235
+ yield block;
10236
+ }
10237
+ }
10238
+ }
10218
10239
  }
10219
10240
  }
10220
10241
  async getBlock(cid) {
@@ -10227,11 +10248,37 @@ You can use close({ resize: true }) to resize header`);
10227
10248
  if (!reader) {
10228
10249
  throw new Error(`missing car reader ${carCid.toString()}`);
10229
10250
  }
10230
- await this.cacheCarReader(reader);
10251
+ await this.cacheCarReader(carCid.toString(), reader).catch((e) => {
10252
+ });
10231
10253
  if (this.getBlockCache.has(sCid))
10232
10254
  return this.getBlockCache.get(sCid);
10233
10255
  throw new Error(`block not in reader: ${cid.toString()}`);
10234
10256
  };
10257
+ const getCompactCarCids = async (carCid) => {
10258
+ const reader = await this.loadCar(carCid);
10259
+ if (!reader) {
10260
+ throw new Error(`missing car reader ${carCid.toString()}`);
10261
+ }
10262
+ const header = await parseCarFile(reader);
10263
+ const compacts = header.compact;
10264
+ let got2;
10265
+ const batchSize2 = 5;
10266
+ for (let i = 0; i < compacts.length; i += batchSize2) {
10267
+ const promises = [];
10268
+ for (let j = i; j < Math.min(i + batchSize2, compacts.length); j++) {
10269
+ promises.push(getCarCid(compacts[j]));
10270
+ }
10271
+ try {
10272
+ got2 = await Promise.any(promises);
10273
+ } catch {
10274
+ }
10275
+ if (got2)
10276
+ break;
10277
+ }
10278
+ if (this.getBlockCache.has(sCid))
10279
+ return this.getBlockCache.get(sCid);
10280
+ throw new Error(`block not in compact reader: ${cid.toString()}`);
10281
+ };
10235
10282
  let got;
10236
10283
  const batchSize = 5;
10237
10284
  for (let i = 0; i < this.carLog.length; i += batchSize) {
@@ -10246,8 +10293,19 @@ You can use close({ resize: true }) to resize header`);
10246
10293
  if (got)
10247
10294
  break;
10248
10295
  }
10249
- if (got) {
10250
- this.getBlockCache.set(sCid, got);
10296
+ if (!got) {
10297
+ for (let i = 0; i < this.carLog.length; i += batchSize) {
10298
+ const promises = [];
10299
+ for (let j = i; j < Math.min(i + batchSize, this.carLog.length); j++) {
10300
+ promises.push(getCompactCarCids(this.carLog[j]));
10301
+ }
10302
+ try {
10303
+ got = await Promise.any(promises);
10304
+ } catch {
10305
+ }
10306
+ if (got)
10307
+ break;
10308
+ }
10251
10309
  }
10252
10310
  return got;
10253
10311
  }
@@ -10354,6 +10412,7 @@ You can use close({ resize: true }) to resize header`);
10354
10412
  compacting = false;
10355
10413
  ebOpts;
10356
10414
  transactions = /* @__PURE__ */ new Set();
10415
+ lastTxMeta = null;
10357
10416
  constructor(ebOpts) {
10358
10417
  this.ebOpts = ebOpts;
10359
10418
  const { name: name7 } = ebOpts;
@@ -10368,6 +10427,7 @@ You can use close({ resize: true }) to resize header`);
10368
10427
  async transaction(fn, opts = { noLoader: false }) {
10369
10428
  const t = new CarTransaction(this);
10370
10429
  const done = await fn(t);
10430
+ this.lastTxMeta = done;
10371
10431
  if (this.loader) {
10372
10432
  const car = await this.loader.commit(t, done, opts);
10373
10433
  if (this.ebOpts.autoCompact && this.loader.carLog.length > this.ebOpts.autoCompact) {
@@ -10413,7 +10473,7 @@ You can use close({ resize: true }) to resize header`);
10413
10473
  throw new Error("loader required to compact");
10414
10474
  if (this.loader.carLog.length < 2)
10415
10475
  return;
10416
- const compactFn = this.ebOpts.compact;
10476
+ const compactFn = this.ebOpts.compact || ((blocks) => this.defaultCompact(blocks));
10417
10477
  if (!compactFn || this.compacting)
10418
10478
  return;
10419
10479
  const blockLog = new CompactionFetcher(this);
@@ -10425,6 +10485,23 @@ You can use close({ resize: true }) to resize header`);
10425
10485
  });
10426
10486
  this.compacting = false;
10427
10487
  }
10488
+ async defaultCompact(blocks) {
10489
+ if (!this.loader) {
10490
+ throw new Error("no loader");
10491
+ }
10492
+ if (!this.lastTxMeta) {
10493
+ throw new Error("no lastTxMeta");
10494
+ }
10495
+ for await (const blk of this.loader.entries(false)) {
10496
+ blocks.loggedBlocks.putSync(blk.cid, blk.bytes);
10497
+ }
10498
+ for (const t of this.transactions) {
10499
+ for await (const blk of t.entries()) {
10500
+ blocks.loggedBlocks.putSync(blk.cid, blk.bytes);
10501
+ }
10502
+ }
10503
+ return this.lastTxMeta;
10504
+ }
10428
10505
  async *entries() {
10429
10506
  const seen = /* @__PURE__ */ new Set();
10430
10507
  if (this.loader) {
@@ -10694,7 +10771,7 @@ You can use close({ resize: true }) to resize header`);
10694
10771
  }));
10695
10772
 
10696
10773
  // ../encrypted-blockstore/dist/lib/store-web.js
10697
- var PACKAGE_VERSION2 = "0.17.4";
10774
+ var PACKAGE_VERSION2 = "0.17.5";
10698
10775
  var match2 = PACKAGE_VERSION2.match(/^([^.]*\.[^.]*)/);
10699
10776
  if (!match2)
10700
10777
  throw new Error("invalid version: " + PACKAGE_VERSION2);
@@ -10832,7 +10909,7 @@ You can use close({ resize: true }) to resize header`);
10832
10909
  const car = await this.loader.carStore.load(dbMeta.car).catch(() => null);
10833
10910
  if (!car) {
10834
10911
  if (cidListIncludes2(this.loader.carLog, dbMeta.car))
10835
- throw new Error(`missing car ${dbMeta.car.toString()}`);
10912
+ throw new Error(`missing local car ${dbMeta.car.toString()}`);
10836
10913
  } else {
10837
10914
  await this.loader.remoteCarStore.save(car);
10838
10915
  }
@@ -10845,7 +10922,7 @@ You can use close({ resize: true }) to resize header`);
10845
10922
  const car = await this.loader.carStore.load(dbMeta.car).catch(() => null);
10846
10923
  if (!car) {
10847
10924
  if (cidListIncludes2(this.loader.carLog, dbMeta.car))
10848
- throw new Error(`missing car ${dbMeta.car.toString()}`);
10925
+ throw new Error(`missing local car ${dbMeta.car.toString()}`);
10849
10926
  } else {
10850
10927
  await this.loader.remoteCarStore.save(car);
10851
10928
  }
@@ -10870,7 +10947,7 @@ You can use close({ resize: true }) to resize header`);
10870
10947
  const res = await Promise.allSettled(uploads);
10871
10948
  const errors = res.filter((r) => r.status === "rejected");
10872
10949
  if (errors.length) {
10873
- console.error("error uploading", JSON.stringify(errors));
10950
+ console.error("error uploading", JSON.stringify(errors.map((e) => e.reason)));
10874
10951
  throw errors[0].reason;
10875
10952
  }
10876
10953
  if (operations.length) {
@@ -20670,7 +20747,6 @@ You can use close({ resize: true }) to resize header`);
20670
20747
  var isCompacting = false;
20671
20748
  async function doCompact(blockLog, head) {
20672
20749
  if (isCompacting) {
20673
- console.log("already compacting");
20674
20750
  return;
20675
20751
  }
20676
20752
  isCompacting = true;
@@ -21377,7 +21453,10 @@ You can use close({ resize: true }) to resize header`);
21377
21453
  const task = queue.shift();
21378
21454
  if (!task)
21379
21455
  continue;
21380
- await worker(task.newHead, task.prevHead, task.updates !== null);
21456
+ await worker(task.newHead, task.prevHead, task.updates !== null).catch((e) => {
21457
+ console.error("int_applyHead worker error", e);
21458
+ throw e;
21459
+ });
21381
21460
  if (task.updates) {
21382
21461
  allUpdates.push(...task.updates);
21383
21462
  }
@@ -21400,6 +21479,9 @@ You can use close({ resize: true }) to resize header`);
21400
21479
  push(task) {
21401
21480
  queue.push(task);
21402
21481
  return process();
21482
+ },
21483
+ size() {
21484
+ return queue.length;
21403
21485
  }
21404
21486
  };
21405
21487
  }
@@ -21501,7 +21583,6 @@ You can use close({ resize: true }) to resize header`);
21501
21583
  try {
21502
21584
  head = await advance(tblocks, head, cid);
21503
21585
  } catch (e) {
21504
- console.log("failed to advance head:", cid.toString());
21505
21586
  continue;
21506
21587
  }
21507
21588
  }