agent-base
Turn a function into an `http.Agent` instance
Last updated 8 months ago by tootallnate .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install agent-base 
SYNC missed versions from official npm registry.

agent-base

Turn a function into an http.Agent instance

This module is a thin wrapper around the base http.Agent class.

It provides an abstract class that must define a connect() function, which is responsible for creating the underlying socket that the HTTP client requests will use.

The connect() function may return an arbitrary Duplex stream, or another http.Agent instance to delegate the request to, and may be asynchronous (by defining an async function).

Instances of this agent can be used with the http and https modules. To differentiate, the options parameter in the connect() function includes a secureEndpoint property, which can be checked to determine what type of socket should be returned.

Some subclasses:

Here are some more interesting uses of agent-base. Send a pull request to list yours!

Example

Here's a minimal example that creates a new net.Socket or tls.Socket based on the secureEndpoint property. This agent can be used with both the http and https modules.

import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import { Agent } from 'agent-base';

class MyAgent extends Agent {
  connect(req, opts) {
    // `secureEndpoint` is true when using the "https" module
    if (opts.secureEndpoint) {
      return tls.connect(opts);
    } else {
      return net.connect(opts);
    }
  }
});

// Keep alive enabled means that `connect()` will only be
// invoked when a new connection needs to be created
const agent = new MyAgent({ keepAlive: true });

// Pass the `agent` option when creating the HTTP request
http.get('http://nodejs.org/api/', { agent }, (res) => {
  console.log('"response" event!', res.headers);
  res.pipe(process.stdout);
});

Current Tags

  • 7.1.1                                ...           latest (8 months ago)

28 Versions

  • 7.1.1                                ...           8 months ago
  • 7.1.0                                ...           a year ago
  • 7.0.2                                ...           a year ago
  • 7.0.1                                ...           2 years ago
  • 7.0.0                                ...           2 years ago
  • 6.0.2                                ...           4 years ago
  • 6.0.1                                ...           4 years ago
  • 6.0.0                                ...           5 years ago
  • 5.1.1                                ...           5 years ago
  • 5.1.0                                ...           5 years ago
  • 5.0.0                                ...           5 years ago
  • 4.3.0                                ...           5 years ago
  • 4.2.1                                ...           6 years ago
  • 4.2.0                                ...           7 years ago
  • 4.1.2                                ...           7 years ago
  • 4.1.1                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.0.1                                ...           7 years ago
  • 4.0.0                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 2.1.1                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.0.2                                ...           9 years ago
  • 1.0.1                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
  • 0.0.1                                ...           11 years ago
Maintainers (1)
Downloads
Total 2
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (11)

© 2010 - cnpmjs.org x YWFE | Home | YWFE