UNPKG

705 BJavaScriptView Raw
1
2/**
3 * Expose compositor.
4 */
5
6module.exports = compose;
7
8/**
9 * Compose `middleware` returning
10 * a fully valid middleware comprised
11 * of all those which are passed.
12 *
13 * @param {Array} middleware
14 * @return {Function}
15 * @api public
16 */
17
18function compose(middleware){
19 return function *(downstream){
20 var done = false;
21 var ctx = this;
22 var i = 0;
23
24 yield *next();
25
26 function next(){
27 var mw = middleware[i++];
28
29 if (!mw) {
30 if (done) throw new Error('middleware yielded control multiple times');
31 done = true;
32 return downstream || noop();
33 }
34
35 return mw.call(ctx, next());
36 }
37 }
38}
39
40/**
41 * Noop.
42 *
43 * @api private
44 */
45
46function *noop(){}
\No newline at end of file