UNPKG

3.19 kBMarkdownView Raw
1proxy-agent
2===========
3### Maps proxy protocols to `http.Agent` implementations
4
5This module provides a function that returns proxying `http.Agent` instances to
6use based off of a given proxy URI.
7
8An LRU cache is used so that `http.Agent` instances are transparently re-used for
9subsequent HTTP requests to the same proxy server.
10
11The currently implemented protocol mappings are listed in the table below:
12
13
14| Protocol | Proxy Agent for `http` requests | Proxy Agent for `https` requests | Example
15|:---------:|:-------------------------------:|:--------------------------------:|:--------:
16| `http` | [http-proxy-agent][] | [https-proxy-agent][] | `http://proxy-server-over-tcp.com:3128`
17| `https` | [http-proxy-agent][] | [https-proxy-agent][] | `https://proxy-server-over-tls.com:3129`
18| `socks` | [socks-proxy-agent][] | [socks-proxy-agent][] | `socks://some-socks-proxy.com:9050`
19
20
21Installation
22------------
23
24Install with `npm`:
25
26``` bash
27$ npm install proxy-agent
28```
29
30
31Example
32-------
33
34``` js
35var http = require('http');
36var proxy = require('proxy-agent');
37
38// HTTP, HTTPS, or SOCKS proxy to use
39var proxyUri = process.env.http_proxy || 'http://168.63.43.102:3128';
40
41var opts = {
42 method: 'GET',
43 host: 'jsonip.org',
44 path: '/',
45 // this is the important part!
46 agent: proxy(proxyUri)
47};
48
49// the rest works just like any other normal HTTP request
50http.get(opts, onresponse);
51
52function onresponse (res) {
53 console.log(res.statusCode, res.headers);
54 res.pipe(process.stdout);
55}
56```
57
58
59API
60---
61
62### proxy(String uri, Boolean secure) → http.Agent
63
64Returns an `http.Agent` instance based off of the given proxy `uri`, and `secure`
65boolean flag. An LRU cache is used, so the same `http.Agent` instance will be
66returned if identical args are passed in.
67
68
69License
70-------
71
72(The MIT License)
73
74Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
75
76Permission is hereby granted, free of charge, to any person obtaining
77a copy of this software and associated documentation files (the
78'Software'), to deal in the Software without restriction, including
79without limitation the rights to use, copy, modify, merge, publish,
80distribute, sublicense, and/or sell copies of the Software, and to
81permit persons to whom the Software is furnished to do so, subject to
82the following conditions:
83
84The above copyright notice and this permission notice shall be
85included in all copies or substantial portions of the Software.
86
87THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
88EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
89MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
90IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
91CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
92TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
93SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
94
95
96[http-proxy-agent]: https://github.com/TooTallNate/node-http-proxy-agent
97[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
98[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent