@@ -6,6 +6,11 @@ const Query = require('./commands').Query;
6
6
const createClientInfo = require ( '../topologies/shared' ) . createClientInfo ;
7
7
const MongoError = require ( '../error' ) . MongoError ;
8
8
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 ;
9
14
let AUTH_PROVIDERS ;
10
15
11
16
function connect ( options , callback ) {
@@ -44,10 +49,6 @@ function connect(options, callback) {
44
49
} ) ;
45
50
}
46
51
47
- function isSupportedServer ( ismaster ) {
48
- return ismaster && typeof ismaster . maxWireVersion === 'number' && ismaster . maxWireVersion >= 2 ;
49
- }
50
-
51
52
function getSaslSupportedMechs ( options ) {
52
53
if ( ! ( options && options . credentials ) ) {
53
54
return { } ;
@@ -71,6 +72,34 @@ function getSaslSupportedMechs(options) {
71
72
return { saslSupportedMechs : `${ authSource } .${ user } ` } ;
72
73
}
73
74
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
+
74
103
function performInitialHandshake ( conn , options , callback ) {
75
104
let compressors = [ ] ;
76
105
if ( options . compression && options . compression . compressors ) {
@@ -98,23 +127,9 @@ function performInitialHandshake(conn, options, callback) {
98
127
return ;
99
128
}
100
129
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 ) ;
118
133
return ;
119
134
}
120
135
0 commit comments