@fireproof/core 0.14.8 → 0.15.0-dev

Sign up to get free protection for your applications and to get access to all the features.
@@ -20977,7 +20977,7 @@ import { homedir } from "os";
20977
20977
  import { mkdir, readFile, writeFile, unlink } from "fs/promises";
20978
20978
 
20979
20979
  // src/version.ts
20980
- var PACKAGE_VERSION = "0.14.8";
20980
+ var PACKAGE_VERSION = "0.15.0-dev";
20981
20981
 
20982
20982
  // src/store.ts
20983
20983
  var match = PACKAGE_VERSION.match(/^([^.]*\.[^.]*)/);
@@ -21005,9 +21005,9 @@ var MetaStore = class extends VersionedStore {
21005
21005
  var DataStore = class {
21006
21006
  tag = "car-base";
21007
21007
  STORAGE_VERSION = STORAGE_VERSION;
21008
- loader;
21009
- constructor(loader) {
21010
- this.loader = loader;
21008
+ name;
21009
+ constructor(name8) {
21010
+ this.name = name8;
21011
21011
  }
21012
21012
  };
21013
21013
 
@@ -21214,7 +21214,7 @@ var DataStore2 = class _DataStore extends DataStore {
21214
21214
  await writePathFile(filepath, car.bytes);
21215
21215
  }
21216
21216
  cidPath(cid) {
21217
- return join(_DataStore.dataDir, this.loader.name, "data", cid.toString() + ".car");
21217
+ return join(_DataStore.dataDir, this.name, "data", cid.toString() + ".car");
21218
21218
  }
21219
21219
  async load(cid) {
21220
21220
  const filepath = this.cidPath(cid);
@@ -21272,7 +21272,7 @@ var Loader = class {
21272
21272
  this.name = name8;
21273
21273
  this.opts = opts || this.opts;
21274
21274
  this.metaStore = new MetaStore2(this.name);
21275
- this.carStore = new DataStore2(this);
21275
+ this.carStore = new DataStore2(this.name);
21276
21276
  this.remoteWAL = new RemoteWAL2(this);
21277
21277
  this.ready = Promise.resolve().then(async () => {
21278
21278
  if (!this.metaStore || !this.carStore || !this.remoteWAL)
@@ -30337,7 +30337,7 @@ var DbLoader = class _DbLoader extends Loader {
30337
30337
  fileStore;
30338
30338
  constructor(name8, clock, opts) {
30339
30339
  super(name8, opts);
30340
- this.fileStore = new DataStore2(this);
30340
+ this.fileStore = new DataStore2(this.name);
30341
30341
  this.clock = clock;
30342
30342
  }
30343
30343
  async _readyForMerge() {
@@ -30542,7 +30542,7 @@ function applyHeadQueue(worker) {
30542
30542
  const task = queue.shift();
30543
30543
  if (!task)
30544
30544
  continue;
30545
- await worker(task.id, task.newHead, task.prevHead, task.updates);
30545
+ await worker(task.newHead, task.prevHead);
30546
30546
  if (task.updates) {
30547
30547
  allUpdates.push(...task.updates);
30548
30548
  }
@@ -30571,7 +30571,7 @@ function applyHeadQueue(worker) {
30571
30571
 
30572
30572
  // src/crdt-clock.ts
30573
30573
  var CRDTClock = class {
30574
- // todo: keep the clock of remote and local changes separate, merge on read
30574
+ // todo: track local and remote clocks independently, merge on read
30575
30575
  // that way we can drop the whole remote if we need to
30576
30576
  // should go with making sure the local clock only references locally available blocks on write
30577
30577
  head = [];
@@ -30587,68 +30587,22 @@ var CRDTClock = class {
30587
30587
  this.head = head;
30588
30588
  }
30589
30589
  async applyHead(newHead, prevHead, updates = null) {
30590
- const taskId = Math.random().toString().slice(2, 8);
30591
30590
  for await (const { updates: updatesAcc, all } of this.applyHeadQueue.push({
30592
- id: taskId,
30593
- // tblocks,
30594
30591
  newHead,
30595
30592
  prevHead,
30596
30593
  updates
30597
30594
  })) {
30598
- ;
30599
- ((updatesAcc2, all2) => {
30600
- void Promise.resolve().then(async () => {
30601
- let intUpdates = updatesAcc2;
30602
- if (this.watchers.size && !all2) {
30603
- const changes = await clockChangesSince(this.blocks, this.head, prevHead, {});
30604
- intUpdates = changes.result;
30605
- }
30606
- this.zoomers.forEach((fn) => fn());
30607
- this.notifyWatchers(intUpdates || []);
30608
- });
30609
- })([...updatesAcc], all);
30595
+ this.processUpdates(updatesAcc, all, prevHead);
30610
30596
  }
30611
30597
  }
30612
- async int_applyHead(taskId, newHead, prevHead) {
30613
- const ogHead = this.head.sort((a, b) => a.toString().localeCompare(b.toString()));
30614
- newHead = newHead.sort((a, b) => a.toString().localeCompare(b.toString()));
30615
- newHead.map(async (cid) => {
30616
- const got = await this.blocks.get(cid);
30617
- if (!got) {
30618
- throw new Error("int_applyHead missing block: " + cid.toString());
30619
- }
30620
- });
30621
- if (ogHead.toString() === newHead.toString()) {
30622
- return;
30623
- }
30624
- const ogPrev = prevHead.sort((a, b) => a.toString().localeCompare(b.toString()));
30625
- if (ogHead.toString() === ogPrev.toString()) {
30626
- this.setHead(newHead);
30627
- return;
30598
+ async processUpdates(updatesAcc, all, prevHead) {
30599
+ let internalUpdates = updatesAcc;
30600
+ if (this.watchers.size && !all) {
30601
+ const changes = await clockChangesSince(this.blocks, this.head, prevHead, {});
30602
+ internalUpdates = changes.result;
30628
30603
  }
30629
- let head = this.head;
30630
- const noLoader = false;
30631
- const withBlocks = async (fn) => {
30632
- if (!this.blocks)
30633
- throw new Error("missing blocks");
30634
- return await this.blocks.transaction(fn, void 0, { noLoader });
30635
- };
30636
- await withBlocks(async (tblocks) => {
30637
- for (const cid of newHead) {
30638
- try {
30639
- head = await advance(tblocks, head, cid);
30640
- } catch (e) {
30641
- console.error("failed to advance", cid.toString(), e);
30642
- continue;
30643
- }
30644
- }
30645
- const result = await root(tblocks, head);
30646
- for (const { cid, bytes } of [...result.additions, ...result.removals]) {
30647
- tblocks.putSync(cid, bytes);
30648
- }
30649
- return { head };
30650
- });
30651
- this.setHead(head);
30604
+ this.zoomers.forEach((fn) => fn());
30605
+ this.notifyWatchers(internalUpdates || []);
30652
30606
  }
30653
30607
  notifyWatchers(updates) {
30654
30608
  this.emptyWatchers.forEach((fn) => fn());
@@ -30663,7 +30617,62 @@ var CRDTClock = class {
30663
30617
  onZoom(fn) {
30664
30618
  this.zoomers.add(fn);
30665
30619
  }
30620
+ async int_applyHead(newHead, prevHead) {
30621
+ const ogHead = sortClockHead(this.head);
30622
+ newHead = sortClockHead(newHead);
30623
+ await validateBlocks(newHead, this.blocks);
30624
+ if (compareClockHeads(ogHead, newHead)) {
30625
+ return;
30626
+ }
30627
+ const ogPrev = sortClockHead(prevHead);
30628
+ if (compareClockHeads(ogHead, ogPrev)) {
30629
+ this.setHead(newHead);
30630
+ return;
30631
+ }
30632
+ let head = this.head;
30633
+ const noLoader = false;
30634
+ if (!this.blocks)
30635
+ throw new Error("missing blocks");
30636
+ await this.blocks.transaction(
30637
+ async (tblocks) => {
30638
+ head = await advanceBlocks(newHead, tblocks, head);
30639
+ const result = await root(tblocks, head);
30640
+ for (const { cid, bytes } of [...result.additions, ...result.removals]) {
30641
+ tblocks.putSync(cid, bytes);
30642
+ }
30643
+ return { head };
30644
+ },
30645
+ void 0,
30646
+ { noLoader }
30647
+ );
30648
+ this.setHead(head);
30649
+ }
30666
30650
  };
30651
+ function sortClockHead(clockHead) {
30652
+ return clockHead.sort((a, b) => a.toString().localeCompare(b.toString()));
30653
+ }
30654
+ async function validateBlocks(newHead, blocks) {
30655
+ newHead.map(async (cid) => {
30656
+ const got = await blocks.get(cid);
30657
+ if (!got) {
30658
+ throw new Error("int_applyHead missing block: " + cid.toString());
30659
+ }
30660
+ });
30661
+ }
30662
+ function compareClockHeads(head1, head2) {
30663
+ return head1.toString() === head2.toString();
30664
+ }
30665
+ async function advanceBlocks(newHead, tblocks, head) {
30666
+ for (const cid of newHead) {
30667
+ try {
30668
+ head = await advance(tblocks, head, cid);
30669
+ } catch (e) {
30670
+ console.error("failed to advance", cid.toString(), e);
30671
+ continue;
30672
+ }
30673
+ }
30674
+ return head;
30675
+ }
30667
30676
 
30668
30677
  // src/crdt.ts
30669
30678
  var CRDT = class {
@@ -30838,6 +30847,9 @@ var Database = class {
30838
30847
  }));
30839
30848
  return { rows, clock: head };
30840
30849
  }
30850
+ async allDocuments() {
30851
+ return this.allDocs();
30852
+ }
30841
30853
  subscribe(listener, updates) {
30842
30854
  if (updates) {
30843
30855
  if (!this._listening) {