vmtx

0.0.5 • Public • Published

vmtx

CI

Executes code in sandbox using vm package.

const assert = require('assert/strict');
const { run } = require('vmtx');

assert.strictEqual(execute('123'), 123);

Examples

Load local files

Path to file is resolved against working directory.

run(`require('./path/to/file')`);

But can also be resolved against custom directory.

run({
    code: 'require("./path/to/file")',
    rootdir: './path/to/dir',
});

Execute js file

const { runFile } = require('vmtx');
runFile('./path/to/file');

Variables

run({
    code: 'a + b',
    variables: {
        a: 1,
        b: 2,
    },
});

Variables are visible only in main code.

Globals

run({
    code: 'doSomething();',
    globals: {
        doSomething() { }
    },
});

Unlike variables globals are visible in other modules also.

Modules

run({
    code: `
        const myModule = require("my-test");
        const myFile = require("./my-file");

        console.log(myModule.myData);
        console.log(myFile.myData);
    `,
    loadModule(moduleName) {
        if (moduleName === 'my-test') {
            return { myData: 'Hello World' };
        }
        return null;
    },
    isFile(filepath) {
        return filepath === path.resolve('./my-file.js');
    },
    readFile(filepath) {
        if (filepath === path.resolve('./my-file.js')) {
            return { myData: 'Hello World' };
        }
    },
});

Execution timeout

run({
    code: 'doSomeLongRunningTask()',
    timeout: 30,
});

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i vmtx

Weekly Downloads

4

Version

0.0.5

License

MIT

Unpacked Size

12.4 kB

Total Files

11

Last publish

Collaborators

  • dmitry_bogomolov