Skip to content

Commit b5e22d3

Browse files
feat: support for HTTP_PROXY and HTTPS_PROXY
1 parent 7e0e171 commit b5e22d3

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ $ EDGEDRIVER_VERSION=105.0.1343.33 npx edgedriver --version
3131
Microsoft Edge WebDriver 105.0.1343.33 (4122bb4646b33f33bca5d269490b9caadfc452b2)
3232
```
3333

34+
# Setting a PROXY URL
35+
36+
Use `HTTPS_PROXY` or `HTTP_PROXY` to set your proxy URL.
37+
3438
# Programmatic Interface
3539

3640
You can import this package with Node.js and start the driver as part of your script and use it e.g. with [WebdriverIO](https://webdriver.io).

package-lock.json

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"eslint-plugin-unicorn": "^56.0.0",
6060
"globals": "^15.11.0",
6161
"husky": "^9.1.6",
62+
"node-fetch": "^3.3.2",
6263
"npm-run-all2": "^7.0.1",
6364
"release-it": "^17.10.0",
6465
"ts-node": "^10.9.2",
@@ -73,6 +74,8 @@
7374
"decamelize": "^6.0.0",
7475
"edge-paths": "^3.0.5",
7576
"fast-xml-parser": "^4.5.0",
77+
"http-proxy-agent": "^7.0.2",
78+
"https-proxy-agent": "^7.0.5",
7679
"which": "^5.0.0"
7780
}
7881
}

src/install.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import os from 'node:os'
44
import cp from 'node:child_process'
55
import { format } from 'node:util'
66

7+
import fetch, { type RequestInit } from 'node-fetch'
78
import { XMLParser } from 'fast-xml-parser'
89
import { BlobReader, BlobWriter, ZipReader } from '@zip.js/zip.js'
10+
import { HttpsProxyAgent } from 'https-proxy-agent'
11+
import { HttpProxyAgent } from 'http-proxy-agent'
912

1013
import findEdgePath from './finder.js'
1114
import { 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+
2333
export 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
}

tests/unit.test.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
import os from 'node:os'
22
import { vi, test, expect } from 'vitest'
3+
import fetch from 'node-fetch'
34

45
import * as pkgExports from '../src/index.js'
56
import { fetchVersion } from '../src/install.js'
67
import { getNameByArchitecture, parseParams } from '../src/utils.js'
78
import { EDGE_PRODUCTS_API } from '../src/constants.js'
89

9-
import apiResponse from './__fixtures__/api.json' assert { type: 'json' }
10-
1110
vi.mock('node:os', () => ({
1211
default: {
1312
arch: vi.fn(),
1413
platform: vi.fn()
1514
}
1615
}))
1716

18-
const origFetch = globalThis.fetch
19-
vi.stubGlobal('fetch', async (url) => {
20-
if (url === EDGE_PRODUCTS_API) {
21-
return {
22-
json: vi.fn().mockResolvedValue(apiResponse)
23-
}
24-
} else if (!url.includes('LATEST_RELEASE')) {
25-
return {
26-
text: vi.fn().mockResolvedValue('��123.456.789.0')
27-
}
28-
}
17+
vi.mock('node-fetch', async (orig) => {
18+
const origFetch: any = await orig()
19+
const apiResponse = await import('./__fixtures__/api.json', { assert: { type: 'json' } })
20+
return {
21+
default: vi.fn(async (url) => {
22+
if (url === EDGE_PRODUCTS_API) {
23+
return {
24+
json: vi.fn().mockResolvedValue(apiResponse.default)
25+
}
26+
} else if (!url.includes('LATEST_RELEASE')) {
27+
return {
28+
text: vi.fn().mockResolvedValue('��123.456.789.0')
29+
}
30+
}
2931

30-
return origFetch(url)
32+
return origFetch.default(url)
33+
})
34+
}
3135
})
3236

3337
test('fetchVersion', async () => {
@@ -53,7 +57,19 @@ test('fetchVersion', async () => {
5357
vi.mocked(os.arch).mockReturnValue('arm64')
5458
vi.mocked(os.platform).mockReturnValue('darwin')
5559
expect(await fetchVersion('stable')).toBe('121.0.2277.112')
60+
})
5661

62+
test('fetchVersion with proxy support', async () => {
63+
vi.resetModules()
64+
process.env.HTTPS_PROXY = 'https://proxy.com'
65+
const { fetchVersion } = await import('../src/install.js')
66+
expect(await fetchVersion('stable')).toBe('121.0.2277.112')
67+
expect(fetch).toBeCalledWith(
68+
expect.any(String),
69+
expect.objectContaining({
70+
agent: expect.any(Object)
71+
})
72+
)
5773
})
5874

5975
test('getNameByArchitecture', () => {

0 commit comments

Comments
 (0)