Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lazy load ProxyAgent to avoid bundling all the undici #1800

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/http-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as ifm from './interfaces'
import * as net from 'net'
import * as pm from './proxy'
import * as tunnel from 'tunnel'
import {ProxyAgent} from 'undici'
import type ProxyAgent from 'undici/types/proxy-agent'
aminya marked this conversation as resolved.
Show resolved Hide resolved

export enum HttpCodes {
OK = 200,
Expand Down Expand Up @@ -722,6 +722,12 @@ 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
Comment on lines +726 to +729
Copy link
Author

@aminya aminya Aug 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be further improved by using dynamic imports. However, I didn't include it in this pull request, because using dynamic imports needs making getAgentDispatcher async, which changes the return type of a public method. Based on my research, this is only used in the github package, so it should be fine, but if we want to go with that, we need to have a major version bump for http-client.

aminya@0edf3eb


proxyAgent = new ProxyAgent({
uri: proxyUrl.href,
pipelining: !this._keepAlive ? 0 : 1,
Expand Down