Skip to content

Commit

Permalink
Fix close event handling in node 11. Closes #3898
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jan 15, 2019
1 parent 7b85840 commit a3a5ca9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/request.js
Expand Up @@ -586,6 +586,12 @@ internals.event = function ({ request }, event, err) {

request._isPayloadPending = false;

if (event === 'close' &&
request.raw.res.finished) {

return;
}

if (event === 'end') {
return;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/transmit.js
Expand Up @@ -246,6 +246,7 @@ internals.pipe = function (request, stream) {

request.raw.req.on('aborted', aborted);
request.raw.req.on('close', close);

request.raw.res.on('close', close);
request.raw.res.on('error', end);
request.raw.res.on('finish', end);
Expand All @@ -265,12 +266,20 @@ internals.pipe = function (request, stream) {
internals.end = function (env, event, err) {

const { request, stream, team } = env;

if (!team) { // Used instead of cleaning up emitter listeners
return;
}

env.team = null;

if (event === 'close' &&
request.raw.res.finished) {

team.attend();
return;
}

if (err) {
request.raw.res.destroy();
Response.drain(stream);
Expand Down
20 changes: 20 additions & 0 deletions test/request.js
Expand Up @@ -1770,4 +1770,24 @@ describe('Request', () => {
await server.stop({ timeout: 1 });
});
});

describe('event()', () => {

it('does not emit request error on normal close', async () => {

const server = Hapi.server();
const events = [];
server.events.on('request', (request, event, tags) => events.push(tags));

server.route({ method: 'GET', path: '/', handler: () => 'ok' });

await server.start();

const { payload } = await Wreck.get('http://localhost:' + server.info.port);
expect(payload.toString()).to.equal('ok');
await server.stop();

expect(events).to.have.length(0);
});
});
});
35 changes: 35 additions & 0 deletions test/transmit.js
Expand Up @@ -1875,6 +1875,41 @@ describe('transmission', () => {
expect(res.statusCode).to.equal(500);
});
});

describe('end()', () => {

it('node v8 and v10 coverage', async () => {

const AbortStream = class extends Stream.Readable {

constructor(request) {

super();
this.request = request;
}

_read(size) {

if (this.isDone) {
return;
}

this.isDone = true;
this.push('here is the response');
this.push(null);

this.request.raw.res.finished = true;
this.request.raw.req.emit('close');
}
};

const server = Hapi.server();
server.route({ method: 'GET', path: '/', handler: (request, h) => new AbortStream(request) });

const res = await server.inject({ url: '/' });
expect(res.statusCode).to.equal(200);
});
});
});


Expand Down

0 comments on commit a3a5ca9

Please sign in to comment.