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.
|
|
|
|
*/
|
|
|
|
|
2022-04-18 19:20:49 -08:00
|
|
|
import { debug, wsServer } from '../utilsBundle';
|
|
|
|
import type { WebSocketServer } from '../utilsBundle';
|
2022-07-31 14:31:17 -07:00
|
|
|
import http from 'http';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Browser } from '../server/browser';
|
2022-07-31 14:31:17 -07:00
|
|
|
import type { Playwright } from '../server/playwright';
|
|
|
|
import { createPlaywright } from '../server/playwright';
|
2022-04-05 14:47:11 -07:00
|
|
|
import { PlaywrightConnection } from './playwrightConnection';
|
2022-07-31 14:31:17 -07:00
|
|
|
import { assert } from '../utils';
|
2022-08-02 21:09:39 -07:00
|
|
|
import { serverSideCallMetadata } from '../server/instrumentation';
|
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}`);
|
|
|
|
}
|
|
|
|
|
2022-07-31 14:31:17 -07:00
|
|
|
export type Mode = 'use-pre-launched-browser' | 'reuse-browser' | 'auto';
|
|
|
|
|
|
|
|
type ServerOptions = {
|
|
|
|
path: string;
|
|
|
|
maxClients: number;
|
|
|
|
enableSocksProxy: boolean;
|
|
|
|
preLaunchedBrowser?: Browser
|
|
|
|
};
|
|
|
|
|
2020-11-20 15:19:39 -08:00
|
|
|
export class PlaywrightServer {
|
2022-07-31 14:31:17 -07:00
|
|
|
private _preLaunchedPlaywright: Playwright | null = null;
|
2022-04-18 19:20:49 -08:00
|
|
|
private _wsServer: WebSocketServer | undefined;
|
2021-04-12 11:14:54 -07:00
|
|
|
private _clientsCount = 0;
|
2022-07-31 14:31:17 -07:00
|
|
|
private _mode: Mode;
|
|
|
|
private _options: ServerOptions;
|
2021-04-12 11:14:54 -07:00
|
|
|
|
2022-07-31 14:31:17 -07:00
|
|
|
constructor(mode: Mode, options: ServerOptions) {
|
|
|
|
this._mode = mode;
|
|
|
|
this._options = options;
|
|
|
|
if (mode === 'use-pre-launched-browser') {
|
|
|
|
assert(options.preLaunchedBrowser);
|
|
|
|
this._preLaunchedPlaywright = options.preLaunchedBrowser.options.rootSdkObject as Playwright;
|
|
|
|
}
|
|
|
|
if (mode === 'reuse-browser')
|
|
|
|
this._preLaunchedPlaywright = createPlaywright('javascript');
|
2021-04-12 11:14:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async listen(port: number = 0): Promise<string> {
|
2022-08-02 21:09:39 -07:00
|
|
|
if (this._mode === 'reuse-browser') {
|
|
|
|
const callMetadata = serverSideCallMetadata();
|
|
|
|
const browser = await this._preLaunchedPlaywright!.chromium.launch(callMetadata, { headless: false });
|
|
|
|
const { context } = await browser.newContextForReuse({ viewport: { width: 800, height: 600 } }, callMetadata);
|
|
|
|
const page = await context.newPage(callMetadata);
|
|
|
|
await page.mainFrame().setContent(callMetadata, `
|
|
|
|
<style>
|
|
|
|
html, body {
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
body {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<div>Playwright will use this page to run tests</div>`);
|
|
|
|
}
|
|
|
|
|
2021-04-12 11:14:54 -07:00
|
|
|
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-07-31 14:31:17 -07:00
|
|
|
const wsEndpoint = typeof address === 'string' ? `${address}${this._options.path}` : `ws://127.0.0.1:${address.port}${this._options.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-07-31 14:31:17 -07:00
|
|
|
this._wsServer = new wsServer({ server, path: this._options.path });
|
2022-02-10 16:36:23 -08:00
|
|
|
const originalShouldHandle = this._wsServer.shouldHandle.bind(this._wsServer);
|
2022-07-31 14:31:17 -07:00
|
|
|
this._wsServer.shouldHandle = request => originalShouldHandle(request) && this._clientsCount < this._options.maxClients;
|
2022-02-10 16:36:23 -08:00
|
|
|
this._wsServer.on('connection', async (ws, request) => {
|
2022-07-31 14:31:17 -07:00
|
|
|
if (this._clientsCount >= this._options.maxClients) {
|
2022-02-10 16:36:23 -08:00
|
|
|
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'];
|
2022-07-31 14:31:17 -07:00
|
|
|
const browserAlias = url.searchParams.get('browser') || (Array.isArray(browserHeader) ? browserHeader[0] : browserHeader) || null;
|
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);
|
2022-07-31 14:31:17 -07:00
|
|
|
const enableSocksProxy = this._options.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-07-31 14:31:17 -07:00
|
|
|
const connection = new PlaywrightConnection(
|
|
|
|
this._mode, ws,
|
|
|
|
{ enableSocksProxy, browserAlias, headless: headlessValue !== '0' },
|
|
|
|
{ playwright: this._preLaunchedPlaywright, browser: this._options.preLaunchedBrowser || null },
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|