Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit efe2f76

Browse files
committed
refactor: bubble swarm up and make a module just for it, preprepare libp2p to be used directly from libp2p instance, take off cat alias, make ping into its own module
1 parent c346c54 commit efe2f76

22 files changed

+155
-140
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"gulp": "^3.9.1",
5050
"idb-plus-blob-store": "^1.1.2",
5151
"idb-pull-blob-store": "^0.4.0",
52-
"interface-ipfs-core": "^0.14.5",
52+
"interface-ipfs-core": "^0.14.6",
5353
"left-pad": "^1.1.1",
5454
"lodash": "^4.15.0",
5555
"ncp": "^2.0.0",
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/core/components/libp2p.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const Libp2pNode = require('libp2p-ipfs').Node
4+
const promisify = require('promisify-es6')
5+
6+
module.exports = function libp2p (self) {
7+
// TODO Just expose libp2p API directly, this start stop wrapping doesn't make that much sense anymore :)
8+
return {
9+
start: promisify((callback) => {
10+
self._libp2pNode = new Libp2pNode(self._peerInfo)
11+
self._libp2pNode.start(() => {
12+
// TODO connect to bootstrap nodes, it will get us more addrs
13+
self._libp2pNode.peerInfo.multiaddrs.forEach((ma) => {
14+
console.log('Swarm listening on', ma.toString())
15+
})
16+
callback()
17+
})
18+
19+
self._libp2pNode.discovery.on('peer', (peerInfo) => {
20+
self._libp2pNode.peerBook.put(peerInfo)
21+
self._libp2pNode.dialByPeerInfo(peerInfo, () => {})
22+
})
23+
self._libp2pNode.swarm.on('peer-mux-established', (peerInfo) => {
24+
self._libp2pNode.peerBook.put(peerInfo)
25+
})
26+
}),
27+
stop: promisify((callback) => {
28+
self._libp2pNode.stop(callback)
29+
})
30+
}
31+
}
File renamed without changes.
File renamed without changes.

src/core/components/ping.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
5+
module.exports = function ping (self) {
6+
return promisify((callback) => {
7+
callback(new Error('Not implemented'))
8+
})
9+
}
File renamed without changes.

src/core/components/swarm.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict'
2+
3+
const multiaddr = require('multiaddr')
4+
const promisify = require('promisify-es6')
5+
6+
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
7+
8+
module.exports = function swarm (self) {
9+
return {
10+
peers: promisify((callback) => {
11+
if (!self.isOnline()) {
12+
return callback(OFFLINE_ERROR)
13+
}
14+
15+
const peers = self._libp2pNode.peerBook.getAll()
16+
const mas = []
17+
Object
18+
.keys(peers)
19+
.forEach((b58Id) => {
20+
peers[b58Id].multiaddrs.forEach((ma) => {
21+
// TODO this should only print the addr we are using
22+
mas.push(ma)
23+
})
24+
})
25+
26+
callback(null, mas)
27+
}),
28+
// all the addrs we know
29+
addrs: promisify((callback) => {
30+
if (!self.isOnline()) {
31+
return callback(OFFLINE_ERROR)
32+
}
33+
const peers = self._libp2pNode.peerBook.getAll()
34+
const mas = []
35+
Object
36+
.keys(peers)
37+
.forEach((b58Id) => {
38+
peers[b58Id].multiaddrs.forEach((ma) => {
39+
// TODO this should only print the addr we are using
40+
mas.push(ma)
41+
})
42+
})
43+
44+
callback(null, mas)
45+
}),
46+
localAddrs: promisify((callback) => {
47+
if (!self.isOnline()) {
48+
return callback(OFFLINE_ERROR)
49+
}
50+
51+
callback(null, self._libp2pNode.peerInfo.multiaddrs)
52+
}),
53+
connect: promisify((maddr, callback) => {
54+
if (!self.isOnline()) {
55+
return callback(OFFLINE_ERROR)
56+
}
57+
58+
if (typeof maddr === 'string') {
59+
maddr = multiaddr(maddr)
60+
}
61+
62+
self._libp2pNode.dialByMultiaddr(maddr, callback)
63+
}),
64+
disconnect: promisify((maddr, callback) => {
65+
if (!self.isOnline()) {
66+
return callback(OFFLINE_ERROR)
67+
}
68+
69+
if (typeof maddr === 'string') {
70+
maddr = multiaddr(maddr)
71+
}
72+
73+
self._libp2pNode.hangUpByMultiaddr(maddr, callback)
74+
}),
75+
filters: promisify((callback) => {
76+
// TODO
77+
throw new Error('Not implemented')
78+
})
79+
}
80+
}
File renamed without changes.

src/core/index.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ const PeerBook = require('peer-book')
77

88
const defaultRepo = require('./default-repo')
99

10-
const goOnline = require('./ipfs/go-online')
11-
const goOffline = require('./ipfs/go-offline')
12-
const isOnline = require('./ipfs/is-online')
13-
const load = require('./ipfs/load')
14-
const version = require('./ipfs/version')
15-
const id = require('./ipfs/id')
16-
const repo = require('./ipfs/repo')
17-
const init = require('./ipfs/init')
18-
const bootstrap = require('./ipfs/bootstrap')
19-
const config = require('./ipfs/config')
20-
const block = require('./ipfs/block')
21-
const object = require('./ipfs/object')
22-
const libp2p = require('./ipfs/libp2p')
23-
const files = require('./ipfs/files')
24-
const bitswap = require('./ipfs/bitswap')
10+
const goOnline = require('./components/go-online')
11+
const goOffline = require('./components/go-offline')
12+
const isOnline = require('./components/is-online')
13+
const load = require('./components/load')
14+
const version = require('./components/version')
15+
const id = require('./components/id')
16+
const repo = require('./components/repo')
17+
const init = require('./components/init')
18+
const bootstrap = require('./components/bootstrap')
19+
const config = require('./components/config')
20+
const block = require('./components/block')
21+
const object = require('./components/object')
22+
const libp2p = require('./components/libp2p')
23+
const swarm = require('./components/swarm')
24+
const ping = require('./components/ping')
25+
const files = require('./components/files')
26+
const bitswap = require('./components/bitswap')
2527

2628
exports = module.exports = IPFS
2729

@@ -35,6 +37,7 @@ function IPFS (repoInstance) {
3537
repoInstance = defaultRepo(repoInstance)
3638
}
3739

40+
// IPFS Core Internals
3841
this._repo = repoInstance
3942
this._peerInfoBook = new PeerBook()
4043
this._peerInfo = null
@@ -43,21 +46,24 @@ function IPFS (repoInstance) {
4346
this._blockS = new BlockService(this._repo)
4447
this._dagS = new DAGService(this._blockS)
4548

49+
// IPFS Core exposed components
50+
// for booting up a node
4651
this.goOnline = goOnline(this)
4752
this.goOffline = goOffline(this)
4853
this.isOnline = isOnline(this)
4954
this.load = load(this)
55+
this.init = init(this)
56+
// interface-ipfs-core defined API
5057
this.version = version(this)
5158
this.id = id(this)
5259
this.repo = repo(this)
53-
this.init = init(this)
5460
this.bootstrap = bootstrap(this)
5561
this.config = config(this)
5662
this.block = block(this)
5763
this.object = object(this)
5864
this.libp2p = libp2p(this)
59-
this.swarm = this.libp2p.swarm // for interface-ipfs-core sake
65+
this.swarm = swarm(this)
6066
this.files = files(this)
61-
this.cat = files(this).cat // Alias for js-ipfs-api cat
6267
this.bitswap = bitswap(this)
68+
this.ping = ping(this)
6369
}

src/core/ipfs/libp2p.js

-111
This file was deleted.

src/http-api/resources/swarm.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ exports.parseAddrs = (request, reply) => {
2727
exports.peers = {
2828
// main route handler which is called after the above `parseArgs`, but only if the args were valid
2929
handler: (request, reply) => {
30-
request.server.app.ipfs.libp2p.swarm.peers((err, peers) => {
30+
request.server.app.ipfs.swarm.peers((err, peers) => {
3131
if (err) {
3232
log.error(err)
3333
return reply({
@@ -48,7 +48,7 @@ exports.peers = {
4848
exports.localAddrs = {
4949
// main route handler which is called after the above `parseArgs`, but only if the args were valid
5050
handler: (request, reply) => {
51-
request.server.app.ipfs.libp2p.swarm.localAddrs((err, addrs) => {
51+
request.server.app.ipfs.swarm.localAddrs((err, addrs) => {
5252
if (err) {
5353
log.error(err)
5454
return reply({
@@ -72,7 +72,7 @@ exports.connect = {
7272
handler: (request, reply) => {
7373
const addr = request.pre.args.addr
7474

75-
request.server.app.ipfs.libp2p.swarm.connect(addr, (err) => {
75+
request.server.app.ipfs.swarm.connect(addr, (err) => {
7676
if (err) {
7777
log.error(err)
7878
return reply({

0 commit comments

Comments
 (0)