Commit 8251296d authored by indexzero's avatar indexzero

[dist minor] Removed vendored pool. Changed all references of sys to util

parent 00014d62
......@@ -2,8 +2,7 @@
var path = require('path'),
fs = require('fs'),
eyes = require('eyes'),
sys = require('sys'),
util = require('util'),
argv = require('optimist').argv,
httpProxy = require('./../lib/node-http-proxy');
......@@ -21,7 +20,7 @@ var help = [
];
if (argv.h || argv.help || Object.keys(argv).length === 2) {
sys.puts(help.join('\n'));
util.puts(help.join('\n'));
process.exit(0);
}
......@@ -29,11 +28,6 @@ var location, config = {},
port = argv.port || 80,
target = argv.target;
//
// Check to see if we should silence the logs
//
config.silent = argv.silent || false;
//
// If we were passed a config, parse it
//
......@@ -42,11 +36,16 @@ if (argv.config) {
var data = fs.readFileSync(argv.config);
config = JSON.parse(data.toString());
} catch (ex) {
sys.puts('Error starting node-http-proxy: ' + ex);
util.puts('Error starting node-http-proxy: ' + ex);
process.exit(1);
}
}
//
// Check to see if we should silence the logs
//
config.silent = typeof argv.silent !== 'undefined' ? argv.silent : config.silent;
//
// If we were passed a target, parse the url string
//
......@@ -73,5 +72,5 @@ server.listen(port);
// Notify that the server is started
//
if (!config.silent) {
sys.puts('node-http-proxy server now listening on port: ' + port);
util.puts('node-http-proxy server now listening on port: ' + port);
}
{
"silent": true,
"silent": false,
"router": {
"localhost": "localhost:9000"
},
......
......@@ -24,7 +24,7 @@
*/
var sys = require('sys'),
var util = require('util'),
colors = require('colors')
http = require('http'),
httpProxy = require('./lib/node-http-proxy');
......@@ -37,24 +37,24 @@ var welcome = '\
# # # # ##### ##### ##### # # ## # \n\
# # # # # # # # # # # # # \n\
# # # # # # # # #### # # # \n';
sys.puts(welcome.rainbow.bold);
util.puts(welcome.rainbow.bold);
//
// Basic Http Proxy Server
//
httpProxy.createServer(9000, 'localhost').listen(8000);
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
//
// Http Proxy Server with Proxy Table
//
httpProxy.createServer({
router: {
'127.0.0.1': 'localhost:9000'
'localhost': 'localhost:9000'
}
}).listen(8001);
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with proxy table'.magenta.underline)
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with proxy table'.magenta.underline)
//
// Http Proxy Server with Latency
......@@ -64,7 +64,7 @@ httpProxy.createServer(function (req, res, proxy) {
proxy.proxyRequest(9000, 'localhost');
}, 200)
}).listen(8002);
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with latency'.magenta.underline);
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with latency'.magenta.underline);
//
//
......@@ -75,7 +75,7 @@ httpProxy.createServer(9000, 'localhost', {
host: 'localhost'
}
}).listen(8003);
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8003 '.yellow + 'with forward proxy'.magenta.underline)
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8003 '.yellow + 'with forward proxy'.magenta.underline)
//
// Http Server with proxyRequest Handler and Latency
......@@ -87,7 +87,7 @@ http.createServer(function (req, res) {
proxy.proxyRequest(9000, 'localhost');
}, 200);
}).listen(8004);
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8004 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8004 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
//
// Target Http Server
......@@ -97,15 +97,15 @@ http.createServer(function (req, res) {
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
//
// Target Http Forwarding Server
//
http.createServer(function (req, res) {
sys.puts('Receiving forward for: ' + req.url)
util.puts('Receiving forward for: ' + req.url)
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully forwarded to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9001);
sys.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9001 '.yellow);
util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9001 '.yellow);
......@@ -24,9 +24,8 @@
*/
var sys = require('sys'),
var util = require('util'),
http = require('http'),
eyes = require('eyes'),
events = require('events'),
pool = require('pool'),
ProxyTable = require('./proxy-table').ProxyTable,
......@@ -58,7 +57,7 @@ exports.createServer = function () {
router = options.router;
forward = options.forward;
silent = options.silent || true;
silent = typeof options.silent !== 'undefined' ? options.silent : true;
if (router) {
proxyTable = new ProxyTable(router, options.silent);
......@@ -70,7 +69,7 @@ exports.createServer = function () {
var server = http.createServer(function (req, res) {
function log (message) {
if (!silent) {
sys.log(message);
util.log(message);
}
}
......@@ -78,9 +77,9 @@ exports.createServer = function () {
log('Incoming HTTP request to: ' + req.headers.host + req.url);
if (forward) {
var forward = new HttpProxy(req, res);
var forwardProxy = new HttpProxy(req, res);
log('Forwarding HTTP request to: ' + forward.host + ':' + forward.port);
forward.forwardRequest(forward.port, forward.host);
forwardProxy.forwardRequest(forward.port, forward.host);
}
// If we were passed a callback to process the request
......
......@@ -30,7 +30,7 @@ var util = require('util'),
var ProxyTable = function (router, silent) {
events.EventEmitter.call(this);
this.silent = silent || true;
this.silent = typeof silent !== 'undefined' ? silent : true;
if (typeof router === 'object') {
// If we are passed an object literal setup
......
......@@ -20,7 +20,7 @@
"vows": ">= 0.5.2"
},
"main": "./lib/node-http-proxy",
"bin": { "http-proxy": "./bin/node-http-proxy" },
"bin": { "node-http-proxy": "./bin/node-http-proxy" },
"scripts": { "test": "vows test/*-test.js --spec" },
"engines": { "node": ">= 0.3.0" }
}
\ No newline at end of file
......@@ -7,7 +7,7 @@
var fs = require('fs'),
vows = require('vows'),
sys = require('sys'),
util = require('util'),
path = require('path'),
request = require('request'),
assert = require('assert'),
......
......@@ -25,7 +25,7 @@
*/
var vows = require('vows'),
sys = require('sys'),
util = require('util'),
request = require('request'),
assert = require('assert'),
helpers = require('./helpers'),
......
......@@ -7,7 +7,7 @@
var fs = require('fs'),
vows = require('vows'),
sys = require('sys'),
util = require('util'),
path = require('path'),
request = require('request'),
assert = require('assert'),
......
# Pool -- Simple HTTP client pooling
## Install
<pre>
npm install pool
</pre>
## Super simple to use
Pool has two core usage scenarios: creating a pool and creating a set of pools. Creating a pool is easy:
<pre>
var pool = require('pool'),
sys = require('sys'),
local = pool.createPool('80', 'localhost');
client = local.request('GET', '/', function (request) {
// You can work with the request here just as you would as if it
// was returned from http.createClient
request.on('end', function () {
sys.puts('Request ended');
});
});
</pre>
Creating a set of pools can be accomplished using a PoolManager:
<pre>
var pool = require('pool'),
manager = pool.createPoolManager(),
local = manager.getPool('80', 'localhost');
client = local.request('GET', '/', function (request) {
// You can work with the request here just as you would as if it
// was returned from http.createClient
request.on('end', function () {
sys.puts('Request ended');
});
});
</pre>
\ No newline at end of file
var sys = require('sys')
, http = require('http')
, events = require('events')
;
function Pool (port, host, https, credentials) {
this.port = port;
this.host = host;
this.https = https;
this.credentials = credentials;
this.clients = [];
this.pending = [];
this.minClients = 0;
this.maxClients = 8;
};
sys.inherits(Pool, events.EventEmitter);
Pool.prototype.getClient = function (cb) {
for (var i=0;i<this.clients.length;i+=1) {
if (!this.clients[i].busy) {
// Check if the client closed unexpectedly
if (this.clients[i].readyState === 'closed') {
delete this.clients[i];
this.clients[i] = http.createClient(this.port, this.host, this.https, this.credentials);
this.clients[i].busy = false;
}
if (this.clients.length > this.maxClients) {
this.clients[i].end();
this.clients.splice(i, 1);
i-=1;
} else {
return cb(this.clients[i]);
}
}
}
if (this.clients.length >= this.maxClients) {
this.pending.push(cb);
} else {
var client = http.createClient(this.port, this.host, this.https, this.credentials);
this.clients.push(client);
cb(client);
}
};
Pool.prototype.request = function () {
// Argument parsing. This gets a little dicey with the
// differences in defaults
var method, url, headers, callback, args;
var self = this;
args = Array.prototype.slice.call(arguments);
if (typeof args[args.length - 1] === 'function') {
callback = args.pop();
}
if (args[0]) method = args[0];
if (args[1]) url = args[1];
if (args[2]) headers = args[2];
if (!headers) headers = {};
if (!headers.Connection) headers.Connection = 'keep-alive';
self.getClient(function (client) {
var errorListener = function (error) {
client.removeListener("error", errorListener);
// Remove the client from the available clients since it has errored
self.clients.splice(self.clients.indexOf(client), 1);
self.emit("error", error);
request.emit("error", error);
};
var request = client.request(method, url, headers);
client.on("error", errorListener);
request.on("response", function (response) {
response.on("end", function () {
client.removeListener("error", errorListener);
client.busy = false;
self.onFree(client);
})
})
client.busy = true;
callback(request);
});
};
Pool.prototype.onFree = function (client) {
if (this.pending.length > 0) this.pending.shift()(client);
};
Pool.prototype.getFree = function () {
return this.clients.filter(function (client) { return !client.busy }).length;
};
Pool.prototype.getBusy = function () {
return this.clients.filter(function (client) { return client.busy }).length;
};
Pool.prototype.setMinClients = function (num) {
this.minClients = num;
if (this.clients.length < num) {
for (var i=this.clients.length;i<num;i+=1) {
var client = http.createClient(this.port, this.host, this.https, this.credentials);
this.clients.push(client);
this.emit('free', client);
}
}
};
Pool.prototype.setMaxClients = function (num) {
this.maxClients = num;
};
Pool.prototype.end = function () {
this.clients.forEach(function (c) {c.end()});
};
function PoolManager () {
this.pools = {};
this.pending = [];
this.minClients = 0;
this.maxClients = 8;
};
PoolManager.prototype.setMaxClients = function (num) {
this.maxClients = num;
for (i in this.pools) {
this.pools[i].setMaxClients(num);
}
};
PoolManager.prototype.setMinClients = function (num) {
this.minClients = num;
for (i in this.pools) {
this.pools[i].setMinClients(num);
}
};
PoolManager.prototype.getPool = function (port, host, https, credentials) {
var k = (port+host+https+credentials);
if (!this.pools[k]) {
this.pools[k] = exports.createPool(port, host, https, credentials);
this.pools[k].setMinClients(this.minClients);
this.pools[k].setMaxClients(this.maxClients);
}
return this.pools[k];
};
exports.createPool = function (port, host, https, credentials) {
return new Pool(port, host, https, credentials);
};
exports.createPoolManager = function () {
return new PoolManager();
};
{ "name" : "pool"
, "description" : "HTTP client pools."
, "tags" : ["http", "simple", "util", "utility"]
, "version" : "0.4.1"
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>"
, "repository" :
{ "type" : "git"
, "url" : "http://github.com/mikeal/node-utils.git"
}
, "bugs" :
{ "web" : "http://github.com/mikeal/node-utils/issues" }
, "engines" : ["node >=0.1.90"]
, "main" : "./main"
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment