@@ -6,10 +6,10 @@ const http = require('http')
66const https = require ( 'https' )
77const os = require ( 'os' )
88const path = require ( 'path' )
9- const tls = require ( 'tls' )
109const zlib = require ( 'zlib' )
1110
1211const tar = require ( 'tar' )
12+ const { createReleaseHttpClient } = require ( './http' )
1313
1414const packageName = 'codecane'
1515
@@ -66,6 +66,11 @@ function createConfig(packageName) {
6666}
6767
6868const CONFIG = createConfig ( packageName )
69+ const { getProxyUrl, httpGet } = createReleaseHttpClient ( {
70+ env : process . env ,
71+ userAgent : CONFIG . userAgent ,
72+ requestTimeout : CONFIG . requestTimeout ,
73+ } )
6974
7075function getPostHogConfig ( ) {
7176 const apiKey =
@@ -131,76 +136,6 @@ function trackUpdateFailed(errorMessage, version, context = {}) {
131136 }
132137}
133138
134- function getProxyUrl ( ) {
135- return (
136- process . env . HTTPS_PROXY ||
137- process . env . https_proxy ||
138- process . env . HTTP_PROXY ||
139- process . env . http_proxy ||
140- null
141- )
142- }
143-
144- function shouldBypassProxy ( hostname ) {
145- const noProxy = process . env . NO_PROXY || process . env . no_proxy || ''
146- if ( ! noProxy ) return false
147- const domains = noProxy . split ( ',' ) . map ( ( d ) => d . trim ( ) . toLowerCase ( ) . replace ( / : \d + $ / , '' ) )
148- const host = hostname . toLowerCase ( )
149- return domains . some ( ( d ) => {
150- if ( d === '*' ) return true
151- if ( d . startsWith ( '.' ) ) return host . endsWith ( d ) || host === d . slice ( 1 )
152- return host === d || host . endsWith ( '.' + d )
153- } )
154- }
155-
156- function connectThroughProxy ( proxyUrl , targetHost , targetPort ) {
157- return new Promise ( ( resolve , reject ) => {
158- const proxy = new URL ( proxyUrl )
159- const isHttpsProxy = proxy . protocol === 'https:'
160- const connectOptions = {
161- hostname : proxy . hostname ,
162- port : proxy . port || ( isHttpsProxy ? 443 : 80 ) ,
163- method : 'CONNECT' ,
164- path : `${ targetHost } :${ targetPort } ` ,
165- headers : {
166- Host : `${ targetHost } :${ targetPort } ` ,
167- } ,
168- }
169-
170- if ( proxy . username || proxy . password ) {
171- const auth = Buffer . from (
172- `${ decodeURIComponent ( proxy . username || '' ) } :${ decodeURIComponent ( proxy . password || '' ) } ` ,
173- ) . toString ( 'base64' )
174- connectOptions . headers [ 'Proxy-Authorization' ] = `Basic ${ auth } `
175- }
176-
177- const transport = isHttpsProxy ? https : http
178- const req = transport . request ( connectOptions )
179-
180- req . on ( 'connect' , ( res , socket ) => {
181- if ( res . statusCode === 200 ) {
182- resolve ( socket )
183- } else {
184- socket . destroy ( )
185- reject (
186- new Error ( `Proxy CONNECT failed with status ${ res . statusCode } ` ) ,
187- )
188- }
189- } )
190-
191- req . on ( 'error' , ( err ) => {
192- reject ( new Error ( `Proxy connection failed: ${ err . message } ` ) )
193- } )
194-
195- req . setTimeout ( CONFIG . requestTimeout , ( ) => {
196- req . destroy ( )
197- reject ( new Error ( 'Proxy connection timeout.' ) )
198- } )
199-
200- req . end ( )
201- } )
202- }
203-
204139const PLATFORM_TARGETS = {
205140 'linux-x64' : `${ packageName } -linux-x64.tar.gz` ,
206141 'linux-arm64' : `${ packageName } -linux-arm64.tar.gz` ,
@@ -225,54 +160,6 @@ const term = {
225160 } ,
226161}
227162
228- async function httpGet ( url , options = { } ) {
229- const parsedUrl = new URL ( url )
230- const proxyUrl = getProxyUrl ( )
231-
232- const reqOptions = {
233- hostname : parsedUrl . hostname ,
234- path : parsedUrl . pathname + parsedUrl . search ,
235- headers : {
236- 'User-Agent' : CONFIG . userAgent ,
237- ...options . headers ,
238- } ,
239- }
240-
241- if ( proxyUrl && ! shouldBypassProxy ( parsedUrl . hostname ) ) {
242- const tunnelSocket = await connectThroughProxy (
243- proxyUrl ,
244- parsedUrl . hostname ,
245- parsedUrl . port || 443 ,
246- )
247- reqOptions . agent = false
248- reqOptions . createConnection = ( ) =>
249- tls . connect ( {
250- socket : tunnelSocket ,
251- servername : parsedUrl . hostname ,
252- } )
253- }
254-
255- return new Promise ( ( resolve , reject ) => {
256- const req = https . get ( reqOptions , ( res ) => {
257- if ( res . statusCode === 302 || res . statusCode === 301 ) {
258- res . resume ( )
259- return httpGet ( new URL ( res . headers . location , url ) . href , options )
260- . then ( resolve )
261- . catch ( reject )
262- }
263- resolve ( res )
264- } )
265-
266- req . on ( 'error' , reject )
267-
268- const timeout = options . timeout || CONFIG . requestTimeout
269- req . setTimeout ( timeout , ( ) => {
270- req . destroy ( )
271- reject ( new Error ( 'Request timeout.' ) )
272- } )
273- } )
274- }
275-
276163async function getLatestVersion ( ) {
277164 try {
278165 const res = await httpGet (
0 commit comments