Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

simpler limit handling

  • Loading branch information...
commit 89b91dd768be5540d182a4ff8f9d49e52f66cbd9 1 parent 765cd39
Christopher Jeffrey chjj authored
24 README.md
Source Rendered
@@ -18,14 +18,19 @@ $ npm install parted
18 18
19 19 ## As a middleware
20 20
  21 +(__NOTE:__ Limit options have been slightly changed for v0.0.6. Refer to the
  22 +example below.)
  23 +
21 24 ``` js
22 25 var parted = require('parted');
23 26
24 27 app.use(parted({
25   - path: __dirname + '/uploads', // custom file path
26   - encodedLimit: 30 * 1024,
27   - jsonLimit: 30 * 1024,
28   - mutlipartLimit: 30 * 1024 * 1024
  28 + // custom file path
  29 + path: __dirname + '/uploads',
  30 + // memory usage limit per request
  31 + limit: 30 * 1024,
  32 + // disk usage limit per request
  33 + diskLimit: 30 * 1024 * 1024
29 34 }));
30 35 ```
31 36
@@ -34,9 +39,14 @@ app.use(parted({
34 39 ### The multipart parser alone
35 40
36 41 ``` js
37   -var parted = require('parted');
  42 +var multipart = require('parted').multipart;
  43 +
  44 +var options = {
  45 + limit: 30 * 1024,
  46 + diskLimit: 30 * 1024 * 1024
  47 +};
38 48
39   -var parser = new parted.multipart(type, options)
  49 +var parser = new multipart(type, options)
40 50 , parts = {};
41 51
42 52 parser.on('error', function(err) {
@@ -49,7 +59,7 @@ parser.on('part', function(field, part) {
49 59 parts[field] = part;
50 60 });
51 61
52   -parser.on('data', function(bytes) {
  62 +parser.on('data', function() {
53 63 console.log('%d bytes written.', this.written);
54 64 });
55 65
5 lib/encoded.js
@@ -40,10 +40,11 @@ Parser.prototype.write = function(data) {
40 40 try {
41 41 this._parse(data);
42 42 this.written += data.length;
43   - this.emit('data', data.length);
44 43 } catch(e) {
45 44 this._error(e);
46 45 }
  46 +
  47 + this.emit('data', data);
47 48 };
48 49
49 50 Parser.prototype.end = function(data) {
@@ -160,7 +161,7 @@ Parser.middleware = function(options) {
160 161 Parser.handle = function(req, res, next, options) {
161 162 var parser = new Parser(options)
162 163 , data = {}
163   - , limit = options.encodedLimit || options.limit;
  164 + , limit = options.limit;
164 165
165 166 parser.on('value', function(key, value) {
166 167 data[key] = value;
9 lib/index.js
@@ -22,6 +22,15 @@ exports = function(options) {
22 22 options = options || {};
23 23
24 24 // LEGACY
  25 + options.limit = options.encodedLimit
  26 + || options.jsonLimit
  27 + || options.limit;
  28 +
  29 + options.diskLimit = options.multipartLimit
  30 + || options.diskLimit
  31 + || options.limit;
  32 +
  33 + // LEGACY
25 34 if (typeof options === 'string') {
26 35 return new exports.multipart(arguments[0], arguments[1]);
27 36 }
2  lib/json.js
@@ -515,7 +515,7 @@ exports.middleware = function(options) {
515 515
516 516 exports.handle = function(req, res, next, options) {
517 517 var parser = new Parser(options)
518   - , limit = options.jsonLimit || options.limit;
  518 + , limit = options.limit;
519 519
520 520 parser.on('error', function(err) {
521 521 req.destroy();
16 lib/multipart.js
@@ -50,6 +50,7 @@ var Parser = function(type, options) {
50 50 this.state = 'start';
51 51 this.pending = 0;
52 52 this.written = 0;
  53 + this.writtenDisk = 0;
53 54 this.buff = new Buffer(200);
54 55
55 56 this.preamble = true;
@@ -290,11 +291,12 @@ Parser.prototype._write = function(data) {
290 291
291 292 if (this.file) {
292 293 this.data.write(data);
  294 + this.writtenDisk += data.length;
293 295 } else {
294 296 this.data += this.decode.write(data);
  297 + this.written += data.length;
295 298 }
296 299
297   - this.written += data.length;
298 300 this.emit('data', data);
299 301 };
300 302
@@ -392,8 +394,9 @@ Parser.middleware = function(options) {
392 394
393 395 Parser.handle = function(req, res, next, options) {
394 396 var parser = new Parser(req.headers['content-type'], options)
395   - , parts = {}
396   - , limit = options.multipartLimit || options.limit;
  397 + , diskLimit = options.diskLimit
  398 + , limit = options.limit
  399 + , parts = {};
397 400
398 401 parser.on('error', function(err) {
399 402 req.destroy();
@@ -404,9 +407,10 @@ Parser.handle = function(req, res, next, options) {
404 407 parts[field] = part;
405 408 });
406 409
407   - if (limit) {
408   - parser.on('data', function(bytes) {
409   - if (this.written > limit) {
  410 + if (limit || diskLimit) {
  411 + parser.on('data', function() {
  412 + if (this.writtenDisk > diskLimit
  413 + || this.written > limit) {
410 414 this.emit('error', new Error('Overflow.'));
411 415 this.destroy();
412 416 }

0 comments on commit 89b91dd

Please sign in to comment.
Something went wrong with that request. Please try again.