Skip to content

Commit 77c3599

Browse files
Ming-George-LeiGeorgeGeorge Leilukekarrys
authored
[pac-proxy-agent]: Since format method is deprecated, use URL constructor instead. (#348)
--------- Co-authored-by: George <[email protected]> Co-authored-by: George Lei <[email protected]> Co-authored-by: Luke Karrys <[email protected]>
1 parent 913a49a commit 77c3599

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

.changeset/giant-vans-refuse.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"pac-proxy-agent": patch
3+
---
4+
5+
use WHATWG URL class to construct url parameter

packages/pac-proxy-agent/src/index.ts

+9-25
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as crypto from 'crypto';
55
import { once } from 'events';
66
import createDebug from 'debug';
77
import { Readable } from 'stream';
8-
import { format, URL } from 'url';
8+
import { URL } from 'url';
99
import { Agent, AgentConnectOpts, toBuffer } from 'agent-base';
1010
import { HttpProxyAgent, HttpProxyAgentOptions } from 'http-proxy-agent';
1111
import { HttpsProxyAgent, HttpsProxyAgentOptions } from 'https-proxy-agent';
@@ -204,32 +204,16 @@ export class PacProxyAgent<Uri extends string> extends Agent {
204204
const resolver = await this.getResolver();
205205

206206
// Calculate the `url` parameter
207+
const protocol = secureEndpoint ? 'https:' : 'http:';
208+
const host =
209+
opts.host && net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;
207210
const defaultPort = secureEndpoint ? 443 : 80;
208-
let path = req.path;
209-
let search: string | null = null;
210-
const firstQuestion = path.indexOf('?');
211-
if (firstQuestion !== -1) {
212-
search = path.substring(firstQuestion);
213-
path = path.substring(0, firstQuestion);
214-
}
215-
216-
const urlOpts = {
217-
...opts,
218-
protocol: secureEndpoint ? 'https:' : 'http:',
219-
pathname: path,
220-
search,
221-
222-
// need to use `hostname` instead of `host` otherwise `port` is ignored
223-
hostname: opts.host,
224-
host: null,
225-
href: null,
226-
227-
// set `port` to null when it is the protocol default port (80 / 443)
228-
port: defaultPort === opts.port ? null : opts.port,
229-
};
230-
const url = format(urlOpts);
211+
const url = Object.assign(
212+
new URL(req.path, `${protocol}//${host}`),
213+
defaultPort ? undefined : { port: opts.port }
214+
);
231215

232-
debug('url: %o', url);
216+
debug('url: %s', url);
233217
let result = await resolver(url);
234218

235219
// Default to "DIRECT" if a falsey value was returned (or nothing)

0 commit comments

Comments
 (0)