recime-bot-runtime
Version:
This runtime is intended to run inside a micro-service container with platform specific integration and module interpreter.
68 lines (67 loc) • 2.63 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var domain = require("domain");
var vm = require("vm");
var RecimeError = /** @class */ (function (_super) {
__extends(RecimeError, _super);
function RecimeError(message) {
return _super.call(this, message) || this;
}
return RecimeError;
}(Error));
;
var code = "\nconst responder = require('recime-message-responder');\n// execute\nexports.handler = (args, done) => {\n return done(responder.respond(args, process));\n};";
var Runnable = /** @class */ (function () {
function Runnable(bot, options) {
this.options = options;
this.bot = bot;
}
Runnable.prototype.run = function (args) {
var _this = this;
return new Promise(function (resolve, reject) {
var options = _this.options;
// uid
options.env['UID'] = options.uid;
options.env['HOME_DIR'] = options.homedir;
// pass in platform for external modules to take advantage of.
options.env['PLATFORM'] = options.platform || "web";
Object.keys(_this.bot.config).forEach(function (key) {
options.env[key] = _this.bot.config[key];
});
var sandbox = {
console: console,
exports: exports,
process: options,
require: require,
__dirname: __dirname
};
var script = new vm.Script(code);
var context = vm.createContext(sandbox);
var Module = script.runInContext(context);
if (typeof Module === 'undefined') {
reject(new RecimeError("Could not resolve the main module."));
return;
}
var local = domain.create();
local.on("error", function (err) {
reject(new RecimeError(err.message));
});
local.run(function () {
// export default
Module(args, resolve);
});
});
};
return Runnable;
}());
exports.Runnable = Runnable;