If this attribute doesn't exist, it throws TypeError: Cannot read property 'objectMode' of undefined
It should check if _readableState exists before it checks for objectMode.
Below is the relevant function and the diff for the fix needed.
Hapi: 8.1.0
Node: 0.10.35
npm: 2.1.18
File: hapi/lib/response.js
internals.Response.prototype._streamify = function (source, next) {
if (source instanceof Stream) {
var stream = (source.socket || source);
if (stream._readableState.objectMode) {
return next(Boom.badImplementation('Cannot reply with stream in object mode'));
}
this._payload = source;
return next();
}
// other code
}
Diff
451c451
< if (stream._readableState.objectMode) {
---
> if (stream._readableState && stream._readableState.objectMode) {
What are you passing that's missing the attribute?
I'm replying with a request stream from request package (https://github.com/request/request).
reply(request.get(url))
I have tried using the request to pipe into reply but found that corrupted the file i was trying to reply with.
@nlf wasn't this covered in your fix in 8.1?
Should've been, I think. Will look again.
I might have broke it in my refactor but please check.
Streams are not required to have a _readableState state property. They will get this if the implementation inherits from stream.Readable, but otherwise there is no such guarantee.
If hapi wants to support all streams, you can't depend on this property. If you only want to support streams2+, you need to do additional checks like here, and return an appropriate error when a classic stream is used.
@Thomathan You should be able to solve your issue using readable.wrap().
@Thomathan Did you solve your issue by using wrap, or you waiting for Hapi to support the old streams interface again?
@kanongil I've been busy working on other things and have not tried wrap yet. However, I will be using that to fix my problem, at least until request updates to streams2+
While this issue has been long closed, I just want to follow up that yes, using Readible.wrap does allow you to stream results from the node-request module.
Thanks for the feedback @arb. In my investigations I did find that it it seems to work but as far as I remember I concluded that it is safer to stream the ClientRequest object returned from the response event. This also allows you to handle http status codes and other header information.