Skip to content
Open
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
28 changes: 17 additions & 11 deletions src/run-code/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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 = ""}) {
// this id is used to run the code as a non-root user
const ID = 1000;

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

if (code === "")
Expand All @@ -20,8 +23,8 @@ 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) => {
Expand All @@ -41,7 +44,10 @@ async function runCode({language = "", code = "", input = ""}) {
}

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

const timer = setTimeout(async () => {
Expand Down Expand Up @@ -76,7 +82,7 @@ async function runCode({language = "", code = "", input = ""}) {

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

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

module.exports = {runCode}
module.exports = { runCode }