chensenlai 10988628a0 语音房项目初始化 | 5 年 前 | |
---|---|---|
.. | ||
node_modules | 5 年 前 | |
.npmignore | 5 年 前 | |
.travis.yml | 5 年 前 | |
LICENSE | 5 年 前 | |
README.md | 5 年 前 | |
index.js | 5 年 前 | |
package.json | 5 年 前 | |
test.js | 5 年 前 |
Convert koa legacy ( 0.x & 1.x ) generator middleware to modern promise middleware ( 2.x ).
It could also convert modern promise middleware back to legacy generator middleware ( useful to help modern middleware support koa 0.x or 1.x ).
It should be able to convert any legacy generator middleware to modern promise middleware ( or convert it back ).
Please let me know ( send a issue ) if you fail to do so.
$ npm install koa-convert
const Koa = require('koa') // koa v2.x
const convert = require('koa-convert')
const app = new Koa()
app.use(modernMiddleware)
app.use(convert(legacyMiddleware))
app.use(convert.compose(legacyMiddleware, modernMiddleware))
function * legacyMiddleware (next) {
// before
yield next
// after
}
function modernMiddleware (ctx, next) {
// before
return next().then(() => {
// after
})
}
In koa 0.x and 1.x ( without experimental flag ), app.use
has an assertion that all ( legacy ) middleware must be generator function and it's tested with fn.constructor.name == 'GeneratorFunction'
at here.
Therefore, we can distinguish legacy and modern middleware with fn.constructor.name == 'GeneratorFunction'
.
app.use(legacyMiddleware)
is everywhere in 0.x and 1.x and it would be painful to manually change all of them to app.use(convert(legacyMiddleware))
.
You can use following snippet to make migration easier.
const _use = app.use
app.use = x => _use.call(app, convert(x))
The above snippet will override app.use
method and implicitly convert all legacy generator middleware to modern promise middleware.
Therefore, you can have both app.use(modernMiddleware)
and app.use(legacyMiddleware)
and your 0.x or 1.x should work without modification.
Complete example:
const Koa = require('koa') // v2.x
const convert = require('koa-convert')
const app = new Koa()
// ---------- override app.use method ----------
const _use = app.use
app.use = x => _use.call(app, convert(x))
// ---------- end ----------
app.use(modernMiddleware)
// this will be converted to modern promise middleware implicitly
app.use(legacyMiddleware)
function * legacyMiddleware (next) {
// before
yield next
// after
}
function modernMiddleware (ctx, next) {
// before
return next().then(() => {
// after
})
}
convert()
Convert legacy generator middleware to modern promise middleware.
modernMiddleware = convert(legacyMiddleware)
convert.compose()
Convert and compose multiple middleware (could mix legacy and modern ones) and return modern promise middleware.
composedModernMiddleware = convert.compose(legacyMiddleware, modernMiddleware)
// or
composedModernMiddleware = convert.compose([legacyMiddleware, modernMiddleware])
convert.back()
Convert modern promise middleware back to legacy generator middleware.
This is useful to help modern promise middleware support koa 0.x or 1.x.
legacyMiddleware = convert.back(modernMiddleware)
MIT