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

WIP http cat #227

Merged
merged 3 commits into from
May 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cli/commands/files/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ module.exports = Command.extend({
if (err) {
throw err
}
if (utils.isDaemonOn()) {
ipfs.cat(path, (err, res) => {
if (err) {
throw err
}
console.log(res.toString())
})
return
}
ipfs.files.cat(path, (err, res) => {
if (err) {
throw (err)
Expand Down
50 changes: 50 additions & 0 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

const bs58 = require('bs58')
const debug = require('debug')
const log = debug('http-api:files')
log.error = debug('http-api:files:error')

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 'key' is required").code(400).takeover()
}

try {
return reply({
key: new Buffer(bs58.decode(request.query.arg))
})
} catch (err) {
log.error(err)
return reply({
Message: 'invalid ipfs ref path',
Code: 0
}).code(500).takeover()
}
}

exports.cat = {
// uses common parseKey method that returns a `key`
parseArgs: exports.parseKey,

// 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

request.server.app.ipfs.files.cat(key, (err, ee) => {
if (err) {
log.error(err)
return reply({
Message: 'Failed to cat file: ' + err,
Code: 0
}).code(500)
}
ee.on('file', (data) => {
return reply(data.stream)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could lead to strange issues when multiple events are emitted from the stream.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be okay. The exporter stream in object mode only emits one data event per file. And cat can only request one file to my knowledge. If we allow at some point for cat to take multiple hashs and return multiple file objects then we could have a problem with this. Thoughts?

Copy link
Contributor

@hackergrrl hackergrrl May 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably will be okay, but I think @dignifiedquire's point was about approaching the mechanism defensively: just because today we can reason out that this should never happen doesn't mean future changes by future authors will always know+remember to hold this requirement. "It's a stream after all, and you can have many data events on streams.."

})
})
}
}
1 change: 1 addition & 0 deletions src/http-api/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ exports.config = require('./config')
exports.block = require('./block')
exports.swarm = require('./swarm')
exports.bitswap = require('./bitswap')
exports.files = require('./files')
18 changes: 18 additions & 0 deletions src/http-api/routes/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

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

module.exports = (server) => {
const api = server.select('API')

api.route({
method: '*',
path: '/api/v0/cat',
config: {
pre: [
{ method: resources.files.cat.parseArgs, assign: 'args' }
],
handler: resources.files.cat.handler
}
})
}
1 change: 1 addition & 0 deletions src/http-api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ module.exports = (server) => {
require('./config')(server)
require('./swarm')(server)
require('./bitswap')(server)
require('./files')(server)
}
52 changes: 52 additions & 0 deletions test/cli-tests/test-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const nexpect = require('nexpect')
const HttpAPI = require('../../src/http-api')
const repoPath = require('./index').repoPath
const _ = require('lodash')

describe('files', () => {
const env = _.clone(process.env)
env.IPFS_PATH = repoPath

describe('api offline', () => {
it('cat', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'cat', 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
done()
})
})
})

describe('api running', () => {
let httpAPI

before((done) => {
httpAPI = new HttpAPI(repoPath)
httpAPI.start((err) => {
expect(err).to.not.exist
done()
})
})

after((done) => {
httpAPI.stop((err) => {
expect(err).to.not.exist
done()
})
})

it('cat', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'cat', 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
done()
})
})
})
})
85 changes: 85 additions & 0 deletions test/http-api-tests/test-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const APIctl = require('ipfs-api')

module.exports = (httpAPI) => {
describe('files', () => {
describe('api', () => {
let api

it('api', () => {
api = httpAPI.server.select('API')
})

describe('/files/cat', () => {
it('returns 400 for request without argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/cat'
}, (res) => {
expect(res.statusCode).to.equal(400)
expect(res.result).to.be.a('string')
done()
})
})

it('returns 500 for request with invalid argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/cat?arg=invalid'
}, (res) => {
expect(res.statusCode).to.equal(500)
expect(res.result.Message).to.be.a('string')
done()
})
})

it('returns a stream', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/cat?arg=QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.payload).to.equal('hello world' + '\n')
done()
})
})
})
})

describe('using js-ipfs-api', () => {
var ctl

it('start IPFS API ctl', (done) => {
ctl = APIctl('/ip4/127.0.0.1/tcp/6001')
done()
})

describe('ipfs.cat', () => {
it('returns error for request without argument', (done) => {
ctl.cat(null, (err, result) => {
expect(err).to.exist
done()
})
})

it('returns error for request with invalid argument', (done) => {
ctl.cat('invalid', (err, result) => {
expect(err).to.exist
done()
})
})

it('returns a stream', (done) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer is not a stream

ctl.cat('QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o', (err, result) => {
expect(err).to.not.exist
expect(result).to.deep.equal(new Buffer('hello world' + '\n'))
done()
})
})
})
})
})
}