@fireproof/core 0.5.18 → 0.5.20

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.
@@ -5564,7 +5564,7 @@ class EventBlock extends Block {
5564
5564
  * @param {Uint8Array} config.bytes
5565
5565
  */
5566
5566
  constructor ({ cid, value, bytes }) {
5567
- // @ts-expect-error
5567
+ // @ts-ignore
5568
5568
  super({ cid, value, bytes });
5569
5569
  }
5570
5570
 
@@ -5618,7 +5618,7 @@ class EventFetcher {
5618
5618
  async function encodeEventBlock (value) {
5619
5619
  // TODO: sort parents
5620
5620
  const { cid, bytes } = await encode$7({ value, codec: codec$1, hasher: sha256$2 });
5621
- // @ts-expect-error
5621
+ // @ts-ignore
5622
5622
  return new Block({ cid, value, bytes })
5623
5623
  }
5624
5624
 
@@ -5629,7 +5629,7 @@ async function encodeEventBlock (value) {
5629
5629
  */
5630
5630
  async function decodeEventBlock (bytes) {
5631
5631
  const { cid, value } = await decode$9({ bytes, codec: codec$1, hasher: sha256$2 });
5632
- // @ts-expect-error
5632
+ // @ts-ignore
5633
5633
  return new Block({ cid, value, bytes })
5634
5634
  }
5635
5635
 
@@ -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,6 +39652,7 @@ 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
39658
  if (!this.valetRootCarCid) return
@@ -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,
@@ -41012,6 +41004,18 @@ class Database {
41012
41004
  }
41013
41005
  }
41014
41006
 
41007
+ index (name) {
41008
+ // iterate over the indexes and gather any with the same name
41009
+ // if there are more than one, throw an error
41010
+ // if there is one, return it
41011
+ // if there are none, return null
41012
+ const indexes = [...this.indexes.values()].filter(index => index.name === name);
41013
+ if (indexes.length > 1) {
41014
+ throw new Error(`Multiple indexes found with name ${name}`)
41015
+ }
41016
+ return indexes[0] || null
41017
+ }
41018
+
41015
41019
  /**
41016
41020
  * Triggers a notification to all listeners
41017
41021
  * of the Fireproof instance so they can repaint UI, etc.
@@ -41100,8 +41104,7 @@ class Database {
41100
41104
  const cids = await cidsToProof(allResp.cids);
41101
41105
  const clockCids = await cidsToProof(allResp.clockCIDs);
41102
41106
  // console.log('allcids', cids, clockCids)
41103
- // todo we need to put the clock head as the last block in the encrypted car
41104
- 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
41105
41108
  }
41106
41109
 
41107
41110
  async allStoredCIDs () {
@@ -41143,7 +41146,7 @@ class Database {
41143
41146
  const clock = opts.clock || this.clock;
41144
41147
  const resp = await get(this.blocks, clock, charwise.encode(key), this.rootCache);
41145
41148
  this.rootCache = { root: resp.root, clockCIDs: resp.clockCIDs };
41146
- // 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
41147
41150
  if (!resp || resp.result === null) {
41148
41151
  throw new Error('Not found')
41149
41152
  }
@@ -41159,12 +41162,11 @@ class Database {
41159
41162
  return doc
41160
41163
  }
41161
41164
  /**
41162
- * @typedef {Object} Document
41165
+ * @typedef {any} Document
41163
41166
  * @property {string} _id - The ID of the document (required)
41164
41167
  * @property {string} [_proof] - The proof of the document (optional)
41165
41168
  * @property {string} [_clock] - The clock of the document (optional)
41166
- * @property {any} [key: string] - Index signature notation to allow any other unknown fields
41167
- * * @property {Object.<string, any>} [otherProperties] - Any other unknown properties (optional)
41169
+ * @property {Object.<string, any>} [unknown: string] - Any other unknown properties (optional)
41168
41170
  */
41169
41171
 
41170
41172
  /**
@@ -41918,9 +41920,7 @@ class DbIndex {
41918
41920
  );
41919
41921
  this.indexByKey = await bulkIndex(blocks, this.indexByKey, oldIndexEntries.concat(indexEntries), dbIndexOpts);
41920
41922
  this.dbHead = result.clock;
41921
- }, false /* don't sync transaction -- maybe move this flag to database.indexBlocks? */);
41922
- // todo index subscriptions
41923
- // this.database.notifyExternal('dbIndex')
41923
+ }, false /* don't sync transaction -- todo move this flag to database.indexBlocks, and concept of sync channels */);
41924
41924
  // console.timeEnd(callTag + '.doTransactionupdateIndex')
41925
41925
  // console.log(`updateIndex ${callTag} <`, this.instanceId, this.dbHead?.toString(), this.indexByKey.cid?.toString(), this.indexById.cid?.toString())
41926
41926
  return didT
@@ -44012,7 +44012,7 @@ class Sync {
44012
44012
  async sendUpdate (blockstore) {
44013
44013
  if (!this.peer || !this.isReady) return
44014
44014
  // console.log('send update from', this.database.instanceId)
44015
- // todo should send updates since last sync
44015
+ // todo should send updates since last sync (currently sends each transaction)
44016
44016
  const newCar = await blocksToCarBlock(blockstore.lastCid, blockstore);
44017
44017
  this.status = 'sending update car';
44018
44018
  this.peer.send(newCar.bytes);
@@ -44097,7 +44097,7 @@ class Fireproof {
44097
44097
  static storage = (name = null, opts = {}) => {
44098
44098
  if (name) {
44099
44099
  opts.name = name;
44100
- // todo this can come from a registry also
44100
+ // todo this can come from a registry also eg remote database / config, etc
44101
44101
  const existing = localGet('fp.' + name);
44102
44102
  if (existing) {
44103
44103
  const existingConfig = JSON.parse(existing);