-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.ts
178 lines (155 loc) · 5.03 KB
/
server.ts
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { exec } from 'child_process';
import { readFile } from 'fs';
import { createServer, IncomingMessage, Server as HttpServer, ServerResponse } from 'http';
import { Socket } from 'net';
import { type } from 'os';
import { join } from 'path';
import { IPlotsContainer } from './models';
export class Server {
private instance: HttpServer;
private plotsContainer: IPlotsContainer = {};
private port: number;
private sockets: {[id: number]: Socket} = {};
private nextSocketID = 0;
constructor(port: number) {
this.port = port;
this.instance = this.createServer();
this.instance.on('connection', (socket: Socket) => {
const id = this.nextSocketID++;
this.sockets[id] = socket;
socket.on('close', () => {
delete this.sockets[id];
});
});
}
/**
* Updates the plotdata, decides make the server listen again
* and opens a new browser window targetting the webservers
* data address.
*/
public spawn(plotsContainer: IPlotsContainer) {
this.plotsContainer = plotsContainer;
if (!this.instance.address()) {
this.instance.listen(this.port);
}
this.openBrowserWindow();
}
/**
* Closes the webserver, destroys all connected sockets
* and clears the plots container.
*/
public clean() {
if (this.instance.address()) {
this.instance.close();
}
for (const socket of Object.values(this.sockets)) {
socket.destroy();
}
this.plotsContainer = {};
}
/**
* Opens the browser window using the opn-NPM module and
* marks the container flag as pending. This means the website
* does not have got its data yet.
*/
private openBrowserWindow() {
for (const plotEntry of Object.entries(this.plotsContainer)) {
if (!plotEntry[1].opened && !plotEntry[1].pending) {
plotEntry[1].pending = true;
this.openWindow(`http://localhost:${this.port}/plots/${plotEntry[0]}/index.html`);
}
}
}
private openWindow(location: string) {
switch (type()) {
case 'Linux': exec(`xdg-open ${location}`); break;
case 'Darwin': exec(`open ${location}`); break;
case 'Windows_NT': exec(`start ${location}`); break;
}
}
/**
* Creates the Webserver instance
*/
private createServer(): HttpServer {
return createServer((req, res) => {
if (!this.serveData(req, res)) {
this.serveWebsite(req, res); // only serve website if request was not served from serveData
}
});
}
/**
* Serves the plot data at /data/:id of the container[id].
* It markes the container as opened and not pending anymore.
* @param req
* @param res
* @returns {boolean} - Whether the request was served or not
*/
private serveData(req: IncomingMessage, res: ServerResponse) {
if (req && req.url && req.url.match(/data\/[0-9]+/)) {
const segments = req.url.split('/');
const id = +segments[segments.length - 1];
const container = this.plotsContainer[id];
const temporaryPlots = container && container.plots;
this.plotsContainer[id].opened = true;
this.plotsContainer[id].pending = false;
res.end(JSON.stringify(temporaryPlots));
this.close();
return true; // request successfully server
}
return false; // request not served
}
/**
* Serves the website at http://localhost:PORT/plots/:id/index.html
* @param req
* @param res
*/
private serveWebsite(req: IncomingMessage, res: ServerResponse) {
if (req && req.url && req.url.match(/plots\/[0-9]+\/index.html/)) {
const segments = req.url.split('/');
const id = segments[segments.length - 2];
readFile(join(__dirname, '..', 'www', 'index.html'), 'utf-8', (error, file) => {
if (error) {
res.writeHead(404);
res.end(JSON.stringify(error));
return;
}
file = file.replace('{{plotid}}', id);
res.writeHead(200);
res.end(file);
});
} else if (req && req.url && (req.url.match(/nodeplotlib.min.js/) || req.url && req.url.match(/plotly.min.js/))) {
const segments = req.url.split('/');
const script = segments[segments.length - 1];
readFile(join(__dirname, '..', 'www', script), 'utf-8', (err, file) => {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
if (script === 'nodeplotlib.min.js') {
file = file.replace('{{port}}', `${this.port}`);
}
res.setHeader('content-type', 'text/javascript');
res.writeHead(200);
res.end(file);
});
} else {
res.writeHead(404);
res.end('Server address not found');
return;
}
}
/**
* Closes the webserver if there are no more pending plots
* and all plots were opened
*/
private close() {
const pending = Object.values(this.plotsContainer)
.reduce((a, b) => a || b.pending, false);
const opened = Object.values(this.plotsContainer)
.reduce((a, b) => a && b.opened, true);
if (this.instance && !pending && opened) {
this.clean();
}
}
}