Socket
Socket
Sign inDemoInstall

agent-base

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

agent-base - npm Package Compare versions

Comparing version 2.1.1 to 3.0.0

index.js

12

History.md
3.0.0 / 2017-06-02
==================
* drop support for Node.js v0.8 and v0.10
* add support for async, Promises, and direct return
* add a couple `options` test cases
* implement a `"timeout"` option
* rename main file to `index.js`
* test Node 8 on Travis
2.1.1 / 2017-05-30
==================
* Revert [fe2162e0ba18123f5b301cba4de1e9dd74e437cd](https://github.com/TooTallNate/node-agent-base/commit/fe2162e0ba18123f5b301cba4de1e9dd74e437cd) and [270bdc92eb8e3bd0444d1e5266e8e9390aeb3095](https://github.com/TooTallNate/node-agent-base/commit/270bdc92eb8e3bd0444d1e5266e8e9390aeb3095) (fixes #7)
* Revert [`fe2162e`](https://github.com/TooTallNate/node-agent-base/commit/fe2162e0ba18123f5b301cba4de1e9dd74e437cd) and [`270bdc9`](https://github.com/TooTallNate/node-agent-base/commit/270bdc92eb8e3bd0444d1e5266e8e9390aeb3095) (fixes #7)

@@ -7,0 +17,0 @@ 2.1.0 / 2017-05-26

10

package.json
{
"name": "agent-base",
"version": "2.1.1",
"version": "3.0.0",
"description": "Turn a function into an `http.Agent` instance",
"main": "agent.js",
"main": "./index.js",
"scripts": {

@@ -26,9 +26,13 @@ "test": "mocha --reporter spec"

"devDependencies": {
"mocha": "2",
"mocha": "3",
"ws": "0.8.0"
},
"dependencies": {
"es6-promisify": "^5.0.0",
"extend": "~3.0.0",
"semver": "~5.0.1"
},
"engines": {
"node": ">= 0.12.0"
}
}
agent-base
==========
### Turn a function into an `http.Agent` instance
### Turn a function into an [`http.Agent`][http.Agent] instance
[![Build Status](https://travis-ci.org/TooTallNate/node-agent-base.svg?branch=master)](https://travis-ci.org/TooTallNate/node-agent-base)

@@ -45,6 +45,6 @@

var endpoint = 'http://nodejs.org/api/';
var opts = url.parse(endpoint);
var parsed = url.parse(endpoint);
// This is the important part!
opts.agent = agent(function (req, opts, fn) {
parsed.agent = agent(function (req, opts, fn) {
var socket;

@@ -61,3 +61,3 @@ // `secureEndpoint` is true when using the https module

// Everything else works just like normal...
http.get(opts, function (res) {
http.get(parsed, function (res) {
console.log('"response" event!', res.headers);

@@ -71,3 +71,3 @@ res.pipe(process.stdout);

## Agent(Function callback) → http.Agent
## Agent(Function callback[, Object options]) → [http.Agent][]

@@ -79,2 +79,6 @@ Creates a base `http.Agent` that will execute the callback function `callback`

The `options` object accepts the following properties:
* `timeout` - Number - Timeout for the `callback()` function in milliseconds. Defaults to Infinity (optional).
The callback function should have the following signature:

@@ -93,3 +97,3 @@

If the `https` module is used to invoke the HTTP request, then the
`secureEndpoint` property on `options` will be set to `true`.
`secureEndpoint` property on `options` _will be set to `true`_.

@@ -127,1 +131,2 @@

[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
[http.Agent]: https://nodejs.org/api/http.html#http_class_http_agent

@@ -1,2 +0,1 @@

/**

@@ -19,6 +18,6 @@ * Module dependencies.

describe('Agent', function () {
describe('subclass', function () {
it('should be subclassable', function (done) {
function MyAgent () {
describe('Agent', function() {
describe('subclass', function() {
it('should be subclassable', function(done) {
function MyAgent() {
Agent.call(this);

@@ -28,3 +27,3 @@ }

MyAgent.prototype.callback = function (req, opts, fn) {
MyAgent.prototype.callback = function(req, opts, fn) {
assert.equal(req.path, '/foo');

@@ -37,8 +36,20 @@ assert.equal(req.getHeader('host'), '127.0.0.1:1234');

var info = url.parse('https://127.0.0.1:1234/foo');
info.agent = new MyAgent;
info.agent = new MyAgent();
https.get(info);
});
});
describe('"error" event', function () {
it('should be invoked on `http.ClientRequest` instance if `callback()` has not been defined', function (done) {
describe('options', function() {
it('should support an options Object as first argument', function() {
var agent = new Agent({ timeout: 1000 });
assert.equal(1000, agent.timeout);
});
it('should support an options Object as second argument', function() {
var agent = new Agent(function() {}, { timeout: 1000 });
assert.equal(1000, agent.timeout);
});
});
describe('"error" event', function() {
it('should be invoked on `http.ClientRequest` instance if `callback()` has not been defined', function(
done
) {
var agent = new Agent();

@@ -48,9 +59,14 @@ var info = url.parse('http://127.0.0.1/foo');

var req = http.get(info);
req.on('error', function (err) {
assert.equal('"agent-base" has no default implementation, you must subclass and override `callback()`', err.message);
req.on('error', function(err) {
assert.equal(
'"agent-base" has no default implementation, you must subclass and override `callback()`',
err.message
);
done();
});
});
it('should be invoked on `http.ClientRequest` instance if Error passed to callback function on the first tick', function (done) {
var agent = new Agent(function (req, opts, fn) {
it('should be invoked on `http.ClientRequest` instance if Error passed to callback function on the first tick', function(
done
) {
var agent = new Agent(function(req, opts, fn) {
fn(new Error('is this caught?'));

@@ -61,3 +77,3 @@ });

var req = http.get(info);
req.on('error', function (err) {
req.on('error', function(err) {
assert.equal('is this caught?', err.message);

@@ -67,5 +83,7 @@ done();

});
it('should be invoked on `http.ClientRequest` instance if Error passed to callback function after the first tick', function (done) {
var agent = new Agent(function (req, opts, fn) {
setTimeout(function () {
it('should be invoked on `http.ClientRequest` instance if Error passed to callback function after the first tick', function(
done
) {
var agent = new Agent(function(req, opts, fn) {
setTimeout(function() {
fn(new Error('is this caught?'));

@@ -77,3 +95,3 @@ }, 10);

var req = http.get(info);
req.on('error', function (err) {
req.on('error', function(err) {
assert.equal('is this caught?', err.message);

@@ -84,4 +102,4 @@ done();

});
describe('artificial "streams"', function () {
it('should send a GET request', function (done) {
describe('artificial "streams"', function() {
it('should send a GET request', function(done) {
var stream = new events.EventEmitter();

@@ -92,3 +110,3 @@

stream.write = function (str) {
stream.write = function(str) {
assert(0 == str.indexOf('GET / HTTP/1.1'));

@@ -99,4 +117,3 @@ done();

// needed for `http` module in Node.js 4
stream.cork = function () {
};
stream.cork = function() {};

@@ -108,3 +125,3 @@ var opts = {

port: 80,
agent: new Agent(function (req, opts, fn) {
agent: new Agent(function(req, opts, fn) {
fn(null, stream);

@@ -116,3 +133,3 @@ })

});
it('should receive a GET response', function (done) {
it('should receive a GET response', function(done) {
var stream = new events.EventEmitter();

@@ -124,7 +141,7 @@ var opts = {

port: 80,
agent: new Agent(function (req, opts, fn) {
agent: new Agent(function(req, opts, fn) {
fn(null, stream);
})
};
var req = http.request(opts, function (res) {
var req = http.request(opts, function(res) {
assert.equal('0.9', res.httpVersion);

@@ -135,11 +152,13 @@ assert.equal(111, res.statusCode);

});
req.end();
// have to nextTick() since `http.ClientRequest` doesn't *actually*
// attach the listeners to the "stream" until the next tick :\
process.nextTick(function () {
var buf = new Buffer('HTTP/0.9 111\r\n' +
'Foo: bar\r\n' +
'Set-Cookie: 1\r\n' +
'Set-Cookie: 2\r\n\r\n');
// have to wait for the "socket" event since `http.ClientRequest`
// doesn't *actually* attach the listeners to the "stream" until
// this happens
req.once('socket', function() {
var buf = new Buffer(
'HTTP/0.9 111\r\n' +
'Foo: bar\r\n' +
'Set-Cookie: 1\r\n' +
'Set-Cookie: 2\r\n\r\n'
);
if ('function' == typeof stream.ondata) {

@@ -153,2 +172,4 @@ // node <= v0.11.3

});
req.end();
});

@@ -158,3 +179,3 @@ });

describe('"http" module', function () {
describe('"http" module', function() {
var server;

@@ -164,5 +185,5 @@ var port;

// setup test HTTP server
before(function (done) {
before(function(done) {
server = http.createServer();
server.listen(0, function () {
server.listen(0, function() {
port = server.address().port;

@@ -174,4 +195,4 @@ done();

// shut down test HTTP server
after(function (done) {
server.once('close', function () {
after(function(done) {
server.once('close', function() {
done();

@@ -182,5 +203,5 @@ });

it('should work for basic HTTP requests', function (done) {
it('should work for basic HTTP requests', function(done) {
var called = false;
var agent = new Agent(function (req, opts, fn) {
var agent = new Agent(function(req, opts, fn) {
called = true;

@@ -193,3 +214,3 @@ var socket = net.connect(opts);

var gotReq = false;
server.once('request', function (req, res) {
server.once('request', function(req, res) {
gotReq = true;

@@ -203,3 +224,3 @@ res.setHeader('X-Foo', 'bar');

info.agent = agent;
http.get(info, function (res) {
http.get(info, function(res) {
assert.equal('bar', res.headers['x-foo']);

@@ -213,6 +234,62 @@ assert.equal('/foo', res.headers['x-url']);

it('should set the `Connection: close` response header', function (done) {
it('should support direct return in `connect()`', function(done) {
var called = false;
var agent = new Agent(function (req, opts, fn) {
var agent = new Agent(function(req, opts) {
called = true;
return net.connect(opts);
});
// add HTTP server "request" listener
var gotReq = false;
server.once('request', function(req, res) {
gotReq = true;
res.setHeader('X-Foo', 'bar');
res.setHeader('X-Url', req.url);
res.end();
});
var info = url.parse('http://127.0.0.1:' + port + '/foo');
info.agent = agent;
http.get(info, function(res) {
assert.equal('bar', res.headers['x-foo']);
assert.equal('/foo', res.headers['x-url']);
assert(gotReq);
assert(called);
done();
});
});
it('should support returning a Promise in `connect()`', function(done) {
var called = false;
var agent = new Agent(function(req, opts) {
return new Promise(function(resolve, reject) {
called = true;
resolve(net.connect(opts));
});
});
// add HTTP server "request" listener
var gotReq = false;
server.once('request', function(req, res) {
gotReq = true;
res.setHeader('X-Foo', 'bar');
res.setHeader('X-Url', req.url);
res.end();
});
var info = url.parse('http://127.0.0.1:' + port + '/foo');
info.agent = agent;
http.get(info, function(res) {
assert.equal('bar', res.headers['x-foo']);
assert.equal('/foo', res.headers['x-url']);
assert(gotReq);
assert(called);
done();
});
});
it('should set the `Connection: close` response header', function(done) {
var called = false;
var agent = new Agent(function(req, opts, fn) {
called = true;
var socket = net.connect(opts);

@@ -224,3 +301,3 @@ fn(null, socket);

var gotReq = false;
server.once('request', function (req, res) {
server.once('request', function(req, res) {
gotReq = true;

@@ -234,3 +311,3 @@ res.setHeader('X-Url', req.url);

info.agent = agent;
http.get(info, function (res) {
http.get(info, function(res) {
assert.equal('/bar', res.headers['x-url']);

@@ -244,4 +321,4 @@ assert.equal('close', res.headers.connection);

it('should pass through options from `http.request()`', function (done) {
var agent = new Agent(function (req, opts, fn) {
it('should pass through options from `http.request()`', function(done) {
var agent = new Agent(function(req, opts, fn) {
assert.equal('google.com', opts.host);

@@ -259,4 +336,4 @@ assert.equal('bar', opts.foo);

it('should default to port 80', function (done) {
var agent = new Agent(function (req, opts, fn) {
it('should default to port 80', function(done) {
var agent = new Agent(function(req, opts, fn) {
assert.equal(80, opts.port);

@@ -274,5 +351,28 @@ done();

});
it('should support the "timeout" option', function(done) {
// ensure we timeout after the "error" event had a chance to trigger
this.timeout(1000);
this.slow(800);
var agent = new Agent(
function(req, opts, fn) {
// this function will time out
},
{ timeout: 100 }
);
var opts = url.parse('http://nodejs.org');
opts.agent = agent;
var req = http.get(opts);
req.once('error', function(err) {
assert.equal('ETIMEOUT', err.code);
req.abort();
done();
});
});
});
describe('"https" module', function () {
describe('"https" module', function() {
var server;

@@ -282,3 +382,3 @@ var port;

// setup test HTTPS server
before(function (done) {
before(function(done) {
var options = {

@@ -289,3 +389,3 @@ key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),

server = https.createServer(options);
server.listen(0, function () {
server.listen(0, function() {
port = server.address().port;

@@ -297,4 +397,4 @@ done();

// shut down test HTTP server
after(function (done) {
server.once('close', function () {
after(function(done) {
server.once('close', function() {
done();

@@ -305,5 +405,5 @@ });

it('should work for basic HTTPS requests', function (done) {
it('should work for basic HTTPS requests', function(done) {
var called = false;
var agent = new Agent(function (req, opts, fn) {
var agent = new Agent(function(req, opts, fn) {
called = true;

@@ -317,3 +417,3 @@ assert(opts.secureEndpoint);

var gotReq = false;
server.once('request', function (req, res) {
server.once('request', function(req, res) {
gotReq = true;

@@ -328,3 +428,3 @@ res.setHeader('X-Foo', 'bar');

info.rejectUnauthorized = false;
https.get(info, function (res) {
https.get(info, function(res) {
assert.equal('bar', res.headers['x-foo']);

@@ -338,4 +438,4 @@ assert.equal('/foo', res.headers['x-url']);

it('should pass through options from `https.request()`', function (done) {
var agent = new Agent(function (req, opts, fn) {
it('should pass through options from `https.request()`', function(done) {
var agent = new Agent(function(req, opts, fn) {
assert.equal('google.com', opts.host);

@@ -353,4 +453,4 @@ assert.equal('bar', opts.foo);

it('should default to port 443', function (done) {
var agent = new Agent(function (req, opts, fn) {
it('should default to port 443', function(done) {
var agent = new Agent(function(req, opts, fn) {
assert.equal(true, opts.secureEndpoint);

@@ -373,3 +473,3 @@ assert.equal(false, opts.rejectUnauthorized);

describe('"ws" server', function () {
describe('"ws" server', function() {
var wss;

@@ -380,6 +480,6 @@ var server;

// setup test HTTP server
before(function (done) {
server = http.createServer()
before(function(done) {
server = http.createServer();
wss = new WebSocket.Server({ server: server });
server.listen(0, function () {
server.listen(0, function() {
port = server.address().port;

@@ -391,4 +491,4 @@ done();

// shut down test HTTP server
after(function (done) {
server.once('close', function () {
after(function(done) {
server.once('close', function() {
done();

@@ -399,5 +499,5 @@ });

it('should work for basic WebSocket connections', function (done) {
it('should work for basic WebSocket connections', function(done) {
function onconnection(ws) {
ws.on('message', function (data) {
ws.on('message', function(data) {
assert.equal('ping', data);

@@ -409,3 +509,3 @@ ws.send('pong');

var agent = new Agent(function (req, opts, fn) {
var agent = new Agent(function(req, opts, fn) {
var socket = net.connect(opts);

@@ -419,7 +519,7 @@ fn(null, socket);

client.on('open', function () {
client.on('open', function() {
client.send('ping');
});
client.on('message', function (data) {
client.on('message', function(data) {
assert.equal('pong', data);

@@ -431,6 +531,5 @@ client.close();

});
});
describe('"wss" server', function () {
describe('"wss" server', function() {
var wss;

@@ -441,3 +540,3 @@ var server;

// setup test HTTP server
before(function (done) {
before(function(done) {
var options = {

@@ -449,3 +548,3 @@ key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),

wss = new WebSocket.Server({ server: server });
server.listen(0, function () {
server.listen(0, function() {
port = server.address().port;

@@ -457,4 +556,4 @@ done();

// shut down test HTTP server
after(function (done) {
server.once('close', function () {
after(function(done) {
server.once('close', function() {
done();

@@ -465,5 +564,5 @@ });

it('should work for secure WebSocket connections', function (done) {
it('should work for secure WebSocket connections', function(done) {
function onconnection(ws) {
ws.on('message', function (data) {
ws.on('message', function(data) {
assert.equal('ping', data);

@@ -475,3 +574,3 @@ ws.send('pong');

var agent = new Agent(function (req, opts, fn) {
var agent = new Agent(function(req, opts, fn) {
var socket = tls.connect(opts);

@@ -486,7 +585,7 @@ fn(null, socket);

client.on('open', function () {
client.on('open', function() {
client.send('ping');
});
client.on('message', function (data) {
client.on('message', function(data) {
assert.equal('pong', data);

@@ -498,3 +597,2 @@ client.close();

});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc