Skip to content

Commit

Permalink
fix: import undici via dynamic imports in http-client
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Aug 18, 2024
1 parent 12dca8f commit 0edf3eb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
17 changes: 9 additions & 8 deletions packages/github/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions packages/github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
"url": "https://github.com/actions/toolkit/issues"
},
"dependencies": {
"@actions/http-client": "^2.2.0",
"@actions/http-client": "^2.2.2",
"@octokit/core": "^5.0.1",
"@octokit/plugin-paginate-rest": "^9.0.0",
"@octokit/plugin-rest-endpoint-methods": "^10.0.0"
"@octokit/plugin-rest-endpoint-methods": "^10.0.0",
"undici": "^5.25.4"
},
"devDependencies": {
"proxy": "^2.1.1"
}
}
}
8 changes: 6 additions & 2 deletions packages/github/src/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as http from 'http'
import * as httpClient from '@actions/http-client'
import {OctokitOptions} from '@octokit/core/dist-types/types'
import {ProxyAgent, fetch} from 'undici'
import type ProxyAgent from 'undici/types/proxy-agent'
import {fetch} from 'undici'

export function getAuthString(
token: string,
Expand Down Expand Up @@ -29,8 +30,11 @@ export function getProxyAgentDispatcher(
}

export function getProxyFetch(destinationUrl): typeof fetch {
const httpDispatcher = getProxyAgentDispatcher(destinationUrl)
let httpDispatcher: ProxyAgent | undefined
const proxyFetch: typeof fetch = async (url, opts) => {
if (httpDispatcher === undefined) {
httpDispatcher = await getProxyAgentDispatcher(destinationUrl)
}
return fetch(url, {
...opts,
dispatcher: httpDispatcher
Expand Down
2 changes: 1 addition & 1 deletion packages/glob/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/http-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions packages/http-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,15 @@ export class HttpClient {
return this._getAgent(parsedUrl)
}

getAgentDispatcher(serverUrl: string): ProxyAgent | undefined {
async getAgentDispatcher(serverUrl: string): Promise<ProxyAgent | undefined> {
const parsedUrl = new URL(serverUrl)
const proxyUrl = pm.getProxyUrl(parsedUrl)
const useProxy = proxyUrl && proxyUrl.hostname
if (!useProxy) {
return
return undefined
}

return this._getProxyAgentDispatcher(parsedUrl, proxyUrl)
return await this._getProxyAgentDispatcher(parsedUrl, proxyUrl)
}

private _prepareRequest(
Expand Down Expand Up @@ -709,7 +709,10 @@ export class HttpClient {
return agent
}

private _getProxyAgentDispatcher(parsedUrl: URL, proxyUrl: URL): ProxyAgent {
private async _getProxyAgentDispatcher(
parsedUrl: URL,
proxyUrl: URL
): Promise<ProxyAgent> {
let proxyAgent

if (this._keepAlive) {
Expand All @@ -724,9 +727,9 @@ export class HttpClient {
const usingSsl = parsedUrl.protocol === 'https:'

// Lazy load ProxyAgent to avoid bundling all the undici
const ProxyAgent =
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
require('undici/lib/proxy-agent') as typeof import('undici/types/proxy-agent').default
const ProxyAgent = (await import(
'undici/lib/proxy-agent'
)) as typeof import('undici/types/proxy-agent').default

proxyAgent = new ProxyAgent({
uri: proxyUrl.href,
Expand Down

0 comments on commit 0edf3eb

Please sign in to comment.