@@ -17,7 +17,6 @@ var fs = require("fs");
17
17
var once = require ( "once" ) ;
18
18
19
19
var FTP_PORT = 21 ;
20
- var DEBUG_MODE = false ;
21
20
var TIMEOUT = 10 * 60 * 1000 ;
22
21
var IDLE_TIME = 30000 ;
23
22
var NOOP = function ( ) { } ;
@@ -31,14 +30,17 @@ var COMMANDS = [
31
30
"chmod" , "size"
32
31
] ;
33
32
34
- function getPasvPort ( text ) {
33
+ function getPasvPort ( text , cb ) {
35
34
var RE_PASV = / ( [ - \d ] + , [ - \d ] + , [ - \d ] + , [ - \d ] + ) , ( [ - \d ] + ) , ( [ - \d ] + ) / ;
36
35
var match = RE_PASV . exec ( text ) ;
37
- if ( ! match ) return false ;
36
+ if ( ! match ) {
37
+ return cb ( new Error ( "Bad passive host/port combination" ) ) ;
38
+ }
38
39
39
- // Array containing the passive host and the port number
40
- return [ match [ 1 ] . replace ( / , / g, "." ) ,
41
- ( parseInt ( match [ 2 ] , 10 ) & 255 ) * 256 + ( parseInt ( match [ 3 ] , 10 ) & 255 ) ] ;
40
+ cb ( null , {
41
+ host : match [ 1 ] . replace ( / , / g, "." ) ,
42
+ port : ( parseInt ( match [ 2 ] , 10 ) & 255 ) * 256 + ( parseInt ( match [ 3 ] , 10 ) & 255 )
43
+ } ) ;
42
44
}
43
45
44
46
function runCmd ( cmd ) {
@@ -75,21 +77,37 @@ var Ftp = module.exports = function(cfg) {
75
77
// Generate generic methods from parameter names. they can easily be
76
78
// overriden if we need special behavior. they accept any parameters given,
77
79
// it is the responsability of the user to validate the parameters.
78
- this . raw = function ( ) {
80
+ this . raw = function ( ) {
79
81
return runCmd . apply ( this , arguments ) ;
80
82
} . bind ( this ) ;
81
83
COMMANDS . forEach ( function ( cmd ) {
82
84
this . raw [ cmd ] = runCmd . bind ( this , cmd ) ;
83
85
} , this ) ;
84
86
87
+ var self = this ;
88
+ this . on ( 'data' , function ( data ) {
89
+ if ( self . debugMode ) {
90
+ self . emit ( 'jsftp_debug' , 'response' , data || { } ) ;
91
+ }
92
+ } ) ;
93
+
85
94
this . socket = this . _createSocket ( this . port , this . host ) ;
86
95
} ;
87
96
88
97
util . inherits ( Ftp , EventEmitter ) ;
89
98
99
+ Ftp . prototype . setDebugMode = function ( debugOn ) {
100
+ this . debugMode = ( debugOn !== false ) ;
101
+ } ;
102
+
90
103
Ftp . prototype . reemit = function ( event ) {
91
104
var self = this ;
92
- return function ( data ) { self . emit ( event , data ) ; }
105
+ return function ( data ) {
106
+ self . emit ( event , data ) ;
107
+ if ( self . debugMode ) {
108
+ self . emit ( 'jsftp_debug' , 'event:' + event , data || { } ) ;
109
+ }
110
+ }
93
111
} ;
94
112
95
113
Ftp . prototype . _createSocket = function ( port , host , firstAction ) {
@@ -155,6 +173,10 @@ Ftp.prototype.send = function(command) {
155
173
156
174
this . emit ( "cmdSend" , command ) ;
157
175
this . pipeline . write ( command + "\r\n" ) ;
176
+
177
+ if ( this . debugMode ) {
178
+ this . emit ( 'jsftp_debug' , 'user_command' , command || { } ) ;
179
+ }
158
180
} ;
159
181
160
182
Ftp . prototype . nextCmd = function ( ) {
@@ -489,11 +511,11 @@ Ftp.prototype.put = function(from, to, callback) {
489
511
return callback ( new Error ( "Local file doesn't exist." ) ) ;
490
512
491
513
fs . stat ( from , function ( err , stats ) {
492
- var totalSize = err ? 0 : stats . size ;
493
- var read = fs . createReadStream ( from , {
494
- bufferSize : 4 * 1024
495
- } ) ;
496
- putReadable ( read , to , totalSize , callback ) ;
514
+ var totalSize = err ? 0 : stats . size ;
515
+ var read = fs . createReadStream ( from , {
516
+ bufferSize : 4 * 1024
517
+ } ) ;
518
+ putReadable ( read , to , totalSize , callback ) ;
497
519
} ) ;
498
520
} ) ;
499
521
} else {
@@ -546,23 +568,21 @@ Ftp.prototype.pasvTimeout = function(socket, cb) {
546
568
socket . destroy ( ) ;
547
569
cb ( new Error ( "Passive socket timeout" ) ) ;
548
570
} ) ;
549
- }
571
+ } ;
550
572
551
573
Ftp . prototype . getPasvSocket = function ( callback ) {
552
574
var timeout = this . timeout ;
553
575
callback = once ( callback || NOOP ) ;
554
576
this . execute ( "pasv" , function ( err , res ) {
555
577
if ( err ) return callback ( err ) ;
556
578
557
- var pasvRes = getPasvPort ( res . text ) ;
558
- if ( pasvRes === false )
559
- return callback ( new Error ( "PASV: Bad host/port combination" ) ) ;
579
+ getPasvPort ( res . text , function ( err , res ) {
580
+ if ( err ) return callback ( err ) ;
560
581
561
- var host = pasvRes [ 0 ] ;
562
- var port = pasvRes [ 1 ] ;
563
- var socket = Net . createConnection ( port , host ) ;
564
- socket . setTimeout ( timeout || TIMEOUT ) ;
565
- callback ( null , socket ) ;
582
+ var socket = Net . createConnection ( res . port , res . host ) ;
583
+ socket . setTimeout ( timeout || TIMEOUT ) ;
584
+ callback ( null , socket ) ;
585
+ } ) ;
566
586
} ) ;
567
587
} ;
568
588
0 commit comments