@leofcoin/peernet 0.10.2 → 0.10.5

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.
@@ -2,13 +2,17 @@ import protons from 'protons'
2
2
  import proto from './../proto/data.proto.js'
3
3
  import CodecFormat from './../codec/codec-format-interface.js'
4
4
 
5
+ /**
6
+ * @extends {CodecFormat}
7
+ */
5
8
  export default class DataMessage extends CodecFormat {
6
9
  get keys() {
7
10
  return ['hash', 'store']
8
11
  }
9
-
12
+ /**
13
+ * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
14
+ */
10
15
  constructor(data) {
11
- const name = 'peernet-data'
12
- super(data, protons(proto).PeernetDataMessage, {name})
16
+ super(data, protons(proto).PeernetDataMessage, {name: 'peernet-data'})
13
17
  }
14
18
  }
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'
@@ -278,7 +278,7 @@ export default class Peernet {
278
278
  */
279
279
  async _protoHandler(message, peer) {
280
280
  const {id, proto} = message
281
- this.bw.down += message.encoded.length
281
+ this.bw.down += proto.encoded.length
282
282
  if (proto.name === 'peernet-peer') {
283
283
  const from = proto.decoded.id
284
284
  if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
@@ -346,22 +346,24 @@ export default class Peernet {
346
346
  let data
347
347
 
348
348
  if (!store) {
349
- data = await this.get(hash)
349
+ store = await this.whichStore([...this.stores], hash)
350
350
  } else {
351
- store = globalThis[`${store}Store`]
352
- if (store.private) {
353
- // TODO: ban
354
- return
355
- } else data = await store.get(hash)
351
+ store = globalThis.stores[`${store}Store`]
356
352
  }
353
+ if (store && !store.private) {
354
+ data = await store.get(hash)
357
355
 
358
- if (data) {
359
- data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
356
+ if (data) {
357
+ data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
360
358
 
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
359
+ const node = await this.prepareMessage(from, data.encoded)
360
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
361
+ this.bw.up += node.encoded.length
362
+ }
363
+ } else {
364
+ // ban (trying to access private st)
364
365
  }
366
+
365
367
  } else if (proto.name === 'peernet-peer') {
366
368
  const from = proto.decoded.id
367
369
  if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
@@ -464,7 +466,7 @@ export default class Peernet {
464
466
  get: async (hash) => {
465
467
  const data = await blockStore.has(hash)
466
468
  if (data) return await blockStore.get(hash)
467
- return this.requestData(hash)
469
+ return this.requestData(hash, 'block')
468
470
  },
469
471
  put: async (hash, data) => {
470
472
  if (await blockStore.has(hash)) return
@@ -601,7 +603,8 @@ export default class Peernet {
601
603
  /**
602
604
  * Get content for given hash
603
605
  *
604
- * @param {String} hash
606
+ * @param {String} hash - the hash of the wanted data
607
+ * @param {String} store - storeName to access
605
608
  */
606
609
  async get(hash, store) {
607
610
  debug(`get ${hash}`)
@@ -611,7 +614,7 @@ export default class Peernet {
611
614
  if (store && await store.has(hash)) data = await store.get(hash)
612
615
  if (data) return data
613
616
 
614
- return this.requestData(hash, 'data')
617
+ return this.requestData(hash, store)
615
618
  }
616
619
 
617
620
  /**
@@ -619,10 +622,11 @@ export default class Peernet {
619
622
  *
620
623
  * @param {String} hash
621
624
  * @param {Buffer} data
625
+ * @param {String} store - storeName to access
622
626
  */
623
627
  async put(hash, data, store = 'data') {
624
628
  store = globalThis[`${store}Store`]
625
- return await store.put(hash, data)
629
+ return store.put(hash, data)
626
630
  }
627
631
 
628
632
  /**