diff --git a/lib/form_data.js b/lib/form_data.js index 5d4ee88..e6551be 100644 --- a/lib/form_data.js +++ b/lib/form_data.js @@ -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; @@ -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);