Commit c26cd908 authored by Julien Fontanet's avatar Julien Fontanet

fix(createReadStream): various issues

parent 83edb5d7
......@@ -21,15 +21,11 @@ module.exports = function createReadStream(path, options, cb) {
return cb(err);
}
var offset = new BigInt(file.EndofFile.length, options.start || 0);
var fileSize = BigInt.fromBuffer(file.EndofFile);
var offset = options.start || 0;
var end = BigInt.fromBuffer(file.EndofFile).toNumber();
var end = options.end;
if (end != null) {
end = new BigInt(8, end + 1);
if (end.gt(fileSize)) {
fileSize = end;
}
if (options.end < end) {
end = options.end + 1; // end option is inclusive
}
var close = request.bind(undefined, 'close', file, connection);
......@@ -49,9 +45,9 @@ module.exports = function createReadStream(path, options, cb) {
return;
}
if (offset.ge(fileSize)) {
if (offset >= end) {
return close(function() {
this.push(null);
stream.push(null);
});
}
......@@ -60,21 +56,18 @@ module.exports = function createReadStream(path, options, cb) {
'read',
{
FileId: file.FileId,
Length: Math.min(maxPacketSize, size),
Offset: offset.toBuffer(),
Length: Math.min(maxPacketSize, size, end - offset),
Offset: new BigInt(8, offset).toBuffer(),
},
connection,
function(err, content) {
try {
if (err != null) {
return process.nextTick(stream.emit.bind(stream, 'error', err));
}
offset = offset.add(content.length);
stream.push(content);
} finally {
running = false;
running = false;
if (err != null) {
return process.nextTick(stream.emit.bind(stream, 'error', err));
}
offset += content.length;
stream.push(content);
}
);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment