@@ -117,19 +117,21 @@ function handleResolutionRequest(filePath: string, res: http.ServerResponse) {
117
117
let resolvedPath = path . resolve ( filePath , "" ) ;
118
118
resolvedPath = resolvedPath . substring ( resolvedPath . indexOf ( "tests" ) ) ;
119
119
resolvedPath = switchToForwardSlashes ( resolvedPath ) ;
120
- send ( "success" , res , resolvedPath ) ;
121
- return ;
120
+ send ( ResponseCode . Success , res , resolvedPath ) ;
122
121
}
123
122
124
- function send ( result : "fail" , res : http . ServerResponse , contents : string , contentType ?: string ) : void ;
125
- function send ( result : "success" , res : http . ServerResponse , contents : string , contentType ?: string ) : void ;
126
- function send ( result : "unknown" , res : http . ServerResponse , contents : string , contentType ?: string ) : void ;
127
- function send ( result : string , res : http . ServerResponse , contents : string , contentType ?: string ) : void
128
- function send ( result : string , res : http . ServerResponse , contents : string , contentType = "binary" ) : void {
129
- const responseCode = result === "success" ? 200 : result === "fail" ? 500 : result === "unknown" ? 404 : parseInt ( result ) ;
123
+ const enum ResponseCode {
124
+ Success = 200 ,
125
+ BadRequest = 400 ,
126
+ NotFound = 404 ,
127
+ MethodNotAllowed = 405 ,
128
+ PayloadTooLarge = 413 ,
129
+ Fail = 500
130
+ }
131
+
132
+ function send ( responseCode : number , res : http . ServerResponse , contents : string , contentType = "binary" ) : void {
130
133
res . writeHead ( responseCode , { "Content-Type" : contentType } ) ;
131
134
res . end ( contents ) ;
132
- return ;
133
135
}
134
136
135
137
// Reads the data from a post request and passes it to the given callback
@@ -142,7 +144,7 @@ function processPost(req: http.ServerRequest, res: http.ServerResponse, callback
142
144
queryData += data ;
143
145
if ( queryData . length > 1e8 ) {
144
146
queryData = "" ;
145
- send ( "413" , res , undefined ) ;
147
+ send ( ResponseCode . PayloadTooLarge , res , undefined ) ;
146
148
console . log ( "ERROR: destroying connection" ) ;
147
149
req . connection . destroy ( ) ;
148
150
}
@@ -155,7 +157,7 @@ function processPost(req: http.ServerRequest, res: http.ServerResponse, callback
155
157
156
158
}
157
159
else {
158
- send ( "405" , res , undefined ) ;
160
+ send ( ResponseCode . MethodNotAllowed , res , undefined ) ;
159
161
}
160
162
}
161
163
@@ -201,16 +203,16 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
201
203
switch ( operation ) {
202
204
case RequestType . GetDir :
203
205
const filesInFolder = dir ( reqPath , "" , { recursive : true } ) ;
204
- send ( "success" , res , filesInFolder . join ( "," ) ) ;
206
+ send ( ResponseCode . Success , res , filesInFolder . join ( "," ) ) ;
205
207
break ;
206
208
case RequestType . GetFile :
207
209
fs . readFile ( reqPath , ( err , file ) => {
208
210
const contentType = contentTypeForExtension ( path . extname ( reqPath ) ) ;
209
211
if ( err ) {
210
- send ( "fail" , res , err . message , contentType ) ;
212
+ send ( ResponseCode . NotFound , res , err . message , contentType ) ;
211
213
}
212
214
else {
213
- send ( "success" , res , < any > file , contentType ) ;
215
+ send ( ResponseCode . Success , res , < any > file , contentType ) ;
214
216
}
215
217
} ) ;
216
218
break ;
@@ -222,33 +224,33 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
222
224
processPost ( req , res , ( data ) => {
223
225
writeFile ( reqPath , data , { recursive : true } ) ;
224
226
} ) ;
225
- send ( "success" , res , undefined ) ;
227
+ send ( ResponseCode . Success , res , undefined ) ;
226
228
break ;
227
229
case RequestType . WriteDir :
228
230
fs . mkdirSync ( reqPath ) ;
229
- send ( "success" , res , undefined ) ;
231
+ send ( ResponseCode . Success , res , undefined ) ;
230
232
break ;
231
233
case RequestType . DeleteFile :
232
234
if ( fs . existsSync ( reqPath ) ) {
233
235
fs . unlinkSync ( reqPath ) ;
234
236
}
235
- send ( "success" , res , undefined ) ;
237
+ send ( ResponseCode . Success , res , undefined ) ;
236
238
break ;
237
239
case RequestType . DeleteDir :
238
240
if ( fs . existsSync ( reqPath ) ) {
239
241
fs . rmdirSync ( reqPath ) ;
240
242
}
241
- send ( "success" , res , undefined ) ;
243
+ send ( ResponseCode . Success , res , undefined ) ;
242
244
break ;
243
245
case RequestType . AppendFile :
244
246
processPost ( req , res , ( data ) => {
245
247
fs . appendFileSync ( reqPath , data ) ;
246
248
} ) ;
247
- send ( "success" , res , undefined ) ;
249
+ send ( ResponseCode . Success , res , undefined ) ;
248
250
break ;
249
251
case RequestType . Unknown :
250
252
default :
251
- send ( "unknown" , res , undefined ) ;
253
+ send ( ResponseCode . BadRequest , res , undefined ) ;
252
254
break ;
253
255
}
254
256
0 commit comments