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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
// @ts-nocheck
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const http = require('http');
const https = require('https');
const url = require('url');
const fs = require('fs');
const path = require('path');
const mime = require('mime');
const WebSocketServer = require('ws').Server;
const fulfillSymbol = Symbol('fullfil callback');
const rejectSymbol = Symbol('reject callback');
class TestServer {
PORT = undefined;
PREFIX = undefined;
CROSS_PROCESS_PREFIX = undefined;
EMPTY_PAGE = undefined;
/**
* @param {string} dirPath
* @param {number} port
* @returns {!TestServer}
*/
static async create(dirPath, port) {
const server = new TestServer(dirPath, port);
await new Promise((x) => server._server.once('listening', x));
return server;
}
/**
* @param {string} dirPath
* @param {number} port
* @returns {!TestServer}
*/
static async createHTTPS(dirPath, port) {
const server = new TestServer(dirPath, port, {
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem')),
passphrase: 'aaaa',
});
await new Promise((x) => server._server.once('listening', x));
return server;
}
/**
* @param {string} dirPath
* @param {number} port
* @param {!Object=} sslOptions
*/
constructor(dirPath, port, sslOptions) {
if (sslOptions)
this._server = https.createServer(sslOptions, this._onRequest.bind(this));
else this._server = http.createServer(this._onRequest.bind(this));
this._server.on('connection', (socket) => this._onSocket(socket));
this._wsServer = new WebSocketServer({ server: this._server });
this._wsServer.on('connection', this._onWebSocketConnection.bind(this));
this._server.listen(port);
this._dirPath = dirPath;
this._startTime = new Date();
this._cachedPathPrefix = null;
/** @type {!Set<!net.Socket>} */
this._sockets = new Set();
/** @type {!Map<string, function(!IncomingMessage, !ServerResponse)>} */
this._routes = new Map();
/** @type {!Map<string, !{username:string, password:string}>} */
this._auths = new Map();
/** @type {!Map<string, string>} */
this._csp = new Map();
/** @type {!Set<string>} */
this._gzipRoutes = new Set();
/** @type {!Map<string, !Promise>} */
this._requestSubscribers = new Map();
}
_onSocket(socket) {
this._sockets.add(socket);
// ECONNRESET is a legit error given
// that tab closing simply kills process.
socket.on('error', (error) => {
if (error.code !== 'ECONNRESET') throw error;
});
socket.once('close', () => this._sockets.delete(socket));
}
/**
* @param {string} pathPrefix
*/
enableHTTPCache(pathPrefix) {
this._cachedPathPrefix = pathPrefix;
}
/**
* @param {string} path
* @param {string} username
* @param {string} password
*/
setAuth(path, username, password) {
this._auths.set(path, { username, password });
}
enableGzip(path) {
this._gzipRoutes.add(path);
}
/**
* @param {string} path
* @param {string} csp
*/
setCSP(path, csp) {
this._csp.set(path, csp);
}
async stop() {
this.reset();
for (const socket of this._sockets) socket.destroy();
this._sockets.clear();
await new Promise((x) => this._server.close(x));
}
/**
* @param {string} path
* @param {function(!IncomingMessage, !ServerResponse)} handler
*/
setRoute(path, handler) {
this._routes.set(path, handler);
}
/**
* @param {string} from
* @param {string} to
*/
setRedirect(from, to) {
this.setRoute(from, (req, res) => {
res.writeHead(302, { location: to });
res.end();
});
}
/**
* @param {string} path
* @returns {!Promise<!IncomingMessage>}
*/
waitForRequest(path) {
let promise = this._requestSubscribers.get(path);
if (promise) return promise;
let fulfill, reject;
promise = new Promise((f, r) => {
fulfill = f;
reject = r;
});
promise[fulfillSymbol] = fulfill;
promise[rejectSymbol] = reject;
this._requestSubscribers.set(path, promise);
return promise;
}
reset() {
this._routes.clear();
this._auths.clear();
this._csp.clear();
this._gzipRoutes.clear();
const error = new Error('Static Server has been reset');
for (const subscriber of this._requestSubscribers.values())
subscriber[rejectSymbol].call(null, error);
this._requestSubscribers.clear();
}
_onRequest(request, response) {
request.on('error', (error) => {
if (error.code === 'ECONNRESET') response.end();
else throw error;
});
request.postBody = new Promise((resolve) => {
let body = '';
request.on('data', (chunk) => (body += chunk));
request.on('end', () => resolve(body));
});
const pathName = url.parse(request.url).path;
if (this._auths.has(pathName)) {
const auth = this._auths.get(pathName);
const credentials = Buffer.from(
(request.headers.authorization || '').split(' ')[1] || '',
'base64'
).toString();
if (credentials !== `${auth.username}:${auth.password}`) {
response.writeHead(401, {
'WWW-Authenticate': 'Basic realm="Secure Area"',
});
response.end('HTTP Error 401 Unauthorized: Access is denied');
return;
}
}
// Notify request subscriber.
if (this._requestSubscribers.has(pathName)) {
this._requestSubscribers.get(pathName)[fulfillSymbol].call(null, request);
this._requestSubscribers.delete(pathName);
}
const handler = this._routes.get(pathName);
if (handler) {
handler.call(null, request, response);
} else {
const pathName = url.parse(request.url).path;
this.serveFile(request, response, pathName);
}
}
/**
* @param {!IncomingMessage} request
* @param {!ServerResponse} response
* @param {string} pathName
*/
serveFile(request, response, pathName) {
if (pathName === '/') pathName = '/index.html';
const filePath = path.join(this._dirPath, pathName.substring(1));
if (
this._cachedPathPrefix !== null &&
filePath.startsWith(this._cachedPathPrefix)
) {
if (request.headers['if-modified-since']) {
response.statusCode = 304; // not modified
response.end();
return;
}
response.setHeader('Cache-Control', 'public, max-age=31536000');
response.setHeader('Last-Modified', this._startTime.toISOString());
} else {
response.setHeader('Cache-Control', 'no-cache, no-store');
}
if (this._csp.has(pathName))
response.setHeader('Content-Security-Policy', this._csp.get(pathName));
fs.readFile(filePath, (err, data) => {
if (err) {
response.statusCode = 404;
response.end(`File not found: ${filePath}`);
return;
}
const mimeType = mime.getType(filePath);
const isTextEncoding = /^text\/|^application\/(javascript|json)/.test(
mimeType
);
const contentType = isTextEncoding
? `${mimeType}; charset=utf-8`
: mimeType;
response.setHeader('Content-Type', contentType);
if (this._gzipRoutes.has(pathName)) {
response.setHeader('Content-Encoding', 'gzip');
const zlib = require('zlib');
zlib.gzip(data, (_, result) => {
response.end(result);
});
} else {
response.end(data);
}
});
}
_onWebSocketConnection(connection) {
connection.send('opened');
}
}
module.exports = { TestServer };
|