{"size": 1020, "ext": "js", "max_stars_repo_path": "lib/seo_crawler.js", "max_stars_repo_name": "manuelxaguilar/seo-crawler", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/seo_crawler.js", "max_issues_repo_name": "manuelxaguilar/seo-crawler", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/seo_crawler.js", "max_forks_repo_name": "manuelxaguilar/seo-crawler", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.8780487805, "max_line_length": 57, "alphanum_fraction": 0.6460784314}
const jobs = require('./utils/jobs');
const cache = require('./utils/cache');
const crawler = require('./utils/crawler');

const seoCrawler = options => async (req, res) => {
  const { jobTitle, renderHTML, crawlerList } = options;
  const siteUrl =
    options.siteUrl ||
    process.env.SITE_URL || 
    req.headers && req.headers.host;
  const isCrawler = crawler.isCrawler({
    headers: req.headers,
    customCrawlers: crawlerList
  });

  if (!isCrawler) {
    const cachedContent = await cache.get(req, jobTitle);

    if (cachedContent.reply) {
      return res.send(cachedContent.reply);
    } else if (cachedContent.newKey) {
      cache.set(res, cachedContent.newKey);

      const newJob = jobs.create(req, options);
      jobs.process({
        jobTitle,
        siteUrl
      });
      jobs.handleResults(newJob, res, renderHTML);

      return;
    } else if (cachedContent.error) {
      console.error(cachedContent.error);
    }
  }

  return res.sendFile(renderHTML);
};

module.exports = seoCrawler;
