Skip to content

Commit

Permalink
fix(gridfs-stream): honor chunk size
Browse files Browse the repository at this point in the history
Fixes NODE-1219
  • Loading branch information
kvwalker authored Apr 10, 2019
1 parent ec53029 commit 9eeb114
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/gridfs-stream/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 42 additions & 0 deletions test/functional/gridfs_stream_tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const stream = require('stream');
const crypto = require('crypto'),
EJSON = require('mongodb-extjson'),
fs = require('fs'),
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9eeb114

Please sign in to comment.