/** * Module dependencies. */ var net = require('net'); var inherits = require('util').inherits; var EventEmitter = require('events').EventEmitter; /** * Module exports. */ module.exports = Agent; /** * Base HTTP "Agent" class. Emulates the node-core `http.Agent` class, but * implemented in a way that can be easily extended for additional functionality. * * This base implementation does no socket pooling, and opens * a new connection for every HTTP request. * * It behaves more-or-less like `agent: false`. * * @api public */ function Agent () { if (!(this instanceof Agent)) return new Agent(); EventEmitter.call(this); } inherits(Agent, EventEmitter); /** * Called by node-core's "_http_client.js" module when creating a new HTTP request * with this Agent instance. * * @api public */ Agent.prototype.addRequest = function (req, host, port, localAddress) { // hint to use "Connection: close" req.shouldKeepAlive = false; // create the `net.Socket` instance var opts = {}; opts.port = port; opts.host = host; if (localAddress) opts.localAddress = localAddress; this.createConnection(opts, function (err, socket) { if (err) { req.emit('error', err); } else { req.onSocket(socket); } }); }; /** * Creates and returns a `net.Socket` instance to use for an HTTP request. * * @api public */ Agent.prototype.createConnection = function (opts, fn) { var socket = net.connect(opts); fn(null, socket); return socket; };