/
playwrightConnection.ts
298 lines (264 loc) · 11.9 KB
/
playwrightConnection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* 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.
*/
import type { WebSocket } from '../utilsBundle';
import type { DispatcherScope, Playwright } from '../server';
import { createPlaywright, DispatcherConnection, RootDispatcher, PlaywrightDispatcher } from '../server';
import { Browser } from '../server/browser';
import { serverSideCallMetadata } from '../server/instrumentation';
import { SocksProxy } from '../common/socksProxy';
import { assert, isUnderTest } from '../utils';
import type { LaunchOptions } from '../server/types';
import { AndroidDevice } from '../server/android/android';
import { DebugControllerDispatcher } from '../server/dispatchers/debugControllerDispatcher';
import { startProfiling, stopProfiling } from '../utils';
import { monotonicTime } from '../utils';
import { debugLogger } from '../common/debugLogger';
export type ClientType = 'controller' | 'launch-browser' | 'reuse-browser' | 'pre-launched-browser-or-android';
type Options = {
socksProxyPattern: string | undefined,
browserName: string | null,
launchOptions: LaunchOptions,
};
type PreLaunched = {
playwright?: Playwright | undefined;
browser?: Browser | undefined;
androidDevice?: AndroidDevice | undefined;
socksProxy?: SocksProxy | undefined;
};
export class PlaywrightConnection {
private _ws: WebSocket;
private _onClose: () => void;
private _dispatcherConnection: DispatcherConnection;
private _cleanups: (() => Promise<void>)[] = [];
private _id: string;
private _disconnected = false;
private _preLaunched: PreLaunched;
private _options: Options;
private _root: DispatcherScope;
private _profileName: string;
constructor(lock: Promise<void>, clientType: ClientType, ws: WebSocket, options: Options, preLaunched: PreLaunched, id: string, onClose: () => void) {
this._ws = ws;
this._preLaunched = preLaunched;
this._options = options;
options.launchOptions = filterLaunchOptions(options.launchOptions);
if (clientType === 'reuse-browser' || clientType === 'pre-launched-browser-or-android')
assert(preLaunched.playwright);
if (clientType === 'pre-launched-browser-or-android')
assert(preLaunched.browser || preLaunched.androidDevice);
this._onClose = onClose;
this._id = id;
this._profileName = `${new Date().toISOString()}-${clientType}`;
this._dispatcherConnection = new DispatcherConnection();
this._dispatcherConnection.onmessage = async message => {
await lock;
if (ws.readyState !== ws.CLOSING) {
const messageString = JSON.stringify(message);
if (debugLogger.isEnabled('server:channel'))
debugLogger.log('server:channel', `[${this._id}] ${monotonicTime() * 1000} SEND ► ${messageString}`);
ws.send(messageString);
}
};
ws.on('message', async (message: string) => {
await lock;
const messageString = Buffer.from(message).toString();
if (debugLogger.isEnabled('server:channel'))
debugLogger.log('server:channel', `[${this._id}] ${monotonicTime() * 1000} ◀ RECV ${messageString}`);
this._dispatcherConnection.dispatch(JSON.parse(messageString));
});
ws.on('close', () => this._onDisconnect());
ws.on('error', (error: Error) => this._onDisconnect(error));
if (clientType === 'controller') {
this._root = this._initDebugControllerMode();
return;
}
this._root = new RootDispatcher(this._dispatcherConnection, async scope => {
await startProfiling();
if (clientType === 'reuse-browser')
return await this._initReuseBrowsersMode(scope);
if (clientType === 'pre-launched-browser-or-android')
return this._preLaunched.browser ? await this._initPreLaunchedBrowserMode(scope) : await this._initPreLaunchedAndroidMode(scope);
if (clientType === 'launch-browser')
return await this._initLaunchBrowserMode(scope);
throw new Error('Unsupported client type: ' + clientType);
});
}
private async _initLaunchBrowserMode(scope: RootDispatcher) {
debugLogger.log('server', `[${this._id}] engaged launch mode for "${this._options.browserName}"`);
const playwright = createPlaywright({ sdkLanguage: 'javascript', isServer: true });
const ownedSocksProxy = await this._createOwnedSocksProxy(playwright);
const browser = await playwright[this._options.browserName as 'chromium'].launch(serverSideCallMetadata(), this._options.launchOptions);
this._cleanups.push(async () => {
for (const browser of playwright.allBrowsers())
await browser.close();
});
browser.on(Browser.Events.Disconnected, () => {
// Underlying browser did close for some reason - force disconnect the client.
this.close({ code: 1001, reason: 'Browser closed' });
});
return new PlaywrightDispatcher(scope, playwright, ownedSocksProxy, browser);
}
private async _initPreLaunchedBrowserMode(scope: RootDispatcher) {
debugLogger.log('server', `[${this._id}] engaged pre-launched (browser) mode`);
const playwright = this._preLaunched.playwright!;
// Note: connected client owns the socks proxy and configures the pattern.
this._preLaunched.socksProxy?.setPattern(this._options.socksProxyPattern);
const browser = this._preLaunched.browser!;
browser.on(Browser.Events.Disconnected, () => {
// Underlying browser did close for some reason - force disconnect the client.
this.close({ code: 1001, reason: 'Browser closed' });
});
const playwrightDispatcher = new PlaywrightDispatcher(scope, playwright, this._preLaunched.socksProxy, browser);
// In pre-launched mode, keep only the pre-launched browser.
for (const b of playwright.allBrowsers()) {
if (b !== browser)
await b.close();
}
this._cleanups.push(() => playwrightDispatcher.cleanup());
return playwrightDispatcher;
}
private async _initPreLaunchedAndroidMode(scope: RootDispatcher) {
debugLogger.log('server', `[${this._id}] engaged pre-launched (Android) mode`);
const playwright = this._preLaunched.playwright!;
const androidDevice = this._preLaunched.androidDevice!;
androidDevice.on(AndroidDevice.Events.Close, () => {
// Underlying browser did close for some reason - force disconnect the client.
this.close({ code: 1001, reason: 'Android device disconnected' });
});
const playwrightDispatcher = new PlaywrightDispatcher(scope, playwright, undefined, undefined, androidDevice);
this._cleanups.push(() => playwrightDispatcher.cleanup());
return playwrightDispatcher;
}
private _initDebugControllerMode(): DebugControllerDispatcher {
debugLogger.log('server', `[${this._id}] engaged reuse controller mode`);
const playwright = this._preLaunched.playwright!;
// Always create new instance based on the reused Playwright instance.
return new DebugControllerDispatcher(this._dispatcherConnection, playwright.debugController);
}
private async _initReuseBrowsersMode(scope: RootDispatcher) {
// Note: reuse browser mode does not support socks proxy, because
// clients come and go, while the browser stays the same.
debugLogger.log('server', `[${this._id}] engaged reuse browsers mode for ${this._options.browserName}`);
const playwright = this._preLaunched.playwright!;
const requestedOptions = launchOptionsHash(this._options.launchOptions);
let browser = playwright.allBrowsers().find(b => {
if (b.options.name !== this._options.browserName)
return false;
const existingOptions = launchOptionsHash(b.options.originalLaunchOptions);
return existingOptions === requestedOptions;
});
// Close remaining browsers of this type+channel. Keep different browser types for the speed.
for (const b of playwright.allBrowsers()) {
if (b === browser)
continue;
if (b.options.name === this._options.browserName && b.options.channel === this._options.launchOptions.channel)
await b.close();
}
if (!browser) {
browser = await playwright[(this._options.browserName || 'chromium') as 'chromium'].launch(serverSideCallMetadata(), {
...this._options.launchOptions,
headless: !!process.env.PW_DEBUG_CONTROLLER_HEADLESS,
});
browser.on(Browser.Events.Disconnected, () => {
// Underlying browser did close for some reason - force disconnect the client.
this.close({ code: 1001, reason: 'Browser closed' });
});
}
this._cleanups.push(async () => {
// Don't close the pages so that user could debug them,
// but close all the empty browsers and contexts to clean up.
for (const browser of playwright.allBrowsers()) {
for (const context of browser.contexts()) {
if (!context.pages().length)
await context.close(serverSideCallMetadata());
else
await context.stopPendingOperations();
}
if (!browser.contexts())
await browser.close();
}
});
const playwrightDispatcher = new PlaywrightDispatcher(scope, playwright, undefined, browser);
return playwrightDispatcher;
}
private async _createOwnedSocksProxy(playwright: Playwright): Promise<SocksProxy | undefined> {
if (!this._options.socksProxyPattern)
return;
const socksProxy = new SocksProxy();
socksProxy.setPattern(this._options.socksProxyPattern);
playwright.options.socksProxyPort = await socksProxy.listen(0);
debugLogger.log('server', `[${this._id}] started socks proxy on port ${playwright.options.socksProxyPort}`);
this._cleanups.push(() => socksProxy.close());
return socksProxy;
}
private async _onDisconnect(error?: Error) {
this._disconnected = true;
debugLogger.log('server', `[${this._id}] disconnected. error: ${error}`);
this._root._dispose();
debugLogger.log('server', `[${this._id}] starting cleanup`);
for (const cleanup of this._cleanups)
await cleanup().catch(() => {});
await stopProfiling(this._profileName);
this._onClose();
debugLogger.log('server', `[${this._id}] finished cleanup`);
}
async close(reason?: { code: number, reason: string }) {
if (this._disconnected)
return;
debugLogger.log('server', `[${this._id}] force closing connection: ${reason?.reason || ''} (${reason?.code || 0})`);
try {
this._ws.close(reason?.code, reason?.reason);
} catch (e) {
}
}
}
function launchOptionsHash(options: LaunchOptions) {
const copy = { ...options };
for (const k of Object.keys(copy)) {
const key = k as keyof LaunchOptions;
if (copy[key] === defaultLaunchOptions[key])
delete copy[key];
}
for (const key of optionsThatAllowBrowserReuse)
delete copy[key];
return JSON.stringify(copy);
}
function filterLaunchOptions(options: LaunchOptions): LaunchOptions {
return {
channel: options.channel,
args: options.args,
ignoreAllDefaultArgs: options.ignoreAllDefaultArgs,
ignoreDefaultArgs: options.ignoreDefaultArgs,
timeout: options.timeout,
headless: options.headless,
proxy: options.proxy,
chromiumSandbox: options.chromiumSandbox,
firefoxUserPrefs: options.firefoxUserPrefs,
slowMo: options.slowMo,
executablePath: isUnderTest() ? options.executablePath : undefined,
};
}
const defaultLaunchOptions: LaunchOptions = {
ignoreAllDefaultArgs: false,
handleSIGINT: false,
handleSIGTERM: false,
handleSIGHUP: false,
headless: true,
devtools: false,
};
const optionsThatAllowBrowserReuse: (keyof LaunchOptions)[] = [
'headless',
'tracesDir',
];