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.
|
|
|
|
*/
|
|
|
|
|
2020-11-11 15:12:10 -08:00
|
|
|
import { LaunchServerOptions, Logger } from './client/types';
|
2020-09-10 15:34:13 -07:00
|
|
|
import { BrowserType } from './server/browserType';
|
2020-08-13 13:24:49 -07:00
|
|
|
import * as ws from 'ws';
|
2020-08-24 14:48:03 -07:00
|
|
|
import { Browser } from './server/browser';
|
2020-08-13 13:24:49 -07:00
|
|
|
import { ChildProcess } from 'child_process';
|
|
|
|
import { EventEmitter } from 'ws';
|
2020-09-02 16:15:43 -07:00
|
|
|
import { Dispatcher, DispatcherScope, DispatcherConnection } from './dispatchers/dispatcher';
|
2020-08-24 14:48:03 -07:00
|
|
|
import { BrowserDispatcher } from './dispatchers/browserDispatcher';
|
|
|
|
import { BrowserContextDispatcher } from './dispatchers/browserContextDispatcher';
|
2020-09-02 16:15:43 -07:00
|
|
|
import * as channels from './protocol/channels';
|
2020-08-24 14:48:03 -07:00
|
|
|
import { BrowserServerLauncher, BrowserServer } from './client/browserType';
|
|
|
|
import { envObjectToArray } from './client/clientHelper';
|
2020-08-28 10:51:55 -07:00
|
|
|
import { createGuid } from './utils/utils';
|
2020-09-02 16:15:43 -07:00
|
|
|
import { SelectorsDispatcher } from './dispatchers/selectorsDispatcher';
|
|
|
|
import { Selectors } from './server/selectors';
|
2020-11-11 15:12:10 -08:00
|
|
|
import { ProtocolLogger } from './server/types';
|
2021-02-09 14:44:48 -08:00
|
|
|
import { CallMetadata, internalCallMetadata, SdkObject } from './server/instrumentation';
|
2020-08-13 13:24:49 -07:00
|
|
|
|
|
|
|
export class BrowserServerLauncherImpl implements BrowserServerLauncher {
|
2020-09-10 15:34:13 -07:00
|
|
|
private _browserType: BrowserType;
|
2020-08-13 13:24:49 -07:00
|
|
|
|
2020-09-10 15:34:13 -07:00
|
|
|
constructor(browserType: BrowserType) {
|
2020-08-13 13:24:49 -07:00
|
|
|
this._browserType = browserType;
|
|
|
|
}
|
|
|
|
|
|
|
|
async launchServer(options: LaunchServerOptions = {}): Promise<BrowserServerImpl> {
|
2021-02-09 14:44:48 -08:00
|
|
|
const browser = await this._browserType.launch(internalCallMetadata(), {
|
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,
|
2020-11-11 15:12:10 -08:00
|
|
|
}, toProtocolLogger(options.logger));
|
2020-11-23 15:23:31 -08:00
|
|
|
return BrowserServerImpl.start(browser, options.port);
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BrowserServerImpl extends EventEmitter implements BrowserServer {
|
|
|
|
private _server: ws.Server;
|
2020-08-19 10:31:59 -07:00
|
|
|
private _browser: Browser;
|
2020-08-13 13:24:49 -07:00
|
|
|
private _wsEndpoint: string;
|
|
|
|
private _process: ChildProcess;
|
2020-11-23 15:23:31 -08:00
|
|
|
private _ready: Promise<void>;
|
2020-08-13 13:24:49 -07:00
|
|
|
|
2020-11-23 15:23:31 -08:00
|
|
|
static async start(browser: Browser, port: number = 0): Promise<BrowserServerImpl> {
|
|
|
|
const server = new BrowserServerImpl(browser, port);
|
|
|
|
await server._ready;
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(browser: Browser, port: number) {
|
2020-08-13 13:24:49 -07:00
|
|
|
super();
|
|
|
|
|
|
|
|
this._browser = browser;
|
2020-11-23 15:23:31 -08:00
|
|
|
this._wsEndpoint = '';
|
2021-01-29 16:00:56 -08:00
|
|
|
this._process = browser.options.browserProcess.process!;
|
2020-11-23 15:23:31 -08:00
|
|
|
|
|
|
|
let readyCallback = () => {};
|
|
|
|
this._ready = new Promise<void>(f => readyCallback = f);
|
2020-08-13 13:24:49 -07:00
|
|
|
|
2020-08-28 10:51:55 -07:00
|
|
|
const token = createGuid();
|
2021-01-11 15:53:45 -08:00
|
|
|
this._server = new ws.Server({ port, path: '/' + token }, () => {
|
2020-11-23 15:23:31 -08:00
|
|
|
const address = this._server.address();
|
|
|
|
this._wsEndpoint = typeof address === 'string' ? `${address}/${token}` : `ws://127.0.0.1:${address.port}/${token}`;
|
|
|
|
readyCallback();
|
|
|
|
});
|
2020-08-13 13:24:49 -07:00
|
|
|
|
|
|
|
this._server.on('connection', (socket: ws, req) => {
|
|
|
|
this._clientAttached(socket);
|
|
|
|
});
|
|
|
|
|
2021-01-29 16:00:56 -08:00
|
|
|
browser.options.browserProcess.onclose = (exitCode, signal) => {
|
2020-08-13 13:24:49 -07:00
|
|
|
this._server.close();
|
|
|
|
this.emit('close', exitCode, signal);
|
2020-08-14 13:19:12 -07:00
|
|
|
};
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
process(): ChildProcess {
|
|
|
|
return this._process;
|
|
|
|
}
|
|
|
|
|
|
|
|
wsEndpoint(): string {
|
|
|
|
return this._wsEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
2021-01-29 16:00:56 -08:00
|
|
|
await this._browser.options.browserProcess.close();
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async kill(): Promise<void> {
|
2021-01-29 16:00:56 -08:00
|
|
|
await this._browser.options.browserProcess.kill();
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private _clientAttached(socket: ws) {
|
|
|
|
const connection = new DispatcherConnection();
|
|
|
|
connection.onmessage = message => {
|
|
|
|
if (socket.readyState !== ws.CLOSING)
|
|
|
|
socket.send(JSON.stringify(message));
|
|
|
|
};
|
|
|
|
socket.on('message', (message: string) => {
|
|
|
|
connection.dispatch(JSON.parse(Buffer.from(message).toString()));
|
|
|
|
});
|
|
|
|
socket.on('error', () => {});
|
2021-02-09 14:44:48 -08:00
|
|
|
const selectors = new Selectors();
|
2020-09-02 16:15:43 -07:00
|
|
|
const scope = connection.rootDispatcher();
|
2020-10-02 17:27:56 -07:00
|
|
|
const remoteBrowser = new RemoteBrowserDispatcher(scope, this._browser, selectors);
|
2020-08-13 13:24:49 -07:00
|
|
|
socket.on('close', () => {
|
|
|
|
// Avoid sending any more messages over closed socket.
|
|
|
|
connection.onmessage = () => {};
|
|
|
|
// Cleanup contexts upon disconnect.
|
2020-10-02 17:27:56 -07:00
|
|
|
remoteBrowser.connectedBrowser.close().catch(e => {});
|
2020-08-13 13:24:49 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
class RemoteBrowserDispatcher extends Dispatcher<SdkObject, channels.RemoteBrowserInitializer> implements channels.PlaywrightChannel {
|
2020-10-02 17:27:56 -07:00
|
|
|
readonly connectedBrowser: ConnectedBrowser;
|
|
|
|
|
|
|
|
constructor(scope: DispatcherScope, browser: Browser, selectors: Selectors) {
|
|
|
|
const connectedBrowser = new ConnectedBrowser(scope, browser, selectors);
|
2021-02-09 09:00:00 -08:00
|
|
|
super(scope, browser, 'RemoteBrowser', {
|
2020-09-02 16:15:43 -07:00
|
|
|
selectors: new SelectorsDispatcher(scope, selectors),
|
2020-10-02 17:27:56 -07:00
|
|
|
browser: connectedBrowser,
|
2020-09-02 16:15:43 -07:00
|
|
|
}, false, 'remoteBrowser');
|
2020-10-02 17:27:56 -07:00
|
|
|
this.connectedBrowser = connectedBrowser;
|
|
|
|
connectedBrowser._remoteBrowser = this;
|
2020-09-02 16:15:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 13:24:49 -07:00
|
|
|
class ConnectedBrowser extends BrowserDispatcher {
|
|
|
|
private _contexts: BrowserContextDispatcher[] = [];
|
2020-09-02 16:15:43 -07:00
|
|
|
private _selectors: Selectors;
|
2020-08-13 13:24:49 -07:00
|
|
|
_closed = false;
|
2020-10-02 17:27:56 -07:00
|
|
|
_remoteBrowser?: RemoteBrowserDispatcher;
|
2020-08-13 13:24:49 -07:00
|
|
|
|
2020-09-02 16:15:43 -07:00
|
|
|
constructor(scope: DispatcherScope, browser: Browser, selectors: Selectors) {
|
|
|
|
super(scope, browser);
|
|
|
|
this._selectors = selectors;
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async newContext(params: channels.BrowserNewContextParams, metadata: CallMetadata): Promise<{ context: channels.BrowserContextChannel }> {
|
2020-11-02 19:42:05 -08:00
|
|
|
if (params.recordVideo) {
|
2020-10-02 17:27:56 -07:00
|
|
|
// TODO: we should create a separate temp directory or accept a launchServer parameter.
|
2021-01-29 16:00:56 -08:00
|
|
|
params.recordVideo.dir = this._object.options.downloadsPath!;
|
2020-10-02 17:27:56 -07:00
|
|
|
}
|
2021-02-09 14:44:48 -08:00
|
|
|
const result = await super.newContext(params, metadata);
|
2020-09-02 16:15:43 -07:00
|
|
|
const dispatcher = result.context as BrowserContextDispatcher;
|
|
|
|
dispatcher._object._setSelectors(this._selectors);
|
|
|
|
this._contexts.push(dispatcher);
|
2020-08-13 13:24:49 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
|
|
|
// Only close our own contexts.
|
2021-02-19 09:33:24 -08:00
|
|
|
await Promise.all(this._contexts.map(context => context.close({}, internalCallMetadata())));
|
2020-08-13 13:24:49 -07:00
|
|
|
this._didClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
_didClose() {
|
|
|
|
if (!this._closed) {
|
|
|
|
// We come here multiple times:
|
|
|
|
// - from ConnectedBrowser.close();
|
|
|
|
// - from underlying Browser.on('close').
|
|
|
|
this._closed = true;
|
|
|
|
super._didClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|