Skip to content

Commit 9eeb114

Browse files
authored
fix(gridfs-stream): honor chunk size
Fixes NODE-1219
1 parent ec53029 commit 9eeb114

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

lib/gridfs-stream/download.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,12 @@ function doRead(_this) {
234234
_this.s.bytesToSkip = 0;
235235
}
236236

237-
if (expectedN === _this.s.expectedEnd && _this.s.bytesToTrim != null) {
238-
sliceEnd = _this.s.bytesToTrim;
239-
}
240-
241-
// If the remaining amount of data left is < chunkSize read the right amount of data
242-
if (_this.s.options.end && _this.s.options.end - _this.s.bytesToSkip < buf.length) {
243-
sliceEnd = _this.s.options.end - _this.s.bytesToSkip;
237+
const atEndOfStream = expectedN === _this.s.expectedEnd - 1;
238+
const bytesLeftToRead = _this.s.options.end - _this.s.bytesToSkip;
239+
if (atEndOfStream && _this.s.bytesToTrim != null) {
240+
sliceEnd = _this.s.file.chunkSize - _this.s.bytesToTrim;
241+
} else if (_this.s.options.end && bytesLeftToRead < doc.data.length()) {
242+
sliceEnd = bytesLeftToRead;
244243
}
245244

246245
if (sliceStart != null || sliceEnd != null) {

test/functional/gridfs_stream_tests.js

+42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const stream = require('stream');
34
const crypto = require('crypto'),
45
EJSON = require('mongodb-extjson'),
56
fs = require('fs'),
@@ -1036,6 +1037,47 @@ describe('GridFS Stream', function() {
10361037
}
10371038
});
10381039

1040+
it('should use chunkSize for download', {
1041+
metadata: { requires: { topology: ['single'] } },
1042+
1043+
// The actual test we wish to run
1044+
test: function(done) {
1045+
if (typeof stream.pipeline !== 'function') {
1046+
this.skip();
1047+
}
1048+
1049+
const configuration = this.configuration;
1050+
const GridFSBucket = configuration.require.GridFSBucket;
1051+
1052+
const client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
1053+
client.connect(function(err, client) {
1054+
const db = client.db(configuration.db);
1055+
const bucket = new GridFSBucket(db, { bucketName: 'gridfs' });
1056+
1057+
const uploadStream = bucket.openUploadStream('test');
1058+
uploadStream.end(Buffer.alloc(40 * 1024 * 1024), err => {
1059+
expect(err).to.be.null;
1060+
const range = {
1061+
start: 35191617,
1062+
end: 35192831
1063+
};
1064+
const downloadStream = bucket.openDownloadStreamByName('test', range);
1065+
const outputStream = fs.createWriteStream('output');
1066+
stream.pipeline(downloadStream, outputStream, err => {
1067+
expect(err).to.not.exist;
1068+
client.close(() => {
1069+
fs.stat('output', (err, stats) => {
1070+
expect(err).to.be.null;
1071+
expect(range.end - range.start).to.equal(stats.size);
1072+
done();
1073+
});
1074+
});
1075+
});
1076+
});
1077+
});
1078+
}
1079+
});
1080+
10391081
var UPLOAD_SPEC = require('./spec/gridfs/gridfs-upload.json');
10401082
UPLOAD_SPEC.tests.forEach(function(specTest) {
10411083
(function(testSpec) {

0 commit comments

Comments
 (0)