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.
* update .gitignore file π .. * rm -rf test file β .. * setup coverage --nyc π½ .. * update ci pipeline π² .. * improve the src --better β¨ .. * update test code --improve --coverge --100 π§ͺ.. * update README.md π .. * better pkg.json π .. * update LICENSE π .. * update README.md π ..
- Loading branch information
1 parent
481010e
commit 290ca57
Showing
8 changed files
with
228 additions
and
69 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 |
---|---|---|
@@ -1 +1,20 @@ | ||
|
||
# OS # | ||
################### | ||
.DS_Store | ||
.idea | ||
Thumbs.db | ||
tmp | ||
temp | ||
|
||
|
||
# Node.js # | ||
################### | ||
node_modules | ||
package-lock.json | ||
|
||
|
||
# NYC # | ||
################### | ||
coverage | ||
.nyc_output |
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,15 @@ | ||
{ | ||
"extension": [ | ||
".js" | ||
], | ||
"exclude": [ | ||
"index.spec.js" | ||
], | ||
"reporter": [ | ||
"text-lcov", | ||
"text", | ||
"lcov" | ||
], | ||
"report-dir": "./coverage", | ||
"temp-dir": "./.nyc_output" | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
language: node_js | ||
node_js: | ||
- "4" | ||
- "5" | ||
script: "npm run test" | ||
- 10 | ||
- 12 | ||
- 14 | ||
script: | ||
- npm run ci |
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
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
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 |
---|---|---|
@@ -1,60 +1,105 @@ | ||
'use strict' | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const co = require('co') | ||
const compose = require('koa-compose') | ||
|
||
/** | ||
* Expose `convert()`. | ||
*/ | ||
|
||
module.exports = convert | ||
|
||
/** | ||
* Convert Koa legacy generator-based middleware | ||
* to modern promise-based middleware. | ||
* | ||
* | ||
* @api public | ||
* */ | ||
|
||
function convert (mw) { | ||
if (typeof mw !== 'function') { | ||
throw new TypeError('middleware must be a function') | ||
} | ||
if (mw.constructor.name !== 'GeneratorFunction') { | ||
// assume it's Promise-based middleware | ||
|
||
// assume it's Promise-based middleware | ||
if ( | ||
mw.constructor.name !== 'GeneratorFunction' && | ||
mw.constructor.name !== 'AsyncGeneratorFunction' | ||
) { | ||
return mw | ||
} | ||
|
||
const converted = function (ctx, next) { | ||
return co.call(ctx, mw.call(ctx, createGenerator(next))) | ||
return co.call( | ||
ctx, | ||
mw.call( | ||
ctx, | ||
(function * (next) { return yield next() })(next) | ||
)) | ||
} | ||
|
||
converted._name = mw._name || mw.name | ||
return converted | ||
} | ||
|
||
function * createGenerator (next) { | ||
return yield next() | ||
} | ||
/** | ||
* Convert and compose multiple middleware | ||
* (could mix legacy and modern ones) | ||
* and return modern promise middleware. | ||
* | ||
* | ||
* @api public | ||
* */ | ||
|
||
// convert.compose(mw, mw, mw) | ||
// convert.compose([mw, mw, mw]) | ||
convert.compose = function (arr) { | ||
if (!Array.isArray(arr)) { | ||
arr = Array.from(arguments) | ||
} | ||
|
||
return compose(arr.map(convert)) | ||
} | ||
|
||
/** | ||
* Convert Koa modern promise-based middleware | ||
* to legacy generator-based middleware. | ||
* | ||
* | ||
* @api public | ||
* */ | ||
|
||
convert.back = function (mw) { | ||
if (typeof mw !== 'function') { | ||
throw new TypeError('middleware must be a function') | ||
} | ||
if (mw.constructor.name === 'GeneratorFunction') { | ||
// assume it's generator middleware | ||
|
||
// assume it's generator middleware | ||
if (mw.constructor.name === 'GeneratorFunction' || mw.constructor.name === 'AsyncGeneratorFunction') { | ||
return mw | ||
} | ||
|
||
const converted = function * (next) { | ||
let ctx = this | ||
const ctx = this | ||
let called = false | ||
// no need try...catch here, it's ok even `mw()` throw exception | ||
yield Promise.resolve(mw(ctx, function () { | ||
|
||
yield mw(ctx, function () { | ||
if (called) { | ||
// guard against multiple next() calls | ||
// https://github.com/koajs/compose/blob/4e3e96baf58b817d71bd44a8c0d78bb42623aa95/index.js#L36 | ||
return Promise.reject(new Error('next() called multiple times')) | ||
throw new Error('next() called multiple times') | ||
} | ||
|
||
called = true | ||
return co.call(ctx, next) | ||
})) | ||
}) | ||
} | ||
|
||
converted._name = mw._name || mw.name | ||
return converted | ||
} |
Oops, something went wrong.