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)
})
})
}
const __makeTransferable = (element) => {
if (
element === null ||
typeof element === 'number' ||
typeof element === 'string' ||
typeof element === 'boolean' ||
typeof element === 'undefined'
) {
// primitives are transferable
return element;
}
// element = __fixFunctions(element);
if (element instanceof Object) {
return new ivm.ExternalCopy(element);
}
throw new Error(`type ${typeof element} is not transferable`);
};
get snapshot() {
if (v8SnapshotChecked) {
return v8EnvSnapshot
}
if (!v8SnapshotsEnabled) {
console.warn("can't use v8 snapshots with this version of node, boot will be slower", process.version)
} else {
console.log("v8 snapshots enabled")
if (fs.existsSync(v8distSnapshot)) {
v8EnvSnapshot = new ivm.ExternalCopy(fs.readFileSync(v8distSnapshot).buffer as ArrayBuffer)
} else if (v8EnvCode) {
v8EnvSnapshot = ivm.Isolate.createSnapshot([
{
code: v8EnvCode,
filename: "dist/v8env.js"
}
])
}
if (fs.existsSync(v8mapDist)) {
v8EnvSourceMap = fs.readFileSync(v8mapDist).toString()
}
}
v8SnapshotChecked = true
return v8EnvSnapshot
}
module.exports = ({ jail, isolate, context }) => {
jail.setSync('_log', new ivm.Reference(function (...args) {
console.log(...args);
}));
jail.setSync('_error', new ivm.Reference(function (...args) {
console.error(...args);
}));
isolate.compileScriptSync(run(function () {
const log = global._log;
delete global._log;
const error = global._error;
delete global._error;
const console = {};
Object.defineProperty(global, 'console', {
value: console,
writable: false,
},
};
const secret = node.get('secrets', {})[command.root];
if (event) {
config.IRC.event = event;
}
if (secret) {
config.IRC.secret = secret;
}
const jail = context.global;
jail.setSync('global', jail.derefInto());
jail.setSync('config', new ivm.ExternalCopy(config).copyInto());
jail.setSync('_ivm', ivm);
jail.setSync('_sendRaw', new ivm.Reference(node.sendRaw));
jail.setSync('_resetBuffer', new ivm.Reference(node.resetBuffer));
jail.setSync('_setNick', new ivm.Reference((str) => {
if (node.getChannelConfig(msgData.to).setNick) {
str = String(str).replace(/[^a-zA-Z0-9]+/g, '');
node.client.send('NICK', str);
return true;
} else {
return false;
}
}));
jail.setSync('_whois', new ivm.Reference((text) => (
text && new Promise((resolve, reject) => {
node.client.whois(text, (data) => {
try {
jail.setSync('_'+name, new ivm.Reference((fnName, ...args) => {
return new ivm.ExternalCopy(obj[fnName](...args)).copyInto();
}));
}
let result = vmContext.applySync(prevContext.derefInto(), args.map(arg => new ivm.ExternalCopy(arg).copyInto()), {timeout: this.timeout});
fs.readFile(path, 'utf8', (err, data) => {
if (err) reject(err);
else resolve(new ivm.ExternalCopy(data).copyInto());
});
} else {
.then(obj => resolve(new ivm.ExternalCopy(obj).copyInto()))
.catch(reject);
function wrapFns(obj, name) {
jail.setSync(
`_${name}Keys`,
new ivm.ExternalCopy(Object.keys(obj)).copyInto(),
);
jail.setSync('_'+name, new ivm.Reference((fnName, ...args) => {
return new ivm.ExternalCopy(obj[fnName](...args)).copyInto();
}));
}