@fireproof/core 0.14.8 → 0.15.0-dev

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.
@@ -20987,7 +20987,7 @@ var import_os = require("os");
20987
20987
  var import_promises = require("fs/promises");
20988
20988
 
20989
20989
  // src/version.ts
20990
- var PACKAGE_VERSION = "0.14.8";
20990
+ var PACKAGE_VERSION = "0.15.0-dev";
20991
20991
 
20992
20992
  // src/store.ts
20993
20993
  var match = PACKAGE_VERSION.match(/^([^.]*\.[^.]*)/);
@@ -21015,9 +21015,9 @@ var MetaStore = class extends VersionedStore {
21015
21015
  var DataStore = class {
21016
21016
  tag = "car-base";
21017
21017
  STORAGE_VERSION = STORAGE_VERSION;
21018
- loader;
21019
- constructor(loader) {
21020
- this.loader = loader;
21018
+ name;
21019
+ constructor(name8) {
21020
+ this.name = name8;
21021
21021
  }
21022
21022
  };
21023
21023
 
@@ -21224,7 +21224,7 @@ var DataStore2 = class _DataStore extends DataStore {
21224
21224
  await writePathFile(filepath, car.bytes);
21225
21225
  }
21226
21226
  cidPath(cid) {
21227
- return (0, import_path.join)(_DataStore.dataDir, this.loader.name, "data", cid.toString() + ".car");
21227
+ return (0, import_path.join)(_DataStore.dataDir, this.name, "data", cid.toString() + ".car");
21228
21228
  }
21229
21229
  async load(cid) {
21230
21230
  const filepath = this.cidPath(cid);
@@ -21282,7 +21282,7 @@ var Loader = class {
21282
21282
  this.name = name8;
21283
21283
  this.opts = opts || this.opts;
21284
21284
  this.metaStore = new MetaStore2(this.name);
21285
- this.carStore = new DataStore2(this);
21285
+ this.carStore = new DataStore2(this.name);
21286
21286
  this.remoteWAL = new RemoteWAL2(this);
21287
21287
  this.ready = Promise.resolve().then(async () => {
21288
21288
  if (!this.metaStore || !this.carStore || !this.remoteWAL)
@@ -30347,7 +30347,7 @@ var DbLoader = class _DbLoader extends Loader {
30347
30347
  fileStore;
30348
30348
  constructor(name8, clock, opts) {
30349
30349
  super(name8, opts);
30350
- this.fileStore = new DataStore2(this);
30350
+ this.fileStore = new DataStore2(this.name);
30351
30351
  this.clock = clock;
30352
30352
  }
30353
30353
  async _readyForMerge() {
@@ -30552,7 +30552,7 @@ function applyHeadQueue(worker) {
30552
30552
  const task = queue.shift();
30553
30553
  if (!task)
30554
30554
  continue;
30555
- await worker(task.id, task.newHead, task.prevHead, task.updates);
30555
+ await worker(task.newHead, task.prevHead);
30556
30556
  if (task.updates) {
30557
30557
  allUpdates.push(...task.updates);
30558
30558
  }
@@ -30581,7 +30581,7 @@ function applyHeadQueue(worker) {
30581
30581
 
30582
30582
  // src/crdt-clock.ts
30583
30583
  var CRDTClock = class {
30584
- // todo: keep the clock of remote and local changes separate, merge on read
30584
+ // todo: track local and remote clocks independently, merge on read
30585
30585
  // that way we can drop the whole remote if we need to
30586
30586
  // should go with making sure the local clock only references locally available blocks on write
30587
30587
  head = [];
@@ -30597,68 +30597,22 @@ var CRDTClock = class {
30597
30597
  this.head = head;
30598
30598
  }
30599
30599
  async applyHead(newHead, prevHead, updates = null) {
30600
- const taskId = Math.random().toString().slice(2, 8);
30601
30600
  for await (const { updates: updatesAcc, all } of this.applyHeadQueue.push({
30602
- id: taskId,
30603
- // tblocks,
30604
30601
  newHead,
30605
30602
  prevHead,
30606
30603
  updates
30607
30604
  })) {
30608
- ;
30609
- ((updatesAcc2, all2) => {
30610
- void Promise.resolve().then(async () => {
30611
- let intUpdates = updatesAcc2;
30612
- if (this.watchers.size && !all2) {
30613
- const changes = await clockChangesSince(this.blocks, this.head, prevHead, {});
30614
- intUpdates = changes.result;
30615
- }
30616
- this.zoomers.forEach((fn) => fn());
30617
- this.notifyWatchers(intUpdates || []);
30618
- });
30619
- })([...updatesAcc], all);
30605
+ this.processUpdates(updatesAcc, all, prevHead);
30620
30606
  }
30621
30607
  }
30622
- async int_applyHead(taskId, newHead, prevHead) {
30623
- const ogHead = this.head.sort((a, b) => a.toString().localeCompare(b.toString()));
30624
- newHead = newHead.sort((a, b) => a.toString().localeCompare(b.toString()));
30625
- newHead.map(async (cid) => {
30626
- const got = await this.blocks.get(cid);
30627
- if (!got) {
30628
- throw new Error("int_applyHead missing block: " + cid.toString());
30629
- }
30630
- });
30631
- if (ogHead.toString() === newHead.toString()) {
30632
- return;
30633
- }
30634
- const ogPrev = prevHead.sort((a, b) => a.toString().localeCompare(b.toString()));
30635
- if (ogHead.toString() === ogPrev.toString()) {
30636
- this.setHead(newHead);
30637
- return;
30608
+ async processUpdates(updatesAcc, all, prevHead) {
30609
+ let internalUpdates = updatesAcc;
30610
+ if (this.watchers.size && !all) {
30611
+ const changes = await clockChangesSince(this.blocks, this.head, prevHead, {});
30612
+ internalUpdates = changes.result;
30638
30613
  }
30639
- let head = this.head;
30640
- const noLoader = false;
30641
- const withBlocks = async (fn) => {
30642
- if (!this.blocks)
30643
- throw new Error("missing blocks");
30644
- return await this.blocks.transaction(fn, void 0, { noLoader });
30645
- };
30646
- await withBlocks(async (tblocks) => {
30647
- for (const cid of newHead) {
30648
- try {
30649
- head = await advance(tblocks, head, cid);
30650
- } catch (e) {
30651
- console.error("failed to advance", cid.toString(), e);
30652
- continue;
30653
- }
30654
- }
30655
- const result = await root(tblocks, head);
30656
- for (const { cid, bytes } of [...result.additions, ...result.removals]) {
30657
- tblocks.putSync(cid, bytes);
30658
- }
30659
- return { head };
30660
- });
30661
- this.setHead(head);
30614
+ this.zoomers.forEach((fn) => fn());
30615
+ this.notifyWatchers(internalUpdates || []);
30662
30616
  }
30663
30617
  notifyWatchers(updates) {
30664
30618
  this.emptyWatchers.forEach((fn) => fn());
@@ -30673,7 +30627,62 @@ var CRDTClock = class {
30673
30627
  onZoom(fn) {
30674
30628
  this.zoomers.add(fn);
30675
30629
  }
30630
+ async int_applyHead(newHead, prevHead) {
30631
+ const ogHead = sortClockHead(this.head);
30632
+ newHead = sortClockHead(newHead);
30633
+ await validateBlocks(newHead, this.blocks);
30634
+ if (compareClockHeads(ogHead, newHead)) {
30635
+ return;
30636
+ }
30637
+ const ogPrev = sortClockHead(prevHead);
30638
+ if (compareClockHeads(ogHead, ogPrev)) {
30639
+ this.setHead(newHead);
30640
+ return;
30641
+ }
30642
+ let head = this.head;
30643
+ const noLoader = false;
30644
+ if (!this.blocks)
30645
+ throw new Error("missing blocks");
30646
+ await this.blocks.transaction(
30647
+ async (tblocks) => {
30648
+ head = await advanceBlocks(newHead, tblocks, head);
30649
+ const result = await root(tblocks, head);
30650
+ for (const { cid, bytes } of [...result.additions, ...result.removals]) {
30651
+ tblocks.putSync(cid, bytes);
30652
+ }
30653
+ return { head };
30654
+ },
30655
+ void 0,
30656
+ { noLoader }
30657
+ );
30658
+ this.setHead(head);
30659
+ }
30676
30660
  };
30661
+ function sortClockHead(clockHead) {
30662
+ return clockHead.sort((a, b) => a.toString().localeCompare(b.toString()));
30663
+ }
30664
+ async function validateBlocks(newHead, blocks) {
30665
+ newHead.map(async (cid) => {
30666
+ const got = await blocks.get(cid);
30667
+ if (!got) {
30668
+ throw new Error("int_applyHead missing block: " + cid.toString());
30669
+ }
30670
+ });
30671
+ }
30672
+ function compareClockHeads(head1, head2) {
30673
+ return head1.toString() === head2.toString();
30674
+ }
30675
+ async function advanceBlocks(newHead, tblocks, head) {
30676
+ for (const cid of newHead) {
30677
+ try {
30678
+ head = await advance(tblocks, head, cid);
30679
+ } catch (e) {
30680
+ console.error("failed to advance", cid.toString(), e);
30681
+ continue;
30682
+ }
30683
+ }
30684
+ return head;
30685
+ }
30677
30686
 
30678
30687
  // src/crdt.ts
30679
30688
  var CRDT = class {
@@ -30848,6 +30857,9 @@ var Database = class {
30848
30857
  }));
30849
30858
  return { rows, clock: head };
30850
30859
  }
30860
+ async allDocuments() {
30861
+ return this.allDocs();
30862
+ }
30851
30863
  subscribe(listener, updates) {
30852
30864
  if (updates) {
30853
30865
  if (!this._listening) {