@leofcoin/peernet 1.1.50 → 1.1.51

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.ts CHANGED
@@ -1,25 +1,16 @@
1
1
  import '@vandeurenglenn/debug'
2
2
  import PubSub from '@vandeurenglenn/little-pubsub'
3
3
  import PeerDiscovery from './discovery/peer-discovery.js'
4
- import DHT from './dht/dht.js'
4
+ import DHT, { DHTProvider, DHTProviderDistanceResult } from './dht/dht.js'
5
5
  import { BufferToUint8Array, protoFor, target } from './utils/utils.js'
6
6
  import MessageHandler from './handlers/message.js'
7
7
  import dataHandler from './handlers/data.js'
8
- import { encapsulatedError, dhtError,
9
- nothingFoundError } from './errors/errors.js'
10
-
8
+ import { dhtError, nothingFoundError } from './errors/errors.js'
11
9
  import LeofcoinStorageClass from '@leofcoin/storage'
12
10
  import { utils as codecUtils } from '@leofcoin/codecs'
13
11
  import Identity from './identity.js'
14
-
15
-
16
- declare global {
17
- var globalSub: PubSub;
18
- var pubsub: PubSub
19
- var peernet: Peernet
20
- var LeofcoinStorage: typeof LeofcoinStorageClass
21
- var LeofcoinStorageClient
22
- }
12
+ import swarm, { PeerId } from '@netpeer/p2pt-swarm'
13
+ import P2PTPeer from '@netpeer/p2pt-swarm/peer'
23
14
 
24
15
  globalThis.LeofcoinStorage = LeofcoinStorageClass
25
16
 
@@ -27,27 +18,24 @@ globalThis.leofcoin = globalThis.leofcoin || {}
27
18
  globalThis.pubsub = globalThis.pubsub || new PubSub()
28
19
  globalThis.globalSub = globalThis.globalSub || new PubSub()
29
20
 
30
-
31
21
  /**
32
22
  * @access public
33
23
  * @example
34
24
  * const peernet = new Peernet();
35
25
  */
36
26
  export default class Peernet {
27
+ storePrefix: string
28
+ root: string
37
29
  identity: Identity
38
30
  stores: string[] = []
31
+ peerId: string
39
32
  /**
40
33
  * @type {Object}
41
34
  * @property {Object} peer Instance of Peer
42
35
  */
43
36
  dht: DHT = new DHT()
44
37
  /** @leofcoin/peernet-swarm/client */
45
- client: {
46
- connections: [],
47
- id: string,
48
- removePeer: (peer: string) => {},
49
- close: () => {}
50
- }
38
+ client: swarm
51
39
  network: string
52
40
  stars: string[]
53
41
  networkVersion: string
@@ -59,9 +47,10 @@ export default class Peernet {
59
47
  autoStart: boolean = true
60
48
  #starting: boolean = false
61
49
  #started: boolean = false
62
- #connections = {}
50
+ #connections: {[index: PeerId]: P2PTPeer} = {}
63
51
  requestProtos = {}
64
52
  _messageHandler: MessageHandler
53
+ _peerHandler: PeerDiscovery
65
54
  protos: {}
66
55
 
67
56
  /**
@@ -98,6 +87,7 @@ export default class Peernet {
98
87
  up: 0,
99
88
  down: 0,
100
89
  }
90
+ // @ts-ignore
101
91
  return this._init(options, password)
102
92
  }
103
93
 
@@ -178,7 +168,7 @@ export default class Peernet {
178
168
  *
179
169
  * @return {Promise} instance of Peernet
180
170
  */
181
- async _init(options, password): Peernet {
171
+ async _init(options, password): Promise<Peernet> {
182
172
  this.storePrefix = options.storePrefix
183
173
  this.root = options.root
184
174
 
@@ -279,7 +269,12 @@ export default class Peernet {
279
269
  } else {
280
270
  process.on('SIGTERM', async () => {
281
271
  process.stdin.resume();
282
- await this.client.destroy()
272
+ try {
273
+ await this.client.destroy()
274
+ } catch (error) {
275
+ // @ts-ignore
276
+ await this.client.close()
277
+ }
283
278
  process.exit()
284
279
  });
285
280
  }
@@ -301,7 +296,7 @@ export default class Peernet {
301
296
  this.#starting = false
302
297
  }
303
298
 
304
- #peerLeft(peer) {
299
+ #peerLeft(peer: P2PTPeer) {
305
300
  for (const [id, _peer] of Object.entries(this.#connections)) {
306
301
  if (_peer.id === peer.id && this.#connections[id] && !this.#connections[id].connected) {
307
302
  delete this.#connections[id]
@@ -434,21 +429,13 @@ export default class Peernet {
434
429
  * @param {String} hash
435
430
  */
436
431
  async providersFor(hash: string, store?: undefined) {
437
- let providers = await this.dht.providersFor(hash)
432
+ let providers = this.dht.providersFor(hash)
438
433
  // walk the network to find a provider
439
- if (!providers || providers.length === 0) {
434
+ let tries = 0
435
+ while (Object.keys(providers).length === 0 && tries < 3) {
436
+ tries += 1
440
437
  await this.walk(hash)
441
- providers = await this.dht.providersFor(hash)
442
- // second walk the network to find a provider
443
- if (!providers || providers.length === 0) {
444
- await this.walk(hash)
445
- providers = await this.dht.providersFor(hash)
446
- }
447
- // last walk
448
- if (!providers || providers.length === 0) {
449
- await this.walk(hash)
450
- providers = await this.dht.providersFor(hash)
451
- }
438
+ providers = this.dht.providersFor(hash)
452
439
  }
453
440
  // undefined if no providers given
454
441
  return providers
@@ -456,40 +443,40 @@ export default class Peernet {
456
443
 
457
444
  get block() {
458
445
  return {
459
- get: async (hash) => {
446
+ get: async (hash: string) => {
460
447
  const data = await blockStore.has(hash)
461
448
  if (data) return blockStore.get(hash)
462
449
  return this.requestData(hash, 'block')
463
450
  },
464
- put: async (hash, data) => {
451
+ put: async (hash: string, data: Uint8Array) => {
465
452
  if (await blockStore.has(hash)) return
466
453
  return await blockStore.put(hash, data)
467
454
  },
468
- has: async (hash) => await blockStore.has(hash, 'block'),
455
+ has: async (hash: string) => await blockStore.has(hash, 'block'),
469
456
  }
470
457
  }
471
458
 
472
459
  get transaction() {
473
460
  return {
474
- get: async (hash) => {
461
+ get: async (hash: string) => {
475
462
  const data = await transactionStore.has(hash)
476
463
  if (data) return await transactionStore.get(hash)
477
464
  return this.requestData(hash, 'transaction')
478
465
  },
479
- put: async (hash, data) => {
466
+ put: async (hash: string, data: Uint8Array) => {
480
467
  if (await transactionStore.has(hash)) return
481
468
  return await transactionStore.put(hash, data)
482
469
  },
483
- has: async (hash) => await transactionStore.has(hash),
470
+ has: async (hash: string) => await transactionStore.has(hash),
484
471
  }
485
472
  }
486
473
 
487
474
  async requestData(hash, store) {
488
475
  const providers = await this.providersFor(hash)
489
- if (!providers || providers.size === 0) throw nothingFoundError(hash)
476
+ if (!providers || Object.keys(providers).length === 0) throw nothingFoundError(hash)
490
477
  debug(`found ${providers.size} provider(s) for ${hash}`)
491
478
  // get closest peer on earth
492
- const closestPeer = Array.from(providers)[0];
479
+ const closestPeer: DHTProvider = Object.values(providers)[0];
493
480
 
494
481
  // get peer instance by id
495
482
  if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name || store)
@@ -508,7 +495,7 @@ export default class Peernet {
508
495
  // fallback and try every provider found
509
496
  const promises = []
510
497
  const providers = await this.providersFor(hash, store)
511
- for (const provider of providers) {
498
+ for (const provider of Object.values(providers)) {
512
499
 
513
500
  const peer = this.#connections[provider.id]
514
501
 
@@ -695,10 +682,10 @@ export default class Peernet {
695
682
  *
696
683
  * @param {String} hash
697
684
  * @param {Buffer} data
698
- * @param {String} store - storeName to access
685
+ * @param {String} storeName - storeName to access
699
686
  */
700
- async put(hash, data, store = 'data') {
701
- store = globalThis[`${store}Store`]
687
+ async put(hash: string, data: Uint8Array, storeName: string | LeofcoinStorageClass = 'data') {
688
+ const store: LeofcoinStorageClass = globalThis[`${storeName}Store`]
702
689
  return store.put(hash, data)
703
690
  }
704
691
 
@@ -732,16 +719,16 @@ export default class Peernet {
732
719
  }
733
720
  }
734
721
 
735
- createHash(data, name) {
736
- return new CodeHash(data, {name})
737
- }
722
+ // createHash(data, name) {
723
+ // return new CodeHash(data, {name})
724
+ // }
738
725
 
739
726
  /**
740
727
  *
741
728
  * @param {String} topic
742
729
  * @param {Method} cb
743
730
  */
744
- async subscribe(topic, callback) {
731
+ async subscribe(topic: string, callback: Function) {
745
732
  // TODO: if peer subscribed
746
733
  globalSub.subscribe(topic, callback)
747
734
  }
@@ -749,13 +736,13 @@ export default class Peernet {
749
736
  async removePeer(peer) {
750
737
  console.log('removepeer', peer.id)
751
738
  const id = peer.id
752
- await this.client._removePeer(peer);
753
- console.log(this.client.peers[id])
754
- if (this.client.peers[id]) {
755
- for (const connection of Object.keys(this.client.peers[id])) {
756
- // if (this.client.peers[id][connection].connected === false) delete this.client.peers[id][connection]
757
- if (this.client.peers[id][connection].connected ) return this.client.emit('peerconnect', connection)
758
- }
739
+ await this.client._removePeer(peer);
740
+ if (this.client.peers[id]) {
741
+ for (const connection of Object.keys(this.client.peers[id])) {
742
+ // if (this.client.peers[id][connection].connected === false) delete this.client.peers[id][connection]
743
+ // @ts-ignore
744
+ if (this.client.peers[id][connection].connected ) return this.client.emit('peerconnect', connection)
745
+ }
759
746
  }
760
747
  }
761
748
 
package/src/types.ts ADDED
@@ -0,0 +1,28 @@
1
+ import '@vandeurenglenn/debug'
2
+ import PubSub from '@vandeurenglenn/little-pubsub'
3
+ import Peernet from './peernet.js'
4
+ import PeerDiscovery from './discovery/peer-discovery.js'
5
+ import DHT from './dht/dht.js'
6
+ import { BufferToUint8Array, protoFor, target } from './utils/utils.js'
7
+ import MessageHandler from './handlers/message.js'
8
+ import dataHandler from './handlers/data.js'
9
+ import { encapsulatedError, dhtError,
10
+ nothingFoundError } from './errors/errors.js'
11
+
12
+ import {LeofcoinStorage as LeofcoinStorageClass } from '@leofcoin/storage'
13
+ import { utils as codecUtils } from '@leofcoin/codecs'
14
+ import Identity from './identity.js'
15
+
16
+
17
+ declare global {
18
+ var debug: (message) => string
19
+ var globalSub: PubSub
20
+ var pubsub: PubSub
21
+ var peernet: Peernet
22
+ var LeofcoinStorage: typeof LeofcoinStorageClass
23
+ var LeofcoinStorageClient
24
+ var messageStore: typeof LeofcoinStorage
25
+ var dataStore: typeof LeofcoinStorage
26
+ var transactionStore: typeof LeofcoinStorage
27
+ var blockStore: typeof LeofcoinStorage
28
+ }