1
1
import { version } from "../version.ts" ;
2
2
import https from "node:https" ;
3
+ import http from "node:http" ;
3
4
import qs from "node:querystring" ;
5
+ import { HttpsProxyAgent } from "npm:https-proxy-agent" ;
4
6
import { RequestTimeoutError } from "./errors.ts" ;
7
+ import { config } from "./config.ts" ;
8
+ import { Buffer } from "node:buffer" ;
5
9
6
10
/**
7
11
* This `_internals` object is needed to support stubbing/spying of
@@ -60,18 +64,30 @@ export function execute(
60
64
...parameters ,
61
65
source : getSource ( ) ,
62
66
} ) ;
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
+
63
78
return new Promise ( ( resolve , reject ) => {
64
79
let timer : number ;
65
- const req = https . get ( url , ( resp ) => {
80
+
81
+ const handleResponse = ( resp : http . IncomingMessage ) => {
66
82
resp . setEncoding ( "utf8" ) ;
67
83
let data = "" ;
68
84
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 ) => {
71
87
data += chunk ;
72
88
} ) ;
73
89
74
- // The whole response has been received. Print out the result.
90
+ // The whole response has been received
75
91
resp . on ( "end" , ( ) => {
76
92
try {
77
93
if ( resp . statusCode == 200 ) {
@@ -85,10 +101,25 @@ export function execute(
85
101
if ( timer ) clearTimeout ( timer ) ;
86
102
}
87
103
} ) ;
88
- } ) . on ( "error" , ( err ) => {
104
+ } ;
105
+
106
+ const handleError = ( err : Error ) => {
89
107
reject ( err ) ;
90
108
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
+ ) ;
92
123
93
124
if ( timeout > 0 ) {
94
125
timer = setTimeout ( ( ) => {
0 commit comments