diff --git a/lib/resolver.js b/lib/resolver.js index b05bd39..fd86aff 100644 --- a/lib/resolver.js +++ b/lib/resolver.js @@ -206,15 +206,8 @@ class DefaultResolver extends Resolver { } loadJS(vm, mod, filename) { - filename = this.pathResolve(filename); - this.checkAccess(mod, filename); - if (this.pathContext(filename, 'js') !== 'host') { - const script = this.readScript(filename); - vm.run(script, {filename, strict: this.isStrict(filename), module: mod, wrapper: 'none', dirname: mod.path}); - } else { - const m = this.hostRequire(filename); - mod.exports = vm.readonly(m); - } + const script = this.readScript(filename); + vm.run(script, {filename, strict: this.isStrict(filename), module: mod, wrapper: 'none', dirname: mod.path}); } loadJSON(vm, mod, filename) { @@ -223,11 +216,7 @@ class DefaultResolver extends Resolver { } loadNode(vm, mod, filename) { - filename = this.pathResolve(filename); - this.checkAccess(mod, filename); - if (this.pathContext(filename, 'node') !== 'host') throw new VMError('Native modules can be required only with context set to \'host\'.'); - const m = this.hostRequire(filename); - mod.exports = vm.readonly(m); + throw new VMError('Native modules can be required only with context set to \'host\'.'); } customResolve(x, path, extList) { diff --git a/test/nodevm.js b/test/nodevm.js index 41669eb..c67a19f 100644 --- a/test/nodevm.js +++ b/test/nodevm.js @@ -12,6 +12,16 @@ const {NodeVM, VMScript, makeResolverFromLegacyOptions} = require('..'); global.isHost = true; +function isVMProxy(obj) { + const key = {}; + const proto = Object.getPrototypeOf(obj); + if (!proto) return undefined; + proto.isVMProxy = key; + const proxy = obj.isVMProxy !== key; + delete proto.isVMProxy; + return proxy; +} + describe('NodeVM', () => { let vm; @@ -228,7 +238,7 @@ describe('modules', () => { assert.ok(vm.run("require('module1')", __filename)); }); - it('allows choosing a context by path', () => { + it('allows choosing a context by path legacy', () => { const vm = new NodeVM({ require: { external: { @@ -241,15 +251,20 @@ describe('modules', () => { } } }); - function isVMProxy(obj) { - const key = {}; - const proto = Object.getPrototypeOf(obj); - if (!proto) return undefined; - proto.isVMProxy = key; - const proxy = obj.isVMProxy !== key; - delete proto.isVMProxy; - return proxy; - } + assert.equal(isVMProxy(vm.run("module.exports = require('mocha')", __filename)), false, 'Mocha is a proxy'); + assert.equal(isVMProxy(vm.run("module.exports = require('module1')", __filename)), true, 'Module1 is not a proxy'); + }); + + it('allows choosing a context by path', () => { + const vm = new NodeVM({ + require: { + external: true, + context(module) { + if (module.includes('mocha')) return 'host'; + return 'sandbox'; + } + } + }); assert.equal(isVMProxy(vm.run("module.exports = require('mocha')", __filename)), false, 'Mocha is a proxy'); assert.equal(isVMProxy(vm.run("module.exports = require('module1')", __filename)), true, 'Module1 is not a proxy'); });