Skip to content

Commit

Permalink
fix(serializeWithBufferAndIndex): write documents to start of interme…
Browse files Browse the repository at this point in the history
…diate buffer
  • Loading branch information
kmahar authored and mbroadst committed Feb 26, 2018
1 parent 8fbfc04 commit b4e4ac5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/bson/bson.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ BSON.prototype.serializeWithBufferAndIndex = function(object, finalBuffer, optio
buffer,
object,
checkKeys,
startIndex || 0,
0,
0,
serializeFunctions,
ignoreUndefined
);
buffer.copy(finalBuffer, startIndex, 0, serializationIndex);

// Return the index
return serializationIndex - 1;
return startIndex + serializationIndex - 1;
};

/**
Expand Down
38 changes: 37 additions & 1 deletion test/node/serialize_with_buffer_tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var createBSON = require('../utils'),
let createBSON = require('../utils'),
expect = require('chai').expect;

describe('serializeWithBuffer', function() {
Expand Down Expand Up @@ -28,4 +28,40 @@ describe('serializeWithBuffer', function() {
expect({ a: 1 }).to.deep.equal(doc);
done();
});

it('correctly serialize 3 different docs into buffer using serializeWithBufferAndIndex', function(
done
) {
const MAXSIZE = 1024 * 1024 * 17;
var bson = createBSON();
let bf = new Buffer(MAXSIZE);

const data = [
{
a: 1,
b: new Date('2019-01-01')
},
{
a: 2,
b: new Date('2019-01-02')
},
{
a: 3,
b: new Date('2019-01-03')
}
];

let idx = 0;
data.forEach(item => {
idx =
bson.serializeWithBufferAndIndex(item, bf, {
index: idx
}) + 1;
});

expect(bson.deserialize(bf.slice(0, 23))).to.deep.equal(data[0]);
expect(bson.deserialize(bf.slice(23, 46))).to.deep.equal(data[1]);
expect(bson.deserialize(bf.slice(46, 69))).to.deep.equal(data[2]);
done();
});
});

0 comments on commit b4e4ac5

Please sign in to comment.