Skip to content

Commit

Permalink
fix(GridFS): fix TypeError: doc.data.length is not a function
Browse files Browse the repository at this point in the history
When using the promoteBuffers connect option, `doc.data` is already a node.js Buffer.
So trying to call `length()` like for `BSON.Binary` fails.
Fixed simply by checking `Buffer.isBuffer(doc.data)` type.
  • Loading branch information
guymguym authored and daprahamian committed Nov 16, 2017
1 parent f4e85c6 commit 811de0c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/gridfs-stream/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,27 @@ function doRead(_this) {
return __handleError(_this, new Error(errmsg));
}

if (doc.data.length() !== expectedLength) {
var buf = Buffer.isBuffer(doc.data) ? doc.data : doc.data.buffer;

if (buf.length !== expectedLength) {
if (bytesRemaining <= 0) {
errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n;
return __handleError(_this, new Error(errmsg));
}

errmsg = 'ChunkIsWrongSize: Got unexpected length: ' +
doc.data.length() + ', expected: ' + expectedLength;
buf.length + ', expected: ' + expectedLength;
return __handleError(_this, new Error(errmsg));
}

_this.s.bytesRead += doc.data.length();
_this.s.bytesRead += buf.length;

if (doc.data.buffer.length === 0) {
if (buf.length === 0) {
return _this.push(null);
}

var sliceStart = null;
var sliceEnd = null;
var buf = doc.data.buffer;

if (_this.s.bytesToSkip != null) {
sliceStart = _this.s.bytesToSkip;
Expand All @@ -241,7 +242,7 @@ function doRead(_this) {

// 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) < doc.data.length()
(_this.s.options.end - _this.s.bytesToSkip) < buf.length
)) {
sliceEnd = (_this.s.options.end - _this.s.bytesToSkip);
}
Expand Down

0 comments on commit 811de0c

Please sign in to comment.