-
Notifications
You must be signed in to change notification settings - Fork 648
/
same-thread-server.ts
532 lines (437 loc) · 16.5 KB
/
same-thread-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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/**
* @fileoverview Simple HTTP server used in hint's tests to mimick certain scenarios.
*/
import * as http from 'http';
import * as https from 'https';
import forEach = require('lodash/forEach');
import random = require('lodash/random');
import * as express from 'express';
// to work with option 'strict', we can't use import.
const onHeaders = require('on-headers');
import { normalizeString } from '@hint/utils-string';
import { normalizeHeaderValue } from '@hint/utils-network';
import { IServer, Message, ServerConfiguration, WebhintMessage } from './types';
import { replacer, reviver } from './buffer-serialization';
/**
* List of ports in our range that are unsafe for Chrome.
* See: https://src.chromium.org/viewvc/chrome/trunk/src/net/base/net_util.cc?view=markup
*/
const unsafePorts = [3659, 4045, 6000, 6665, 6666, 6667, 6668, 6669];
/** A testing server for webhint's hints */
/* istanbul ignore next */
export class SameThreadServer implements IServer {
private static usedPorts = new Set(unsafePorts);
private static maxPort: number = 65535;
private static minPort: number = 3000;
private _app: express.Application;
private _server: https.Server | http.Server = {} as http.Server;
private _port: number;
private _isHTTPS: boolean | undefined;
private static readonly _cert: string =
`-----BEGIN CERTIFICATE-----
MIIDBTCCAe2gAwIBAgIJAPrIPsJ7DJ0SMA0GCSqGSIb3DQEBBQUAMBkxFzAVBgNV
BAMMDmxvY2FsaG9zdDo4NDQzMB4XDTE3MDcwNzE3MTI0OVoXDTI3MDcwNTE3MTI0
OVowGTEXMBUGA1UEAwwObG9jYWxob3N0Ojg0NDMwggEiMA0GCSqGSIb3DQEBAQUA
A4IBDwAwggEKAoIBAQDmIlQAv6ZAGrEg3cHP+NpfqQV+LS0WxSczPAcZmDXJea5e
UAt7Gc9gI7ULIHW68TJ8rGVdZKKriPqXmG/vY9+u/UZ1IwcQZWMTPTDL8Ku1/lDj
0zxO3avujEnHGcgp1RdfS0TsigS5rSWmx+dt9a7zbn1oCh2l6e1HeFd0NF15tXZG
shL5CXxAip6hhZ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BOEFirB4Zalji9datqhfWv3HvMqLDrEHMWAhD9ahQv1TUkGVxZ/N
xnh64tuptfPC78Ylw+rEsUOyuWSmi1yzAxQRxaNKmAw7xZ8dPqG/6SLm5B0xYA46
IsW9AGST1xe4XVCLy+FIoo1RVpfJyp8h9zSzDASh/F1+5DY1PUJQ
-----END RSA PRIVATE KEY-----`;
private getNewValidPort(): number {
let port = random(SameThreadServer.minPort, SameThreadServer.maxPort);
while (SameThreadServer.usedPorts.has(port)) {
port = random(SameThreadServer.minPort, SameThreadServer.maxPort);
}
return port;
}
public constructor(isHTTPS?: boolean) {
this._port = this.getNewValidPort();
this._app = express();
this._app.disable('x-powered-by');
this._app.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-cache');
next();
});
this._isHTTPS = isHTTPS;
}
/**
* Because we don't know the port until we start the server, we need to update
* the references to http://localhost in the HTML to http://localhost:finalport.
*/
private updateLocalhost(html: string): string {
return html.replace(/\/\/localhost\//g, `//localhost:${this._port}/`);
}
private handleHeaders(res: express.Response, headers: any) {
onHeaders(res, () => {
Object.entries(headers).forEach(([header, value]) => {
if (value !== null) {
res.setHeader(header, value as string);
} else {
res.removeHeader(header);
}
});
});
}
private getContent(value: any): string {
if (typeof value === 'string') {
return this.updateLocalhost(value);
} else if (value && typeof value.content !== 'undefined') {
return typeof value.content === 'string' ? this.updateLocalhost(value.content) : value.content;
}
return '';
}
private getNumberOfMatches(req: express.Request, requestConditions: any) {
const headers = requestConditions.request && requestConditions.request.headers;
/*
* Matching is done only based on headers, as for the time
* beeing there is no need to match based on other things.
*/
if (!headers) {
return 0;
}
let numberOfMatches = 0;
for (const [header, value] of Object.entries(headers)) {
// TODO: handle `string[]` in `req.headers`
const headerValue = normalizeHeaderValue(req.headers as any, header);
if ((headerValue !== normalizeString(value as string)) || (!headerValue && (value === null))) {
return 0;
}
numberOfMatches++;
}
return numberOfMatches;
}
private getValue(req: express.Request, config: any) {
let bestNumberOfMatches = 1;
let bestMatch = null;
for (const [key, value] of Object.entries(config)) {
let requestConditions;
try {
requestConditions = JSON.parse(key);
const newNumberOfMatches = this.getNumberOfMatches(req, requestConditions);
if (newNumberOfMatches >= bestNumberOfMatches) {
bestMatch = value;
bestNumberOfMatches = newNumberOfMatches;
}
} catch (e) {
// Ignore invalid keys.
}
}
return bestMatch;
}
private isConditionalConfig(configuration: any): boolean {
/*
* The following is done to quickly determine the type of
* configuration. Possible options are:
*
* 1) Simple config
* (server response is always the same)
*
* {
* name: ...,
* reports: [{ message: ... }],
* serverConfig: {
* '/': {
* content: ...,
* headers: ...
* },
* ...
* }
* }
*
*
* 2) Conditional config
* (server response depends on the request)
*
* {
* name: ...
* reports: [{ message: ... }],
* serverConfig: {
* [JSON.stringify({
* headers: {
* ...
* }
* })]: {
* '/': {
* content: ...,
* headers: ...
* },
* },
* ...
* }
* }
*/
try {
return typeof JSON.parse(Object.entries(configuration)[0][0]) === 'object';
} catch (e) {
// Ignore.
}
return false;
}
private normalizeConfig(configuration: any) {
const config: any = {};
/*
* This function convers something such as:
*
* {
* '{"request":{"headers":{"Accept-Encoding":"gzip"}}}': {
* '/': {
* content: ...
* headers: ...
* },
* ...
* }
* '{"request":{"headers":{"Accept-Encoding":"br"}}}': {
* '/': {
* content: ...
* headers: ...
* },
* ...
* }
* ...
* }
*
* to
*
* {
* '/': {
* '{"request":{"headers":{"Accept-Encoding":"gzip"}}}': {
* content: ...
* headers: ...
* },
* '{"request":{"headers":{"Accept-Encoding":"br"}}}': {
* content: ...
* headers: ...
* },
* ...
* }
* ...
* }
*
*/
for (const [k, v] of Object.entries(configuration)) {
for (const [key, value] of Object.entries(v as {})) {
config[key] = {
...config[key],
...{ [k]: value }
};
}
}
return config;
}
/** Applies the configuration for routes to the server. */
public configure(configuration: ServerConfiguration) {
let customFavicon = false;
if (typeof configuration === 'string') {
this._app.get('/', (req, res) => {
res.send(configuration);
});
return;
}
const conditionalConfig = this.isConditionalConfig(configuration);
const config = conditionalConfig ? this.normalizeConfig(configuration) : configuration;
forEach(config, (val, key) => {
customFavicon = customFavicon || key === '/favicon.ico';
this._app.get(key, (req, res) => {
const value: any = conditionalConfig ? this.getValue(req, val) : val;
const content = this.getContent(value);
/*
* Hacky way to make `request` fail, but required
* for testing cases such as the internet connection
* being down when a particular request is made.
*/
if (value === null) {
res.redirect(301, 'test://fa.il');
return;
}
const redirects = [301, 302, 303, 307, 308];
if (value && redirects.includes(value.status)) {
res.redirect(value.status, content);
return;
}
if (value && value.auth) {
if (req.headers.authorization !== value.auth) {
res.setHeader('WWW-Authorization', 'Basic realm="testing", charset="utf-8"');
res.send();
return;
}
}
if (value && value.status) {
res.status(value.status);
}
if (value && value.headers) {
this.handleHeaders(res, value.headers);
}
if (content !== 'timeout') { // if `content === 'timeout'`, leaves the server hanging to test the implementation of timeout.
res.send(content);
}
});
});
if (!customFavicon) {
this._app.get('/favicon.ico', (req, res) => {
res.status(200);
res.setHeader('Content-Length', '0');
res.setHeader('Content-Type', 'image/x-icon');
res.end();
});
}
}
/** Starts listening on the given port. */
public start(): Promise<null> {
return new Promise((resolve, reject) => {
let options;
if (this._isHTTPS) {
options = {
cert: SameThreadServer._cert,
key: SameThreadServer._key
};
this._server = https.createServer(options, this._app);
} else {
this._server = http.createServer(this._app);
}
// TODO: need to find a way to cast `err` to a [System Error](https://nodejs.org/dist/latest-v7.x/docs/api/errors.html#errors_system_errors)
this._server.on('error', (err: any) => {
// Need to use `console.log` because otherwise we don't get the output
console.log(`Server Error: ${err.code}`);
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
setImmediate(() => {
this._port = this.getNewValidPort();
this._server.close();
this._server.listen(this._port);
});
} else {
reject(err);
}
});
this._server.once('listening', () => {
resolve();
});
this._server.listen(this._port);
});
}
/** Stops the server and frees the port. */
public stop() {
this._server.close();
return Promise.resolve(null);
}
public getPort() {
return this._port;
}
public get port() {
return this._port;
}
}
/**
* We assume that if there's something it means it should be https:
*
* `node server.js https` == `node server.js something`
*/
const useHttps = !!process.argv[2];
const server = new SameThreadServer(useHttps);
type Action = (message: WebhintMessage) => Promise<any>;
/** Wrapper to do the filtering of actions based on the type */
/* istanbul ignore next */
const action = (fn: Action) => {
const type = fn.name;
return async (message: Message) => {
if (message.webhint.type !== type) {
return false;
}
const payload = await fn(message.webhint);
if (process.send) {
const message = {
webhint: {
payload,
type
}
} as Message;
const serializedMessage = JSON.stringify(message, replacer);
process.send(serializedMessage);
}
return true;
};
};
/* istanbul ignore next */
const start = (message: WebhintMessage) => {
return server.start();
};
/* istanbul ignore next */
const stop = (message: WebhintMessage) => {
// TODO: Disconnect and stop process?
return server.stop();
};
/* istanbul ignore next */
const port = (message: WebhintMessage) => {
return Promise.resolve(server.port);
};
/* istanbul ignore next */
const configure = (message: WebhintMessage) => {
const { payload } = message;
server.configure(payload as ServerConfiguration);
return Promise.resolve();
};
/* istanbul ignore next */
const serverActions = [
action(port),
action(start),
action(configure),
action(stop)
];
process.on('message', /* istanbul ignore next */ async (serializedMessage: string) => {
const message = typeof serializedMessage === 'string' ?
JSON.parse(serializedMessage, reviver) :
serializedMessage;
if (!message.webhint) {
// Message is not for us
return;
}
const dupe = Array.from(serverActions);
let handled = false;
while (!handled) {
const action = dupe.shift();
if (!action) {
break;
}
handled = await action(message);
if (handled) {
break;
}
}
});