-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (151 loc) · 5.75 KB
/
index.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const APP_LISTENING_PORT = 8087;
const JSON_MEDIA_TYPE = {'Content-Type':'application/json'};
//const CODECOVERAGE_SERVER = "127.0.0.1";
const CODECOVERAGE_SERVER = "18.191.138.177";
const CODECOVERAGE_PORT = 8083;
const HTTP_STATUS_CODE = {
SUCCESS: {
CODE: 200,
MSG: {'msg': 'Success'}
},
CREATED: {
CODE: 201,
MSG: {'msg': 'Created'}
},
BAD_REQUEST: {
CODE: 400,
MSG: {'msg': 'Bad Request'}
},
METHOD_NOT_ALLOWED: {
CODE: 405,
MSG: {'msg': 'Request method not allowed'}
},
CONFLICT: {
CODE: 409,
MSG: {'msg': 'The request could not be completed due to a conflict with the current state of the target resource'}
},
UNSUPPORTED_MEDIA_TYPE: {
CODE: 415,
MSG: {'msg': 'Unsupported media type'}
},
EXPECTATION_FAILED: {
CODE: 417,
MSG: {'msg': 'Expectation failed'}
},
INTERALL_ERROR: {
CODE: 500,
MSG: {'msg': 'Internal server error'}
}
};
const express = require('express');
const request = require('request');
const http = require('http');
const bodyparser = require('body-parser');
const CDP = require('chrome-remote-interface');
var browserClients = {};
var appUtils = new AppUtils();
const app = express();
app.use(bodyparser.json({limit: '500mb', extended: true}))
app.use(bodyparser.urlencoded({limit: '500mb', extended: true}))
function AppUtils(){
this.sendResponse = function sendResponse(r, s, d){
r.setHeader("Content-Type", "application/json");
r.status(s);
r.json(d);
r.send();
};
this.senInternalErorResponse = function senInternalErorResponse(r){
r.setHeader("Content-Type", "application/json");
r.status(HTTP_STATUS_CODE.INTERALL_ERROR.CODE);
r.json(HTTP_STATUS_CODE.INTERALL_ERROR.MSG);
r.send();
};
this.convertJsonToString = function convertJsonToString(json){
return JSON.stringify(json);
};
this.generateScretKey = function generateScretKey(){
return Math.random().toString(36).substring(7);
};
this.GUID = function GUID() {
return (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase();
};
};
// To start a profiler on remote system
app.post("/start", function(req, res){
try {
CDP((client) => {
const {Network, Page, Profiler} = client;
//Preserve client information for further operations
var _id = appUtils.GUID();
browserClients[_id] = {client: client, url: req.body.hostname} ;
// enable events then start!
Promise.all([
Network.enable(),
Page.enable(),
Profiler.enable(),
Profiler.start(),
Profiler.startPreciseCoverage(),
Profiler.takePreciseCoverage(),
]).then(() => {
Page.navigate({url: req.body.hostname});
appUtils.sendResponse(res, HTTP_STATUS_CODE.SUCCESS.CODE, {"_id": _id});
}).catch((err) => {
client.close(); // cannot connect to the remote endpoint -- close connection
appUtils.sendResponse(res, HTTP_STATUS_CODE.INTERALL_ERROR.CODE, {"msg": "Cannot connect to the remote endpoint"});
});
}).on('error', (err) => {
appUtils.sendResponse(res, HTTP_STATUS_CODE.INTERALL_ERROR.CODE, {"msg": "Cannot connect to the remote endpoint"});
});
} catch(exc) {
appUtils.sendResponse(res, HTTP_STATUS_CODE.INTERALL_ERROR.CODE, HTTP_STATUS_CODE.INTERALL_ERROR.MSG);
}
});
// To stop a profiler as well generate the code coverager report
app.post("/report", function(req, res){
var reqData = req.body;
var clientObj = browserClients[reqData._id];
try {
if (clientObj && clientObj.client && reqData.emailAddress) {
clientObj.client.Profiler.getBestEffortCoverage().then((data) => {
var postData = JSON.stringify({
_id: reqData._id,
data: JSON.stringify(data),
emailAddress: reqData.emailAddress,
url: clientObj.url
});
var options = {
hostname: CODECOVERAGE_SERVER,
port: CODECOVERAGE_PORT,
path: '/report',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
};
var httpReq = http.request(options, (data) => {
delete browserClients[reqData._id]; // Delete client object
});
httpReq.on('error', (e) => {
delete browserClients[reqData._id]; // Delete client object
});
httpReq.write(postData);
httpReq.end();
appUtils.sendResponse(res, HTTP_STATUS_CODE.SUCCESS.CODE, {"msg": "Report will be sent to your mail address"});
}).catch((err) => {
clientObj.client.close(); // close connection
appUtils.senInternalErorResponse(res);
});
} else {
clientObj.client.close(); // close connection
appUtils.sendResponse(res, HTTP_STATUS_CODE.EXPECTATION_FAILED.CODE, HTTP_STATUS_CODE.EXPECTATION_FAILED.MSG);
}
} catch(exc) {
clientObj.client.close(); // close connection
appUtils.senInternalErorResponse(res);
}
});
// Start server goes here
var server = app.listen(APP_LISTENING_PORT, function(){
console.log('Listening on port ' + this.address().port);
});