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" ;
6
6
const ROOT_DIR = `${ process . cwd ( ) } ` ;
7
- const SOURCE_DIR = path . join ( ROOT_DIR , ' executor' ) ;
7
+ const SOURCE_DIR = path . join ( ROOT_DIR , " executor" ) ;
8
8
const TARGET_DIR = `/app/codes` ;
9
- const IMAGE_NAME = ' executor:1.0' ;
9
+ const IMAGE_NAME = " executor:1.0" ;
10
10
//const VOL_NAME = `my_vol`;
11
11
const VOL_NAME = SOURCE_DIR ;
12
12
13
13
class CodeService {
14
14
async execute ( code , input , lang , id ) {
15
15
//console.log('code', code);
16
16
try {
17
- ! input ? ( input = '' ) : null ;
17
+ ! input ? ( input = "" ) : null ;
18
18
19
19
// validating code
20
20
// await this.validateCode(code, input, lang, id);
@@ -26,7 +26,7 @@ class CodeService {
26
26
) ;
27
27
if ( ! isValid ) {
28
28
throw {
29
- message
29
+ message,
30
30
} ;
31
31
}
32
32
@@ -54,7 +54,7 @@ class CodeService {
54
54
) ;
55
55
56
56
if ( OUTPUT ) {
57
- console . log ( ' output' , OUTPUT . toString ( ) ) ;
57
+ console . log ( " output" , OUTPUT . toString ( ) ) ;
58
58
return OUTPUT . toString ( ) ;
59
59
}
60
60
} catch ( error ) {
@@ -65,28 +65,28 @@ class CodeService {
65
65
async writeFile ( code , lang , input , id ) {
66
66
let fileName = `${ id } code` ;
67
67
switch ( lang ) {
68
- case ' javascript' : {
69
- fileName += ' .js' ;
68
+ case " javascript" : {
69
+ fileName += " .js" ;
70
70
break ;
71
71
}
72
- case ' cpp' : {
73
- fileName += ' .cpp' ;
72
+ case " cpp" : {
73
+ fileName += " .cpp" ;
74
74
break ;
75
75
}
76
- case ' python' : {
77
- fileName += ' .py' ;
76
+ case " python" : {
77
+ fileName += " .py" ;
78
78
break ;
79
79
}
80
- case ' java' : {
81
- fileName += ' .java' ;
80
+ case " java" : {
81
+ fileName += " .java" ;
82
82
break ;
83
83
}
84
- case 'c' : {
85
- fileName += '.c' ;
84
+ case "c" : {
85
+ fileName += ".c" ;
86
86
break ;
87
87
}
88
88
default : {
89
- throw { message : ' Invalid language' } ;
89
+ throw { message : " Invalid language" } ;
90
90
}
91
91
}
92
92
const write = util . promisify ( fs . writeFile ) ;
@@ -96,60 +96,60 @@ class CodeService {
96
96
await write ( path . join ( SOURCE_DIR , `${ id } input.txt` ) , input ) ;
97
97
return {
98
98
file : fileName ,
99
- inputFile : `${ id } input.txt`
99
+ inputFile : `${ id } input.txt` ,
100
100
} ;
101
101
} catch ( error ) {
102
102
throw { message : error } ;
103
103
}
104
104
}
105
105
106
106
async writeCommand ( lang , file , input , id , code ) {
107
- let command = '' ;
107
+ let command = "" ;
108
108
switch ( lang ) {
109
- case ' javascript' : {
110
- command = `cd ${ TARGET_DIR } && node ${ file } < ${ input } ` ;
109
+ case " javascript" : {
110
+ command = `cd " ${ TARGET_DIR } " && node ${ file } < ${ input } ` ;
111
111
break ;
112
112
}
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 } ` ;
115
115
break ;
116
116
}
117
- case ' python' : {
118
- command = `cd ${ TARGET_DIR } && python ${ file } < ${ input } ` ;
117
+ case " python" : {
118
+ command = `cd " ${ TARGET_DIR } " && python ${ file } < ${ input } ` ;
119
119
break ;
120
120
}
121
- case ' java' : {
121
+ case " java" : {
122
122
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 } ` ;
126
126
break ;
127
127
}
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 } ` ;
130
130
break ;
131
131
}
132
132
default : {
133
- throw { message : ' Invalid language' } ;
133
+ throw { message : " Invalid language" } ;
134
134
}
135
135
}
136
136
137
137
const containerName = `${ id } container` ;
138
138
139
139
const runCode = `docker exec ${ containerName } sh -c "${ command } "` ;
140
140
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 } ` ;
142
142
143
143
return { runCode, runContainer } ;
144
144
}
145
145
146
146
async execChild ( runCode , runContainer , id , file , inputFile , lang , code ) {
147
147
return new Promise ( ( resolve , reject ) => {
148
148
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 } ;
151
151
} ) ;
152
- execCont . stdout . on ( ' data' , ( ) => {
152
+ execCont . stdout . on ( " data" , ( ) => {
153
153
exec ( `${ runCode } ` , async ( error , stdout , stderr ) => {
154
154
await this . endContainer ( id ) ;
155
155
await this . deleteFiles ( file , inputFile , lang , id , code ) ;
@@ -164,26 +164,26 @@ class CodeService {
164
164
}
165
165
166
166
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 ) => {
168
168
if ( err ) throw { message : err } ;
169
169
} ) ;
170
170
if ( inputName ) {
171
- fs . unlinkSync ( path . join ( SOURCE_DIR , inputName ) , err => {
171
+ fs . unlinkSync ( path . join ( SOURCE_DIR , inputName ) , ( err ) => {
172
172
if ( err ) throw { message : err } ;
173
173
} ) ;
174
174
}
175
- if ( lang == ' cpp' || lang == 'c' ) {
175
+ if ( lang == " cpp" || lang == "c" ) {
176
176
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 ) => {
178
178
if ( err ) throw err ;
179
179
} ) ;
180
180
}
181
- if ( lang == ' java' ) {
181
+ if ( lang == " java" ) {
182
182
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 ) ;
185
185
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 ) => {
187
187
if ( err ) throw err ;
188
188
} ) ;
189
189
}
@@ -195,25 +195,25 @@ class CodeService {
195
195
exec ( `${ exit } ` , ( error , stdout , stderr ) => {
196
196
if ( error ) {
197
197
console . log ( error ) ;
198
- } else console . log ( ' Container stoped and deleted' ) ;
198
+ } else console . log ( " Container stoped and deleted" ) ;
199
199
} ) ;
200
200
}
201
201
202
202
async extractJavaClassName ( s ) {
203
- let prefix = ' class' ;
204
- let suffix = '{' ;
203
+ let prefix = " class" ;
204
+ let suffix = "{" ;
205
205
let i = s . indexOf ( prefix ) ;
206
206
if ( i >= 0 ) {
207
207
s = s . substring ( i + prefix . length ) ;
208
208
} else {
209
- return '' ;
209
+ return "" ;
210
210
}
211
211
if ( suffix ) {
212
212
i = s . indexOf ( suffix ) ;
213
213
if ( i >= 0 ) {
214
214
s = s . substring ( 0 , i ) ;
215
215
} else {
216
- return '' ;
216
+ return "" ;
217
217
}
218
218
}
219
219
return s ;
0 commit comments