Comparing version 0.0.1 to 0.0.2
@@ -111,5 +111,2 @@ /* | ||
function exposeGlobalTimers(config) { | ||
var globAll, | ||
globInc, | ||
globOut; | ||
@@ -133,18 +130,17 @@ // Sets the global timeout value for all outgoing requests | ||
// set values from config | ||
if (config) { | ||
globAll = parseInt(config.global_timeout, 10); | ||
if (globAll > 0) { | ||
process.setAllRequestsTimeout(globAll); | ||
} | ||
config.global_timeout = parseInt(config.global_timeout, 10); | ||
if (config.global_timeout > 0) { | ||
process.setAllRequestsTimeout(config.global_timeout); | ||
} | ||
globInc = parseInt(config.inc_req_timeout, 10); | ||
if (globInc > 0) { | ||
process.setIncomingRequestsTimeout(globInc); | ||
} | ||
config.inc_req_timeout = parseInt(config.inc_req_timeout, 10); | ||
if (config.inc_req_timeout > 0) { | ||
process.setIncomingRequestsTimeout(config.inc_req_timeout); | ||
} | ||
globOut = parseInt(config.out_req_timeout, 10); | ||
if (globOut > 0) { | ||
process.setOutgoingRequestsTimeout(globOut); | ||
} | ||
config.out_req_timeout = parseInt(config.out_req_timeout, 10); | ||
if (config.out_req_timeout > 0) { | ||
process.setOutgoingRequestsTimeout(config.out_req_timeout); | ||
} | ||
} | ||
@@ -159,3 +155,4 @@ | ||
if (config && config.idle_timeout > 0) { | ||
config.idle_timeout = parseInt(config.idle_timeout, 10); | ||
if (config.idle_timeout > 0) { | ||
if (req.socket) { | ||
@@ -168,4 +165,5 @@ req.socket.setTimeout(config.idle_timeout); | ||
config.incoming_timeout = parseInt(config.incoming_timeout, 10); | ||
// sets the timeout on the request | ||
if ((config && config.incoming_timeout > 0) || globalIncoming > 0) { | ||
if (config.incoming_timeout > 0 || globalIncoming > 0) { | ||
timeout = config.incoming_timeout || globalIncoming; | ||
@@ -176,10 +174,2 @@ closer = new Closer(resp, timeout); | ||
function setNoDelay(conf, req) { | ||
var socket; | ||
if (conf.socket_no_delay) { | ||
socket = req.connection && req.connection.socket ? req.connection.socket : req.socket; | ||
socket.setNoDelay(true); | ||
} | ||
} | ||
/* | ||
@@ -189,6 +179,5 @@ * Sets the maxSockects | ||
function setDefaultMaxSockets(conf) { | ||
var num = parseInt(conf.max_sockets, 10); | ||
if (!isNaN(num) && num > 0) { | ||
http.globalAgent.maxSockets = num; | ||
conf.max_sockets = parseInt(conf.max_sockets, 10); | ||
if (conf.max_sockets > 0) { | ||
http.globalAgent.maxSockets = conf.max_sockets; | ||
} | ||
@@ -198,8 +187,14 @@ } | ||
module.exports = function (config) { | ||
if (config && (config.enable === "true" || config.enable === true)) { | ||
exposeGlobalTimers(config); | ||
} | ||
exposeGlobalTimers(config); | ||
return function (req, resp, next) { | ||
var count = null, | ||
conf = (req.mod_config) ? mergeConfig(config, req.mod_config) : config; | ||
if (!conf || (conf.enable !== "true" && conf.enable !== true)) { | ||
next(); | ||
return; | ||
} | ||
@@ -209,15 +204,16 @@ if (typeof conf.post_max_size === "string") { | ||
} | ||
// allow the setting of maxSockets even if the rest is disabled | ||
if (conf) { | ||
setDefaultMaxSockets(conf); | ||
if (typeof conf.uri_max_length === "string") { | ||
conf.uri_max_length = parseInt(conf.uri_max_length, 10); | ||
} | ||
if ((conf.enable !== "true" && conf.enable !== true) || conf.post_max_size < 0) { | ||
next(); | ||
if (conf.uri_max_length > 0 && req.url.length > conf.uri_max_length ) { | ||
resp.writeHead(413, 'Request-URI Too Long', {'Content-Type': 'text/plain'}); | ||
resp.end('413 Request-URI Too Long'); | ||
return; | ||
} | ||
setDefaultMaxSockets(conf); | ||
instrumentReq(conf, req, resp); | ||
setNoDelay(conf, req); | ||
@@ -232,3 +228,3 @@ req.on('data', function (data) { | ||
if (count > conf.post_max_size || (conf.file_uploads !== "true" && conf.file_uploads !== true)) { | ||
if ((conf.post_max_size > 0 && count > conf.post_max_size) || (conf.file_uploads !== "true" && conf.file_uploads !== true)) { | ||
@@ -235,0 +231,0 @@ if (!resp._header) { |
@@ -5,3 +5,3 @@ { | ||
"author": "Abhinav Raj <abhinavr@yahoo-inc.com>", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"devDependencies": { | ||
@@ -32,2 +32,2 @@ "ytestrunner": "*", | ||
} | ||
} | ||
} |
@@ -12,3 +12,6 @@ limits | ||
* Limiting the total size of upload | ||
In config, use: { post_max_size: [bytes] } | ||
In config, use: { post_max_size: [bytes] }, if 0, this functionality is disabled | ||
* Limiting the length of uri | ||
In config, use: { uri_max_length: [number] }, if 0 this functionality is disabled | ||
@@ -29,4 +32,8 @@ * Setting a global absolute timeout for both incoming and outgoing connections | ||
In config, use: { max_sockets: [number] }, if 0 - nothing will be set. | ||
* Setting the http.Agent.defaultMaxSockets for the entire app | ||
In config, use: { socket_no_delay: [boolean] }, if false - nothing will be set. | ||
To completely disable module use config, { enable: false } | ||
To completely disable module use config, { enable: false }. | ||
Each of the above functionality is disabled if corresponding config attribute is not set. | ||
@@ -57,2 +64,11 @@ install | ||
app.listen(8000); | ||
``` | ||
``` | ||
Build Status | ||
------------ | ||
[![Build Status](https://secure.travis-ci.org/yahoo/node-limits.png?branch=master)](http://travis-ci.org/yahoo/node-limits) | ||
Node Badge | ||
---------- | ||
[![NPM](https://nodei.co/npm/limits.png)](https://nodei.co/npm/limits/) |
@@ -101,3 +101,16 @@ var http = require('http'), | ||
}, | ||
'Verify that limit is disabled with null config' : function() { | ||
var testee = mod_limits(); | ||
var req = getReq(), | ||
resp = getResp(), | ||
next = false; | ||
testee(req, resp, function() { | ||
next = true; | ||
}); | ||
Assert.isNull(req.listener); | ||
Assert.isTrue(next); | ||
}, | ||
'Verify that we can disable limits' : function() { | ||
@@ -120,3 +133,2 @@ var testee = mod_limits({ | ||
}, | ||
'Verify that limits works with string data' : function() { | ||
@@ -309,4 +321,3 @@ var testee = mod_limits({ | ||
"enable" : "true", | ||
"idle_timeout" : 0, // will be overwritten by local config | ||
"socket_no_delay" : true | ||
"idle_timeout" : 0 // will be overwritten by local config | ||
}); | ||
@@ -316,4 +327,3 @@ | ||
resp = getResp(), | ||
next = false, | ||
noDelay = false; | ||
next = false; | ||
@@ -329,5 +339,2 @@ req.mod_config = { | ||
socketCalled = true; | ||
}, | ||
setNoDelay : function() { | ||
noDelay = true; | ||
} | ||
@@ -342,5 +349,3 @@ } | ||
Assert.isTrue(socketCalled); | ||
Assert.isTrue(noDelay); | ||
noDelay = false; | ||
@@ -353,5 +358,2 @@ socketCalled = false; | ||
}, | ||
setNoDelay : function() { | ||
noDelay = true; | ||
}, | ||
pause : function() { | ||
@@ -361,3 +363,2 @@ } | ||
}; | ||
delete req.socket; | ||
@@ -369,6 +370,40 @@ | ||
Assert.isTrue(socketCalled); | ||
Assert.isTrue(noDelay); | ||
}, | ||
'Verify that limit works with correct url length' : function() { | ||
var testee = mod_limits({ | ||
"enable" : "true", | ||
"uri_max_length" : 1000, | ||
}); | ||
var req = getReq(), | ||
resp = getResp(), | ||
next = false; | ||
testee(req, resp, function() { | ||
next = true; | ||
}); | ||
Assert.isTrue(next); | ||
Assert.isTrue(resp.status !== 413); | ||
}, | ||
'Verify that limit give 413 for over the length url' : function() { | ||
var testee = mod_limits({ | ||
"enable" : "true", | ||
"uri_max_length" : "12", //length is 13 in the req object coming from getReq | ||
}); | ||
var req = getReq(), | ||
resp = getResp(), | ||
next = false; | ||
testee(req, resp, function() { | ||
next = true; | ||
}); | ||
Assert.isTrue(!next); | ||
Assert.isTrue(resp.status === 413); | ||
} | ||
})); | ||
// vim:ts=4 sw=4 et |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
22952
555
72
4