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

Add additional multipart headers as an optional parameter #183

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
18 changes: 16 additions & 2 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function Multipart(boy, cfg) {
field.removeAllListeners('end');
}

var additionalHeaders = {};
part.on('header', function(header) {
var contype,
fieldname,
Expand Down Expand Up @@ -169,6 +170,10 @@ function Multipart(boy, cfg) {
else
encoding = '7bit';

for (var h in header) {
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should just iterate over Object.keys(header) to avoid traversing prototypes and the like.

if (!isRequiredHeader(h)) additionalHeaders[h] = header[h][0];
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should not be forcing the first value all the time, in case someone is interested in seeing all of the duplicate header values. Maybe at the very least we could just return the first value if header[h].length === 1 otherwise use the array value. Also just always assigning to header[h] so it's always an array for consistency is fine by me too.

}

var onData,
onEnd;
if (contype === 'application/octet-stream' || filename !== undefined) {
Expand Down Expand Up @@ -210,7 +215,8 @@ function Multipart(boy, cfg) {
cb();
}
};
boy.emit('file', fieldname, file, filename, encoding, contype);

boy.emit('file', fieldname, file, filename, encoding, contype, additionalHeaders);

onData = function(data) {
if ((nsize += data.length) > fileSizeLimit) {
Expand Down Expand Up @@ -258,7 +264,8 @@ function Multipart(boy, cfg) {
curField = undefined;
if (buffer.length)
buffer = decodeText(buffer, 'binary', charset);
boy.emit('field', fieldname, buffer, false, truncated, encoding, contype);

boy.emit('field', fieldname, buffer, false, truncated, encoding, contype, additionalHeaders);
--nends;
checkFinished();
};
Expand Down Expand Up @@ -317,6 +324,13 @@ function FileStream(opts) {

this.truncated = false;
}

function isRequiredHeader(h) {
return h === 'content-type'
|| h === 'content-disposition'
|| h === 'content-transfer-encoding';
}

inherits(FileStream, ReadableStream);

FileStream.prototype._read = function(n) {};
Expand Down