11import { version } from "../version.ts" ;
22import https from "node:https" ;
3+ import http from "node:http" ;
34import qs from "node:querystring" ;
5+ import { HttpsProxyAgent } from "npm:https-proxy-agent" ;
46import { RequestTimeoutError } from "./errors.ts" ;
7+ import { config } from "./config.ts" ;
8+ import { Buffer } from "node:buffer" ;
59
610/**
711 * This `_internals` object is needed to support stubbing/spying of
@@ -60,18 +64,30 @@ export function execute(
6064 ...parameters ,
6165 source : getSource ( ) ,
6266 } ) ;
67+
68+ // Check if we should use a proxy
69+ const urlObj = new URL ( url ) ;
70+ const shouldUseProxy = ! config . no_proxy ?. split ( "," ) . some ( ( domain ) =>
71+ urlObj . hostname . endsWith ( domain . trim ( ) )
72+ ) ;
73+
74+ const proxyUrl = shouldUseProxy
75+ ? ( urlObj . protocol === "https:" ? config . https_proxy : config . http_proxy )
76+ : undefined ;
77+
6378 return new Promise ( ( resolve , reject ) => {
6479 let timer : number ;
65- const req = https . get ( url , ( resp ) => {
80+
81+ const handleResponse = ( resp : http . IncomingMessage ) => {
6682 resp . setEncoding ( "utf8" ) ;
6783 let data = "" ;
6884
69- // A chunk of data has been recieved.
70- resp . on ( "data" , ( chunk ) => {
85+ // A chunk of data has been received
86+ resp . on ( "data" , ( chunk : Buffer ) => {
7187 data += chunk ;
7288 } ) ;
7389
74- // The whole response has been received. Print out the result.
90+ // The whole response has been received
7591 resp . on ( "end" , ( ) => {
7692 try {
7793 if ( resp . statusCode == 200 ) {
@@ -85,10 +101,25 @@ export function execute(
85101 if ( timer ) clearTimeout ( timer ) ;
86102 }
87103 } ) ;
88- } ) . on ( "error" , ( err ) => {
104+ } ;
105+
106+ const handleError = ( err : Error ) => {
89107 reject ( err ) ;
90108 if ( timer ) clearTimeout ( timer ) ;
91- } ) ;
109+ } ;
110+
111+ const options : https . RequestOptions = {
112+ timeout : timeout > 0 ? timeout : undefined ,
113+ } ;
114+
115+ if ( proxyUrl ) {
116+ options . agent = new HttpsProxyAgent ( proxyUrl ) ;
117+ }
118+
119+ const req = https . get ( url , options , handleResponse ) . on (
120+ "error" ,
121+ handleError ,
122+ ) ;
92123
93124 if ( timeout > 0 ) {
94125 timer = setTimeout ( ( ) => {
0 commit comments