@@ -11,9 +11,9 @@ var async = require('async');
1111module . exports = FormData ;
1212function FormData ( ) {
1313 if ( ! ( this instanceof FormData ) ) {
14- throw new TypeError ( " Failed to construct ' FormData' : Please use the 'new' operator, this object constructor cannot be called as a function." )
14+ throw new TypeError ( ' Failed to construct FormData: Please use the _new_ operator, this object constructor cannot be called as a function.' ) ;
1515 }
16-
16+
1717 this . _overheadLength = 0 ;
1818 this . _valueLength = 0 ;
1919 this . _lengthRetrievers = [ ] ;
@@ -33,7 +33,9 @@ FormData.prototype.append = function(field, value, options) {
3333 var append = CombinedStream . prototype . append . bind ( this ) ;
3434
3535 // all that streamy business can't handle numbers
36- if ( typeof value == 'number' ) value = '' + value ;
36+ if ( typeof value == 'number' ) {
37+ value = '' + value ;
38+ }
3739
3840 // https://github.com/felixge/node-form-data/issues/38
3941 if ( util . isArray ( value ) ) {
@@ -83,60 +85,62 @@ FormData.prototype._trackLength = function(header, value, options) {
8385
8486 // no need to bother with the length
8587 if ( ! options . knownLength )
86- this . _lengthRetrievers . push ( function ( next ) {
87-
88- if ( value . hasOwnProperty ( 'fd' ) ) {
89-
90- // take read range into a account
91- // `end` = Infinity –> read file till the end
92- //
93- // TODO: Looks like there is bug in Node fs.createReadStream
94- // it doesn't respect `end` options without `start` options
95- // Fix it when node fixes it.
96- // https://github.com/joyent/node/issues/7819
97- if ( value . end != undefined && value . end != Infinity && value . start != undefined ) {
98-
99- // when end specified
100- // no need to calculate range
101- // inclusive, starts with 0
102- next ( null , value . end + 1 - ( value . start ? value . start : 0 ) ) ;
88+ {
89+ this . _lengthRetrievers . push ( function ( next ) {
90+
91+ if ( value . hasOwnProperty ( 'fd' ) ) {
92+
93+ // take read range into a account
94+ // `end` = Infinity –> read file till the end
95+ //
96+ // TODO: Looks like there is bug in Node fs.createReadStream
97+ // it doesn't respect `end` options without `start` options
98+ // Fix it when node fixes it.
99+ // https://github.com/joyent/node/issues/7819
100+ if ( value . end != undefined && value . end != Infinity && value . start != undefined ) {
101+
102+ // when end specified
103+ // no need to calculate range
104+ // inclusive, starts with 0
105+ next ( null , value . end + 1 - ( value . start ? value . start : 0 ) ) ;
106+
107+ // not that fast snoopy
108+ } else {
109+ // still need to fetch file size from fs
110+ fs . stat ( value . path , function ( err , stat ) {
111+
112+ var fileSize ;
113+
114+ if ( err ) {
115+ next ( err ) ;
116+ return ;
117+ }
118+
119+ // update final size based on the range options
120+ fileSize = stat . size - ( value . start ? value . start : 0 ) ;
121+ next ( null , fileSize ) ;
122+ } ) ;
123+ }
124+
125+ // or http response
126+ } else if ( value . hasOwnProperty ( 'httpVersion' ) ) {
127+ next ( null , + value . headers [ 'content-length' ] ) ;
128+
129+ // or request stream http://github.com/mikeal/request
130+ } else if ( value . hasOwnProperty ( 'httpModule' ) ) {
131+ // wait till response come back
132+ value . on ( 'response' , function ( response ) {
133+ value . pause ( ) ;
134+ next ( null , + response . headers [ 'content-length' ] ) ;
135+ } ) ;
136+ value . resume ( ) ;
103137
104- // not that fast snoopy
138+ // something else
105139 } else {
106- // still need to fetch file size from fs
107- fs . stat ( value . path , function ( err , stat ) {
108-
109- var fileSize ;
110-
111- if ( err ) {
112- next ( err ) ;
113- return ;
114- }
115-
116- // update final size based on the range options
117- fileSize = stat . size - ( value . start ? value . start : 0 ) ;
118- next ( null , fileSize ) ;
119- } ) ;
140+ next ( 'Unknown stream' ) ;
120141 }
121-
122- // or http response
123- } else if ( value . hasOwnProperty ( 'httpVersion' ) ) {
124- next ( null , + value . headers [ 'content-length' ] ) ;
125-
126- // or request stream http://github.com/mikeal/request
127- } else if ( value . hasOwnProperty ( 'httpModule' ) ) {
128- // wait till response come back
129- value . on ( 'response' , function ( response ) {
130- value . pause ( ) ;
131- next ( null , + response . headers [ 'content-length' ] ) ;
132- } ) ;
133- value . resume ( ) ;
134-
135- // something else
136- } else {
137- next ( 'Unknown stream' ) ;
138- }
139- } ) ;
142+ } ) ;
143+ }
140144} ;
141145
142146FormData . prototype . _multiPartHeader = function ( field , value , options ) {
@@ -189,11 +193,11 @@ FormData.prototype._multiPartHeader = function(field, value, options) {
189193 contents += prop + ': ' + headers [ prop ] . join ( '; ' ) + FormData . LINE_BREAK ;
190194 }
191195 }
192-
196+
193197 return '--' + this . getBoundary ( ) + FormData . LINE_BREAK + contents + FormData . LINE_BREAK ;
194198} ;
195199
196- FormData . prototype . _multiPartFooter = function ( field , value , options ) {
200+ FormData . prototype . _multiPartFooter = function ( field , value ) {
197201 return function ( next ) {
198202 var footer = FormData . LINE_BREAK ;
199203
@@ -211,16 +215,19 @@ FormData.prototype._lastBoundary = function() {
211215} ;
212216
213217FormData . prototype . getHeaders = function ( userHeaders ) {
218+ var header ;
214219 var formHeaders = {
215220 'content-type' : 'multipart/form-data; boundary=' + this . getBoundary ( )
216221 } ;
217222
218- for ( var header in userHeaders ) {
219- formHeaders [ header . toLowerCase ( ) ] = userHeaders [ header ] ;
223+ for ( header in userHeaders ) {
224+ if ( userHeaders . hasOwnProperty ( header ) ) {
225+ formHeaders [ header . toLowerCase ( ) ] = userHeaders [ header ] ;
226+ }
220227 }
221228
222229 return formHeaders ;
223- }
230+ } ;
224231
225232FormData . prototype . getCustomHeaders = function ( contentType ) {
226233 contentType = contentType ? contentType : 'multipart/form-data' ;
@@ -231,7 +238,7 @@ FormData.prototype.getCustomHeaders = function(contentType) {
231238 } ;
232239
233240 return formHeaders ;
234- }
241+ } ;
235242
236243FormData . prototype . getBoundary = function ( ) {
237244 if ( ! this . _boundary ) {
@@ -255,7 +262,7 @@ FormData.prototype._generateBoundary = function() {
255262// Note: getLengthSync DOESN'T calculate streams length
256263// As workaround one can calculate file size manually
257264// and add it as knownLength option
258- FormData . prototype . getLengthSync = function ( debug ) {
265+ FormData . prototype . getLengthSync = function ( ) {
259266 var knownLength = this . _overheadLength + this . _valueLength ;
260267
261268 // Don't get confused, there are 3 "internal" streams for each keyval pair
@@ -302,7 +309,6 @@ FormData.prototype.getLength = function(cb) {
302309} ;
303310
304311FormData . prototype . submit = function ( params , cb ) {
305-
306312 var request
307313 , options
308314 , defaults = {
@@ -358,11 +364,11 @@ FormData.prototype.submit = function(params, cb) {
358364} ;
359365
360366FormData . prototype . _error = function ( err ) {
361- if ( this . error ) return ;
362-
363- this . error = err ;
364- this . pause ( ) ;
365- this . emit ( 'error' , err ) ;
367+ if ( ! this . error ) {
368+ this . error = err ;
369+ this . pause ( ) ;
370+ this . emit ( 'error' , err ) ;
371+ }
366372} ;
367373
368374/*
@@ -372,7 +378,9 @@ FormData.prototype._error = function(err) {
372378// populates missing values
373379function populate ( dst , src ) {
374380 for ( var prop in src ) {
375- if ( ! dst [ prop ] ) dst [ prop ] = src [ prop ] ;
381+ if ( src . hasOwnProperty ( prop ) && ! dst [ prop ] ) {
382+ dst [ prop ] = src [ prop ] ;
383+ }
376384 }
377385 return dst ;
378386}
0 commit comments