Spaces:
Sleeping
Sleeping
File size: 1,087 Bytes
05c5ed5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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);
};
|