-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
38 lines (26 loc) · 813 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"use strict"
const isgen = require("is-generator-function")
const wrap = require("co").wrap
const curry = Function.bind.bind(Function.call)
module.exports = adapt
module.exports.Adapter = Adapter
function* tick(next) { yield next() }
function adapt(middleware) {
// skip for non-function
if (typeof middleware !== 'function') return middleware
// skip for modern middleware
if (!isgen(middleware)) return middleware
// avoid wrapping middleware for every request
let fn = wrap(curry(middleware))
return function(ctx, next) {
return fn(ctx, tick(next))
}
}
function Adapter(Koa) {
var use = Koa && Koa.prototype && Koa.prototype.use
if (!use || typeof use !== "function") return Koa
Koa.prototype.use = function(fn) {
return use.call(this, adapt(fn))
}
return Koa
}