-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchUtil.js
More file actions
53 lines (47 loc) · 1.54 KB
/
fetchUtil.js
File metadata and controls
53 lines (47 loc) · 1.54 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
import { HttpsProxyAgent } from 'https-proxy-agent';
import { PROXY_URL, PROXY_ENABLED } from './config.js';
import { logger } from './src/util/logger.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;
if (PROXY_ENABLED) {
try {
proxyAgent = new HttpsProxyAgent(PROXY_URL);
logger.info({ proxy: PROXY_URL.replace(/:[^:@]*@/, ':****@') }, 'Proxy enabled');
} catch (error) {
logger.error({ err: error.message }, 'Failed to initialize proxy agent; continuing without proxy');
}
}
/**
* 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
};
}