Vm2: Extending require() further

Created on 15 Jul 2016  ·  4Comments  ·  Source: patriksimek/vm2

I would like an option which is a function that takes vm2's require as an argument and returns a new function that should be used instead of vm2's require.

feature request stale

Most helpful comment

Somewhat relevant: I'm using a custom file system for user submitted code. Is there a way I can manually deal with local requires, like require('./otherUserSubmittedFile'), fetching the file from the custom file system and handing it over to vm2 to then sandbox?

Update:
Very rough, but have implemented something along the lines of custom/virtual files: https://github.com/bartjoyce/vm2-with-custom-fs

Usage, would be something like this:

NodeVM.file('/customfs/main.js', {
  require: {
    external: true,
    root: '/customfs/', // restrict local requires to within the custom fs
  },
  customfs: {
    getFileContents: (filename) => /* handle /customfs/ file read */,
    exists: (filename) => /* ... */,
    isDirectory: (filename) => /* ... */
  }
})

All 4 comments

Somewhat relevant: I'm using a custom file system for user submitted code. Is there a way I can manually deal with local requires, like require('./otherUserSubmittedFile'), fetching the file from the custom file system and handing it over to vm2 to then sandbox?

Update:
Very rough, but have implemented something along the lines of custom/virtual files: https://github.com/bartjoyce/vm2-with-custom-fs

Usage, would be something like this:

NodeVM.file('/customfs/main.js', {
  require: {
    external: true,
    root: '/customfs/', // restrict local requires to within the custom fs
  },
  customfs: {
    getFileContents: (filename) => /* handle /customfs/ file read */,
    exists: (filename) => /* ... */,
    isDirectory: (filename) => /* ... */
  }
})

@bartjoyce I need the same thing! Your link is broken, did you move it somewhere?

@callumlocke I've since moved on to other projects and have stopped maintaining the code. The code I wrote is here: https://github.com/bartjoyce/vm2/

It's 30 commits behind, but at least you can see the modifications I've made.

Important commit is this one: https://github.com/bartjoyce/vm2/commit/302e910610c0e1f1ef7a1d15a19a02828715f630

I've made additional changes to vm2 that are not in my repo. If you want to see those too, let me know.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

patriksimek picture patriksimek  ·  5Comments

unxcepted picture unxcepted  ·  11Comments

CapacitorSet picture CapacitorSet  ·  13Comments

patriksimek picture patriksimek  ·  15Comments

ozsay picture ozsay  ·  5Comments