npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongodb-stitch-bson

v1.0.1

Published

MongoDB BSON library for Stitch

Downloads

12

Readme

BSON parser

BSON is short for Bin­ary JSON and is the bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. You can learn more about it in the specification.

This browser version of the BSON parser is compiled using rollup and the current version is pre-compiled in the dist directory.

This is the default BSON parser, however, there is a C++ Node.js addon version as well that does not support the browser. It can be found at mongod-js/bson-ext.

Usage

To build a new version perform the following operations:

npm install
npm run build

A simple example of how to use BSON in the browser:

<script src="./dist/bson.bundle.js"></script>

<script>
  function start() {
    // Get the Long type
    var Long = BSON.Long;
    // Create a bson parser instance
    var bson = new BSON();

    // Serialize document
    var doc = { long: Long.fromNumber(100) }

    // Serialize a document
    var data = bson.serialize(doc)
    // De serialize it again
    var doc_2 = bson.deserialize(data)
  }
</script>

A simple example of how to use BSON in Node.js:

// Get BSON parser class
var BSON = require('mongodb-stitch-bson')
// Get the Long type
var Long = BSON.Long;
// Create a bson parser instance
var bson = new BSON();

// Serialize document
var doc = { long: Long.fromNumber(100) }

// Serialize a document
var data = bson.serialize(doc)
console.log('data:', data)

// Deserialize the resulting Buffer
var doc_2 = bson.deserialize(data)
console.log('doc_2:', doc_2)

A simple example of how to use BSON in ES6:

// Get BSON parser class
import BSON from "mongodb-stitch-bson";
// Get the Long type
var Long = BSON.Long;
// Create a bson parser instance
var bson = new BSON();

// Serialize document
var doc = { long: Long.fromNumber(100) }

// Serialize a document
var data = bson.serialize(doc)
console.log('data:', data)

// Deserialize the resulting Buffer
var doc_2 = bson.deserialize(data)
console.log('doc_2:', doc_2)

Installation

npm install bson

API

BSON types

For all BSON types documentation, please refer to the documentation for the MongoDB Node.js driver.

BSON serialization and deserialiation

new BSON() - Creates a new BSON serializer/deserializer you can use to serialize and deserialize BSON.

BSON.serialize

The BSON serialize method takes a JavaScript object and an optional options object and returns a Node.js Buffer.

  • BSON.serialize(object, options)
    • @param {Object} object the JavaScript object to serialize.
    • @param {Boolean} [options.checkKeys=false] the serializer will check if keys are valid.
    • @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
    • @param {Boolean} [options.ignoreUndefined=true]
    • @return {Buffer} returns a Buffer instance.

BSON.serializeWithBufferAndIndex

The BSON serializeWithBufferAndIndex method takes an object, a target buffer instance and an optional options object and returns the end serialization index in the final buffer.

  • BSON.serializeWithBufferAndIndex(object, buffer, options)
    • @param {Object} object the JavaScript object to serialize.
    • @param {Buffer|Uint8Array} buffer the Buffer you pre-allocated to store the serialized BSON object.
    • @param {Boolean} [options.checkKeys=false] the serializer will check if keys are valid.
    • @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
    • @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields.
    • @param {Number} [options.index=0] the index in the buffer where we wish to start serializing into.
    • @return {Number} returns the index pointing to the last written byte in the buffer.

BSON.calculateObjectSize

The BSON calculateObjectSize method takes a JavaScript object and an optional options object and returns the size of the BSON object.

  • BSON.calculateObjectSize(object, options)
    • @param {Object} object the JavaScript object to serialize.
    • @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
    • @param {Boolean} [options.ignoreUndefined=true]
    • @return {Buffer} returns a Buffer instance.

BSON.deserialize

The BSON deserialize method takes a Node.js Buffer and an optional options object and returns a deserialized JavaScript object.

  • BSON.deserialize(buffer, options)
    • @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
    • @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
    • @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
    • @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
    • @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance.
    • @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
    • @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
    • @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
    • @return {Object} returns the deserialized Javascript Object.

BSON.deserializeStream

The BSON deserializeStream method takes a Node.js Buffer, startIndex and allow more control over deserialization of a Buffer containing concatenated BSON documents.

  • BSON.deserializeStream(buffer, startIndex, numberOfDocuments, documents, docStartIndex, options)
    • @param {Buffer|Uint8Array} buffer the buffer containing the serialized set of BSON documents.
    • @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
    • @param {Number} numberOfDocuments number of documents to deserialize.
    • @param {Array} documents an array where to store the deserialized documents.
    • @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
    • @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
    • @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
    • @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
    • @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
    • @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance.
    • @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
    • @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
    • @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
    • @return {Number} returns the next index in the buffer after deserialization x numbers of documents.

FAQ

Why does undefined get converted to null?

The undefined BSON type has been deprecated for many years, so this library has dropped support for it. Use the ignoreUndefined option (for example, from the driver ) to instead remove undefined keys.

How do I add custom serialization logic?

This library looks for toBSON() functions on every path, and calls the toBSON() function to get the value to serialize.

var bson = new BSON();

class CustomSerialize {
  toBSON() {
    return 42;
  }
}

const obj = { answer: new CustomSerialize() };
// "{ answer: 42 }"
console.log(bson.deserialize(bson.serialize(obj)));

What are the various files in dist?

  • bson.bundle.js is a bundled up version of the library that is suitable for inclusion in an HTML page via a <script> tag.
  • bson.esm.js is a rolled up version of the library that is suitable for interoperation with bundlers that work better with ES modules.
  • bson.browser.esm.js is similar to bson.esm.js but is ultimately intened for consumers producing browser bundles. It also pulls in any browser specific dependencies/code that may be needed.
  • bson.browser.umd.js is similar to the source code of this library but is ultimately intened for consumers producing browser bundlers expecting a UMD format. It also pulls in any browser specific dependencies/code that may be needed.