javascript-proxy-headers 0.4.0 keeps undici and got requests on the proxy you configured, and it makes the ky adapter return a native Response. If you use the undici, got, or ky adapter, upgrade. Your settings and request code stay the same, except that ky users shouldn't rely on clone() to copy proxyHeaders.
npm install javascript-proxy-headers@0.4.0
Install the HTTP client you use too, such as got, undici, or ky, if it isn't already in your project. Other adapters are unchanged in this release.
undici requests no longer go direct
On 0.3.0, an undici request could reach the target directly instead of going through the proxy. 0.4.0 keeps that request on the CONNECT tunnel, so your call site can stay the same if you already pass proxy and proxyHeaders. If the proxy accepts CONNECT but doesn't forward the tunnel, 0.4.0 doesn't open a connection to the target, and the request fails with UND_ERR_CONNECT_TIMEOUT once your connectTimeout passes.
The undici adapter also gains two options: tlsOptions for the TLS connection to the target, and connectTimeout, a timeout in milliseconds for the CONNECT and TLS handshake.
got now rejects plain HTTP
The got adapter is HTTPS-only. Without a guard, got uses its default HTTP agent for an http:// URL and connects directly, which skips the proxy. 0.4.0 rejects http:// URLs and HTTPS-to-HTTP redirects with a TypeError:
javascript-proxy-headers/got only supports HTTPS; HTTP would bypass the proxy
Your HTTPS got code doesn't need to change. If you see this error, switch the URL to https://.
ky 2.2 and later gets a Response it accepts
ky 2.2 and later requires fetch to resolve with a global Response, and the node-fetch Response the adapter returned failed that check. 0.4.0 returns a global Response with the same proxyHeaders Map attached. You still need node-fetch, and createProxyKy(...) is still async.
response.clone() doesn't copy proxyHeaders. If you clone a response, read proxyHeaders from the original.
Example: choose a country and read the proxy IP
This got example sends X-ProxyMesh-Country on the CONNECT request and reads the proxy's reply. Use the host and port from Proxy Server Locations. X-ProxyMesh-Country works only on the World and Open proxies.
import { createProxyGot } from 'javascript-proxy-headers/got';
const client = await createProxyGot({
proxy: 'http://USERNAME:PASSWORD@PROXY_HOST:PORT',
proxyHeaders: { 'X-ProxyMesh-Country': 'US' },
});
const response = await client('https://api.ipify.org?format=json');
console.log(response.proxyHeaders.get('x-proxymesh-ip'));
Look for the proxy IP in response.proxyHeaders, not in response.headers. The request and response headers article lists every header ProxyMesh accepts and returns.
Coming from 0.2.x
0.3.0 already moved the proxy's CONNECT response headers off response.headers, and 0.4.0 doesn't change that. If you still read response.headers['x-proxymesh-ip'], switch to response.proxyHeaders.get('x-proxymesh-ip').
Install it from javascript-proxy-headers 0.4.0 on npm. The GitHub release and the full diff from 0.3.0 list the changes. The README and docs cover each adapter, and the JavaScript proxy configuration examples show the ProxyMesh side.