@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/exports/peernet.js
CHANGED
|
@@ -425,17 +425,18 @@ class Peernet {
|
|
|
425
425
|
autoStart = true;
|
|
426
426
|
#starting = false;
|
|
427
427
|
#started = false;
|
|
428
|
-
#connections = {};
|
|
429
428
|
requestProtos = {};
|
|
430
429
|
_messageHandler;
|
|
431
430
|
_peerHandler;
|
|
432
431
|
protos;
|
|
432
|
+
version;
|
|
433
433
|
/**
|
|
434
434
|
* @access public
|
|
435
435
|
* @param {Object} options
|
|
436
436
|
* @param {String} options.network - desired network
|
|
437
437
|
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
438
438
|
* @param {String} options.root - path to root directory
|
|
439
|
+
* @param {String} options.version - path to root directory
|
|
439
440
|
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
440
441
|
*
|
|
441
442
|
* @return {Promise} instance of Peernet
|
|
@@ -450,6 +451,7 @@ class Peernet {
|
|
|
450
451
|
this.network = options.network || 'leofcoin';
|
|
451
452
|
this.autoStart = options.autoStart === undefined ? true : options.autoStart;
|
|
452
453
|
this.stars = options.stars;
|
|
454
|
+
this.version = options.version;
|
|
453
455
|
const parts = this.network.split(':');
|
|
454
456
|
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet';
|
|
455
457
|
if (!options.storePrefix)
|
|
@@ -514,13 +516,13 @@ class Peernet {
|
|
|
514
516
|
* @return {Array} peerId
|
|
515
517
|
*/
|
|
516
518
|
get peers() {
|
|
517
|
-
return Object.entries(this
|
|
519
|
+
return Object.entries(this.client.connections);
|
|
518
520
|
}
|
|
519
521
|
get connections() {
|
|
520
|
-
return
|
|
522
|
+
return this.client.connections;
|
|
521
523
|
}
|
|
522
524
|
get peerEntries() {
|
|
523
|
-
return Object.values(this
|
|
525
|
+
return Object.values(this.client.connections);
|
|
524
526
|
}
|
|
525
527
|
/**
|
|
526
528
|
* @return {String} id - peerId
|
|
@@ -575,55 +577,58 @@ class Peernet {
|
|
|
575
577
|
await this.identity.load(password);
|
|
576
578
|
this._peerHandler = new PeerDiscovery(this.id);
|
|
577
579
|
this.peerId = this.id;
|
|
578
|
-
this.addRequestHandler('handshake', () => {
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
})
|
|
583
|
-
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
}
|
|
605
|
-
|
|
580
|
+
// this.addRequestHandler('handshake', () => {
|
|
581
|
+
// return new peernet.protos['peernet-response']({
|
|
582
|
+
// response: { peerId: this.id }
|
|
583
|
+
// })
|
|
584
|
+
// })
|
|
585
|
+
// pubsub.subscribe('peer:discovered', async (peer) => {
|
|
586
|
+
// // console.log(peer);
|
|
587
|
+
// if (this.requestProtos['version'] && !peer.version) {
|
|
588
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
589
|
+
// request: 'version'
|
|
590
|
+
// })
|
|
591
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
592
|
+
// let response = await peer.request(node.encoded)
|
|
593
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
594
|
+
// peer.version = response.decoded.response.version
|
|
595
|
+
// }
|
|
596
|
+
// if (!peer.peerId) {
|
|
597
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
598
|
+
// request: 'handshake'
|
|
599
|
+
// })
|
|
600
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
601
|
+
// let response = await peer.request(node.encoded)
|
|
602
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
603
|
+
// // todo: response.decoded should be the response and not response.peerId
|
|
604
|
+
// // todo: ignore above and remove discover completly
|
|
605
|
+
// response.decoded.response.peerId
|
|
606
|
+
// }
|
|
607
|
+
// this.connections[peer.peerId] = peer
|
|
608
|
+
// pubsub.publish('peer:connected', peer)
|
|
609
|
+
// todo: cleanup discovered
|
|
610
|
+
// })
|
|
611
|
+
// pubsub.subscribe('peer:left', this.#peerLeft.bind(this))
|
|
606
612
|
/**
|
|
607
613
|
* converts data -> message -> proto
|
|
608
614
|
* @see DataHandler
|
|
609
615
|
*/
|
|
610
616
|
pubsub.subscribe('peer:data', dataHandler);
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
else {
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
}
|
|
617
|
+
// // todo: remove below, already handles in the swarm
|
|
618
|
+
// if (globalThis.navigator) {
|
|
619
|
+
// globalThis.addEventListener('beforeunload', async () => this.client.close())
|
|
620
|
+
// } else {
|
|
621
|
+
// process.on('SIGTERM', async () => {
|
|
622
|
+
// process.stdin.resume()
|
|
623
|
+
// try {
|
|
624
|
+
// await this.client.close()
|
|
625
|
+
// } catch (error) {
|
|
626
|
+
// // @ts-ignore
|
|
627
|
+
// await this.client.close()
|
|
628
|
+
// }
|
|
629
|
+
// process.exit()
|
|
630
|
+
// })
|
|
631
|
+
// }
|
|
627
632
|
if (this.autoStart)
|
|
628
633
|
await this.start();
|
|
629
634
|
return this;
|
|
@@ -632,23 +637,30 @@ class Peernet {
|
|
|
632
637
|
if (this.#starting || this.#started)
|
|
633
638
|
return;
|
|
634
639
|
this.#starting = true;
|
|
635
|
-
const importee = await import('@netpeer/
|
|
640
|
+
const importee = await import('@netpeer/swarm/client');
|
|
636
641
|
/**
|
|
637
642
|
* @access public
|
|
638
643
|
* @type {PeernetClient}
|
|
639
644
|
*/
|
|
640
|
-
|
|
645
|
+
console.log(this.stars);
|
|
646
|
+
this.client = new importee.default({
|
|
647
|
+
peerId: this.id,
|
|
648
|
+
networkVersion: this.networkVersion,
|
|
649
|
+
version: this.version,
|
|
650
|
+
stars: this.stars
|
|
651
|
+
});
|
|
641
652
|
this.#started = true;
|
|
642
653
|
this.#starting = false;
|
|
643
654
|
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
}
|
|
655
|
+
// todo: remove, handled in swarm now
|
|
656
|
+
// #peerLeft(peer: SwarmPeer) {
|
|
657
|
+
// for (const [id, _peer] of Object.entries(this.connections)) {
|
|
658
|
+
// if (_peer.id === peer.id && this.connections[id] && !this.connections[id].connected) {
|
|
659
|
+
// delete this.connections[id]
|
|
660
|
+
// this.removePeer(_peer)
|
|
661
|
+
// }
|
|
662
|
+
// }
|
|
663
|
+
// }
|
|
652
664
|
addRequestHandler(name, method) {
|
|
653
665
|
this.requestProtos[name] = method;
|
|
654
666
|
}
|
|
@@ -752,7 +764,7 @@ class Peernet {
|
|
|
752
764
|
this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
753
765
|
};
|
|
754
766
|
let walks = [];
|
|
755
|
-
for (const [peerId, peer] of Object.entries(this
|
|
767
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
756
768
|
if (peerId !== this.id) {
|
|
757
769
|
walks.push(walk(peer, peerId));
|
|
758
770
|
}
|
|
@@ -819,7 +831,7 @@ class Peernet {
|
|
|
819
831
|
if (!closestPeer || !closestPeer.id)
|
|
820
832
|
return this.requestData(hash, store?.name || store);
|
|
821
833
|
const id = closestPeer.id;
|
|
822
|
-
const peer = this
|
|
834
|
+
const peer = this.connections[id];
|
|
823
835
|
if (peer?.connected) {
|
|
824
836
|
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
825
837
|
hash,
|
|
@@ -833,7 +845,7 @@ class Peernet {
|
|
|
833
845
|
const promises = [];
|
|
834
846
|
const providers = await this.providersFor(hash, store);
|
|
835
847
|
for (const provider of Object.values(providers)) {
|
|
836
|
-
const peer = this
|
|
848
|
+
const peer = this.connections[provider.id];
|
|
837
849
|
if (peer)
|
|
838
850
|
promises.push(peer.request(node.encoded));
|
|
839
851
|
}
|
|
@@ -1047,7 +1059,7 @@ class Peernet {
|
|
|
1047
1059
|
// globalSub.publish(topic, data)
|
|
1048
1060
|
const id = Math.random().toString(36).slice(-12);
|
|
1049
1061
|
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
|
|
1050
|
-
for (const [peerId, peer] of Object.entries(this
|
|
1062
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
1051
1063
|
if (peerId !== this.id) {
|
|
1052
1064
|
const node = await this.prepareMessage(data);
|
|
1053
1065
|
this.sendMessage(peer, id, node.encoded);
|
|
@@ -6,7 +6,8 @@ import DHT from './dht/dht.js';
|
|
|
6
6
|
import MessageHandler from './handlers/message.js';
|
|
7
7
|
import { Storage as LeofcoinStorageClass } from '@leofcoin/storage';
|
|
8
8
|
import Identity from './identity.js';
|
|
9
|
-
import swarm from '@netpeer/
|
|
9
|
+
import swarm from '@netpeer/swarm/client';
|
|
10
|
+
import SwarmPeer from '@netpeer/swarm/peer';
|
|
10
11
|
declare global {
|
|
11
12
|
var LeofcoinStorage: typeof LeofcoinStorageClass;
|
|
12
13
|
var peernet: Peernet;
|
|
@@ -51,12 +52,14 @@ export default class Peernet {
|
|
|
51
52
|
_messageHandler: MessageHandler;
|
|
52
53
|
_peerHandler: PeerDiscovery;
|
|
53
54
|
protos: {};
|
|
55
|
+
version: any;
|
|
54
56
|
/**
|
|
55
57
|
* @access public
|
|
56
58
|
* @param {Object} options
|
|
57
59
|
* @param {String} options.network - desired network
|
|
58
60
|
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
59
61
|
* @param {String} options.root - path to root directory
|
|
62
|
+
* @param {String} options.version - path to root directory
|
|
60
63
|
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
61
64
|
*
|
|
62
65
|
* @return {Promise} instance of Peernet
|
|
@@ -82,13 +85,15 @@ export default class Peernet {
|
|
|
82
85
|
*
|
|
83
86
|
* @return {Array} peerId
|
|
84
87
|
*/
|
|
85
|
-
get peers(): [string,
|
|
86
|
-
get connections():
|
|
87
|
-
|
|
88
|
+
get peers(): [string, SwarmPeer][];
|
|
89
|
+
get connections(): {
|
|
90
|
+
[x: string]: SwarmPeer;
|
|
91
|
+
};
|
|
92
|
+
get peerEntries(): SwarmPeer[];
|
|
88
93
|
/**
|
|
89
94
|
* @return {String} id - peerId
|
|
90
95
|
*/
|
|
91
|
-
getConnection(id: any):
|
|
96
|
+
getConnection(id: any): SwarmPeer;
|
|
92
97
|
/**
|
|
93
98
|
* @private
|
|
94
99
|
*
|
|
@@ -238,6 +243,6 @@ export default class Peernet {
|
|
|
238
243
|
* @param {Method} cb
|
|
239
244
|
*/
|
|
240
245
|
subscribe(topic: string, callback: Function): Promise<void>;
|
|
241
|
-
removePeer(peer: any): Promise<
|
|
246
|
+
removePeer(peer: any): Promise<any>;
|
|
242
247
|
get Buffer(): BufferConstructor;
|
|
243
248
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.68",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/peernet.js",
|
|
6
6
|
"exports": {
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"@leofcoin/peernet-swarm": "^1.1.0",
|
|
38
38
|
"@leofcoin/storage": "^3.0.0",
|
|
39
39
|
"@netpeer/p2pt-swarm": "^1.3.5",
|
|
40
|
+
"@netpeer/swarm": "^0.8.1",
|
|
40
41
|
"@types/node": "^18.11.18",
|
|
41
42
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
42
43
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
@@ -48,7 +49,7 @@
|
|
|
48
49
|
"multi-signature": "^1.3.1",
|
|
49
50
|
"qr-scanner": "^1.4.2",
|
|
50
51
|
"qrcode": "^1.5.1",
|
|
51
|
-
"socket-request-client": "^
|
|
52
|
+
"socket-request-client": "^2.0.6",
|
|
52
53
|
"socket-request-server": "^1.5.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
package/rollup.config.js
CHANGED
|
@@ -1,76 +1,70 @@
|
|
|
1
|
-
import resolve, { nodeResolve } from
|
|
2
|
-
import commonjs from
|
|
3
|
-
import json from
|
|
4
|
-
import wasm from
|
|
5
|
-
import rimraf from
|
|
6
|
-
import typescript from
|
|
7
|
-
import modify from
|
|
1
|
+
import resolve, { nodeResolve } from '@rollup/plugin-node-resolve'
|
|
2
|
+
import commonjs from '@rollup/plugin-commonjs'
|
|
3
|
+
import json from '@rollup/plugin-json'
|
|
4
|
+
import wasm from '@rollup/plugin-wasm'
|
|
5
|
+
import rimraf from 'rimraf'
|
|
6
|
+
import typescript from '@rollup/plugin-typescript'
|
|
7
|
+
import modify from 'rollup-plugin-modify'
|
|
8
8
|
|
|
9
|
-
rimraf.sync(
|
|
9
|
+
rimraf.sync('./exports/**')
|
|
10
10
|
|
|
11
11
|
export default [
|
|
12
12
|
{
|
|
13
|
-
input: [
|
|
14
|
-
"./src/peernet.ts",
|
|
15
|
-
"./node_modules/@leofcoin/storage/exports/browser-store.js",
|
|
16
|
-
],
|
|
13
|
+
input: ['./src/peernet.ts', './node_modules/@leofcoin/storage/exports/browser-store.js'],
|
|
17
14
|
output: {
|
|
18
|
-
format:
|
|
19
|
-
dir:
|
|
15
|
+
format: 'es',
|
|
16
|
+
dir: './exports/browser'
|
|
20
17
|
},
|
|
21
18
|
plugins: [
|
|
22
19
|
json(),
|
|
23
20
|
wasm(),
|
|
24
21
|
modify({
|
|
25
|
-
"@netpeer/
|
|
22
|
+
"await import('@netpeer/swarm/client')": (await import('@netpeer/swarm/browser')).default.toString()
|
|
26
23
|
}),
|
|
27
24
|
resolve({
|
|
28
25
|
browser: true,
|
|
29
26
|
preferBuiltins: false,
|
|
30
|
-
mainFields: [
|
|
27
|
+
mainFields: ['browser', 'module', 'main']
|
|
31
28
|
}),
|
|
32
29
|
commonjs({
|
|
33
|
-
mainFields: [
|
|
30
|
+
mainFields: ['browser', 'module', 'main']
|
|
34
31
|
}),
|
|
35
32
|
|
|
36
|
-
typescript({ compilerOptions: { outDir:
|
|
33
|
+
typescript({ compilerOptions: { outDir: './exports/browser' } })
|
|
37
34
|
],
|
|
38
|
-
external: [
|
|
35
|
+
external: ['./prompts/password.js']
|
|
39
36
|
},
|
|
40
37
|
{
|
|
41
|
-
input: [
|
|
42
|
-
"./src/peernet.ts",
|
|
43
|
-
"./node_modules/@leofcoin/storage/exports/store.js",
|
|
44
|
-
],
|
|
38
|
+
input: ['./src/peernet.ts', './node_modules/@leofcoin/storage/exports/store.js'],
|
|
45
39
|
output: {
|
|
46
|
-
format:
|
|
47
|
-
dir:
|
|
40
|
+
format: 'es',
|
|
41
|
+
dir: './exports'
|
|
48
42
|
},
|
|
49
43
|
plugins: [
|
|
50
44
|
modify({
|
|
51
|
-
|
|
45
|
+
'@netpeer/swarm/client': '@netpeer/swarm/client'
|
|
52
46
|
}),
|
|
53
47
|
typescript({
|
|
54
48
|
compilerOptions: {
|
|
55
|
-
outDir:
|
|
56
|
-
declarationDir:
|
|
57
|
-
}
|
|
58
|
-
})
|
|
49
|
+
outDir: './exports',
|
|
50
|
+
declarationDir: './exports/types'
|
|
51
|
+
}
|
|
52
|
+
})
|
|
59
53
|
],
|
|
60
|
-
external: [
|
|
54
|
+
external: ['./prompts/password.js']
|
|
61
55
|
},
|
|
62
56
|
{
|
|
63
|
-
input: [
|
|
57
|
+
input: ['./src/prompts/password/browser.js'],
|
|
64
58
|
output: {
|
|
65
|
-
format:
|
|
66
|
-
file:
|
|
67
|
-
}
|
|
59
|
+
format: 'es',
|
|
60
|
+
file: './exports/browser/src/prompts/password.js'
|
|
61
|
+
}
|
|
68
62
|
},
|
|
69
63
|
{
|
|
70
|
-
input: [
|
|
64
|
+
input: ['./src/prompts/password/node.js'],
|
|
71
65
|
output: {
|
|
72
|
-
format:
|
|
73
|
-
file:
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
]
|
|
66
|
+
format: 'es',
|
|
67
|
+
file: './exports/src/prompts/password.js'
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
]
|