@fireproof/core 0.6.1 → 0.6.3-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.
package/src/valet.js CHANGED
@@ -6,8 +6,9 @@ import * as CBW from '@ipld/car/buffer-writer'
6
6
  import * as raw from 'multiformats/codecs/raw'
7
7
  import * as Block from 'multiformats/block'
8
8
  import * as dagcbor from '@ipld/dag-cbor'
9
- import { openDB } from 'idb'
10
9
  import cargoQueue from 'async/cargoQueue.js'
10
+ import { Loader } from './loader.js'
11
+
11
12
  // @ts-ignore
12
13
 
13
14
  // @ts-ignore
@@ -53,6 +54,7 @@ export class Valet {
53
54
  constructor (name = 'default', keyMaterial) {
54
55
  this.name = name
55
56
  this.setKeyMaterial(keyMaterial)
57
+ this.loader = new Loader(name, this.keyId) // todo send this config.loader, if we ever need it
56
58
  this.uploadQueue = cargoQueue(async (tasks, callback) => {
57
59
  // console.log(
58
60
  // 'queue worker',
@@ -87,6 +89,10 @@ export class Valet {
87
89
  })
88
90
  }
89
91
 
92
+ saveHeader (header) {
93
+ return this.loader.saveHeader(header)
94
+ }
95
+
90
96
  getKeyMaterial () {
91
97
  return this.keyMaterial
92
98
  }
@@ -127,19 +133,6 @@ export class Valet {
127
133
  }
128
134
  }
129
135
 
130
- withDB = async dbWorkFun => {
131
- if (!this.idb) {
132
- this.idb = await openDB(`fp.${this.keyId}.${this.name}.valet`, 3, {
133
- upgrade (db, oldVersion, newVersion, transaction) {
134
- if (oldVersion < 1) {
135
- db.createObjectStore('cars')
136
- }
137
- }
138
- })
139
- }
140
- return await dbWorkFun(this.idb)
141
- }
142
-
143
136
  /**
144
137
  * Iterate over all blocks in the store.
145
138
  *
@@ -180,6 +173,7 @@ export class Valet {
180
173
  blockHasher: blockOpts.hasher,
181
174
  blockCodec: blockOpts.codec
182
175
  })
176
+ this.valetRoot = indexNode
183
177
  }
184
178
 
185
179
  const got = await indexNode.get(cid)
@@ -226,6 +220,7 @@ export class Valet {
226
220
  * @param {*} value
227
221
  */
228
222
  async parkCar (carCid, value, cids) {
223
+ // const callId = Math.random().toString(36).substring(7)
229
224
  // console.log('parkCar', this.instanceId, this.name, carCid, cids)
230
225
  const combinedReader = await this.getCombinedReader(carCid)
231
226
  const mapNode = await addCidsToCarIndex(
@@ -238,7 +233,7 @@ export class Valet {
238
233
  this.valetRoot = mapNode
239
234
  this.valetRootCid = mapNode.cid
240
235
  // make a block set with all the cids of the map
241
- const saveValetBlocks = new VMemoryBlockstore() // todo this blockstore should read from the last valetCid car also
236
+ const saveValetBlocks = new VMemoryBlockstore()
242
237
 
243
238
  for await (const cidx of mapNode.cids()) {
244
239
  const bytes = await combinedReader.get(cidx)
@@ -251,7 +246,8 @@ export class Valet {
251
246
  newValetCidCar = await blocksToCarBlock(this.valetRootCid, saveValetBlocks)
252
247
  }
253
248
  // console.log('newValetCidCar', this.name, Math.floor(newValetCidCar.bytes.length / 1024))
254
- await this.writeCars([
249
+ // console.log('writeCars', callId, carCid.toString(), newValetCidCar.cid.toString())
250
+ await this.loader.writeCars([
255
251
  {
256
252
  cid: carCid,
257
253
  bytes: value,
@@ -267,6 +263,8 @@ export class Valet {
267
263
 
268
264
  this.valetRootCarCid = newValetCidCar.cid // goes to clock
269
265
 
266
+ // console.log('wroteCars', callId, carCid.toString(), newValetCidCar.cid.toString())
267
+
270
268
  // console.log('parked car', carCid, value.length, Array.from(cids))
271
269
  // upload to web3.storage if we have credentials
272
270
  if (this.uploadFunction) {
@@ -283,29 +281,13 @@ export class Valet {
283
281
  }
284
282
  }
285
283
 
286
- async writeCars (cars) {
287
- return await this.withDB(async db => {
288
- const tx = db.transaction(['cars'], 'readwrite')
289
- for (const { cid, bytes, replaces } of cars) {
290
- await tx.objectStore('cars').put(bytes, cid.toString())
291
- // todo remove old maps
292
- if (replaces) {
293
- await tx.objectStore('cars').delete(replaces.toString())
294
- }
295
- }
296
- return await tx.done
297
- })
298
- }
299
-
300
284
  remoteBlockFunction = null
301
285
 
302
286
  async getCarReader (carCid) {
303
287
  carCid = carCid.toString()
304
- const carBytes = await this.withDB(async db => {
305
- const tx = db.transaction(['cars'], 'readonly')
306
- // console.log('getCarReader', carCid)
307
- return await tx.objectStore('cars').get(carCid)
308
- })
288
+ const carBytes = await this.loader.readCar(carCid)
289
+ // const callID = Math.random().toString(36).substring(7)
290
+ // console.log('getCarReader', callID, carCid)
309
291
  const reader = await CarReader.fromBytes(carBytes)
310
292
  if (this.keyMaterial) {
311
293
  const roots = await reader.getRoots()
@@ -329,7 +311,7 @@ export class Valet {
329
311
 
330
312
  // last block is the root ??? todo
331
313
  const rootBlock = blocks[blocks.length - 1]
332
-
314
+ // console.log('got reader', callID, carCid)
333
315
  return {
334
316
  root: rootBlock,
335
317
  get: async dataCID => {
@@ -456,6 +438,8 @@ const blocksFromEncryptedCarBlock = async (cid, get, keyMaterial) => {
456
438
 
457
439
  const addCidsToCarIndex = async (blockstore, valetRoot, valetRootCid, bulkOperations) => {
458
440
  let indexNode
441
+ // const callID = Math.random().toString(32).substring(2, 8)
442
+ // console.log('addCidsToCarIndex', callID, valetRootCid, bulkOperations.length)
459
443
  if (valetRootCid) {
460
444
  if (valetRoot) {
461
445
  indexNode = valetRoot
@@ -475,6 +459,7 @@ const addCidsToCarIndex = async (blockstore, valetRoot, valetRootCid, bulkOperat
475
459
  // console.log('adding', key, value)
476
460
  await indexNode.set(key, value)
477
461
  }
462
+ // console.log('newCidsToCarIndex', callID, indexNode.cid, bulkOperations.length)
478
463
  return indexNode
479
464
  }
480
465
 
package/src/utils.js DELETED
@@ -1,16 +0,0 @@
1
-
2
- /* global localStorage */
3
- let storageSupported = false
4
- try {
5
- storageSupported = window.localStorage && true
6
- } catch (e) {}
7
- export function localGet (key) {
8
- if (storageSupported) {
9
- return localStorage && localStorage.getItem(key)
10
- }
11
- }
12
- export function localSet (key, value) {
13
- if (storageSupported) {
14
- return localStorage && localStorage.setItem(key, value)
15
- }
16
- }