/**
 * main
 * @author iswujixiang@gmail.com
 */

const Stream    = require('stream');

const Koa       = require('koa');
const compose   = require('koa-compose');

const app       = new Koa;

const k2e       = middlewares => (req, res, next) => {

    const ctx   = app.createContext(req, res);

    if (!Array.isArray(middlewares)) {
        middlewares = [middlewares];
    }

    compose(middlewares)(ctx)

        .then(() => {
            // allow bypassing koa
            /* istanbul ignore if */
            if (ctx.respond === false) {
                return next();
            }

            /* istanbul ignore if */
            if (!ctx.writable) {
                return next();
            }

            const {res, body} = ctx;
            if (null != body) {
                if (Buffer.isBuffer(body)) {
                    return res.send(body);
                }
                if (typeof body === 'string') {
                    return res.send(body);
                }
                if (body instanceof Stream) {
                    return body.pipe(res);
                }

                // body: json
                return res.json(body);
            }

            next();

        })

        .catch(next);
};

module.exports  = k2e;
/**
 * main
 * @author iswujixiang@gmail.com
 */

const Stream    = require('stream');

const Koa       = require('koa');
const compose   = require('koa-compose');

const app       = new Koa;

const k2e       = middlewares => {
    if (!Array.isArray(middlewares)) {
        middlewares = [middlewares];
    }

    const middleware = compose(middlewares);

    return (req, res, next) => {

        const ctx   = app.createContext(req, res);

        middleware(ctx)

            .then(() => {
                // allow bypassing koa
                /* istanbul ignore if */
                if (ctx.respond === false) {
                    return next();
                }

                /* istanbul ignore if */
                if (!ctx.writable) {
                    return next();
                }

                const {res, body} = ctx;
                if (null != body) {
                    if (Buffer.isBuffer(body)) {
                        return res.send(body);
                    }
                    if (typeof body === 'string') {
                        return res.send(body);
                    }
                    if (body instanceof Stream) {
                        return body.pipe(res);
                    }

                    // body: json
                    return res.json(body);
                }

                next();

            })

            .catch(next);
    };
};

module.exports  = k2e;
use closure to save the composed middleware