Skip to content

Commit

Permalink
fix: only set keep-alive header before Node.js 14.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Sep 7, 2020
1 parent a7ae46c commit bd5cb2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/middleware/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

'use strict';

const semver = require('semver');

module.exports = options => {
// Node.js >=14.8.0 will set Keep-Alive Header, see https://github.com/nodejs/node/pull/34561
const shouldPatchKeepAliveHeader = semver.lt(process.version, '14.8.0');

return async function meta(ctx, next) {
if (options.logging) {
ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s', ctx.host, ctx.header['user-agent']);
Expand All @@ -13,9 +18,9 @@ module.exports = options => {
// total response time header
ctx.set('x-readtime', Date.now() - ctx.starttime);

// try to support Keep-Alive Header
// try to support Keep-Alive Header when < 14.8.0
const server = ctx.app.server;
if (server && server.keepAliveTimeout && server.keepAliveTimeout >= 1000 && ctx.header.connection !== 'close') {
if (shouldPatchKeepAliveHeader && server && server.keepAliveTimeout && server.keepAliveTimeout >= 1000 && ctx.header.connection !== 'close') {
/**
* use Math.floor instead of parseInt. More: https://github.com/eggjs/egg/pull/2702
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ms": "^2.1.1",
"mz": "^2.7.0",
"on-finished": "^2.3.0",
"semver": "^7.3.2",
"sendmessage": "^1.1.0",
"urllib": "^2.33.0",
"utility": "^1.15.0",
Expand Down

0 comments on commit bd5cb2d

Please sign in to comment.