Skip to content

Commit 07e981f

Browse files
committed
fix: get rid of url.parse in network code
1 parent 896cb85 commit 07e981f

1 file changed

Lines changed: 37 additions & 21 deletions

File tree

  • packages/playwright-core/src/server/utils

packages/playwright-core/src/server/utils/network.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ export type HTTPRequestParams = {
3939
export const NET_DEFAULT_TIMEOUT = 30_000;
4040

4141
export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.IncomingMessage) => void, onError: (error: Error) => void): { cancel(error: Error | undefined): void } {
42-
const parsedUrl = url.parse(params.url);
43-
let options: https.RequestOptions = {
44-
...parsedUrl,
42+
const parsedUrl = new URL(params.url);
43+
const options: https.RequestOptions = {
4544
agent: parsedUrl.protocol === 'https:' ? httpsHappyEyeballsAgent : httpHappyEyeballsAgent,
4645
method: params.method || 'GET',
4746
headers: params.headers,
@@ -51,19 +50,15 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
5150

5251
const proxyURL = getProxyForUrl(params.url);
5352
if (proxyURL) {
54-
const parsedProxyURL = url.parse(proxyURL);
53+
const parsedProxyURL = new URL(proxyURL);
5554
if (params.url.startsWith('http:')) {
56-
options = {
57-
path: parsedUrl.href,
58-
host: parsedProxyURL.hostname,
59-
port: parsedProxyURL.port,
60-
headers: options.headers,
61-
method: options.method
62-
};
55+
parsedUrl.pathname = parsedUrl.href;
56+
parsedUrl.host = parsedProxyURL.host;
6357
} else {
64-
(parsedProxyURL as any).secureProxy = parsedProxyURL.protocol === 'https:';
65-
66-
options.agent = new HttpsProxyAgent(parsedProxyURL);
58+
options.agent = new HttpsProxyAgent({
59+
...convertURLtoLegacyUrl(parsedProxyURL),
60+
secureProxy: parsedProxyURL.protocol === 'https:',
61+
});
6762
options.rejectUnauthorized = false;
6863
}
6964
}
@@ -81,8 +76,8 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
8176
}
8277
};
8378
const request = options.protocol === 'https:' ?
84-
https.request(options, requestCallback) :
85-
http.request(options, requestCallback);
79+
https.request(parsedUrl, options, requestCallback) :
80+
http.request(parsedUrl, options, requestCallback);
8681
request.on('error', onError);
8782
if (params.socketTimeout !== undefined) {
8883
request.setTimeout(params.socketTimeout, () => {
@@ -137,23 +132,27 @@ export function createProxyAgent(proxy?: ProxySettings, forUrl?: URL) {
137132
if (!/^\w+:\/\//.test(proxyServer))
138133
proxyServer = 'http://' + proxyServer;
139134

140-
const proxyOpts = url.parse(proxyServer);
135+
const proxyOpts = new URL(proxyServer);
141136
if (proxyOpts.protocol?.startsWith('socks')) {
142137
return new SocksProxyAgent({
143138
host: proxyOpts.hostname,
144139
port: proxyOpts.port || undefined,
145140
});
146141
}
147-
if (proxy.username)
148-
proxyOpts.auth = `${proxy.username}:${proxy.password || ''}`;
142+
if (proxy.username) {
143+
proxyOpts.username = proxy.username;
144+
proxyOpts.password = proxy.password || '';
145+
}
149146

150147
if (forUrl && ['ws:', 'wss:'].includes(forUrl.protocol)) {
151148
// Force CONNECT method for WebSockets.
152-
return new HttpsProxyAgent(proxyOpts);
149+
// TODO: switch to URL instance instead of legacy object once https-proxy-agent supports it.
150+
return new HttpsProxyAgent(convertURLtoLegacyUrl(proxyOpts));
153151
}
154152

155153
// TODO: We should use HttpProxyAgent conditional on proxyOpts.protocol instead of always using CONNECT method.
156-
return new HttpsProxyAgent(proxyOpts);
154+
// TODO: switch to URL instance instead of legacy object once https-proxy-agent supports it.
155+
return new HttpsProxyAgent(convertURLtoLegacyUrl(proxyOpts));
157156
}
158157

159158
export function createHttpServer(requestListener?: (req: http.IncomingMessage, res: http.ServerResponse) => void): http.Server;
@@ -226,3 +225,20 @@ function decorateServer(server: net.Server) {
226225
return close.call(server, callback);
227226
};
228227
}
228+
229+
function convertURLtoLegacyUrl(url: URL): url.Url {
230+
return {
231+
auth: url.username ? url.username + ':' + url.password : null,
232+
hash: url.hash || null,
233+
host: url.hostname ? url.hostname + ':' + url.port : null,
234+
hostname: url.hostname || null,
235+
href: url.href,
236+
path: url.pathname + url.search,
237+
pathname: url.pathname,
238+
protocol: url.protocol,
239+
search: url.search || null,
240+
slashes: true,
241+
port: url.port || null,
242+
query: url.search.slice(1) || null,
243+
};
244+
}

0 commit comments

Comments
 (0)