<commit_before>/**
 * Dependencies
 */
var EventEmitter = require('events').EventEmitter;
var http = require('http');
var Param = require(__dirname + '/Param');
var util = require('util');
var request = require('request');

/**
 * Module exports
 */
module.exports = Feed;

/**
 * Constructor.
 *
 * @param {object} options
 */
function Feed(options) {
  
  /**
   * Configuration
   * @var {object}
   */
  this.config = {
    host: null,
    port: 80,
    user: null,
    password: <PASSWORD>
  };

  /**
   * Chunk buffer
   * @var {string}
   */
  this.chunkBuffer = '';

  // Handle options input
  for (var key in options) {
    if (typeof (this.config[key] !== 'undefined')) {
      this.config[key] = options[key];
    }
  }

  // Check that all config options are defined
  for (var key in this.config) {
    if (this.config[key] == null) {
      throw new Error('Option "' + key + '" is required');
    }
  }
}
util.inherits(Feed, EventEmitter);

/**
 * Connect to specified server & start processing data.
 */
Feed.prototype.connect = function() {
  
  var feed = this;

  this.req = http.request({
    hostname: feed.config.host,
    port: feed.config.port,
    path: '/',
    method: 'STATS',
    auth: feed.config.user + ':' + feed.config.password,
  }, function(res){
    res.on('data', function (chunk) {
      feed.handleChunk(chunk);
    });

    feed.req.socket.on('close', function() {
      feed.emit('disconnect');
    })
  });

  this.req.end();
}

/**
 * Processes data chunk
 *
 * @param {Buffer} chunk
 */
Feed.prototype.handleChunk = function(chunk) {
  chunk = chunk.toString();
  var lines = chunk.split(/\r\n|\r|\n/g);

  // Add buffer to the first line if present
  if (this.chunkBuffer) {
    lines[0] = this.chunkBuffer + lines[0];
    this.chunkBuffer = '';
  }

  // Save last chunk to buffer if necessary
  if (/\n$/.test(chunk) === false) {
    this.chunkBuffer = lines.pop();
  }

  for (var i = 0; i < lines.length; i++) {
    lines[i] = lines[i].trim();
    if (lines[i]) feed.handleRawEvent(lines[i]);
  }
}

/**
 * Processes raw icecast event.
 * 
 * @param {string} rawEvent
 */
Feed.prototype.handleRawEvent = function(rawEvent) {

  var event = this.parse(rawEvent);

  // Build event params 
  var params = [event.data];
  if (event.mount) {
    params.unshift(event.mount);
  }

  // Emit wilcard (*) event<commit_msg>Fixed feed typo<commit_after>/**
 * Dependencies
 */
var EventEmitter = require('events').EventEmitter;
var http = require('http');
var Param = require(__dirname + '/Param');
var util = require('util');
var request = require('request');

/**
 * Module exports
 */
module.exports = Feed;

/**
 * Constructor.
 *
 * @param {object} options
 */
function Feed(options) {
  
  /**
   * Configuration
   * @var {object}
   */
  this.config = {
    host: null,
    port: 80,
    user: null,
    password: <PASSWORD>
  };

  /**
   * Chunk buffer
   * @var {string}
   */
  this.chunkBuffer = '';

  // Handle options input
  for (var key in options) {
    if (typeof (this.config[key] !== 'undefined')) {
      this.config[key] = options[key];
    }
  }

  // Check that all config options are defined
  for (var key in this.config) {
    if (this.config[key] == null) {
      throw new Error('Option "' + key + '" is required');
    }
  }
}
util.inherits(Feed, EventEmitter);

/**
 * Connect to specified server & start processing data.
 */
Feed.prototype.connect = function() {
  
  var feed = this;

  this.req = http.request({
    hostname: feed.config.host,
    port: feed.config.port,
    path: '/',
    method: 'STATS',
    auth: feed.config.user + ':' + feed.config.password,
  }, function(res){
    res.on('data', function (chunk) {
      feed.handleChunk(chunk);
    });

    feed.req.socket.on('close', function() {
      feed.emit('disconnect');
    })
  });

  this.req.end();
}

/**
 * Processes data chunk
 *
 * @param {Buffer} chunk
 */
Feed.prototype.handleChunk = function(chunk) {
  chunk = chunk.toString();
  var lines = chunk.split(/\r\n|\r|\n/g);

  // Add buffer to the first line if present
  if (this.chunkBuffer) {
    lines[0] = this.chunkBuffer + lines[0];
    this.chunkBuffer = '';
  }

  // Save last chunk to buffer if necessary
  if (/\n$/.test(chunk) === false) {
    this.chunkBuffer = lines.pop();
  }

  for (var i = 0; i < lines.length; i++) {
    lines[i] = lines[i].trim();
    if (lines[i]) this.handleRawEvent(lines[i]);
  }
}

/**
 * Processes raw icecast event.
 * 
 * @param {string} rawEvent
 */
Feed.prototype.handleRawEvent = function(rawEvent) {

  var event = this.parse(rawEvent);

  // Build event params 
  var params = [event.data];
  if (event.mount) {
    params.unshift(event.mount);
  }

  // Emit wilcard (*) event
{'fraction_non_alphanumeric': 0.1749788672865596, 'fraction_numerical': 0.002113271344040575, 'mean_word_length': 2.7474267616785433, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7074971', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1585, 'n_words': 381}