@@ -6,7 +6,7 @@ var https = require('https');
66var parseUrl = require ( 'url' ) . parse ;
77var fs = require ( 'fs' ) ;
88var mime = require ( 'mime-types' ) ;
9- var async = require ( 'async ' ) ;
9+ var asynckit = require ( 'asynckit ' ) ;
1010var populate = require ( './populate.js' ) ;
1111
1212// Public API
@@ -24,12 +24,12 @@ util.inherits(FormData, CombinedStream);
2424 */
2525function FormData ( ) {
2626 if ( ! ( this instanceof FormData ) ) {
27- throw new TypeError ( 'Failed to construct FormData: Please use the _new_ operator, this object constructor cannot be called as a function.' ) ;
27+ return new FormData ( ) ;
2828 }
2929
3030 this . _overheadLength = 0 ;
3131 this . _valueLength = 0 ;
32- this . _lengthRetrievers = [ ] ;
32+ this . _valuesToMeasure = [ ] ;
3333
3434 CombinedStream . call ( this ) ;
3535}
@@ -101,60 +101,62 @@ FormData.prototype._trackLength = function(header, value, options) {
101101
102102 // no need to bother with the length
103103 if ( ! options . knownLength ) {
104- this . _lengthRetrievers . push ( function ( next ) {
105-
106- if ( value . hasOwnProperty ( 'fd' ) ) {
107-
108- // take read range into a account
109- // `end` = Infinity –> read file till the end
110- //
111- // TODO: Looks like there is bug in Node fs.createReadStream
112- // it doesn't respect `end` options without `start` options
113- // Fix it when node fixes it.
114- // https://github.com/joyent/node/issues/7819
115- if ( value . end != undefined && value . end != Infinity && value . start != undefined ) {
116-
117- // when end specified
118- // no need to calculate range
119- // inclusive, starts with 0
120- next ( null , value . end + 1 - ( value . start ? value . start : 0 ) ) ;
121-
122- // not that fast snoopy
123- } else {
124- // still need to fetch file size from fs
125- fs . stat ( value . path , function ( err , stat ) {
126-
127- var fileSize ;
128-
129- if ( err ) {
130- next ( err ) ;
131- return ;
132- }
133-
134- // update final size based on the range options
135- fileSize = stat . size - ( value . start ? value . start : 0 ) ;
136- next ( null , fileSize ) ;
137- } ) ;
104+ this . _valuesToMeasure . push ( value ) ;
105+ }
106+ } ;
107+
108+ FormData . prototype . _lengthRetriever = function ( value , callback ) {
109+
110+ if ( value . hasOwnProperty ( 'fd' ) ) {
111+
112+ // take read range into a account
113+ // `end` = Infinity –> read file till the end
114+ //
115+ // TODO: Looks like there is bug in Node fs.createReadStream
116+ // it doesn't respect `end` options without `start` options
117+ // Fix it when node fixes it.
118+ // https://github.com/joyent/node/issues/7819
119+ if ( value . end != undefined && value . end != Infinity && value . start != undefined ) {
120+
121+ // when end specified
122+ // no need to calculate range
123+ // inclusive, starts with 0
124+ callback ( null , value . end + 1 - ( value . start ? value . start : 0 ) ) ;
125+
126+ // not that fast snoopy
127+ } else {
128+ // still need to fetch file size from fs
129+ fs . stat ( value . path , function ( err , stat ) {
130+
131+ var fileSize ;
132+
133+ if ( err ) {
134+ callback ( err ) ;
135+ return ;
138136 }
139137
140- // or http response
141- } else if ( value . hasOwnProperty ( 'httpVersion' ) ) {
142- next ( null , + value . headers [ 'content-length' ] ) ;
143-
144- // or request stream http://github.com/mikeal/request
145- } else if ( value . hasOwnProperty ( 'httpModule' ) ) {
146- // wait till response come back
147- value . on ( 'response' , function ( response ) {
148- value . pause ( ) ;
149- next ( null , + response . headers [ 'content-length' ] ) ;
150- } ) ;
151- value . resume ( ) ;
152-
153- // something else
154- } else {
155- next ( 'Unknown stream' ) ;
156- }
138+ // update final size based on the range options
139+ fileSize = stat . size - ( value . start ? value . start : 0 ) ;
140+ callback ( null , fileSize ) ;
141+ } ) ;
142+ }
143+
144+ // or http response
145+ } else if ( value . hasOwnProperty ( 'httpVersion' ) ) {
146+ callback ( null , + value . headers [ 'content-length' ] ) ;
147+
148+ // or request stream http://github.com/mikeal/request
149+ } else if ( value . hasOwnProperty ( 'httpModule' ) ) {
150+ // wait till response come back
151+ value . on ( 'response' , function ( response ) {
152+ value . pause ( ) ;
153+ callback ( null , + response . headers [ 'content-length' ] ) ;
157154 } ) ;
155+ value . resume ( ) ;
156+
157+ // something else
158+ } else {
159+ callback ( 'Unknown stream' ) ;
158160 }
159161} ;
160162
@@ -291,18 +293,6 @@ FormData.prototype.getHeaders = function(userHeaders) {
291293 return formHeaders ;
292294} ;
293295
294- // TODO: Looks like unused function
295- FormData . prototype . getCustomHeaders = function ( contentType ) {
296- contentType = contentType ? contentType : 'multipart/form-data' ;
297-
298- var formHeaders = {
299- 'content-type' : contentType + '; boundary=' + this . getBoundary ( ) ,
300- 'content-length' : this . getLengthSync ( )
301- } ;
302-
303- return formHeaders ;
304- } ;
305-
306296FormData . prototype . getBoundary = function ( ) {
307297 if ( ! this . _boundary ) {
308298 this . _generateBoundary ( ) ;
@@ -335,7 +325,7 @@ FormData.prototype.getLengthSync = function() {
335325 }
336326
337327 // https://github.com/form-data/form-data/issues/40
338- if ( this . _lengthRetrievers . length ) {
328+ if ( this . _valuesToMeasure . length ) {
339329 // Some async length retrievers are present
340330 // therefore synchronous length calculation is false.
341331 // Please use getLength(callback) to get proper length
@@ -352,12 +342,12 @@ FormData.prototype.getLength = function(cb) {
352342 knownLength += this . _lastBoundary ( ) . length ;
353343 }
354344
355- if ( ! this . _lengthRetrievers . length ) {
345+ if ( ! this . _valuesToMeasure . length ) {
356346 process . nextTick ( cb . bind ( this , null , knownLength ) ) ;
357347 return ;
358348 }
359349
360- async . parallel ( this . _lengthRetrievers , function ( err , values ) {
350+ asynckit . parallel ( this . _valuesToMeasure , this . _lengthRetriever , function ( err , values ) {
361351 if ( err ) {
362352 cb ( err ) ;
363353 return ;
0 commit comments