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-08-24 14:48:03 -07:00
|
|
|
import { LaunchServerOptions } 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-10-02 17:27:56 -07:00
|
|
|
import * as fs from 'fs';
|
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-10-02 17:27:56 -07:00
|
|
|
import { BrowserContext, Video } from './server/browserContext';
|
|
|
|
import { StreamDispatcher } from './dispatchers/streamDispatcher';
|
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> {
|
2020-08-18 09:37:40 -07:00
|
|
|
const browser = await this._browserType.launch({
|
|
|
|
...options,
|
|
|
|
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
|
|
|
|
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
|
|
|
|
env: options.env ? envObjectToArray(options.env) : undefined,
|
|
|
|
});
|
2020-08-19 10:31:59 -07:00
|
|
|
return new BrowserServerImpl(this._browserType, browser, options.port);
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BrowserServerImpl extends EventEmitter implements BrowserServer {
|
|
|
|
private _server: ws.Server;
|
2020-09-10 15:34:13 -07:00
|
|
|
private _browserType: BrowserType;
|
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-09-10 15:34:13 -07:00
|
|
|
constructor(browserType: BrowserType, browser: Browser, port: number = 0) {
|
2020-08-13 13:24:49 -07:00
|
|
|
super();
|
|
|
|
|
|
|
|
this._browserType = browserType;
|
|
|
|
this._browser = browser;
|
|
|
|
|
2020-08-28 10:51:55 -07:00
|
|
|
const token = createGuid();
|
2020-08-13 13:24:49 -07:00
|
|
|
this._server = new ws.Server({ port });
|
|
|
|
const address = this._server.address();
|
|
|
|
this._wsEndpoint = typeof address === 'string' ? `${address}/${token}` : `ws://127.0.0.1:${address.port}/${token}`;
|
2020-08-14 13:19:12 -07:00
|
|
|
this._process = browser._options.browserProcess.process;
|
2020-08-13 13:24:49 -07:00
|
|
|
|
|
|
|
this._server.on('connection', (socket: ws, req) => {
|
|
|
|
if (req.url !== '/' + token) {
|
|
|
|
socket.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._clientAttached(socket);
|
|
|
|
});
|
|
|
|
|
2020-08-14 13:19:12 -07: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> {
|
2020-08-14 13:19:12 -07:00
|
|
|
await this._browser._options.browserProcess.close();
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async kill(): Promise<void> {
|
2020-08-14 13:19:12 -07: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', () => {});
|
2020-09-02 16:15:43 -07:00
|
|
|
const selectors = new Selectors();
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 16:15:43 -07:00
|
|
|
class RemoteBrowserDispatcher extends Dispatcher<{}, 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);
|
2020-09-02 16:15:43 -07:00
|
|
|
super(scope, {}, 'RemoteBrowser', {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-02 16:15:43 -07:00
|
|
|
async newContext(params: channels.BrowserNewContextParams): 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.
|
2020-11-02 19:42:05 -08:00
|
|
|
params.recordVideo.dir = this._object._options.downloadsPath!;
|
2020-10-02 17:27:56 -07:00
|
|
|
}
|
2020-08-13 13:24:49 -07:00
|
|
|
const result = await super.newContext(params);
|
2020-09-02 16:15:43 -07:00
|
|
|
const dispatcher = result.context as BrowserContextDispatcher;
|
2020-10-02 17:27:56 -07:00
|
|
|
dispatcher._object.on(BrowserContext.Events.VideoStarted, (video: Video) => this._sendVideo(dispatcher, video));
|
2020-09-02 16:15:43 -07:00
|
|
|
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.
|
|
|
|
await Promise.all(this._contexts.map(context => context.close()));
|
|
|
|
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-10-02 17:27:56 -07:00
|
|
|
|
|
|
|
private _sendVideo(contextDispatcher: BrowserContextDispatcher, video: Video) {
|
|
|
|
video._waitForCallbackOnFinish(async () => {
|
|
|
|
const readable = fs.createReadStream(video._path);
|
|
|
|
await new Promise(f => readable.on('readable', f));
|
|
|
|
const stream = new StreamDispatcher(this._remoteBrowser!._scope, readable);
|
2020-10-13 22:15:51 -07:00
|
|
|
this._remoteBrowser!._dispatchEvent('video', {
|
|
|
|
stream,
|
|
|
|
context: contextDispatcher,
|
|
|
|
relativePath: video._relativePath
|
|
|
|
});
|
2020-10-02 17:27:56 -07:00
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
readable.on('close', resolve);
|
|
|
|
readable.on('end', resolve);
|
|
|
|
readable.on('error', resolve);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-08-13 13:24:49 -07:00
|
|
|
}
|