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) { ...@@ -21,15 +21,11 @@ module.exports = function createReadStream(path, options, cb) {
return cb(err); return cb(err);
} }
var offset = new BigInt(file.EndofFile.length, options.start || 0); var offset = options.start || 0;
var fileSize = BigInt.fromBuffer(file.EndofFile); var end = BigInt.fromBuffer(file.EndofFile).toNumber();
var end = options.end; if (options.end < end) {
if (end != null) { end = options.end + 1; // end option is inclusive
end = new BigInt(8, end + 1);
if (end.gt(fileSize)) {
fileSize = end;
}
} }
var close = request.bind(undefined, 'close', file, connection); var close = request.bind(undefined, 'close', file, connection);
...@@ -49,9 +45,9 @@ module.exports = function createReadStream(path, options, cb) { ...@@ -49,9 +45,9 @@ module.exports = function createReadStream(path, options, cb) {
return; return;
} }
if (offset.ge(fileSize)) { if (offset >= end) {
return close(function() { return close(function() {
this.push(null); stream.push(null);
}); });
} }
...@@ -60,21 +56,18 @@ module.exports = function createReadStream(path, options, cb) { ...@@ -60,21 +56,18 @@ module.exports = function createReadStream(path, options, cb) {
'read', 'read',
{ {
FileId: file.FileId, FileId: file.FileId,
Length: Math.min(maxPacketSize, size), Length: Math.min(maxPacketSize, size, end - offset),
Offset: offset.toBuffer(), Offset: new BigInt(8, offset).toBuffer(),
}, },
connection, connection,
function(err, content) { function(err, content) {
try { running = false;
if (err != null) { if (err != null) {
return process.nextTick(stream.emit.bind(stream, 'error', err)); return process.nextTick(stream.emit.bind(stream, 'error', err));
}
offset = offset.add(content.length);
stream.push(content);
} finally {
running = false;
} }
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