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 (#1408)
Browse files Browse the repository at this point in the history
* fix: allow put empty block & add X-Stream-Output header on get

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 <alan@tableflip.io>

* chore: update interface-ipfs-core dependency

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>

* chore: update ipfs-api dependency

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>

* chore: fix the hack, open the repo not the datastore

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw authored Jun 26, 2018
1 parent 2e94184 commit 52f7aa7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"expose-loader": "~0.7.5",
"form-data": "^2.3.2",
"hat": "0.0.3",
"interface-ipfs-core": "~0.68.2",
"interface-ipfs-core": "~0.69.0",
"ipfsd-ctl": "~0.37.3",
"lodash": "^4.17.10",
"mocha": "^5.1.1",
Expand Down Expand Up @@ -107,7 +107,7 @@
"hoek": "^5.0.3",
"human-to-milliseconds": "^1.0.0",
"interface-datastore": "^0.4.1",
"ipfs-api": "^22.0.0",
"ipfs-api": "^22.1.1",
"ipfs-bitswap": "~0.20.0",
"ipfs-block": "~0.7.1",
"ipfs-block-service": "~0.14.0",
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports = function pin (self) {
}),

// hack for CLI tests
cb => repo.closed ? repo.datastore.open(cb) : cb(null, null),
cb => repo.closed ? repo.open(cb) : cb(null, null),

// save root to datastore under a consistent key
cb => repo.datastore.put(pinDataStoreKey, root.multihash, cb)
Expand Down
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 52f7aa7

Please sign in to comment.