Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit f88da4e

Browse files
committed
chore(wireVersion): update max-wire-version
Also updates error messages for wire-version incompatibility. Fixes NODE-1697
1 parent 2111cdb commit f88da4e

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

lib/connection/connect.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const Query = require('./commands').Query;
66
const createClientInfo = require('../topologies/shared').createClientInfo;
77
const MongoError = require('../error').MongoError;
88
const defaultAuthProviders = require('../auth/defaultAuthProviders').defaultAuthProviders;
9+
const WIRE_CONSTANTS = require('../wireprotocol/constants');
10+
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
11+
const MAX_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_SERVER_VERSION;
12+
const MIN_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_WIRE_VERSION;
13+
const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION;
914
let AUTH_PROVIDERS;
1015

1116
function connect(options, callback) {
@@ -44,10 +49,6 @@ function connect(options, callback) {
4449
});
4550
}
4651

47-
function isSupportedServer(ismaster) {
48-
return ismaster && typeof ismaster.maxWireVersion === 'number' && ismaster.maxWireVersion >= 2;
49-
}
50-
5152
function getSaslSupportedMechs(options) {
5253
if (!(options && options.credentials)) {
5354
return {};
@@ -71,6 +72,34 @@ function getSaslSupportedMechs(options) {
7172
return { saslSupportedMechs: `${authSource}.${user}` };
7273
}
7374

75+
function checkSupportedServer(ismaster, options) {
76+
const serverVersionHighEnough =
77+
ismaster &&
78+
typeof ismaster.maxWireVersion === 'number' &&
79+
ismaster.maxWireVersion >= MIN_SUPPORTED_WIRE_VERSION;
80+
const serverVersionLowEnough =
81+
ismaster &&
82+
typeof ismaster.minWireVersion === 'number' &&
83+
ismaster.minWireVersion <= MAX_SUPPORTED_WIRE_VERSION;
84+
85+
if (serverVersionHighEnough) {
86+
if (serverVersionLowEnough) {
87+
return null;
88+
}
89+
90+
const message = `Server at ${options.host}:${options.port} reports minimum wire version ${
91+
ismaster.minWireVersion
92+
}, but this version of the Node.js Driver requires at most ${MAX_SUPPORTED_WIRE_VERSION} (MongoDB ${MAX_SUPPORTED_SERVER_VERSION})`;
93+
return new MongoError(message);
94+
}
95+
96+
const message = `Server at ${options.host}:${
97+
options.port
98+
} reports maximum wire version ${ismaster.maxWireVersion ||
99+
0}, but this version of the Node.js Driver requires at least ${MIN_SUPPORTED_WIRE_VERSION} (MongoDB ${MIN_SUPPORTED_SERVER_VERSION})`;
100+
return new MongoError(message);
101+
}
102+
74103
function performInitialHandshake(conn, options, callback) {
75104
let compressors = [];
76105
if (options.compression && options.compression.compressors) {
@@ -98,23 +127,9 @@ function performInitialHandshake(conn, options, callback) {
98127
return;
99128
}
100129

101-
if (!isSupportedServer(ismaster)) {
102-
const latestSupportedVersion = '2.6';
103-
const latestSupportedMaxWireVersion = 2;
104-
const message =
105-
'Server at ' +
106-
options.host +
107-
':' +
108-
options.port +
109-
' reports wire version ' +
110-
(ismaster.maxWireVersion || 0) +
111-
', but this version of the Node.js Driver requires at least ' +
112-
latestSupportedMaxWireVersion +
113-
' (MongoDB' +
114-
latestSupportedVersion +
115-
').';
116-
117-
callback(new MongoError(message), null);
130+
const supportedServerErr = checkSupportedServer(ismaster, options);
131+
if (supportedServerErr) {
132+
callback(supportedServerErr, null);
118133
return;
119134
}
120135

lib/sdam/topology_description.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
const ServerType = require('./server_description').ServerType;
33
const ServerDescription = require('./server_description').ServerDescription;
44
const ReadPreference = require('../topologies/read_preference');
5+
const WIRE_CONSTANTS = require('../wireprotocol/constants');
56

67
// contstants related to compatability checks
7-
const MIN_SUPPORTED_SERVER_VERSION = '2.6';
8-
const MIN_SUPPORTED_WIRE_VERSION = 2;
9-
const MAX_SUPPORTED_WIRE_VERSION = 5;
8+
const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION;
9+
const MAX_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_SERVER_VERSION;
10+
const MIN_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_WIRE_VERSION;
11+
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
1012

1113
// An enumeration of topology types we know about
1214
const TopologyType = {
@@ -66,7 +68,7 @@ class TopologyDescription {
6668
this.compatible = false;
6769
this.compatibilityError = `Server at ${serverDescription.address} requires wire version ${
6870
serverDescription.minWireVersion
69-
}, but this version of the driver only supports up to ${MAX_SUPPORTED_WIRE_VERSION}.`;
71+
}, but this version of the driver only supports up to ${MAX_SUPPORTED_WIRE_VERSION} (MongoDB ${MAX_SUPPORTED_SERVER_VERSION})`;
7072
}
7173

7274
if (serverDescription.maxWireVersion < MIN_SUPPORTED_WIRE_VERSION) {

lib/wireprotocol/constants.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const MIN_SUPPORTED_SERVER_VERSION = '2.6';
4+
const MAX_SUPPORTED_SERVER_VERSION = '4.2';
5+
const MIN_SUPPORTED_WIRE_VERSION = 2;
6+
const MAX_SUPPORTED_WIRE_VERSION = 8;
7+
8+
module.exports = {
9+
MIN_SUPPORTED_SERVER_VERSION,
10+
MAX_SUPPORTED_SERVER_VERSION,
11+
MIN_SUPPORTED_WIRE_VERSION,
12+
MAX_SUPPORTED_WIRE_VERSION
13+
};

0 commit comments

Comments
 (0)