MCPcopy
hub / github.com/mongodb/node-mongodb-native / performInitialHandshake

Function performInitialHandshake

src/cmap/connect.ts:90–210  ·  view source on GitHub ↗
(
  conn: Connection,
  options: ConnectionOptions
)

Source from the content-addressed store, hash-verified

88}
89
90export async function performInitialHandshake(
91 conn: Connection,
92 options: ConnectionOptions
93): Promise<void> {
94 const credentials = options.credentials;
95
96 if (credentials) {
97 if (
98 !(credentials.mechanism === AuthMechanism.MONGODB_DEFAULT) &&
99 !options.authProviders.getOrCreateProvider(
100 credentials.mechanism,
101 credentials.mechanismProperties
102 )
103 ) {
104 throw new MongoInvalidArgumentError(`AuthMechanism '${credentials.mechanism}' not supported`);
105 }
106 }
107
108 const authContext = new AuthContext(conn, credentials, options);
109 conn.authContext = authContext;
110
111 // If we encounter an error preparing the handshake document, do NOT apply backpressure labels. Errors
112 // encountered building the handshake document are all client-side, and do not indicate an overloaded server.
113 const handshakeDoc = await prepareHandshakeDocument(authContext);
114
115 // @ts-expect-error: TODO(NODE-5141): The options need to be filtered properly, Connection options differ from Command options
116 const handshakeOptions: CommandOptions = { ...options, raw: false };
117 if (typeof options.connectTimeoutMS === 'number') {
118 // The handshake technically is a monitoring check, so its socket timeout should be connectTimeoutMS
119 handshakeOptions.socketTimeoutMS = options.connectTimeoutMS;
120 }
121
122 const start = new Date().getTime();
123
124 const response = await executeHandshake(handshakeDoc, handshakeOptions);
125
126 if (!('isWritablePrimary' in response)) {
127 // Provide hello-style response document.
128 response.isWritablePrimary = response[LEGACY_HELLO_COMMAND];
129 }
130
131 if (response.helloOk) {
132 conn.helloOk = true;
133 }
134
135 const supportedServerErr = checkSupportedServer(response, options);
136 if (supportedServerErr) {
137 throw supportedServerErr;
138 }
139
140 if (options.loadBalanced) {
141 if (!response.serviceId) {
142 throw new MongoCompatibilityError(
143 'Driver attempted to initialize in load balancing mode, ' +
144 'but the server does not support this mode.'
145 );
146 }
147 }

Callers 2

checkServerFunction · 0.90
connectFunction · 0.85

Calls 8

needsRetryableWriteLabelFunction · 0.90
prepareHandshakeDocumentFunction · 0.85
executeHandshakeFunction · 0.85
checkSupportedServerFunction · 0.85
getOrCreateProviderMethod · 0.80
resolveAuthMechanismMethod · 0.80
addErrorLabelMethod · 0.80
authMethod · 0.45

Tested by

no test coverage detected