How to use isolated-vm - 10 common examples

To help you get started, we’ve selected a few isolated-vm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github superfly / fly / src / isolate.ts View on Github external
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)
		})
	})
}
github legraphista / isolated-threads / src / helpers / transferable.js View on Github external
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`);
};
github superfly / fly / packages / core / src / v8env.ts View on Github external
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
  }
github legraphista / isolated-threads / src / helpers / bootstrap / console.js View on Github external
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,
github kirjavascript / nibblrjr / irc / evaluate / index.js View on Github external
},
        };

        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 {
github kirjavascript / nibblrjr / irc / evaluate / index.js View on Github external
jail.setSync('_'+name, new ivm.Reference((fnName, ...args) => {
                return new ivm.ExternalCopy(obj[fnName](...args)).copyInto();
            }));
        }
github Izzzio / izzzio / modules / smartContracts / VM.js View on Github external
        let result = vmContext.applySync(prevContext.derefInto(), args.map(arg => new ivm.ExternalCopy(arg).copyInto()), {timeout: this.timeout});
github kirjavascript / nibblrjr / irc / evaluate / index.js View on Github external
fs.readFile(path, 'utf8', (err, data) => {
                            if (err) reject(err);
                            else resolve(new ivm.ExternalCopy(data).copyInto());
                        });
                    } else {
github kirjavascript / nibblrjr / irc / evaluate / index.js View on Github external
                    .then(obj => resolve(new ivm.ExternalCopy(obj).copyInto()))
                    .catch(reject);
github kirjavascript / nibblrjr / irc / evaluate / index.js View on Github external
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();
            }));
        }

isolated-vm

Access to multiple isolates

ISC
Latest version published 5 months ago

Package Health Score

82 / 100
Full package analysis

Similar packages