Browse Source

Merge pull request #10417 from hangxingliu/liu/fix-build-proxy

build: load the proxy from the system env for Pyodide to download packages
Timothy Jaeryang Baek 2 months ago
parent
commit
2b913a99a3
3 changed files with 43 additions and 0 deletions
  1. 10 0
      package-lock.json
  2. 1 0
      package.json
  3. 32 0
      scripts/prepare-pyodide.js

+ 10 - 0
package-lock.json

@@ -63,6 +63,7 @@
 				"svelte-sonner": "^0.3.19",
 				"tippy.js": "^6.3.7",
 				"turndown": "^7.2.0",
+				"undici": "^7.3.0",
 				"uuid": "^9.0.1",
 				"vite-plugin-static-copy": "^2.2.0"
 			},
@@ -11528,6 +11529,15 @@
 				"node": "*"
 			}
 		},
+		"node_modules/undici": {
+			"version": "7.3.0",
+			"resolved": "https://registry.npmjs.org/undici/-/undici-7.3.0.tgz",
+			"integrity": "sha512-Qy96NND4Dou5jKoSJ2gm8ax8AJM/Ey9o9mz7KN1bb9GP+G0l20Zw8afxTnY2f4b7hmhn/z8aC2kfArVQlAhFBw==",
+			"license": "MIT",
+			"engines": {
+				"node": ">=20.18.1"
+			}
+		},
 		"node_modules/undici-types": {
 			"version": "5.26.5",
 			"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",

+ 1 - 0
package.json

@@ -106,6 +106,7 @@
 		"svelte-sonner": "^0.3.19",
 		"tippy.js": "^6.3.7",
 		"turndown": "^7.2.0",
+		"undici": "^7.3.0",
 		"uuid": "^9.0.1",
 		"vite-plugin-static-copy": "^2.2.0"
 	},

+ 32 - 0
scripts/prepare-pyodide.js

@@ -16,8 +16,39 @@ const packages = [
 ];
 
 import { loadPyodide } from 'pyodide';
+import { setGlobalDispatcher, ProxyAgent } from 'undici';
 import { writeFile, readFile, copyFile, readdir, rmdir } from 'fs/promises';
 
+/**
+ * Loading network proxy configurations from the environment variables.
+ * And the proxy config with lowercase name has the highest priority to use.
+ */
+function initNetworkProxyFromEnv() {
+	// we assume all subsequent requests in this script are HTTPS:
+	// https://cdn.jsdelivr.net
+	// https://pypi.org
+	// https://files.pythonhosted.org
+	const allProxy = process.env.all_proxy || process.env.ALL_PROXY;
+	const httpsProxy = process.env.https_proxy || process.env.HTTPS_PROXY;
+	const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY;
+	const preferedProxy = httpsProxy || allProxy || httpProxy;
+	/**
+	 * use only http(s) proxy because socks5 proxy is not supported currently:
+	 * @see https://github.com/nodejs/undici/issues/2224
+	 */
+	if (!preferedProxy || !preferedProxy.startsWith('http')) return;
+	let preferedProxyURL
+	try {
+		preferedProxyURL = new URL(preferedProxy).toString();
+	} catch {
+		console.warn(`Invalid network proxy URL: "${preferedProxy}"`);
+		return;
+	}
+	const dispatcher = new ProxyAgent({ uri: preferedProxyURL });
+	setGlobalDispatcher(dispatcher);
+	console.log(`Initialized network proxy "${preferedProxy}" from env`);
+}
+
 async function downloadPackages() {
 	console.log('Setting up pyodide + micropip');
 
@@ -84,5 +115,6 @@ async function copyPyodide() {
 	}
 }
 
+initNetworkProxyFromEnv();
 await downloadPackages();
 await copyPyodide();