From 5ef0872d0354040a69e191a5650f8b9e1295d46c Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 20 Dec 2020 23:40:48 +0530 Subject: [PATCH 01/13] Added Promises --- index.js | 50 ++++++++++++++++++++++++++++-------------- utils/file-executor.js | 40 +++++++++++++++++++++++++++------ 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index ace0146..8035687 100644 --- a/index.js +++ b/index.js @@ -1,21 +1,39 @@ -const { execute } = require("./utils/file-executor.js"); -const {exec} = require("child_process"); -const fs = require("fs"); +const { execPromise, writeFilePromise, deleteFilePromise } = require("./utils/file-executor.js"); const data = `console.log("Hello World!!!");`; -fs.writeFile("temp/code.js", data, (err) => { - if (err) console.log(err); - console.log("Successfully Written to File."); - exec("node temp/code.js", function(error, stdout, stderr) { - console.log(error); - console.log(stdout); - console.log(stderr); +// fs.writeFile("temp/code.js", data, (err) => { +// if (err) console.log(err); +// console.log("Successfully Written to File."); +// exec("node temp/code.js", function(error, stdout, stderr) { +// console.log(error); +// console.log(stdout); +// console.log(stderr); - fs.unlink("temp/code.js", () => { - console.log("File Deleted!!!") - }); - }); -}); -// execute(); \ No newline at end of file +// fs.unlink("temp/code.js", () => { +// console.log("File Deleted!!!") +// }); +// }); +// }); + +const test = () => { + try { + await writeFilePromise("temp/code.js", data); + const output = await execPromise("node temp/code.js"); + console.log(output); + await deleteFilePromise("temp/code.js"); + } catch(err) { + console.log(err); + } + + // writeFilePromise("temp/code.js", data) + // .then(() => execPromise("node temp/code.js")) + // .then((output) => { + // console.log(output); + // return deleteFilePromise("temp/code.js"); + // }) + // .catch((err) => console.log(err)) +} + +test(); \ No newline at end of file diff --git a/utils/file-executor.js b/utils/file-executor.js index 9b3b2fc..64c2092 100644 --- a/utils/file-executor.js +++ b/utils/file-executor.js @@ -1,11 +1,37 @@ const { exec } = require('child_process'); +const fs = require("fs"); -const execute = () => { - exec("node temp/code.js", function(error, stdout, stderr) { - console.log(error); - console.log(stdout); - console.log(stderr) +// const execute = () => { +// exec("node temp/code.js", function(error, stdout, stderr) { +// console.log(error); +// console.log(stdout); +// console.log(stderr) +// }); +// } + +const execPromise = (command) => new Promise((resolve, reject) => { + exec(command, function(error, stdout, stderr) { + if(error) reject(error); + else { + const isError = Boolean(stderr); + resolve({ isError, output: stdout || stderr }); + } }); -} +}); + +const writeFilePromise = (location, data) => new Promise((resolve, reject) => { + fs.writeFile(location, data, function(err) { + if(err) reject(err); + else resolve(); + }) +}); + +const deleteFilePromise = (location) => new Promise((resolve, reject) => { + fs.unlink(location, (err) => { + if(err) reject(err); + else resolve(); + }) +}) + -module.exports = { execute }; \ No newline at end of file +module.exports = { execPromise, writeFilePromise, deleteFilePromise }; \ No newline at end of file From 1bbcb4cd783ca9a918eb939e4999f5ecdaf4cd78 Mon Sep 17 00:00:00 2001 From: Poorvi Vaish Date: Tue, 29 Dec 2020 14:54:03 +0530 Subject: [PATCH 02/13] Added first API --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 5ed7716..2445ff8 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ app.post("/execNode", async(req, res)=>{ const data = req.body.code; // console.log(data); const output = await execNode(data); - // console.log(output); + console.log(output); res.send(output); }) app.listen(3000, ()=> From d13aabf92cf56bfd15e644eb9af0a88a3af68784 Mon Sep 17 00:00:00 2001 From: Poorvi Vaish Date: Sun, 3 Jan 2021 23:50:51 +0530 Subject: [PATCH 03/13] Added Dockerfile --- .dockerignore | 2 ++ Dockerfile | 15 +++++++++++++++ index.js | 22 ++++++++++++++++++++-- utils/executor.js | 36 +++++++++++++++++++++++++++++++++--- utils/temp/code.js | 1 - utils/temp/code.py | 6 ++++++ 6 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile delete mode 100644 utils/temp/code.js create mode 100644 utils/temp/code.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..5171c54 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +npm-debug.log \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..013bbd7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM node:12.18 + +WORKDIR /rce + +RUN apt-get install python3 -y +RUN apt-get update && \ + apt-get -y install gcc mono-mcs && \ + rm -rf /var/lib/apt/lists/* + +copy . . +RUN npm install +ENV PORT=8080 +EXPOSE 8080 + +CMD [ "node", "index.js" ] \ No newline at end of file diff --git a/index.js b/index.js index 2445ff8..11be3aa 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ const express = require('express'); const cors = require('cors'); -const { execNode } = require('./utils/executor'); +const { execNode, execPython, execCpp } = require('./utils/executor'); const app = express(); +const PORT = process.env.PORT || 3000; app.use(express.json()); app.use(cors()); @@ -19,7 +20,24 @@ app.post("/execNode", async(req, res)=>{ console.log(output); res.send(output); }) -app.listen(3000, ()=> +app.post("/execPython", async(req, res)=>{ + + const data = req.body.code; + // console.log(data); + const output = await execPython(data); + console.log(output); + res.send(output); +}) + +app.post("/execCpp", async(req, res)=>{ + + const data = req.body.code; + // console.log(data); + const output = await execCpp(data); + console.log(output); + res.send(output); +}) +app.listen(PORT, ()=> { console.log("Server started!!"); }); \ No newline at end of file diff --git a/utils/executor.js b/utils/executor.js index 95b3536..48381bb 100644 --- a/utils/executor.js +++ b/utils/executor.js @@ -2,10 +2,11 @@ const { execPromise, writeFilePromise, deleteFilePromise } = require("./file-executor.js"); const execNode = async (data) => { - const filePath = __dirname + "/temp/code.js" + const filename = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); + const filePath = __dirname + `/temp/${filename}.js` try { await writeFilePromise(filePath, data); - const output = await execPromise("node utils/temp/code.js"); + const output = await execPromise(`node utils/temp/${filename}.js`); // console.log(output); await deleteFilePromise(filePath); return output; @@ -14,4 +15,33 @@ const execNode = async (data) => { return err; } } -module.exports = { execNode } \ No newline at end of file +const execCpp = async (data) => { + const filename = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); + const filePath = __dirname + `/temp/${filename}.cpp` +try { + await writeFilePromise(filePath, data); + const output = await execPromise(`g++ -o utils/temp/${filename} utils/temp/${filename}.cpp && ./utils/temp/${filename}`); + // console.log(output); + await deleteFilePromise(filePath); + await deleteFilePromise(`./utils/temp/${filename}`); + return output; +} catch(err) { + // console.log(err); + return err; +} +} +const execPython = async (data) => { + const filename = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); + const filePath = __dirname + `/temp/${filename}.py` +try { + await writeFilePromise(filePath, data); + const output = await execPromise(`python3 utils/temp/${filename}.py`); + // console.log(output); + await deleteFilePromise(filePath); + return output; +} catch(err) { + // console.log(err); + return err; +} +} +module.exports = { execNode, execPython, execCpp } \ No newline at end of file diff --git a/utils/temp/code.js b/utils/temp/code.js deleted file mode 100644 index 24dc37f..0000000 --- a/utils/temp/code.js +++ /dev/null @@ -1 +0,0 @@ -console.log("22); \ No newline at end of file diff --git a/utils/temp/code.py b/utils/temp/code.py new file mode 100644 index 0000000..67239e7 --- /dev/null +++ b/utils/temp/code.py @@ -0,0 +1,6 @@ +#include + #include + using namespace std; + int main(){ + std::cout<<"helloo world"; + return 0;} \ No newline at end of file From a5b0a50461a2438654cf1bd42262f4e40362f77d Mon Sep 17 00:00:00 2001 From: Poorvi Vaish Date: Sun, 31 Jan 2021 00:31:31 +0530 Subject: [PATCH 04/13] Error handling --- index.js | 1 + utils/file-executor.js | 6 +++--- utils/temp/hdyjo.py | 1 + utils/temp/ncqzo.py | 1 + utils/temp/nmtrv.py | 1 + utils/temp/vxoyc.py | 1 + 6 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 utils/temp/hdyjo.py create mode 100644 utils/temp/ncqzo.py create mode 100644 utils/temp/nmtrv.py create mode 100644 utils/temp/vxoyc.py diff --git a/index.js b/index.js index 11be3aa..d269f3e 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,7 @@ app.post("/execPython", async(req, res)=>{ // console.log(data); const output = await execPython(data); console.log(output); + res.send(output); }) diff --git a/utils/file-executor.js b/utils/file-executor.js index 5eea4b1..e90d60a 100644 --- a/utils/file-executor.js +++ b/utils/file-executor.js @@ -4,8 +4,8 @@ const fs = require("fs"); const execPromise = (command) => new Promise((resolve, reject) => { exec(command, function(error, stdout, stderr) { - // console.log(stdout, stderr,error); - if(error) reject(error); + // console.log(stdout, stderr, error); + if(error) reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); else { const isError = Boolean(stderr); resolve({ isError, output: stdout || stderr }); @@ -28,4 +28,4 @@ const deleteFilePromise = (location) => new Promise((resolve, reject) => { }) -module.exports = { execPromise, writeFilePromise, deleteFilePromise }; \ No newline at end of file +module.exports = { execPromise, writeFilePromise, deleteFilePromise }; \ No newline at end of file diff --git a/utils/temp/hdyjo.py b/utils/temp/hdyjo.py new file mode 100644 index 0000000..b746df4 --- /dev/null +++ b/utils/temp/hdyjo.py @@ -0,0 +1 @@ +print"helloooo" \ No newline at end of file diff --git a/utils/temp/ncqzo.py b/utils/temp/ncqzo.py new file mode 100644 index 0000000..b746df4 --- /dev/null +++ b/utils/temp/ncqzo.py @@ -0,0 +1 @@ +print"helloooo" \ No newline at end of file diff --git a/utils/temp/nmtrv.py b/utils/temp/nmtrv.py new file mode 100644 index 0000000..75484fa --- /dev/null +++ b/utils/temp/nmtrv.py @@ -0,0 +1 @@ +print("helloooo ) \ No newline at end of file diff --git a/utils/temp/vxoyc.py b/utils/temp/vxoyc.py new file mode 100644 index 0000000..b746df4 --- /dev/null +++ b/utils/temp/vxoyc.py @@ -0,0 +1 @@ +print"helloooo" \ No newline at end of file From 0b5ab51d7cca13a25bed714509f75358ce0ff197 Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 31 Jan 2021 11:52:10 +0530 Subject: [PATCH 05/13] Create deploy.yml --- .github/workflows/deploy.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..fbd7397 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,32 @@ +on: + release: + types: [created] + +name: Deploy to Production + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Push to Docker Hub + uses: docker/build-push-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + repository: poorvivaish/rce + tags: latest + + - name: Setup key + id: setup-key + env: + EC2_KEY: ${{ secrets.EC2_KEY }} + run: | + echo "$EC2_KEY" >> $HOME/key.pem + chmod 400 $HOME/key.pem + - name: Update image on server + run: ssh -i $HOME/key.pem -o StrictHostKeyChecking=no ubuntu@52.206.192.83 './deploy.sh' From e850a1219293be5ed5386a07d4a6de7b78328226 Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 31 Jan 2021 15:34:14 +0530 Subject: [PATCH 06/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index fbd7397..2685cb9 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -29,4 +29,4 @@ jobs: echo "$EC2_KEY" >> $HOME/key.pem chmod 400 $HOME/key.pem - name: Update image on server - run: ssh -i $HOME/key.pem -o StrictHostKeyChecking=no ubuntu@52.206.192.83 './deploy.sh' + run: ssh -i $HOME/key.pem -o StrictHostKeyChecking=no ubuntu@3.139.253.83 './deploy.sh' From 03cbd10198a07b0b4fb994627c3fd71d83edd3b5 Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 31 Jan 2021 15:44:50 +0530 Subject: [PATCH 07/13] Update file-executor.js --- utils/file-executor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/file-executor.js b/utils/file-executor.js index e90d60a..85a5a31 100644 --- a/utils/file-executor.js +++ b/utils/file-executor.js @@ -3,7 +3,7 @@ const fs = require("fs"); const execPromise = (command) => new Promise((resolve, reject) => { - exec(command, function(error, stdout, stderr) { + exec(command, { timeout: 2000, maxBuffer: 1024 * 1024 * 5 }, function(error, stdout, stderr) { // console.log(stdout, stderr, error); if(error) reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); else { @@ -28,4 +28,4 @@ const deleteFilePromise = (location) => new Promise((resolve, reject) => { }) -module.exports = { execPromise, writeFilePromise, deleteFilePromise }; \ No newline at end of file +module.exports = { execPromise, writeFilePromise, deleteFilePromise }; From c770550ce12b9f52a7ad8cf3ec84c572c39067fb Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 31 Jan 2021 16:47:56 +0530 Subject: [PATCH 08/13] Update README.md --- README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b0c0d50..e3dadc1 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,62 @@ # Remote-Code-Executor -RCE is a Remote Code Executor, as the name suggests Is a Docker-based sandbox environment to run a code snippet. It will create a new docker based container for every submitted code, run it in the isolated container, and return the output. It will support major languages C, C++, and can be extended to other language support too. +RCE is a Remote Code Executor, as the name suggests Is a Docker-based sandbox environment to run a code snippet. It will create a new file for each code input, execute it, delete the file and return the output. It supports major languages, i.e., C++, JavaScript and Python, and can be extended to other language support too. # Features: -- Backend APIs and logic to handle the submitted code, create a docker container, execute it, and return the results. -- Minimal UI for user interaction and code submission. +- Backend APIs and logic to handle the submitted code, execute it, and return the results. - Can be extended to an Online Code Judge and full-fledged coding/interview platform. # Tech stack: - Node.js -- Any Frontend framework or Basic HTML - Javascript - Docker -- Bash scripting. +- Bash scripting +- Nginx +- Github Actions + +# Concepts Used +- Frontend and Backend were made separate for making it possible to scale them individually. +- exec function from `child-processes` package in NodeJS was used to execute programs. +- Application was containerized and is present on DockerHub, so that it can be used anywhere. +- Server has a Nginx Web Server front, for high performance and stability. +- HTTPS for the server was made possible by using Certbot and Lets Encrypt. +- Concepts of Functional Programming was used to make code more readable and modular. +- A CI/CD pipeline was setup using Github Actions for Quick Deployment. + +# Steps to Run Locally +- Clone the Repo on your local computer +- Run `npm install` to install all the packages +```bash +npm install +``` +- Run `npm start` to start the server locally +```bash +npm start +``` +- Now you can view the server live at `http://localhost:3000` + +# APIs +## `/execNode`, `/execCpp`, `execPython` +### Request Format +```json +{ + "code": "console.log(\"Hello World\");" +} +``` + +### Response Format +```json +{ + "isError": "true or false based on the code", + "output": "EXECUTION OUTPUT or ERROR", + "message": "Error Message if there was any" +} +``` + +# Basic Architecture +Basic Architecture + +# CI/CD Flow +CI CD Flow + +# Hosted Link +The server is hosted and live at [rce.manish.codes](https://rce.manish.codes) From 900d55752cf63b46a90b910d33b896f2140a3434 Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 31 Jan 2021 18:50:16 +0530 Subject: [PATCH 09/13] Fixed C++ Execution Problem --- index.js | 40 ++++++++--------- package.json | 2 +- utils/executor.js | 98 +++++++++++++++++++++++++----------------- utils/file-executor.js | 48 ++++++++++++--------- utils/temp/bdqvs.js | 1 + utils/temp/cimvq.js | 2 + utils/temp/fiuqs.js | 1 + utils/temp/gwedq.py | 2 + utils/temp/juteb.cpp | 2 + utils/temp/kyezm.cpp | 2 + utils/temp/ljrgb.cpp | 9 ++++ utils/temp/odebx.cpp | 2 + utils/temp/pleah.js | 2 + utils/temp/qopvm.py | 2 + utils/temp/rupwx.cpp | 2 + utils/temp/trarb.py | 2 + utils/temp/ucfvm.py | 2 + utils/temp/usjsp.js | 1 + utils/temp/zuqlk.js | 2 + 19 files changed, 139 insertions(+), 83 deletions(-) create mode 100644 utils/temp/bdqvs.js create mode 100644 utils/temp/cimvq.js create mode 100644 utils/temp/fiuqs.js create mode 100644 utils/temp/gwedq.py create mode 100644 utils/temp/juteb.cpp create mode 100644 utils/temp/kyezm.cpp create mode 100644 utils/temp/ljrgb.cpp create mode 100644 utils/temp/odebx.cpp create mode 100644 utils/temp/pleah.js create mode 100644 utils/temp/qopvm.py create mode 100644 utils/temp/rupwx.cpp create mode 100644 utils/temp/trarb.py create mode 100644 utils/temp/ucfvm.py create mode 100644 utils/temp/usjsp.js create mode 100644 utils/temp/zuqlk.js diff --git a/index.js b/index.js index d269f3e..62c3d7a 100644 --- a/index.js +++ b/index.js @@ -1,44 +1,42 @@ -const express = require('express'); -const cors = require('cors'); -const { execNode, execPython, execCpp } = require('./utils/executor'); +const express = require("express"); +const cors = require("cors"); +const { execNode, execPython, execCpp } = require("./utils/executor"); const app = express(); const PORT = process.env.PORT || 3000; app.use(express.json()); app.use(cors()); -app.use(express.urlencoded({ - extended: true -})) -app.get('/', (req, res) => { - res.send("First server!!") +app.use( + express.urlencoded({ + extended: true, + }) +); +app.get("/", (req, res) => { + res.send("First server!!"); }); -app.post("/execNode", async(req, res)=>{ - +app.post("/execNode", async (req, res) => { const data = req.body.code; // console.log(data); const output = await execNode(data); console.log(output); res.send(output); -}) -app.post("/execPython", async(req, res)=>{ - +}); +app.post("/execPython", async (req, res) => { const data = req.body.code; // console.log(data); const output = await execPython(data); console.log(output); - + res.send(output); -}) +}); -app.post("/execCpp", async(req, res)=>{ - +app.post("/execCpp", async (req, res) => { const data = req.body.code; // console.log(data); const output = await execCpp(data); console.log(output); res.send(output); -}) -app.listen(PORT, ()=> -{ +}); +app.listen(PORT, () => { console.log("Server started!!"); -}); \ No newline at end of file +}); diff --git a/package.json b/package.json index 7e1e2ec..cb6d314 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "nodemon index.js" + "start": "node index.js" }, "repository": { "type": "git", diff --git a/utils/executor.js b/utils/executor.js index 48381bb..3567487 100644 --- a/utils/executor.js +++ b/utils/executor.js @@ -1,47 +1,65 @@ +const { + execPromise, + writeFilePromise, + deleteFilePromise, +} = require("./file-executor.js"); -const { execPromise, writeFilePromise, deleteFilePromise } = require("./file-executor.js"); - -const execNode = async (data) => { - const filename = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); - const filePath = __dirname + `/temp/${filename}.js` +const execNode = async (data) => { + const filename = Math.random() + .toString(36) + .replace(/[^a-z]+/g, "") + .substr(0, 5); + const filePath = __dirname + `/temp/${filename}.js`; + try { + await writeFilePromise(filePath, data); + const output = await execPromise(`node utils/temp/${filename}.js`, { + timeout: 2000, + maxBuffer: 1024 * 1024 * 5, + }); + await deleteFilePromise(filePath); + return output; + } catch (err) { + return err; + } +}; +const execCpp = async (data) => { + const filename = Math.random() + .toString(36) + .replace(/[^a-z]+/g, "") + .substr(0, 5); + const filePath = __dirname + `/temp/${filename}.cpp`; + try { + await writeFilePromise(filePath, data); + await execPromise( + `g++ -o utils/temp/${filename} utils/temp/${filename}.cpp` + ); + const output = await execPromise(`utils/temp/${filename}`, { + timeout: 2000, + maxBuffer: 1024 * 1024 * 5, + }); + await deleteFilePromise(filePath); + await deleteFilePromise(`./utils/temp/${filename}`); + return output; + } catch (err) { + return err; + } +}; +const execPython = async (data) => { + const filename = Math.random() + .toString(36) + .replace(/[^a-z]+/g, "") + .substr(0, 5); + const filePath = __dirname + `/temp/${filename}.py`; try { await writeFilePromise(filePath, data); - const output = await execPromise(`node utils/temp/${filename}.js`); - // console.log(output); + const output = await execPromise(`python3 utils/temp/${filename}.py`, { + timeout: 2000, + maxBuffer: 1024 * 1024 * 5, + }); await deleteFilePromise(filePath); return output; - } catch(err) { - // console.log(err); + } catch (err) { return err; } -} -const execCpp = async (data) => { - const filename = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); - const filePath = __dirname + `/temp/${filename}.cpp` -try { - await writeFilePromise(filePath, data); - const output = await execPromise(`g++ -o utils/temp/${filename} utils/temp/${filename}.cpp && ./utils/temp/${filename}`); - // console.log(output); - await deleteFilePromise(filePath); - await deleteFilePromise(`./utils/temp/${filename}`); - return output; -} catch(err) { - // console.log(err); - return err; -} -} -const execPython = async (data) => { - const filename = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); - const filePath = __dirname + `/temp/${filename}.py` -try { - await writeFilePromise(filePath, data); - const output = await execPromise(`python3 utils/temp/${filename}.py`); - // console.log(output); - await deleteFilePromise(filePath); - return output; -} catch(err) { - // console.log(err); - return err; -} -} -module.exports = { execNode, execPython, execCpp } \ No newline at end of file +}; +module.exports = { execNode, execPython, execCpp }; diff --git a/utils/file-executor.js b/utils/file-executor.js index 85a5a31..f72db63 100644 --- a/utils/file-executor.js +++ b/utils/file-executor.js @@ -1,31 +1,37 @@ -const { exec } = require('child_process'); +const { exec } = require("child_process"); const fs = require("fs"); - -const execPromise = (command) => new Promise((resolve, reject) => { - exec(command, { timeout: 2000, maxBuffer: 1024 * 1024 * 5 }, function(error, stdout, stderr) { +const execPromise = (command) => + new Promise((resolve, reject) => { + exec( + command, + { timeout: 2000, maxBuffer: 1024 * 1024 * 5 }, + function (error, stdout, stderr) { // console.log(stdout, stderr, error); - if(error) reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); + if (error) + reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); else { - const isError = Boolean(stderr); - resolve({ isError, output: stdout || stderr }); + const isError = Boolean(stderr); + resolve({ isError, output: stdout || stderr }); } - }); -}); + } + ); + }); -const writeFilePromise = (location, data) => new Promise((resolve, reject) => { - fs.writeFile(location, data, function(err) { - if(err) reject(err); - else resolve(); - }) -}); +const writeFilePromise = (location, data) => + new Promise((resolve, reject) => { + fs.writeFile(location, data, function (err) { + if (err) reject(err); + else resolve(); + }); + }); -const deleteFilePromise = (location) => new Promise((resolve, reject) => { +const deleteFilePromise = (location) => + new Promise((resolve, reject) => { fs.unlink(location, (err) => { - if(err) reject(err); - else resolve(); - }) -}) - + if (err) reject(err); + else resolve(); + }); + }); module.exports = { execPromise, writeFilePromise, deleteFilePromise }; diff --git a/utils/temp/bdqvs.js b/utils/temp/bdqvs.js new file mode 100644 index 0000000..8e23576 --- /dev/null +++ b/utils/temp/bdqvs.js @@ -0,0 +1 @@ +print("Hello World") \ No newline at end of file diff --git a/utils/temp/cimvq.js b/utils/temp/cimvq.js new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/cimvq.js @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/fiuqs.js b/utils/temp/fiuqs.js new file mode 100644 index 0000000..318d154 --- /dev/null +++ b/utils/temp/fiuqs.js @@ -0,0 +1 @@ +console.log("Hello World"; \ No newline at end of file diff --git a/utils/temp/gwedq.py b/utils/temp/gwedq.py new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/gwedq.py @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/juteb.cpp b/utils/temp/juteb.cpp new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/juteb.cpp @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/kyezm.cpp b/utils/temp/kyezm.cpp new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/kyezm.cpp @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/ljrgb.cpp b/utils/temp/ljrgb.cpp new file mode 100644 index 0000000..4da2965 --- /dev/null +++ b/utils/temp/ljrgb.cpp @@ -0,0 +1,9 @@ +//Sample Code + +#include +using namespace std; + +int main() { + cout << "Hello World This is Manish" << endl; + return 0; +} \ No newline at end of file diff --git a/utils/temp/odebx.cpp b/utils/temp/odebx.cpp new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/odebx.cpp @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/pleah.js b/utils/temp/pleah.js new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/pleah.js @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/qopvm.py b/utils/temp/qopvm.py new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/qopvm.py @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/rupwx.cpp b/utils/temp/rupwx.cpp new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/rupwx.cpp @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/trarb.py b/utils/temp/trarb.py new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/trarb.py @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/ucfvm.py b/utils/temp/ucfvm.py new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/ucfvm.py @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file diff --git a/utils/temp/usjsp.js b/utils/temp/usjsp.js new file mode 100644 index 0000000..692a9c9 --- /dev/null +++ b/utils/temp/usjsp.js @@ -0,0 +1 @@ +swggh \ No newline at end of file diff --git a/utils/temp/zuqlk.js b/utils/temp/zuqlk.js new file mode 100644 index 0000000..77ee476 --- /dev/null +++ b/utils/temp/zuqlk.js @@ -0,0 +1,2 @@ +//Enter your code heresf +svb \ No newline at end of file From b513d7c8b479eef4b3d6679564d6d2f369f87c5f Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 31 Jan 2021 18:56:45 +0530 Subject: [PATCH 10/13] Fixed C++ Execution Problem --- utils/executor.js | 13 ++++++++----- utils/temp/bdqvs.js | 1 - utils/temp/cimvq.js | 2 -- utils/temp/code.py | 6 ------ utils/temp/fiuqs.js | 1 - utils/temp/gwedq.py | 2 -- utils/temp/hdyjo.py | 1 - utils/temp/juteb.cpp | 2 -- utils/temp/kyezm.cpp | 2 -- utils/temp/ljrgb.cpp | 9 --------- utils/temp/ncqzo.py | 1 - utils/temp/nmtrv.py | 1 - utils/temp/odebx.cpp | 2 -- utils/temp/pleah.js | 2 -- utils/temp/qopvm.py | 2 -- utils/temp/rupwx.cpp | 2 -- utils/temp/trarb.py | 2 -- utils/temp/ucfvm.py | 2 -- utils/temp/usjsp.js | 1 - utils/temp/vxoyc.py | 1 - utils/temp/zuqlk.js | 2 -- 21 files changed, 8 insertions(+), 49 deletions(-) delete mode 100644 utils/temp/bdqvs.js delete mode 100644 utils/temp/cimvq.js delete mode 100644 utils/temp/code.py delete mode 100644 utils/temp/fiuqs.js delete mode 100644 utils/temp/gwedq.py delete mode 100644 utils/temp/hdyjo.py delete mode 100644 utils/temp/juteb.cpp delete mode 100644 utils/temp/kyezm.cpp delete mode 100644 utils/temp/ljrgb.cpp delete mode 100644 utils/temp/ncqzo.py delete mode 100644 utils/temp/nmtrv.py delete mode 100644 utils/temp/odebx.cpp delete mode 100644 utils/temp/pleah.js delete mode 100644 utils/temp/qopvm.py delete mode 100644 utils/temp/rupwx.cpp delete mode 100644 utils/temp/trarb.py delete mode 100644 utils/temp/ucfvm.py delete mode 100644 utils/temp/usjsp.js delete mode 100644 utils/temp/vxoyc.py delete mode 100644 utils/temp/zuqlk.js diff --git a/utils/executor.js b/utils/executor.js index 3567487..4929e46 100644 --- a/utils/executor.js +++ b/utils/executor.js @@ -16,12 +16,13 @@ const execNode = async (data) => { timeout: 2000, maxBuffer: 1024 * 1024 * 5, }); - await deleteFilePromise(filePath); + await deleteFilePromise(`./utils/temp/${filename}.js`); return output; } catch (err) { return err; } }; + const execCpp = async (data) => { const filename = Math.random() .toString(36) @@ -33,17 +34,18 @@ const execCpp = async (data) => { await execPromise( `g++ -o utils/temp/${filename} utils/temp/${filename}.cpp` ); - const output = await execPromise(`utils/temp/${filename}`, { + const output = await execPromise(`./utils/temp/${filename}`, { timeout: 2000, maxBuffer: 1024 * 1024 * 5, }); - await deleteFilePromise(filePath); + await deleteFilePromise(`./utils/temp/${filename}.cpp`); await deleteFilePromise(`./utils/temp/${filename}`); return output; } catch (err) { return err; } }; + const execPython = async (data) => { const filename = Math.random() .toString(36) @@ -52,14 +54,15 @@ const execPython = async (data) => { const filePath = __dirname + `/temp/${filename}.py`; try { await writeFilePromise(filePath, data); - const output = await execPromise(`python3 utils/temp/${filename}.py`, { + const output = await execPromise(`python3 ./utils/temp/${filename}.py`, { timeout: 2000, maxBuffer: 1024 * 1024 * 5, }); - await deleteFilePromise(filePath); + await deleteFilePromise(`./utils/temp/${filename}.py`); return output; } catch (err) { return err; } }; + module.exports = { execNode, execPython, execCpp }; diff --git a/utils/temp/bdqvs.js b/utils/temp/bdqvs.js deleted file mode 100644 index 8e23576..0000000 --- a/utils/temp/bdqvs.js +++ /dev/null @@ -1 +0,0 @@ -print("Hello World") \ No newline at end of file diff --git a/utils/temp/cimvq.js b/utils/temp/cimvq.js deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/cimvq.js +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/code.py b/utils/temp/code.py deleted file mode 100644 index 67239e7..0000000 --- a/utils/temp/code.py +++ /dev/null @@ -1,6 +0,0 @@ -#include - #include - using namespace std; - int main(){ - std::cout<<"helloo world"; - return 0;} \ No newline at end of file diff --git a/utils/temp/fiuqs.js b/utils/temp/fiuqs.js deleted file mode 100644 index 318d154..0000000 --- a/utils/temp/fiuqs.js +++ /dev/null @@ -1 +0,0 @@ -console.log("Hello World"; \ No newline at end of file diff --git a/utils/temp/gwedq.py b/utils/temp/gwedq.py deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/gwedq.py +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/hdyjo.py b/utils/temp/hdyjo.py deleted file mode 100644 index b746df4..0000000 --- a/utils/temp/hdyjo.py +++ /dev/null @@ -1 +0,0 @@ -print"helloooo" \ No newline at end of file diff --git a/utils/temp/juteb.cpp b/utils/temp/juteb.cpp deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/juteb.cpp +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/kyezm.cpp b/utils/temp/kyezm.cpp deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/kyezm.cpp +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/ljrgb.cpp b/utils/temp/ljrgb.cpp deleted file mode 100644 index 4da2965..0000000 --- a/utils/temp/ljrgb.cpp +++ /dev/null @@ -1,9 +0,0 @@ -//Sample Code - -#include -using namespace std; - -int main() { - cout << "Hello World This is Manish" << endl; - return 0; -} \ No newline at end of file diff --git a/utils/temp/ncqzo.py b/utils/temp/ncqzo.py deleted file mode 100644 index b746df4..0000000 --- a/utils/temp/ncqzo.py +++ /dev/null @@ -1 +0,0 @@ -print"helloooo" \ No newline at end of file diff --git a/utils/temp/nmtrv.py b/utils/temp/nmtrv.py deleted file mode 100644 index 75484fa..0000000 --- a/utils/temp/nmtrv.py +++ /dev/null @@ -1 +0,0 @@ -print("helloooo ) \ No newline at end of file diff --git a/utils/temp/odebx.cpp b/utils/temp/odebx.cpp deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/odebx.cpp +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/pleah.js b/utils/temp/pleah.js deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/pleah.js +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/qopvm.py b/utils/temp/qopvm.py deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/qopvm.py +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/rupwx.cpp b/utils/temp/rupwx.cpp deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/rupwx.cpp +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/trarb.py b/utils/temp/trarb.py deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/trarb.py +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/ucfvm.py b/utils/temp/ucfvm.py deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/ucfvm.py +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file diff --git a/utils/temp/usjsp.js b/utils/temp/usjsp.js deleted file mode 100644 index 692a9c9..0000000 --- a/utils/temp/usjsp.js +++ /dev/null @@ -1 +0,0 @@ -swggh \ No newline at end of file diff --git a/utils/temp/vxoyc.py b/utils/temp/vxoyc.py deleted file mode 100644 index b746df4..0000000 --- a/utils/temp/vxoyc.py +++ /dev/null @@ -1 +0,0 @@ -print"helloooo" \ No newline at end of file diff --git a/utils/temp/zuqlk.js b/utils/temp/zuqlk.js deleted file mode 100644 index 77ee476..0000000 --- a/utils/temp/zuqlk.js +++ /dev/null @@ -1,2 +0,0 @@ -//Enter your code heresf -svb \ No newline at end of file From efb96124431b988b836f209bae702865d11f656b Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 31 Jan 2021 19:03:48 +0530 Subject: [PATCH 11/13] Fixed C++ Execution Problem --- utils/executor.js | 4 ++-- utils/file-executor.js | 20 ++++++++------------ utils/temp/dummy | 0 3 files changed, 10 insertions(+), 14 deletions(-) create mode 100644 utils/temp/dummy diff --git a/utils/executor.js b/utils/executor.js index 4929e46..67fa568 100644 --- a/utils/executor.js +++ b/utils/executor.js @@ -12,7 +12,7 @@ const execNode = async (data) => { const filePath = __dirname + `/temp/${filename}.js`; try { await writeFilePromise(filePath, data); - const output = await execPromise(`node utils/temp/${filename}.js`, { + const output = await execPromise(`node ./utils/temp/${filename}.js`, { timeout: 2000, maxBuffer: 1024 * 1024 * 5, }); @@ -32,7 +32,7 @@ const execCpp = async (data) => { try { await writeFilePromise(filePath, data); await execPromise( - `g++ -o utils/temp/${filename} utils/temp/${filename}.cpp` + `g++ -o ./utils/temp/${filename} ./utils/temp/${filename}.cpp` ); const output = await execPromise(`./utils/temp/${filename}`, { timeout: 2000, diff --git a/utils/file-executor.js b/utils/file-executor.js index f72db63..f3a1482 100644 --- a/utils/file-executor.js +++ b/utils/file-executor.js @@ -3,19 +3,15 @@ const fs = require("fs"); const execPromise = (command) => new Promise((resolve, reject) => { - exec( - command, - { timeout: 2000, maxBuffer: 1024 * 1024 * 5 }, - function (error, stdout, stderr) { - // console.log(stdout, stderr, error); - if (error) - reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); - else { - const isError = Boolean(stderr); - resolve({ isError, output: stdout || stderr }); - } + exec(command, params, function (error, stdout, stderr) { + // console.log(stdout, stderr, error); + if (error) + reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); + else { + const isError = Boolean(stderr); + resolve({ isError, output: stdout || stderr }); } - ); + }); }); const writeFilePromise = (location, data) => diff --git a/utils/temp/dummy b/utils/temp/dummy new file mode 100644 index 0000000..e69de29 From c9f4ac0912878ea1d87bc549db22c357579dffae Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Sun, 31 Jan 2021 19:20:58 +0530 Subject: [PATCH 12/13] Fixed C++ Execution Problem --- utils/executor.js | 34 +++++++++++++++------------------- utils/file-executor.js | 27 ++++++++++++++++++--------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/utils/executor.js b/utils/executor.js index 67fa568..e21f11f 100644 --- a/utils/executor.js +++ b/utils/executor.js @@ -12,17 +12,15 @@ const execNode = async (data) => { const filePath = __dirname + `/temp/${filename}.js`; try { await writeFilePromise(filePath, data); - const output = await execPromise(`node ./utils/temp/${filename}.js`, { - timeout: 2000, - maxBuffer: 1024 * 1024 * 5, - }); - await deleteFilePromise(`./utils/temp/${filename}.js`); + const output = await execPromise(`node utils/temp/${filename}.js`); + // console.log(output); + await deleteFilePromise(filePath); return output; } catch (err) { + // console.log(err); return err; } }; - const execCpp = async (data) => { const filename = Math.random() .toString(36) @@ -32,20 +30,20 @@ const execCpp = async (data) => { try { await writeFilePromise(filePath, data); await execPromise( - `g++ -o ./utils/temp/${filename} ./utils/temp/${filename}.cpp` + `g++ -o utils/temp/${filename} utils/temp/${filename}.cpp`, + true ); - const output = await execPromise(`./utils/temp/${filename}`, { - timeout: 2000, - maxBuffer: 1024 * 1024 * 5, - }); - await deleteFilePromise(`./utils/temp/${filename}.cpp`); + + const output = await execPromise(`./utils/temp/${filename}`); + // console.log(output); + await deleteFilePromise(filePath); await deleteFilePromise(`./utils/temp/${filename}`); return output; } catch (err) { + // console.log(err); return err; } }; - const execPython = async (data) => { const filename = Math.random() .toString(36) @@ -54,15 +52,13 @@ const execPython = async (data) => { const filePath = __dirname + `/temp/${filename}.py`; try { await writeFilePromise(filePath, data); - const output = await execPromise(`python3 ./utils/temp/${filename}.py`, { - timeout: 2000, - maxBuffer: 1024 * 1024 * 5, - }); - await deleteFilePromise(`./utils/temp/${filename}.py`); + const output = await execPromise(`python3 utils/temp/${filename}.py`); + // console.log(output); + await deleteFilePromise(filePath); return output; } catch (err) { + // console.log(err); return err; } }; - module.exports = { execNode, execPython, execCpp }; diff --git a/utils/file-executor.js b/utils/file-executor.js index f3a1482..7fa4cc6 100644 --- a/utils/file-executor.js +++ b/utils/file-executor.js @@ -1,17 +1,26 @@ const { exec } = require("child_process"); const fs = require("fs"); -const execPromise = (command) => +const execPromise = (command, noBuffer = false) => new Promise((resolve, reject) => { - exec(command, params, function (error, stdout, stderr) { - // console.log(stdout, stderr, error); - if (error) - reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); - else { - const isError = Boolean(stderr); - resolve({ isError, output: stdout || stderr }); + exec( + command, + noBuffer + ? {} + : { + timeout: 2000, + maxBuffer: 1024 * 1024 * 5, + }, + function (error, stdout, stderr) { + // console.log(stdout, stderr, error); + if (error) + reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); + else { + const isError = Boolean(stderr); + resolve({ isError, output: stdout || stderr }); + } } - }); + ); }); const writeFilePromise = (location, data) => From 5ec2995e0e7a5c79eee1a509d1fd8c5af32e1799 Mon Sep 17 00:00:00 2001 From: Poorvi Vaish Date: Mon, 31 May 2021 20:43:37 +0530 Subject: [PATCH 13/13] Added User Input Feature --- index.js | 11 +++++++---- utils/executor.js | 21 +++++++++++++++------ utils/file-executor.js | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 62c3d7a..396373e 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ const express = require("express"); const cors = require("cors"); const { execNode, execPython, execCpp } = require("./utils/executor"); const app = express(); -const PORT = process.env.PORT || 3000; +const PORT = process.env.PORT || 3001; app.use(express.json()); app.use(cors()); @@ -16,15 +16,17 @@ app.get("/", (req, res) => { }); app.post("/execNode", async (req, res) => { const data = req.body.code; + const input = req.body.input; // console.log(data); - const output = await execNode(data); + const output = await execNode(data, input || ""); console.log(output); res.send(output); }); app.post("/execPython", async (req, res) => { const data = req.body.code; + const input = req.body.input; // console.log(data); - const output = await execPython(data); + const output = await execPython(data, input || ""); console.log(output); res.send(output); @@ -32,8 +34,9 @@ app.post("/execPython", async (req, res) => { app.post("/execCpp", async (req, res) => { const data = req.body.code; + const input = req.body.input; // console.log(data); - const output = await execCpp(data); + const output = await execCpp(data, input ||""); console.log(output); res.send(output); }); diff --git a/utils/executor.js b/utils/executor.js index e21f11f..8336446 100644 --- a/utils/executor.js +++ b/utils/executor.js @@ -4,57 +4,66 @@ const { deleteFilePromise, } = require("./file-executor.js"); -const execNode = async (data) => { +const execNode = async (data, input="") => { const filename = Math.random() .toString(36) .replace(/[^a-z]+/g, "") .substr(0, 5); const filePath = __dirname + `/temp/${filename}.js`; + const inputPath = __dirname + `/temp/${filename}.input`; try { await writeFilePromise(filePath, data); - const output = await execPromise(`node utils/temp/${filename}.js`); + await writeFilePromise(inputPath, input); + const output = await execPromise(`node utils/temp/${filename}.js < utils/temp/${filename}.input`); // console.log(output); await deleteFilePromise(filePath); + await deleteFilePromise(inputPath); return output; } catch (err) { // console.log(err); return err; } }; -const execCpp = async (data) => { +const execCpp = async (data, input="") => { const filename = Math.random() .toString(36) .replace(/[^a-z]+/g, "") .substr(0, 5); const filePath = __dirname + `/temp/${filename}.cpp`; + const inputPath = __dirname + `/temp/${filename}.input`; try { await writeFilePromise(filePath, data); + await writeFilePromise(inputPath, input); await execPromise( `g++ -o utils/temp/${filename} utils/temp/${filename}.cpp`, true ); - const output = await execPromise(`./utils/temp/${filename}`); + const output = await execPromise(`./utils/temp/${filename} < utils/temp/${filename}.input`); // console.log(output); await deleteFilePromise(filePath); await deleteFilePromise(`./utils/temp/${filename}`); + await deleteFilePromise(inputPath); return output; } catch (err) { // console.log(err); return err; } }; -const execPython = async (data) => { +const execPython = async (data, input="") => { const filename = Math.random() .toString(36) .replace(/[^a-z]+/g, "") .substr(0, 5); const filePath = __dirname + `/temp/${filename}.py`; + const inputPath = __dirname + `/temp/${filename}.input`; try { await writeFilePromise(filePath, data); - const output = await execPromise(`python3 utils/temp/${filename}.py`); + await writeFilePromise(inputPath, input); + const output = await execPromise(`python3 utils/temp/${filename}.py < utils/temp/${filename}.input`); // console.log(output); await deleteFilePromise(filePath); + await deleteFilePromise(inputPath); return output; } catch (err) { // console.log(err); diff --git a/utils/file-executor.js b/utils/file-executor.js index 7fa4cc6..17594af 100644 --- a/utils/file-executor.js +++ b/utils/file-executor.js @@ -12,7 +12,7 @@ const execPromise = (command, noBuffer = false) => maxBuffer: 1024 * 1024 * 5, }, function (error, stdout, stderr) { - // console.log(stdout, stderr, error); + console.log(stdout, stderr, error); if (error) reject(JSON.stringify(error, Object.getOwnPropertyNames(error))); else {