mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-03-16 22:10:31 +00:00
98 lines
2.2 KiB
JavaScript
98 lines
2.2 KiB
JavaScript
// Generated by CoffeeScript 1.6.3
|
|
/*
|
|
|
|
Dynamic Modules
|
|
===============
|
|
> Compiles CoffeeScript modules and loads JS modules in a VM, together
|
|
> with only a few allowed node.js modules.
|
|
*/
|
|
|
|
|
|
(function() {
|
|
var cryptico, cs, exports, needle, vm,
|
|
_this = this;
|
|
|
|
vm = require('vm');
|
|
|
|
needle = require('needle');
|
|
|
|
cs = require('coffee-script');
|
|
|
|
cryptico = require('my-cryptico');
|
|
|
|
/*
|
|
Module call
|
|
-----------
|
|
Initializes the dynamic module handler.
|
|
|
|
@param {Object} args
|
|
*/
|
|
|
|
|
|
exports = module.exports = function(args) {
|
|
var numBits, passPhrase;
|
|
_this.log = args.logger;
|
|
if (!_this.strPublicKey && args['keygen']) {
|
|
passPhrase = args['keygen'];
|
|
numBits = 1024;
|
|
_this.oPrivateRSAkey = cryptico.generateRSAKey(passPhrase, numBits);
|
|
_this.strPublicKey = cryptico.publicKeyString(_this.oPrivateRSAkey);
|
|
_this.log.info("Public Key generated: " + _this.strPublicKey);
|
|
}
|
|
return module.exports;
|
|
};
|
|
|
|
exports.getPublicKey = function() {
|
|
return _this.strPublicKey;
|
|
};
|
|
|
|
/*
|
|
Try to run a JS module from a string, together with the
|
|
given parameters. If it is written in CoffeeScript we
|
|
compile it first into JS.
|
|
|
|
@public compileString ( *src, id, params, lang* )
|
|
@param {String} src
|
|
@param {String} id
|
|
@param {Object} params
|
|
@param {String} lang
|
|
*/
|
|
|
|
|
|
exports.compileString = function(src, userId, modId, params, lang) {
|
|
var answ, err, ret, sandbox;
|
|
answ = {
|
|
code: 200,
|
|
message: 'Successfully compiled'
|
|
};
|
|
if (lang === 'CoffeeScript') {
|
|
try {
|
|
src = cs.compile(src);
|
|
} catch (_error) {
|
|
err = _error;
|
|
answ.code = 400;
|
|
answ.message = 'Compilation of CoffeeScript failed at line ' + err.location.first_line;
|
|
}
|
|
}
|
|
sandbox = {
|
|
id: userId + '.' + modId + '.vm',
|
|
params: params,
|
|
needle: needle,
|
|
log: console.log,
|
|
exports: {}
|
|
};
|
|
try {
|
|
vm.runInNewContext(src, sandbox, sandbox.id);
|
|
} catch (_error) {
|
|
err = _error;
|
|
answ.code = 400;
|
|
answ.message = 'Loading Module failed: ' + err.message;
|
|
}
|
|
ret = {
|
|
answ: answ,
|
|
module: sandbox.exports
|
|
};
|
|
return ret;
|
|
};
|
|
|
|
}).call(this);
|