Skip to content

Commit

Permalink
implement stream backpressure
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiez authored and julien-f committed Apr 11, 2021
1 parent 80b74f0 commit d4d03e6
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/api/createReadStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,10 @@ module.exports = function createReadStream(path, options, cb) {
});
};
}
var running = false;
stream._read = function(size) {
if (running) {
return;
}

if (offset >= end) {
return shouldClose
? close(function() {
stream.push(null);
})
: stream.push(null);
}
var running = false;

running = true;
function read(size) {
request(
'read',
{
Expand All @@ -69,15 +58,37 @@ module.exports = function createReadStream(path, options, cb) {
},
connection,
function(err, content) {
running = false;
if (err != null) {
return process.nextTick(stream.emit.bind(stream, 'error', err));
}

offset += content.length;
stream.push(content);
if (stream.push(content)) {
if (end - offset === 0) {
running = false;
stream.push(null);
} else read(size);
} else {
running = false;
}
}
);
}
stream._read = function(size) {
if (running) {
return;
}

if (offset >= end) {
return shouldClose
? close(function() {
stream.push(null);
})
: stream.push(null);
} else {
running = true;
read(size);
}
};
cb(null, stream);
}
Expand Down

0 comments on commit d4d03e6

Please sign in to comment.