Skip to content

Commit 06bbef2

Browse files
kvwalkerdaprahamian
authored andcommitted
fix(ReadPreference): only allow valid ReadPreference modes
Fixes NODE-1425
1 parent ff7166d commit 06bbef2

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/core/topologies/read_preference.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
* @return {ReadPreference}
1313
*/
1414
const ReadPreference = function(mode, tags, options) {
15+
if (!ReadPreference.isValid(mode)) {
16+
throw new TypeError(`provided mode ${mode} is an invalid ReadPreference mode`);
17+
}
18+
1519
// TODO(major): tags MUST be an array of tagsets
1620
if (tags && !Array.isArray(tags)) {
1721
console.warn(
@@ -43,7 +47,7 @@ const ReadPreference = function(mode, tags, options) {
4347
this.minWireVersion = 5;
4448
}
4549

46-
if (this.mode === ReadPreference.PRIMARY || this.mode === true) {
50+
if (this.mode === ReadPreference.PRIMARY) {
4751
if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) {
4852
throw new TypeError('Primary read preference cannot be combined with tags');
4953
}
@@ -77,8 +81,8 @@ const VALID_MODES = [
7781
ReadPreference.SECONDARY,
7882
ReadPreference.SECONDARY_PREFERRED,
7983
ReadPreference.NEAREST,
80-
true,
81-
false,
84+
// true,
85+
// false,
8286
null
8387
];
8488

test/functional/readpreference_tests.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var test = require('./shared').assert;
33
var setupDatabase = require('./shared').setupDatabase;
4+
const expect = require('chai').expect;
45

56
describe('ReadPreference', function() {
67
before(function() {
@@ -546,4 +547,18 @@ describe('ReadPreference', function() {
546547
});
547548
}
548549
});
550+
551+
it('Should throw an error on an invalid readPreference', function(done) {
552+
const configuration = this.configuration;
553+
554+
const client = configuration.newClient();
555+
client.connect((err, client) => {
556+
const db = client.db(configuration.db);
557+
expect(db.collection.bind(db, 'test', { readPreference: 'invalid' })).to.throw(
558+
'provided mode invalid is an invalid ReadPreference mode'
559+
);
560+
client.close();
561+
done();
562+
});
563+
});
549564
});

0 commit comments

Comments
 (0)