This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from ipfs/cat/http
WIP http cat
- Loading branch information
Showing
14 changed files
with
228 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, stream) => { | ||
if (err) { | ||
log.error(err) | ||
return reply({ | ||
Message: 'Failed to cat file: ' + err, | ||
Code: 0 | ||
}).code(500) | ||
} | ||
stream.on('data', (data) => { | ||
return reply(data.stream) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* 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 buffer', (done) => { | ||
api.inject({ | ||
method: 'GET', | ||
url: '/api/v0/cat?arg=QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' | ||
}, (res) => { | ||
expect(res.statusCode).to.equal(200) | ||
expect(res.rawPayload).to.deep.equal(new Buffer('hello world' + '\n')) | ||
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 buffer', (done) => { | ||
ctl.cat('QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o', (err, result) => { | ||
expect(err).to.not.exist | ||
expect(result).to.deep.equal(new Buffer('hello world' + '\n')) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |