@leofcoin/peernet 0.10.3 → 0.10.4

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/peernet.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import Pubsub from '@vandeurenglenn/little-pubsub'
2
2
  import Client from './client'
3
- import LeofcoinStorage from './../node_modules/@leofcoin/storage/src/level.js'
3
+ import LeofcoinStorage from '@leofcoin/storage'
4
4
  import http from './http/http.js'
5
5
  import httpClient from './http/client/client.js'
6
6
  import LeofcoinStorageClient from './http/client/storage.js'
@@ -346,22 +346,22 @@ export default class Peernet {
346
346
  let data
347
347
 
348
348
  if (!store) {
349
- data = await this.get(hash)
350
- } else {
351
- store = globalThis[`${store}Store`]
352
- if (store.private) {
353
- // TODO: ban
354
- return
355
- } else data = await store.get(hash)
349
+ store = await this.whichStore([...this.stores], hash)
356
350
  }
351
+ if (store && !store.private) {
352
+ data = await store.get(hash)
357
353
 
358
- if (data) {
359
- data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
354
+ if (data) {
355
+ data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
360
356
 
361
- const node = await this.prepareMessage(from, data.encoded)
362
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
363
- this.bw.up += node.encoded.length
357
+ const node = await this.prepareMessage(from, data.encoded)
358
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
359
+ this.bw.up += node.encoded.length
360
+ }
361
+ } else {
362
+ // ban (trying to access private store)
364
363
  }
364
+
365
365
  } else if (proto.name === 'peernet-peer') {
366
366
  const from = proto.decoded.id
367
367
  if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
@@ -464,7 +464,7 @@ export default class Peernet {
464
464
  get: async (hash) => {
465
465
  const data = await blockStore.has(hash)
466
466
  if (data) return await blockStore.get(hash)
467
- return this.requestData(hash)
467
+ return this.requestData(hash, 'block')
468
468
  },
469
469
  put: async (hash, data) => {
470
470
  if (await blockStore.has(hash)) return
@@ -601,7 +601,8 @@ export default class Peernet {
601
601
  /**
602
602
  * Get content for given hash
603
603
  *
604
- * @param {String} hash
604
+ * @param {String} hash - the hash of the wanted data
605
+ * @param {String} store - storeName to access
605
606
  */
606
607
  async get(hash, store) {
607
608
  debug(`get ${hash}`)
@@ -611,7 +612,7 @@ export default class Peernet {
611
612
  if (store && await store.has(hash)) data = await store.get(hash)
612
613
  if (data) return data
613
614
 
614
- return this.requestData(hash, 'data')
615
+ return this.requestData(hash, store)
615
616
  }
616
617
 
617
618
  /**
@@ -619,10 +620,11 @@ export default class Peernet {
619
620
  *
620
621
  * @param {String} hash
621
622
  * @param {Buffer} data
623
+ * @param {String} store - storeName to access
622
624
  */
623
625
  async put(hash, data, store = 'data') {
624
626
  store = globalThis[`${store}Store`]
625
- return await store.put(hash, data)
627
+ return store.put(hash, data)
626
628
  }
627
629
 
628
630
  /**