0

I am using bson-ext as a native binding.

Building the native .node file has worked well so far. I installed GCC, make and ran node-gyp configure and node-gyp build. I am now including the file in my node app like so:

var bson =  require('./bson/build/Release/bson.node');
var object = bson.BSON.prototype;
console.log(object.serialize('hey'))

The problem is, I am getting this error:

console.log(object.serialize({test: "turkey"}))

TypeError: Illegal invocation

I get confused here because when I do console.log(object)

It outputs:

BSON {
calculateObjectSize: [Function: calculateObjectSize],
serialize: [Function: serialize],
serializeWithBufferAndIndex: [Function: serializeWithBufferAndIndex],
deserialize: [Function: deserialize],
deserializeStream: [Function: deserializeStream] }

These are the native functions from C++ that were built to bson.node, right? But, now I am just not sure how to call them. I have checked their github page as well for documentation, but with no luck.

Edit: The following:

var bson =  require('./bson/build/Release/bson');
console.log(bson.BSON)

outputs:

[Function: BSON]

Next, I run:

console.log(bson.BSON().serialize({turkey: 'hey'}));

But receive:

console.log(bson.BSON().serialize({turkey: 'hey'}));

TypeError: First argument passed in must be an array of types

Then I run:

 console.log(bson.BSON(['BSON']).serialize({turkey: 'hey'}));

And receive:

Segmentation fault

Community
  • 1
  • 1
NiCk Newman
  • 1,716
  • 7
  • 23
  • 48

1 Answers1

0

I found a solution inside the mongodb package.

(Specifically at \mongodb\node_modules\mongodb-core\lib\replset.js @ line 15).

They are using .native()

BSON = require('bson').native().BSON;
var newBSON = new bson();
console.log(newBSON.serialize('hello'));

Which does work:

<Buffer 32 00 00 00 02 30 00 02 00 00 00 68 00 02 31 00 02 00 00 00 65 00 02 32 00 02 00 00 00 6c 00 02 33 00 02 00 00 00 6c 00 02 34 00 02 00 00 00 6f 00 00>

However, (Slightly off-topic, but, I made a question yesterday Why is JSON faster than BSON, and the conclusion was native code vs non-native code. But, now, even after installing bson natively, the performance results are roughly the same... bummer.

Community
  • 1
  • 1
NiCk Newman
  • 1,716
  • 7
  • 23
  • 48