node.js - Node : Is it possible to eval js code using runInNewContext and limit its execution time by a timeout?

标签 node.js

我想在 node.js 中使用 runInNewContext 执行不受信任的 js 代码,但据我所知,没有办法限制其执行时间。这也是一个同步操作。有没有办法设置它的超时或它的异步版本,让我可以从“外部”控制它的执行?

更新:在外部进程中运行是不好的:

  • 占用太多资源
  • 更重要的是,我需要代码通过沙箱环境访问我的数据/代码

最佳答案

使用 dnode 在外部进程中运行脚本或 child_process.fork , 设置截止时间计时器,如果超时则终止进程,如果脚本完成则设置计时器。

关于node.js - Node : Is it possible to eval js code using runInNewContext and limit its execution time by a timeout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6529342/

相关文章:

node.js - 使用 Express.js Node.js header (cURL) 中的 token

javascript - nodejs单线程?如果有很多并发请求,它是如何工作的

javascript - 如何从对象数组中获取数据?

javascript - 如果mongodb中存在相同的key,如何更新数据?

javascript - 在 extjs 项目中使用 Moment.js

javascript - Nodejs计时器不工作

node.js - Express 在浏览器中返回 200,但使用 curl -I 返回 404

node.js - vscode 通过扩展提供到 json 模式的链接

node.js - 如何将 package.json 中的每个依赖项更新到最新版本?

node.js - 将图像文件从React前端上传到Node/Express/Mongoose/MongoDB后端(不起作用)