<commit_before>import * as fs from 'fs';
import * as http from 'http';
import * as path from 'path';

import WebHook from './webhook';
import respond from './responder';

const Promise = require('bluebird');
const Random = require('random-js');

const SENTENCE_FOLDER = '../../data/';

export default class API {
  sentencesCache: String[];
  webhook: WebHook;
  randomEngine: any;

  constructor() {
    this.webhook = new WebHook();
    this.getSentences();
    this.randomEngine = Random.engines.mt19937();
    this.randomEngine.autoSeed();
  }

  private getSentenceFolder() {
    return path.join(__dirname, SENTENCE_FOLDER);
  }

  private getRandomSentences(count: number): Promise<string[]> {
    return this.getSentences().then((sentences: string[]) => {
      let randoms = [];
      for (var i = 0; i < count; i++) {
        let distribution = Random.integer(0, sentences.length - 1);
        let randomIndex = distribution(this.randomEngine);
        randoms.push(sentences[randomIndex]);
      }
      return randoms;
    });
  }

  private getFilesInFolder(folderpath: string) {
    return new Promise(
      (res: (files: string[]) => void, rej: (error: any) => void) => {
        fs.readdir(folderpath, (err: any, files: string[]) => {
          if (err) {
            rej(err);
            return;
          }

          res(files);
        });
      }
    );
  }

  private getFileContents(filepath: string) {
    return new Promise(
      (res: (contents: string) => void, rej: (error: any) => void) => {
        fs.readFile(filepath, (err: any, data: Buffer) => {
          if (err) {
            rej(err);
            return;
          }

          res(data.toString());
        });
      }
    );
  }
  /**
   * Is this request directed at the api?
   */
  isApiRequest(request: http.IncomingMessage) {
    return request.url.includes('/api/');
  }

  /**
   * Give api response.
   */
  handleRequest(request: http.IncomingMessage, response: http.ServerResponse) {
    // Most often this will be a sentence request.
    if (request.url.includes('/sentence')) {
      let parts = request.url.split('/');
      let index = parts.indexOf('sentence');
      let count = parts[index + 1] && parseInt(parts[index + 1], 10);
      this.returnRandomSentence(response, count);
      // Webhooks from github.
    } else if (this.webhook.isHookRequest(request)) {<commit_msg>specify encoding when reading files<commit_after>import * as fs from 'fs';
import * as http from 'http';
import * as path from 'path';

import WebHook from './webhook';
import respond from './responder';

const Promise = require('bluebird');
const Random = require('random-js');

const SENTENCE_FOLDER = '../../data/';

export default class API {
  sentencesCache: String[];
  webhook: WebHook;
  randomEngine: any;

  constructor() {
    this.webhook = new WebHook();
    this.getSentences();
    this.randomEngine = Random.engines.mt19937();
    this.randomEngine.autoSeed();
  }

  private getSentenceFolder() {
    return path.join(__dirname, SENTENCE_FOLDER);
  }

  private getRandomSentences(count: number): Promise<string[]> {
    return this.getSentences().then((sentences: string[]) => {
      let randoms = [];
      for (var i = 0; i < count; i++) {
        let distribution = Random.integer(0, sentences.length - 1);
        let randomIndex = distribution(this.randomEngine);
        randoms.push(sentences[randomIndex]);
      }
      return randoms;
    });
  }

  private getFilesInFolder(folderpath: string) {
    return new Promise(
      (res: (files: string[]) => void, rej: (error: any) => void) => {
        fs.readdir(folderpath, (err: any, files: string[]) => {
          if (err) {
            rej(err);
            return;
          }

          res(files);
        });
      }
    );
  }

  private getFileContents(filepath: string) {
    return new Promise(
      (res: (contents: string) => void, rej: (error: any) => void) => {
        fs.readFile(filepath, 'utf8', (err: any, data: string) => {
          if (err) {
            rej(err);
            return;
          }

          res(data);
        });
      }
    );
  }
  /**
   * Is this request directed at the api?
   */
  isApiRequest(request: http.IncomingMessage) {
    return request.url.includes('/api/');
  }

  /**
   * Give api response.
   */
  handleRequest(request: http.IncomingMessage, response: http.ServerResponse) {
    // Most often this will be a sentence request.
    if (request.url.includes('/sentence')) {
      let parts = request.url.split('/');
      let index = parts.indexOf('sentence');
      let count = parts[index + 1] && parseInt(parts[index + 1], 10);
      this.returnRandomSentence(response, count);
      // Webhooks from github.
    } else if (this.webhook.isHookRequest(request)) {
{'fraction_non_alphanumeric': 0.1493845190903401, 'fraction_numerical': 0.005215939912372209, 'mean_word_length': 2.5590200445434297, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '3268132', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1455, 'n_words': 363}