@leofcoin/peernet 0.16.2 → 0.16.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.16.2",
3
+ "version": "0.16.4",
4
4
  "description": "",
5
5
  "main": "src/peernet.js",
6
6
  "type": "module",
@@ -21,7 +21,7 @@
21
21
  "@leofcoin/codec-format-interface": "^1.4.0",
22
22
  "@leofcoin/generate-account": "^1.0.4",
23
23
  "@leofcoin/multi-wallet": "^2.1.2",
24
- "@leofcoin/peernet-swarm": "^0.4.0",
24
+ "@leofcoin/peernet-swarm": "^0.4.4",
25
25
  "@leofcoin/storage": "^3.0.0",
26
26
  "@vandeurenglenn/base32": "^1.1.0",
27
27
  "@vandeurenglenn/base58": "^1.1.0",
@@ -32,7 +32,7 @@ export default class MessageHandler {
32
32
  * or the messageNode itself
33
33
  */
34
34
  async prepareMessage(message) {
35
- if (message.keys.indexOf('signature') !== -1) {
35
+ if (message.keys.includes('signature')) {
36
36
  message = await this.hashAndSignMessage(message)
37
37
  }
38
38
 
package/src/peernet.js CHANGED
@@ -43,13 +43,12 @@ export default class Peernet {
43
43
  this.network = options.network || 'leofcoin'
44
44
  this.stars = options.stars
45
45
  const parts = this.network.split(':')
46
- this.networkVersion = options.networkVersion ? options.networkVersion : parts.length > 1 ? parts[1] : 'mainnet'
46
+ this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet'
47
47
 
48
48
  if (!options.storePrefix) options.storePrefix = 'lfc'
49
49
  if (!options.port) options.port = 2000
50
50
  if (!options.root) {
51
- if (parts[1]) options.root = `.${parts[0]}/${parts[1]}`
52
- else options.root = `.${this.network}`
51
+ parts[1] ? options.root = `.${parts[0]}/${parts[1]}` : options.root = `.${this.network}`
53
52
  }
54
53
 
55
54
  globalThis.peernet = this
@@ -139,7 +138,7 @@ export default class Peernet {
139
138
  * @property {Object} peer Instance of Peer
140
139
  */
141
140
  this.stores = []
142
- this.codecs = codecs
141
+ this.codecs = {...codecs}
143
142
  this.requestProtos = {}
144
143
  this.storePrefix = options.storePrefix
145
144
  this.root = options.root
@@ -207,7 +206,7 @@ export default class Peernet {
207
206
  // TODO: remove when on mainnet
208
207
  try {
209
208
  this.accounts = JSON.parse(accounts)
210
- } catch (e) {
209
+ } catch {
211
210
  this.accounts = [accounts.split(',')]
212
211
  }
213
212
  } else {
@@ -278,6 +277,51 @@ export default class Peernet {
278
277
 
279
278
  }
280
279
 
280
+ async handleDHT(peer, id, proto) {
281
+ let { hash, store } = proto.decoded
282
+ let has;
283
+
284
+ if (store) {
285
+ store = globalThis[`${store}Store`]
286
+ has = store.private ? false : await store.has(hash)
287
+ } else {
288
+ has = await this.has(hash)
289
+ }
290
+
291
+ const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has})
292
+ const node = await this.prepareMessage(data)
293
+
294
+ this.sendMessage(peer, id, node.encoded)
295
+ }
296
+
297
+ async handleData(peer, id, proto) {
298
+ let { hash, store } = proto.decoded
299
+ let data
300
+ store = globalThis[`${store}Store`] || await this.whichStore([...this.stores], hash)
301
+
302
+ if (store && !store.private) {
303
+ data = await store.get(hash)
304
+
305
+ if (data) {
306
+ data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
307
+
308
+ const node = await this.prepareMessage(data)
309
+ this.sendMessage(peer, id, node.encoded)
310
+ }
311
+ } else {
312
+ // ban (trying to access private st)
313
+ }
314
+ }
315
+
316
+ async handleRequest(peer, id, proto) {
317
+ const method = this.requestProtos[proto.decoded.request]
318
+ if (method) {
319
+ const data = await method()
320
+ const node = await this.prepareMessage(data)
321
+ this.sendMessage(peer, id, node.encoded)
322
+ }
323
+ }
324
+
281
325
  /**
282
326
  * @private
283
327
  *
@@ -285,56 +329,25 @@ export default class Peernet {
285
329
  * @param {PeernetPeer} peer - peernet peer
286
330
  */
287
331
  async _protoHandler(message, peer, from) {
288
-
289
332
  const {id, proto} = message
290
333
  this.bw.down += proto.encoded.length
291
- if (proto.name === 'peernet-dht') {
292
- let { hash, store } = proto.decoded
293
- let has;
294
-
295
- if (!store) {
296
- has = await this.has(hash)
297
- } else {
298
- store = globalThis[`${store}Store`]
299
- if (store.private) has = false
300
- else has = await store.has(hash)
301
- }
302
- const data = await new globalThis.peernet.protos['peernet-dht-response']({hash, has})
303
- const node = await this.prepareMessage(data)
334
+ switch(proto.name) {
335
+ case 'peernet-dht': {
336
+ this.handleDHT(peer, id, proto)
337
+ break
338
+ }
339
+ case 'peenet-data': {
340
+ this.handleData(peer, id, proto)
341
+ break
342
+ }
343
+ case 'peernet-request': {
344
+ this.handleRequest(peer, id, proto)
345
+ }
304
346
 
305
- this.sendMessage(peer, id, node.encoded)
306
- } else if (proto.name === 'peernet-data') {
307
- let { hash, store } = proto.decoded
308
- let data
309
- if (!store) {
310
- store = await this.whichStore([...this.stores], hash)
311
- } else {
312
- store = globalThis[`${store}Store`]
313
- }
314
- if (store && !store.private) {
315
- data = await store.get(hash)
316
-
317
- if (data) {
318
- data = await new globalThis.peernet.protos['peernet-data-response']({hash, data});
319
-
320
- const node = await this.prepareMessage(data)
321
- this.sendMessage(peer, id, node.encoded)
322
- }
323
- } else {
324
- // ban (trying to access private st)
325
- }
326
-
327
- } else if (proto.name === 'peernet-request') {
328
- const method = this.requestProtos[proto.decoded.request]
329
- if (method) {
330
- const data = await method()
331
- const node = await this.prepareMessage(data)
332
- this.sendMessage(peer, id, node.encoded)
333
- }
334
- } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
335
- globalSub.publish(proto.decoded.topic, proto.decoded.data)
347
+ case 'peernet-ps': {
348
+ if (peer.peerId !== this.id) globalSub.publish(proto.decoded.topic, proto.decoded.data)
336
349
  }
337
- // }
350
+ }
338
351
  }
339
352
 
340
353
  /**
@@ -441,7 +454,7 @@ export default class Peernet {
441
454
  // get closest peer on earth
442
455
  const closestPeer = await this.dht.closestPeer(providers)
443
456
  // get peer instance by id
444
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
457
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name || store)
445
458
 
446
459
  const id = closestPeer.id
447
460
  if (this.connections) {
@@ -449,7 +462,7 @@ export default class Peernet {
449
462
  if (peer.peerId === id) return peer
450
463
  })
451
464
 
452
- let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
465
+ let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name || store});
453
466
 
454
467
  const node = await this.prepareMessage(data)
455
468
  if (closest[0]) data = await closest[0].request(node.encoded)
@@ -466,7 +479,7 @@ export default class Peernet {
466
479
 
467
480
  // this.put(hash, proto.decoded.data)
468
481
  }
469
- return null
482
+ return
470
483
  }
471
484
 
472
485
 
@@ -572,8 +585,7 @@ export default class Peernet {
572
585
  async ls(hash, options) {
573
586
  let data
574
587
  const has = await dataStore.has(hash)
575
- if (has) data = await dataStore.get(hash)
576
- else data = await this.requestData(hash, 'data')
588
+ data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data')
577
589
 
578
590
  const node = await new peernet.protos['peernet-file'](data)
579
591
  await node.decode()
@@ -590,8 +602,7 @@ export default class Peernet {
590
602
  async cat(hash, options) {
591
603
  let data
592
604
  const has = await dataStore.has(hash)
593
- if (has) data = await dataStore.get(hash)
594
- else data = await this.requestData(hash, 'data')
605
+ data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data')
595
606
  const node = await new peernet.protos['peernet-file'](data)
596
607
 
597
608
  if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
@@ -612,7 +623,7 @@ export default class Peernet {
612
623
  const has = await store.has(hash)
613
624
  if (has) return store
614
625
  if (stores.length > 0) return this.whichStore(stores, hash)
615
- } else return null
626
+ } else return
616
627
  }
617
628
 
618
629
  /**
@@ -629,7 +640,7 @@ export default class Peernet {
629
640
  if (store && await store.has(hash)) data = await store.get(hash)
630
641
  if (data) return data
631
642
 
632
- return this.requestData(hash, store?.name ? store.name : store)
643
+ return this.requestData(hash, store?.name || store)
633
644
  }
634
645
 
635
646
  /**
@@ -651,8 +662,7 @@ export default class Peernet {
651
662
  async has(hash) {
652
663
  const store = await this.whichStore([...this.stores], hash)
653
664
  if (store) {
654
- if (store.private) return false
655
- else return true
665
+ return store.private ? false : true
656
666
  }
657
667
  return false
658
668
  }
@@ -684,9 +694,9 @@ export default class Peernet {
684
694
  * @param {String} topic
685
695
  * @param {Method} cb
686
696
  */
687
- async subscribe(topic, cb) {
697
+ async subscribe(topic, callback) {
688
698
  // TODO: if peer subscribed
689
- globalSub.subscribe(topic, cb)
699
+ globalSub.subscribe(topic, callback)
690
700
  }
691
701
 
692
702
  async removePeer(peer) {