mirror of
https://github.com/jsbin/jsbin.git
synced 2025-12-08 20:12:59 +00:00
42 lines
943 B
JavaScript
42 lines
943 B
JavaScript
'use strict';
|
|
|
|
var fork = require('child_process').fork;
|
|
|
|
module.exports = function (language, source) {
|
|
return new Promise(function (resolve, reject) {
|
|
var child = fork(__dirname);
|
|
var output = '';
|
|
|
|
var timeout = setTimeout(function () {
|
|
console.error(language + ' processor timeout');
|
|
child.kill();
|
|
}, 1000);
|
|
|
|
child.on('stderr', function (data) {
|
|
console.error(language + ' processor errors');
|
|
console.error(data);
|
|
});
|
|
|
|
child.on('message', function (message) {
|
|
output += message;
|
|
});
|
|
|
|
child.on('exit', function () {
|
|
clearTimeout(timeout);
|
|
try {
|
|
resolve(JSON.parse(output));
|
|
} catch (e) {
|
|
reject({
|
|
ouput: {
|
|
result: '',
|
|
errors: null
|
|
},
|
|
error: new Error('Could not parse response from', language)
|
|
});
|
|
}
|
|
});
|
|
|
|
child.send({ language: language, data: source});
|
|
});
|
|
};
|