summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/packages/testserver/src/index.ts
blob: 2618fd4d0de64e7cc1c7869835d2508850f26927 (plain)
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
/**
 * @license
 * Copyright 2017 Google Inc.
 * SPDX-License-Identifier: Apache-2.0
 */

import assert from 'assert';
import {readFile, readFileSync} from 'fs';
import {
  createServer as createHttpServer,
  type IncomingMessage,
  type RequestListener,
  type Server as HttpServer,
  type ServerResponse,
} from 'http';
import {
  createServer as createHttpsServer,
  type Server as HttpsServer,
  type ServerOptions as HttpsServerOptions,
} from 'https';
import type {AddressInfo} from 'net';
import {join} from 'path';
import type {Duplex} from 'stream';
import {gzip} from 'zlib';

import {getType as getMimeType} from 'mime';
import {Server as WebSocketServer, type WebSocket} from 'ws';

interface Subscriber {
  resolve: (msg: IncomingMessage) => void;
  reject: (err?: Error) => void;
  promise: Promise<IncomingMessage>;
}

type TestIncomingMessage = IncomingMessage & {postBody?: Promise<string>};

export class TestServer {
  PORT!: number;
  PREFIX!: string;
  CROSS_PROCESS_PREFIX!: string;
  EMPTY_PAGE!: string;

  #dirPath: string;
  #server: HttpsServer | HttpServer;
  #wsServer: WebSocketServer;

  #startTime = new Date();
  #cachedPathPrefix?: string;

  #connections = new Set<Duplex>();
  #routes = new Map<
    string,
    (msg: IncomingMessage, res: ServerResponse) => void
  >();
  #auths = new Map<string, {username: string; password: string}>();
  #csp = new Map<string, string>();
  #gzipRoutes = new Set<string>();
  #requestSubscribers = new Map<string, Subscriber>();
  #requests = new Set<ServerResponse>();

  static async create(dirPath: string): Promise<TestServer> {
    let res!: (value: unknown) => void;
    const promise = new Promise(resolve => {
      res = resolve;
    });
    const server = new TestServer(dirPath);
    server.#server.once('listening', res);
    server.#server.listen(0);
    await promise;
    return server;
  }

  static async createHTTPS(dirPath: string): Promise<TestServer> {
    let res!: (value: unknown) => void;
    const promise = new Promise(resolve => {
      res = resolve;
    });
    const server = new TestServer(dirPath, {
      key: readFileSync(join(__dirname, '..', 'key.pem')),
      cert: readFileSync(join(__dirname, '..', 'cert.pem')),
      passphrase: 'aaaa',
    });
    server.#server.once('listening', res);
    server.#server.listen(0);
    await promise;
    return server;
  }

  constructor(dirPath: string, sslOptions?: HttpsServerOptions) {
    this.#dirPath = dirPath;

    if (sslOptions) {
      this.#server = createHttpsServer(sslOptions, this.#onRequest);
    } else {
      this.#server = createHttpServer(this.#onRequest);
    }
    this.#server.on('connection', this.#onServerConnection);
    // Disable this as sometimes the socket will timeout
    // We rely on the fact that we will close the server at the end
    this.#server.keepAliveTimeout = 0;
    this.#wsServer = new WebSocketServer({server: this.#server});
    this.#wsServer.on('connection', this.#onWebSocketConnection);
  }

  #onServerConnection = (connection: Duplex): void => {
    this.#connections.add(connection);
    // ECONNRESET is a legit error given
    // that tab closing simply kills process.
    connection.on('error', error => {
      if ((error as NodeJS.ErrnoException).code !== 'ECONNRESET') {
        throw error;
      }
    });
    connection.once('close', () => {
      return this.#connections.delete(connection);
    });
  };

  get port(): number {
    return (this.#server.address() as AddressInfo).port;
  }

  enableHTTPCache(pathPrefix: string): void {
    this.#cachedPathPrefix = pathPrefix;
  }

  setAuth(path: string, username: string, password: string): void {
    this.#auths.set(path, {username, password});
  }

  enableGzip(path: string): void {
    this.#gzipRoutes.add(path);
  }

  setCSP(path: string, csp: string): void {
    this.#csp.set(path, csp);
  }

  async stop(): Promise<void> {
    this.reset();
    for (const socket of this.#connections) {
      socket.destroy();
    }
    this.#connections.clear();
    await new Promise(x => {
      return this.#server.close(x);
    });
  }

  setRoute(
    path: string,
    handler: (req: IncomingMessage, res: ServerResponse) => void
  ): void {
    this.#routes.set(path, handler);
  }

  setRedirect(from: string, to: string): void {
    this.setRoute(from, (_, res) => {
      res.writeHead(302, {location: to});
      res.end();
    });
  }

  waitForRequest(path: string): Promise<TestIncomingMessage> {
    const subscriber = this.#requestSubscribers.get(path);
    if (subscriber) {
      return subscriber.promise;
    }
    let resolve!: (value: IncomingMessage) => void;
    let reject!: (reason?: Error) => void;
    const promise = new Promise<IncomingMessage>((res, rej) => {
      resolve = res;
      reject = rej;
    });
    this.#requestSubscribers.set(path, {resolve, reject, promise});
    return promise;
  }

  reset(): void {
    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.reject.call(undefined, error);
    }
    this.#requestSubscribers.clear();
    for (const request of this.#requests.values()) {
      if (!request.writableEnded) {
        request.end();
      }
    }
    this.#requests.clear();
  }

  #onRequest: RequestListener = (
    request: TestIncomingMessage,
    response
  ): void => {
    this.#requests.add(response);

    request.on('error', (error: {code: string}) => {
      if (error.code === 'ECONNRESET') {
        response.end();
      } else {
        throw error;
      }
    });
    request.postBody = new Promise(resolve => {
      let body = '';
      request.on('data', (chunk: string) => {
        return (body += chunk);
      });
      request.on('end', () => {
        return resolve(body);
      });
    });
    assert(request.url);
    const url = new URL(request.url, `https://${request.headers.host}`);
    const path = url.pathname + url.search;
    const auth = this.#auths.get(path);
    if (auth) {
      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;
      }
    }
    const subscriber = this.#requestSubscribers.get(path);
    if (subscriber) {
      subscriber.resolve.call(undefined, request);
      this.#requestSubscribers.delete(path);
    }
    const handler = this.#routes.get(path);
    if (handler) {
      handler.call(undefined, request, response);
    } else {
      this.serveFile(request, response, path);
    }
  };

  serveFile(
    request: IncomingMessage,
    response: ServerResponse,
    pathName: string
  ): void {
    if (pathName === '/') {
      pathName = '/index.html';
    }
    const filePath = join(this.#dirPath, pathName.substring(1));

    if (this.#cachedPathPrefix && 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');
    }
    const csp = this.#csp.get(pathName);
    if (csp) {
      response.setHeader('Content-Security-Policy', csp);
    }

    readFile(filePath, (err, data) => {
      // This can happen if the request is not awaited but started
      // in the test and get clean via `reset()`
      if (response.writableEnded) {
        return;
      }

      if (err) {
        response.statusCode = 404;
        response.end(`File not found: ${filePath}`);
        return;
      }
      const mimeType = getMimeType(filePath);
      if (mimeType) {
        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');
        gzip(data, (_, result) => {
          response.end(result);
        });
      } else {
        response.end(data);
      }
    });
  }

  #onWebSocketConnection = (socket: WebSocket): void => {
    socket.send('opened');
  };
}