1# hpagent
2
3[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)  ![build](https://github.com/delvedor/hpagent/workflows/build/badge.svg)
4
5A ready to use http and https agent for working with proxies that keeps connections alive!
6
7## Install
8
9```
10npm install hpagent
11```
12
13## Usage
14
15Based on your infrastructure, you should use the http agent or the https agent.
16The following table will help you picking the right one.
17
18| Type              | Proxy  | Server |
19|-------------------|--------|--------|
20| `HttpProxyAgent`  | HTTP   | HTTP   |
21| `HttpProxyAgent`  | HTTPS  | HTTP   |
22| `HttpsProxyAgent` | HTTP   | HTTPS  |
23| `HttpsProxyAgent` | HTTPS  | HTTPS  |
24
25```js
26const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent')
27```
28
29Once 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.
30
31```js
32const http = require('http')
33const { HttpProxyAgent } = require('hpagent')
34
35const agent = new HttpProxyAgent({
36  keepAlive: true,
37  keepAliveMsecs: 1000,
38  maxSockets: 256,
39  maxFreeSockets: 256,
40  proxy: 'http://localhost:8080'
41})
42
43http.get('http://localhost:9200', { agent })
44    .on('response', console.log)
45    .end()
46```
47
48If your proxy requires basic authentication, you can configure it in the proxy url:
49
50```js
51const http = require('http')
52const { HttpProxyAgent } = require('hpagent')
53
54const agent = new HttpProxyAgent({
55  keepAlive: true,
56  keepAliveMsecs: 1000,
57  maxSockets: 256,
58  maxFreeSockets: 256,
59  proxy: 'http://user:pwd@localhost:8080'
60})
61
62http.get('http://localhost:9200', { agent })
63    .on('response', console.log)
64    .end()
65```
66
67## Integrations
68
69Following you can find the list of userland http libraries that are tested with this agent.
70
71### [got](https://github.com/sindresorhus/got)
72
73```js
74got('http://localhost:9200', {
75  agent: {
76    http: new HttpProxyAgent({
77      keepAlive: true,
78      keepAliveMsecs: 1000,
79      maxSockets: 256,
80      maxFreeSockets: 256,
81      scheduling: 'lifo',
82      proxy: 'http://localhost:8080'
83    })
84  }
85})
86```
87
88### [needle](https://github.com/tomas/needle)
89
90```js
91needle('get', 'http://localhost:9200', {
92  agent: new HttpProxyAgent({
93    keepAlive: true,
94    keepAliveMsecs: 1000,
95    maxSockets: 256,
96    maxFreeSockets: 256,
97    scheduling: 'lifo',
98    proxy: 'http://localhost:8080'
99  })
100})
101```
102
103### [node-fetch](https://github.com/node-fetch/node-fetch)
104
105```js
106fetch('http://localhost:9200', {
107  agent: new HttpProxyAgent({
108    keepAlive: true,
109    keepAliveMsecs: 1000,
110    maxSockets: 256,
111    maxFreeSockets: 256,
112    scheduling: 'lifo',
113    proxy: 'http://localhost:8080'
114  })
115})
116```
117
118## License
119
120This software is licensed under the [MIT](./LICENSE).
121