@@ -4,8 +4,11 @@ import os from 'node:os'
44import cp from 'node:child_process'
55import { format } from 'node:util'
66
7+ import fetch , { type RequestInit } from 'node-fetch'
78import { XMLParser } from 'fast-xml-parser'
89import { BlobReader , BlobWriter , ZipReader } from '@zip.js/zip.js'
10+ import { HttpsProxyAgent } from 'https-proxy-agent'
11+ import { HttpProxyAgent } from 'http-proxy-agent'
912
1013import findEdgePath from './finder.js'
1114import { TAGGED_VERSIONS , EDGE_PRODUCTS_API , EDGEDRIVER_BUCKET , TAGGED_VERSION_URL , LATEST_RELEASE_URL , DOWNLOAD_URL , BINARY_FILE , log } from './constants.js'
@@ -20,6 +23,13 @@ interface ProductAPIResponse {
2023 } [ ]
2124}
2225
26+ const fetchOpts : RequestInit = { }
27+ if ( process . env . HTTPS_PROXY ) {
28+ fetchOpts . agent = new HttpsProxyAgent ( process . env . HTTPS_PROXY )
29+ } else if ( process . env . HTTP_PROXY ) {
30+ fetchOpts . agent = new HttpProxyAgent ( process . env . HTTP_PROXY )
31+ }
32+
2333export async function download (
2434 edgeVersion : string = process . env . EDGEDRIVER_VERSION ,
2535 cacheDir : string = process . env . EDGEDRIVER_CACHE_DIR || os . tmpdir ( )
@@ -55,7 +65,7 @@ async function downloadDriver(version: string) {
5565 try {
5666 const downloadUrl = format ( DOWNLOAD_URL , version , getNameByArchitecture ( ) )
5767 log . info ( `Downloading Edgedriver from ${ downloadUrl } ` )
58- const res = await fetch ( downloadUrl )
68+ const res = await fetch ( downloadUrl , fetchOpts )
5969
6070 if ( ! res . body || ! res . ok || res . status !== 200 ) {
6171 throw new Error ( `Failed to download binary from ${ downloadUrl } (statusCode ${ res . status } )` )
@@ -75,6 +85,7 @@ async function downloadDriver(version: string) {
7585 : 'linux'
7686 log . info ( `Attempt to fetch latest v${ majorVersion } for ${ platform } from ${ EDGEDRIVER_BUCKET } ` )
7787 const versions = await fetch ( EDGEDRIVER_BUCKET , {
88+ ...fetchOpts ,
7889 headers : {
7990 accept : '*/*' ,
8091 'accept-language' : 'en-US,en;q=0.9' ,
@@ -95,11 +106,11 @@ async function downloadDriver(version: string) {
95106 }
96107
97108 log . info ( `Downloading alternative Edgedriver version from ${ alternativeDownloadUrl } ` )
98- const versionResponse = await fetch ( alternativeDownloadUrl )
109+ const versionResponse = await fetch ( alternativeDownloadUrl , fetchOpts )
99110 const alternativeVersion = sanitizeVersion ( await versionResponse . text ( ) )
100111 const downloadUrl = format ( DOWNLOAD_URL , alternativeVersion , getNameByArchitecture ( ) )
101112 log . info ( `Downloading Edgedriver from ${ downloadUrl } ` )
102- const res = await fetch ( downloadUrl )
113+ const res = await fetch ( downloadUrl , fetchOpts )
103114 if ( ! res . body || ! res . ok || res . status !== 200 ) {
104115 throw new Error ( `Failed to download binary from ${ downloadUrl } (statusCode ${ res . status } )` )
105116 }
@@ -168,7 +179,7 @@ export async function fetchVersion (edgeVersion: string) {
168179 * if browser version is a tagged version, e.g. stable, beta, dev, canary
169180 */
170181 if ( TAGGED_VERSIONS . includes ( edgeVersion . toLowerCase ( ) ) ) {
171- const apiResponse = await fetch ( EDGE_PRODUCTS_API ) . catch ( ( err ) => {
182+ const apiResponse = await fetch ( EDGE_PRODUCTS_API , fetchOpts ) . catch ( ( err ) => {
172183 log . error ( `Couldn't fetch version from ${ EDGE_PRODUCTS_API } : ${ err . stack } ` )
173184 return { json : async ( ) => [ ] as ProductAPIResponse [ ] }
174185 } )
@@ -193,7 +204,7 @@ export async function fetchVersion (edgeVersion: string) {
193204 return productVersion
194205 }
195206
196- const res = await fetch ( format ( TAGGED_VERSION_URL , edgeVersion . toUpperCase ( ) ) )
207+ const res = await fetch ( format ( TAGGED_VERSION_URL , edgeVersion . toUpperCase ( ) ) , fetchOpts )
197208 return sanitizeVersion ( await res . text ( ) )
198209 }
199210
@@ -205,7 +216,7 @@ export async function fetchVersion (edgeVersion: string) {
205216 const [ major ] = edgeVersion . match ( MATCH_VERSION )
206217 const url = format ( LATEST_RELEASE_URL , major . toString ( ) . toUpperCase ( ) , platform . toUpperCase ( ) )
207218 log . info ( `Fetching latest version from ${ url } ` )
208- const res = await fetch ( url )
219+ const res = await fetch ( url , fetchOpts )
209220 if ( ! res . ok || res . status !== 200 ) {
210221 throw new Error ( `Couldn't detect version for ${ edgeVersion } ` )
211222 }
0 commit comments