Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 68a764c

Browse files
authored
Merge pull request #36 from ipfs/refactor/hapi-18
refactor: migrate to hapi 18
2 parents daee4b7 + e9069e7 commit 68a764c

File tree

11 files changed

+447
-548
lines changed

11 files changed

+447
-548
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
"interface-datastore": "~0.6.0",
5858
"ipfs-multipart": "~0.1.0",
5959
"ipfs-unixfs": "~0.1.16",
60-
"ipfs-unixfs-importer": "~0.38.0",
6160
"ipfs-unixfs-exporter": "~0.35.5",
61+
"ipfs-unixfs-importer": "~0.38.0",
6262
"ipld-dag-pb": "~0.15.0",
6363
"is-pull-stream": "~0.0.0",
6464
"is-stream": "^1.1.0",

src/http/cp.js

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,49 @@
22

33
const Joi = require('joi')
44

5-
const mfsCp = (api) => {
6-
api.route({
7-
method: 'POST',
8-
path: '/api/v0/files/cp',
9-
config: {
10-
handler: (request, reply) => {
11-
const {
12-
ipfs
13-
} = request.server.app
14-
const {
15-
arg,
16-
parents,
17-
format,
18-
hashAlg,
19-
shardSplitThreshold
20-
} = request.query
5+
const mfsCp = {
6+
method: 'POST',
7+
path: '/api/v0/files/cp',
8+
async handler (request, h) {
9+
const {
10+
ipfs
11+
} = request.server.app
12+
const {
13+
arg,
14+
parents,
15+
format,
16+
hashAlg,
17+
shardSplitThreshold
18+
} = request.query
2119

22-
const args = arg.concat({
23-
parents,
24-
format,
25-
hashAlg,
26-
shardSplitThreshold
27-
})
20+
const args = arg.concat({
21+
parents,
22+
format,
23+
hashAlg,
24+
shardSplitThreshold
25+
})
2826

29-
return ipfs.files.cp.apply(null, args)
30-
.then(() => reply())
31-
.catch(error => {
32-
reply({
33-
Message: error.message,
34-
Code: error.code || 0,
35-
Type: 'error'
36-
}).code(500).takeover()
37-
})
27+
await ipfs.files.cp.apply(null, args)
28+
29+
return h.response()
30+
},
31+
options: {
32+
validate: {
33+
options: {
34+
allowUnknown: true,
35+
stripUnknown: true
3836
},
39-
validate: {
40-
options: {
41-
allowUnknown: true,
42-
stripUnknown: true
43-
},
44-
query: Joi.object().keys({
45-
arg: Joi.array().items(Joi.string()).min(2),
46-
parents: Joi.boolean().default(false),
47-
format: Joi.string().valid([
48-
'dag-pb',
49-
'dag-cbor'
50-
]).default('dag-pb'),
51-
hashAlg: Joi.string().default('sha2-256')
52-
})
53-
}
37+
query: Joi.object().keys({
38+
arg: Joi.array().items(Joi.string()).min(2),
39+
parents: Joi.boolean().default(false),
40+
format: Joi.string().valid([
41+
'dag-pb',
42+
'dag-cbor'
43+
]).default('dag-pb'),
44+
hashAlg: Joi.string().default('sha2-256')
45+
})
5446
}
55-
})
47+
}
5648
}
5749

5850
module.exports = mfsCp

src/http/flush.js

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,32 @@
22

33
const Joi = require('joi')
44

5-
const mfsFlush = (api) => {
6-
api.route({
7-
method: 'POST',
8-
path: '/api/v0/files/flush',
9-
config: {
10-
handler: (request, reply) => {
11-
const {
12-
ipfs
13-
} = request.server.app
14-
const {
15-
arg
16-
} = request.query
5+
const mfsFlush = {
6+
method: 'POST',
7+
path: '/api/v0/files/flush',
8+
async handler (request, h) {
9+
const {
10+
ipfs
11+
} = request.server.app
12+
const {
13+
arg
14+
} = request.query
1715

18-
return ipfs.files.flush.call(null, arg)
19-
.then(() => reply())
20-
.catch(error => {
21-
reply({
22-
Message: error.message,
23-
Code: error.code || 0,
24-
Type: 'error'
25-
}).code(500).takeover()
26-
})
16+
await ipfs.files.flush.call(null, arg)
17+
18+
return h.response()
19+
},
20+
options: {
21+
validate: {
22+
options: {
23+
allowUnknown: true,
24+
stripUnknown: true
2725
},
28-
validate: {
29-
options: {
30-
allowUnknown: true,
31-
stripUnknown: true
32-
},
33-
query: Joi.object().keys({
34-
arg: Joi.string().required()
35-
})
36-
}
26+
query: Joi.object().keys({
27+
arg: Joi.string().required()
28+
})
3729
}
38-
})
30+
}
3931
}
4032

4133
module.exports = mfsFlush

src/http/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ const rm = require('./rm')
1010
const stat = require('./stat')
1111
const write = require('./write')
1212

13-
module.exports = (api) => {
14-
cp(api)
15-
flush(api)
16-
ls(api)
17-
mkdir(api)
18-
mv(api)
19-
read(api)
20-
rm(api)
21-
stat(api)
22-
write(api)
23-
}
13+
module.exports = [
14+
cp,
15+
flush,
16+
ls,
17+
mkdir,
18+
mv,
19+
read,
20+
rm,
21+
stat,
22+
write
23+
]

src/http/ls.js

Lines changed: 64 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -14,103 +14,83 @@ const mapEntry = (entry) => {
1414
}
1515
}
1616

17-
const mfsLs = (api) => {
18-
api.route({
19-
method: 'POST',
20-
path: '/api/v0/files/ls',
21-
config: {
22-
handler: (request, reply) => {
23-
const {
24-
ipfs
25-
} = request.server.app
26-
const {
27-
arg,
17+
const mfsLs = {
18+
method: 'POST',
19+
path: '/api/v0/files/ls',
20+
async handler (request, h) {
21+
const {
22+
ipfs
23+
} = request.server.app
24+
const {
25+
arg,
26+
long,
27+
cidBase,
28+
stream
29+
} = request.query
30+
31+
if (stream) {
32+
const responseStream = await new Promise((resolve, reject) => {
33+
const readableStream = ipfs.files.lsReadableStream(arg, {
2834
long,
29-
cidBase,
30-
stream
31-
} = request.query
35+
cidBase
36+
})
3237

33-
if (stream) {
34-
const readableStream = ipfs.files.lsReadableStream(arg, {
35-
long,
36-
cidBase
37-
})
38+
let passThrough
3839

39-
if (!readableStream._read) {
40-
// make the stream look like a Streams2 to appease Hapi
41-
readableStream._read = () => {}
42-
readableStream._readableState = {}
40+
readableStream.on('data', (entry) => {
41+
if (!passThrough) {
42+
passThrough = new PassThrough()
43+
resolve(passThrough)
4344
}
4445

45-
let passThrough
46-
47-
readableStream.on('data', (entry) => {
48-
if (!passThrough) {
49-
passThrough = new PassThrough()
50-
51-
reply(passThrough)
52-
.header('X-Stream-Output', '1')
53-
}
46+
passThrough.write(JSON.stringify(mapEntry(entry)) + '\n')
47+
})
5448

55-
passThrough.write(JSON.stringify(mapEntry(entry)) + '\n')
56-
})
49+
readableStream.once('end', (entry) => {
50+
if (passThrough) {
51+
passThrough.end(entry ? JSON.stringify(mapEntry(entry)) + '\n' : undefined)
52+
}
53+
})
5754

58-
readableStream.once('end', (entry) => {
59-
if (passThrough) {
60-
passThrough.end(entry ? JSON.stringify(mapEntry(entry)) + '\n' : undefined)
61-
}
62-
})
55+
readableStream.once('error', (error) => {
56+
reject(error)
57+
})
58+
})
6359

64-
readableStream.once('error', (error) => {
65-
reply({
66-
Message: error.message,
67-
Code: error.code || 0,
68-
Type: 'error'
69-
}).code(500).takeover()
70-
})
60+
return h.response(responseStream).header('X-Stream-Output', '1')
61+
}
7162

72-
return
73-
}
63+
const files = await ipfs.files.ls(arg, {
64+
long,
65+
cidBase
66+
})
7467

75-
return ipfs.files.ls(arg, {
76-
long,
77-
cidBase
78-
})
79-
.then(files => {
80-
reply({
81-
Entries: files.map(mapEntry)
82-
})
83-
})
84-
.catch(error => {
85-
reply({
86-
Message: error.message,
87-
Code: error.code || 0,
88-
Type: 'error'
89-
}).code(500).takeover()
90-
})
68+
return h.response({
69+
Entries: files.map(mapEntry)
70+
})
71+
},
72+
options: {
73+
validate: {
74+
options: {
75+
allowUnknown: true,
76+
stripUnknown: true
9177
},
92-
validate: {
93-
options: {
94-
allowUnknown: true,
95-
stripUnknown: true
96-
},
97-
query: Joi.object().keys({
98-
arg: Joi.string().default('/'),
99-
long: Joi.boolean().default(false),
100-
cidBase: Joi.string().default('base58btc'),
101-
stream: Joi.boolean().default(false)
78+
query: Joi.object().keys({
79+
arg: Joi.string().default('/'),
80+
long: Joi.boolean().default(false),
81+
cidBase: Joi.string().default('base58btc'),
82+
stream: Joi.boolean().default(false)
83+
})
84+
.rename('l', 'long', {
85+
override: true,
86+
ignoreUndefined: true
87+
})
88+
.rename('s', 'stream', {
89+
override: true,
90+
ignoreUndefined: true
10291
})
103-
.rename('l', 'long', {
104-
override: true,
105-
ignoreUndefined: true
106-
})
107-
.rename('s', 'stream', {
108-
override: true,
109-
ignoreUndefined: true
110-
})
111-
}
11292
}
113-
})
93+
}
11494
}
11595

11696
module.exports = mfsLs

0 commit comments

Comments
 (0)