Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post MVP: Specifying import policy for new contexts #5

Open
zenparsing opened this issue Feb 25, 2019 · 0 comments
Open

Post MVP: Specifying import policy for new contexts #5

zenparsing opened this issue Feb 25, 2019 · 0 comments

Comments

@zenparsing
Copy link
Owner

Users should be able to specify an import policy for newly created contexts. By default, importing should not be allowed.

The previous implementation in vm.js allowed an importPolicy option to vm.createContext:

const ModuleLoader = require('internal/modules/esm/module_loader');
const { setModuleLoaderForContext } = internalBinding('module_wrap');

function validateImportPolicy(prop, propName) {
  if (prop === undefined)
    return;
  if (typeof prop.resolve !== 'function')
    throw new ERR_INVALID_ARG_TYPE(`${propName}.resolve`, 'Function', prop.resolve);
  if (typeof prop.load !== 'function')
    throw new ERR_INVALID_ARG_TYPE(`${propName}.resolve`, 'Function', prop.resolve);
}

function createContext(sandbox = {}, options = {}) {
  // ...

  validateImportPolicy(importPolicy, 'options.importPolicy');

  makeContext(sandbox, name, origin, strings, wasm);

  if (importPolicy) {
    setModuleLoaderForContext(sandbox, new ModuleLoader({
      context: sandbox,
      resolve: importPolicy.resolve,
      load: importPolicy.load
    }));
  }

  return sandbox;
}
@zenparsing zenparsing changed the title Post MVP feature: specifying import policy for new contexts Post MVP: Specifying import policy for new contexts Feb 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant