javascript sandbox
用途
https://github.com/gf3/sandbox
- Can be used to execute untrusted code.
- Support for timeouts (e.g. prevent infinite loops)
- Support for memory errors (and memory errors)
- Handles errors gracefully
- Restricted code (cannot access node.js methods)
- Supports
console.log
and- Supports interprocess messaging with the sandboxed code
例子
https://github.com/gf3/sandbox/blob/master/example/example.js
var Sandbox = require("../lib/sandbox")
, s = new Sandbox()
// Example 1 - Standard JS
s.run( "1 + 1", function( output ) {
console.log( "Example 1: " + output.result + "\n" )
})
通信
箱内和向外相互通信。
Sandboxed code:
onmessage = function(message){
if (message === 'hello from outside') {
postMessage('hello from inside');
}
};
Sandbox:
var sandbox = new Sandbox();
sandbox.run(sandboxed_code);
sandbox.on('message', function(message){
// Handle message sent from the inside
// In this example message will be 'hello from inside'
});
sandbox.postMessage('hello from outside');
// Example 11 - IPC Messaging s.run( "onmessage = function(message){ if (message === 'hello from outside') { postMessage('hello from inside'); };", function(output){ }) s.on('message', function(message){ console.log("Example 11: received message sent from inside the sandbox '" + message + "'\n") }); var test_message = "hello from outside"; console.log("Example 11: sending message into the sandbox '" + test_message + "'"); s.postMessage(test_message);
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)