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

feat(http): make swarm interface-ipfs-core compliant #489

Merged
merged 5 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"gulp": "^3.9.1",
"idb-plus-blob-store": "^1.1.2",
"idb-pull-blob-store": "^0.4.0",
"interface-ipfs-core": "^0.14.6",
"interface-ipfs-core": "^0.15.0",
"left-pad": "^1.1.1",
"lodash": "^4.15.0",
"ncp": "^2.0.0",
Expand All @@ -68,7 +68,7 @@
"detect-node": "^2.0.3",
"glob": "^7.0.6",
"hapi": "^15.0.3",
"ipfs-api": "^8.0.4",
"ipfs-api": "^9.0.0",
"ipfs-bitswap": "^0.7.0",
"ipfs-block": "^0.3.0",
"ipfs-block-service": "^0.5.0",
Expand All @@ -81,10 +81,12 @@
"joi": "^9.0.4",
"libp2p-ipfs": "^0.14.1",
"libp2p-ipfs-browser": "^0.15.1",
"lodash.flatmap": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.has": "^4.5.2",
"lodash.set": "^4.3.2",
"lodash.sortby": "^4.7.0",
"lodash.values": "^4.3.0",
"mafmt": "^2.1.2",
"map-limit": "0.0.1",
"multiaddr": "^2.0.3",
Expand Down
16 changes: 15 additions & 1 deletion src/cli/commands/swarm/addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ module.exports = {
if (err) {
throw err
}
// TODO

ipfs.swarm.addrs((err, res) => {
if (err) {
throw err
}

res.forEach((peer) => {
const count = peer.multiaddrs.length
console.log(`${peer.id.toB58String()} (${count})`)
peer.multiaddrs.forEach((addr) => {
const res = addr.decapsulate('ipfs').toString()
console.log(`\t${res}`)
})
})
})
})
}
}
4 changes: 2 additions & 2 deletions src/cli/commands/swarm/addrs/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ module.exports = {
throw err
}

res.Strings.forEach((addr) => {
console.log(addr)
res.forEach((addr) => {
console.log(addr.toString())
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/swarm/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ module.exports = {
throw err
}

res.Strings.forEach((addr) => {
console.log(addr)
res.forEach((addr) => {
console.log(addr.toString())
})
})
})
Expand Down
32 changes: 12 additions & 20 deletions src/core/components/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const multiaddr = require('multiaddr')
const promisify = require('promisify-es6')
const flatMap = require('lodash.flatmap')
const values = require('lodash.values')

const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR

Expand All @@ -13,43 +15,31 @@ module.exports = function swarm (self) {
}

const peers = self._libp2pNode.peerBook.getAll()
const mas = []
Object
.keys(peers)
.forEach((b58Id) => {
peers[b58Id].multiaddrs.forEach((ma) => {
// TODO this should only print the addr we are using
mas.push(ma)
})
})
const mas = flatMap(Object.keys(peers), (id) => {
return peers[id].multiaddrs
})

callback(null, mas)
}),

// all the addrs we know
addrs: promisify((callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
}
const peers = self._libp2pNode.peerBook.getAll()
const mas = []
Object
.keys(peers)
.forEach((b58Id) => {
peers[b58Id].multiaddrs.forEach((ma) => {
// TODO this should only print the addr we are using
mas.push(ma)
})
})

callback(null, mas)
const peers = values(self._libp2pNode.peerBook.getAll())
callback(null, peers)
}),

localAddrs: promisify((callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
}

callback(null, self._libp2pNode.peerInfo.multiaddrs)
}),

connect: promisify((maddr, callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
Expand All @@ -61,6 +51,7 @@ module.exports = function swarm (self) {

self._libp2pNode.dialByMultiaddr(maddr, callback)
}),

disconnect: promisify((maddr, callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
Expand All @@ -72,6 +63,7 @@ module.exports = function swarm (self) {

self._libp2pNode.hangUpByMultiaddr(maddr, callback)
}),

filters: promisify((callback) => {
// TODO
throw new Error('Not implemented')
Expand Down
64 changes: 56 additions & 8 deletions src/http-api/resources/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,28 @@ exports.parseAddrs = (request, reply) => {
}

exports.peers = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
request.server.app.ipfs.swarm.peers((err, peers) => {
const ipfs = request.server.app.ipfs
ipfs.swarm.peers((err, peers) => {
if (err) {
log.error(err)
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

return reply({
Strings: peers.map((addr) => addr.toString())
})
})
}
}

exports.addrs = {
handler: (request, reply) => {
const ipfs = request.server.app.ipfs
ipfs.swarm.addrs((err, peers) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -36,19 +55,22 @@ exports.peers = {
}).code(500)
}

const addrs = {}
peers.forEach((peer) => {
addrs[peer.id.toB58String()] = peer.multiaddrs.map((addr) => addr.toString())
})

return reply({
Strings: Object.keys(peers)
.map((key) =>
`${peers[key].multiaddrs[0].toString()}/ipfs/${peers[key].id.toB58String()}`)
Addrs: addrs
})
})
}
}

exports.localAddrs = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
request.server.app.ipfs.swarm.localAddrs((err, addrs) => {
const ipfs = request.server.app.ipfs
ipfs.swarm.localAddrs((err, addrs) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -71,8 +93,9 @@ exports.connect = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const addr = request.pre.args.addr
const ipfs = request.server.app.ipfs

request.server.app.ipfs.swarm.connect(addr, (err) => {
ipfs.swarm.connect(addr, (err) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -87,3 +110,28 @@ exports.connect = {
})
}
}

exports.disconnect = {
// uses common parseAddr method that returns a `addr`
parseArgs: exports.parseAddrs,

// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const addr = request.pre.args.addr
const ipfs = request.server.app.ipfs

ipfs.swarm.disconnect(addr, (err) => {
if (err) {
log.error(err)
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

return reply({
Strings: [`disconnect ${addr} success`]
})
})
}
}
31 changes: 17 additions & 14 deletions src/http-api/routes/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ module.exports = (server) => {
}
})

// api.route({
// method: '*',
// path: '/api/v0/swarm/addrs',
// config: {
// handler: resources.swarm.addrs.handler
// }
// })
api.route({
method: '*',
path: '/api/v0/swarm/addrs',
config: {
handler: resources.swarm.addrs.handler
}
})

api.route({
method: '*',
Expand All @@ -40,13 +40,16 @@ module.exports = (server) => {
}
})

// api.route({
// method: '*',
// path: '/api/v0/swarm/disconnect',
// config: {
// handler: resources.swarm.disconnect
// }
// })
api.route({
method: '*',
path: '/api/v0/swarm/disconnect',
config: {
pre: [
{ method: resources.swarm.disconnect.parseArgs, assign: 'args' }
],
handler: resources.swarm.disconnect.handler
}
})

// TODO
// api.route({
Expand Down
25 changes: 20 additions & 5 deletions test/cli/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('swarm', function () {
expect(err).to.not.exist
ipfs.id((err, identity) => {
expect(err).to.not.exist
ipfsAddr = `${identity.addresses[0]}/ipfs/${identity.id}`
ipfsAddr = identity.addresses[0]
console.log('addr', ipfsAddr)
done()
})
})
Expand All @@ -51,18 +52,32 @@ describe('swarm', function () {
})
})

// TODO revisit these once interface-ipfs-core over http-api are done
it.skip('connect', (done) => {
it('connect', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'connect', ipfsAddr], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout).to.be.eql([
`connect ${ipfsAddr} success`
])
done()
})
})

it.skip('peers', (done) => {
it('peers', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'peers'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout).to.be.eql([
ipfsAddr
])
done()
})
})

it('addrs', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'addrs'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
Expand All @@ -71,7 +86,7 @@ describe('swarm', function () {
})
})

it.skip('addrs local', (done) => {
it('addrs local', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'addrs', 'local'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
Expand Down
7 changes: 2 additions & 5 deletions test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

'use strict'

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

Expand All @@ -17,7 +16,5 @@ const common = {
fc.dismantle(callback)
}
}
*/
// TODO
// Needs: https://github.com/ipfs/js-libp2p-ipfs/pull/16
// test.swarm(common)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diasdavid they are uncommented?

test.swarm(common)