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

Commit

Permalink
fix: allow put empty block & add X-Stream-Output header on get
Browse files Browse the repository at this point in the history
This PR allows the test in ipfs-inactive/interface-js-ipfs-core#308 to pass.

X-Stream-Output header is added to block.get reply for parity with go-ipfs.

block.put is altered to allow an empty block to be put (again for fetaure parity with go-ipfs) but only if there is a multipart file part for it. Error responses have also been improved.

refs ipfs-inactive/js-ipfs-http-client#789
tested by ipfs-inactive/interface-js-ipfs-core#308

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
alanshaw committed Jun 21, 2018
1 parent 45b705d commit 2e41564
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/http/api/resources/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ exports.get = {
}

if (block) {
return reply(block.data)
return reply(block.data).header('X-Stream-Output', '1')
}

return reply({
Message: 'Block was unwanted before it could be remotely retrieved',
Code: 0
Expand All @@ -63,32 +64,40 @@ exports.put = {
// pre request handler that parses the args and returns `data` which is assigned to `request.pre.args`
parseArgs: (request, reply) => {
if (!request.payload) {
return reply("File argument 'data' is required").code(400).takeover()
return reply({
Message: "File argument 'data' is required",
Code: 0
}).code(400).takeover()
}

const parser = multipart.reqParser(request.payload)
var file

parser.on('file', (fileName, fileStream) => {
file = Buffer.alloc(0)

fileStream.on('data', (data) => {
file = data
file = Buffer.concat([file, data])
})
})

parser.on('end', () => {
if (!file) {
return reply("File argument 'data' is required").code(400).takeover()
return reply({
Message: "File argument 'data' is required",
Code: 0
}).code(400).takeover()
}

return reply({
data: file.toString()
data: file
})
})
},

// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const data = Buffer.from(request.pre.args.data)
const data = request.pre.args.data
const ipfs = request.server.app.ipfs

waterfall([
Expand Down

0 comments on commit 2e41564

Please sign in to comment.