Gathering detailed insights and metrics for bson-ext
Gathering detailed insights and metrics for bson-ext
Gathering detailed insights and metrics for bson-ext
Gathering detailed insights and metrics for bson-ext
npm install bson-ext
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (63.56%)
C++ (31.19%)
Perl (3.24%)
Shell (1.39%)
Python (0.37%)
Batchfile (0.17%)
Makefile (0.07%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
42 Stars
150 Commits
25 Forks
12 Watchers
18 Branches
25 Contributors
Updated on Mar 26, 2025
Latest Version
4.0.3
Package Id
bson-ext@4.0.3
Unpacked Size
131.33 kB
Size
29.45 kB
File Count
18
NPM Version
8.1.4
Node Version
14.18.2
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
6
A BSON parser Node.JS native addon.
BSON is short for Binary JSON and is the binary-encoded serialization of JSON-like documents. You can learn more about it in the specification.
While this library is compatible with the mongodb driver version 4+, bson-ext will soon be deprecated and no longer supported. It is strongly recommended that js-bson be used instead.
NOTE: bson-ext version 4+ works with js-bson version 4+ and the mongodb driver version 4+.
1npm install bson-ext
A simple example of how to use BSON in Node.js
:
1// Get BSON parser class 2const BSON = require('bson-ext') 3// Get the Long type 4const Long = BSON.Long; 5 6// Serialize document 7const doc = { long: Long.fromNumber(100) } 8 9// Serialize a document 10const data = BSON.serialize(doc) 11console.log('data:', data) 12 13// Deserialize the resulting Buffer 14var docRoundTrip = bson.deserialize(data) 15console.log('docRoundTrip:', docRoundTrip)
To build a new version perform the following operation.
1npm install 2npm run build
For all BSON types documentation, please refer to the documentation for the MongoDB Node.js driver.
The BSON serialize
method takes a JavaScript object and an optional options object and returns a Node.js Buffer.
1/** 2 * The BSON library accepts plain javascript objects. 3 * It serializes to BSON by iterating the keys 4 */ 5interface Document { 6 [key: string]: any; 7} 8 9interface SerializeOptions { 10 /** the serializer will check if keys are valid. */ 11 checkKeys?: boolean; 12 /** serialize the javascript functions **(default:false)**. */ 13 serializeFunctions?: boolean; 14 /** serialize will not emit undefined fields **(default:true)** */ 15 ignoreUndefined?: boolean; 16} 17 18/** 19 * Serialize a Javascript object. 20 * 21 * @param object - the Javascript object to serialize. 22 * @returns Buffer object containing the serialized object. 23 */ 24function serialize(object: Document, options?: SerializeOptions): Buffer;
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.
1/** 2 * Serialize a Javascript object using a predefined Buffer and index into the buffer, 3 * useful when pre-allocating the space for serialization. 4 * 5 * @param object - the Javascript object to serialize. 6 * @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object. 7 * @returns the index pointing to the last written byte in the buffer. 8 */ 9function serializeWithBufferAndIndex(object: Document, finalBuffer: Buffer, options?: SerializeOptions): number;
The BSON calculateObjectSize
method takes a JavaScript object and an optional options object and returns the size of the BSON object.
1interface CalculateObjectSizeOptions { 2 /** serialize the javascript functions **(default:false)**. */ 3 serializeFunctions?: boolean; 4 /** serialize will not emit undefined fields **(default:true)** */ 5 ignoreUndefined?: boolean; 6} 7 8 9/** 10 * Calculate the bson size for a passed in Javascript object. 11 * 12 * @param object - the Javascript object to calculate the BSON byte size for 13 * @returns size of BSON object in bytes 14 * @public 15 */ 16function calculateObjectSize(object: Document, options?: CalculateObjectSizeOptions): number;
The BSON deserialize
method takes a Node.js Buffer and an optional options object and returns a deserialized JavaScript object.
1interface DeserializeOptions { 2 /** evaluate functions in the BSON document scoped to the object deserialized. */ 3 evalFunctions?: boolean; 4 /** cache evaluated functions for reuse. */ 5 cacheFunctions?: boolean; 6 /** when deserializing a Long will fit it into a Number if it's smaller than 53 bits */ 7 promoteLongs?: boolean; 8 /** when deserializing a Binary will return it as a node.js Buffer instance. */ 9 promoteBuffers?: boolean; 10 /** when deserializing will promote BSON values to their Node.js closest equivalent types. */ 11 promoteValues?: boolean; 12 /** allow to specify if there what fields we wish to return as unserialized raw buffer. */ 13 fieldsAsRaw?: Document; 14 /** return BSON regular expressions as BSONRegExp instances. */ 15 bsonRegExp?: boolean; 16 /** allows the buffer to be larger than the parsed BSON object */ 17 allowObjectSmallerThanBufferSize?: boolean; 18 /** Offset into buffer to begin reading document from */ 19 index?: number; 20} 21 22/** 23 * Deserialize data as BSON. 24 * 25 * @param buffer - the buffer containing the serialized set of BSON documents. 26 * @returns returns the deserialized Javascript Object. 27 * @public 28 */ 29function deserialize(buffer: Buffer | ArrayBufferView | ArrayBuffer, options?: DeserializeOptions): Document;
The BSON deserializeStream
method takes a Node.js Buffer, startIndex
and allow more control over deserialization of a Buffer containing concatenated BSON documents.
1/** 2 * Deserialize stream data as BSON documents. 3 * 4 * @param data - the buffer containing the serialized set of BSON documents. 5 * @param startIndex - the start index in the data Buffer where the deserialization is to start. 6 * @param numberOfDocuments - number of documents to deserialize. 7 * @param documents - an array where to store the deserialized documents. 8 * @param docStartIndex - the index in the documents array from where to start inserting documents. 9 * @param options - additional options used for the deserialization. 10 * @returns next index in the buffer after deserialization **x** numbers of documents. 11 * @public 12 */ 13function deserializeStream(data: Buffer | ArrayBufferView | ArrayBuffer, startIndex: number, numberOfDocuments: number, documents: Document[], docStartIndex: number, options: DeserializeOptions): number;
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 13/30 approved changesets -- score normalized to 4
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
Project has not signed or included provenance with any releases.
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
29 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More