Skip to content

Commit 0fe1a61

Browse files
committed
2 parents bbb399a + fec3f73 commit 0fe1a61

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

rce-server/server/api/services/code.service.js

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import fs from 'fs';
2-
import path from 'path';
3-
import util from 'util';
4-
import { execFile, spawn, exec } from 'child_process';
5-
import ValidationService from './validation.service';
1+
import fs from "fs";
2+
import path from "path";
3+
import util from "util";
4+
import { execFile, spawn, exec } from "child_process";
5+
import ValidationService from "./validation.service";
66
const ROOT_DIR = `${process.cwd()}`;
7-
const SOURCE_DIR = path.join(ROOT_DIR, 'executor');
7+
const SOURCE_DIR = path.join(ROOT_DIR, "executor");
88
const TARGET_DIR = `/app/codes`;
9-
const IMAGE_NAME = 'executor:1.0';
9+
const IMAGE_NAME = "executor:1.0";
1010
//const VOL_NAME = `my_vol`;
1111
const VOL_NAME = SOURCE_DIR;
1212

1313
class CodeService {
1414
async execute(code, input, lang, id) {
1515
//console.log('code', code);
1616
try {
17-
!input ? (input = '') : null;
17+
!input ? (input = "") : null;
1818

1919
// validating code
2020
// await this.validateCode(code, input, lang, id);
@@ -26,7 +26,7 @@ class CodeService {
2626
);
2727
if (!isValid) {
2828
throw {
29-
message
29+
message,
3030
};
3131
}
3232

@@ -54,7 +54,7 @@ class CodeService {
5454
);
5555

5656
if (OUTPUT) {
57-
console.log('output', OUTPUT.toString());
57+
console.log("output", OUTPUT.toString());
5858
return OUTPUT.toString();
5959
}
6060
} catch (error) {
@@ -65,28 +65,28 @@ class CodeService {
6565
async writeFile(code, lang, input, id) {
6666
let fileName = `${id}code`;
6767
switch (lang) {
68-
case 'javascript': {
69-
fileName += '.js';
68+
case "javascript": {
69+
fileName += ".js";
7070
break;
7171
}
72-
case 'cpp': {
73-
fileName += '.cpp';
72+
case "cpp": {
73+
fileName += ".cpp";
7474
break;
7575
}
76-
case 'python': {
77-
fileName += '.py';
76+
case "python": {
77+
fileName += ".py";
7878
break;
7979
}
80-
case 'java': {
81-
fileName += '.java';
80+
case "java": {
81+
fileName += ".java";
8282
break;
8383
}
84-
case 'c': {
85-
fileName += '.c';
84+
case "c": {
85+
fileName += ".c";
8686
break;
8787
}
8888
default: {
89-
throw { message: 'Invalid language' };
89+
throw { message: "Invalid language" };
9090
}
9191
}
9292
const write = util.promisify(fs.writeFile);
@@ -96,60 +96,60 @@ class CodeService {
9696
await write(path.join(SOURCE_DIR, `${id}input.txt`), input);
9797
return {
9898
file: fileName,
99-
inputFile: `${id}input.txt`
99+
inputFile: `${id}input.txt`,
100100
};
101101
} catch (error) {
102102
throw { message: error };
103103
}
104104
}
105105

106106
async writeCommand(lang, file, input, id, code) {
107-
let command = '';
107+
let command = "";
108108
switch (lang) {
109-
case 'javascript': {
110-
command = `cd ${TARGET_DIR} && node ${file} < ${input}`;
109+
case "javascript": {
110+
command = `cd "${TARGET_DIR}" && node ${file} < ${input}`;
111111
break;
112112
}
113-
case 'cpp': {
114-
command = `cd ${TARGET_DIR} && g++ -o ${id} ${file} && ./${id} < ${input}`;
113+
case "cpp": {
114+
command = `cd "${TARGET_DIR}" && g++ -o ${id} ${file} && ./${id} < ${input}`;
115115
break;
116116
}
117-
case 'python': {
118-
command = `cd ${TARGET_DIR} && python ${file} < ${input}`;
117+
case "python": {
118+
command = `cd "${TARGET_DIR}" && python ${file} < ${input}`;
119119
break;
120120
}
121-
case 'java': {
121+
case "java": {
122122
let className = await this.extractJavaClassName(code);
123-
className = className.split(/\s/).join('');
124-
console.log('class ', className);
125-
command = `cd ${TARGET_DIR} && javac ${file} && java ${className} < ${input}`;
123+
className = className.split(/\s/).join("");
124+
console.log("class ", className);
125+
command = `cd "${TARGET_DIR}" && javac ${file} && java ${className} < ${input}`;
126126
break;
127127
}
128-
case 'c': {
129-
command = `cd ${TARGET_DIR} && gcc -o ${id} ${file} && ./${id} < ${input}`;
128+
case "c": {
129+
command = `cd "${TARGET_DIR}" && gcc -o ${id} ${file} && ./${id} < ${input}`;
130130
break;
131131
}
132132
default: {
133-
throw { message: 'Invalid language' };
133+
throw { message: "Invalid language" };
134134
}
135135
}
136136

137137
const containerName = `${id}container`;
138138

139139
const runCode = `docker exec ${containerName} sh -c "${command}"`;
140140

141-
const runContainer = `docker run -it -d --name ${containerName} -v ${VOL_NAME}:${TARGET_DIR} ${IMAGE_NAME}`;
141+
const runContainer = `docker run -it -d --name ${containerName} -v "${VOL_NAME}":${TARGET_DIR} ${IMAGE_NAME}`;
142142

143143
return { runCode, runContainer };
144144
}
145145

146146
async execChild(runCode, runContainer, id, file, inputFile, lang, code) {
147147
return new Promise((resolve, reject) => {
148148
const execCont = exec(`${runContainer}`);
149-
execCont.on('error', err => {
150-
throw { status: '404', message: err };
149+
execCont.on("error", (err) => {
150+
throw { status: "404", message: err };
151151
});
152-
execCont.stdout.on('data', () => {
152+
execCont.stdout.on("data", () => {
153153
exec(`${runCode}`, async (error, stdout, stderr) => {
154154
await this.endContainer(id);
155155
await this.deleteFiles(file, inputFile, lang, id, code);
@@ -164,26 +164,26 @@ class CodeService {
164164
}
165165

166166
async deleteFiles(fileName, inputName, lang, id, code) {
167-
fs.unlinkSync(path.join(SOURCE_DIR, fileName), err => {
167+
fs.unlinkSync(path.join(SOURCE_DIR, fileName), (err) => {
168168
if (err) throw { message: err };
169169
});
170170
if (inputName) {
171-
fs.unlinkSync(path.join(SOURCE_DIR, inputName), err => {
171+
fs.unlinkSync(path.join(SOURCE_DIR, inputName), (err) => {
172172
if (err) throw { message: err };
173173
});
174174
}
175-
if (lang == 'cpp' || lang == 'c') {
175+
if (lang == "cpp" || lang == "c") {
176176
if (fs.existsSync(path.join(SOURCE_DIR, id)))
177-
fs.unlinkSync(path.join(SOURCE_DIR, id), err => {
177+
fs.unlinkSync(path.join(SOURCE_DIR, id), (err) => {
178178
if (err) throw err;
179179
});
180180
}
181-
if (lang == 'java') {
181+
if (lang == "java") {
182182
let className = await this.extractJavaClassName(code);
183-
className = className.split(/\s/).join('');
184-
console.log('delete', className);
183+
className = className.split(/\s/).join("");
184+
console.log("delete", className);
185185
if (fs.existsSync(path.join(SOURCE_DIR, `${className}.class`)))
186-
fs.unlinkSync(path.join(SOURCE_DIR, `${className}.class`), err => {
186+
fs.unlinkSync(path.join(SOURCE_DIR, `${className}.class`), (err) => {
187187
if (err) throw err;
188188
});
189189
}
@@ -195,25 +195,25 @@ class CodeService {
195195
exec(`${exit}`, (error, stdout, stderr) => {
196196
if (error) {
197197
console.log(error);
198-
} else console.log('Container stoped and deleted');
198+
} else console.log("Container stoped and deleted");
199199
});
200200
}
201201

202202
async extractJavaClassName(s) {
203-
let prefix = 'class';
204-
let suffix = '{';
203+
let prefix = "class";
204+
let suffix = "{";
205205
let i = s.indexOf(prefix);
206206
if (i >= 0) {
207207
s = s.substring(i + prefix.length);
208208
} else {
209-
return '';
209+
return "";
210210
}
211211
if (suffix) {
212212
i = s.indexOf(suffix);
213213
if (i >= 0) {
214214
s = s.substring(0, i);
215215
} else {
216-
return '';
216+
return "";
217217
}
218218
}
219219
return s;

0 commit comments

Comments
 (0)