From 4682ed230d67bb1b23a69e60bbf6015f2135a0c4 Mon Sep 17 00:00:00 2001 From: TZ Date: Thu, 28 Apr 2022 14:53:25 +0800 Subject: [PATCH] feat: eggScriptsConfig support node-options --- README.md | 8 +++--- lib/command.js | 14 +++++++++- test/fixtures/egg-scripts-node-options/app.js | 6 +++++ .../config/config.default.js | 3 +++ .../egg-scripts-node-options/package.json | 11 ++++++++ test/start.test.js | 26 +++++++++++++++++++ 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/egg-scripts-node-options/app.js create mode 100644 test/fixtures/egg-scripts-node-options/config/config.default.js create mode 100644 test/fixtures/egg-scripts-node-options/package.json diff --git a/README.md b/README.md index 9bf64a5..31d7c29 100644 --- a/README.md +++ b/README.md @@ -79,11 +79,13 @@ $ eggctl stop [options] In addition to the command line specification, options can also be specified in `package.json`. However, the command line designation takes precedence. -```json +```js { "eggScriptsConfig": { "port": 1234, - "ignore-stderr": true + "ignore-stderr": true, + // will pass as `node --max-http-header-size=20000` + "node-options--max-http-header-size": "20000" } } ``` @@ -108,4 +110,4 @@ Please open an issue [here](https://github.com/eggjs/egg/issues?q=is%3Aissue+is% This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Tue Mar 08 2022 09:52:13 GMT+0800`. - \ No newline at end of file + diff --git a/lib/command.js b/lib/command.js index 589b75e..7944ae3 100644 --- a/lib/command.js +++ b/lib/command.js @@ -73,7 +73,19 @@ class Command extends BaseCommand { // read argv from eggScriptsConfig in package.json if (eggScriptsConfig && typeof eggScriptsConfig === 'object') { for (const key in pkgInfo.eggScriptsConfig) { - if (argv[key] == null) argv[key] = pkgInfo.eggScriptsConfig[key]; + const v = pkgInfo.eggScriptsConfig[key]; + // like https://github.com/node-modules/common-bin/blob/master/lib/helper.js#L180 + if (key.startsWith('node-options--')) { + const newKey = key.replace('node-options--', ''); + if (execArgvObj[newKey] == null) { + execArgvObj[newKey] = v; + } + } else { + if (argv[key] == null) { + // only set if key is not pass from command line + argv[key] = v; + } + } } } diff --git a/test/fixtures/egg-scripts-node-options/app.js b/test/fixtures/egg-scripts-node-options/app.js new file mode 100644 index 0000000..3fa4a19 --- /dev/null +++ b/test/fixtures/egg-scripts-node-options/app.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = () => { + console.log('process.execArgv:', process.execArgv); + console.log('maxHeaderSize:', require('http').maxHeaderSize); +}; diff --git a/test/fixtures/egg-scripts-node-options/config/config.default.js b/test/fixtures/egg-scripts-node-options/config/config.default.js new file mode 100644 index 0000000..c997e00 --- /dev/null +++ b/test/fixtures/egg-scripts-node-options/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.keys = '123456'; diff --git a/test/fixtures/egg-scripts-node-options/package.json b/test/fixtures/egg-scripts-node-options/package.json new file mode 100644 index 0000000..3541f16 --- /dev/null +++ b/test/fixtures/egg-scripts-node-options/package.json @@ -0,0 +1,11 @@ +{ + "name": "example", + "version": "1.0.0", + "dependencies": { + "egg": "^1.0.0" + }, + "eggScriptsConfig": { + "workers": 1, + "node-options--max-http-header-size": "20000" + } +} diff --git a/test/start.test.js b/test/start.test.js index ca22a4b..e0aa759 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -548,6 +548,32 @@ describe('test/start.test.js', () => { }); }); + describe('read eggScriptsConfig', () => { + let app; + let fixturePath; + + before(function* () { + fixturePath = path.join(__dirname, 'fixtures/egg-scripts-node-options'); + yield utils.cleanup(fixturePath); + }); + + after(function* () { + app.proc.kill('SIGTERM'); + yield utils.cleanup(fixturePath); + }); + + it('should start', function* () { + app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]); + app.debug(); + app.expect('code', 0); + + yield sleep(waitTime); + + assert(app.stderr === ''); + assert(app.stdout.match(/maxHeaderSize: 20000/)); + }); + }); + describe('subDir as baseDir', () => { let app; const rootDir = path.join(__dirname, '..');