'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Router = require('koa-router');
var http = require('http');

var httpRouter = Router();
var bundleWrapper = 'function __weex_bundle_entry__(define, require, document, bootstrap,register, render, __weex_define__, __weex_bootstrap__){';
httpRouter.get('/getScript', _regenerator2.default.mark(function _callee(next) {
    var scriptPath, body;
    return _regenerator2.default.wrap(function _callee$(_context) {
        while (1) {
            switch (_context.prev = _context.next) {
                case 0:
                    scriptPath = this.query.path;
                    _context.next = 3;
                    return new Promise(function (resolve, reject) {
                        http.get(scriptPath, function (res) {
                            var chunks = [];

                            res.setEncoding('utf8');

                            res.on('data', function (chunk) {
                                chunks.push(chunk);
                            });

                            res.on('end', function () {
                                resolve(chunks.join(''));
                            });
                        }).on('error', function (e) {
                            reject(e.message);
                        });
                    });

                case 3:
                    body = _context.sent;


                    this.response.status = 200;
                    this.response.contentType = 'text/javascript';
                    this.response.body = bundleWrapper + body + '\n}';

                case 7:
                case 'end':
                    return _context.stop();
            }
        }
    }, _callee, this);
}));
module.exports = httpRouter;