<commit_before>/*!
 * ws: a node.js websocket client
 * Copyright(c) 2011 <NAME> <<EMAIL>>
 * MIT Licensed
 */

var events = require('events')
  , util = require('util')
  , EventEmitter = events.EventEmitter
  , ErrorCodes = require('./ErrorCodes');

/**
 * HyBi Sender implementation
 */

function Sender (socket) {
  this._socket = socket;
  this.firstFragment = true;
}

/**
 * Inherits from EventEmitter.
 */

util.inherits(Sender, events.EventEmitter);

/**
 * Sends a close instruction to the remote party.
 *
 * @api public
 */

Sender.prototype.close = function(code, data, options) {
  if (typeof code !== 'undefined') {
    if (typeof code !== 'number' || 
      !ErrorCodes.isValidErrorCode(code)) throw new Error('first argument must be a valid error code number');
  }
  code = code || 1000;
  var dataBuffer = new Buffer(2 + (data ? Buffer.byteLength(data) : 0));
  dataBuffer.writeUInt16BE(code, 0);
  if (dataBuffer.length > 2) dataBuffer.write(data, 2);
  var buf = this.frameData(0x8, dataBuffer, true, true); // always masked
  this._socket.write(buf, 'binary');
}

/**
 * Sends a ping message to the remote party.
 *
 * @api public
 */

Sender.prototype.ping = function(data, options) {
  var mask = options && options.mask;
  var buf = this.frameData(0x9, data || '', true, mask);
  this._socket.write(buf, 'binary');
}

/**
 * Sends a pong message to the remote party.
 *
 * @api public
 */

Sender.prototype.pong = function(data, options) {
  var mask = options && options.mask;
  var buf = this.frameData(0xa, data || '', true, mask);
  this._socket.write(buf, 'binary');
}

/**
 * Sends text or binary data to the remote party.
 *
 * @api public
 */

Sender.prototype.send = function(data, options, cb) {
  var buf;
  var finalFragment = options && options.fin === false ? false : true;
  var mask = options && options.mask;
  var opcode = options && options.binary ? 2 : 1;
  if (this.firstFragment === false) opcode = 0;
  else this.firstFragment = false;
  buf = this.frameData(opcode, data, finalFragment, mask);
  if (finalFragment) this.firstFragment = true
  this._socket.write(buf, 'binary', cb);
}

/**
 * Frames a piece of data according to the HyBi WebSocket protocol.
 *
 * @api private
 */

Sender.prototype.frameData = function(opcode, data, finalFragment, maskData) {
  var dataBuffer = getBufferFromData(data)
    , dataLength = dataBuffer.length
    , dataOffset = maskData ? 6 : 2
    , secondByte = dataLength;
  if (dataLength >= 65536) {
    dataOffset += 8;
    secondByte = 127;
  }
  else if (dataLength > 125) {
    dataOffset += 2;
    secondByte = 126;
  }
  var outputBuffer = new Buffer(dataLength + dataOffset);
  if (finalFragment) opcode = opcode | 0x80;
  outputBuffer[0] = opcode;
  switch (secondByte) {
    case 126:
      outputBuffer.writeUInt16BE(dataLength, 2);
      break;
    case 127:
      outputBuffer.writeUInt32BE(0, 2);
      outputBuffer.writeUInt32BE(dataLength, 6);
  }
  if (maskData) {
    var mask = this._randomMask || (this._randomMask = getRandomMask());
    mask.copy(outputBuffer, dataOffset - 4);
    applyMaskToBuffer(dataBuffer, mask);
    secondByte = secondByte | 0x80;
  }
  outputBuffer[1] = secondByte;
  dataBuffer.copy(outputBuffer, dataOffset);
  return outputBuffer;
}

module.exports = Sender;

function applyMaskToBuffer(buf, mask) {
  if (typeof buf == 'string') buf = new Buffer(buf);
  for (var i = 0, l = buf.length; i < l; ++i) buf[i] ^= mask[i % 4];
  return buf;
}

function getBufferFromData(data) {
  if (!data) return new Buffer(0);
  if (data instanceof Buffer) return data;
  return (data && typeof data.buffer !== 'undefined')
     ? getArrayBuffer(data.buffer)
     : new Buffer(data);
}

function getArrayBuffer(array) {
  var l = array.byteLength
    , buffer = new Buffer(l);
  for (var i = 0; i < l; ++i) {
    buffer[i] = array[i];
  }
  return buffer;
}

function getRandomMask() {
  return new Buffer([
    ~~(Math.random() * 255),
    ~~(Math.random() * 255),
    ~~(Math.random() * 255),
    ~~(Math.random() * 255)
  ]);
}
<commit_msg>node v0.4 compatibility<commit_after>/*!
 * ws: a node.js websocket client
 * Copyright(c) 2011 <NAME> <<EMAIL>>
 * MIT Licensed
 */

var events = require('events')
  , util = require('util')
  , EventEmitter = events.EventEmitter
  , ErrorCodes = require('./ErrorCodes');

/**
 * Node version 0.4 and 0.6 compatibility
 */

var writeUInt16BE = /^v0\.4/.test(process.version) == false
  ? Buffer.prototype.writeUInt16BE
  : function(value, offset) {
    this[offset] = value >>> 8;
    this[offset + 1] = value % 256;
  };

var writeUInt32BE = /^v0\.4/.test(process.version) == false
  ? Buffer.prototype.writeUInt32BE
  : function(value, offset) {
    for (var i = offset + 3; i >= offset; --i) {
      this[i] = value & 0xff;
      value >>>= 8;
    }
  };

/**
 * HyBi Sender implementation
 */

function Sender (socket) {
  this._socket = socket;
  this.firstFragment = true;
}

/**
 * Inherits from EventEmitter.
 */

util.inherits(Sender, events.EventEmitter);

/**
 * Sends a close instruction to the remote party.
 *
 * @api public
 */

Sender.prototype.close = function(code, data, options) {
  if (typeof code !== 'undefined') {
    if (typeof code !== 'number' || 
      !ErrorCodes.isValidErrorCode(code)) throw new Error('first argument must be a valid error code number');
  }
  code = code || 1000;
  var dataBuffer = new Buffer(2 + (data ? Buffer.byteLength(data) : 0));
  writeUInt16BE.call(dataBuffer, code, 0);
  if (dataBuffer.length > 2) dataBuffer.write(data, 2);
  var buf = this.frameData(0x8, dataBuffer, true, true); // always masked
  this._socket.write(buf, 'binary');
}

/**
 * Sends a ping message to the remote party.
 *
 * @api public
 */

Sender.prototype.ping = function(data, options) {
  var mask = options && options.mask;
  var buf = this.frameData(0x9, data || '', true, mask);
  this._socket.write(buf, 'binary');
}

/**
 * Sends a pong message to the remote party.
 *
 * @api public
 */

Sender.prototype.pong = function(data, options) {
  var mask = options && options.mask;
  var buf = this.frameData(0xa, data || '', true, mask);
  this._socket.write(buf, 'binary');
}

/**
 * Sends text or binary data to the remote party.
 *
 * @api public
 */

Sender.prototype.send = function(data, options, cb) {
  var buf;
  var finalFragment = options && options.fin === false ? false : true;
  var mask = options && options.mask;
  var opcode = options && options.binary ? 2 : 1;
  if (this.firstFragment === false) opcode = 0;
  else this.firstFragment = false;
  buf = this.frameData(opcode, data, finalFragment, mask);
  if (finalFragment) this.firstFragment = true
  this._socket.write(buf, 'binary', cb);
}

/**
 * Frames a piece of data according to the HyBi WebSocket protocol.
 *
 * @api private
 */

Sender.prototype.frameData = function(opcode, data, finalFragment, maskData) {
  var dataBuffer = getBufferFromData(data)
    , dataLength = dataBuffer.length
    , dataOffset = maskData ? 6 : 2
    , secondByte = dataLength;
  if (dataLength >= 65536) {
    dataOffset += 8;
    secondByte = 127;
  }
  else if (dataLength > 125) {
    dataOffset += 2;
    secondByte = 126;
  }
  var outputBuffer = new Buffer(dataLength + dataOffset);
  if (finalFragment) opcode = opcode | 0x80;
  outputBuffer[0] = opcode;
  switch (secondByte) {
    case 126:
      writeUInt16BE.call(outputBuffer, dataLength, 2);
      break;
    case 127:
      writeUInt32BE.call(outputBuffer, 0, 2);
      writeUInt32BE.call(outputBuffer, dataLength, 6);
  }
  if (maskData) {
    var mask = this._randomMask || (this._randomMask = getRandomMask());
    mask.copy(outputBuffer, dataOffset - 4);
    applyMaskToBuffer(dataBuffer, mask);
    secondByte = secondByte | 0x80;
  }
  outputBuffer[1] = secondByte;
  dataBuffer.copy(outputBuffer, dataOffset);
  return outputBuffer;
}

module.exports = Sender;

function applyMaskToBuffer(buf, mask) {
  if (typeof buf == 'string') buf = new Buffer(buf);
  for (var i = 0, l = buf.length; i < l; ++i) buf[i] ^= mask[i % 4];
  return buf;
}

function getBufferFromData(data) {
  if (!data) return new Buffer(0);
  if (data instanceof Buffer) return data;
  return (data && typeof data.buffer !== 'undefined')
     ? getArrayBuffer(data.buffer)
     : new Buffer(data);
}

function getArrayBuffer(array) {
  var l = array.byteLength
    , buffer = new Buffer(l);
  for (var i = 0; i < l; ++i) {
    buffer[i] = array[i];
  }
  return buffer;
}

function getRandomMask() {
  return new Buffer([
    ~~(Math.random() * 255),
    ~~(Math.random() * 255),
    ~~(Math.random() * 255),
    ~~(Math.random() * 255)
  ]);
}

{'fraction_non_alphanumeric': 0.14864551979624913, 'fraction_numerical': 0.021995832368603842, 'mean_word_length': 3.4325295023088764, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 22, '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': '7607678', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2863, 'n_words': 786}