-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (66 loc) · 1.72 KB
/
index.js
File metadata and controls
73 lines (66 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var querystring = require('querystring');
var http = require('http');
var fs = require('fs');
module.exports.compile = function(settings , source , callback ){
requestMaker(settings , source , 0 , callback);
};
module.exports.run = function(settings , source , callback){
requestMaker(settings , source , 1 , callback);
};
module.exports.compileFile = function(settings , filePath , callback){
fs.readFile(filePath , { encoding : 'utf8'} , function(err,data){
if(err)
callback(err , data);
else
{
requestMaker(settings, data, 0, callback);
}
});
};
module.exports.runFile = function(settings , filePath , callback){
fs.readFile(filePath , { encoding : 'utf8'} , function(err,data){
if(err)
callback(err , data);
else
{
requestMaker(settings, data, 1, callback);
}
});
};
requestMaker = function( post_data , source , mode , callback ){
data = querystring.stringify({
'client_secret': post_data.client_secret ,
'async': post_data.async ,
'source': source,
'lang': post_data.lang ,
'time_limit': post_data.time_limit ,
'memory_limit': post_data.memory_limit
});
var options = {
hostname: 'api.hackerearth.com',
port: 80,
path: mode == 0 ? '/code/compile' : '/code/run',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
var response = '';
var req = http.request(options , function(res){
res.on('data' , function(chunk){
response += chunk.toString();
});
res.on('end' , function(){
callback(null , response );
});
res.on('error' , function(err){
callback(err , null );
});
});
req.on('error' , function(err){
callback(err , null);
});
req.write(data);
req.end();
};