@leofcoin/peernet 1.1.49 → 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/exports/browser/browser-store.js +58 -141
- package/exports/browser/client-3221a5d7.js +17580 -0
- package/exports/browser/dht/dht.d.ts +29 -7
- package/exports/browser/identity.d.ts +15 -0
- package/exports/browser/{index-5f6ba5a4.js → index-36ed3c80.js} +2 -2
- package/exports/browser/{messages-5569e6ce.js → messages-b7f50660.js} +2 -2
- package/exports/browser/{peernet-9c2a381a.js → peernet-1b30b405.js} +433 -532
- package/exports/browser/peernet.d.ts +25 -32
- package/exports/browser/peernet.js +2 -2
- package/exports/browser/types.d.ts +16 -0
- package/exports/browser/value-4e80eeeb.js +73 -0
- package/exports/peernet.js +36 -35
- package/exports/store.js +117 -193
- package/exports/types/dht/dht.d.ts +29 -7
- package/exports/types/identity.d.ts +15 -0
- package/exports/types/peernet.d.ts +25 -32
- package/exports/types/types.d.ts +16 -0
- package/package.json +2 -2
- package/src/dht/dht.ts +36 -16
- package/src/peernet.ts +48 -61
- package/src/types.ts +28 -0
- package/exports/browser/client-6072af1a.js +0 -41092
- package/exports/browser/value-157ab062.js +0 -64
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 {
|
|
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
|
/**
|
|
@@ -93,11 +82,12 @@ export default class Peernet {
|
|
|
93
82
|
parts[1] ? options.root = `.${parts[0]}/${parts[1]}` : options.root = `.${this.network}`
|
|
94
83
|
}
|
|
95
84
|
|
|
96
|
-
peernet = this
|
|
85
|
+
globalThis.peernet = this
|
|
97
86
|
this.bw = {
|
|
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
|
-
|
|
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 =
|
|
432
|
+
let providers = this.dht.providersFor(hash)
|
|
438
433
|
// walk the network to find a provider
|
|
439
|
-
|
|
434
|
+
let tries = 0
|
|
435
|
+
while (Object.keys(providers).length === 0 && tries < 3) {
|
|
436
|
+
tries += 1
|
|
440
437
|
await this.walk(hash)
|
|
441
|
-
providers =
|
|
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.
|
|
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 =
|
|
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}
|
|
685
|
+
* @param {String} storeName - storeName to access
|
|
699
686
|
*/
|
|
700
|
-
async put(hash, data,
|
|
701
|
-
store = globalThis[`${
|
|
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
|
-
|
|
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
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
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
|
+
}
|