Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Commit

Permalink
add implementation, readme and test
Browse files Browse the repository at this point in the history
  • Loading branch information
gyson committed Oct 11, 2015
1 parent 26014cb commit c1bdd75
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
54 changes: 54 additions & 0 deletions README.md
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
23 changes: 23 additions & 0 deletions index.js
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()
}
25 changes: 25 additions & 0 deletions package.json
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"
}
}
61 changes: 61 additions & 0 deletions test.js
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()
})
})
})

0 comments on commit c1bdd75

Please sign in to comment.