-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests_results.js
139 lines (116 loc) · 3.38 KB
/
tests_results.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
/*!*
*
* Copyright (C) Highsoft AS
*
* */
const ChildProcess = require('child_process');
const ClientPath = require('path').posix;
const FS = require('fs');
const HTTP = require('http');
const ServerPath = require('path');
const CONFIGURATION = {
baseDirectory: process.cwd(),
serverPort: 8000
};
const CONTENT_TYPES = {
'.bin': 'application/octet-stream',
'.css': 'text/css',
'.gif': 'image/gif',
'.htm': 'text/html',
'.html': 'text/html',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg',
'.js': 'application/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.svg': 'image/svg',
'.txt': 'text/plain'
};
function onClientError (error, socket) {
console.error(error);
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
}
function onError (error) {
console.error(error);
process.exit(1);
}
function responseNotFound (response) {
console.info('=> 404 Not Found');
response.writeHead(404, 'Not Found');
response.end();
}
function responseNotSupported (response) {
console.info('=> 405 Method Not Allowed');
response.writeHead(405, 'Method Not Allowed');
response.end();
}
function responseRedirect (response, location) {
console.info('=> 303 See Other');
response.writeHead(303, 'See Other', { 'Location': location });
response.end();
}
function serve (request, response) {
const baseDirectory = CONFIGURATION.baseDirectory;
const requestedClientPath = request.url;
const requestedMethod = request.method;
const requestedServerPath = ServerPath.join(baseDirectory, request.url);
console.info(`${requestedMethod} ${requestedClientPath}`);
switch (requestedMethod) {
default:
responseNotSupported(response);
return;
case 'GET':
case 'HEAD':
break;
}
if (
!requestedServerPath.startsWith(baseDirectory) ||
!FS.existsSync(requestedServerPath)
) {
responseNotFound(response);
return;
}
const requestServerStats = FS.statSync(requestedServerPath);
if (requestServerStats.isDirectory()) {
responseRedirect(response, ClientPath.join(requestedClientPath, 'index.html'));
return;
}
let contentLength = requestServerStats.size;
let contentType = (CONTENT_TYPES[ServerPath.extname(requestedServerPath)] || CONTENT_TYPES['.bin']);
if (response.finished) {
return;
}
response.writeHead(
200,
{
'Content-Length': contentLength,
'Content-Type': contentType
}
);
if (requestedMethod === 'HEAD') {
response.end();
}
else {
response.end(FS.readFileSync(requestedServerPath));
}
}
HTTP
.createServer(serve)
.on('clientError', onClientError)
.on('error', onError)
.listen(CONFIGURATION.serverPort, () => {
const host = 'localhost:' + CONFIGURATION.serverPort;
const hostURL = 'http://' + host + '/tests_results.html';
console.info(`Listening on ${host}...`);
switch (process.platform) {
default:
ChildProcess.exec('xdg-open ' + hostURL);
break;
case 'darwin':
ChildProcess.exec('open ' + hostURL);
break;
case 'win32':
ChildProcess.exec('cmd /c start ' + hostURL);
break;
}
});