<commit_before>// Copyright <NAME> <<EMAIL>>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

'use strict';

const pkgInfo = require('./package.json');
const utils = require('./lib/utils');
const parseOpts = require('./lib/utils/parseOpts');
const errMsgs = require('./lib/utils/errorMsgs').index;

const Promise = utils.Promise;
const debug = utils.debug(utils.pathToName(__filename));


class Bluebox {

  constructor(opts) {
    this.shodanKey = opts.shodanKey || null;
    // Loading all present modules.
    this.modules = utils.extend(
      utils.requireDir(module, './lib/modules'),
      utils.requireDir(module, './lib/modules/private')
    );

    debug('Started', { version: pkgInfo.version });
  }


  version() { return pkgInfo.version; }


  help() { return this.modules; }


  getShodanKey() { return this.shodanKey; }


  setShodanKey(value) { this.shodanKey = value; }


  run(moduleName, cfg) {
    return new Promise((resolve, reject) => {
      debug('Running module:', { name: moduleName, cfg });

      if (!this.modules[moduleName]) {
        reject(new Error(errMsgs.notFound));
        return;
      }

      const blueModule = this.modules[moduleName];

      // Parsing the paremeters passed by the client.
      let confWithKey;
      try {
        confWithKey = parseOpts(cfg, blueModule.options);
      } catch (err) {
        reject(new Error(`${errMsgs.parseOpts} : ${err.message}`));
      }
      if (moduleName.substr(0, 6) === 'shodan') {
        if (!this.shodanKey) { reject(new Error(errMsgs.noKey)); }
        confWithKey.key = this.shodanKey;
      }

      // Returning another promise.
      resolve(blueModule.run(confWithKey));
    });
  }
}

module.exports = Bluebox;<commit_msg>Important fix, I was using the promises in a bad way.<commit_after>// Copyright <NAME> <<EMAIL>>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

'use strict';

const pkgInfo = require('./package.json');
const utils = require('./lib/utils');
const parseOpts = require('./lib/utils/parseOpts');
const errMsgs = require('./lib/utils/errorMsgs').index;

const Promise = utils.Promise;
const debug = utils.debug(utils.pathToName(__filename));


class Bluebox {

  constructor(opts) {
    this.shodanKey = opts.shodanKey || null;
    // Loading all present modules.
    this.modules = utils.extend(
      utils.requireDir(module, './lib/modules'),
      utils.requireDir(module, './lib/modules/private')
    );

    debug('Started', { version: pkgInfo.version });
  }


  version() { return pkgInfo.version; }


  help() { return this.modules; }


  getShodanKey() { return this.shodanKey; }


  setShodanKey(value) { this.shodanKey = value; }


  // Should always return a promise.
  run(moduleName, cfg) {
    debug('Running module:', { name: moduleName, cfg });

    if (!this.modules[moduleName]) {
      return Promise.reject(new Error(errMsgs.notFound));
    }

    const blueModule = this.modules[moduleName];

    // Parsing the paremeters passed by the client.
    let confWithKey;
    try {
      confWithKey = parseOpts(cfg, blueModule.options);
    } catch (err) {
      return Promise.reject(new Error(`${errMsgs.parseOpts} : ${err.message}`));
    }
    if (moduleName.substr(0, 6) === 'shodan') {
      if (!this.shodanKey) { return Promise.reject(new Error(errMsgs.noKey)); }
      confWithKey.key = this.shodanKey;
    }

    return blueModule.run(confWithKey);
  }
}

module.exports = Bluebox;
{'fraction_non_alphanumeric': 0.13120414328873545, 'fraction_numerical': 0.001294777729823047, 'mean_word_length': 3.186991869918699, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2778660', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1367, 'n_words': 424}