Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function createIsolate(): Promise {
const ivmIso = new ivm.Isolate({ snapshot })
return new Promise((resolve, reject) => {
createContext(ivmIso).then(function (ctx) {
const iso = new Isolate(ivmIso, ctx)
// test the isolate!
iso.test().then(() => resolve(iso)).catch(reject)
})
})
}
index.createIsolate = ({ memoryLimit, inspector } = {}) => {
const isolate = new ivm.Isolate({ memoryLimit, inspector });
const context = isolate.createContextSync({ inspector });
const jail = context.globalReference();
jail.setSync('global', jail.derefInto());
return {
isolate,
context,
jail
};
};
constructor(options) {
this.ramLimit = (typeof options === 'undefined' || typeof options.ramLimit === 'undefined' ? 32 : options.ramLimit);
this.ivm = ivm;
this.isolate = new ivm.Isolate({memoryLimit: this.ramLimit});
this.script = '';
this.state = undefined;
this.context = undefined;
this.timeout = (typeof options === 'undefined' || typeof options.timeLimit === 'undefined' ? 1000 : options.timeLimit);
this.cpuLimit = (typeof options === 'undefined' || typeof options.cpuLimit === 'undefined' ? 500 : options.cpuLimit);
this.logging = (typeof options === 'undefined' || typeof options.logging === 'undefined' ? true : options.logging);
this.busy = false;
this.waitingForResponse = false;
this.logPrefix = (typeof options === 'undefined' || typeof options.logPrefix === 'undefined' ? '' : options.logPrefix);
}
async function evaluate({
canBroadcast = false,
printResult = false,
script,
msgData,
node,
command,
event,
}) {
const isolate = new ivm.Isolate({ memoryLimit: 128 });
const context = await isolate.createContext();
const dispose = () => {
if (!isolate.isDisposed) {
isolate.dispose();
context.release();
}
};
try {
const channels = Object.entries(_.cloneDeep(node.client.chans))
.reduce((acc, [key, value]) => {
delete value.users;
acc[key.toLowerCase()] = value;
return acc;
}, {});