HTTP/HTTPS Proxy Agent for Node.js
hpagent is a Node.js library providing HttpProxyAgent and HttpsProxyAgent classes designed to facilitate HTTP and HTTPS requests through proxy servers while leveraging connection keep-alive mechanisms. Currently at version 1.2.0, it maintains an active development status with periodic releases introducing features and bug fixes, such as strict TypeScript support and improved proxy handling. A key differentiator is its ability to serve as a drop-in replacement for Node.js's native `http` and `https` agents, offering robust proxy configuration options, including basic authentication via URL or custom headers, and the ability to pass specific options for the proxy CONNECT request. The library explicitly demonstrates compatibility with popular userland HTTP clients like `got`, `needle`, `node-fetch`, and `simple-get`, and ships with TypeScript type definitions, making it suitable for modern Node.js applications (Node.js >=14 is required since v1.0.0).
Common errors
-
Error: self signed certificate in certificate chain
cause Attempting to connect to an HTTPS proxy that uses a self-signed or untrusted TLS certificate without providing the necessary CA certificates.fixAdd the proxy's certificate(s) to the `proxyConnectOptions.ca` array, or configure Node.js to trust the certificate globally (less secure). -
ERR_INVALID_URL: Invalid URL
cause The `proxy` URL string provided to the agent constructor is malformed or uses an unsupported protocol scheme.fixEnsure the `proxy` option is a valid URL string, including the protocol (e.g., `http://`, `https://`), host, and port, like `http://localhost:8080`.
Warnings
- breaking hpagent v1.0.0 dropped support for Node.js versions older than 14. Users on Node.js < 14 must upgrade their runtime or remain on hpagent v0.x.
- gotcha Choosing the correct agent (HttpProxyAgent vs. HttpsProxyAgent) is crucial. The selection depends on *both* the proxy's protocol and the target server's protocol. Incorrect selection will lead to connection errors.
- gotcha Proxy authentication requires careful handling. Basic authentication can be embedded directly in the proxy URL (e.g., `http://user:pwd@host:port`) or provided via custom `Proxy-Authorization` headers within `proxyConnectOptions.headers`. Misconfigured or missing credentials will result in authentication failures.
- gotcha When connecting to an HTTPS proxy that uses a self-signed or otherwise untrusted TLS certificate, you must provide the proxy's certificate(s) via the `proxyConnectOptions.ca` array to prevent TLS handshake errors.
Install
-
npm install hpagent -
yarn add hpagent -
pnpm add hpagent
Imports
- HttpProxyAgent
const HttpProxyAgent = require('hpagent').HttpProxyAgentimport { HttpProxyAgent } from 'hpagent' - HttpsProxyAgent
const HttpsProxyAgent = require('hpagent').HttpsProxyAgentimport { HttpsProxyAgent } from 'hpagent' - HttpProxyAgentOptions
import type { HttpProxyAgentOptions } from 'hpagent'
Quickstart
import http from 'node:http';
import { HttpProxyAgent } from 'hpagent';
const PROXY_USER = process.env.PROXY_USERNAME ?? 'user';
const PROXY_PASS = process.env.PROXY_PASSWORD ?? 'pwd';
const PROXY_HOST = process.env.PROXY_HOST ?? 'localhost';
const PROXY_PORT = process.env.PROXY_PORT ?? '8080';
const TARGET_HOST = process.env.TARGET_HOST ?? 'localhost';
const TARGET_PORT = process.env.TARGET_PORT ?? '9200';
const agent = new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
proxy: `http://${PROXY_USER}:${PROXY_PASS}@${PROXY_HOST}:${PROXY_PORT}`
});
http.get(`http://${TARGET_HOST}:${TARGET_PORT}`, { agent })
.on('response', (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
})
.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
})
.end();