Skip to content

Commit

Permalink
fix: 🛠️ update proxy setup logic in fetch module
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Nov 21, 2024
1 parent d5a57e4 commit 82333ed
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
6 changes: 3 additions & 3 deletions docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1686,9 +1686,9 @@ camelcase@^8.0.0:
integrity sha512-8WB3Jcas3swSvjIeA2yvCJ+Miyz5l1ZmB6HFb9R1317dt9LCQoswg/BGrmAmkWVEszSrrg4RwmO46qIm2OEnSA==

caniuse-lite@^1.0.30001669:
version "1.0.30001680"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001680.tgz#5380ede637a33b9f9f1fc6045ea99bd142f3da5e"
integrity sha512-rPQy70G6AGUMnbwS1z6Xg+RkHYPAi18ihs47GH0jcxIG7wArmPgY3XbS2sRdBbxJljp3thdT8BIqv9ccCypiPA==
version "1.0.30001682"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001682.tgz#e44dab724829893a4e4fe679ed077eb4cdc34039"
integrity sha512-rJFwz3yRO6NU6Y8aEJKPzS4fngOE8j05pd33FW5Uk9v9b5StWNhGFeVpogwS2FFl78wNDGW5NsVvlwySPEDU5w==

ccount@^2.0.0:
version "2.0.1"
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,5 @@
"npm-check-updates": "^17.1.11",
"prettier": "^3.3.3",
"zx": "^8.2.2"
},
"dependencies": {
"https-proxy-agent": "^7.0.5"
}
}
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"gpt-tokenizer": "^2.6.2",
"html-escaper": "^3.0.3",
"html-to-text": "^9.0.5",
"https-proxy-agent": "^7.0.5",
"ignore": "^6.0.2",
"inflection": "^3.0.0",
"ini": "^5.0.0",
Expand Down
24 changes: 11 additions & 13 deletions packages/core/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import { errorMessage } from "./error"
import { logVerbose, roundWithPrecision, toStringList } from "./util"
import { CancellationToken } from "./cancellation"
import { readText } from "./fs"
import { HttpsProxyAgent } from 'https-proxy-agent';


// We create a proxy based on Node.js environment variables.
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
const agent = proxy ? new HttpsProxyAgent(proxy) : null;

import { HttpsProxyAgent } from "https-proxy-agent"

/**
* Creates a fetch function with retry logic.
Expand Down Expand Up @@ -47,11 +41,15 @@ export async function createFetch(
cancellationToken,
} = options || {}

// We enrich crossFetch with the proxy.
let crossFetchWithProxy = (url: string | WorkspaceFile, options = {}) => {
return crossFetch(url, { ...options, agent })
};
// We create a proxy based on Node.js environment variables.
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY
const agent = proxy ? new HttpsProxyAgent(proxy) : null

// We enrich crossFetch with the proxy.
const crossFetchWithProxy: typeof fetch = agent
? (url, options) =>
crossFetch(url, { ...(options || {}), agent } as any)
: crossFetch

// Return the default fetch if no retry status codes are specified
if (!retryOn?.length) return crossFetchWithProxy
Expand Down Expand Up @@ -177,11 +175,11 @@ export function traceFetchPost(
: "***") // Mask other authorization headers
)
const cmd = `curl ${url} \\
--no-buffer \\
${Object.entries(headers)
.map(([k, v]) => `-H "${k}: ${v}"`)
.join("\\\n")} \\
.join(" \\\n")} \\
-d '${JSON.stringify(body, null, 2).replace(/'/g, "'\\''")}'
--no-buffer
`
if (trace) trace.detailsFenced(`✉️ fetch`, cmd, "bash")
else logVerbose(cmd)
Expand Down
6 changes: 3 additions & 3 deletions slides/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2068,9 +2068,9 @@ cacheable-request@^10.2.8:
responselike "^3.0.0"

caniuse-lite@^1.0.30001669:
version "1.0.30001680"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001680.tgz#5380ede637a33b9f9f1fc6045ea99bd142f3da5e"
integrity sha512-rPQy70G6AGUMnbwS1z6Xg+RkHYPAi18ihs47GH0jcxIG7wArmPgY3XbS2sRdBbxJljp3thdT8BIqv9ccCypiPA==
version "1.0.30001682"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001682.tgz#e44dab724829893a4e4fe679ed077eb4cdc34039"
integrity sha512-rJFwz3yRO6NU6Y8aEJKPzS4fngOE8j05pd33FW5Uk9v9b5StWNhGFeVpogwS2FFl78wNDGW5NsVvlwySPEDU5w==

ccount@^2.0.0:
version "2.0.1"
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3380,7 +3380,7 @@ https-proxy-agent@^5.0.0:
agent-base "6"
debug "4"

https-proxy-agent@^7.0.0:
https-proxy-agent@^7.0.0, https-proxy-agent@^7.0.5:
version "7.0.5"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-7.0.5.tgz#9e8b5013873299e11fab6fd548405da2d6c602b2"
integrity sha512-1e4Wqeblerz+tMKPIq2EMGiiWW1dIjZOksyHWSUm1rmuvw/how9hBHZ38lAGj5ID4Ik6EdkOw7NmWPy6LAwalw==
Expand Down

2 comments on commit 82333ed

@sahuguet
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM except for the dependency change.
https-proxy-agent is needed when running the scripts in prod.

@sahuguet
Copy link
Contributor

Choose a reason for hiding this comment

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

VS Code is using the lower_case version of the env variables.

Http: Proxy
The proxy setting to use. If not set, will be inherited from the http_proxy and https_proxy environment variables.

We should probably add them to the expression
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || process.env.https_proxy || process.env.http_proxy;

Please sign in to comment.