Skip to content

Commit

Permalink
pass options.knownLength to set length at beginning, w/o waiting for …
Browse files Browse the repository at this point in the history
…async size calculation
  • Loading branch information
Ben Buckman committed Oct 24, 2012
1 parent 9fa34f4 commit e2ac039
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/form_data.js
Expand Up @@ -35,18 +35,28 @@ FormData.prototype.append = function(field, value, options) {
append(value);
append(footer);

this._trackLength(header, value)
// pass along options.knownLength
this._trackLength(header, value, options);
};

FormData.prototype._trackLength = function(header, value) {
FormData.prototype._trackLength = function(header, value, options) {
var valueLength = 0;
if (Buffer.isBuffer(value)) {

// used w/ trackLengthSync(), when length is known.
// e.g. for streaming directly from a remote server,
// w/ a known file a size, and not wanting to wait for
// incoming file to finish to get its size.
if (options.knownLength != null) {
valueLength += +options.knownLength;
} else if (Buffer.isBuffer(value)) {
valueLength = value.length;
} else if (typeof value === 'string') {
valueLength = Buffer.byteLength(value);
}

this._valueLength += valueLength;

// @check why add CRLF? does this account for custom/multiple CRLFs?
this._overheadLength +=
Buffer.byteLength(header) +
+ FormData.LINE_BREAK.length;
Expand All @@ -58,8 +68,13 @@ FormData.prototype._trackLength = function(header, value) {

this._lengthRetrievers.push(function(next) {

// do we already know the size?
// 0 additional leaves value from getSyncLength()
if (options.knownLength != null) {
next(null, 0);

// check if it's local file
if (value.hasOwnProperty('fd')) {
} else if (value.hasOwnProperty('fd')) {
fs.stat(value.path, function(err, stat) {
if (err) {
next(err);
Expand Down

0 comments on commit e2ac039

Please sign in to comment.