What is hpagent?
The hpagent npm package provides HTTP and HTTPS agents optimized for HTTP/1.1 keep-alive connections. It is designed to improve performance by reusing TCP connections for multiple requests, reducing latency and resource consumption.
What are hpagent's main functionalities?
HTTP Agent
This feature allows you to create an HTTP agent that uses a proxy server. The agent is configured to keep connections alive, which can improve performance by reusing TCP connections.
const { HttpProxyAgent } = require('hpagent');
const http = require('http');
const agent = new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
proxy: 'http://localhost:8080'
});
http.get('http://example.com', { agent }, (res) => {
res.on('data', (chunk) => {
console.log(chunk.toString());
});
});
HTTPS Agent
This feature allows you to create an HTTPS agent that uses a proxy server. Similar to the HTTP agent, it is configured to keep connections alive for better performance.
const { HttpsProxyAgent } = require('hpagent');
const https = require('https');
const agent = new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
proxy: 'http://localhost:8080'
});
https.get('https://example.com', { agent }, (res) => {
res.on('data', (chunk) => {
console.log(chunk.toString());
});
});
Other packages similar to hpagent
https-proxy-agent
The https-proxy-agent package provides an HTTP/HTTPS agent for proxying HTTP and HTTPS requests. It is similar to hpagent but does not focus specifically on keep-alive connections. It is more general-purpose and widely used for proxying requests.
http-proxy-agent
The http-proxy-agent package provides an HTTP agent for proxying HTTP requests. Like https-proxy-agent, it is a general-purpose proxy agent and does not specifically optimize for keep-alive connections. It is useful for scenarios where you need to proxy HTTP requests.
agentkeepalive
The agentkeepalive package is an HTTP/HTTPS agent that focuses on keep-alive connections. It is similar to hpagent in its goal to improve performance by reusing TCP connections. However, it does not include built-in support for proxying requests.
hpagent
A ready to use http and https agent for working with proxies that keeps connections alive!
Install
npm install hpagent
Usage
Based on your infrastructure, you should use the http agent or the https agent.
The following table will help you picking the right one.
Type | Proxy | Server |
---|
HttpProxyAgent | HTTP | HTTP |
HttpProxyAgent | HTTPS | HTTP |
HttpsProxyAgent | HTTP | HTTPS |
HttpsProxyAgent | HTTPS | HTTPS |
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent')
Once you have understood the right agent for your use case, you can instance it. It takes the same parameter of the Node.js core's http(s) agent and an additional proxy
option, which is the url of your proxy.
const http = require('http')
const { HttpProxyAgent } = require('hpagent')
const agent = new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
proxy: 'http://localhost:8080'
})
http.get('http://localhost:9200', { agent })
.on('response', console.log)
.end()
If your proxy requires basic authentication, you can configure it in the proxy url:
const http = require('http')
const { HttpProxyAgent } = require('hpagent')
const agent = new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
proxy: 'http://user:pwd@localhost:8080'
})
http.get('http://localhost:9200', { agent })
.on('response', console.log)
.end()
Integrations
Following you can find the list of userland http libraries that are tested with this agent.
got('http://localhost:9200', {
agent: {
http: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: 'http://localhost:8080'
})
}
})
needle('get', 'http://localhost:9200', {
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: 'http://localhost:8080'
})
})
fetch('http://localhost:9200', {
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: 'http://localhost:8080'
})
})
sget.concat({
url: `http://${server.address().address}:${server.address().port}`,
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
}, function (err, response, data) {
})
License
This software is licensed under the MIT.