First Commit

This commit is contained in:
SowinskiBraeden committed 2020-11-25 09:10:06 -08:00
commit 601e886c14
2123 files changed
+334815

No files matched your search

+293
View File
@@ -0,0 +1,293 @@
'use strict';
const applyWriteConcern = require('./utils').applyWriteConcern;
const AddUserOperation = require('./operations/add_user');
const ExecuteDbAdminCommandOperation = require('./operations/execute_db_admin_command');
const RemoveUserOperation = require('./operations/remove_user');
const ValidateCollectionOperation = require('./operations/validate_collection');
const ListDatabasesOperation = require('./operations/list_databases');
const executeOperation = require('./operations/execute_operation');
/**
* @fileOverview The **Admin** class is an internal class that allows convenient access to
* the admin functionality and commands for MongoDB.
*
* **ADMIN Cannot directly be instantiated**
* @example
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
*
* // Connect using MongoClient
* MongoClient.connect(url, function(err, client) {
* // Use the admin database for the operation
* const adminDb = client.db(dbName).admin();
*
* // List all the available databases
* adminDb.listDatabases(function(err, dbs) {
* test.equal(null, err);
* test.ok(dbs.databases.length > 0);
* client.close();
* });
* });
*/
/**
* Create a new Admin instance (INTERNAL TYPE, do not instantiate directly)
* @class
* @return {Admin} a collection instance.
*/
function Admin(db, topology, promiseLibrary) {
if (!(this instanceof Admin)) return new Admin(db, topology);
// Internal state
this.s = {
db: db,
topology: topology,
promiseLibrary: promiseLibrary
};
}
/**
* The callback format for results
* @callback Admin~resultCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {object} result The result object if the command was executed successfully.
*/
/**
* Execute a command
* @method
* @param {object} command The command hash
* @param {object} [options] Optional settings.
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.command = function(command, options, callback) {
const args = Array.prototype.slice.call(arguments, 1);
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
options = args.length ? args.shift() : {};
const commandOperation = new ExecuteDbAdminCommandOperation(this.s.db, command, options);
return executeOperation(this.s.db.s.topology, commandOperation, callback);
};
/**
* Retrieve the server information for the current
* instance of the db client
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.buildInfo = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const cmd = { buildinfo: 1 };
const buildInfoOperation = new ExecuteDbAdminCommandOperation(this.s.db, cmd, options);
return executeOperation(this.s.db.s.topology, buildInfoOperation, callback);
};
/**
* Retrieve the server information for the current
* instance of the db client
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.serverInfo = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const cmd = { buildinfo: 1 };
const serverInfoOperation = new ExecuteDbAdminCommandOperation(this.s.db, cmd, options);
return executeOperation(this.s.db.s.topology, serverInfoOperation, callback);
};
/**
* Retrieve this db's server status.
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.serverStatus = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const serverStatusOperation = new ExecuteDbAdminCommandOperation(
this.s.db,
{ serverStatus: 1 },
options
);
return executeOperation(this.s.db.s.topology, serverStatusOperation, callback);
};
/**
* Ping the MongoDB server and retrieve results
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.ping = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const cmd = { ping: 1 };
const pingOperation = new ExecuteDbAdminCommandOperation(this.s.db, cmd, options);
return executeOperation(this.s.db.s.topology, pingOperation, callback);
};
/**
* Add a user to the database.
* @method
* @param {string} username The username.
* @param {string} password The password.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
* @param {object} [options.customData] Custom data associated with the user (only Mongodb 2.6 or higher)
* @param {object[]} [options.roles] Roles associated with the created user (only Mongodb 2.6 or higher)
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.addUser = function(username, password, options, callback) {
const args = Array.prototype.slice.call(arguments, 2);
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
// Special case where there is no password ($external users)
if (typeof username === 'string' && password != null && typeof password === 'object') {
options = password;
password = null;
}
options = args.length ? args.shift() : {};
options = Object.assign({}, options);
// Get the options
options = applyWriteConcern(options, { db: this.s.db });
// Set the db name to admin
options.dbName = 'admin';
const addUserOperation = new AddUserOperation(this.s.db, username, password, options);
return executeOperation(this.s.db.s.topology, addUserOperation, callback);
};
/**
* Remove a user from a database
* @method
* @param {string} username The username.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.removeUser = function(username, options, callback) {
const args = Array.prototype.slice.call(arguments, 1);
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
options = args.length ? args.shift() : {};
options = Object.assign({}, options);
// Get the options
options = applyWriteConcern(options, { db: this.s.db });
// Set the db name
options.dbName = 'admin';
const removeUserOperation = new RemoveUserOperation(this.s.db, username, options);
return executeOperation(this.s.db.s.topology, removeUserOperation, callback);
};
/**
* Validate an existing collection
*
* @param {string} collectionName The name of the collection to validate.
* @param {object} [options] Optional settings.
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.validateCollection = function(collectionName, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const validateCollectionOperation = new ValidateCollectionOperation(
this,
collectionName,
options
);
return executeOperation(this.s.db.s.topology, validateCollectionOperation, callback);
};
/**
* List the available databases
*
* @param {object} [options] Optional settings.
* @param {boolean} [options.nameOnly=false] Whether the command should return only db names, or names and size info.
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.listDatabases = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
return executeOperation(
this.s.db.s.topology,
new ListDatabasesOperation(this.s.db, options),
callback
);
};
/**
* Get ReplicaSet status
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.replSetGetStatus = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const replSetGetStatusOperation = new ExecuteDbAdminCommandOperation(
this.s.db,
{ replSetGetStatus: 1 },
options
);
return executeOperation(this.s.db.s.topology, replSetGetStatusOperation, callback);
};
module.exports = Admin;
+370
View File
@@ -0,0 +1,370 @@
'use strict';
const MongoError = require('./core').MongoError;
const Cursor = require('./cursor');
const CursorState = require('./core/cursor').CursorState;
const deprecate = require('util').deprecate;
/**
* @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
* allowing for iteration over the results returned from the underlying query. It supports
* one by one document iteration, conversion to an array or can be iterated as a Node 4.X
* or higher stream
*
* **AGGREGATIONCURSOR Cannot directly be instantiated**
* @example
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, client) {
* // Create a collection we want to drop later
* const col = client.db(dbName).collection('createIndexExample1');
* // Insert a bunch of documents
* col.insert([{a:1, b:1}
* , {a:2, b:2}, {a:3, b:3}
* , {a:4, b:4}], {w:1}, function(err, result) {
* test.equal(null, err);
* // Show that duplicate records got dropped
* col.aggregation({}, {cursor: {}}).toArray(function(err, items) {
* test.equal(null, err);
* test.equal(4, items.length);
* client.close();
* });
* });
* });
*/
/**
* Namespace provided by the browser.
* @external Readable
*/
/**
* Creates a new Aggregation Cursor instance (INTERNAL TYPE, do not instantiate directly)
* @class AggregationCursor
* @extends external:Readable
* @fires AggregationCursor#data
* @fires AggregationCursor#end
* @fires AggregationCursor#close
* @fires AggregationCursor#readable
* @return {AggregationCursor} an AggregationCursor instance.
*/
class AggregationCursor extends Cursor {
constructor(topology, operation, options) {
super(topology, operation, options);
}
/**
* Set the batch size for the cursor.
* @method
* @param {number} value The batchSize for the cursor.
* @throws {MongoError}
* @return {AggregationCursor}
*/
batchSize(value) {
if (this.s.state === CursorState.CLOSED || this.isDead()) {
throw MongoError.create({ message: 'Cursor is closed', driver: true });
}
if (typeof value !== 'number') {
throw MongoError.create({ message: 'batchSize requires an integer', driver: true });
}
this.operation.options.batchSize = value;
this.setCursorBatchSize(value);
return this;
}
/**
* Add a geoNear stage to the aggregation pipeline
* @method
* @param {object} document The geoNear stage document.
* @return {AggregationCursor}
*/
geoNear(document) {
this.operation.addToPipeline({ $geoNear: document });
return this;
}
/**
* Add a group stage to the aggregation pipeline
* @method
* @param {object} document The group stage document.
* @return {AggregationCursor}
*/
group(document) {
this.operation.addToPipeline({ $group: document });
return this;
}
/**
* Add a limit stage to the aggregation pipeline
* @method
* @param {number} value The state limit value.
* @return {AggregationCursor}
*/
limit(value) {
this.operation.addToPipeline({ $limit: value });
return this;
}
/**
* Add a match stage to the aggregation pipeline
* @method
* @param {object} document The match stage document.
* @return {AggregationCursor}
*/
match(document) {
this.operation.addToPipeline({ $match: document });
return this;
}
/**
* Add a maxTimeMS stage to the aggregation pipeline
* @method
* @param {number} value The state maxTimeMS value.
* @return {AggregationCursor}
*/
maxTimeMS(value) {
this.operation.options.maxTimeMS = value;
return this;
}
/**
* Add a out stage to the aggregation pipeline
* @method
* @param {number} destination The destination name.
* @return {AggregationCursor}
*/
out(destination) {
this.operation.addToPipeline({ $out: destination });
return this;
}
/**
* Add a project stage to the aggregation pipeline
* @method
* @param {object} document The project stage document.
* @return {AggregationCursor}
*/
project(document) {
this.operation.addToPipeline({ $project: document });
return this;
}
/**
* Add a lookup stage to the aggregation pipeline
* @method
* @param {object} document The lookup stage document.
* @return {AggregationCursor}
*/
lookup(document) {
this.operation.addToPipeline({ $lookup: document });
return this;
}
/**
* Add a redact stage to the aggregation pipeline
* @method
* @param {object} document The redact stage document.
* @return {AggregationCursor}
*/
redact(document) {
this.operation.addToPipeline({ $redact: document });
return this;
}
/**
* Add a skip stage to the aggregation pipeline
* @method
* @param {number} value The state skip value.
* @return {AggregationCursor}
*/
skip(value) {
this.operation.addToPipeline({ $skip: value });
return this;
}
/**
* Add a sort stage to the aggregation pipeline
* @method
* @param {object} document The sort stage document.
* @return {AggregationCursor}
*/
sort(document) {
this.operation.addToPipeline({ $sort: document });
return this;
}
/**
* Add a unwind stage to the aggregation pipeline
* @method
* @param {number} field The unwind field name.
* @return {AggregationCursor}
*/
unwind(field) {
this.operation.addToPipeline({ $unwind: field });
return this;
}
/**
* Return the cursor logger
* @method
* @return {Logger} return the cursor logger
* @ignore
*/
getLogger() {
return this.logger;
}
}
// aliases
AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
// deprecated methods
deprecate(
AggregationCursor.prototype.geoNear,
'The `$geoNear` stage is deprecated in MongoDB 4.0, and removed in version 4.2.'
);
/**
* AggregationCursor stream data event, fired for each document in the cursor.
*
* @event AggregationCursor#data
* @type {object}
*/
/**
* AggregationCursor stream end event
*
* @event AggregationCursor#end
* @type {null}
*/
/**
* AggregationCursor stream close event
*
* @event AggregationCursor#close
* @type {null}
*/
/**
* AggregationCursor stream readable event
*
* @event AggregationCursor#readable
* @type {null}
*/
/**
* Get the next available document from the cursor, returns null if no more documents are available.
* @function AggregationCursor.prototype.next
* @param {AggregationCursor~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/
/**
* Check if there is any document still available in the cursor
* @function AggregationCursor.prototype.hasNext
* @param {AggregationCursor~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/
/**
* The callback format for results
* @callback AggregationCursor~toArrayResultCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {object[]} documents All the documents the satisfy the cursor.
*/
/**
* Returns an array of documents. The caller is responsible for making sure that there
* is enough memory to store the results. Note that the array only contain partial
* results when this cursor had been previously accessed. In that case,
* cursor.rewind() can be used to reset the cursor.
* @method AggregationCursor.prototype.toArray
* @param {AggregationCursor~toArrayResultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/
/**
* The callback format for results
* @callback AggregationCursor~resultCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {(object|null)} result The result object if the command was executed successfully.
*/
/**
* Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
* not all of the elements will be iterated if this cursor had been previously accessed.
* In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
* **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
* at any given time if batch size is specified. Otherwise, the caller is responsible
* for making sure that the entire result can fit the memory.
* @method AggregationCursor.prototype.each
* @deprecated
* @param {AggregationCursor~resultCallback} callback The result callback.
* @throws {MongoError}
* @return {null}
*/
/**
* Close the cursor, sending a AggregationCursor command and emitting close.
* @method AggregationCursor.prototype.close
* @param {AggregationCursor~resultCallback} [callback] The result callback.
* @return {Promise} returns Promise if no callback passed
*/
/**
* Is the cursor closed
* @method AggregationCursor.prototype.isClosed
* @return {boolean}
*/
/**
* Execute the explain for the cursor
* @method AggregationCursor.prototype.explain
* @param {AggregationCursor~resultCallback} [callback] The result callback.
* @return {Promise} returns Promise if no callback passed
*/
/**
* Clone the cursor
* @function AggregationCursor.prototype.clone
* @return {AggregationCursor}
*/
/**
* Resets the cursor
* @function AggregationCursor.prototype.rewind
* @return {AggregationCursor}
*/
/**
* The callback format for the forEach iterator method
* @callback AggregationCursor~iteratorCallback
* @param {Object} doc An emitted document for the iterator
*/
/**
* The callback error format for the forEach iterator method
* @callback AggregationCursor~endCallback
* @param {MongoError} error An error instance representing the error during the execution.
*/
/**
* Iterates over all the documents for this cursor using the iterator, callback pattern.
* @method AggregationCursor.prototype.forEach
* @param {AggregationCursor~iteratorCallback} iterator The iteration callback.
* @param {AggregationCursor~endCallback} callback The end callback.
* @throws {MongoError}
* @return {null}
*/
module.exports = AggregationCursor;
+31
View File
@@ -0,0 +1,31 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
class Instrumentation extends EventEmitter {
constructor() {
super();
}
instrument(MongoClient, callback) {
// store a reference to the original functions
this.$MongoClient = MongoClient;
const $prototypeConnect = (this.$prototypeConnect = MongoClient.prototype.connect);
const instrumentation = this;
MongoClient.prototype.connect = function(callback) {
this.s.options.monitorCommands = true;
this.on('commandStarted', event => instrumentation.emit('started', event));
this.on('commandSucceeded', event => instrumentation.emit('succeeded', event));
this.on('commandFailed', event => instrumentation.emit('failed', event));
return $prototypeConnect.call(this, callback);
};
if (typeof callback === 'function') callback(null, this);
}
uninstrument() {
this.$MongoClient.prototype.connect = this.$prototypeConnect;
}
}
module.exports = Instrumentation;
+5
View File
@@ -0,0 +1,5 @@
{
"parserOptions": {
"ecmaVersion": 2018
}
}
+33
View File
@@ -0,0 +1,33 @@
'use strict';
// async function* asyncIterator() {
// while (true) {
// const value = await this.next();
// if (!value) {
// await this.close();
// return;
// }
// yield value;
// }
// }
// TODO: change this to the async generator function above
function asyncIterator() {
const cursor = this;
return {
next: function() {
return Promise.resolve()
.then(() => cursor.next())
.then(value => {
if (!value) {
return cursor.close().then(() => ({ value, done: true }));
}
return { value, done: false };
});
}
};
}
exports.asyncIterator = asyncIterator;
+1162
View File
File diff suppressed because it is too large. Load diff
+105
View File
@@ -0,0 +1,105 @@
'use strict';
const common = require('./common');
const BulkOperationBase = common.BulkOperationBase;
const Batch = common.Batch;
const bson = common.bson;
const utils = require('../utils');
const toError = utils.toError;
/**
* Add to internal list of Operations
*
* @param {OrderedBulkOperation} bulkOperation
* @param {number} docType number indicating the document type
* @param {object} document
* @return {OrderedBulkOperation}
*/
function addToOperationsList(bulkOperation, docType, document) {
// Get the bsonSize
const bsonSize = bson.calculateObjectSize(document, {
checkKeys: false,
// Since we don't know what the user selected for BSON options here,
// err on the safe side, and check the size with ignoreUndefined: false.
ignoreUndefined: false
});
// Throw error if the doc is bigger than the max BSON size
if (bsonSize >= bulkOperation.s.maxBatchSizeBytes)
throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBatchSizeBytes);
// Create a new batch object if we don't have a current one
if (bulkOperation.s.currentBatch == null)
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
const maxKeySize = bulkOperation.s.maxKeySize;
// Check if we need to create a new batch
if (
bulkOperation.s.currentBatchSize + 1 >= bulkOperation.s.maxWriteBatchSize ||
bulkOperation.s.currentBatchSizeBytes + maxKeySize + bsonSize >=
bulkOperation.s.maxBatchSizeBytes ||
bulkOperation.s.currentBatch.batchType !== docType
) {
// Save the batch to the execution stack
bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
// Create a new batch
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
// Reset the current size trackers
bulkOperation.s.currentBatchSize = 0;
bulkOperation.s.currentBatchSizeBytes = 0;
}
if (docType === common.INSERT) {
bulkOperation.s.bulkResult.insertedIds.push({
index: bulkOperation.s.currentIndex,
_id: document._id
});
}
// We have an array of documents
if (Array.isArray(document)) {
throw toError('operation passed in cannot be an Array');
}
bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
bulkOperation.s.currentBatch.operations.push(document);
bulkOperation.s.currentBatchSize += 1;
bulkOperation.s.currentBatchSizeBytes += maxKeySize + bsonSize;
bulkOperation.s.currentIndex += 1;
// Return bulkOperation
return bulkOperation;
}
/**
* Create a new OrderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
* @class
* @extends BulkOperationBase
* @property {number} length Get the number of operations in the bulk.
* @return {OrderedBulkOperation} a OrderedBulkOperation instance.
*/
class OrderedBulkOperation extends BulkOperationBase {
constructor(topology, collection, options) {
options = options || {};
options = Object.assign(options, { addToOperationsList });
super(topology, collection, options, true);
}
}
/**
* Returns an unordered batch object
* @ignore
*/
function initializeOrderedBulkOp(topology, collection, options) {
return new OrderedBulkOperation(topology, collection, options);
}
initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
module.exports = initializeOrderedBulkOp;
module.exports.Bulk = OrderedBulkOperation;
+117
View File
@@ -0,0 +1,117 @@
'use strict';
const common = require('./common');
const BulkOperationBase = common.BulkOperationBase;
const Batch = common.Batch;
const bson = common.bson;
const utils = require('../utils');
const toError = utils.toError;
/**
* Add to internal list of Operations
*
* @param {UnorderedBulkOperation} bulkOperation
* @param {number} docType number indicating the document type
* @param {object} document
* @return {UnorderedBulkOperation}
*/
function addToOperationsList(bulkOperation, docType, document) {
// Get the bsonSize
const bsonSize = bson.calculateObjectSize(document, {
checkKeys: false,
// Since we don't know what the user selected for BSON options here,
// err on the safe side, and check the size with ignoreUndefined: false.
ignoreUndefined: false
});
// Throw error if the doc is bigger than the max BSON size
if (bsonSize >= bulkOperation.s.maxBatchSizeBytes)
throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBatchSizeBytes);
// Holds the current batch
bulkOperation.s.currentBatch = null;
// Get the right type of batch
if (docType === common.INSERT) {
bulkOperation.s.currentBatch = bulkOperation.s.currentInsertBatch;
} else if (docType === common.UPDATE) {
bulkOperation.s.currentBatch = bulkOperation.s.currentUpdateBatch;
} else if (docType === common.REMOVE) {
bulkOperation.s.currentBatch = bulkOperation.s.currentRemoveBatch;
}
const maxKeySize = bulkOperation.s.maxKeySize;
// Create a new batch object if we don't have a current one
if (bulkOperation.s.currentBatch == null)
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
// Check if we need to create a new batch
if (
bulkOperation.s.currentBatch.size + 1 >= bulkOperation.s.maxWriteBatchSize ||
bulkOperation.s.currentBatch.sizeBytes + maxKeySize + bsonSize >=
bulkOperation.s.maxBatchSizeBytes ||
bulkOperation.s.currentBatch.batchType !== docType
) {
// Save the batch to the execution stack
bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
// Create a new batch
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
}
// We have an array of documents
if (Array.isArray(document)) {
throw toError('operation passed in cannot be an Array');
}
bulkOperation.s.currentBatch.operations.push(document);
bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1;
// Save back the current Batch to the right type
if (docType === common.INSERT) {
bulkOperation.s.currentInsertBatch = bulkOperation.s.currentBatch;
bulkOperation.s.bulkResult.insertedIds.push({
index: bulkOperation.s.bulkResult.insertedIds.length,
_id: document._id
});
} else if (docType === common.UPDATE) {
bulkOperation.s.currentUpdateBatch = bulkOperation.s.currentBatch;
} else if (docType === common.REMOVE) {
bulkOperation.s.currentRemoveBatch = bulkOperation.s.currentBatch;
}
// Update current batch size
bulkOperation.s.currentBatch.size += 1;
bulkOperation.s.currentBatch.sizeBytes += maxKeySize + bsonSize;
// Return bulkOperation
return bulkOperation;
}
/**
* Create a new UnorderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
* @class
* @extends BulkOperationBase
* @property {number} length Get the number of operations in the bulk.
* @return {UnorderedBulkOperation} a UnorderedBulkOperation instance.
*/
class UnorderedBulkOperation extends BulkOperationBase {
constructor(topology, collection, options) {
options = options || {};
options = Object.assign(options, { addToOperationsList });
super(topology, collection, options, false);
}
}
/**
* Returns an unordered batch object
* @ignore
*/
function initializeUnorderedBulkOp(topology, collection, options) {
return new UnorderedBulkOperation(topology, collection, options);
}
initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
module.exports = initializeUnorderedBulkOp;
module.exports.Bulk = UnorderedBulkOperation;
+576
View File
@@ -0,0 +1,576 @@
'use strict';
const EventEmitter = require('events');
const isResumableError = require('./error').isResumableError;
const MongoError = require('./core').MongoError;
const Cursor = require('./cursor');
const relayEvents = require('./core/utils').relayEvents;
const maxWireVersion = require('./core/utils').maxWireVersion;
const AggregateOperation = require('./operations/aggregate');
const CHANGE_STREAM_OPTIONS = ['resumeAfter', 'startAfter', 'startAtOperationTime', 'fullDocument'];
const CURSOR_OPTIONS = ['batchSize', 'maxAwaitTimeMS', 'collation', 'readPreference'].concat(
CHANGE_STREAM_OPTIONS
);
const CHANGE_DOMAIN_TYPES = {
COLLECTION: Symbol('Collection'),
DATABASE: Symbol('Database'),
CLUSTER: Symbol('Cluster')
};
/**
* @typedef ResumeToken
* @description Represents the logical starting point for a new or resuming {@link ChangeStream} on the server.
* @see https://docs.mongodb.com/master/changeStreams/#change-stream-resume-token
*/
/**
* @typedef OperationTime
* @description Represents a specific point in time on a server. Can be retrieved by using {@link Db#command}
* @see https://docs.mongodb.com/manual/reference/method/db.runCommand/#response
*/
/**
* @typedef ChangeStreamOptions
* @description Options that can be passed to a ChangeStream. Note that startAfter, resumeAfter, and startAtOperationTime are all mutually exclusive, and the server will error if more than one is specified.
* @property {string} [fullDocument='default'] Allowed values: default, updateLookup. When set to updateLookup, the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.
* @property {number} [maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a change stream query.
* @property {ResumeToken} [resumeAfter] Allows you to start a changeStream after a specified event. See {@link https://docs.mongodb.com/master/changeStreams/#resumeafter-for-change-streams|ChangeStream documentation}.
* @property {ResumeToken} [startAfter] Similar to resumeAfter, but will allow you to start after an invalidated event. See {@link https://docs.mongodb.com/master/changeStreams/#startafter-for-change-streams|ChangeStream documentation}.
* @property {OperationTime} [startAtOperationTime] Will start the changeStream after the specified operationTime.
* @property {number} [batchSize] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
* @property {object} [collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
* @property {ReadPreference} [readPreference] The read preference. Defaults to the read preference of the database or collection. See {@link https://docs.mongodb.com/manual/reference/read-preference|read preference documentation}.
*/
/**
* Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}.
* @class ChangeStream
* @since 3.0.0
* @param {(MongoClient|Db|Collection)} parent The parent object that created this change stream
* @param {Array} pipeline An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents
* @param {ChangeStreamOptions} [options] Optional settings
* @fires ChangeStream#close
* @fires ChangeStream#change
* @fires ChangeStream#end
* @fires ChangeStream#error
* @fires ChangeStream#resumeTokenChanged
* @return {ChangeStream} a ChangeStream instance.
*/
class ChangeStream extends EventEmitter {
constructor(parent, pipeline, options) {
super();
const Collection = require('./collection');
const Db = require('./db');
const MongoClient = require('./mongo_client');
this.pipeline = pipeline || [];
this.options = options || {};
this.parent = parent;
this.namespace = parent.s.namespace;
if (parent instanceof Collection) {
this.type = CHANGE_DOMAIN_TYPES.COLLECTION;
this.topology = parent.s.db.serverConfig;
} else if (parent instanceof Db) {
this.type = CHANGE_DOMAIN_TYPES.DATABASE;
this.topology = parent.serverConfig;
} else if (parent instanceof MongoClient) {
this.type = CHANGE_DOMAIN_TYPES.CLUSTER;
this.topology = parent.topology;
} else {
throw new TypeError(
'parent provided to ChangeStream constructor is not an instance of Collection, Db, or MongoClient'
);
}
this.promiseLibrary = parent.s.promiseLibrary;
if (!this.options.readPreference && parent.s.readPreference) {
this.options.readPreference = parent.s.readPreference;
}
// Create contained Change Stream cursor
this.cursor = createChangeStreamCursor(this, options);
// Listen for any `change` listeners being added to ChangeStream
this.on('newListener', eventName => {
if (eventName === 'change' && this.cursor && this.listenerCount('change') === 0) {
this.cursor.on('data', change =>
processNewChange({ changeStream: this, change, eventEmitter: true })
);
}
});
// Listen for all `change` listeners being removed from ChangeStream
this.on('removeListener', eventName => {
if (eventName === 'change' && this.listenerCount('change') === 0 && this.cursor) {
this.cursor.removeAllListeners('data');
}
});
}
/**
* @property {ResumeToken} resumeToken
* The cached resume token that will be used to resume
* after the most recently returned change.
*/
get resumeToken() {
return this.cursor.resumeToken;
}
/**
* Check if there is any document still available in the Change Stream
* @function ChangeStream.prototype.hasNext
* @param {ChangeStream~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/
hasNext(callback) {
return this.cursor.hasNext(callback);
}
/**
* Get the next available document from the Change Stream, returns null if no more documents are available.
* @function ChangeStream.prototype.next
* @param {ChangeStream~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/
next(callback) {
var self = this;
if (this.isClosed()) {
if (callback) return callback(new Error('Change Stream is not open.'), null);
return self.promiseLibrary.reject(new Error('Change Stream is not open.'));
}
return this.cursor
.next()
.then(change => processNewChange({ changeStream: self, change, callback }))
.catch(error => processNewChange({ changeStream: self, error, callback }));
}
/**
* Is the cursor closed
* @method ChangeStream.prototype.isClosed
* @return {boolean}
*/
isClosed() {
if (this.cursor) {
return this.cursor.isClosed();
}
return true;
}
/**
* Close the Change Stream
* @method ChangeStream.prototype.close
* @param {ChangeStream~resultCallback} [callback] The result callback.
* @return {Promise} returns Promise if no callback passed
*/
close(callback) {
if (!this.cursor) {
if (callback) return callback();
return this.promiseLibrary.resolve();
}
// Tidy up the existing cursor
const cursor = this.cursor;
if (callback) {
return cursor.close(err => {
['data', 'close', 'end', 'error'].forEach(event => cursor.removeAllListeners(event));
delete this.cursor;
return callback(err);
});
}
const PromiseCtor = this.promiseLibrary || Promise;
return new PromiseCtor((resolve, reject) => {
cursor.close(err => {
['data', 'close', 'end', 'error'].forEach(event => cursor.removeAllListeners(event));
delete this.cursor;
if (err) return reject(err);
resolve();
});
});
}
/**
* This method pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream.
* @method
* @param {Writable} destination The destination for writing data
* @param {object} [options] {@link https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options|Pipe options}
* @return {null}
*/
pipe(destination, options) {
if (!this.pipeDestinations) {
this.pipeDestinations = [];
}
this.pipeDestinations.push(destination);
return this.cursor.pipe(destination, options);
}
/**
* This method will remove the hooks set up for a previous pipe() call.
* @param {Writable} [destination] The destination for writing data
* @return {null}
*/
unpipe(destination) {
if (this.pipeDestinations && this.pipeDestinations.indexOf(destination) > -1) {
this.pipeDestinations.splice(this.pipeDestinations.indexOf(destination), 1);
}
return this.cursor.unpipe(destination);
}
/**
* Return a modified Readable stream including a possible transform method.
* @method
* @param {object} [options] Optional settings.
* @param {function} [options.transform] A transformation method applied to each document emitted by the stream.
* @return {Cursor}
*/
stream(options) {
this.streamOptions = options;
return this.cursor.stream(options);
}
/**
* This method will cause a stream in flowing mode to stop emitting data events. Any data that becomes available will remain in the internal buffer.
* @return {null}
*/
pause() {
return this.cursor.pause();
}
/**
* This method will cause the readable stream to resume emitting data events.
* @return {null}
*/
resume() {
return this.cursor.resume();
}
}
class ChangeStreamCursor extends Cursor {
constructor(topology, operation, options) {
super(topology, operation, options);
options = options || {};
this._resumeToken = null;
this.startAtOperationTime = options.startAtOperationTime;
if (options.startAfter) {
this.resumeToken = options.startAfter;
} else if (options.resumeAfter) {
this.resumeToken = options.resumeAfter;
}
}
set resumeToken(token) {
this._resumeToken = token;
this.emit('resumeTokenChanged', token);
}
get resumeToken() {
return this._resumeToken;
}
get resumeOptions() {
const result = {};
for (const optionName of CURSOR_OPTIONS) {
if (this.options[optionName]) result[optionName] = this.options[optionName];
}
if (this.resumeToken || this.startAtOperationTime) {
['resumeAfter', 'startAfter', 'startAtOperationTime'].forEach(key => delete result[key]);
if (this.resumeToken) {
result.resumeAfter = this.resumeToken;
} else if (this.startAtOperationTime && maxWireVersion(this.server) >= 7) {
result.startAtOperationTime = this.startAtOperationTime;
}
}
return result;
}
_initializeCursor(callback) {
super._initializeCursor((err, result) => {
if (err) {
callback(err, null);
return;
}
const response = result.documents[0];
if (
this.startAtOperationTime == null &&
this.resumeAfter == null &&
this.startAfter == null &&
maxWireVersion(this.server) >= 7
) {
this.startAtOperationTime = response.operationTime;
}
const cursor = response.cursor;
if (cursor.postBatchResumeToken) {
this.cursorState.postBatchResumeToken = cursor.postBatchResumeToken;
if (cursor.firstBatch.length === 0) {
this.resumeToken = cursor.postBatchResumeToken;
}
}
this.emit('response');
callback(err, result);
});
}
_getMore(callback) {
super._getMore((err, response) => {
if (err) {
callback(err, null);
return;
}
const cursor = response.cursor;
if (cursor.postBatchResumeToken) {
this.cursorState.postBatchResumeToken = cursor.postBatchResumeToken;
if (cursor.nextBatch.length === 0) {
this.resumeToken = cursor.postBatchResumeToken;
}
}
this.emit('response');
callback(err, response);
});
}
}
/**
* @event ChangeStreamCursor#response
* internal event DO NOT USE
* @ignore
*/
// Create a new change stream cursor based on self's configuration
function createChangeStreamCursor(self, options) {
const changeStreamStageOptions = { fullDocument: options.fullDocument || 'default' };
applyKnownOptions(changeStreamStageOptions, options, CHANGE_STREAM_OPTIONS);
if (self.type === CHANGE_DOMAIN_TYPES.CLUSTER) {
changeStreamStageOptions.allChangesForCluster = true;
}
const pipeline = [{ $changeStream: changeStreamStageOptions }].concat(self.pipeline);
const cursorOptions = applyKnownOptions({}, options, CURSOR_OPTIONS);
const changeStreamCursor = new ChangeStreamCursor(
self.topology,
new AggregateOperation(self.parent, pipeline, options),
cursorOptions
);
relayEvents(changeStreamCursor, self, ['resumeTokenChanged', 'end', 'close']);
/**
* Fired for each new matching change in the specified namespace. Attaching a `change`
* event listener to a Change Stream will switch the stream into flowing mode. Data will
* then be passed as soon as it is available.
*
* @event ChangeStream#change
* @type {object}
*/
if (self.listenerCount('change') > 0) {
changeStreamCursor.on('data', function(change) {
processNewChange({ changeStream: self, change, eventEmitter: true });
});
}
/**
* Change stream close event
*
* @event ChangeStream#close
* @type {null}
*/
/**
* Change stream end event
*
* @event ChangeStream#end
* @type {null}
*/
/**
* Emitted each time the change stream stores a new resume token.
*
* @event ChangeStream#resumeTokenChanged
* @type {ResumeToken}
*/
/**
* Fired when the stream encounters an error.
*
* @event ChangeStream#error
* @type {Error}
*/
changeStreamCursor.on('error', function(error) {
processNewChange({ changeStream: self, error, eventEmitter: true });
});
if (self.pipeDestinations) {
const cursorStream = changeStreamCursor.stream(self.streamOptions);
for (let pipeDestination in self.pipeDestinations) {
cursorStream.pipe(pipeDestination);
}
}
return changeStreamCursor;
}
function applyKnownOptions(target, source, optionNames) {
optionNames.forEach(name => {
if (source[name]) {
target[name] = source[name];
}
});
return target;
}
// This method performs a basic server selection loop, satisfying the requirements of
// ChangeStream resumability until the new SDAM layer can be used.
const SELECTION_TIMEOUT = 30000;
function waitForTopologyConnected(topology, options, callback) {
setTimeout(() => {
if (options && options.start == null) options.start = process.hrtime();
const start = options.start || process.hrtime();
const timeout = options.timeout || SELECTION_TIMEOUT;
const readPreference = options.readPreference;
if (topology.isConnected({ readPreference })) return callback(null, null);
const hrElapsed = process.hrtime(start);
const elapsed = (hrElapsed[0] * 1e9 + hrElapsed[1]) / 1e6;
if (elapsed > timeout) return callback(new MongoError('Timed out waiting for connection'));
waitForTopologyConnected(topology, options, callback);
}, 3000); // this is an arbitrary wait time to allow SDAM to transition
}
// Handle new change events. This method brings together the routes from the callback, event emitter, and promise ways of using ChangeStream.
function processNewChange(args) {
const changeStream = args.changeStream;
const error = args.error;
const change = args.change;
const callback = args.callback;
const eventEmitter = args.eventEmitter || false;
// If the changeStream is closed, then it should not process a change.
if (changeStream.isClosed()) {
// We do not error in the eventEmitter case.
if (eventEmitter) {
return;
}
const error = new MongoError('ChangeStream is closed');
return typeof callback === 'function'
? callback(error, null)
: changeStream.promiseLibrary.reject(error);
}
const cursor = changeStream.cursor;
const topology = changeStream.topology;
const options = changeStream.cursor.options;
if (error) {
if (isResumableError(error) && !changeStream.attemptingResume) {
changeStream.attemptingResume = true;
// stop listening to all events from old cursor
['data', 'close', 'end', 'error'].forEach(event =>
changeStream.cursor.removeAllListeners(event)
);
// close internal cursor, ignore errors
changeStream.cursor.close();
// attempt recreating the cursor
if (eventEmitter) {
waitForTopologyConnected(topology, { readPreference: options.readPreference }, err => {
if (err) {
changeStream.emit('error', err);
changeStream.emit('close');
return;
}
changeStream.cursor = createChangeStreamCursor(changeStream, cursor.resumeOptions);
});
return;
}
if (callback) {
waitForTopologyConnected(topology, { readPreference: options.readPreference }, err => {
if (err) return callback(err, null);
changeStream.cursor = createChangeStreamCursor(changeStream, cursor.resumeOptions);
changeStream.next(callback);
});
return;
}
return new Promise((resolve, reject) => {
waitForTopologyConnected(topology, { readPreference: options.readPreference }, err => {
if (err) return reject(err);
resolve();
});
})
.then(
() => (changeStream.cursor = createChangeStreamCursor(changeStream, cursor.resumeOptions))
)
.then(() => changeStream.next());
}
if (eventEmitter) return changeStream.emit('error', error);
if (typeof callback === 'function') return callback(error, null);
return changeStream.promiseLibrary.reject(error);
}
changeStream.attemptingResume = false;
if (change && !change._id) {
const noResumeTokenError = new Error(
'A change stream document has been received that lacks a resume token (_id).'
);
if (eventEmitter) return changeStream.emit('error', noResumeTokenError);
if (typeof callback === 'function') return callback(noResumeTokenError, null);
return changeStream.promiseLibrary.reject(noResumeTokenError);
}
// cache the resume token
if (cursor.bufferedCount() === 0 && cursor.cursorState.postBatchResumeToken) {
cursor.resumeToken = cursor.cursorState.postBatchResumeToken;
} else {
cursor.resumeToken = change._id;
}
// wipe the startAtOperationTime if there was one so that there won't be a conflict
// between resumeToken and startAtOperationTime if we need to reconnect the cursor
changeStream.options.startAtOperationTime = undefined;
// Return the change
if (eventEmitter) return changeStream.emit('change', change);
if (typeof callback === 'function') return callback(error, change);
return changeStream.promiseLibrary.resolve(change);
}
/**
* The callback format for results
* @callback ChangeStream~resultCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {(object|null)} result The result object if the command was executed successfully.
*/
module.exports = ChangeStream;
+2062
View File
File diff suppressed because it is too large. Load diff
+269
View File
@@ -0,0 +1,269 @@
'use strict';
const ReadPreference = require('./core').ReadPreference;
const MongoError = require('./core').MongoError;
const Cursor = require('./cursor');
const CursorState = require('./core/cursor').CursorState;
/**
* @fileOverview The **CommandCursor** class is an internal class that embodies a
* generalized cursor based on a MongoDB command allowing for iteration over the
* results returned. It supports one by one document iteration, conversion to an
* array or can be iterated as a Node 0.10.X or higher stream
*
* **CommandCursor Cannot directly be instantiated**
* @example
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, client) {
* // Create a collection we want to drop later
* const col = client.db(dbName).collection('listCollectionsExample1');
* // Insert a bunch of documents
* col.insert([{a:1, b:1}
* , {a:2, b:2}, {a:3, b:3}
* , {a:4, b:4}], {w:1}, function(err, result) {
* test.equal(null, err);
* // List the database collections available
* db.listCollections().toArray(function(err, items) {
* test.equal(null, err);
* client.close();
* });
* });
* });
*/
/**
* Namespace provided by the browser.
* @external Readable
*/
/**
* Creates a new Command Cursor instance (INTERNAL TYPE, do not instantiate directly)
* @class CommandCursor
* @extends external:Readable
* @fires CommandCursor#data
* @fires CommandCursor#end
* @fires CommandCursor#close
* @fires CommandCursor#readable
* @return {CommandCursor} an CommandCursor instance.
*/
class CommandCursor extends Cursor {
constructor(topology, ns, cmd, options) {
super(topology, ns, cmd, options);
}
/**
* Set the ReadPreference for the cursor.
* @method
* @param {(string|ReadPreference)} readPreference The new read preference for the cursor.
* @throws {MongoError}
* @return {Cursor}
*/
setReadPreference(readPreference) {
if (this.s.state === CursorState.CLOSED || this.isDead()) {
throw MongoError.create({ message: 'Cursor is closed', driver: true });
}
if (this.s.state !== CursorState.INIT) {
throw MongoError.create({
message: 'cannot change cursor readPreference after cursor has been accessed',
driver: true
});
}
if (readPreference instanceof ReadPreference) {
this.options.readPreference = readPreference;
} else if (typeof readPreference === 'string') {
this.options.readPreference = new ReadPreference(readPreference);
} else {
throw new TypeError('Invalid read preference: ' + readPreference);
}
return this;
}
/**
* Set the batch size for the cursor.
* @method
* @param {number} value The batchSize for the cursor.
* @throws {MongoError}
* @return {CommandCursor}
*/
batchSize(value) {
if (this.s.state === CursorState.CLOSED || this.isDead()) {
throw MongoError.create({ message: 'Cursor is closed', driver: true });
}
if (typeof value !== 'number') {
throw MongoError.create({ message: 'batchSize requires an integer', driver: true });
}
if (this.cmd.cursor) {
this.cmd.cursor.batchSize = value;
}
this.setCursorBatchSize(value);
return this;
}
/**
* Add a maxTimeMS stage to the aggregation pipeline
* @method
* @param {number} value The state maxTimeMS value.
* @return {CommandCursor}
*/
maxTimeMS(value) {
if (this.topology.lastIsMaster().minWireVersion > 2) {
this.cmd.maxTimeMS = value;
}
return this;
}
/**
* Return the cursor logger
* @method
* @return {Logger} return the cursor logger
* @ignore
*/
getLogger() {
return this.logger;
}
}
// aliases
CommandCursor.prototype.get = CommandCursor.prototype.toArray;
/**
* CommandCursor stream data event, fired for each document in the cursor.
*
* @event CommandCursor#data
* @type {object}
*/
/**
* CommandCursor stream end event
*
* @event CommandCursor#end
* @type {null}
*/
/**
* CommandCursor stream close event
*
* @event CommandCursor#close
* @type {null}
*/
/**
* CommandCursor stream readable event
*
* @event CommandCursor#readable
* @type {null}
*/
/**
* Get the next available document from the cursor, returns null if no more documents are available.
* @function CommandCursor.prototype.next
* @param {CommandCursor~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/
/**
* Check if there is any document still available in the cursor
* @function CommandCursor.prototype.hasNext
* @param {CommandCursor~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/
/**
* The callback format for results
* @callback CommandCursor~toArrayResultCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {object[]} documents All the documents the satisfy the cursor.
*/
/**
* Returns an array of documents. The caller is responsible for making sure that there
* is enough memory to store the results. Note that the array only contain partial
* results when this cursor had been previously accessed.
* @method CommandCursor.prototype.toArray
* @param {CommandCursor~toArrayResultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/
/**
* The callback format for results
* @callback CommandCursor~resultCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {(object|null)} result The result object if the command was executed successfully.
*/
/**
* Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
* not all of the elements will be iterated if this cursor had been previously accessed.
* In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
* **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
* at any given time if batch size is specified. Otherwise, the caller is responsible
* for making sure that the entire result can fit the memory.
* @method CommandCursor.prototype.each
* @param {CommandCursor~resultCallback} callback The result callback.
* @throws {MongoError}
* @return {null}
*/
/**
* Close the cursor, sending a KillCursor command and emitting close.
* @method CommandCursor.prototype.close
* @param {CommandCursor~resultCallback} [callback] The result callback.
* @return {Promise} returns Promise if no callback passed
*/
/**
* Is the cursor closed
* @method CommandCursor.prototype.isClosed
* @return {boolean}
*/
/**
* Clone the cursor
* @function CommandCursor.prototype.clone
* @return {CommandCursor}
*/
/**
* Resets the cursor
* @function CommandCursor.prototype.rewind
* @return {CommandCursor}
*/
/**
* The callback format for the forEach iterator method
* @callback CommandCursor~iteratorCallback
* @param {Object} doc An emitted document for the iterator
*/
/**
* The callback error format for the forEach iterator method
* @callback CommandCursor~endCallback
* @param {MongoError} error An error instance representing the error during the execution.
*/
/*
* Iterates over all the documents for this cursor using the iterator, callback pattern.
* @method CommandCursor.prototype.forEach
* @param {CommandCursor~iteratorCallback} iterator The iteration callback.
* @param {CommandCursor~endCallback} callback The end callback.
* @throws {MongoError}
* @return {null}
*/
module.exports = CommandCursor;
+10
View File
@@ -0,0 +1,10 @@
'use strict';
module.exports = {
SYSTEM_NAMESPACE_COLLECTION: 'system.namespaces',
SYSTEM_INDEX_COLLECTION: 'system.indexes',
SYSTEM_PROFILE_COLLECTION: 'system.profile',
SYSTEM_USER_COLLECTION: 'system.users',
SYSTEM_COMMAND_COLLECTION: '$cmd',
SYSTEM_JS_COLLECTION: 'system.js'
};
+158
View File
@@ -0,0 +1,158 @@
'use strict';
const MongoError = require('../error').MongoError;
/**
* Creates a new AuthProvider, which dictates how to authenticate for a given
* mechanism.
* @class
*/
class AuthProvider {
constructor(bson) {
this.bson = bson;
this.authStore = [];
}
/**
* Authenticate
* @method
* @param {SendAuthCommand} sendAuthCommand Writes an auth command directly to a specific connection
* @param {Connection[]} connections Connections to authenticate using this authenticator
* @param {MongoCredentials} credentials Authentication credentials
* @param {authResultCallback} callback The callback to return the result from the authentication
*/
auth(sendAuthCommand, connections, credentials, callback) {
// Total connections
let count = connections.length;
if (count === 0) {
callback(null, null);
return;
}
// Valid connections
let numberOfValidConnections = 0;
let errorObject = null;
const execute = connection => {
this._authenticateSingleConnection(sendAuthCommand, connection, credentials, (err, r) => {
// Adjust count
count = count - 1;
// If we have an error
if (err) {
errorObject = new MongoError(err);
} else if (r && (r.$err || r.errmsg)) {
errorObject = new MongoError(r);
} else {
numberOfValidConnections = numberOfValidConnections + 1;
}
// Still authenticating against other connections.
if (count !== 0) {
return;
}
// We have authenticated all connections
if (numberOfValidConnections > 0) {
// Store the auth details
this.addCredentials(credentials);
// Return correct authentication
callback(null, true);
} else {
if (errorObject == null) {
errorObject = new MongoError(`failed to authenticate using ${credentials.mechanism}`);
}
callback(errorObject, false);
}
});
};
const executeInNextTick = _connection => process.nextTick(() => execute(_connection));
// For each connection we need to authenticate
while (connections.length > 0) {
executeInNextTick(connections.shift());
}
}
/**
* Implementation of a single connection authenticating. Is meant to be overridden.
* Will error if called directly
* @ignore
*/
_authenticateSingleConnection(/*sendAuthCommand, connection, credentials, callback*/) {
throw new Error('_authenticateSingleConnection must be overridden');
}
/**
* Adds credentials to store only if it does not exist
* @param {MongoCredentials} credentials credentials to add to store
*/
addCredentials(credentials) {
const found = this.authStore.some(cred => cred.equals(credentials));
if (!found) {
this.authStore.push(credentials);
}
}
/**
* Re authenticate pool
* @method
* @param {SendAuthCommand} sendAuthCommand Writes an auth command directly to a specific connection
* @param {Connection[]} connections Connections to authenticate using this authenticator
* @param {authResultCallback} callback The callback to return the result from the authentication
*/
reauthenticate(sendAuthCommand, connections, callback) {
const authStore = this.authStore.slice(0);
let count = authStore.length;
if (count === 0) {
return callback(null, null);
}
for (let i = 0; i < authStore.length; i++) {
this.auth(sendAuthCommand, connections, authStore[i], function(err) {
count = count - 1;
if (count === 0) {
callback(err, null);
}
});
}
}
/**
* Remove credentials that have been previously stored in the auth provider
* @method
* @param {string} source Name of database we are removing authStore details about
* @return {object}
*/
logout(source) {
this.authStore = this.authStore.filter(credentials => credentials.source !== source);
}
}
/**
* A function that writes authentication commands to a specific connection
* @callback SendAuthCommand
* @param {Connection} connection The connection to write to
* @param {Command} command A command with a toBin method that can be written to a connection
* @param {AuthWriteCallback} callback Callback called when command response is received
*/
/**
* A callback for a specific auth command
* @callback AuthWriteCallback
* @param {Error} err If command failed, an error from the server
* @param {object} r The response from the server
*/
/**
* This is a result from an authentication strategy
*
* @callback authResultCallback
* @param {error} error An error object. Set to null if no error present
* @param {boolean} result The result of the authentication process
*/
module.exports = { AuthProvider };
+29
View File
@@ -0,0 +1,29 @@
'use strict';
const MongoCR = require('./mongocr');
const X509 = require('./x509');
const Plain = require('./plain');
const GSSAPI = require('./gssapi');
const SSPI = require('./sspi');
const ScramSHA1 = require('./scram').ScramSHA1;
const ScramSHA256 = require('./scram').ScramSHA256;
/**
* Returns the default authentication providers.
*
* @param {BSON} bson Bson definition
* @returns {Object} a mapping of auth names to auth types
*/
function defaultAuthProviders(bson) {
return {
mongocr: new MongoCR(bson),
x509: new X509(bson),
plain: new Plain(bson),
gssapi: new GSSAPI(bson),
sspi: new SSPI(bson),
'scram-sha-1': new ScramSHA1(bson),
'scram-sha-256': new ScramSHA256(bson)
};
}
module.exports = { defaultAuthProviders };
+241
View File
@@ -0,0 +1,241 @@
'use strict';
const AuthProvider = require('./auth_provider').AuthProvider;
const retrieveKerberos = require('../utils').retrieveKerberos;
let kerberos;
/**
* Creates a new GSSAPI authentication mechanism
* @class
* @extends AuthProvider
*/
class GSSAPI extends AuthProvider {
/**
* Implementation of authentication for a single connection
* @override
*/
_authenticateSingleConnection(sendAuthCommand, connection, credentials, callback) {
const source = credentials.source;
const username = credentials.username;
const password = credentials.password;
const mechanismProperties = credentials.mechanismProperties;
const gssapiServiceName =
mechanismProperties['gssapiservicename'] ||
mechanismProperties['gssapiServiceName'] ||
'mongodb';
GSSAPIInitialize(
this,
kerberos.processes.MongoAuthProcess,
source,
username,
password,
source,
gssapiServiceName,
sendAuthCommand,
connection,
mechanismProperties,
callback
);
}
/**
* Authenticate
* @override
* @method
*/
auth(sendAuthCommand, connections, credentials, callback) {
if (kerberos == null) {
try {
kerberos = retrieveKerberos();
} catch (e) {
return callback(e, null);
}
}
super.auth(sendAuthCommand, connections, credentials, callback);
}
}
//
// Initialize step
var GSSAPIInitialize = function(
self,
MongoAuthProcess,
db,
username,
password,
authdb,
gssapiServiceName,
sendAuthCommand,
connection,
options,
callback
) {
// Create authenticator
var mongo_auth_process = new MongoAuthProcess(
connection.host,
connection.port,
gssapiServiceName,
options
);
// Perform initialization
mongo_auth_process.init(username, password, function(err) {
if (err) return callback(err, false);
// Perform the first step
mongo_auth_process.transition('', function(err, payload) {
if (err) return callback(err, false);
// Call the next db step
MongoDBGSSAPIFirstStep(
self,
mongo_auth_process,
payload,
db,
username,
password,
authdb,
sendAuthCommand,
connection,
callback
);
});
});
};
//
// Perform first step against mongodb
var MongoDBGSSAPIFirstStep = function(
self,
mongo_auth_process,
payload,
db,
username,
password,
authdb,
sendAuthCommand,
connection,
callback
) {
// Build the sasl start command
var command = {
saslStart: 1,
mechanism: 'GSSAPI',
payload: payload,
autoAuthorize: 1
};
// Write the commmand on the connection
sendAuthCommand(connection, '$external.$cmd', command, (err, doc) => {
if (err) return callback(err, false);
// Execute mongodb transition
mongo_auth_process.transition(doc.payload, function(err, payload) {
if (err) return callback(err, false);
// MongoDB API Second Step
MongoDBGSSAPISecondStep(
self,
mongo_auth_process,
payload,
doc,
db,
username,
password,
authdb,
sendAuthCommand,
connection,
callback
);
});
});
};
//
// Perform first step against mongodb
var MongoDBGSSAPISecondStep = function(
self,
mongo_auth_process,
payload,
doc,
db,
username,
password,
authdb,
sendAuthCommand,
connection,
callback
) {
// Build Authentication command to send to MongoDB
var command = {
saslContinue: 1,
conversationId: doc.conversationId,
payload: payload
};
// Execute the command
// Write the commmand on the connection
sendAuthCommand(connection, '$external.$cmd', command, (err, doc) => {
if (err) return callback(err, false);
// Call next transition for kerberos
mongo_auth_process.transition(doc.payload, function(err, payload) {
if (err) return callback(err, false);
// Call the last and third step
MongoDBGSSAPIThirdStep(
self,
mongo_auth_process,
payload,
doc,
db,
username,
password,
authdb,
sendAuthCommand,
connection,
callback
);
});
});
};
var MongoDBGSSAPIThirdStep = function(
self,
mongo_auth_process,
payload,
doc,
db,
username,
password,
authdb,
sendAuthCommand,
connection,
callback
) {
// Build final command
var command = {
saslContinue: 1,
conversationId: doc.conversationId,
payload: payload
};
// Execute the command
sendAuthCommand(connection, '$external.$cmd', command, (err, r) => {
if (err) return callback(err, false);
mongo_auth_process.transition(null, function(err) {
if (err) return callback(err, null);
callback(null, r);
});
});
};
/**
* This is a result from a authentication strategy
*
* @callback authResultCallback
* @param {error} error An error object. Set to null if no error present
* @param {boolean} result The result of the authentication process
*/
module.exports = GSSAPI;
+81
View File
@@ -0,0 +1,81 @@
'use strict';
// Resolves the default auth mechanism according to
// https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst
function getDefaultAuthMechanism(ismaster) {
if (ismaster) {
// If ismaster contains saslSupportedMechs, use scram-sha-256
// if it is available, else scram-sha-1
if (Array.isArray(ismaster.saslSupportedMechs)) {
return ismaster.saslSupportedMechs.indexOf('SCRAM-SHA-256') >= 0
? 'scram-sha-256'
: 'scram-sha-1';
}
// Fallback to legacy selection method. If wire version >= 3, use scram-sha-1
if (ismaster.maxWireVersion >= 3) {
return 'scram-sha-1';
}
}
// Default for wireprotocol < 3
return 'mongocr';
}
/**
* A representation of the credentials used by MongoDB
* @class
* @property {string} mechanism The method used to authenticate
* @property {string} [username] The username used for authentication
* @property {string} [password] The password used for authentication
* @property {string} [source] The database that the user should authenticate against
* @property {object} [mechanismProperties] Special properties used by some types of auth mechanisms
*/
class MongoCredentials {
/**
* Creates a new MongoCredentials object
* @param {object} [options]
* @param {string} [options.username] The username used for authentication
* @param {string} [options.password] The password used for authentication
* @param {string} [options.source] The database that the user should authenticate against
* @param {string} [options.mechanism] The method used to authenticate
* @param {object} [options.mechanismProperties] Special properties used by some types of auth mechanisms
*/
constructor(options) {
options = options || {};
this.username = options.username;
this.password = options.password;
this.source = options.source || options.db;
this.mechanism = options.mechanism || 'default';
this.mechanismProperties = options.mechanismProperties;
}
/**
* Determines if two MongoCredentials objects are equivalent
* @param {MongoCredentials} other another MongoCredentials object
* @returns {boolean} true if the two objects are equal.
*/
equals(other) {
return (
this.mechanism === other.mechanism &&
this.username === other.username &&
this.password === other.password &&
this.source === other.source
);
}
/**
* If the authentication mechanism is set to "default", resolves the authMechanism
* based on the server version and server supported sasl mechanisms.
*
* @param {Object} [ismaster] An ismaster response from the server
*/
resolveAuthMechanism(ismaster) {
// If the mechanism is not "default", then it does not need to be resolved
if (this.mechanism.toLowerCase() === 'default') {
this.mechanism = getDefaultAuthMechanism(ismaster);
}
}
}
module.exports = { MongoCredentials };
+51
View File
@@ -0,0 +1,51 @@
'use strict';
const crypto = require('crypto');
const AuthProvider = require('./auth_provider').AuthProvider;
/**
* Creates a new MongoCR authentication mechanism
*
* @extends AuthProvider
*/
class MongoCR extends AuthProvider {
/**
* Implementation of authentication for a single connection
* @override
*/
_authenticateSingleConnection(sendAuthCommand, connection, credentials, callback) {
const username = credentials.username;
const password = credentials.password;
const source = credentials.source;
sendAuthCommand(connection, `${source}.$cmd`, { getnonce: 1 }, (err, r) => {
let nonce = null;
let key = null;
// Get nonce
if (err == null) {
nonce = r.nonce;
// Use node md5 generator
let md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ':mongo:' + password, 'utf8');
const hash_password = md5.digest('hex');
// Final key
md5 = crypto.createHash('md5');
md5.update(nonce + username + hash_password, 'utf8');
key = md5.digest('hex');
}
const authenticateCommand = {
authenticate: 1,
user: username,
nonce,
key
};
sendAuthCommand(connection, `${source}.$cmd`, authenticateCommand, callback);
});
}
}
module.exports = MongoCR;
+35
View File
@@ -0,0 +1,35 @@
'use strict';
const retrieveBSON = require('../connection/utils').retrieveBSON;
const AuthProvider = require('./auth_provider').AuthProvider;
// TODO: can we get the Binary type from this.bson instead?
const BSON = retrieveBSON();
const Binary = BSON.Binary;
/**
* Creates a new Plain authentication mechanism
*
* @extends AuthProvider
*/
class Plain extends AuthProvider {
/**
* Implementation of authentication for a single connection
* @override
*/
_authenticateSingleConnection(sendAuthCommand, connection, credentials, callback) {
const username = credentials.username;
const password = credentials.password;
const payload = new Binary(`\x00${username}\x00${password}`);
const command = {
saslStart: 1,
mechanism: 'PLAIN',
payload: payload,
autoAuthorize: 1
};
sendAuthCommand(connection, '$external.$cmd', command, callback);
}
}
module.exports = Plain;
+293
View File
@@ -0,0 +1,293 @@
'use strict';
const crypto = require('crypto');
const Buffer = require('safe-buffer').Buffer;
const retrieveBSON = require('../connection/utils').retrieveBSON;
const MongoError = require('../error').MongoError;
const AuthProvider = require('./auth_provider').AuthProvider;
const BSON = retrieveBSON();
const Binary = BSON.Binary;
let saslprep;
try {
saslprep = require('saslprep');
} catch (e) {
// don't do anything;
}
var parsePayload = function(payload) {
var dict = {};
var parts = payload.split(',');
for (var i = 0; i < parts.length; i++) {
var valueParts = parts[i].split('=');
dict[valueParts[0]] = valueParts[1];
}
return dict;
};
var passwordDigest = function(username, password) {
if (typeof username !== 'string') throw new MongoError('username must be a string');
if (typeof password !== 'string') throw new MongoError('password must be a string');
if (password.length === 0) throw new MongoError('password cannot be empty');
// Use node md5 generator
var md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ':mongo:' + password, 'utf8');
return md5.digest('hex');
};
// XOR two buffers
function xor(a, b) {
if (!Buffer.isBuffer(a)) a = Buffer.from(a);
if (!Buffer.isBuffer(b)) b = Buffer.from(b);
const length = Math.max(a.length, b.length);
const res = [];
for (let i = 0; i < length; i += 1) {
res.push(a[i] ^ b[i]);
}
return Buffer.from(res).toString('base64');
}
function H(method, text) {
return crypto
.createHash(method)
.update(text)
.digest();
}
function HMAC(method, key, text) {
return crypto
.createHmac(method, key)
.update(text)
.digest();
}
var _hiCache = {};
var _hiCacheCount = 0;
var _hiCachePurge = function() {
_hiCache = {};
_hiCacheCount = 0;
};
const hiLengthMap = {
sha256: 32,
sha1: 20
};
function HI(data, salt, iterations, cryptoMethod) {
// omit the work if already generated
const key = [data, salt.toString('base64'), iterations].join('_');
if (_hiCache[key] !== undefined) {
return _hiCache[key];
}
// generate the salt
const saltedData = crypto.pbkdf2Sync(
data,
salt,
iterations,
hiLengthMap[cryptoMethod],
cryptoMethod
);
// cache a copy to speed up the next lookup, but prevent unbounded cache growth
if (_hiCacheCount >= 200) {
_hiCachePurge();
}
_hiCache[key] = saltedData;
_hiCacheCount += 1;
return saltedData;
}
/**
* Creates a new ScramSHA authentication mechanism
* @class
* @extends AuthProvider
*/
class ScramSHA extends AuthProvider {
constructor(bson, cryptoMethod) {
super(bson);
this.cryptoMethod = cryptoMethod || 'sha1';
}
static _getError(err, r) {
if (err) {
return err;
}
if (r.$err || r.errmsg) {
return new MongoError(r);
}
}
/**
* @ignore
*/
_executeScram(sendAuthCommand, connection, credentials, nonce, callback) {
let username = credentials.username;
const password = credentials.password;
const db = credentials.source;
const cryptoMethod = this.cryptoMethod;
let mechanism = 'SCRAM-SHA-1';
let processedPassword;
if (cryptoMethod === 'sha256') {
mechanism = 'SCRAM-SHA-256';
processedPassword = saslprep ? saslprep(password) : password;
} else {
try {
processedPassword = passwordDigest(username, password);
} catch (e) {
return callback(e);
}
}
// Clean up the user
username = username.replace('=', '=3D').replace(',', '=2C');
// NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8.
// Since the username is not sasl-prep-d, we need to do this here.
const firstBare = Buffer.concat([
Buffer.from('n=', 'utf8'),
Buffer.from(username, 'utf8'),
Buffer.from(',r=', 'utf8'),
Buffer.from(nonce, 'utf8')
]);
// Build command structure
const saslStartCmd = {
saslStart: 1,
mechanism,
payload: new Binary(Buffer.concat([Buffer.from('n,,', 'utf8'), firstBare])),
autoAuthorize: 1
};
// Write the commmand on the connection
sendAuthCommand(connection, `${db}.$cmd`, saslStartCmd, (err, r) => {
let tmpError = ScramSHA._getError(err, r);
if (tmpError) {
return callback(tmpError, null);
}
const payload = Buffer.isBuffer(r.payload) ? new Binary(r.payload) : r.payload;
const dict = parsePayload(payload.value());
const iterations = parseInt(dict.i, 10);
const salt = dict.s;
const rnonce = dict.r;
// Set up start of proof
const withoutProof = `c=biws,r=${rnonce}`;
const saltedPassword = HI(
processedPassword,
Buffer.from(salt, 'base64'),
iterations,
cryptoMethod
);
if (iterations && iterations < 4096) {
const error = new MongoError(`Server returned an invalid iteration count ${iterations}`);
return callback(error, false);
}
const clientKey = HMAC(cryptoMethod, saltedPassword, 'Client Key');
const storedKey = H(cryptoMethod, clientKey);
const authMessage = [firstBare, payload.value().toString('base64'), withoutProof].join(',');
const clientSignature = HMAC(cryptoMethod, storedKey, authMessage);
const clientProof = `p=${xor(clientKey, clientSignature)}`;
const clientFinal = [withoutProof, clientProof].join(',');
const saslContinueCmd = {
saslContinue: 1,
conversationId: r.conversationId,
payload: new Binary(Buffer.from(clientFinal))
};
sendAuthCommand(connection, `${db}.$cmd`, saslContinueCmd, (err, r) => {
if (!r || r.done !== false) {
return callback(err, r);
}
const retrySaslContinueCmd = {
saslContinue: 1,
conversationId: r.conversationId,
payload: Buffer.alloc(0)
};
sendAuthCommand(connection, `${db}.$cmd`, retrySaslContinueCmd, callback);
});
});
}
/**
* Implementation of authentication for a single connection
* @override
*/
_authenticateSingleConnection(sendAuthCommand, connection, credentials, callback) {
// Create a random nonce
crypto.randomBytes(24, (err, buff) => {
if (err) {
return callback(err, null);
}
return this._executeScram(
sendAuthCommand,
connection,
credentials,
buff.toString('base64'),
callback
);
});
}
/**
* Authenticate
* @override
* @method
*/
auth(sendAuthCommand, connections, credentials, callback) {
this._checkSaslprep();
super.auth(sendAuthCommand, connections, credentials, callback);
}
_checkSaslprep() {
const cryptoMethod = this.cryptoMethod;
if (cryptoMethod === 'sha256') {
if (!saslprep) {
console.warn('Warning: no saslprep library specified. Passwords will not be sanitized');
}
}
}
}
/**
* Creates a new ScramSHA1 authentication mechanism
* @class
* @extends ScramSHA
*/
class ScramSHA1 extends ScramSHA {
constructor(bson) {
super(bson, 'sha1');
}
}
/**
* Creates a new ScramSHA256 authentication mechanism
* @class
* @extends ScramSHA
*/
class ScramSHA256 extends ScramSHA {
constructor(bson) {
super(bson, 'sha256');
}
}
module.exports = { ScramSHA1, ScramSHA256 };
+131
View File
@@ -0,0 +1,131 @@
'use strict';
const AuthProvider = require('./auth_provider').AuthProvider;
const retrieveKerberos = require('../utils').retrieveKerberos;
let kerberos;
/**
* Creates a new SSPI authentication mechanism
* @class
* @extends AuthProvider
*/
class SSPI extends AuthProvider {
/**
* Implementation of authentication for a single connection
* @override
*/
_authenticateSingleConnection(sendAuthCommand, connection, credentials, callback) {
// TODO: Destructure this
const username = credentials.username;
const password = credentials.password;
const mechanismProperties = credentials.mechanismProperties;
const gssapiServiceName =
mechanismProperties['gssapiservicename'] ||
mechanismProperties['gssapiServiceName'] ||
'mongodb';
SSIPAuthenticate(
this,
kerberos.processes.MongoAuthProcess,
username,
password,
gssapiServiceName,
sendAuthCommand,
connection,
mechanismProperties,
callback
);
}
/**
* Authenticate
* @override
* @method
*/
auth(sendAuthCommand, connections, credentials, callback) {
if (kerberos == null) {
try {
kerberos = retrieveKerberos();
} catch (e) {
return callback(e, null);
}
}
super.auth(sendAuthCommand, connections, credentials, callback);
}
}
function SSIPAuthenticate(
self,
MongoAuthProcess,
username,
password,
gssapiServiceName,
sendAuthCommand,
connection,
options,
callback
) {
const authProcess = new MongoAuthProcess(
connection.host,
connection.port,
gssapiServiceName,
options
);
function authCommand(command, authCb) {
sendAuthCommand(connection, '$external.$cmd', command, authCb);
}
authProcess.init(username, password, err => {
if (err) return callback(err, false);
authProcess.transition('', (err, payload) => {
if (err) return callback(err, false);
const command = {
saslStart: 1,
mechanism: 'GSSAPI',
payload,
autoAuthorize: 1
};
authCommand(command, (err, doc) => {
if (err) return callback(err, false);
authProcess.transition(doc.payload, (err, payload) => {
if (err) return callback(err, false);
const command = {
saslContinue: 1,
conversationId: doc.conversationId,
payload
};
authCommand(command, (err, doc) => {
if (err) return callback(err, false);
authProcess.transition(doc.payload, (err, payload) => {
if (err) return callback(err, false);
const command = {
saslContinue: 1,
conversationId: doc.conversationId,
payload
};
authCommand(command, (err, response) => {
if (err) return callback(err, false);
authProcess.transition(null, err => {
if (err) return callback(err, null);
callback(null, response);
});
});
});
});
});
});
});
});
}
module.exports = SSPI;
+26
View File
@@ -0,0 +1,26 @@
'use strict';
const AuthProvider = require('./auth_provider').AuthProvider;
/**
* Creates a new X509 authentication mechanism
* @class
* @extends AuthProvider
*/
class X509 extends AuthProvider {
/**
* Implementation of authentication for a single connection
* @override
*/
_authenticateSingleConnection(sendAuthCommand, connection, credentials, callback) {
const username = credentials.username;
const command = { authenticate: 1, mechanism: 'MONGODB-X509' };
if (username) {
command.user = username;
}
sendAuthCommand(connection, '$external.$cmd', command, callback);
}
}
module.exports = X509;
+236
View File
@@ -0,0 +1,236 @@
'use strict';
const Msg = require('../connection/msg').Msg;
const KillCursor = require('../connection/commands').KillCursor;
const GetMore = require('../connection/commands').GetMore;
const calculateDurationInMs = require('../utils').calculateDurationInMs;
/** Commands that we want to redact because of the sensitive nature of their contents */
const SENSITIVE_COMMANDS = new Set([
'authenticate',
'saslStart',
'saslContinue',
'getnonce',
'createUser',
'updateUser',
'copydbgetnonce',
'copydbsaslstart',
'copydb'
]);
// helper methods
const extractCommandName = commandDoc => Object.keys(commandDoc)[0];
const namespace = command => command.ns;
const databaseName = command => command.ns.split('.')[0];
const collectionName = command => command.ns.split('.')[1];
const generateConnectionId = pool => `${pool.options.host}:${pool.options.port}`;
const maybeRedact = (commandName, result) => (SENSITIVE_COMMANDS.has(commandName) ? {} : result);
const LEGACY_FIND_QUERY_MAP = {
$query: 'filter',
$orderby: 'sort',
$hint: 'hint',
$comment: 'comment',
$maxScan: 'maxScan',
$max: 'max',
$min: 'min',
$returnKey: 'returnKey',
$showDiskLoc: 'showRecordId',
$maxTimeMS: 'maxTimeMS',
$snapshot: 'snapshot'
};
const LEGACY_FIND_OPTIONS_MAP = {
numberToSkip: 'skip',
numberToReturn: 'batchSize',
returnFieldsSelector: 'projection'
};
const OP_QUERY_KEYS = [
'tailable',
'oplogReplay',
'noCursorTimeout',
'awaitData',
'partial',
'exhaust'
];
/**
* Extract the actual command from the query, possibly upconverting if it's a legacy
* format
*
* @param {Object} command the command
*/
const extractCommand = command => {
if (command instanceof GetMore) {
return {
getMore: command.cursorId,
collection: collectionName(command),
batchSize: command.numberToReturn
};
}
if (command instanceof KillCursor) {
return {
killCursors: collectionName(command),
cursors: command.cursorIds
};
}
if (command instanceof Msg) {
return command.command;
}
if (command.query && command.query.$query) {
let result;
if (command.ns === 'admin.$cmd') {
// upconvert legacy command
result = Object.assign({}, command.query.$query);
} else {
// upconvert legacy find command
result = { find: collectionName(command) };
Object.keys(LEGACY_FIND_QUERY_MAP).forEach(key => {
if (typeof command.query[key] !== 'undefined')
result[LEGACY_FIND_QUERY_MAP[key]] = command.query[key];
});
}
Object.keys(LEGACY_FIND_OPTIONS_MAP).forEach(key => {
if (typeof command[key] !== 'undefined') result[LEGACY_FIND_OPTIONS_MAP[key]] = command[key];
});
OP_QUERY_KEYS.forEach(key => {
if (command[key]) result[key] = command[key];
});
if (typeof command.pre32Limit !== 'undefined') {
result.limit = command.pre32Limit;
}
if (command.query.$explain) {
return { explain: result };
}
return result;
}
return command.query ? command.query : command;
};
const extractReply = (command, reply) => {
if (command instanceof GetMore) {
return {
ok: 1,
cursor: {
id: reply.message.cursorId,
ns: namespace(command),
nextBatch: reply.message.documents
}
};
}
if (command instanceof KillCursor) {
return {
ok: 1,
cursorsUnknown: command.cursorIds
};
}
// is this a legacy find command?
if (command.query && typeof command.query.$query !== 'undefined') {
return {
ok: 1,
cursor: {
id: reply.message.cursorId,
ns: namespace(command),
firstBatch: reply.message.documents
}
};
}
// in the event of a `noResponse` command, just return
if (reply === null) return reply;
return reply.result;
};
/** An event indicating the start of a given command */
class CommandStartedEvent {
/**
* Create a started event
*
* @param {Pool} pool the pool that originated the command
* @param {Object} command the command
*/
constructor(pool, command) {
const cmd = extractCommand(command);
const commandName = extractCommandName(cmd);
// NOTE: remove in major revision, this is not spec behavior
if (SENSITIVE_COMMANDS.has(commandName)) {
this.commandObj = {};
this.commandObj[commandName] = true;
}
Object.assign(this, {
command: cmd,
databaseName: databaseName(command),
commandName,
requestId: command.requestId,
connectionId: generateConnectionId(pool)
});
}
}
/** An event indicating the success of a given command */
class CommandSucceededEvent {
/**
* Create a succeeded event
*
* @param {Pool} pool the pool that originated the command
* @param {Object} command the command
* @param {Object} reply the reply for this command from the server
* @param {Array} started a high resolution tuple timestamp of when the command was first sent, to calculate duration
*/
constructor(pool, command, reply, started) {
const cmd = extractCommand(command);
const commandName = extractCommandName(cmd);
Object.assign(this, {
duration: calculateDurationInMs(started),
commandName,
reply: maybeRedact(commandName, extractReply(command, reply)),
requestId: command.requestId,
connectionId: generateConnectionId(pool)
});
}
}
/** An event indicating the failure of a given command */
class CommandFailedEvent {
/**
* Create a failure event
*
* @param {Pool} pool the pool that originated the command
* @param {Object} command the command
* @param {MongoError|Object} error the generated error or a server error response
* @param {Array} started a high resolution tuple timestamp of when the command was first sent, to calculate duration
*/
constructor(pool, command, error, started) {
const cmd = extractCommand(command);
const commandName = extractCommandName(cmd);
Object.assign(this, {
duration: calculateDurationInMs(started),
commandName,
failure: maybeRedact(commandName, error),
requestId: command.requestId,
connectionId: generateConnectionId(pool)
});
}
}
module.exports = {
CommandStartedEvent,
CommandSucceededEvent,
CommandFailedEvent
};
+36
View File
@@ -0,0 +1,36 @@
'use strict';
/**
* Creates a new CommandResult instance
* @class
* @param {object} result CommandResult object
* @param {Connection} connection A connection instance associated with this result
* @return {CommandResult} A cursor instance
*/
var CommandResult = function(result, connection, message) {
this.result = result;
this.connection = connection;
this.message = message;
};
/**
* Convert CommandResult to JSON
* @method
* @return {object}
*/
CommandResult.prototype.toJSON = function() {
let result = Object.assign({}, this, this.result);
delete result.message;
return result;
};
/**
* Convert CommandResult to String representation
* @method
* @return {string}
*/
CommandResult.prototype.toString = function() {
return JSON.stringify(this.toJSON());
};
module.exports = CommandResult;
+507
View File
@@ -0,0 +1,507 @@
'use strict';
var retrieveBSON = require('./utils').retrieveBSON;
var BSON = retrieveBSON();
var Long = BSON.Long;
const Buffer = require('safe-buffer').Buffer;
// Incrementing request id
var _requestId = 0;
// Wire command operation ids
var opcodes = require('../wireprotocol/shared').opcodes;
// Query flags
var OPTS_TAILABLE_CURSOR = 2;
var OPTS_SLAVE = 4;
var OPTS_OPLOG_REPLAY = 8;
var OPTS_NO_CURSOR_TIMEOUT = 16;
var OPTS_AWAIT_DATA = 32;
var OPTS_EXHAUST = 64;
var OPTS_PARTIAL = 128;
// Response flags
var CURSOR_NOT_FOUND = 1;
var QUERY_FAILURE = 2;
var SHARD_CONFIG_STALE = 4;
var AWAIT_CAPABLE = 8;
/**************************************************************
* QUERY
**************************************************************/
var Query = function(bson, ns, query, options) {
var self = this;
// Basic options needed to be passed in
if (ns == null) throw new Error('ns must be specified for query');
if (query == null) throw new Error('query must be specified for query');
// Validate that we are not passing 0x00 in the collection name
if (ns.indexOf('\x00') !== -1) {
throw new Error('namespace cannot contain a null character');
}
// Basic options
this.bson = bson;
this.ns = ns;
this.query = query;
// Additional options
this.numberToSkip = options.numberToSkip || 0;
this.numberToReturn = options.numberToReturn || 0;
this.returnFieldSelector = options.returnFieldSelector || null;
this.requestId = Query.getRequestId();
// special case for pre-3.2 find commands, delete ASAP
this.pre32Limit = options.pre32Limit;
// Serialization option
this.serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
this.ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false;
this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16;
this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : true;
this.batchSize = self.numberToReturn;
// Flags
this.tailable = false;
this.slaveOk = typeof options.slaveOk === 'boolean' ? options.slaveOk : false;
this.oplogReplay = false;
this.noCursorTimeout = false;
this.awaitData = false;
this.exhaust = false;
this.partial = false;
};
//
// Assign a new request Id
Query.prototype.incRequestId = function() {
this.requestId = _requestId++;
};
//
// Assign a new request Id
Query.nextRequestId = function() {
return _requestId + 1;
};
//
// Uses a single allocated buffer for the process, avoiding multiple memory allocations
Query.prototype.toBin = function() {
var self = this;
var buffers = [];
var projection = null;
// Set up the flags
var flags = 0;
if (this.tailable) {
flags |= OPTS_TAILABLE_CURSOR;
}
if (this.slaveOk) {
flags |= OPTS_SLAVE;
}
if (this.oplogReplay) {
flags |= OPTS_OPLOG_REPLAY;
}
if (this.noCursorTimeout) {
flags |= OPTS_NO_CURSOR_TIMEOUT;
}
if (this.awaitData) {
flags |= OPTS_AWAIT_DATA;
}
if (this.exhaust) {
flags |= OPTS_EXHAUST;
}
if (this.partial) {
flags |= OPTS_PARTIAL;
}
// If batchSize is different to self.numberToReturn
if (self.batchSize !== self.numberToReturn) self.numberToReturn = self.batchSize;
// Allocate write protocol header buffer
var header = Buffer.alloc(
4 * 4 + // Header
4 + // Flags
Buffer.byteLength(self.ns) +
1 + // namespace
4 + // numberToSkip
4 // numberToReturn
);
// Add header to buffers
buffers.push(header);
// Serialize the query
var query = self.bson.serialize(this.query, {
checkKeys: this.checkKeys,
serializeFunctions: this.serializeFunctions,
ignoreUndefined: this.ignoreUndefined
});
// Add query document
buffers.push(query);
if (self.returnFieldSelector && Object.keys(self.returnFieldSelector).length > 0) {
// Serialize the projection document
projection = self.bson.serialize(this.returnFieldSelector, {
checkKeys: this.checkKeys,
serializeFunctions: this.serializeFunctions,
ignoreUndefined: this.ignoreUndefined
});
// Add projection document
buffers.push(projection);
}
// Total message size
var totalLength = header.length + query.length + (projection ? projection.length : 0);
// Set up the index
var index = 4;
// Write total document length
header[3] = (totalLength >> 24) & 0xff;
header[2] = (totalLength >> 16) & 0xff;
header[1] = (totalLength >> 8) & 0xff;
header[0] = totalLength & 0xff;
// Write header information requestId
header[index + 3] = (this.requestId >> 24) & 0xff;
header[index + 2] = (this.requestId >> 16) & 0xff;
header[index + 1] = (this.requestId >> 8) & 0xff;
header[index] = this.requestId & 0xff;
index = index + 4;
// Write header information responseTo
header[index + 3] = (0 >> 24) & 0xff;
header[index + 2] = (0 >> 16) & 0xff;
header[index + 1] = (0 >> 8) & 0xff;
header[index] = 0 & 0xff;
index = index + 4;
// Write header information OP_QUERY
header[index + 3] = (opcodes.OP_QUERY >> 24) & 0xff;
header[index + 2] = (opcodes.OP_QUERY >> 16) & 0xff;
header[index + 1] = (opcodes.OP_QUERY >> 8) & 0xff;
header[index] = opcodes.OP_QUERY & 0xff;
index = index + 4;
// Write header information flags
header[index + 3] = (flags >> 24) & 0xff;
header[index + 2] = (flags >> 16) & 0xff;
header[index + 1] = (flags >> 8) & 0xff;
header[index] = flags & 0xff;
index = index + 4;
// Write collection name
index = index + header.write(this.ns, index, 'utf8') + 1;
header[index - 1] = 0;
// Write header information flags numberToSkip
header[index + 3] = (this.numberToSkip >> 24) & 0xff;
header[index + 2] = (this.numberToSkip >> 16) & 0xff;
header[index + 1] = (this.numberToSkip >> 8) & 0xff;
header[index] = this.numberToSkip & 0xff;
index = index + 4;
// Write header information flags numberToReturn
header[index + 3] = (this.numberToReturn >> 24) & 0xff;
header[index + 2] = (this.numberToReturn >> 16) & 0xff;
header[index + 1] = (this.numberToReturn >> 8) & 0xff;
header[index] = this.numberToReturn & 0xff;
index = index + 4;
// Return the buffers
return buffers;
};
Query.getRequestId = function() {
return ++_requestId;
};
/**************************************************************
* GETMORE
**************************************************************/
var GetMore = function(bson, ns, cursorId, opts) {
opts = opts || {};
this.numberToReturn = opts.numberToReturn || 0;
this.requestId = _requestId++;
this.bson = bson;
this.ns = ns;
this.cursorId = cursorId;
};
//
// Uses a single allocated buffer for the process, avoiding multiple memory allocations
GetMore.prototype.toBin = function() {
var length = 4 + Buffer.byteLength(this.ns) + 1 + 4 + 8 + 4 * 4;
// Create command buffer
var index = 0;
// Allocate buffer
var _buffer = Buffer.alloc(length);
// Write header information
// index = write32bit(index, _buffer, length);
_buffer[index + 3] = (length >> 24) & 0xff;
_buffer[index + 2] = (length >> 16) & 0xff;
_buffer[index + 1] = (length >> 8) & 0xff;
_buffer[index] = length & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, requestId);
_buffer[index + 3] = (this.requestId >> 24) & 0xff;
_buffer[index + 2] = (this.requestId >> 16) & 0xff;
_buffer[index + 1] = (this.requestId >> 8) & 0xff;
_buffer[index] = this.requestId & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, 0);
_buffer[index + 3] = (0 >> 24) & 0xff;
_buffer[index + 2] = (0 >> 16) & 0xff;
_buffer[index + 1] = (0 >> 8) & 0xff;
_buffer[index] = 0 & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, OP_GETMORE);
_buffer[index + 3] = (opcodes.OP_GETMORE >> 24) & 0xff;
_buffer[index + 2] = (opcodes.OP_GETMORE >> 16) & 0xff;
_buffer[index + 1] = (opcodes.OP_GETMORE >> 8) & 0xff;
_buffer[index] = opcodes.OP_GETMORE & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, 0);
_buffer[index + 3] = (0 >> 24) & 0xff;
_buffer[index + 2] = (0 >> 16) & 0xff;
_buffer[index + 1] = (0 >> 8) & 0xff;
_buffer[index] = 0 & 0xff;
index = index + 4;
// Write collection name
index = index + _buffer.write(this.ns, index, 'utf8') + 1;
_buffer[index - 1] = 0;
// Write batch size
// index = write32bit(index, _buffer, numberToReturn);
_buffer[index + 3] = (this.numberToReturn >> 24) & 0xff;
_buffer[index + 2] = (this.numberToReturn >> 16) & 0xff;
_buffer[index + 1] = (this.numberToReturn >> 8) & 0xff;
_buffer[index] = this.numberToReturn & 0xff;
index = index + 4;
// Write cursor id
// index = write32bit(index, _buffer, cursorId.getLowBits());
_buffer[index + 3] = (this.cursorId.getLowBits() >> 24) & 0xff;
_buffer[index + 2] = (this.cursorId.getLowBits() >> 16) & 0xff;
_buffer[index + 1] = (this.cursorId.getLowBits() >> 8) & 0xff;
_buffer[index] = this.cursorId.getLowBits() & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, cursorId.getHighBits());
_buffer[index + 3] = (this.cursorId.getHighBits() >> 24) & 0xff;
_buffer[index + 2] = (this.cursorId.getHighBits() >> 16) & 0xff;
_buffer[index + 1] = (this.cursorId.getHighBits() >> 8) & 0xff;
_buffer[index] = this.cursorId.getHighBits() & 0xff;
index = index + 4;
// Return buffer
return _buffer;
};
/**************************************************************
* KILLCURSOR
**************************************************************/
var KillCursor = function(bson, ns, cursorIds) {
this.ns = ns;
this.requestId = _requestId++;
this.cursorIds = cursorIds;
};
//
// Uses a single allocated buffer for the process, avoiding multiple memory allocations
KillCursor.prototype.toBin = function() {
var length = 4 + 4 + 4 * 4 + this.cursorIds.length * 8;
// Create command buffer
var index = 0;
var _buffer = Buffer.alloc(length);
// Write header information
// index = write32bit(index, _buffer, length);
_buffer[index + 3] = (length >> 24) & 0xff;
_buffer[index + 2] = (length >> 16) & 0xff;
_buffer[index + 1] = (length >> 8) & 0xff;
_buffer[index] = length & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, requestId);
_buffer[index + 3] = (this.requestId >> 24) & 0xff;
_buffer[index + 2] = (this.requestId >> 16) & 0xff;
_buffer[index + 1] = (this.requestId >> 8) & 0xff;
_buffer[index] = this.requestId & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, 0);
_buffer[index + 3] = (0 >> 24) & 0xff;
_buffer[index + 2] = (0 >> 16) & 0xff;
_buffer[index + 1] = (0 >> 8) & 0xff;
_buffer[index] = 0 & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, OP_KILL_CURSORS);
_buffer[index + 3] = (opcodes.OP_KILL_CURSORS >> 24) & 0xff;
_buffer[index + 2] = (opcodes.OP_KILL_CURSORS >> 16) & 0xff;
_buffer[index + 1] = (opcodes.OP_KILL_CURSORS >> 8) & 0xff;
_buffer[index] = opcodes.OP_KILL_CURSORS & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, 0);
_buffer[index + 3] = (0 >> 24) & 0xff;
_buffer[index + 2] = (0 >> 16) & 0xff;
_buffer[index + 1] = (0 >> 8) & 0xff;
_buffer[index] = 0 & 0xff;
index = index + 4;
// Write batch size
// index = write32bit(index, _buffer, this.cursorIds.length);
_buffer[index + 3] = (this.cursorIds.length >> 24) & 0xff;
_buffer[index + 2] = (this.cursorIds.length >> 16) & 0xff;
_buffer[index + 1] = (this.cursorIds.length >> 8) & 0xff;
_buffer[index] = this.cursorIds.length & 0xff;
index = index + 4;
// Write all the cursor ids into the array
for (var i = 0; i < this.cursorIds.length; i++) {
// Write cursor id
// index = write32bit(index, _buffer, cursorIds[i].getLowBits());
_buffer[index + 3] = (this.cursorIds[i].getLowBits() >> 24) & 0xff;
_buffer[index + 2] = (this.cursorIds[i].getLowBits() >> 16) & 0xff;
_buffer[index + 1] = (this.cursorIds[i].getLowBits() >> 8) & 0xff;
_buffer[index] = this.cursorIds[i].getLowBits() & 0xff;
index = index + 4;
// index = write32bit(index, _buffer, cursorIds[i].getHighBits());
_buffer[index + 3] = (this.cursorIds[i].getHighBits() >> 24) & 0xff;
_buffer[index + 2] = (this.cursorIds[i].getHighBits() >> 16) & 0xff;
_buffer[index + 1] = (this.cursorIds[i].getHighBits() >> 8) & 0xff;
_buffer[index] = this.cursorIds[i].getHighBits() & 0xff;
index = index + 4;
}
// Return buffer
return _buffer;
};
var Response = function(bson, message, msgHeader, msgBody, opts) {
opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false };
this.parsed = false;
this.raw = message;
this.data = msgBody;
this.bson = bson;
this.opts = opts;
// Read the message header
this.length = msgHeader.length;
this.requestId = msgHeader.requestId;
this.responseTo = msgHeader.responseTo;
this.opCode = msgHeader.opCode;
this.fromCompressed = msgHeader.fromCompressed;
// Read the message body
this.responseFlags = msgBody.readInt32LE(0);
this.cursorId = new Long(msgBody.readInt32LE(4), msgBody.readInt32LE(8));
this.startingFrom = msgBody.readInt32LE(12);
this.numberReturned = msgBody.readInt32LE(16);
// Preallocate document array
this.documents = new Array(this.numberReturned);
// Flag values
this.cursorNotFound = (this.responseFlags & CURSOR_NOT_FOUND) !== 0;
this.queryFailure = (this.responseFlags & QUERY_FAILURE) !== 0;
this.shardConfigStale = (this.responseFlags & SHARD_CONFIG_STALE) !== 0;
this.awaitCapable = (this.responseFlags & AWAIT_CAPABLE) !== 0;
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;
};
Response.prototype.isParsed = function() {
return this.parsed;
};
Response.prototype.parse = function(options) {
// Don't parse again if not needed
if (this.parsed) return;
options = options || {};
// Allow the return of raw documents instead of parsing
var raw = options.raw || false;
var documentsReturnedIn = options.documentsReturnedIn || null;
var promoteLongs =
typeof options.promoteLongs === 'boolean' ? options.promoteLongs : this.opts.promoteLongs;
var promoteValues =
typeof options.promoteValues === 'boolean' ? options.promoteValues : this.opts.promoteValues;
var promoteBuffers =
typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : this.opts.promoteBuffers;
var bsonSize, _options;
// Set up the options
_options = {
promoteLongs: promoteLongs,
promoteValues: promoteValues,
promoteBuffers: promoteBuffers
};
// Position within OP_REPLY at which documents start
// (See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#wire-op-reply)
this.index = 20;
//
// Parse Body
//
for (var i = 0; i < this.numberReturned; i++) {
bsonSize =
this.data[this.index] |
(this.data[this.index + 1] << 8) |
(this.data[this.index + 2] << 16) |
(this.data[this.index + 3] << 24);
// If we have raw results specified slice the return document
if (raw) {
this.documents[i] = this.data.slice(this.index, this.index + bsonSize);
} else {
this.documents[i] = this.bson.deserialize(
this.data.slice(this.index, this.index + bsonSize),
_options
);
}
// Adjust the index
this.index = this.index + bsonSize;
}
if (this.documents.length === 1 && documentsReturnedIn != null && raw) {
const fieldsAsRaw = {};
fieldsAsRaw[documentsReturnedIn] = true;
_options.fieldsAsRaw = fieldsAsRaw;
const doc = this.bson.deserialize(this.documents[0], _options);
this.documents = [doc];
}
// Set parsed
this.parsed = true;
};
module.exports = {
Query: Query,
GetMore: GetMore,
Response: Response,
KillCursor: KillCursor
};
+370
View File
@@ -0,0 +1,370 @@
'use strict';
const net = require('net');
const tls = require('tls');
const Connection = require('./connection');
const Query = require('./commands').Query;
const createClientInfo = require('../topologies/shared').createClientInfo;
const MongoError = require('../error').MongoError;
const MongoNetworkError = require('../error').MongoNetworkError;
const defaultAuthProviders = require('../auth/defaultAuthProviders').defaultAuthProviders;
const WIRE_CONSTANTS = require('../wireprotocol/constants');
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
const MAX_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_SERVER_VERSION;
const MIN_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_WIRE_VERSION;
const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION;
let AUTH_PROVIDERS;
function connect(options, callback) {
if (AUTH_PROVIDERS == null) {
AUTH_PROVIDERS = defaultAuthProviders(options.bson);
}
if (options.family !== void 0) {
makeConnection(options.family, options, (err, socket) => {
if (err) {
callback(err, socket); // in the error case, `socket` is the originating error event name
return;
}
performInitialHandshake(new Connection(socket, options), options, callback);
});
return;
}
return makeConnection(6, options, (err, ipv6Socket) => {
if (err) {
makeConnection(4, options, (err, ipv4Socket) => {
if (err) {
callback(err, ipv4Socket); // in the error case, `ipv4Socket` is the originating error event name
return;
}
performInitialHandshake(new Connection(ipv4Socket, options), options, callback);
});
return;
}
performInitialHandshake(new Connection(ipv6Socket, options), options, callback);
});
}
function getSaslSupportedMechs(options) {
if (!(options && options.credentials)) {
return {};
}
const credentials = options.credentials;
// TODO: revisit whether or not items like `options.user` and `options.dbName` should be checked here
const authMechanism = credentials.mechanism;
const authSource = credentials.source || options.dbName || 'admin';
const user = credentials.username || options.user;
if (typeof authMechanism === 'string' && authMechanism.toUpperCase() !== 'DEFAULT') {
return {};
}
if (!user) {
return {};
}
return { saslSupportedMechs: `${authSource}.${user}` };
}
function checkSupportedServer(ismaster, options) {
const serverVersionHighEnough =
ismaster &&
typeof ismaster.maxWireVersion === 'number' &&
ismaster.maxWireVersion >= MIN_SUPPORTED_WIRE_VERSION;
const serverVersionLowEnough =
ismaster &&
typeof ismaster.minWireVersion === 'number' &&
ismaster.minWireVersion <= MAX_SUPPORTED_WIRE_VERSION;
if (serverVersionHighEnough) {
if (serverVersionLowEnough) {
return null;
}
const message = `Server at ${options.host}:${options.port} reports minimum wire version ${
ismaster.minWireVersion
}, but this version of the Node.js Driver requires at most ${MAX_SUPPORTED_WIRE_VERSION} (MongoDB ${MAX_SUPPORTED_SERVER_VERSION})`;
return new MongoError(message);
}
const message = `Server at ${options.host}:${
options.port
} reports maximum wire version ${ismaster.maxWireVersion ||
0}, but this version of the Node.js Driver requires at least ${MIN_SUPPORTED_WIRE_VERSION} (MongoDB ${MIN_SUPPORTED_SERVER_VERSION})`;
return new MongoError(message);
}
function performInitialHandshake(conn, options, _callback) {
const callback = function(err, ret) {
if (err && conn) {
conn.destroy();
}
_callback(err, ret);
};
let compressors = [];
if (options.compression && options.compression.compressors) {
compressors = options.compression.compressors;
}
const handshakeDoc = Object.assign(
{
ismaster: true,
client: createClientInfo(options),
compression: compressors
},
getSaslSupportedMechs(options)
);
const start = new Date().getTime();
runCommand(conn, 'admin.$cmd', handshakeDoc, options, (err, ismaster) => {
if (err) {
callback(err, null);
return;
}
if (ismaster.ok === 0) {
callback(new MongoError(ismaster), null);
return;
}
const supportedServerErr = checkSupportedServer(ismaster, options);
if (supportedServerErr) {
callback(supportedServerErr, null);
return;
}
// resolve compression
if (ismaster.compression) {
const agreedCompressors = compressors.filter(
compressor => ismaster.compression.indexOf(compressor) !== -1
);
if (agreedCompressors.length) {
conn.agreedCompressor = agreedCompressors[0];
}
if (options.compression && options.compression.zlibCompressionLevel) {
conn.zlibCompressionLevel = options.compression.zlibCompressionLevel;
}
}
// NOTE: This is metadata attached to the connection while porting away from
// handshake being done in the `Server` class. Likely, it should be
// relocated, or at very least restructured.
conn.ismaster = ismaster;
conn.lastIsMasterMS = new Date().getTime() - start;
const credentials = options.credentials;
if (!ismaster.arbiterOnly && credentials) {
credentials.resolveAuthMechanism(ismaster);
authenticate(conn, credentials, callback);
return;
}
callback(null, conn);
});
}
const LEGAL_SSL_SOCKET_OPTIONS = [
'pfx',
'key',
'passphrase',
'cert',
'ca',
'ciphers',
'NPNProtocols',
'ALPNProtocols',
'servername',
'ecdhCurve',
'secureProtocol',
'secureContext',
'session',
'minDHSize',
'crl',
'rejectUnauthorized'
];
function parseConnectOptions(family, options) {
const host = typeof options.host === 'string' ? options.host : 'localhost';
if (host.indexOf('/') !== -1) {
return { path: host };
}
const result = {
family,
host,
port: typeof options.port === 'number' ? options.port : 27017,
rejectUnauthorized: false
};
return result;
}
function parseSslOptions(family, options) {
const result = parseConnectOptions(family, options);
// Merge in valid SSL options
for (const name in options) {
if (options[name] != null && LEGAL_SSL_SOCKET_OPTIONS.indexOf(name) !== -1) {
result[name] = options[name];
}
}
// Override checkServerIdentity behavior
if (options.checkServerIdentity === false) {
// Skip the identiy check by retuning undefined as per node documents
// https://nodejs.org/api/tls.html#tls_tls_connect_options_callback
result.checkServerIdentity = function() {
return undefined;
};
} else if (typeof options.checkServerIdentity === 'function') {
result.checkServerIdentity = options.checkServerIdentity;
}
// Set default sni servername to be the same as host
if (result.servername == null) {
result.servername = result.host;
}
return result;
}
function makeConnection(family, options, _callback) {
const useSsl = typeof options.ssl === 'boolean' ? options.ssl : false;
const keepAlive = typeof options.keepAlive === 'boolean' ? options.keepAlive : true;
let keepAliveInitialDelay =
typeof options.keepAliveInitialDelay === 'number' ? options.keepAliveInitialDelay : 300000;
const noDelay = typeof options.noDelay === 'boolean' ? options.noDelay : true;
const connectionTimeout =
typeof options.connectionTimeout === 'number' ? options.connectionTimeout : 30000;
const socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000;
const rejectUnauthorized =
typeof options.rejectUnauthorized === 'boolean' ? options.rejectUnauthorized : true;
if (keepAliveInitialDelay > socketTimeout) {
keepAliveInitialDelay = Math.round(socketTimeout / 2);
}
let socket;
const callback = function(err, ret) {
if (err && socket) {
socket.destroy();
}
_callback(err, ret);
};
try {
if (useSsl) {
socket = tls.connect(parseSslOptions(family, options));
if (typeof socket.disableRenegotiation === 'function') {
socket.disableRenegotiation();
}
} else {
socket = net.createConnection(parseConnectOptions(family, options));
}
} catch (err) {
return callback(err);
}
socket.setKeepAlive(keepAlive, keepAliveInitialDelay);
socket.setTimeout(connectionTimeout);
socket.setNoDelay(noDelay);
const errorEvents = ['error', 'close', 'timeout', 'parseError'];
function errorHandler(eventName) {
return err => {
errorEvents.forEach(event => socket.removeAllListeners(event));
socket.removeListener('connect', connectHandler);
callback(connectionFailureError(eventName, err), eventName);
};
}
function connectHandler() {
errorEvents.forEach(event => socket.removeAllListeners(event));
if (socket.authorizationError && rejectUnauthorized) {
return callback(socket.authorizationError);
}
socket.setTimeout(socketTimeout);
callback(null, socket);
}
socket.once('error', errorHandler('error'));
socket.once('close', errorHandler('close'));
socket.once('timeout', errorHandler('timeout'));
socket.once('parseError', errorHandler('parseError'));
socket.once('connect', connectHandler);
}
const CONNECTION_ERROR_EVENTS = ['error', 'close', 'timeout', 'parseError'];
function runCommand(conn, ns, command, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
const socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000;
const bson = conn.options.bson;
const query = new Query(bson, ns, command, {
numberToSkip: 0,
numberToReturn: 1
});
function errorHandler(err) {
conn.resetSocketTimeout();
CONNECTION_ERROR_EVENTS.forEach(eventName => conn.removeListener(eventName, errorHandler));
conn.removeListener('message', messageHandler);
callback(err, null);
}
function messageHandler(msg) {
if (msg.responseTo !== query.requestId) {
return;
}
conn.resetSocketTimeout();
CONNECTION_ERROR_EVENTS.forEach(eventName => conn.removeListener(eventName, errorHandler));
conn.removeListener('message', messageHandler);
msg.parse({ promoteValues: true });
callback(null, msg.documents[0]);
}
conn.setSocketTimeout(socketTimeout);
CONNECTION_ERROR_EVENTS.forEach(eventName => conn.once(eventName, errorHandler));
conn.on('message', messageHandler);
conn.write(query.toBin());
}
function authenticate(conn, credentials, callback) {
const mechanism = credentials.mechanism;
if (!AUTH_PROVIDERS[mechanism]) {
callback(new MongoError(`authMechanism '${mechanism}' not supported`));
return;
}
const provider = AUTH_PROVIDERS[mechanism];
provider.auth(runCommand, [conn], credentials, err => {
if (err) return callback(err);
callback(null, conn);
});
}
function connectionFailureError(type, err) {
switch (type) {
case 'error':
return new MongoNetworkError(err);
case 'timeout':
return new MongoNetworkError(`connection timed out`);
case 'close':
return new MongoNetworkError(`connection closed`);
default:
return new MongoNetworkError(`unknown network error`);
}
}
module.exports = connect;
+624
View File
@@ -0,0 +1,624 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const crypto = require('crypto');
const debugOptions = require('./utils').debugOptions;
const parseHeader = require('../wireprotocol/shared').parseHeader;
const decompress = require('../wireprotocol/compression').decompress;
const Response = require('./commands').Response;
const BinMsg = require('./msg').BinMsg;
const MongoNetworkError = require('../error').MongoNetworkError;
const MongoError = require('../error').MongoError;
const Logger = require('./logger');
const OP_COMPRESSED = require('../wireprotocol/shared').opcodes.OP_COMPRESSED;
const OP_MSG = require('../wireprotocol/shared').opcodes.OP_MSG;
const MESSAGE_HEADER_SIZE = require('../wireprotocol/shared').MESSAGE_HEADER_SIZE;
const Buffer = require('safe-buffer').Buffer;
let _id = 0;
const DEFAULT_MAX_BSON_MESSAGE_SIZE = 1024 * 1024 * 16 * 4;
const DEBUG_FIELDS = [
'host',
'port',
'size',
'keepAlive',
'keepAliveInitialDelay',
'noDelay',
'connectionTimeout',
'socketTimeout',
'ssl',
'ca',
'crl',
'cert',
'rejectUnauthorized',
'promoteLongs',
'promoteValues',
'promoteBuffers',
'checkServerIdentity'
];
let connectionAccountingSpy = undefined;
let connectionAccounting = false;
let connections = {};
/**
* A class representing a single connection to a MongoDB server
*
* @fires Connection#connect
* @fires Connection#close
* @fires Connection#error
* @fires Connection#timeout
* @fires Connection#parseError
* @fires Connection#message
*/
class Connection extends EventEmitter {
/**
* Creates a new Connection instance
*
* @param {Socket} socket The socket this connection wraps
* @param {Object} [options] Optional settings
* @param {string} [options.host] The host the socket is connected to
* @param {number} [options.port] The port used for the socket connection
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
* @param {number} [options.keepAliveInitialDelay=300000] Initial delay before TCP keep alive enabled
* @param {number} [options.connectionTimeout=30000] TCP Connection timeout setting
* @param {number} [options.socketTimeout=360000] TCP Socket timeout setting
* @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.
*/
constructor(socket, options) {
super();
options = options || {};
if (!options.bson) {
throw new TypeError('must pass in valid bson parser');
}
this.id = _id++;
this.options = options;
this.logger = Logger('Connection', options);
this.bson = options.bson;
this.tag = options.tag;
this.maxBsonMessageSize = options.maxBsonMessageSize || DEFAULT_MAX_BSON_MESSAGE_SIZE;
this.port = options.port || 27017;
this.host = options.host || 'localhost';
this.socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000;
// These values are inspected directly in tests, but maybe not necessary to keep around
this.keepAlive = typeof options.keepAlive === 'boolean' ? options.keepAlive : true;
this.keepAliveInitialDelay =
typeof options.keepAliveInitialDelay === 'number' ? options.keepAliveInitialDelay : 300000;
this.connectionTimeout =
typeof options.connectionTimeout === 'number' ? options.connectionTimeout : 30000;
if (this.keepAliveInitialDelay > this.socketTimeout) {
this.keepAliveInitialDelay = Math.round(this.socketTimeout / 2);
}
// Debug information
if (this.logger.isDebug()) {
this.logger.debug(
`creating connection ${this.id} with options [${JSON.stringify(
debugOptions(DEBUG_FIELDS, options)
)}]`
);
}
// Response options
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
};
// Flushing
this.flushing = false;
this.queue = [];
// Internal state
this.writeStream = null;
this.destroyed = false;
// Create hash method
const hash = crypto.createHash('sha1');
hash.update(this.address);
this.hashedName = hash.digest('hex');
// All operations in flight on the connection
this.workItems = [];
// setup socket
this.socket = socket;
this.socket.once('error', errorHandler(this));
this.socket.once('timeout', timeoutHandler(this));
this.socket.once('close', closeHandler(this));
this.socket.on('data', dataHandler(this));
if (connectionAccounting) {
addConnection(this.id, this);
}
}
setSocketTimeout(value) {
if (this.socket) {
this.socket.setTimeout(value);
}
}
resetSocketTimeout() {
if (this.socket) {
this.socket.setTimeout(this.socketTimeout);
}
}
static enableConnectionAccounting(spy) {
if (spy) {
connectionAccountingSpy = spy;
}
connectionAccounting = true;
connections = {};
}
static disableConnectionAccounting() {
connectionAccounting = false;
connectionAccountingSpy = undefined;
}
static connections() {
return connections;
}
get address() {
return `${this.host}:${this.port}`;
}
/**
* Unref this connection
* @method
* @return {boolean}
*/
unref() {
if (this.socket == null) {
this.once('connect', () => this.socket.unref());
return;
}
this.socket.unref();
}
/**
* Destroy connection
* @method
*/
destroy(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
options = Object.assign({ force: false }, options);
if (connectionAccounting) {
deleteConnection(this.id);
}
if (this.socket == null) {
this.destroyed = true;
return;
}
if (options.force) {
this.socket.destroy();
this.destroyed = true;
if (typeof callback === 'function') callback(null, null);
return;
}
this.socket.end(err => {
this.destroyed = true;
if (typeof callback === 'function') callback(err, null);
});
}
/**
* Write to connection
* @method
* @param {Command} command Command to write out need to implement toBin and toBinUnified
*/
write(buffer) {
// Debug Log
if (this.logger.isDebug()) {
if (!Array.isArray(buffer)) {
this.logger.debug(`writing buffer [${buffer.toString('hex')}] to ${this.address}`);
} else {
for (let i = 0; i < buffer.length; i++)
this.logger.debug(`writing buffer [${buffer[i].toString('hex')}] to ${this.address}`);
}
}
// Double check that the connection is not destroyed
if (this.socket.destroyed === false) {
// Write out the command
if (!Array.isArray(buffer)) {
this.socket.write(buffer, 'binary');
return true;
}
// Iterate over all buffers and write them in order to the socket
for (let i = 0; i < buffer.length; i++) {
this.socket.write(buffer[i], 'binary');
}
return true;
}
// Connection is destroyed return write failed
return false;
}
/**
* Return id of connection as a string
* @method
* @return {string}
*/
toString() {
return '' + this.id;
}
/**
* Return json object of connection
* @method
* @return {object}
*/
toJSON() {
return { id: this.id, host: this.host, port: this.port };
}
/**
* Is the connection connected
* @method
* @return {boolean}
*/
isConnected() {
if (this.destroyed) return false;
return !this.socket.destroyed && this.socket.writable;
}
}
function deleteConnection(id) {
// console.log("=== deleted connection " + id + " :: " + (connections[id] ? connections[id].port : ''))
delete connections[id];
if (connectionAccountingSpy) {
connectionAccountingSpy.deleteConnection(id);
}
}
function addConnection(id, connection) {
// console.log("=== added connection " + id + " :: " + connection.port)
connections[id] = connection;
if (connectionAccountingSpy) {
connectionAccountingSpy.addConnection(id, connection);
}
}
//
// Connection handlers
function errorHandler(conn) {
return function(err) {
if (connectionAccounting) deleteConnection(conn.id);
// Debug information
if (conn.logger.isDebug()) {
conn.logger.debug(
`connection ${conn.id} for [${conn.address}] errored out with [${JSON.stringify(err)}]`
);
}
conn.emit('error', new MongoNetworkError(err), conn);
};
}
function timeoutHandler(conn) {
return function() {
if (connectionAccounting) deleteConnection(conn.id);
if (conn.logger.isDebug()) {
conn.logger.debug(`connection ${conn.id} for [${conn.address}] timed out`);
}
conn.emit(
'timeout',
new MongoNetworkError(`connection ${conn.id} to ${conn.address} timed out`),
conn
);
};
}
function closeHandler(conn) {
return function(hadError) {
if (connectionAccounting) deleteConnection(conn.id);
if (conn.logger.isDebug()) {
conn.logger.debug(`connection ${conn.id} with for [${conn.address}] closed`);
}
if (!hadError) {
conn.emit(
'close',
new MongoNetworkError(`connection ${conn.id} to ${conn.address} closed`),
conn
);
}
};
}
// Handle a message once it is received
function processMessage(conn, message) {
const msgHeader = parseHeader(message);
if (msgHeader.opCode !== OP_COMPRESSED) {
const ResponseConstructor = msgHeader.opCode === OP_MSG ? BinMsg : Response;
conn.emit(
'message',
new ResponseConstructor(
conn.bson,
message,
msgHeader,
message.slice(MESSAGE_HEADER_SIZE),
conn.responseOptions
),
conn
);
return;
}
msgHeader.fromCompressed = true;
let index = MESSAGE_HEADER_SIZE;
msgHeader.opCode = message.readInt32LE(index);
index += 4;
msgHeader.length = message.readInt32LE(index);
index += 4;
const compressorID = message[index];
index++;
decompress(compressorID, message.slice(index), (err, decompressedMsgBody) => {
if (err) {
conn.emit('error', err);
return;
}
if (decompressedMsgBody.length !== msgHeader.length) {
conn.emit(
'error',
new MongoError(
'Decompressing a compressed message from the server failed. The message is corrupt.'
)
);
return;
}
const ResponseConstructor = msgHeader.opCode === OP_MSG ? BinMsg : Response;
conn.emit(
'message',
new ResponseConstructor(
conn.bson,
message,
msgHeader,
decompressedMsgBody,
conn.responseOptions
),
conn
);
});
}
function dataHandler(conn) {
return function(data) {
// Parse until we are done with the data
while (data.length > 0) {
// If we still have bytes to read on the current message
if (conn.bytesRead > 0 && conn.sizeOfMessage > 0) {
// Calculate the amount of remaining bytes
const remainingBytesToRead = conn.sizeOfMessage - conn.bytesRead;
// Check if the current chunk contains the rest of the message
if (remainingBytesToRead > data.length) {
// Copy the new data into the exiting buffer (should have been allocated when we know the message size)
data.copy(conn.buffer, conn.bytesRead);
// Adjust the number of bytes read so it point to the correct index in the buffer
conn.bytesRead = conn.bytesRead + data.length;
// Reset state of buffer
data = Buffer.alloc(0);
} else {
// Copy the missing part of the data into our current buffer
data.copy(conn.buffer, conn.bytesRead, 0, remainingBytesToRead);
// Slice the overflow into a new buffer that we will then re-parse
data = data.slice(remainingBytesToRead);
// Emit current complete message
const emitBuffer = conn.buffer;
// Reset state of buffer
conn.buffer = null;
conn.sizeOfMessage = 0;
conn.bytesRead = 0;
conn.stubBuffer = null;
processMessage(conn, emitBuffer);
}
} else {
// Stub buffer is kept in case we don't get enough bytes to determine the
// size of the message (< 4 bytes)
if (conn.stubBuffer != null && conn.stubBuffer.length > 0) {
// If we have enough bytes to determine the message size let's do it
if (conn.stubBuffer.length + data.length > 4) {
// Prepad the data
const newData = Buffer.alloc(conn.stubBuffer.length + data.length);
conn.stubBuffer.copy(newData, 0);
data.copy(newData, conn.stubBuffer.length);
// Reassign for parsing
data = newData;
// Reset state of buffer
conn.buffer = null;
conn.sizeOfMessage = 0;
conn.bytesRead = 0;
conn.stubBuffer = null;
} else {
// Add the the bytes to the stub buffer
const newStubBuffer = Buffer.alloc(conn.stubBuffer.length + data.length);
// Copy existing stub buffer
conn.stubBuffer.copy(newStubBuffer, 0);
// Copy missing part of the data
data.copy(newStubBuffer, conn.stubBuffer.length);
// Exit parsing loop
data = Buffer.alloc(0);
}
} else {
if (data.length > 4) {
// Retrieve the message size
const sizeOfMessage = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
// If we have a negative sizeOfMessage emit error and return
if (sizeOfMessage < 0 || sizeOfMessage > conn.maxBsonMessageSize) {
const errorObject = {
err: 'socketHandler',
trace: '',
bin: conn.buffer,
parseState: {
sizeOfMessage: sizeOfMessage,
bytesRead: conn.bytesRead,
stubBuffer: conn.stubBuffer
}
};
// We got a parse Error fire it off then keep going
conn.emit('parseError', errorObject, conn);
return;
}
// Ensure that the size of message is larger than 0 and less than the max allowed
if (
sizeOfMessage > 4 &&
sizeOfMessage < conn.maxBsonMessageSize &&
sizeOfMessage > data.length
) {
conn.buffer = Buffer.alloc(sizeOfMessage);
// Copy all the data into the buffer
data.copy(conn.buffer, 0);
// Update bytes read
conn.bytesRead = data.length;
// Update sizeOfMessage
conn.sizeOfMessage = sizeOfMessage;
// Ensure stub buffer is null
conn.stubBuffer = null;
// Exit parsing loop
data = Buffer.alloc(0);
} else if (
sizeOfMessage > 4 &&
sizeOfMessage < conn.maxBsonMessageSize &&
sizeOfMessage === data.length
) {
const emitBuffer = data;
// Reset state of buffer
conn.buffer = null;
conn.sizeOfMessage = 0;
conn.bytesRead = 0;
conn.stubBuffer = null;
// Exit parsing loop
data = Buffer.alloc(0);
// Emit the message
processMessage(conn, emitBuffer);
} else if (sizeOfMessage <= 4 || sizeOfMessage > conn.maxBsonMessageSize) {
const errorObject = {
err: 'socketHandler',
trace: null,
bin: data,
parseState: {
sizeOfMessage: sizeOfMessage,
bytesRead: 0,
buffer: null,
stubBuffer: null
}
};
// We got a parse Error fire it off then keep going
conn.emit('parseError', errorObject, conn);
// Clear out the state of the parser
conn.buffer = null;
conn.sizeOfMessage = 0;
conn.bytesRead = 0;
conn.stubBuffer = null;
// Exit parsing loop
data = Buffer.alloc(0);
} else {
const emitBuffer = data.slice(0, sizeOfMessage);
// Reset state of buffer
conn.buffer = null;
conn.sizeOfMessage = 0;
conn.bytesRead = 0;
conn.stubBuffer = null;
// Copy rest of message
data = data.slice(sizeOfMessage);
// Emit the message
processMessage(conn, emitBuffer);
}
} else {
// Create a buffer that contains the space for the non-complete message
conn.stubBuffer = Buffer.alloc(data.length);
// Copy the data to the stub buffer
data.copy(conn.stubBuffer, 0);
// Exit parsing loop
data = Buffer.alloc(0);
}
}
}
}
};
}
/**
* A server connect event, used to verify that the connection is up and running
*
* @event Connection#connect
* @type {Connection}
*/
/**
* The server connection closed, all pool connections closed
*
* @event Connection#close
* @type {Connection}
*/
/**
* The server connection caused an error, all pool connections closed
*
* @event Connection#error
* @type {Connection}
*/
/**
* The server connection timed out, all pool connections closed
*
* @event Connection#timeout
* @type {Connection}
*/
/**
* The driver experienced an invalid message, all pool connections closed
*
* @event Connection#parseError
* @type {Connection}
*/
/**
* An event emitted each time the connection receives a parsed message from the wire
*
* @event Connection#message
* @type {Connection}
*/
module.exports = Connection;
+246
View File
@@ -0,0 +1,246 @@
'use strict';
var f = require('util').format,
MongoError = require('../error').MongoError;
// Filters for classes
var classFilters = {};
var filteredClasses = {};
var level = null;
// Save the process id
var pid = process.pid;
// current logger
var currentLogger = null;
/**
* Creates a new Logger instance
* @class
* @param {string} className The Class name associated with the logging instance
* @param {object} [options=null] Optional settings.
* @param {Function} [options.logger=null] Custom logger function;
* @param {string} [options.loggerLevel=error] Override default global log level.
* @return {Logger} a Logger instance.
*/
var Logger = function(className, options) {
if (!(this instanceof Logger)) return new Logger(className, options);
options = options || {};
// Current reference
this.className = className;
// Current logger
if (options.logger) {
currentLogger = options.logger;
} else if (currentLogger == null) {
currentLogger = console.log;
}
// Set level of logging, default is error
if (options.loggerLevel) {
level = options.loggerLevel || 'error';
}
// Add all class names
if (filteredClasses[this.className] == null) classFilters[this.className] = true;
};
/**
* Log a message at the debug level
* @method
* @param {string} message The message to log
* @param {object} object additional meta data to log
* @return {null}
*/
Logger.prototype.debug = function(message, object) {
if (
this.isDebug() &&
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className]))
) {
var dateTime = new Date().getTime();
var msg = f('[%s-%s:%s] %s %s', 'DEBUG', this.className, pid, dateTime, message);
var state = {
type: 'debug',
message: message,
className: this.className,
pid: pid,
date: dateTime
};
if (object) state.meta = object;
currentLogger(msg, state);
}
};
/**
* Log a message at the warn level
* @method
* @param {string} message The message to log
* @param {object} object additional meta data to log
* @return {null}
*/
(Logger.prototype.warn = function(message, object) {
if (
this.isWarn() &&
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className]))
) {
var dateTime = new Date().getTime();
var msg = f('[%s-%s:%s] %s %s', 'WARN', this.className, pid, dateTime, message);
var state = {
type: 'warn',
message: message,
className: this.className,
pid: pid,
date: dateTime
};
if (object) state.meta = object;
currentLogger(msg, state);
}
}),
/**
* Log a message at the info level
* @method
* @param {string} message The message to log
* @param {object} object additional meta data to log
* @return {null}
*/
(Logger.prototype.info = function(message, object) {
if (
this.isInfo() &&
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className]))
) {
var dateTime = new Date().getTime();
var msg = f('[%s-%s:%s] %s %s', 'INFO', this.className, pid, dateTime, message);
var state = {
type: 'info',
message: message,
className: this.className,
pid: pid,
date: dateTime
};
if (object) state.meta = object;
currentLogger(msg, state);
}
}),
/**
* Log a message at the error level
* @method
* @param {string} message The message to log
* @param {object} object additional meta data to log
* @return {null}
*/
(Logger.prototype.error = function(message, object) {
if (
this.isError() &&
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className]))
) {
var dateTime = new Date().getTime();
var msg = f('[%s-%s:%s] %s %s', 'ERROR', this.className, pid, dateTime, message);
var state = {
type: 'error',
message: message,
className: this.className,
pid: pid,
date: dateTime
};
if (object) state.meta = object;
currentLogger(msg, state);
}
}),
/**
* Is the logger set at info level
* @method
* @return {boolean}
*/
(Logger.prototype.isInfo = function() {
return level === 'info' || level === 'debug';
}),
/**
* Is the logger set at error level
* @method
* @return {boolean}
*/
(Logger.prototype.isError = function() {
return level === 'error' || level === 'info' || level === 'debug';
}),
/**
* Is the logger set at error level
* @method
* @return {boolean}
*/
(Logger.prototype.isWarn = function() {
return level === 'error' || level === 'warn' || level === 'info' || level === 'debug';
}),
/**
* Is the logger set at debug level
* @method
* @return {boolean}
*/
(Logger.prototype.isDebug = function() {
return level === 'debug';
});
/**
* Resets the logger to default settings, error and no filtered classes
* @method
* @return {null}
*/
Logger.reset = function() {
level = 'error';
filteredClasses = {};
};
/**
* Get the current logger function
* @method
* @return {function}
*/
Logger.currentLogger = function() {
return currentLogger;
};
/**
* Set the current logger function
* @method
* @param {function} logger Logger function.
* @return {null}
*/
Logger.setCurrentLogger = function(logger) {
if (typeof logger !== 'function') throw new MongoError('current logger must be a function');
currentLogger = logger;
};
/**
* Set what classes to log.
* @method
* @param {string} type The type of filter (currently only class)
* @param {string[]} values The filters to apply
* @return {null}
*/
Logger.filter = function(type, values) {
if (type === 'class' && Array.isArray(values)) {
filteredClasses = {};
values.forEach(function(x) {
filteredClasses[x] = true;
});
}
};
/**
* Set the current log level
* @method
* @param {string} level Set current log level (debug, info, error)
* @return {null}
*/
Logger.setLevel = function(_level) {
if (_level !== 'info' && _level !== 'error' && _level !== 'debug' && _level !== 'warn') {
throw new Error(f('%s is an illegal logging level', _level));
}
level = _level;
};
module.exports = Logger;
+220
View File
@@ -0,0 +1,220 @@
'use strict';
// Implementation of OP_MSG spec:
// https://github.com/mongodb/specifications/blob/master/source/message/OP_MSG.rst
//
// struct Section {
// uint8 payloadType;
// union payload {
// document document; // payloadType == 0
// struct sequence { // payloadType == 1
// int32 size;
// cstring identifier;
// document* documents;
// };
// };
// };
// struct OP_MSG {
// struct MsgHeader {
// int32 messageLength;
// int32 requestID;
// int32 responseTo;
// int32 opCode = 2013;
// };
// uint32 flagBits;
// Section+ sections;
// [uint32 checksum;]
// };
const opcodes = require('../wireprotocol/shared').opcodes;
const databaseNamespace = require('../wireprotocol/shared').databaseNamespace;
const ReadPreference = require('../topologies/read_preference');
// Incrementing request id
let _requestId = 0;
// Msg Flags
const OPTS_CHECKSUM_PRESENT = 1;
const OPTS_MORE_TO_COME = 2;
const OPTS_EXHAUST_ALLOWED = 1 << 16;
class Msg {
constructor(bson, ns, command, options) {
// Basic options needed to be passed in
if (command == null) throw new Error('query must be specified for query');
// Basic options
this.bson = bson;
this.ns = ns;
this.command = command;
this.command.$db = databaseNamespace(ns);
if (options.readPreference && options.readPreference.mode !== ReadPreference.PRIMARY) {
this.command.$readPreference = options.readPreference.toJSON();
}
// Ensure empty options
this.options = options || {};
// Additional options
this.requestId = Msg.getRequestId();
// Serialization option
this.serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
this.ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false;
this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16;
// flags
this.checksumPresent = false;
this.moreToCome = options.moreToCome || false;
this.exhaustAllowed = false;
}
toBin() {
const buffers = [];
let flags = 0;
if (this.checksumPresent) {
flags |= OPTS_CHECKSUM_PRESENT;
}
if (this.moreToCome) {
flags |= OPTS_MORE_TO_COME;
}
if (this.exhaustAllowed) {
flags |= OPTS_EXHAUST_ALLOWED;
}
const header = new Buffer(
4 * 4 + // Header
4 // Flags
);
buffers.push(header);
let totalLength = header.length;
const command = this.command;
totalLength += this.makeDocumentSegment(buffers, command);
header.writeInt32LE(totalLength, 0); // messageLength
header.writeInt32LE(this.requestId, 4); // requestID
header.writeInt32LE(0, 8); // responseTo
header.writeInt32LE(opcodes.OP_MSG, 12); // opCode
header.writeUInt32LE(flags, 16); // flags
return buffers;
}
makeDocumentSegment(buffers, document) {
const payloadTypeBuffer = new Buffer(1);
payloadTypeBuffer[0] = 0;
const documentBuffer = this.serializeBson(document);
buffers.push(payloadTypeBuffer);
buffers.push(documentBuffer);
return payloadTypeBuffer.length + documentBuffer.length;
}
serializeBson(document) {
return this.bson.serialize(document, {
checkKeys: this.checkKeys,
serializeFunctions: this.serializeFunctions,
ignoreUndefined: this.ignoreUndefined
});
}
}
Msg.getRequestId = function() {
_requestId = (_requestId + 1) & 0x7fffffff;
return _requestId;
};
class BinMsg {
constructor(bson, message, msgHeader, msgBody, opts) {
opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false };
this.parsed = false;
this.raw = message;
this.data = msgBody;
this.bson = bson;
this.opts = opts;
// Read the message header
this.length = msgHeader.length;
this.requestId = msgHeader.requestId;
this.responseTo = msgHeader.responseTo;
this.opCode = msgHeader.opCode;
this.fromCompressed = msgHeader.fromCompressed;
// Read response flags
this.responseFlags = msgBody.readInt32LE(0);
this.checksumPresent = (this.responseFlags & OPTS_CHECKSUM_PRESENT) !== 0;
this.moreToCome = (this.responseFlags & OPTS_MORE_TO_COME) !== 0;
this.exhaustAllowed = (this.responseFlags & OPTS_EXHAUST_ALLOWED) !== 0;
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.documents = [];
}
isParsed() {
return this.parsed;
}
parse(options) {
// Don't parse again if not needed
if (this.parsed) return;
options = options || {};
this.index = 4;
// Allow the return of raw documents instead of parsing
const raw = options.raw || false;
const documentsReturnedIn = options.documentsReturnedIn || null;
const promoteLongs =
typeof options.promoteLongs === 'boolean' ? options.promoteLongs : this.opts.promoteLongs;
const promoteValues =
typeof options.promoteValues === 'boolean' ? options.promoteValues : this.opts.promoteValues;
const promoteBuffers =
typeof options.promoteBuffers === 'boolean'
? options.promoteBuffers
: this.opts.promoteBuffers;
// Set up the options
const _options = {
promoteLongs: promoteLongs,
promoteValues: promoteValues,
promoteBuffers: promoteBuffers
};
while (this.index < this.data.length) {
const payloadType = this.data.readUInt8(this.index++);
if (payloadType === 1) {
console.error('TYPE 1');
} else if (payloadType === 0) {
const bsonSize = this.data.readUInt32LE(this.index);
const bin = this.data.slice(this.index, this.index + bsonSize);
this.documents.push(raw ? bin : this.bson.deserialize(bin, _options));
this.index += bsonSize;
}
}
if (this.documents.length === 1 && documentsReturnedIn != null && raw) {
const fieldsAsRaw = {};
fieldsAsRaw[documentsReturnedIn] = true;
_options.fieldsAsRaw = fieldsAsRaw;
const doc = this.bson.deserialize(this.documents[0], _options);
this.documents = [doc];
}
this.parsed = true;
}
}
module.exports = { Msg, BinMsg };
+1285
View File
File diff suppressed because it is too large. Load diff
+57
View File
@@ -0,0 +1,57 @@
'use strict';
const require_optional = require('require_optional');
function debugOptions(debugFields, options) {
var finaloptions = {};
debugFields.forEach(function(n) {
finaloptions[n] = options[n];
});
return finaloptions;
}
function retrieveBSON() {
var BSON = require('bson');
BSON.native = false;
try {
var optionalBSON = require_optional('bson-ext');
if (optionalBSON) {
optionalBSON.native = true;
return optionalBSON;
}
} catch (err) {} // eslint-disable-line
return BSON;
}
// Throw an error if an attempt to use Snappy is made when Snappy is not installed
function noSnappyWarning() {
throw new Error(
'Attempted to use Snappy compression, but Snappy is not installed. Install or disable Snappy compression and try again.'
);
}
// Facilitate loading Snappy optionally
function retrieveSnappy() {
var snappy = null;
try {
snappy = require_optional('snappy');
} catch (error) {} // eslint-disable-line
if (!snappy) {
snappy = {
compress: noSnappyWarning,
uncompress: noSnappyWarning,
compressSync: noSnappyWarning,
uncompressSync: noSnappyWarning
};
}
return snappy;
}
module.exports = {
debugOptions,
retrieveBSON,
retrieveSnappy
};
+886
View File
@@ -0,0 +1,886 @@
'use strict';
const Logger = require('./connection/logger');
const retrieveBSON = require('./connection/utils').retrieveBSON;
const MongoError = require('./error').MongoError;
const MongoNetworkError = require('./error').MongoNetworkError;
const mongoErrorContextSymbol = require('./error').mongoErrorContextSymbol;
const collationNotSupported = require('./utils').collationNotSupported;
const ReadPreference = require('./topologies/read_preference');
const isUnifiedTopology = require('./utils').isUnifiedTopology;
const executeOperation = require('../operations/execute_operation');
const Readable = require('stream').Readable;
const SUPPORTS = require('../utils').SUPPORTS;
const MongoDBNamespace = require('../utils').MongoDBNamespace;
const OperationBase = require('../operations/operation').OperationBase;
const BSON = retrieveBSON();
const Long = BSON.Long;
// Possible states for a cursor
const CursorState = {
INIT: 0,
OPEN: 1,
CLOSED: 2,
GET_MORE: 3
};
//
// Handle callback (including any exceptions thrown)
function handleCallback(callback, err, result) {
try {
callback(err, result);
} catch (err) {
process.nextTick(function() {
throw err;
});
}
}
/**
* This is a cursor results callback
*
* @callback resultCallback
* @param {error} error An error object. Set to null if no error present
* @param {object} document
*/
/**
* @fileOverview The **Cursor** class is an internal class that embodies a cursor on MongoDB
* allowing for iteration over the results returned from the underlying query.
*
* **CURSORS Cannot directly be instantiated**
*/
/**
* The core cursor class. All cursors in the driver build off of this one.
*
* @property {number} cursorBatchSize The current cursorBatchSize for the cursor
* @property {number} cursorLimit The current cursorLimit for the cursor
* @property {number} cursorSkip The current cursorSkip for the cursor
*/
class CoreCursor extends Readable {
/**
* Create a new core `Cursor` instance.
* **NOTE** Not to be instantiated directly
*
* @param {object} topology The server topology instance.
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {{object}|Long} cmd The selector (can be a command or a cursorId)
* @param {object} [options=null] Optional settings.
* @param {object} [options.batchSize=1000] Batchsize for the operation
* @param {array} [options.documents=[]] Initial documents list for cursor
* @param {object} [options.transforms=null] Transform methods for the cursor results
* @param {function} [options.transforms.query] Transform the value returned from the initial query
* @param {function} [options.transforms.doc] Transform each document returned from Cursor.prototype._next
*/
constructor(topology, ns, cmd, options) {
super({ objectMode: true });
options = options || {};
if (ns instanceof OperationBase) {
this.operation = ns;
ns = this.operation.ns.toString();
options = this.operation.options;
cmd = this.operation.cmd ? this.operation.cmd : {};
}
// Cursor pool
this.pool = null;
// Cursor server
this.server = null;
// Do we have a not connected handler
this.disconnectHandler = options.disconnectHandler;
// Set local values
this.bson = topology.s.bson;
this.ns = ns;
this.namespace = MongoDBNamespace.fromString(ns);
this.cmd = cmd;
this.options = options;
this.topology = topology;
// All internal state
this.cursorState = {
cursorId: null,
cmd,
documents: options.documents || [],
cursorIndex: 0,
dead: false,
killed: false,
init: false,
notified: false,
limit: options.limit || cmd.limit || 0,
skip: options.skip || cmd.skip || 0,
batchSize: options.batchSize || cmd.batchSize || 1000,
currentLimit: 0,
// Result field name if not a cursor (contains the array of results)
transforms: options.transforms,
raw: options.raw || (cmd && cmd.raw)
};
if (typeof options.session === 'object') {
this.cursorState.session = options.session;
}
// Add promoteLong to cursor state
const topologyOptions = topology.s.options;
if (typeof topologyOptions.promoteLongs === 'boolean') {
this.cursorState.promoteLongs = topologyOptions.promoteLongs;
} else if (typeof options.promoteLongs === 'boolean') {
this.cursorState.promoteLongs = options.promoteLongs;
}
// Add promoteValues to cursor state
if (typeof topologyOptions.promoteValues === 'boolean') {
this.cursorState.promoteValues = topologyOptions.promoteValues;
} else if (typeof options.promoteValues === 'boolean') {
this.cursorState.promoteValues = options.promoteValues;
}
// Add promoteBuffers to cursor state
if (typeof topologyOptions.promoteBuffers === 'boolean') {
this.cursorState.promoteBuffers = topologyOptions.promoteBuffers;
} else if (typeof options.promoteBuffers === 'boolean') {
this.cursorState.promoteBuffers = options.promoteBuffers;
}
if (topologyOptions.reconnect) {
this.cursorState.reconnect = topologyOptions.reconnect;
}
// Logger
this.logger = Logger('Cursor', topologyOptions);
//
// Did we pass in a cursor id
if (typeof cmd === 'number') {
this.cursorState.cursorId = Long.fromNumber(cmd);
this.cursorState.lastCursorId = this.cursorState.cursorId;
} else if (cmd instanceof Long) {
this.cursorState.cursorId = cmd;
this.cursorState.lastCursorId = cmd;
}
// TODO: remove as part of NODE-2104
if (this.operation) {
this.operation.cursorState = this.cursorState;
}
}
setCursorBatchSize(value) {
this.cursorState.batchSize = value;
}
cursorBatchSize() {
return this.cursorState.batchSize;
}
setCursorLimit(value) {
this.cursorState.limit = value;
}
cursorLimit() {
return this.cursorState.limit;
}
setCursorSkip(value) {
this.cursorState.skip = value;
}
cursorSkip() {
return this.cursorState.skip;
}
/**
* Retrieve the next document from the cursor
* @method
* @param {resultCallback} callback A callback function
*/
_next(callback) {
nextFunction(this, callback);
}
/**
* Clone the cursor
* @method
* @return {Cursor}
*/
clone() {
return this.topology.cursor(this.ns, this.cmd, this.options);
}
/**
* Checks if the cursor is dead
* @method
* @return {boolean} A boolean signifying if the cursor is dead or not
*/
isDead() {
return this.cursorState.dead === true;
}
/**
* Checks if the cursor was killed by the application
* @method
* @return {boolean} A boolean signifying if the cursor was killed by the application
*/
isKilled() {
return this.cursorState.killed === true;
}
/**
* Checks if the cursor notified it's caller about it's death
* @method
* @return {boolean} A boolean signifying if the cursor notified the callback
*/
isNotified() {
return this.cursorState.notified === true;
}
/**
* Returns current buffered documents length
* @method
* @return {number} The number of items in the buffered documents
*/
bufferedCount() {
return this.cursorState.documents.length - this.cursorState.cursorIndex;
}
/**
* Returns current buffered documents
* @method
* @return {Array} An array of buffered documents
*/
readBufferedDocuments(number) {
const unreadDocumentsLength = this.cursorState.documents.length - this.cursorState.cursorIndex;
const length = number < unreadDocumentsLength ? number : unreadDocumentsLength;
let elements = this.cursorState.documents.slice(
this.cursorState.cursorIndex,
this.cursorState.cursorIndex + length
);
// Transform the doc with passed in transformation method if provided
if (this.cursorState.transforms && typeof this.cursorState.transforms.doc === 'function') {
// Transform all the elements
for (let i = 0; i < elements.length; i++) {
elements[i] = this.cursorState.transforms.doc(elements[i]);
}
}
// Ensure we do not return any more documents than the limit imposed
// Just return the number of elements up to the limit
if (
this.cursorState.limit > 0 &&
this.cursorState.currentLimit + elements.length > this.cursorState.limit
) {
elements = elements.slice(0, this.cursorState.limit - this.cursorState.currentLimit);
this.kill();
}
// Adjust current limit
this.cursorState.currentLimit = this.cursorState.currentLimit + elements.length;
this.cursorState.cursorIndex = this.cursorState.cursorIndex + elements.length;
// Return elements
return elements;
}
/**
* Resets local state for this cursor instance, and issues a `killCursors` command to the server
*
* @param {resultCallback} callback A callback function
*/
kill(callback) {
// Set cursor to dead
this.cursorState.dead = true;
this.cursorState.killed = true;
// Remove documents
this.cursorState.documents = [];
// If no cursor id just return
if (
this.cursorState.cursorId == null ||
this.cursorState.cursorId.isZero() ||
this.cursorState.init === false
) {
if (callback) callback(null, null);
return;
}
this.server.killCursors(this.ns, this.cursorState, callback);
}
/**
* Resets the cursor
*/
rewind() {
if (this.cursorState.init) {
if (!this.cursorState.dead) {
this.kill();
}
this.cursorState.currentLimit = 0;
this.cursorState.init = false;
this.cursorState.dead = false;
this.cursorState.killed = false;
this.cursorState.notified = false;
this.cursorState.documents = [];
this.cursorState.cursorId = null;
this.cursorState.cursorIndex = 0;
}
}
// Internal methods
_read() {
if ((this.s && this.s.state === CursorState.CLOSED) || this.isDead()) {
return this.push(null);
}
// Get the next item
this._next((err, result) => {
if (err) {
if (this.listeners('error') && this.listeners('error').length > 0) {
this.emit('error', err);
}
if (!this.isDead()) this.close();
// Emit end event
this.emit('end');
return this.emit('finish');
}
// If we provided a transformation method
if (
this.cursorState.streamOptions &&
typeof this.cursorState.streamOptions.transform === 'function' &&
result != null
) {
return this.push(this.cursorState.streamOptions.transform(result));
}
// If we provided a map function
if (
this.cursorState.transforms &&
typeof this.cursorState.transforms.doc === 'function' &&
result != null
) {
return this.push(this.cursorState.transforms.doc(result));
}
// Return the result
this.push(result);
if (result === null && this.isDead()) {
this.once('end', () => {
this.close();
this.emit('finish');
});
}
});
}
_endSession(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
const session = this.cursorState.session;
if (session && (options.force || session.owner === this)) {
this.cursorState.session = undefined;
if (this.operation) {
this.operation.clearSession();
}
session.endSession(callback);
return true;
}
if (callback) {
callback();
}
return false;
}
_getMore(callback) {
if (this.logger.isDebug()) {
this.logger.debug(`schedule getMore call for query [${JSON.stringify(this.query)}]`);
}
// Set the current batchSize
let batchSize = this.cursorState.batchSize;
if (
this.cursorState.limit > 0 &&
this.cursorState.currentLimit + batchSize > this.cursorState.limit
) {
batchSize = this.cursorState.limit - this.cursorState.currentLimit;
}
this.server.getMore(this.ns, this.cursorState, batchSize, this.options, callback);
}
_initializeCursor(callback) {
const cursor = this;
// NOTE: this goes away once cursors use `executeOperation`
if (isUnifiedTopology(cursor.topology) && cursor.topology.shouldCheckForSessionSupport()) {
cursor.topology.selectServer(ReadPreference.primaryPreferred, err => {
if (err) {
callback(err);
return;
}
cursor._next(callback);
});
return;
}
function done(err, result) {
if (
cursor.cursorState.cursorId &&
cursor.cursorState.cursorId.isZero() &&
cursor._endSession
) {
cursor._endSession();
}
if (
cursor.cursorState.documents.length === 0 &&
cursor.cursorState.cursorId &&
cursor.cursorState.cursorId.isZero() &&
!cursor.cmd.tailable &&
!cursor.cmd.awaitData
) {
return setCursorNotified(cursor, callback);
}
callback(err, result);
}
const queryCallback = (err, r) => {
if (err) {
return done(err);
}
const result = r.message;
if (result.queryFailure) {
return done(new MongoError(result.documents[0]), null);
}
// Check if we have a command cursor
if (
Array.isArray(result.documents) &&
result.documents.length === 1 &&
(!cursor.cmd.find || (cursor.cmd.find && cursor.cmd.virtual === false)) &&
(typeof result.documents[0].cursor !== 'string' ||
result.documents[0]['$err'] ||
result.documents[0]['errmsg'] ||
Array.isArray(result.documents[0].result))
) {
// We have an error document, return the error
if (result.documents[0]['$err'] || result.documents[0]['errmsg']) {
return done(new MongoError(result.documents[0]), null);
}
// We have a cursor document
if (result.documents[0].cursor != null && typeof result.documents[0].cursor !== 'string') {
const id = result.documents[0].cursor.id;
// If we have a namespace change set the new namespace for getmores
if (result.documents[0].cursor.ns) {
cursor.ns = result.documents[0].cursor.ns;
}
// Promote id to long if needed
cursor.cursorState.cursorId = typeof id === 'number' ? Long.fromNumber(id) : id;
cursor.cursorState.lastCursorId = cursor.cursorState.cursorId;
cursor.cursorState.operationTime = result.documents[0].operationTime;
// If we have a firstBatch set it
if (Array.isArray(result.documents[0].cursor.firstBatch)) {
cursor.cursorState.documents = result.documents[0].cursor.firstBatch; //.reverse();
}
// Return after processing command cursor
return done(null, result);
}
if (Array.isArray(result.documents[0].result)) {
cursor.cursorState.documents = result.documents[0].result;
cursor.cursorState.cursorId = Long.ZERO;
return done(null, result);
}
}
// Otherwise fall back to regular find path
const cursorId = result.cursorId || 0;
cursor.cursorState.cursorId = cursorId instanceof Long ? cursorId : Long.fromNumber(cursorId);
cursor.cursorState.documents = result.documents;
cursor.cursorState.lastCursorId = result.cursorId;
// Transform the results with passed in transformation method if provided
if (
cursor.cursorState.transforms &&
typeof cursor.cursorState.transforms.query === 'function'
) {
cursor.cursorState.documents = cursor.cursorState.transforms.query(result);
}
done(null, result);
};
if (cursor.operation) {
if (cursor.logger.isDebug()) {
cursor.logger.debug(
`issue initial query [${JSON.stringify(cursor.cmd)}] with flags [${JSON.stringify(
cursor.query
)}]`
);
}
executeOperation(cursor.topology, cursor.operation, (err, result) => {
if (err) {
done(err);
return;
}
cursor.server = cursor.operation.server;
cursor.cursorState.init = true;
// NOTE: this is a special internal method for cloning a cursor, consider removing
if (cursor.cursorState.cursorId != null) {
return done();
}
queryCallback(err, result);
});
return;
}
// Very explicitly choose what is passed to selectServer
const serverSelectOptions = {};
if (cursor.cursorState.session) {
serverSelectOptions.session = cursor.cursorState.session;
}
if (cursor.operation) {
serverSelectOptions.readPreference = cursor.operation.readPreference;
} else if (cursor.options.readPreference) {
serverSelectOptions.readPreference = cursor.options.readPreference;
}
return cursor.topology.selectServer(serverSelectOptions, (err, server) => {
if (err) {
const disconnectHandler = cursor.disconnectHandler;
if (disconnectHandler != null) {
return disconnectHandler.addObjectAndMethod(
'cursor',
cursor,
'next',
[callback],
callback
);
}
return callback(err);
}
cursor.server = server;
cursor.cursorState.init = true;
if (collationNotSupported(cursor.server, cursor.cmd)) {
return callback(new MongoError(`server ${cursor.server.name} does not support collation`));
}
// NOTE: this is a special internal method for cloning a cursor, consider removing
if (cursor.cursorState.cursorId != null) {
return done();
}
if (cursor.logger.isDebug()) {
cursor.logger.debug(
`issue initial query [${JSON.stringify(cursor.cmd)}] with flags [${JSON.stringify(
cursor.query
)}]`
);
}
if (cursor.cmd.find != null) {
server.query(cursor.ns, cursor.cmd, cursor.cursorState, cursor.options, queryCallback);
return;
}
const commandOptions = Object.assign({ session: cursor.cursorState.session }, cursor.options);
server.command(cursor.ns, cursor.cmd, commandOptions, queryCallback);
});
}
}
if (SUPPORTS.ASYNC_ITERATOR) {
CoreCursor.prototype[Symbol.asyncIterator] = require('../async/async_iterator').asyncIterator;
}
/**
* Validate if the pool is dead and return error
*/
function isConnectionDead(self, callback) {
if (self.pool && self.pool.isDestroyed()) {
self.cursorState.killed = true;
const err = new MongoNetworkError(
`connection to host ${self.pool.host}:${self.pool.port} was destroyed`
);
_setCursorNotifiedImpl(self, () => callback(err));
return true;
}
return false;
}
/**
* Validate if the cursor is dead but was not explicitly killed by user
*/
function isCursorDeadButNotkilled(self, callback) {
// Cursor is dead but not marked killed, return null
if (self.cursorState.dead && !self.cursorState.killed) {
self.cursorState.killed = true;
setCursorNotified(self, callback);
return true;
}
return false;
}
/**
* Validate if the cursor is dead and was killed by user
*/
function isCursorDeadAndKilled(self, callback) {
if (self.cursorState.dead && self.cursorState.killed) {
handleCallback(callback, new MongoError('cursor is dead'));
return true;
}
return false;
}
/**
* Validate if the cursor was killed by the user
*/
function isCursorKilled(self, callback) {
if (self.cursorState.killed) {
setCursorNotified(self, callback);
return true;
}
return false;
}
/**
* Mark cursor as being dead and notified
*/
function setCursorDeadAndNotified(self, callback) {
self.cursorState.dead = true;
setCursorNotified(self, callback);
}
/**
* Mark cursor as being notified
*/
function setCursorNotified(self, callback) {
_setCursorNotifiedImpl(self, () => handleCallback(callback, null, null));
}
function _setCursorNotifiedImpl(self, callback) {
self.cursorState.notified = true;
self.cursorState.documents = [];
self.cursorState.cursorIndex = 0;
if (self._endSession) {
self._endSession(undefined, () => callback());
return;
}
return callback();
}
function nextFunction(self, callback) {
// We have notified about it
if (self.cursorState.notified) {
return callback(new Error('cursor is exhausted'));
}
// Cursor is killed return null
if (isCursorKilled(self, callback)) return;
// Cursor is dead but not marked killed, return null
if (isCursorDeadButNotkilled(self, callback)) return;
// We have a dead and killed cursor, attempting to call next should error
if (isCursorDeadAndKilled(self, callback)) return;
// We have just started the cursor
if (!self.cursorState.init) {
// Topology is not connected, save the call in the provided store to be
// Executed at some point when the handler deems it's reconnected
if (!self.topology.isConnected(self.options)) {
// Only need this for single server, because repl sets and mongos
// will always continue trying to reconnect
if (self.topology._type === 'server' && !self.topology.s.options.reconnect) {
// Reconnect is disabled, so we'll never reconnect
return callback(new MongoError('no connection available'));
}
if (self.disconnectHandler != null) {
if (self.topology.isDestroyed()) {
// Topology was destroyed, so don't try to wait for it to reconnect
return callback(new MongoError('Topology was destroyed'));
}
self.disconnectHandler.addObjectAndMethod('cursor', self, 'next', [callback], callback);
return;
}
}
self._initializeCursor((err, result) => {
if (err || result === null) {
callback(err, result);
return;
}
nextFunction(self, callback);
});
return;
}
if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
// Ensure we kill the cursor on the server
self.kill();
// Set cursor in dead and notified state
return setCursorDeadAndNotified(self, callback);
} else if (
self.cursorState.cursorIndex === self.cursorState.documents.length &&
!Long.ZERO.equals(self.cursorState.cursorId)
) {
// Ensure an empty cursor state
self.cursorState.documents = [];
self.cursorState.cursorIndex = 0;
// Check if topology is destroyed
if (self.topology.isDestroyed())
return callback(
new MongoNetworkError('connection destroyed, not possible to instantiate cursor')
);
// Check if connection is dead and return if not possible to
// execute a getMore on this connection
if (isConnectionDead(self, callback)) return;
// Execute the next get more
self._getMore(function(err, doc, connection) {
if (err) {
if (err instanceof MongoError) {
err[mongoErrorContextSymbol].isGetMore = true;
}
return handleCallback(callback, err);
}
if (self.cursorState.cursorId && self.cursorState.cursorId.isZero() && self._endSession) {
self._endSession();
}
// Save the returned connection to ensure all getMore's fire over the same connection
self.connection = connection;
// Tailable cursor getMore result, notify owner about it
// No attempt is made here to retry, this is left to the user of the
// core module to handle to keep core simple
if (
self.cursorState.documents.length === 0 &&
self.cmd.tailable &&
Long.ZERO.equals(self.cursorState.cursorId)
) {
// No more documents in the tailed cursor
return handleCallback(
callback,
new MongoError({
message: 'No more documents in tailed cursor',
tailable: self.cmd.tailable,
awaitData: self.cmd.awaitData
})
);
} else if (
self.cursorState.documents.length === 0 &&
self.cmd.tailable &&
!Long.ZERO.equals(self.cursorState.cursorId)
) {
return nextFunction(self, callback);
}
if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
return setCursorDeadAndNotified(self, callback);
}
nextFunction(self, callback);
});
} else if (
self.cursorState.documents.length === self.cursorState.cursorIndex &&
self.cmd.tailable &&
Long.ZERO.equals(self.cursorState.cursorId)
) {
return handleCallback(
callback,
new MongoError({
message: 'No more documents in tailed cursor',
tailable: self.cmd.tailable,
awaitData: self.cmd.awaitData
})
);
} else if (
self.cursorState.documents.length === self.cursorState.cursorIndex &&
Long.ZERO.equals(self.cursorState.cursorId)
) {
setCursorDeadAndNotified(self, callback);
} else {
if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
// Ensure we kill the cursor on the server
self.kill();
// Set cursor in dead and notified state
return setCursorDeadAndNotified(self, callback);
}
// Increment the current cursor limit
self.cursorState.currentLimit += 1;
// Get the document
let doc = self.cursorState.documents[self.cursorState.cursorIndex++];
// Doc overflow
if (!doc || doc.$err) {
// Ensure we kill the cursor on the server
self.kill();
// Set cursor in dead and notified state
return setCursorDeadAndNotified(self, function() {
handleCallback(callback, new MongoError(doc ? doc.$err : undefined));
});
}
// Transform the doc with passed in transformation method if provided
if (self.cursorState.transforms && typeof self.cursorState.transforms.doc === 'function') {
doc = self.cursorState.transforms.doc(doc);
}
// Return the document
handleCallback(callback, null, doc);
}
}
module.exports = {
CursorState,
CoreCursor
};
+232
View File
@@ -0,0 +1,232 @@
'use strict';
const mongoErrorContextSymbol = Symbol('mongoErrorContextSymbol');
const maxWireVersion = require('./utils').maxWireVersion;
/**
* Creates a new MongoError
*
* @augments Error
* @param {Error|string|object} message The error message
* @property {string} message The error message
* @property {string} stack The error call stack
*/
class MongoError extends Error {
constructor(message) {
if (message instanceof Error) {
super(message.message);
this.stack = message.stack;
} else {
if (typeof message === 'string') {
super(message);
} else {
super(message.message || message.errmsg || message.$err || 'n/a');
for (var name in message) {
this[name] = message[name];
}
}
Error.captureStackTrace(this, this.constructor);
}
this.name = 'MongoError';
this[mongoErrorContextSymbol] = this[mongoErrorContextSymbol] || {};
}
/**
* Creates a new MongoError object
*
* @param {Error|string|object} options The options used to create the error.
* @return {MongoError} A MongoError instance
* @deprecated Use `new MongoError()` instead.
*/
static create(options) {
return new MongoError(options);
}
hasErrorLabel(label) {
return this.errorLabels && this.errorLabels.indexOf(label) !== -1;
}
}
/**
* Creates a new MongoNetworkError
*
* @param {Error|string|object} message The error message
* @property {string} message The error message
* @property {string} stack The error call stack
*/
class MongoNetworkError extends MongoError {
constructor(message) {
super(message);
this.name = 'MongoNetworkError';
// This is added as part of the transactions specification
this.errorLabels = ['TransientTransactionError'];
}
}
/**
* An error used when attempting to parse a value (like a connection string)
*
* @param {Error|string|object} message The error message
* @property {string} message The error message
*/
class MongoParseError extends MongoError {
constructor(message) {
super(message);
this.name = 'MongoParseError';
}
}
/**
* An error signifying a timeout event
*
* @param {Error|string|object} message The error message
* @property {string} message The error message
*/
class MongoTimeoutError extends MongoError {
constructor(message) {
super(message);
this.name = 'MongoTimeoutError';
}
}
function makeWriteConcernResultObject(input) {
const output = Object.assign({}, input);
if (output.ok === 0) {
output.ok = 1;
delete output.errmsg;
delete output.code;
delete output.codeName;
}
return output;
}
/**
* An error thrown when the server reports a writeConcernError
*
* @param {Error|string|object} message The error message
* @param {object} result The result document (provided if ok: 1)
* @property {string} message The error message
* @property {object} [result] The result document (provided if ok: 1)
*/
class MongoWriteConcernError extends MongoError {
constructor(message, result) {
super(message);
this.name = 'MongoWriteConcernError';
if (result != null) {
this.result = makeWriteConcernResultObject(result);
}
}
}
// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
const RETRYABLE_ERROR_CODES = new Set([
6, // HostUnreachable
7, // HostNotFound
89, // NetworkTimeout
91, // ShutdownInProgress
189, // PrimarySteppedDown
9001, // SocketException
10107, // NotMaster
11600, // InterruptedAtShutdown
11602, // InterruptedDueToReplStateChange
13435, // NotMasterNoSlaveOk
13436 // NotMasterOrSecondary
]);
/**
* Determines whether an error is something the driver should attempt to retry
*
* @param {MongoError|Error} error
*/
function isRetryableError(error) {
return (
RETRYABLE_ERROR_CODES.has(error.code) ||
error instanceof MongoNetworkError ||
error.message.match(/not master/) ||
error.message.match(/node is recovering/)
);
}
const SDAM_RECOVERING_CODES = new Set([
91, // ShutdownInProgress
189, // PrimarySteppedDown
11600, // InterruptedAtShutdown
11602, // InterruptedDueToReplStateChange
13436 // NotMasterOrSecondary
]);
const SDAM_NOTMASTER_CODES = new Set([
10107, // NotMaster
13435 // NotMasterNoSlaveOk
]);
const SDAM_NODE_SHUTTING_DOWN_ERROR_CODES = new Set([
11600, // InterruptedAtShutdown
91 // ShutdownInProgress
]);
function isRecoveringError(err) {
if (err.code && SDAM_RECOVERING_CODES.has(err.code)) {
return true;
}
return err.message.match(/not master or secondary/) || err.message.match(/node is recovering/);
}
function isNotMasterError(err) {
if (err.code && SDAM_NOTMASTER_CODES.has(err.code)) {
return true;
}
if (isRecoveringError(err)) {
return false;
}
return err.message.match(/not master/);
}
function isNodeShuttingDownError(err) {
return err.code && SDAM_NODE_SHUTTING_DOWN_ERROR_CODES.has(err.code);
}
/**
* Determines whether SDAM can recover from a given error. If it cannot
* then the pool will be cleared, and server state will completely reset
* locally.
*
* @see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-master-and-node-is-recovering
* @param {MongoError|Error} error
* @param {Server} server
*/
function isSDAMUnrecoverableError(error, server) {
if (error instanceof MongoParseError) {
return true;
}
if (isRecoveringError(error) || isNotMasterError(error)) {
if (maxWireVersion(server) >= 8 && !isNodeShuttingDownError(error)) {
return false;
}
return true;
}
return false;
}
module.exports = {
MongoError,
MongoNetworkError,
MongoParseError,
MongoTimeoutError,
MongoWriteConcernError,
mongoErrorContextSymbol,
isRetryableError,
isSDAMUnrecoverableError
};
+50
View File
@@ -0,0 +1,50 @@
'use strict';
let BSON = require('bson');
const require_optional = require('require_optional');
const EJSON = require('./utils').retrieveEJSON();
try {
// Attempt to grab the native BSON parser
const BSONNative = require_optional('bson-ext');
// If we got the native parser, use it instead of the
// Javascript one
if (BSONNative) {
BSON = BSONNative;
}
} catch (err) {} // eslint-disable-line
module.exports = {
// Errors
MongoError: require('./error').MongoError,
MongoNetworkError: require('./error').MongoNetworkError,
MongoParseError: require('./error').MongoParseError,
MongoTimeoutError: require('./error').MongoTimeoutError,
MongoWriteConcernError: require('./error').MongoWriteConcernError,
mongoErrorContextSymbol: require('./error').mongoErrorContextSymbol,
// Core
Connection: require('./connection/connection'),
Server: require('./topologies/server'),
ReplSet: require('./topologies/replset'),
Mongos: require('./topologies/mongos'),
Logger: require('./connection/logger'),
Cursor: require('./cursor').CoreCursor,
ReadPreference: require('./topologies/read_preference'),
Sessions: require('./sessions'),
BSON: BSON,
EJSON: EJSON,
Topology: require('./sdam/topology'),
// Raw operations
Query: require('./connection/commands').Query,
// Auth mechanisms
MongoCredentials: require('./auth/mongo_credentials').MongoCredentials,
defaultAuthProviders: require('./auth/defaultAuthProviders').defaultAuthProviders,
MongoCR: require('./auth/mongocr'),
X509: require('./auth/x509'),
Plain: require('./auth/plain'),
GSSAPI: require('./auth/gssapi'),
ScramSHA1: require('./auth/scram').ScramSHA1,
ScramSHA256: require('./auth/scram').ScramSHA256,
// Utilities
parseConnectionString: require('./uri_parser')
};
+228
View File
@@ -0,0 +1,228 @@
'use strict';
const ServerDescription = require('./server_description').ServerDescription;
const calculateDurationInMs = require('../utils').calculateDurationInMs;
/**
* Published when server description changes, but does NOT include changes to the RTT.
*
* @property {Object} topologyId A unique identifier for the topology
* @property {ServerAddress} address The address (host/port pair) of the server
* @property {ServerDescription} previousDescription The previous server description
* @property {ServerDescription} newDescription The new server description
*/
class ServerDescriptionChangedEvent {
constructor(topologyId, address, previousDescription, newDescription) {
Object.assign(this, { topologyId, address, previousDescription, newDescription });
}
}
/**
* Published when server is initialized.
*
* @property {Object} topologyId A unique identifier for the topology
* @property {ServerAddress} address The address (host/port pair) of the server
*/
class ServerOpeningEvent {
constructor(topologyId, address) {
Object.assign(this, { topologyId, address });
}
}
/**
* Published when server is closed.
*
* @property {ServerAddress} address The address (host/port pair) of the server
* @property {Object} topologyId A unique identifier for the topology
*/
class ServerClosedEvent {
constructor(topologyId, address) {
Object.assign(this, { topologyId, address });
}
}
/**
* Published when topology description changes.
*
* @property {Object} topologyId
* @property {TopologyDescription} previousDescription The old topology description
* @property {TopologyDescription} newDescription The new topology description
*/
class TopologyDescriptionChangedEvent {
constructor(topologyId, previousDescription, newDescription) {
Object.assign(this, { topologyId, previousDescription, newDescription });
}
}
/**
* Published when topology is initialized.
*
* @param {Object} topologyId A unique identifier for the topology
*/
class TopologyOpeningEvent {
constructor(topologyId) {
Object.assign(this, { topologyId });
}
}
/**
* Published when topology is closed.
*
* @param {Object} topologyId A unique identifier for the topology
*/
class TopologyClosedEvent {
constructor(topologyId) {
Object.assign(this, { topologyId });
}
}
/**
* Fired when the server monitors ismaster command is started - immediately before
* the ismaster command is serialized into raw BSON and written to the socket.
*
* @property {Object} connectionId The connection id for the command
*/
class ServerHeartbeatStartedEvent {
constructor(connectionId) {
Object.assign(this, { connectionId });
}
}
/**
* Fired when the server monitors ismaster succeeds.
*
* @param {Number} duration The execution time of the event in ms
* @param {Object} reply The command reply
* @param {Object} connectionId The connection id for the command
*/
class ServerHeartbeatSucceededEvent {
constructor(duration, reply, connectionId) {
Object.assign(this, { duration, reply, connectionId });
}
}
/**
* Fired when the server monitors ismaster fails, either with an “ok: 0” or a socket exception.
*
* @param {Number} duration The execution time of the event in ms
* @param {MongoError|Object} failure The command failure
* @param {Object} connectionId The connection id for the command
*/
class ServerHeartbeatFailedEvent {
constructor(duration, failure, connectionId) {
Object.assign(this, { duration, failure, connectionId });
}
}
/**
* Performs a server check as described by the SDAM spec.
*
* NOTE: This method automatically reschedules itself, so that there is always an active
* monitoring process
*
* @param {Server} server The server to monitor
*/
function monitorServer(server, options) {
options = options || {};
const heartbeatFrequencyMS = options.heartbeatFrequencyMS || 10000;
if (options.initial === true) {
server.s.monitorId = setTimeout(() => monitorServer(server), heartbeatFrequencyMS);
return;
}
// executes a single check of a server
const checkServer = callback => {
let start = process.hrtime();
// emit a signal indicating we have started the heartbeat
server.emit('serverHeartbeatStarted', new ServerHeartbeatStartedEvent(server.name));
// NOTE: legacy monitoring event
process.nextTick(() => server.emit('monitoring', server));
server.command(
'admin.$cmd',
{ ismaster: true },
{
monitoring: true,
socketTimeout: server.s.options.connectionTimeout || 2000
},
(err, result) => {
let duration = calculateDurationInMs(start);
if (err) {
server.emit(
'serverHeartbeatFailed',
new ServerHeartbeatFailedEvent(duration, err, server.name)
);
return callback(err, null);
}
const isMaster = result.result;
server.emit(
'serverHeartbeatSucceded',
new ServerHeartbeatSucceededEvent(duration, isMaster, server.name)
);
return callback(null, isMaster);
}
);
};
const successHandler = isMaster => {
server.s.monitoring = false;
// emit an event indicating that our description has changed
server.emit('descriptionReceived', new ServerDescription(server.description.address, isMaster));
// schedule the next monitoring process
server.s.monitorId = setTimeout(() => monitorServer(server), heartbeatFrequencyMS);
};
// run the actual monitoring loop
server.s.monitoring = true;
checkServer((err, isMaster) => {
if (!err) {
successHandler(isMaster);
return;
}
// According to the SDAM specification's "Network error during server check" section, if
// an ismaster call fails we reset the server's pool. If a server was once connected,
// change its type to `Unknown` only after retrying once.
server.s.pool.reset(() => {
// otherwise re-attempt monitoring once
checkServer((error, isMaster) => {
if (error) {
server.s.monitoring = false;
// we revert to an `Unknown` by emitting a default description with no isMaster
server.emit(
'descriptionReceived',
new ServerDescription(server.description.address, null, { error })
);
// we do not reschedule monitoring in this case
return;
}
successHandler(isMaster);
});
});
});
}
module.exports = {
ServerDescriptionChangedEvent,
ServerOpeningEvent,
ServerClosedEvent,
TopologyDescriptionChangedEvent,
TopologyOpeningEvent,
TopologyClosedEvent,
ServerHeartbeatStartedEvent,
ServerHeartbeatSucceededEvent,
ServerHeartbeatFailedEvent,
monitorServer
};
+491
View File
@@ -0,0 +1,491 @@
'use strict';
const EventEmitter = require('events');
const MongoError = require('../error').MongoError;
const Pool = require('../connection/pool');
const relayEvents = require('../utils').relayEvents;
const wireProtocol = require('../wireprotocol');
const BSON = require('../connection/utils').retrieveBSON();
const createClientInfo = require('../topologies/shared').createClientInfo;
const Logger = require('../connection/logger');
const ServerDescription = require('./server_description').ServerDescription;
const ReadPreference = require('../topologies/read_preference');
const monitorServer = require('./monitoring').monitorServer;
const MongoParseError = require('../error').MongoParseError;
const MongoNetworkError = require('../error').MongoNetworkError;
const collationNotSupported = require('../utils').collationNotSupported;
const debugOptions = require('../connection/utils').debugOptions;
const isSDAMUnrecoverableError = require('../error').isSDAMUnrecoverableError;
// Used for filtering out fields for logging
const DEBUG_FIELDS = [
'reconnect',
'reconnectTries',
'reconnectInterval',
'emitError',
'cursorFactory',
'host',
'port',
'size',
'keepAlive',
'keepAliveInitialDelay',
'noDelay',
'connectionTimeout',
'checkServerIdentity',
'socketTimeout',
'ssl',
'ca',
'crl',
'cert',
'key',
'rejectUnauthorized',
'promoteLongs',
'promoteValues',
'promoteBuffers',
'servername'
];
const STATE_DISCONNECTED = 0;
const STATE_CONNECTING = 1;
const STATE_CONNECTED = 2;
/**
*
* @fires Server#serverHeartbeatStarted
* @fires Server#serverHeartbeatSucceeded
* @fires Server#serverHeartbeatFailed
*/
class Server extends EventEmitter {
/**
* Create a server
*
* @param {ServerDescription} description
* @param {Object} options
*/
constructor(description, options, topology) {
super();
this.s = {
// the server description
description,
// a saved copy of the incoming options
options,
// the server logger
logger: Logger('Server', options),
// the bson parser
bson: options.bson || new BSON(),
// client metadata for the initial handshake
clientInfo: createClientInfo(options),
// state variable to determine if there is an active server check in progress
monitoring: false,
// the implementation of the monitoring method
monitorFunction: options.monitorFunction || monitorServer,
// the connection pool
pool: null,
// the server state
state: STATE_DISCONNECTED,
credentials: options.credentials,
topology
};
}
get description() {
return this.s.description;
}
get name() {
return this.s.description.address;
}
get autoEncrypter() {
if (this.s.options && this.s.options.autoEncrypter) {
return this.s.options.autoEncrypter;
}
return null;
}
/**
* Initiate server connect
*/
connect(options) {
options = options || {};
// do not allow connect to be called on anything that's not disconnected
if (this.s.pool && !this.s.pool.isDisconnected() && !this.s.pool.isDestroyed()) {
throw new MongoError(`Server instance in invalid state ${this.s.pool.state}`);
}
// create a pool
const addressParts = this.description.address.split(':');
const poolOptions = Object.assign(
{ host: addressParts[0], port: parseInt(addressParts[1], 10) },
this.s.options,
options,
{ bson: this.s.bson }
);
// NOTE: this should only be the case if we are connecting to a single server
poolOptions.reconnect = true;
this.s.pool = new Pool(this, poolOptions);
// setup listeners
this.s.pool.on('connect', connectEventHandler(this));
this.s.pool.on('close', errorEventHandler(this));
this.s.pool.on('error', errorEventHandler(this));
this.s.pool.on('parseError', parseErrorEventHandler(this));
// it is unclear whether consumers should even know about these events
// this.s.pool.on('timeout', timeoutEventHandler(this));
// this.s.pool.on('reconnect', reconnectEventHandler(this));
// this.s.pool.on('reconnectFailed', errorEventHandler(this));
// relay all command monitoring events
relayEvents(this.s.pool, this, ['commandStarted', 'commandSucceeded', 'commandFailed']);
this.s.state = STATE_CONNECTING;
// If auth settings have been provided, use them
if (options.auth) {
this.s.pool.connect.apply(this.s.pool, options.auth);
return;
}
this.s.pool.connect();
}
/**
* Destroy the server connection
*
* @param {Boolean} [options.force=false] Force destroy the pool
*/
destroy(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = Object.assign({}, { force: false }, options);
const done = err => {
this.emit('closed');
this.s.state = STATE_DISCONNECTED;
if (typeof callback === 'function') {
callback(err, null);
}
};
if (!this.s.pool) {
return done();
}
['close', 'error', 'timeout', 'parseError', 'connect'].forEach(event => {
this.s.pool.removeAllListeners(event);
});
if (this.s.monitorId) {
clearTimeout(this.s.monitorId);
}
this.s.pool.destroy(options.force, done);
}
/**
* Immediately schedule monitoring of this server. If there already an attempt being made
* this will be a no-op.
*/
monitor(options) {
options = options || {};
if (this.s.state !== STATE_CONNECTED || this.s.monitoring) return;
if (this.s.monitorId) clearTimeout(this.s.monitorId);
this.s.monitorFunction(this, options);
}
/**
* Execute a command
*
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object} cmd The command hash
* @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.checkKeys=false] Specify if the bson parser should validate keys.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {Boolean} [options.fullResult=false] Return the full envelope instead of just the result document.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {opResultCallback} callback A callback function
*/
command(ns, cmd, options, callback) {
if (typeof options === 'function') {
(callback = options), (options = {}), (options = options || {});
}
const error = basicReadValidations(this, options);
if (error) {
return callback(error, null);
}
// Clone the options
options = Object.assign({}, options, { wireProtocolCommand: false });
// Debug log
if (this.s.logger.isDebug()) {
this.s.logger.debug(
`executing command [${JSON.stringify({
ns,
cmd,
options: debugOptions(DEBUG_FIELDS, options)
})}] against ${this.name}`
);
}
// error if collation not supported
if (collationNotSupported(this, cmd)) {
callback(new MongoError(`server ${this.name} does not support collation`));
return;
}
wireProtocol.command(this, ns, cmd, options, (err, result) => {
if (err) {
if (options.session && err instanceof MongoNetworkError) {
options.session.serverSession.isDirty = true;
}
if (isSDAMUnrecoverableError(err, this)) {
this.emit('error', err);
}
}
callback(err, result);
});
}
/**
* Execute a query against the server
*
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object} cmd The command document for the query
* @param {object} options Optional settings
* @param {function} callback
*/
query(ns, cmd, cursorState, options, callback) {
wireProtocol.query(this, ns, cmd, cursorState, options, (err, result) => {
if (err) {
if (options.session && err instanceof MongoNetworkError) {
options.session.serverSession.isDirty = true;
}
if (isSDAMUnrecoverableError(err, this)) {
this.emit('error', err);
}
}
callback(err, result);
});
}
/**
* Execute a `getMore` against the server
*
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object} cursorState State data associated with the cursor calling this method
* @param {object} options Optional settings
* @param {function} callback
*/
getMore(ns, cursorState, batchSize, options, callback) {
wireProtocol.getMore(this, ns, cursorState, batchSize, options, (err, result) => {
if (err) {
if (options.session && err instanceof MongoNetworkError) {
options.session.serverSession.isDirty = true;
}
if (isSDAMUnrecoverableError(err, this)) {
this.emit('error', err);
}
}
callback(err, result);
});
}
/**
* Execute a `killCursors` command against the server
*
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object} cursorState State data associated with the cursor calling this method
* @param {function} callback
*/
killCursors(ns, cursorState, callback) {
wireProtocol.killCursors(this, ns, cursorState, (err, result) => {
if (err && isSDAMUnrecoverableError(err, this)) {
this.emit('error', err);
}
if (typeof callback === 'function') {
callback(err, result);
}
});
}
/**
* Insert one or more documents
* @method
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {array} ops An array of documents to insert
* @param {boolean} [options.ordered=true] Execute in order or out of order
* @param {object} [options.writeConcern={}] Write concern for the operation
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {opResultCallback} callback A callback function
*/
insert(ns, ops, options, callback) {
executeWriteOperation({ server: this, op: 'insert', ns, ops }, options, callback);
}
/**
* Perform one or more update operations
* @method
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {array} ops An array of updates
* @param {boolean} [options.ordered=true] Execute in order or out of order
* @param {object} [options.writeConcern={}] Write concern for the operation
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {opResultCallback} callback A callback function
*/
update(ns, ops, options, callback) {
executeWriteOperation({ server: this, op: 'update', ns, ops }, options, callback);
}
/**
* Perform one or more remove operations
* @method
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {array} ops An array of removes
* @param {boolean} [options.ordered=true] Execute in order or out of order
* @param {object} [options.writeConcern={}] Write concern for the operation
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {opResultCallback} callback A callback function
*/
remove(ns, ops, options, callback) {
executeWriteOperation({ server: this, op: 'remove', ns, ops }, options, callback);
}
}
Object.defineProperty(Server.prototype, 'clusterTime', {
get: function() {
return this.s.topology.clusterTime;
},
set: function(clusterTime) {
this.s.topology.clusterTime = clusterTime;
}
});
function basicWriteValidations(server) {
if (!server.s.pool) {
return new MongoError('server instance is not connected');
}
if (server.s.pool.isDestroyed()) {
return new MongoError('server instance pool was destroyed');
}
return null;
}
function basicReadValidations(server, options) {
const error = basicWriteValidations(server, options);
if (error) {
return error;
}
if (options.readPreference && !(options.readPreference instanceof ReadPreference)) {
return new MongoError('readPreference must be an instance of ReadPreference');
}
}
function executeWriteOperation(args, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
// TODO: once we drop Node 4, use destructuring either here or in arguments.
const server = args.server;
const op = args.op;
const ns = args.ns;
const ops = Array.isArray(args.ops) ? args.ops : [args.ops];
const error = basicWriteValidations(server, options);
if (error) {
callback(error, null);
return;
}
if (collationNotSupported(server, options)) {
callback(new MongoError(`server ${server.name} does not support collation`));
return;
}
return wireProtocol[op](server, ns, ops, options, (err, result) => {
if (err) {
if (options.session && err instanceof MongoNetworkError) {
options.session.serverSession.isDirty = true;
}
if (isSDAMUnrecoverableError(err, server)) {
server.emit('error', err);
}
}
callback(err, result);
});
}
function connectEventHandler(server) {
return function(pool, conn) {
const ismaster = conn.ismaster;
server.s.lastIsMasterMS = conn.lastIsMasterMS;
if (conn.agreedCompressor) {
server.s.pool.options.agreedCompressor = conn.agreedCompressor;
}
if (conn.zlibCompressionLevel) {
server.s.pool.options.zlibCompressionLevel = conn.zlibCompressionLevel;
}
if (conn.ismaster.$clusterTime) {
const $clusterTime = conn.ismaster.$clusterTime;
server.s.sclusterTime = $clusterTime;
}
// log the connection event if requested
if (server.s.logger.isInfo()) {
server.s.logger.info(
`server ${server.name} connected with ismaster [${JSON.stringify(ismaster)}]`
);
}
// emit an event indicating that our description has changed
server.emit('descriptionReceived', new ServerDescription(server.description.address, ismaster));
// we are connected and handshaked (guaranteed by the pool)
server.s.state = STATE_CONNECTED;
server.emit('connect', server);
};
}
function errorEventHandler(server) {
return function(err) {
if (err) {
server.emit('error', new MongoNetworkError(err));
}
server.emit('close');
};
}
function parseErrorEventHandler(server) {
return function(err) {
server.s.state = STATE_DISCONNECTED;
server.emit('error', new MongoParseError(err));
};
}
module.exports = Server;
+163
View File
@@ -0,0 +1,163 @@
'use strict';
// An enumeration of server types we know about
const ServerType = {
Standalone: 'Standalone',
Mongos: 'Mongos',
PossiblePrimary: 'PossiblePrimary',
RSPrimary: 'RSPrimary',
RSSecondary: 'RSSecondary',
RSArbiter: 'RSArbiter',
RSOther: 'RSOther',
RSGhost: 'RSGhost',
Unknown: 'Unknown'
};
const WRITABLE_SERVER_TYPES = new Set([
ServerType.RSPrimary,
ServerType.Standalone,
ServerType.Mongos
]);
const DATA_BEARING_SERVER_TYPES = new Set([
ServerType.RSPrimary,
ServerType.RSSecondary,
ServerType.Mongos,
ServerType.Standalone
]);
const ISMASTER_FIELDS = [
'minWireVersion',
'maxWireVersion',
'maxBsonObjectSize',
'maxMessageSizeBytes',
'maxWriteBatchSize',
'compression',
'me',
'hosts',
'passives',
'arbiters',
'tags',
'setName',
'setVersion',
'electionId',
'primary',
'logicalSessionTimeoutMinutes',
'saslSupportedMechs',
'__nodejs_mock_server__',
'$clusterTime'
];
/**
* The client's view of a single server, based on the most recent ismaster outcome.
*
* Internal type, not meant to be directly instantiated
*/
class ServerDescription {
/**
* Create a ServerDescription
* @param {String} address The address of the server
* @param {Object} [ismaster] An optional ismaster response for this server
* @param {Object} [options] Optional settings
* @param {Number} [options.roundTripTime] The round trip time to ping this server (in ms)
*/
constructor(address, ismaster, options) {
options = options || {};
ismaster = Object.assign(
{
minWireVersion: 0,
maxWireVersion: 0,
hosts: [],
passives: [],
arbiters: [],
tags: []
},
ismaster
);
this.address = address;
this.error = options.error || null;
this.roundTripTime = options.roundTripTime || 0;
this.lastUpdateTime = Date.now();
this.lastWriteDate = ismaster.lastWrite ? ismaster.lastWrite.lastWriteDate : null;
this.opTime = ismaster.lastWrite ? ismaster.lastWrite.opTime : null;
this.type = parseServerType(ismaster);
// direct mappings
ISMASTER_FIELDS.forEach(field => {
if (typeof ismaster[field] !== 'undefined') this[field] = ismaster[field];
});
// normalize case for hosts
if (this.me) this.me = this.me.toLowerCase();
this.hosts = this.hosts.map(host => host.toLowerCase());
this.passives = this.passives.map(host => host.toLowerCase());
this.arbiters = this.arbiters.map(host => host.toLowerCase());
}
get allHosts() {
return this.hosts.concat(this.arbiters).concat(this.passives);
}
/**
* @return {Boolean} Is this server available for reads
*/
get isReadable() {
return this.type === ServerType.RSSecondary || this.isWritable;
}
/**
* @return {Boolean} Is this server data bearing
*/
get isDataBearing() {
return DATA_BEARING_SERVER_TYPES.has(this.type);
}
/**
* @return {Boolean} Is this server available for writes
*/
get isWritable() {
return WRITABLE_SERVER_TYPES.has(this.type);
}
}
/**
* Parses an `ismaster` message and determines the server type
*
* @param {Object} ismaster The `ismaster` message to parse
* @return {ServerType}
*/
function parseServerType(ismaster) {
if (!ismaster || !ismaster.ok) {
return ServerType.Unknown;
}
if (ismaster.isreplicaset) {
return ServerType.RSGhost;
}
if (ismaster.msg && ismaster.msg === 'isdbgrid') {
return ServerType.Mongos;
}
if (ismaster.setName) {
if (ismaster.hidden) {
return ServerType.RSOther;
} else if (ismaster.ismaster) {
return ServerType.RSPrimary;
} else if (ismaster.secondary) {
return ServerType.RSSecondary;
} else if (ismaster.arbiterOnly) {
return ServerType.RSArbiter;
} else {
return ServerType.RSOther;
}
}
return ServerType.Standalone;
}
module.exports = {
ServerDescription,
ServerType
};
+244
View File
@@ -0,0 +1,244 @@
'use strict';
const ServerType = require('./server_description').ServerType;
const TopologyType = require('./topology_description').TopologyType;
const ReadPreference = require('../topologies/read_preference');
const MongoError = require('../error').MongoError;
// max staleness constants
const IDLE_WRITE_PERIOD = 10000;
const SMALLEST_MAX_STALENESS_SECONDS = 90;
/**
* Returns a server selector that selects for writable servers
*/
function writableServerSelector() {
return function(topologyDescription, servers) {
return latencyWindowReducer(topologyDescription, servers.filter(s => s.isWritable));
};
}
/**
* Reduces the passed in array of servers by the rules of the "Max Staleness" specification
* found here: https://github.com/mongodb/specifications/blob/master/source/max-staleness/max-staleness.rst
*
* @param {ReadPreference} readPreference The read preference providing max staleness guidance
* @param {topologyDescription} topologyDescription The topology description
* @param {ServerDescription[]} servers The list of server descriptions to be reduced
* @return {ServerDescription[]} The list of servers that satisfy the requirements of max staleness
*/
function maxStalenessReducer(readPreference, topologyDescription, servers) {
if (readPreference.maxStalenessSeconds == null || readPreference.maxStalenessSeconds < 0) {
return servers;
}
const maxStaleness = readPreference.maxStalenessSeconds;
const maxStalenessVariance =
(topologyDescription.heartbeatFrequencyMS + IDLE_WRITE_PERIOD) / 1000;
if (maxStaleness < maxStalenessVariance) {
throw new MongoError(`maxStalenessSeconds must be at least ${maxStalenessVariance} seconds`);
}
if (maxStaleness < SMALLEST_MAX_STALENESS_SECONDS) {
throw new MongoError(
`maxStalenessSeconds must be at least ${SMALLEST_MAX_STALENESS_SECONDS} seconds`
);
}
if (topologyDescription.type === TopologyType.ReplicaSetWithPrimary) {
const primary = servers.filter(primaryFilter)[0];
return servers.reduce((result, server) => {
const stalenessMS =
server.lastUpdateTime -
server.lastWriteDate -
(primary.lastUpdateTime - primary.lastWriteDate) +
topologyDescription.heartbeatFrequencyMS;
const staleness = stalenessMS / 1000;
if (staleness <= readPreference.maxStalenessSeconds) result.push(server);
return result;
}, []);
} else if (topologyDescription.type === TopologyType.ReplicaSetNoPrimary) {
const sMax = servers.reduce((max, s) => (s.lastWriteDate > max.lastWriteDate ? s : max));
return servers.reduce((result, server) => {
const stalenessMS =
sMax.lastWriteDate - server.lastWriteDate + topologyDescription.heartbeatFrequencyMS;
const staleness = stalenessMS / 1000;
if (staleness <= readPreference.maxStalenessSeconds) result.push(server);
return result;
}, []);
}
return servers;
}
/**
* Determines whether a server's tags match a given set of tags
*
* @param {String[]} tagSet The requested tag set to match
* @param {String[]} serverTags The server's tags
*/
function tagSetMatch(tagSet, serverTags) {
const keys = Object.keys(tagSet);
const serverTagKeys = Object.keys(serverTags);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if (serverTagKeys.indexOf(key) === -1 || serverTags[key] !== tagSet[key]) {
return false;
}
}
return true;
}
/**
* Reduces a set of server descriptions based on tags requested by the read preference
*
* @param {ReadPreference} readPreference The read preference providing the requested tags
* @param {ServerDescription[]} servers The list of server descriptions to reduce
* @return {ServerDescription[]} The list of servers matching the requested tags
*/
function tagSetReducer(readPreference, servers) {
if (
readPreference.tags == null ||
(Array.isArray(readPreference.tags) && readPreference.tags.length === 0)
) {
return servers;
}
for (let i = 0; i < readPreference.tags.length; ++i) {
const tagSet = readPreference.tags[i];
const serversMatchingTagset = servers.reduce((matched, server) => {
if (tagSetMatch(tagSet, server.tags)) matched.push(server);
return matched;
}, []);
if (serversMatchingTagset.length) {
return serversMatchingTagset;
}
}
return [];
}
/**
* Reduces a list of servers to ensure they fall within an acceptable latency window. This is
* further specified in the "Server Selection" specification, found here:
* https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst
*
* @param {topologyDescription} topologyDescription The topology description
* @param {ServerDescription[]} servers The list of servers to reduce
* @returns {ServerDescription[]} The servers which fall within an acceptable latency window
*/
function latencyWindowReducer(topologyDescription, servers) {
const low = servers.reduce(
(min, server) => (min === -1 ? server.roundTripTime : Math.min(server.roundTripTime, min)),
-1
);
const high = low + topologyDescription.localThresholdMS;
return servers.reduce((result, server) => {
if (server.roundTripTime <= high && server.roundTripTime >= low) result.push(server);
return result;
}, []);
}
// filters
function primaryFilter(server) {
return server.type === ServerType.RSPrimary;
}
function secondaryFilter(server) {
return server.type === ServerType.RSSecondary;
}
function nearestFilter(server) {
return server.type === ServerType.RSSecondary || server.type === ServerType.RSPrimary;
}
function knownFilter(server) {
return server.type !== ServerType.Unknown;
}
/**
* Returns a function which selects servers based on a provided read preference
*
* @param {ReadPreference} readPreference The read preference to select with
*/
function readPreferenceServerSelector(readPreference) {
if (!readPreference.isValid()) {
throw new TypeError('Invalid read preference specified');
}
return function(topologyDescription, servers) {
const commonWireVersion = topologyDescription.commonWireVersion;
if (
commonWireVersion &&
(readPreference.minWireVersion && readPreference.minWireVersion > commonWireVersion)
) {
throw new MongoError(
`Minimum wire version '${
readPreference.minWireVersion
}' required, but found '${commonWireVersion}'`
);
}
if (
topologyDescription.type === TopologyType.Single ||
topologyDescription.type === TopologyType.Sharded
) {
return latencyWindowReducer(topologyDescription, servers.filter(knownFilter));
}
if (readPreference.mode === ReadPreference.PRIMARY) {
return servers.filter(primaryFilter);
}
if (readPreference.mode === ReadPreference.SECONDARY) {
return latencyWindowReducer(
topologyDescription,
tagSetReducer(
readPreference,
maxStalenessReducer(readPreference, topologyDescription, servers)
)
).filter(secondaryFilter);
} else if (readPreference.mode === ReadPreference.NEAREST) {
return latencyWindowReducer(
topologyDescription,
tagSetReducer(
readPreference,
maxStalenessReducer(readPreference, topologyDescription, servers)
)
).filter(nearestFilter);
} else if (readPreference.mode === ReadPreference.SECONDARY_PREFERRED) {
const result = latencyWindowReducer(
topologyDescription,
tagSetReducer(
readPreference,
maxStalenessReducer(readPreference, topologyDescription, servers)
)
).filter(secondaryFilter);
return result.length === 0 ? servers.filter(primaryFilter) : result;
} else if (readPreference.mode === ReadPreference.PRIMARY_PREFERRED) {
const result = servers.filter(primaryFilter);
if (result.length) {
return result;
}
return latencyWindowReducer(
topologyDescription,
tagSetReducer(
readPreference,
maxStalenessReducer(readPreference, topologyDescription, servers)
)
).filter(secondaryFilter);
}
};
}
module.exports = {
writableServerSelector,
readPreferenceServerSelector
};
+135
View File
@@ -0,0 +1,135 @@
'use strict';
const Logger = require('../connection/logger');
const EventEmitter = require('events').EventEmitter;
const dns = require('dns');
/**
* Determines whether a provided address matches the provided parent domain in order
* to avoid certain attack vectors.
*
* @param {String} srvAddress The address to check against a domain
* @param {String} parentDomain The domain to check the provided address against
* @return {Boolean} Whether the provided address matches the parent domain
*/
function matchesParentDomain(srvAddress, parentDomain) {
const regex = /^.*?\./;
const srv = `.${srvAddress.replace(regex, '')}`;
const parent = `.${parentDomain.replace(regex, '')}`;
return srv.endsWith(parent);
}
class SrvPollingEvent {
constructor(srvRecords) {
this.srvRecords = srvRecords;
}
addresses() {
return new Set(this.srvRecords.map(record => `${record.name}:${record.port}`));
}
}
class SrvPoller extends EventEmitter {
/**
* @param {object} options
* @param {string} options.srvHost
* @param {number} [options.heartbeatFrequencyMS]
* @param {function} [options.logger]
* @param {string} [options.loggerLevel]
*/
constructor(options) {
super();
if (!options || !options.srvHost) {
throw new TypeError('options for SrvPoller must exist and include srvHost');
}
this.srvHost = options.srvHost;
this.rescanSrvIntervalMS = 60000;
this.heartbeatFrequencyMS = options.heartbeatFrequencyMS || 10000;
this.logger = Logger('srvPoller', options);
this.haMode = false;
this.generation = 0;
this._timeout = null;
}
get srvAddress() {
return `_mongodb._tcp.${this.srvHost}`;
}
get intervalMS() {
return this.haMode ? this.heartbeatFrequencyMS : this.rescanSrvIntervalMs;
}
start() {
if (!this._timeout) {
this.schedule();
}
}
stop() {
if (this._timeout) {
clearTimeout(this._timeout);
this.generation += 1;
this._timeout = null;
}
}
schedule() {
clearTimeout(this._timeout);
this._timeout = setTimeout(() => this._poll(), this.intervalMS);
}
success(srvRecords) {
this.haMode = false;
this.schedule();
this.emit('srvRecordDiscovery', new SrvPollingEvent(srvRecords));
}
failure(message, obj) {
this.logger.warn(message, obj);
this.haMode = true;
this.schedule();
}
parentDomainMismatch(srvRecord) {
this.logger.warn(
`parent domain mismatch on SRV record (${srvRecord.name}:${srvRecord.port})`,
srvRecord
);
}
_poll() {
const generation = this.generation;
dns.resolveSrv(this.srvAddress, (err, srvRecords) => {
if (generation !== this.generation) {
return;
}
if (err) {
this.failure('DNS error', err);
return;
}
const finalAddresses = [];
srvRecords.forEach(record => {
if (matchesParentDomain(record.name, this.srvHost)) {
finalAddresses.push(record);
} else {
this.parentDomainMismatch(record);
}
});
if (!finalAddresses.length) {
this.failure('No valid addresses found at host');
return;
}
this.success(finalAddresses);
});
}
}
module.exports.SrvPollingEvent = SrvPollingEvent;
module.exports.SrvPoller = SrvPoller;
+1158
View File
File diff suppressed because it is too large. Load diff
+408
View File
@@ -0,0 +1,408 @@
'use strict';
const ServerType = require('./server_description').ServerType;
const ServerDescription = require('./server_description').ServerDescription;
const WIRE_CONSTANTS = require('../wireprotocol/constants');
// contstants related to compatability checks
const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION;
const MAX_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_SERVER_VERSION;
const MIN_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_WIRE_VERSION;
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
// An enumeration of topology types we know about
const TopologyType = {
Single: 'Single',
ReplicaSetNoPrimary: 'ReplicaSetNoPrimary',
ReplicaSetWithPrimary: 'ReplicaSetWithPrimary',
Sharded: 'Sharded',
Unknown: 'Unknown'
};
// Representation of a deployment of servers
class TopologyDescription {
/**
* Create a TopologyDescription
*
* @param {string} topologyType
* @param {Map<string, ServerDescription>} serverDescriptions the a map of address to ServerDescription
* @param {string} setName
* @param {number} maxSetVersion
* @param {ObjectId} maxElectionId
*/
constructor(
topologyType,
serverDescriptions,
setName,
maxSetVersion,
maxElectionId,
commonWireVersion,
options,
error
) {
options = options || {};
// TODO: consider assigning all these values to a temporary value `s` which
// we use `Object.freeze` on, ensuring the internal state of this type
// is immutable.
this.type = topologyType || TopologyType.Unknown;
this.setName = setName || null;
this.maxSetVersion = maxSetVersion || null;
this.maxElectionId = maxElectionId || null;
this.servers = serverDescriptions || new Map();
this.stale = false;
this.compatible = true;
this.compatibilityError = null;
this.logicalSessionTimeoutMinutes = null;
this.heartbeatFrequencyMS = options.heartbeatFrequencyMS || 0;
this.localThresholdMS = options.localThresholdMS || 0;
this.options = options;
this.error = error;
this.commonWireVersion = commonWireVersion || null;
// determine server compatibility
for (const serverDescription of this.servers.values()) {
if (serverDescription.type === ServerType.Unknown) continue;
if (serverDescription.minWireVersion > MAX_SUPPORTED_WIRE_VERSION) {
this.compatible = false;
this.compatibilityError = `Server at ${serverDescription.address} requires wire version ${
serverDescription.minWireVersion
}, but this version of the driver only supports up to ${MAX_SUPPORTED_WIRE_VERSION} (MongoDB ${MAX_SUPPORTED_SERVER_VERSION})`;
}
if (serverDescription.maxWireVersion < MIN_SUPPORTED_WIRE_VERSION) {
this.compatible = false;
this.compatibilityError = `Server at ${serverDescription.address} reports wire version ${
serverDescription.maxWireVersion
}, but this version of the driver requires at least ${MIN_SUPPORTED_WIRE_VERSION} (MongoDB ${MIN_SUPPORTED_SERVER_VERSION}).`;
break;
}
}
// Whenever a client updates the TopologyDescription from an ismaster response, it MUST set
// TopologyDescription.logicalSessionTimeoutMinutes to the smallest logicalSessionTimeoutMinutes
// value among ServerDescriptions of all data-bearing server types. If any have a null
// logicalSessionTimeoutMinutes, then TopologyDescription.logicalSessionTimeoutMinutes MUST be
// set to null.
const readableServers = Array.from(this.servers.values()).filter(s => s.isReadable);
this.logicalSessionTimeoutMinutes = readableServers.reduce((result, server) => {
if (server.logicalSessionTimeoutMinutes == null) return null;
if (result == null) return server.logicalSessionTimeoutMinutes;
return Math.min(result, server.logicalSessionTimeoutMinutes);
}, null);
}
/**
* Returns a new TopologyDescription based on the SrvPollingEvent
* @param {SrvPollingEvent} ev The event
*/
updateFromSrvPollingEvent(ev) {
const newAddresses = ev.addresses();
const serverDescriptions = new Map(this.servers);
for (const server of this.servers) {
if (newAddresses.has(server[0])) {
newAddresses.delete(server[0]);
} else {
serverDescriptions.delete(server[0]);
}
}
if (serverDescriptions.size === this.servers.size && newAddresses.size === 0) {
return this;
}
for (const address of newAddresses) {
serverDescriptions.set(address, new ServerDescription(address));
}
return new TopologyDescription(
this.type,
serverDescriptions,
this.setName,
this.maxSetVersion,
this.maxElectionId,
this.commonWireVersion,
this.options,
null
);
}
/**
* Returns a copy of this description updated with a given ServerDescription
*
* @param {ServerDescription} serverDescription
*/
update(serverDescription) {
const address = serverDescription.address;
// NOTE: there are a number of prime targets for refactoring here
// once we support destructuring assignments
// potentially mutated values
let topologyType = this.type;
let setName = this.setName;
let maxSetVersion = this.maxSetVersion;
let maxElectionId = this.maxElectionId;
let commonWireVersion = this.commonWireVersion;
let error = serverDescription.error || null;
const serverType = serverDescription.type;
let serverDescriptions = new Map(this.servers);
// update common wire version
if (serverDescription.maxWireVersion !== 0) {
if (commonWireVersion == null) {
commonWireVersion = serverDescription.maxWireVersion;
} else {
commonWireVersion = Math.min(commonWireVersion, serverDescription.maxWireVersion);
}
}
// update the actual server description
serverDescriptions.set(address, serverDescription);
if (topologyType === TopologyType.Single) {
// once we are defined as single, that never changes
return new TopologyDescription(
TopologyType.Single,
serverDescriptions,
setName,
maxSetVersion,
maxElectionId,
commonWireVersion,
this.options,
error
);
}
if (topologyType === TopologyType.Unknown) {
if (serverType === ServerType.Standalone) {
serverDescriptions.delete(address);
} else {
topologyType = topologyTypeForServerType(serverType);
}
}
if (topologyType === TopologyType.Sharded) {
if ([ServerType.Mongos, ServerType.Unknown].indexOf(serverType) === -1) {
serverDescriptions.delete(address);
}
}
if (topologyType === TopologyType.ReplicaSetNoPrimary) {
if ([ServerType.Mongos, ServerType.Unknown].indexOf(serverType) >= 0) {
serverDescriptions.delete(address);
}
if (serverType === ServerType.RSPrimary) {
const result = updateRsFromPrimary(
serverDescriptions,
setName,
serverDescription,
maxSetVersion,
maxElectionId
);
(topologyType = result[0]),
(setName = result[1]),
(maxSetVersion = result[2]),
(maxElectionId = result[3]);
} else if (
[ServerType.RSSecondary, ServerType.RSArbiter, ServerType.RSOther].indexOf(serverType) >= 0
) {
const result = updateRsNoPrimaryFromMember(serverDescriptions, setName, serverDescription);
(topologyType = result[0]), (setName = result[1]);
}
}
if (topologyType === TopologyType.ReplicaSetWithPrimary) {
if ([ServerType.Standalone, ServerType.Mongos].indexOf(serverType) >= 0) {
serverDescriptions.delete(address);
topologyType = checkHasPrimary(serverDescriptions);
} else if (serverType === ServerType.RSPrimary) {
const result = updateRsFromPrimary(
serverDescriptions,
setName,
serverDescription,
maxSetVersion,
maxElectionId
);
(topologyType = result[0]),
(setName = result[1]),
(maxSetVersion = result[2]),
(maxElectionId = result[3]);
} else if (
[ServerType.RSSecondary, ServerType.RSArbiter, ServerType.RSOther].indexOf(serverType) >= 0
) {
topologyType = updateRsWithPrimaryFromMember(
serverDescriptions,
setName,
serverDescription
);
} else {
topologyType = checkHasPrimary(serverDescriptions);
}
}
return new TopologyDescription(
topologyType,
serverDescriptions,
setName,
maxSetVersion,
maxElectionId,
commonWireVersion,
this.options,
error
);
}
/**
* Determines if the topology description has any known servers
*/
get hasKnownServers() {
return Array.from(this.servers.values()).some(sd => sd.type !== ServerDescription.Unknown);
}
/**
* Determines if this topology description has a data-bearing server available.
*/
get hasDataBearingServers() {
return Array.from(this.servers.values()).some(sd => sd.isDataBearing);
}
/**
* Determines if the topology has a definition for the provided address
*
* @param {String} address
* @return {Boolean} Whether the topology knows about this server
*/
hasServer(address) {
return this.servers.has(address);
}
}
function topologyTypeForServerType(serverType) {
if (serverType === ServerType.Mongos) return TopologyType.Sharded;
if (serverType === ServerType.RSPrimary) return TopologyType.ReplicaSetWithPrimary;
return TopologyType.ReplicaSetNoPrimary;
}
function updateRsFromPrimary(
serverDescriptions,
setName,
serverDescription,
maxSetVersion,
maxElectionId
) {
setName = setName || serverDescription.setName;
if (setName !== serverDescription.setName) {
serverDescriptions.delete(serverDescription.address);
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
}
const electionIdOID = serverDescription.electionId ? serverDescription.electionId.$oid : null;
const maxElectionIdOID = maxElectionId ? maxElectionId.$oid : null;
if (serverDescription.setVersion != null && electionIdOID != null) {
if (maxSetVersion != null && maxElectionIdOID != null) {
if (maxSetVersion > serverDescription.setVersion || maxElectionIdOID > electionIdOID) {
// this primary is stale, we must remove it
serverDescriptions.set(
serverDescription.address,
new ServerDescription(serverDescription.address)
);
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
}
}
maxElectionId = serverDescription.electionId;
}
if (
serverDescription.setVersion != null &&
(maxSetVersion == null || serverDescription.setVersion > maxSetVersion)
) {
maxSetVersion = serverDescription.setVersion;
}
// We've heard from the primary. Is it the same primary as before?
for (const address of serverDescriptions.keys()) {
const server = serverDescriptions.get(address);
if (server.type === ServerType.RSPrimary && server.address !== serverDescription.address) {
// Reset old primary's type to Unknown.
serverDescriptions.set(address, new ServerDescription(server.address));
// There can only be one primary
break;
}
}
// Discover new hosts from this primary's response.
serverDescription.allHosts.forEach(address => {
if (!serverDescriptions.has(address)) {
serverDescriptions.set(address, new ServerDescription(address));
}
});
// Remove hosts not in the response.
const currentAddresses = Array.from(serverDescriptions.keys());
const responseAddresses = serverDescription.allHosts;
currentAddresses.filter(addr => responseAddresses.indexOf(addr) === -1).forEach(address => {
serverDescriptions.delete(address);
});
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
}
function updateRsWithPrimaryFromMember(serverDescriptions, setName, serverDescription) {
if (setName == null) {
throw new TypeError('setName is required');
}
if (
setName !== serverDescription.setName ||
(serverDescription.me && serverDescription.address !== serverDescription.me)
) {
serverDescriptions.delete(serverDescription.address);
}
return checkHasPrimary(serverDescriptions);
}
function updateRsNoPrimaryFromMember(serverDescriptions, setName, serverDescription) {
let topologyType = TopologyType.ReplicaSetNoPrimary;
setName = setName || serverDescription.setName;
if (setName !== serverDescription.setName) {
serverDescriptions.delete(serverDescription.address);
return [topologyType, setName];
}
serverDescription.allHosts.forEach(address => {
if (!serverDescriptions.has(address)) {
serverDescriptions.set(address, new ServerDescription(address));
}
});
if (serverDescription.me && serverDescription.address !== serverDescription.me) {
serverDescriptions.delete(serverDescription.address);
}
return [topologyType, setName];
}
function checkHasPrimary(serverDescriptions) {
for (const addr of serverDescriptions.keys()) {
if (serverDescriptions.get(addr).type === ServerType.RSPrimary) {
return TopologyType.ReplicaSetWithPrimary;
}
}
return TopologyType.ReplicaSetNoPrimary;
}
module.exports = {
TopologyType,
TopologyDescription
};
+767
View File
@@ -0,0 +1,767 @@
'use strict';
const retrieveBSON = require('./connection/utils').retrieveBSON;
const EventEmitter = require('events');
const BSON = retrieveBSON();
const Binary = BSON.Binary;
const uuidV4 = require('./utils').uuidV4;
const MongoError = require('./error').MongoError;
const isRetryableError = require('././error').isRetryableError;
const MongoNetworkError = require('./error').MongoNetworkError;
const MongoWriteConcernError = require('./error').MongoWriteConcernError;
const Transaction = require('./transactions').Transaction;
const TxnState = require('./transactions').TxnState;
const isPromiseLike = require('./utils').isPromiseLike;
const ReadPreference = require('./topologies/read_preference');
const isTransactionCommand = require('./transactions').isTransactionCommand;
const resolveClusterTime = require('./topologies/shared').resolveClusterTime;
const isSharded = require('./wireprotocol/shared').isSharded;
const maxWireVersion = require('./utils').maxWireVersion;
const minWireVersionForShardedTransactions = 8;
function assertAlive(session, callback) {
if (session.serverSession == null) {
const error = new MongoError('Cannot use a session that has ended');
if (typeof callback === 'function') {
callback(error, null);
return false;
}
throw error;
}
return true;
}
/**
* Options to pass when creating a Client Session
* @typedef {Object} SessionOptions
* @property {boolean} [causalConsistency=true] Whether causal consistency should be enabled on this session
* @property {TransactionOptions} [defaultTransactionOptions] The default TransactionOptions to use for transactions started on this session.
*/
/**
* A BSON document reflecting the lsid of a {@link ClientSession}
* @typedef {Object} SessionId
*/
/**
* A class representing a client session on the server
* WARNING: not meant to be instantiated directly.
* @class
* @hideconstructor
*/
class ClientSession extends EventEmitter {
/**
* Create a client session.
* WARNING: not meant to be instantiated directly
*
* @param {Topology} topology The current client's topology (Internal Class)
* @param {ServerSessionPool} sessionPool The server session pool (Internal Class)
* @param {SessionOptions} [options] Optional settings
* @param {Object} [clientOptions] Optional settings provided when creating a client in the porcelain driver
*/
constructor(topology, sessionPool, options, clientOptions) {
super();
if (topology == null) {
throw new Error('ClientSession requires a topology');
}
if (sessionPool == null || !(sessionPool instanceof ServerSessionPool)) {
throw new Error('ClientSession requires a ServerSessionPool');
}
options = options || {};
clientOptions = clientOptions || {};
this.topology = topology;
this.sessionPool = sessionPool;
this.hasEnded = false;
this.serverSession = sessionPool.acquire();
this.clientOptions = clientOptions;
this.supports = {
causalConsistency:
typeof options.causalConsistency !== 'undefined' ? options.causalConsistency : true
};
this.clusterTime = options.initialClusterTime;
this.operationTime = null;
this.explicit = !!options.explicit;
this.owner = options.owner;
this.defaultTransactionOptions = Object.assign({}, options.defaultTransactionOptions);
this.transaction = new Transaction();
}
/**
* The server id associated with this session
* @type {SessionId}
*/
get id() {
return this.serverSession.id;
}
/**
* Ends this session on the server
*
* @param {Object} [options] Optional settings. Currently reserved for future use
* @param {Function} [callback] Optional callback for completion of this operation
*/
endSession(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
if (this.hasEnded) {
if (typeof callback === 'function') callback(null, null);
return;
}
if (this.serverSession && this.inTransaction()) {
this.abortTransaction(); // pass in callback?
}
// mark the session as ended, and emit a signal
this.hasEnded = true;
this.emit('ended', this);
// release the server session back to the pool
this.sessionPool.release(this.serverSession);
this.serverSession = null;
// spec indicates that we should ignore all errors for `endSessions`
if (typeof callback === 'function') callback(null, null);
}
/**
* Advances the operationTime for a ClientSession.
*
* @param {Timestamp} operationTime the `BSON.Timestamp` of the operation type it is desired to advance to
*/
advanceOperationTime(operationTime) {
if (this.operationTime == null) {
this.operationTime = operationTime;
return;
}
if (operationTime.greaterThan(this.operationTime)) {
this.operationTime = operationTime;
}
}
/**
* Used to determine if this session equals another
* @param {ClientSession} session
* @return {boolean} true if the sessions are equal
*/
equals(session) {
if (!(session instanceof ClientSession)) {
return false;
}
return this.id.id.buffer.equals(session.id.id.buffer);
}
/**
* Increment the transaction number on the internal ServerSession
*/
incrementTransactionNumber() {
this.serverSession.txnNumber++;
}
/**
* @returns {boolean} whether this session is currently in a transaction or not
*/
inTransaction() {
return this.transaction.isActive;
}
/**
* Starts a new transaction with the given options.
*
* @param {TransactionOptions} options Options for the transaction
*/
startTransaction(options) {
assertAlive(this);
if (this.inTransaction()) {
throw new MongoError('Transaction already in progress');
}
const topologyMaxWireVersion = maxWireVersion(this.topology);
if (
isSharded(this.topology) &&
topologyMaxWireVersion != null &&
topologyMaxWireVersion < minWireVersionForShardedTransactions
) {
throw new MongoError('Transactions are not supported on sharded clusters in MongoDB < 4.2.');
}
// increment txnNumber
this.incrementTransactionNumber();
// create transaction state
this.transaction = new Transaction(
Object.assign({}, this.clientOptions, options || this.defaultTransactionOptions)
);
this.transaction.transition(TxnState.STARTING_TRANSACTION);
}
/**
* Commits the currently active transaction in this session.
*
* @param {Function} [callback] optional callback for completion of this operation
* @return {Promise} A promise is returned if no callback is provided
*/
commitTransaction(callback) {
if (typeof callback === 'function') {
endTransaction(this, 'commitTransaction', callback);
return;
}
return new Promise((resolve, reject) => {
endTransaction(
this,
'commitTransaction',
(err, reply) => (err ? reject(err) : resolve(reply))
);
});
}
/**
* Aborts the currently active transaction in this session.
*
* @param {Function} [callback] optional callback for completion of this operation
* @return {Promise} A promise is returned if no callback is provided
*/
abortTransaction(callback) {
if (typeof callback === 'function') {
endTransaction(this, 'abortTransaction', callback);
return;
}
return new Promise((resolve, reject) => {
endTransaction(
this,
'abortTransaction',
(err, reply) => (err ? reject(err) : resolve(reply))
);
});
}
/**
* This is here to ensure that ClientSession is never serialized to BSON.
* @ignore
*/
toBSON() {
throw new Error('ClientSession cannot be serialized to BSON.');
}
/**
* A user provided function to be run within a transaction
*
* @callback WithTransactionCallback
* @param {ClientSession} session The parent session of the transaction running the operation. This should be passed into each operation within the lambda.
* @returns {Promise} The resulting Promise of operations run within this transaction
*/
/**
* Runs a provided lambda within a transaction, retrying either the commit operation
* or entire transaction as needed (and when the error permits) to better ensure that
* the transaction can complete successfully.
*
* IMPORTANT: This method requires the user to return a Promise, all lambdas that do not
* return a Promise will result in undefined behavior.
*
* @param {WithTransactionCallback} fn
* @param {TransactionOptions} [options] Optional settings for the transaction
*/
withTransaction(fn, options) {
const startTime = Date.now();
return attemptTransaction(this, startTime, fn, options);
}
}
const MAX_WITH_TRANSACTION_TIMEOUT = 120000;
const UNSATISFIABLE_WRITE_CONCERN_CODE = 100;
const UNKNOWN_REPL_WRITE_CONCERN_CODE = 79;
const MAX_TIME_MS_EXPIRED_CODE = 50;
const NON_DETERMINISTIC_WRITE_CONCERN_ERRORS = new Set([
'CannotSatisfyWriteConcern',
'UnknownReplWriteConcern',
'UnsatisfiableWriteConcern'
]);
function hasNotTimedOut(startTime, max) {
return Date.now() - startTime < max;
}
function isUnknownTransactionCommitResult(err) {
return (
isMaxTimeMSExpiredError(err) ||
(!NON_DETERMINISTIC_WRITE_CONCERN_ERRORS.has(err.codeName) &&
err.code !== UNSATISFIABLE_WRITE_CONCERN_CODE &&
err.code !== UNKNOWN_REPL_WRITE_CONCERN_CODE)
);
}
function isMaxTimeMSExpiredError(err) {
return (
err.code === MAX_TIME_MS_EXPIRED_CODE ||
(err.writeConcernError && err.writeConcernError.code === MAX_TIME_MS_EXPIRED_CODE)
);
}
function attemptTransactionCommit(session, startTime, fn, options) {
return session.commitTransaction().catch(err => {
if (
err instanceof MongoError &&
hasNotTimedOut(startTime, MAX_WITH_TRANSACTION_TIMEOUT) &&
!isMaxTimeMSExpiredError(err)
) {
if (err.hasErrorLabel('UnknownTransactionCommitResult')) {
return attemptTransactionCommit(session, startTime, fn, options);
}
if (err.hasErrorLabel('TransientTransactionError')) {
return attemptTransaction(session, startTime, fn, options);
}
}
throw err;
});
}
const USER_EXPLICIT_TXN_END_STATES = new Set([
TxnState.NO_TRANSACTION,
TxnState.TRANSACTION_COMMITTED,
TxnState.TRANSACTION_ABORTED
]);
function userExplicitlyEndedTransaction(session) {
return USER_EXPLICIT_TXN_END_STATES.has(session.transaction.state);
}
function attemptTransaction(session, startTime, fn, options) {
session.startTransaction(options);
let promise;
try {
promise = fn(session);
} catch (err) {
promise = Promise.reject(err);
}
if (!isPromiseLike(promise)) {
session.abortTransaction();
throw new TypeError('Function provided to `withTransaction` must return a Promise');
}
return promise
.then(() => {
if (userExplicitlyEndedTransaction(session)) {
return;
}
return attemptTransactionCommit(session, startTime, fn, options);
})
.catch(err => {
function maybeRetryOrThrow(err) {
if (
err instanceof MongoError &&
err.hasErrorLabel('TransientTransactionError') &&
hasNotTimedOut(startTime, MAX_WITH_TRANSACTION_TIMEOUT)
) {
return attemptTransaction(session, startTime, fn, options);
}
if (isMaxTimeMSExpiredError(err)) {
if (err.errorLabels == null) {
err.errorLabels = [];
}
err.errorLabels.push('UnknownTransactionCommitResult');
}
throw err;
}
if (session.transaction.isActive) {
return session.abortTransaction().then(() => maybeRetryOrThrow(err));
}
return maybeRetryOrThrow(err);
});
}
function endTransaction(session, commandName, callback) {
if (!assertAlive(session, callback)) {
// checking result in case callback was called
return;
}
// handle any initial problematic cases
let txnState = session.transaction.state;
if (txnState === TxnState.NO_TRANSACTION) {
callback(new MongoError('No transaction started'));
return;
}
if (commandName === 'commitTransaction') {
if (
txnState === TxnState.STARTING_TRANSACTION ||
txnState === TxnState.TRANSACTION_COMMITTED_EMPTY
) {
// the transaction was never started, we can safely exit here
session.transaction.transition(TxnState.TRANSACTION_COMMITTED_EMPTY);
callback(null, null);
return;
}
if (txnState === TxnState.TRANSACTION_ABORTED) {
callback(new MongoError('Cannot call commitTransaction after calling abortTransaction'));
return;
}
} else {
if (txnState === TxnState.STARTING_TRANSACTION) {
// the transaction was never started, we can safely exit here
session.transaction.transition(TxnState.TRANSACTION_ABORTED);
callback(null, null);
return;
}
if (txnState === TxnState.TRANSACTION_ABORTED) {
callback(new MongoError('Cannot call abortTransaction twice'));
return;
}
if (
txnState === TxnState.TRANSACTION_COMMITTED ||
txnState === TxnState.TRANSACTION_COMMITTED_EMPTY
) {
callback(new MongoError('Cannot call abortTransaction after calling commitTransaction'));
return;
}
}
// construct and send the command
const command = { [commandName]: 1 };
// apply a writeConcern if specified
let writeConcern;
if (session.transaction.options.writeConcern) {
writeConcern = Object.assign({}, session.transaction.options.writeConcern);
} else if (session.clientOptions && session.clientOptions.w) {
writeConcern = { w: session.clientOptions.w };
}
if (txnState === TxnState.TRANSACTION_COMMITTED) {
writeConcern = Object.assign({ wtimeout: 10000 }, writeConcern, { w: 'majority' });
}
if (writeConcern) {
Object.assign(command, { writeConcern });
}
if (commandName === 'commitTransaction' && session.transaction.options.maxTimeMS) {
Object.assign(command, { maxTimeMS: session.transaction.options.maxTimeMS });
}
function commandHandler(e, r) {
if (commandName === 'commitTransaction') {
session.transaction.transition(TxnState.TRANSACTION_COMMITTED);
if (
e &&
(e instanceof MongoNetworkError ||
e instanceof MongoWriteConcernError ||
isRetryableError(e) ||
isMaxTimeMSExpiredError(e))
) {
if (e.errorLabels) {
const idx = e.errorLabels.indexOf('TransientTransactionError');
if (idx !== -1) {
e.errorLabels.splice(idx, 1);
}
} else {
e.errorLabels = [];
}
if (isUnknownTransactionCommitResult(e)) {
e.errorLabels.push('UnknownTransactionCommitResult');
// per txns spec, must unpin session in this case
session.transaction.unpinServer();
}
}
} else {
session.transaction.transition(TxnState.TRANSACTION_ABORTED);
}
callback(e, r);
}
// The spec indicates that we should ignore all errors on `abortTransaction`
function transactionError(err) {
return commandName === 'commitTransaction' ? err : null;
}
if (
// Assumption here that commandName is "commitTransaction" or "abortTransaction"
session.transaction.recoveryToken &&
supportsRecoveryToken(session)
) {
command.recoveryToken = session.transaction.recoveryToken;
}
// send the command
session.topology.command('admin.$cmd', command, { session }, (err, reply) => {
if (err && isRetryableError(err)) {
// SPEC-1185: apply majority write concern when retrying commitTransaction
if (command.commitTransaction) {
// per txns spec, must unpin session in this case
session.transaction.unpinServer();
command.writeConcern = Object.assign({ wtimeout: 10000 }, command.writeConcern, {
w: 'majority'
});
}
return session.topology.command('admin.$cmd', command, { session }, (_err, _reply) =>
commandHandler(transactionError(_err), _reply)
);
}
commandHandler(transactionError(err), reply);
});
}
function supportsRecoveryToken(session) {
const topology = session.topology;
return !!topology.s.options.useRecoveryToken;
}
/**
* Reflects the existence of a session on the server. Can be reused by the session pool.
* WARNING: not meant to be instantiated directly. For internal use only.
* @ignore
*/
class ServerSession {
constructor() {
this.id = { id: new Binary(uuidV4(), Binary.SUBTYPE_UUID) };
this.lastUse = Date.now();
this.txnNumber = 0;
this.isDirty = false;
}
/**
* Determines if the server session has timed out.
* @ignore
* @param {Date} sessionTimeoutMinutes The server's "logicalSessionTimeoutMinutes"
* @return {boolean} true if the session has timed out.
*/
hasTimedOut(sessionTimeoutMinutes) {
// Take the difference of the lastUse timestamp and now, which will result in a value in
// milliseconds, and then convert milliseconds to minutes to compare to `sessionTimeoutMinutes`
const idleTimeMinutes = Math.round(
(((Date.now() - this.lastUse) % 86400000) % 3600000) / 60000
);
return idleTimeMinutes > sessionTimeoutMinutes - 1;
}
}
/**
* Maintains a pool of Server Sessions.
* For internal use only
* @ignore
*/
class ServerSessionPool {
constructor(topology) {
if (topology == null) {
throw new Error('ServerSessionPool requires a topology');
}
this.topology = topology;
this.sessions = [];
}
/**
* Ends all sessions in the session pool.
* @ignore
*/
endAllPooledSessions() {
if (this.sessions.length) {
this.topology.endSessions(this.sessions.map(session => session.id));
this.sessions = [];
}
}
/**
* Acquire a Server Session from the pool.
* Iterates through each session in the pool, removing any stale sessions
* along the way. The first non-stale session found is removed from the
* pool and returned. If no non-stale session is found, a new ServerSession
* is created.
* @ignore
* @returns {ServerSession}
*/
acquire() {
const sessionTimeoutMinutes = this.topology.logicalSessionTimeoutMinutes;
while (this.sessions.length) {
const session = this.sessions.shift();
if (!session.hasTimedOut(sessionTimeoutMinutes)) {
return session;
}
}
return new ServerSession();
}
/**
* Release a session to the session pool
* Adds the session back to the session pool if the session has not timed out yet.
* This method also removes any stale sessions from the pool.
* @ignore
* @param {ServerSession} session The session to release to the pool
*/
release(session) {
const sessionTimeoutMinutes = this.topology.logicalSessionTimeoutMinutes;
while (this.sessions.length) {
const pooledSession = this.sessions[this.sessions.length - 1];
if (pooledSession.hasTimedOut(sessionTimeoutMinutes)) {
this.sessions.pop();
} else {
break;
}
}
if (!session.hasTimedOut(sessionTimeoutMinutes)) {
if (session.isDirty) {
return;
}
// otherwise, readd this session to the session pool
this.sessions.unshift(session);
}
}
}
// TODO: this should be codified in command construction
// @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.rst#read-concern
function commandSupportsReadConcern(command, options) {
if (
command.aggregate ||
command.count ||
command.distinct ||
command.find ||
command.parallelCollectionScan ||
command.geoNear ||
command.geoSearch
) {
return true;
}
if (command.mapReduce && options.out && (options.out.inline === 1 || options.out === 'inline')) {
return true;
}
return false;
}
/**
* Optionally decorate a command with sessions specific keys
*
* @param {ClientSession} session the session tracking transaction state
* @param {Object} command the command to decorate
* @param {Object} topology the topology for tracking the cluster time
* @param {Object} [options] Optional settings passed to calling operation
* @return {MongoError|null} An error, if some error condition was met
*/
function applySession(session, command, options) {
const serverSession = session.serverSession;
if (serverSession == null) {
// TODO: merge this with `assertAlive`, did not want to throw a try/catch here
return new MongoError('Cannot use a session that has ended');
}
// mark the last use of this session, and apply the `lsid`
serverSession.lastUse = Date.now();
command.lsid = serverSession.id;
// first apply non-transaction-specific sessions data
const inTransaction = session.inTransaction() || isTransactionCommand(command);
const isRetryableWrite = options.willRetryWrite;
const shouldApplyReadConcern = commandSupportsReadConcern(command);
if (serverSession.txnNumber && (isRetryableWrite || inTransaction)) {
command.txnNumber = BSON.Long.fromNumber(serverSession.txnNumber);
}
// now attempt to apply transaction-specific sessions data
if (!inTransaction) {
if (session.transaction.state !== TxnState.NO_TRANSACTION) {
session.transaction.transition(TxnState.NO_TRANSACTION);
}
// TODO: the following should only be applied to read operation per spec.
// for causal consistency
if (session.supports.causalConsistency && session.operationTime && shouldApplyReadConcern) {
command.readConcern = command.readConcern || {};
Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
}
return;
}
if (options.readPreference && !options.readPreference.equals(ReadPreference.primary)) {
return new MongoError(
`Read preference in a transaction must be primary, not: ${options.readPreference.mode}`
);
}
// `autocommit` must always be false to differentiate from retryable writes
command.autocommit = false;
if (session.transaction.state === TxnState.STARTING_TRANSACTION) {
session.transaction.transition(TxnState.TRANSACTION_IN_PROGRESS);
command.startTransaction = true;
const readConcern =
session.transaction.options.readConcern || session.clientOptions.readConcern;
if (readConcern) {
command.readConcern = readConcern;
}
if (session.supports.causalConsistency && session.operationTime) {
command.readConcern = command.readConcern || {};
Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
}
}
}
function updateSessionFromResponse(session, document) {
if (document.$clusterTime) {
resolveClusterTime(session, document.$clusterTime);
}
if (document.operationTime && session && session.supports.causalConsistency) {
session.advanceOperationTime(document.operationTime);
}
if (document.recoveryToken && session && session.inTransaction()) {
session.transaction._recoveryToken = document.recoveryToken;
}
}
module.exports = {
ClientSession,
ServerSession,
ServerSessionPool,
TxnState,
applySession,
updateSessionFromResponse,
commandSupportsReadConcern
};
+61
View File
@@ -0,0 +1,61 @@
'use strict';
var fs = require('fs');
/* Note: because this plugin uses process.on('uncaughtException'), only one
* of these can exist at any given time. This plugin and anything else that
* uses process.on('uncaughtException') will conflict. */
exports.attachToRunner = function(runner, outputFile) {
var smokeOutput = { results: [] };
var runningTests = {};
var integraPlugin = {
beforeTest: function(test, callback) {
test.startTime = Date.now();
runningTests[test.name] = test;
callback();
},
afterTest: function(test, callback) {
smokeOutput.results.push({
status: test.status,
start: test.startTime,
end: Date.now(),
test_file: test.name,
exit_code: 0,
url: ''
});
delete runningTests[test.name];
callback();
},
beforeExit: function(obj, callback) {
fs.writeFile(outputFile, JSON.stringify(smokeOutput), function() {
callback();
});
}
};
// In case of exception, make sure we write file
process.on('uncaughtException', function(err) {
// Mark all currently running tests as failed
for (var testName in runningTests) {
smokeOutput.results.push({
status: 'fail',
start: runningTests[testName].startTime,
end: Date.now(),
test_file: testName,
exit_code: 0,
url: ''
});
}
// write file
fs.writeFileSync(outputFile, JSON.stringify(smokeOutput));
// Standard NodeJS uncaught exception handler
console.error(err.stack);
process.exit(1);
});
runner.plugin(integraPlugin);
return integraPlugin;
};
File diff suppressed because it is too large. Load diff
+202
View File
@@ -0,0 +1,202 @@
'use strict';
/**
* The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is
* used to construct connections.
* @class
* @param {string} mode A string describing the read preference mode (primary|primaryPreferred|secondary|secondaryPreferred|nearest)
* @param {array} tags The tags object
* @param {object} [options] Additional read preference options
* @param {number} [options.maxStalenessSeconds] Max secondary read staleness in seconds, Minimum value is 90 seconds.
* @see https://docs.mongodb.com/manual/core/read-preference/
* @return {ReadPreference}
*/
const ReadPreference = function(mode, tags, options) {
if (!ReadPreference.isValid(mode)) {
throw new TypeError(`Invalid read preference mode ${mode}`);
}
// TODO(major): tags MUST be an array of tagsets
if (tags && !Array.isArray(tags)) {
console.warn(
'ReadPreference tags must be an array, this will change in the next major version'
);
if (typeof tags.maxStalenessSeconds !== 'undefined') {
// this is likely an options object
options = tags;
tags = undefined;
} else {
tags = [tags];
}
}
this.mode = mode;
this.tags = tags;
options = options || {};
if (options.maxStalenessSeconds != null) {
if (options.maxStalenessSeconds <= 0) {
throw new TypeError('maxStalenessSeconds must be a positive integer');
}
this.maxStalenessSeconds = options.maxStalenessSeconds;
// NOTE: The minimum required wire version is 5 for this read preference. If the existing
// topology has a lower value then a MongoError will be thrown during server selection.
this.minWireVersion = 5;
}
if (this.mode === ReadPreference.PRIMARY) {
if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) {
throw new TypeError('Primary read preference cannot be combined with tags');
}
if (this.maxStalenessSeconds) {
throw new TypeError('Primary read preference cannot be combined with maxStalenessSeconds');
}
}
};
// Support the deprecated `preference` property introduced in the porcelain layer
Object.defineProperty(ReadPreference.prototype, 'preference', {
enumerable: true,
get: function() {
return this.mode;
}
});
/*
* Read preference mode constants
*/
ReadPreference.PRIMARY = 'primary';
ReadPreference.PRIMARY_PREFERRED = 'primaryPreferred';
ReadPreference.SECONDARY = 'secondary';
ReadPreference.SECONDARY_PREFERRED = 'secondaryPreferred';
ReadPreference.NEAREST = 'nearest';
const VALID_MODES = [
ReadPreference.PRIMARY,
ReadPreference.PRIMARY_PREFERRED,
ReadPreference.SECONDARY,
ReadPreference.SECONDARY_PREFERRED,
ReadPreference.NEAREST,
null
];
/**
* Construct a ReadPreference given an options object.
*
* @param {object} options The options object from which to extract the read preference.
* @return {ReadPreference}
*/
ReadPreference.fromOptions = function(options) {
const readPreference = options.readPreference;
const readPreferenceTags = options.readPreferenceTags;
if (readPreference == null) {
return null;
}
if (typeof readPreference === 'string') {
return new ReadPreference(readPreference, readPreferenceTags);
} else if (!(readPreference instanceof ReadPreference) && typeof readPreference === 'object') {
const mode = readPreference.mode || readPreference.preference;
if (mode && typeof mode === 'string') {
return new ReadPreference(mode, readPreference.tags, {
maxStalenessSeconds: readPreference.maxStalenessSeconds
});
}
}
return readPreference;
};
/**
* Validate if a mode is legal
*
* @method
* @param {string} mode The string representing the read preference mode.
* @return {boolean} True if a mode is valid
*/
ReadPreference.isValid = function(mode) {
return VALID_MODES.indexOf(mode) !== -1;
};
/**
* Validate if a mode is legal
*
* @method
* @param {string} mode The string representing the read preference mode.
* @return {boolean} True if a mode is valid
*/
ReadPreference.prototype.isValid = function(mode) {
return ReadPreference.isValid(typeof mode === 'string' ? mode : this.mode);
};
const needSlaveOk = ['primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];
/**
* Indicates that this readPreference needs the "slaveOk" bit when sent over the wire
* @method
* @return {boolean}
* @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
*/
ReadPreference.prototype.slaveOk = function() {
return needSlaveOk.indexOf(this.mode) !== -1;
};
/**
* Are the two read preference equal
* @method
* @param {ReadPreference} readPreference The read preference with which to check equality
* @return {boolean} True if the two ReadPreferences are equivalent
*/
ReadPreference.prototype.equals = function(readPreference) {
return readPreference.mode === this.mode;
};
/**
* Return JSON representation
* @method
* @return {Object} A JSON representation of the ReadPreference
*/
ReadPreference.prototype.toJSON = function() {
const readPreference = { mode: this.mode };
if (Array.isArray(this.tags)) readPreference.tags = this.tags;
if (this.maxStalenessSeconds) readPreference.maxStalenessSeconds = this.maxStalenessSeconds;
return readPreference;
};
/**
* Primary read preference
* @member
* @type {ReadPreference}
*/
ReadPreference.primary = new ReadPreference('primary');
/**
* Primary Preferred read preference
* @member
* @type {ReadPreference}
*/
ReadPreference.primaryPreferred = new ReadPreference('primaryPreferred');
/**
* Secondary read preference
* @member
* @type {ReadPreference}
*/
ReadPreference.secondary = new ReadPreference('secondary');
/**
* Secondary Preferred read preference
* @member
* @type {ReadPreference}
*/
ReadPreference.secondaryPreferred = new ReadPreference('secondaryPreferred');
/**
* Nearest read preference
* @member
* @type {ReadPreference}
*/
ReadPreference.nearest = new ReadPreference('nearest');
module.exports = ReadPreference;
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+984
View File
@@ -0,0 +1,984 @@
'use strict';
var inherits = require('util').inherits,
f = require('util').format,
EventEmitter = require('events').EventEmitter,
ReadPreference = require('./read_preference'),
Logger = require('../connection/logger'),
debugOptions = require('../connection/utils').debugOptions,
retrieveBSON = require('../connection/utils').retrieveBSON,
Pool = require('../connection/pool'),
MongoError = require('../error').MongoError,
MongoNetworkError = require('../error').MongoNetworkError,
wireProtocol = require('../wireprotocol'),
CoreCursor = require('../cursor').CoreCursor,
sdam = require('./shared'),
createClientInfo = require('./shared').createClientInfo,
createCompressionInfo = require('./shared').createCompressionInfo,
resolveClusterTime = require('./shared').resolveClusterTime,
SessionMixins = require('./shared').SessionMixins,
relayEvents = require('../utils').relayEvents;
const collationNotSupported = require('../utils').collationNotSupported;
// Used for filtering out fields for loggin
var debugFields = [
'reconnect',
'reconnectTries',
'reconnectInterval',
'emitError',
'cursorFactory',
'host',
'port',
'size',
'keepAlive',
'keepAliveInitialDelay',
'noDelay',
'connectionTimeout',
'checkServerIdentity',
'socketTimeout',
'ssl',
'ca',
'crl',
'cert',
'key',
'rejectUnauthorized',
'promoteLongs',
'promoteValues',
'promoteBuffers',
'servername'
];
// Server instance id
var id = 0;
var serverAccounting = false;
var servers = {};
var BSON = retrieveBSON();
/**
* Creates a new Server instance
* @class
* @param {boolean} [options.reconnect=true] Server will attempt to reconnect on loss of connection
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
* @param {number} [options.monitoring=true] Enable the server state monitoring (calling ismaster at monitoringInterval)
* @param {number} [options.monitoringInterval=5000] The interval of calling ismaster when monitoring is enabled.
* @param {Cursor} [options.cursorFactory=Cursor] The cursor factory class used for all query cursors
* @param {string} options.host The server host
* @param {number} options.port The server port
* @param {number} [options.size=5] Server connection pool size
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
* @param {number} [options.keepAliveInitialDelay=300000] Initial delay before TCP keep alive enabled
* @param {boolean} [options.noDelay=true] TCP Connection no delay
* @param {number} [options.connectionTimeout=30000] TCP Connection timeout setting
* @param {number} [options.socketTimeout=360000] TCP Socket timeout setting
* @param {boolean} [options.ssl=false] Use SSL for connection
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
* @param {Buffer} [options.ca] SSL Certificate store binary buffer
* @param {Buffer} [options.crl] SSL Certificate revocation store binary buffer
* @param {Buffer} [options.cert] SSL Certificate binary buffer
* @param {Buffer} [options.key] SSL Key file binary buffer
* @param {string} [options.passphrase] SSL Certificate pass phrase
* @param {boolean} [options.rejectUnauthorized=true] Reject unauthorized server certificates
* @param {string} [options.servername=null] String containing the server name requested via TLS SNI.
* @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 {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
* @return {Server} A cursor instance
* @fires Server#connect
* @fires Server#close
* @fires Server#error
* @fires Server#timeout
* @fires Server#parseError
* @fires Server#reconnect
* @fires Server#reconnectFailed
* @fires Server#serverHeartbeatStarted
* @fires Server#serverHeartbeatSucceeded
* @fires Server#serverHeartbeatFailed
* @fires Server#topologyOpening
* @fires Server#topologyClosed
* @fires Server#topologyDescriptionChanged
* @property {string} type the topology type.
* @property {string} parserType the parser type used (c++ or js).
*/
var Server = function(options) {
options = options || {};
// Add event listener
EventEmitter.call(this);
// Server instance id
this.id = id++;
// Internal state
this.s = {
// Options
options: options,
// Logger
logger: Logger('Server', options),
// Factory overrides
Cursor: options.cursorFactory || CoreCursor,
// BSON instance
bson:
options.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
]),
// Pool
pool: null,
// Disconnect handler
disconnectHandler: options.disconnectHandler,
// Monitor thread (keeps the connection alive)
monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : true,
// Is the server in a topology
inTopology: !!options.parent,
// Monitoring timeout
monitoringInterval:
typeof options.monitoringInterval === 'number' ? options.monitoringInterval : 5000,
// Topology id
topologyId: -1,
compression: { compressors: createCompressionInfo(options) },
// Optional parent topology
parent: options.parent
};
// If this is a single deployment we need to track the clusterTime here
if (!this.s.parent) {
this.s.clusterTime = null;
}
// Curent ismaster
this.ismaster = null;
// Current ping time
this.lastIsMasterMS = -1;
// The monitoringProcessId
this.monitoringProcessId = null;
// Initial connection
this.initialConnect = true;
// Default type
this._type = 'server';
// Set the client info
this.clientInfo = createClientInfo(options);
// Max Stalleness values
// last time we updated the ismaster state
this.lastUpdateTime = 0;
// Last write time
this.lastWriteDate = 0;
// Stalleness
this.staleness = 0;
};
inherits(Server, EventEmitter);
Object.assign(Server.prototype, SessionMixins);
Object.defineProperty(Server.prototype, 'type', {
enumerable: true,
get: function() {
return this._type;
}
});
Object.defineProperty(Server.prototype, 'parserType', {
enumerable: true,
get: function() {
return BSON.native ? 'c++' : 'js';
}
});
Object.defineProperty(Server.prototype, 'logicalSessionTimeoutMinutes', {
enumerable: true,
get: function() {
if (!this.ismaster) return null;
return this.ismaster.logicalSessionTimeoutMinutes || null;
}
});
// In single server deployments we track the clusterTime directly on the topology, however
// in Mongos and ReplSet deployments we instead need to delegate the clusterTime up to the
// tracking objects so we can ensure we are gossiping the maximum time received from the
// server.
Object.defineProperty(Server.prototype, 'clusterTime', {
enumerable: true,
set: function(clusterTime) {
const settings = this.s.parent ? this.s.parent : this.s;
resolveClusterTime(settings, clusterTime);
},
get: function() {
const settings = this.s.parent ? this.s.parent : this.s;
return settings.clusterTime || null;
}
});
Server.enableServerAccounting = function() {
serverAccounting = true;
servers = {};
};
Server.disableServerAccounting = function() {
serverAccounting = false;
};
Server.servers = function() {
return servers;
};
Object.defineProperty(Server.prototype, 'name', {
enumerable: true,
get: function() {
return this.s.options.host + ':' + this.s.options.port;
}
});
function disconnectHandler(self, type, ns, cmd, options, callback) {
// Topology is not connected, save the call in the provided store to be
// Executed at some point when the handler deems it's reconnected
if (
!self.s.pool.isConnected() &&
self.s.options.reconnect &&
self.s.disconnectHandler != null &&
!options.monitoring
) {
self.s.disconnectHandler.add(type, ns, cmd, options, callback);
return true;
}
// If we have no connection error
if (!self.s.pool.isConnected()) {
callback(new MongoError(f('no connection available to server %s', self.name)));
return true;
}
}
function monitoringProcess(self) {
return function() {
// Pool was destroyed do not continue process
if (self.s.pool.isDestroyed()) return;
// Emit monitoring Process event
self.emit('monitoring', self);
// Perform ismaster call
// Get start time
var start = new Date().getTime();
// Execute the ismaster query
self.command(
'admin.$cmd',
{ ismaster: true },
{
socketTimeout:
typeof self.s.options.connectionTimeout !== 'number'
? 2000
: self.s.options.connectionTimeout,
monitoring: true
},
(err, result) => {
// Set initial lastIsMasterMS
self.lastIsMasterMS = new Date().getTime() - start;
if (self.s.pool.isDestroyed()) return;
// Update the ismaster view if we have a result
if (result) {
self.ismaster = result.result;
}
// Re-schedule the monitoring process
self.monitoringProcessId = setTimeout(monitoringProcess(self), self.s.monitoringInterval);
}
);
};
}
var eventHandler = function(self, event) {
return function(err, conn) {
// Log information of received information if in info mode
if (self.s.logger.isInfo()) {
var object = err instanceof MongoError ? JSON.stringify(err) : {};
self.s.logger.info(
f('server %s fired event %s out with message %s', self.name, event, object)
);
}
// Handle connect event
if (event === 'connect') {
self.initialConnect = false;
self.ismaster = conn.ismaster;
self.lastIsMasterMS = conn.lastIsMasterMS;
if (conn.agreedCompressor) {
self.s.pool.options.agreedCompressor = conn.agreedCompressor;
}
if (conn.zlibCompressionLevel) {
self.s.pool.options.zlibCompressionLevel = conn.zlibCompressionLevel;
}
if (conn.ismaster.$clusterTime) {
const $clusterTime = conn.ismaster.$clusterTime;
self.clusterTime = $clusterTime;
}
// It's a proxy change the type so
// the wireprotocol will send $readPreference
if (self.ismaster.msg === 'isdbgrid') {
self._type = 'mongos';
}
// Have we defined self monitoring
if (self.s.monitoring) {
self.monitoringProcessId = setTimeout(monitoringProcess(self), self.s.monitoringInterval);
}
// Emit server description changed if something listening
sdam.emitServerDescriptionChanged(self, {
address: self.name,
arbiters: [],
hosts: [],
passives: [],
type: sdam.getTopologyType(self)
});
if (!self.s.inTopology) {
// Emit topology description changed if something listening
sdam.emitTopologyDescriptionChanged(self, {
topologyType: 'Single',
servers: [
{
address: self.name,
arbiters: [],
hosts: [],
passives: [],
type: sdam.getTopologyType(self)
}
]
});
}
// Log the ismaster if available
if (self.s.logger.isInfo()) {
self.s.logger.info(
f('server %s connected with ismaster [%s]', self.name, JSON.stringify(self.ismaster))
);
}
// Emit connect
self.emit('connect', self);
} else if (
event === 'error' ||
event === 'parseError' ||
event === 'close' ||
event === 'timeout' ||
event === 'reconnect' ||
event === 'attemptReconnect' ||
'reconnectFailed'
) {
// Remove server instance from accounting
if (
serverAccounting &&
['close', 'timeout', 'error', 'parseError', 'reconnectFailed'].indexOf(event) !== -1
) {
// Emit toplogy opening event if not in topology
if (!self.s.inTopology) {
self.emit('topologyOpening', { topologyId: self.id });
}
delete servers[self.id];
}
if (event === 'close') {
// Closing emits a server description changed event going to unknown.
sdam.emitServerDescriptionChanged(self, {
address: self.name,
arbiters: [],
hosts: [],
passives: [],
type: 'Unknown'
});
}
// Reconnect failed return error
if (event === 'reconnectFailed') {
self.emit('reconnectFailed', err);
// Emit error if any listeners
if (self.listeners('error').length > 0) {
self.emit('error', err);
}
// Terminate
return;
}
// On first connect fail
if (
['disconnected', 'connecting'].indexOf(self.s.pool.state) !== -1 &&
self.initialConnect &&
['close', 'timeout', 'error', 'parseError'].indexOf(event) !== -1
) {
self.initialConnect = false;
return self.emit(
'error',
new MongoNetworkError(
f('failed to connect to server [%s] on first connect [%s]', self.name, err)
)
);
}
// Reconnect event, emit the server
if (event === 'reconnect') {
// Reconnecting emits a server description changed event going from unknown to the
// current server type.
sdam.emitServerDescriptionChanged(self, {
address: self.name,
arbiters: [],
hosts: [],
passives: [],
type: sdam.getTopologyType(self)
});
return self.emit(event, self);
}
// Emit the event
self.emit(event, err);
}
};
};
/**
* Initiate server connect
*/
Server.prototype.connect = function(options) {
var self = this;
options = options || {};
// Set the connections
if (serverAccounting) servers[this.id] = this;
// Do not allow connect to be called on anything that's not disconnected
if (self.s.pool && !self.s.pool.isDisconnected() && !self.s.pool.isDestroyed()) {
throw new MongoError(f('server instance in invalid state %s', self.s.pool.state));
}
// Create a pool
self.s.pool = new Pool(this, Object.assign(self.s.options, options, { bson: this.s.bson }));
// Set up listeners
self.s.pool.on('close', eventHandler(self, 'close'));
self.s.pool.on('error', eventHandler(self, 'error'));
self.s.pool.on('timeout', eventHandler(self, 'timeout'));
self.s.pool.on('parseError', eventHandler(self, 'parseError'));
self.s.pool.on('connect', eventHandler(self, 'connect'));
self.s.pool.on('reconnect', eventHandler(self, 'reconnect'));
self.s.pool.on('reconnectFailed', eventHandler(self, 'reconnectFailed'));
// Set up listeners for command monitoring
relayEvents(self.s.pool, self, ['commandStarted', 'commandSucceeded', 'commandFailed']);
// Emit toplogy opening event if not in topology
if (!self.s.inTopology) {
this.emit('topologyOpening', { topologyId: self.id });
}
// Emit opening server event
self.emit('serverOpening', {
topologyId: self.s.topologyId !== -1 ? self.s.topologyId : self.id,
address: self.name
});
self.s.pool.connect();
};
/**
* Authenticate the topology.
* @method
* @param {MongoCredentials} credentials The credentials for authentication we are using
* @param {authResultCallback} callback A callback function
*/
Server.prototype.auth = function(credentials, callback) {
if (typeof callback === 'function') callback(null, null);
};
/**
* Get the server description
* @method
* @return {object}
*/
Server.prototype.getDescription = function() {
var ismaster = this.ismaster || {};
var description = {
type: sdam.getTopologyType(this),
address: this.name
};
// Add fields if available
if (ismaster.hosts) description.hosts = ismaster.hosts;
if (ismaster.arbiters) description.arbiters = ismaster.arbiters;
if (ismaster.passives) description.passives = ismaster.passives;
if (ismaster.setName) description.setName = ismaster.setName;
return description;
};
/**
* Returns the last known ismaster document for this server
* @method
* @return {object}
*/
Server.prototype.lastIsMaster = function() {
return this.ismaster;
};
/**
* Unref all connections belong to this server
* @method
*/
Server.prototype.unref = function() {
this.s.pool.unref();
};
/**
* Figure out if the server is connected
* @method
* @return {boolean}
*/
Server.prototype.isConnected = function() {
if (!this.s.pool) return false;
return this.s.pool.isConnected();
};
/**
* Figure out if the server instance was destroyed by calling destroy
* @method
* @return {boolean}
*/
Server.prototype.isDestroyed = function() {
if (!this.s.pool) return false;
return this.s.pool.isDestroyed();
};
function basicWriteValidations(self) {
if (!self.s.pool) return new MongoError('server instance is not connected');
if (self.s.pool.isDestroyed()) return new MongoError('server instance pool was destroyed');
}
function basicReadValidations(self, options) {
basicWriteValidations(self, options);
if (options.readPreference && !(options.readPreference instanceof ReadPreference)) {
throw new Error('readPreference must be an instance of ReadPreference');
}
}
/**
* Execute a command
* @method
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object} cmd The command hash
* @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.checkKeys=false] Specify if the bson parser should validate keys.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {Boolean} [options.fullResult=false] Return the full envelope instead of just the result document.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {opResultCallback} callback A callback function
*/
Server.prototype.command = function(ns, cmd, options, callback) {
var self = this;
if (typeof options === 'function') {
(callback = options), (options = {}), (options = options || {});
}
var result = basicReadValidations(self, options);
if (result) return callback(result);
// Clone the options
options = Object.assign({}, options, { wireProtocolCommand: false });
// Debug log
if (self.s.logger.isDebug())
self.s.logger.debug(
f(
'executing command [%s] against %s',
JSON.stringify({
ns: ns,
cmd: cmd,
options: debugOptions(debugFields, options)
}),
self.name
)
);
// If we are not connected or have a disconnectHandler specified
if (disconnectHandler(self, 'command', ns, cmd, options, callback)) return;
// error if collation not supported
if (collationNotSupported(this, cmd)) {
return callback(new MongoError(`server ${this.name} does not support collation`));
}
wireProtocol.command(self, ns, cmd, options, callback);
};
/**
* Execute a query against the server
*
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object} cmd The command document for the query
* @param {object} options Optional settings
* @param {function} callback
*/
Server.prototype.query = function(ns, cmd, cursorState, options, callback) {
wireProtocol.query(this, ns, cmd, cursorState, options, callback);
};
/**
* Execute a `getMore` against the server
*
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object} cursorState State data associated with the cursor calling this method
* @param {object} options Optional settings
* @param {function} callback
*/
Server.prototype.getMore = function(ns, cursorState, batchSize, options, callback) {
wireProtocol.getMore(this, ns, cursorState, batchSize, options, callback);
};
/**
* Execute a `killCursors` command against the server
*
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object} cursorState State data associated with the cursor calling this method
* @param {function} callback
*/
Server.prototype.killCursors = function(ns, cursorState, callback) {
wireProtocol.killCursors(this, ns, cursorState, callback);
};
/**
* Insert one or more documents
* @method
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {array} ops An array of documents to insert
* @param {boolean} [options.ordered=true] Execute in order or out of order
* @param {object} [options.writeConcern={}] Write concern for the operation
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {opResultCallback} callback A callback function
*/
Server.prototype.insert = function(ns, ops, options, callback) {
var self = this;
if (typeof options === 'function') {
(callback = options), (options = {}), (options = options || {});
}
var result = basicWriteValidations(self, options);
if (result) return callback(result);
// If we are not connected or have a disconnectHandler specified
if (disconnectHandler(self, 'insert', ns, ops, options, callback)) return;
// Setup the docs as an array
ops = Array.isArray(ops) ? ops : [ops];
// Execute write
return wireProtocol.insert(self, ns, ops, options, callback);
};
/**
* Perform one or more update operations
* @method
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {array} ops An array of updates
* @param {boolean} [options.ordered=true] Execute in order or out of order
* @param {object} [options.writeConcern={}] Write concern for the operation
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {opResultCallback} callback A callback function
*/
Server.prototype.update = function(ns, ops, options, callback) {
var self = this;
if (typeof options === 'function') {
(callback = options), (options = {}), (options = options || {});
}
var result = basicWriteValidations(self, options);
if (result) return callback(result);
// If we are not connected or have a disconnectHandler specified
if (disconnectHandler(self, 'update', ns, ops, options, callback)) return;
// error if collation not supported
if (collationNotSupported(this, options)) {
return callback(new MongoError(`server ${this.name} does not support collation`));
}
// Setup the docs as an array
ops = Array.isArray(ops) ? ops : [ops];
// Execute write
return wireProtocol.update(self, ns, ops, options, callback);
};
/**
* Perform one or more remove operations
* @method
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {array} ops An array of removes
* @param {boolean} [options.ordered=true] Execute in order or out of order
* @param {object} [options.writeConcern={}] Write concern for the operation
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {opResultCallback} callback A callback function
*/
Server.prototype.remove = function(ns, ops, options, callback) {
var self = this;
if (typeof options === 'function') {
(callback = options), (options = {}), (options = options || {});
}
var result = basicWriteValidations(self, options);
if (result) return callback(result);
// If we are not connected or have a disconnectHandler specified
if (disconnectHandler(self, 'remove', ns, ops, options, callback)) return;
// error if collation not supported
if (collationNotSupported(this, options)) {
return callback(new MongoError(`server ${this.name} does not support collation`));
}
// Setup the docs as an array
ops = Array.isArray(ops) ? ops : [ops];
// Execute write
return wireProtocol.remove(self, ns, ops, options, callback);
};
/**
* Get a new cursor
* @method
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
* @param {object|Long} cmd Can be either a command returning a cursor or a cursorId
* @param {object} [options] Options for the cursor
* @param {object} [options.batchSize=0] Batchsize for the operation
* @param {array} [options.documents=[]] Initial documents list for cursor
* @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {ClientSession} [options.session=null] Session to use for the operation
* @param {object} [options.topology] The internal topology of the created cursor
* @returns {Cursor}
*/
Server.prototype.cursor = function(ns, cmd, options) {
options = options || {};
const topology = options.topology || this;
// Set up final cursor type
var FinalCursor = options.cursorFactory || this.s.Cursor;
// Return the cursor
return new FinalCursor(topology, ns, cmd, options);
};
/**
* Compare two server instances
* @method
* @param {Server} server Server to compare equality against
* @return {boolean}
*/
Server.prototype.equals = function(server) {
if (typeof server === 'string') return this.name.toLowerCase() === server.toLowerCase();
if (server.name) return this.name.toLowerCase() === server.name.toLowerCase();
return false;
};
/**
* All raw connections
* @method
* @return {Connection[]}
*/
Server.prototype.connections = function() {
return this.s.pool.allConnections();
};
/**
* Selects a server
* @method
* @param {function} selector Unused
* @param {ReadPreference} [options.readPreference] Unused
* @param {ClientSession} [options.session] Unused
* @return {Server}
*/
Server.prototype.selectServer = function(selector, options, callback) {
if (typeof selector === 'function' && typeof callback === 'undefined')
(callback = selector), (selector = undefined), (options = {});
if (typeof options === 'function')
(callback = options), (options = selector), (selector = undefined);
callback(null, this);
};
var listeners = ['close', 'error', 'timeout', 'parseError', 'connect'];
/**
* Destroy the server connection
* @method
* @param {boolean} [options.emitClose=false] Emit close event on destroy
* @param {boolean} [options.emitDestroy=false] Emit destroy event on destroy
* @param {boolean} [options.force=false] Force destroy the pool
*/
Server.prototype.destroy = function(options, callback) {
if (this._destroyed) {
if (typeof callback === 'function') callback(null, null);
return;
}
options = options || {};
var self = this;
// Set the connections
if (serverAccounting) delete servers[this.id];
// Destroy the monitoring process if any
if (this.monitoringProcessId) {
clearTimeout(this.monitoringProcessId);
}
// No pool, return
if (!self.s.pool) {
this._destroyed = true;
if (typeof callback === 'function') callback(null, null);
return;
}
// Emit close event
if (options.emitClose) {
self.emit('close', self);
}
// Emit destroy event
if (options.emitDestroy) {
self.emit('destroy', self);
}
// Remove all listeners
listeners.forEach(function(event) {
self.s.pool.removeAllListeners(event);
});
// Emit opening server event
if (self.listeners('serverClosed').length > 0)
self.emit('serverClosed', {
topologyId: self.s.topologyId !== -1 ? self.s.topologyId : self.id,
address: self.name
});
// Emit toplogy opening event if not in topology
if (self.listeners('topologyClosed').length > 0 && !self.s.inTopology) {
self.emit('topologyClosed', { topologyId: self.id });
}
if (self.s.logger.isDebug()) {
self.s.logger.debug(f('destroy called on server %s', self.name));
}
// Destroy the pool
this.s.pool.destroy(options.force, callback);
this._destroyed = true;
};
/**
* A server connect event, used to verify that the connection is up and running
*
* @event Server#connect
* @type {Server}
*/
/**
* A server reconnect event, used to verify that the server topology has reconnected
*
* @event Server#reconnect
* @type {Server}
*/
/**
* A server opening SDAM monitoring event
*
* @event Server#serverOpening
* @type {object}
*/
/**
* A server closed SDAM monitoring event
*
* @event Server#serverClosed
* @type {object}
*/
/**
* A server description SDAM change monitoring event
*
* @event Server#serverDescriptionChanged
* @type {object}
*/
/**
* A topology open SDAM event
*
* @event Server#topologyOpening
* @type {object}
*/
/**
* A topology closed SDAM event
*
* @event Server#topologyClosed
* @type {object}
*/
/**
* A topology structure SDAM change event
*
* @event Server#topologyDescriptionChanged
* @type {object}
*/
/**
* Server reconnect failed
*
* @event Server#reconnectFailed
* @type {Error}
*/
/**
* Server connection pool closed
*
* @event Server#close
* @type {object}
*/
/**
* Server connection pool caused an error
*
* @event Server#error
* @type {Error}
*/
/**
* Server destroyed was called
*
* @event Server#destroy
* @type {Server}
*/
module.exports = Server;
+475
View File
@@ -0,0 +1,475 @@
'use strict';
const os = require('os');
const f = require('util').format;
const ReadPreference = require('./read_preference');
const Buffer = require('safe-buffer').Buffer;
const TopologyType = require('../sdam/topology_description').TopologyType;
const MongoError = require('../error').MongoError;
const MMAPv1_RETRY_WRITES_ERROR_CODE = 20;
/**
* Emit event if it exists
* @method
*/
function emitSDAMEvent(self, event, description) {
if (self.listeners(event).length > 0) {
self.emit(event, description);
}
}
// Get package.json variable
var driverVersion = require('../../../package.json').version;
var nodejsversion = f('Node.js %s, %s', process.version, os.endianness());
var type = os.type();
var name = process.platform;
var architecture = process.arch;
var release = os.release();
function createClientInfo(options) {
// Build default client information
var clientInfo = options.clientInfo
? clone(options.clientInfo)
: {
driver: {
name: 'nodejs-core',
version: driverVersion
},
os: {
type: type,
name: name,
architecture: architecture,
version: release
}
};
// Is platform specified
if (clientInfo.platform && clientInfo.platform.indexOf('mongodb-core') === -1) {
clientInfo.platform = f('%s, mongodb-core: %s', clientInfo.platform, driverVersion);
} else if (!clientInfo.platform) {
clientInfo.platform = nodejsversion;
}
// Do we have an application specific string
if (options.appname) {
// Cut at 128 bytes
var buffer = Buffer.from(options.appname);
// Return the truncated appname
var appname = buffer.length > 128 ? buffer.slice(0, 128).toString('utf8') : options.appname;
// Add to the clientInfo
clientInfo.application = { name: appname };
}
return clientInfo;
}
function createCompressionInfo(options) {
if (!options.compression || !options.compression.compressors) {
return [];
}
// Check that all supplied compressors are valid
options.compression.compressors.forEach(function(compressor) {
if (compressor !== 'snappy' && compressor !== 'zlib') {
throw new Error('compressors must be at least one of snappy or zlib');
}
});
return options.compression.compressors;
}
function clone(object) {
return JSON.parse(JSON.stringify(object));
}
var getPreviousDescription = function(self) {
if (!self.s.serverDescription) {
self.s.serverDescription = {
address: self.name,
arbiters: [],
hosts: [],
passives: [],
type: 'Unknown'
};
}
return self.s.serverDescription;
};
var emitServerDescriptionChanged = function(self, description) {
if (self.listeners('serverDescriptionChanged').length > 0) {
// Emit the server description changed events
self.emit('serverDescriptionChanged', {
topologyId: self.s.topologyId !== -1 ? self.s.topologyId : self.id,
address: self.name,
previousDescription: getPreviousDescription(self),
newDescription: description
});
self.s.serverDescription = description;
}
};
var getPreviousTopologyDescription = function(self) {
if (!self.s.topologyDescription) {
self.s.topologyDescription = {
topologyType: 'Unknown',
servers: [
{
address: self.name,
arbiters: [],
hosts: [],
passives: [],
type: 'Unknown'
}
]
};
}
return self.s.topologyDescription;
};
var emitTopologyDescriptionChanged = function(self, description) {
if (self.listeners('topologyDescriptionChanged').length > 0) {
// Emit the server description changed events
self.emit('topologyDescriptionChanged', {
topologyId: self.s.topologyId !== -1 ? self.s.topologyId : self.id,
address: self.name,
previousDescription: getPreviousTopologyDescription(self),
newDescription: description
});
self.s.serverDescription = description;
}
};
var changedIsMaster = function(self, currentIsmaster, ismaster) {
var currentType = getTopologyType(self, currentIsmaster);
var newType = getTopologyType(self, ismaster);
if (newType !== currentType) return true;
return false;
};
var getTopologyType = function(self, ismaster) {
if (!ismaster) {
ismaster = self.ismaster;
}
if (!ismaster) return 'Unknown';
if (ismaster.ismaster && ismaster.msg === 'isdbgrid') return 'Mongos';
if (ismaster.ismaster && !ismaster.hosts) return 'Standalone';
if (ismaster.ismaster) return 'RSPrimary';
if (ismaster.secondary) return 'RSSecondary';
if (ismaster.arbiterOnly) return 'RSArbiter';
return 'Unknown';
};
var inquireServerState = function(self) {
return function(callback) {
if (self.s.state === 'destroyed') return;
// Record response time
var start = new Date().getTime();
// emitSDAMEvent
emitSDAMEvent(self, 'serverHeartbeatStarted', { connectionId: self.name });
// Attempt to execute ismaster command
self.command('admin.$cmd', { ismaster: true }, { monitoring: true }, function(err, r) {
if (!err) {
// Legacy event sender
self.emit('ismaster', r, self);
// Calculate latencyMS
var latencyMS = new Date().getTime() - start;
// Server heart beat event
emitSDAMEvent(self, 'serverHeartbeatSucceeded', {
durationMS: latencyMS,
reply: r.result,
connectionId: self.name
});
// Did the server change
if (changedIsMaster(self, self.s.ismaster, r.result)) {
// Emit server description changed if something listening
emitServerDescriptionChanged(self, {
address: self.name,
arbiters: [],
hosts: [],
passives: [],
type: !self.s.inTopology ? 'Standalone' : getTopologyType(self)
});
}
// Updat ismaster view
self.s.ismaster = r.result;
// Set server response time
self.s.isMasterLatencyMS = latencyMS;
} else {
emitSDAMEvent(self, 'serverHeartbeatFailed', {
durationMS: latencyMS,
failure: err,
connectionId: self.name
});
}
// Peforming an ismaster monitoring callback operation
if (typeof callback === 'function') {
return callback(err, r);
}
// Perform another sweep
self.s.inquireServerStateTimeout = setTimeout(inquireServerState(self), self.s.haInterval);
});
};
};
//
// Clone the options
var cloneOptions = function(options) {
var opts = {};
for (var name in options) {
opts[name] = options[name];
}
return opts;
};
function Interval(fn, time) {
var timer = false;
this.start = function() {
if (!this.isRunning()) {
timer = setInterval(fn, time);
}
return this;
};
this.stop = function() {
clearInterval(timer);
timer = false;
return this;
};
this.isRunning = function() {
return timer !== false;
};
}
function Timeout(fn, time) {
var timer = false;
this.start = function() {
if (!this.isRunning()) {
timer = setTimeout(fn, time);
}
return this;
};
this.stop = function() {
clearTimeout(timer);
timer = false;
return this;
};
this.isRunning = function() {
if (timer && timer._called) return false;
return timer !== false;
};
}
function diff(previous, current) {
// Difference document
var diff = {
servers: []
};
// Previous entry
if (!previous) {
previous = { servers: [] };
}
// Check if we have any previous servers missing in the current ones
for (var i = 0; i < previous.servers.length; i++) {
var found = false;
for (var j = 0; j < current.servers.length; j++) {
if (current.servers[j].address.toLowerCase() === previous.servers[i].address.toLowerCase()) {
found = true;
break;
}
}
if (!found) {
// Add to the diff
diff.servers.push({
address: previous.servers[i].address,
from: previous.servers[i].type,
to: 'Unknown'
});
}
}
// Check if there are any severs that don't exist
for (j = 0; j < current.servers.length; j++) {
found = false;
// Go over all the previous servers
for (i = 0; i < previous.servers.length; i++) {
if (previous.servers[i].address.toLowerCase() === current.servers[j].address.toLowerCase()) {
found = true;
break;
}
}
// Add the server to the diff
if (!found) {
diff.servers.push({
address: current.servers[j].address,
from: 'Unknown',
to: current.servers[j].type
});
}
}
// Got through all the servers
for (i = 0; i < previous.servers.length; i++) {
var prevServer = previous.servers[i];
// Go through all current servers
for (j = 0; j < current.servers.length; j++) {
var currServer = current.servers[j];
// Matching server
if (prevServer.address.toLowerCase() === currServer.address.toLowerCase()) {
// We had a change in state
if (prevServer.type !== currServer.type) {
diff.servers.push({
address: prevServer.address,
from: prevServer.type,
to: currServer.type
});
}
}
}
}
// Return difference
return diff;
}
/**
* Shared function to determine clusterTime for a given topology
*
* @param {*} topology
* @param {*} clusterTime
*/
function resolveClusterTime(topology, $clusterTime) {
if (topology.clusterTime == null) {
topology.clusterTime = $clusterTime;
} else {
if ($clusterTime.clusterTime.greaterThan(topology.clusterTime.clusterTime)) {
topology.clusterTime = $clusterTime;
}
}
}
// NOTE: this is a temporary move until the topologies can be more formally refactored
// to share code.
const SessionMixins = {
endSessions: function(sessions, callback) {
if (!Array.isArray(sessions)) {
sessions = [sessions];
}
// TODO:
// When connected to a sharded cluster the endSessions command
// can be sent to any mongos. When connected to a replica set the
// endSessions command MUST be sent to the primary if the primary
// is available, otherwise it MUST be sent to any available secondary.
// Is it enough to use: ReadPreference.primaryPreferred ?
this.command(
'admin.$cmd',
{ endSessions: sessions },
{ readPreference: ReadPreference.primaryPreferred },
() => {
// intentionally ignored, per spec
if (typeof callback === 'function') callback();
}
);
}
};
function topologyType(topology) {
if (topology.description) {
return topology.description.type;
}
if (topology.type === 'mongos') {
return TopologyType.Sharded;
} else if (topology.type === 'replset') {
return TopologyType.ReplicaSetWithPrimary;
}
return TopologyType.Single;
}
const RETRYABLE_WIRE_VERSION = 6;
/**
* Determines whether the provided topology supports retryable writes
*
* @param {Mongos|Replset} topology
*/
const isRetryableWritesSupported = function(topology) {
const maxWireVersion = topology.lastIsMaster().maxWireVersion;
if (maxWireVersion < RETRYABLE_WIRE_VERSION) {
return false;
}
if (!topology.logicalSessionTimeoutMinutes) {
return false;
}
if (topologyType(topology) === TopologyType.Single) {
return false;
}
return true;
};
const MMAPv1_RETRY_WRITES_ERROR_MESSAGE =
'This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.';
function getMMAPError(err) {
if (err.code !== MMAPv1_RETRY_WRITES_ERROR_CODE || !err.errmsg.includes('Transaction numbers')) {
return err;
}
// According to the retryable writes spec, we must replace the error message in this case.
// We need to replace err.message so the thrown message is correct and we need to replace err.errmsg to meet the spec requirement.
const newErr = new MongoError({
message: MMAPv1_RETRY_WRITES_ERROR_MESSAGE,
errmsg: MMAPv1_RETRY_WRITES_ERROR_MESSAGE,
originalError: err
});
return newErr;
}
module.exports.SessionMixins = SessionMixins;
module.exports.resolveClusterTime = resolveClusterTime;
module.exports.inquireServerState = inquireServerState;
module.exports.getTopologyType = getTopologyType;
module.exports.emitServerDescriptionChanged = emitServerDescriptionChanged;
module.exports.emitTopologyDescriptionChanged = emitTopologyDescriptionChanged;
module.exports.cloneOptions = cloneOptions;
module.exports.createClientInfo = createClientInfo;
module.exports.createCompressionInfo = createCompressionInfo;
module.exports.clone = clone;
module.exports.diff = diff;
module.exports.Interval = Interval;
module.exports.Timeout = Timeout;
module.exports.isRetryableWritesSupported = isRetryableWritesSupported;
module.exports.getMMAPError = getMMAPError;
+168
View File
@@ -0,0 +1,168 @@
'use strict';
const MongoError = require('./error').MongoError;
let TxnState;
let stateMachine;
(() => {
const NO_TRANSACTION = 'NO_TRANSACTION';
const STARTING_TRANSACTION = 'STARTING_TRANSACTION';
const TRANSACTION_IN_PROGRESS = 'TRANSACTION_IN_PROGRESS';
const TRANSACTION_COMMITTED = 'TRANSACTION_COMMITTED';
const TRANSACTION_COMMITTED_EMPTY = 'TRANSACTION_COMMITTED_EMPTY';
const TRANSACTION_ABORTED = 'TRANSACTION_ABORTED';
TxnState = {
NO_TRANSACTION,
STARTING_TRANSACTION,
TRANSACTION_IN_PROGRESS,
TRANSACTION_COMMITTED,
TRANSACTION_COMMITTED_EMPTY,
TRANSACTION_ABORTED
};
stateMachine = {
[NO_TRANSACTION]: [NO_TRANSACTION, STARTING_TRANSACTION],
[STARTING_TRANSACTION]: [
TRANSACTION_IN_PROGRESS,
TRANSACTION_COMMITTED,
TRANSACTION_COMMITTED_EMPTY,
TRANSACTION_ABORTED
],
[TRANSACTION_IN_PROGRESS]: [
TRANSACTION_IN_PROGRESS,
TRANSACTION_COMMITTED,
TRANSACTION_ABORTED
],
[TRANSACTION_COMMITTED]: [
TRANSACTION_COMMITTED,
TRANSACTION_COMMITTED_EMPTY,
STARTING_TRANSACTION,
NO_TRANSACTION
],
[TRANSACTION_ABORTED]: [STARTING_TRANSACTION, NO_TRANSACTION],
[TRANSACTION_COMMITTED_EMPTY]: [TRANSACTION_COMMITTED_EMPTY, NO_TRANSACTION]
};
})();
/**
* The MongoDB ReadConcern, which allows for control of the consistency and isolation properties
* of the data read from replica sets and replica set shards.
* @typedef {Object} ReadConcern
* @property {'local'|'available'|'majority'|'linearizable'|'snapshot'} level The readConcern Level
* @see https://docs.mongodb.com/manual/reference/read-concern/
*/
/**
* A MongoDB WriteConcern, which describes the level of acknowledgement
* requested from MongoDB for write operations.
* @typedef {Object} WriteConcern
* @property {number|'majority'|string} [w=1] requests acknowledgement that the write operation has
* propagated to a specified number of mongod hosts
* @property {boolean} [j=false] requests acknowledgement from MongoDB that the write operation has
* been written to the journal
* @property {number} [wtimeout] a time limit, in milliseconds, for the write concern
* @see https://docs.mongodb.com/manual/reference/write-concern/
*/
/**
* Configuration options for a transaction.
* @typedef {Object} TransactionOptions
* @property {ReadConcern} [readConcern] A default read concern for commands in this transaction
* @property {WriteConcern} [writeConcern] A default writeConcern for commands in this transaction
* @property {ReadPreference} [readPreference] A default read preference for commands in this transaction
*/
/**
* A class maintaining state related to a server transaction. Internal Only
* @ignore
*/
class Transaction {
/**
* Create a transaction
*
* @ignore
* @param {TransactionOptions} [options] Optional settings
*/
constructor(options) {
options = options || {};
this.state = TxnState.NO_TRANSACTION;
this.options = {};
if (options.writeConcern || typeof options.w !== 'undefined') {
const w = options.writeConcern ? options.writeConcern.w : options.w;
if (w <= 0) {
throw new MongoError('Transactions do not support unacknowledged write concern');
}
this.options.writeConcern = options.writeConcern ? options.writeConcern : { w: options.w };
}
if (options.readConcern) this.options.readConcern = options.readConcern;
if (options.readPreference) this.options.readPreference = options.readPreference;
if (options.maxCommitTimeMS) this.options.maxTimeMS = options.maxCommitTimeMS;
// TODO: This isn't technically necessary
this._pinnedServer = undefined;
this._recoveryToken = undefined;
}
get server() {
return this._pinnedServer;
}
get recoveryToken() {
return this._recoveryToken;
}
get isPinned() {
return !!this.server;
}
/**
* @ignore
* @return Whether this session is presently in a transaction
*/
get isActive() {
return (
[TxnState.STARTING_TRANSACTION, TxnState.TRANSACTION_IN_PROGRESS].indexOf(this.state) !== -1
);
}
/**
* Transition the transaction in the state machine
* @ignore
* @param {TxnState} state The new state to transition to
*/
transition(nextState) {
const nextStates = stateMachine[this.state];
if (nextStates && nextStates.indexOf(nextState) !== -1) {
this.state = nextState;
if (this.state === TxnState.NO_TRANSACTION || this.state === TxnState.STARTING_TRANSACTION) {
this.unpinServer();
}
return;
}
throw new MongoError(
`Attempted illegal state transition from [${this.state}] to [${nextState}]`
);
}
pinServer(server) {
if (this.isActive) {
this._pinnedServer = server;
}
}
unpinServer() {
this._pinnedServer = undefined;
}
}
function isTransactionCommand(command) {
return !!(command.commitTransaction || command.abortTransaction);
}
module.exports = { TxnState, Transaction, isTransactionCommand };
+631
View File
@@ -0,0 +1,631 @@
'use strict';
const URL = require('url');
const qs = require('querystring');
const dns = require('dns');
const MongoParseError = require('./error').MongoParseError;
const ReadPreference = require('./topologies/read_preference');
/**
* The following regular expression validates a connection string and breaks the
* provide string into the following capture groups: [protocol, username, password, hosts]
*/
const HOSTS_RX = /(mongodb(?:\+srv|)):\/\/(?: (?:[^:]*) (?: : ([^@]*) )? @ )?([^/?]*)(?:\/|)(.*)/;
/**
* Determines whether a provided address matches the provided parent domain in order
* to avoid certain attack vectors.
*
* @param {String} srvAddress The address to check against a domain
* @param {String} parentDomain The domain to check the provided address against
* @return {Boolean} Whether the provided address matches the parent domain
*/
function matchesParentDomain(srvAddress, parentDomain) {
const regex = /^.*?\./;
const srv = `.${srvAddress.replace(regex, '')}`;
const parent = `.${parentDomain.replace(regex, '')}`;
return srv.endsWith(parent);
}
/**
* Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal
* connection string.
*
* @param {string} uri The connection string to parse
* @param {object} options Optional user provided connection string options
* @param {function} callback
*/
function parseSrvConnectionString(uri, options, callback) {
const result = URL.parse(uri, true);
if (result.hostname.split('.').length < 3) {
return callback(new MongoParseError('URI does not have hostname, domain name and tld'));
}
result.domainLength = result.hostname.split('.').length;
if (result.pathname && result.pathname.match(',')) {
return callback(new MongoParseError('Invalid URI, cannot contain multiple hostnames'));
}
if (result.port) {
return callback(new MongoParseError(`Ports not accepted with '${PROTOCOL_MONGODB_SRV}' URIs`));
}
// Resolve the SRV record and use the result as the list of hosts to connect to.
const lookupAddress = result.host;
dns.resolveSrv(`_mongodb._tcp.${lookupAddress}`, (err, addresses) => {
if (err) return callback(err);
if (addresses.length === 0) {
return callback(new MongoParseError('No addresses found at host'));
}
for (let i = 0; i < addresses.length; i++) {
if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) {
return callback(
new MongoParseError('Server record does not share hostname with parent URI')
);
}
}
// Convert the original URL to a non-SRV URL.
result.protocol = 'mongodb';
result.host = addresses.map(address => `${address.name}:${address.port}`).join(',');
// Default to SSL true if it's not specified.
if (
!('ssl' in options) &&
(!result.search || !('ssl' in result.query) || result.query.ssl === null)
) {
result.query.ssl = true;
}
// Resolve TXT record and add options from there if they exist.
dns.resolveTxt(lookupAddress, (err, record) => {
if (err) {
if (err.code !== 'ENODATA') {
return callback(err);
}
record = null;
}
if (record) {
if (record.length > 1) {
return callback(new MongoParseError('Multiple text records not allowed'));
}
record = qs.parse(record[0].join(''));
if (Object.keys(record).some(key => key !== 'authSource' && key !== 'replicaSet')) {
return callback(
new MongoParseError('Text record must only set `authSource` or `replicaSet`')
);
}
Object.assign(result.query, record);
}
// Set completed options back into the URL object.
result.search = qs.stringify(result.query);
const finalString = URL.format(result);
parseConnectionString(finalString, options, (err, ret) => {
if (err) {
callback(err);
return;
}
callback(null, Object.assign({}, ret, { srvHost: lookupAddress }));
});
});
});
}
/**
* Parses a query string item according to the connection string spec
*
* @param {string} key The key for the parsed value
* @param {Array|String} value The value to parse
* @return {Array|Object|String} The parsed value
*/
function parseQueryStringItemValue(key, value) {
if (Array.isArray(value)) {
// deduplicate and simplify arrays
value = value.filter((v, idx) => value.indexOf(v) === idx);
if (value.length === 1) value = value[0];
} else if (value.indexOf(':') > 0) {
value = value.split(',').reduce((result, pair) => {
const parts = pair.split(':');
result[parts[0]] = parseQueryStringItemValue(key, parts[1]);
return result;
}, {});
} else if (value.indexOf(',') > 0) {
value = value.split(',').map(v => {
return parseQueryStringItemValue(key, v);
});
} else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
value = value.toLowerCase() === 'true';
} else if (!Number.isNaN(value) && !STRING_OPTIONS.has(key)) {
const numericValue = parseFloat(value);
if (!Number.isNaN(numericValue)) {
value = parseFloat(value);
}
}
return value;
}
// Options that are known boolean types
const BOOLEAN_OPTIONS = new Set([
'slaveok',
'slave_ok',
'sslvalidate',
'fsync',
'safe',
'retrywrites',
'j'
]);
// Known string options, only used to bypass Number coercion in `parseQueryStringItemValue`
const STRING_OPTIONS = new Set(['authsource', 'replicaset']);
// Supported text representations of auth mechanisms
// NOTE: this list exists in native already, if it is merged here we should deduplicate
const AUTH_MECHANISMS = new Set([
'GSSAPI',
'MONGODB-X509',
'MONGODB-CR',
'DEFAULT',
'SCRAM-SHA-1',
'SCRAM-SHA-256',
'PLAIN'
]);
// Lookup table used to translate normalized (lower-cased) forms of connection string
// options to their expected camelCase version
const CASE_TRANSLATION = {
replicaset: 'replicaSet',
connecttimeoutms: 'connectTimeoutMS',
sockettimeoutms: 'socketTimeoutMS',
maxpoolsize: 'maxPoolSize',
minpoolsize: 'minPoolSize',
maxidletimems: 'maxIdleTimeMS',
waitqueuemultiple: 'waitQueueMultiple',
waitqueuetimeoutms: 'waitQueueTimeoutMS',
wtimeoutms: 'wtimeoutMS',
readconcern: 'readConcern',
readconcernlevel: 'readConcernLevel',
readpreference: 'readPreference',
maxstalenessseconds: 'maxStalenessSeconds',
readpreferencetags: 'readPreferenceTags',
authsource: 'authSource',
authmechanism: 'authMechanism',
authmechanismproperties: 'authMechanismProperties',
gssapiservicename: 'gssapiServiceName',
localthresholdms: 'localThresholdMS',
serverselectiontimeoutms: 'serverSelectionTimeoutMS',
serverselectiontryonce: 'serverSelectionTryOnce',
heartbeatfrequencyms: 'heartbeatFrequencyMS',
retrywrites: 'retryWrites',
uuidrepresentation: 'uuidRepresentation',
zlibcompressionlevel: 'zlibCompressionLevel',
tlsallowinvalidcertificates: 'tlsAllowInvalidCertificates',
tlsallowinvalidhostnames: 'tlsAllowInvalidHostnames',
tlsinsecure: 'tlsInsecure',
tlscafile: 'tlsCAFile',
tlscertificatekeyfile: 'tlsCertificateKeyFile',
tlscertificatekeyfilepassword: 'tlsCertificateKeyFilePassword',
wtimeout: 'wTimeoutMS',
j: 'journal'
};
/**
* Sets the value for `key`, allowing for any required translation
*
* @param {object} obj The object to set the key on
* @param {string} key The key to set the value for
* @param {*} value The value to set
* @param {object} options The options used for option parsing
*/
function applyConnectionStringOption(obj, key, value, options) {
// simple key translation
if (key === 'journal') {
key = 'j';
} else if (key === 'wtimeoutms') {
key = 'wtimeout';
}
// more complicated translation
if (BOOLEAN_OPTIONS.has(key)) {
value = value === 'true' || value === true;
} else if (key === 'appname') {
value = decodeURIComponent(value);
} else if (key === 'readconcernlevel') {
obj['readConcernLevel'] = value;
key = 'readconcern';
value = { level: value };
}
// simple validation
if (key === 'compressors') {
value = Array.isArray(value) ? value : [value];
if (!value.every(c => c === 'snappy' || c === 'zlib')) {
throw new MongoParseError(
'Value for `compressors` must be at least one of: `snappy`, `zlib`'
);
}
}
if (key === 'authmechanism' && !AUTH_MECHANISMS.has(value)) {
throw new MongoParseError(
'Value for `authMechanism` must be one of: `DEFAULT`, `GSSAPI`, `PLAIN`, `MONGODB-X509`, `SCRAM-SHA-1`, `SCRAM-SHA-256`'
);
}
if (key === 'readpreference' && !ReadPreference.isValid(value)) {
throw new MongoParseError(
'Value for `readPreference` must be one of: `primary`, `primaryPreferred`, `secondary`, `secondaryPreferred`, `nearest`'
);
}
if (key === 'zlibcompressionlevel' && (value < -1 || value > 9)) {
throw new MongoParseError('zlibCompressionLevel must be an integer between -1 and 9');
}
// special cases
if (key === 'compressors' || key === 'zlibcompressionlevel') {
obj.compression = obj.compression || {};
obj = obj.compression;
}
if (key === 'authmechanismproperties') {
if (typeof value.SERVICE_NAME === 'string') obj.gssapiServiceName = value.SERVICE_NAME;
if (typeof value.SERVICE_REALM === 'string') obj.gssapiServiceRealm = value.SERVICE_REALM;
if (typeof value.CANONICALIZE_HOST_NAME !== 'undefined') {
obj.gssapiCanonicalizeHostName = value.CANONICALIZE_HOST_NAME;
}
}
if (key === 'readpreferencetags' && Array.isArray(value)) {
value = splitArrayOfMultipleReadPreferenceTags(value);
}
// set the actual value
if (options.caseTranslate && CASE_TRANSLATION[key]) {
obj[CASE_TRANSLATION[key]] = value;
return;
}
obj[key] = value;
}
const USERNAME_REQUIRED_MECHANISMS = new Set([
'GSSAPI',
'MONGODB-CR',
'PLAIN',
'SCRAM-SHA-1',
'SCRAM-SHA-256'
]);
function splitArrayOfMultipleReadPreferenceTags(value) {
const parsedTags = [];
for (let i = 0; i < value.length; i++) {
parsedTags[i] = {};
value[i].split(',').forEach(individualTag => {
const splitTag = individualTag.split(':');
parsedTags[i][splitTag[0]] = splitTag[1];
});
}
return parsedTags;
}
/**
* Modifies the parsed connection string object taking into account expectations we
* have for authentication-related options.
*
* @param {object} parsed The parsed connection string result
* @return The parsed connection string result possibly modified for auth expectations
*/
function applyAuthExpectations(parsed) {
if (parsed.options == null) {
return;
}
const options = parsed.options;
const authSource = options.authsource || options.authSource;
if (authSource != null) {
parsed.auth = Object.assign({}, parsed.auth, { db: authSource });
}
const authMechanism = options.authmechanism || options.authMechanism;
if (authMechanism != null) {
if (
USERNAME_REQUIRED_MECHANISMS.has(authMechanism) &&
(!parsed.auth || parsed.auth.username == null)
) {
throw new MongoParseError(`Username required for mechanism \`${authMechanism}\``);
}
if (authMechanism === 'GSSAPI') {
if (authSource != null && authSource !== '$external') {
throw new MongoParseError(
`Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
);
}
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
}
if (authMechanism === 'MONGODB-X509') {
if (parsed.auth && parsed.auth.password != null) {
throw new MongoParseError(`Password not allowed for mechanism \`${authMechanism}\``);
}
if (authSource != null && authSource !== '$external') {
throw new MongoParseError(
`Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
);
}
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
}
if (authMechanism === 'PLAIN') {
if (parsed.auth && parsed.auth.db == null) {
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
}
}
}
// default to `admin` if nothing else was resolved
if (parsed.auth && parsed.auth.db == null) {
parsed.auth = Object.assign({}, parsed.auth, { db: 'admin' });
}
return parsed;
}
/**
* Parses a query string according the connection string spec.
*
* @param {String} query The query string to parse
* @param {object} [options] The options used for options parsing
* @return {Object|Error} The parsed query string as an object, or an error if one was encountered
*/
function parseQueryString(query, options) {
const result = {};
let parsedQueryString = qs.parse(query);
checkTLSOptions(parsedQueryString);
for (const key in parsedQueryString) {
const value = parsedQueryString[key];
if (value === '' || value == null) {
throw new MongoParseError('Incomplete key value pair for option');
}
const normalizedKey = key.toLowerCase();
const parsedValue = parseQueryStringItemValue(normalizedKey, value);
applyConnectionStringOption(result, normalizedKey, parsedValue, options);
}
// special cases for known deprecated options
if (result.wtimeout && result.wtimeoutms) {
delete result.wtimeout;
console.warn('Unsupported option `wtimeout` specified');
}
return Object.keys(result).length ? result : null;
}
/**
* Checks a query string for invalid tls options according to the URI options spec.
*
* @param {string} queryString The query string to check
* @throws {MongoParseError}
*/
function checkTLSOptions(queryString) {
const queryStringKeys = Object.keys(queryString);
if (
queryStringKeys.indexOf('tlsInsecure') !== -1 &&
(queryStringKeys.indexOf('tlsAllowInvalidCertificates') !== -1 ||
queryStringKeys.indexOf('tlsAllowInvalidHostnames') !== -1)
) {
throw new MongoParseError(
'The `tlsInsecure` option cannot be used with `tlsAllowInvalidCertificates` or `tlsAllowInvalidHostnames`.'
);
}
const tlsValue = assertTlsOptionsAreEqual('tls', queryString, queryStringKeys);
const sslValue = assertTlsOptionsAreEqual('ssl', queryString, queryStringKeys);
if (tlsValue != null && sslValue != null) {
if (tlsValue !== sslValue) {
throw new MongoParseError('All values of `tls` and `ssl` must be the same.');
}
}
}
/**
* Checks a query string to ensure all tls/ssl options are the same.
*
* @param {string} key The key (tls or ssl) to check
* @param {string} queryString The query string to check
* @throws {MongoParseError}
* @return The value of the tls/ssl option
*/
function assertTlsOptionsAreEqual(optionName, queryString, queryStringKeys) {
const queryStringHasTLSOption = queryStringKeys.indexOf(optionName) !== -1;
let optionValue;
if (Array.isArray(queryString[optionName])) {
optionValue = queryString[optionName][0];
} else {
optionValue = queryString[optionName];
}
if (queryStringHasTLSOption) {
if (Array.isArray(queryString[optionName])) {
const firstValue = queryString[optionName][0];
queryString[optionName].forEach(tlsValue => {
if (tlsValue !== firstValue) {
throw new MongoParseError('All values of ${optionName} must be the same.');
}
});
}
}
return optionValue;
}
const PROTOCOL_MONGODB = 'mongodb';
const PROTOCOL_MONGODB_SRV = 'mongodb+srv';
const SUPPORTED_PROTOCOLS = [PROTOCOL_MONGODB, PROTOCOL_MONGODB_SRV];
/**
* Parses a MongoDB connection string
*
* @param {*} uri the MongoDB connection string to parse
* @param {object} [options] Optional settings.
* @param {boolean} [options.caseTranslate] Whether the parser should translate options back into camelCase after normalization
* @param {parseCallback} callback
*/
function parseConnectionString(uri, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = Object.assign({}, { caseTranslate: true }, options);
// Check for bad uris before we parse
try {
URL.parse(uri);
} catch (e) {
return callback(new MongoParseError('URI malformed, cannot be parsed'));
}
const cap = uri.match(HOSTS_RX);
if (!cap) {
return callback(new MongoParseError('Invalid connection string'));
}
const protocol = cap[1];
if (SUPPORTED_PROTOCOLS.indexOf(protocol) === -1) {
return callback(new MongoParseError('Invalid protocol provided'));
}
if (protocol === PROTOCOL_MONGODB_SRV) {
return parseSrvConnectionString(uri, options, callback);
}
const dbAndQuery = cap[4].split('?');
const db = dbAndQuery.length > 0 ? dbAndQuery[0] : null;
const query = dbAndQuery.length > 1 ? dbAndQuery[1] : null;
let parsedOptions;
try {
parsedOptions = parseQueryString(query, options);
} catch (parseError) {
return callback(parseError);
}
parsedOptions = Object.assign({}, parsedOptions, options);
const auth = { username: null, password: null, db: db && db !== '' ? qs.unescape(db) : null };
if (parsedOptions.auth) {
// maintain support for legacy options passed into `MongoClient`
if (parsedOptions.auth.username) auth.username = parsedOptions.auth.username;
if (parsedOptions.auth.user) auth.username = parsedOptions.auth.user;
if (parsedOptions.auth.password) auth.password = parsedOptions.auth.password;
}
if (cap[4].split('?')[0].indexOf('@') !== -1) {
return callback(new MongoParseError('Unescaped slash in userinfo section'));
}
const authorityParts = cap[3].split('@');
if (authorityParts.length > 2) {
return callback(new MongoParseError('Unescaped at-sign in authority section'));
}
if (authorityParts.length > 1) {
const authParts = authorityParts.shift().split(':');
if (authParts.length > 2) {
return callback(new MongoParseError('Unescaped colon in authority section'));
}
auth.username = qs.unescape(authParts[0]);
auth.password = authParts[1] ? qs.unescape(authParts[1]) : null;
}
let hostParsingError = null;
const hosts = authorityParts
.shift()
.split(',')
.map(host => {
let parsedHost = URL.parse(`mongodb://${host}`);
if (parsedHost.path === '/:') {
hostParsingError = new MongoParseError('Double colon in host identifier');
return null;
}
// heuristically determine if we're working with a domain socket
if (host.match(/\.sock/)) {
parsedHost.hostname = qs.unescape(host);
parsedHost.port = null;
}
if (Number.isNaN(parsedHost.port)) {
hostParsingError = new MongoParseError('Invalid port (non-numeric string)');
return;
}
const result = {
host: parsedHost.hostname,
port: parsedHost.port ? parseInt(parsedHost.port) : 27017
};
if (result.port === 0) {
hostParsingError = new MongoParseError('Invalid port (zero) with hostname');
return;
}
if (result.port > 65535) {
hostParsingError = new MongoParseError('Invalid port (larger than 65535) with hostname');
return;
}
if (result.port < 0) {
hostParsingError = new MongoParseError('Invalid port (negative number)');
return;
}
return result;
})
.filter(host => !!host);
if (hostParsingError) {
return callback(hostParsingError);
}
if (hosts.length === 0 || hosts[0].host === '' || hosts[0].host === null) {
return callback(new MongoParseError('No hostname or hostnames provided in connection string'));
}
const result = {
hosts: hosts,
auth: auth.db || auth.username ? auth : null,
options: Object.keys(parsedOptions).length ? parsedOptions : null
};
if (result.auth && result.auth.db) {
result.defaultDatabase = result.auth.db;
}
try {
applyAuthExpectations(result);
} catch (authError) {
return callback(authError);
}
callback(null, result);
}
module.exports = parseConnectionString;
+172
View File
@@ -0,0 +1,172 @@
'use strict';
const crypto = require('crypto');
const requireOptional = require('require_optional');
/**
* Generate a UUIDv4
*/
const uuidV4 = () => {
const result = crypto.randomBytes(16);
result[6] = (result[6] & 0x0f) | 0x40;
result[8] = (result[8] & 0x3f) | 0x80;
return result;
};
/**
* Returns the duration calculated from two high resolution timers in milliseconds
*
* @param {Object} started A high resolution timestamp created from `process.hrtime()`
* @returns {Number} The duration in milliseconds
*/
const calculateDurationInMs = started => {
const hrtime = process.hrtime(started);
return (hrtime[0] * 1e9 + hrtime[1]) / 1e6;
};
/**
* Relays events for a given listener and emitter
*
* @param {EventEmitter} listener the EventEmitter to listen to the events from
* @param {EventEmitter} emitter the EventEmitter to relay the events to
*/
function relayEvents(listener, emitter, events) {
events.forEach(eventName => listener.on(eventName, event => emitter.emit(eventName, event)));
}
function retrieveKerberos() {
let kerberos;
try {
kerberos = requireOptional('kerberos');
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
throw new Error('The `kerberos` module was not found. Please install it and try again.');
}
throw err;
}
return kerberos;
}
// Throw an error if an attempt to use EJSON is made when it is not installed
const noEJSONError = function() {
throw new Error('The `mongodb-extjson` module was not found. Please install it and try again.');
};
// Facilitate loading EJSON optionally
function retrieveEJSON() {
let EJSON = null;
try {
EJSON = requireOptional('mongodb-extjson');
} catch (error) {} // eslint-disable-line
if (!EJSON) {
EJSON = {
parse: noEJSONError,
deserialize: noEJSONError,
serialize: noEJSONError,
stringify: noEJSONError,
setBSONModule: noEJSONError,
BSON: noEJSONError
};
}
return EJSON;
}
/**
* A helper function for determining `maxWireVersion` between legacy and new topology
* instances
*
* @private
* @param {(Topology|Server)} topologyOrServer
*/
function maxWireVersion(topologyOrServer) {
if (topologyOrServer.ismaster) {
return topologyOrServer.ismaster.maxWireVersion;
}
if (typeof topologyOrServer.lastIsMaster === 'function') {
const lastIsMaster = topologyOrServer.lastIsMaster();
if (lastIsMaster) {
return lastIsMaster.maxWireVersion;
}
}
if (topologyOrServer.description) {
return topologyOrServer.description.maxWireVersion;
}
return null;
}
/*
* Checks that collation is supported by server.
*
* @param {Server} [server] to check against
* @param {object} [cmd] object where collation may be specified
* @param {function} [callback] callback function
* @return true if server does not support collation
*/
function collationNotSupported(server, cmd) {
return cmd && cmd.collation && maxWireVersion(server) < 5;
}
/**
* Checks if a given value is a Promise
*
* @param {*} maybePromise
* @return true if the provided value is a Promise
*/
function isPromiseLike(maybePromise) {
return maybePromise && typeof maybePromise.then === 'function';
}
/**
* Applies the function `eachFn` to each item in `arr`, in parallel.
*
* @param {array} arr an array of items to asynchronusly iterate over
* @param {function} eachFn A function to call on each item of the array. The callback signature is `(item, callback)`, where the callback indicates iteration is complete.
* @param {function} callback The callback called after every item has been iterated
*/
function eachAsync(arr, eachFn, callback) {
if (arr.length === 0) {
callback(null);
return;
}
const length = arr.length;
let completed = 0;
function eachCallback(err) {
if (err) {
callback(err, null);
return;
}
if (++completed === length) {
callback(null);
}
}
for (let idx = 0; idx < length; ++idx) {
eachFn(arr[idx], eachCallback);
}
}
function isUnifiedTopology(topology) {
return topology.description != null;
}
module.exports = {
uuidV4,
calculateDurationInMs,
relayEvents,
collationNotSupported,
retrieveEJSON,
retrieveKerberos,
maxWireVersion,
isPromiseLike,
eachAsync,
isUnifiedTopology
};
+170
View File
@@ -0,0 +1,170 @@
'use strict';
const Query = require('../connection/commands').Query;
const Msg = require('../connection/msg').Msg;
const MongoError = require('../error').MongoError;
const getReadPreference = require('./shared').getReadPreference;
const isSharded = require('./shared').isSharded;
const databaseNamespace = require('./shared').databaseNamespace;
const isTransactionCommand = require('../transactions').isTransactionCommand;
const applySession = require('../sessions').applySession;
function isClientEncryptionEnabled(server) {
return server.autoEncrypter;
}
function command(server, ns, cmd, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
if (cmd == null) {
return callback(new MongoError(`command ${JSON.stringify(cmd)} does not return a cursor`));
}
if (!isClientEncryptionEnabled(server)) {
_command(server, ns, cmd, options, callback);
return;
}
_cryptCommand(server, ns, cmd, options, callback);
}
function _command(server, ns, cmd, options, callback) {
const bson = server.s.bson;
const pool = server.s.pool;
const readPreference = getReadPreference(cmd, options);
const shouldUseOpMsg = supportsOpMsg(server);
const session = options.session;
let clusterTime = server.clusterTime;
let finalCmd = Object.assign({}, cmd);
if (hasSessionSupport(server) && session) {
if (
session.clusterTime &&
session.clusterTime.clusterTime.greaterThan(clusterTime.clusterTime)
) {
clusterTime = session.clusterTime;
}
const err = applySession(session, finalCmd, options);
if (err) {
return callback(err);
}
}
// if we have a known cluster time, gossip it
if (clusterTime) {
finalCmd.$clusterTime = clusterTime;
}
if (
isSharded(server) &&
!shouldUseOpMsg &&
readPreference &&
readPreference.preference !== 'primary'
) {
finalCmd = {
$query: finalCmd,
$readPreference: readPreference.toJSON()
};
}
const commandOptions = Object.assign(
{
command: true,
numberToSkip: 0,
numberToReturn: -1,
checkKeys: false
},
options
);
// This value is not overridable
commandOptions.slaveOk = readPreference.slaveOk();
const cmdNs = `${databaseNamespace(ns)}.$cmd`;
const message = shouldUseOpMsg
? new Msg(bson, cmdNs, finalCmd, commandOptions)
: new Query(bson, cmdNs, finalCmd, commandOptions);
const inTransaction = session && (session.inTransaction() || isTransactionCommand(finalCmd));
const commandResponseHandler = inTransaction
? function(err) {
if (
!cmd.commitTransaction &&
err &&
err instanceof MongoError &&
err.hasErrorLabel('TransientTransactionError')
) {
session.transaction.unpinServer();
}
return callback.apply(null, arguments);
}
: callback;
try {
pool.write(message, commandOptions, commandResponseHandler);
} catch (err) {
commandResponseHandler(err);
}
}
function hasSessionSupport(topology) {
if (topology == null) return false;
if (topology.description) {
return topology.description.maxWireVersion >= 6;
}
return topology.ismaster == null ? false : topology.ismaster.maxWireVersion >= 6;
}
function supportsOpMsg(topologyOrServer) {
const description = topologyOrServer.ismaster
? topologyOrServer.ismaster
: topologyOrServer.description;
if (description == null) {
return false;
}
return description.maxWireVersion >= 6 && description.__nodejs_mock_server__ == null;
}
function _cryptCommand(server, ns, cmd, options, callback) {
const shouldBypassAutoEncryption = !!server.s.options.bypassAutoEncryption;
const autoEncrypter = server.autoEncrypter;
function commandResponseHandler(err, response) {
if (err || response == null) {
callback(err, response);
return;
}
autoEncrypter.decrypt(response.result, (err, decrypted) => {
if (err) {
callback(err, null);
return;
}
response.result = decrypted;
response.message.documents = [decrypted];
callback(null, response);
});
}
if (shouldBypassAutoEncryption) {
_command(server, ns, cmd, options, commandResponseHandler);
return;
}
autoEncrypter.encrypt(ns, cmd, (err, encrypted) => {
if (err) {
callback(err, null);
return;
}
_command(server, ns, encrypted, options, commandResponseHandler);
});
}
module.exports = command;
+73
View File
@@ -0,0 +1,73 @@
'use strict';
var Snappy = require('../connection/utils').retrieveSnappy(),
zlib = require('zlib');
var compressorIDs = {
snappy: 1,
zlib: 2
};
var uncompressibleCommands = [
'ismaster',
'saslStart',
'saslContinue',
'getnonce',
'authenticate',
'createUser',
'updateUser',
'copydbSaslStart',
'copydbgetnonce',
'copydb'
];
// Facilitate compressing a message using an agreed compressor
var compress = function(self, dataToBeCompressed, callback) {
switch (self.options.agreedCompressor) {
case 'snappy':
Snappy.compress(dataToBeCompressed, callback);
break;
case 'zlib':
// Determine zlibCompressionLevel
var zlibOptions = {};
if (self.options.zlibCompressionLevel) {
zlibOptions.level = self.options.zlibCompressionLevel;
}
zlib.deflate(dataToBeCompressed, zlibOptions, callback);
break;
default:
throw new Error(
'Attempt to compress message using unknown compressor "' +
self.options.agreedCompressor +
'".'
);
}
};
// Decompress a message using the given compressor
var decompress = function(compressorID, compressedData, callback) {
if (compressorID < 0 || compressorID > compressorIDs.length) {
throw new Error(
'Server sent message compressed using an unsupported compressor. (Received compressor ID ' +
compressorID +
')'
);
}
switch (compressorID) {
case compressorIDs.snappy:
Snappy.uncompress(compressedData, callback);
break;
case compressorIDs.zlib:
zlib.inflate(compressedData, callback);
break;
default:
callback(null, compressedData);
}
};
module.exports = {
compressorIDs: compressorIDs,
uncompressibleCommands: uncompressibleCommands,
compress: compress,
decompress: decompress
};
+13
View File
@@ -0,0 +1,13 @@
'use strict';
const MIN_SUPPORTED_SERVER_VERSION = '2.6';
const MAX_SUPPORTED_SERVER_VERSION = '4.2';
const MIN_SUPPORTED_WIRE_VERSION = 2;
const MAX_SUPPORTED_WIRE_VERSION = 8;
module.exports = {
MIN_SUPPORTED_SERVER_VERSION,
MAX_SUPPORTED_SERVER_VERSION,
MIN_SUPPORTED_WIRE_VERSION,
MAX_SUPPORTED_WIRE_VERSION
};
+90
View File
@@ -0,0 +1,90 @@
'use strict';
const GetMore = require('../connection/commands').GetMore;
const retrieveBSON = require('../connection/utils').retrieveBSON;
const MongoError = require('../error').MongoError;
const MongoNetworkError = require('../error').MongoNetworkError;
const BSON = retrieveBSON();
const Long = BSON.Long;
const collectionNamespace = require('./shared').collectionNamespace;
const maxWireVersion = require('../utils').maxWireVersion;
const applyCommonQueryOptions = require('./shared').applyCommonQueryOptions;
const command = require('./command');
function getMore(server, ns, cursorState, batchSize, options, callback) {
options = options || {};
const wireVersion = maxWireVersion(server);
function queryCallback(err, result) {
if (err) return callback(err);
const response = result.message;
// If we have a timed out query or a cursor that was killed
if (response.cursorNotFound) {
return callback(new MongoNetworkError('cursor killed or timed out'), null);
}
if (wireVersion < 4) {
const cursorId =
typeof response.cursorId === 'number'
? Long.fromNumber(response.cursorId)
: response.cursorId;
cursorState.documents = response.documents;
cursorState.cursorId = cursorId;
callback(null, null, response.connection);
return;
}
// We have an error detected
if (response.documents[0].ok === 0) {
return callback(new MongoError(response.documents[0]));
}
// Ensure we have a Long valid cursor id
const cursorId =
typeof response.documents[0].cursor.id === 'number'
? Long.fromNumber(response.documents[0].cursor.id)
: response.documents[0].cursor.id;
cursorState.documents = response.documents[0].cursor.nextBatch;
cursorState.cursorId = cursorId;
callback(null, response.documents[0], response.connection);
}
if (wireVersion < 4) {
const bson = server.s.bson;
const getMoreOp = new GetMore(bson, ns, cursorState.cursorId, { numberToReturn: batchSize });
const queryOptions = applyCommonQueryOptions({}, cursorState);
server.s.pool.write(getMoreOp, queryOptions, queryCallback);
return;
}
const getMoreCmd = {
getMore: cursorState.cursorId,
collection: collectionNamespace(ns),
batchSize: Math.abs(batchSize)
};
if (cursorState.cmd.tailable && typeof cursorState.cmd.maxAwaitTimeMS === 'number') {
getMoreCmd.maxTimeMS = cursorState.cmd.maxAwaitTimeMS;
}
const commandOptions = Object.assign(
{
returnFieldSelector: null,
documentsReturnedIn: 'nextBatch'
},
options
);
if (cursorState.session) {
commandOptions.session = cursorState.session;
}
command(server, ns, getMoreCmd, commandOptions, queryCallback);
}
module.exports = getMore;
+18
View File
@@ -0,0 +1,18 @@
'use strict';
const writeCommand = require('./write_command');
module.exports = {
insert: function insert(server, ns, ops, options, callback) {
writeCommand(server, 'insert', 'documents', ns, ops, options, callback);
},
update: function update(server, ns, ops, options, callback) {
writeCommand(server, 'update', 'updates', ns, ops, options, callback);
},
remove: function remove(server, ns, ops, options, callback) {
writeCommand(server, 'delete', 'deletes', ns, ops, options, callback);
},
killCursors: require('./kill_cursors'),
getMore: require('./get_more'),
query: require('./query'),
command: require('./command')
};
+70
View File
@@ -0,0 +1,70 @@
'use strict';
const KillCursor = require('../connection/commands').KillCursor;
const MongoError = require('../error').MongoError;
const MongoNetworkError = require('../error').MongoNetworkError;
const collectionNamespace = require('./shared').collectionNamespace;
const maxWireVersion = require('../utils').maxWireVersion;
const command = require('./command');
function killCursors(server, ns, cursorState, callback) {
callback = typeof callback === 'function' ? callback : () => {};
const cursorId = cursorState.cursorId;
if (maxWireVersion(server) < 4) {
const bson = server.s.bson;
const pool = server.s.pool;
const killCursor = new KillCursor(bson, ns, [cursorId]);
const options = {
immediateRelease: true,
noResponse: true
};
if (typeof cursorState.session === 'object') {
options.session = cursorState.session;
}
if (pool && pool.isConnected()) {
try {
pool.write(killCursor, options, callback);
} catch (err) {
if (typeof callback === 'function') {
callback(err, null);
} else {
console.warn(err);
}
}
}
return;
}
const killCursorCmd = {
killCursors: collectionNamespace(ns),
cursors: [cursorId]
};
const options = {};
if (typeof cursorState.session === 'object') options.session = cursorState.session;
command(server, ns, killCursorCmd, options, (err, result) => {
if (err) {
return callback(err);
}
const response = result.message;
if (response.cursorNotFound) {
return callback(new MongoNetworkError('cursor killed or timed out'), null);
}
if (!Array.isArray(response.documents) || response.documents.length === 0) {
return callback(
new MongoError(`invalid killCursors result returned for cursor id ${cursorId}`)
);
}
callback(null, response.documents[0]);
});
}
module.exports = killCursors;
+231
View File
@@ -0,0 +1,231 @@
'use strict';
const Query = require('../connection/commands').Query;
const MongoError = require('../error').MongoError;
const getReadPreference = require('./shared').getReadPreference;
const collectionNamespace = require('./shared').collectionNamespace;
const isSharded = require('./shared').isSharded;
const maxWireVersion = require('../utils').maxWireVersion;
const applyCommonQueryOptions = require('./shared').applyCommonQueryOptions;
const command = require('./command');
function query(server, ns, cmd, cursorState, options, callback) {
options = options || {};
if (cursorState.cursorId != null) {
return callback();
}
if (cmd == null) {
return callback(new MongoError(`command ${JSON.stringify(cmd)} does not return a cursor`));
}
if (maxWireVersion(server) < 4) {
const query = prepareLegacyFindQuery(server, ns, cmd, cursorState, options);
const queryOptions = applyCommonQueryOptions({}, cursorState);
if (typeof query.documentsReturnedIn === 'string') {
queryOptions.documentsReturnedIn = query.documentsReturnedIn;
}
server.s.pool.write(query, queryOptions, callback);
return;
}
const readPreference = getReadPreference(cmd, options);
const findCmd = prepareFindCommand(server, ns, cmd, cursorState, options);
// NOTE: This actually modifies the passed in cmd, and our code _depends_ on this
// side-effect. Change this ASAP
cmd.virtual = false;
const commandOptions = Object.assign(
{
documentsReturnedIn: 'firstBatch',
numberToReturn: 1,
slaveOk: readPreference.slaveOk()
},
options
);
if (cmd.readPreference) {
commandOptions.readPreference = readPreference;
}
if (cursorState.session) {
commandOptions.session = cursorState.session;
}
command(server, ns, findCmd, commandOptions, callback);
}
function prepareFindCommand(server, ns, cmd, cursorState) {
cursorState.batchSize = cmd.batchSize || cursorState.batchSize;
let findCmd = {
find: collectionNamespace(ns)
};
if (cmd.query) {
if (cmd.query['$query']) {
findCmd.filter = cmd.query['$query'];
} else {
findCmd.filter = cmd.query;
}
}
let sortValue = cmd.sort;
if (Array.isArray(sortValue)) {
const sortObject = {};
if (sortValue.length > 0 && !Array.isArray(sortValue[0])) {
let sortDirection = sortValue[1];
if (sortDirection === 'asc') {
sortDirection = 1;
} else if (sortDirection === 'desc') {
sortDirection = -1;
}
sortObject[sortValue[0]] = sortDirection;
} else {
for (let i = 0; i < sortValue.length; i++) {
let sortDirection = sortValue[i][1];
if (sortDirection === 'asc') {
sortDirection = 1;
} else if (sortDirection === 'desc') {
sortDirection = -1;
}
sortObject[sortValue[i][0]] = sortDirection;
}
}
sortValue = sortObject;
}
if (cmd.sort) findCmd.sort = sortValue;
if (cmd.fields) findCmd.projection = cmd.fields;
if (cmd.hint) findCmd.hint = cmd.hint;
if (cmd.skip) findCmd.skip = cmd.skip;
if (cmd.limit) findCmd.limit = cmd.limit;
if (cmd.limit < 0) {
findCmd.limit = Math.abs(cmd.limit);
findCmd.singleBatch = true;
}
if (typeof cmd.batchSize === 'number') {
if (cmd.batchSize < 0) {
if (cmd.limit !== 0 && Math.abs(cmd.batchSize) < Math.abs(cmd.limit)) {
findCmd.limit = Math.abs(cmd.batchSize);
}
findCmd.singleBatch = true;
}
findCmd.batchSize = Math.abs(cmd.batchSize);
}
if (cmd.comment) findCmd.comment = cmd.comment;
if (cmd.maxScan) findCmd.maxScan = cmd.maxScan;
if (cmd.maxTimeMS) findCmd.maxTimeMS = cmd.maxTimeMS;
if (cmd.min) findCmd.min = cmd.min;
if (cmd.max) findCmd.max = cmd.max;
findCmd.returnKey = cmd.returnKey ? cmd.returnKey : false;
findCmd.showRecordId = cmd.showDiskLoc ? cmd.showDiskLoc : false;
if (cmd.snapshot) findCmd.snapshot = cmd.snapshot;
if (cmd.tailable) findCmd.tailable = cmd.tailable;
if (cmd.oplogReplay) findCmd.oplogReplay = cmd.oplogReplay;
if (cmd.noCursorTimeout) findCmd.noCursorTimeout = cmd.noCursorTimeout;
if (cmd.awaitData) findCmd.awaitData = cmd.awaitData;
if (cmd.awaitdata) findCmd.awaitData = cmd.awaitdata;
if (cmd.partial) findCmd.partial = cmd.partial;
if (cmd.collation) findCmd.collation = cmd.collation;
if (cmd.readConcern) findCmd.readConcern = cmd.readConcern;
// If we have explain, we need to rewrite the find command
// to wrap it in the explain command
if (cmd.explain) {
findCmd = {
explain: findCmd
};
}
return findCmd;
}
function prepareLegacyFindQuery(server, ns, cmd, cursorState, options) {
options = options || {};
const bson = server.s.bson;
const readPreference = getReadPreference(cmd, options);
cursorState.batchSize = cmd.batchSize || cursorState.batchSize;
let numberToReturn = 0;
if (
cursorState.limit < 0 ||
(cursorState.limit !== 0 && cursorState.limit < cursorState.batchSize) ||
(cursorState.limit > 0 && cursorState.batchSize === 0)
) {
numberToReturn = cursorState.limit;
} else {
numberToReturn = cursorState.batchSize;
}
const numberToSkip = cursorState.skip || 0;
const findCmd = {};
if (isSharded(server) && readPreference) {
findCmd['$readPreference'] = readPreference.toJSON();
}
if (cmd.sort) findCmd['$orderby'] = cmd.sort;
if (cmd.hint) findCmd['$hint'] = cmd.hint;
if (cmd.snapshot) findCmd['$snapshot'] = cmd.snapshot;
if (typeof cmd.returnKey !== 'undefined') findCmd['$returnKey'] = cmd.returnKey;
if (cmd.maxScan) findCmd['$maxScan'] = cmd.maxScan;
if (cmd.min) findCmd['$min'] = cmd.min;
if (cmd.max) findCmd['$max'] = cmd.max;
if (typeof cmd.showDiskLoc !== 'undefined') findCmd['$showDiskLoc'] = cmd.showDiskLoc;
if (cmd.comment) findCmd['$comment'] = cmd.comment;
if (cmd.maxTimeMS) findCmd['$maxTimeMS'] = cmd.maxTimeMS;
if (cmd.explain) {
// nToReturn must be 0 (match all) or negative (match N and close cursor)
// nToReturn > 0 will give explain results equivalent to limit(0)
numberToReturn = -Math.abs(cmd.limit || 0);
findCmd['$explain'] = true;
}
findCmd['$query'] = cmd.query;
if (cmd.readConcern && cmd.readConcern.level !== 'local') {
throw new MongoError(
`server find command does not support a readConcern level of ${cmd.readConcern.level}`
);
}
if (cmd.readConcern) {
cmd = Object.assign({}, cmd);
delete cmd['readConcern'];
}
const serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
const ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false;
const query = new Query(bson, ns, findCmd, {
numberToSkip: numberToSkip,
numberToReturn: numberToReturn,
pre32Limit: typeof cmd.limit !== 'undefined' ? cmd.limit : undefined,
checkKeys: false,
returnFieldSelector: cmd.fields,
serializeFunctions: serializeFunctions,
ignoreUndefined: ignoreUndefined
});
if (typeof cmd.tailable === 'boolean') query.tailable = cmd.tailable;
if (typeof cmd.oplogReplay === 'boolean') query.oplogReplay = cmd.oplogReplay;
if (typeof cmd.noCursorTimeout === 'boolean') query.noCursorTimeout = cmd.noCursorTimeout;
if (typeof cmd.awaitData === 'boolean') query.awaitData = cmd.awaitData;
if (typeof cmd.partial === 'boolean') query.partial = cmd.partial;
query.slaveOk = readPreference.slaveOk();
return query;
}
module.exports = query;
+115
View File
@@ -0,0 +1,115 @@
'use strict';
const ReadPreference = require('../topologies/read_preference');
const MongoError = require('../error').MongoError;
const ServerType = require('../sdam/server_description').ServerType;
const TopologyDescription = require('../sdam/topology_description').TopologyDescription;
const MESSAGE_HEADER_SIZE = 16;
const COMPRESSION_DETAILS_SIZE = 9; // originalOpcode + uncompressedSize, compressorID
// OPCODE Numbers
// Defined at https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#request-opcodes
var opcodes = {
OP_REPLY: 1,
OP_UPDATE: 2001,
OP_INSERT: 2002,
OP_QUERY: 2004,
OP_GETMORE: 2005,
OP_DELETE: 2006,
OP_KILL_CURSORS: 2007,
OP_COMPRESSED: 2012,
OP_MSG: 2013
};
var getReadPreference = function(cmd, options) {
// Default to command version of the readPreference
var readPreference = cmd.readPreference || new ReadPreference('primary');
// If we have an option readPreference override the command one
if (options.readPreference) {
readPreference = options.readPreference;
}
if (typeof readPreference === 'string') {
readPreference = new ReadPreference(readPreference);
}
if (!(readPreference instanceof ReadPreference)) {
throw new MongoError('read preference must be a ReadPreference instance');
}
return readPreference;
};
// Parses the header of a wire protocol message
var parseHeader = function(message) {
return {
length: message.readInt32LE(0),
requestId: message.readInt32LE(4),
responseTo: message.readInt32LE(8),
opCode: message.readInt32LE(12)
};
};
function applyCommonQueryOptions(queryOptions, options) {
Object.assign(queryOptions, {
raw: typeof options.raw === 'boolean' ? options.raw : false,
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : false,
fullResult: typeof options.fullResult === 'boolean' ? options.fullResult : false
});
if (typeof options.socketTimeout === 'number') {
queryOptions.socketTimeout = options.socketTimeout;
}
if (options.session) {
queryOptions.session = options.session;
}
if (typeof options.documentsReturnedIn === 'string') {
queryOptions.documentsReturnedIn = options.documentsReturnedIn;
}
return queryOptions;
}
function isSharded(topologyOrServer) {
if (topologyOrServer.type === 'mongos') return true;
if (topologyOrServer.description && topologyOrServer.description.type === ServerType.Mongos) {
return true;
}
// NOTE: This is incredibly inefficient, and should be removed once command construction
// happens based on `Server` not `Topology`.
if (topologyOrServer.description && topologyOrServer.description instanceof TopologyDescription) {
const servers = Array.from(topologyOrServer.description.servers.values());
return servers.some(server => server.type === ServerType.Mongos);
}
return false;
}
function databaseNamespace(ns) {
return ns.split('.')[0];
}
function collectionNamespace(ns) {
return ns
.split('.')
.slice(1)
.join('.');
}
module.exports = {
getReadPreference,
MESSAGE_HEADER_SIZE,
COMPRESSION_DETAILS_SIZE,
opcodes,
parseHeader,
applyCommonQueryOptions,
isSharded,
databaseNamespace,
collectionNamespace
};
+50
View File
@@ -0,0 +1,50 @@
'use strict';
const MongoError = require('../error').MongoError;
const collectionNamespace = require('./shared').collectionNamespace;
const command = require('./command');
function writeCommand(server, type, opsField, ns, ops, options, callback) {
if (ops.length === 0) throw new MongoError(`${type} must contain at least one document`);
if (typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
const writeConcern = options.writeConcern;
const writeCommand = {};
writeCommand[type] = collectionNamespace(ns);
writeCommand[opsField] = ops;
writeCommand.ordered = ordered;
if (writeConcern && Object.keys(writeConcern).length > 0) {
writeCommand.writeConcern = writeConcern;
}
if (options.collation) {
for (let i = 0; i < writeCommand[opsField].length; i++) {
if (!writeCommand[opsField][i].collation) {
writeCommand[opsField][i].collation = options.collation;
}
}
}
if (options.bypassDocumentValidation === true) {
writeCommand.bypassDocumentValidation = options.bypassDocumentValidation;
}
const commandOptions = Object.assign(
{
checkKeys: type === 'insert',
numberToReturn: 1
},
options
);
command(server, ns, writeCommand, commandOptions, callback);
}
module.exports = writeCommand;
+1089
View File
File diff suppressed because it is too large. Load diff
+1029
View File
File diff suppressed because it is too large. Load diff
+32
View File
@@ -0,0 +1,32 @@
'use strict';
let collection;
let cursor;
let db;
function loadCollection() {
if (!collection) {
collection = require('./collection');
}
return collection;
}
function loadCursor() {
if (!cursor) {
cursor = require('./cursor');
}
return cursor;
}
function loadDb() {
if (!db) {
db = require('./db');
}
return db;
}
module.exports = {
loadCollection,
loadCursor,
loadDb
};
+45
View File
@@ -0,0 +1,45 @@
'use strict';
const MongoNetworkError = require('./core').MongoNetworkError;
const mongoErrorContextSymbol = require('./core').mongoErrorContextSymbol;
const GET_MORE_NON_RESUMABLE_CODES = new Set([
136, // CappedPositionLost
237, // CursorKilled
11601 // Interrupted
]);
// From spec@https://github.com/mongodb/specifications/blob/7a2e93d85935ee4b1046a8d2ad3514c657dc74fa/source/change-streams/change-streams.rst#resumable-error:
//
// An error is considered resumable if it meets any of the following criteria:
// - any error encountered which is not a server error (e.g. a timeout error or network error)
// - any server error response from a getMore command excluding those containing the error label
// NonRetryableChangeStreamError and those containing the following error codes:
// - Interrupted: 11601
// - CappedPositionLost: 136
// - CursorKilled: 237
//
// An error on an aggregate command is not a resumable error. Only errors on a getMore command may be considered resumable errors.
function isGetMoreError(error) {
if (error[mongoErrorContextSymbol]) {
return error[mongoErrorContextSymbol].isGetMore;
}
}
function isResumableError(error) {
if (!isGetMoreError(error)) {
return false;
}
if (error instanceof MongoNetworkError) {
return true;
}
return !(
GET_MORE_NON_RESUMABLE_CODES.has(error.code) ||
error.hasErrorLabel('NonRetryableChangeStreamError')
);
}
module.exports = { GET_MORE_NON_RESUMABLE_CODES, isResumableError };
+421
View File
@@ -0,0 +1,421 @@
'use strict';
var stream = require('stream'),
util = require('util');
module.exports = GridFSBucketReadStream;
/**
* A readable stream that enables you to read buffers from GridFS.
*
* Do not instantiate this class directly. Use `openDownloadStream()` instead.
*
* @class
* @param {Collection} chunks Handle for chunks collection
* @param {Collection} files Handle for files collection
* @param {Object} readPreference The read preference to use
* @param {Object} filter The query to use to find the file document
* @param {Object} [options] Optional settings.
* @param {Number} [options.sort] Optional sort for the file find query
* @param {Number} [options.skip] Optional skip for the file find query
* @param {Number} [options.start] Optional 0-based offset in bytes to start streaming from
* @param {Number} [options.end] Optional 0-based offset in bytes to stop streaming before
* @fires GridFSBucketReadStream#error
* @fires GridFSBucketReadStream#file
* @return {GridFSBucketReadStream} a GridFSBucketReadStream instance.
*/
function GridFSBucketReadStream(chunks, files, readPreference, filter, options) {
this.s = {
bytesRead: 0,
chunks: chunks,
cursor: null,
expected: 0,
files: files,
filter: filter,
init: false,
expectedEnd: 0,
file: null,
options: options,
readPreference: readPreference
};
stream.Readable.call(this);
}
util.inherits(GridFSBucketReadStream, stream.Readable);
/**
* An error occurred
*
* @event GridFSBucketReadStream#error
* @type {Error}
*/
/**
* Fires when the stream loaded the file document corresponding to the
* provided id.
*
* @event GridFSBucketReadStream#file
* @type {object}
*/
/**
* Emitted when a chunk of data is available to be consumed.
*
* @event GridFSBucketReadStream#data
* @type {object}
*/
/**
* Fired when the stream is exhausted (no more data events).
*
* @event GridFSBucketReadStream#end
* @type {object}
*/
/**
* Fired when the stream is exhausted and the underlying cursor is killed
*
* @event GridFSBucketReadStream#close
* @type {object}
*/
/**
* Reads from the cursor and pushes to the stream.
* @method
*/
GridFSBucketReadStream.prototype._read = function() {
var _this = this;
if (this.destroyed) {
return;
}
waitForFile(_this, function() {
doRead(_this);
});
};
/**
* Sets the 0-based offset in bytes to start streaming from. Throws
* an error if this stream has entered flowing mode
* (e.g. if you've already called `on('data')`)
* @method
* @param {Number} start Offset in bytes to start reading at
* @return {GridFSBucketReadStream}
*/
GridFSBucketReadStream.prototype.start = function(start) {
throwIfInitialized(this);
this.s.options.start = start;
return this;
};
/**
* Sets the 0-based offset in bytes to start streaming from. Throws
* an error if this stream has entered flowing mode
* (e.g. if you've already called `on('data')`)
* @method
* @param {Number} end Offset in bytes to stop reading at
* @return {GridFSBucketReadStream}
*/
GridFSBucketReadStream.prototype.end = function(end) {
throwIfInitialized(this);
this.s.options.end = end;
return this;
};
/**
* Marks this stream as aborted (will never push another `data` event)
* and kills the underlying cursor. Will emit the 'end' event, and then
* the 'close' event once the cursor is successfully killed.
*
* @method
* @param {GridFSBucket~errorCallback} [callback] called when the cursor is successfully closed or an error occurred.
* @fires GridFSBucketWriteStream#close
* @fires GridFSBucketWriteStream#end
*/
GridFSBucketReadStream.prototype.abort = function(callback) {
var _this = this;
this.push(null);
this.destroyed = true;
if (this.s.cursor) {
this.s.cursor.close(function(error) {
_this.emit('close');
callback && callback(error);
});
} else {
if (!this.s.init) {
// If not initialized, fire close event because we will never
// get a cursor
_this.emit('close');
}
callback && callback();
}
};
/**
* @ignore
*/
function throwIfInitialized(self) {
if (self.s.init) {
throw new Error('You cannot change options after the stream has entered' + 'flowing mode!');
}
}
/**
* @ignore
*/
function doRead(_this) {
if (_this.destroyed) {
return;
}
_this.s.cursor.next(function(error, doc) {
if (_this.destroyed) {
return;
}
if (error) {
return __handleError(_this, error);
}
if (!doc) {
_this.push(null);
process.nextTick(() => {
_this.s.cursor.close(function(error) {
if (error) {
__handleError(_this, error);
return;
}
_this.emit('close');
});
});
return;
}
var bytesRemaining = _this.s.file.length - _this.s.bytesRead;
var expectedN = _this.s.expected++;
var expectedLength = Math.min(_this.s.file.chunkSize, bytesRemaining);
if (doc.n > expectedN) {
var errmsg = 'ChunkIsMissing: Got unexpected n: ' + doc.n + ', expected: ' + expectedN;
return __handleError(_this, new Error(errmsg));
}
if (doc.n < expectedN) {
errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n + ', expected: ' + expectedN;
return __handleError(_this, new Error(errmsg));
}
var buf = Buffer.isBuffer(doc.data) ? doc.data : doc.data.buffer;
if (buf.length !== expectedLength) {
if (bytesRemaining <= 0) {
errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n;
return __handleError(_this, new Error(errmsg));
}
errmsg =
'ChunkIsWrongSize: Got unexpected length: ' + buf.length + ', expected: ' + expectedLength;
return __handleError(_this, new Error(errmsg));
}
_this.s.bytesRead += buf.length;
if (buf.length === 0) {
return _this.push(null);
}
var sliceStart = null;
var sliceEnd = null;
if (_this.s.bytesToSkip != null) {
sliceStart = _this.s.bytesToSkip;
_this.s.bytesToSkip = 0;
}
const atEndOfStream = expectedN === _this.s.expectedEnd - 1;
const bytesLeftToRead = _this.s.options.end - _this.s.bytesToSkip;
if (atEndOfStream && _this.s.bytesToTrim != null) {
sliceEnd = _this.s.file.chunkSize - _this.s.bytesToTrim;
} else if (_this.s.options.end && bytesLeftToRead < doc.data.length()) {
sliceEnd = bytesLeftToRead;
}
if (sliceStart != null || sliceEnd != null) {
buf = buf.slice(sliceStart || 0, sliceEnd || buf.length);
}
_this.push(buf);
});
}
/**
* @ignore
*/
function init(self) {
var findOneOptions = {};
if (self.s.readPreference) {
findOneOptions.readPreference = self.s.readPreference;
}
if (self.s.options && self.s.options.sort) {
findOneOptions.sort = self.s.options.sort;
}
if (self.s.options && self.s.options.skip) {
findOneOptions.skip = self.s.options.skip;
}
self.s.files.findOne(self.s.filter, findOneOptions, function(error, doc) {
if (error) {
return __handleError(self, error);
}
if (!doc) {
var identifier = self.s.filter._id ? self.s.filter._id.toString() : self.s.filter.filename;
var errmsg = 'FileNotFound: file ' + identifier + ' was not found';
var err = new Error(errmsg);
err.code = 'ENOENT';
return __handleError(self, err);
}
// If document is empty, kill the stream immediately and don't
// execute any reads
if (doc.length <= 0) {
self.push(null);
return;
}
if (self.destroyed) {
// If user destroys the stream before we have a cursor, wait
// until the query is done to say we're 'closed' because we can't
// cancel a query.
self.emit('close');
return;
}
self.s.bytesToSkip = handleStartOption(self, doc, self.s.options);
var filter = { files_id: doc._id };
// Currently (MongoDB 3.4.4) skip function does not support the index,
// it needs to retrieve all the documents first and then skip them. (CS-25811)
// As work around we use $gte on the "n" field.
if (self.s.options && self.s.options.start != null) {
var skip = Math.floor(self.s.options.start / doc.chunkSize);
if (skip > 0) {
filter['n'] = { $gte: skip };
}
}
self.s.cursor = self.s.chunks.find(filter).sort({ n: 1 });
if (self.s.readPreference) {
self.s.cursor.setReadPreference(self.s.readPreference);
}
self.s.expectedEnd = Math.ceil(doc.length / doc.chunkSize);
self.s.file = doc;
self.s.bytesToTrim = handleEndOption(self, doc, self.s.cursor, self.s.options);
self.emit('file', doc);
});
}
/**
* @ignore
*/
function waitForFile(_this, callback) {
if (_this.s.file) {
return callback();
}
if (!_this.s.init) {
init(_this);
_this.s.init = true;
}
_this.once('file', function() {
callback();
});
}
/**
* @ignore
*/
function handleStartOption(stream, doc, options) {
if (options && options.start != null) {
if (options.start > doc.length) {
throw new Error(
'Stream start (' +
options.start +
') must not be ' +
'more than the length of the file (' +
doc.length +
')'
);
}
if (options.start < 0) {
throw new Error('Stream start (' + options.start + ') must not be ' + 'negative');
}
if (options.end != null && options.end < options.start) {
throw new Error(
'Stream start (' +
options.start +
') must not be ' +
'greater than stream end (' +
options.end +
')'
);
}
stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) * doc.chunkSize;
stream.s.expected = Math.floor(options.start / doc.chunkSize);
return options.start - stream.s.bytesRead;
}
}
/**
* @ignore
*/
function handleEndOption(stream, doc, cursor, options) {
if (options && options.end != null) {
if (options.end > doc.length) {
throw new Error(
'Stream end (' +
options.end +
') must not be ' +
'more than the length of the file (' +
doc.length +
')'
);
}
if (options.start < 0) {
throw new Error('Stream end (' + options.end + ') must not be ' + 'negative');
}
var start = options.start != null ? Math.floor(options.start / doc.chunkSize) : 0;
cursor.limit(Math.ceil(options.end / doc.chunkSize) - start);
stream.s.expectedEnd = Math.ceil(options.end / doc.chunkSize);
return Math.ceil(options.end / doc.chunkSize) * doc.chunkSize - options.end;
}
}
/**
* @ignore
*/
function __handleError(_this, error) {
_this.emit('error', error);
}
+358
View File
@@ -0,0 +1,358 @@
'use strict';
var Emitter = require('events').EventEmitter;
var GridFSBucketReadStream = require('./download');
var GridFSBucketWriteStream = require('./upload');
var shallowClone = require('../utils').shallowClone;
var toError = require('../utils').toError;
var util = require('util');
var executeLegacyOperation = require('../utils').executeLegacyOperation;
var DEFAULT_GRIDFS_BUCKET_OPTIONS = {
bucketName: 'fs',
chunkSizeBytes: 255 * 1024
};
module.exports = GridFSBucket;
/**
* Constructor for a streaming GridFS interface
* @class
* @param {Db} db A db handle
* @param {object} [options] Optional settings.
* @param {string} [options.bucketName="fs"] The 'files' and 'chunks' collections will be prefixed with the bucket name followed by a dot.
* @param {number} [options.chunkSizeBytes=255 * 1024] Number of bytes stored in each chunk. Defaults to 255KB
* @param {object} [options.writeConcern] Optional write concern to be passed to write operations, for instance `{ w: 1 }`
* @param {object} [options.readPreference] Optional read preference to be passed to read operations
* @fires GridFSBucketWriteStream#index
* @return {GridFSBucket}
*/
function GridFSBucket(db, options) {
Emitter.apply(this);
this.setMaxListeners(0);
if (options && typeof options === 'object') {
options = shallowClone(options);
var keys = Object.keys(DEFAULT_GRIDFS_BUCKET_OPTIONS);
for (var i = 0; i < keys.length; ++i) {
if (!options[keys[i]]) {
options[keys[i]] = DEFAULT_GRIDFS_BUCKET_OPTIONS[keys[i]];
}
}
} else {
options = DEFAULT_GRIDFS_BUCKET_OPTIONS;
}
this.s = {
db: db,
options: options,
_chunksCollection: db.collection(options.bucketName + '.chunks'),
_filesCollection: db.collection(options.bucketName + '.files'),
checkedIndexes: false,
calledOpenUploadStream: false,
promiseLibrary: db.s.promiseLibrary || Promise
};
}
util.inherits(GridFSBucket, Emitter);
/**
* When the first call to openUploadStream is made, the upload stream will
* check to see if it needs to create the proper indexes on the chunks and
* files collections. This event is fired either when 1) it determines that
* no index creation is necessary, 2) when it successfully creates the
* necessary indexes.
*
* @event GridFSBucket#index
* @type {Error}
*/
/**
* Returns a writable stream (GridFSBucketWriteStream) for writing
* buffers to GridFS. The stream's 'id' property contains the resulting
* file's id.
* @method
* @param {string} filename The value of the 'filename' key in the files doc
* @param {object} [options] Optional settings.
* @param {number} [options.chunkSizeBytes] Optional overwrite this bucket's chunkSizeBytes for this file
* @param {object} [options.metadata] Optional object to store in the file document's `metadata` field
* @param {string} [options.contentType] Optional string to store in the file document's `contentType` field
* @param {array} [options.aliases] Optional array of strings to store in the file document's `aliases` field
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
* @return {GridFSBucketWriteStream}
*/
GridFSBucket.prototype.openUploadStream = function(filename, options) {
if (options) {
options = shallowClone(options);
} else {
options = {};
}
if (!options.chunkSizeBytes) {
options.chunkSizeBytes = this.s.options.chunkSizeBytes;
}
return new GridFSBucketWriteStream(this, filename, options);
};
/**
* Returns a writable stream (GridFSBucketWriteStream) for writing
* buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting
* file's id.
* @method
* @param {string|number|object} id A custom id used to identify the file
* @param {string} filename The value of the 'filename' key in the files doc
* @param {object} [options] Optional settings.
* @param {number} [options.chunkSizeBytes] Optional overwrite this bucket's chunkSizeBytes for this file
* @param {object} [options.metadata] Optional object to store in the file document's `metadata` field
* @param {string} [options.contentType] Optional string to store in the file document's `contentType` field
* @param {array} [options.aliases] Optional array of strings to store in the file document's `aliases` field
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
* @return {GridFSBucketWriteStream}
*/
GridFSBucket.prototype.openUploadStreamWithId = function(id, filename, options) {
if (options) {
options = shallowClone(options);
} else {
options = {};
}
if (!options.chunkSizeBytes) {
options.chunkSizeBytes = this.s.options.chunkSizeBytes;
}
options.id = id;
return new GridFSBucketWriteStream(this, filename, options);
};
/**
* Returns a readable stream (GridFSBucketReadStream) for streaming file
* data from GridFS.
* @method
* @param {ObjectId} id The id of the file doc
* @param {Object} [options] Optional settings.
* @param {Number} [options.start] Optional 0-based offset in bytes to start streaming from
* @param {Number} [options.end] Optional 0-based offset in bytes to stop streaming before
* @return {GridFSBucketReadStream}
*/
GridFSBucket.prototype.openDownloadStream = function(id, options) {
var filter = { _id: id };
options = {
start: options && options.start,
end: options && options.end
};
return new GridFSBucketReadStream(
this.s._chunksCollection,
this.s._filesCollection,
this.s.options.readPreference,
filter,
options
);
};
/**
* Deletes a file with the given id
* @method
* @param {ObjectId} id The id of the file doc
* @param {GridFSBucket~errorCallback} [callback]
*/
GridFSBucket.prototype.delete = function(id, callback) {
return executeLegacyOperation(this.s.db.s.topology, _delete, [this, id, callback], {
skipSessions: true
});
};
/**
* @ignore
*/
function _delete(_this, id, callback) {
_this.s._filesCollection.deleteOne({ _id: id }, function(error, res) {
if (error) {
return callback(error);
}
_this.s._chunksCollection.deleteMany({ files_id: id }, function(error) {
if (error) {
return callback(error);
}
// Delete orphaned chunks before returning FileNotFound
if (!res.result.n) {
var errmsg = 'FileNotFound: no file with id ' + id + ' found';
return callback(new Error(errmsg));
}
callback();
});
});
}
/**
* Convenience wrapper around find on the files collection
* @method
* @param {Object} filter
* @param {Object} [options] Optional settings for cursor
* @param {number} [options.batchSize] Optional batch size for cursor
* @param {number} [options.limit] Optional limit for cursor
* @param {number} [options.maxTimeMS] Optional maxTimeMS for cursor
* @param {boolean} [options.noCursorTimeout] Optionally set cursor's `noCursorTimeout` flag
* @param {number} [options.skip] Optional skip for cursor
* @param {object} [options.sort] Optional sort for cursor
* @return {Cursor}
*/
GridFSBucket.prototype.find = function(filter, options) {
filter = filter || {};
options = options || {};
var cursor = this.s._filesCollection.find(filter);
if (options.batchSize != null) {
cursor.batchSize(options.batchSize);
}
if (options.limit != null) {
cursor.limit(options.limit);
}
if (options.maxTimeMS != null) {
cursor.maxTimeMS(options.maxTimeMS);
}
if (options.noCursorTimeout != null) {
cursor.addCursorFlag('noCursorTimeout', options.noCursorTimeout);
}
if (options.skip != null) {
cursor.skip(options.skip);
}
if (options.sort != null) {
cursor.sort(options.sort);
}
return cursor;
};
/**
* Returns a readable stream (GridFSBucketReadStream) for streaming the
* file with the given name from GridFS. If there are multiple files with
* the same name, this will stream the most recent file with the given name
* (as determined by the `uploadDate` field). You can set the `revision`
* option to change this behavior.
* @method
* @param {String} filename The name of the file to stream
* @param {Object} [options] Optional settings
* @param {number} [options.revision=-1] The revision number relative to the oldest file with the given filename. 0 gets you the oldest file, 1 gets you the 2nd oldest, -1 gets you the newest.
* @param {Number} [options.start] Optional 0-based offset in bytes to start streaming from
* @param {Number} [options.end] Optional 0-based offset in bytes to stop streaming before
* @return {GridFSBucketReadStream}
*/
GridFSBucket.prototype.openDownloadStreamByName = function(filename, options) {
var sort = { uploadDate: -1 };
var skip = null;
if (options && options.revision != null) {
if (options.revision >= 0) {
sort = { uploadDate: 1 };
skip = options.revision;
} else {
skip = -options.revision - 1;
}
}
var filter = { filename: filename };
options = {
sort: sort,
skip: skip,
start: options && options.start,
end: options && options.end
};
return new GridFSBucketReadStream(
this.s._chunksCollection,
this.s._filesCollection,
this.s.options.readPreference,
filter,
options
);
};
/**
* Renames the file with the given _id to the given string
* @method
* @param {ObjectId} id the id of the file to rename
* @param {String} filename new name for the file
* @param {GridFSBucket~errorCallback} [callback]
*/
GridFSBucket.prototype.rename = function(id, filename, callback) {
return executeLegacyOperation(this.s.db.s.topology, _rename, [this, id, filename, callback], {
skipSessions: true
});
};
/**
* @ignore
*/
function _rename(_this, id, filename, callback) {
var filter = { _id: id };
var update = { $set: { filename: filename } };
_this.s._filesCollection.updateOne(filter, update, function(error, res) {
if (error) {
return callback(error);
}
if (!res.result.n) {
return callback(toError('File with id ' + id + ' not found'));
}
callback();
});
}
/**
* Removes this bucket's files collection, followed by its chunks collection.
* @method
* @param {GridFSBucket~errorCallback} [callback]
*/
GridFSBucket.prototype.drop = function(callback) {
return executeLegacyOperation(this.s.db.s.topology, _drop, [this, callback], {
skipSessions: true
});
};
/**
* Return the db logger
* @method
* @return {Logger} return the db logger
* @ignore
*/
GridFSBucket.prototype.getLogger = function() {
return this.s.db.s.logger;
};
/**
* @ignore
*/
function _drop(_this, callback) {
_this.s._filesCollection.drop(function(error) {
if (error) {
return callback(error);
}
_this.s._chunksCollection.drop(function(error) {
if (error) {
return callback(error);
}
return callback();
});
});
}
/**
* Callback format for all GridFSBucket methods that can accept a callback.
* @callback GridFSBucket~errorCallback
* @param {MongoError} error An error instance representing any errors that occurred
*/
+538
View File
@@ -0,0 +1,538 @@
'use strict';
var core = require('../core');
var crypto = require('crypto');
var stream = require('stream');
var util = require('util');
var Buffer = require('safe-buffer').Buffer;
var ERROR_NAMESPACE_NOT_FOUND = 26;
module.exports = GridFSBucketWriteStream;
/**
* A writable stream that enables you to write buffers to GridFS.
*
* Do not instantiate this class directly. Use `openUploadStream()` instead.
*
* @class
* @param {GridFSBucket} bucket Handle for this stream's corresponding bucket
* @param {string} filename The value of the 'filename' key in the files doc
* @param {object} [options] Optional settings.
* @param {string|number|object} [options.id] Custom file id for the GridFS file.
* @param {number} [options.chunkSizeBytes] The chunk size to use, in bytes
* @param {number} [options.w] The write concern
* @param {number} [options.wtimeout] The write concern timeout
* @param {number} [options.j] The journal write concern
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
* @fires GridFSBucketWriteStream#error
* @fires GridFSBucketWriteStream#finish
* @return {GridFSBucketWriteStream} a GridFSBucketWriteStream instance.
*/
function GridFSBucketWriteStream(bucket, filename, options) {
options = options || {};
this.bucket = bucket;
this.chunks = bucket.s._chunksCollection;
this.filename = filename;
this.files = bucket.s._filesCollection;
this.options = options;
// Signals the write is all done
this.done = false;
this.id = options.id ? options.id : core.BSON.ObjectId();
this.chunkSizeBytes = this.options.chunkSizeBytes;
this.bufToStore = Buffer.alloc(this.chunkSizeBytes);
this.length = 0;
this.md5 = !options.disableMD5 && crypto.createHash('md5');
this.n = 0;
this.pos = 0;
this.state = {
streamEnd: false,
outstandingRequests: 0,
errored: false,
aborted: false,
promiseLibrary: this.bucket.s.promiseLibrary
};
if (!this.bucket.s.calledOpenUploadStream) {
this.bucket.s.calledOpenUploadStream = true;
var _this = this;
checkIndexes(this, function() {
_this.bucket.s.checkedIndexes = true;
_this.bucket.emit('index');
});
}
}
util.inherits(GridFSBucketWriteStream, stream.Writable);
/**
* An error occurred
*
* @event GridFSBucketWriteStream#error
* @type {Error}
*/
/**
* `end()` was called and the write stream successfully wrote the file
* metadata and all the chunks to MongoDB.
*
* @event GridFSBucketWriteStream#finish
* @type {object}
*/
/**
* Write a buffer to the stream.
*
* @method
* @param {Buffer} chunk Buffer to write
* @param {String} encoding Optional encoding for the buffer
* @param {Function} callback Function to call when the chunk was added to the buffer, or if the entire chunk was persisted to MongoDB if this chunk caused a flush.
* @return {Boolean} False if this write required flushing a chunk to MongoDB. True otherwise.
*/
GridFSBucketWriteStream.prototype.write = function(chunk, encoding, callback) {
var _this = this;
return waitForIndexes(this, function() {
return doWrite(_this, chunk, encoding, callback);
});
};
/**
* Places this write stream into an aborted state (all future writes fail)
* and deletes all chunks that have already been written.
*
* @method
* @param {GridFSBucket~errorCallback} callback called when chunks are successfully removed or error occurred
* @return {Promise} if no callback specified
*/
GridFSBucketWriteStream.prototype.abort = function(callback) {
if (this.state.streamEnd) {
var error = new Error('Cannot abort a stream that has already completed');
if (typeof callback === 'function') {
return callback(error);
}
return this.state.promiseLibrary.reject(error);
}
if (this.state.aborted) {
error = new Error('Cannot call abort() on a stream twice');
if (typeof callback === 'function') {
return callback(error);
}
return this.state.promiseLibrary.reject(error);
}
this.state.aborted = true;
this.chunks.deleteMany({ files_id: this.id }, function(error) {
if (typeof callback === 'function') callback(error);
});
};
/**
* Tells the stream that no more data will be coming in. The stream will
* persist the remaining data to MongoDB, write the files document, and
* then emit a 'finish' event.
*
* @method
* @param {Buffer} chunk Buffer to write
* @param {String} encoding Optional encoding for the buffer
* @param {Function} callback Function to call when all files and chunks have been persisted to MongoDB
*/
GridFSBucketWriteStream.prototype.end = function(chunk, encoding, callback) {
var _this = this;
if (typeof chunk === 'function') {
(callback = chunk), (chunk = null), (encoding = null);
} else if (typeof encoding === 'function') {
(callback = encoding), (encoding = null);
}
if (checkAborted(this, callback)) {
return;
}
this.state.streamEnd = true;
if (callback) {
this.once('finish', function(result) {
callback(null, result);
});
}
if (!chunk) {
waitForIndexes(this, function() {
writeRemnant(_this);
});
return;
}
this.write(chunk, encoding, function() {
writeRemnant(_this);
});
};
/**
* @ignore
*/
function __handleError(_this, error, callback) {
if (_this.state.errored) {
return;
}
_this.state.errored = true;
if (callback) {
return callback(error);
}
_this.emit('error', error);
}
/**
* @ignore
*/
function createChunkDoc(filesId, n, data) {
return {
_id: core.BSON.ObjectId(),
files_id: filesId,
n: n,
data: data
};
}
/**
* @ignore
*/
function checkChunksIndex(_this, callback) {
_this.chunks.listIndexes().toArray(function(error, indexes) {
if (error) {
// Collection doesn't exist so create index
if (error.code === ERROR_NAMESPACE_NOT_FOUND) {
var index = { files_id: 1, n: 1 };
_this.chunks.createIndex(index, { background: false, unique: true }, function(error) {
if (error) {
return callback(error);
}
callback();
});
return;
}
return callback(error);
}
var hasChunksIndex = false;
indexes.forEach(function(index) {
if (index.key) {
var keys = Object.keys(index.key);
if (keys.length === 2 && index.key.files_id === 1 && index.key.n === 1) {
hasChunksIndex = true;
}
}
});
if (hasChunksIndex) {
callback();
} else {
index = { files_id: 1, n: 1 };
var indexOptions = getWriteOptions(_this);
indexOptions.background = false;
indexOptions.unique = true;
_this.chunks.createIndex(index, indexOptions, function(error) {
if (error) {
return callback(error);
}
callback();
});
}
});
}
/**
* @ignore
*/
function checkDone(_this, callback) {
if (_this.done) return true;
if (_this.state.streamEnd && _this.state.outstandingRequests === 0 && !_this.state.errored) {
// Set done so we dont' trigger duplicate createFilesDoc
_this.done = true;
// Create a new files doc
var filesDoc = createFilesDoc(
_this.id,
_this.length,
_this.chunkSizeBytes,
_this.md5 && _this.md5.digest('hex'),
_this.filename,
_this.options.contentType,
_this.options.aliases,
_this.options.metadata
);
if (checkAborted(_this, callback)) {
return false;
}
_this.files.insertOne(filesDoc, getWriteOptions(_this), function(error) {
if (error) {
return __handleError(_this, error, callback);
}
_this.emit('finish', filesDoc);
});
return true;
}
return false;
}
/**
* @ignore
*/
function checkIndexes(_this, callback) {
_this.files.findOne({}, { _id: 1 }, function(error, doc) {
if (error) {
return callback(error);
}
if (doc) {
return callback();
}
_this.files.listIndexes().toArray(function(error, indexes) {
if (error) {
// Collection doesn't exist so create index
if (error.code === ERROR_NAMESPACE_NOT_FOUND) {
var index = { filename: 1, uploadDate: 1 };
_this.files.createIndex(index, { background: false }, function(error) {
if (error) {
return callback(error);
}
checkChunksIndex(_this, callback);
});
return;
}
return callback(error);
}
var hasFileIndex = false;
indexes.forEach(function(index) {
var keys = Object.keys(index.key);
if (keys.length === 2 && index.key.filename === 1 && index.key.uploadDate === 1) {
hasFileIndex = true;
}
});
if (hasFileIndex) {
checkChunksIndex(_this, callback);
} else {
index = { filename: 1, uploadDate: 1 };
var indexOptions = getWriteOptions(_this);
indexOptions.background = false;
_this.files.createIndex(index, indexOptions, function(error) {
if (error) {
return callback(error);
}
checkChunksIndex(_this, callback);
});
}
});
});
}
/**
* @ignore
*/
function createFilesDoc(_id, length, chunkSize, md5, filename, contentType, aliases, metadata) {
var ret = {
_id: _id,
length: length,
chunkSize: chunkSize,
uploadDate: new Date(),
filename: filename
};
if (md5) {
ret.md5 = md5;
}
if (contentType) {
ret.contentType = contentType;
}
if (aliases) {
ret.aliases = aliases;
}
if (metadata) {
ret.metadata = metadata;
}
return ret;
}
/**
* @ignore
*/
function doWrite(_this, chunk, encoding, callback) {
if (checkAborted(_this, callback)) {
return false;
}
var inputBuf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding);
_this.length += inputBuf.length;
// Input is small enough to fit in our buffer
if (_this.pos + inputBuf.length < _this.chunkSizeBytes) {
inputBuf.copy(_this.bufToStore, _this.pos);
_this.pos += inputBuf.length;
callback && callback();
// Note that we reverse the typical semantics of write's return value
// to be compatible with node's `.pipe()` function.
// True means client can keep writing.
return true;
}
// Otherwise, buffer is too big for current chunk, so we need to flush
// to MongoDB.
var inputBufRemaining = inputBuf.length;
var spaceRemaining = _this.chunkSizeBytes - _this.pos;
var numToCopy = Math.min(spaceRemaining, inputBuf.length);
var outstandingRequests = 0;
while (inputBufRemaining > 0) {
var inputBufPos = inputBuf.length - inputBufRemaining;
inputBuf.copy(_this.bufToStore, _this.pos, inputBufPos, inputBufPos + numToCopy);
_this.pos += numToCopy;
spaceRemaining -= numToCopy;
if (spaceRemaining === 0) {
if (_this.md5) {
_this.md5.update(_this.bufToStore);
}
var doc = createChunkDoc(_this.id, _this.n, _this.bufToStore);
++_this.state.outstandingRequests;
++outstandingRequests;
if (checkAborted(_this, callback)) {
return false;
}
_this.chunks.insertOne(doc, getWriteOptions(_this), function(error) {
if (error) {
return __handleError(_this, error);
}
--_this.state.outstandingRequests;
--outstandingRequests;
if (!outstandingRequests) {
_this.emit('drain', doc);
callback && callback();
checkDone(_this);
}
});
spaceRemaining = _this.chunkSizeBytes;
_this.pos = 0;
++_this.n;
}
inputBufRemaining -= numToCopy;
numToCopy = Math.min(spaceRemaining, inputBufRemaining);
}
// Note that we reverse the typical semantics of write's return value
// to be compatible with node's `.pipe()` function.
// False means the client should wait for the 'drain' event.
return false;
}
/**
* @ignore
*/
function getWriteOptions(_this) {
var obj = {};
if (_this.options.writeConcern) {
obj.w = _this.options.writeConcern.w;
obj.wtimeout = _this.options.writeConcern.wtimeout;
obj.j = _this.options.writeConcern.j;
}
return obj;
}
/**
* @ignore
*/
function waitForIndexes(_this, callback) {
if (_this.bucket.s.checkedIndexes) {
return callback(false);
}
_this.bucket.once('index', function() {
callback(true);
});
return true;
}
/**
* @ignore
*/
function writeRemnant(_this, callback) {
// Buffer is empty, so don't bother to insert
if (_this.pos === 0) {
return checkDone(_this, callback);
}
++_this.state.outstandingRequests;
// Create a new buffer to make sure the buffer isn't bigger than it needs
// to be.
var remnant = Buffer.alloc(_this.pos);
_this.bufToStore.copy(remnant, 0, 0, _this.pos);
if (_this.md5) {
_this.md5.update(remnant);
}
var doc = createChunkDoc(_this.id, _this.n, remnant);
// If the stream was aborted, do not write remnant
if (checkAborted(_this, callback)) {
return false;
}
_this.chunks.insertOne(doc, getWriteOptions(_this), function(error) {
if (error) {
return __handleError(_this, error);
}
--_this.state.outstandingRequests;
checkDone(_this);
});
}
/**
* @ignore
*/
function checkAborted(_this, callback) {
if (_this.state.aborted) {
if (typeof callback === 'function') {
callback(new Error('this stream has been aborted'));
}
return true;
}
return false;
}
+236
View File
@@ -0,0 +1,236 @@
'use strict';
var Binary = require('../core').BSON.Binary,
ObjectID = require('../core').BSON.ObjectID;
var Buffer = require('safe-buffer').Buffer;
/**
* Class for representing a single chunk in GridFS.
*
* @class
*
* @param file {GridStore} The {@link GridStore} object holding this chunk.
* @param mongoObject {object} The mongo object representation of this chunk.
*
* @throws Error when the type of data field for {@link mongoObject} is not
* supported. Currently supported types for data field are instances of
* {@link String}, {@link Array}, {@link Binary} and {@link Binary}
* from the bson module
*
* @see Chunk#buildMongoObject
*/
var Chunk = function(file, mongoObject, writeConcern) {
if (!(this instanceof Chunk)) return new Chunk(file, mongoObject);
this.file = file;
var mongoObjectFinal = mongoObject == null ? {} : mongoObject;
this.writeConcern = writeConcern || { w: 1 };
this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id;
this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n;
this.data = new Binary();
if (typeof mongoObjectFinal.data === 'string') {
var buffer = Buffer.alloc(mongoObjectFinal.data.length);
buffer.write(mongoObjectFinal.data, 0, mongoObjectFinal.data.length, 'binary');
this.data = new Binary(buffer);
} else if (Array.isArray(mongoObjectFinal.data)) {
buffer = Buffer.alloc(mongoObjectFinal.data.length);
var data = mongoObjectFinal.data.join('');
buffer.write(data, 0, data.length, 'binary');
this.data = new Binary(buffer);
} else if (mongoObjectFinal.data && mongoObjectFinal.data._bsontype === 'Binary') {
this.data = mongoObjectFinal.data;
} else if (!Buffer.isBuffer(mongoObjectFinal.data) && !(mongoObjectFinal.data == null)) {
throw Error('Illegal chunk format');
}
// Update position
this.internalPosition = 0;
};
/**
* Writes a data to this object and advance the read/write head.
*
* @param data {string} the data to write
* @param callback {function(*, GridStore)} This will be called after executing
* this method. The first parameter will contain null and the second one
* will contain a reference to this object.
*/
Chunk.prototype.write = function(data, callback) {
this.data.write(data, this.internalPosition, data.length, 'binary');
this.internalPosition = this.data.length();
if (callback != null) return callback(null, this);
return this;
};
/**
* Reads data and advances the read/write head.
*
* @param length {number} The length of data to read.
*
* @return {string} The data read if the given length will not exceed the end of
* the chunk. Returns an empty String otherwise.
*/
Chunk.prototype.read = function(length) {
// Default to full read if no index defined
length = length == null || length === 0 ? this.length() : length;
if (this.length() - this.internalPosition + 1 >= length) {
var data = this.data.read(this.internalPosition, length);
this.internalPosition = this.internalPosition + length;
return data;
} else {
return '';
}
};
Chunk.prototype.readSlice = function(length) {
if (this.length() - this.internalPosition >= length) {
var data = null;
if (this.data.buffer != null) {
//Pure BSON
data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);
} else {
//Native BSON
data = Buffer.alloc(length);
length = this.data.readInto(data, this.internalPosition);
}
this.internalPosition = this.internalPosition + length;
return data;
} else {
return null;
}
};
/**
* Checks if the read/write head is at the end.
*
* @return {boolean} Whether the read/write head has reached the end of this
* chunk.
*/
Chunk.prototype.eof = function() {
return this.internalPosition === this.length() ? true : false;
};
/**
* Reads one character from the data of this chunk and advances the read/write
* head.
*
* @return {string} a single character data read if the the read/write head is
* not at the end of the chunk. Returns an empty String otherwise.
*/
Chunk.prototype.getc = function() {
return this.read(1);
};
/**
* Clears the contents of the data in this chunk and resets the read/write head
* to the initial position.
*/
Chunk.prototype.rewind = function() {
this.internalPosition = 0;
this.data = new Binary();
};
/**
* Saves this chunk to the database. Also overwrites existing entries having the
* same id as this chunk.
*
* @param callback {function(*, GridStore)} This will be called after executing
* this method. The first parameter will contain null and the second one
* will contain a reference to this object.
*/
Chunk.prototype.save = function(options, callback) {
var self = this;
if (typeof options === 'function') {
callback = options;
options = {};
}
self.file.chunkCollection(function(err, collection) {
if (err) return callback(err);
// Merge the options
var writeOptions = { upsert: true };
for (var name in options) writeOptions[name] = options[name];
for (name in self.writeConcern) writeOptions[name] = self.writeConcern[name];
if (self.data.length() > 0) {
self.buildMongoObject(function(mongoObject) {
var options = { forceServerObjectId: true };
for (var name in self.writeConcern) {
options[name] = self.writeConcern[name];
}
collection.replaceOne({ _id: self.objectId }, mongoObject, writeOptions, function(err) {
callback(err, self);
});
});
} else {
callback(null, self);
}
// });
});
};
/**
* Creates a mongoDB object representation of this chunk.
*
* @param callback {function(Object)} This will be called after executing this
* method. The object will be passed to the first parameter and will have
* the structure:
*
* <pre><code>
* {
* '_id' : , // {number} id for this chunk
* 'files_id' : , // {number} foreign key to the file collection
* 'n' : , // {number} chunk number
* 'data' : , // {bson#Binary} the chunk data itself
* }
* </code></pre>
*
* @see <a href="http://www.mongodb.org/display/DOCS/GridFS+Specification#GridFSSpecification-{{chunks}}">MongoDB GridFS Chunk Object Structure</a>
*/
Chunk.prototype.buildMongoObject = function(callback) {
var mongoObject = {
files_id: this.file.fileId,
n: this.chunkNumber,
data: this.data
};
// If we are saving using a specific ObjectId
if (this.objectId != null) mongoObject._id = this.objectId;
callback(mongoObject);
};
/**
* @return {number} the length of the data
*/
Chunk.prototype.length = function() {
return this.data.length();
};
/**
* The position of the read/write head
* @name position
* @lends Chunk#
* @field
*/
Object.defineProperty(Chunk.prototype, 'position', {
enumerable: true,
get: function() {
return this.internalPosition;
},
set: function(value) {
this.internalPosition = value;
}
});
/**
* The default chunk size
* @constant
*/
Chunk.DEFAULT_CHUNK_SIZE = 1024 * 255;
module.exports = Chunk;
+1913
View File
File diff suppressed because it is too large. Load diff
+479
View File
@@ -0,0 +1,479 @@
'use strict';
const ChangeStream = require('./change_stream');
const Db = require('./db');
const EventEmitter = require('events').EventEmitter;
const executeOperation = require('./operations/execute_operation');
const inherits = require('util').inherits;
const MongoError = require('./core').MongoError;
const deprecate = require('util').deprecate;
const WriteConcern = require('./write_concern');
const MongoDBNamespace = require('./utils').MongoDBNamespace;
const ReadPreference = require('./core/topologies/read_preference');
// Operations
const ConnectOperation = require('./operations/connect');
const CloseOperation = require('./operations/close');
/**
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
*
* @example
* // Connect using a MongoClient instance
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* const mongoClient = new MongoClient(url);
* mongoClient.connect(function(err, client) {
* const db = client.db(dbName);
* client.close();
* });
*
* @example
* // Connect using the MongoClient.connect static method
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, client) {
* const db = client.db(dbName);
* client.close();
* });
*/
/**
* A string specifying the level of a ReadConcern
* @typedef {'local'|'available'|'majority'|'linearizable'|'snapshot'} ReadConcernLevel
* @see https://docs.mongodb.com/manual/reference/read-concern/index.html#read-concern-levels
*/
/**
* Configuration options for a automatic client encryption.
*
* **NOTE**: Support for client side encryption is in beta. Backwards-breaking changes may be made before the final release.
*
* @typedef {Object} AutoEncryptionOptions
* @property {MongoClient} [keyVaultClient] A `MongoClient` used to fetch keys from a key vault
* @property {string} [keyVaultNamespace] The namespace where keys are stored in the key vault
* @property {object} [kmsProviders] Provider details for the desired Key Management Service to use for encryption
* @property {object} [kmsProviders.aws] Optional settings for the AWS KMS provider
* @property {string} [kmsProviders.aws.accessKeyId] The access key used for the AWS KMS provider
* @property {string} [kmsProviders.aws.secretAccessKey] The secret access key used for the AWS KMS provider
* @property {object} [kmsProviders.local] Optional settings for the local KMS provider
* @property {string} [kmsProviders.local.key] The master key used to encrypt/decrypt data keys
* @property {object} [schemaMap] A map of namespaces to a local JSON schema for encryption
* @property {boolean} [bypassAutoEncryption] Allows the user to bypass auto encryption, maintaining implicit decryption
* @property {object} [extraOptions] Extra options related to the mongocryptd process
* @property {string} [extraOptions.mongocryptURI] A local process the driver communicates with to determine how to encrypt values in a command. Defaults to "mongodb://%2Fvar%2Fmongocryptd.sock" if domain sockets are available or "mongodb://localhost:27020" otherwise
* @property {boolean} [extraOptions.mongocryptdBypassSpawn=false] If true, autoEncryption will not attempt to spawn a mongocryptd before connecting
* @property {string} [extraOptions.mongocryptdSpawnPath] The path to the mongocryptd executable on the system
* @property {string[]} [extraOptions.mongocryptdSpawnArgs] Command line arguments to use when auto-spawning a mongocryptd
*/
/**
* Creates a new MongoClient instance
* @class
* @param {string} url The connection URI string
* @param {object} [options] Optional settings
* @param {number} [options.poolSize=5] The maximum size of the individual server pool
* @param {boolean} [options.ssl=false] Enable SSL connection.
* @param {boolean} [options.sslValidate=false] Validate mongod server certificate against Certificate Authority
* @param {buffer} [options.sslCA=undefined] SSL Certificate store binary buffer
* @param {buffer} [options.sslCert=undefined] SSL Certificate binary buffer
* @param {buffer} [options.sslKey=undefined] SSL Key file binary buffer
* @param {string} [options.sslPass=undefined] SSL Certificate pass phrase
* @param {buffer} [options.sslCRL=undefined] SSL Certificate revocation list binary buffer
* @param {boolean} [options.autoReconnect=true] Enable autoReconnect for single server instances
* @param {boolean} [options.noDelay=true] TCP Connection no delay
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
* @param {number} [options.keepAliveInitialDelay=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
* @param {number} [options.connectTimeoutMS=30000] TCP Connection timeout setting
* @param {number} [options.family] Version of IP stack. Can be 4, 6 or null (default).
* If null, will attempt to connect with IPv6, and will fall back to IPv4 on failure
* @param {number} [options.socketTimeoutMS=360000] TCP Socket timeout setting
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
* @param {boolean} [options.ha=true] Control if high availability monitoring runs for Replicaset or Mongos proxies
* @param {number} [options.haInterval=10000] The High availability period for replicaset inquiry
* @param {string} [options.replicaSet=undefined] The Replicaset set name
* @param {number} [options.secondaryAcceptableLatencyMS=15] Cutoff latency point in MS for Replicaset member selection
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for Mongos proxies selection
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available
* @param {string} [options.authSource=undefined] Define the database to authenticate against
* @param {(number|string)} [options.w] The write concern
* @param {number} [options.wtimeout] The write concern timeout
* @param {boolean} [options.j=false] Specify a journal write concern
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers
* @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
* @param {object} [options.promiseLibrary] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
* @param {object} [options.readConcern] Specify a read concern for the collection (only MongoDB 3.2 or higher supported)
* @param {ReadConcernLevel} [options.readConcern.level='local'] Specify a read concern level for the collection operations (only MongoDB 3.2 or higher supported)
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed)
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug)
* @param {object} [options.logger=undefined] Custom logger object
* @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.promoteLongs=true] Promotes long values to number if they fit inside the 53 bits resolution
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness
* @param {string} [options.appname=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
* @param {string} [options.auth.user=undefined] The username for auth
* @param {string} [options.auth.password=undefined] The password for auth
* @param {string} [options.authMechanism=undefined] Mechanism for authentication: MDEFAULT, GSSAPI, PLAIN, MONGODB-X509, or SCRAM-SHA-1
* @param {object} [options.compression] Type of compression to use: snappy or zlib
* @param {boolean} [options.fsync=false] Specify a file sync write concern
* @param {array} [options.readPreferenceTags] Read preference tags
* @param {number} [options.numberOfRetries=5] The number of retries for a tailable cursor
* @param {boolean} [options.auto_reconnect=true] Enable auto reconnecting for single server instances
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this client
* @param {number} [options.minSize] If present, the connection pool will be initialized with minSize connections, and will never dip below minSize connections
* @param {boolean} [options.useNewUrlParser=false] Determines whether or not to use the new url parser. Enables the new, spec-compliant, url parser shipped in the core driver. This url parser fixes a number of problems with the original parser, and aims to outright replace that parser in the near future.
* @param {boolean} [options.useUnifiedTopology] Enables the new unified topology layer
* @param {AutoEncryptionOptions} [options.autoEncryption] Optionally enable client side auto encryption
* @param {MongoClient~connectCallback} [callback] The command result callback
* @return {MongoClient} a MongoClient instance
*/
function MongoClient(url, options) {
if (!(this instanceof MongoClient)) return new MongoClient(url, options);
// Set up event emitter
EventEmitter.call(this);
// The internal state
this.s = {
url: url,
options: options || {},
promiseLibrary: null,
dbCache: new Map(),
sessions: new Set(),
writeConcern: WriteConcern.fromOptions(options),
namespace: new MongoDBNamespace('admin')
};
// Get the promiseLibrary
const promiseLibrary = this.s.options.promiseLibrary || Promise;
// Add the promise to the internal state
this.s.promiseLibrary = promiseLibrary;
}
/**
* @ignore
*/
inherits(MongoClient, EventEmitter);
Object.defineProperty(MongoClient.prototype, 'writeConcern', {
enumerable: true,
get: function() {
return this.s.writeConcern;
}
});
Object.defineProperty(MongoClient.prototype, 'readPreference', {
enumerable: true,
get: function() {
return ReadPreference.primary;
}
});
/**
* The callback format for results
* @callback MongoClient~connectCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {MongoClient} client The connected client.
*/
/**
* Connect to MongoDB using a url as documented at
*
* docs.mongodb.org/manual/reference/connection-string/
*
* Note that for replicasets the replicaSet query parameter is required in the 2.0 driver
*
* @method
* @param {MongoClient~connectCallback} [callback] The command result callback
* @return {Promise<MongoClient>} returns Promise if no callback passed
*/
MongoClient.prototype.connect = function(callback) {
if (typeof callback === 'string') {
throw new TypeError('`connect` only accepts a callback');
}
const operation = new ConnectOperation(this);
return executeOperation(this, operation, callback);
};
MongoClient.prototype.logout = deprecate(function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
if (typeof callback === 'function') callback(null, true);
}, 'Multiple authentication is prohibited on a connected client, please only authenticate once per MongoClient');
/**
* Close the db and its underlying connections
* @method
* @param {boolean} [force=false] Force close, emitting no events
* @param {Db~noResultCallback} [callback] The result callback
* @return {Promise} returns Promise if no callback passed
*/
MongoClient.prototype.close = function(force, callback) {
if (typeof force === 'function') (callback = force), (force = false);
const operation = new CloseOperation(this, force);
return executeOperation(this, operation, callback);
};
/**
* Create a new Db instance sharing the current socket connections. Be aware that the new db instances are
* related in a parent-child relationship to the original instance so that events are correctly emitted on child
* db instances. Child db instances are cached so performing db('db1') twice will return the same instance.
* You can control these behaviors with the options noListener and returnNonCachedInstance.
*
* @method
* @param {string} [dbName] The name of the database we want to use. If not provided, use database name from connection string.
* @param {object} [options] Optional settings.
* @param {boolean} [options.noListener=false] Do not make the db an event listener to the original connection.
* @param {boolean} [options.returnNonCachedInstance=false] Control if you want to return a cached instance or have a new one created
* @return {Db}
*/
MongoClient.prototype.db = function(dbName, options) {
options = options || {};
// Default to db from connection string if not provided
if (!dbName) {
dbName = this.s.options.dbName;
}
// Copy the options and add out internal override of the not shared flag
const finalOptions = Object.assign({}, this.s.options, options);
// Do we have the db in the cache already
if (this.s.dbCache.has(dbName) && finalOptions.returnNonCachedInstance !== true) {
return this.s.dbCache.get(dbName);
}
// Add promiseLibrary
finalOptions.promiseLibrary = this.s.promiseLibrary;
// If no topology throw an error message
if (!this.topology) {
throw new MongoError('MongoClient must be connected before calling MongoClient.prototype.db');
}
// Return the db object
const db = new Db(dbName, this.topology, finalOptions);
// Add the db to the cache
this.s.dbCache.set(dbName, db);
// Return the database
return db;
};
/**
* Check if MongoClient is connected
*
* @method
* @param {object} [options] Optional settings.
* @param {boolean} [options.noListener=false] Do not make the db an event listener to the original connection.
* @param {boolean} [options.returnNonCachedInstance=false] Control if you want to return a cached instance or have a new one created
* @return {boolean}
*/
MongoClient.prototype.isConnected = function(options) {
options = options || {};
if (!this.topology) return false;
return this.topology.isConnected(options);
};
/**
* Connect to MongoDB using a url as documented at
*
* docs.mongodb.org/manual/reference/connection-string/
*
* Note that for replicasets the replicaSet query parameter is required in the 2.0 driver
*
* @method
* @static
* @param {string} url The connection URI string
* @param {object} [options] Optional settings
* @param {number} [options.poolSize=5] The maximum size of the individual server pool
* @param {boolean} [options.ssl=false] Enable SSL connection.
* @param {boolean} [options.sslValidate=false] Validate mongod server certificate against Certificate Authority
* @param {buffer} [options.sslCA=undefined] SSL Certificate store binary buffer
* @param {buffer} [options.sslCert=undefined] SSL Certificate binary buffer
* @param {buffer} [options.sslKey=undefined] SSL Key file binary buffer
* @param {string} [options.sslPass=undefined] SSL Certificate pass phrase
* @param {buffer} [options.sslCRL=undefined] SSL Certificate revocation list binary buffer
* @param {boolean} [options.autoReconnect=true] Enable autoReconnect for single server instances
* @param {boolean} [options.noDelay=true] TCP Connection no delay
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
* @param {boolean} [options.keepAliveInitialDelay=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
* @param {number} [options.connectTimeoutMS=30000] TCP Connection timeout setting
* @param {number} [options.family] Version of IP stack. Can be 4, 6 or null (default).
* If null, will attempt to connect with IPv6, and will fall back to IPv4 on failure
* @param {number} [options.socketTimeoutMS=360000] TCP Socket timeout setting
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
* @param {boolean} [options.ha=true] Control if high availability monitoring runs for Replicaset or Mongos proxies
* @param {number} [options.haInterval=10000] The High availability period for replicaset inquiry
* @param {string} [options.replicaSet=undefined] The Replicaset set name
* @param {number} [options.secondaryAcceptableLatencyMS=15] Cutoff latency point in MS for Replicaset member selection
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for Mongos proxies selection
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available
* @param {string} [options.authSource=undefined] Define the database to authenticate against
* @param {(number|string)} [options.w] The write concern
* @param {number} [options.wtimeout] The write concern timeout
* @param {boolean} [options.j=false] Specify a journal write concern
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers
* @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
* @param {object} [options.promiseLibrary] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
* @param {object} [options.readConcern] Specify a read concern for the collection (only MongoDB 3.2 or higher supported)
* @param {ReadConcernLevel} [options.readConcern.level='local'] Specify a read concern level for the collection operations (only MongoDB 3.2 or higher supported)
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed)
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug)
* @param {object} [options.logger=undefined] Custom logger object
* @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.promoteLongs=true] Promotes long values to number if they fit inside the 53 bits resolution
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness
* @param {string} [options.appname=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
* @param {string} [options.auth.user=undefined] The username for auth
* @param {string} [options.auth.password=undefined] The password for auth
* @param {string} [options.authMechanism=undefined] Mechanism for authentication: MDEFAULT, GSSAPI, PLAIN, MONGODB-X509, or SCRAM-SHA-1
* @param {object} [options.compression] Type of compression to use: snappy or zlib
* @param {boolean} [options.fsync=false] Specify a file sync write concern
* @param {array} [options.readPreferenceTags] Read preference tags
* @param {number} [options.numberOfRetries=5] The number of retries for a tailable cursor
* @param {boolean} [options.auto_reconnect=true] Enable auto reconnecting for single server instances
* @param {number} [options.minSize] If present, the connection pool will be initialized with minSize connections, and will never dip below minSize connections
* @param {MongoClient~connectCallback} [callback] The command result callback
* @return {Promise<MongoClient>} returns Promise if no callback passed
*/
MongoClient.connect = function(url, options, callback) {
const args = Array.prototype.slice.call(arguments, 1);
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
options = args.length ? args.shift() : null;
options = options || {};
// Create client
const mongoClient = new MongoClient(url, options);
// Execute the connect method
return mongoClient.connect(callback);
};
/**
* Starts a new session on the server
*
* @param {SessionOptions} [options] optional settings for a driver session
* @return {ClientSession} the newly established session
*/
MongoClient.prototype.startSession = function(options) {
options = Object.assign({ explicit: true }, options);
if (!this.topology) {
throw new MongoError('Must connect to a server before calling this method');
}
if (!this.topology.hasSessionSupport()) {
throw new MongoError('Current topology does not support sessions');
}
return this.topology.startSession(options, this.s.options);
};
/**
* Runs a given operation with an implicitly created session. The lifetime of the session
* will be handled without the need for user interaction.
*
* NOTE: presently the operation MUST return a Promise (either explicit or implicity as an async function)
*
* @param {Object} [options] Optional settings to be appled to implicitly created session
* @param {Function} operation An operation to execute with an implicitly created session. The signature of this MUST be `(session) => {}`
* @return {Promise}
*/
MongoClient.prototype.withSession = function(options, operation) {
if (typeof options === 'function') (operation = options), (options = undefined);
const session = this.startSession(options);
let cleanupHandler = (err, result, opts) => {
// prevent multiple calls to cleanupHandler
cleanupHandler = () => {
throw new ReferenceError('cleanupHandler was called too many times');
};
opts = Object.assign({ throw: true }, opts);
session.endSession();
if (err) {
if (opts.throw) throw err;
return Promise.reject(err);
}
};
try {
const result = operation(session);
return Promise.resolve(result)
.then(result => cleanupHandler(null, result))
.catch(err => cleanupHandler(err, null, { throw: true }));
} catch (err) {
return cleanupHandler(err, null, { throw: false });
}
};
/**
* Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this cluster. Will ignore all changes to system collections, as well as the local, admin,
* and config databases.
* @method
* @since 3.1.0
* @param {Array} [pipeline] An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
* @param {object} [options] Optional settings
* @param {string} [options.fullDocument='default'] Allowed values: default, updateLookup. When set to updateLookup, the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.
* @param {object} [options.resumeAfter] Specifies the logical starting point for the new change stream. This should be the _id field from a previously returned change stream document.
* @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a change stream query
* @param {number} [options.batchSize] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
* @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
* @param {ReadPreference} [options.readPreference] The read preference. See {@link https://docs.mongodb.com/manual/reference/read-preference|read preference documentation}.
* @param {Timestamp} [options.startAtOperationTime] receive change events that occur after the specified timestamp
* @param {ClientSession} [options.session] optional session to use for this operation
* @return {ChangeStream} a ChangeStream instance.
*/
MongoClient.prototype.watch = function(pipeline, options) {
pipeline = pipeline || [];
options = options || {};
// Allow optionally not specifying a pipeline
if (!Array.isArray(pipeline)) {
options = pipeline;
pipeline = [];
}
return new ChangeStream(this, pipeline, options);
};
/**
* Return the mongo client logger
* @method
* @return {Logger} return the mongo client logger
* @ignore
*/
MongoClient.prototype.getLogger = function() {
return this.s.options.logger;
};
module.exports = MongoClient;
+96
View File
@@ -0,0 +1,96 @@
'use strict';
const Aspect = require('./operation').Aspect;
const CommandOperation = require('./command');
const defineAspects = require('./operation').defineAspects;
const crypto = require('crypto');
const handleCallback = require('../utils').handleCallback;
const toError = require('../utils').toError;
class AddUserOperation extends CommandOperation {
constructor(db, username, password, options) {
super(db, options);
this.username = username;
this.password = password;
}
_buildCommand() {
const db = this.db;
const username = this.username;
const password = this.password;
const options = this.options;
// Get additional values
let roles = Array.isArray(options.roles) ? options.roles : [];
// If not roles defined print deprecated message
// TODO: handle deprecation properly
if (roles.length === 0) {
console.log('Creating a user without roles is deprecated in MongoDB >= 2.6');
}
// Check the db name and add roles if needed
if (
(db.databaseName.toLowerCase() === 'admin' || options.dbName === 'admin') &&
!Array.isArray(options.roles)
) {
roles = ['root'];
} else if (!Array.isArray(options.roles)) {
roles = ['dbOwner'];
}
const digestPassword = db.s.topology.lastIsMaster().maxWireVersion >= 7;
let userPassword = password;
if (!digestPassword) {
// Use node md5 generator
const md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ':mongo:' + password);
userPassword = md5.digest('hex');
}
// Build the command to execute
const command = {
createUser: username,
customData: options.customData || {},
roles: roles,
digestPassword
};
// No password
if (typeof password === 'string') {
command.pwd = userPassword;
}
return command;
}
execute(callback) {
const options = this.options;
// Error out if digestPassword set
if (options.digestPassword != null) {
return callback(
toError(
"The digestPassword option is not supported via add_user. Please use db.command('createUser', ...) instead for this option."
)
);
}
// Attempt to execute auth command
super.execute((err, r) => {
if (!err) {
return handleCallback(callback, err, r);
}
return handleCallback(callback, err, null);
});
}
}
defineAspects(AddUserOperation, Aspect.WRITE_OPERATION);
module.exports = AddUserOperation;
+62
View File
@@ -0,0 +1,62 @@
'use strict';
const executeCommand = require('./db_ops').executeCommand;
const executeDbAdminCommand = require('./db_ops').executeDbAdminCommand;
/**
* Get ReplicaSet status
*
* @param {Admin} a collection instance.
* @param {Object} [options] Optional settings. See Admin.prototype.replSetGetStatus for a list of options.
* @param {Admin~resultCallback} [callback] The command result callback.
*/
function replSetGetStatus(admin, options, callback) {
executeDbAdminCommand(admin.s.db, { replSetGetStatus: 1 }, options, callback);
}
/**
* Retrieve this db's server status.
*
* @param {Admin} a collection instance.
* @param {Object} [options] Optional settings. See Admin.prototype.serverStatus for a list of options.
* @param {Admin~resultCallback} [callback] The command result callback
*/
function serverStatus(admin, options, callback) {
executeDbAdminCommand(admin.s.db, { serverStatus: 1 }, options, callback);
}
/**
* Validate an existing collection
*
* @param {Admin} a collection instance.
* @param {string} collectionName The name of the collection to validate.
* @param {Object} [options] Optional settings. See Admin.prototype.validateCollection for a list of options.
* @param {Admin~resultCallback} [callback] The command result callback.
*/
function validateCollection(admin, collectionName, options, callback) {
const command = { validate: collectionName };
const keys = Object.keys(options);
// Decorate command with extra options
for (let i = 0; i < keys.length; i++) {
if (options.hasOwnProperty(keys[i]) && keys[i] !== 'session') {
command[keys[i]] = options[keys[i]];
}
}
executeCommand(admin.s.db, command, options, (err, doc) => {
if (err != null) return callback(err, null);
if (doc.ok === 0) return callback(new Error('Error with validate command'), null);
if (doc.result != null && doc.result.constructor !== String)
return callback(new Error('Error with validation data'), null);
if (doc.result != null && doc.result.match(/exception|corrupt/) != null)
return callback(new Error('Error: invalid collection ' + collectionName), null);
if (doc.valid != null && !doc.valid)
return callback(new Error('Error: invalid collection ' + collectionName), null);
return callback(null, doc);
});
}
module.exports = { replSetGetStatus, serverStatus, validateCollection };
+106
View File
@@ -0,0 +1,106 @@
'use strict';
const CommandOperationV2 = require('./command_v2');
const MongoError = require('../core').MongoError;
const maxWireVersion = require('../core/utils').maxWireVersion;
const ReadPreference = require('../core').ReadPreference;
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const DB_AGGREGATE_COLLECTION = 1;
const MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT = 8;
class AggregateOperation extends CommandOperationV2 {
constructor(parent, pipeline, options) {
super(parent, options, { fullResponse: true });
this.target =
parent.s.namespace && parent.s.namespace.collection
? parent.s.namespace.collection
: DB_AGGREGATE_COLLECTION;
this.pipeline = pipeline;
// determine if we have a write stage, override read preference if so
this.hasWriteStage = false;
if (typeof options.out === 'string') {
this.pipeline = this.pipeline.concat({ $out: options.out });
this.hasWriteStage = true;
} else if (pipeline.length > 0) {
const finalStage = pipeline[pipeline.length - 1];
if (finalStage.$out || finalStage.$merge) {
this.hasWriteStage = true;
}
}
if (this.hasWriteStage) {
this.readPreference = ReadPreference.primary;
}
if (options.explain && (this.readConcern || this.writeConcern)) {
throw new MongoError(
'"explain" cannot be used on an aggregate call with readConcern/writeConcern'
);
}
if (options.cursor != null && typeof options.cursor !== 'object') {
throw new MongoError('cursor options must be an object');
}
}
get canRetryRead() {
return !this.hasWriteStage;
}
addToPipeline(stage) {
this.pipeline.push(stage);
}
execute(server, callback) {
const options = this.options;
const serverWireVersion = maxWireVersion(server);
const command = { aggregate: this.target, pipeline: this.pipeline };
if (this.hasWriteStage && serverWireVersion < MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT) {
this.readConcern = null;
}
if (serverWireVersion >= 5) {
if (this.hasWriteStage && this.writeConcern) {
Object.assign(command, { writeConcern: this.writeConcern });
}
}
if (options.bypassDocumentValidation === true) {
command.bypassDocumentValidation = options.bypassDocumentValidation;
}
if (typeof options.allowDiskUse === 'boolean') {
command.allowDiskUse = options.allowDiskUse;
}
if (options.hint) {
command.hint = options.hint;
}
if (options.explain) {
options.full = false;
command.explain = options.explain;
}
command.cursor = options.cursor || {};
if (options.batchSize && !this.hasWriteStage) {
command.cursor.batchSize = options.batchSize;
}
super.executeCommand(server, command, callback);
}
}
defineAspects(AggregateOperation, [
Aspect.READ_OPERATION,
Aspect.RETRYABLE,
Aspect.EXECUTE_WITH_SELECTION
]);
module.exports = AggregateOperation;
+104
View File
@@ -0,0 +1,104 @@
'use strict';
const applyRetryableWrites = require('../utils').applyRetryableWrites;
const applyWriteConcern = require('../utils').applyWriteConcern;
const MongoError = require('../core').MongoError;
const OperationBase = require('./operation').OperationBase;
class BulkWriteOperation extends OperationBase {
constructor(collection, operations, options) {
super(options);
this.collection = collection;
this.operations = operations;
}
execute(callback) {
const coll = this.collection;
const operations = this.operations;
let options = this.options;
// Add ignoreUndfined
if (coll.s.options.ignoreUndefined) {
options = Object.assign({}, options);
options.ignoreUndefined = coll.s.options.ignoreUndefined;
}
// Create the bulk operation
const bulk =
options.ordered === true || options.ordered == null
? coll.initializeOrderedBulkOp(options)
: coll.initializeUnorderedBulkOp(options);
// Do we have a collation
let collation = false;
// for each op go through and add to the bulk
try {
for (let i = 0; i < operations.length; i++) {
// Get the operation type
const key = Object.keys(operations[i])[0];
// Check if we have a collation
if (operations[i][key].collation) {
collation = true;
}
// Pass to the raw bulk
bulk.raw(operations[i]);
}
} catch (err) {
return callback(err, null);
}
// Final options for retryable writes and write concern
let finalOptions = Object.assign({}, options);
finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
const writeCon = finalOptions.writeConcern ? finalOptions.writeConcern : {};
const capabilities = coll.s.topology.capabilities();
// Did the user pass in a collation, check if our write server supports it
if (collation && capabilities && !capabilities.commandsTakeCollation) {
return callback(new MongoError('server/primary/mongos does not support collation'));
}
// Execute the bulk
bulk.execute(writeCon, finalOptions, (err, r) => {
// We have connection level error
if (!r && err) {
return callback(err, null);
}
r.insertedCount = r.nInserted;
r.matchedCount = r.nMatched;
r.modifiedCount = r.nModified || 0;
r.deletedCount = r.nRemoved;
r.upsertedCount = r.getUpsertedIds().length;
r.upsertedIds = {};
r.insertedIds = {};
// Update the n
r.n = r.insertedCount;
// Inserted documents
const inserted = r.getInsertedIds();
// Map inserted ids
for (let i = 0; i < inserted.length; i++) {
r.insertedIds[inserted[i].index] = inserted[i]._id;
}
// Upserted documents
const upserted = r.getUpsertedIds();
// Map upserted ids
for (let i = 0; i < upserted.length; i++) {
r.upsertedIds[upserted[i].index] = upserted[i]._id;
}
// Return the results
callback(null, r);
});
}
}
module.exports = BulkWriteOperation;
+46
View File
@@ -0,0 +1,46 @@
'use strict';
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const OperationBase = require('./operation').OperationBase;
class CloseOperation extends OperationBase {
constructor(client, force) {
super();
this.client = client;
this.force = force;
}
execute(callback) {
const client = this.client;
const force = this.force;
const completeClose = err => {
client.emit('close', client);
for (const item of client.s.dbCache) {
item[1].emit('close', client);
}
client.removeAllListeners('close');
callback(err, null);
};
if (client.topology == null) {
completeClose();
return;
}
client.topology.close(force, err => {
const autoEncrypter = client.topology.s.options.autoEncrypter;
if (!autoEncrypter) {
completeClose(err);
return;
}
autoEncrypter.teardown(force, err2 => completeClose(err || err2));
});
}
}
defineAspects(CloseOperation, [Aspect.SKIP_SESSION]);
module.exports = CloseOperation;
+374
View File
@@ -0,0 +1,374 @@
'use strict';
const applyWriteConcern = require('../utils').applyWriteConcern;
const Code = require('../core').BSON.Code;
const createIndexDb = require('./db_ops').createIndex;
const decorateWithCollation = require('../utils').decorateWithCollation;
const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
const ensureIndexDb = require('./db_ops').ensureIndex;
const evaluate = require('./db_ops').evaluate;
const executeCommand = require('./db_ops').executeCommand;
const resolveReadPreference = require('../utils').resolveReadPreference;
const handleCallback = require('../utils').handleCallback;
const indexInformationDb = require('./db_ops').indexInformation;
const Long = require('../core').BSON.Long;
const MongoError = require('../core').MongoError;
const ReadPreference = require('../core').ReadPreference;
const toError = require('../utils').toError;
const insertDocuments = require('./common_functions').insertDocuments;
const updateDocuments = require('./common_functions').updateDocuments;
/**
* Group function helper
* @ignore
*/
// var groupFunction = function () {
// var c = db[ns].find(condition);
// var map = new Map();
// var reduce_function = reduce;
//
// while (c.hasNext()) {
// var obj = c.next();
// var key = {};
//
// for (var i = 0, len = keys.length; i < len; ++i) {
// var k = keys[i];
// key[k] = obj[k];
// }
//
// var aggObj = map.get(key);
//
// if (aggObj == null) {
// var newObj = Object.extend({}, key);
// aggObj = Object.extend(newObj, initial);
// map.put(key, aggObj);
// }
//
// reduce_function(obj, aggObj);
// }
//
// return { "result": map.values() };
// }.toString();
const groupFunction =
'function () {\nvar c = db[ns].find(condition);\nvar map = new Map();\nvar reduce_function = reduce;\n\nwhile (c.hasNext()) {\nvar obj = c.next();\nvar key = {};\n\nfor (var i = 0, len = keys.length; i < len; ++i) {\nvar k = keys[i];\nkey[k] = obj[k];\n}\n\nvar aggObj = map.get(key);\n\nif (aggObj == null) {\nvar newObj = Object.extend({}, key);\naggObj = Object.extend(newObj, initial);\nmap.put(key, aggObj);\n}\n\nreduce_function(obj, aggObj);\n}\n\nreturn { "result": map.values() };\n}';
// Check the update operation to ensure it has atomic operators.
function checkForAtomicOperators(update) {
if (Array.isArray(update)) {
return update.reduce((err, u) => err || checkForAtomicOperators(u), null);
}
const keys = Object.keys(update);
// same errors as the server would give for update doc lacking atomic operators
if (keys.length === 0) {
return toError('The update operation document must contain at least one atomic operator.');
}
if (keys[0][0] !== '$') {
return toError('the update operation document must contain atomic operators.');
}
}
/**
* Create an index on the db and collection.
*
* @method
* @param {Collection} a Collection instance.
* @param {(string|object)} fieldOrSpec Defines the index.
* @param {object} [options] Optional settings. See Collection.prototype.createIndex for a list of options.
* @param {Collection~resultCallback} [callback] The command result callback
*/
function createIndex(coll, fieldOrSpec, options, callback) {
createIndexDb(coll.s.db, coll.collectionName, fieldOrSpec, options, callback);
}
/**
* Create multiple indexes in the collection. This method is only supported for
* MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported
* error. Index specifications are defined at http://docs.mongodb.org/manual/reference/command/createIndexes/.
*
* @method
* @param {Collection} a Collection instance.
* @param {array} indexSpecs An array of index specifications to be created
* @param {Object} [options] Optional settings. See Collection.prototype.createIndexes for a list of options.
* @param {Collection~resultCallback} [callback] The command result callback
*/
function createIndexes(coll, indexSpecs, options, callback) {
const capabilities = coll.s.topology.capabilities();
// Ensure we generate the correct name if the parameter is not set
for (let i = 0; i < indexSpecs.length; i++) {
if (indexSpecs[i].name == null) {
const keys = [];
// Did the user pass in a collation, check if our write server supports it
if (indexSpecs[i].collation && capabilities && !capabilities.commandsTakeCollation) {
return callback(new MongoError('server/primary/mongos does not support collation'));
}
for (let name in indexSpecs[i].key) {
keys.push(`${name}_${indexSpecs[i].key[name]}`);
}
// Set the name
indexSpecs[i].name = keys.join('_');
}
}
options = Object.assign({}, options, { readPreference: ReadPreference.PRIMARY });
// Execute the index
executeCommand(
coll.s.db,
{
createIndexes: coll.collectionName,
indexes: indexSpecs
},
options,
callback
);
}
/**
* Ensure that an index exists. If the index does not exist, this function creates it.
*
* @method
* @param {Collection} a Collection instance.
* @param {(string|object)} fieldOrSpec Defines the index.
* @param {object} [options] Optional settings. See Collection.prototype.ensureIndex for a list of options.
* @param {Collection~resultCallback} [callback] The command result callback
*/
function ensureIndex(coll, fieldOrSpec, options, callback) {
ensureIndexDb(coll.s.db, coll.collectionName, fieldOrSpec, options, callback);
}
/**
* Run a group command across a collection.
*
* @method
* @param {Collection} a Collection instance.
* @param {(object|array|function|code)} keys An object, array or function expressing the keys to group by.
* @param {object} condition An optional condition that must be true for a row to be considered.
* @param {object} initial Initial value of the aggregation counter object.
* @param {(function|Code)} reduce The reduce function aggregates (reduces) the objects iterated
* @param {(function|Code)} finalize An optional function to be run on each item in the result set just before the item is returned.
* @param {boolean} command Specify if you wish to run using the internal group command or using eval, default is true.
* @param {object} [options] Optional settings. See Collection.prototype.group for a list of options.
* @param {Collection~resultCallback} [callback] The command result callback
* @deprecated MongoDB 3.6 or higher will no longer support the group command. We recommend rewriting using the aggregation framework.
*/
function group(coll, keys, condition, initial, reduce, finalize, command, options, callback) {
// Execute using the command
if (command) {
const reduceFunction = reduce && reduce._bsontype === 'Code' ? reduce : new Code(reduce);
const selector = {
group: {
ns: coll.collectionName,
$reduce: reduceFunction,
cond: condition,
initial: initial,
out: 'inline'
}
};
// if finalize is defined
if (finalize != null) selector.group['finalize'] = finalize;
// Set up group selector
if ('function' === typeof keys || (keys && keys._bsontype === 'Code')) {
selector.group.$keyf = keys && keys._bsontype === 'Code' ? keys : new Code(keys);
} else {
const hash = {};
keys.forEach(key => {
hash[key] = 1;
});
selector.group.key = hash;
}
options = Object.assign({}, options);
// Ensure we have the right read preference inheritance
options.readPreference = resolveReadPreference(coll, options);
// Do we have a readConcern specified
decorateWithReadConcern(selector, coll, options);
// Have we specified collation
try {
decorateWithCollation(selector, coll, options);
} catch (err) {
return callback(err, null);
}
// Execute command
executeCommand(coll.s.db, selector, options, (err, result) => {
if (err) return handleCallback(callback, err, null);
handleCallback(callback, null, result.retval);
});
} else {
// Create execution scope
const scope = reduce != null && reduce._bsontype === 'Code' ? reduce.scope : {};
scope.ns = coll.collectionName;
scope.keys = keys;
scope.condition = condition;
scope.initial = initial;
// Pass in the function text to execute within mongodb.
const groupfn = groupFunction.replace(/ reduce;/, reduce.toString() + ';');
evaluate(coll.s.db, new Code(groupfn, scope), null, options, (err, results) => {
if (err) return handleCallback(callback, err, null);
handleCallback(callback, null, results.result || results);
});
}
}
/**
* Retrieve all the indexes on the collection.
*
* @method
* @param {Collection} a Collection instance.
* @param {Object} [options] Optional settings. See Collection.prototype.indexes for a list of options.
* @param {Collection~resultCallback} [callback] The command result callback
*/
function indexes(coll, options, callback) {
options = Object.assign({}, { full: true }, options);
indexInformationDb(coll.s.db, coll.collectionName, options, callback);
}
/**
* Check if one or more indexes exist on the collection. This fails on the first index that doesn't exist.
*
* @method
* @param {Collection} a Collection instance.
* @param {(string|array)} indexes One or more index names to check.
* @param {Object} [options] Optional settings. See Collection.prototype.indexExists for a list of options.
* @param {Collection~resultCallback} [callback] The command result callback
*/
function indexExists(coll, indexes, options, callback) {
indexInformation(coll, options, (err, indexInformation) => {
// If we have an error return
if (err != null) return handleCallback(callback, err, null);
// Let's check for the index names
if (!Array.isArray(indexes))
return handleCallback(callback, null, indexInformation[indexes] != null);
// Check in list of indexes
for (let i = 0; i < indexes.length; i++) {
if (indexInformation[indexes[i]] == null) {
return handleCallback(callback, null, false);
}
}
// All keys found return true
return handleCallback(callback, null, true);
});
}
/**
* Retrieve this collection's index info.
*
* @method
* @param {Collection} a Collection instance.
* @param {object} [options] Optional settings. See Collection.prototype.indexInformation for a list of options.
* @param {Collection~resultCallback} [callback] The command result callback
*/
function indexInformation(coll, options, callback) {
indexInformationDb(coll.s.db, coll.collectionName, options, callback);
}
/**
* Return N parallel cursors for a collection to allow parallel reading of the entire collection. There are
* no ordering guarantees for returned results.
*
* @method
* @param {Collection} a Collection instance.
* @param {object} [options] Optional settings. See Collection.prototype.parallelCollectionScan for a list of options.
* @param {Collection~parallelCollectionScanCallback} [callback] The command result callback
*/
function parallelCollectionScan(coll, options, callback) {
// Create command object
const commandObject = {
parallelCollectionScan: coll.collectionName,
numCursors: options.numCursors
};
// Do we have a readConcern specified
decorateWithReadConcern(commandObject, coll, options);
// Store the raw value
const raw = options.raw;
delete options['raw'];
// Execute the command
executeCommand(coll.s.db, commandObject, options, (err, result) => {
if (err) return handleCallback(callback, err, null);
if (result == null)
return handleCallback(
callback,
new Error('no result returned for parallelCollectionScan'),
null
);
options = Object.assign({ explicitlyIgnoreSession: true }, options);
const cursors = [];
// Add the raw back to the option
if (raw) options.raw = raw;
// Create command cursors for each item
for (let i = 0; i < result.cursors.length; i++) {
const rawId = result.cursors[i].cursor.id;
// Convert cursorId to Long if needed
const cursorId = typeof rawId === 'number' ? Long.fromNumber(rawId) : rawId;
// Add a command cursor
cursors.push(coll.s.topology.cursor(coll.namespace, cursorId, options));
}
handleCallback(callback, null, cursors);
});
}
/**
* Save a document.
*
* @method
* @param {Collection} a Collection instance.
* @param {object} doc Document to save
* @param {object} [options] Optional settings. See Collection.prototype.save for a list of options.
* @param {Collection~writeOpCallback} [callback] The command result callback
* @deprecated use insertOne, insertMany, updateOne or updateMany
*/
function save(coll, doc, options, callback) {
// Get the write concern options
const finalOptions = applyWriteConcern(
Object.assign({}, options),
{ db: coll.s.db, collection: coll },
options
);
// Establish if we need to perform an insert or update
if (doc._id != null) {
finalOptions.upsert = true;
return updateDocuments(coll, { _id: doc._id }, doc, finalOptions, callback);
}
// Insert the document
insertDocuments(coll, [doc], finalOptions, (err, result) => {
if (callback == null) return;
if (doc == null) return handleCallback(callback, null, null);
if (err) return handleCallback(callback, err, null);
handleCallback(callback, null, result);
});
}
module.exports = {
checkForAtomicOperators,
createIndex,
createIndexes,
ensureIndex,
group,
indexes,
indexExists,
indexInformation,
parallelCollectionScan,
save
};
+55
View File
@@ -0,0 +1,55 @@
'use strict';
const OperationBase = require('./operation').OperationBase;
const handleCallback = require('../utils').handleCallback;
let collection;
function loadCollection() {
if (!collection) {
collection = require('../collection');
}
return collection;
}
class CollectionsOperation extends OperationBase {
constructor(db, options) {
super(options);
this.db = db;
}
execute(callback) {
const db = this.db;
let options = this.options;
let Collection = loadCollection();
options = Object.assign({}, options, { nameOnly: true });
// Let's get the collection names
db.listCollections({}, options).toArray((err, documents) => {
if (err != null) return handleCallback(callback, err, null);
// Filter collections removing any illegal ones
documents = documents.filter(doc => {
return doc.name.indexOf('$') === -1;
});
// Return the collection objects
handleCallback(
callback,
null,
documents.map(d => {
return new Collection(
db,
db.s.topology,
db.databaseName,
d.name,
db.s.pkFactory,
db.s.options
);
})
);
});
}
}
module.exports = CollectionsOperation;
+120
View File
@@ -0,0 +1,120 @@
'use strict';
const Aspect = require('./operation').Aspect;
const OperationBase = require('./operation').OperationBase;
const applyWriteConcern = require('../utils').applyWriteConcern;
const debugOptions = require('../utils').debugOptions;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const ReadPreference = require('../core').ReadPreference;
const resolveReadPreference = require('../utils').resolveReadPreference;
const MongoDBNamespace = require('../utils').MongoDBNamespace;
const debugFields = [
'authSource',
'w',
'wtimeout',
'j',
'native_parser',
'forceServerObjectId',
'serializeFunctions',
'raw',
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bufferMaxEntries',
'numberOfRetries',
'retryMiliSeconds',
'readPreference',
'pkFactory',
'parentDb',
'promiseLibrary',
'noListener'
];
class CommandOperation extends OperationBase {
constructor(db, options, collection, command) {
super(options);
if (!this.hasAspect(Aspect.WRITE_OPERATION)) {
if (collection != null) {
this.options.readPreference = resolveReadPreference(collection, options);
} else {
this.options.readPreference = resolveReadPreference(db, options);
}
} else {
if (collection != null) {
applyWriteConcern(this.options, { db, coll: collection }, this.options);
} else {
applyWriteConcern(this.options, { db }, this.options);
}
this.options.readPreference = ReadPreference.primary;
}
this.db = db;
if (command != null) {
this.command = command;
}
if (collection != null) {
this.collection = collection;
}
}
_buildCommand() {
if (this.command != null) {
return this.command;
}
}
execute(callback) {
const db = this.db;
const options = Object.assign({}, this.options);
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
let command;
try {
command = this._buildCommand();
} catch (e) {
return callback(e);
}
// Get the db name we are executing against
const dbName = options.dbName || options.authdb || db.databaseName;
// Convert the readPreference if its not a write
if (this.hasAspect(Aspect.WRITE_OPERATION)) {
if (options.writeConcern && (!options.session || !options.session.inTransaction())) {
command.writeConcern = options.writeConcern;
}
}
// Debug information
if (db.s.logger.isDebug()) {
db.s.logger.debug(
`executing command ${JSON.stringify(
command
)} against ${dbName}.$cmd with options [${JSON.stringify(
debugOptions(debugFields, options)
)}]`
);
}
const namespace =
this.namespace != null ? this.namespace : new MongoDBNamespace(dbName, '$cmd');
// Execute command
db.s.topology.command(namespace, command, options, (err, result) => {
if (err) return handleCallback(callback, err);
if (options.full) return handleCallback(callback, null, result);
handleCallback(callback, null, result.result);
});
}
}
module.exports = CommandOperation;
+109
View File
@@ -0,0 +1,109 @@
'use strict';
const Aspect = require('./operation').Aspect;
const OperationBase = require('./operation').OperationBase;
const resolveReadPreference = require('../utils').resolveReadPreference;
const ReadConcern = require('../read_concern');
const WriteConcern = require('../write_concern');
const maxWireVersion = require('../core/utils').maxWireVersion;
const commandSupportsReadConcern = require('../core/sessions').commandSupportsReadConcern;
const MongoError = require('../error').MongoError;
const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;
class CommandOperationV2 extends OperationBase {
constructor(parent, options, operationOptions) {
super(options);
this.ns = parent.s.namespace.withCollection('$cmd');
this.readPreference = resolveReadPreference(parent, this.options);
this.readConcern = resolveReadConcern(parent, this.options);
this.writeConcern = resolveWriteConcern(parent, this.options);
this.explain = false;
if (operationOptions && typeof operationOptions.fullResponse === 'boolean') {
this.fullResponse = true;
}
// TODO: A lot of our code depends on having the read preference in the options. This should
// go away, but also requires massive test rewrites.
this.options.readPreference = this.readPreference;
// TODO(NODE-2056): make logger another "inheritable" property
if (parent.s.logger) {
this.logger = parent.s.logger;
} else if (parent.s.db && parent.s.db.logger) {
this.logger = parent.s.db.logger;
}
}
executeCommand(server, cmd, callback) {
// TODO: consider making this a non-enumerable property
this.server = server;
const options = this.options;
const serverWireVersion = maxWireVersion(server);
const inTransaction = this.session && this.session.inTransaction();
if (this.readConcern && commandSupportsReadConcern(cmd) && !inTransaction) {
Object.assign(cmd, { readConcern: this.readConcern });
}
if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
callback(
new MongoError(
`Server ${
server.name
}, which reports wire version ${serverWireVersion}, does not support collation`
)
);
return;
}
if (serverWireVersion >= SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION)) {
Object.assign(cmd, { writeConcern: this.writeConcern });
}
if (options.collation && typeof options.collation === 'object') {
Object.assign(cmd, { collation: options.collation });
}
}
if (typeof options.maxTimeMS === 'number') {
cmd.maxTimeMS = options.maxTimeMS;
}
if (typeof options.comment === 'string') {
cmd.comment = options.comment;
}
if (this.logger && this.logger.isDebug()) {
this.logger.debug(`executing command ${JSON.stringify(cmd)} against ${this.ns}`);
}
server.command(this.ns.toString(), cmd, this.options, (err, result) => {
if (err) {
callback(err, null);
return;
}
if (this.fullResponse) {
callback(null, result);
return;
}
callback(null, result.result);
});
}
}
function resolveWriteConcern(parent, options) {
return WriteConcern.fromOptions(options) || parent.writeConcern;
}
function resolveReadConcern(parent, options) {
return ReadConcern.fromOptions(options) || parent.readConcern;
}
module.exports = CommandOperationV2;
+406
View File
@@ -0,0 +1,406 @@
'use strict';
const applyRetryableWrites = require('../utils').applyRetryableWrites;
const applyWriteConcern = require('../utils').applyWriteConcern;
const decorateWithCollation = require('../utils').decorateWithCollation;
const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
const executeCommand = require('./db_ops').executeCommand;
const formattedOrderClause = require('../utils').formattedOrderClause;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const ReadPreference = require('../core').ReadPreference;
const toError = require('../utils').toError;
const CursorState = require('../core/cursor').CursorState;
/**
* Build the count command.
*
* @method
* @param {collectionOrCursor} an instance of a collection or cursor
* @param {object} query The query for the count.
* @param {object} [options] Optional settings. See Collection.prototype.count and Cursor.prototype.count for a list of options.
*/
function buildCountCommand(collectionOrCursor, query, options) {
const skip = options.skip;
const limit = options.limit;
let hint = options.hint;
const maxTimeMS = options.maxTimeMS;
query = query || {};
// Final query
const cmd = {
count: options.collectionName,
query: query
};
if (collectionOrCursor.s.numberOfRetries) {
// collectionOrCursor is a cursor
if (collectionOrCursor.options.hint) {
hint = collectionOrCursor.options.hint;
} else if (collectionOrCursor.cmd.hint) {
hint = collectionOrCursor.cmd.hint;
}
decorateWithCollation(cmd, collectionOrCursor, collectionOrCursor.cmd);
} else {
decorateWithCollation(cmd, collectionOrCursor, options);
}
// Add limit, skip and maxTimeMS if defined
if (typeof skip === 'number') cmd.skip = skip;
if (typeof limit === 'number') cmd.limit = limit;
if (typeof maxTimeMS === 'number') cmd.maxTimeMS = maxTimeMS;
if (hint) cmd.hint = hint;
// Do we have a readConcern specified
decorateWithReadConcern(cmd, collectionOrCursor);
return cmd;
}
function deleteCallback(err, r, callback) {
if (callback == null) return;
if (err && callback) return callback(err);
if (r == null) return callback(null, { result: { ok: 1 } });
r.deletedCount = r.result.n;
if (callback) callback(null, r);
}
/**
* Find and update a document.
*
* @method
* @param {Collection} a Collection instance.
* @param {object} query Query object to locate the object to modify.
* @param {array} sort If multiple docs match, choose the first one in the specified sort order as the object to manipulate.
* @param {object} doc The fields/vals to be updated.
* @param {object} [options] Optional settings. See Collection.prototype.findAndModify for a list of options.
* @param {Collection~findAndModifyCallback} [callback] The command result callback
* @deprecated use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead
*/
function findAndModify(coll, query, sort, doc, options, callback) {
// Create findAndModify command object
const queryObject = {
findAndModify: coll.collectionName,
query: query
};
sort = formattedOrderClause(sort);
if (sort) {
queryObject.sort = sort;
}
queryObject.new = options.new ? true : false;
queryObject.remove = options.remove ? true : false;
queryObject.upsert = options.upsert ? true : false;
const projection = options.projection || options.fields;
if (projection) {
queryObject.fields = projection;
}
if (options.arrayFilters) {
queryObject.arrayFilters = options.arrayFilters;
delete options.arrayFilters;
}
if (doc && !options.remove) {
queryObject.update = doc;
}
if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;
// Either use override on the function, or go back to default on either the collection
// level or db
options.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
// No check on the documents
options.checkKeys = false;
// Final options for retryable writes and write concern
let finalOptions = Object.assign({}, options);
finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
// Decorate the findAndModify command with the write Concern
if (finalOptions.writeConcern) {
queryObject.writeConcern = finalOptions.writeConcern;
}
// Have we specified bypassDocumentValidation
if (finalOptions.bypassDocumentValidation === true) {
queryObject.bypassDocumentValidation = finalOptions.bypassDocumentValidation;
}
finalOptions.readPreference = ReadPreference.primary;
// Have we specified collation
try {
decorateWithCollation(queryObject, coll, finalOptions);
} catch (err) {
return callback(err, null);
}
// Execute the command
executeCommand(coll.s.db, queryObject, finalOptions, (err, result) => {
if (err) return handleCallback(callback, err, null);
return handleCallback(callback, null, result);
});
}
/**
* Retrieves this collections index info.
*
* @method
* @param {Db} db The Db instance on which to retrieve the index info.
* @param {string} name The name of the collection.
* @param {object} [options] Optional settings. See Db.prototype.indexInformation for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function indexInformation(db, name, options, callback) {
// If we specified full information
const full = options['full'] == null ? false : options['full'];
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed())
return callback(new MongoError('topology was destroyed'));
// Process all the results from the index command and collection
function processResults(indexes) {
// Contains all the information
let info = {};
// Process all the indexes
for (let i = 0; i < indexes.length; i++) {
const index = indexes[i];
// Let's unpack the object
info[index.name] = [];
for (let name in index.key) {
info[index.name].push([name, index.key[name]]);
}
}
return info;
}
// Get the list of indexes of the specified collection
db
.collection(name)
.listIndexes(options)
.toArray((err, indexes) => {
if (err) return callback(toError(err));
if (!Array.isArray(indexes)) return handleCallback(callback, null, []);
if (full) return handleCallback(callback, null, indexes);
handleCallback(callback, null, processResults(indexes));
});
}
function prepareDocs(coll, docs, options) {
const forceServerObjectId =
typeof options.forceServerObjectId === 'boolean'
? options.forceServerObjectId
: coll.s.db.options.forceServerObjectId;
// no need to modify the docs if server sets the ObjectId
if (forceServerObjectId === true) {
return docs;
}
return docs.map(doc => {
if (forceServerObjectId !== true && doc._id == null) {
doc._id = coll.s.pkFactory.createPk();
}
return doc;
});
}
// Get the next available document from the cursor, returns null if no more documents are available.
function nextObject(cursor, callback) {
if (cursor.s.state === CursorState.CLOSED || (cursor.isDead && cursor.isDead())) {
return handleCallback(
callback,
MongoError.create({ message: 'Cursor is closed', driver: true })
);
}
if (cursor.s.state === CursorState.INIT && cursor.cmd && cursor.cmd.sort) {
try {
cursor.cmd.sort = formattedOrderClause(cursor.cmd.sort);
} catch (err) {
return handleCallback(callback, err);
}
}
// Get the next object
cursor._next((err, doc) => {
cursor.s.state = CursorState.OPEN;
if (err) return handleCallback(callback, err);
handleCallback(callback, null, doc);
});
}
function insertDocuments(coll, docs, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
// Ensure we are operating on an array op docs
docs = Array.isArray(docs) ? docs : [docs];
// Final options for retryable writes and write concern
let finalOptions = Object.assign({}, options);
finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
// If keep going set unordered
if (finalOptions.keepGoing === true) finalOptions.ordered = false;
finalOptions.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
docs = prepareDocs(coll, docs, options);
// File inserts
coll.s.topology.insert(coll.s.namespace, docs, finalOptions, (err, result) => {
if (callback == null) return;
if (err) return handleCallback(callback, err);
if (result == null) return handleCallback(callback, null, null);
if (result.result.code) return handleCallback(callback, toError(result.result));
if (result.result.writeErrors)
return handleCallback(callback, toError(result.result.writeErrors[0]));
// Add docs to the list
result.ops = docs;
// Return the results
handleCallback(callback, null, result);
});
}
function removeDocuments(coll, selector, options, callback) {
if (typeof options === 'function') {
(callback = options), (options = {});
} else if (typeof selector === 'function') {
callback = selector;
options = {};
selector = {};
}
// Create an empty options object if the provided one is null
options = options || {};
// Final options for retryable writes and write concern
let finalOptions = Object.assign({}, options);
finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
// If selector is null set empty
if (selector == null) selector = {};
// Build the op
const op = { q: selector, limit: 0 };
if (options.single) {
op.limit = 1;
} else if (finalOptions.retryWrites) {
finalOptions.retryWrites = false;
}
// Have we specified collation
try {
decorateWithCollation(finalOptions, coll, options);
} catch (err) {
return callback(err, null);
}
// Execute the remove
coll.s.topology.remove(coll.s.namespace, [op], finalOptions, (err, result) => {
if (callback == null) return;
if (err) return handleCallback(callback, err, null);
if (result == null) return handleCallback(callback, null, null);
if (result.result.code) return handleCallback(callback, toError(result.result));
if (result.result.writeErrors) {
return handleCallback(callback, toError(result.result.writeErrors[0]));
}
// Return the results
handleCallback(callback, null, result);
});
}
function updateDocuments(coll, selector, document, options, callback) {
if ('function' === typeof options) (callback = options), (options = null);
if (options == null) options = {};
if (!('function' === typeof callback)) callback = null;
// If we are not providing a selector or document throw
if (selector == null || typeof selector !== 'object')
return callback(toError('selector must be a valid JavaScript object'));
if (document == null || typeof document !== 'object')
return callback(toError('document must be a valid JavaScript object'));
// Final options for retryable writes and write concern
let finalOptions = Object.assign({}, options);
finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
// Do we return the actual result document
// Either use override on the function, or go back to default on either the collection
// level or db
finalOptions.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
// Execute the operation
const op = { q: selector, u: document };
op.upsert = options.upsert !== void 0 ? !!options.upsert : false;
op.multi = options.multi !== void 0 ? !!options.multi : false;
if (finalOptions.arrayFilters) {
op.arrayFilters = finalOptions.arrayFilters;
delete finalOptions.arrayFilters;
}
if (finalOptions.retryWrites && op.multi) {
finalOptions.retryWrites = false;
}
// Have we specified collation
try {
decorateWithCollation(finalOptions, coll, options);
} catch (err) {
return callback(err, null);
}
// Update options
coll.s.topology.update(coll.s.namespace, [op], finalOptions, (err, result) => {
if (callback == null) return;
if (err) return handleCallback(callback, err, null);
if (result == null) return handleCallback(callback, null, null);
if (result.result.code) return handleCallback(callback, toError(result.result));
if (result.result.writeErrors)
return handleCallback(callback, toError(result.result.writeErrors[0]));
// Return the results
handleCallback(callback, null, result);
});
}
function updateCallback(err, r, callback) {
if (callback == null) return;
if (err) return callback(err);
if (r == null) return callback(null, { result: { ok: 1 } });
r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
r.upsertedId =
Array.isArray(r.result.upserted) && r.result.upserted.length > 0
? r.result.upserted[0] // FIXME(major): should be `r.result.upserted[0]._id`
: null;
r.upsertedCount =
Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
r.matchedCount =
Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? 0 : r.result.n;
callback(null, r);
}
module.exports = {
buildCountCommand,
deleteCallback,
findAndModify,
indexInformation,
nextObject,
prepareDocs,
insertDocuments,
removeDocuments,
updateDocuments,
updateCallback
};
+706
View File
@@ -0,0 +1,706 @@
'use strict';
const OperationBase = require('./operation').OperationBase;
const defineAspects = require('./operation').defineAspects;
const Aspect = require('./operation').Aspect;
const deprecate = require('util').deprecate;
const Logger = require('../core').Logger;
const MongoCredentials = require('../core').MongoCredentials;
const MongoError = require('../core').MongoError;
const Mongos = require('../topologies/mongos');
const NativeTopology = require('../topologies/native_topology');
const parse = require('../core').parseConnectionString;
const ReadConcern = require('../read_concern');
const ReadPreference = require('../core').ReadPreference;
const ReplSet = require('../topologies/replset');
const Server = require('../topologies/server');
const ServerSessionPool = require('../core').Sessions.ServerSessionPool;
let client;
function loadClient() {
if (!client) {
client = require('../mongo_client');
}
return client;
}
const legacyParse = deprecate(
require('../url_parser'),
'current URL string parser is deprecated, and will be removed in a future version. ' +
'To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.'
);
const AUTH_MECHANISM_INTERNAL_MAP = {
DEFAULT: 'default',
'MONGODB-CR': 'mongocr',
PLAIN: 'plain',
'MONGODB-X509': 'x509',
'SCRAM-SHA-1': 'scram-sha-1',
'SCRAM-SHA-256': 'scram-sha-256'
};
const monitoringEvents = [
'timeout',
'close',
'serverOpening',
'serverDescriptionChanged',
'serverHeartbeatStarted',
'serverHeartbeatSucceeded',
'serverHeartbeatFailed',
'serverClosed',
'topologyOpening',
'topologyClosed',
'topologyDescriptionChanged',
'commandStarted',
'commandSucceeded',
'commandFailed',
'joined',
'left',
'ping',
'ha',
'all',
'fullsetup',
'open'
];
const VALID_AUTH_MECHANISMS = new Set([
'DEFAULT',
'MONGODB-CR',
'PLAIN',
'MONGODB-X509',
'SCRAM-SHA-1',
'SCRAM-SHA-256',
'GSSAPI'
]);
const validOptionNames = [
'poolSize',
'ssl',
'sslValidate',
'sslCA',
'sslCert',
'sslKey',
'sslPass',
'sslCRL',
'autoReconnect',
'noDelay',
'keepAlive',
'keepAliveInitialDelay',
'connectTimeoutMS',
'family',
'socketTimeoutMS',
'reconnectTries',
'reconnectInterval',
'ha',
'haInterval',
'replicaSet',
'secondaryAcceptableLatencyMS',
'acceptableLatencyMS',
'connectWithNoPrimary',
'authSource',
'w',
'wtimeout',
'j',
'forceServerObjectId',
'serializeFunctions',
'ignoreUndefined',
'raw',
'bufferMaxEntries',
'readPreference',
'pkFactory',
'promiseLibrary',
'readConcern',
'maxStalenessSeconds',
'loggerLevel',
'logger',
'promoteValues',
'promoteBuffers',
'promoteLongs',
'domainsEnabled',
'checkServerIdentity',
'validateOptions',
'appname',
'auth',
'user',
'password',
'authMechanism',
'compression',
'fsync',
'readPreferenceTags',
'numberOfRetries',
'auto_reconnect',
'minSize',
'monitorCommands',
'retryWrites',
'retryReads',
'useNewUrlParser',
'useUnifiedTopology',
'serverSelectionTimeoutMS',
'useRecoveryToken',
'autoEncryption'
];
const ignoreOptionNames = ['native_parser'];
const legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db'];
// Validate options object
function validOptions(options) {
const _validOptions = validOptionNames.concat(legacyOptionNames);
for (const name in options) {
if (ignoreOptionNames.indexOf(name) !== -1) {
continue;
}
if (_validOptions.indexOf(name) === -1) {
if (options.validateOptions) {
return new MongoError(`option ${name} is not supported`);
} else {
console.warn(`the options [${name}] is not supported`);
}
}
if (legacyOptionNames.indexOf(name) !== -1) {
console.warn(
`the server/replset/mongos/db options are deprecated, ` +
`all their options are supported at the top level of the options object [${validOptionNames}]`
);
}
}
}
const LEGACY_OPTIONS_MAP = validOptionNames.reduce((obj, name) => {
obj[name.toLowerCase()] = name;
return obj;
}, {});
class ConnectOperation extends OperationBase {
constructor(mongoClient) {
super();
this.mongoClient = mongoClient;
}
execute(callback) {
const mongoClient = this.mongoClient;
const err = validOptions(mongoClient.s.options);
// Did we have a validation error
if (err) return callback(err);
// Fallback to callback based connect
connect(mongoClient, mongoClient.s.url, mongoClient.s.options, err => {
if (err) return callback(err);
callback(null, mongoClient);
});
}
}
defineAspects(ConnectOperation, [Aspect.SKIP_SESSION]);
function addListeners(mongoClient, topology) {
topology.on('authenticated', createListener(mongoClient, 'authenticated'));
topology.on('error', createListener(mongoClient, 'error'));
topology.on('timeout', createListener(mongoClient, 'timeout'));
topology.on('close', createListener(mongoClient, 'close'));
topology.on('parseError', createListener(mongoClient, 'parseError'));
topology.once('open', createListener(mongoClient, 'open'));
topology.once('fullsetup', createListener(mongoClient, 'fullsetup'));
topology.once('all', createListener(mongoClient, 'all'));
topology.on('reconnect', createListener(mongoClient, 'reconnect'));
}
function assignTopology(client, topology) {
client.topology = topology;
topology.s.sessionPool =
topology instanceof NativeTopology
? new ServerSessionPool(topology)
: new ServerSessionPool(topology.s.coreTopology);
}
// Clear out all events
function clearAllEvents(topology) {
monitoringEvents.forEach(event => topology.removeAllListeners(event));
}
// Collect all events in order from SDAM
function collectEvents(mongoClient, topology) {
let MongoClient = loadClient();
const collectedEvents = [];
if (mongoClient instanceof MongoClient) {
monitoringEvents.forEach(event => {
topology.on(event, (object1, object2) => {
if (event === 'open') {
collectedEvents.push({ event: event, object1: mongoClient });
} else {
collectedEvents.push({ event: event, object1: object1, object2: object2 });
}
});
});
}
return collectedEvents;
}
const emitDeprecationForNonUnifiedTopology = deprecate(() => {},
'current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. ' + 'To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.');
function connect(mongoClient, url, options, callback) {
options = Object.assign({}, options);
// If callback is null throw an exception
if (callback == null) {
throw new Error('no callback function provided');
}
let didRequestAuthentication = false;
const logger = Logger('MongoClient', options);
// Did we pass in a Server/ReplSet/Mongos
if (url instanceof Server || url instanceof ReplSet || url instanceof Mongos) {
return connectWithUrl(mongoClient, url, options, connectCallback);
}
const parseFn = options.useNewUrlParser ? parse : legacyParse;
const transform = options.useNewUrlParser ? transformUrlOptions : legacyTransformUrlOptions;
parseFn(url, options, (err, _object) => {
// Do not attempt to connect if parsing error
if (err) return callback(err);
// Flatten
const object = transform(_object);
// Parse the string
const _finalOptions = createUnifiedOptions(object, options);
// Check if we have connection and socket timeout set
if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
if (_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;
if (_finalOptions.retryWrites == null) _finalOptions.retryWrites = true;
if (_finalOptions.useRecoveryToken == null) _finalOptions.useRecoveryToken = true;
if (_finalOptions.db_options && _finalOptions.db_options.auth) {
delete _finalOptions.db_options.auth;
}
// Store the merged options object
mongoClient.s.options = _finalOptions;
// Failure modes
if (object.servers.length === 0) {
return callback(new Error('connection string must contain at least one seed host'));
}
if (_finalOptions.auth && !_finalOptions.credentials) {
try {
didRequestAuthentication = true;
_finalOptions.credentials = generateCredentials(
mongoClient,
_finalOptions.auth.user,
_finalOptions.auth.password,
_finalOptions
);
} catch (err) {
return callback(err);
}
}
if (_finalOptions.useUnifiedTopology) {
return createTopology(mongoClient, 'unified', _finalOptions, connectCallback);
}
emitDeprecationForNonUnifiedTopology();
// Do we have a replicaset then skip discovery and go straight to connectivity
if (_finalOptions.replicaSet || _finalOptions.rs_name) {
return createTopology(mongoClient, 'replicaset', _finalOptions, connectCallback);
} else if (object.servers.length > 1) {
return createTopology(mongoClient, 'mongos', _finalOptions, connectCallback);
} else {
return createServer(mongoClient, _finalOptions, connectCallback);
}
});
function connectCallback(err, topology) {
const warningMessage = `seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name`;
if (err && err.message === 'no mongos proxies found in seed list') {
if (logger.isWarn()) {
logger.warn(warningMessage);
}
// Return a more specific error message for MongoClient.connect
return callback(new MongoError(warningMessage));
}
if (didRequestAuthentication) {
mongoClient.emit('authenticated', null, true);
}
// Return the error and db instance
callback(err, topology);
}
}
function connectWithUrl(mongoClient, url, options, connectCallback) {
// Set the topology
assignTopology(mongoClient, url);
// Add listeners
addListeners(mongoClient, url);
// Propagate the events to the client
relayEvents(mongoClient, url);
let finalOptions = Object.assign({}, options);
// If we have a readPreference passed in by the db options, convert it from a string
if (typeof options.readPreference === 'string' || typeof options.read_preference === 'string') {
finalOptions.readPreference = new ReadPreference(
options.readPreference || options.read_preference
);
}
const isDoingAuth = finalOptions.user || finalOptions.password || finalOptions.authMechanism;
if (isDoingAuth && !finalOptions.credentials) {
try {
finalOptions.credentials = generateCredentials(
mongoClient,
finalOptions.user,
finalOptions.password,
finalOptions
);
} catch (err) {
return connectCallback(err, url);
}
}
return url.connect(finalOptions, connectCallback);
}
function createListener(mongoClient, event) {
const eventSet = new Set(['all', 'fullsetup', 'open', 'reconnect']);
return (v1, v2) => {
if (eventSet.has(event)) {
return mongoClient.emit(event, mongoClient);
}
mongoClient.emit(event, v1, v2);
};
}
function createServer(mongoClient, options, callback) {
// Pass in the promise library
options.promiseLibrary = mongoClient.s.promiseLibrary;
// Set default options
const servers = translateOptions(options);
const server = servers[0];
// Propagate the events to the client
const collectedEvents = collectEvents(mongoClient, server);
// Connect to topology
server.connect(options, (err, topology) => {
if (err) {
server.close(true);
return callback(err);
}
// Clear out all the collected event listeners
clearAllEvents(server);
// Relay all the events
relayEvents(mongoClient, server);
// Add listeners
addListeners(mongoClient, server);
// Check if we are really speaking to a mongos
const ismaster = topology.lastIsMaster();
// Set the topology
assignTopology(mongoClient, topology);
// Do we actually have a mongos
if (ismaster && ismaster.msg === 'isdbgrid') {
// Destroy the current connection
topology.close();
// Create mongos connection instead
return createTopology(mongoClient, 'mongos', options, callback);
}
// Fire all the events
replayEvents(mongoClient, collectedEvents);
// Otherwise callback
callback(err, topology);
});
}
function createTopology(mongoClient, topologyType, options, callback) {
// Pass in the promise library
options.promiseLibrary = mongoClient.s.promiseLibrary;
const translationOptions = {};
if (topologyType === 'unified') translationOptions.createServers = false;
// Set default options
const servers = translateOptions(options, translationOptions);
// Create the topology
let topology;
if (topologyType === 'mongos') {
topology = new Mongos(servers, options);
} else if (topologyType === 'replicaset') {
topology = new ReplSet(servers, options);
} else if (topologyType === 'unified') {
topology = new NativeTopology(options.servers, options);
}
// Add listeners
addListeners(mongoClient, topology);
// Propagate the events to the client
relayEvents(mongoClient, topology);
// Open the connection
topology.connect(options, (err, newTopology) => {
if (err) {
topology.close(true);
return callback(err);
}
assignTopology(mongoClient, newTopology);
if (options.autoEncryption == null) {
callback(null, newTopology);
return;
}
// setup for client side encryption
let AutoEncrypter;
try {
require.resolve('mongodb-client-encryption');
} catch (err) {
callback(
new MongoError(
'Auto-encryption requested, but the module is not installed. Please add `mongodb-client-encryption` as a dependency of your project'
)
);
return;
}
try {
AutoEncrypter = require('mongodb-client-encryption')(require('../../index')).AutoEncrypter;
} catch (err) {
callback(err);
return;
}
const mongoCryptOptions = Object.assign({}, options.autoEncryption);
topology.s.options.autoEncrypter = new AutoEncrypter(mongoClient, mongoCryptOptions);
topology.s.options.autoEncrypter.init(err => {
if (err) return callback(err, null);
callback(null, newTopology);
});
});
}
function createUnifiedOptions(finalOptions, options) {
const childOptions = [
'mongos',
'server',
'db',
'replset',
'db_options',
'server_options',
'rs_options',
'mongos_options'
];
const noMerge = ['readconcern', 'compression'];
for (const name in options) {
if (noMerge.indexOf(name.toLowerCase()) !== -1) {
finalOptions[name] = options[name];
} else if (childOptions.indexOf(name.toLowerCase()) !== -1) {
finalOptions = mergeOptions(finalOptions, options[name], false);
} else {
if (
options[name] &&
typeof options[name] === 'object' &&
!Buffer.isBuffer(options[name]) &&
!Array.isArray(options[name])
) {
finalOptions = mergeOptions(finalOptions, options[name], true);
} else {
finalOptions[name] = options[name];
}
}
}
return finalOptions;
}
function generateCredentials(client, username, password, options) {
options = Object.assign({}, options);
// the default db to authenticate against is 'self'
// if authententicate is called from a retry context, it may be another one, like admin
const source = options.authSource || options.authdb || options.dbName;
// authMechanism
const authMechanismRaw = options.authMechanism || 'DEFAULT';
const authMechanism = authMechanismRaw.toUpperCase();
if (!VALID_AUTH_MECHANISMS.has(authMechanism)) {
throw MongoError.create({
message: `authentication mechanism ${authMechanismRaw} not supported', options.authMechanism`,
driver: true
});
}
if (authMechanism === 'GSSAPI') {
return new MongoCredentials({
mechanism: process.platform === 'win32' ? 'sspi' : 'gssapi',
mechanismProperties: options,
source,
username,
password
});
}
return new MongoCredentials({
mechanism: AUTH_MECHANISM_INTERNAL_MAP[authMechanism],
source,
username,
password
});
}
function legacyTransformUrlOptions(object) {
return mergeOptions(createUnifiedOptions({}, object), object, false);
}
function mergeOptions(target, source, flatten) {
for (const name in source) {
if (source[name] && typeof source[name] === 'object' && flatten) {
target = mergeOptions(target, source[name], flatten);
} else {
target[name] = source[name];
}
}
return target;
}
function relayEvents(mongoClient, topology) {
const serverOrCommandEvents = [
'serverOpening',
'serverDescriptionChanged',
'serverHeartbeatStarted',
'serverHeartbeatSucceeded',
'serverHeartbeatFailed',
'serverClosed',
'topologyOpening',
'topologyClosed',
'topologyDescriptionChanged',
'commandStarted',
'commandSucceeded',
'commandFailed',
'joined',
'left',
'ping',
'ha'
];
serverOrCommandEvents.forEach(event => {
topology.on(event, (object1, object2) => {
mongoClient.emit(event, object1, object2);
});
});
}
//
// Replay any events due to single server connection switching to Mongos
//
function replayEvents(mongoClient, events) {
for (let i = 0; i < events.length; i++) {
mongoClient.emit(events[i].event, events[i].object1, events[i].object2);
}
}
function transformUrlOptions(_object) {
let object = Object.assign({ servers: _object.hosts }, _object.options);
for (let name in object) {
const camelCaseName = LEGACY_OPTIONS_MAP[name];
if (camelCaseName) {
object[camelCaseName] = object[name];
}
}
const hasUsername = _object.auth && _object.auth.username;
const hasAuthMechanism = _object.options && _object.options.authMechanism;
if (hasUsername || hasAuthMechanism) {
object.auth = Object.assign({}, _object.auth);
if (object.auth.db) {
object.authSource = object.authSource || object.auth.db;
}
if (object.auth.username) {
object.auth.user = object.auth.username;
}
}
if (_object.defaultDatabase) {
object.dbName = _object.defaultDatabase;
}
if (object.maxpoolsize) {
object.poolSize = object.maxpoolsize;
}
if (object.readconcernlevel) {
object.readConcern = new ReadConcern(object.readconcernlevel);
}
if (object.wtimeoutms) {
object.wtimeout = object.wtimeoutms;
}
if (_object.srvHost) {
object.srvHost = _object.srvHost;
}
return object;
}
function translateOptions(options, translationOptions) {
translationOptions = Object.assign({}, { createServers: true }, translationOptions);
// If we have a readPreference passed in by the db options
if (typeof options.readPreference === 'string' || typeof options.read_preference === 'string') {
options.readPreference = new ReadPreference(options.readPreference || options.read_preference);
}
// Do we have readPreference tags, add them
if (options.readPreference && (options.readPreferenceTags || options.read_preference_tags)) {
options.readPreference.tags = options.readPreferenceTags || options.read_preference_tags;
}
// Do we have maxStalenessSeconds
if (options.maxStalenessSeconds) {
options.readPreference.maxStalenessSeconds = options.maxStalenessSeconds;
}
// Set the socket and connection timeouts
if (options.socketTimeoutMS == null) options.socketTimeoutMS = 360000;
if (options.connectTimeoutMS == null) options.connectTimeoutMS = 30000;
if (!translationOptions.createServers) {
return;
}
// Create server instances
return options.servers.map(serverObj => {
return serverObj.domain_socket
? new Server(serverObj.domain_socket, 27017, options)
: new Server(serverObj.host, serverObj.port, options);
});
}
module.exports = ConnectOperation;
+72
View File
@@ -0,0 +1,72 @@
'use strict';
const Aspect = require('./operation').Aspect;
const buildCountCommand = require('./common_functions').buildCountCommand;
const defineAspects = require('./operation').defineAspects;
const OperationBase = require('./operation').OperationBase;
class CountOperation extends OperationBase {
constructor(cursor, applySkipLimit, options) {
super(options);
this.cursor = cursor;
this.applySkipLimit = applySkipLimit;
}
execute(callback) {
const cursor = this.cursor;
const applySkipLimit = this.applySkipLimit;
const options = this.options;
if (applySkipLimit) {
if (typeof cursor.cursorSkip() === 'number') options.skip = cursor.cursorSkip();
if (typeof cursor.cursorLimit() === 'number') options.limit = cursor.cursorLimit();
}
// Ensure we have the right read preference inheritance
if (options.readPreference) {
cursor.setReadPreference(options.readPreference);
}
if (
typeof options.maxTimeMS !== 'number' &&
cursor.cmd &&
typeof cursor.cmd.maxTimeMS === 'number'
) {
options.maxTimeMS = cursor.cmd.maxTimeMS;
}
let finalOptions = {};
finalOptions.skip = options.skip;
finalOptions.limit = options.limit;
finalOptions.hint = options.hint;
finalOptions.maxTimeMS = options.maxTimeMS;
// Command
finalOptions.collectionName = cursor.namespace.collection;
let command;
try {
command = buildCountCommand(cursor, cursor.cmd.query, finalOptions);
} catch (err) {
return callback(err);
}
// Set cursor server to the same as the topology
cursor.server = cursor.topology.s.coreTopology;
// Execute the command
cursor.topology.command(
cursor.namespace.withCollection('$cmd'),
command,
cursor.options,
(err, result) => {
callback(err, result ? result.result.n : null);
}
);
}
}
defineAspects(CountOperation, Aspect.SKIP_SESSION);
module.exports = CountOperation;
+41
View File
@@ -0,0 +1,41 @@
'use strict';
const AggregateOperation = require('./aggregate');
class CountDocumentsOperation extends AggregateOperation {
constructor(collection, query, options) {
const pipeline = [{ $match: query }];
if (typeof options.skip === 'number') {
pipeline.push({ $skip: options.skip });
}
if (typeof options.limit === 'number') {
pipeline.push({ $limit: options.limit });
}
pipeline.push({ $group: { _id: 1, n: { $sum: 1 } } });
super(collection, pipeline, options);
}
execute(server, callback) {
super.execute(server, (err, result) => {
if (err) {
callback(err, null);
return;
}
// NOTE: We're avoiding creating a cursor here to reduce the callstack.
const response = result.result;
if (response.cursor == null || response.cursor.firstBatch == null) {
callback(null, 0);
return;
}
const docs = response.cursor.firstBatch;
callback(null, docs.length ? docs[0].n : 0);
});
}
}
module.exports = CountDocumentsOperation;
+118
View File
@@ -0,0 +1,118 @@
'use strict';
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const CommandOperation = require('./command');
const applyWriteConcern = require('../utils').applyWriteConcern;
const handleCallback = require('../utils').handleCallback;
const loadCollection = require('../dynamic_loaders').loadCollection;
const MongoError = require('../core').MongoError;
const ReadPreference = require('../core').ReadPreference;
// Filter out any write concern options
const illegalCommandFields = [
'w',
'wtimeout',
'j',
'fsync',
'autoIndexId',
'strict',
'serializeFunctions',
'pkFactory',
'raw',
'readPreference',
'session',
'readConcern',
'writeConcern'
];
class CreateCollectionOperation extends CommandOperation {
constructor(db, name, options) {
super(db, options);
this.name = name;
}
_buildCommand() {
const name = this.name;
const options = this.options;
// Create collection command
const cmd = { create: name };
// Add all optional parameters
for (let n in options) {
if (
options[n] != null &&
typeof options[n] !== 'function' &&
illegalCommandFields.indexOf(n) === -1
) {
cmd[n] = options[n];
}
}
return cmd;
}
execute(callback) {
const db = this.db;
const name = this.name;
const options = this.options;
let Collection = loadCollection();
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
let listCollectionOptions = Object.assign({}, options, { nameOnly: true });
listCollectionOptions = applyWriteConcern(listCollectionOptions, { db }, listCollectionOptions);
// Check if we have the name
db
.listCollections({ name }, listCollectionOptions)
.setReadPreference(ReadPreference.PRIMARY)
.toArray((err, collections) => {
if (err != null) return handleCallback(callback, err, null);
if (collections.length > 0 && listCollectionOptions.strict) {
return handleCallback(
callback,
MongoError.create({
message: `Collection ${name} already exists. Currently in strict mode.`,
driver: true
}),
null
);
} else if (collections.length > 0) {
try {
return handleCallback(
callback,
null,
new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
);
} catch (err) {
return handleCallback(callback, err);
}
}
// Execute command
super.execute(err => {
if (err) return handleCallback(callback, err);
try {
return handleCallback(
callback,
null,
new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
);
} catch (err) {
return handleCallback(callback, err);
}
});
});
}
}
defineAspects(CreateCollectionOperation, Aspect.WRITE_OPERATION);
module.exports = CreateCollectionOperation;
+92
View File
@@ -0,0 +1,92 @@
'use strict';
const Aspect = require('./operation').Aspect;
const CommandOperation = require('./command');
const defineAspects = require('./operation').defineAspects;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const parseIndexOptions = require('../utils').parseIndexOptions;
const keysToOmit = new Set([
'name',
'key',
'writeConcern',
'w',
'wtimeout',
'j',
'fsync',
'readPreference',
'session'
]);
class CreateIndexOperation extends CommandOperation {
constructor(db, name, fieldOrSpec, options) {
super(db, options);
// Build the index
const indexParameters = parseIndexOptions(fieldOrSpec);
// Generate the index name
const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
// Set up the index
const indexesObject = { name: indexName, key: indexParameters.fieldHash };
this.name = name;
this.fieldOrSpec = fieldOrSpec;
this.indexes = indexesObject;
}
_buildCommand() {
const options = this.options;
const name = this.name;
const indexes = this.indexes;
// merge all the options
for (let optionName in options) {
if (!keysToOmit.has(optionName)) {
indexes[optionName] = options[optionName];
}
}
// Create command, apply write concern to command
const cmd = { createIndexes: name, indexes: [indexes] };
return cmd;
}
execute(callback) {
const db = this.db;
const options = this.options;
const indexes = this.indexes;
// Get capabilities
const capabilities = db.s.topology.capabilities();
// Did the user pass in a collation, check if our write server supports it
if (options.collation && capabilities && !capabilities.commandsTakeCollation) {
// Create a new error
const error = new MongoError('server/primary/mongos does not support collation');
error.code = 67;
// Return the error
return callback(error);
}
// Ensure we have a callback
if (options.writeConcern && typeof callback !== 'function') {
throw MongoError.create({
message: 'Cannot use a writeConcern without a provided callback',
driver: true
});
}
// Attempt to run using createIndexes command
super.execute((err, result) => {
if (err == null) return handleCallback(callback, err, indexes.name);
return handleCallback(callback, err, result);
});
}
}
defineAspects(CreateIndexOperation, Aspect.WRITE_OPERATION);
module.exports = CreateIndexOperation;
+61
View File
@@ -0,0 +1,61 @@
'use strict';
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const OperationBase = require('./operation').OperationBase;
const executeCommand = require('./db_ops').executeCommand;
const MongoError = require('../core').MongoError;
const ReadPreference = require('../core').ReadPreference;
class CreateIndexesOperation extends OperationBase {
constructor(collection, indexSpecs, options) {
super(options);
this.collection = collection;
this.indexSpecs = indexSpecs;
}
execute(callback) {
const coll = this.collection;
const indexSpecs = this.indexSpecs;
let options = this.options;
const capabilities = coll.s.topology.capabilities();
// Ensure we generate the correct name if the parameter is not set
for (let i = 0; i < indexSpecs.length; i++) {
if (indexSpecs[i].name == null) {
const keys = [];
// Did the user pass in a collation, check if our write server supports it
if (indexSpecs[i].collation && capabilities && !capabilities.commandsTakeCollation) {
return callback(new MongoError('server/primary/mongos does not support collation'));
}
for (let name in indexSpecs[i].key) {
keys.push(`${name}_${indexSpecs[i].key[name]}`);
}
// Set the name
indexSpecs[i].name = keys.join('_');
}
}
options = Object.assign({}, options, { readPreference: ReadPreference.PRIMARY });
// Execute the index
executeCommand(
coll.s.db,
{
createIndexes: coll.collectionName,
indexes: indexSpecs
},
options,
callback
);
}
}
defineAspects(CreateIndexesOperation, Aspect.WRITE_OPERATION);
module.exports = CreateIndexesOperation;
+239
View File
@@ -0,0 +1,239 @@
'use strict';
const buildCountCommand = require('./collection_ops').buildCountCommand;
const formattedOrderClause = require('../utils').formattedOrderClause;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const push = Array.prototype.push;
const CursorState = require('../core/cursor').CursorState;
/**
* Get the count of documents for this cursor.
*
* @method
* @param {Cursor} cursor The Cursor instance on which to count.
* @param {boolean} [applySkipLimit=true] Specifies whether the count command apply limit and skip settings should be applied on the cursor or in the provided options.
* @param {object} [options] Optional settings. See Cursor.prototype.count for a list of options.
* @param {Cursor~countResultCallback} [callback] The result callback.
*/
function count(cursor, applySkipLimit, opts, callback) {
if (applySkipLimit) {
if (typeof cursor.cursorSkip() === 'number') opts.skip = cursor.cursorSkip();
if (typeof cursor.cursorLimit() === 'number') opts.limit = cursor.cursorLimit();
}
// Ensure we have the right read preference inheritance
if (opts.readPreference) {
cursor.setReadPreference(opts.readPreference);
}
if (
typeof opts.maxTimeMS !== 'number' &&
cursor.cmd &&
typeof cursor.cmd.maxTimeMS === 'number'
) {
opts.maxTimeMS = cursor.cmd.maxTimeMS;
}
let options = {};
options.skip = opts.skip;
options.limit = opts.limit;
options.hint = opts.hint;
options.maxTimeMS = opts.maxTimeMS;
// Command
options.collectionName = cursor.namespace.collection;
let command;
try {
command = buildCountCommand(cursor, cursor.cmd.query, options);
} catch (err) {
return callback(err);
}
// Set cursor server to the same as the topology
cursor.server = cursor.topology.s.coreTopology;
// Execute the command
cursor.topology.command(
cursor.namespace.withCollection('$cmd'),
command,
cursor.options,
(err, result) => {
callback(err, result ? result.result.n : null);
}
);
}
/**
* Iterates over all the documents for this cursor. See Cursor.prototype.each for more information.
*
* @method
* @deprecated
* @param {Cursor} cursor The Cursor instance on which to run.
* @param {Cursor~resultCallback} callback The result callback.
*/
function each(cursor, callback) {
if (!callback) throw MongoError.create({ message: 'callback is mandatory', driver: true });
if (cursor.isNotified()) return;
if (cursor.s.state === CursorState.CLOSED || cursor.isDead()) {
return handleCallback(
callback,
MongoError.create({ message: 'Cursor is closed', driver: true })
);
}
if (cursor.s.state === CursorState.INIT) {
cursor.s.state = CursorState.OPEN;
}
// Define function to avoid global scope escape
let fn = null;
// Trampoline all the entries
if (cursor.bufferedCount() > 0) {
while ((fn = loop(cursor, callback))) fn(cursor, callback);
each(cursor, callback);
} else {
cursor.next((err, item) => {
if (err) return handleCallback(callback, err);
if (item == null) {
return cursor.close({ skipKillCursors: true }, () => handleCallback(callback, null, null));
}
if (handleCallback(callback, null, item) === false) return;
each(cursor, callback);
});
}
}
/**
* Check if there is any document still available in the cursor.
*
* @method
* @param {Cursor} cursor The Cursor instance on which to run.
* @param {Cursor~resultCallback} [callback] The result callback.
*/
function hasNext(cursor, callback) {
if (cursor.s.currentDoc) {
return callback(null, true);
}
if (cursor.isNotified()) {
return callback(null, false);
}
nextObject(cursor, (err, doc) => {
if (err) return callback(err, null);
if (cursor.s.state === CursorState.CLOSED || cursor.isDead()) {
return callback(null, false);
}
if (!doc) return callback(null, false);
cursor.s.currentDoc = doc;
callback(null, true);
});
}
// Trampoline emptying the number of retrieved items
// without incurring a nextTick operation
function loop(cursor, callback) {
// No more items we are done
if (cursor.bufferedCount() === 0) return;
// Get the next document
cursor._next(callback);
// Loop
return loop;
}
/**
* Get the next available document from the cursor. Returns null if no more documents are available.
*
* @method
* @param {Cursor} cursor The Cursor instance from which to get the next document.
* @param {Cursor~resultCallback} [callback] The result callback.
*/
function next(cursor, callback) {
// Return the currentDoc if someone called hasNext first
if (cursor.s.currentDoc) {
const doc = cursor.s.currentDoc;
cursor.s.currentDoc = null;
return callback(null, doc);
}
// Return the next object
nextObject(cursor, callback);
}
// Get the next available document from the cursor, returns null if no more documents are available.
function nextObject(cursor, callback) {
if (cursor.s.state === CursorState.CLOSED || (cursor.isDead && cursor.isDead()))
return handleCallback(
callback,
MongoError.create({ message: 'Cursor is closed', driver: true })
);
if (cursor.s.state === CursorState.INIT && cursor.cmd.sort) {
try {
cursor.cmd.sort = formattedOrderClause(cursor.cmd.sort);
} catch (err) {
return handleCallback(callback, err);
}
}
// Get the next object
cursor._next((err, doc) => {
cursor.s.state = CursorState.OPEN;
if (err) return handleCallback(callback, err);
handleCallback(callback, null, doc);
});
}
/**
* Returns an array of documents. See Cursor.prototype.toArray for more information.
*
* @method
* @param {Cursor} cursor The Cursor instance from which to get the next document.
* @param {Cursor~toArrayResultCallback} [callback] The result callback.
*/
function toArray(cursor, callback) {
const items = [];
// Reset cursor
cursor.rewind();
cursor.s.state = CursorState.INIT;
// Fetch all the documents
const fetchDocs = () => {
cursor._next((err, doc) => {
if (err) {
return cursor._endSession
? cursor._endSession(() => handleCallback(callback, err))
: handleCallback(callback, err);
}
if (doc == null) {
return cursor.close({ skipKillCursors: true }, () => handleCallback(callback, null, items));
}
// Add doc to items
items.push(doc);
// Get all buffered objects
if (cursor.bufferedCount() > 0) {
let docs = cursor.readBufferedDocuments(cursor.bufferedCount());
// Transform the doc if transform method added
if (cursor.s.transforms && typeof cursor.s.transforms.doc === 'function') {
docs = docs.map(cursor.s.transforms.doc);
}
push.apply(items, docs);
}
// Attempt a fetch
fetchDocs();
});
};
fetchDocs();
}
module.exports = { count, each, hasNext, next, toArray };
+831
View File
@@ -0,0 +1,831 @@
'use strict';
const applyWriteConcern = require('../utils').applyWriteConcern;
const Code = require('../core').BSON.Code;
const resolveReadPreference = require('../utils').resolveReadPreference;
const crypto = require('crypto');
const debugOptions = require('../utils').debugOptions;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const parseIndexOptions = require('../utils').parseIndexOptions;
const ReadPreference = require('../core').ReadPreference;
const toError = require('../utils').toError;
const CONSTANTS = require('../constants');
const MongoDBNamespace = require('../utils').MongoDBNamespace;
const count = require('./collection_ops').count;
const findOne = require('./collection_ops').findOne;
const remove = require('./collection_ops').remove;
const updateOne = require('./collection_ops').updateOne;
let collection;
function loadCollection() {
if (!collection) {
collection = require('../collection');
}
return collection;
}
let db;
function loadDb() {
if (!db) {
db = require('../db');
}
return db;
}
const debugFields = [
'authSource',
'w',
'wtimeout',
'j',
'native_parser',
'forceServerObjectId',
'serializeFunctions',
'raw',
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bufferMaxEntries',
'numberOfRetries',
'retryMiliSeconds',
'readPreference',
'pkFactory',
'parentDb',
'promiseLibrary',
'noListener'
];
/**
* Add a user to the database.
* @method
* @param {Db} db The Db instance on which to add a user.
* @param {string} username The username.
* @param {string} password The password.
* @param {object} [options] Optional settings. See Db.prototype.addUser for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function addUser(db, username, password, options, callback) {
let Db = loadDb();
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed())
return callback(new MongoError('topology was destroyed'));
// Attempt to execute auth command
executeAuthCreateUserCommand(db, username, password, options, (err, r) => {
// We need to perform the backward compatible insert operation
if (err && err.code === -5000) {
const finalOptions = applyWriteConcern(Object.assign({}, options), { db }, options);
// Use node md5 generator
const md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ':mongo:' + password);
const userPassword = md5.digest('hex');
// If we have another db set
const dbToUse = options.dbName ? new Db(options.dbName, db.s.topology, db.s.options) : db;
// Fetch a user collection
const collection = dbToUse.collection(CONSTANTS.SYSTEM_USER_COLLECTION);
// Check if we are inserting the first user
count(collection, {}, finalOptions, (err, count) => {
// We got an error (f.ex not authorized)
if (err != null) return handleCallback(callback, err, null);
// Check if the user exists and update i
const findOptions = Object.assign({ projection: { dbName: 1 } }, finalOptions);
collection.find({ user: username }, findOptions).toArray(err => {
// We got an error (f.ex not authorized)
if (err != null) return handleCallback(callback, err, null);
// Add command keys
finalOptions.upsert = true;
// We have a user, let's update the password or upsert if not
updateOne(
collection,
{ user: username },
{ $set: { user: username, pwd: userPassword } },
finalOptions,
err => {
if (count === 0 && err)
return handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
if (err) return handleCallback(callback, err, null);
handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
}
);
});
});
return;
}
if (err) return handleCallback(callback, err);
handleCallback(callback, err, r);
});
}
/**
* Fetch all collections for the current db.
*
* @method
* @param {Db} db The Db instance on which to fetch collections.
* @param {object} [options] Optional settings. See Db.prototype.collections for a list of options.
* @param {Db~collectionsResultCallback} [callback] The results callback
*/
function collections(db, options, callback) {
let Collection = loadCollection();
options = Object.assign({}, options, { nameOnly: true });
// Let's get the collection names
db.listCollections({}, options).toArray((err, documents) => {
if (err != null) return handleCallback(callback, err, null);
// Filter collections removing any illegal ones
documents = documents.filter(doc => {
return doc.name.indexOf('$') === -1;
});
// Return the collection objects
handleCallback(
callback,
null,
documents.map(d => {
return new Collection(
db,
db.s.topology,
db.databaseName,
d.name,
db.s.pkFactory,
db.s.options
);
})
);
});
}
/**
* Creates an index on the db and collection.
* @method
* @param {Db} db The Db instance on which to create an index.
* @param {string} name Name of the collection to create the index on.
* @param {(string|object)} fieldOrSpec Defines the index.
* @param {object} [options] Optional settings. See Db.prototype.createIndex for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function createIndex(db, name, fieldOrSpec, options, callback) {
// Get the write concern options
let finalOptions = Object.assign({}, { readPreference: ReadPreference.PRIMARY }, options);
finalOptions = applyWriteConcern(finalOptions, { db }, options);
// Ensure we have a callback
if (finalOptions.writeConcern && typeof callback !== 'function') {
throw MongoError.create({
message: 'Cannot use a writeConcern without a provided callback',
driver: true
});
}
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed())
return callback(new MongoError('topology was destroyed'));
// Attempt to run using createIndexes command
createIndexUsingCreateIndexes(db, name, fieldOrSpec, finalOptions, (err, result) => {
if (err == null) return handleCallback(callback, err, result);
/**
* The following errors mean that the server recognized `createIndex` as a command so we don't need to fallback to an insert:
* 67 = 'CannotCreateIndex' (malformed index options)
* 85 = 'IndexOptionsConflict' (index already exists with different options)
* 86 = 'IndexKeySpecsConflict' (index already exists with the same name)
* 11000 = 'DuplicateKey' (couldn't build unique index because of dupes)
* 11600 = 'InterruptedAtShutdown' (interrupted at shutdown)
* 197 = 'InvalidIndexSpecificationOption' (`_id` with `background: true`)
*/
if (
err.code === 67 ||
err.code === 11000 ||
err.code === 85 ||
err.code === 86 ||
err.code === 11600 ||
err.code === 197
) {
return handleCallback(callback, err, result);
}
// Create command
const doc = createCreateIndexCommand(db, name, fieldOrSpec, options);
// Set no key checking
finalOptions.checkKeys = false;
// Insert document
db.s.topology.insert(
db.s.namespace.withCollection(CONSTANTS.SYSTEM_INDEX_COLLECTION),
doc,
finalOptions,
(err, result) => {
if (callback == null) return;
if (err) return handleCallback(callback, err);
if (result == null) return handleCallback(callback, null, null);
if (result.result.writeErrors)
return handleCallback(callback, MongoError.create(result.result.writeErrors[0]), null);
handleCallback(callback, null, doc.name);
}
);
});
}
// Add listeners to topology
function createListener(db, e, object) {
function listener(err) {
if (object.listeners(e).length > 0) {
object.emit(e, err, db);
// Emit on all associated db's if available
for (let i = 0; i < db.s.children.length; i++) {
db.s.children[i].emit(e, err, db.s.children[i]);
}
}
}
return listener;
}
/**
* Drop a collection from the database, removing it permanently. New accesses will create a new collection.
*
* @method
* @param {Db} db The Db instance on which to drop the collection.
* @param {string} name Name of collection to drop
* @param {Object} [options] Optional settings. See Db.prototype.dropCollection for a list of options.
* @param {Db~resultCallback} [callback] The results callback
*/
function dropCollection(db, name, options, callback) {
executeCommand(db, name, options, (err, result) => {
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
if (err) return handleCallback(callback, err);
if (result.ok) return handleCallback(callback, null, true);
handleCallback(callback, null, false);
});
}
/**
* Drop a database, removing it permanently from the server.
*
* @method
* @param {Db} db The Db instance to drop.
* @param {Object} cmd The command document.
* @param {Object} [options] Optional settings. See Db.prototype.dropDatabase for a list of options.
* @param {Db~resultCallback} [callback] The results callback
*/
function dropDatabase(db, cmd, options, callback) {
executeCommand(db, cmd, options, (err, result) => {
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
if (callback == null) return;
if (err) return handleCallback(callback, err, null);
handleCallback(callback, null, result.ok ? true : false);
});
}
/**
* Ensures that an index exists. If it does not, creates it.
*
* @method
* @param {Db} db The Db instance on which to ensure the index.
* @param {string} name The index name
* @param {(string|object)} fieldOrSpec Defines the index.
* @param {object} [options] Optional settings. See Db.prototype.ensureIndex for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function ensureIndex(db, name, fieldOrSpec, options, callback) {
// Get the write concern options
const finalOptions = applyWriteConcern({}, { db }, options);
// Create command
const selector = createCreateIndexCommand(db, name, fieldOrSpec, options);
const index_name = selector.name;
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed())
return callback(new MongoError('topology was destroyed'));
// Merge primary readPreference
finalOptions.readPreference = ReadPreference.PRIMARY;
// Check if the index already exists
indexInformation(db, name, finalOptions, (err, indexInformation) => {
if (err != null && err.code !== 26) return handleCallback(callback, err, null);
// If the index does not exist, create it
if (indexInformation == null || !indexInformation[index_name]) {
createIndex(db, name, fieldOrSpec, options, callback);
} else {
if (typeof callback === 'function') return handleCallback(callback, null, index_name);
}
});
}
/**
* Evaluate JavaScript on the server
*
* @method
* @param {Db} db The Db instance.
* @param {Code} code JavaScript to execute on server.
* @param {(object|array)} parameters The parameters for the call.
* @param {object} [options] Optional settings. See Db.prototype.eval for a list of options.
* @param {Db~resultCallback} [callback] The results callback
* @deprecated Eval is deprecated on MongoDB 3.2 and forward
*/
function evaluate(db, code, parameters, options, callback) {
let finalCode = code;
let finalParameters = [];
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed())
return callback(new MongoError('topology was destroyed'));
// If not a code object translate to one
if (!(finalCode && finalCode._bsontype === 'Code')) finalCode = new Code(finalCode);
// Ensure the parameters are correct
if (parameters != null && !Array.isArray(parameters) && typeof parameters !== 'function') {
finalParameters = [parameters];
} else if (parameters != null && Array.isArray(parameters) && typeof parameters !== 'function') {
finalParameters = parameters;
}
// Create execution selector
let cmd = { $eval: finalCode, args: finalParameters };
// Check if the nolock parameter is passed in
if (options['nolock']) {
cmd['nolock'] = options['nolock'];
}
// Set primary read preference
options.readPreference = new ReadPreference(ReadPreference.PRIMARY);
// Execute the command
executeCommand(db, cmd, options, (err, result) => {
if (err) return handleCallback(callback, err, null);
if (result && result.ok === 1) return handleCallback(callback, null, result.retval);
if (result)
return handleCallback(
callback,
MongoError.create({ message: `eval failed: ${result.errmsg}`, driver: true }),
null
);
handleCallback(callback, err, result);
});
}
/**
* Execute a command
*
* @method
* @param {Db} db The Db instance on which to execute the command.
* @param {object} command The command hash
* @param {object} [options] Optional settings. See Db.prototype.command for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function executeCommand(db, command, options, callback) {
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed())
return callback(new MongoError('topology was destroyed'));
// Get the db name we are executing against
const dbName = options.dbName || options.authdb || db.databaseName;
// Convert the readPreference if its not a write
options.readPreference = resolveReadPreference(db, options);
// Debug information
if (db.s.logger.isDebug())
db.s.logger.debug(
`executing command ${JSON.stringify(
command
)} against ${dbName}.$cmd with options [${JSON.stringify(
debugOptions(debugFields, options)
)}]`
);
// Execute command
db.s.topology.command(db.s.namespace.withCollection('$cmd'), command, options, (err, result) => {
if (err) return handleCallback(callback, err);
if (options.full) return handleCallback(callback, null, result);
handleCallback(callback, null, result.result);
});
}
/**
* Runs a command on the database as admin.
*
* @method
* @param {Db} db The Db instance on which to execute the command.
* @param {object} command The command hash
* @param {object} [options] Optional settings. See Db.prototype.executeDbAdminCommand for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function executeDbAdminCommand(db, command, options, callback) {
const namespace = new MongoDBNamespace('admin', '$cmd');
db.s.topology.command(namespace, command, options, (err, result) => {
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
if (err) return handleCallback(callback, err);
handleCallback(callback, null, result.result);
});
}
/**
* Retrieves this collections index info.
*
* @method
* @param {Db} db The Db instance on which to retrieve the index info.
* @param {string} name The name of the collection.
* @param {object} [options] Optional settings. See Db.prototype.indexInformation for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function indexInformation(db, name, options, callback) {
// If we specified full information
const full = options['full'] == null ? false : options['full'];
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed())
return callback(new MongoError('topology was destroyed'));
// Process all the results from the index command and collection
function processResults(indexes) {
// Contains all the information
let info = {};
// Process all the indexes
for (let i = 0; i < indexes.length; i++) {
const index = indexes[i];
// Let's unpack the object
info[index.name] = [];
for (let name in index.key) {
info[index.name].push([name, index.key[name]]);
}
}
return info;
}
// Get the list of indexes of the specified collection
db
.collection(name)
.listIndexes(options)
.toArray((err, indexes) => {
if (err) return callback(toError(err));
if (!Array.isArray(indexes)) return handleCallback(callback, null, []);
if (full) return handleCallback(callback, null, indexes);
handleCallback(callback, null, processResults(indexes));
});
}
/**
* Retrieve the current profiling information for MongoDB
*
* @method
* @param {Db} db The Db instance on which to retrieve the profiling info.
* @param {Object} [options] Optional settings. See Db.protoype.profilingInfo for a list of options.
* @param {Db~resultCallback} [callback] The command result callback.
* @deprecated Query the system.profile collection directly.
*/
function profilingInfo(db, options, callback) {
try {
db
.collection('system.profile')
.find({}, options)
.toArray(callback);
} catch (err) {
return callback(err, null);
}
}
/**
* Remove a user from a database
*
* @method
* @param {Db} db The Db instance on which to remove the user.
* @param {string} username The username.
* @param {object} [options] Optional settings. See Db.prototype.removeUser for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function removeUser(db, username, options, callback) {
let Db = loadDb();
// Attempt to execute command
executeAuthRemoveUserCommand(db, username, options, (err, result) => {
if (err && err.code === -5000) {
const finalOptions = applyWriteConcern(Object.assign({}, options), { db }, options);
// If we have another db set
const db = options.dbName ? new Db(options.dbName, db.s.topology, db.s.options) : db;
// Fetch a user collection
const collection = db.collection(CONSTANTS.SYSTEM_USER_COLLECTION);
// Locate the user
findOne(collection, { user: username }, finalOptions, (err, user) => {
if (user == null) return handleCallback(callback, err, false);
remove(collection, { user: username }, finalOptions, err => {
handleCallback(callback, err, true);
});
});
return;
}
if (err) return handleCallback(callback, err);
handleCallback(callback, err, result);
});
}
// Validate the database name
function validateDatabaseName(databaseName) {
if (typeof databaseName !== 'string')
throw MongoError.create({ message: 'database name must be a string', driver: true });
if (databaseName.length === 0)
throw MongoError.create({ message: 'database name cannot be the empty string', driver: true });
if (databaseName === '$external') return;
const invalidChars = [' ', '.', '$', '/', '\\'];
for (let i = 0; i < invalidChars.length; i++) {
if (databaseName.indexOf(invalidChars[i]) !== -1)
throw MongoError.create({
message: "database names cannot contain the character '" + invalidChars[i] + "'",
driver: true
});
}
}
/**
* Create the command object for Db.prototype.createIndex.
*
* @param {Db} db The Db instance on which to create the command.
* @param {string} name Name of the collection to create the index on.
* @param {(string|object)} fieldOrSpec Defines the index.
* @param {Object} [options] Optional settings. See Db.prototype.createIndex for a list of options.
* @return {Object} The insert command object.
*/
function createCreateIndexCommand(db, name, fieldOrSpec, options) {
const indexParameters = parseIndexOptions(fieldOrSpec);
const fieldHash = indexParameters.fieldHash;
// Generate the index name
const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
const selector = {
ns: db.s.namespace.withCollection(name).toString(),
key: fieldHash,
name: indexName
};
// Ensure we have a correct finalUnique
const finalUnique = options == null || 'object' === typeof options ? false : options;
// Set up options
options = options == null || typeof options === 'boolean' ? {} : options;
// Add all the options
const keysToOmit = Object.keys(selector);
for (let optionName in options) {
if (keysToOmit.indexOf(optionName) === -1) {
selector[optionName] = options[optionName];
}
}
if (selector['unique'] == null) selector['unique'] = finalUnique;
// Remove any write concern operations
const removeKeys = ['w', 'wtimeout', 'j', 'fsync', 'readPreference', 'session'];
for (let i = 0; i < removeKeys.length; i++) {
delete selector[removeKeys[i]];
}
// Return the command creation selector
return selector;
}
/**
* Create index using the createIndexes command.
*
* @param {Db} db The Db instance on which to execute the command.
* @param {string} name Name of the collection to create the index on.
* @param {(string|object)} fieldOrSpec Defines the index.
* @param {Object} [options] Optional settings. See Db.prototype.createIndex for a list of options.
* @param {Db~resultCallback} [callback] The command result callback.
*/
function createIndexUsingCreateIndexes(db, name, fieldOrSpec, options, callback) {
// Build the index
const indexParameters = parseIndexOptions(fieldOrSpec);
// Generate the index name
const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
// Set up the index
const indexes = [{ name: indexName, key: indexParameters.fieldHash }];
// merge all the options
const keysToOmit = Object.keys(indexes[0]).concat([
'writeConcern',
'w',
'wtimeout',
'j',
'fsync',
'readPreference',
'session'
]);
for (let optionName in options) {
if (keysToOmit.indexOf(optionName) === -1) {
indexes[0][optionName] = options[optionName];
}
}
// Get capabilities
const capabilities = db.s.topology.capabilities();
// Did the user pass in a collation, check if our write server supports it
if (indexes[0].collation && capabilities && !capabilities.commandsTakeCollation) {
// Create a new error
const error = new MongoError('server/primary/mongos does not support collation');
error.code = 67;
// Return the error
return callback(error);
}
// Create command, apply write concern to command
const cmd = applyWriteConcern({ createIndexes: name, indexes }, { db }, options);
// ReadPreference primary
options.readPreference = ReadPreference.PRIMARY;
// Build the command
executeCommand(db, cmd, options, (err, result) => {
if (err) return handleCallback(callback, err, null);
if (result.ok === 0) return handleCallback(callback, toError(result), null);
// Return the indexName for backward compatibility
handleCallback(callback, null, indexName);
});
}
/**
* Run the createUser command.
*
* @param {Db} db The Db instance on which to execute the command.
* @param {string} username The username of the user to add.
* @param {string} password The password of the user to add.
* @param {object} [options] Optional settings. See Db.prototype.addUser for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function executeAuthCreateUserCommand(db, username, password, options, callback) {
// Special case where there is no password ($external users)
if (typeof username === 'string' && password != null && typeof password === 'object') {
options = password;
password = null;
}
// Unpack all options
if (typeof options === 'function') {
callback = options;
options = {};
}
// Error out if we digestPassword set
if (options.digestPassword != null) {
return callback(
toError(
"The digestPassword option is not supported via add_user. Please use db.command('createUser', ...) instead for this option."
)
);
}
// Get additional values
const customData = options.customData != null ? options.customData : {};
let roles = Array.isArray(options.roles) ? options.roles : [];
const maxTimeMS = typeof options.maxTimeMS === 'number' ? options.maxTimeMS : null;
// If not roles defined print deprecated message
if (roles.length === 0) {
console.log('Creating a user without roles is deprecated in MongoDB >= 2.6');
}
// Get the error options
const commandOptions = { writeCommand: true };
if (options['dbName']) commandOptions.dbName = options['dbName'];
// Add maxTimeMS to options if set
if (maxTimeMS != null) commandOptions.maxTimeMS = maxTimeMS;
// Check the db name and add roles if needed
if (
(db.databaseName.toLowerCase() === 'admin' || options.dbName === 'admin') &&
!Array.isArray(options.roles)
) {
roles = ['root'];
} else if (!Array.isArray(options.roles)) {
roles = ['dbOwner'];
}
const digestPassword = db.s.topology.lastIsMaster().maxWireVersion >= 7;
// Build the command to execute
let command = {
createUser: username,
customData: customData,
roles: roles,
digestPassword
};
// Apply write concern to command
command = applyWriteConcern(command, { db }, options);
let userPassword = password;
if (!digestPassword) {
// Use node md5 generator
const md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ':mongo:' + password);
userPassword = md5.digest('hex');
}
// No password
if (typeof password === 'string') {
command.pwd = userPassword;
}
// Force write using primary
commandOptions.readPreference = ReadPreference.primary;
// Execute the command
executeCommand(db, command, commandOptions, (err, result) => {
if (err && err.ok === 0 && err.code === undefined)
return handleCallback(callback, { code: -5000 }, null);
if (err) return handleCallback(callback, err, null);
handleCallback(
callback,
!result.ok ? toError(result) : null,
result.ok ? [{ user: username, pwd: '' }] : null
);
});
}
/**
* Run the dropUser command.
*
* @param {Db} db The Db instance on which to execute the command.
* @param {string} username The username of the user to remove.
* @param {object} [options] Optional settings. See Db.prototype.removeUser for a list of options.
* @param {Db~resultCallback} [callback] The command result callback
*/
function executeAuthRemoveUserCommand(db, username, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed())
return callback(new MongoError('topology was destroyed'));
// Get the error options
const commandOptions = { writeCommand: true };
if (options['dbName']) commandOptions.dbName = options['dbName'];
// Get additional values
const maxTimeMS = typeof options.maxTimeMS === 'number' ? options.maxTimeMS : null;
// Add maxTimeMS to options if set
if (maxTimeMS != null) commandOptions.maxTimeMS = maxTimeMS;
// Build the command to execute
let command = {
dropUser: username
};
// Apply write concern to command
command = applyWriteConcern(command, { db }, options);
// Force write using primary
commandOptions.readPreference = ReadPreference.primary;
// Execute the command
executeCommand(db, command, commandOptions, (err, result) => {
if (err && !err.ok && err.code === undefined) return handleCallback(callback, { code: -5000 });
if (err) return handleCallback(callback, err, null);
handleCallback(callback, null, result.ok ? true : false);
});
}
module.exports = {
addUser,
collections,
createListener,
createIndex,
dropCollection,
dropDatabase,
ensureIndex,
evaluate,
executeCommand,
executeDbAdminCommand,
indexInformation,
profilingInfo,
removeUser,
validateDatabaseName
};
+25
View File
@@ -0,0 +1,25 @@
'use strict';
const OperationBase = require('./operation').OperationBase;
const deleteCallback = require('./common_functions').deleteCallback;
const removeDocuments = require('./common_functions').removeDocuments;
class DeleteManyOperation extends OperationBase {
constructor(collection, filter, options) {
super(options);
this.collection = collection;
this.filter = filter;
}
execute(callback) {
const coll = this.collection;
const filter = this.filter;
const options = this.options;
options.single = false;
removeDocuments(coll, filter, options, (err, r) => deleteCallback(err, r, callback));
}
}
module.exports = DeleteManyOperation;
+25
View File
@@ -0,0 +1,25 @@
'use strict';
const OperationBase = require('./operation').OperationBase;
const deleteCallback = require('./common_functions').deleteCallback;
const removeDocuments = require('./common_functions').removeDocuments;
class DeleteOneOperation extends OperationBase {
constructor(collection, filter, options) {
super(options);
this.collection = collection;
this.filter = filter;
}
execute(callback) {
const coll = this.collection;
const filter = this.filter;
const options = this.options;
options.single = true;
removeDocuments(coll, filter, options, (err, r) => deleteCallback(err, r, callback));
}
}
module.exports = DeleteOneOperation;
+85
View File
@@ -0,0 +1,85 @@
'use strict';
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const CommandOperationV2 = require('./command_v2');
const decorateWithCollation = require('../utils').decorateWithCollation;
const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
/**
* Return a list of distinct values for the given key across a collection.
*
* @class
* @property {Collection} a Collection instance.
* @property {string} key Field of the document to find distinct values for.
* @property {object} query The query for filtering the set of documents to which we apply the distinct filter.
* @property {object} [options] Optional settings. See Collection.prototype.distinct for a list of options.
*/
class DistinctOperation extends CommandOperationV2 {
/**
* Construct a Distinct operation.
*
* @param {Collection} a Collection instance.
* @param {string} key Field of the document to find distinct values for.
* @param {object} query The query for filtering the set of documents to which we apply the distinct filter.
* @param {object} [options] Optional settings. See Collection.prototype.distinct for a list of options.
*/
constructor(collection, key, query, options) {
super(collection, options);
this.collection = collection;
this.key = key;
this.query = query;
}
/**
* Execute the operation.
*
* @param {Collection~resultCallback} [callback] The command result callback
*/
execute(server, callback) {
const coll = this.collection;
const key = this.key;
const query = this.query;
const options = this.options;
// Distinct command
const cmd = {
distinct: coll.collectionName,
key: key,
query: query
};
// Add maxTimeMS if defined
if (typeof options.maxTimeMS === 'number') {
cmd.maxTimeMS = options.maxTimeMS;
}
// Do we have a readConcern specified
decorateWithReadConcern(cmd, coll, options);
// Have we specified collation
try {
decorateWithCollation(cmd, coll, options);
} catch (err) {
return callback(err, null);
}
super.executeCommand(server, cmd, (err, result) => {
if (err) {
callback(err);
return;
}
callback(null, this.options.full ? result : result.values);
});
}
}
defineAspects(DistinctOperation, [
Aspect.READ_OPERATION,
Aspect.RETRYABLE,
Aspect.EXECUTE_WITH_SELECTION
]);
module.exports = DistinctOperation;
+53
View File
@@ -0,0 +1,53 @@
'use strict';
const Aspect = require('./operation').Aspect;
const CommandOperation = require('./command');
const defineAspects = require('./operation').defineAspects;
const handleCallback = require('../utils').handleCallback;
class DropOperation extends CommandOperation {
constructor(db, options) {
const finalOptions = Object.assign({}, options, db.s.options);
if (options.session) {
finalOptions.session = options.session;
}
super(db, finalOptions);
}
execute(callback) {
super.execute((err, result) => {
if (err) return handleCallback(callback, err);
if (result.ok) return handleCallback(callback, null, true);
handleCallback(callback, null, false);
});
}
}
defineAspects(DropOperation, Aspect.WRITE_OPERATION);
class DropCollectionOperation extends DropOperation {
constructor(db, name, options) {
super(db, options);
this.name = name;
this.namespace = `${db.namespace}.${name}`;
}
_buildCommand() {
return { drop: this.name };
}
}
class DropDatabaseOperation extends DropOperation {
_buildCommand() {
return { dropDatabase: 1 };
}
}
module.exports = {
DropOperation,
DropCollectionOperation,
DropDatabaseOperation
};
+42
View File
@@ -0,0 +1,42 @@
'use strict';
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const CommandOperation = require('./command');
const applyWriteConcern = require('../utils').applyWriteConcern;
const handleCallback = require('../utils').handleCallback;
class DropIndexOperation extends CommandOperation {
constructor(collection, indexName, options) {
super(collection.s.db, options, collection);
this.collection = collection;
this.indexName = indexName;
}
_buildCommand() {
const collection = this.collection;
const indexName = this.indexName;
const options = this.options;
let cmd = { dropIndexes: collection.collectionName, index: indexName };
// Decorate command with writeConcern if supported
cmd = applyWriteConcern(cmd, { db: collection.s.db, collection }, options);
return cmd;
}
execute(callback) {
// Execute command
super.execute((err, result) => {
if (typeof callback !== 'function') return;
if (err) return handleCallback(callback, err, null);
handleCallback(callback, null, result);
});
}
}
defineAspects(DropIndexOperation, Aspect.WRITE_OPERATION);
module.exports = DropIndexOperation;
+23
View File
@@ -0,0 +1,23 @@
'use strict';
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const DropIndexOperation = require('./drop_index');
const handleCallback = require('../utils').handleCallback;
class DropIndexesOperation extends DropIndexOperation {
constructor(collection, options) {
super(collection, '*', options);
}
execute(callback) {
super.execute(err => {
if (err) return handleCallback(callback, err, false);
handleCallback(callback, null, true);
});
}
}
defineAspects(DropIndexesOperation, Aspect.WRITE_OPERATION);
module.exports = DropIndexesOperation;
+58
View File
@@ -0,0 +1,58 @@
'use strict';
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const CommandOperationV2 = require('./command_v2');
class EstimatedDocumentCountOperation extends CommandOperationV2 {
constructor(collection, query, options) {
if (typeof options === 'undefined') {
options = query;
query = undefined;
}
super(collection, options);
this.collectionName = collection.s.namespace.collection;
if (query) {
this.query = query;
}
}
execute(server, callback) {
const options = this.options;
const cmd = { count: this.collectionName };
if (this.query) {
cmd.query = this.query;
}
if (typeof options.skip === 'number') {
cmd.skip = options.skip;
}
if (typeof options.limit === 'number') {
cmd.limit = options.limit;
}
if (options.hint) {
cmd.hint = options.hint;
}
super.executeCommand(server, cmd, (err, response) => {
if (err) {
callback(err);
return;
}
callback(null, response.n);
});
}
}
defineAspects(EstimatedDocumentCountOperation, [
Aspect.READ_OPERATION,
Aspect.RETRYABLE,
Aspect.EXECUTE_WITH_SELECTION
]);
module.exports = EstimatedDocumentCountOperation;
+34
View File
@@ -0,0 +1,34 @@
'use strict';
const OperationBase = require('./operation').OperationBase;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const MongoDBNamespace = require('../utils').MongoDBNamespace;
class ExecuteDbAdminCommandOperation extends OperationBase {
constructor(db, selector, options) {
super(options);
this.db = db;
this.selector = selector;
}
execute(callback) {
const db = this.db;
const selector = this.selector;
const options = this.options;
const namespace = new MongoDBNamespace('admin', '$cmd');
db.s.topology.command(namespace, selector, options, (err, result) => {
// Did the user destroy the topology
if (db.serverConfig && db.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
if (err) return handleCallback(callback, err);
handleCallback(callback, null, result.result);
});
}
}
module.exports = ExecuteDbAdminCommandOperation;
+198
View File
@@ -0,0 +1,198 @@
'use strict';
const MongoError = require('../core/error').MongoError;
const Aspect = require('./operation').Aspect;
const OperationBase = require('./operation').OperationBase;
const ReadPreference = require('../core/topologies/read_preference');
const isRetryableError = require('../core/error').isRetryableError;
const maxWireVersion = require('../core/utils').maxWireVersion;
const isUnifiedTopology = require('../core/utils').isUnifiedTopology;
/**
* Executes the given operation with provided arguments.
*
* This method reduces large amounts of duplication in the entire codebase by providing
* a single point for determining whether callbacks or promises should be used. Additionally
* it allows for a single point of entry to provide features such as implicit sessions, which
* are required by the Driver Sessions specification in the event that a ClientSession is
* not provided
*
* @param {object} topology The topology to execute this operation on
* @param {Operation} operation The operation to execute
* @param {function} callback The command result callback
*/
function executeOperation(topology, operation, callback) {
if (topology == null) {
throw new TypeError('This method requires a valid topology instance');
}
if (!(operation instanceof OperationBase)) {
throw new TypeError('This method requires a valid operation instance');
}
if (
isUnifiedTopology(topology) &&
!operation.hasAspect(Aspect.SKIP_SESSION) &&
topology.shouldCheckForSessionSupport()
) {
return selectServerForSessionSupport(topology, operation, callback);
}
const Promise = topology.s.promiseLibrary;
// The driver sessions spec mandates that we implicitly create sessions for operations
// that are not explicitly provided with a session.
let session, owner;
if (!operation.hasAspect(Aspect.SKIP_SESSION) && topology.hasSessionSupport()) {
if (operation.session == null) {
owner = Symbol();
session = topology.startSession({ owner });
operation.session = session;
} else if (operation.session.hasEnded) {
throw new MongoError('Use of expired sessions is not permitted');
}
}
const makeExecuteCallback = (resolve, reject) =>
function executeCallback(err, result) {
if (session && session.owner === owner) {
session.endSession(() => {
if (operation.session === session) {
operation.clearSession();
}
if (err) return reject(err);
resolve(result);
});
} else {
if (err) return reject(err);
resolve(result);
}
};
// Execute using callback
if (typeof callback === 'function') {
const handler = makeExecuteCallback(
result => callback(null, result),
err => callback(err, null)
);
try {
if (operation.hasAspect(Aspect.EXECUTE_WITH_SELECTION)) {
return executeWithServerSelection(topology, operation, handler);
} else {
return operation.execute(handler);
}
} catch (e) {
handler(e);
throw e;
}
}
return new Promise(function(resolve, reject) {
const handler = makeExecuteCallback(resolve, reject);
try {
if (operation.hasAspect(Aspect.EXECUTE_WITH_SELECTION)) {
return executeWithServerSelection(topology, operation, handler);
} else {
return operation.execute(handler);
}
} catch (e) {
handler(e);
}
});
}
function supportsRetryableReads(server) {
return maxWireVersion(server) >= 6;
}
function executeWithServerSelection(topology, operation, callback) {
const readPreference = operation.readPreference || ReadPreference.primary;
const inTransaction = operation.session && operation.session.inTransaction();
if (inTransaction && !readPreference.equals(ReadPreference.primary)) {
callback(
new MongoError(
`Read preference in a transaction must be primary, not: ${readPreference.mode}`
)
);
return;
}
const serverSelectionOptions = {
readPreference,
session: operation.session
};
function callbackWithRetry(err, result) {
if (err == null) {
return callback(null, result);
}
if (!isRetryableError(err)) {
return callback(err);
}
// select a new server, and attempt to retry the operation
topology.selectServer(serverSelectionOptions, (err, server) => {
if (err || !supportsRetryableReads(server)) {
callback(err, null);
return;
}
operation.execute(server, callback);
});
}
// select a server, and execute the operation against it
topology.selectServer(serverSelectionOptions, (err, server) => {
if (err) {
callback(err, null);
return;
}
const shouldRetryReads =
topology.s.options.retryReads !== false &&
(operation.session && !inTransaction) &&
supportsRetryableReads(server) &&
operation.canRetryRead;
if (operation.hasAspect(Aspect.RETRYABLE) && shouldRetryReads) {
operation.execute(server, callbackWithRetry);
return;
}
operation.execute(server, callback);
});
}
// TODO: This is only supported for unified topology, it should go away once
// we remove support for legacy topology types.
function selectServerForSessionSupport(topology, operation, callback) {
const Promise = topology.s.promiseLibrary;
let result;
if (typeof callback !== 'function') {
result = new Promise((resolve, reject) => {
callback = (err, result) => {
if (err) return reject(err);
resolve(result);
};
});
}
topology.selectServer(ReadPreference.primaryPreferred, err => {
if (err) {
callback(err);
return;
}
executeOperation(topology, operation, callback);
});
return result;
}
module.exports = executeOperation;
+23
View File
@@ -0,0 +1,23 @@
'use strict';
const Aspect = require('./operation').Aspect;
const CoreCursor = require('../core/cursor').CoreCursor;
const defineAspects = require('./operation').defineAspects;
const OperationBase = require('./operation').OperationBase;
class ExplainOperation extends OperationBase {
constructor(cursor) {
super();
this.cursor = cursor;
}
execute() {
const cursor = this.cursor;
return CoreCursor.prototype._next.apply(cursor, arguments);
}
}
defineAspects(ExplainOperation, Aspect.SKIP_SESSION);
module.exports = ExplainOperation;
+35
View File
@@ -0,0 +1,35 @@
'use strict';
const OperationBase = require('./operation').OperationBase;
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const resolveReadPreference = require('../utils').resolveReadPreference;
class FindOperation extends OperationBase {
constructor(collection, ns, command, options) {
super(options);
this.ns = ns;
this.cmd = command;
this.readPreference = resolveReadPreference(collection, this.options);
}
execute(server, callback) {
// copied from `CommandOperationV2`, to be subclassed in the future
this.server = server;
const cursorState = this.cursorState || {};
// TOOD: use `MongoDBNamespace` through and through
server.query(this.ns.toString(), this.cmd, cursorState, this.options, callback);
}
}
defineAspects(FindOperation, [
Aspect.READ_OPERATION,
Aspect.RETRYABLE,
Aspect.EXECUTE_WITH_SELECTION,
Aspect.SKIP_SESSION
]);
module.exports = FindOperation;
+98
View File
@@ -0,0 +1,98 @@
'use strict';
const OperationBase = require('./operation').OperationBase;
const applyRetryableWrites = require('../utils').applyRetryableWrites;
const applyWriteConcern = require('../utils').applyWriteConcern;
const decorateWithCollation = require('../utils').decorateWithCollation;
const executeCommand = require('./db_ops').executeCommand;
const formattedOrderClause = require('../utils').formattedOrderClause;
const handleCallback = require('../utils').handleCallback;
const ReadPreference = require('../core').ReadPreference;
class FindAndModifyOperation extends OperationBase {
constructor(collection, query, sort, doc, options) {
super(options);
this.collection = collection;
this.query = query;
this.sort = sort;
this.doc = doc;
}
execute(callback) {
const coll = this.collection;
const query = this.query;
const sort = formattedOrderClause(this.sort);
const doc = this.doc;
let options = this.options;
// Create findAndModify command object
const queryObject = {
findAndModify: coll.collectionName,
query: query
};
if (sort) {
queryObject.sort = sort;
}
queryObject.new = options.new ? true : false;
queryObject.remove = options.remove ? true : false;
queryObject.upsert = options.upsert ? true : false;
const projection = options.projection || options.fields;
if (projection) {
queryObject.fields = projection;
}
if (options.arrayFilters) {
queryObject.arrayFilters = options.arrayFilters;
}
if (doc && !options.remove) {
queryObject.update = doc;
}
if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;
// Either use override on the function, or go back to default on either the collection
// level or db
options.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
// No check on the documents
options.checkKeys = false;
// Final options for retryable writes and write concern
options = applyRetryableWrites(options, coll.s.db);
options = applyWriteConcern(options, { db: coll.s.db, collection: coll }, options);
// Decorate the findAndModify command with the write Concern
if (options.writeConcern) {
queryObject.writeConcern = options.writeConcern;
}
// Have we specified bypassDocumentValidation
if (options.bypassDocumentValidation === true) {
queryObject.bypassDocumentValidation = options.bypassDocumentValidation;
}
options.readPreference = ReadPreference.primary;
// Have we specified collation
try {
decorateWithCollation(queryObject, coll, options);
} catch (err) {
return callback(err, null);
}
// Execute the command
executeCommand(coll.s.db, queryObject, options, (err, result) => {
if (err) return handleCallback(callback, err, null);
return handleCallback(callback, null, result);
});
}
}
module.exports = FindAndModifyOperation;
Loaded 100 of 2123 files, more files were not shown because too many files have changed in this diff. Show more