feat(learner): JS/TS exercise runner in a Web Worker with timeout and network blocked
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
752e44696f
commit
9ff5862382
5 files changed
+268
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import type { Feature } from "@stc/contracts";
|
||||
import { badRequest, body, json } from "./http";
|
||||
|
||||
const MAX = 200_000;
|
||||
const ts = new Bun.Transpiler({ loader: "ts", target: "browser" });
|
||||
|
||||
/** Type-strips TypeScript for the in-browser runner (the worker itself runs plain JavaScript). */
|
||||
export function transpileTs(code: string): string {
|
||||
return ts.transformSync(code);
|
||||
}
|
||||
|
||||
export function transpileRoutes(): Feature["routes"] {
|
||||
return {
|
||||
"/api/learner/transpile": {
|
||||
POST: async (req) => {
|
||||
const b = await body<{ code?: string }>(req);
|
||||
if (!b || typeof b.code !== "string") return badRequest("code is required");
|
||||
if (b.code.length > MAX) return badRequest("code is too long");
|
||||
try {
|
||||
return json({ code: transpileTs(b.code) });
|
||||
} catch (e) {
|
||||
return json({ error: (e as Error).message }, 422);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user