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

Commit 09d9f40

Browse files
feat(http): make interface-ipfs-core compliant
Fixes #439
1 parent 3acf9c0 commit 09d9f40

File tree

4 files changed

+70
-27
lines changed

4 files changed

+70
-27
lines changed

current.json

Whitespace-only changes.

src/http-api/resources/swarm.js

+51-8
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ exports.parseAddrs = (request, reply) => {
2525
}
2626

2727
exports.peers = {
28-
// main route handler which is called after the above `parseArgs`, but only if the args were valid
2928
handler: (request, reply) => {
30-
request.server.app.ipfs.libp2p.swarm.peers((err, peers) => {
29+
const ipfs = request.server.app.ipfs
30+
ipfs.libp2p.swarm.peers((err, peers) => {
3131
if (err) {
3232
log.error(err)
3333
return reply({
@@ -37,18 +37,35 @@ exports.peers = {
3737
}
3838

3939
return reply({
40-
Strings: Object.keys(peers)
41-
.map((key) =>
42-
`${peers[key].multiaddrs[0].toString()}/ipfs/${peers[key].id.toB58String()}`)
40+
Strings: peers.map((addr) => addr.toString())
41+
})
42+
})
43+
}
44+
}
45+
46+
exports.addrs = {
47+
handler: (request, reply) => {
48+
const ipfs = request.server.app.ipfs
49+
ipfs.libp2p.swarm.addrs((err, addrs) => {
50+
if (err) {
51+
log.error(err)
52+
return reply({
53+
Message: err.toString(),
54+
Code: 0
55+
}).code(500)
56+
}
57+
58+
return reply({
59+
Addrs: addrs.map((addr) => addr.toString())
4360
})
4461
})
4562
}
4663
}
4764

4865
exports.localAddrs = {
49-
// main route handler which is called after the above `parseArgs`, but only if the args were valid
5066
handler: (request, reply) => {
51-
request.server.app.ipfs.libp2p.swarm.localAddrs((err, addrs) => {
67+
const ipfs = request.server.app.ipfs
68+
ipfs.libp2p.swarm.localAddrs((err, addrs) => {
5269
if (err) {
5370
log.error(err)
5471
return reply({
@@ -71,8 +88,9 @@ exports.connect = {
7188
// main route handler which is called after the above `parseArgs`, but only if the args were valid
7289
handler: (request, reply) => {
7390
const addr = request.pre.args.addr
91+
const ipfs = request.server.app.ipfs
7492

75-
request.server.app.ipfs.libp2p.swarm.connect(addr, (err) => {
93+
ipfs.libp2p.swarm.connect(addr, (err) => {
7694
if (err) {
7795
log.error(err)
7896
return reply({
@@ -87,3 +105,28 @@ exports.connect = {
87105
})
88106
}
89107
}
108+
109+
exports.disconnect = {
110+
// uses common parseAddr method that returns a `addr`
111+
parseArgs: exports.parseAddrs,
112+
113+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
114+
handler: (request, reply) => {
115+
const addr = request.pre.args.addr
116+
const ipfs = request.server.app.ipfs
117+
118+
ipfs.libp2p.swarm.disconnect(addr, (err) => {
119+
if (err) {
120+
log.error(err)
121+
return reply({
122+
Message: err.toString(),
123+
Code: 0
124+
}).code(500)
125+
}
126+
127+
return reply({
128+
Strings: [`disconnect ${addr} success`]
129+
})
130+
})
131+
}
132+
}

src/http-api/routes/swarm.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ module.exports = (server) => {
1313
}
1414
})
1515

16-
// api.route({
17-
// method: '*',
18-
// path: '/api/v0/swarm/addrs',
19-
// config: {
20-
// handler: resources.swarm.addrs.handler
21-
// }
22-
// })
16+
api.route({
17+
method: '*',
18+
path: '/api/v0/swarm/addrs',
19+
config: {
20+
handler: resources.swarm.addrs.handler
21+
}
22+
})
2323

2424
api.route({
2525
method: '*',
@@ -40,13 +40,16 @@ module.exports = (server) => {
4040
}
4141
})
4242

43-
// api.route({
44-
// method: '*',
45-
// path: '/api/v0/swarm/disconnect',
46-
// config: {
47-
// handler: resources.swarm.disconnect
48-
// }
49-
// })
43+
api.route({
44+
method: '*',
45+
path: '/api/v0/swarm/disconnect',
46+
config: {
47+
pre: [
48+
{ method: resources.swarm.disconnect.parseArgs, assign: 'args' }
49+
],
50+
handler: resources.swarm.disconnect.handler
51+
}
52+
})
5053

5154
// TODO
5255
// api.route({

test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
'use strict'
44

5-
/*
65
const test = require('interface-ipfs-core')
76
const FactoryClient = require('./../../utils/factory-http')
87

@@ -17,7 +16,5 @@ const common = {
1716
fc.dismantle(callback)
1817
}
1918
}
20-
*/
21-
// TODO
22-
// Needs: https://github.com/ipfs/js-libp2p-ipfs/pull/16
23-
// test.swarm(common)
19+
20+
test.swarm(common)

0 commit comments

Comments
 (0)