Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
Merge pull request #287 from XmiliaH/fix-282
Browse files Browse the repository at this point in the history
Added ability to set process.env & argv
  • Loading branch information
XmiliaH authored Apr 29, 2020
2 parents 58b2482 + ca1036c commit 1330a7e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ export interface NodeVMOptions extends VMOptions {
wrapper?: "commonjs" | "none";
/** File extensions that the internal module resolver should accept. */
sourceExtensions?: string[];
/**
* Array of arguments passed to `process.argv`.
* This object will not be copied and the script can change this object.
*/
argv?: string[];
/**
* Environment map passed to `process.env`.
* This object will not be copied and the script can change this object.
*/
env?: any;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,10 @@ class NodeVM extends VM {
* @param {("commonjs"|"none")} [options.wrapper="commonjs"] - <code>commonjs</code> to wrap script into CommonJS wrapper,
* <code>none</code> to retrieve value returned by the script.
* @param {string[]} [options.sourceExtensions=["js"]] - Array of file extensions to treat as source code.
* @param {string[]} [options.argv=[]] - Array of arguments passed to <code>process.argv</code>.
* This object will not be copied and the script can change this object.
* @param {Object} [options.env={}] - Environment map passed to <code>process.env</code>.
* This object will not be copied and the script can change this object.
* @throws {VMError} If the compiler is unknown.
*/
constructor(options = {}) {
Expand All @@ -985,13 +989,13 @@ class NodeVM extends VM {
let sandboxScript = CACHE.sandboxScript;
if (!sandboxScript) {
CACHE.sandboxScript = sandboxScript = loadAndCompileScript(`${__dirname}/sandbox.js`,
'(function (vm, host, Contextify, Decontextify, Buffer) { ', '\n})');
'(function (vm, host, Contextify, Decontextify, Buffer, options) { ', '\n})');
}

const closure = sandboxScript.runInContext(this._context, DEFAULT_RUN_OPTIONS);

Object.defineProperty(this, '_prepareRequire', {
value: closure.call(this._context, this, HOST, this._internal.Contextify, this._internal.Decontextify, this._internal.Buffer)
value: closure.call(this._context, this, HOST, this._internal.Contextify, this._internal.Decontextify, this._internal.Buffer, options)
});

// prepare global sandbox
Expand Down
8 changes: 5 additions & 3 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-shadow, no-invalid-this */
/* global vm, host, Contextify, Decontextify, VMError */
/* global vm, host, Contextify, Decontextify, VMError, options */

'use strict';

Expand Down Expand Up @@ -502,15 +502,17 @@ return ((vm, host) => {
return this;
}

const {argv: optionArgv, env: optionsEnv} = options;

// FIXME wrong class structure
global.process = {
argv: [],
argv: optionArgv !== undefined ? Contextify.value(optionArgv) : [],
title: host.process.title,
version: host.process.version,
versions: Contextify.readonly(host.process.versions),
arch: host.process.arch,
platform: host.process.platform,
env: {},
env: optionsEnv !== undefined ? Contextify.value(optionsEnv) : {},
pid: host.process.pid,
features: Contextify.readonly(host.process.features),
nextTick: function nextTick(callback, ...args) {
Expand Down
14 changes: 13 additions & 1 deletion test/nodevm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,27 @@ global.isVM = false;
describe('NodeVM', () => {
let vm;

const customArgv = [];
const customEnv = {};

before(() => {
vm = new NodeVM;
vm = new NodeVM({
argv: customArgv,
env: customEnv
});
});

it('globals', () => {
const ex = vm.run('module.exports = global');
assert.equal(ex.isVM, true);
});

it('options', ()=>{
const vmProcess = vm.run('module.exports = process');
assert.equal(vmProcess.argv, customArgv);
assert.equal(vmProcess.env, customEnv);
});

it('errors', () => {
assert.throws(() => vm.run('notdefined'), /notdefined is not defined/);
});
Expand Down

0 comments on commit 1330a7e

Please sign in to comment.