Spaces:
Runtime error
Runtime error
Download src/lib/code-runner/worker.ts from shyota/better-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/shyota/better-chatbot/resolve/main/src/lib/code-runner/worker.ts
- Command line
-
hf download hf://spaces/shyota/better-chatbot/src/lib/code-runner/worker.ts
-
curl -L -o worker.ts https://huggingface.co/spaces/shyota/better-chatbot/resolve/main/src/lib/code-runner/worker.ts
1.09 kB
| import { | |
| CodeRunnerResult, | |
| CodeWorkerEvent, | |
| CodeWorkerRequest, | |
| CodeWorkerResult, | |
| } from "./code-runner.interface"; | |
| import { safeJsRun } from "./safe-js-run"; | |
| import { safePythonRun } from "./safe-python-run"; | |
| self.onmessage = async (event) => { | |
| const { code, type, timeout = 300000, id } = event.data as CodeWorkerRequest; | |
| const engine = type == "javascript" ? safeJsRun : safePythonRun; | |
| const result = await engine({ | |
| code, | |
| timeout, | |
| onLog(entry) { | |
| const logEvent: CodeWorkerEvent = { | |
| id, | |
| type: "log", | |
| entry, | |
| }; | |
| self.postMessage(logEvent); | |
| }, | |
| }).catch((error) => { | |
| const errorResult: CodeRunnerResult = { | |
| success: false, | |
| logs: [ | |
| { | |
| type: "error", | |
| args: [ | |
| { | |
| type: "data", | |
| value: error.message, | |
| }, | |
| ], | |
| }, | |
| ], | |
| error: error.message, | |
| }; | |
| return errorResult; | |
| }); | |
| const resultEvent: CodeWorkerResult = { | |
| id, | |
| type: "result", | |
| result, | |
| }; | |
| self.postMessage(resultEvent); | |
| }; | |