<commit_before>// 3rd party modules
const logger = require('winston');

class DynamicRouter {
  constructor() {
    this.addonRouters = [];
  }

  router(req, res, next) {
    if (this.addonRouters.length > 0) {
      this.recursiveRoute(0, req, res, next);
    } else {
      next();
    }
  }

  recursiveRoute(i, req, res, next) {
    logger.debug(`Addon: ${this.addonRouters[i].addon.name} is routing the request`);
    if (i >= this.addonRouters.length - 1) {
      // This is the last router, call the higher level next if nothing matches
      this.addonRouters[i].router(req, res, next);
    } else {
      // If there is more routers, call this router with the next router as next parameter
      this.addonRouters[i].router(req, res, () => this.recursiveRoute(i + 1, req, res, next));
    }
  }

  removeRouter(addon) {
    for (let i = 0; i < this.addonRouters.length; i++) {
      if (this.addonRouters[i].addon.name === addon.name) {
        this.addonRouters.splice(i, 1);

        logger.debug(`Removed router for addon: ${addon.name}.`);
        return;
      }
    }

    logger.warn(`Could not find and remove router for addon: ${addon.name}.`);
  }
}


module.exports = DynamicRouter;<commit_msg>commented dynamic router + refactored recursive function away in favour of async waterfall<commit_after>// 3rd party modules
const logger = require('winston');
const async = require('async');

/**
 * Collection of routers that act like a single router to allow addons to
 * dynamically define and undefine routes
 */
class DynamicRouter {
  constructor() {
    this.addonRouters = [];
  }

  /**
   * Route entrypoint that is fed to express app
   */
  router(pReq, pRes, pNext) {
    const resolveRoute = (router, req, res, next) => { router(req, res, next); };
    const routers = this.addonRouters.map(pAddonRouter => resolveRoute.bind(this, pAddonRouter.router, pReq, pRes));
    async.waterfall(routers, pNext);
  }

  /**
   * Remove router that is linked to the provided addon
   */
  removeRouter(addon) {
    for (let i = 0; i < this.addonRouters.length; i++) {
      if (this.addonRouters[i].addon.name === addon.name) {
        this.addonRouters.splice(i, 1);

        logger.debug(`Removed router for addon: ${addon.name}.`);
        return;
      }
    }

    logger.warn(`Could not find and remove router for addon: ${addon.name}.`);
  }
}


module.exports = DynamicRouter;
{'fraction_non_alphanumeric': 0.13780183180682765, 'fraction_numerical': 0.004163197335553705, 'mean_word_length': 3.0183946488294313, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '1754239', 'n_tokens_mistral': 825, 'n_tokens_neox': 796, 'n_words': 216}