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

Commit

Permalink
refactor: migrate http api to use hapi 18 (#1844)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
alanshaw committed Feb 5, 2019
1 parent bbe561b commit dba3085
Show file tree
Hide file tree
Showing 69 changed files with 3,108 additions and 4,052 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
"fsm-event": "^2.1.0",
"get-folder-size": "^2.0.0",
"glob": "^7.1.3",
"hapi": "^16.6.2",
"hapi-set-header": "^1.0.2",
"hapi": "^18.0.0",
"hapi-pino": "^5.2.0",
"hoek": "^6.1.2",
"human-to-milliseconds": "^1.0.0",
"interface-datastore": "~0.6.0",
Expand All @@ -110,7 +110,7 @@
"ipfs-block-service": "~0.15.1",
"ipfs-http-client": "^29.0.0",
"ipfs-http-response": "~0.2.1",
"ipfs-mfs": "~0.8.0",
"ipfs-mfs": "0.9.0",
"ipfs-multipart": "~0.1.0",
"ipfs-repo": "~0.26.1",
"ipfs-unixfs": "~0.1.16",
Expand Down
29 changes: 18 additions & 11 deletions src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
'use strict'

const promisify = require('promisify-es6')
const utils = require('../utils')
const print = utils.print

let httpAPI
const { getRepoPath, print, ipfsPathHelp } = require('../utils')

module.exports = {
command: 'daemon',
Expand All @@ -13,7 +9,7 @@ module.exports = {

builder (yargs) {
return yargs
.epilog(utils.ipfsPathHelp)
.epilog(ipfsPathHelp)
.option('enable-sharding-experiment', {
type: 'boolean',
default: false
Expand All @@ -40,14 +36,25 @@ module.exports = {
argv.resolve((async () => {
print('Initializing IPFS daemon...')

const repoPath = utils.getRepoPath()
const repoPath = getRepoPath()

// Required inline to reduce startup time
const HttpAPI = require('../../http')
httpAPI = new HttpAPI(process.env.IPFS_PATH, null, argv)
const HttpApi = require('../../http')
const api = new HttpApi({
silent: argv.silent,
repo: process.env.IPFS_PATH,
offline: argv.offline,
pass: argv.pass,
EXPERIMENTAL: {
pubsub: argv.enablePubsubExperiment,
ipnsPubsub: argv.enableNamesysPubsub,
dht: argv.enableDhtExperiment,
sharding: argv.enableShardingExperiment
}
})

try {
await promisify(httpAPI.start)()
await api.start()
} catch (err) {
if (err.code === 'ENOENT' && err.message.match(/uninitialized/i)) {
print('Error: no initialized ipfs repo found in ' + repoPath)
Expand All @@ -61,7 +68,7 @@ module.exports = {

const cleanup = async () => {
print(`Received interrupt signal, shutting down..`)
await promisify(httpAPI.stop)()
await api.stop()
process.exit(0)
}

Expand Down
74 changes: 29 additions & 45 deletions src/http/api/resources/bitswap.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use strict'

const boom = require('boom')
const Joi = require('joi')
const multibase = require('multibase')
const { cidToString } = require('../../../utils/cid')
const parseKey = require('./block').parseKey

exports = module.exports
const { parseKey } = require('./block')

exports.wantlist = {
validate: {
Expand All @@ -15,19 +12,17 @@ exports.wantlist = {
}).unknown()
},

handler: (request, reply) => {
async handler (request, h) {
const { ipfs } = request.server.app
const peerId = request.query.peer
const cidBase = request.query['cid-base']

request.server.app.ipfs.bitswap.wantlist(peerId, (err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
reply({
Keys: list.Keys.map(k => ({
'/': cidToString(k['/'], { base: cidBase, upgrade: false })
}))
})
const list = await ipfs.bitswap.wantlist(peerId)

return h.response({
Keys: list.Keys.map(k => ({
'/': cidToString(k['/'], { base: cidBase, upgrade: false })
}))
})
}
}
Expand All @@ -39,33 +34,26 @@ exports.stat = {
}).unknown()
},

handler: (request, reply) => {
const ipfs = request.server.app.ipfs
async handler (request, h) {
const { ipfs } = request.server.app
const cidBase = request.query['cid-base']

ipfs.bitswap.stat((err, stats) => {
if (err) {
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}
const stats = await ipfs.bitswap.stat()

stats.wantlist = stats.wantlist.map(k => ({
'/': cidToString(k['/'], { base: cidBase, upgrade: false })
}))
stats.wantlist = stats.wantlist.map(k => ({
'/': cidToString(k['/'], { base: cidBase, upgrade: false })
}))

reply({
ProvideBufLen: stats.provideBufLen,
BlocksReceived: stats.blocksReceived,
Wantlist: stats.wantlist,
Peers: stats.peers,
DupBlksReceived: stats.dupBlksReceived,
DupDataReceived: stats.dupDataReceived,
DataReceived: stats.dataReceived,
BlocksSent: stats.blocksSent,
DataSent: stats.dataSent
})
return h.response({
ProvideBufLen: stats.provideBufLen,
BlocksReceived: stats.blocksReceived,
Wantlist: stats.wantlist,
Peers: stats.peers,
DupBlksReceived: stats.dupBlksReceived,
DupDataReceived: stats.dupDataReceived,
DataReceived: stats.dataReceived,
BlocksSent: stats.blocksSent,
DataSent: stats.dataSent
})
}
}
Expand All @@ -81,14 +69,10 @@ exports.unwant = {
parseArgs: parseKey,

// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
async handler (request, h) {
const key = request.pre.args.key
const ipfs = request.server.app.ipfs
ipfs.bitswap.unwant(key, (err) => {
if (err) {
return reply(boom.badRequest(err))
}
reply({ key: cidToString(key, { base: request.query['cid-base'], upgrade: false }) })
})
const { ipfs } = request.server.app
await ipfs.bitswap.unwant(key)
return h.response({ key: cidToString(key, { base: request.query['cid-base'], upgrade: false }) })
}
}
Loading

0 comments on commit dba3085

Please sign in to comment.