@@ -28,6 +28,15 @@ FormData.prototype.append = function(field, value, options) {
2828 // all that streamy business can't handle numbers
2929 if ( typeof value == 'number' ) value = '' + value ;
3030
31+ // https://github.com/felixge/node-form-data/issues/38
32+ if ( util . isArray ( value ) )
33+ {
34+ // Please convert your array into string
35+ // the way web server expects it
36+ this . _error ( new Error ( 'Arrays are not supported.' ) ) ;
37+ return ;
38+ }
39+
3140 var header = this . _multiPartHeader ( field , value , options ) ;
3241 var footer = this . _multiPartFooter ( field , value , options ) ;
3342
@@ -42,7 +51,7 @@ FormData.prototype.append = function(field, value, options) {
4251FormData . prototype . _trackLength = function ( header , value , options ) {
4352 var valueLength = 0 ;
4453
45- // used w/ trackLengthSync (), when length is known.
54+ // used w/ getLengthSync (), when length is known.
4655 // e.g. for streaming directly from a remote server,
4756 // w/ a known file a size, and not wanting to wait for
4857 // incoming file to finish to get its size.
@@ -66,15 +75,11 @@ FormData.prototype._trackLength = function(header, value, options) {
6675 return ;
6776 }
6877
78+ // no need to bother with the length
79+ if ( ! options . knownLength )
6980 this . _lengthRetrievers . push ( function ( next ) {
7081
71- // do we already know the size?
72- // 0 additional leaves value from getSyncLength()
73- if ( options . knownLength != null ) {
74- next ( null , 0 ) ;
75-
76- // check if it's local file
77- } else if ( value . hasOwnProperty ( 'fd' ) ) {
82+ if ( value . hasOwnProperty ( 'fd' ) ) {
7883 fs . stat ( value . path , function ( err , stat ) {
7984 if ( err ) {
8085 next ( err ) ;
@@ -197,14 +202,27 @@ FormData.prototype._generateBoundary = function() {
197202 this . _boundary = boundary ;
198203} ;
199204
200- FormData . prototype . getLengthSync = function ( ) {
201- var knownLength = this . _overheadLength + this . _valueLength ;
205+ // Note: getLengthSync DOESN'T calculate streams length
206+ // As workaround one can calculate file size manually
207+ // and add it as knownLength option
208+ FormData . prototype . getLengthSync = function ( debug ) {
209+ var knownLength = this . _overheadLength + this . _valueLength ;
202210
203- if ( this . _streams . length ) {
204- knownLength += this . _lastBoundary ( ) . length ;
205- }
211+ // Don't get confused, there are 3 "internal" streams for each keyval pair
212+ // so it basically checks if there is any value added to the form
213+ if ( this . _streams . length ) {
214+ knownLength += this . _lastBoundary ( ) . length ;
215+ }
216+
217+ // https://github.com/felixge/node-form-data/issues/40
218+ if ( this . _lengthRetrievers . length ) {
219+ // Some async length retrivers are present
220+ // therefore synchronous length calculation is false.
221+ // Please use getLength(callback) to get proper length
222+ this . _error ( new Error ( 'Cannot calculate proper length in synchronous way.' ) ) ;
223+ }
206224
207- return knownLength ;
225+ return knownLength ;
208226} ;
209227
210228FormData . prototype . getLength = function ( cb ) {
@@ -279,6 +297,14 @@ FormData.prototype.submit = function(params, cb) {
279297 } . bind ( this ) ) ;
280298} ;
281299
300+ FormData . prototype . _error = function ( err ) {
301+ if ( this . error ) return ;
302+
303+ this . error = err ;
304+ this . pause ( ) ;
305+ this . emit ( 'error' , err ) ;
306+ } ;
307+
282308/*
283309 * Santa's little helpers
284310 */
0 commit comments