Skip to content

Commit 37ebd24

Browse files
committed
fix: only set keep-alive header before Node.js 14.8.0
1 parent a7ae46c commit 37ebd24

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

app/middleware/meta.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
'use strict';
66

7+
const semver = require('semver');
8+
79
module.exports = options => {
810
return async function meta(ctx, next) {
911
if (options.logging) {
@@ -13,9 +15,12 @@ module.exports = options => {
1315
// total response time header
1416
ctx.set('x-readtime', Date.now() - ctx.starttime);
1517

16-
// try to support Keep-Alive Header
18+
// Node.js >=14.8.0 will set Keep-Alive Header, see https://github.com/nodejs/node/pull/34561
19+
const shouldPatchKeepAliveHeader = semver.lt(process.version, '14.8.0');
20+
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

Lines changed: 1 addition & 0 deletions
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)