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

Commit

Permalink
test: fix current tests, add more cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Sep 30, 2016
1 parent 7015586 commit 5fe4674
Show file tree
Hide file tree
Showing 24 changed files with 484 additions and 995 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"aegir": "^8.0.1",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"execa": "^0.4.0",
"expose-loader": "^0.7.1",
"form-data": "^2.0.0",
"fs-pull-blob-store": "^0.3.0",
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ module.exports = {
stats.Wantlist = stats.Wantlist || []
stats.Peers = stats.Peers || []

console.log(`
bitswap status
console.log(`bitswap status
blocks received: ${stats.BlocksReceived}
dup blocks received: ${stats.DupBlksReceived}
dup data received: ${stats.DupDataReceived}B
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commands/block/stat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')
Expand All @@ -24,7 +23,7 @@ module.exports = {
throw err
}

console.log('Key:', bs58.encode(stats.key).toString())
console.log('Key:', stats.key)
console.log('Size:', stats.size)
})
})
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/bootstrap/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
if (err) {
throw err
}

ipfs.bootstrap.add(argv.peer, (err, list) => {
if (err) {
throw err
Expand Down
16 changes: 7 additions & 9 deletions src/cli/commands/files/cat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const waterfall = require('run-waterfall')
const debug = require('debug')
const utils = require('../../utils')
const log = debug('cli:files')
Expand All @@ -14,19 +15,16 @@ module.exports = {

handler (argv) {
const path = argv['ipfs-path']
utils.getIPFS((err, ipfs) => {

waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.files.cat(path, cb)
], (err, file) => {
if (err) {
throw err
}

ipfs.files.cat(path, onFile)
file.pipe(process.stdout)
})
}
}

function onFile (err, file) {
if (err) {
throw (err)
}
file.pipe(process.stdout)
}
3 changes: 1 addition & 2 deletions src/cli/commands/files/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ module.exports = {
if (err) {
throw err
}

console.log(`Saving file(s) to ${ipfsPath}`)
pull(
toPull.source(stream),
pull.asyncMap(fileHandler(dir)),
pull.onEnd((err) => {
console.log('finished writing')
if (err) {
throw err
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function block (self) {
return callback(err)
}
callback(null, {
key: hash,
key: multihash.toB58String(hash),
size: block.data.length
})
})
Expand Down
12 changes: 6 additions & 6 deletions src/http-api/resources/block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const bs58 = require('bs58')
const mh = require('multihashes')
const multipart = require('ipfs-multipart')
const Block = require('ipfs-block')
const debug = require('debug')
Expand All @@ -17,7 +17,7 @@ exports.parseKey = (request, reply) => {

try {
return reply({
key: new Buffer(bs58.decode(request.query.arg))
key: mh.fromB58String(request.query.arg)
})
} catch (err) {
log.error(err)
Expand Down Expand Up @@ -93,7 +93,7 @@ exports.put = {
}

return reply({
Key: bs58.encode(block.key).toString(),
Key: mh.toB58String(block.key),
Size: block.data.length
})
})
Expand All @@ -112,7 +112,7 @@ exports.del = {
if (err) {
log.error(err)
return reply({
Message: 'Failed to get block stats: ' + err,
Message: 'Failed to delete block: ' + err,
Code: 0
}).code(500)
}
Expand All @@ -129,7 +129,7 @@ exports.stat = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const key = request.pre.args.key

console.log('fetching', key)
request.server.app.ipfs.block.stat(key, (err, block) => {
if (err) {
log.error(err)
Expand All @@ -140,7 +140,7 @@ exports.stat = {
}

return reply({
Key: bs58.encode(block.key).toString(),
Key: block.key,
Size: block.size
})
})
Expand Down
58 changes: 47 additions & 11 deletions src/http-api/resources/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,64 @@
'use strict'

const boom = require('boom')
const multiaddr = require('multiaddr')

exports = module.exports

// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
exports.parseKey = (request, reply) => {
if (!request.query.arg) {
return reply("Argument 'multiaddr' is required").code(400).takeover()
}

try {
return reply({
addr: multiaddr(request.query.arg)
})
} catch (err) {
return reply({
Message: 'Not a valid multiaddr',
Code: 0
}).code(500).takeover()
}
}

exports.list = (request, reply) => {
request.server.app.ipfs.bootstrap.list((err, list) => {
const ipfs = request.server.app.ipfs
ipfs.bootstrap.list((err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply(list)
})
}

exports.add = (request, reply) => {
// request.server.app.ipfs.id((err, id) => {
// if (err) { return reply(boom.badRequest(err)) }
// return reply(id)
// })
exports.add = {
parseArgs: exports.parseKey,
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr

ipfs.bootstrap.add(addr.toString(), (err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply()
})
}
}

exports.rm = (request, reply) => {
// request.server.app.ipfs.id((err, id) => {
// if (err) { return reply(boom.badRequest(err)) }
// return reply(id)
// })
exports.rm = {
parseArgs: exports.parseKey,
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr

ipfs.bootstrap.rm(addr.toString(), (err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply()
})
}
}
64 changes: 35 additions & 29 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
const pushable = require('pull-pushable')
const EOL = require('os').EOL
const through = require('through2')
const toStream = require('pull-stream-to-stream')

exports = module.exports

Expand Down Expand Up @@ -55,8 +55,10 @@ exports.cat = {
// - _read method
// - _readableState object
// are there :(
stream._read = () => {}
stream._readableState = {}
if (!stream._read) {
stream._read = () => {}
stream._readableState = {}
}
return reply(stream).header('X-Stream-Output', '1')
})
}
Expand All @@ -72,7 +74,7 @@ exports.get = {
const ipfs = request.server.app.ipfs
const pack = tar.pack()

ipfs.files.get(key, (err, stream) => {
ipfs.files.getPull(key, (err, stream) => {
if (err) {
log.error(err)

Expand All @@ -83,33 +85,37 @@ exports.get = {
return
}

stream.pipe(through.obj((file, enc, cb) => {
const header = {name: file.path}

if (!file.content) {
header.type = 'directory'
pack.entry(header)
cb()
} else {
header.size = file.size
const packStream = pack.entry(header, cb)
if (!packStream) {
// this happens if the request is aborted
// we just skip things then
return cb()
pull(
stream,
pull.asyncMap((file, cb) => {
const header = {name: file.path}
if (!file.content) {
header.type = 'directory'
pack.entry(header)
cb()
} else {
header.size = file.size
const packStream = pack.entry(header, cb)
if (!packStream) {
// this happens if the request is aborted
// we just skip things then
log('other side hung up')
return cb()
}
toStream.source(file.content).pipe(packStream)
}
}),
pull.onEnd((err) => {
if (err) {
log.error(err)
pack.emit('error', err)
pack.destroy()
return
}
file.content.pipe(packStream)
}
}), () => {
if (err) {
log.error(err)
pack.emit('error', err)
pack.destroy()
return
}

pack.finalize()
})
pack.finalize()
})
)

// the reply must read the tar stream,
// to pull values through
Expand Down
23 changes: 8 additions & 15 deletions src/http-api/routes/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const resources = require('./../resources')
const Joi = require('joi')

module.exports = (server) => {
const api = server.select('API')
Expand All @@ -17,14 +16,11 @@ module.exports = (server) => {
api.route({
method: '*',
path: '/api/v0/bootstrap/add',
handler: resources.bootstrap.add,
config: {
validate: {
query: {
arg: Joi.string().required(), // multiaddr to add
default: Joi.boolean()
}
}
pre: [
{ method: resources.bootstrap.add.parseArgs, assign: 'args' }
],
handler: resources.bootstrap.add.handler
}
})

Expand All @@ -39,14 +35,11 @@ module.exports = (server) => {
api.route({
method: '*',
path: '/api/v0/bootstrap/rm',
handler: resources.bootstrap.rm,
config: {
validate: {
query: {
arg: Joi.string().required(), // multiaddr to rm
all: Joi.boolean()
}
}
pre: [
{ method: resources.bootstrap.rm.parseArgs, assign: 'args' }
],
handler: resources.bootstrap.rm.handler
}
})
}
Loading

0 comments on commit 5fe4674

Please sign in to comment.