@leofcoin/peernet 1.1.66 → 1.1.68
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-2c73e2ef.js +25 -0
- package/exports/browser/{index-34c93510.js → index-e6b55b51.js} +1 -1
- package/exports/browser/{messages-b7f41cba.js → messages-606d459f.js} +1 -1
- package/exports/browser/{peernet-72528dd9.js → peernet-b93a3e33.js} +306 -63
- package/exports/browser/peernet.d.ts +11 -6
- package/exports/browser/peernet.js +1 -1
- package/exports/peernet.js +74 -62
- package/exports/types/peernet.d.ts +11 -6
- package/package.json +3 -2
- package/rollup.config.js +35 -41
- package/src/peernet.ts +83 -68
- package/test.js +60 -62
- package/test2.js +8 -4
- package/exports/browser/client-493cbd83.js +0 -10783
package/src/peernet.ts
CHANGED
|
@@ -9,8 +9,8 @@ import { dhtError, nothingFoundError } from './errors/errors.js'
|
|
|
9
9
|
import { Storage as LeofcoinStorageClass } from '@leofcoin/storage'
|
|
10
10
|
import { utils as codecUtils } from '@leofcoin/codecs'
|
|
11
11
|
import Identity from './identity.js'
|
|
12
|
-
import swarm
|
|
13
|
-
import
|
|
12
|
+
import swarm from '@netpeer/swarm/client'
|
|
13
|
+
import SwarmPeer from '@netpeer/swarm/peer'
|
|
14
14
|
|
|
15
15
|
globalThis.LeofcoinStorage = LeofcoinStorageClass
|
|
16
16
|
|
|
@@ -60,11 +60,11 @@ export default class Peernet {
|
|
|
60
60
|
autoStart: boolean = true
|
|
61
61
|
#starting: boolean = false
|
|
62
62
|
#started: boolean = false
|
|
63
|
-
#connections: { [index: PeerId]: P2PTPeer } = {}
|
|
64
63
|
requestProtos = {}
|
|
65
64
|
_messageHandler: MessageHandler
|
|
66
65
|
_peerHandler: PeerDiscovery
|
|
67
66
|
protos: {}
|
|
67
|
+
version
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* @access public
|
|
@@ -72,6 +72,7 @@ export default class Peernet {
|
|
|
72
72
|
* @param {String} options.network - desired network
|
|
73
73
|
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
74
74
|
* @param {String} options.root - path to root directory
|
|
75
|
+
* @param {String} options.version - path to root directory
|
|
75
76
|
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
76
77
|
*
|
|
77
78
|
* @return {Promise} instance of Peernet
|
|
@@ -86,6 +87,7 @@ export default class Peernet {
|
|
|
86
87
|
this.network = options.network || 'leofcoin'
|
|
87
88
|
this.autoStart = options.autoStart === undefined ? true : options.autoStart
|
|
88
89
|
this.stars = options.stars
|
|
90
|
+
this.version = options.version
|
|
89
91
|
const parts = this.network.split(':')
|
|
90
92
|
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet'
|
|
91
93
|
|
|
@@ -162,15 +164,15 @@ export default class Peernet {
|
|
|
162
164
|
* @return {Array} peerId
|
|
163
165
|
*/
|
|
164
166
|
get peers() {
|
|
165
|
-
return Object.entries(this
|
|
167
|
+
return Object.entries(this.client.connections)
|
|
166
168
|
}
|
|
167
169
|
|
|
168
170
|
get connections() {
|
|
169
|
-
return
|
|
171
|
+
return this.client.connections
|
|
170
172
|
}
|
|
171
173
|
|
|
172
174
|
get peerEntries() {
|
|
173
|
-
return Object.values(this
|
|
175
|
+
return Object.values(this.client.connections)
|
|
174
176
|
}
|
|
175
177
|
|
|
176
178
|
/**
|
|
@@ -248,40 +250,44 @@ export default class Peernet {
|
|
|
248
250
|
this._peerHandler = new PeerDiscovery(this.id)
|
|
249
251
|
this.peerId = this.id
|
|
250
252
|
|
|
251
|
-
this.addRequestHandler('handshake', () => {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
})
|
|
256
|
-
|
|
257
|
-
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
pubsub.
|
|
253
|
+
// this.addRequestHandler('handshake', () => {
|
|
254
|
+
// return new peernet.protos['peernet-response']({
|
|
255
|
+
// response: { peerId: this.id }
|
|
256
|
+
// })
|
|
257
|
+
// })
|
|
258
|
+
|
|
259
|
+
// pubsub.subscribe('peer:discovered', async (peer) => {
|
|
260
|
+
// // console.log(peer);
|
|
261
|
+
|
|
262
|
+
// if (this.requestProtos['version'] && !peer.version) {
|
|
263
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
264
|
+
// request: 'version'
|
|
265
|
+
// })
|
|
266
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
267
|
+
// let response = await peer.request(node.encoded)
|
|
268
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
269
|
+
// peer.version = response.decoded.response.version
|
|
270
|
+
// }
|
|
271
|
+
|
|
272
|
+
// if (!peer.peerId) {
|
|
273
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
274
|
+
// request: 'handshake'
|
|
275
|
+
// })
|
|
276
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
277
|
+
// let response = await peer.request(node.encoded)
|
|
278
|
+
|
|
279
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
280
|
+
// // todo: response.decoded should be the response and not response.peerId
|
|
281
|
+
// // todo: ignore above and remove discover completly
|
|
282
|
+
// response.decoded.response.peerId
|
|
283
|
+
// }
|
|
284
|
+
|
|
285
|
+
// this.connections[peer.peerId] = peer
|
|
286
|
+
// pubsub.publish('peer:connected', peer)
|
|
287
|
+
// todo: cleanup discovered
|
|
288
|
+
// })
|
|
289
|
+
|
|
290
|
+
// pubsub.subscribe('peer:left', this.#peerLeft.bind(this))
|
|
285
291
|
|
|
286
292
|
/**
|
|
287
293
|
* converts data -> message -> proto
|
|
@@ -289,20 +295,21 @@ export default class Peernet {
|
|
|
289
295
|
*/
|
|
290
296
|
pubsub.subscribe('peer:data', dataHandler)
|
|
291
297
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
298
|
+
// // todo: remove below, already handles in the swarm
|
|
299
|
+
// if (globalThis.navigator) {
|
|
300
|
+
// globalThis.addEventListener('beforeunload', async () => this.client.close())
|
|
301
|
+
// } else {
|
|
302
|
+
// process.on('SIGTERM', async () => {
|
|
303
|
+
// process.stdin.resume()
|
|
304
|
+
// try {
|
|
305
|
+
// await this.client.close()
|
|
306
|
+
// } catch (error) {
|
|
307
|
+
// // @ts-ignore
|
|
308
|
+
// await this.client.close()
|
|
309
|
+
// }
|
|
310
|
+
// process.exit()
|
|
311
|
+
// })
|
|
312
|
+
// }
|
|
306
313
|
if (this.autoStart) await this.start()
|
|
307
314
|
return this
|
|
308
315
|
}
|
|
@@ -311,24 +318,32 @@ export default class Peernet {
|
|
|
311
318
|
if (this.#starting || this.#started) return
|
|
312
319
|
|
|
313
320
|
this.#starting = true
|
|
314
|
-
const importee = await import('@netpeer/
|
|
321
|
+
const importee = await import('@netpeer/swarm/client')
|
|
315
322
|
/**
|
|
316
323
|
* @access public
|
|
317
324
|
* @type {PeernetClient}
|
|
318
325
|
*/
|
|
319
|
-
|
|
326
|
+
console.log(this.stars)
|
|
327
|
+
|
|
328
|
+
this.client = new importee.default({
|
|
329
|
+
peerId: this.id,
|
|
330
|
+
networkVersion: this.networkVersion,
|
|
331
|
+
version: this.version,
|
|
332
|
+
stars: this.stars
|
|
333
|
+
})
|
|
320
334
|
this.#started = true
|
|
321
335
|
this.#starting = false
|
|
322
336
|
}
|
|
323
337
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
338
|
+
// todo: remove, handled in swarm now
|
|
339
|
+
// #peerLeft(peer: SwarmPeer) {
|
|
340
|
+
// for (const [id, _peer] of Object.entries(this.connections)) {
|
|
341
|
+
// if (_peer.id === peer.id && this.connections[id] && !this.connections[id].connected) {
|
|
342
|
+
// delete this.connections[id]
|
|
343
|
+
// this.removePeer(_peer)
|
|
344
|
+
// }
|
|
345
|
+
// }
|
|
346
|
+
// }
|
|
332
347
|
|
|
333
348
|
addRequestHandler(name, method) {
|
|
334
349
|
this.requestProtos[name] = method
|
|
@@ -445,7 +460,7 @@ export default class Peernet {
|
|
|
445
460
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
|
|
446
461
|
}
|
|
447
462
|
let walks = []
|
|
448
|
-
for (const [peerId, peer] of Object.entries(this
|
|
463
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
449
464
|
if (peerId !== this.id) {
|
|
450
465
|
walks.push(walk(peer, peerId))
|
|
451
466
|
}
|
|
@@ -512,7 +527,7 @@ export default class Peernet {
|
|
|
512
527
|
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name || store)
|
|
513
528
|
|
|
514
529
|
const id = closestPeer.id
|
|
515
|
-
const peer = this
|
|
530
|
+
const peer = this.connections[id]
|
|
516
531
|
|
|
517
532
|
if (peer?.connected) {
|
|
518
533
|
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
@@ -528,7 +543,7 @@ export default class Peernet {
|
|
|
528
543
|
const promises = []
|
|
529
544
|
const providers = await this.providersFor(hash, store)
|
|
530
545
|
for (const provider of Object.values(providers)) {
|
|
531
|
-
const peer = this
|
|
546
|
+
const peer = this.connections[provider.id]
|
|
532
547
|
|
|
533
548
|
if (peer) promises.push(peer.request(node.encoded))
|
|
534
549
|
}
|
|
@@ -743,7 +758,7 @@ export default class Peernet {
|
|
|
743
758
|
// globalSub.publish(topic, data)
|
|
744
759
|
const id = Math.random().toString(36).slice(-12)
|
|
745
760
|
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic })
|
|
746
|
-
for (const [peerId, peer] of Object.entries(this
|
|
761
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
747
762
|
if (peerId !== this.id) {
|
|
748
763
|
const node = await this.prepareMessage(data)
|
|
749
764
|
this.sendMessage(peer, id, node.encoded)
|
package/test.js
CHANGED
|
@@ -1,64 +1,62 @@
|
|
|
1
1
|
import Client from './exports/peernet.js'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
3
|
+
const client = await new Client({
|
|
4
|
+
network: 'leofcoin:peach',
|
|
5
|
+
networkVersion: 'peach',
|
|
6
|
+
stars: ['wss://star.leofcoin.org']
|
|
7
|
+
})
|
|
8
|
+
console.log(pubsub.subscribers)
|
|
9
|
+
pubsub.subscribe('peer:discovered', (data) => {
|
|
10
|
+
// console.log(data);
|
|
11
|
+
console.log(`discovered: ${Object.keys(peernet.client.discovered).length}`)
|
|
12
|
+
console.log(Object.keys(peernet.client.discovered))
|
|
13
|
+
})
|
|
14
|
+
pubsub.subscribe('peer:connected', (data) => {
|
|
15
|
+
// console.log(data);
|
|
16
|
+
console.log(`connections: ${peernet.connections.length}`)
|
|
17
|
+
})
|
|
18
|
+
pubsub.subscribe('peer:left', (data) => {
|
|
19
|
+
console.log(`connections: ${peernet.connections.length}`)
|
|
20
|
+
console.log(`discovered: ${Object.keys(peernet.client.discovered).length}`)
|
|
21
|
+
// console.log(data);
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// peernet.addFolder([{
|
|
25
|
+
// path: 'assets/asset.png',
|
|
26
|
+
// content: Buffer.from('png')
|
|
27
|
+
// }, {
|
|
28
|
+
// path: 'index.html',
|
|
29
|
+
// content: Buffer.from('html')
|
|
30
|
+
// }]).then(hash => peernet.ls(hash).then(paths => peernet.cat(paths[0].hash).then(content => console.log(content))))
|
|
31
|
+
|
|
32
|
+
// pubsub.subscribe('peer:connected', async peer => {
|
|
33
|
+
// chainStore.put('localBlock', '00000')
|
|
34
|
+
// const request = await new globalThis.peernet.protos['peernet-request']({
|
|
35
|
+
// request:'lastBlock'
|
|
36
|
+
// })
|
|
37
|
+
// const to = peer.id
|
|
38
|
+
// await peernet.data.put('hello', 'hi')
|
|
39
|
+
// console.log({request});
|
|
40
|
+
// const node = await peernet.prepareMessage(request)
|
|
41
|
+
// console.log({node});
|
|
42
|
+
// let response = await peer.request(node.encoded)
|
|
43
|
+
// console.log({response});
|
|
44
|
+
// const keys = Object.keys(response)
|
|
45
|
+
// const uint8Array = new Uint8Array(keys.length)
|
|
46
|
+
// for (const key of keys) {
|
|
47
|
+
// uint8Array[Number(key)] = response[key]
|
|
48
|
+
// }
|
|
49
|
+
// // const proto = await new globalThis.peernet.protos['peernet-message'](uint8Array)
|
|
50
|
+
// // console.log(proto.decoded.data);
|
|
51
|
+
// response = await new globalThis.peernet.protos['peernet-response'](uint8Array)
|
|
52
|
+
// console.log({response});
|
|
53
|
+
|
|
54
|
+
// const block = new TextDecoder().decode(response.decoded.response)
|
|
55
|
+
// console.log(block);
|
|
56
|
+
// const task = () => setTimeout(() => {
|
|
57
|
+
// console.log(peernet.connections[0]?.connected);
|
|
58
|
+
// console.log(pubsub.subscribers);
|
|
59
|
+
// task()
|
|
60
|
+
// }, 5000);
|
|
61
|
+
// task()
|
|
62
|
+
// })
|
package/test2.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import Client from './exports/peernet.js'
|
|
2
|
+
;(async () => {
|
|
3
|
+
const client = await new Client({
|
|
4
|
+
root: '.peernet/test2',
|
|
5
|
+
network: 'peach',
|
|
6
|
+
networkVersion: 'peach',
|
|
7
|
+
stars: [['wss://star.leofcoin.org']]
|
|
8
|
+
})
|
|
5
9
|
const job = () => client.publish('socket-data', 'hello')
|
|
6
10
|
setTimeout(async () => {
|
|
7
11
|
job()
|