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-03 17:32:29 -07:00
|
|
|
import type { LaunchOptions } from '../server/types';
|
2022-08-03 19:37:06 -07:00
|
|
|
import { ManualPromise } from '../utils/manualPromise';
|
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-09-15 15:53:18 -07:00
|
|
|
// TODO: replace 'reuse-browser' with 'allow-reuse' in 1.27.
|
2022-07-31 14:31:17 -07:00
|
|
|
export type Mode = 'use-pre-launched-browser' | 'reuse-browser' | 'auto';
|
|
|
|
|
|
|
|
type ServerOptions = {
|
|
|
|
path: string;
|
2022-08-03 19:37:06 -07:00
|
|
|
maxIncomingConnections: number;
|
|
|
|
maxConcurrentConnections: number;
|
2022-07-31 14:31:17 -07:00
|
|
|
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;
|
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;
|
|
|
|
}
|
2021-04-12 11:14:54 -07:00
|
|
|
}
|
|
|
|
|
2022-09-15 15:53:18 -07:00
|
|
|
preLaunchedPlaywright(): Playwright {
|
|
|
|
if (!this._preLaunchedPlaywright)
|
|
|
|
this._preLaunchedPlaywright = createPlaywright('javascript');
|
2022-08-05 19:34:57 -07:00
|
|
|
return this._preLaunchedPlaywright;
|
|
|
|
}
|
|
|
|
|
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-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-09-15 15:53:18 -07:00
|
|
|
const browserSemaphore = new Semaphore(this._options.maxConcurrentConnections);
|
|
|
|
const controllerSemaphore = new Semaphore(1);
|
2022-08-03 19:37:06 -07:00
|
|
|
this._wsServer.on('connection', (ws, request) => {
|
2022-09-15 15:53:18 -07:00
|
|
|
if (browserSemaphore.requested() >= this._options.maxIncomingConnections) {
|
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-08-04 15:04:00 -07:00
|
|
|
const browserName = url.searchParams.get('browser') || (Array.isArray(browserHeader) ? browserHeader[0] : browserHeader) || null;
|
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 === '*';
|
2022-08-03 17:32:29 -07:00
|
|
|
|
|
|
|
const launchOptionsHeader = request.headers['x-playwright-launch-options'] || '';
|
|
|
|
let launchOptions: LaunchOptions = {};
|
|
|
|
try {
|
|
|
|
launchOptions = JSON.parse(Array.isArray(launchOptionsHeader) ? launchOptionsHeader[0] : launchOptionsHeader);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
|
2022-04-05 14:47:11 -07:00
|
|
|
const log = newLogger();
|
|
|
|
log(`serving connection: ${request.url}`);
|
2022-09-15 15:53:18 -07:00
|
|
|
const isReuseControllerClient = !!request.headers['x-playwright-reuse-controller'];
|
|
|
|
const semaphore = isReuseControllerClient ? controllerSemaphore : browserSemaphore;
|
|
|
|
|
|
|
|
// If we started in the legacy reuse-browser mode, create this._preLaunchedPlaywright.
|
|
|
|
// If we get a reuse-controller request, create this._preLaunchedPlaywright.
|
|
|
|
if (isReuseControllerClient || (this._mode === 'reuse-browser') && !this._preLaunchedPlaywright)
|
|
|
|
this.preLaunchedPlaywright();
|
|
|
|
|
|
|
|
// If we have a playwright to reuse, consult controller for reuse mode.
|
|
|
|
let mode = this._mode;
|
|
|
|
if (mode === 'auto' && this._preLaunchedPlaywright?.reuseController.reuseBrowser())
|
|
|
|
mode = 'reuse-browser';
|
|
|
|
|
|
|
|
if (mode === 'reuse-browser')
|
|
|
|
semaphore.setMax(1);
|
|
|
|
else
|
|
|
|
semaphore.setMax(this._options.maxConcurrentConnections);
|
|
|
|
|
2022-07-31 14:31:17 -07:00
|
|
|
const connection = new PlaywrightConnection(
|
2022-08-03 19:37:06 -07:00
|
|
|
semaphore.aquire(),
|
2022-09-15 15:53:18 -07:00
|
|
|
mode, ws, isReuseControllerClient,
|
2022-08-04 15:04:00 -07:00
|
|
|
{ enableSocksProxy, browserName, launchOptions },
|
2022-07-31 14:31:17 -07:00
|
|
|
{ playwright: this._preLaunchedPlaywright, browser: this._options.preLaunchedBrowser || null },
|
2022-08-03 19:37:06 -07:00
|
|
|
log, () => semaphore.release());
|
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');
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 19:37:06 -07:00
|
|
|
|
|
|
|
export class Semaphore {
|
|
|
|
private _max: number;
|
|
|
|
private _aquired = 0;
|
|
|
|
private _queue: ManualPromise[] = [];
|
2022-09-15 15:53:18 -07:00
|
|
|
|
2022-08-03 19:37:06 -07:00
|
|
|
constructor(max: number) {
|
|
|
|
this._max = max;
|
|
|
|
}
|
|
|
|
|
2022-09-15 15:53:18 -07:00
|
|
|
setMax(max: number) {
|
|
|
|
this._max = max;
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:37:06 -07:00
|
|
|
aquire(): Promise<void> {
|
|
|
|
const lock = new ManualPromise();
|
|
|
|
this._queue.push(lock);
|
|
|
|
this._flush();
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
requested() {
|
|
|
|
return this._aquired + this._queue.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
release() {
|
|
|
|
--this._aquired;
|
|
|
|
this._flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _flush() {
|
|
|
|
while (this._aquired < this._max && this._queue.length) {
|
|
|
|
++this._aquired;
|
|
|
|
this._queue.shift()!.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|