@@ -16,8 +16,39 @@ const packages = [
1616] ;
1717
1818import { loadPyodide } from 'pyodide' ;
19+ import { setGlobalDispatcher , ProxyAgent } from 'undici' ;
1920import { 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+
2152async function downloadPackages ( ) {
2253 console . log ( 'Setting up pyodide + micropip' ) ;
2354
@@ -84,5 +115,6 @@ async function copyPyodide() {
84115 }
85116}
86117
118+ initNetworkProxyFromEnv ( ) ;
87119await downloadPackages ( ) ;
88120await copyPyodide ( ) ;
0 commit comments