Skip to content

Commit b4e4ac5

Browse files
kmaharmbroadst
authored andcommitted
fix(serializeWithBufferAndIndex): write documents to start of intermediate buffer
1 parent 8fbfc04 commit b4e4ac5

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Diff for: lib/bson/bson.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ BSON.prototype.serializeWithBufferAndIndex = function(object, finalBuffer, optio
9696
buffer,
9797
object,
9898
checkKeys,
99-
startIndex || 0,
99+
0,
100100
0,
101101
serializeFunctions,
102102
ignoreUndefined
103103
);
104104
buffer.copy(finalBuffer, startIndex, 0, serializationIndex);
105105

106106
// Return the index
107-
return serializationIndex - 1;
107+
return startIndex + serializationIndex - 1;
108108
};
109109

110110
/**

Diff for: test/node/serialize_with_buffer_tests.js

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

3-
var createBSON = require('../utils'),
3+
let createBSON = require('../utils'),
44
expect = require('chai').expect;
55

66
describe('serializeWithBuffer', function() {
@@ -28,4 +28,40 @@ describe('serializeWithBuffer', function() {
2828
expect({ a: 1 }).to.deep.equal(doc);
2929
done();
3030
});
31+
32+
it('correctly serialize 3 different docs into buffer using serializeWithBufferAndIndex', function(
33+
done
34+
) {
35+
const MAXSIZE = 1024 * 1024 * 17;
36+
var bson = createBSON();
37+
let bf = new Buffer(MAXSIZE);
38+
39+
const data = [
40+
{
41+
a: 1,
42+
b: new Date('2019-01-01')
43+
},
44+
{
45+
a: 2,
46+
b: new Date('2019-01-02')
47+
},
48+
{
49+
a: 3,
50+
b: new Date('2019-01-03')
51+
}
52+
];
53+
54+
let idx = 0;
55+
data.forEach(item => {
56+
idx =
57+
bson.serializeWithBufferAndIndex(item, bf, {
58+
index: idx
59+
}) + 1;
60+
});
61+
62+
expect(bson.deserialize(bf.slice(0, 23))).to.deep.equal(data[0]);
63+
expect(bson.deserialize(bf.slice(23, 46))).to.deep.equal(data[1]);
64+
expect(bson.deserialize(bf.slice(46, 69))).to.deep.equal(data[2]);
65+
done();
66+
});
3167
});

0 commit comments

Comments
 (0)