|
1 | 1 | import express from 'express'; |
2 | 2 | // import cors from 'cors'; |
3 | 3 | import { exec as rawExec } from 'child_process'; |
| 4 | +import { performance } from 'perf_hooks'; |
4 | 5 | import util from 'util'; |
5 | 6 | import fs from 'fs'; |
6 | 7 | import { fileURLToPath } from 'url'; |
@@ -244,34 +245,51 @@ const clean = s => |
244 | 245 |
|
245 | 246 | /* ---------- ONE handler used by both endpoints ---------- */ |
246 | 247 | async function runCode(req, res) { |
| 248 | + const startTime = performance.now(); |
| 249 | + console.log('Received request for /runCode'); |
| 250 | + |
247 | 251 | const { code = '', language = '' } = req.body || {}; |
248 | 252 |
|
249 | | - /* 1. validate language ------------------------------------------------ */ |
| 253 | + /* 1. Validate language */ |
250 | 254 | const cfg = languageConfigs[language]; |
251 | | - if (!cfg) return res.status(400).json({ error: 'Unsupported language' }); |
| 255 | + if (!cfg) { |
| 256 | + console.log('Unsupported language received:', language); |
| 257 | + return res.status(400).json({ error: 'Unsupported language' }); |
| 258 | + } |
| 259 | + console.log(`Language validated: ${language}`); |
252 | 260 |
|
253 | | - /* 2. write code to its temp file -------------------------------------- */ |
254 | | - await fs.promises.mkdir(tempDir, { recursive: true }); |
255 | | - await fs.promises.writeFile(cfg.filePath, code); |
| 261 | + try { |
| 262 | + /* 2. Write code to its temp file */ |
| 263 | + await fs.promises.mkdir(tempDir, { recursive: true }); |
| 264 | + await fs.promises.writeFile(cfg.filePath, code); |
| 265 | + const writeTime = performance.now(); |
| 266 | + console.log(`File written in ${((writeTime - startTime) / 1000).toFixed(2)}s`); |
256 | 267 |
|
257 | | - /* 3. build the shell command ----------------------------------------- */ |
258 | | - const cmd = buildCommand(cfg.filePath, language); |
| 268 | + /* 3. Build the shell command */ |
| 269 | + const cmd = buildCommand(cfg.filePath, language); |
| 270 | + console.log(`Executing command: ${cmd}`); |
259 | 271 |
|
260 | | - /* 4. exec with hard limits ------------------------------------------- */ |
261 | | - try { |
| 272 | + /* 4. Exec with hard limits */ |
262 | 273 | const { stdout, stderr } = await exec(cmd, { |
263 | | - timeout: 10_000, // ⏱️ kill after 10 s |
264 | | - maxBuffer: 1_048_576 // 1 MiB stdout+stderr |
| 274 | + timeout: 15000, // Increased to 15s |
| 275 | + maxBuffer: 1024 * 1024 // 1 MiB |
265 | 276 | }); |
| 277 | + |
| 278 | + const execTime = performance.now(); |
| 279 | + console.log(`Command executed in ${((execTime - writeTime) / 1000).toFixed(2)}s`); |
266 | 280 |
|
267 | 281 | return res.json({ |
268 | 282 | output: stdout.trim(), |
269 | 283 | executionTime: pick(stderr, /Execution Time:\s*(.+?)\s*seconds/), |
270 | 284 | memoryUsage: pick(stderr, /Memory Used:\s*(.+?)\s*KB/) |
271 | 285 | }); |
| 286 | + |
272 | 287 | } catch (err) { |
273 | | - /* timeout → err.killed === true, compile/runtime error → err.code */ |
274 | | - const status = err.killed ? 408 : 400; |
| 288 | + const errorTime = performance.now(); |
| 289 | + console.error(`Error after ${((errorTime - startTime) / 1000).toFixed(2)}s:`, err); |
| 290 | + |
| 291 | + /* Timeout → err.killed === true, compile/runtime error → err.code */ |
| 292 | + const status = err.killed ? 408 : 400; // 408 for Request Timeout |
275 | 293 | const stderr = err.stderr || err.message || ''; |
276 | 294 | return res.status(status).json({ error: clean(stderr) }); |
277 | 295 | } |
|
0 commit comments