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
.
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.
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 tovm2
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: