Skip to content

Commit 2b913a9

Browse files
authored
Merge pull request open-webui#10417 from hangxingliu/liu/fix-build-proxy
build: load the proxy from the system env for Pyodide to download packages
2 parents 0994a07 + 3ae7302 commit 2b913a9

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"svelte-sonner": "^0.3.19",
107107
"tippy.js": "^6.3.7",
108108
"turndown": "^7.2.0",
109+
"undici": "^7.3.0",
109110
"uuid": "^9.0.1",
110111
"vite-plugin-static-copy": "^2.2.0"
111112
},

scripts/prepare-pyodide.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,39 @@ const packages = [
1616
];
1717

1818
import { loadPyodide } from 'pyodide';
19+
import { setGlobalDispatcher, ProxyAgent } from 'undici';
1920
import { writeFile, readFile, copyFile, readdir, rmdir } from 'fs/promises';
2021

22+
/**
23+
* Loading network proxy configurations from the environment variables.
24+
* And the proxy config with lowercase name has the highest priority to use.
25+
*/
26+
function initNetworkProxyFromEnv() {
27+
// we assume all subsequent requests in this script are HTTPS:
28+
// https://cdn.jsdelivr.net
29+
// https://pypi.org
30+
// https://files.pythonhosted.org
31+
const allProxy = process.env.all_proxy || process.env.ALL_PROXY;
32+
const httpsProxy = process.env.https_proxy || process.env.HTTPS_PROXY;
33+
const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY;
34+
const preferedProxy = httpsProxy || allProxy || httpProxy;
35+
/**
36+
* use only http(s) proxy because socks5 proxy is not supported currently:
37+
* @see https://github.com/nodejs/undici/issues/2224
38+
*/
39+
if (!preferedProxy || !preferedProxy.startsWith('http')) return;
40+
let preferedProxyURL
41+
try {
42+
preferedProxyURL = new URL(preferedProxy).toString();
43+
} catch {
44+
console.warn(`Invalid network proxy URL: "${preferedProxy}"`);
45+
return;
46+
}
47+
const dispatcher = new ProxyAgent({ uri: preferedProxyURL });
48+
setGlobalDispatcher(dispatcher);
49+
console.log(`Initialized network proxy "${preferedProxy}" from env`);
50+
}
51+
2152
async function downloadPackages() {
2253
console.log('Setting up pyodide + micropip');
2354

@@ -84,5 +115,6 @@ async function copyPyodide() {
84115
}
85116
}
86117

118+
initNetworkProxyFromEnv();
87119
await downloadPackages();
88120
await copyPyodide();

0 commit comments

Comments
 (0)