Skip to content

Commit

Permalink
fix: log not recorded on node 16 when client close
Browse files Browse the repository at this point in the history
  • Loading branch information
yunnysunny committed Sep 1, 2022
1 parent 29eccac commit 1c87267
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# v0.15.2
## Fix
1. Fix log not recorded on node 16+ when client close the http underlay socket. see issue [40775](https://github.com/nodejs/node/issues/40775) on node also.

# v0.15.1
## Fix
1. Fix document url.
Expand Down
9 changes: 5 additions & 4 deletions index.js
Expand Up @@ -130,10 +130,11 @@ function middleware({
}
}

// res.end = function() {
// origianlEnd.apply(res,arguments);
// doLog();
// };
res.on('close', function() {
aborted = !res.writableFinished;
doLog();
hasLoged = true;
});
res.on('finish', function() {
doLog();
hasLoged = true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@yunnysunny/request-logging",
"version": "0.15.1",
"version": "0.15.2",
"description": "Print the express request log to console and save it to kafka when required, and even can send alram message when the response code greater than 500.",
"main": "index.js",
"types": "dist/index.d.ts",
Expand Down
35 changes: 35 additions & 0 deletions test/node/abort.js
@@ -0,0 +1,35 @@
const http = require('http');

let clientReq;
const server = http.createServer(function (req, res) {
req.on('aborted', function () {
console.log('ABORTED');
server.close();
});

req.on('end', () => {
console.log('DESTROY');
clientReq.destroy();
});

req.resume(); // read all client data
res.on('finish', function() {
console.log('res finish');
});
res.on('close', function() {
console.log('res close');
});
});

server.listen(5000, () => {
clientReq = http.request({
method: 'POST',
port: server.address().port,
headers: { connection: 'keep-alive' }
}, (res) => {
});

clientReq.on('error', (err) => console.log('CLIENT ERROR', err));

clientReq.end();
});

0 comments on commit 1c87267

Please sign in to comment.