Skip to content

Commit

Permalink
👌 IMPROVE: Don't need to patch keep-alive header on Node.js >= 14.20.0 (
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 30, 2022
1 parent 6ac4cdb commit d820b73
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 24 deletions.
16 changes: 0 additions & 16 deletions app/middleware/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
* meta middleware, should be the first middleware
*/

'use strict';

const { performance } = require('perf_hooks');
const semver = require('semver');

module.exports = options => {
// Node.js >=14.8.0 and >= 12.19.0 will set Keep-Alive Header, see https://github.com/nodejs/node/pull/34561
const shouldPatchKeepAliveHeader = !semver.satisfies(process.version, '>=14.8.0 || ^12.19.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 @@ -22,15 +16,5 @@ module.exports = options => {
} else {
ctx.set('x-readtime', Date.now() - ctx.starttime);
}

// try to support Keep-Alive Header when < 14.8.0
const server = ctx.app.server;
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
*/
const timeout = Math.floor(server.keepAliveTimeout / 1000);
ctx.set('keep-alive', `timeout=${timeout}`);
}
};
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"ms": "^2.1.3",
"mz": "^2.7.0",
"on-finished": "^2.4.1",
"semver": "^7.3.7",
"sendmessage": "^1.1.0",
"urllib": "^2.33.0",
"urllib-next": "^3.1.3",
Expand Down
2 changes: 1 addition & 1 deletion site/docs/core/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Reusable package brings a few pros in:

## Deploy

Node.js(`>= 8.0.0`) is required so that you should make sure it is pre-installed in runtime environment.
Node.js(`>= 14.20.0`) is required so that you should make sure it is pre-installed in runtime environment.

Egg takes `egg-cluster` to create [Master](https://github.com/eggjs/egg/blob/master/docs/source/en/core/cluster-and-ipc.md#master) process, which you can rely on to secure the application instead of daemon manager like [pm2]. The API is also really convenient for developers to achieve that, just `egg.startCluster`.

Expand Down
2 changes: 1 addition & 1 deletion site/docs/core/deployment.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $ tar -zcvf ../release.tgz .

## 部署

服务器需要预装 Node.js,框架支持的 Node 版本为 `>= 8.0.0`
服务器需要预装 Node.js,框架支持的 Node 版本为 `>= 14.20.0`

框架内置了 [egg-cluster] 来启动 [Master 进程](./cluster-and-ipc.md#master),Master 有足够的稳定性,不再需要使用 [pm2] 等进程守护模块。

Expand Down
1 change: 0 additions & 1 deletion site/docs/intro/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ In case the previous versions still requires maintenance:

[co]: https://github.com/tj/co
[egg-schedule]: https://github.com/eggjs/egg-schedule
[egg-view]: https://github.com/eggjs/egg-view
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
[app.toasyncfunction]: https://github.com/eggjs/egg-core/blob/da4ba1784175c43217125f3d5cd7f0be3d5396bf/lib/egg.js#L344
[app.topromise]: https://github.com/eggjs/egg-core/blob/da4ba1784175c43217125f3d5cd7f0be3d5396bf/lib/egg.js#L353
1 change: 0 additions & 1 deletion site/docs/intro/migration.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ task = app.toAsyncFunction(schedule.task);

[co]: https://github.com/tj/co
[egg-schedule]: https://github.com/eggjs/egg-schedule
[egg-view]: https://github.com/eggjs/egg-view
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
[app.toasyncfunction]: https://github.com/eggjs/egg-core/blob/da4ba1784175c43217125f3d5cd7f0be3d5396bf/lib/egg.js#L344
[app.topromise]: https://github.com/eggjs/egg-core/blob/da4ba1784175c43217125f3d5cd7f0be3d5396bf/lib/egg.js#L353
4 changes: 1 addition & 3 deletions test/app/middleware/meta.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const assert = require('assert');
const mm = require('egg-mock');
const fs = require('mz/fs');
const fs = require('fs/promises');
const utils = require('../../utils');

describe('test/app/middleware/meta.test.js', () => {
Expand Down

0 comments on commit d820b73

Please sign in to comment.