sergi/jsftp

Getting multiple files

Closed this issue · 3 comments

Hi there,

I know this is some async condundrum I have gotten myself into, but I am trying to download multiple files, inside the callback from the .list method.

So something like this:

  ftp = new JSFTP(conf.FTPConfig)
  ftp.ls(conf.remoteFolder, (err, fileList) => {
    if (err) return console.error(err)

    for (let i=0; i<fileList.length; i++){
      file = fileList[i]
      ftp.get(file.name, `./Downloaded/${file.name}`, err => {
        if (err) return console.error(err)
        console.log(`${file.name} copied.`)
      })
    }
  })

If I don't use a loop and only download one file, everything works fine.
But with a loop, I keep getting the following errors and I don't get any of the files (except one or two empty file placeholders):

(In short): Error: 503 Bad sequence of commands, and further below: 'Probably trying a PASV operation while one is in progress

It looks like it kicks off all my .get calls inside the loop (obviously without waiting for any of them to finish, which is fine), but then fails to actually execute all the .get calls this way?
Should they not just run in parallel, asynchronously?

{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11)
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8)
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11)
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8)
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11)
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8)
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11)
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8)
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11)
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8)
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11)
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8)
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: connect ECONNREFUSED 192.168.100.161:61229
    at Object.exports._errnoException (util.js:1007:11)
    at exports._exceptionWithHostPort (util.js:1030:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '192.168.100.161',
  port: 61229,
  msg: 'Probably trying a PASV operation while one is in progress' }

@batjko not sure if it helps but what I did in #192 might what you are looking for. I am having a totally separate issue with the file structure.

@SteveyPugs Yea I did read up on Async and considered using it to make my .get() calls synchronous.
I figure FTP servers don't serve multiple requests on the same connection.
So that's the right way to do it, but as my case was only for a quick interview test, I ended up simply creating a new connection for each file (each iteration of the loop), just to keep parallel .get() requests and get on with it.

Not the most scalable solution perhaps, but then it did get me the job ;)

@batjko glad I could help. If you have any insight on #192 that would good haha.