From 73dc426fb041ce5dd33da6921f2471e3d464d96c Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 23 Jan 2025 00:38:40 +0800 Subject: [PATCH 1/2] fix: support EGG_OPTIONS_PATH_TO_REGEXP_MODULE env --- lib/egg.js | 7 +++++++ test/utils/router-with-pathToRegexpModule.test.js | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/egg.js b/lib/egg.js index d0f5b52c..32b0ffd1 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -31,14 +31,21 @@ class EggCore extends KoaApplication { constructor(options = {}) { options.baseDir = options.baseDir || process.cwd(); options.type = options.type || 'application'; + options.pathToRegexpModule = options.pathToRegexpModule || process.env.EGG_OPTIONS_PATH_TO_REGEXP_MODULE; if (typeof options.pathToRegexpModule === 'string') { /** * Usage: + * * ```js * const app = new Application({ * pathToRegexpModule: '/path/to/path-to-regexp-v8', * }); * ``` + * + * Or you can use `EGG_OPTIONS_PATH_TO_REGEXP_MODULE` environment variable. + * ```bash + * EGG_OPTIONS_PATH_TO_REGEXP_MODULE=/path/to/path-to-regexp-v8 npm start + * ``` */ options.pathToRegexpModule = require(options.pathToRegexpModule); } diff --git a/test/utils/router-with-pathToRegexpModule.test.js b/test/utils/router-with-pathToRegexpModule.test.js index 79d302f1..b37f6ca4 100644 --- a/test/utils/router-with-pathToRegexpModule.test.js +++ b/test/utils/router-with-pathToRegexpModule.test.js @@ -1,8 +1,11 @@ const assert = require('assert'); const request = require('supertest'); +const mm = require('mm'); const utils = require('../utils'); describe('test/utils/router-with-pathToRegexpModule.test.js', () => { + afterEach(mm.restore); + let app; before(() => { app = utils.createApp('router-app-with-pathToRegexpModule', { @@ -329,8 +332,9 @@ describe('test/utils/router-with-pathToRegexpModule.test.js', () => { }); }); - describe('router middleware', () => { + describe('set by env: EGG_OPTIONS_PATH_TO_REGEXP_MODULE', () => { before(() => { + mm(process.env, 'EGG_OPTIONS_PATH_TO_REGEXP_MODULE', 'path-to-regexp-v8'); app = utils.createApp('router-in-app'); app.loader.loadAll(); return app.ready(); From 7c644effc86c4b21725e97ac5cf5de3a4c53206f Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 23 Jan 2025 00:45:48 +0800 Subject: [PATCH 2/2] f --- lib/egg.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/egg.js b/lib/egg.js index 32b0ffd1..93bfb33f 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -31,8 +31,8 @@ class EggCore extends KoaApplication { constructor(options = {}) { options.baseDir = options.baseDir || process.cwd(); options.type = options.type || 'application'; - options.pathToRegexpModule = options.pathToRegexpModule || process.env.EGG_OPTIONS_PATH_TO_REGEXP_MODULE; - if (typeof options.pathToRegexpModule === 'string') { + const pathToRegexpModule = options.pathToRegexpModule || process.env.EGG_OPTIONS_PATH_TO_REGEXP_MODULE; + if (typeof pathToRegexpModule === 'string') { /** * Usage: * @@ -47,7 +47,7 @@ class EggCore extends KoaApplication { * EGG_OPTIONS_PATH_TO_REGEXP_MODULE=/path/to/path-to-regexp-v8 npm start * ``` */ - options.pathToRegexpModule = require(options.pathToRegexpModule); + options.pathToRegexpModule = require(pathToRegexpModule); } assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');