mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-14 13:45:36 +03:00
b29b7df47e
There are a few ways for `connect()` to finish: - `Browser.close()` from the client side. - Browser on the server side did exit (e.g. crashed). - Connection was dropped by either of the sides. We reduce all the cases to the last one by dropping the connection when client wants calls `Browser.close()` or server-side browser exits. In all these cases we should properly cleanup on the server side, and ensure that all promises reject on the client side.
130 lines
3.8 KiB
TypeScript
130 lines
3.8 KiB
TypeScript
/**
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
import path from 'path';
|
|
import { spawn } from 'child_process';
|
|
import type { BrowserType, Browser, LaunchOptions } from '../../index';
|
|
|
|
const playwrightPath = path.join(__dirname, '..', '..');
|
|
|
|
export type RemoteServerOptions = {
|
|
stallOnClose?: boolean;
|
|
disconnectOnSIGHUP?: boolean;
|
|
inCluster?: boolean;
|
|
url?: string;
|
|
};
|
|
|
|
export class RemoteServer {
|
|
_output: Map<any, any>;
|
|
_outputCallback: Map<any, any>;
|
|
_browserType: BrowserType;
|
|
_child: import('child_process').ChildProcess;
|
|
_exitPromise: Promise<unknown>;
|
|
_exitAndDisconnectPromise: Promise<any>;
|
|
_browser: Browser;
|
|
_didExit: boolean;
|
|
_wsEndpoint: string;
|
|
|
|
async _start(browserType: BrowserType, browserOptions: LaunchOptions, remoteServerOptions: RemoteServerOptions = {}) {
|
|
this._output = new Map();
|
|
this._outputCallback = new Map();
|
|
this._didExit = false;
|
|
|
|
this._browserType = browserType;
|
|
const launchOptions = {
|
|
...browserOptions,
|
|
handleSIGINT: true,
|
|
handleSIGTERM: true,
|
|
handleSIGHUP: true,
|
|
executablePath: browserOptions.channel ? undefined : browserOptions.executablePath || browserType.executablePath(),
|
|
logger: undefined,
|
|
};
|
|
const options = {
|
|
playwrightPath,
|
|
browserTypeName: browserType.name(),
|
|
launchOptions,
|
|
...remoteServerOptions,
|
|
};
|
|
this._child = spawn('node', [path.join(__dirname, 'remote-server-impl.js'), JSON.stringify(options)], { env: process.env });
|
|
this._child.on('error', (...args) => console.log('ERROR', ...args));
|
|
this._exitPromise = new Promise(resolve => this._child.on('exit', (exitCode, signal) => {
|
|
this._didExit = true;
|
|
resolve(exitCode);
|
|
}));
|
|
|
|
let outputString = '';
|
|
this._child.stdout.on('data', data => {
|
|
outputString += data.toString();
|
|
// Uncomment to debug.
|
|
// console.log(data.toString());
|
|
let match;
|
|
while ((match = outputString.match(/\(([^()]+)=>([^()]+)\)/))) {
|
|
const key = match[1];
|
|
const value = match[2];
|
|
this._addOutput(key, value);
|
|
outputString = outputString.substring(match.index + match[0].length);
|
|
}
|
|
});
|
|
this._child.stderr.on('data', data => {
|
|
console.log(data.toString());
|
|
});
|
|
|
|
this._wsEndpoint = await this.out('wsEndpoint');
|
|
|
|
if (remoteServerOptions.url) {
|
|
this._browser = await this._browserType.connect({ wsEndpoint: this._wsEndpoint });
|
|
const page = await this._browser.newPage();
|
|
await page.goto(remoteServerOptions.url);
|
|
}
|
|
}
|
|
|
|
_addOutput(key, value) {
|
|
this._output.set(key, value);
|
|
const cb = this._outputCallback.get(key);
|
|
this._outputCallback.delete(key);
|
|
if (cb)
|
|
cb();
|
|
}
|
|
|
|
async out(key) {
|
|
if (!this._output.has(key))
|
|
await new Promise(f => this._outputCallback.set(key, f));
|
|
return this._output.get(key);
|
|
}
|
|
|
|
wsEndpoint() {
|
|
return this._wsEndpoint;
|
|
}
|
|
|
|
child() {
|
|
return this._child;
|
|
}
|
|
|
|
async childExitCode() {
|
|
return await this._exitPromise;
|
|
}
|
|
|
|
async close() {
|
|
if (this._browser) {
|
|
await this._browser.close();
|
|
this._browser = undefined;
|
|
}
|
|
if (!this._didExit)
|
|
this._child.kill();
|
|
return await this.childExitCode();
|
|
}
|
|
}
|