@@ -112,19 +112,15 @@ Ftp.prototype.reemit = function(event) {
112
112
} ;
113
113
114
114
Ftp . prototype . _createSocket = function ( port , host , firstAction ) {
115
- if ( this . socket && this . socket . destroy ) this . socket . destroy ( ) ;
115
+ if ( this . socket && this . socket . destroy ) {
116
+ this . socket . destroy ( ) ;
117
+ }
116
118
117
119
this . authenticated = false ;
118
120
var socket = Net . createConnection ( port , host , firstAction || NOOP ) ;
119
121
socket . on ( 'connect' , this . reemit ( 'connect' ) ) ;
120
122
socket . on ( 'timeout' , this . reemit ( 'timeout' ) ) ;
121
123
122
- this . _createStreams ( socket ) ;
123
-
124
- return socket ;
125
- } ;
126
-
127
- Ftp . prototype . _createStreams = function ( socket ) {
128
124
this . pipeline = es . pipeline ( socket , this . resParser ) ;
129
125
130
126
var self = this ;
@@ -133,6 +129,8 @@ Ftp.prototype._createStreams = function(socket) {
133
129
self . parseResponse . call ( self , data ) ;
134
130
} ) ;
135
131
this . pipeline . on ( 'error' , this . reemit ( 'error' ) ) ;
132
+
133
+ return socket ;
136
134
} ;
137
135
138
136
Ftp . prototype . parseResponse = function ( data ) {
@@ -198,31 +196,28 @@ Ftp.prototype.nextCmd = function() {
198
196
* @param {function } callback
199
197
*/
200
198
Ftp . prototype . execute = function ( action , callback ) {
201
- if ( ! callback ) callback = NOOP ;
202
-
203
199
if ( this . socket && this . socket . writable ) {
204
- return this . runCommand ( action , callback ) ;
200
+ return this . runCommand ( action , callback || NOOP ) ;
205
201
}
206
202
207
203
var self = this ;
208
204
this . authenticated = false ;
209
205
this . socket = this . _createSocket ( this . port , this . host , function ( ) {
210
- self . runCommand ( action , callback ) ;
206
+ self . runCommand ( action , callback || NOOP ) ;
211
207
} ) ;
212
208
} ;
213
209
214
210
Ftp . prototype . runCommand = function ( action , callback ) {
215
- var self = this ;
216
-
217
- function executeCmd ( ) {
218
- self . cmdBuffer_ . push ( [ action , callback ] ) ;
219
- self . nextCmd ( ) ;
220
- }
211
+ var executeCmd = ( function _executeCmd ( ) {
212
+ this . cmdBuffer_ . push ( [ action , callback ] ) ;
213
+ this . nextCmd ( ) ;
214
+ } ) . bind ( this ) ;
221
215
222
- if ( self . authenticated || / f e a t | s y s t | u s e r | p a s s / . test ( action ) ) {
216
+ if ( this . authenticated || / f e a t | s y s t | u s e r | p a s s / . test ( action ) ) {
223
217
return executeCmd ( ) ;
224
218
}
225
219
220
+ var self = this ;
226
221
this . getFeatures ( function ( ) {
227
222
self . auth ( self . user , self . pass , executeCmd ) ;
228
223
} ) ;
@@ -425,6 +420,11 @@ Ftp.prototype.get = function(remotePath, localPath, callback) {
425
420
return callback ( err ) ;
426
421
}
427
422
423
+ if ( ! socket ) {
424
+ return callback ( new Error (
425
+ 'An unknown error occurred when trying to retrieve PASV socket' ) ) ;
426
+ }
427
+
428
428
var writeStream = fs . createWriteStream ( localPath ) ;
429
429
writeStream . on ( 'error' , callback ) ;
430
430
@@ -584,6 +584,12 @@ Ftp.prototype.getPasvSocket = function(callback) {
584
584
585
585
var socket = Net . createConnection ( res . port , res . host ) ;
586
586
socket . setTimeout ( timeout || TIMEOUT ) ;
587
+ socket . on ( 'error' , function ( err ) {
588
+ if ( err . code === 'ECONNREFUSED' ) {
589
+ err . msg = 'Probably trying a PASV operation while one is in progress' ;
590
+ }
591
+ callback ( err ) ;
592
+ } ) ;
587
593
callback ( null , socket ) ;
588
594
} ) ;
589
595
} ) ;
@@ -635,7 +641,7 @@ Ftp.prototype.ls = function(filePath, callback) {
635
641
// and we set the variable `useList` to true, to avoid extra round
636
642
// trips to the server to check.
637
643
if ( ( err && ( err . code === 502 || err . code === 500 ) ) ||
638
- ( self . system && self . system . indexOf ( 'hummingbird' ) > - 1 ) )
644
+ ( self . system && self . system . indexOf ( 'hummingbird' ) > - 1 ) )
639
645
// Not sure if the 'hummingbird' system check ^^^ is still
640
646
// necessary. If they support any standards, the 500 error
641
647
// should have us covered. Let's leave it for now.
0 commit comments