Skip to content

Commit acd1f8b

Browse files
author
shashankiifetech
committed
2 parents cb840f4 + 1ead0df commit acd1f8b

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

server.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express';
22
// import cors from 'cors';
33
import { exec as rawExec } from 'child_process';
4+
import { performance } from 'perf_hooks';
45
import util from 'util';
56
import fs from 'fs';
67
import { fileURLToPath } from 'url';
@@ -244,34 +245,51 @@ const clean = s =>
244245

245246
/* ---------- ONE handler used by both endpoints ---------- */
246247
async function runCode(req, res) {
248+
const startTime = performance.now();
249+
console.log('Received request for /runCode');
250+
247251
const { code = '', language = '' } = req.body || {};
248252

249-
/* 1. validate language ------------------------------------------------ */
253+
/* 1. Validate language */
250254
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}`);
252260

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`);
256267

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}`);
259271

260-
/* 4. exec with hard limits ------------------------------------------- */
261-
try {
272+
/* 4. Exec with hard limits */
262273
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
265276
});
277+
278+
const execTime = performance.now();
279+
console.log(`Command executed in ${((execTime - writeTime) / 1000).toFixed(2)}s`);
266280

267281
return res.json({
268282
output: stdout.trim(),
269283
executionTime: pick(stderr, /Execution Time:\s*(.+?)\s*seconds/),
270284
memoryUsage: pick(stderr, /Memory Used:\s*(.+?)\s*KB/)
271285
});
286+
272287
} 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
275293
const stderr = err.stderr || err.message || '';
276294
return res.status(status).json({ error: clean(stderr) });
277295
}

0 commit comments

Comments
 (0)