Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support limits.totalSize for multipart forms #351

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ class Multipart extends Writable {
const partsLimit = (limits && typeof limits.parts === 'number'
? limits.parts
: Infinity);
this.totalSizeLimit = (limits && typeof limits.totalSize === 'number')
? limits.totalSize
: Infinity;

this._writtenSize = 0;

let parts = -1; // Account for initial boundary
let fields = 0;
Expand Down Expand Up @@ -563,8 +568,14 @@ retrydata:
}

_write(chunk, enc, cb) {
this._writecb = cb;
this._bparser.push(chunk, 0);
this._writtenSize += chunk.length;
if (this._writtenSize > this.totalSizeLimit) {
this.destroy(new Error('Max size reached!'));
} else {
this._writecb = cb;
this._bparser.push(chunk, 0);
}

if (this._writecb)
callAndUnsetCb(this);
}
Expand Down
83 changes: 83 additions & 0 deletions test/test-types-multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,89 @@ const tests = [
],
what: 'Fields only'
},
{ source: [
['------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
'Content-Disposition: form-data; name="cont"',
'',
'some random content',
'------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
'Content-Disposition: form-data; name="pass"',
'',
'some random pass',
'------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
'Content-Disposition: form-data; name=bit',
'',
'2',
'------WebKitFormBoundaryTB2MiQ36fnSJlrhY--'
].join('\r\n')
],
limits: {
totalSize: 200
},
events:[],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this the byte-by-byte test will fail

boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
expected: [
{ error: 'Max size reached!' }
],
what: 'MaxSize limit'
},
{ source: [
['------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
'Content-Disposition: form-data; name="cont"',
'',
'some random content',
'------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
'Content-Disposition: form-data; name="pass"',
'',
'some random pass',
'------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
'Content-Disposition: form-data; name=bit',
'',
'2',
'------WebKitFormBoundaryTB2MiQ36fnSJlrhY--'
].join('\r\n')
],
limits: {
totalSize: 350
},
boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
expected: [
{
type: 'field',
name: 'cont',
val: 'some random content',
info: {
nameTruncated: false,
valueTruncated: false,
encoding: '7bit',
mimeType: 'text/plain'
}
},
{
type: 'field',
name: 'pass',
val: 'some random pass',
info: {
nameTruncated: false,
valueTruncated: false,
encoding: '7bit',
mimeType: 'text/plain'
}
},
{
type: 'field',
name: 'bit',
val: '2',
info: {
nameTruncated: false,
valueTruncated: false,
encoding: '7bit',
mimeType: 'text/plain'
}
},
],
what: 'MaxSize limit is able to read entire thing'
},
{ source: [
''
],
Expand Down