Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Fixed index passing issue for serializeWithBufferAndIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Nov 28, 2016
1 parent 0315a93 commit 49db5aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions ext/bson.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,15 @@ NAN_METHOD(BSON::SerializeWithBufferAndIndex) {
return Nan::ThrowError("ignoreUndefined argument must be a boolean");
}
}

// Check if we have the checkKeys variable
if(NanHas(options, "index")) {
if(NanGet(options, "index")->IsUint32()) {
index = Nan::To<uint32_t>(NanGet(options, "index")).FromJust();
} else {
return Nan::ThrowError("index argument must be an integer");
}
}
}

try {
Expand Down
14 changes: 11 additions & 3 deletions test/node/serialize_with_buffer_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ var BSON = require('../..'),
f = require('util').format,
assert = require('assert');

var bson = new BSON();
function createBSON() {
return new BSON([BSON.Binary, BSON.Code, BSON.DBRef, BSON.Decimal128,
BSON.Double, BSON.Int32, BSON.Long, BSON.Map, BSON.MaxKey, BSON.MinKey,
BSON.ObjectId, BSON.BSONRegExp, BSON.Symbol, BSON.Timestamp]);
}

/**
* @ignore
*/
exports['correctly serialize into buffer using serializeWithBufferAndIndex'] = function(test) {
var bson = createBSON();
// Create a buffer
var b = new Buffer(256);
// Serialize from index 0
var r = bson.serializeWithBufferAndIndex({a:1}, false, b, 0, false, false);
var r = bson.serializeWithBufferAndIndex({a:1}, b);
test.equal(11, r);

// Serialize from index r+1
var r = bson.serializeWithBufferAndIndex({a:1}, false, b, r + 1, false, false);
var r = bson.serializeWithBufferAndIndex({a:1}, b, {
index: r + 1
});
console.log("index :: " + r)
test.equal(23, r);

// Deserialize the buffers
Expand Down

0 comments on commit 49db5aa

Please sign in to comment.