<commit_before>const _ = require('lodash');
const uuid = require('uuid');
const when = require('when');
const sequence = require('when/sequence');

const BaseConnector = require('./connector/base');
const FileSystem = require('./fs');
const Commands = require('./commands');
const errors = require('./errors');
const DEFAULT_MESSAGE = require('./messages');

class FtpConnection {
  constructor(server, options) {
    this.server = server;
    this.id = uuid.v4();
    this.log = options.log.child({id: this.id, ip: this.ip});
    this.commands = new Commands(this);
    this.encoding = 'utf-8';

    this.connector = new BaseConnector(this);

    this.commandSocket = options.socket;
    this.commandSocket.on('error', err => {
      this.log.error(err, 'Client error');
      this.server.emit('client-error', {connection: this, context: 'commandSocket', error: err});
    });
    this.commandSocket.on('data', this._handleData.bind(this));
    this.commandSocket.on('timeout', () => {});
    this.commandSocket.on('close', () => {
      if (this.connector) this.connector.end();
      if (this.commandSocket && !this.commandSocket.destroyed) this.commandSocket.destroy();
    });
  }

  _handleData(data) {
    const messages = _.compact(data.toString('utf-8').split('\r\n'));
    this.log.trace(messages, 'Messages');
    return sequence(messages.map(message => this.commands.handle.bind(this.commands, message)));
  }

  get ip() {
    try {
      return this.commandSocket.remoteAddress;
    } catch (ex) {
      return null;
    }
  }

  close(code = 421, message = 'Closing connection') {
    return when
      .resolve(code)
      .then(_code => _code && this.reply(_code, message))
      .then(() => this.commandSocket && this.commandSocket.end());
  }

  login(username, password) {
    return when.try(() => {
      const loginListeners = this.server.listeners('login');
      if (!loginListeners || !loginListeners.length) {
        if (!this.server.options.anoymous) throw new errors.GeneralError('No "login" listener setup', 500);
      } else {
        return this.server.emitPromise('login', {connection: this, username, password});<commit_msg>fix: get dataSocket ip if available<commit_after>const _ = require('lodash');
const uuid = require('uuid');
const when = require('when');
const sequence = require('when/sequence');

const BaseConnector = require('./connector/base');
const FileSystem = require('./fs');
const Commands = require('./commands');
const errors = require('./errors');
const DEFAULT_MESSAGE = require('./messages');

class FtpConnection {
  constructor(server, options) {
    this.server = server;
    this.id = uuid.v4();
    this.log = options.log.child({id: this.id, ip: this.ip});
    this.commands = new Commands(this);
    this.encoding = 'utf-8';

    this.connector = new BaseConnector(this);

    this.commandSocket = options.socket;
    this.commandSocket.on('error', err => {
      this.log.error(err, 'Client error');
      this.server.emit('client-error', {connection: this, context: 'commandSocket', error: err});
    });
    this.commandSocket.on('data', this._handleData.bind(this));
    this.commandSocket.on('timeout', () => {});
    this.commandSocket.on('close', () => {
      if (this.connector) this.connector.end();
      if (this.commandSocket && !this.commandSocket.destroyed) this.commandSocket.destroy();
    });
  }

  _handleData(data) {
    const messages = _.compact(data.toString('utf-8').split('\r\n'));
    this.log.trace(messages, 'Messages');
    return sequence(messages.map(message => this.commands.handle.bind(this.commands, message)));
  }

  get ip() {
    try {
      return this.dataSocket ? this.dataSocket.remoteAddress : this.commandSocket.remoteAddress;
    } catch (ex) {
      return null;
    }
  }

  close(code = 421, message = 'Closing connection') {
    return when
      .resolve(code)
      .then(_code => _code && this.reply(_code, message))
      .then(() => this.commandSocket && this.commandSocket.end());
  }

  login(username, password) {
    return when.try(() => {
      const loginListeners = this.server.listeners('login');
      if (!loginListeners || !loginListeners.length) {
        if (!this.server.options.anoymous) throw new errors.GeneralError('No "login" listener setup', 500);
      } else {
        return this.server.emitPromise('login', {connection: this, username, password});
{'fraction_non_alphanumeric': 0.1703096539162113, 'fraction_numerical': 0.004098360655737705, 'mean_word_length': 3.638859556494192, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '1154950', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1294, 'n_words': 270}