<commit_before>/*eslint-disable no-unused-vars */
'use strict';

const Promise = require('bluebird');
Promise.series = (iterable, action) => {
  return Promise.mapSeries(
    iterable.map(action),
    (value, index, length) => value || iterable[index].name || null
  );
};

const dns = require('dns');
const net = require('net');
const EventEmitter = require('events');
const redis = require('redis');
const moment = require('moment');
const util = require('util');
const humanize = require('humanize-duration');
const spawn = require('child_process').spawn;
const Route = require('route-parser');

const request = require('request');
const pRequest = require('fwsp-prequest');
const Utils = require('fwsp-jsutils');
const ServerResponse = require('fwsp-server-response');
let serverResponse = new ServerResponse();
const UMFMessage = require('fwsp-umf-message');

let HYDRA_REDIS_DB = 0;
const redisPreKey = 'hydra:service';
const mcMessageKey = 'hydra:service:mc';
const MAX_ENTRIES_IN_HEALTH_LOG = 1024;
const PRESENCE_UPDATE_INTERVAL = 5000; // unit = milli-seconds, so every 5 seconds
const HEALTH_UPDATE_INTERVAL = 5000;
const KEY_EXPIRATION_TTL = parseInt((PRESENCE_UPDATE_INTERVAL / 1000) * 2);
const UMF_INVALID_MESSAGE = 'UMF message requires "to", "from" and "body" fields';

/**
 * @name Hydra
 * @summary Base class for Hydra.
 * @fires Hydra#log
 * @fires Hydra#message
 */
class Hydra extends EventEmitter {
  constructor() {
    super();

    this.mcMessageChannelClient;
    this.mcDirectMessageChannelClient;
    this.config = null;
    this.serviceName = '';
    this.serviceDescription = '';
    this.serviceVersion = '';
    this.isService = false;
    this.initialized = false;
    this.redisdb = null;
    this._updatePresence = this._updatePresence.bind(this);
    this._updateHealthCheck = this._updateHealthCheck.bind(this);
    this.registeredRoutes = [];
    this.registeredPlugins = [];

    this.publisherChannels = {};
    this.subscriberChannels = {};
  }

  /**
   * @name use
   * @summary Adds plugins to Hydra
   * @param {...object} plugins - plugins to register
   * @return {object} - Promise which will resolve when all plugins are registered
   */
  use(...plugins) {
    return Promise.series(plugins, plugin => this._registerPlugin(plugin));
  }

  /**
   * @name _registerPlugin
   * @summary Registers a plugin with Hydra
   * @param {object} plugin - HydraPlugin to use
   * @return {object} Promise or value
   */
  _registerPlugin(plugin) {
    this.registeredPlugins.push(plugin);
    return plugin.setHydra(this);
  }

  /**
   * @name init
   * @summary Register plugins then continue initialization
   * @param {object} config - configuration object containing hydra specific keys/values
   * @return {object} promise - resolves with this._init
   */
  init(config) {
    return new Promise((resolve, reject) => {
      Promise.series(this.registeredPlugins, plugin => plugin.setConfig(config))
        .then((...results) => {
          resolve(this._init(config));
        })
        .catch(err => this._logMessage('error', err.toString()));
    });
  }

  /**
   * @name _init
   * @summary Initialize Hydra with config object.
   * @param {object} config - configuration object containing hydra specific keys/values
   * @return {object} promise - resolving if init success or rejecting otherwise
   */
  _init(config) {
    return new Promise((resolve, reject) => {
      let ready = () => {
        Promise.series(this.registeredPlugins, plugin => plugin.onServiceReady())
          .then((...results) => {
            resolve();<commit_msg>Eric's patch for promise warning<commit_after>/*eslint-disable no-unused-vars */
'use strict';

const Promise = require('bluebird');
Promise.series = (iterable, action) => {
  return Promise.mapSeries(
    iterable.map(action),
    (value, index, length) => value || iterable[index].name || null
  );
};

const dns = require('dns');
const net = require('net');
const EventEmitter = require('events');
const redis = require('redis');
const moment = require('moment');
const util = require('util');
const humanize = require('humanize-duration');
const spawn = require('child_process').spawn;
const Route = require('route-parser');

const request = require('request');
const pRequest = require('fwsp-prequest');
const Utils = require('fwsp-jsutils');
const ServerResponse = require('fwsp-server-response');
let serverResponse = new ServerResponse();
const UMFMessage = require('fwsp-umf-message');

let HYDRA_REDIS_DB = 0;
const redisPreKey = 'hydra:service';
const mcMessageKey = 'hydra:service:mc';
const MAX_ENTRIES_IN_HEALTH_LOG = 1024;
const PRESENCE_UPDATE_INTERVAL = 5000; // unit = milli-seconds, so every 5 seconds
const HEALTH_UPDATE_INTERVAL = 5000;
const KEY_EXPIRATION_TTL = parseInt((PRESENCE_UPDATE_INTERVAL / 1000) * 2);
const UMF_INVALID_MESSAGE = 'UMF message requires "to", "from" and "body" fields';

/**
 * @name Hydra
 * @summary Base class for Hydra.
 * @fires Hydra#log
 * @fires Hydra#message
 */
class Hydra extends EventEmitter {
  constructor() {
    super();

    this.mcMessageChannelClient;
    this.mcDirectMessageChannelClient;
    this.config = null;
    this.serviceName = '';
    this.serviceDescription = '';
    this.serviceVersion = '';
    this.isService = false;
    this.initialized = false;
    this.redisdb = null;
    this._updatePresence = this._updatePresence.bind(this);
    this._updateHealthCheck = this._updateHealthCheck.bind(this);
    this.registeredRoutes = [];
    this.registeredPlugins = [];

    this.publisherChannels = {};
    this.subscriberChannels = {};
  }

  /**
   * @name use
   * @summary Adds plugins to Hydra
   * @param {...object} plugins - plugins to register
   * @return {object} - Promise which will resolve when all plugins are registered
   */
  use(...plugins) {
    return Promise.series(plugins, plugin => this._registerPlugin(plugin));
  }

  /**
   * @name _registerPlugin
   * @summary Registers a plugin with Hydra
   * @param {object} plugin - HydraPlugin to use
   * @return {object} Promise or value
   */
  _registerPlugin(plugin) {
    this.registeredPlugins.push(plugin);
    return plugin.setHydra(this);
  }

  /**
   * @name init
   * @summary Register plugins then continue initialization
   * @param {object} config - configuration object containing hydra specific keys/values
   * @return {object} promise - resolves with this._init
   */
   init(config) {
     return new Promise((resolve, reject) => {
       Promise.series(this.registeredPlugins, plugin => plugin.setConfig(config))
         .then((...results) => {
           return this._init(config);
         })
         .then(() => resolve())
         .catch(err => this._logMessage('error', err.toString()));
     });
   }

  /**
   * @name _init
   * @summary Initialize Hydra with config object.
   * @param {object} config - configuration object containing hydra specific keys/values
   * @return {object} promise - resolving if init success or rejecting otherwise
   */
  _init(config) {
    return new Promise((resolve, reject) => {
      let ready = () => {
        Promise.series(this.registeredPlugins, plugin => plugin.onServiceReady())
          .then((...results) => {
            resolve();
{'fraction_non_alphanumeric': 0.1353735869864902, 'fraction_numerical': 0.005238489109456851, 'mean_word_length': 3.6866925064599485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '1383132', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2112, 'n_words': 544}