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

Commit 1273e72

Browse files
authored
feat!(NODE-2944): Support JS BSON v4 (#67)
1 parent f20eb1e commit 1273e72

13 files changed

+2219
-1356
lines changed

.evergreen/config.yml

+1-9
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ functions:
9090
NODE_FORCE_PUBLISH=1 NODE_GITHUB_TOKEN=${github_token} ${PROJECT_DIRECTORY}/.evergreen/run-prebuild.sh
9191
9292
tasks:
93-
- name: node-tests-v4
94-
tags: ["tests", "node_v4"]
95-
commands:
96-
- func: fetch source
97-
vars:
98-
NODE_MAJOR_VERSION: 4
99-
- func: install dependencies
100-
- func: run tests
10193
- name: node-tests-v6
10294
tags: ["tests"]
10395
commands:
@@ -183,4 +175,4 @@ buildvariants:
183175
has_packages: true
184176
packager_distro: suse12
185177
packager_arch: s390x
186-
tasks: [".tests !.node_v4", ".prebuild", ".force"]
178+
tasks: [".tests", ".prebuild", ".force"]

.evergreen/run-prebuild.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ run_prebuild() {
1414
else
1515
echo "Github token detected. Running prebuild."
1616
npm run prebuild -- -u $NODE_GITHUB_TOKEN
17-
npm run prebuild-legacy -- -u $NODE_GITHUB_TOKEN
1817
echo "Prebuild's successfully submitted"
1918
fi
2019
}
@@ -49,4 +48,4 @@ else
4948
npm run prebuild
5049
echo "Local prebuild successful."
5150
ls ./prebuilds
52-
fi
51+
fi

.evergreen/run-tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ else
1919
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
2020
fi
2121

22-
npm test
22+
npm test

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ builderror.log
4444
bson.sublime-project
4545
bson.sublime-workspace
4646
.vagrant/
47+
48+
prebuilds/

benchmarks/serialization_benchmark.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
var BSON = require('../');
2-
var bson = new BSON([BSON.Binary, BSON.Code, BSON.DBRef, BSON.Decimal128,
3-
BSON.Double, BSON.Int32, BSON.Long, BSON.Map, BSON.MaxKey, BSON.MinKey,
4-
BSON.ObjectId, BSON.BSONRegExp, BSON.Symbol, BSON.Timestamp]);
52
var BSONJS = require('bson');
6-
var bsonJS = new BSONJS();
73

84
function generateRecord(recnum) {
95
// Definition of a 'Document'
@@ -79,7 +75,7 @@ var iterations = 100000;
7975
// var iterations = 10000;
8076
// var iterations = 1;
8177
var doc = generateRecord(0)
82-
var buffer = bson.serialize(doc);
78+
var buffer = BSON.serialize(doc);
8379
var start = new Date();
8480

8581
// console.log("=====================================================")
@@ -89,7 +85,7 @@ var start = new Date();
8985
// Serialize
9086
//
9187
for(var i = 0; i < iterations; i++) {
92-
bson.serialize(doc);
88+
BSON.serialize(doc);
9389
}
9490

9591
var end = new Date();
@@ -99,7 +95,7 @@ console.log("totalMS = " + (end.getTime() - start.getTime()));
9995
var start = new Date();
10096

10197
for(var i = 0; i < iterations; i++) {
102-
bsonJS.serialize(doc);
98+
BSONJS.serialize(doc);
10399
}
104100

105101
var end = new Date();
@@ -112,7 +108,7 @@ console.log("totalMS = " + (end.getTime() - start.getTime()));
112108
var start = new Date();
113109

114110
for(var i = 0; i < iterations; i++) {
115-
bson.deserialize(buffer);
111+
BSON.deserialize(buffer);
116112
}
117113

118114
var end = new Date();
@@ -122,7 +118,7 @@ console.log("totalMS = " + (end.getTime() - start.getTime()));
122118
var start = new Date();
123119

124120
for(var i = 0; i < iterations; i++) {
125-
bsonJS.deserialize(buffer);
121+
BSONJS.deserialize(buffer);
126122
}
127123

128124
var end = new Date();

lib/index.js

+84-44
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,87 @@
1-
var BSON = require('bindings')('bson').BSON;
2-
var jsBson = require('bson');
3-
4-
// BSON MAX VALUES
5-
BSON.BSON_INT32_MAX = 0x7FFFFFFF;
6-
BSON.BSON_INT32_MIN = -0x80000000;
7-
8-
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
9-
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
10-
11-
// JS MAX PRECISE VALUES
12-
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
13-
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
14-
15-
// Decorate BSON with types from js-bson
16-
[
17-
'Binary',
18-
'Code',
19-
'DBRef',
20-
'Decimal128',
21-
'Double',
22-
'Int32',
23-
'Long',
24-
'Map',
25-
'MaxKey',
26-
'MinKey',
27-
'ObjectId',
28-
'BSONRegExp',
29-
'Symbol',
30-
'Timestamp'
31-
].forEach(function(type) {
32-
BSON[type] = jsBson[type];
33-
});
1+
var BSONExtModule = require('bindings')('bson').BSON;
342

35-
// special case for deprecated names
36-
BSON.ObjectID = BSON.ObjectId;
3+
var BSONJS = require('bson');
4+
5+
const BSON = new BSONExtModule([
6+
BSONJS.Binary,
7+
BSONJS.Code,
8+
BSONJS.DBRef,
9+
BSONJS.Decimal128,
10+
BSONJS.Double,
11+
BSONJS.Int32,
12+
BSONJS.Long,
13+
BSONJS.Map,
14+
BSONJS.MaxKey,
15+
BSONJS.MinKey,
16+
BSONJS.ObjectId,
17+
BSONJS.BSONRegExp,
18+
BSONJS.BSONSymbol,
19+
BSONJS.Timestamp
20+
]);
21+
22+
module.exports = Object.create(null);
23+
24+
module.exports.BSON_INT32_MAX = 0x7fffffff;
25+
module.exports.BSON_INT32_MIN = -0x80000000;
26+
27+
module.exports.BSON_INT64_MAX = BSONJS.Long.MAX_VALUE;
28+
module.exports.BSON_INT64_MIN = BSONJS.Long.MIN_VALUE;
29+
30+
module.exports.JS_INT_MAX = Number.MAX_SAFE_INTEGER;
31+
module.exports.JS_INT_MIN = Number.MIN_SAFE_INTEGER;
3732

3833
// Just add constants to the Native BSON parser
39-
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
40-
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
41-
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
42-
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
43-
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
44-
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
45-
46-
// Return the BSON
47-
module.exports = BSON;
34+
module.exports.BSON_BINARY_SUBTYPE_DEFAULT = 0;
35+
module.exports.BSON_BINARY_SUBTYPE_FUNCTION = 1;
36+
module.exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
37+
module.exports.BSON_BINARY_SUBTYPE_UUID = 3;
38+
module.exports.BSON_BINARY_SUBTYPE_UUID_NEW = 4;
39+
module.exports.BSON_BINARY_SUBTYPE_MD5 = 5;
40+
module.exports.BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
41+
module.exports.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
42+
43+
/** @type {import('bson').Binary} */
44+
module.exports.Binary = BSONJS.Binary;
45+
/** @type {import('bson').Code} */
46+
module.exports.Code = BSONJS.Code;
47+
/** @type {import('bson').DBRef} */
48+
module.exports.DBRef = BSONJS.DBRef;
49+
/** @type {import('bson').Decimal128} */
50+
module.exports.Decimal128 = BSONJS.Decimal128;
51+
/** @type {import('bson').Double} */
52+
module.exports.Double = BSONJS.Double;
53+
/** @type {import('bson').Int32} */
54+
module.exports.Int32 = BSONJS.Int32;
55+
/** @type {import('bson').Long} */
56+
module.exports.Long = BSONJS.Long;
57+
/** @type {import('bson').Map} */
58+
module.exports.Map = BSONJS.Map;
59+
/** @type {import('bson').MaxKey} */
60+
module.exports.MaxKey = BSONJS.MaxKey;
61+
/** @type {import('bson').MinKey} */
62+
module.exports.MinKey = BSONJS.MinKey;
63+
/** @type {import('bson').ObjectId} */
64+
module.exports.ObjectId = BSONJS.ObjectId;
65+
/** @type {import('bson').BSONRegExp} */
66+
module.exports.BSONRegExp = BSONJS.BSONRegExp;
67+
/** @type {import('bson').BSONSymbol} */
68+
module.exports.BSONSymbol = BSONJS.BSONSymbol;
69+
/** @type {import('bson').Timestamp} */
70+
module.exports.Timestamp = BSONJS.Timestamp;
71+
72+
// special case for deprecated names
73+
/** @type {import('bson').ObjectId} */
74+
module.exports.ObjectID = BSONJS.ObjectId;
75+
76+
/** @type {import('bson').calculateObjectSize} */
77+
module.exports.calculateObjectSize = BSON.calculateObjectSize.bind(BSON);
78+
/** @type {import('bson').serialize} */
79+
module.exports.serialize = BSON.serialize.bind(BSON);
80+
/** @type {import('bson').serializeWithBufferAndIndex} */
81+
module.exports.serializeWithBufferAndIndex = BSON.serializeWithBufferAndIndex.bind(BSON);
82+
/** @type {import('bson').deserialize} */
83+
module.exports.deserialize = BSON.deserialize.bind(BSON);
84+
/** @type {import('bson').deserializeStream} */
85+
module.exports.deserializeStream = BSON.deserializeStream.bind(BSON);
86+
87+
Object.freeze(module.exports);

0 commit comments

Comments
 (0)