Skip to content

Commit

Permalink
[pac-proxy-agent]: Since format method is deprecated, use URL constru…
Browse files Browse the repository at this point in the history
…ctor instead. (#348)


---------

Co-authored-by: George <minggeorgelei@163.com>
Co-authored-by: George Lei <mlei@opentext.com>
Co-authored-by: Luke Karrys <luke@lukekarrys.com>
  • Loading branch information
4 people authored Dec 6, 2024
1 parent 913a49a commit 77c3599
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-vans-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pac-proxy-agent": patch
---

use WHATWG URL class to construct url parameter
34 changes: 9 additions & 25 deletions packages/pac-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as crypto from 'crypto';
import { once } from 'events';
import createDebug from 'debug';
import { Readable } from 'stream';
import { format, URL } from 'url';
import { URL } from 'url';
import { Agent, AgentConnectOpts, toBuffer } from 'agent-base';
import { HttpProxyAgent, HttpProxyAgentOptions } from 'http-proxy-agent';
import { HttpsProxyAgent, HttpsProxyAgentOptions } from 'https-proxy-agent';
Expand Down Expand Up @@ -204,32 +204,16 @@ export class PacProxyAgent<Uri extends string> extends Agent {
const resolver = await this.getResolver();

// Calculate the `url` parameter
const protocol = secureEndpoint ? 'https:' : 'http:';
const host =
opts.host && net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;
const defaultPort = secureEndpoint ? 443 : 80;
let path = req.path;
let search: string | null = null;
const firstQuestion = path.indexOf('?');
if (firstQuestion !== -1) {
search = path.substring(firstQuestion);
path = path.substring(0, firstQuestion);
}

const urlOpts = {
...opts,
protocol: secureEndpoint ? 'https:' : 'http:',
pathname: path,
search,

// need to use `hostname` instead of `host` otherwise `port` is ignored
hostname: opts.host,
host: null,
href: null,

// set `port` to null when it is the protocol default port (80 / 443)
port: defaultPort === opts.port ? null : opts.port,
};
const url = format(urlOpts);
const url = Object.assign(
new URL(req.path, `${protocol}//${host}`),
defaultPort ? undefined : { port: opts.port }
);

debug('url: %o', url);
debug('url: %s', url);
let result = await resolver(url);

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

0 comments on commit 77c3599

Please sign in to comment.