I'm trying to use a stream as a reply.
const Transform = require('readable-stream').Transform
// ...
const output = new Transform({
objectMode: true,
transform (chunk, encoding, cb) {
this.push(JSON.stringify(transformBandwidth(chunk)) + '\n')
cb()
}
})
res.pipe(output)
reply(output)
.header('x-chunked-output', '1')
I tried removing the objectMode: true so I was changing it from a Stream in object mode to a regular Stream, but it didn't work and i received 'ERR_EMPTY_RESPONSE' instead.
Debug: internal, implementation, error
Error: Cannot reply with stream in object mode
at module.exports.internals.Response.internals.Response._streamify (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\response.js:580:30)
at module.exports.internals.Response.internals.Response._marshal (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\response.js:558:21)
at internals.state (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\transmit.js:80:18)
at Items.parallel (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\transmit.js:459:20)
at Object.exports.parallel (D:\Code\ipfs\js-ipfs\node_modules\items\lib\index.js:47:9)
at Object.internals.state (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\transmit.js:452:11)
at Object.internals.marshal (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\transmit.js:59:15)
at Object.exports.send (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\transmit.js:30:15)
at transmit (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\request.js:497:25)
at Hoek.once (D:\Code\ipfs\js-ipfs\node_modules\hapi\lib\protect.js:50:16)
The output on the browser.
Apparently, wrapping it up with new Readable().wrap(output) makes it work. But why?
@hacdias This is hacky, but you can pipe directly into the raw response object:
myStream.pipe(req.raw.response);
Thanks for the tip @hugomd
This should not be used like that, you're bypassing entirely hapi.
Your stream declares objectMode on both sides, it's normal that hapi rejects it. Use readableObjectMode and writableObjectMode.
Not sure if it is related, but I am also getting this error when trying to return data from a route endpoint after reading a CSV file.
My endpoint is as follows:
server.route({
method: 'POST',
path: '/return_account_owner',
config: {
handler: (request, h) => {
const { command, text, token } = request.payload;
let accountOwner = null;
let stream = fs.createReadStream('./accounts.csv');
let csvStream = csv
.parse()
.on("data", function(data){
if(text == data[1]){
accountOwner = data[2];
}
})
.on("end", function(){
console.log(accountOwner);
return accountOwner;
});
return h.response(stream.pipe(csvStream))
.type('text/event-stream')
.header('Connection', 'keep-alive')
.header('Cache-Control', 'no-cache');
}
}
Would it be best to use transforms and readable-stream like the OP for this scenario or is there another workaround for reading data from a stream and returning the result?
I am getting the same error Debug: internal, implementation, error
Error: Cannot reply with stream in object mode. I'm using Hapi v17.3
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.
Most helpful comment
Apparently, wrapping it up with
new Readable().wrap(output)makes it work. But why?