Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: CI for 5.x

on:
push:
Expand All @@ -13,3 +13,5 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
version: '14.19.0, 14, 16, 18, 20'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
12 changes: 12 additions & 0 deletions .github/workflows/release-5.x.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Release for 5.x
on:
push:
branches: [ 5.x ]

jobs:
release:
name: Node.js
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ test/fixtures/egg/node_modules/egg-core
package-lock.json
run
test/fixtures/*/timing.json
.tshy*
.package-lock.json
19 changes: 5 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# egg-core

[![NPM version][npm-image]][npm-url]
[![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)
[![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)
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
Expand All @@ -17,7 +17,7 @@

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

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

## Usage

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

EggCore record boot progress with `Timing`, include:

- Process start time
- 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`)
- Application start time
Expand Down Expand Up @@ -274,18 +275,8 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).

[MIT](LICENSE)

[egg]: https://github.com/eggjs/egg
<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<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/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<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/>|
|[<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/>|
|[<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/>|
[<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/>

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`.
[![Contributors](https://contrib.rocks/image?repo=eggjs/core)](https://github.com/eggjs/core/graphs/contributors)

<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
17 changes: 16 additions & 1 deletion lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ class EggCore extends KoaApplication {
* @param {String} [options.baseDir=process.cwd()] - the directory of application
* @param {String} [options.type=application|agent] - whether it's running in app worker or agent worker
* @param {Object} [options.plugins] - custom plugins
* @param {Object | String} [options.pathToRegexpModule] - custom `path-to-regexp` module, default is `path-to-regexp@1`
* @since 1.0.0
*/
constructor(options = {}) {
options.baseDir = options.baseDir || process.cwd();
options.type = options.type || 'application';
if (typeof options.pathToRegexpModule === 'string') {
/**
* Usage:
* ```js
* const app = new Application({
* pathToRegexpModule: '/path/to/path-to-regexp-v8',
* });
* ```
*/
options.pathToRegexpModule = require(options.pathToRegexpModule);
}

assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');
assert(fs.existsSync(options.baseDir), `Directory ${options.baseDir} not exists`);
Expand Down Expand Up @@ -301,7 +313,10 @@ class EggCore extends KoaApplication {
if (this[ROUTER]) {
return this[ROUTER];
}
const router = this[ROUTER] = new Router({ sensitive: true }, this);
const router = this[ROUTER] = new Router({
sensitive: true,
pathToRegexpModule: this.options.pathToRegexpModule,
}, this);
// register router middleware
this.beforeStart(() => {
this.use(router.middleware());
Expand Down
11 changes: 8 additions & 3 deletions lib/loader/mixin/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ module.exports = {
assert(is.function(mw), `Middleware ${name} must be a function, but actual is ${inspect(mw)}`);
mw._name = name;
// middlewares support options.enable, options.ignore and options.match
mw = wrapMiddleware(mw, options);
mw = wrapMiddleware(mw, {
...options,
pathToRegexpModule: app.options.pathToRegexpModule,
});
if (mw) {
if (debug.enabled) {
// show mw debug log on every request
Expand Down Expand Up @@ -104,9 +107,11 @@ function wrapMiddleware(mw, options) {
mw = utils.middleware(mw);

// support options.match and options.ignore
if (!options.match && !options.ignore) return mw;
const match = pathMatching(options);
if (!options.match && !options.ignore) {
return mw;
}

const match = pathMatching(options);
const fn = (ctx, next) => {
if (!match(ctx)) return next();
return mw(ctx, next);
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
"scripts": {
"lint": "eslint .",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test -p",
"test-local": "egg-bin test -p --full-trace",
"test-single": "egg-bin test",
"cov": "egg-bin cov -p",
"ci": "npm run lint && npm run cov",
"contributor": "git-contributor"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eggjs/egg-core.git"
"url": "git+https://github.com/eggjs/core.git"
},
"keywords": [
"egg",
Expand All @@ -34,7 +34,7 @@
"bugs": {
"url": "https://github.com/eggjs/egg/issues"
},
"homepage": "https://github.com/eggjs/egg-core#readme",
"homepage": "https://github.com/eggjs/core#readme",
"engines": {
"node": ">= 14.19.0"
},
Expand All @@ -53,17 +53,18 @@
"supertest": "^4.0.2",
"ts-node": "^10.9.1",
"typescript": "^4.9.4",
"urllib": "^3.10.0"
"urllib": "^3.10.0",
"path-to-regexp-v8": "npm:path-to-regexp@8"
},
"dependencies": {
"@eggjs/router": "^2.0.0",
"@eggjs/router": "^2.2.0",
"@types/depd": "^1.1.32",
"@types/koa": "^2.13.5",
"co": "^4.6.0",
"debug": "^4.1.1",
"depd": "^2.0.0",
"egg-logger": "^3.1.0",
"egg-path-matching": "^1.0.1",
"egg-path-matching": "^1.2.0",
"extend2": "^1.0.0",
"gals": "^1.0.1",
"get-ready": "^2.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = app => {
return class AsyncController extends app.Controller {
async index() {
this.ctx.body.push('async');
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

// 测试 app.resources 遇到 controller 没有足够的 action 的场景

exports.index = function* () {
this.body = 'index';
};

exports.new = function* () {
this.body = 'new';
};

exports.show = function* () {
this.body = 'show - ' + this.params.id;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.router = function* () {
yield this.render('locals/router.html');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

// 测试 app.resources 遇到 controller 没有足够的 action 的场景

exports.index = function* () {
this.body = 'index';
};

exports.new = function* () {
this.body = 'new';
};

exports.show = function* () {
this.body = 'show - ' + this.params.id;
};

exports.delete = function* () {
this.body = `delete - ${this.params.id}`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function* () {
this.body = [];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.get = function* () {
this.body = this.params[0];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

exports.index = function* () {
this.body = 'index';
};

exports.new = function* () {
this.body = 'new';
};

exports.create = function* () {
this.body = 'create';
};

exports.show = function* () {
this.body = 'show - ' + this.params.id;
};

exports.edit = function* () {
this.body = 'edit - ' + this.params.id;
};

exports.update = function* () {
this.body = 'update - ' + this.params.id;
};

exports.destroy = function* () {
this.body = 'destroy - ' + this.params.id;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = function() {
return async (ctx, next) => {
await next();
ctx.body.push('async');
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function() {
return function(ctx, next) {
return next().then(() => {
ctx.body.push('common');
});
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = function() {
return function*(next) {
yield next;
this.body.push('generator');
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = function() {
return function*(next) {
this.body = [];
this.body.push('generator before');
yield next;
this.body.push('generator after');
};
};
34 changes: 34 additions & 0 deletions test/fixtures/router-app-with-pathToRegexpModule/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = function (app) {
const common = app.middlewares.common();
const asyncMw = app.middlewares.async();
const generator = app.middlewares.generator();
const generatorBoth = app.middlewares.generatorBoth();

app
.get('/locals/router', app.controller.locals.router)
.get('/members/index', 'members.index')
.delete('/members/delete/:id', 'members.delete')
.del('/members/del/:id', 'members.delete')
.resources('posts', '/posts', 'posts')
.resources('members', '/members', app.controller.members)
.resources('/comments', app.controller.comments)
// not work on path-to-regexp@8
// .get('comment_index', '/comments/:id?filter=', app.controller.comments.index)
.get('params', '/params/:a/:b', app.controller.locals.router)
.get('/middleware', common, asyncMw, generator, 'middleware')
.get('middleware', '/named_middleware', common, asyncMw, generator, 'middleware')
.get('/mix', generatorBoth , 'async.index')
.register('/comments', [ 'post' ] , app.controller.comments.new)
.register('/register_middleware', [ 'get' ], [ common, asyncMw, generator, 'middleware' ])
.redirect('/redirect', '/middleware', 302);

app.router
.get('/router_middleware', common, asyncMw, generator, 'middleware')
.redirect('/router_redirect', '/middleware');

// not work on path-to-regexp@8
// app.get('packages', /^\/packages\/(.*)/, 'package.get');

app.get([ '/url1', '/url2' ], 'members.index')
app.get([ '/urlm1', '/urlm2' ], common, asyncMw, generator, 'middleware')
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
posts: /posts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

exports.keys = 'my';

exports.security = {
csrf: {
enable: false,
},
};
3 changes: 3 additions & 0 deletions test/fixtures/router-app-with-pathToRegexpModule/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "router-app-with-pathToRegexpModule"
}
Loading
Loading