You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. agent-base
  2. ==========
  3. ### Turn a function into an [`http.Agent`][http.Agent] instance
  4. [![Build Status](https://travis-ci.org/TooTallNate/node-agent-base.svg?branch=master)](https://travis-ci.org/TooTallNate/node-agent-base)
  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.
  8. #### Some subclasses:
  9. Here's some more interesting uses of `agent-base`.
  10. Send a pull request to list yours!
  11. * [`http-proxy-agent`][http-proxy-agent]: An HTTP(s) proxy `http.Agent` implementation for HTTP endpoints
  12. * [`https-proxy-agent`][https-proxy-agent]: An HTTP(s) proxy `http.Agent` implementation for HTTPS endpoints
  13. * [`pac-proxy-agent`][pac-proxy-agent]: A PAC file proxy `http.Agent` implementation for HTTP and HTTPS
  14. * [`socks-proxy-agent`][socks-proxy-agent]: A SOCKS (v4a) proxy `http.Agent` implementation for HTTP and HTTPS
  15. Installation
  16. ------------
  17. Install with `npm`:
  18. ``` bash
  19. $ npm install agent-base
  20. ```
  21. Example
  22. -------
  23. Here's a minimal example that creates a new `net.Socket` connection to the server
  24. for every HTTP request (i.e. the equivalent of `agent: false` option):
  25. ```js
  26. var net = require('net');
  27. var tls = require('tls');
  28. var url = require('url');
  29. var http = require('http');
  30. var agent = require('agent-base');
  31. var endpoint = 'http://nodejs.org/api/';
  32. var parsed = url.parse(endpoint);
  33. // This is the important part!
  34. parsed.agent = agent(function (req, opts) {
  35. var socket;
  36. // `secureEndpoint` is true when using the https module
  37. if (opts.secureEndpoint) {
  38. socket = tls.connect(opts);
  39. } else {
  40. socket = net.connect(opts);
  41. }
  42. return socket;
  43. });
  44. // Everything else works just like normal...
  45. http.get(parsed, function (res) {
  46. console.log('"response" event!', res.headers);
  47. res.pipe(process.stdout);
  48. });
  49. ```
  50. Returning a Promise or using an `async` function is also supported:
  51. ```js
  52. agent(async function (req, opts) {
  53. await sleep(1000);
  54. // etc…
  55. });
  56. ```
  57. Return another `http.Agent` instance to "pass through" the responsibility
  58. for that HTTP request to that agent:
  59. ```js
  60. agent(function (req, opts) {
  61. return opts.secureEndpoint ? https.globalAgent : http.globalAgent;
  62. });
  63. ```
  64. API
  65. ---
  66. ## Agent(Function callback[, Object options]) → [http.Agent][]
  67. Creates a base `http.Agent` that will execute the callback function `callback`
  68. for every HTTP request that it is used as the `agent` for. The callback function
  69. is responsible for creating a `stream.Duplex` instance of some kind that will be
  70. used as the underlying socket in the HTTP request.
  71. The `options` object accepts the following properties:
  72. * `timeout` - Number - Timeout for the `callback()` function in milliseconds. Defaults to Infinity (optional).
  73. The callback function should have the following signature:
  74. ### callback(http.ClientRequest req, Object options, Function cb) → undefined
  75. The ClientRequest `req` can be accessed to read request headers and
  76. and the path, etc. The `options` object contains the options passed
  77. to the `http.request()`/`https.request()` function call, and is formatted
  78. to be directly passed to `net.connect()`/`tls.connect()`, or however
  79. else you want a Socket to be created. Pass the created socket to
  80. the callback function `cb` once created, and the HTTP request will
  81. continue to proceed.
  82. If the `https` module is used to invoke the HTTP request, then the
  83. `secureEndpoint` property on `options` _will be set to `true`_.
  84. License
  85. -------
  86. (The MIT License)
  87. Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
  88. Permission is hereby granted, free of charge, to any person obtaining
  89. a copy of this software and associated documentation files (the
  90. 'Software'), to deal in the Software without restriction, including
  91. without limitation the rights to use, copy, modify, merge, publish,
  92. distribute, sublicense, and/or sell copies of the Software, and to
  93. permit persons to whom the Software is furnished to do so, subject to
  94. the following conditions:
  95. The above copyright notice and this permission notice shall be
  96. included in all copies or substantial portions of the Software.
  97. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  98. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  99. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  100. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  101. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  102. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  103. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  104. [http-proxy-agent]: https://github.com/TooTallNate/node-http-proxy-agent
  105. [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
  106. [pac-proxy-agent]: https://github.com/TooTallNate/node-pac-proxy-agent
  107. [socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
  108. [http.Agent]: https://nodejs.org/api/http.html#http_class_http_agent