|
| 1 | +/** |
| 2 | + * needle extension for proxy header support. |
| 3 | + */ |
| 4 | + |
| 5 | +import { createRequire } from 'module'; |
| 6 | +import { ProxyHeadersAgent } from './core/proxy-headers-agent.js'; |
| 7 | + |
| 8 | +const require = createRequire(import.meta.url); |
| 9 | + |
| 10 | +function mergeProxyHeadersIntoResponse(res, map) { |
| 11 | + if (!res || !map) return; |
| 12 | + for (const [key, value] of map) { |
| 13 | + if (res.headers[key] == null) { |
| 14 | + res.headers[key] = value; |
| 15 | + } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Perform a GET with proxy headers (promise-based). |
| 21 | + * |
| 22 | + * @param {string} url - Target URL (HTTPS recommended) |
| 23 | + * @param {Object} options - Options |
| 24 | + * @param {string} options.proxy - Proxy URL |
| 25 | + * @param {Object} [options.proxyHeaders] - CONNECT headers |
| 26 | + * @param {Function} [options.onProxyConnect] - CONNECT callback |
| 27 | + * @param {Object} [options.needleOptions] - Extra needle options |
| 28 | + * @returns {Promise<import('needle').NeedleResponse>} |
| 29 | + */ |
| 30 | +export async function proxyNeedleGet(url, options = {}) { |
| 31 | + const { proxy, proxyHeaders = {}, onProxyConnect, needleOptions = {} } = options; |
| 32 | + |
| 33 | + if (!proxy) { |
| 34 | + throw new Error('proxy option is required'); |
| 35 | + } |
| 36 | + |
| 37 | + const needle = require('needle'); |
| 38 | + |
| 39 | + const agent = new ProxyHeadersAgent(proxy, { |
| 40 | + proxyHeaders, |
| 41 | + onProxyConnect, |
| 42 | + }); |
| 43 | + |
| 44 | + return new Promise((resolve, reject) => { |
| 45 | + needle.get( |
| 46 | + url, |
| 47 | + { |
| 48 | + ...needleOptions, |
| 49 | + agent, |
| 50 | + proxy: null, |
| 51 | + use_proxy_from_env_var: false, |
| 52 | + }, |
| 53 | + (err, res) => { |
| 54 | + if (err) { |
| 55 | + reject(err); |
| 56 | + return; |
| 57 | + } |
| 58 | + mergeProxyHeadersIntoResponse(res, agent.lastProxyHeaders); |
| 59 | + res.proxyAgent = agent; |
| 60 | + resolve(res); |
| 61 | + }, |
| 62 | + ); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Create a small API bound to one proxy configuration. |
| 68 | + * |
| 69 | + * @param {Object} options - Same as proxyNeedleGet base options (without url) |
| 70 | + * @returns {{ get: Function, proxyAgent: ProxyHeadersAgent }} |
| 71 | + */ |
| 72 | +export function createProxyNeedle(options) { |
| 73 | + const { proxy, proxyHeaders = {}, onProxyConnect, needleOptions = {} } = options; |
| 74 | + |
| 75 | + if (!proxy) { |
| 76 | + throw new Error('proxy option is required'); |
| 77 | + } |
| 78 | + |
| 79 | + const agent = new ProxyHeadersAgent(proxy, { |
| 80 | + proxyHeaders, |
| 81 | + onProxyConnect, |
| 82 | + }); |
| 83 | + |
| 84 | + const needle = require('needle'); |
| 85 | + |
| 86 | + return { |
| 87 | + proxyAgent: agent, |
| 88 | + get(url, opts = {}) { |
| 89 | + return new Promise((resolve, reject) => { |
| 90 | + needle.get( |
| 91 | + url, |
| 92 | + { |
| 93 | + ...needleOptions, |
| 94 | + ...opts, |
| 95 | + agent, |
| 96 | + proxy: null, |
| 97 | + use_proxy_from_env_var: false, |
| 98 | + }, |
| 99 | + (err, res) => { |
| 100 | + if (err) { |
| 101 | + reject(err); |
| 102 | + return; |
| 103 | + } |
| 104 | + mergeProxyHeadersIntoResponse(res, agent.lastProxyHeaders); |
| 105 | + res.proxyAgent = agent; |
| 106 | + resolve(res); |
| 107 | + }, |
| 108 | + ); |
| 109 | + }); |
| 110 | + }, |
| 111 | + }; |
| 112 | +} |
0 commit comments