2020-08-13 13:24:49 -07: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-06 13:57:14 -08:00
|
|
|
import type { LaunchServerOptions, Logger } from './client/types';
|
2020-08-13 13:24:49 -07:00
|
|
|
import { EventEmitter } from 'ws';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { BrowserServerLauncher, BrowserServer } from './client/browserType';
|
2020-08-24 14:48:03 -07:00
|
|
|
import { envObjectToArray } from './client/clientHelper';
|
2020-08-28 10:51:55 -07:00
|
|
|
import { createGuid } from './utils/utils';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { ProtocolLogger } from './server/types';
|
2022-03-17 17:27:33 -08:00
|
|
|
import { serverSideCallMetadata } from './server/instrumentation';
|
2022-02-10 16:36:23 -08:00
|
|
|
import { createPlaywright } from './server/playwright';
|
|
|
|
import { PlaywrightServer } from './remote/playwrightServer';
|
2022-02-09 09:54:16 -08:00
|
|
|
import { helper } from './server/helper';
|
|
|
|
import { rewriteErrorMessage } from './utils/stackTrace';
|
2020-08-13 13:24:49 -07:00
|
|
|
|
|
|
|
export class BrowserServerLauncherImpl implements BrowserServerLauncher {
|
2021-06-01 12:21:24 -07:00
|
|
|
private _browserName: 'chromium' | 'firefox' | 'webkit';
|
2020-08-13 13:24:49 -07:00
|
|
|
|
2021-06-01 12:21:24 -07:00
|
|
|
constructor(browserName: 'chromium' | 'firefox' | 'webkit') {
|
|
|
|
this._browserName = browserName;
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
|
2021-04-12 11:14:54 -07:00
|
|
|
async launchServer(options: LaunchServerOptions = {}): Promise<BrowserServer> {
|
2021-08-20 21:32:21 +02:00
|
|
|
const playwright = createPlaywright('javascript');
|
2021-04-12 11:14:54 -07:00
|
|
|
// 1. Pre-launch the browser
|
2022-03-17 17:27:33 -08:00
|
|
|
const metadata = serverSideCallMetadata();
|
2022-02-09 09:54:16 -08:00
|
|
|
const browser = await playwright[this._browserName].launch(metadata, {
|
2020-08-18 09:37:40 -07:00
|
|
|
...options,
|
|
|
|
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
|
|
|
|
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
|
|
|
|
env: options.env ? envObjectToArray(options.env) : undefined,
|
2022-02-09 09:54:16 -08:00
|
|
|
}, toProtocolLogger(options.logger)).catch(e => {
|
|
|
|
const log = helper.formatBrowserLogs(metadata.log);
|
|
|
|
rewriteErrorMessage(e, `${e.message} Failed to launch browser.${log}`);
|
|
|
|
throw e;
|
|
|
|
});
|
2020-11-23 15:23:31 -08:00
|
|
|
|
2021-08-22 09:04:47 -07:00
|
|
|
let path = `/${createGuid()}`;
|
|
|
|
if (options.wsPath)
|
|
|
|
path = options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}`;
|
|
|
|
|
2021-04-12 11:14:54 -07:00
|
|
|
// 2. Start the server
|
2022-02-14 15:10:58 -08:00
|
|
|
const server = new PlaywrightServer(path, Infinity, false, browser);
|
2021-04-12 11:14:54 -07:00
|
|
|
const wsEndpoint = await server.listen(options.port);
|
|
|
|
|
|
|
|
// 3. Return the BrowserServer interface
|
|
|
|
const browserServer = new EventEmitter() as (BrowserServer & EventEmitter);
|
|
|
|
browserServer.process = () => browser.options.browserProcess.process!;
|
|
|
|
browserServer.wsEndpoint = () => wsEndpoint;
|
|
|
|
browserServer.close = () => browser.options.browserProcess.close();
|
|
|
|
browserServer.kill = () => browser.options.browserProcess.kill();
|
2021-05-06 09:34:06 -07:00
|
|
|
(browserServer as any)._disconnectForTest = () => server.close();
|
2021-04-12 11:14:54 -07:00
|
|
|
browser.options.browserProcess.onclose = async (exitCode, signal) => {
|
|
|
|
server.close();
|
|
|
|
browserServer.emit('close', exitCode, signal);
|
|
|
|
};
|
|
|
|
return browserServer;
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 15:12:10 -08:00
|
|
|
|
|
|
|
function toProtocolLogger(logger: Logger | undefined): ProtocolLogger | undefined {
|
|
|
|
return logger ? (direction: 'send' | 'receive', message: object) => {
|
|
|
|
if (logger.isEnabled('protocol', 'verbose'))
|
|
|
|
logger.log('protocol', 'verbose', (direction === 'send' ? 'SEND ► ' : '◀ RECV ') + JSON.stringify(message), [], {});
|
|
|
|
} : undefined;
|
|
|
|
}
|