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

Commit dba3085

Browse files
author
Alan Shaw
authored
refactor: migrate http api to use hapi 18 (#1844)
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent bbe561b commit dba3085

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3108
-4052
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
"fsm-event": "^2.1.0",
101101
"get-folder-size": "^2.0.0",
102102
"glob": "^7.1.3",
103-
"hapi": "^16.6.2",
104-
"hapi-set-header": "^1.0.2",
103+
"hapi": "^18.0.0",
104+
"hapi-pino": "^5.2.0",
105105
"hoek": "^6.1.2",
106106
"human-to-milliseconds": "^1.0.0",
107107
"interface-datastore": "~0.6.0",
@@ -110,7 +110,7 @@
110110
"ipfs-block-service": "~0.15.1",
111111
"ipfs-http-client": "^29.0.0",
112112
"ipfs-http-response": "~0.2.1",
113-
"ipfs-mfs": "~0.8.0",
113+
"ipfs-mfs": "0.9.0",
114114
"ipfs-multipart": "~0.1.0",
115115
"ipfs-repo": "~0.26.1",
116116
"ipfs-unixfs": "~0.1.16",

src/cli/commands/daemon.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
4-
const utils = require('../utils')
5-
const print = utils.print
6-
7-
let httpAPI
3+
const { getRepoPath, print, ipfsPathHelp } = require('../utils')
84

95
module.exports = {
106
command: 'daemon',
@@ -13,7 +9,7 @@ module.exports = {
139

1410
builder (yargs) {
1511
return yargs
16-
.epilog(utils.ipfsPathHelp)
12+
.epilog(ipfsPathHelp)
1713
.option('enable-sharding-experiment', {
1814
type: 'boolean',
1915
default: false
@@ -40,14 +36,25 @@ module.exports = {
4036
argv.resolve((async () => {
4137
print('Initializing IPFS daemon...')
4238

43-
const repoPath = utils.getRepoPath()
39+
const repoPath = getRepoPath()
4440

4541
// Required inline to reduce startup time
46-
const HttpAPI = require('../../http')
47-
httpAPI = new HttpAPI(process.env.IPFS_PATH, null, argv)
42+
const HttpApi = require('../../http')
43+
const api = new HttpApi({
44+
silent: argv.silent,
45+
repo: process.env.IPFS_PATH,
46+
offline: argv.offline,
47+
pass: argv.pass,
48+
EXPERIMENTAL: {
49+
pubsub: argv.enablePubsubExperiment,
50+
ipnsPubsub: argv.enableNamesysPubsub,
51+
dht: argv.enableDhtExperiment,
52+
sharding: argv.enableShardingExperiment
53+
}
54+
})
4855

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

6269
const cleanup = async () => {
6370
print(`Received interrupt signal, shutting down..`)
64-
await promisify(httpAPI.stop)()
71+
await api.stop()
6572
process.exit(0)
6673
}
6774

src/http/api/resources/bitswap.js

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
'use strict'
22

3-
const boom = require('boom')
43
const Joi = require('joi')
54
const multibase = require('multibase')
65
const { cidToString } = require('../../../utils/cid')
7-
const parseKey = require('./block').parseKey
8-
9-
exports = module.exports
6+
const { parseKey } = require('./block')
107

118
exports.wantlist = {
129
validate: {
@@ -15,19 +12,17 @@ exports.wantlist = {
1512
}).unknown()
1613
},
1714

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

22-
request.server.app.ipfs.bitswap.wantlist(peerId, (err, list) => {
23-
if (err) {
24-
return reply(boom.badRequest(err))
25-
}
26-
reply({
27-
Keys: list.Keys.map(k => ({
28-
'/': cidToString(k['/'], { base: cidBase, upgrade: false })
29-
}))
30-
})
20+
const list = await ipfs.bitswap.wantlist(peerId)
21+
22+
return h.response({
23+
Keys: list.Keys.map(k => ({
24+
'/': cidToString(k['/'], { base: cidBase, upgrade: false })
25+
}))
3126
})
3227
}
3328
}
@@ -39,33 +34,26 @@ exports.stat = {
3934
}).unknown()
4035
},
4136

42-
handler: (request, reply) => {
43-
const ipfs = request.server.app.ipfs
37+
async handler (request, h) {
38+
const { ipfs } = request.server.app
4439
const cidBase = request.query['cid-base']
4540

46-
ipfs.bitswap.stat((err, stats) => {
47-
if (err) {
48-
return reply({
49-
Message: err.toString(),
50-
Code: 0
51-
}).code(500)
52-
}
41+
const stats = await ipfs.bitswap.stat()
5342

54-
stats.wantlist = stats.wantlist.map(k => ({
55-
'/': cidToString(k['/'], { base: cidBase, upgrade: false })
56-
}))
43+
stats.wantlist = stats.wantlist.map(k => ({
44+
'/': cidToString(k['/'], { base: cidBase, upgrade: false })
45+
}))
5746

58-
reply({
59-
ProvideBufLen: stats.provideBufLen,
60-
BlocksReceived: stats.blocksReceived,
61-
Wantlist: stats.wantlist,
62-
Peers: stats.peers,
63-
DupBlksReceived: stats.dupBlksReceived,
64-
DupDataReceived: stats.dupDataReceived,
65-
DataReceived: stats.dataReceived,
66-
BlocksSent: stats.blocksSent,
67-
DataSent: stats.dataSent
68-
})
47+
return h.response({
48+
ProvideBufLen: stats.provideBufLen,
49+
BlocksReceived: stats.blocksReceived,
50+
Wantlist: stats.wantlist,
51+
Peers: stats.peers,
52+
DupBlksReceived: stats.dupBlksReceived,
53+
DupDataReceived: stats.dupDataReceived,
54+
DataReceived: stats.dataReceived,
55+
BlocksSent: stats.blocksSent,
56+
DataSent: stats.dataSent
6957
})
7058
}
7159
}
@@ -81,14 +69,10 @@ exports.unwant = {
8169
parseArgs: parseKey,
8270

8371
// main route handler which is called after the above `parseArgs`, but only if the args were valid
84-
handler: (request, reply) => {
72+
async handler (request, h) {
8573
const key = request.pre.args.key
86-
const ipfs = request.server.app.ipfs
87-
ipfs.bitswap.unwant(key, (err) => {
88-
if (err) {
89-
return reply(boom.badRequest(err))
90-
}
91-
reply({ key: cidToString(key, { base: request.query['cid-base'], upgrade: false }) })
92-
})
74+
const { ipfs } = request.server.app
75+
await ipfs.bitswap.unwant(key)
76+
return h.response({ key: cidToString(key, { base: request.query['cid-base'], upgrade: false }) })
9377
}
9478
}

0 commit comments

Comments
 (0)