diff --git a/lib/response.js b/lib/response.js index 241824e10..37d80ab7d 100755 --- a/lib/response.js +++ b/lib/response.js @@ -98,7 +98,7 @@ exports = module.exports = internals.Response = class { this.variety = 'buffer'; this._contentType = 'application/octet-stream'; } - else if (source instanceof Stream) { + else if (Streams.isReadableStream(source)) { this.variety = 'stream'; this._contentType = 'application/octet-stream'; } @@ -529,7 +529,7 @@ exports = module.exports = internals.Response = class { // Stream source - if (source instanceof Stream) { + if (Streams.isStream(source)) { if (typeof source._read !== 'function') { throw Boom.badImplementation('Stream must have a readable interface'); } @@ -607,7 +607,7 @@ exports = module.exports = internals.Response = class { } const stream = this._payload || this.source; - if (stream instanceof Stream) { + if (Streams.isReadableStream(stream)) { internals.Response.drain(stream); } } diff --git a/lib/streams.js b/lib/streams.js index 3f9c9c95b..da7bdb66f 100755 --- a/lib/streams.js +++ b/lib/streams.js @@ -7,6 +7,16 @@ const internals = { team: Symbol('team') }; +exports.isStream = stream => + stream !== null && + typeof stream === 'object' && + typeof stream.pipe === 'function'; + +exports.isReadableStream = stream => + exports.isStream(stream) && + stream.readable !== false && + typeof stream._read === 'function' && + typeof stream._readableState === 'object'; exports.drain = function (stream) {