Skip to content

Commit b0f83b8

Browse files
committed
refactor: notify when attempting to use deprecated events/options
When using the unified topology, a number of options and events are now deprecated. This will notify users of that, and point them to our documentation. NODE-2348
1 parent 3b076b3 commit b0f83b8

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/core/sdam/topology.js

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const SrvPoller = require('./srv_polling').SrvPoller;
2323
const getMMAPError = require('../topologies/shared').getMMAPError;
2424
const makeStateMachine = require('../utils').makeStateMachine;
2525
const eachAsync = require('../utils').eachAsync;
26+
const emitDeprecationWarning = require('../../utils').emitDeprecationWarning;
2627

2728
const common = require('./common');
2829
const drainTimerQueue = common.drainTimerQueue;
@@ -69,6 +70,13 @@ const stateTransition = makeStateMachine({
6970
[STATE_CLOSING]: [STATE_CLOSING, STATE_CLOSED]
7071
});
7172

73+
const DEPRECATED_OPTIONS = new Set([
74+
'autoReconnect',
75+
'reconnectTries',
76+
'reconnectInterval',
77+
'bufferMaxEntries'
78+
]);
79+
7280
/**
7381
* A container of server instances representing a connection to a MongoDB topology.
7482
*
@@ -110,6 +118,14 @@ class Topology extends EventEmitter {
110118
}
111119

112120
options = Object.assign({}, common.TOPOLOGY_DEFAULTS, options);
121+
DEPRECATED_OPTIONS.forEach(optionName => {
122+
if (options[optionName]) {
123+
emitDeprecationWarning(
124+
`The option \`${optionName}\` is incompatible with the unified topology, please read more by visiting http://bit.ly/2D8WfT6`,
125+
'DeprecationWarning'
126+
);
127+
}
128+
});
113129

114130
const topologyType = topologyTypeFromSeedlist(seedlist, options);
115131
const topologyId = globalTopologyCounter++;

lib/operations/connect.js

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const ReadPreference = require('../core').ReadPreference;
1515
const ReplSet = require('../topologies/replset');
1616
const Server = require('../topologies/server');
1717
const ServerSessionPool = require('../core').Sessions.ServerSessionPool;
18+
const emitDeprecationWarning = require('../utils').emitDeprecationWarning;
1819

1920
let client;
2021
function loadClient() {
@@ -438,6 +439,18 @@ function createServer(mongoClient, options, callback) {
438439
});
439440
}
440441

442+
const DEPRECATED_UNIFIED_EVENTS = new Set(['reconnect', 'reconnectFailed', 'attemptReconnect']);
443+
function registerDeprecatedEventNotifiers(client) {
444+
client.on('newListener', eventName => {
445+
if (DEPRECATED_UNIFIED_EVENTS.has(eventName)) {
446+
emitDeprecationWarning(
447+
`The \`${eventName}\` event is no longer supported by the unified topology, please read more by visiting http://bit.ly/2D8WfT6`,
448+
'DeprecationWarning'
449+
);
450+
}
451+
});
452+
}
453+
441454
function createTopology(mongoClient, topologyType, options, callback) {
442455
// Pass in the promise library
443456
options.promiseLibrary = mongoClient.s.promiseLibrary;
@@ -456,6 +469,7 @@ function createTopology(mongoClient, topologyType, options, callback) {
456469
topology = new ReplSet(servers, options);
457470
} else if (topologyType === 'unified') {
458471
topology = new NativeTopology(options.servers, options);
472+
registerDeprecatedEventNotifiers(mongoClient);
459473
}
460474

461475
// Add listeners

lib/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -712,5 +712,6 @@ module.exports = {
712712
deprecateOptions,
713713
SUPPORTS,
714714
MongoDBNamespace,
715-
resolveReadPreference
715+
resolveReadPreference,
716+
emitDeprecationWarning
716717
};

0 commit comments

Comments
 (0)