11// This file is copied from proxy-from-env with added support to use something
22// other than environment variables.
33
4- import { parse as parseUrl } from "url" ;
4+ import { parse as parseUrl } from "node: url" ;
55
66const DEFAULT_PORTS : Record < string , number > = {
77 ftp : 21 ,
@@ -34,7 +34,8 @@ export function getProxyForUrl(
3434 // Stripping ports in this way instead of using parsedUrl.hostname to make
3535 // sure that the brackets around IPv6 addresses are kept.
3636 hostname = hostname . replace ( / : \d * $ / , "" ) ;
37- const port = ( portRaw && parseInt ( portRaw ) ) || DEFAULT_PORTS [ proto ] || 0 ;
37+ const port =
38+ ( portRaw && Number . parseInt ( portRaw ) ) || DEFAULT_PORTS [ proto ] || 0 ;
3839 if ( ! shouldProxy ( hostname , port , noProxy ) ) {
3940 return "" ; // Don't proxy URLs that match NO_PROXY.
4041 }
@@ -45,7 +46,7 @@ export function getProxyForUrl(
4546 getEnv ( proto + "_proxy" ) ||
4647 getEnv ( "npm_config_proxy" ) ||
4748 getEnv ( "all_proxy" ) ;
48- if ( proxy && proxy . indexOf ( "://" ) === - 1 ) {
49+ if ( proxy ?. includes ( "://" ) ) {
4950 // Missing scheme in proxy, default to the requested URL's scheme.
5051 proxy = proto + "://" + proxy ;
5152 }
@@ -81,9 +82,9 @@ function shouldProxy(
8182 if ( ! proxy ) {
8283 return true ; // Skip zero-length hosts.
8384 }
84- const parsedProxy = proxy . match ( / ^ ( .+ ) : ( \d + ) $ / ) ;
85+ const parsedProxy = / ^ ( .+ ) : ( \d + ) $ / . exec ( proxy ) ;
8586 let parsedProxyHostname = parsedProxy ? parsedProxy [ 1 ] : proxy ;
86- const parsedProxyPort = parsedProxy ? parseInt ( parsedProxy [ 2 ] ) : 0 ;
87+ const parsedProxyPort = parsedProxy ? Number . parseInt ( parsedProxy [ 2 ] ) : 0 ;
8788 if ( parsedProxyPort && parsedProxyPort !== port ) {
8889 return true ; // Skip if ports don't match.
8990 }
@@ -93,7 +94,7 @@ function shouldProxy(
9394 return hostname !== parsedProxyHostname ;
9495 }
9596
96- if ( parsedProxyHostname . charAt ( 0 ) === "*" ) {
97+ if ( parsedProxyHostname . startsWith ( "*" ) ) {
9798 // Remove leading wildcard.
9899 parsedProxyHostname = parsedProxyHostname . slice ( 1 ) ;
99100 }
0 commit comments