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

Commit 5fe4674

Browse files
dignifiedquiredaviddias
authored andcommitted
test: fix current tests, add more cli tests
1 parent 7015586 commit 5fe4674

File tree

24 files changed

+484
-995
lines changed

24 files changed

+484
-995
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"aegir": "^8.0.1",
4444
"buffer-loader": "0.0.1",
4545
"chai": "^3.5.0",
46+
"execa": "^0.4.0",
4647
"expose-loader": "^0.7.1",
4748
"form-data": "^2.0.0",
4849
"fs-pull-blob-store": "^0.3.0",

src/cli/commands/bitswap/stat.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ module.exports = {
2323
stats.Wantlist = stats.Wantlist || []
2424
stats.Peers = stats.Peers || []
2525

26-
console.log(`
27-
bitswap status
26+
console.log(`bitswap status
2827
blocks received: ${stats.BlocksReceived}
2928
dup blocks received: ${stats.DupBlksReceived}
3029
dup data received: ${stats.DupDataReceived}B

src/cli/commands/block/stat.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const utils = require('../../utils')
4-
const bs58 = require('bs58')
54
const debug = require('debug')
65
const log = debug('cli:block')
76
log.error = debug('cli:block:error')
@@ -24,7 +23,7 @@ module.exports = {
2423
throw err
2524
}
2625

27-
console.log('Key:', bs58.encode(stats.key).toString())
26+
console.log('Key:', stats.key)
2827
console.log('Size:', stats.size)
2928
})
3029
})

src/cli/commands/bootstrap/add.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
if (err) {
1818
throw err
1919
}
20+
2021
ipfs.bootstrap.add(argv.peer, (err, list) => {
2122
if (err) {
2223
throw err

src/cli/commands/files/cat.js

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

3+
const waterfall = require('run-waterfall')
34
const debug = require('debug')
45
const utils = require('../../utils')
56
const log = debug('cli:files')
@@ -14,19 +15,16 @@ module.exports = {
1415

1516
handler (argv) {
1617
const path = argv['ipfs-path']
17-
utils.getIPFS((err, ipfs) => {
18+
19+
waterfall([
20+
(cb) => utils.getIPFS(cb),
21+
(ipfs, cb) => ipfs.files.cat(path, cb)
22+
], (err, file) => {
1823
if (err) {
1924
throw err
2025
}
2126

22-
ipfs.files.cat(path, onFile)
27+
file.pipe(process.stdout)
2328
})
2429
}
2530
}
26-
27-
function onFile (err, file) {
28-
if (err) {
29-
throw (err)
30-
}
31-
file.pipe(process.stdout)
32-
}

src/cli/commands/files/get.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ module.exports = {
9898
if (err) {
9999
throw err
100100
}
101-
101+
console.log(`Saving file(s) to ${ipfsPath}`)
102102
pull(
103103
toPull.source(stream),
104104
pull.asyncMap(fileHandler(dir)),
105105
pull.onEnd((err) => {
106-
console.log('finished writing')
107106
if (err) {
108107
throw err
109108
}

src/core/components/block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = function block (self) {
3333
return callback(err)
3434
}
3535
callback(null, {
36-
key: hash,
36+
key: multihash.toB58String(hash),
3737
size: block.data.length
3838
})
3939
})

src/http-api/resources/block.js

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

3-
const bs58 = require('bs58')
3+
const mh = require('multihashes')
44
const multipart = require('ipfs-multipart')
55
const Block = require('ipfs-block')
66
const debug = require('debug')
@@ -17,7 +17,7 @@ exports.parseKey = (request, reply) => {
1717

1818
try {
1919
return reply({
20-
key: new Buffer(bs58.decode(request.query.arg))
20+
key: mh.fromB58String(request.query.arg)
2121
})
2222
} catch (err) {
2323
log.error(err)
@@ -93,7 +93,7 @@ exports.put = {
9393
}
9494

9595
return reply({
96-
Key: bs58.encode(block.key).toString(),
96+
Key: mh.toB58String(block.key),
9797
Size: block.data.length
9898
})
9999
})
@@ -112,7 +112,7 @@ exports.del = {
112112
if (err) {
113113
log.error(err)
114114
return reply({
115-
Message: 'Failed to get block stats: ' + err,
115+
Message: 'Failed to delete block: ' + err,
116116
Code: 0
117117
}).code(500)
118118
}
@@ -129,7 +129,7 @@ exports.stat = {
129129
// main route handler which is called after the above `parseArgs`, but only if the args were valid
130130
handler: (request, reply) => {
131131
const key = request.pre.args.key
132-
132+
console.log('fetching', key)
133133
request.server.app.ipfs.block.stat(key, (err, block) => {
134134
if (err) {
135135
log.error(err)
@@ -140,7 +140,7 @@ exports.stat = {
140140
}
141141

142142
return reply({
143-
Key: bs58.encode(block.key).toString(),
143+
Key: block.key,
144144
Size: block.size
145145
})
146146
})

src/http-api/resources/bootstrap.js

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
11
'use strict'
22

33
const boom = require('boom')
4+
const multiaddr = require('multiaddr')
45

56
exports = module.exports
67

8+
// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
9+
exports.parseKey = (request, reply) => {
10+
if (!request.query.arg) {
11+
return reply("Argument 'multiaddr' is required").code(400).takeover()
12+
}
13+
14+
try {
15+
return reply({
16+
addr: multiaddr(request.query.arg)
17+
})
18+
} catch (err) {
19+
return reply({
20+
Message: 'Not a valid multiaddr',
21+
Code: 0
22+
}).code(500).takeover()
23+
}
24+
}
25+
726
exports.list = (request, reply) => {
8-
request.server.app.ipfs.bootstrap.list((err, list) => {
27+
const ipfs = request.server.app.ipfs
28+
ipfs.bootstrap.list((err, list) => {
929
if (err) {
1030
return reply(boom.badRequest(err))
1131
}
1232
return reply(list)
1333
})
1434
}
1535

16-
exports.add = (request, reply) => {
17-
// request.server.app.ipfs.id((err, id) => {
18-
// if (err) { return reply(boom.badRequest(err)) }
19-
// return reply(id)
20-
// })
36+
exports.add = {
37+
parseArgs: exports.parseKey,
38+
handler (request, reply) {
39+
const ipfs = request.server.app.ipfs
40+
const addr = request.pre.args.addr
41+
42+
ipfs.bootstrap.add(addr.toString(), (err, list) => {
43+
if (err) {
44+
return reply(boom.badRequest(err))
45+
}
46+
return reply()
47+
})
48+
}
2149
}
2250

23-
exports.rm = (request, reply) => {
24-
// request.server.app.ipfs.id((err, id) => {
25-
// if (err) { return reply(boom.badRequest(err)) }
26-
// return reply(id)
27-
// })
51+
exports.rm = {
52+
parseArgs: exports.parseKey,
53+
handler (request, reply) {
54+
const ipfs = request.server.app.ipfs
55+
const addr = request.pre.args.addr
56+
57+
ipfs.bootstrap.rm(addr.toString(), (err, list) => {
58+
if (err) {
59+
return reply(boom.badRequest(err))
60+
}
61+
return reply()
62+
})
63+
}
2864
}

src/http-api/resources/files.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const pull = require('pull-stream')
1010
const toPull = require('stream-to-pull-stream')
1111
const pushable = require('pull-pushable')
1212
const EOL = require('os').EOL
13-
const through = require('through2')
13+
const toStream = require('pull-stream-to-stream')
1414

1515
exports = module.exports
1616

@@ -55,8 +55,10 @@ exports.cat = {
5555
// - _read method
5656
// - _readableState object
5757
// are there :(
58-
stream._read = () => {}
59-
stream._readableState = {}
58+
if (!stream._read) {
59+
stream._read = () => {}
60+
stream._readableState = {}
61+
}
6062
return reply(stream).header('X-Stream-Output', '1')
6163
})
6264
}
@@ -72,7 +74,7 @@ exports.get = {
7274
const ipfs = request.server.app.ipfs
7375
const pack = tar.pack()
7476

75-
ipfs.files.get(key, (err, stream) => {
77+
ipfs.files.getPull(key, (err, stream) => {
7678
if (err) {
7779
log.error(err)
7880

@@ -83,33 +85,37 @@ exports.get = {
8385
return
8486
}
8587

86-
stream.pipe(through.obj((file, enc, cb) => {
87-
const header = {name: file.path}
88-
89-
if (!file.content) {
90-
header.type = 'directory'
91-
pack.entry(header)
92-
cb()
93-
} else {
94-
header.size = file.size
95-
const packStream = pack.entry(header, cb)
96-
if (!packStream) {
97-
// this happens if the request is aborted
98-
// we just skip things then
99-
return cb()
88+
pull(
89+
stream,
90+
pull.asyncMap((file, cb) => {
91+
const header = {name: file.path}
92+
if (!file.content) {
93+
header.type = 'directory'
94+
pack.entry(header)
95+
cb()
96+
} else {
97+
header.size = file.size
98+
const packStream = pack.entry(header, cb)
99+
if (!packStream) {
100+
// this happens if the request is aborted
101+
// we just skip things then
102+
log('other side hung up')
103+
return cb()
104+
}
105+
toStream.source(file.content).pipe(packStream)
106+
}
107+
}),
108+
pull.onEnd((err) => {
109+
if (err) {
110+
log.error(err)
111+
pack.emit('error', err)
112+
pack.destroy()
113+
return
100114
}
101-
file.content.pipe(packStream)
102-
}
103-
}), () => {
104-
if (err) {
105-
log.error(err)
106-
pack.emit('error', err)
107-
pack.destroy()
108-
return
109-
}
110115

111-
pack.finalize()
112-
})
116+
pack.finalize()
117+
})
118+
)
113119

114120
// the reply must read the tar stream,
115121
// to pull values through

src/http-api/routes/bootstrap.js

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

33
const resources = require('./../resources')
4-
const Joi = require('joi')
54

65
module.exports = (server) => {
76
const api = server.select('API')
@@ -17,14 +16,11 @@ module.exports = (server) => {
1716
api.route({
1817
method: '*',
1918
path: '/api/v0/bootstrap/add',
20-
handler: resources.bootstrap.add,
2119
config: {
22-
validate: {
23-
query: {
24-
arg: Joi.string().required(), // multiaddr to add
25-
default: Joi.boolean()
26-
}
27-
}
20+
pre: [
21+
{ method: resources.bootstrap.add.parseArgs, assign: 'args' }
22+
],
23+
handler: resources.bootstrap.add.handler
2824
}
2925
})
3026

@@ -39,14 +35,11 @@ module.exports = (server) => {
3935
api.route({
4036
method: '*',
4137
path: '/api/v0/bootstrap/rm',
42-
handler: resources.bootstrap.rm,
4338
config: {
44-
validate: {
45-
query: {
46-
arg: Joi.string().required(), // multiaddr to rm
47-
all: Joi.boolean()
48-
}
49-
}
39+
pre: [
40+
{ method: resources.bootstrap.rm.parseArgs, assign: 'args' }
41+
],
42+
handler: resources.bootstrap.rm.handler
5043
}
5144
})
5245
}

0 commit comments

Comments
 (0)