@fireproof/core 0.17.5 → 0.17.6

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -10029,7 +10029,7 @@ You can use close({ resize: true }) to resize header`);
10029
10029
  }
10030
10030
  return bytes;
10031
10031
  }
10032
- var PACKAGE_VERSION = "0.17.4";
10032
+ var PACKAGE_VERSION = "0.17.5";
10033
10033
  var match = PACKAGE_VERSION.match(/^([^.]*\.[^.]*)/);
10034
10034
  if (!match)
10035
10035
  throw new Error("invalid version: " + PACKAGE_VERSION);
@@ -10099,6 +10099,7 @@ You can use close({ resize: true }) to resize header`);
10099
10099
  key;
10100
10100
  keyId;
10101
10101
  seenCompacted = /* @__PURE__ */ new Set();
10102
+ processedCars = /* @__PURE__ */ new Set();
10102
10103
  writing = Promise.resolve();
10103
10104
  getBlockCache = /* @__PURE__ */ new Map();
10104
10105
  seenMeta = /* @__PURE__ */ new Set();
@@ -10187,7 +10188,7 @@ You can use close({ resize: true }) to resize header`);
10187
10188
  // can these skip the queue? or have a file queue?
10188
10189
  async _commitInternalFiles(t, done, opts = { noLoader: false, compact: false }) {
10189
10190
  await this.ready;
10190
- const { files: roots } = this.makeFileCarHeader(done, this.carLog, !!opts.compact);
10191
+ const { files: roots } = this.makeFileCarHeader(done);
10191
10192
  const { cid, bytes } = await this.prepareCarFile(roots[0], t, !!opts.public);
10192
10193
  await this.fileStore.save({ cid, bytes });
10193
10194
  await this.remoteWAL.enqueueFile(cid, !!opts.public);
@@ -10207,7 +10208,10 @@ You can use close({ resize: true }) to resize header`);
10207
10208
  }
10208
10209
  }
10209
10210
  }
10210
- async cacheCarReader(reader) {
10211
+ async cacheCarReader(carCidStr, reader) {
10212
+ if (this.processedCars.has(carCidStr))
10213
+ return;
10214
+ this.processedCars.add(carCidStr);
10211
10215
  for await (const block of reader.blocks()) {
10212
10216
  const sBlock = block.cid.toString();
10213
10217
  if (!this.getBlockCache.has(sBlock)) {
@@ -10237,7 +10241,7 @@ You can use close({ resize: true }) to resize header`);
10237
10241
  const theKey = isPublic ? null : await this._getKey();
10238
10242
  return theKey && this.ebOpts.crypto ? await encryptedEncodeCarFile(this.ebOpts.crypto, theKey, root2, t) : await encodeCarFile([root2], t);
10239
10243
  }
10240
- makeFileCarHeader(result, cars, compact = false) {
10244
+ makeFileCarHeader(result) {
10241
10245
  const files = [];
10242
10246
  for (const [, meta] of Object.entries(result.files)) {
10243
10247
  if (meta && typeof meta === "object" && "cid" in meta && meta !== null) {
@@ -10272,10 +10276,27 @@ You can use close({ resize: true }) to resize header`);
10272
10276
  // await this.remoteWAL!.enqueue(dbMeta, { public: false })
10273
10277
  // }
10274
10278
  // }
10275
- async *entries() {
10279
+ async *entries(cache22 = true) {
10276
10280
  await this.ready;
10277
- for (const [, block] of this.getBlockCache) {
10278
- yield block;
10281
+ if (cache22) {
10282
+ for (const [, block] of this.getBlockCache) {
10283
+ yield block;
10284
+ }
10285
+ } else {
10286
+ for (const [, block] of this.getBlockCache) {
10287
+ yield block;
10288
+ }
10289
+ for (const cid of this.carLog) {
10290
+ const reader = await this.loadCar(cid);
10291
+ if (!reader)
10292
+ throw new Error(`missing car reader ${cid.toString()}`);
10293
+ for await (const block of reader.blocks()) {
10294
+ const sCid = block.cid.toString();
10295
+ if (!this.getBlockCache.has(sCid)) {
10296
+ yield block;
10297
+ }
10298
+ }
10299
+ }
10279
10300
  }
10280
10301
  }
10281
10302
  async getBlock(cid) {
@@ -10288,11 +10309,37 @@ You can use close({ resize: true }) to resize header`);
10288
10309
  if (!reader) {
10289
10310
  throw new Error(`missing car reader ${carCid.toString()}`);
10290
10311
  }
10291
- await this.cacheCarReader(reader);
10312
+ await this.cacheCarReader(carCid.toString(), reader).catch((e) => {
10313
+ });
10292
10314
  if (this.getBlockCache.has(sCid))
10293
10315
  return this.getBlockCache.get(sCid);
10294
10316
  throw new Error(`block not in reader: ${cid.toString()}`);
10295
10317
  };
10318
+ const getCompactCarCids = async (carCid) => {
10319
+ const reader = await this.loadCar(carCid);
10320
+ if (!reader) {
10321
+ throw new Error(`missing car reader ${carCid.toString()}`);
10322
+ }
10323
+ const header = await parseCarFile(reader);
10324
+ const compacts = header.compact;
10325
+ let got2;
10326
+ const batchSize2 = 5;
10327
+ for (let i = 0; i < compacts.length; i += batchSize2) {
10328
+ const promises = [];
10329
+ for (let j = i; j < Math.min(i + batchSize2, compacts.length); j++) {
10330
+ promises.push(getCarCid(compacts[j]));
10331
+ }
10332
+ try {
10333
+ got2 = await Promise.any(promises);
10334
+ } catch {
10335
+ }
10336
+ if (got2)
10337
+ break;
10338
+ }
10339
+ if (this.getBlockCache.has(sCid))
10340
+ return this.getBlockCache.get(sCid);
10341
+ throw new Error(`block not in compact reader: ${cid.toString()}`);
10342
+ };
10296
10343
  let got;
10297
10344
  const batchSize = 5;
10298
10345
  for (let i = 0; i < this.carLog.length; i += batchSize) {
@@ -10307,8 +10354,19 @@ You can use close({ resize: true }) to resize header`);
10307
10354
  if (got)
10308
10355
  break;
10309
10356
  }
10310
- if (got) {
10311
- this.getBlockCache.set(sCid, got);
10357
+ if (!got) {
10358
+ for (let i = 0; i < this.carLog.length; i += batchSize) {
10359
+ const promises = [];
10360
+ for (let j = i; j < Math.min(i + batchSize, this.carLog.length); j++) {
10361
+ promises.push(getCompactCarCids(this.carLog[j]));
10362
+ }
10363
+ try {
10364
+ got = await Promise.any(promises);
10365
+ } catch {
10366
+ }
10367
+ if (got)
10368
+ break;
10369
+ }
10312
10370
  }
10313
10371
  return got;
10314
10372
  }
@@ -10415,6 +10473,7 @@ You can use close({ resize: true }) to resize header`);
10415
10473
  compacting = false;
10416
10474
  ebOpts;
10417
10475
  transactions = /* @__PURE__ */ new Set();
10476
+ lastTxMeta = null;
10418
10477
  constructor(ebOpts) {
10419
10478
  this.ebOpts = ebOpts;
10420
10479
  const { name: name7 } = ebOpts;
@@ -10429,6 +10488,7 @@ You can use close({ resize: true }) to resize header`);
10429
10488
  async transaction(fn, opts = { noLoader: false }) {
10430
10489
  const t = new CarTransaction(this);
10431
10490
  const done = await fn(t);
10491
+ this.lastTxMeta = done;
10432
10492
  if (this.loader) {
10433
10493
  const car = await this.loader.commit(t, done, opts);
10434
10494
  if (this.ebOpts.autoCompact && this.loader.carLog.length > this.ebOpts.autoCompact) {
@@ -10474,7 +10534,7 @@ You can use close({ resize: true }) to resize header`);
10474
10534
  throw new Error("loader required to compact");
10475
10535
  if (this.loader.carLog.length < 2)
10476
10536
  return;
10477
- const compactFn = this.ebOpts.compact;
10537
+ const compactFn = this.ebOpts.compact || ((blocks) => this.defaultCompact(blocks));
10478
10538
  if (!compactFn || this.compacting)
10479
10539
  return;
10480
10540
  const blockLog = new CompactionFetcher(this);
@@ -10486,6 +10546,23 @@ You can use close({ resize: true }) to resize header`);
10486
10546
  });
10487
10547
  this.compacting = false;
10488
10548
  }
10549
+ async defaultCompact(blocks) {
10550
+ if (!this.loader) {
10551
+ throw new Error("no loader");
10552
+ }
10553
+ if (!this.lastTxMeta) {
10554
+ throw new Error("no lastTxMeta");
10555
+ }
10556
+ for await (const blk of this.loader.entries(false)) {
10557
+ blocks.loggedBlocks.putSync(blk.cid, blk.bytes);
10558
+ }
10559
+ for (const t of this.transactions) {
10560
+ for await (const blk of t.entries()) {
10561
+ blocks.loggedBlocks.putSync(blk.cid, blk.bytes);
10562
+ }
10563
+ }
10564
+ return this.lastTxMeta;
10565
+ }
10489
10566
  async *entries() {
10490
10567
  const seen = /* @__PURE__ */ new Set();
10491
10568
  if (this.loader) {
@@ -18784,7 +18861,7 @@ ${key.data.toString("base64")}
18784
18861
  var import_path = __require("path");
18785
18862
  var import_os = __require("os");
18786
18863
  var import_promises = __require("fs/promises");
18787
- var PACKAGE_VERSION2 = "0.17.4";
18864
+ var PACKAGE_VERSION2 = "0.17.5";
18788
18865
  var match2 = PACKAGE_VERSION2.match(/^([^.]*\.[^.]*)/);
18789
18866
  if (!match2)
18790
18867
  throw new Error("invalid version: " + PACKAGE_VERSION2);
@@ -18922,7 +18999,7 @@ ${key.data.toString("base64")}
18922
18999
  const car = await this.loader.carStore.load(dbMeta.car).catch(() => null);
18923
19000
  if (!car) {
18924
19001
  if (cidListIncludes2(this.loader.carLog, dbMeta.car))
18925
- throw new Error(`missing car ${dbMeta.car.toString()}`);
19002
+ throw new Error(`missing local car ${dbMeta.car.toString()}`);
18926
19003
  } else {
18927
19004
  await this.loader.remoteCarStore.save(car);
18928
19005
  }
@@ -18935,7 +19012,7 @@ ${key.data.toString("base64")}
18935
19012
  const car = await this.loader.carStore.load(dbMeta.car).catch(() => null);
18936
19013
  if (!car) {
18937
19014
  if (cidListIncludes2(this.loader.carLog, dbMeta.car))
18938
- throw new Error(`missing car ${dbMeta.car.toString()}`);
19015
+ throw new Error(`missing local car ${dbMeta.car.toString()}`);
18939
19016
  } else {
18940
19017
  await this.loader.remoteCarStore.save(car);
18941
19018
  }
@@ -18960,7 +19037,7 @@ ${key.data.toString("base64")}
18960
19037
  const res = await Promise.allSettled(uploads);
18961
19038
  const errors = res.filter((r) => r.status === "rejected");
18962
19039
  if (errors.length) {
18963
- console.error("error uploading", JSON.stringify(errors));
19040
+ console.error("error uploading", JSON.stringify(errors.map((e) => e.reason)));
18964
19041
  throw errors[0].reason;
18965
19042
  }
18966
19043
  if (operations.length) {
@@ -28723,7 +28800,6 @@ ${key.data.toString("base64")}
28723
28800
  var isCompacting = false;
28724
28801
  async function doCompact(blockLog, head) {
28725
28802
  if (isCompacting) {
28726
- console.log("already compacting");
28727
28803
  return;
28728
28804
  }
28729
28805
  isCompacting = true;
@@ -29430,7 +29506,10 @@ ${key.data.toString("base64")}
29430
29506
  const task = queue.shift();
29431
29507
  if (!task)
29432
29508
  continue;
29433
- await worker(task.newHead, task.prevHead, task.updates !== null);
29509
+ await worker(task.newHead, task.prevHead, task.updates !== null).catch((e) => {
29510
+ console.error("int_applyHead worker error", e);
29511
+ throw e;
29512
+ });
29434
29513
  if (task.updates) {
29435
29514
  allUpdates.push(...task.updates);
29436
29515
  }
@@ -29453,6 +29532,9 @@ ${key.data.toString("base64")}
29453
29532
  push(task) {
29454
29533
  queue.push(task);
29455
29534
  return process2();
29535
+ },
29536
+ size() {
29537
+ return queue.length;
29456
29538
  }
29457
29539
  };
29458
29540
  }
@@ -29554,7 +29636,6 @@ ${key.data.toString("base64")}
29554
29636
  try {
29555
29637
  head = await advance(tblocks, head, cid);
29556
29638
  } catch (e) {
29557
- console.log("failed to advance head:", cid.toString());
29558
29639
  continue;
29559
29640
  }
29560
29641
  }