@@ -2,9 +2,20 @@ agent-base
22==========
33### Turn a function into an [ ` http.Agent ` ] [ http.Agent ] instance
44
5- This module provides an ` http.Agent ` generator. That is, you pass it an async
6- callback function, and it returns a new ` http.Agent ` instance that will invoke the
7- given callback function when sending outbound HTTP requests.
5+ This module is a thin wrapper around the base ` http.Agent ` class.
6+
7+ It provides an absract class that must define a ` connect() ` function,
8+ which is responsible for creating the underlying socket that the HTTP
9+ client requests will use.
10+
11+ The ` connect() ` function may return an arbitrary ` Duplex ` stream, or
12+ another ` http.Agent ` instance to delegate the request to, and may be
13+ asynchronous (by defining an ` async ` function).
14+
15+ Instances of this agent can be used with the ` http ` and ` https `
16+ modules. To differentiate, the options parameter in the ` connect() `
17+ function includes a ` secureEndpoint ` property, which can be checked
18+ to determine what type of socket should be returned.
819
920#### Some subclasses:
1021
@@ -16,96 +27,41 @@ Send a pull request to list yours!
1627 * [ ` pac-proxy-agent ` ] [ pac-proxy-agent ] : A PAC file proxy ` http.Agent ` implementation for HTTP and HTTPS
1728 * [ ` socks-proxy-agent ` ] [ socks-proxy-agent ] : A SOCKS proxy ` http.Agent ` implementation for HTTP and HTTPS
1829
19-
20- Installation
21- ------------
22-
23- Install with ` npm ` :
24-
25- ``` bash
26- $ npm install agent-base
27- ```
28-
29-
3030Example
3131-------
3232
33- Here's a minimal example that creates a new ` net.Socket ` connection to the server
34- for every HTTP request (i.e. the equivalent of ` agent: false ` option):
33+ Here's a minimal example that creates a new ` net.Socket ` or ` tls.Socket `
34+ based on the ` secureEndpoint ` property. This agent can be used with both
35+ the ` http ` and ` https ` modules.
3536
3637``` ts
3738import * as net from ' net' ;
3839import * as tls from ' tls' ;
3940import * as http from ' http' ;
4041import { Agent } from ' agent-base' ;
4142
42- const agent = new Agent (function (req , opts ) {
43- var socket;
44- // `secureEndpoint` is true when using the "https" module
45- if (opts .secureEndpoint ) {
46- socket = tls .connect (opts );
47- } else {
48- socket = net .connect (opts );
43+ class MyAgent extends Agent {
44+ connect(req , opts ) {
45+ // `secureEndpoint` is true when using the "https" module
46+ if (opts .secureEndpoint ) {
47+ return tls .connect (opts );
48+ } else {
49+ return net .connect (opts );
50+ }
4951 }
50- return socket ;
5152});
5253
54+ // Keep alive enabled means that `connect()` will only be
55+ // invoked when a new connection needs to be created
56+ const agent = new MyAgent ({ keepAlive: true });
57+
5358// Pass the `agent` option when creating the HTTP request
5459http .get (' http://nodejs.org/api/' , { agent }, (res ) => {
5560 console .log (' "response" event!' , res .headers );
5661 res .pipe (process .stdout );
5762});
5863```
5964
60- Returning a Promise or using an ` async ` function is also supported:
61-
62- ``` ts
63- new Agent (async (req , opts ) => {
64- await sleep (1000 );
65- // etc…
66- });
67- ```
68-
69- Return another ` http.Agent ` instance to "pass through" the responsibility
70- for that HTTP request to that agent:
71-
72- ``` ts
73- new Agent ((req , opts ) => {
74- return opts .secureEndpoint ? https .globalAgent : http .globalAgent ;
75- });
76- ```
77-
78-
79- API
80- ---
81-
82- ## Agent(Function callback[ , Object options] ) → [ http.Agent] [ ]
83-
84- Creates a base ` http.Agent ` that will execute the callback function ` callback `
85- for every HTTP request that it is used as the ` agent ` for. The callback function
86- is responsible for creating a ` stream.Duplex ` instance of some kind that will be
87- used as the underlying socket in the HTTP request.
88-
89- The ` options ` object accepts the following properties:
90-
91- * ` timeout ` - Number - Timeout for the ` callback() ` function in milliseconds. Defaults to Infinity (optional).
92-
93- The callback function should have the following signature:
94-
95- ### callback(http.ClientRequest req, Object options, Function cb) → undefined
96-
97- The ClientRequest ` req ` can be accessed to read request headers and
98- and the path, etc. The ` options ` object contains the options passed
99- to the ` http.request() ` /` https.request() ` function call, and is formatted
100- to be directly passed to ` net.connect() ` /` tls.connect() ` , or however
101- else you want a Socket to be created. Pass the created socket to
102- the callback function ` cb ` once created, and the HTTP request will
103- continue to proceed.
104-
105- If the ` https ` module is used to invoke the HTTP request, then the
106- ` secureEndpoint ` property on ` options ` _ will be set to ` true ` _ .
107-
108-
10965License
11066-------
11167
0 commit comments