<commit_before>'use strict';

const path = require('path');
const fs = require('fs');
const cleanBaseURL = require('clean-base-url');

class HistorySupportAddon {
  /**
   * This addon is used to serve the `index.html` file at every requested
   * URL that begins with `rootURL` and is expecting `text/html` output.
   *
   * @class HistorySupportAddon
   * @constructor
   */
  constructor(project) {
    this.project = project;
    this.name = 'history-support-middleware';
  }

  shouldAddMiddleware(environment) {
    let config = this.project.config(environment);
    let locationType = config.locationType;
    let historySupportMiddleware = config.historySupportMiddleware;

    if (typeof historySupportMiddleware === 'boolean') {
      return historySupportMiddleware;
    }

    return ['auto', 'history'].indexOf(locationType) !== -1;
  }

  serverMiddleware(config) {
    if (this.shouldAddMiddleware(config.options.environment)) {
      this.project.ui.writeWarnLine(
        'Empty `rootURL` is not supported. Disable history support, or use an absolute `rootURL`',
        config.options.rootURL !== ''
      );

      this.addMiddleware(config);
    }
  }

  addMiddleware(config) {
    let app = config.app;
    let options = config.options;
    let watcher = options.watcher;

    let baseURL = options.rootURL === '' ? '/' : cleanBaseURL(options.rootURL || options.baseURL);

    app.use(async (req, _, next) => {
      try {
        const results = await watcher;
        if (this.shouldHandleRequest(req, options)) {
          let assetPath = req.path.slice(baseURL.length);
          let isFile = false;

          try {
            isFile = fs.statSync(path.join(results.directory, assetPath)).isFile();
          } catch (err) {
            /* ignore */
          }
          if (!isFile) {
            req.serveUrl = `${baseURL}index.html`;
          }
        }
      } finally {
        next();
      }
    });
  }

  shouldHandleRequest(req, options) {
    let acceptHeaders = req.headers.accept || [];
    let hasHTMLHeader = acceptHeaders.indexOf('text/html') !== -1;<commit_msg>Don't exit process when requesting a failed build<commit_after>'use strict';

const path = require('path');
const fs = require('fs');
const cleanBaseURL = require('clean-base-url');

class HistorySupportAddon {
  /**
   * This addon is used to serve the `index.html` file at every requested
   * URL that begins with `rootURL` and is expecting `text/html` output.
   *
   * @class HistorySupportAddon
   * @constructor
   */
  constructor(project) {
    this.project = project;
    this.name = 'history-support-middleware';
  }

  shouldAddMiddleware(environment) {
    let config = this.project.config(environment);
    let locationType = config.locationType;
    let historySupportMiddleware = config.historySupportMiddleware;

    if (typeof historySupportMiddleware === 'boolean') {
      return historySupportMiddleware;
    }

    return ['auto', 'history'].indexOf(locationType) !== -1;
  }

  serverMiddleware(config) {
    if (this.shouldAddMiddleware(config.options.environment)) {
      this.project.ui.writeWarnLine(
        'Empty `rootURL` is not supported. Disable history support, or use an absolute `rootURL`',
        config.options.rootURL !== ''
      );

      this.addMiddleware(config);
    }
  }

  addMiddleware(config) {
    let app = config.app;
    let options = config.options;
    let watcher = options.watcher;

    let baseURL = options.rootURL === '' ? '/' : cleanBaseURL(options.rootURL || options.baseURL);

    app.use(async (req, _, next) => {
      try {
        let results;
        try {
          results = await watcher;
        } catch (e) {
          // This means there was a build error, so we won't actually be serving
          // index.html, and we have nothing to do. We have to catch it here,
          // though, or it will go uncaught and cause the process to exit.
          return;
        }

        if (this.shouldHandleRequest(req, options)) {
          let assetPath = req.path.slice(baseURL.length);
          let isFile = false;

          try {
            isFile = fs.statSync(path.join(results.directory, assetPath)).isFile();
          } catch (err) {
            /* ignore */
          }
          if (!isFile) {
            req.serveUrl = `${baseURL}index.html`;
          }
        }
      } finally {
        next();
      }
    });
  }

  shouldHandleRequest(req, options) {
    let acceptHeaders = req.headers.accept || [];
    let hasHTMLHeader = acceptHeaders.indexOf('text/html') !== -1;
{'fraction_non_alphanumeric': 0.12450765864332604, 'fraction_numerical': 0.00087527352297593, 'mean_word_length': 2.5270061728395063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4895409', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1305, 'n_words': 321}