@fireproof/core 0.5.19 → 0.5.21

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.
@@ -39552,29 +39552,29 @@ class Valet {
39552
39552
  // )
39553
39553
  if (this.uploadFunction) {
39554
39554
  // todo we can coalesce these into a single car file
39555
- return await this.withDB(async db => {
39556
- for (const task of tasks) {
39557
- await this.uploadFunction(task.carCid, task.value);
39558
- // update the indexedb to mark this car as no longer pending
39559
- const carMeta = await db.get('cidToCar', task.carCid);
39560
- delete carMeta.pending;
39561
- await db.put('cidToCar', carMeta);
39562
- }
39563
- })
39555
+ // todo remove idb usage here
39556
+ for (const task of tasks) {
39557
+ await this.uploadFunction(task.carCid, task.value);
39558
+ // todo update syncCidMap to say this has been synced
39559
+ // const carMeta = await db.get('cidToCar', task.carCid)
39560
+ // delete carMeta.pending
39561
+ // await db.put('cidToCar', carMeta)
39562
+ }
39564
39563
  }
39565
39564
  callback();
39566
39565
  });
39567
39566
 
39568
39567
  this.uploadQueue.drain(async () => {
39569
- return await this.withDB(async db => {
39570
- const carKeys = (await db.getAllFromIndex('cidToCar', 'pending')).map(c => c.car);
39571
- for (const carKey of carKeys) {
39572
- await this.uploadFunction(carKey, await db.get('cars', carKey));
39573
- const carMeta = await db.get('cidToCar', carKey);
39574
- delete carMeta.pending;
39575
- await db.put('cidToCar', carMeta);
39576
- }
39577
- })
39568
+ // todo read syncCidMap and sync any that are still unsynced
39569
+ // return await this.withDB(async db => {
39570
+ // const carKeys = (await db.getAllFromIndex('cidToCar', 'pending')).map(c => c.car)
39571
+ // for (const carKey of carKeys) {
39572
+ // await this.uploadFunction(carKey, await db.get('cars', carKey))
39573
+ // const carMeta = await db.get('cidToCar', carKey)
39574
+ // delete carMeta.pending
39575
+ // await db.put('cidToCar', carMeta)
39576
+ // }
39577
+ // })
39578
39578
  });
39579
39579
  }
39580
39580
 
@@ -39620,16 +39620,10 @@ class Valet {
39620
39620
 
39621
39621
  withDB = async dbWorkFun => {
39622
39622
  if (!this.idb) {
39623
- this.idb = await openDB(`fp.${this.keyId}.${this.name}.valet`, 2, {
39623
+ this.idb = await openDB(`fp.${this.keyId}.${this.name}.valet`, 3, {
39624
39624
  upgrade (db, oldVersion, newVersion, transaction) {
39625
39625
  if (oldVersion < 1) {
39626
- db.createObjectStore('cars'); // todo use database name
39627
- const cidToCar = db.createObjectStore('cidToCar', { keyPath: 'car' });
39628
- cidToCar.createIndex('cids', 'cids', { multiEntry: true });
39629
- }
39630
- if (oldVersion < 2) {
39631
- const cidToCar = transaction.objectStore('cidToCar');
39632
- cidToCar.createIndex('pending', 'pending');
39626
+ db.createObjectStore('cars');
39633
39627
  }
39634
39628
  }
39635
39629
  });
@@ -39645,13 +39639,11 @@ class Valet {
39645
39639
  */
39646
39640
  async * cids () {
39647
39641
  // console.log('valet cids')
39648
- const db = await this.withDB(async db => db);
39649
- const tx = db.transaction(['cidToCar'], 'readonly');
39650
- let cursor = await tx.store.openCursor();
39651
- while (cursor) {
39652
- yield { cid: cursor.key, car: cursor.value.car };
39653
- cursor = await cursor.continue();
39654
- }
39642
+ // todo use cidMap
39643
+ // while (cursor) {
39644
+ // yield { cid: cursor.key, car: cursor.value.car }
39645
+ // cursor = await cursor.continue()
39646
+ // }
39655
39647
  }
39656
39648
 
39657
39649
  setRootCarCid (cid) {
@@ -39660,9 +39652,10 @@ class Valet {
39660
39652
  this.valetRootCid = null;
39661
39653
  }
39662
39654
 
39655
+ // todo memoize this
39663
39656
  async getCarCIDForCID (cid) {
39664
39657
  // make a car reader for this.valetRootCarCid
39665
- if (!this.valetRootCarCid) return
39658
+ if (!this.valetRootCarCid) return { result: null }
39666
39659
 
39667
39660
  let indexNode;
39668
39661
  if (this.valetRoot) {
@@ -39685,15 +39678,6 @@ class Valet {
39685
39678
  return { result: got }
39686
39679
  }
39687
39680
 
39688
- async OLDgetCarCIDForCID (cid) {
39689
- const carCid = await this.withDB(async db => {
39690
- const tx = db.transaction(['cars', 'cidToCar'], 'readonly');
39691
- const indexResp = await tx.objectStore('cidToCar').index('cids').get(cid);
39692
- return indexResp?.car
39693
- });
39694
- return { result: carCid }
39695
- }
39696
-
39697
39681
  async getCombinedReader (carCid) {
39698
39682
  let carMapReader;
39699
39683
  if (this.valetRootCarCid) {
@@ -39758,15 +39742,20 @@ class Valet {
39758
39742
  newValetCidCar = await blocksToCarBlock(this.valetRootCid, saveValetBlocks);
39759
39743
  }
39760
39744
  // console.log('newValetCidCar', this.name, Math.floor(newValetCidCar.bytes.length / 1024))
39761
- await this.withDB(async db => {
39762
- const tx = db.transaction(['cars'], 'readwrite');
39763
- await tx.objectStore('cars').put(value, carCid.toString());
39764
- if (newValetCidCar) {
39765
- if (this.valetRootCarCid) ;
39766
- await tx.objectStore('cars').put(newValetCidCar.bytes, newValetCidCar.cid.toString());
39745
+ await this.writeCars([
39746
+ {
39747
+ cid: carCid,
39748
+ bytes: value,
39749
+ replaces: null
39750
+ },
39751
+ {
39752
+ cid: newValetCidCar.cid,
39753
+ bytes: newValetCidCar.bytes,
39754
+ replaces: null
39755
+ // replaces: this.valetRootCarCid // todo
39767
39756
  }
39768
- return await tx.done
39769
- });
39757
+ ]);
39758
+
39770
39759
  this.valetRootCarCid = newValetCidCar.cid; // goes to clock
39771
39760
 
39772
39761
  // console.log('parked car', carCid, value.length, Array.from(cids))
@@ -39783,6 +39772,20 @@ class Valet {
39783
39772
  }
39784
39773
  }
39785
39774
 
39775
+ async writeCars (cars) {
39776
+ return await this.withDB(async db => {
39777
+ const tx = db.transaction(['cars'], 'readwrite');
39778
+ for (const { cid, bytes, replaces } of cars) {
39779
+ await tx.objectStore('cars').put(bytes, cid.toString());
39780
+ // todo remove old maps
39781
+ if (replaces) {
39782
+ await tx.objectStore('cars').delete(replaces.toString());
39783
+ }
39784
+ }
39785
+ return await tx.done
39786
+ })
39787
+ }
39788
+
39786
39789
  remoteBlockFunction = null
39787
39790
 
39788
39791
  async getCarReader (carCid) {
@@ -39813,7 +39816,7 @@ class Valet {
39813
39816
  };
39814
39817
  const { blocks } = await blocksFromEncryptedCarBlock(roots[0], readerGetWithCodec, this.keyMaterial);
39815
39818
 
39816
- // last block is the root ???
39819
+ // last block is the root ??? todo
39817
39820
  const rootBlock = blocks[blocks.length - 1];
39818
39821
 
39819
39822
  return {
@@ -40081,7 +40084,6 @@ class TransactionBlockstore {
40081
40084
 
40082
40085
  async networkGet (key) {
40083
40086
  if (this.remoteBlockFunction) {
40084
- // todo why is this on valet?
40085
40087
  const value = await husher(key, async () => await this.remoteBlockFunction(key));
40086
40088
  if (value) {
40087
40089
  // console.log('networkGot: ' + key, value.length)
@@ -40419,13 +40421,6 @@ const prollyRootFromAncestor = async (events, ancestor, getBlock) => {
40419
40421
  }
40420
40422
  };
40421
40423
 
40422
- // async function bigMerge (events, head, getBlock) {
40423
- // const allRoots = await Promise.all(head.map(async h => prollyRootFromAncestor(events, h, getBlock)))
40424
- // console.log('allRoots', allRoots)
40425
- // // todo query over all roots and merge them, but how do they not have a common ancestor? they all start with the _sync root
40426
- // throw new Error('not implemented')
40427
- // }
40428
-
40429
40424
  const doProllyBulk = async (inBlocks, head, event, doFull = false) => {
40430
40425
  const { getBlock, blocks } = makeGetAndPutBlock(inBlocks); // this is doubled with eventfetcher
40431
40426
  let bulkSorted = [];
@@ -40594,8 +40589,6 @@ async function eventsSince (blocks, head, since) {
40594
40589
  *
40595
40590
  */
40596
40591
  async function getAll (blocks, head, rootCache = null, doFull = false) {
40597
- // todo use the root node left around from put, etc
40598
- // move load to a central place
40599
40592
  if (!head.length) {
40600
40593
  return { root: null, clockCIDs: new CIDCounter(), cids: new CIDCounter(), result: [] }
40601
40594
  }
@@ -40975,7 +40968,6 @@ class Database {
40975
40968
  * @instance
40976
40969
  */
40977
40970
  toJSON () {
40978
- // todo this also needs to return the index roots...
40979
40971
  return {
40980
40972
  clock: this.clockToJSON(),
40981
40973
  name: this.name,
@@ -41112,8 +41104,7 @@ class Database {
41112
41104
  const cids = await cidsToProof(allResp.cids);
41113
41105
  const clockCids = await cidsToProof(allResp.clockCIDs);
41114
41106
  // console.log('allcids', cids, clockCids)
41115
- // todo we need to put the clock head as the last block in the encrypted car
41116
- return [...cids, ...clockCids] // need a single block version of clock head, maybe an encoded block for it
41107
+ return [...cids, ...clockCids] // clock CID last -- need to handle multiple entry clocks
41117
41108
  }
41118
41109
 
41119
41110
  async allStoredCIDs () {
@@ -41155,7 +41146,7 @@ class Database {
41155
41146
  const clock = opts.clock || this.clock;
41156
41147
  const resp = await get(this.blocks, clock, charwise.encode(key), this.rootCache);
41157
41148
  this.rootCache = { root: resp.root, clockCIDs: resp.clockCIDs };
41158
- // this tombstone is temporary until we can get the prolly tree to delete
41149
+ // ? this tombstone is temporary until we can get the prolly tree to delete
41159
41150
  if (!resp || resp.result === null) {
41160
41151
  throw new Error('Not found')
41161
41152
  }
@@ -41929,9 +41920,7 @@ class DbIndex {
41929
41920
  );
41930
41921
  this.indexByKey = await bulkIndex(blocks, this.indexByKey, oldIndexEntries.concat(indexEntries), dbIndexOpts);
41931
41922
  this.dbHead = result.clock;
41932
- }, false /* don't sync transaction -- maybe move this flag to database.indexBlocks? */);
41933
- // todo index subscriptions
41934
- // this.database.notifyExternal('dbIndex')
41923
+ }, false /* don't sync transaction -- todo move this flag to database.indexBlocks, and concept of sync channels */);
41935
41924
  // console.timeEnd(callTag + '.doTransactionupdateIndex')
41936
41925
  // console.log(`updateIndex ${callTag} <`, this.instanceId, this.dbHead?.toString(), this.indexByKey.cid?.toString(), this.indexById.cid?.toString())
41937
41926
  return didT
@@ -44023,7 +44012,7 @@ class Sync {
44023
44012
  async sendUpdate (blockstore) {
44024
44013
  if (!this.peer || !this.isReady) return
44025
44014
  // console.log('send update from', this.database.instanceId)
44026
- // todo should send updates since last sync
44015
+ // todo should send updates since last sync (currently sends each transaction)
44027
44016
  const newCar = await blocksToCarBlock(blockstore.lastCid, blockstore);
44028
44017
  this.status = 'sending update car';
44029
44018
  this.peer.send(newCar.bytes);
@@ -44108,7 +44097,7 @@ class Fireproof {
44108
44097
  static storage = (name = null, opts = {}) => {
44109
44098
  if (name) {
44110
44099
  opts.name = name;
44111
- // todo this can come from a registry also
44100
+ // todo this can come from a registry also eg remote database / config, etc
44112
44101
  const existing = localGet('fp.' + name);
44113
44102
  if (existing) {
44114
44103
  const existingConfig = JSON.parse(existing);