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

Method command

src/sdam/server.ts:282–384  ·  view source on GitHub ↗
(
    operation: AbstractOperation<TResult>,
    timeoutContext: TimeoutContext
  )

Source from the content-addressed store, hash-verified

280 }
281
282 public async command<TResult>(
283 operation: AbstractOperation<TResult>,
284 timeoutContext: TimeoutContext
285 ): Promise<InstanceType<typeof operation.SERVER_COMMAND_RESPONSE_TYPE>> {
286 if (this.s.state === STATE_CLOSING || this.s.state === STATE_CLOSED) {
287 throw new MongoServerClosedError();
288 }
289 const session = operation.session;
290
291 let conn = session?.pinnedConnection;
292
293 this.incrementOperationCount();
294 if (conn == null) {
295 try {
296 conn = await this.pool.checkOut({ timeoutContext, signal: operation.options.signal });
297 } catch (checkoutError) {
298 this.decrementOperationCount();
299 if (!(checkoutError instanceof PoolClearedError)) this.handleError(checkoutError);
300 throw checkoutError;
301 }
302 }
303
304 let reauthPromise: Promise<void> | null = null;
305 const cleanup = () => {
306 this.decrementOperationCount();
307 if (session?.pinnedConnection !== conn) {
308 if (reauthPromise != null) {
309 // The reauth promise only exists if it hasn't thrown.
310 const checkBackIn = () => {
311 this.pool.checkIn(conn);
312 };
313 void reauthPromise.then(checkBackIn, checkBackIn);
314 } else {
315 this.pool.checkIn(conn);
316 }
317 }
318 };
319
320 let cmd;
321 try {
322 cmd = operation.buildCommand(conn, session);
323 } catch (e) {
324 cleanup();
325 throw e;
326 }
327
328 const options = operation.buildOptions(timeoutContext);
329 const ns = operation.ns;
330
331 if (this.loadBalanced && isPinnableCommand(cmd, session) && !session?.pinnedConnection) {
332 session?.pin(conn);
333 }
334
335 options.directConnection = this.topology.s.options.directConnection;
336
337 const omitReadPreference =
338 operation instanceof AggregateOperation &&
339 operation.hasWriteStage &&

Callers 3

checkServerFunction · 0.45
measureRoundTripTimeMethod · 0.45

Calls 15

handleErrorMethod · 0.95
decorateCommandErrorMethod · 0.95
maxWireVersionFunction · 0.90
throwIfWriteConcernErrorFunction · 0.90
squashErrorFunction · 0.90
abortableFunction · 0.90
isPinnableCommandFunction · 0.85
checkOutMethod · 0.80
pinMethod · 0.80
reauthenticateMethod · 0.65

Tested by

no test coverage detected