Skip to content

Commit d25d32e

Browse files
authored
fix: only set keep-alive header before Node.js 14.8.0 (#4457)
1 parent a7ae46c commit d25d32e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

app/middleware/meta.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
'use strict';
66

7+
const semver = require('semver');
8+
79
module.exports = options => {
10+
// Node.js >=14.8.0 will set Keep-Alive Header, see https://github.com/nodejs/node/pull/34561
11+
const shouldPatchKeepAliveHeader = semver.lt(process.version, '14.8.0');
12+
813
return async function meta(ctx, next) {
914
if (options.logging) {
1015
ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s', ctx.host, ctx.header['user-agent']);
@@ -13,9 +18,9 @@ module.exports = options => {
1318
// total response time header
1419
ctx.set('x-readtime', Date.now() - ctx.starttime);
1520

16-
// try to support Keep-Alive Header
21+
// try to support Keep-Alive Header when < 14.8.0
1722
const server = ctx.app.server;
18-
if (server && server.keepAliveTimeout && server.keepAliveTimeout >= 1000 && ctx.header.connection !== 'close') {
23+
if (shouldPatchKeepAliveHeader && server && server.keepAliveTimeout && server.keepAliveTimeout >= 1000 && ctx.header.connection !== 'close') {
1924
/**
2025
* use Math.floor instead of parseInt. More: https://github.com/eggjs/egg/pull/2702
2126
*/

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"ms": "^2.1.1",
5050
"mz": "^2.7.0",
5151
"on-finished": "^2.3.0",
52+
"semver": "^7.3.2",
5253
"sendmessage": "^1.1.0",
5354
"urllib": "^2.33.0",
5455
"utility": "^1.15.0",

0 commit comments

Comments
 (0)