Skip to content

Commit

Permalink
Cleanup for #3946
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Sep 14, 2019
1 parent bad4911 commit 63cbe6d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
14 changes: 10 additions & 4 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ exports = module.exports = internals.Response = class {
this.variety = 'buffer';
this._contentType = 'application/octet-stream';
}
else if (Streams.isReadableStream(source)) {
else if (Streams.isStream(source)) {
this.variety = 'stream';
this._contentType = 'application/octet-stream';
}
Expand Down Expand Up @@ -591,7 +591,13 @@ exports = module.exports = internals.Response = class {
return null;
}

return (this._events.hasListeners('finish') || this._events.hasListeners('peek') ? new internals.Response.Peek(this._events) : null);
if (this._events.hasListeners('peek') ||
this._events.hasListeners('finish')) {

return new internals.Response.Peek(this._events);
}

return null;
}

_close(request) {
Expand All @@ -607,14 +613,14 @@ exports = module.exports = internals.Response = class {
}

const stream = this._payload || this.source;
if (Streams.isReadableStream(stream)) {
if (Streams.isStream(stream)) {
internals.Response.drain(stream);
}
}

_isPayloadSupported() {

return (this.request.method !== 'head' && this.statusCode !== 304 && this.statusCode !== 204);
return this.request.method !== 'head' && this.statusCode !== 304 && this.statusCode !== 204;
}

static drain(stream) {
Expand Down
14 changes: 4 additions & 10 deletions lib/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@ const internals = {
team: Symbol('team')
};


exports.isStream = function (stream) {

return stream !== null &&
typeof stream === 'object' &&
typeof stream.pipe === 'function';
return stream &&
typeof stream === 'object' &&
typeof stream.pipe === 'function';
};

exports.isReadableStream = function (stream) {

return exports.isStream(stream) &&
stream.readable !== false &&
typeof stream._read === 'function' &&
typeof stream._readableState === 'object';
};

exports.drain = function (stream) {

Expand Down
25 changes: 25 additions & 0 deletions test/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,31 @@ describe('Response', () => {
expect(output).to.equal('1234567890!');
});

it('peeks into the response stream (finish only)', async () => {

const server = Hapi.server();

let output = false;
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {

const response = h.response('1234567890');

response.events.once('finish', () => {

output = true;
});

return response;
}
});

await server.inject('/');
expect(output).to.be.true();
});

it('peeks into the response stream (empty)', async () => {

const server = Hapi.server();
Expand Down

0 comments on commit 63cbe6d

Please sign in to comment.