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

Commit

Permalink
chore(release): 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed May 25, 2021
1 parent 1273e72 commit 4cde30a
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 70 deletions.
4 changes: 3 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Change Log
# Changelog

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [4.0.0](https://github.com/mongodb-js/bson-ext/compare/v2.0.5...v4.0.0) (2021-05-25)

<a name="2.0.5"></a>
## [2.0.5](https://github.com/mongodb-js/bson-ext/compare/v2.0.4...v2.0.5) (2020-07-31)

Expand Down
191 changes: 124 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,121 +1,178 @@
# BSON parser
# BSON-EXT

_A BSON parser Node.JS native addon._

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](http://bsonspec.org).

This module is a C++ addon for Node.js that implements the BSON parsing and serialization outside of the JavaScript environment in order to increase speed and memory efficiency for certain workloads. It uses the same types as the pure JavaScript implementation, which can be found at [mongodb/js-bson](https://github.com/mongodb/js-bson).

## Usage
**NOTE: bson-ext version 4+ works with js-bson version 4+ and the mongodb driver version 4+.**

To build a new version perform the following operation.
## Installation

```sh
npm install bson-ext
```
npm install
npm run build
```

## Usage

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

```js
// Get BSON parser class
var BSON = require('bson-ext')
const BSON = require('bson-ext')
// Get the Long type
var Long = BSON.Long;
// Create a bson parser instance, passing in all the types
// This is needed so the C++ parser has references to the classes and can
// use them to serialize and deserialize the types.
var bson = 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]);
const Long = BSON.Long;

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

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

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

## Installation
## Compiling

To build a new version perform the following operation.

`npm install bson-ext`
```sh
npm install
npm run build
```

## API

### BSON types

For all BSON types documentation, please refer to the documentation for the [MongoDB Node.js driver](https://github.com/mongodb/node-mongodb-native).

### BSON serialization and deserialiation

**`new BSON()`** - Creates a new BSON serializer/deserializer you can use to serialize and deserialize BSON.
For all BSON types documentation, please refer to the documentation for the [MongoDB Node.js driver](http://mongodb.github.io/node-mongodb-native/4.0).

#### 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.
```typescript
/**
* The BSON library accepts plain javascript objects.
* It serializes to BSON by iterating the keys
*/
interface Document {
[key: string]: any;
}

interface SerializeOptions {
/** the serializer will check if keys are valid. */
checkKeys?: boolean;
/** serialize the javascript functions **(default:false)**. */
serializeFunctions?: boolean;
/** serialize will not emit undefined fields **(default:true)** */
ignoreUndefined?: boolean;
}

/**
* Serialize a Javascript object.
*
* @param object - the Javascript object to serialize.
* @returns Buffer object containing the serialized object.
*/
function serialize(object: Document, options?: SerializeOptions): Buffer;
```

#### 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} 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.
```typescript
/**
* Serialize a Javascript object using a predefined Buffer and index into the buffer,
* useful when pre-allocating the space for serialization.
*
* @param object - the Javascript object to serialize.
* @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.
* @returns the index pointing to the last written byte in the buffer.
*/
function serializeWithBufferAndIndex(object: Document, finalBuffer: Buffer, options?: SerializeOptions): number;
```

#### 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.
```typescript
interface CalculateObjectSizeOptions {
/** serialize the javascript functions **(default:false)**. */
serializeFunctions?: boolean;
/** serialize will not emit undefined fields **(default:true)** */
ignoreUndefined?: boolean;
}


/**
* Calculate the bson size for a passed in Javascript object.
*
* @param object - the Javascript object to calculate the BSON byte size for
* @returns size of BSON object in bytes
* @public
*/
function calculateObjectSize(object: Document, options?: CalculateObjectSizeOptions): number;
```

#### 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 {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
```typescript
interface DeserializeOptions {
/** evaluate functions in the BSON document scoped to the object deserialized. */
evalFunctions?: boolean;
/** cache evaluated functions for reuse. */
cacheFunctions?: boolean;
/** when deserializing a Long will fit it into a Number if it's smaller than 53 bits */
promoteLongs?: boolean;
/** when deserializing a Binary will return it as a node.js Buffer instance. */
promoteBuffers?: boolean;
/** when deserializing will promote BSON values to their Node.js closest equivalent types. */
promoteValues?: boolean;
/** allow to specify if there what fields we wish to return as unserialized raw buffer. */
fieldsAsRaw?: Document;
/** return BSON regular expressions as BSONRegExp instances. */
bsonRegExp?: boolean;
/** allows the buffer to be larger than the parsed BSON object */
allowObjectSmallerThanBufferSize?: boolean;
/** Offset into buffer to begin reading document from */
index?: number;
}

/**
* Deserialize data as BSON.
*
* @param buffer - the buffer containing the serialized set of BSON documents.
* @returns returns the deserialized Javascript Object.
* @public
*/
function deserialize(buffer: Buffer | ArrayBufferView | ArrayBuffer, options?: DeserializeOptions): Document;
```

#### 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} 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 {Object} returns the deserialized JavaScript Object.
```typescript
/**
* Deserialize stream data as BSON documents.
*
* @param data - the buffer containing the serialized set of BSON documents.
* @param startIndex - the start index in the data Buffer where the deserialization is to start.
* @param numberOfDocuments - number of documents to deserialize.
* @param documents - an array where to store the deserialized documents.
* @param docStartIndex - the index in the documents array from where to start inserting documents.
* @param options - additional options used for the deserialization.
* @returns next index in the buffer after deserialization **x** numbers of documents.
* @public
*/
function deserializeStream(data: Buffer | ArrayBufferView | ArrayBuffer, startIndex: number, numberOfDocuments: number, documents: Document[], docStartIndex: number, options: DeserializeOptions): number;
```
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bson",
"parser"
],
"version": "2.0.5",
"version": "4.0.0",
"author": "Christian Amor Kvalheim <christkv@gmail.com>",
"contributors": [],
"repository": {
Expand Down

0 comments on commit 4cde30a

Please sign in to comment.