Skip to content

Commit 91a67e0

Browse files
authored
feat(NODE-3793): Remove offensive language from code and tests (#3082)
1 parent 8a3bab7 commit 91a67e0

File tree

8 files changed

+132
-51
lines changed

8 files changed

+132
-51
lines changed

src/cmap/commands.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let _requestId = 0;
1212

1313
// Query flags
1414
const OPTS_TAILABLE_CURSOR = 2;
15-
const OPTS_SLAVE = 4;
15+
const OPTS_SECONDARY = 4;
1616
const OPTS_OPLOG_REPLAY = 8;
1717
const OPTS_NO_CURSOR_TIMEOUT = 16;
1818
const OPTS_AWAIT_DATA = 32;
@@ -41,7 +41,7 @@ export interface OpQueryOptions extends CommandOptions {
4141
ignoreUndefined?: boolean;
4242
maxBsonSize?: number;
4343
checkKeys?: boolean;
44-
slaveOk?: boolean;
44+
secondaryOk?: boolean;
4545

4646
requestId?: number;
4747
moreToCome?: boolean;
@@ -67,7 +67,7 @@ export class Query {
6767
checkKeys: boolean;
6868
batchSize: number;
6969
tailable: boolean;
70-
slaveOk: boolean;
70+
secondaryOk: boolean;
7171
oplogReplay: boolean;
7272
noCursorTimeout: boolean;
7373
awaitData: boolean;
@@ -112,7 +112,7 @@ export class Query {
112112

113113
// Flags
114114
this.tailable = false;
115-
this.slaveOk = typeof options.slaveOk === 'boolean' ? options.slaveOk : false;
115+
this.secondaryOk = typeof options.secondaryOk === 'boolean' ? options.secondaryOk : false;
116116
this.oplogReplay = false;
117117
this.noCursorTimeout = false;
118118
this.awaitData = false;
@@ -146,8 +146,8 @@ export class Query {
146146
flags |= OPTS_TAILABLE_CURSOR;
147147
}
148148

149-
if (this.slaveOk) {
150-
flags |= OPTS_SLAVE;
149+
if (this.secondaryOk) {
150+
flags |= OPTS_SECONDARY;
151151
}
152152

153153
if (this.oplogReplay) {

src/cmap/connection.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export interface QueryOptions extends BSONSerializeOptions {
9696
/** @internal */
9797
export interface CommandOptions extends BSONSerializeOptions {
9898
command?: boolean;
99-
slaveOk?: boolean;
99+
secondaryOk?: boolean;
100100
/** Specify read preference if command supports it */
101101
readPreference?: ReadPreferenceLike;
102102
raw?: boolean;
@@ -427,7 +427,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
427427
numberToReturn: -1,
428428
checkKeys: false,
429429
// This value is not overridable
430-
slaveOk: readPreference.slaveOk()
430+
secondaryOk: readPreference.secondaryOk()
431431
},
432432
options
433433
);
@@ -472,7 +472,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
472472
numberToReturn,
473473
pre32Limit: typeof limit === 'number' ? limit : undefined,
474474
checkKeys: false,
475-
slaveOk: readPreference.slaveOk()
475+
secondaryOk: readPreference.secondaryOk()
476476
};
477477

478478
if (options.projection) {

src/collection.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ export interface CollectionOptions
112112
extends BSONSerializeOptions,
113113
WriteConcernOptions,
114114
LoggerOptions {
115+
/**
116+
* @deprecated Use readPreference instead
117+
*/
115118
slaveOk?: boolean;
116119
/** Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) */
117120
readConcern?: ReadConcernLike;
@@ -127,7 +130,6 @@ export interface CollectionPrivate {
127130
namespace: MongoDBNamespace;
128131
readPreference?: ReadPreference;
129132
bsonOptions: BSONSerializeOptions;
130-
slaveOk?: boolean;
131133
collectionHint?: Hint;
132134
readConcern?: ReadConcern;
133135
writeConcern?: WriteConcern;
@@ -181,8 +183,7 @@ export class Collection<TSchema extends Document = Document> {
181183
readPreference: ReadPreference.fromOptions(options),
182184
bsonOptions: resolveBSONOptions(options, db),
183185
readConcern: ReadConcern.fromOptions(options),
184-
writeConcern: WriteConcern.fromOptions(options),
185-
slaveOk: options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk
186+
writeConcern: WriteConcern.fromOptions(options)
186187
};
187188
}
188189

src/db.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,18 @@ export class Db {
185185
return this.s.options;
186186
}
187187

188-
// slaveOk specified
188+
/**
189+
* slaveOk specified
190+
* @deprecated Use secondaryOk instead
191+
*/
189192
get slaveOk(): boolean {
193+
return this.secondaryOk;
194+
}
195+
196+
/**
197+
* Check if a secondary can be used (because the read preference is *not* set to primary)
198+
*/
199+
get secondaryOk(): boolean {
190200
return this.s.readPreference?.preference !== 'primary' || false;
191201
}
192202

src/read_preference.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,27 @@ export class ReadPreference {
232232
}
233233

234234
/**
235-
* Indicates that this readPreference needs the "slaveOk" bit when sent over the wire
236-
*
235+
* Indicates that this readPreference needs the "secondaryOk" bit when sent over the wire
236+
* @deprecated Use secondaryOk instead
237237
* @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
238238
*/
239239
slaveOk(): boolean {
240-
const NEEDS_SLAVEOK = new Set<string>([
240+
return this.secondaryOk();
241+
}
242+
243+
/**
244+
* Indicates that this readPreference needs the "SecondaryOk" bit when sent over the wire
245+
* @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
246+
*/
247+
secondaryOk(): boolean {
248+
const NEEDS_SECONDARYOK = new Set<string>([
241249
ReadPreference.PRIMARY_PREFERRED,
242250
ReadPreference.SECONDARY,
243251
ReadPreference.SECONDARY_PREFERRED,
244252
ReadPreference.NEAREST
245253
]);
246254

247-
return NEEDS_SLAVEOK.has(this.mode);
255+
return NEEDS_SECONDARYOK.has(this.mode);
248256
}
249257

250258
/**

test/functional/cursor.test.js

-33
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const { Writable } = require('stream');
1111
const { ReadPreference } = require('../../src/read_preference');
1212
const { ServerType } = require('../../src/sdam/common');
1313
const { formatSort } = require('../../src/sort');
14-
const { FindCursor } = require('../../src/cursor/find_cursor');
1514

1615
describe('Cursor', function () {
1716
before(function () {
@@ -3732,38 +3731,6 @@ describe('Cursor', function () {
37323731
}
37333732
});
37343733

3735-
// NOTE: This is skipped because I don't think its correct or adds value. The expected error
3736-
// is not an error with hasNext (from server), but rather a local TypeError which should
3737-
// be caught anyway. The only solution here would be to wrap the entire top level call
3738-
// in a try/catch which is not going to happen.
3739-
it.skip('Should propagate hasNext errors when using a callback', {
3740-
metadata: {
3741-
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
3742-
},
3743-
test: function (done) {
3744-
const configuration = this.configuration;
3745-
var client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
3746-
client.connect((err, client) => {
3747-
expect(err).to.not.exist;
3748-
this.defer(() => client.close());
3749-
3750-
const db = client.db(configuration.db);
3751-
const cursor = new FindCursor(
3752-
db.s.topology,
3753-
db.s.namespace,
3754-
{},
3755-
{ limit: 0, skip: 0, slaveOk: false, readPreference: 42 }
3756-
);
3757-
3758-
cursor.hasNext(err => {
3759-
test.ok(err !== null);
3760-
test.equal(err.message, 'readPreference must be a ReadPreference instance');
3761-
done();
3762-
});
3763-
});
3764-
}
3765-
});
3766-
37673734
it(
37683735
'should return implicit session to pool when client-side cursor exhausts results on initial query',
37693736
{

test/unit/db.test.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { expect } from 'chai';
2+
3+
import { MongoClient } from '../../src';
4+
import { Db, DbOptions } from '../../src/db';
5+
import { ReadPreference } from '../../src/read_preference';
6+
7+
describe('class Db', function () {
8+
describe('secondaryOk', function () {
9+
const client = new MongoClient('mongodb://localhost:27017');
10+
const legacy_secondary_ok = 'slaveOk';
11+
const secondary_ok = 'secondaryOk';
12+
13+
it('should be false when readPreference is Primary', function () {
14+
const options: DbOptions = { readPreference: ReadPreference.PRIMARY };
15+
const mydb = new Db(client, 'mydb', options);
16+
17+
expect(mydb).property(secondary_ok).to.be.false;
18+
expect(mydb).property(legacy_secondary_ok).to.be.false;
19+
});
20+
21+
it('should be true when readPreference is Primary Preferred', function () {
22+
const options: DbOptions = { readPreference: ReadPreference.PRIMARY_PREFERRED };
23+
const mydb = new Db(client, 'mydb', options);
24+
25+
expect(mydb).property(secondary_ok).to.be.true;
26+
expect(mydb).property(legacy_secondary_ok).to.be.true;
27+
});
28+
29+
it('should be true when readPreference is Secondary', function () {
30+
const options: DbOptions = { readPreference: ReadPreference.SECONDARY };
31+
const mydb = new Db(client, 'mydb', options);
32+
33+
expect(mydb).property(secondary_ok).to.be.true;
34+
expect(mydb).property(legacy_secondary_ok).to.be.true;
35+
});
36+
37+
it('should be true when readPreference is Secondary Preferred', function () {
38+
const options: DbOptions = { readPreference: ReadPreference.SECONDARY_PREFERRED };
39+
const mydb = new Db(client, 'mydb', options);
40+
41+
expect(mydb).property(secondary_ok).to.be.true;
42+
expect(mydb).property(legacy_secondary_ok).to.be.true;
43+
});
44+
45+
it('should be true when readPreference is Nearest', function () {
46+
const options: DbOptions = { readPreference: ReadPreference.NEAREST };
47+
const mydb = new Db(client, 'mydb', options);
48+
49+
expect(mydb).property(secondary_ok).to.be.true;
50+
expect(mydb).property(legacy_secondary_ok).to.be.true;
51+
});
52+
});
53+
});

test/unit/read_preference.test.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ReadPreference } from '../../src';
44

55
describe('class ReadPreference', function () {
66
const maxStalenessSeconds = 1234;
7-
const { PRIMARY, SECONDARY, NEAREST } = ReadPreference;
7+
const { PRIMARY, PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED, NEAREST } = ReadPreference;
88
const TAGS = [{ loc: 'dc' }];
99
describe('::constructor', function () {
1010
it('should accept (mode)', function () {
@@ -132,4 +132,46 @@ describe('class ReadPreference', function () {
132132
).to.throw('Primary read preference cannot be combined with maxStalenessSeconds');
133133
});
134134
});
135+
136+
describe('secondaryOk()', function () {
137+
it('should be false when readPreference is Primary', function () {
138+
const readPreference = ReadPreference.fromOptions({
139+
readPreference: PRIMARY
140+
});
141+
expect(readPreference.secondaryOk()).to.be.false;
142+
expect(readPreference.slaveOk()).to.be.false;
143+
});
144+
145+
it('should be true when readPreference is Primary Preferred', function () {
146+
const readPreference = ReadPreference.fromOptions({
147+
readPreference: PRIMARY_PREFERRED
148+
});
149+
expect(readPreference.secondaryOk()).to.be.true;
150+
expect(readPreference.slaveOk()).to.be.true;
151+
});
152+
153+
it('should be true when readPreference is Secondary', function () {
154+
const readPreference = ReadPreference.fromOptions({
155+
readPreference: SECONDARY
156+
});
157+
expect(readPreference.secondaryOk()).to.be.true;
158+
expect(readPreference.slaveOk()).to.be.true;
159+
});
160+
161+
it('should be true when readPreference is Secondary Preferred', function () {
162+
const readPreference = ReadPreference.fromOptions({
163+
readPreference: SECONDARY_PREFERRED
164+
});
165+
expect(readPreference.secondaryOk()).to.be.true;
166+
expect(readPreference.slaveOk()).to.be.true;
167+
});
168+
169+
it('should be true when readPreference is Nearest', function () {
170+
const readPreference = ReadPreference.fromOptions({
171+
readPreference: NEAREST
172+
});
173+
expect(readPreference.secondaryOk()).to.be.true;
174+
expect(readPreference.slaveOk()).to.be.true;
175+
});
176+
});
135177
});

0 commit comments

Comments
 (0)