Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENV NODE_VERSION 16

# Update and install dependencies, including Node 16 directly
RUN apt-get update && apt-get -y install gcc mono-mcs golang-go \
default-jre default-jdk python3-pip python3 curl && \
default-jre default-jdk python3-pip python3 curl fpc && \
curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && \
apt-get install -y nodejs && \
rm -rf /var/lib/apt/lists/*
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Whichever language you might mention in the language field, it would be automati
| GoLang | go |
| C# | cs |
| NodeJS | js |
| Pascal | pas |

More coming very soon!

Expand Down Expand Up @@ -119,6 +120,10 @@ This endpoint allows you to list all languages supported and their versions.
"language": "cs",
"info": "Mono C# compiler version 4.6.2.0\n"
}
{
"language": "pas",
"info": "fpc"
}
]
}
```
Expand Down
Empty file added logs/express.log
Empty file.
11 changes: 11 additions & 0 deletions src/file-system/removeCodeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const removeCodeFile = async (uuid, lang, outputExt) => {
console.error(`Failed to delete output file: ${outputFile}`, err);
}
}

if (lang === 'pas') {
const objectFile = join(OUTPUTS_DIR, `${uuid}.o`);
try {
if (require("fs").existsSync(objectFile)) {
unlinkSync(objectFile);
}
} catch (err) {
console.error(`Failed to delete object file: ${objectFile}`, err);
}
}
};

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ app.get('/list', async (req, res) => {
sendResponse(res, 200, {supportedLanguages: body})
})

app.listen(port);
app.listen(port,'0.0.0.0');
Empty file added src/logs/express.log
Empty file.
67 changes: 48 additions & 19 deletions src/run-code/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const {commandMap, supportedLanguages} = require("./instructions")
const {createCodeFile} = require("../file-system/createCodeFile")
const {removeCodeFile} = require("../file-system/removeCodeFile")
const {info} = require("./info")
const { commandMap, supportedLanguages } = require("./instructions")
const { createCodeFile } = require("../file-system/createCodeFile")
const { removeCodeFile } = require("../file-system/removeCodeFile")
const { info } = require("./info")

const {spawn} = require("child_process");
const { spawn } = require("child_process");

async function runCode({language = "", code = "", input = ""}) {
async function runCode({ language = "", code = "", input = "" }) {
const timeout = 30;

if (code === "")
Expand All @@ -20,30 +20,59 @@ async function runCode({language = "", code = "", input = ""}) {
error: `Please enter a valid language. Check documentation for more details: https://github.com/Jaagrav/CodeX-API#readme. The languages currently supported are: ${supportedLanguages.join(', ')}.`
}

const {jobID} = await createCodeFile(language, code);
const {compileCodeCommand, compilationArgs, executeCodeCommand, executionArgs, outputExt} = commandMap(jobID, language);
const { jobID } = await createCodeFile(language, code);
const { compileCodeCommand, compilationArgs, executeCodeCommand, executionArgs, outputExt } = commandMap(jobID, language);

if (compileCodeCommand) {
await new Promise((resolve, reject) => {
const compileCode = spawn(compileCodeCommand, compilationArgs || [])
compileCode.stderr.on('data', (error) => {
if (compileCodeCommand) {
await new Promise((resolve, reject) => {
const compileCode = spawn(compileCodeCommand, compilationArgs || [])
let compileError = "";

compileCode.stderr.on('data', (data) => {
const msg = data.toString();
compileError += msg; // Accumulate error output

if (language === 'pas' && msg.includes("link.res contains output sections")) {
return;
}

// Only reject on critical errors immediately
if (msg.includes("error:") || msg.includes("Error:")) {
reject({
status: 200,
output: '',
error: error.toString(),
error: compileError,
language
})
});
compileCode.on('exit', () => {
}
});

compileCode.on('close', (code) => {
if (code !== 0) {
reject({
status: 200,
output: '',
error: compileError,
language
})
} else {
resolve()
})
}
})
}
})
}

const result = await new Promise((resolve, reject) => {
const executeCode = spawn(executeCodeCommand, executionArgs || []);
let output = "", error = "";

executeCode.on('error', (err) => {
reject({
status: 500,
error: `Failed to execute code: ${err.message}`
});
});

const timer = setTimeout(async () => {
executeCode.kill("SIGHUP");

Expand Down Expand Up @@ -76,7 +105,7 @@ async function runCode({language = "", code = "", input = ""}) {

executeCode.on('exit', (err) => {
clearTimeout(timer);
resolve({output, error});
resolve({ output, error });
});
})

Expand All @@ -89,4 +118,4 @@ async function runCode({language = "", code = "", input = ""}) {
}
}

module.exports = {runCode}
module.exports = { runCode }
18 changes: 15 additions & 3 deletions src/run-code/instructions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {join} = require('path')
const { join } = require('path')

const CODES_DIR = process.env.CODES_DIR || "/tmp/codes";
const OUTPUTS_DIR = process.env.OUTPUTS_DIR || "/tmp/outputs";
Expand Down Expand Up @@ -76,9 +76,21 @@ const commandMap = (jobID, language) => {
outputExt: 'exe',
compilerInfoCommand: 'mcs --version'
}
case 'pas':
return {
compileCodeCommand: 'fpc',
compilationArgs: [
`${CODES_DIR}/${jobID}.pas`,
`-FE${OUTPUTS_DIR}`,
`-o${jobID}`
],
executeCodeCommand: `${OUTPUTS_DIR}/${jobID}`,
outputExt: '',
compilerInfoCommand: 'fpc -iV'
};
}
}

const supportedLanguages = ['java', 'cpp', 'py', 'c', 'js', 'go', 'cs'];
const supportedLanguages = ['java', 'cpp', 'py', 'c', 'js', 'go', 'cs','pas'];

module.exports = {commandMap, supportedLanguages}
module.exports = { commandMap, supportedLanguages }
Loading