Skip to content

Commit

Permalink
fix: support EGG_OPTIONS_PATH_TO_REGEXP_MODULE env (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jan 22, 2025
1 parent 7a255e8 commit f253ea2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,23 @@ class EggCore extends KoaApplication {
constructor(options = {}) {
options.baseDir = options.baseDir || process.cwd();
options.type = options.type || 'application';
if (typeof options.pathToRegexpModule === 'string') {
const pathToRegexpModule = options.pathToRegexpModule || process.env.EGG_OPTIONS_PATH_TO_REGEXP_MODULE;
if (typeof 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);
options.pathToRegexpModule = require(pathToRegexpModule);
}

assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');
Expand Down
6 changes: 5 additions & 1 deletion test/utils/router-with-pathToRegexpModule.test.js
Original file line number Diff line number Diff line change
@@ -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', {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f253ea2

Please sign in to comment.