Is there a way to send a message to Client to stop sending data while uploading files?
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 5.8k
- Forks
- 734
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no repository files or tests. Start by reading the ssh2/ssh2-streams SFTP WRITE handling and the Node.js Readable backpressure behavior, then determine whether upload pause/resume is supported by the protocol or needs an API change. Done would be a maintainer-confirmed behavior and corresponding implementation or documentation plan.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend, networking
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 18/100