Skip to content

Commit e2ac039

Browse files
author
Ben Buckman
committed
pass options.knownLength to set length at beginning, w/o waiting for async size calculation
1 parent 9fa34f4 commit e2ac039

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

lib/form_data.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,28 @@ FormData.prototype.append = function(field, value, options) {
3535
append(value);
3636
append(footer);
3737

38-
this._trackLength(header, value)
38+
// pass along options.knownLength
39+
this._trackLength(header, value, options);
3940
};
4041

41-
FormData.prototype._trackLength = function(header, value) {
42+
FormData.prototype._trackLength = function(header, value, options) {
4243
var valueLength = 0;
43-
if (Buffer.isBuffer(value)) {
44+
45+
// used w/ trackLengthSync(), when length is known.
46+
// e.g. for streaming directly from a remote server,
47+
// w/ a known file a size, and not wanting to wait for
48+
// incoming file to finish to get its size.
49+
if (options.knownLength != null) {
50+
valueLength += +options.knownLength;
51+
} else if (Buffer.isBuffer(value)) {
4452
valueLength = value.length;
4553
} else if (typeof value === 'string') {
4654
valueLength = Buffer.byteLength(value);
4755
}
4856

4957
this._valueLength += valueLength;
58+
59+
// @check why add CRLF? does this account for custom/multiple CRLFs?
5060
this._overheadLength +=
5161
Buffer.byteLength(header) +
5262
+ FormData.LINE_BREAK.length;
@@ -58,8 +68,13 @@ FormData.prototype._trackLength = function(header, value) {
5868

5969
this._lengthRetrievers.push(function(next) {
6070

71+
// do we already know the size?
72+
// 0 additional leaves value from getSyncLength()
73+
if (options.knownLength != null) {
74+
next(null, 0);
75+
6176
// check if it's local file
62-
if (value.hasOwnProperty('fd')) {
77+
} else if (value.hasOwnProperty('fd')) {
6378
fs.stat(value.path, function(err, stat) {
6479
if (err) {
6580
next(err);

0 commit comments

Comments
 (0)