Skip to content

Commit

Permalink
Avoid rewriting response.source to preserve existing API
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Feb 17, 2015
1 parent 351ad41 commit 0037838
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,14 @@ internals.Response.prototype._setSource = function (source, variety) {
else if (source instanceof Stream) {
this.variety = 'stream';

// Immediately convert classic readable streams to a streams2 readable
// Immediately wrap classic readable streams in a streams2 readable to preserve initial data

var isReadable = (typeof source._read === 'function' && typeof source._readableState === 'object');
if (!isReadable) {
var isWritable = (typeof source._write === 'function' && typeof source._writableState === 'object');
if (!isWritable && source.readable) {
var readable = new Stream.Readable().wrap(source);
readable.statusCode = source.statusCode;
readable.headers = source.headers;

source = readable;
this._payload = new Stream.Readable().wrap(source);
this._payload.source = source;
}
}
}
Expand Down Expand Up @@ -461,6 +458,10 @@ internals.Response.prototype._marshal = function (next) {
internals.Response.prototype._streamify = function (source, next) {

if (source instanceof Stream) {
if (this._payload && this._payload.source === source) { // Use cached payload from _setSource()
source = this._payload;
}

if (typeof source._read !== 'function' || typeof source._readableState !== 'object') {
return next(Boom.badImplementation('Reply stream must be readable'));
}
Expand Down
2 changes: 2 additions & 0 deletions lib/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ internals.fail = function (request, boom, callback) {
message: boom.message
};

response._close();

response._payload = new Response.Payload(JSON.stringify(minimal), {});
}

Expand Down

0 comments on commit 0037838

Please sign in to comment.