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
Merged
WIP http cat #227
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.."