Skip to content

Commit 1728bdf

Browse files
committed
chore: simplify default function for path context
1 parent 7d16a56 commit 1728bdf

File tree

4 files changed

+6
-7
lines changed

4 files changed

+6
-7
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ Unlike `VM`, `NodeVM` allows you to require modules in the same way that you wou
141141
* `require.builtin` - Array of allowed built-in modules, accepts ["\*"] for all (default: none). **WARNING**: "\*" can be dangerous as new built-ins can be added.
142142
* `require.root` - Restricted path(s) where local modules can be required (default: every path).
143143
* `require.mock` - Collection of mock modules (both external or built-in).
144-
* `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.
145-
* `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.
144+
* `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.
146145
* `require.import` - An array of modules to be loaded into NodeVM on start.
147146
* `require.resolve` - An additional lookup function in case a module wasn't found in one of the traditional node lookup paths.
148147
* `require.customRequire` - Use instead of the `require` function to load modules from the host.

lib/nodevm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @callback pathContextCallback
2626
* @param {string} modulePath - The full path to the module filename being requested.
27-
* @param {string} extensionType - The type of extension (node, js, json)
27+
* @param {string} extensionType - The module type (node = native, js = cjs/esm module)
2828
* @return {("host"|"sandbox")} The context for this module.
2929
*/
3030

lib/resolver-compat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class LegacyResolver extends DefaultResolver {
113113
loadJS(vm, mod, filename) {
114114
filename = this.pathResolve(filename);
115115
this.checkAccess(mod, filename);
116-
if (this.pathContext(filename, 'js') === 'sandbox') {
116+
if (this.pathContext(filename, 'js') !== 'host') {
117117
const trustedMod = this.trustedMods.get(mod);
118118
const script = this.readScript(filename);
119119
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) {
332332
};
333333
}
334334

335-
const pathContext = typeof options.context === 'function' ? ((module, ext) => options.context(module, ext) || 'sandbox') : (() => context);
335+
const pathContext = typeof context === 'function' ? context : (() => context);
336336

337337
if (typeof externalOpt !== 'object') {
338338
return new DefaultResolver(fsOpt, builtins, checkPath, [], pathContext, newCustomResolver, hostRequire, compiler, strict);

lib/resolver.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class DefaultResolver extends Resolver {
197197
loadJS(vm, mod, filename) {
198198
filename = this.pathResolve(filename);
199199
this.checkAccess(mod, filename);
200-
if (this.pathContext(filename, 'js') === 'sandbox') {
200+
if (this.pathContext(filename, 'js') !== 'host') {
201201
const script = this.readScript(filename);
202202
vm.run(script, {filename, strict: this.strict, module: mod, wrapper: 'none', dirname: mod.path});
203203
} else {
@@ -216,7 +216,7 @@ class DefaultResolver extends Resolver {
216216
loadNode(vm, mod, filename) {
217217
filename = this.pathResolve(filename);
218218
this.checkAccess(mod, filename);
219-
if (this.pathContext(filename, 'node') === 'sandbox') throw new VMError('Native modules can be required only with context set to \'host\'.');
219+
if (this.pathContext(filename, 'node') !== 'host') throw new VMError('Native modules can be required only with context set to \'host\'.');
220220
const m = this.hostRequire(filename);
221221
mod.exports = vm.readonly(m);
222222
}

0 commit comments

Comments
 (0)