forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (120 loc) · 3.38 KB
/
index.js
1
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict'
const Libp2p = require('libp2p')
const IPFS = require('ipfs')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WebSocketStar = require('libp2p-websocket-star')
const Bootstrap = require('libp2p-bootstrap')
const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const assert = require('assert')
/**
* Options for the libp2p bundle
* @typedef {Object} libp2pBundle~options
* @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node
* @property {PeerBook} peerBook - The PeerBook of the IPFS node
* @property {Object} config - The config of the IPFS node
* @property {Object} options - The options given to the IPFS node
*/
/**
* This is the bundle we will use to create our fully customized libp2p bundle.
*
* @param {libp2pBundle~options} opts The options to use when generating the libp2p node
* @returns {Libp2p} Our new libp2p node
*/
const libp2pBundle = (opts) => {
// Set convenience variables to clearly showcase some of the useful things that are available
const peerInfo = opts.peerInfo
const peerBook = opts.peerBook
const bootstrapList = opts.config.Bootstrap
// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node
const wsstar = new WebSocketStar({
id: peerInfo.id
})
// Build and return our libp2p node
return new Libp2p({
peerInfo,
peerBook,
// Lets limit the connection managers peers and have it check peer health less frequently
connectionManager: {
maxPeers: 25,
pollInterval: 5000
},
modules: {
transport: [
TCP,
wsstar
],
streamMuxer: [
MPLEX,
SPDY
],
connEncryption: [
SECIO
],
peerDiscovery: [
MulticastDNS,
Bootstrap,
wsstar.discovery
],
dht: KadDHT
},
config: {
peerDiscovery: {
mdns: {
interval: 10000,
enabled: true
},
bootstrap: {
interval: 10000,
enabled: true,
list: bootstrapList
}
},
// Turn on relay with hop active so we can connect to more peers
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
},
dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
dht: true,
pubsub: true
}
}
})
}
// Now that we have our custom libp2p bundle, let's start up the ipfs node!
const node = new IPFS({
libp2p: libp2pBundle
})
// Listen for the node to start, so we can log out some metrics
node.once('start', (err) => {
assert.ifError(err, 'Should startup without issue')
// Lets log out the number of peers we have every 2 seconds
setInterval(() => {
node.swarm.peers((err, peers) => {
if (err) {
console.log('An error occurred trying to check our peers:', err)
process.exit(1)
}
console.log(`The node now has ${peers.length} peers.`)
})
}, 2000)
// Log out the bandwidth stats every 4 seconds so we can see how our configuration is doing
setInterval(() => {
node.stats.bw((err, stats) => {
if (err) {
console.log('An error occurred trying to check our stats:', err)
}
console.log(`\nBandwidth Stats: ${JSON.stringify(stats, null, 2)}\n`)
})
}, 4000)
})