Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ http.createServer((req, res) => {

* **preservePath** - _boolean_ - If paths in filenames from file parts in a `'multipart/form-data'` request shall be preserved. **Default:** `false`.

* **bufferOutput** - _boolean_ - If provided, fields part will be emitted as Buffer and not as text. **Default:** `false`.

* **limits** - _object_ - Various limits on incoming data. Valid properties are:

* **fieldNameSize** - _integer_ - Max field name size (in bytes). **Default:** `100`.
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function getInstance(cfg) {
defCharset: undefined,
defParamCharset: undefined,
preservePath: false,
bufferOutput: false,
};
if (cfg.highWaterMark)
instanceCfg.highWaterMark = cfg.highWaterMark;
Expand All @@ -30,6 +31,7 @@ function getInstance(cfg) {
instanceCfg.defCharset = cfg.defCharset;
instanceCfg.defParamCharset = cfg.defParamCharset;
instanceCfg.preservePath = cfg.preservePath;
instanceCfg.bufferOutput = cfg.bufferOutput;
return new type(instanceCfg);
}

Expand Down
19 changes: 12 additions & 7 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class Multipart extends Writable {
: nullDecoder);
const defCharset = (cfg.defCharset || 'utf8');
const preservePath = cfg.preservePath;
const bufferOutput = cfg.bufferOutput;
const fileOpts = {
autoDestroy: true,
emitClose: true,
Expand Down Expand Up @@ -520,17 +521,21 @@ retrydata:
let data;
switch (field.length) {
case 0:
data = '';
data = bufferOutput ? null : '';
break;
case 1:
data = convertToUTF8(field[0], partCharset, 0);
data = bufferOutput
? field[0]
: convertToUTF8(field[0], partCharset, 0);
break;
default:
data = convertToUTF8(
Buffer.concat(field, fieldSize),
partCharset,
0
);
data = bufferOutput
? Buffer.concat(field, fieldSize)
: convertToUTF8(
Buffer.concat(field, fieldSize),
partCharset,
0
);
}
field = undefined;
fieldSize = 0;
Expand Down
94 changes: 94 additions & 0 deletions test/test-types-multipart-buffers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';

const assert = require('assert');
const { inspect } = require('util');

const { mustCall } = require(`${__dirname}/common.js`);

const busboy = require('../lib');

const input = Buffer.from([
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; '
+ 'name="content"',
'Content-Type: text/plain',
'',
'A'.repeat(1023),
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
].join('\r\n'));
const boundary = '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k';
const expected = [
{ type: 'field',
name: 'content',
val: Buffer.from('A'.repeat(1023)),
info: {
nameTruncated: false,
valueTruncated: false,
encoding: '7bit',
mimeType: 'text/plain',
},
},
];
const bb = busboy({
headers: {
'content-type': `multipart/form-data; boundary=${boundary}`,
},
bufferOutput: true
});
const results = [];

bb.on('field', (name, val, info) => {
results.push({ type: 'field', name, val, info });
});

bb.on('file', (name, stream, info) => {
const data = [];
let nb = 0;
const file = {
type: 'file',
name,
data: null,
info,
limited: false,
};
results.push(file);
stream.on('data', (d) => {
data.push(d);
nb += d.length;
}).on('limit', () => {
file.limited = true;
}).on('close', () => {
file.data = Buffer.concat(data, nb);
assert.strictEqual(stream.truncated, file.limited);
}).once('error', (err) => {
file.err = err.message;
});
});

bb.on('error', (err) => {
results.push({ error: err.message });
});

bb.on('partsLimit', () => {
results.push('partsLimit');
});

bb.on('filesLimit', () => {
results.push('filesLimit');
});

bb.on('fieldsLimit', () => {
results.push('fieldsLimit');
});

bb.on('close', mustCall(() => {
assert.deepStrictEqual(
results,
expected,
'Results mismatch.\n'
+ `Parsed: ${inspect(results)}\n`
+ `Expected: ${inspect(expected)}`
);
}));

bb.end(input);