Skip to content

Commit 72ba3a8

Browse files
authored
feat: support custom pathToRegexpModule on app.options (#290)
deps on - eggjs/router#18 - eggjs/egg-path-matching#11
1 parent 338a7c6 commit 72ba3a8

23 files changed

+568
-25
lines changed

.github/workflows/nodejs.yml .github/workflows/nodejs-5.x.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI
1+
name: CI for 5.x
22

33
on:
44
push:
@@ -13,3 +13,5 @@ jobs:
1313
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1414
with:
1515
version: '14.19.0, 14, 16, 18, 20'
16+
secrets:
17+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/release-5.x.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Release for 5.x
2+
on:
3+
push:
4+
branches: [ 5.x ]
5+
6+
jobs:
7+
release:
8+
name: Node.js
9+
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
10+
secrets:
11+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
12+
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ test/fixtures/egg/node_modules/egg-core
1111
package-lock.json
1212
run
1313
test/fixtures/*/timing.json
14+
.tshy*
15+
.package-lock.json

README.md

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# egg-core
22

33
[![NPM version][npm-image]][npm-url]
4-
[![Node.js CI](https://github.com/eggjs/egg-core/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-core/actions/workflows/nodejs.yml)
4+
[![Node.js CI](https://github.com/eggjs/egg-core/actions/workflows/nodejs-5.x.yml/badge.svg)](https://github.com/eggjs/egg-core/actions/workflows/nodejs-5.x.yml)
55
[![Test coverage][codecov-image]][codecov-url]
66
[![Known Vulnerabilities][snyk-image]][snyk-url]
77
[![npm download][download-image]][download-url]
@@ -17,7 +17,7 @@
1717

1818
A core Pluggable framework based on [koa](https://github.com/koajs/koa).
1919

20-
**Don't use it directly, see [egg].**
20+
**Don't use it directly, see [egg](https://github.com/eggjs/egg).**
2121

2222
## Usage
2323

@@ -236,6 +236,7 @@ filter | `Function` | a function that filter the exports which can b
236236
## Timing
237237

238238
EggCore record boot progress with `Timing`, include:
239+
239240
- Process start time
240241
- Script start time(node don't implement an interface like `process.uptime` to record the script start running time, framework can implement a prestart file used with node `--require` options to set `process.scriptTime`)
241242
- Application start time
@@ -274,18 +275,8 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
274275

275276
[MIT](LICENSE)
276277

277-
[egg]: https://github.com/eggjs/egg
278-
<!-- GITCONTRIBUTOR_START -->
279-
280278
## Contributors
281279

282-
|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/1207064?v=4" width="100px;"/><br/><sub><b>gxcsoccer</b></sub>](https://github.com/gxcsoccer)<br/>|
283-
| :---: | :---: | :---: | :---: | :---: | :---: |
284-
|[<img src="https://avatars.githubusercontent.com/u/6897780?v=4" width="100px;"/><br/><sub><b>killagu</b></sub>](https://github.com/killagu)<br/>|[<img src="https://avatars.githubusercontent.com/u/2170848?v=4" width="100px;"/><br/><sub><b>iyuq</b></sub>](https://github.com/iyuq)<br/>|[<img src="https://avatars.githubusercontent.com/u/5243774?v=4" width="100px;"/><br/><sub><b>ngot</b></sub>](https://github.com/ngot)<br/>|[<img src="https://avatars.githubusercontent.com/u/17722900?v=4" width="100px;"/><br/><sub><b>initial-wu</b></sub>](https://github.com/initial-wu)<br/>|[<img src="https://avatars.githubusercontent.com/u/1763067?v=4" width="100px;"/><br/><sub><b>waitingsong</b></sub>](https://github.com/waitingsong)<br/>|[<img src="https://avatars.githubusercontent.com/u/7315743?v=4" width="100px;"/><br/><sub><b>AnzerWall</b></sub>](https://github.com/AnzerWall)<br/>|
285-
|[<img src="https://avatars.githubusercontent.com/u/174904?v=4" width="100px;"/><br/><sub><b>army8735</b></sub>](https://github.com/army8735)<br/>|[<img src="https://avatars.githubusercontent.com/u/5938871?v=4" width="100px;"/><br/><sub><b>njugray</b></sub>](https://github.com/njugray)<br/>|[<img src="https://avatars.githubusercontent.com/u/327019?v=4" width="100px;"/><br/><sub><b>JacksonTian</b></sub>](https://github.com/JacksonTian)<br/>|[<img src="https://avatars.githubusercontent.com/u/16460813?v=4" width="100px;"/><br/><sub><b>JimmyDaddy</b></sub>](https://github.com/JimmyDaddy)<br/>|[<img src="https://avatars.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|[<img src="https://avatars.githubusercontent.com/u/6913898?v=4" width="100px;"/><br/><sub><b>monkindey</b></sub>](https://github.com/monkindey)<br/>|
286-
|[<img src="https://avatars.githubusercontent.com/u/1148428?v=4" width="100px;"/><br/><sub><b>mattma</b></sub>](https://github.com/mattma)<br/>|[<img src="https://avatars.githubusercontent.com/u/456108?v=4" width="100px;"/><br/><sub><b>shaoshuai0102</b></sub>](https://github.com/shaoshuai0102)<br/>|[<img src="https://avatars.githubusercontent.com/u/7530656?v=4" width="100px;"/><br/><sub><b>zhang740</b></sub>](https://github.com/zhang740)<br/>|[<img src="https://avatars.githubusercontent.com/u/457552?v=4" width="100px;"/><br/><sub><b>dsonet</b></sub>](https://github.com/dsonet)<br/>|[<img src="https://avatars.githubusercontent.com/u/3995814?v=4" width="100px;"/><br/><sub><b>chenbin92</b></sub>](https://github.com/chenbin92)<br/>|[<img src="https://avatars.githubusercontent.com/u/19908330?v=4" width="100px;"/><br/><sub><b>hyj1991</b></sub>](https://github.com/hyj1991)<br/>|
287-
[<img src="https://avatars.githubusercontent.com/u/8816730?v=4" width="100px;"/><br/><sub><b>maxming2333</b></sub>](https://github.com/maxming2333)<br/>|[<img src="https://avatars.githubusercontent.com/u/26317926?v=4" width="100px;"/><br/><sub><b>supperchong</b></sub>](https://github.com/supperchong)<br/>|[<img src="https://avatars.githubusercontent.com/u/18463189?v=4" width="100px;"/><br/><sub><b>ZhangDianPeng</b></sub>](https://github.com/ZhangDianPeng)<br/>|[<img src="https://avatars.githubusercontent.com/u/15242708?v=4" width="100px;"/><br/><sub><b>mosaic101</b></sub>](https://github.com/mosaic101)<br/>
288-
289-
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed Nov 24 2021 22:24:39 GMT+0800`.
280+
[![Contributors](https://contrib.rocks/image?repo=eggjs/core)](https://github.com/eggjs/core/graphs/contributors)
290281

291-
<!-- GITCONTRIBUTOR_END -->
282+
Made with [contributors-img](https://contrib.rocks).

lib/egg.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,23 @@ class EggCore extends KoaApplication {
2525
* @param {String} [options.baseDir=process.cwd()] - the directory of application
2626
* @param {String} [options.type=application|agent] - whether it's running in app worker or agent worker
2727
* @param {Object} [options.plugins] - custom plugins
28+
* @param {Object | String} [options.pathToRegexpModule] - custom `path-to-regexp` module, default is `path-to-regexp@1`
2829
* @since 1.0.0
2930
*/
3031
constructor(options = {}) {
3132
options.baseDir = options.baseDir || process.cwd();
3233
options.type = options.type || 'application';
34+
if (typeof options.pathToRegexpModule === 'string') {
35+
/**
36+
* Usage:
37+
* ```js
38+
* const app = new Application({
39+
* pathToRegexpModule: '/path/to/path-to-regexp-v8',
40+
* });
41+
* ```
42+
*/
43+
options.pathToRegexpModule = require(options.pathToRegexpModule);
44+
}
3345

3446
assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');
3547
assert(fs.existsSync(options.baseDir), `Directory ${options.baseDir} not exists`);
@@ -301,7 +313,10 @@ class EggCore extends KoaApplication {
301313
if (this[ROUTER]) {
302314
return this[ROUTER];
303315
}
304-
const router = this[ROUTER] = new Router({ sensitive: true }, this);
316+
const router = this[ROUTER] = new Router({
317+
sensitive: true,
318+
pathToRegexpModule: this.options.pathToRegexpModule,
319+
}, this);
305320
// register router middleware
306321
this.beforeStart(() => {
307322
this.use(router.middleware());

lib/loader/mixin/middleware.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ module.exports = {
7676
assert(is.function(mw), `Middleware ${name} must be a function, but actual is ${inspect(mw)}`);
7777
mw._name = name;
7878
// middlewares support options.enable, options.ignore and options.match
79-
mw = wrapMiddleware(mw, options);
79+
mw = wrapMiddleware(mw, {
80+
...options,
81+
pathToRegexpModule: app.options.pathToRegexpModule,
82+
});
8083
if (mw) {
8184
if (debug.enabled) {
8285
// show mw debug log on every request
@@ -104,9 +107,11 @@ function wrapMiddleware(mw, options) {
104107
mw = utils.middleware(mw);
105108

106109
// support options.match and options.ignore
107-
if (!options.match && !options.ignore) return mw;
108-
const match = pathMatching(options);
110+
if (!options.match && !options.ignore) {
111+
return mw;
112+
}
109113

114+
const match = pathMatching(options);
110115
const fn = (ctx, next) => {
111116
if (!match(ctx)) return next();
112117
return mw(ctx, next);

package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
"scripts": {
1616
"lint": "eslint .",
1717
"test": "npm run lint -- --fix && npm run test-local",
18-
"test-local": "egg-bin test -p",
18+
"test-local": "egg-bin test -p --full-trace",
1919
"test-single": "egg-bin test",
2020
"cov": "egg-bin cov -p",
2121
"ci": "npm run lint && npm run cov",
2222
"contributor": "git-contributor"
2323
},
2424
"repository": {
2525
"type": "git",
26-
"url": "git+https://github.com/eggjs/egg-core.git"
26+
"url": "git+https://github.com/eggjs/core.git"
2727
},
2828
"keywords": [
2929
"egg",
@@ -34,7 +34,7 @@
3434
"bugs": {
3535
"url": "https://github.com/eggjs/egg/issues"
3636
},
37-
"homepage": "https://github.com/eggjs/egg-core#readme",
37+
"homepage": "https://github.com/eggjs/core#readme",
3838
"engines": {
3939
"node": ">= 14.19.0"
4040
},
@@ -53,17 +53,18 @@
5353
"supertest": "^4.0.2",
5454
"ts-node": "^10.9.1",
5555
"typescript": "^4.9.4",
56-
"urllib": "^3.10.0"
56+
"urllib": "^3.10.0",
57+
"path-to-regexp-v8": "npm:path-to-regexp@8"
5758
},
5859
"dependencies": {
59-
"@eggjs/router": "^2.0.0",
60+
"@eggjs/router": "^2.2.0",
6061
"@types/depd": "^1.1.32",
6162
"@types/koa": "^2.13.5",
6263
"co": "^4.6.0",
6364
"debug": "^4.1.1",
6465
"depd": "^2.0.0",
6566
"egg-logger": "^3.1.0",
66-
"egg-path-matching": "^1.0.1",
67+
"egg-path-matching": "^1.2.0",
6768
"extend2": "^1.0.0",
6869
"gals": "^1.0.1",
6970
"get-ready": "^2.0.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
return class AsyncController extends app.Controller {
5+
async index() {
6+
this.ctx.body.push('async');
7+
}
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
// 测试 app.resources 遇到 controller 没有足够的 action 的场景
4+
5+
exports.index = function* () {
6+
this.body = 'index';
7+
};
8+
9+
exports.new = function* () {
10+
this.body = 'new';
11+
};
12+
13+
exports.show = function* () {
14+
this.body = 'show - ' + this.params.id;
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
exports.router = function* () {
4+
yield this.render('locals/router.html');
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
// 测试 app.resources 遇到 controller 没有足够的 action 的场景
4+
5+
exports.index = function* () {
6+
this.body = 'index';
7+
};
8+
9+
exports.new = function* () {
10+
this.body = 'new';
11+
};
12+
13+
exports.show = function* () {
14+
this.body = 'show - ' + this.params.id;
15+
};
16+
17+
exports.delete = function* () {
18+
this.body = `delete - ${this.params.id}`;
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = function* () {
4+
this.body = [];
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
exports.get = function* () {
4+
this.body = this.params[0];
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
exports.index = function* () {
4+
this.body = 'index';
5+
};
6+
7+
exports.new = function* () {
8+
this.body = 'new';
9+
};
10+
11+
exports.create = function* () {
12+
this.body = 'create';
13+
};
14+
15+
exports.show = function* () {
16+
this.body = 'show - ' + this.params.id;
17+
};
18+
19+
exports.edit = function* () {
20+
this.body = 'edit - ' + this.params.id;
21+
};
22+
23+
exports.update = function* () {
24+
this.body = 'update - ' + this.params.id;
25+
};
26+
27+
exports.destroy = function* () {
28+
this.body = 'destroy - ' + this.params.id;
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = function() {
4+
return async (ctx, next) => {
5+
await next();
6+
ctx.body.push('async');
7+
};
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = function() {
4+
return function(ctx, next) {
5+
return next().then(() => {
6+
ctx.body.push('common');
7+
});
8+
};
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = function() {
4+
return function*(next) {
5+
yield next;
6+
this.body.push('generator');
7+
};
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = function() {
4+
return function*(next) {
5+
this.body = [];
6+
this.body.push('generator before');
7+
yield next;
8+
this.body.push('generator after');
9+
};
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = function (app) {
2+
const common = app.middlewares.common();
3+
const asyncMw = app.middlewares.async();
4+
const generator = app.middlewares.generator();
5+
const generatorBoth = app.middlewares.generatorBoth();
6+
7+
app
8+
.get('/locals/router', app.controller.locals.router)
9+
.get('/members/index', 'members.index')
10+
.delete('/members/delete/:id', 'members.delete')
11+
.del('/members/del/:id', 'members.delete')
12+
.resources('posts', '/posts', 'posts')
13+
.resources('members', '/members', app.controller.members)
14+
.resources('/comments', app.controller.comments)
15+
// not work on path-to-regexp@8
16+
// .get('comment_index', '/comments/:id?filter=', app.controller.comments.index)
17+
.get('params', '/params/:a/:b', app.controller.locals.router)
18+
.get('/middleware', common, asyncMw, generator, 'middleware')
19+
.get('middleware', '/named_middleware', common, asyncMw, generator, 'middleware')
20+
.get('/mix', generatorBoth , 'async.index')
21+
.register('/comments', [ 'post' ] , app.controller.comments.new)
22+
.register('/register_middleware', [ 'get' ], [ common, asyncMw, generator, 'middleware' ])
23+
.redirect('/redirect', '/middleware', 302);
24+
25+
app.router
26+
.get('/router_middleware', common, asyncMw, generator, 'middleware')
27+
.redirect('/router_redirect', '/middleware');
28+
29+
// not work on path-to-regexp@8
30+
// app.get('packages', /^\/packages\/(.*)/, 'package.get');
31+
32+
app.get([ '/url1', '/url2' ], 'members.index')
33+
app.get([ '/urlm1', '/urlm2' ], common, asyncMw, generator, 'middleware')
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
posts: /posts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
exports.keys = 'my';
4+
5+
exports.security = {
6+
csrf: {
7+
enable: false,
8+
},
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "router-app-with-pathToRegexpModule"
3+
}

0 commit comments

Comments
 (0)