forked from microsoft/playwright
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebkit.ts
412 lines (370 loc) · 15.3 KB
/
webkit.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* Copyright 2017 Google Inc. All rights reserved.
* Modifications 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 { BrowserFetcher, OnProgressCallback, BrowserFetcherOptions } from './browserFetcher';
import { DeviceDescriptors } from '../deviceDescriptors';
import { TimeoutError } from '../errors';
import * as types from '../types';
import { WKBrowser } from '../webkit/wkBrowser';
import { execSync } from 'child_process';
import { PipeTransport } from './pipeTransport';
import { launchProcess } from './processLauncher';
import * as fs from 'fs';
import * as path from 'path';
import * as platform from '../platform';
import * as util from 'util';
import * as os from 'os';
import { assert, helper } from '../helper';
import { kBrowserCloseMessageId } from '../webkit/wkConnection';
import { LaunchOptions, BrowserArgOptions, BrowserType } from './browserType';
import { ConnectionTransport } from '../transport';
import * as ws from 'ws';
import * as uuidv4 from 'uuid/v4';
import { ConnectOptions, LaunchType } from '../browser';
import { BrowserServer } from './browserServer';
import { Events } from '../events';
import { BrowserContext } from '../browserContext';
export class WebKit implements BrowserType {
private _downloadPath: string;
private _downloadHost: string;
readonly _revision: string;
constructor(downloadPath: string, downloadHost: (string|undefined), preferredRevision: string) {
this._downloadPath = downloadPath;
this._downloadHost = downloadHost || 'https://playwright.azureedge.net';
this._revision = preferredRevision;
}
name() {
return 'webkit';
}
async downloadBrowserIfNeeded(onProgress?: OnProgressCallback) {
const fetcher = this._createBrowserFetcher();
const revisionInfo = fetcher.revisionInfo();
// Do nothing if the revision is already downloaded.
if (revisionInfo.local)
return;
await fetcher.download(revisionInfo.revision, onProgress);
}
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<WKBrowser> {
if (options && (options as any).userDataDir)
throw new Error('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistent` instead');
const { browserServer, transport } = await this._launchServer(options, 'local');
const browser = await WKBrowser.connect(transport!, options && options.slowMo);
(browser as any)['__server__'] = browserServer;
return browser;
}
async launchServer(options?: LaunchOptions & { port?: number }): Promise<BrowserServer> {
return (await this._launchServer(options, 'server', undefined, options && options.port)).browserServer;
}
async launchPersistent(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext> {
const { timeout = 30000 } = options || {};
const { transport } = await this._launchServer(options, 'persistent', userDataDir);
const browser = await WKBrowser.connect(transport!, undefined, true);
await helper.waitWithTimeout(browser._waitForFirstPageTarget(), 'first page', timeout);
return browser._defaultContext;
}
private async _launchServer(options: LaunchOptions = {}, launchType: LaunchType, userDataDir?: string, port?: number): Promise<{ browserServer: BrowserServer, transport?: ConnectionTransport }> {
const {
ignoreDefaultArgs = false,
args = [],
dumpio = false,
executablePath = null,
env = process.env,
handleSIGINT = true,
handleSIGTERM = true,
handleSIGHUP = true,
} = options;
let temporaryUserDataDir: string | null = null;
if (!userDataDir) {
userDataDir = await mkdtempAsync(WEBKIT_PROFILE_PATH);
temporaryUserDataDir = userDataDir!;
}
const webkitArguments = [];
if (!ignoreDefaultArgs)
webkitArguments.push(...this._defaultArgs(options, launchType, userDataDir!, port || 0));
else if (Array.isArray(ignoreDefaultArgs))
webkitArguments.push(...this._defaultArgs(options, launchType, userDataDir!, port || 0).filter(arg => ignoreDefaultArgs.indexOf(arg) === -1));
else
webkitArguments.push(...args);
let webkitExecutable = executablePath;
if (!executablePath) {
const {missingText, executablePath} = this._resolveExecutablePath();
if (missingText)
throw new Error(missingText);
webkitExecutable = executablePath;
}
let transport: ConnectionTransport | undefined = undefined;
let browserServer: BrowserServer | undefined = undefined;
const { launchedProcess, gracefullyClose } = await launchProcess({
executablePath: webkitExecutable!,
args: webkitArguments,
env: { ...env, CURL_COOKIE_JAR_PATH: path.join(userDataDir!, 'cookiejar.db') },
handleSIGINT,
handleSIGTERM,
handleSIGHUP,
dumpio,
pipe: true,
tempDir: temporaryUserDataDir || undefined,
attemptToGracefullyClose: async () => {
if (!transport)
return Promise.reject();
// We try to gracefully close to prevent crash reporting and core dumps.
// Note that it's fine to reuse the pipe transport, since
// our connection ignores kBrowserCloseMessageId.
const message = JSON.stringify({method: 'Playwright.close', params: {}, id: kBrowserCloseMessageId});
transport.send(message);
},
onkill: (exitCode, signal) => {
if (browserServer)
browserServer.emit(Events.BrowserServer.Close, exitCode, signal);
},
});
// For local launch scenario close will terminate the browser process.
transport = new PipeTransport(launchedProcess.stdio[3] as NodeJS.WritableStream, launchedProcess.stdio[4] as NodeJS.ReadableStream, () => browserServer!.close());
browserServer = new BrowserServer(launchedProcess, gracefullyClose, launchType === 'server' ? await wrapTransportWithWebSocket(transport, port || 0) : null);
if (launchType === 'server')
return { browserServer };
return { browserServer, transport };
}
async connect(options: ConnectOptions): Promise<WKBrowser> {
return await platform.connectToWebsocket(options.wsEndpoint, transport => {
return WKBrowser.connect(transport, options.slowMo);
});
}
executablePath(): string {
return this._resolveExecutablePath().executablePath;
}
get devices(): types.Devices {
return DeviceDescriptors;
}
get errors(): { TimeoutError: typeof TimeoutError } {
return { TimeoutError };
}
_defaultArgs(options: BrowserArgOptions = {}, launchType: LaunchType, userDataDir: string, port: number): string[] {
const {
devtools = false,
headless = !devtools,
args = [],
} = options;
if (devtools)
throw new Error('Option "devtools" is not supported by WebKit');
const userDataDirArg = args.find(arg => arg.startsWith('--user-data-dir='));
if (userDataDirArg)
throw new Error('Pass userDataDir parameter instead of specifying --user-data-dir argument');
if (launchType !== 'persistent' && args.find(arg => !arg.startsWith('-')))
throw new Error('Arguments can not specify page to be opened');
const webkitArguments = ['--inspector-pipe'];
if (headless)
webkitArguments.push('--headless');
if (launchType === 'persistent')
webkitArguments.push(`--user-data-dir=${userDataDir}`);
else
webkitArguments.push(`--no-startup-window`);
webkitArguments.push(...args);
return webkitArguments;
}
_createBrowserFetcher(options?: BrowserFetcherOptions): BrowserFetcher {
const downloadURLs = {
linux: '%s/builds/webkit/%s/minibrowser-gtk-wpe.zip',
mac: '%s/builds/webkit/%s/minibrowser-mac-%s.zip',
win64: '%s/builds/webkit/%s/minibrowser-win64.zip',
};
const defaultOptions = {
path: path.join(this._downloadPath, '.local-webkit'),
host: this._downloadHost,
platform: (() => {
const platform = os.platform();
if (platform === 'darwin')
return 'mac';
if (platform === 'linux')
return 'linux';
if (platform === 'win32')
return 'win64';
return platform;
})()
};
options = {
...defaultOptions,
...options,
};
assert(!!(downloadURLs as any)[options.platform!], 'Unsupported platform: ' + options.platform);
return new BrowserFetcher(options.path!, options.platform!, this._revision, (platform: string, revision: string) => {
return {
downloadUrl: (platform === 'mac') ?
util.format(downloadURLs[platform], options!.host, revision, getMacVersion()) :
util.format((downloadURLs as any)[platform], options!.host, revision),
executablePath: platform.startsWith('win') ? 'MiniBrowser.exe' : 'pw_run.sh',
};
});
}
_resolveExecutablePath(): { executablePath: string; missingText: string | null; } {
const browserFetcher = this._createBrowserFetcher();
const revisionInfo = browserFetcher.revisionInfo();
const missingText = !revisionInfo.local ? `WebKit revision is not downloaded. Run "npm install"` : null;
return { executablePath: revisionInfo.executablePath, missingText };
}
}
const mkdtempAsync = platform.promisify(fs.mkdtemp);
const WEBKIT_PROFILE_PATH = path.join(os.tmpdir(), 'playwright_dev_profile-');
let cachedMacVersion: string | undefined = undefined;
function getMacVersion(): string {
if (!cachedMacVersion) {
const [major, minor] = execSync('sw_vers -productVersion').toString('utf8').trim().split('.');
assert(+major === 10 && +minor >= 14, 'Error: unsupported macOS version, macOS 10.14 and newer are supported');
cachedMacVersion = major + '.' + minor;
}
return cachedMacVersion;
}
class SequenceNumberMixer<V> {
static _lastSequenceNumber = 1;
private _values = new Map<number, V>();
generate(value: V): number {
const sequenceNumber = ++SequenceNumberMixer._lastSequenceNumber;
this._values.set(sequenceNumber, value);
return sequenceNumber;
}
take(sequenceNumber: number): V | undefined {
const value = this._values.get(sequenceNumber);
this._values.delete(sequenceNumber);
return value;
}
}
function wrapTransportWithWebSocket(transport: ConnectionTransport, port: number) {
const server = new ws.Server({ port });
const guid = uuidv4();
const idMixer = new SequenceNumberMixer<{id: number, socket: ws}>();
const pendingBrowserContextCreations = new Set<number>();
const pendingBrowserContextDeletions = new Map<number, string>();
const browserContextIds = new Map<string, ws>();
const pageProxyIds = new Map<string, ws>();
const sockets = new Set<ws>();
transport.onmessage = message => {
const parsedMessage = JSON.parse(message);
if ('id' in parsedMessage) {
if (parsedMessage.id === -9999)
return;
// Process command response.
const value = idMixer.take(parsedMessage.id);
if (!value)
return;
const { id, socket } = value;
if (!socket || socket.readyState === ws.CLOSING) {
if (pendingBrowserContextCreations.has(id)) {
transport.send(JSON.stringify({
id: ++SequenceNumberMixer._lastSequenceNumber,
method: 'Playwright.deleteContext',
params: { browserContextId: parsedMessage.result.browserContextId }
}));
}
return;
}
if (pendingBrowserContextCreations.has(parsedMessage.id)) {
// Browser.createContext response -> establish context attribution.
browserContextIds.set(parsedMessage.result.browserContextId, socket);
pendingBrowserContextCreations.delete(parsedMessage.id);
}
const deletedContextId = pendingBrowserContextDeletions.get(parsedMessage.id);
if (deletedContextId) {
// Browser.deleteContext response -> remove context attribution.
browserContextIds.delete(deletedContextId);
pendingBrowserContextDeletions.delete(parsedMessage.id);
}
parsedMessage.id = id;
socket.send(JSON.stringify(parsedMessage));
return;
}
// Process notification response.
const { method, params, pageProxyId } = parsedMessage;
if (pageProxyId) {
const socket = pageProxyIds.get(pageProxyId);
if (!socket || socket.readyState === ws.CLOSING) {
// Drop unattributed messages on the floor.
return;
}
socket.send(message);
return;
}
if (method === 'Playwright.pageProxyCreated') {
const socket = browserContextIds.get(params.pageProxyInfo.browserContextId);
if (!socket || socket.readyState === ws.CLOSING) {
// Drop unattributed messages on the floor.
return;
}
pageProxyIds.set(params.pageProxyInfo.pageProxyId, socket);
socket.send(message);
return;
}
if (method === 'Playwright.pageProxyDestroyed') {
const socket = pageProxyIds.get(params.pageProxyId);
pageProxyIds.delete(params.pageProxyId);
if (socket && socket.readyState !== ws.CLOSING)
socket.send(message);
return;
}
if (method === 'Playwright.provisionalLoadFailed') {
const socket = pageProxyIds.get(params.pageProxyId);
if (socket && socket.readyState !== ws.CLOSING)
socket.send(message);
return;
}
};
server.on('connection', (socket: ws, req) => {
if (req.url !== '/' + guid) {
socket.close();
return;
}
sockets.add(socket);
socket.on('message', (message: string) => {
const parsedMessage = JSON.parse(Buffer.from(message).toString());
const { id, method, params } = parsedMessage;
const seqNum = idMixer.generate({ id, socket });
transport.send(JSON.stringify({ ...parsedMessage, id: seqNum }));
if (method === 'Playwright.createContext')
pendingBrowserContextCreations.add(seqNum);
if (method === 'Playwright.deleteContext')
pendingBrowserContextDeletions.set(seqNum, params.browserContextId);
});
socket.on('close', (socket as any).__closeListener = () => {
for (const [pageProxyId, s] of pageProxyIds) {
if (s === socket)
pageProxyIds.delete(pageProxyId);
}
for (const [browserContextId, s] of browserContextIds) {
if (s === socket) {
transport.send(JSON.stringify({
id: ++SequenceNumberMixer._lastSequenceNumber,
method: 'Playwright.deleteContext',
params: { browserContextId }
}));
browserContextIds.delete(browserContextId);
}
}
sockets.delete(socket);
});
});
transport.onclose = () => {
for (const socket of sockets) {
socket.removeListener('close', (socket as any).__closeListener);
socket.close(undefined, 'Browser disconnected');
}
server.close();
transport.onmessage = undefined;
transport.onclose = undefined;
};
const address = server.address();
if (typeof address === 'string')
return address + '/' + guid;
return 'ws://127.0.0.1:' + address.port + '/' + guid;
}