ssh2-streams icon indicating copy to clipboard operation
ssh2-streams copied to clipboard

Is there a way to send a message to Client to stop sending data while uploading files?

Open ishan111111 opened this issue 3 years ago • 0 comments

We have a server based on ssh2 and ssh2-streams package. During uploading files, we are pushing data to a readable stream which is consumed by some other method. Is there a way to communicate to client to stop sending any more data when our readable stream's buffer is full?

I have observed that if we don't send the OK response from the write event handler from server, the client stops sending data after like 60 chunks and waits for OK response for corresponding requestId's before sending any more data.

In the previous version of ssh2 , we were doing something like this:

const myReadable = new MyReadable({}, this.sftpStream);
this.sftpStream.on('WRITE', (requestId, handle, offset, data) => { 
   myReadable.pushData(data);

  this.sftpStream.status(requestId, SFTP_STATUS_CODE.OK) 

 });
 class MyReadable extends stream.Readable {

    constructor(options, sftpStream) {
        super(options);
        this.sftpStream = sftpStream;
    }

    pushData = (data) => {
        if (!this.push(data) && !this.sftpStream.isPaused()) {
            this.sftpStream.pause();
        }
    }
    
    _read() {
        if (this.sftpStream.isPaused()) {
            this.sftpStream.resume();
        }
    }
}

I am not sure but the above code must have caused the sftpStream to go in paused mode when calling this.sftpStream.pause() and it might have stopped sending response back to client until this.sftpStream.resume() is called, switching back the sftpStream into flowing mode.

Since the new version of ssh2 is released, we don't have pause and resume methods available on this.sftpStream. We have modified the code something like this:

const myReadable = new MyReadable({}, this.sftpStream);
this.sftpStream.on('WRITE', (requestId, handle, offset, data) => {
    
    if (!myReadable.pushData(data))
    return myReadable.pushToPending(requestId)


 
    this.sftpStream.status(requestId, SFTP_STATUS_CODE.OK)
});
class MyReadable extends stream.Readable {

    pendingResponse = [];


    constructor(options, sftpStream) {
        super(options);
        this.sftpStream = sftpStream;
    }

    pushData = (data) => {
       return this.push(data);
    }


    _read() {
      while (this.pendingResponse.length > 0) {
          const requestId = this.pendingResponse.shift();
          this.sftpStream.status(requestId, SFTP_STATUS_CODE.OK);
      }
    }

    pushToPending(requestId) {
        this.pendingResponse.push(requestId)
    }
}

Can you please give me suggestion on if it is a good way to handle such scenario or some better way exists? Is there a way to send a message from server to client to pause and resume the upload?

Thanks

ishan111111 avatar Nov 06 '21 15:11 ishan111111