From 1728bdf8d2e59c74264396948394f005e3f9a431 Mon Sep 17 00:00:00 2001 From: Blake Byrnes Date: Sat, 13 May 2023 14:28:39 -0400 Subject: [PATCH] chore: simplify default function for path context --- README.md | 3 +-- lib/nodevm.js | 2 +- lib/resolver-compat.js | 4 ++-- lib/resolver.js | 4 ++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index aa8f650..e73363b 100644 --- a/README.md +++ b/README.md @@ -141,8 +141,7 @@ Unlike `VM`, `NodeVM` allows you to require modules in the same way that you wou * `require.builtin` - Array of allowed built-in modules, accepts ["\*"] for all (default: none). **WARNING**: "\*" can be dangerous as new built-ins can be added. * `require.root` - Restricted path(s) where local modules can be required (default: every path). * `require.mock` - Collection of mock modules (both external or built-in). -* `require.context` - `host` (default) to require modules in the host and proxy them into the sandbox. `sandbox` to load, compile, and require modules in the sandbox. Except for `events`, built-in modules are always required in the host and proxied into the sandbox. -* `require.pathContext` - A callback allowing custom context to be determined per module. Parameters are the module name, and extension. The callback must return `host` or `sandbox` as per above. +* `require.context` - `host` (default) to require modules in the host and proxy them into the sandbox. `sandbox` to load, compile, and require modules in the sandbox. `callback(moduleFilename, ext)` to dynamically choose a context per module. The default will be sandbox is nothing is specified. Except for `events`, built-in modules are always required in the host and proxied into the sandbox. * `require.import` - An array of modules to be loaded into NodeVM on start. * `require.resolve` - An additional lookup function in case a module wasn't found in one of the traditional node lookup paths. * `require.customRequire` - Use instead of the `require` function to load modules from the host. diff --git a/lib/nodevm.js b/lib/nodevm.js index 38a6305..2d69987 100644 --- a/lib/nodevm.js +++ b/lib/nodevm.js @@ -24,7 +24,7 @@ * * @callback pathContextCallback * @param {string} modulePath - The full path to the module filename being requested. - * @param {string} extensionType - The type of extension (node, js, json) + * @param {string} extensionType - The module type (node = native, js = cjs/esm module) * @return {("host"|"sandbox")} The context for this module. */ diff --git a/lib/resolver-compat.js b/lib/resolver-compat.js index 7983dbc..9021a04 100644 --- a/lib/resolver-compat.js +++ b/lib/resolver-compat.js @@ -113,7 +113,7 @@ class LegacyResolver extends DefaultResolver { loadJS(vm, mod, filename) { filename = this.pathResolve(filename); this.checkAccess(mod, filename); - if (this.pathContext(filename, 'js') === 'sandbox') { + if (this.pathContext(filename, 'js') !== 'host') { const trustedMod = this.trustedMods.get(mod); const script = this.readScript(filename); vm.run(script, {filename, strict: true, module: mod, wrapper: 'none', dirname: trustedMod ? trustedMod.path : mod.path}); @@ -332,7 +332,7 @@ function resolverFromOptions(vm, options, override, compiler) { }; } - const pathContext = typeof options.context === 'function' ? ((module, ext) => options.context(module, ext) || 'sandbox') : (() => context); + const pathContext = typeof context === 'function' ? context : (() => context); if (typeof externalOpt !== 'object') { return new DefaultResolver(fsOpt, builtins, checkPath, [], pathContext, newCustomResolver, hostRequire, compiler, strict); diff --git a/lib/resolver.js b/lib/resolver.js index 22f9061..71eb31d 100644 --- a/lib/resolver.js +++ b/lib/resolver.js @@ -197,7 +197,7 @@ class DefaultResolver extends Resolver { loadJS(vm, mod, filename) { filename = this.pathResolve(filename); this.checkAccess(mod, filename); - if (this.pathContext(filename, 'js') === 'sandbox') { + if (this.pathContext(filename, 'js') !== 'host') { const script = this.readScript(filename); vm.run(script, {filename, strict: this.strict, module: mod, wrapper: 'none', dirname: mod.path}); } else { @@ -216,7 +216,7 @@ class DefaultResolver extends Resolver { loadNode(vm, mod, filename) { filename = this.pathResolve(filename); this.checkAccess(mod, filename); - if (this.pathContext(filename, 'node') === 'sandbox') throw new VMError('Native modules can be required only with context set to \'host\'.'); + 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); }