-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathbrowserArgs.ts
150 lines (128 loc) · 3.79 KB
/
browserArgs.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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { once } from '../../common/objUtils';
/**
* Gets the method through which the browser will expose CDP--either via
* its stdio pipes, or on a port number.
*/
export type BrowserConnection = 'pipe' | number;
const debugPortArg = '--remote-debugging-port';
const debugPipeArg = '--remote-debugging-pipe';
const argsToMap = (args: ReadonlyArray<string>) => {
const map: { [key: string]: string | null } = {};
for (const arg of args) {
const delimiter = arg.indexOf('=');
if (delimiter === -1) {
map[arg] = null;
} else {
map[arg.slice(0, delimiter)] = arg.slice(delimiter + 1);
}
}
return map;
};
const mapToArgs = (map: { [key: string]: string | null | undefined }) => {
const out: string[] = [];
for (const key of Object.keys(map)) {
const value = map[key];
if (value === undefined) {
continue;
}
out.push(value === null ? key : `${key}=${value}`);
}
return out;
};
/**
* Type used for managing the list of arguments passed to the browser.
*/
export class BrowserArgs {
/**
* Chrome default arguments.
*/
public static readonly default = new BrowserArgs([
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-renderer-backgrounding',
'--disable-sync',
'--metrics-recording-only',
'--no-first-run',
'--no-default-browser-check',
]);
private readonly argMap = once(() => argsToMap(this.args));
constructor(private readonly args: ReadonlyArray<string> = []) {}
/**
* Adds or overwrites an argument.
*/
public add(key: string, value: string | null = null) {
return new BrowserArgs(mapToArgs({ ...this.argMap(), [key]: value }));
}
/**
* Removes an argument.
*/
public remove(key: string) {
return new BrowserArgs(mapToArgs({ ...this.argMap(), [key]: undefined }));
}
/**
* Merges the set of arguments into this one.
*/
public merge(args: ReadonlyArray<string> | BrowserArgs) {
return new BrowserArgs(
mapToArgs({
...this.argMap(),
...(args instanceof BrowserArgs ? args.argMap() : argsToMap(args)),
}),
);
}
/**
* Sets the connection the browser args, returning an updated list of args.
*/
public setConnection(connection: BrowserConnection): BrowserArgs {
return new BrowserArgs(
mapToArgs({
...this.argMap(),
[debugPipeArg]: connection === 'pipe' ? null : undefined,
[debugPortArg]: connection !== 'pipe' ? String(connection) : undefined,
}),
);
}
/**
* Gets the preferred connection for this browser based on the arguments.
*/
public getSuggestedConnection(): BrowserConnection | undefined {
const args = this.argMap();
if (args.hasOwnProperty(debugPipeArg)) {
return 'pipe';
}
const port = args[debugPortArg];
if (port === undefined) {
return undefined;
}
return (port && Number(port)) || 0;
}
/**
* Returns a new set of browser args that pass the predicate.
*/
public filter(predicate: (key: string, value: string | null) => boolean) {
const args = this.argMap();
const out: string[] = [];
for (const key of Object.keys(args)) {
const value = args[key];
if (!predicate(key, value)) {
continue;
}
out.push(value === null ? key : `${key}=${value}`);
}
return new BrowserArgs(out);
}
/**
* Gets the array of arguments.
*/
public toArray() {
return this.args.slice();
}
}