diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-01-17 02:11:31 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-01-17 02:11:31 +0900 |
| commit | deacd0dfc195bca41af631114804d29937337cd8 (patch) | |
| tree | f1f83580e5bc892c0794ac41632bc0cce3498f65 /services/sandbox-exec/index.js | |
| parent | 38ddeb28ec846ee966d0fe6873585d697a9ef373 (diff) | |
| download | phperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.tar.gz phperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.tar.zst phperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.zip | |
.
Diffstat (limited to 'services/sandbox-exec/index.js')
| -rw-r--r-- | services/sandbox-exec/index.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/services/sandbox-exec/index.js b/services/sandbox-exec/index.js new file mode 100644 index 0000000..8c256ab --- /dev/null +++ b/services/sandbox-exec/index.js @@ -0,0 +1,38 @@ +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('./lib/exec.js'); + + proc.send({ code, input }); + + proc.on('message', (result) => { + resolve(result); + proc.kill(); + }); + + setTimeout(() => { + resolve({ + status: 'TLE', + stdout: '', + stderr: `Time Limit Exceeded: ${timeoutMsec} msec`, + }); + proc.kill(); + }, timeoutMsec); + }); +}; + +const app = new Hono(); + +app.post('/exec', async (c) => { + const { code, input, timeout } = await c.req.json(); + const result = await execPhp(code, input, timeout); + return c.json(result); +}); + +serve({ + fetch: app.fetch, + port: 8888, +}) |
