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

fix: pass vm scrip options through #2238

Merged
merged 4 commits into from
Sep 30, 2018
Merged

fix: pass vm scrip options through #2238

merged 4 commits into from
Sep 30, 2018

Conversation

SimenB
Copy link
Contributor

@SimenB SimenB commented May 21, 2018

In order to prevent scripts from potentially running forever, it'd be great to be able to pass a timeout to it.

Docs: https://nodejs.org/api/vm.html#vm_script_runincontext_contextifiedsandbox_options

@TimothyGu
Copy link
Member

Can you update the corresponding documentation in README?

@SimenB
Copy link
Contributor Author

SimenB commented May 22, 2018

Done! 🙂

@domenic
Copy link
Member

domenic commented May 22, 2018

Why not use the options argument to the Script constructor?

@SimenB
Copy link
Contributor Author

SimenB commented May 22, 2018

Oh, that's a very good point! That works for my use case then. Feel free to close if you don't want this 🙂

@SimenB
Copy link
Contributor Author

SimenB commented May 23, 2018

@domenic actually, that doesn't seem to work.

This snippet hangs forever:

const vm = require('vm');

const script = new vm.Script('while(true){}', {timeout: 1000});

script.runInContext(vm.createContext());

While this snippet throws a timeout error:

const vm = require('vm');

const script = new vm.Script('while(true){}');

script.runInContext(vm.createContext(), {timeout: 1000});

Not sure if it's a bug in Node, or something with how scripts are executed.

This is running node 8.11.1

@SimenB
Copy link
Contributor Author

SimenB commented May 23, 2018

To my eyes it looks like new Script doesn't care about the timeout, and the docs are wrong:

https://github.com/nodejs/node/blob/9461f327f50c9885508392938053a62985d13259/lib/vm.js#L45-L52

@TimothyGu
Copy link
Member

@SimenB Could you file an issue at https://github.com/nodejs/node? I'd be happy to look at it over there!

@SimenB
Copy link
Contributor Author

SimenB commented May 26, 2018

Done! nodejs/node#20982

@SimenB
Copy link
Contributor Author

SimenB commented May 26, 2018

This is apparently faulty docs, so the PR is back on the table, I think :)

Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test to the runVMScript tests?

README.md Outdated
@@ -282,6 +282,8 @@ dom.window.ran === 3;

This is somewhat-advanced functionality, and we advise sticking to normal DOM APIs (such as `window.eval()` or `document.createElement("script")`) unless you have very specific needs.

`runVMScript` also takes an `options` object as its second argument. See the [Node docs](https://nodejs.org/api/vm.html#vm_script_runincontext_contextifiedsandbox_options) for details.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits:

  • I've tried to stick with adding parens for function/method names, so runVMScript().
  • I've tried to stick to Node.js instead of Node.

Also maybe we should update the header to runVMScript(script[, options])? Dunno, could go either way.

@SimenB
Copy link
Contributor Author

SimenB commented Sep 19, 2018

@domenic sorry, I completely forgot about this! I've rebased, addressed your comments on the docs and added a test 🙂

Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, thank you!

@domenic domenic merged commit c294f50 into jsdom:master Sep 30, 2018
@SimenB SimenB deleted the patch-1 branch September 30, 2018 08:32
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

Successfully merging this pull request may close these issues.

3 participants