agent-base
Advanced tools
Comparing version 4.2.0 to 4.2.1
12
index.js
@@ -97,2 +97,3 @@ 'use strict'; | ||
const timeoutMs = this.timeout; | ||
const freeSocket = this.freeSocket; | ||
@@ -137,6 +138,10 @@ function onerror(err) { | ||
} else if (socket) { | ||
function onfree() { | ||
freeSocket(socket, opts); | ||
} | ||
socket.on('free', onfree); | ||
req.onSocket(socket); | ||
} else { | ||
const err = new Error( | ||
`no Duplex stream was returned to agent-base for \`${req.method} ${req.path}\`` | ||
'no Duplex stream was returned to agent-base for `' + req.method + ' ' + req.path + '`' | ||
); | ||
@@ -163,1 +168,6 @@ onerror(err); | ||
}; | ||
Agent.prototype.freeSocket = function freeSocket(socket, opts) { | ||
// TODO reuse sockets | ||
socket.destroy(); | ||
}; |
{ | ||
"name": "agent-base", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"description": "Turn a function into an `http.Agent` instance", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -401,2 +401,26 @@ /** | ||
it('should free sockets after use', function(done) { | ||
var agent = new Agent(function(req, opts, fn) { | ||
var socket = net.connect(opts); | ||
fn(null, socket); | ||
}); | ||
// add HTTP server "request" listener | ||
var gotReq = false; | ||
server.once('request', function(req, res) { | ||
gotReq = true; | ||
res.end(); | ||
}); | ||
var info = url.parse('http://127.0.0.1:' + port + '/foo'); | ||
info.agent = agent; | ||
http.get(info, function(res) { | ||
res.socket.emit('free'); | ||
assert.equal(true, res.socket.destroyed); | ||
assert(gotReq); | ||
done(); | ||
}); | ||
}); | ||
describe('PassthroughAgent', function() { | ||
@@ -403,0 +427,0 @@ it('should pass through to `http.globalAgent`', function(done) { |
35006
805
3