aboutsummaryrefslogtreecommitdiffhomepage
path: root/worker/php/index.mjs
blob: c2bd818c151173782a9510ddb52104d181d74af9 (plain)
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
import { fork } from "node:child_process";
import { serve } from "@hono/node-server";
import { Hono } from "hono";

const execPhp = (code, input, timeoutMsec) => {
  return new Promise((resolve, _reject) => {
    const proc = fork("./exec.mjs");

    proc.send({ code, input });

    proc.on("message", (result) => {
      resolve(result);
      proc.kill();
    });

    setTimeout(() => {
      resolve({
        status: "timeout",
        stdout: "",
        stderr: `Time Limit Exceeded: ${timeoutMsec} msec`,
      });
      proc.kill();
    }, timeoutMsec);
  });
};

const app = new Hono();

app.post("/exec", async (c) => {
  console.log("worker/exec");
  const { code, stdin, max_duration_ms } = await c.req.json();
  const result = await execPhp(code, stdin, max_duration_ms);
  return c.json(result);
});

serve({
  fetch: app.fetch,
  port: 80,
});