<commit_before>var Promise = require('bluebird');
var Helpers = require('./helpers');
var TEventEmitter = require('./traits/t-event-emitter');
var TTransactionGateway = require('./traits/t-transaction-gateway');
var JanusError = require('./error');
var Transaction = require('./transaction');
var WebsocketConnection = require('./websocket-connection');
var Session = require('./session');

/**
 * @param {string} id
 * @param {string} address
 * @param {Object} [options]
 * @param {string} [options.token]
 * @param {string} [options.apisecret]
 * @param {boolean|number} [options.keepalive]
 *
 * Important! Please listen to `error` events on a newly created instance in Node environment. For more details please look https://nodejs.org/api/events.html#events_error_events.
 * @constructor
 * @extends TEventEmitter
 * @extends TTransactionGateway
 */
function Connection(id, address, options) {
  /** @type {string} */
  this._id = id;

  /** @type {string} */
  this._address = address;

  /** @type {Object} */
  this._options = options || {};

  /** @type {Object} */
  this._sessions = {};

  /** @type {WebsocketConnection} */
  this._websocketConnection = new WebsocketConnection();
  this._installWebsocketListeners();
}

Helpers.extend(Connection.prototype, TEventEmitter, TTransactionGateway);

/**
 * @see {@link Connection}
 * @return {Connection}
 */
Connection.create = function(id, address, options) {
  return new Connection(id, address, options);
};

/**
 * @protected
 */
Connection.prototype._installWebsocketListeners = function() {
  this._websocketConnection.on('open', this.emit.bind(this));
  this._websocketConnection.on('error', this.emit.bind(this));
  this._websocketConnection.on('close', this.emit.bind(this));
  this._websocketConnection.on('message', this.processIncomeMessage.bind(this));
};

/**
 * @return {string}
 */
Connection.prototype.getId = function() {
  return this._id;
};

/**
 * @return {string}
 */
Connection.prototype.getAddress = function() {
  return this._address;
};

/**
 * @return {Object}
 */
Connection.prototype.getOptions = function() {
  return this._options;
};

/**
 * @return {Promise}
 */
Connection.prototype.open = function() {
  return this._websocketConnection.open(this._address, 'janus-protocol');
};

/**
 * @return {Promise}
 */
Connection.prototype.close = function() {
  if (this._websocketConnection.isOpened()) {
    this._websocketConnection.close();
  }
  else {
    this.emit('close');
  }
};

/**
 * @return {Promise}
 */
Connection.prototype.createSession = function() {
  return this.sendSync({janus: 'create'});
};

/**
 * @param {string} sessionId
 * @return {boolean}
 */
Connection.prototype.hasSession = function(sessionId) {
  return !!this.getSession(sessionId);
};

/**
 * @param {string} sessionId
 * @return {Session}
 */
Connection.prototype.getSession = function(sessionId) {
  return this._sessions[sessionId];
};

/**
 * @param {Session} session
 */
Connection.prototype.addSession = function(session) {
  this._sessions[session.getId()] = session;
  session.once('destroy', function() {
    this.removeSession(session.getId());
  }.bind(this));
};

/**
 * @param {string} sessionId
 */
Connection.prototype.removeSession = function(sessionId) {
  delete this._sessions[sessionId];
};

/**
 * @param {Object} message
 * @return {Promise}
 */
Connection.prototype.send = function(message) {
  if (this._options['token']) {
    message.token = this._options['token'];
  }
  if (this._options['apisecret']) {
    message.apisecret = this._options['apisecret'];
  }
  return this._websocketConnection.send(message);
};

/**
 * @param {Object} message
 * @return {Promise}
 */
Connection.prototype.processOutcomeMessage = function(message) {
  var janusMessage = message['janus'];
  if ('create' === janusMessage) {
    return this._onCreate(message);
  }
  var sessionId = message['session_id'];
  if (sessionId) {
    if (this.hasSession(sessionId)) {
      return this.getSession(sessionId).processOutcomeMessage(message);<commit_msg>Always add transaction on connection.send if it's not present<commit_after>var Promise = require('bluebird');
var Helpers = require('./helpers');
var TEventEmitter = require('./traits/t-event-emitter');
var TTransactionGateway = require('./traits/t-transaction-gateway');
var JanusError = require('./error');
var Transaction = require('./transaction');
var WebsocketConnection = require('./websocket-connection');
var Session = require('./session');

/**
 * @param {string} id
 * @param {string} address
 * @param {Object} [options]
 * @param {string} [options.token]
 * @param {string} [options.apisecret]
 * @param {boolean|number} [options.keepalive]
 *
 * Important! Please listen to `error` events on a newly created instance in Node environment. For more details please look https://nodejs.org/api/events.html#events_error_events.
 * @constructor
 * @extends TEventEmitter
 * @extends TTransactionGateway
 */
function Connection(id, address, options) {
  /** @type {string} */
  this._id = id;

  /** @type {string} */
  this._address = address;

  /** @type {Object} */
  this._options = options || {};

  /** @type {Object} */
  this._sessions = {};

  /** @type {WebsocketConnection} */
  this._websocketConnection = new WebsocketConnection();
  this._installWebsocketListeners();
}

Helpers.extend(Connection.prototype, TEventEmitter, TTransactionGateway);

/**
 * @see {@link Connection}
 * @return {Connection}
 */
Connection.create = function(id, address, options) {
  return new Connection(id, address, options);
};

/**
 * @protected
 */
Connection.prototype._installWebsocketListeners = function() {
  this._websocketConnection.on('open', this.emit.bind(this));
  this._websocketConnection.on('error', this.emit.bind(this));
  this._websocketConnection.on('close', this.emit.bind(this));
  this._websocketConnection.on('message', this.processIncomeMessage.bind(this));
};

/**
 * @return {string}
 */
Connection.prototype.getId = function() {
  return this._id;
};

/**
 * @return {string}
 */
Connection.prototype.getAddress = function() {
  return this._address;
};

/**
 * @return {Object}
 */
Connection.prototype.getOptions = function() {
  return this._options;
};

/**
 * @return {Promise}
 */
Connection.prototype.open = function() {
  return this._websocketConnection.open(this._address, 'janus-protocol');
};

/**
 * @return {Promise}
 */
Connection.prototype.close = function() {
  if (this._websocketConnection.isOpened()) {
    this._websocketConnection.close();
  }
  else {
    this.emit('close');
  }
};

/**
 * @return {Promise}
 */
Connection.prototype.createSession = function() {
  return this.sendSync({janus: 'create'});
};

/**
 * @param {string} sessionId
 * @return {boolean}
 */
Connection.prototype.hasSession = function(sessionId) {
  return !!this.getSession(sessionId);
};

/**
 * @param {string} sessionId
 * @return {Session}
 */
Connection.prototype.getSession = function(sessionId) {
  return this._sessions[sessionId];
};

/**
 * @param {Session} session
 */
Connection.prototype.addSession = function(session) {
  this._sessions[session.getId()] = session;
  session.once('destroy', function() {
    this.removeSession(session.getId());
  }.bind(this));
};

/**
 * @param {string} sessionId
 */
Connection.prototype.removeSession = function(sessionId) {
  delete this._sessions[sessionId];
};

/**
 * @param {Object} message
 * @return {Promise}
 */
Connection.prototype.send = function(message) {
  if (this._options['token']) {
    message.token = this._options['token'];
  }
  if (this._options['apisecret']) {
    message.apisecret = this._options['apisecret'];
  }
  if (!message['transaction']) {
    message['transaction'] = Transaction.generateRandomId();
  }
  return this._websocketConnection.send(message);
};

/**
 * @param {Object} message
 * @return {Promise}
 */
Connection.prototype.processOutcomeMessage = function(message) {
  var janusMessage = message['janus'];
  if ('create' === janusMessage) {
    return this._onCreate(message);
  }
  var sessionId = message['session_id'];
  if (sessionId) {
    if (this.hasSession(sessionId)) {
      return this.getSession(sessionId).processOutcomeMessage(message);
{'fraction_non_alphanumeric': 0.1710109622411693, 'fraction_numerical': 0.0, 'mean_word_length': 4.578125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1631090', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2490, 'n_words': 487}