Runno终极指南:在浏览器中安全运行代码的完整解决方案
【免费下载链接】runnoSandboxed runtime for programming languages and WASI binaries. Works in the browser, on your server, or via MCP.项目地址: https://gitcode.com/gh_mirrors/ru/runno
想要在浏览器中安全运行Python、Ruby、JavaScript等编程语言代码吗?Runno正是你需要的终极解决方案!这个基于WebAssembly的沙盒运行时让代码执行变得前所未有的简单和安全。无论你是教育工作者创建互动教程,开发者构建代码演示,还是需要在客户端安全执行用户代码,Runno都能提供完整的浏览器代码执行体验。
🚀 Runno是什么?
Runno是一个JavaScript包集合,用于在各种语言中运行沙盒化代码。它基于WebAssembly和WASI技术,提供了完全隔离的执行环境,让你可以在浏览器中安全运行代码而无需担心安全问题。
核心功能亮点:
- 🔒安全沙盒:基于WebAssembly的完全隔离环境
- 🌐浏览器原生:无需服务器,完全在客户端运行
- 📦多语言支持:Python、Ruby、JavaScript、C/C++、PHP、SQLite
- ⚡即用即得:简单的Web组件和API接口
🎯 Runno的三大核心组件
1. @runno/runtime - 浏览器端运行时
这是Runno最常用的部分,提供Web组件让你直接在HTML中嵌入可运行的代码示例:
<runno-run runtime="python" editor controls> print("Hello, World!") name = input("What's your name? ") print(f"Welcome {name}!") </runno-run>这个简单的组件就能创建一个完整的Python代码编辑器,用户可以直接在浏览器中运行代码并查看结果!
2. @runno/sandbox - Node.js沙盒
如果你需要在服务器端安全运行代码,@runno/sandbox是你的最佳选择:
import { runCode } from "@runno/sandbox"; const result = await runCode("python", 'print("Hello from Python!")'); console.log(result.stdout); // "Hello from Python!"这个包特别适合:
- 🔍 代码自动评分系统
- 🤖 AI生成的代码执行
- 🧪 用户提交代码的测试环境
3. @runno/wasi - WASI二进制执行
Runno还能直接运行WASI(WebAssembly System Interface)二进制文件:
import { WASI } from "@runno/wasi"; const result = WASI.start(fetch("/program.wasm"), { args: ["program", "--option", "value"], env: { KEY: "value" }, fs: { "/input.txt": { content: "File content here", mode: "string" } } });🛠️ 快速开始使用Runno
浏览器端集成(最简单的方式)
- 安装依赖
npm install @runno/runtime- 导入并初始化
import "@runno/runtime";- 在HTML中使用
<runno-run runtime="python" editor controls> # 这里是你的Python代码 print("Runno让代码运行变得简单!") </runno-run>服务器端使用
import { runCode } from "@runno/sandbox"; // 运行Python代码 const pythonResult = await runCode("python", ` def fibonacci(n): if n <= 1: return n return fibonacci(n-1) + fibonacci(n-2) print(fibonacci(10)) `); // 运行Ruby代码 const rubyResult = await runCode("ruby", 'puts "Hello from Ruby!"'); // 运行JavaScript代码 const jsResult = await runCode("quickjs", 'console.log("Hello from JS!")');🌟 Runno的独特优势
安全第一的设计理念
Runno的安全架构基于多层防护:
- WebAssembly隔离:代码在虚拟化环境中运行
- 虚拟文件系统:无法访问真实文件系统
- 无网络访问:完全离线运行
- 资源限制:可配置内存和时间限制
教育场景的完美匹配
Runno特别适合教育场景:
- 📚互动教程:学生可以直接在教程中运行代码
- 🎓在线考试:安全执行学生提交的代码
- 🧑💻编程练习:即时反馈学习效果
- 🔧代码演示:技术文档中的可运行示例
企业级应用场景
- 代码评审工具:安全运行同事的代码变更
- CI/CD管道:在隔离环境中测试构建
- 用户自定义脚本:让用户安全执行自定义逻辑
- 数据转换服务:在沙盒中处理敏感数据
📊 支持的语言和运行时
Runno目前支持以下编程语言:
| 语言 | 运行时 | 版本 | 特点 |
|---|---|---|---|
| Python | python | 3.11.3 | 完整的Python环境 |
| Ruby | ruby | 3.2.0 | 标准MRI Ruby |
| JavaScript | quickjs | WASMEdge | QuickJS引擎 |
| C语言 | clang | Binji编译 | C编译器 |
| C++语言 | clangpp | Binji编译 | C++编译器 |
| PHP | php-cgi | 8.2.0 | PHP CGI环境 |
| SQLite | sqlite | 最新版 | 数据库操作 |
🔧 高级功能详解
虚拟文件系统支持
Runno提供了完整的虚拟文件系统,让你的代码可以像在真实环境中一样操作文件:
<runno-run runtime="python" editor controls> # 读取虚拟文件 with open("/data.txt", "r") as f: content = f.read() print(f"File content: {content}") <runno-file path="/data.txt"> 这是虚拟文件的内容 可以有多行文本 </runno-file> </runno-run>输入输出处理
Runno完美支持标准输入输出:
<runno-run runtime="python" editor controls> # 交互式输入 name = input("请输入你的名字: ") age = int(input("请输入你的年龄: ")) print(f"你好 {name},你今年 {age} 岁!") print(f"10年后你将 {age + 10} 岁") </runno-run>超时控制
防止代码无限运行:
import { runCode } from "@runno/sandbox"; const result = await runCode("python", ` while True: pass # 无限循环 `, { timeout: 5 }); // 5秒超时 if (result.resultType === "timeout") { console.log("代码执行超时"); }🚀 实际应用案例
案例1:在线编程教育平台
// 学生提交的代码 const studentCode = ` def calculate_average(numbers): return sum(numbers) / len(numbers) numbers = [85, 90, 78, 92, 88] print(f"平均分: {calculate_average(numbers)}") `; // 在沙盒中安全执行 const result = await runCode("python", studentCode); // 检查输出 if (result.stdout.includes("平均分: 86.6")) { console.log("学生答案正确!"); } else { console.log("需要进一步指导"); }案例2:技术博客互动示例
<!-- 在技术博客中嵌入可运行代码 --> <article> <h2>Python列表推导式示例</h2> <p>下面是一个使用列表推导式的例子,你可以直接运行它:</p> <runno-run runtime="python" editor controls> # 使用列表推导式生成平方数 squares = [x**2 for x in range(10)] print(f"0-9的平方: {squares}") # 过滤偶数 evens = [x for x in range(20) if x % 2 == 0] print(f"0-19的偶数: {evens}") </runno-run> </article>案例3:API文档中的代码测试
// API文档中的代码示例 import { runCode } from "@runno/sandbox"; // 演示如何计算斐波那契数列 const fibonacciExample = ` def fibonacci(n, memo={}): if n in memo: return memo[n] if n <= 2: return 1 memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo) return memo[n] print(f"fibonacci(10) = {fibonacci(10)}") print(f"fibonacci(20) = {fibonacci(20)}") `; // 读者可以复制这段代码并在自己的环境中运行📈 性能与限制
性能特点
- ⚡快速启动:WebAssembly即时编译
- 💾内存高效:可控的内存使用
- 🔄并发安全:支持多个实例并行运行
当前限制
- 🌐无网络访问:代码无法进行网络请求
- 📁文件系统限制:只能访问虚拟文件系统
- 🧩模块限制:部分语言模块可能不可用
- ⏱️执行时间:适合短时间运行的代码
🎯 最佳实践指南
1. 为教育场景优化
<!-- 添加清晰的说明和提示 --> <div class="code-example"> <h3>Python基础:变量和类型</h3> <p>尝试修改下面的代码,看看会发生什么:</p> <runno-run runtime="python" editor controls> # 修改变量的值并运行 name = "Alice" age = 25 height = 1.68 print(f"姓名: {name}") print(f"年龄: {age}") print(f"身高: {height}米") # 尝试修改上面的值并重新运行 </runno-run> </div>2. 错误处理策略
import { runCode } from "@runno/sandbox"; async function safeCodeExecution(code, runtime = "python") { try { const result = await runCode(runtime, code, { timeout: 10, // 10秒超时 }); if (result.resultType === "complete") { return { success: true, output: result.stdout, error: result.stderr }; } else if (result.resultType === "timeout") { return { success: false, error: "代码执行超时" }; } else { return { success: false, error: "代码执行失败" }; } } catch (error) { return { success: false, error: `执行错误: ${error.message}` }; } }3. 资源管理
// 控制资源使用 const resourceOptions = { timeout: 30, // 30秒超时 // 可以添加更多资源限制选项 }; // 批量处理时限制并发 const MAX_CONCURRENT = 3; const semaphore = new Semaphore(MAX_CONCURRENT); async function processMultipleCodes(codeList) { const results = []; for (const code of codeList) { await semaphore.acquire(); try { const result = await runCode("python", code, resourceOptions); results.push(result); } finally { semaphore.release(); } } return results; }🔮 未来展望
Runno正在不断进化,未来计划包括:
- 更多语言支持:扩展支持Go、Rust等现代语言
- 性能优化:更快的启动时间和更低的内存占用
- 增强安全性:更细粒度的权限控制
- 云集成:与云服务的无缝集成
- 开发者工具:更好的调试和分析工具
🏁 开始使用Runno
快速安装
# 浏览器端使用 npm install @runno/runtime # 服务器端使用 npm install @runno/sandbox # WASI支持 npm install @runno/wasi配置HTTP头
为了让Runno在浏览器中正常工作,需要设置以下HTTP头:
Cross-Origin-Opener-Policy: same-origin Cross-Origin-Embedder-Policy: require-corp查看完整文档
访问项目的官方文档获取更多详细信息:docs/official.md
💡 小贴士
- 从简单开始:先尝试基本的代码示例,再逐步增加复杂度
- 测试边界情况:确保你的代码在各种输入下都能正常工作
- 利用虚拟文件系统:对于需要文件操作的场景特别有用
- 监控资源使用:长时间运行的代码需要适当超时设置
- 保持更新:定期更新Runno包以获得最新功能和安全修复
Runno为在浏览器中安全运行代码提供了完整的解决方案,无论是教育、演示还是生产环境,它都能提供可靠、安全的代码执行能力。立即开始使用Runno,让你的网页应用拥有强大的代码执行能力!
【免费下载链接】runnoSandboxed runtime for programming languages and WASI binaries. Works in the browser, on your server, or via MCP.项目地址: https://gitcode.com/gh_mirrors/ru/runno
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考