Skip to content

Commit

Permalink
fix(NODE-3150): added bsonRegExp option for v3.6 (#2843)
Browse files Browse the repository at this point in the history
  • Loading branch information
andymina committed Jun 28, 2021
1 parent 750760c commit e4a9a57
Show file tree
Hide file tree
Showing 23 changed files with 112 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/cmap/connection.js
Expand Up @@ -317,6 +317,7 @@ function write(command, options, callback) {
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false,
raw: typeof options.raw === 'boolean' ? options.raw : false
};

Expand Down
9 changes: 9 additions & 0 deletions lib/collection.js
Expand Up @@ -120,6 +120,8 @@ function Collection(db, topology, dbName, name, pkFactory, options) {
options == null || options.promoteBuffers == null
? db.s.options.promoteBuffers
: options.promoteBuffers;
const bsonRegExp =
options == null || options.bsonRegExp == null ? db.s.options.bsonRegExp : options.bsonRegExp;
const collectionHint = null;

const namespace = new MongoDBNamespace(dbName, name);
Expand Down Expand Up @@ -156,6 +158,8 @@ function Collection(db, topology, dbName, name, pkFactory, options) {
promoteValues: promoteValues,
// promoteBuffers
promoteBuffers: promoteBuffers,
// bsonRegExp
bsonRegExp: bsonRegExp,
// internalHint
internalHint: internalHint,
// collectionHint
Expand Down Expand Up @@ -303,6 +307,7 @@ const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot', 'oplogReplay']
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {boolean} [options.partial=false] Specify if the cursor should return partial results when querying against a sharded system
* @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
Expand Down Expand Up @@ -451,6 +456,8 @@ Collection.prototype.find = deprecateOptions(
newOptions.promoteValues = this.s.promoteValues;
if (newOptions.promoteBuffers == null && typeof this.s.promoteBuffers === 'boolean')
newOptions.promoteBuffers = this.s.promoteBuffers;
if (newOptions.bsonRegExp == null && typeof this.s.bsonRegExp === 'boolean')
newOptions.bsonRegExp = this.s.bsonRegExp;

// Sort options
if (findCommand.sort) {
Expand Down Expand Up @@ -1075,6 +1082,7 @@ Collection.prototype.save = deprecate(function(doc, options, callback) {
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {boolean} [options.partial=false] Specify if the cursor should return partial results when querying against a sharded system
* @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
Expand Down Expand Up @@ -1899,6 +1907,7 @@ Collection.prototype.findAndRemove = deprecate(function(query, sort, options, ca
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
* @param {string} [options.comment] Add a comment to an aggregation command
* @param {string|object} [options.hint] Add an index selection hint to an aggregation command
Expand Down
13 changes: 11 additions & 2 deletions lib/core/connection/commands.js
Expand Up @@ -398,7 +398,12 @@ KillCursor.prototype.toBin = function() {
};

var Response = function(bson, message, msgHeader, msgBody, opts) {
opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false };
opts = opts || {
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
bsonRegExp: false
};
this.parsed = false;
this.raw = message;
this.data = msgBody;
Expand Down Expand Up @@ -429,6 +434,7 @@ var Response = function(bson, message, msgHeader, msgBody, opts) {
this.promoteLongs = typeof opts.promoteLongs === 'boolean' ? opts.promoteLongs : true;
this.promoteValues = typeof opts.promoteValues === 'boolean' ? opts.promoteValues : true;
this.promoteBuffers = typeof opts.promoteBuffers === 'boolean' ? opts.promoteBuffers : false;
this.bsonRegExp = typeof opts.bsonRegExp === 'boolean' ? opts.bsonRegExp : false;
};

Response.prototype.isParsed = function() {
Expand All @@ -449,13 +455,16 @@ Response.prototype.parse = function(options) {
typeof options.promoteValues === 'boolean' ? options.promoteValues : this.opts.promoteValues;
var promoteBuffers =
typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : this.opts.promoteBuffers;
var bsonRegExp =
typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : this.opts.bsonRegExp;
var bsonSize, _options;

// Set up the options
_options = {
promoteLongs: promoteLongs,
promoteValues: promoteValues,
promoteBuffers: promoteBuffers
promoteBuffers: promoteBuffers,
bsonRegExp: bsonRegExp
};

// Position within OP_REPLY at which documents start
Expand Down
5 changes: 4 additions & 1 deletion lib/core/connection/connection.js
Expand Up @@ -38,6 +38,7 @@ const DEBUG_FIELDS = [
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bsonRegExp',
'checkServerIdentity'
];

Expand Down Expand Up @@ -73,6 +74,7 @@ class Connection extends EventEmitter {
* @param {boolean} [options.promoteLongs] Convert Long values from the db into Numbers if they fit into 53 bits
* @param {boolean} [options.promoteValues] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {number} [options.maxBsonMessageSize=0x4000000] Largest possible size of a BSON message (for legacy purposes)
*/
constructor(socket, options) {
Expand Down Expand Up @@ -117,7 +119,8 @@ class Connection extends EventEmitter {
this.responseOptions = {
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false
};

// Flushing
Expand Down
13 changes: 11 additions & 2 deletions lib/core/connection/msg.js
Expand Up @@ -139,7 +139,12 @@ Msg.getRequestId = function() {

class BinMsg {
constructor(bson, message, msgHeader, msgBody, opts) {
opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false };
opts = opts || {
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
bsonRegExp: false
};
this.parsed = false;
this.raw = message;
this.data = msgBody;
Expand All @@ -161,6 +166,7 @@ class BinMsg {
this.promoteLongs = typeof opts.promoteLongs === 'boolean' ? opts.promoteLongs : true;
this.promoteValues = typeof opts.promoteValues === 'boolean' ? opts.promoteValues : true;
this.promoteBuffers = typeof opts.promoteBuffers === 'boolean' ? opts.promoteBuffers : false;
this.bsonRegExp = typeof opts.bsonRegExp === 'boolean' ? opts.bsonRegExp : false;

this.documents = [];
}
Expand All @@ -186,12 +192,15 @@ class BinMsg {
typeof options.promoteBuffers === 'boolean'
? options.promoteBuffers
: this.opts.promoteBuffers;
const bsonRegExp =
typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : this.opts.bsonRegExp;

// Set up the options
const _options = {
promoteLongs: promoteLongs,
promoteValues: promoteValues,
promoteBuffers: promoteBuffers
promoteBuffers: promoteBuffers,
bsonRegExp: bsonRegExp
};

while (this.index < this.data.length) {
Expand Down
4 changes: 4 additions & 0 deletions lib/core/connection/pool.js
Expand Up @@ -76,6 +76,7 @@ var _id = 0;
* @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
* @fires Pool#connect
* @fires Pool#close
Expand Down Expand Up @@ -127,6 +128,7 @@ var Pool = function(topology, options) {
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
bsonRegExp: false,
// Reconnection options
reconnect: true,
reconnectInterval: 1000,
Expand Down Expand Up @@ -870,6 +872,7 @@ Pool.prototype.write = function(command, options, cb) {
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
bsonRegExp: false,
fullResult: false
};

Expand All @@ -879,6 +882,7 @@ Pool.prototype.write = function(command, options, cb) {
typeof options.promoteValues === 'boolean' ? options.promoteValues : true;
operation.promoteBuffers =
typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false;
operation.bsonRegExp = typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false;
operation.raw = typeof options.raw === 'boolean' ? options.raw : false;
operation.immediateRelease =
typeof options.immediateRelease === 'boolean' ? options.immediateRelease : false;
Expand Down
7 changes: 7 additions & 0 deletions lib/core/cursor.js
Expand Up @@ -146,6 +146,13 @@ class CoreCursor extends Readable {
this.cursorState.promoteBuffers = options.promoteBuffers;
}

// Add bsonRegExp to cursor state
if (typeof topologyOptions.bsonRegExp === 'boolean') {
this.cursorState.bsonRegExp = topologyOptions.bsonRegExp;
} else if (typeof options.bsonRegExp === 'boolean') {
this.cursorState.bsonRegExp = options.bsonRegExp;
}

if (topologyOptions.reconnect) {
this.cursorState.reconnect = topologyOptions.reconnect;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/core/sdam/monitor.js
Expand Up @@ -85,7 +85,8 @@ class Monitor extends EventEmitter {
raw: false,
promoteLongs: true,
promoteValues: true,
promoteBuffers: true
promoteBuffers: true,
bsonRegExp: true
}
);

Expand Down
1 change: 1 addition & 0 deletions lib/core/sdam/server.js
Expand Up @@ -50,6 +50,7 @@ const DEBUG_FIELDS = [
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bsonRegExp',
'servername'
];

Expand Down
1 change: 1 addition & 0 deletions lib/core/topologies/mongos.js
Expand Up @@ -88,6 +88,7 @@ var handlers = ['connect', 'close', 'error', 'timeout', 'parseError'];
* @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this topology
* @return {Mongos} A cursor instance
Expand Down
1 change: 1 addition & 0 deletions lib/core/topologies/replset.js
Expand Up @@ -88,6 +88,7 @@ var handlers = ['connect', 'close', 'error', 'timeout', 'parseError'];
* @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {number} [options.pingInterval=5000] Ping interval to check the response time to the different servers
* @param {number} [options.localThresholdMS=15] Cutoff latency point in MS for Replicaset member selection
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
Expand Down
2 changes: 2 additions & 0 deletions lib/core/topologies/server.js
Expand Up @@ -47,6 +47,7 @@ var debugFields = [
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bsonRegExp',
'servername'
];

Expand Down Expand Up @@ -89,6 +90,7 @@ function topologyId(server) {
* @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {string} [options.appname=null] Application name, passed in on ismaster call and logged in mongod server logs. Maximum size 128 bytes.
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this topology
Expand Down
1 change: 1 addition & 0 deletions lib/core/wireprotocol/shared.js
Expand Up @@ -57,6 +57,7 @@ function applyCommonQueryOptions(queryOptions, options) {
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false,
monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : false,
fullResult: typeof options.fullResult === 'boolean' ? options.fullResult : false
});
Expand Down
7 changes: 5 additions & 2 deletions lib/db.js
Expand Up @@ -83,7 +83,6 @@ const legalOptionNames = [
'bufferMaxEntries',
'authSource',
'ignoreUndefined',
'promoteLongs',
'promiseLibrary',
'readConcern',
'retryMiliSeconds',
Expand All @@ -95,6 +94,7 @@ const legalOptionNames = [
'promoteBuffers',
'promoteLongs',
'promoteValues',
'bsonRegExp',
'compression',
'retryWrites'
];
Expand All @@ -117,6 +117,7 @@ const legalOptionNames = [
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys.
Expand Down Expand Up @@ -323,6 +324,7 @@ Db.prototype.command = function(command, options, callback) {
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
* @param {string} [options.comment] Add a comment to an aggregation command
* @param {string|object} [options.hint] Add an index selection hint to an aggregation command
Expand Down Expand Up @@ -391,7 +393,8 @@ const COLLECTION_OPTION_KEYS = [
'ignoreUndefined',
'promoteValues',
'promoteBuffers',
'promoteLongs'
'promoteLongs',
'bsonRegExp'
];

/**
Expand Down
1 change: 1 addition & 0 deletions lib/mongo_client.js
Expand Up @@ -144,6 +144,7 @@ const validOptions = require('./operations/connect').validOptions;
* @property {boolean} [promoteValues] (**default**: true) Promotes BSON values to native types where possible, set to false to only receive wrapper types
* @property {boolean} [promoteBuffers] (**default**: false) Promotes Binary BSON values to native Node Buffers
* @property {boolean} [promoteLongs] (**default**: true) Promotes long values to number if they fit inside the 53 bits resolution
* * @param {boolean} [bsonRegExp] (**default**: false) By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @property {boolean} [domainsEnabled] (**default**: false) Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit
* @property {object} [validateOptions] (**default**: false) Validate MongoClient passed in options for correctness
* @property {string} [appname] (**default**: undefined) The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections
Expand Down
1 change: 1 addition & 0 deletions lib/operations/command.js
Expand Up @@ -22,6 +22,7 @@ const debugFields = [
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bsonRegExp',
'bufferMaxEntries',
'numberOfRetries',
'retryMiliSeconds',
Expand Down
1 change: 1 addition & 0 deletions lib/operations/connect.js
Expand Up @@ -122,6 +122,7 @@ const validOptionNames = [
'promoteValues',
'promoteBuffers',
'promoteLongs',
'bsonRegExp',
'domainsEnabled',
'checkServerIdentity',
'validateOptions',
Expand Down
1 change: 1 addition & 0 deletions lib/operations/db_ops.js
Expand Up @@ -24,6 +24,7 @@ const debugFields = [
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bsonRegExp',
'bufferMaxEntries',
'numberOfRetries',
'retryMiliSeconds',
Expand Down

0 comments on commit e4a9a57

Please sign in to comment.