2020-11-20 15:19:39 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-02-11 06:36:15 -08:00
|
|
|
import debug from 'debug';
|
2020-11-20 15:19:39 -08:00
|
|
|
import * as http from 'http';
|
2022-02-10 16:36:23 -08:00
|
|
|
import WebSocket from 'ws';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Browser } from '../server/browser';
|
2022-04-05 14:47:11 -07:00
|
|
|
import { PlaywrightConnection } from './playwrightConnection';
|
2020-11-20 15:19:39 -08:00
|
|
|
|
|
|
|
const debugLog = debug('pw:server');
|
|
|
|
|
2022-04-05 14:47:11 -07:00
|
|
|
let lastConnectionId = 0;
|
|
|
|
const kConnectionSymbol = Symbol('kConnection');
|
|
|
|
|
|
|
|
function newLogger() {
|
|
|
|
const id = ++lastConnectionId;
|
|
|
|
return (message: string) => debugLog(`[id=${id}] ${message}`);
|
|
|
|
}
|
|
|
|
|
2020-11-20 15:19:39 -08:00
|
|
|
export class PlaywrightServer {
|
2022-02-10 16:36:23 -08:00
|
|
|
private _path: string;
|
|
|
|
private _maxClients: number;
|
2022-02-14 15:10:58 -08:00
|
|
|
private _enableSocksProxy: boolean;
|
2022-02-10 16:36:23 -08:00
|
|
|
private _browser: Browser | undefined;
|
|
|
|
private _wsServer: WebSocket.Server | undefined;
|
2021-04-12 11:14:54 -07:00
|
|
|
private _clientsCount = 0;
|
|
|
|
|
2022-02-14 15:10:58 -08:00
|
|
|
static async startDefault(options: { path?: string, maxClients?: number, enableSocksProxy?: boolean } = {}): Promise<PlaywrightServer> {
|
|
|
|
const { path = '/ws', maxClients = 1, enableSocksProxy = true } = options;
|
|
|
|
return new PlaywrightServer(path, maxClients, enableSocksProxy);
|
2021-04-12 11:14:54 -07:00
|
|
|
}
|
2020-11-20 15:19:39 -08:00
|
|
|
|
2022-02-14 15:10:58 -08:00
|
|
|
constructor(path: string, maxClients: number, enableSocksProxy: boolean, browser?: Browser) {
|
2022-02-10 16:36:23 -08:00
|
|
|
this._path = path;
|
|
|
|
this._maxClients = maxClients;
|
2022-02-14 15:10:58 -08:00
|
|
|
this._enableSocksProxy = enableSocksProxy;
|
2022-02-10 16:36:23 -08:00
|
|
|
this._browser = browser;
|
2021-04-12 11:14:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async listen(port: number = 0): Promise<string> {
|
|
|
|
const server = http.createServer((request, response) => {
|
2020-11-20 15:19:39 -08:00
|
|
|
response.end('Running');
|
|
|
|
});
|
2021-04-12 11:14:54 -07:00
|
|
|
server.on('error', error => debugLog(error));
|
|
|
|
|
2021-06-02 14:35:17 -07:00
|
|
|
const wsEndpoint = await new Promise<string>((resolve, reject) => {
|
2021-04-12 11:14:54 -07:00
|
|
|
server.listen(port, () => {
|
|
|
|
const address = server.address();
|
2021-10-01 19:40:47 -07:00
|
|
|
if (!address) {
|
|
|
|
reject(new Error('Could not bind server socket'));
|
|
|
|
return;
|
|
|
|
}
|
2022-02-10 16:36:23 -08:00
|
|
|
const wsEndpoint = typeof address === 'string' ? `${address}${this._path}` : `ws://127.0.0.1:${address.port}${this._path}`;
|
2021-04-12 11:14:54 -07:00
|
|
|
resolve(wsEndpoint);
|
2021-06-02 14:35:17 -07:00
|
|
|
}).on('error', reject);
|
2021-04-12 11:14:54 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
debugLog('Listening at ' + wsEndpoint);
|
2020-11-20 15:19:39 -08:00
|
|
|
|
2022-02-10 16:36:23 -08:00
|
|
|
this._wsServer = new WebSocket.Server({ server, path: this._path });
|
|
|
|
const originalShouldHandle = this._wsServer.shouldHandle.bind(this._wsServer);
|
|
|
|
this._wsServer.shouldHandle = request => originalShouldHandle(request) && this._clientsCount < this._maxClients;
|
|
|
|
this._wsServer.on('connection', async (ws, request) => {
|
|
|
|
if (this._clientsCount >= this._maxClients) {
|
|
|
|
ws.close(1013, 'Playwright Server is busy');
|
2020-11-20 15:19:39 -08:00
|
|
|
return;
|
|
|
|
}
|
2022-04-05 14:47:11 -07:00
|
|
|
const url = new URL('http://localhost' + (request.url || ''));
|
|
|
|
const browserHeader = request.headers['x-playwright-browser'];
|
|
|
|
const browserAlias = url.searchParams.get('browser') || (Array.isArray(browserHeader) ? browserHeader[0] : browserHeader);
|
2022-04-07 10:19:56 -07:00
|
|
|
const headlessHeader = request.headers['x-playwright-headless'];
|
|
|
|
const headlessValue = url.searchParams.get('headless') || (Array.isArray(headlessHeader) ? headlessHeader[0] : headlessHeader);
|
2022-04-05 14:47:11 -07:00
|
|
|
const proxyHeader = request.headers['x-playwright-proxy'];
|
|
|
|
const proxyValue = url.searchParams.get('proxy') || (Array.isArray(proxyHeader) ? proxyHeader[0] : proxyHeader);
|
|
|
|
const enableSocksProxy = this._enableSocksProxy && proxyValue === '*';
|
2021-04-12 11:14:54 -07:00
|
|
|
this._clientsCount++;
|
2022-04-05 14:47:11 -07:00
|
|
|
const log = newLogger();
|
|
|
|
log(`serving connection: ${request.url}`);
|
2022-04-07 10:19:56 -07:00
|
|
|
const connection = new PlaywrightConnection(ws, enableSocksProxy, browserAlias, headlessValue !== '0', this._browser, log, () => this._clientsCount--);
|
2022-02-10 16:36:23 -08:00
|
|
|
(ws as any)[kConnectionSymbol] = connection;
|
2020-11-20 15:19:39 -08:00
|
|
|
});
|
2021-04-12 11:14:54 -07:00
|
|
|
|
|
|
|
return wsEndpoint;
|
2020-11-20 15:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
2022-02-10 16:36:23 -08:00
|
|
|
const server = this._wsServer;
|
|
|
|
if (!server)
|
2020-11-20 15:19:39 -08:00
|
|
|
return;
|
2022-02-10 16:36:23 -08:00
|
|
|
debugLog('closing websocket server');
|
|
|
|
const waitForClose = new Promise(f => server.close(f));
|
2021-04-16 17:07:56 -07:00
|
|
|
// First disconnect all remaining clients.
|
2022-02-10 16:36:23 -08:00
|
|
|
await Promise.all(Array.from(server.clients).map(async ws => {
|
2022-04-05 14:47:11 -07:00
|
|
|
const connection = (ws as any)[kConnectionSymbol] as PlaywrightConnection | undefined;
|
2022-02-10 16:36:23 -08:00
|
|
|
if (connection)
|
|
|
|
await connection.close();
|
|
|
|
try {
|
|
|
|
ws.terminate();
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}));
|
2022-02-01 21:27:34 +01:00
|
|
|
await waitForClose;
|
2022-02-10 16:36:23 -08:00
|
|
|
debugLog('closing http server');
|
|
|
|
await new Promise(f => server.options.server!.close(f));
|
2021-05-06 09:34:06 -07:00
|
|
|
this._wsServer = undefined;
|
2022-02-10 16:36:23 -08:00
|
|
|
debugLog('closed server');
|
|
|
|
}
|
|
|
|
}
|