@@ -17,7 +17,6 @@ var fs = require("fs");
1717var once = require ( "once" ) ;
1818
1919var FTP_PORT = 21 ;
20- var DEBUG_MODE = false ;
2120var TIMEOUT = 10 * 60 * 1000 ;
2221var IDLE_TIME = 30000 ;
2322var NOOP = function ( ) { } ;
@@ -31,14 +30,17 @@ var COMMANDS = [
3130 "chmod" , "size"
3231] ;
3332
34- function getPasvPort ( text ) {
33+ function getPasvPort ( text , cb ) {
3534 var RE_PASV = / ( [ - \d ] + , [ - \d ] + , [ - \d ] + , [ - \d ] + ) , ( [ - \d ] + ) , ( [ - \d ] + ) / ;
3635 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+ }
3839
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+ } ) ;
4244}
4345
4446function runCmd ( cmd ) {
@@ -75,21 +77,37 @@ var Ftp = module.exports = function(cfg) {
7577 // Generate generic methods from parameter names. they can easily be
7678 // overriden if we need special behavior. they accept any parameters given,
7779 // it is the responsability of the user to validate the parameters.
78- this . raw = function ( ) {
80+ this . raw = function ( ) {
7981 return runCmd . apply ( this , arguments ) ;
8082 } . bind ( this ) ;
8183 COMMANDS . forEach ( function ( cmd ) {
8284 this . raw [ cmd ] = runCmd . bind ( this , cmd ) ;
8385 } , this ) ;
8486
87+ var self = this ;
88+ this . on ( 'data' , function ( data ) {
89+ if ( self . debugMode ) {
90+ self . emit ( 'jsftp_debug' , 'response' , data || { } ) ;
91+ }
92+ } ) ;
93+
8594 this . socket = this . _createSocket ( this . port , this . host ) ;
8695} ;
8796
8897util . inherits ( Ftp , EventEmitter ) ;
8998
99+ Ftp . prototype . setDebugMode = function ( debugOn ) {
100+ this . debugMode = ( debugOn !== false ) ;
101+ } ;
102+
90103Ftp . prototype . reemit = function ( event ) {
91104 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+ }
93111} ;
94112
95113Ftp . prototype . _createSocket = function ( port , host , firstAction ) {
@@ -155,6 +173,10 @@ Ftp.prototype.send = function(command) {
155173
156174 this . emit ( "cmdSend" , command ) ;
157175 this . pipeline . write ( command + "\r\n" ) ;
176+
177+ if ( this . debugMode ) {
178+ this . emit ( 'jsftp_debug' , 'user_command' , command || { } ) ;
179+ }
158180} ;
159181
160182Ftp . prototype . nextCmd = function ( ) {
@@ -489,11 +511,11 @@ Ftp.prototype.put = function(from, to, callback) {
489511 return callback ( new Error ( "Local file doesn't exist." ) ) ;
490512
491513 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 ) ;
497519 } ) ;
498520 } ) ;
499521 } else {
@@ -546,23 +568,21 @@ Ftp.prototype.pasvTimeout = function(socket, cb) {
546568 socket . destroy ( ) ;
547569 cb ( new Error ( "Passive socket timeout" ) ) ;
548570 } ) ;
549- }
571+ } ;
550572
551573Ftp . prototype . getPasvSocket = function ( callback ) {
552574 var timeout = this . timeout ;
553575 callback = once ( callback || NOOP ) ;
554576 this . execute ( "pasv" , function ( err , res ) {
555577 if ( err ) return callback ( err ) ;
556578
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 ) ;
560581
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+ } ) ;
566586 } ) ;
567587} ;
568588
0 commit comments