lib/mongodb/mongodb-core/lib/wireprotocol/3_2_support.js
function decorateWithSessionsData(command, session, options) {
  if (!session) {
    return;
  }

  // first apply non-transaction-specific sessions data
  const serverSession = session.serverSession;
  const inTransaction = session.inTransaction() || isTransactionCommand(command);
  const isRetryableWrite = options.willRetryWrite;

  if (serverSession.txnNumber && (isRetryableWrite || inTransaction)) {
    command.txnNumber = BSON.Long.fromNumber(serverSession.txnNumber);
  }

  // now attempt to apply transaction-specific sessions data
  if (!inTransaction) {
    if (session.transaction.state !== TxnState.NO_TRANSACTION) {
      session.transaction.transition(TxnState.NO_TRANSACTION);
    }

    // for causal consistency
    if (session.supports.causalConsistency && session.operationTime) {
      command.readConcern = command.readConcern || {};
      Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
    }

    return;
  }

  if (options.readPreference && !options.readPreference.equals(ReadPreference.primary)) {
    return new MongoError(
      `Read preference in a transaction must be primary, not: ${options.readPreference.mode}`
    );
  }

  // `autocommit` must always be false to differentiate from retryable writes
  command.autocommit = false;

  if (session.transaction.state === TxnState.STARTING_TRANSACTION) {
    session.transaction.transition(TxnState.TRANSACTION_IN_PROGRESS);
    command.startTransaction = true;

    const readConcern =
      session.transaction.options.readConcern || session.clientOptions.readConcern;
    if (readConcern) {
      command.readConcern = readConcern;
    }

    if (session.supports.causalConsistency && session.operationTime) {
      command.readConcern = command.readConcern || {};
      Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
    }
  }
}
function decorateWithSessionsData(command, session, options) {
  if (!session) {
    return;
  }

  // first apply non-transaction-specific sessions data
  const serverSession = session.serverSession;
  const inTransaction = session.inTransaction() || isTransactionCommand(command);
  const isRetryableWrite = options.willRetryWrite;

  if (serverSession.txnNumber && (isRetryableWrite || inTransaction)) {
    command.txnNumber = BSON.Long.fromNumber(serverSession.txnNumber);
  }

  // now attempt to apply transaction-specific sessions data
  if (!inTransaction) {
    if (session.transaction.state !== TxnState.NO_TRANSACTION) {
      session.transaction.transition(TxnState.NO_TRANSACTION);
    }

    // for causal consistency
    if (session.supports.causalConsistency && session.operationTime) {
      command.readConcern = command.readConcern || {};
      Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
    }

    return;
  }

  if (options.readPreference && !options.readPreference.equals(ReadPreference.primary)) {
    return new MongoError(
      `Read preference in a transaction must be primary, not: ${options.readPreference.mode}`
    );
  }

  // `autocommit` must always be false to differentiate from retryable writes
  command.autocommit = false;

  if (session.transaction.state === TxnState.STARTING_TRANSACTION) {
    session.transaction.transition(TxnState.TRANSACTION_IN_PROGRESS);
    command.startTransaction = true;

    const readConcern =
      session.transaction.options.readConcern || session.clientOptions.readConcern;
    if (readConcern) {
      command.readConcern = readConcern;
    }

    if (session.supports.causalConsistency && session.operationTime) {
      command.readConcern = command.readConcern || {};
      Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
    }
  }
}
function decorateWithSessionsData ( command , session , options ) { if ( ! session ) { return ; } // first apply non-transaction-specific sessions data const serverSession = session . serverSession ; const inTransaction = session . inTransaction ( ) || isTransactionCommand ( command ) ; const isRetryableWrite = options . willRetryWrite ; if ( serverSession . txnNumber && ( isRetryableWrite || inTransaction ) ) { command . txnNumber = BSON . Long . fromNumber ( serverSession . txnNumber ) ; } // now attempt to apply transaction-specific sessions data if ( ! inTransaction ) { if ( session . transaction . state !== TxnState . NO_TRANSACTION ) { session . transaction . transition ( TxnState . NO_TRANSACTION ) ; } // for causal consistency if ( session . supports . causalConsistency && session . operationTime ) { command . readConcern = command . readConcern || { } ; Object . assign ( command . readConcern , { afterClusterTime : session . operationTime } ) ; } return ; } if ( options . readPreference && ! options . readPreference . equals ( ReadPreference . primary ) ) { return new MongoError ( ` ${ options . readPreference . mode } ` ) ; } // `autocommit` must always be false to differentiate from retryable writes command . autocommit = false ; if ( session . transaction . state === TxnState . STARTING_TRANSACTION ) { session . transaction . transition ( TxnState . TRANSACTION_IN_PROGRESS ) ; command . startTransaction = true ; const readConcern = session . transaction . options . readConcern || session . clientOptions . readConcern ; if ( readConcern ) { command . readConcern = readConcern ; } if ( session . supports . causalConsistency && session . operationTime ) { command . readConcern = command . readConcern || { } ; Object . assign ( command . readConcern , { afterClusterTime : session . operationTime } ) ; } } }
Optionally decorate a command with sessions specific keys

@param {Object} command the command to decorate
@param {ClientSession} session the session tracking transaction state
@param {Object} [options] Optional settings passed to calling operation
@param {Function} [callback] Optional callback passed from calling operation
@return {MongoError|null} An error, if some error condition was met
Optionally decorate a command with sessions specific keys