This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
# koa-convert | ||
|
||
Convert koa generator-based middleware to promise-based middleware. | ||
|
||
## Installation | ||
|
||
``` | ||
$ npm install koa-convert | ||
``` | ||
|
||
## Related Issues | ||
|
||
* koa [#415](https://github.com/koajs/koa/issues/415) | ||
* koa-compose [#27](https://github.com/koajs/compose/pull/27) | ||
|
||
## Usage | ||
|
||
```js | ||
// | ||
// convert a generator-based middleware to promise-based middleware | ||
// | ||
let promiseBased = convert(function* generatorBased(next) { | ||
yield next | ||
// or | ||
// yield* next | ||
}) | ||
|
||
// | ||
// convert array of middleware | ||
// | ||
let mws = [ | ||
// will convert it to promise-based middleware | ||
function* generatorMW (next) { | ||
yield next | ||
}, | ||
// will convert it to promise-based middleware | ||
function* generatorMW(next) { | ||
yield* next | ||
}, | ||
// return itself if it's not generator-based middleware | ||
function (ctx, next) { | ||
return next() | ||
}, | ||
// return itself if it's not generator-based middleware | ||
async function (ctx, next) { | ||
await next() | ||
}, | ||
].map(convert) | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
const co = require('co') | ||
|
||
module.exports = convert | ||
|
||
function convert(mw) { | ||
if (typeof mw !== 'function') { | ||
throw new TypeError(mw + ' is not function') | ||
} | ||
if (mw.constructor.name === 'GeneratorFunction') { | ||
return function (ctx, next) { | ||
return co.call(ctx, mw.call(ctx, createGenerator(next))) | ||
} | ||
} else { | ||
// assume it's Promise-based middleware | ||
return mw | ||
} | ||
} | ||
|
||
function* createGenerator(next) { | ||
return yield next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "koa-convert", | ||
"version": "0.0.1", | ||
"description": "convert koa generator-based middleware to promise-based middleware", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/gyson/koa-convert.git" | ||
}, | ||
"author": "gyson <eilian.yunsong@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/gyson/koa-convert/issues" | ||
}, | ||
"homepage": "https://github.com/gyson/koa-convert#readme", | ||
"dependencies": { | ||
"co": "^4.6.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
const co = require('co') | ||
const assert = require('assert') | ||
const convert = require('./index') | ||
|
||
describe('Koa Convert', function () { | ||
it('should works', function (done) { | ||
let call = [] | ||
let ctx = {} | ||
let mw = convert(function* (next) { | ||
assert.ok(ctx === this) | ||
call.push(1) | ||
}) | ||
|
||
mw(ctx, function () { | ||
done(new Error('this should not be called')) | ||
}).then(function () { | ||
assert.deepEqual(call, [1]) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should works with `yield next`', function (done) { | ||
let call = [] | ||
let ctx = {} | ||
let mw = convert(function* (next) { | ||
assert.ok(ctx === this) | ||
call.push(1) | ||
yield next | ||
call.push(3) | ||
}) | ||
|
||
mw(ctx, function () { | ||
call.push(2) | ||
return Promise.resolve() | ||
}).then(function () { | ||
assert.deepEqual(call, [1, 2, 3]) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should works with `yield* next`', function (done) { | ||
let call = [] | ||
let ctx = {} | ||
let mw = convert(function* (next) { | ||
assert.ok(ctx === this) | ||
call.push(1) | ||
yield* next | ||
call.push(3) | ||
}) | ||
|
||
mw(ctx, function () { | ||
call.push(2) | ||
return Promise.resolve() | ||
}).then(function () { | ||
assert.deepEqual(call, [1, 2, 3]) | ||
done() | ||
}) | ||
}) | ||
}) |