<commit_before>var sys = require('sys');
var net = require('net');
var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;

var Query = require(__dirname+'/query');
var utils = require(__dirname + '/utils');

var Client = function(config) {
  EventEmitter.call(this);
  config = config || {};
  this.user = config.user;
  this.database = config.database;
  this.port = config.port || 5432;
  this.host = config.host;
  this.queryQueue = [];
  this.stream = config.stream || new net.Stream();
  this.queryQueue = [];
  this.password = config.password || '';
  this.lastBuffer = false;
  this.lastOffset = 0;
  this.buffer = null;
  this.offset = null;
  this.encoding = 'utf8';
};

sys.inherits(Client, EventEmitter);

var p = Client.prototype;

p.connect = function() {
  if(this.stream.readyState == 'closed'){
    this.stream.connect(this.port, this.host);
  }
  var self = this;
  this.stream.on('connect', function() {
    var data = ['user',self.user,'database', self.database, '\0'].join('\0');
    var byteLength = Buffer.byteLength(data);
    var fullBuffer = new Buffer(byteLength + 4);
    fullBuffer[0] = 0;
    fullBuffer[1] = 3;
    fullBuffer[2] = 0;
    fullBuffer[3] = 0;
    fullBuffer.write(data, 4);
    self.send(null, fullBuffer);
  });

  this.stream.on('data', function(buffer) {
    self.setBuffer(buffer);
    var msg;
    while(msg = self.parseMessage()) {
      self.emit('message', msg);
      self.emit(msg.name, msg);
    }
  });

  this.on('authenticationCleartextPassword', function() {
    self.send('p', new Buffer(self.password + '\0', self.encoding));
  });

  this.on('authenticationMD5Password', function(msg) {
    var enc = function(string) {
      return crypto.createHash('md5').update(string).digest('hex');
    }
    var md5password = "<PASSWORD>" + enc(enc(self.password + self.user) + msg.salt.toString('binary')) + "\0";
    self.send('p', new Buffer(md5password, self.encoding));
  });

  this.on('readyForQuery', function() {
    self.readyForQuery = true;
    self.pulseQueryQueue();
  });

  this.on('rowDescription', function(msg) {
    self.processRowDescription(msg);
  });

  this.on('dataRow', function(msg) {
    self.processDataRow(msg);
  });
};

p.send = function(code, bodyBuffer) {
  var length = bodyBuffer.length + 4;
  var buffer = Buffer(length + (code ? 1 : 0));
  var offset = 0;
  if(code) {
    buffer[offset++] = Buffer(code, this.encoding) [0];
  }
  this.writeInt32(buffer, offset, length);
  bodyBuffer.copy(buffer, offset+4, 0);
  return this.stream.write(buffer);
};

p.writeInt32 = function(buffer, offset, value) {
  buffer[offset++] = value >>> 24 & 0xFF;
  buffer[offset++] = value >>> 16 & 0xFF;
  buffer[offset++] = value >>> 8 & 0xFF;
  buffer[offset++] = value >>> 0 & 0xFF;
};

p.end = function() {
  var terminationBuffer = new Buffer([0x58,0,0,0,4]);
  var wrote = this.stream.end(terminationBuffer);
};

p.query = function(text) {
  this.queryQueue.push({type: 'simpleQuery', text: text });
  this.pulseQueryQueue();
  return this;
};

p.pulseQueryQueue = function(ready) {
  if(!this.readyForQuery) {
    return;
  };
  var query = this.queryQueue.shift();
  if(query) {
    var self = this;
    this.readyForQuery = false;
    if(query.type !== 'simpleQuery') {
      throw new Error("do not know how to handle this");
    }
    this.send('Q', new Buffer(query.text + '\0', this.encoding));
  }
};
//query handling

var intParser = {
  fromDbValue: parseInt
};

var floatParser = {
  fromDbValue: parseFloat
};

var timeParser = {
  fromDbValue: function(isoTime) {
    var when = new Date();
    var split = isoTime.split(':');
    when.setHours(split[0]);
    when.setMinutes(split[1]);
    when.setSeconds(split[2].split('-') [0]);
    return when;
  }
};

var dateParser = {
  fromDbValue: function(isoDate) {
    return Date.parse(isoDate);
  }
};

Query.dataTypes = {
  20: intParser,
  21: intParser,
  23: intParser,
  26: intParser,
  1700: floatParser,
  700: floatParser,
  701: floatParser,
  1083: timeParser,
  1266: timeParser,
  1114: dateParser,
  1184: dateParser
};

p.processRowDescription = function(description) {
  this.fields = description.fields;
};

p.processDataRow = function(dataRow) {
  var row = dataRow.fields;
  var fields = this.fields || [];
  var field, dataType;
  for(var i = 0, len = row.length; i < len; i++) {
    field = fields[i] || 0
    var dataType = Query.dataTypes[field.dataTypeID];
    if(dataType) {
      row[i] = dataType.fromDbValue(row[i]);
    }
  }
  this.emit('row',row);
};


//parsing methods
p.setBuffer = function(buffer) {
  if(this.lastBuffer) {    //we have unfinished biznaz
    //need to combine last two buffers
    var remaining = this.lastBuffer.length - this.lastOffset;
    var combinedBuffer = new Buffer(buffer.length + remaining);
    this.lastBuffer.copy(combinedBuffer, 0, this.lastOffset);<commit_msg>removed reamining references to query<commit_after>var sys = require('sys');
var net = require('net');
var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;

var utils = require(__dirname + '/utils');

var Client = function(config) {
  EventEmitter.call(this);
  config = config || {};
  this.user = config.user;
  this.database = config.database;
  this.port = config.port || 5432;
  this.host = config.host;
  this.queryQueue = [];
  this.stream = config.stream || new net.Stream();
  this.queryQueue = [];
  this.password = config.password || '';
  this.lastBuffer = false;
  this.lastOffset = 0;
  this.buffer = null;
  this.offset = null;
  this.encoding = 'utf8';
};

sys.inherits(Client, EventEmitter);

var p = Client.prototype;

p.connect = function() {
  if(this.stream.readyState == 'closed'){
    this.stream.connect(this.port, this.host);
  }
  var self = this;
  this.stream.on('connect', function() {
    var data = ['user',self.user,'database', self.database, '\0'].join('\0');
    var byteLength = Buffer.byteLength(data);
    var fullBuffer = new Buffer(byteLength + 4);
    fullBuffer[0] = 0;
    fullBuffer[1] = 3;
    fullBuffer[2] = 0;
    fullBuffer[3] = 0;
    fullBuffer.write(data, 4);
    self.send(null, fullBuffer);
  });

  this.stream.on('data', function(buffer) {
    self.setBuffer(buffer);
    var msg;
    while(msg = self.parseMessage()) {
      self.emit('message', msg);
      self.emit(msg.name, msg);
    }
  });

  this.on('authenticationCleartextPassword', function() {
    self.send('p', new Buffer(self.password + '\0', self.encoding));
  });

  this.on('authenticationMD5Password', function(msg) {
    var enc = function(string) {
      return crypto.createHash('md5').update(string).digest('hex');
    }
    var md5password = "<PASSWORD>" + enc(enc(self.password + self.user) + msg.salt.toString('binary')) + "\0";
    self.send('p', new Buffer(md5password, self.encoding));
  });

  this.on('readyForQuery', function() {
    self.readyForQuery = true;
    self.pulseQueryQueue();
  });

  this.on('rowDescription', function(msg) {
    self.processRowDescription(msg);
  });

  this.on('dataRow', function(msg) {
    self.processDataRow(msg);
  });
};

p.send = function(code, bodyBuffer) {
  var length = bodyBuffer.length + 4;
  var buffer = Buffer(length + (code ? 1 : 0));
  var offset = 0;
  if(code) {
    buffer[offset++] = Buffer(code, this.encoding) [0];
  }
  this.writeInt32(buffer, offset, length);
  bodyBuffer.copy(buffer, offset+4, 0);
  return this.stream.write(buffer);
};

p.writeInt32 = function(buffer, offset, value) {
  buffer[offset++] = value >>> 24 & 0xFF;
  buffer[offset++] = value >>> 16 & 0xFF;
  buffer[offset++] = value >>> 8 & 0xFF;
  buffer[offset++] = value >>> 0 & 0xFF;
};

p.end = function() {
  var terminationBuffer = new Buffer([0x58,0,0,0,4]);
  var wrote = this.stream.end(terminationBuffer);
};

p.query = function(text) {
  this.queryQueue.push({type: 'simpleQuery', text: text });
  this.pulseQueryQueue();
  return this;
};

p.pulseQueryQueue = function(ready) {
  if(!this.readyForQuery) {
    return;
  };
  var query = this.queryQueue.shift();
  if(query) {
    var self = this;
    this.readyForQuery = false;
    if(query.type !== 'simpleQuery') {
      throw new Error("do not know how to handle this");
    }
    this.send('Q', new Buffer(query.text + '\0', this.encoding));
  }
};
//query handling

var intParser = {
  fromDbValue: parseInt
};

var floatParser = {
  fromDbValue: parseFloat
};

var timeParser = {
  fromDbValue: function(isoTime) {
    var when = new Date();
    var split = isoTime.split(':');
    when.setHours(split[0]);
    when.setMinutes(split[1]);
    when.setSeconds(split[2].split('-') [0]);
    return when;
  }
};

var dateParser = {
  fromDbValue: function(isoDate) {
    return Date.parse(isoDate);
  }
};

Client.dataTypes = {
  20: intParser,
  21: intParser,
  23: intParser,
  26: intParser,
  1700: floatParser,
  700: floatParser,
  701: floatParser,
  1083: timeParser,
  1266: timeParser,
  1114: dateParser,
  1184: dateParser
};

p.processRowDescription = function(description) {
  this.fields = description.fields;
};

p.processDataRow = function(dataRow) {
  var row = dataRow.fields;
  var fields = this.fields || [];
  var field, dataType;
  for(var i = 0, len = row.length; i < len; i++) {
    field = fields[i] || 0
    var dataType = Client.dataTypes[field.dataTypeID];
    if(dataType) {
      row[i] = dataType.fromDbValue(row[i]);
    }
  }
  this.emit('row',row);
};


//parsing methods
p.setBuffer = function(buffer) {
  if(this.lastBuffer) {    //we have unfinished biznaz
    //need to combine last two buffers
    var remaining = this.lastBuffer.length - this.lastOffset;
    var combinedBuffer = new Buffer(buffer.length + remaining);
    this.lastBuffer.copy(combinedBuffer, 0, this.lastOffset);
{'fraction_non_alphanumeric': 0.16876342436330163, 'fraction_numerical': 0.019228802291091338, 'mean_word_length': 3.6253547776726585, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, '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': '5060259', 'n_tokens_mistral': 3426, 'n_tokens_neox': 3312, 'n_words': 704}