Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished cleanup and code testing

  • Loading branch information...
commit aae83eadad49f97f94d43ecc9c933fcbeb29b3a8 1 parent 2c3df28
@christkv christkv authored
View
12 lib/mongodb/connection/mongos.js
@@ -253,7 +253,7 @@ Mongos.prototype.isConnected = function() {
/**
* @ignore
*/
-Mongos.prototype.checkoutWriter = function(read) {
+Mongos.prototype.checkoutWriter = function() {
// No current mongo, just pick first server
if(this._currentMongos == null && this.servers.length > 0) {
return this.servers[0].checkoutWriter();
@@ -264,7 +264,15 @@ Mongos.prototype.checkoutWriter = function(read) {
/**
* @ignore
*/
-Mongos.prototype.checkoutReader = function() {
+Mongos.prototype.checkoutReader = function(read) {
+ // If we have a read preference object unpack it
+ if(typeof read == 'object' && read['_type'] == 'ReadPreference') {
+ // Validate if the object is using a valid mode
+ if(!read.isValid()) throw new Error("Illegal readPreference mode specified, " + read.mode);
+ } else if(!ReadPreference.isValid(read)) {
+ throw new Error("Illegal readPreference mode specified, " + read);
+ }
+
// No current mongo, just pick first server
if(this._currentMongos == null && this.servers.length > 0) {
return this.servers[0].checkoutReader();
View
19 lib/mongodb/connection/read_preference.js
@@ -14,7 +14,6 @@
* @return {ReadPreference}
*/
var ReadPreference = function(mode, tags) {
-// Set up basic
if(!(this instanceof ReadPreference))
return new ReadPreference(mode, tags);
this._type = 'ReadPreference';
@@ -24,7 +23,23 @@ var ReadPreference = function(mode, tags) {
/**
* @ignore
- * @api private
+ */
+ReadPreference.isValid = function(_mode) {
+ return (_mode == ReadPreference.PRIMARY || _mode == ReadPreference.PRIMARY_PREFERRED
+ || _mode == ReadPreference.SECONDARY || _mode == ReadPreference.SECONDARY_PREFERRED
+ || _mode == ReadPreference.NEAREST);
+}
+
+/**
+ * @ignore
+ */
+ReadPreference.prototype.isValid = function(mode) {
+ var _mode = typeof mode == 'string' ? mode : this.mode;
+ return ReadPreference.isValid(_mode);
+}
+
+/**
+ * @ignore
*/
ReadPreference.prototype.toObject = function() {
var object = {mode:this.mode};
View
8 lib/mongodb/connection/repl_set.js
@@ -605,6 +605,8 @@ ReplSet.prototype.connect = function(parent, options, callback) {
this.once("connectionError", function(err) {
self._serverState = 'disconnected';
+ // Ensure it's all closed
+ self.close();
// Perform the callback
if(typeof callback == 'function') {
var internalCallback = callback;
@@ -743,9 +745,11 @@ var _pickFromTags = function(self, tags) {
*/
ReplSet.prototype.checkoutReader = function(readPreference, tags) {
var connection = null;
-
// If we have a read preference object unpack it
if(typeof readPreference == 'object' && readPreference['_type'] == 'ReadPreference') {
+ // Validate if the object is using a valid mode
+ if(!readPreference.isValid()) throw new Error("Illegal readPreference mode specified, " + readPreference.mode);
+ // Set the tag
tags = readPreference.tags;
readPreference = readPreference.mode;
} else if(typeof readPreference == 'object' && readPreference['_type'] != 'ReadPreference') {
@@ -755,7 +759,7 @@ ReplSet.prototype.checkoutReader = function(readPreference, tags) {
// Set up our read Preference, allowing us to override the readPreference
var finalReadPreference = readPreference != null ? readPreference : this._readPreference;
finalReadPreference = finalReadPreference == true ? ReadPreference.SECONDARY_PREFERRED : finalReadPreference;
-
+
// If we are reading from a primary
if(finalReadPreference == 'primary') {
// If we provide a tags set send an error
View
9 lib/mongodb/cursor.js
@@ -286,7 +286,7 @@ Cursor.prototype.limit = function(limit, callback) {
*/
Cursor.prototype.setReadPreference = function(readPreference, tags, callback) {
if(typeof tags == 'function') callback = tags;
- callback = callback || function(){};
+ callback = callback || function() {};
if(this.queryRun == true || this.state == Cursor.CLOSED) {
callback(new Error("Cannot change read preference on executed query or closed cursor"));
@@ -506,8 +506,11 @@ Cursor.prototype.nextObject = function(callback) {
// If we have no connection set on this cursor check one out
if(self.connection == null) {
- // Fetch either a reader or writer dependent on the specified read option
- self.connection = this.read == null ? self.db.serverConfig.checkoutWriter() : self.db.serverConfig.checkoutReader(this.read);
+ try {
+ self.connection = this.read == null ? self.db.serverConfig.checkoutWriter() : self.db.serverConfig.checkoutReader(this.read);
+ } catch(err) {
+ return callback(err, null);
+ }
}
// Execute the command
View
11 lib/mongodb/db.js
@@ -1441,11 +1441,16 @@ var __executeQueryCommand = function(self, db_command, options, callback) {
var raw = options['raw'] != null ? options['raw'] : self.raw;
var onAll = options['onAll'] != null ? options['onAll'] : false;
var specifiedConnection = options['connection'] != null ? options['connection'] : null;
-
+
// Correct read preference to default primary if set to false, null or primary
- read = (read == null || read == 'primary' || read == false) ? ReadPreference.PRIMARY : read;
+ if(!(typeof read == 'object') && read._type == 'ReadPreference') {
@eknkc
eknkc added a note

Would this be true in any situation? If it's not object, there is no _type field. This block does not seem to check non ReadPreference object instances for validity.

This is 2 years old :) So I decided to comment first instead of sending a pull request, I might be missing something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ read = (read == null || read == 'primary' || read == false) ? ReadPreference.PRIMARY : read;
+ if(!ReadPreference.isValid(read)) return callback(new Error("Illegal readPreference mode specified, " + read));
+ } else if(typeof read == 'object' && read._type == 'ReadPreference') {
+ if(!read.isValid()) return callback(new Error("Illegal readPreference mode specified, " + read.mode));
+ }
+
// If we have a read preference set and we are a mongos pass the read preference on to the mongos instance,
- // TODO Fix this if not specified
if(self.serverConfig.isMongos() && read != null && read != false) {
db_command.setMongosReadPreference(read);
}
Please sign in to comment.
Something went wrong with that request. Please try again.