Comparing version 1.1.0 to 1.2.0
1.2.0 / 2016-11-21 | ||
================== | ||
* refactor: don't use core middleware when enable = false (#32) | ||
* feat: core middlewares support enable/match/ignore options (#31) | ||
1.1.0 / 2016-11-09 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -7,2 +7,3 @@ 'use strict'; | ||
const inspect = require('util').inspect; | ||
const pathMatching = require('egg-path-matching'); | ||
@@ -48,2 +49,3 @@ module.exports = { | ||
debug('middlewareNames: %j', middlewareNames); | ||
const middlewaresMap = new Map(); | ||
for (const name of middlewareNames) { | ||
@@ -53,2 +55,6 @@ if (!app.middlewares[name]) { | ||
} | ||
if (middlewaresMap.has(name)) { | ||
throw new TypeError(`Middleware ${name} redefined`); | ||
} | ||
middlewaresMap.set(name, true); | ||
@@ -62,5 +68,15 @@ const options = this.config[name] || {}; | ||
mw._name = name; | ||
app.use(mw); | ||
debug('Use middleware: %s with options: %j', name, options); | ||
this.options.logger.info('[egg:loader] Use middleware: %s', name); | ||
// core middleware support options.enable, options.ignore and options.match | ||
const isCoreMiddleware = this.config.coreMiddleware.indexOf(name) >= 0; | ||
if (isCoreMiddleware) { | ||
if (options.enable !== false) { | ||
app.use(wrapCoreMiddleware(mw, options)); | ||
debug('Use core middleware: %s with options: %j', name, options); | ||
} | ||
} else { | ||
app.use(mw); | ||
debug('Use app middleware: %s with options: %j', name, options); | ||
} | ||
this.options.logger.info('[egg:loader] Use %s middleware: %s', isCoreMiddleware ? 'core' : 'app', name); | ||
} | ||
@@ -72,1 +88,11 @@ | ||
}; | ||
function wrapCoreMiddleware(mw, options) { | ||
const match = pathMatching(options); | ||
const fn = function* (next) { | ||
if (!match(this)) return yield next; | ||
yield mw.call(this, next); | ||
}; | ||
fn._name = mw._name + 'coreMiddlewareWrapper'; | ||
return fn; | ||
} |
{ | ||
"name": "egg-core", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A core Pluggable framework based on koa", | ||
@@ -52,2 +52,3 @@ "main": "index.js", | ||
"egg-logger": "^1.4.0", | ||
"egg-path-matching": "^1.0.0", | ||
"extend": "^3.0.0", | ||
@@ -54,0 +55,0 @@ "globby": "^6.1.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60726
1633
15
+ Addedegg-path-matching@^1.0.0
+ Addedegg-path-matching@1.1.0(transitive)