var vm                = require('vm')
  , _                 = require('underscore')
  , domain            = require('domain')
  , Promise           = require('bluebird')
  , util              = require('util')
  , babel             = require('babel-core')
  , App               = require('./app')
  , man               = require('taskmill-core-man')
  , Context           = require('./context').Context
  ;


function Sandbox(worker, options) {
  this.worker = worker;
  this.options = options;

  var code = options.content;

  var es6 = babel.transform(code); // => { code, map, ast }

  this.script = new vm.Script(es6.code, { filename : this.options.filename });

  this.context = vm.createContext(new Context(worker, options.id));

  this.manual = man.get(es6);
};

Sandbox.prototype.handle = function(req, res, next) {
  var next = _.once(next);

  var d = domain.create();

  d.on('error', function(err) {
    return next(err);
  });

  d.run(function() {
    this.script.runInContext(this.context, { timeout : 500 });

    var fct = this.context.module.exports;
    if (_.isFunction(fct)) {
      var app = new App(req, res, next, this.options);

      if (!_.isUndefined(this.manual.type)) {
        res.set('$type', this.manual.type);
      }

      if (this.manual.output) {
        if (_.has(this.manual.output, 'content-type')) {
          res.set('Content-Type', this.manual.output['content-type']);
        }
      }

      req.url = req.headers['$originalurl'];

      fct.call(app, req, res, next);
    } else {
      // todo [akamel] unset content-type header
      throw {
        '#system' : {
            type    : 'exception'
          , error   : 'module.exports not set to a function'
        }
      };
    }
  }.bind(this));
};

module.exports = Sandbox;
@@ -1576,16 +1576,82 @@
 er%0A     
+ throw new Error('module.exports not set to a function');%0A      //
  throw %7B
@@ -1656,16 +1656,19 @@
  %7B%0A     
+ //
    '#sys
@@ -1682,22 +1682,25 @@
 %7B%0A      
+//
       
+ 
 type    
@@ -1714,24 +1714,27 @@
 ption'%0A     
+ //
      , error
@@ -1778,24 +1778,27 @@
 ction'%0A     
+ //
    %7D%0A      %7D
@@ -1795,16 +1795,19 @@
  %7D%0A     
+ //
  %7D;%0A    

change to Error obj