Skip to content

Commit

Permalink
SSH2Stream: fix decompression of large packets
Browse files Browse the repository at this point in the history
When a zlib decompression stream exceeds the highWaterMark, the
flush() callback won't be called until all of the current
uncompressed data is read.
  • Loading branch information
mscdex committed Apr 29, 2016
1 parent 71bf14b commit 8c0906c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/ssh.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,18 @@ SSH2Stream.prototype._transform = function(chunk, encoding, callback, decomp) {
if (!decomp) {
debug('DEBUG: Parser: Decompressing');
decompress.instance.write(instate.payload);
decompress.instance.flush(Z_PARTIAL_FLUSH, function() {
instate.payload = decompress.instance.read();
var decompBuf = [];
var decompBufLen = 0;
decompress.instance.on('readable', function() {
var buf;
while (buf = this.read()) {
decompBuf.push(buf);
decompBufLen += buf.length;
}
}).flush(Z_PARTIAL_FLUSH, function() {
decompress.instance.removeAllListeners('readable');
instate.payload = Buffer.concat(decompBuf, decompBufLen);
decompBuf = null;
var nextSlice;
if (i === chlen)
nextSlice = EMPTY_BUFFER; // Avoid slicing a zero-length buffer
Expand Down

0 comments on commit 8c0906c

Please sign in to comment.