Commit 1dce7e84 authored by nilanjandaw's avatar nilanjandaw

added support for isolate, process, containers

parents
test/
bitnami*
node_modules
package-lock.json
\ No newline at end of file
const isolateBackend = require('./isolate')
const { spawn } = require('child_process');
const libSupport = require('./lib')
const fs = require('fs')
function runIsolate(filename) {
let timeStart = Date.now()
let {isolate, context} = isolateBackend.createIsolate();
// Let's see what happens when we try to blow the isolate's memory
fs.readFile(filename, 'utf-8',(err, data) => {
isolate.compileScript(data).then(script => {
script.run(context)
.then(result => {
console.log(result);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference);
});
})
});
}
function runProcess(filename) {
let timeStart = Date.now()
const process = spawn('node', [filename]);
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("process time taken: ", timeDifference);
});
process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
process.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
}
function runContainer(path) {
let buildStart = Date.now()
let imageName = libSupport.makeid(10)
const process = spawn('docker', ["build", "-t", imageName, path, "-q"]);
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
process.on('close', (code) => {
console.log(`child process exited with code ${code}`);
let timeDifference = Math.ceil((Date.now() - buildStart))
console.log("image build time taken: ", timeDifference);
let timeStart = Date.now()
const process = spawn('docker', ["run", "--name", imageName, imageName]);
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("container run time taken: ", timeDifference);
});
process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
process.on('close', (code) => {
})
});
}
runIsolate('./test/script.js');
runProcess('./test/script.js');
runContainer("test/");
\ No newline at end of file
// Create a new isolate limited to 128MB
const ivm = require('isolated-vm');
function createIsolate() {
let context;
const isolate = new ivm.Isolate({ memoryLimit: 128 });
// Create a new context within this isolate. Each context has its own copy of all the builtin
// Objects. So for instance if one context does Object.prototype.foo = 1 this would not affect any
// other contexts.
context = isolate.createContextSync();
// Get a Reference{} to the global object within the context.
const jail = context.global;
// This make the global object available in the context as `global`. We use `derefInto()` here
// because otherwise `global` would actually be a Reference{} object in the new isolate.
jail.setSync('global', jail.derefInto());
// We will create a basic `log` function for the new isolate to use.
const logCallback = function(...args) {
console.log(...args);
};
context.evalClosureSync(`global.console.log = function(...args) {
$0.applyIgnored(undefined, args, { arguments: { copy: true } });
}`, [ logCallback ], { arguments: { reference: true } });
// And let's test it out:
// context.evalSync('logging sync test.');
return {isolate, context};
}
module.exports.createIsolate = createIsolate;
function makeid(length) {
var result = '';
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
module.exports.makeid = makeid;
\ No newline at end of file
{
"name": "hybrid",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"isolated-vm": "^3.0.0"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment