Skip to content

Commit

Permalink
[dist] Complete JSHint compliance except for too many var statements
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jul 22, 2012
1 parent 46c184c commit 36226da
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 156 deletions.
81 changes: 40 additions & 41 deletions lib/node-http-proxy.js
Expand Up @@ -79,9 +79,9 @@ exports.createServer = function () {
case 'number': port = arg; break;
case 'object': options = arg || {}; break;
case 'function': callback = arg; handlers.push(callback); break;
};
}
});

//
// Helper function to create intelligent error message(s)
// for the very liberal arguments parsing performed by
Expand All @@ -90,36 +90,35 @@ exports.createServer = function () {
function validArguments() {
var conditions = {
'port and host': function () {
return port && host;
return port && host;
},
'options.target or options.router': function () {
return options && (options.router ||
return options && (options.router ||
(options.target && options.target.host && options.target.port));
},
'or proxy handlers': function () {
return handlers && handlers.length;
}
}
};

var missing = Object.keys(conditions).filter(function (name) {
return !conditions[name]();
});

if (missing.length === 3) {
message = 'Cannot proxy without ' + missing.join(', ');
return false;
}

return true;
}
}

if (!validArguments()) {
//
// If `host`, `port` and `options` are all not passed (with valid
// If `host`, `port` and `options` are all not passed (with valid
// options) then this server is improperly configured.
//
throw new Error(message);
return;
}

//
Expand All @@ -130,7 +129,7 @@ exports.createServer = function () {
options.target = options.target || {};
options.target.port = options.target.port || port;
options.target.host = options.target.host || host;

if (options.target && options.target.host && options.target.port) {
//
// If an explicit `host` and `port` combination has been passed
Expand All @@ -148,31 +147,31 @@ exports.createServer = function () {
// we have to assume that this is a "go-anywhere" Proxy (i.e. a `RoutingProxy`).
//
proxy = new RoutingProxy(options);

if (options.router) {
//
// If a routing table has been supplied than we assume
// If a routing table has been supplied than we assume
// the user intends us to add the "proxy" middleware layer
// for them
// for them
//
handlers.push(function (req, res) {
proxy.proxyRequest(req, res);
});

proxy.on('routes', function (routes) {
server.emit('routes', routes);
});
}
}
}

//
// Create the `http[s].Server` instance which will use
// an instance of `httpProxy.HttpProxy`.
//
handler = handlers.length > 1
handler = handlers.length > 1
? exports.stack(handlers, proxy)
: function (req, res) { handlers[0](req, res, proxy) };

server = options.https
? https.createServer(options.https, handler)
: http.createServer(handler);
Expand All @@ -184,8 +183,8 @@ exports.createServer = function () {
if (!callback) {
//
// If an explicit callback has not been supplied then
// automagically proxy the request using the `HttpProxy`
// instance we have created.
// automagically proxy the request using the `HttpProxy`
// instance we have created.
//
server.on('upgrade', function (req, socket, head) {
proxy.proxyWebSocketRequest(req, socket, head);
Expand Down Expand Up @@ -222,7 +221,7 @@ exports.createServer = function () {
//
exports.buffer = function (obj) {
var events = [],
onData,
onData,
onEnd;

obj.on('data', onData = function (data, encoding) {
Expand All @@ -240,11 +239,11 @@ exports.buffer = function (obj) {
},
destroy: function () {
this.end();
this.resume = function () {
console.error("Cannot resume buffer after destroying it.");
};
onData = onEnd = events = obj = null;
this.resume = function () {
console.error("Cannot resume buffer after destroying it.");
};

onData = onEnd = events = obj = null;
},
resume: function () {
this.end();
Expand Down Expand Up @@ -278,10 +277,10 @@ exports.setMaxSockets = function (value) {
//
// ### function stack (middlewares, proxy)
// #### @middlewares {Array} Array of functions to stack.
// #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
// #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
// Iteratively build up a single handler to the `http.Server`
// `request` event (i.e. `function (req, res)`) by wrapping
// each middleware `layer` into a `child` middleware which
// each middleware `layer` into a `child` middleware which
// is in invoked by the parent (i.e. predecessor in the Array).
//
// adapted from https://github.com/creationix/stack
Expand All @@ -295,17 +294,17 @@ exports.stack = function stack (middlewares, proxy) {
if (err) {
if (res._headerSent) {
res.destroy();
}
}
else {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Internal Server Error');
}

console.error('Error in middleware(s): %s', err.stack);
return;
}

if (child) {
child(req, res);
}
Expand Down Expand Up @@ -344,29 +343,29 @@ exports._getAgent = function _getAgent (options) {
if (!options || !options.host) {
throw new Error('`options.host` is required to create an Agent.');
}

if (!options.port) {
options.port = options.https ? 443 : 80;
}

var Agent = options.https ? https.Agent : http.Agent,
agent;

agent = new Agent({
host: options.host,
agent = new Agent({
host: options.host,
port: options.port
});

agent.maxSockets = options.maxSockets || maxSockets;

return agent;
}
};

//
// ### function _getProtocol (options)
// #### @options {Object} Options for the proxy target.
// Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
// based on the `options` supplied.
// Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
// based on the `options` supplied.
//
exports._getProtocol = function _getProtocol (options) {
return options.https ? https : http;
Expand All @@ -382,7 +381,7 @@ exports._getProtocol = function _getProtocol (options) {
//
exports._getBase = function _getBase (options) {
var result = function () {};

if (options.https && typeof options.https === 'object') {
['ca', 'cert', 'key'].forEach(function (key) {
if (options.https[key]) {
Expand Down
18 changes: 10 additions & 8 deletions lib/node-http-proxy/http-proxy.js
Expand Up @@ -222,9 +222,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// origin of the host header to the target URL! Please
// don't revert this without documenting it!
//
if(this.changeOrigin)
if (this.changeOrigin) {
outgoing.headers.host = this.target.host + ':' + this.target.port;

}

//
// Open new HTTP request to internal resource with will act
// as a reverse proxy pass
Expand Down Expand Up @@ -287,12 +288,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// already been called and the 'error' event listener
// removed.
//
var ended = false
var ended = false;
response.on('close', function () {
if(!ended) response.emit('end')
})
if (!ended) { response.emit('end') }
});

response.on('end', function () {
ended = true
ended = true;
if (!errState) {
reverseProxy.removeListener('error', proxyError);

Expand Down Expand Up @@ -566,11 +568,11 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
proxySocket.end();
detach();
});
};
}

function getPort (port) {
port = port || 80;
return port - 80 === 0 ? '' : ':' + port
return port - 80 === 0 ? '' : ':' + port;
}

//
Expand Down
2 changes: 1 addition & 1 deletion lib/node-http-proxy/proxy-table.js
Expand Up @@ -165,7 +165,7 @@ ProxyTable.prototype.getProxyLocation = function (req) {
}

var target = req.headers.host.split(':')[0];
if (this.hostnameOnly == true) {
if (this.hostnameOnly === true) {
if (this.router.hasOwnProperty(target)) {
var location = this.router[target].split(':'),
host = location[0],
Expand Down
12 changes: 8 additions & 4 deletions lib/node-http-proxy/routing-proxy.js
Expand Up @@ -167,6 +167,8 @@ RoutingProxy.prototype.close = function () {
//
RoutingProxy.prototype.proxyRequest = function (req, res, options) {
options = options || {};

var location;

//
// Check the proxy table for this instance to see if we need
Expand Down Expand Up @@ -237,6 +239,10 @@ RoutingProxy.prototype.proxyRequest = function (req, res, options) {
RoutingProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options) {
options = options || {};

var location,
proxy,
key;

if (this.proxyTable && !options.host) {
location = this.proxyTable.getProxyLocation(req);

Expand All @@ -248,8 +254,7 @@ RoutingProxy.prototype.proxyWebSocketRequest = function (req, socket, head, opti
options.host = location.host;
}

var key = this._getKey(options),
proxy;
key = this._getKey(options);

if (!this.proxies[key]) {
this.add(options);
Expand All @@ -270,11 +275,10 @@ RoutingProxy.prototype._getKey = function (options) {
if (!options || ((!options.host || !options.port)
&& (!options.target || !options.target.host || !options.target.port))) {
throw new Error('options.host and options.port or options.target are required.');
return;
}

return [
options.host || options.target.host,
options.port || options.target.port
].join(':');
}
};

0 comments on commit 36226da

Please sign in to comment.