UNPKG

3.3 kBMarkdownView Raw
1https-proxy-agent
2================
3### An HTTP(s) proxy `http.Agent` implementation for HTTPS
4
5This module provides an `http.Agent` implementation that connects to a specified
6HTTP or HTTPS proxy server, and can be used with the built-in `https` module.
7
8Specifically, this `Agent` implementation connects to an intermediary "proxy"
9server and issues the [CONNECT HTTP method][CONNECT], which tells the proxy to
10open a direct TCP connection to the destination server.
11
12Since this agent implements the CONNECT HTTP method, it also works with other
13protocols that use this method when connecting over proxies (i.e. WebSockets).
14See the "Examples" section below for more.
15
16Examples
17--------
18
19#### `https` module example
20
21```ts
22import * as https from 'https';
23import { HttpsProxyAgent } from 'https-proxy-agent';
24
25const agent = new HttpsProxyAgent('http://168.63.76.32:3128');
26
27https.get('https://example.com', { agent }, (res) => {
28 console.log('"response" event!', res.headers);
29 res.pipe(process.stdout);
30});
31```
32
33#### `ws` WebSocket connection example
34
35```ts
36import WebSocket from 'ws';
37import { HttpsProxyAgent } from 'https-proxy-agent';
38
39const agent = new HttpsProxyAgent('http://168.63.76.32:3128');
40const socket = new WebSocket('ws://echo.websocket.org', { agent });
41
42socket.on('open', function () {
43 console.log('"open" event!');
44 socket.send('hello world');
45});
46
47socket.on('message', function (data, flags) {
48 console.log('"message" event! %j %j', data, flags);
49 socket.close();
50});
51```
52
53API
54---
55
56### new HttpsProxyAgent(proxy: string | URL, options?: HttpsProxyAgentOptions)
57
58The `HttpsProxyAgent` class implements an `http.Agent` subclass that connects
59to the specified "HTTP(s) proxy server" in order to proxy HTTPS and/or WebSocket
60requests. This is achieved by using the [HTTP `CONNECT` method][CONNECT].
61
62The `proxy` argument is the URL for the proxy server.
63
64The `options` argument accepts the usual `http.Agent` constructor options, and
65some additional properties:
66
67 * `headers` - Object containing additional headers to send to the proxy server
68 in the `CONNECT` request.
69
70
71License
72-------
73
74(The MIT License)
75
76Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
77
78Permission is hereby granted, free of charge, to any person obtaining
79a copy of this software and associated documentation files (the
80'Software'), to deal in the Software without restriction, including
81without limitation the rights to use, copy, modify, merge, publish,
82distribute, sublicense, and/or sell copies of the Software, and to
83permit persons to whom the Software is furnished to do so, subject to
84the following conditions:
85
86The above copyright notice and this permission notice shall be
87included in all copies or substantial portions of the Software.
88
89THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
90EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
91MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
92IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
93CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
94TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
95SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
96
97[CONNECT]: http://en.wikipedia.org/wiki/HTTP_tunnel#HTTP_CONNECT_Tunneling