Skip to content

Commit

Permalink
feat: Emit close event on http.ServerResponse (#263)
Browse files Browse the repository at this point in the history
feat: Emit all events written from socket to resposne, instead assuming emitter in serverless-http

feat: Use response#finish event to trigger socket end, instead of vice-versa

feat: Clean up emitter, use once
  • Loading branch information
astuyve authored Jan 7, 2023
1 parent 80bfb3e commit 46c8910
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ module.exports = class ServerlessResponse extends http.ServerResponse {
}
},
});

this.once('finish', () => {
this.emit('close')
});
}

};
20 changes: 19 additions & 1 deletion test/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const url = require('url'),
fs = require('fs'),
expect = require('chai').expect,
request = require('./util/request');
request = require('./util/request'),
sinon = require('sinon');

describe('generic http listener', () => {
let app;
Expand Down Expand Up @@ -153,4 +154,21 @@ describe('generic http listener', () => {
});
});
});

it('should emit close once', () => {
const stub = sinon.stub().returns(true);
app = function (req, res) {
res.on('close', stub);
res.writeHead(200);
res.emit('finish'); // Test multiple finish deliveries
res.end();
};

return request(app, {
httpMethod: 'GET',
path: '/',
}).then(() => {
expect(stub.calledOnce).to.be.true;
});
});
});

0 comments on commit 46c8910

Please sign in to comment.