Commit f7d3ad6d authored by Xavier Guimard's avatar Xavier Guimard

New upstream version 3.1.4+~3.0.2+~3.1.3

parent 2ab9515f
<a name="3.0.2"></a>
## [3.0.2](https://github.com/mongodb/js-bson/compare/v3.0.1...v3.0.2) (2018-07-13)
### Bug Fixes
* **revert:** Reverting v3.0.1 ([efb0720](https://github.com/mongodb/js-bson/commit/efb0720))
<a name="3.0.0"></a>
# [3.0.0](https://github.com/mongodb/js-bson/compare/v2.0.8...v3.0.0) (2018-06-13)
### Features
* **ObjectID:** use FNV-1a hash for objectId ([4f545b1](https://github.com/mongodb/js-bson/commit/4f545b1))
* **rollup:** initial commit of rollup-generated bundle ([474b8f7](https://github.com/mongodb/js-bson/commit/474b8f7))
* **rollup:** switch from webpack to rollup for bundling ([98068fa](https://github.com/mongodb/js-bson/commit/98068fa))
<a name="2.0.8"></a>
## [2.0.8](https://github.com/mongodb/js-bson/compare/v2.0.7...v2.0.8) (2018-06-06)
### Bug Fixes
* **readme:** clarify documentation about deserialize methods ([e311056](https://github.com/mongodb/js-bson/commit/e311056))
* **serialization:** normalize function stringification ([21eb0b0](https://github.com/mongodb/js-bson/commit/21eb0b0))
<a name="2.0.7"></a>
## [2.0.7](https://github.com/mongodb/js-bson/compare/v2.0.6...v2.0.7) (2018-05-31)
### Bug Fixes
* **binary:** add type checking for buffer ([cbfb25d](https://github.com/mongodb/js-bson/commit/cbfb25d))
<a name="2.0.6"></a>
## [2.0.6](https://github.com/mongodb/js-bson/compare/v2.0.5...v2.0.6) (2018-04-27)
### Bug Fixes
* **deserializeStream:** allow multiple documents to be deserialized ([6fc5984](https://github.com/mongodb/js-bson/commit/6fc5984)), closes [#244](https://github.com/mongodb/js-bson/issues/244)
<a name="2.0.5"></a>
## [2.0.5](https://github.com/mongodb/js-bson/compare/v2.0.4...v2.0.5) (2018-04-06)
### Bug Fixes
* **regexp:** properly construct new BSONRegExp when constructor called without new ([#242](https://github.com/mongodb/js-bson/issues/242)) ([93ae799](https://github.com/mongodb/js-bson/commit/93ae799))
<a name="2.0.4"></a>
## [2.0.4](https://github.com/mongodb/js-bson/compare/v2.0.3...v2.0.4) (2018-03-12)
<a name="2.0.3"></a>
## [2.0.3](https://github.com/mongodb/js-bson/compare/v2.0.2...v2.0.3) (2018-03-12)
### Features
* **serialization:** support arbitrary sizes for the internal serialization buffer ([a6bd45c](https://github.com/mongodb/js-bson/commit/a6bd45c))
<a name="2.0.2"></a>
## [2.0.2](https://github.com/mongodb/js-bson/compare/v2.0.1...v2.0.2) (2018-03-02)
### Bug Fixes
* make sure all functions are named consistently ([6df9022](https://github.com/mongodb/js-bson/commit/6df9022))
<a name="2.0.1"></a>
## [2.0.1](https://github.com/mongodb/js-bson/compare/v2.0.0...v2.0.1) (2018-02-28)
### Bug Fixes
* **serializer:** ensure RegExp options are alphabetically sorted ([d60659d](https://github.com/mongodb/js-bson/commit/d60659d))
### Features
* **db-ref:** support passing a namespace into a DBRef ctor ([604831b](https://github.com/mongodb/js-bson/commit/604831b))
<a name="2.0.0"></a>
# 2.0.0 (2018-02-26)
### Bug Fixes
* **browser:** fixing browser property in package.json ([095fba9](https://github.com/mongodb/js-bson/commit/095fba9))
* **dbref:** only upgrade objects with allowed $keys to DBRefs ([98eb9e2](https://github.com/mongodb/js-bson/commit/98eb9e2))
* **decimal128:** add basic guard against REDOS attacks ([511ecc4](https://github.com/mongodb/js-bson/commit/511ecc4))
* **Decimal128:** update toString and fromString methods to correctly handle the case of too many significant digits ([25ed43e](https://github.com/mongodb/js-bson/commit/25ed43e))
* **objectid:** if pid is 1, use random value ([e188ae6](https://github.com/mongodb/js-bson/commit/e188ae6))
* **serializeWithBufferAndIndex:** write documents to start of intermediate buffer ([b4e4ac5](https://github.com/mongodb/js-bson/commit/b4e4ac5))
1.0.4 2016-01-11
----------------
- #204 remove Buffer.from as it's partially broken in early 4.x.x. series of node releases.
1.0.3 2016-01-03
----------------
- Fixed toString for ObjectId so it will work with inspect.
1.0.2 2016-01-02
----------------
- Minor optimizations for ObjectID to use Buffer.from where available.
1.0.1 2016-12-06
----------------
- Reverse behavior for undefined to be serialized as NULL. MongoDB 3.4 does not allow for undefined comparisons.
1.0.0 2016-12-06
----------------
- Introduced new BSON API and documentation.
0.5.7 2016-11-18
-----------------
- NODE-848 BSON Regex flags must be alphabetically ordered.
0.5.6 2016-10-19
-----------------
- NODE-833, Detects cyclic dependencies in documents and throws error if one is found.
- Fix(deserializer): corrected the check for (size + index) comparison… (Issue #195, https://github.com/JoelParke).
0.5.5 2016-09-15
-----------------
- Added DBPointer up conversion to DBRef
0.5.4 2016-08-23
-----------------
- Added promoteValues flag (default to true) allowing user to specify if deserialization should be into wrapper classes only.
0.5.3 2016-07-11
-----------------
- Throw error if ObjectId is not a string or a buffer.
0.5.2 2016-07-11
-----------------
- All values encoded big-endian style for ObjectId.
0.5.1 2016-07-11
-----------------
- Fixed encoding/decoding issue in ObjectId timestamp generation.
- Removed BinaryParser dependency from the serializer/deserializer.
0.5.0 2016-07-05
-----------------
- Added Decimal128 type and extended test suite to include entire bson corpus.
0.4.23 2016-04-08
-----------------
- Allow for proper detection of ObjectId or objects that look like ObjectId, improving compatibility across third party libraries.
- Remove one package from dependency due to having been pulled from NPM.
0.4.22 2016-03-04
-----------------
- Fix "TypeError: data.copy is not a function" in Electron (Issue #170, https://github.com/kangas).
- Fixed issue with undefined type on deserializing.
0.4.21 2016-01-12
-----------------
- Minor optimizations to avoid non needed object creation.
0.4.20 2015-10-15
-----------------
- Added bower file to repository.
- Fixed browser pid sometimes set greater than 0xFFFF on browsers (Issue #155, https://github.com/rahatarmanahmed)
0.4.19 2015-10-15
-----------------
- Remove all support for bson-ext.
0.4.18 2015-10-15
-----------------
- ObjectID equality check should return boolean instead of throwing exception for invalid oid string #139
- add option for deserializing binary into Buffer object #116
0.4.17 2015-10-15
-----------------
- Validate regexp string for null bytes and throw if there is one.
0.4.16 2015-10-07
-----------------
- Fixed issue with return statement in Map.js.
0.4.15 2015-10-06
-----------------
- Exposed Map correctly via index.js file.
0.4.14 2015-10-06
-----------------
- Exposed Map correctly via bson.js file.
0.4.13 2015-10-06
-----------------
- Added ES6 Map type serialization as well as a polyfill for ES5.
0.4.12 2015-09-18
-----------------
- Made ignore undefined an optional parameter.
0.4.11 2015-08-06
-----------------
- Minor fix for invalid key checking.
0.4.10 2015-08-06
-----------------
- NODE-38 Added new BSONRegExp type to allow direct serialization to MongoDB type.
- Some performance improvements by in lining code.
0.4.9 2015-08-06
----------------
- Undefined fields are omitted from serialization in objects.
0.4.8 2015-07-14
----------------
- Fixed size validation to ensure we can deserialize from dumped files.
0.4.7 2015-06-26
----------------
- Added ability to instruct deserializer to return raw BSON buffers for named array fields.
- Minor deserialization optimization by moving inlined function out.
0.4.6 2015-06-17
----------------
- Fixed serializeWithBufferAndIndex bug.
0.4.5 2015-06-17
----------------
- Removed any references to the shared buffer to avoid non GC collectible bson instances.
0.4.4 2015-06-17
----------------
- Fixed rethrowing of error when not RangeError.
0.4.3 2015-06-17
----------------
- Start buffer at 64K and double as needed, meaning we keep a low memory profile until needed.
0.4.2 2015-06-16
----------------
- More fixes for corrupt Bson
0.4.1 2015-06-16
----------------
- More fixes for corrupt Bson
0.4.0 2015-06-16
----------------
- New JS serializer serializing into a single buffer then copying out the new buffer. Performance is similar to current C++ parser.
- Removed bson-ext extension dependency for now.
0.3.2 2015-03-27
----------------
- Removed node-gyp from install script in package.json.
0.3.1 2015-03-27
----------------
- Return pure js version on native() call if failed to initialize.
0.3.0 2015-03-26
----------------
- Pulled out all C++ code into bson-ext and made it an optional dependency.
0.2.21 2015-03-21
-----------------
- Updated Nan to 1.7.0 to support io.js and node 0.12.0
0.2.19 2015-02-16
-----------------
- Updated Nan to 1.6.2 to support io.js and node 0.12.0
0.2.18 2015-01-20
-----------------
- Updated Nan to 1.5.1 to support io.js
0.2.16 2014-12-17
-----------------
- Made pid cycle on 0xffff to avoid weird overflows on creation of ObjectID's
0.2.12 2014-08-24
-----------------
- Fixes for fortify review of c++ extension
- toBSON correctly allows returns of non objects
0.2.3 2013-10-01
----------------
- Drying of ObjectId code for generation of id (Issue #54, https://github.com/moredip)
- Fixed issue where corrupt CString's could cause endless loop
- Support for Node 0.11.X > (Issue #49, https://github.com/kkoopa)
0.1.4 2012-09-25
----------------
- Added precompiled c++ native extensions for win32 ia32 and x64
This diff is collapsed.
# 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](http://bsonspec.org).
This browser version of the BSON parser is compiled using [rollup](https://rollupjs.org/) 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](https://github.com/mongodb-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:
```html
<script src="./dist/bson.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`:
```js
// Get BSON parser class
var BSON = require('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](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.
#### 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} 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} 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](http://bsonspec.org/spec.html), so this library has dropped support for it. Use the `ignoreUndefined` option (for example, from the [driver](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect) ) 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.
```javascript
var bson = new BSON();
class CustomSerialize {
toBSON() {
return 42;
}
}
const obj = { answer: new CustomSerialize() };
// "{ answer: 42 }"
console.log(bson.deserialize(bson.serialize(obj)));
```
{
"name": "bson",
"description": "A bson parser for node.js and the browser",
"keywords": [
"mongodb",
"bson",
"parser"
],
"author": "Christian Amor Kvalheim <christkv@gmail.com>",
"main": "./dist/bson.js",
"license": "Apache-2.0",
"moduleType": [
"globals",
"node"
],
"ignore": [
"**/.*",
"alternate_parsers",
"benchmarks",
"bower_components",
"node_modules",
"test",
"tools"
]
}
This diff is collapsed.
var BSON = require('./lib/bson/bson'),
Binary = require('./lib/bson/binary'),
Code = require('./lib/bson/code'),
DBRef = require('./lib/bson/db_ref'),
Decimal128 = require('./lib/bson/decimal128'),
Double = require('./lib/bson/double'),
Int32 = require('./lib/bson/int_32'),
Long = require('./lib/bson/long'),
Map = require('./lib/bson/map'),
MaxKey = require('./lib/bson/max_key'),
MinKey = require('./lib/bson/min_key'),
ObjectId = require('./lib/bson/objectid'),
BSONRegExp = require('./lib/bson/regexp'),
Symbol = require('./lib/bson/symbol'),
Timestamp = require('./lib/bson/timestamp');
// BSON MAX VALUES
BSON.BSON_INT32_MAX = 0x7fffffff;
BSON.BSON_INT32_MIN = -0x80000000;
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
// JS MAX PRECISE VALUES
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
// Add BSON types to function creation
BSON.Binary = Binary;
BSON.Code = Code;
BSON.DBRef = DBRef;
BSON.Decimal128 = Decimal128;
BSON.Double = Double;
BSON.Int32 = Int32;
BSON.Long = Long;
BSON.Map = Map;
BSON.MaxKey = MaxKey;
BSON.MinKey = MinKey;
BSON.ObjectId = ObjectId;
BSON.ObjectID = ObjectId;
BSON.BSONRegExp = BSONRegExp;
BSON.Symbol = Symbol;
BSON.Timestamp = Timestamp;
// Return the BSON
module.exports = BSON;
This diff is collapsed.
This diff is collapsed.
'use strict';
/**
* A class representation of the BSON Code type.
*
* @class
* @param {(string|function)} code a string or function.
* @param {Object} [scope] an optional scope for the function.
* @return {Code}
*/
function Code(code, scope) {
if (!(this instanceof Code)) return new Code(code, scope);
this._bsontype = 'Code';
this.code = code;
this.scope = scope;
}
/**
* @ignore
*/
Code.prototype.toJSON = function() {
return { scope: this.scope, code: this.code };
};
module.exports = Code;
module.exports.Code = Code;
'use strict';
/**
* A class representation of the BSON DBRef type.
*
* @class
* @param {string} collection the collection name.
* @param {ObjectID} oid the reference ObjectID.
* @param {string} [db] optional db name, if omitted the reference is local to the current db.
* @return {DBRef}
*/
function DBRef(collection, oid, db, fields) {
if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
// check if namespace has been provided
var parts = collection.split('.');
if (parts.length === 2) {
db = parts.shift();
collection = parts.shift();
}
this._bsontype = 'DBRef';
this.collection = collection;
this.oid = oid;
this.db = db;
this.fields = fields || {};
}
/**
* @ignore
* @api private
*/
DBRef.prototype.toJSON = function() {
var o = {
$ref: this.collection,
$id: this.oid
};
if (this.db != null) o.$db = this.db;
o = Object.assign(o, this.fields);
return o;
};
module.exports = DBRef;
module.exports.DBRef = DBRef;
This diff is collapsed.
'use strict';
/**
* A class representation of the BSON Double type.
*
* @class
* @param {number} value the number we want to represent as a double.
* @return {Double}
*/
function Double(value) {
if (!(this instanceof Double)) return new Double(value);
this._bsontype = 'Double';
this.value = value;
}
/**
* Access the number value.
*
* @method
* @return {number} returns the wrapped double number.
*/
Double.prototype.valueOf = function() {
return this.value;
};
/**
* @ignore
*/
Double.prototype.toJSON = function() {
return this.value;
};
module.exports = Double;
module.exports.Double = Double;
'use strict';
// Copyright (c) 2008, Fair Oaks Labs, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//
// Modifications to writeIEEE754 to support negative zeroes made by Brian White
var readIEEE754 = function(buffer, offset, endian, mLen, nBytes) {
var e,
m,
bBE = endian === 'big',
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,
nBits = -7,
i = bBE ? 0 : nBytes - 1,
d = bBE ? 1 : -1,
s = buffer[offset + i];
i += d;
e = s & ((1 << -nBits) - 1);
s >>= -nBits;
nBits += eLen;