diff --git a/lib/gridfs-stream/download.js b/lib/gridfs-stream/download.js index 9012602670a..4583a943ed4 100644 --- a/lib/gridfs-stream/download.js +++ b/lib/gridfs-stream/download.js @@ -234,13 +234,12 @@ function doRead(_this) { _this.s.bytesToSkip = 0; } - if (expectedN === _this.s.expectedEnd && _this.s.bytesToTrim != null) { - sliceEnd = _this.s.bytesToTrim; - } - - // If the remaining amount of data left is < chunkSize read the right amount of data - if (_this.s.options.end && _this.s.options.end - _this.s.bytesToSkip < buf.length) { - sliceEnd = _this.s.options.end - _this.s.bytesToSkip; + const atEndOfStream = expectedN === _this.s.expectedEnd - 1; + const bytesLeftToRead = _this.s.options.end - _this.s.bytesToSkip; + if (atEndOfStream && _this.s.bytesToTrim != null) { + sliceEnd = _this.s.file.chunkSize - _this.s.bytesToTrim; + } else if (_this.s.options.end && bytesLeftToRead < doc.data.length()) { + sliceEnd = bytesLeftToRead; } if (sliceStart != null || sliceEnd != null) { diff --git a/test/functional/gridfs_stream_tests.js b/test/functional/gridfs_stream_tests.js index 5c87e6294c1..45c8bcc4e71 100644 --- a/test/functional/gridfs_stream_tests.js +++ b/test/functional/gridfs_stream_tests.js @@ -1,5 +1,6 @@ 'use strict'; +const stream = require('stream'); const crypto = require('crypto'), EJSON = require('mongodb-extjson'), fs = require('fs'), @@ -1036,6 +1037,47 @@ describe('GridFS Stream', function() { } }); + it('should use chunkSize for download', { + metadata: { requires: { topology: ['single'] } }, + + // The actual test we wish to run + test: function(done) { + if (typeof stream.pipeline !== 'function') { + this.skip(); + } + + const configuration = this.configuration; + const GridFSBucket = configuration.require.GridFSBucket; + + const client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 }); + client.connect(function(err, client) { + const db = client.db(configuration.db); + const bucket = new GridFSBucket(db, { bucketName: 'gridfs' }); + + const uploadStream = bucket.openUploadStream('test'); + uploadStream.end(Buffer.alloc(40 * 1024 * 1024), err => { + expect(err).to.be.null; + const range = { + start: 35191617, + end: 35192831 + }; + const downloadStream = bucket.openDownloadStreamByName('test', range); + const outputStream = fs.createWriteStream('output'); + stream.pipeline(downloadStream, outputStream, err => { + expect(err).to.not.exist; + client.close(() => { + fs.stat('output', (err, stats) => { + expect(err).to.be.null; + expect(range.end - range.start).to.equal(stats.size); + done(); + }); + }); + }); + }); + }); + } + }); + var UPLOAD_SPEC = require('./spec/gridfs/gridfs-upload.json'); UPLOAD_SPEC.tests.forEach(function(specTest) { (function(testSpec) {