Permalink
Browse files

Merge branch 'better_limits'

  • Loading branch information...
2 parents 765cd39 + 89b91dd commit 6322f3b49767ca118677d872a01251634b4c057f @chjj chjj committed Oct 19, 2011
Showing with 40 additions and 16 deletions.
  1. +17 −7 README.md
  2. +3 −2 lib/encoded.js
  3. +9 −0 lib/index.js
  4. +1 −1 lib/json.js
  5. +10 −6 lib/multipart.js
View
24 README.md
@@ -18,14 +18,19 @@ $ npm install parted
## As a middleware
+(__NOTE:__ Limit options have been slightly changed for v0.0.6. Refer to the
+example below.)
+
``` js
var parted = require('parted');
app.use(parted({
- path: __dirname + '/uploads', // custom file path
- encodedLimit: 30 * 1024,
- jsonLimit: 30 * 1024,
- mutlipartLimit: 30 * 1024 * 1024
+ // custom file path
+ path: __dirname + '/uploads',
+ // memory usage limit per request
+ limit: 30 * 1024,
+ // disk usage limit per request
+ diskLimit: 30 * 1024 * 1024
}));
```
@@ -34,9 +39,14 @@ app.use(parted({
### The multipart parser alone
``` js
-var parted = require('parted');
+var multipart = require('parted').multipart;
+
+var options = {
+ limit: 30 * 1024,
+ diskLimit: 30 * 1024 * 1024
+};
-var parser = new parted.multipart(type, options)
+var parser = new multipart(type, options)
, parts = {};
parser.on('error', function(err) {
@@ -49,7 +59,7 @@ parser.on('part', function(field, part) {
parts[field] = part;
});
-parser.on('data', function(bytes) {
+parser.on('data', function() {
console.log('%d bytes written.', this.written);
});
View
5 lib/encoded.js
@@ -40,10 +40,11 @@ Parser.prototype.write = function(data) {
try {
this._parse(data);
this.written += data.length;
- this.emit('data', data.length);
} catch(e) {
this._error(e);
}
+
+ this.emit('data', data);
};
Parser.prototype.end = function(data) {
@@ -160,7 +161,7 @@ Parser.middleware = function(options) {
Parser.handle = function(req, res, next, options) {
var parser = new Parser(options)
, data = {}
- , limit = options.encodedLimit || options.limit;
+ , limit = options.limit;
parser.on('value', function(key, value) {
data[key] = value;
View
9 lib/index.js
@@ -22,6 +22,15 @@ exports = function(options) {
options = options || {};
// LEGACY
+ options.limit = options.encodedLimit
+ || options.jsonLimit
+ || options.limit;
+
+ options.diskLimit = options.multipartLimit
+ || options.diskLimit
+ || options.limit;
+
+ // LEGACY
if (typeof options === 'string') {
return new exports.multipart(arguments[0], arguments[1]);
}
View
2 lib/json.js
@@ -515,7 +515,7 @@ exports.middleware = function(options) {
exports.handle = function(req, res, next, options) {
var parser = new Parser(options)
- , limit = options.jsonLimit || options.limit;
+ , limit = options.limit;
parser.on('error', function(err) {
req.destroy();
View
16 lib/multipart.js
@@ -50,6 +50,7 @@ var Parser = function(type, options) {
this.state = 'start';
this.pending = 0;
this.written = 0;
+ this.writtenDisk = 0;
this.buff = new Buffer(200);
this.preamble = true;
@@ -290,11 +291,12 @@ Parser.prototype._write = function(data) {
if (this.file) {
this.data.write(data);
+ this.writtenDisk += data.length;
} else {
this.data += this.decode.write(data);
+ this.written += data.length;
}
- this.written += data.length;
this.emit('data', data);
};
@@ -392,8 +394,9 @@ Parser.middleware = function(options) {
Parser.handle = function(req, res, next, options) {
var parser = new Parser(req.headers['content-type'], options)
- , parts = {}
- , limit = options.multipartLimit || options.limit;
+ , diskLimit = options.diskLimit
+ , limit = options.limit
+ , parts = {};
parser.on('error', function(err) {
req.destroy();
@@ -404,9 +407,10 @@ Parser.handle = function(req, res, next, options) {
parts[field] = part;
});
- if (limit) {
- parser.on('data', function(bytes) {
- if (this.written > limit) {
+ if (limit || diskLimit) {
+ parser.on('data', function() {
+ if (this.writtenDisk > diskLimit
+ || this.written > limit) {
this.emit('error', new Error('Overflow.'));
this.destroy();
}

0 comments on commit 6322f3b

Please sign in to comment.