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

make the config API spec compliant #340

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"fs-blob-store": "^5.2.1",
"glob": "^7.0.5",
"hapi": "^14.0.0",
"ipfs-api": "^7.0.0",
"ipfs-bitswap": "^0.6.0",
"ipfs-api": "^6.0.3",
"ipfs-block": "^0.3.0",
"ipfs-block-service": "^0.4.0",
"ipfs-merkle-dag": "^0.6.2",
Expand All @@ -80,11 +80,11 @@
"joi": "^9.0.4",
"libp2p-ipfs": "^0.12.1",
"libp2p-ipfs-browser": "^0.12.1",
"lodash.get": "^4.4.0",
"lodash.set": "^4.3.0",
"lodash.get": "^4.4.1",
"lodash.set": "^4.3.1",
"mafmt": "^2.1.1",
"multihashes": "^0.2.2",
"multiaddr": "^2.0.2",
"multihashes": "^0.2.2",
"path-exists": "^3.0.0",
"peer-book": "^0.3.0",
"peer-id": "^0.7.0",
Expand Down Expand Up @@ -117,4 +117,4 @@
"kumavis <kumavis@users.noreply.github.com>",
"nginnever <ginneversource@gmail.com>"
]
}
}
4 changes: 2 additions & 2 deletions src/cli/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = Command.extend({
})
}

ipfs.config.show((err, config) => {
ipfs.config.get((err, config) => {
if (err) {
log.error(err)
throw new Error('failed to read the config')
Expand Down Expand Up @@ -78,7 +78,7 @@ module.exports = Command.extend({
})
}

ipfs.config.show((err, originalConfig) => {
ipfs.config.get((err, originalConfig) => {
if (err) {
log.error(err)
throw new Error('failed to read the config')
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/config/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = Command.extend({
}

function getConfig (next) {
ipfs.config.show((err, config) => {
ipfs.config.get((err, config) => {
if (err) {
log.error(err)
next(new Error('failed to get the config'))
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/config/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = Command.extend({
if (err) {
throw err
}
ipfs.config.show((err, config) => {
ipfs.config.get((err, config) => {
if (err) {
throw err
}
Expand Down
1 change: 1 addition & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function IPFS (repoInstance) {
this.block = block(this)
this.object = object(this)
this.libp2p = libp2p(this)
this.swarm = this.libp2p.swarm // for interface-ipfs-core sake
this.files = files(this)
this.cat = files(this).cat // Alias for js-ipfs-api cat
this.bitswap = bitswap(this)
Expand Down
56 changes: 49 additions & 7 deletions src/core/ipfs/config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
'use strict'

const promisify = require('promisify-es6')
const _get = require('lodash.get')
const _set = require('lodash.set')

module.exports = function config (self) {
return {
// cli only feature built with show and replace
// edit: (callback) => {},
replace: (config, callback) => {
get: promisify((key, callback) => {
if (typeof key === 'function') {
callback = key
key = undefined
}

if (!key) {
return self._repo.config.get(callback)
}

if (typeof key !== 'string') {
return callback(new Error('Invalid key type'))
}

self._repo.config.get((err, config) => {
if (err) {
return callback(err)
}
const value = _get(config, key, undefined)
if (!value) {
callback(new Error('Key does not exist in config'))
} else {
callback(null, value)
}
})
}),
set: promisify((key, value, callback) => {
if (!key || typeof key !== 'string') {
return callback(new Error('Invalid key type'))
}

if (!value || Buffer.isBuffer(value)) {
return callback(new Error('Invalid value type'))
}

self._repo.config.get((err, config) => {
if (err) {
return callback(err)
}
_set(config, key, value)
self.config.replace(config, callback)
})
}),
replace: promisify((config, callback) => {
self._repo.config.set(config, callback)
},
show: (callback) => {
self._repo.config.get(callback)
}
})
}
}
10 changes: 5 additions & 5 deletions src/core/ipfs/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ module.exports = function id (self) {

function ready () {
callback(null, {
ID: self._peerInfo.id.toB58String(),
PublicKey: self._peerInfo.id.pubKey.bytes.toString('base64'),
Addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }).sort(),
AgentVersion: 'js-ipfs',
ProtocolVersion: '9000'
id: self._peerInfo.id.toB58String(),
publicKey: self._peerInfo.id.pubKey.bytes.toString('base64'),
addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }).sort(),
agentVersion: 'js-ipfs',
protocolVersion: '9000'
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exports = module.exports = function HttpApi (repo) {
fs.writeFileSync(apiPath, 'api is on by js-ipfs', {flag: 'w+'})
}

this.ipfs.config.show((err, config) => {
this.ipfs.config.get((err, config) => {
if (err) {
return callback(err)
}
Expand Down
8 changes: 4 additions & 4 deletions src/http-api/resources/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ exports.getOrSet = {

if (value === undefined) {
// Get the value of a given key
return request.server.app.ipfs.config.show((err, config) => {
return request.server.app.ipfs.config.get((err, config) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -84,7 +84,7 @@ exports.getOrSet = {
})
} else {
// Set the new value of a given key
request.server.app.ipfs.config.show((err, originalConfig) => {
request.server.app.ipfs.config.get((err, originalConfig) => {
if (err) {
log.error(err)
return reply({
Expand Down Expand Up @@ -113,8 +113,8 @@ exports.getOrSet = {
}
}

exports.show = (request, reply) => {
return request.server.app.ipfs.config.show((err, config) => {
exports.get = (request, reply) => {
return request.server.app.ipfs.config.get((err, config) => {
if (err) {
log.error(err)
return reply({
Expand Down
13 changes: 11 additions & 2 deletions src/http-api/resources/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ exports = module.exports

exports.get = (request, reply) => {
request.server.app.ipfs.id((err, id) => {
if (err) { return reply(boom.badRequest(err)) }
return reply(id)
if (err) {
return reply(boom.badRequest(err))
}

return reply({
ID: id.id,
PublicKey: id.publicKey,
Addresses: id.addresses,
AgentVersion: id.agentVersion,
ProtocolVersion: id.protocolVersion
})
})
}
8 changes: 4 additions & 4 deletions src/http-api/routes/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ module.exports = (server) => {

// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L818
api.route({
method: 'GET',
method: '*',
path: '/api/v0/bootstrap',
handler: resources.bootstrap.list
})

// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L866
api.route({
method: 'GET',
method: '*',
path: '/api/v0/bootstrap/add',
handler: resources.bootstrap.add,
config: {
Expand All @@ -30,14 +30,14 @@ module.exports = (server) => {

// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L1081
api.route({
method: 'GET',
method: '*',
path: '/api/v0/bootstrap/list',
handler: resources.bootstrap.list
})

// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L1131
api.route({
method: 'GET',
method: '*',
path: '/api/v0/bootstrap/rm',
handler: resources.bootstrap.rm,
config: {
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/routes/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = (server) => {
api.route({
method: '*',
path: '/api/v0/config/show',
handler: resources.config.show
handler: resources.config.get
})

api.route({
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/routes/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = (server) => {
const api = server.select('API')

api.route({
method: 'GET',
method: '*',
path: '/api/v0/id',
handler: resources.id.get
})
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/routes/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (server) => {
const api = server.select('API')

api.route({
method: 'GET',
method: '*',
path: '/api/v0/repo',
handler: resources.repo
})
Expand Down
12 changes: 6 additions & 6 deletions src/http-api/routes/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ module.exports = (server) => {
const api = server.select('API')

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

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

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

api.route({
method: 'GET',
method: '*',
path: '/api/v0/swarm/connect',
config: {
pre: [
Expand All @@ -41,7 +41,7 @@ module.exports = (server) => {
})

// api.route({
// method: 'GET',
// method: '*',
// path: '/api/v0/swarm/disconnect',
// config: {
// handler: resources.swarm.disconnect
Expand All @@ -50,7 +50,7 @@ module.exports = (server) => {

// TODO
// api.route({
// method: 'GET',
// method: '*',
// path: '/api/v0/swarm/filters',
// config: {
// handler: resources.swarm.disconnect
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/routes/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = (server) => {
const api = server.select('API')

api.route({
method: 'GET',
method: '*',
path: '/api/v0/version',
handler: resources.version.get
})
Expand Down
Loading