@@ -119,93 +119,92 @@ function performInitialHandshake(
119
119
120
120
const authContext = new AuthContext ( conn , credentials , options ) ;
121
121
conn . authContext = authContext ;
122
- prepareHandshakeDocument ( authContext , ( err , handshakeDoc ) => {
123
- if ( err || ! handshakeDoc ) {
124
- return callback ( err ) ;
125
- }
126
-
127
- const handshakeOptions : Document = Object . assign ( { } , options ) ;
128
- if ( typeof options . connectTimeoutMS === 'number' ) {
129
- // The handshake technically is a monitoring check, so its socket timeout should be connectTimeoutMS
130
- handshakeOptions . socketTimeoutMS = options . connectTimeoutMS ;
131
- }
132
-
133
- const start = new Date ( ) . getTime ( ) ;
134
- conn . command ( ns ( 'admin.$cmd' ) , handshakeDoc , handshakeOptions , ( err , response ) => {
135
- if ( err ) {
136
- callback ( err ) ;
137
- return ;
122
+ prepareHandshakeDocument ( authContext ) . then (
123
+ handshakeDoc => {
124
+ const handshakeOptions : Document = Object . assign ( { } , options ) ;
125
+ if ( typeof options . connectTimeoutMS === 'number' ) {
126
+ // The handshake technically is a monitoring check, so its socket timeout should be connectTimeoutMS
127
+ handshakeOptions . socketTimeoutMS = options . connectTimeoutMS ;
138
128
}
139
129
140
- if ( response ?. ok === 0 ) {
141
- callback ( new MongoServerError ( response ) ) ;
142
- return ;
143
- }
130
+ const start = new Date ( ) . getTime ( ) ;
131
+ conn . command ( ns ( 'admin.$cmd' ) , handshakeDoc , handshakeOptions , ( err , response ) => {
132
+ if ( err ) {
133
+ callback ( err ) ;
134
+ return ;
135
+ }
144
136
145
- if ( ! ( 'isWritablePrimary' in response ) ) {
146
- // Provide hello-style response document.
147
- response . isWritablePrimary = response [ LEGACY_HELLO_COMMAND ] ;
148
- }
137
+ if ( response ?. ok === 0 ) {
138
+ callback ( new MongoServerError ( response ) ) ;
139
+ return ;
140
+ }
149
141
150
- if ( response . helloOk ) {
151
- conn . helloOk = true ;
152
- }
142
+ if ( ! ( 'isWritablePrimary' in response ) ) {
143
+ // Provide hello-style response document.
144
+ response . isWritablePrimary = response [ LEGACY_HELLO_COMMAND ] ;
145
+ }
153
146
154
- const supportedServerErr = checkSupportedServer ( response , options ) ;
155
- if ( supportedServerErr ) {
156
- callback ( supportedServerErr ) ;
157
- return ;
158
- }
147
+ if ( response . helloOk ) {
148
+ conn . helloOk = true ;
149
+ }
159
150
160
- if ( options . loadBalanced ) {
161
- if ( ! response . serviceId ) {
162
- return callback (
163
- new MongoCompatibilityError (
164
- 'Driver attempted to initialize in load balancing mode, ' +
165
- 'but the server does not support this mode.'
166
- )
167
- ) ;
151
+ const supportedServerErr = checkSupportedServer ( response , options ) ;
152
+ if ( supportedServerErr ) {
153
+ callback ( supportedServerErr ) ;
154
+ return ;
168
155
}
169
- }
170
156
171
- // NOTE: This is metadata attached to the connection while porting away from
172
- // handshake being done in the `Server` class. Likely, it should be
173
- // relocated, or at very least restructured.
174
- conn . hello = response ;
175
- conn . lastHelloMS = new Date ( ) . getTime ( ) - start ;
176
-
177
- if ( ! response . arbiterOnly && credentials ) {
178
- // store the response on auth context
179
- authContext . response = response ;
180
-
181
- const resolvedCredentials = credentials . resolveAuthMechanism ( response ) ;
182
- const provider = AUTH_PROVIDERS . get ( resolvedCredentials . mechanism ) ;
183
- if ( ! provider ) {
184
- return callback (
185
- new MongoInvalidArgumentError (
186
- `No AuthProvider for ${ resolvedCredentials . mechanism } defined.`
187
- )
188
- ) ;
157
+ if ( options . loadBalanced ) {
158
+ if ( ! response . serviceId ) {
159
+ return callback (
160
+ new MongoCompatibilityError (
161
+ 'Driver attempted to initialize in load balancing mode, ' +
162
+ 'but the server does not support this mode.'
163
+ )
164
+ ) ;
165
+ }
189
166
}
190
- provider . auth ( authContext , err => {
191
- if ( err ) {
192
- if ( err instanceof MongoError ) {
193
- err . addErrorLabel ( MongoErrorLabel . HandshakeError ) ;
194
- if ( needsRetryableWriteLabel ( err , response . maxWireVersion ) ) {
195
- err . addErrorLabel ( MongoErrorLabel . RetryableWriteError ) ;
167
+
168
+ // NOTE: This is metadata attached to the connection while porting away from
169
+ // handshake being done in the `Server` class. Likely, it should be
170
+ // relocated, or at very least restructured.
171
+ conn . hello = response ;
172
+ conn . lastHelloMS = new Date ( ) . getTime ( ) - start ;
173
+
174
+ if ( ! response . arbiterOnly && credentials ) {
175
+ // store the response on auth context
176
+ authContext . response = response ;
177
+
178
+ const resolvedCredentials = credentials . resolveAuthMechanism ( response ) ;
179
+ const provider = AUTH_PROVIDERS . get ( resolvedCredentials . mechanism ) ;
180
+ if ( ! provider ) {
181
+ return callback (
182
+ new MongoInvalidArgumentError (
183
+ `No AuthProvider for ${ resolvedCredentials . mechanism } defined.`
184
+ )
185
+ ) ;
186
+ }
187
+ provider . auth ( authContext , err => {
188
+ if ( err ) {
189
+ if ( err instanceof MongoError ) {
190
+ err . addErrorLabel ( MongoErrorLabel . HandshakeError ) ;
191
+ if ( needsRetryableWriteLabel ( err , response . maxWireVersion ) ) {
192
+ err . addErrorLabel ( MongoErrorLabel . RetryableWriteError ) ;
193
+ }
196
194
}
195
+ return callback ( err ) ;
197
196
}
198
- return callback ( err ) ;
199
- }
200
- callback ( undefined , conn ) ;
201
- } ) ;
197
+ callback ( undefined , conn ) ;
198
+ } ) ;
202
199
203
- return ;
204
- }
200
+ return ;
201
+ }
205
202
206
- callback ( undefined , conn ) ;
207
- } ) ;
208
- } ) ;
203
+ callback ( undefined , conn ) ;
204
+ } ) ;
205
+ } ,
206
+ error => callback ( error )
207
+ ) ;
209
208
}
210
209
211
210
export interface HandshakeDocument extends Document {
@@ -226,10 +225,9 @@ export interface HandshakeDocument extends Document {
226
225
*
227
226
* This function is only exposed for testing purposes.
228
227
*/
229
- export function prepareHandshakeDocument (
230
- authContext : AuthContext ,
231
- callback : Callback < HandshakeDocument >
232
- ) {
228
+ export async function prepareHandshakeDocument (
229
+ authContext : AuthContext
230
+ ) : Promise < HandshakeDocument > {
233
231
const options = authContext . options ;
234
232
const compressors = options . compressors ? options . compressors : [ ] ;
235
233
const { serverApi } = authContext . connection ;
@@ -253,23 +251,19 @@ export function prepareHandshakeDocument(
253
251
const provider = AUTH_PROVIDERS . get ( AuthMechanism . MONGODB_SCRAM_SHA256 ) ;
254
252
if ( ! provider ) {
255
253
// This auth mechanism is always present.
256
- return callback (
257
- new MongoInvalidArgumentError (
258
- `No AuthProvider for ${ AuthMechanism . MONGODB_SCRAM_SHA256 } defined.`
259
- )
254
+ throw new MongoInvalidArgumentError (
255
+ `No AuthProvider for ${ AuthMechanism . MONGODB_SCRAM_SHA256 } defined.`
260
256
) ;
261
257
}
262
- return provider . prepare ( handshakeDoc , authContext , callback ) ;
258
+ return provider . prepare ( handshakeDoc , authContext ) ;
263
259
}
264
260
const provider = AUTH_PROVIDERS . get ( credentials . mechanism ) ;
265
261
if ( ! provider ) {
266
- return callback (
267
- new MongoInvalidArgumentError ( `No AuthProvider for ${ credentials . mechanism } defined.` )
268
- ) ;
262
+ throw new MongoInvalidArgumentError ( `No AuthProvider for ${ credentials . mechanism } defined.` ) ;
269
263
}
270
- return provider . prepare ( handshakeDoc , authContext , callback ) ;
264
+ return provider . prepare ( handshakeDoc , authContext ) ;
271
265
}
272
- callback ( undefined , handshakeDoc ) ;
266
+ return handshakeDoc ;
273
267
}
274
268
275
269
/** @public */
0 commit comments