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

Commit 2e41564

Browse files
committed
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>
1 parent 45b705d commit 2e41564

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/http/api/resources/block.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ exports.get = {
4949
}
5050

5151
if (block) {
52-
return reply(block.data)
52+
return reply(block.data).header('X-Stream-Output', '1')
5353
}
54+
5455
return reply({
5556
Message: 'Block was unwanted before it could be remotely retrieved',
5657
Code: 0
@@ -63,32 +64,40 @@ exports.put = {
6364
// pre request handler that parses the args and returns `data` which is assigned to `request.pre.args`
6465
parseArgs: (request, reply) => {
6566
if (!request.payload) {
66-
return reply("File argument 'data' is required").code(400).takeover()
67+
return reply({
68+
Message: "File argument 'data' is required",
69+
Code: 0
70+
}).code(400).takeover()
6771
}
6872

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

7276
parser.on('file', (fileName, fileStream) => {
77+
file = Buffer.alloc(0)
78+
7379
fileStream.on('data', (data) => {
74-
file = data
80+
file = Buffer.concat([file, data])
7581
})
7682
})
7783

7884
parser.on('end', () => {
7985
if (!file) {
80-
return reply("File argument 'data' is required").code(400).takeover()
86+
return reply({
87+
Message: "File argument 'data' is required",
88+
Code: 0
89+
}).code(400).takeover()
8190
}
8291

8392
return reply({
84-
data: file.toString()
93+
data: file
8594
})
8695
})
8796
},
8897

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

94103
waterfall([

0 commit comments

Comments
 (0)