-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchUtil.js
More file actions
54 lines (48 loc) · 1.6 KB
/
fetchUtil.js
File metadata and controls
54 lines (48 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { HttpsProxyAgent } from 'https-proxy-agent';
import { PROXY_URL, PROXY_ENABLED } from './config.js';
/**
* Proxy-aware fetch wrapper
* Supports HTTP/HTTPS proxies via the PROXY_URL environment variable
* Falls back to standard fetch if no proxy is configured
*/
let proxyAgent = null;
// Initialize proxy agent if configured
if (PROXY_ENABLED) {
try {
proxyAgent = new HttpsProxyAgent(PROXY_URL);
console.log(`Proxy enabled: ${PROXY_URL.replace(/:[^:@]*@/, ':****@')}`); // Hide password in logs
} catch (error) {
console.error(`Failed to initialize proxy agent: ${error.message}`);
console.error('Proxy will be disabled. Continuing without proxy support.');
}
}
/**
* Proxy-aware fetch function
* @param {string} url - URL to fetch
* @param {object} options - Fetch options
* @returns {Promise<Response>} Fetch response
*/
export async function proxyFetch(url, options = {}) {
// If proxy is enabled and agent is initialized, add it to options
if (proxyAgent && (url.startsWith('http://') || url.startsWith('https://'))) {
// Create a new options object to avoid mutating the original
const proxyOptions = {
...options,
// Use dispatcher for undici (Node's fetch implementation)
dispatcher: proxyAgent
};
return fetch(url, proxyOptions);
}
// Use standard fetch if no proxy configured
return fetch(url, options);
}
/**
* Get proxy status information
* @returns {object} Proxy status
*/
export function getProxyStatus() {
return {
enabled: PROXY_ENABLED,
url: PROXY_ENABLED ? PROXY_URL.replace(/:[^:@]*@/, ':****@') : null
};
}