<commit_before>function auth (sync, host, credentials, callback) {
  if (!sync.options.auth) {
    sync.authenticated = true
    if (callback) callback()
    return
  }

  sync.authenticating = true
  sync.options.auth(credentials, host).then(function (access) {
    if (access) {
      sync.authenticated = true
      sync.authenticating = false

      if (callback) callback()
      for (var i = 0; i < sync.unauthenticated.length; i++) {
        sync.onMessage(sync.unauthenticated[i])
      }
      sync.unauthenticated = []
    } else {
      sync.sendError('Wrong credentials', 'auth')
      sync.destroy()
    }
  })
}

module.exports = {

  sendConnect: function sendConnect () {
    var message = ['connect', this.protocol, this.host, this.otherSynced]
    if (this.options.credentials) message.push(this.options.credentials)
    if (this.options.fixTime) this.connectSended = this.log.timer()[0]
    this.startTimeout()
    this.send(message)
    this.syncSince(this.synced)
  },

  sendConnected: function sendConnected (start, end) {
    var message = ['connected', this.protocol, this.host, [start, end]]
    if (this.options.credentials) message.push(this.options.credentials)
    this.send(message)
  },

  connectMessage: function connectMessage (ver, host, synced, credentials) {
    this.otherHost = host
    this.otherProtocol = ver

    var major = this.protocol[0]
    if (major !== ver[0]) {
      this.sendError('Only ' + major + '.x protocols are supported, ' +
                     'but you use ' + ver.join('.'), 'protocol')
      this.destroy()
      return
    }

    var sync = this
    var start = this.log.timer()[0]
    auth(this, host, credentials, function () {
      sync.sendConnected(start, sync.log.timer()[0])
      sync.syncSince(synced)
    })
  },

  connectedMessage: function connectedMessage (ver, host, time, credentials) {
    this.endTimeout()
    this.otherHost = host
    this.otherProtocol = ver

    if (this.options.fixTime) {
      var now = this.log.timer()[0]
      var authTime = time[1] - time[0]
      var roundTrip = now - this.connectSended - authTime
      this.timeFix = this.connectSended - time[0] + roundTrip / 2
    }

    auth(this, host, credentials)
  }

}<commit_msg>Send events after connected received to fix time<commit_after>function auth (sync, host, credentials, callback) {
  if (!sync.options.auth) {
    sync.authenticated = true
    callback()
    return
  }

  sync.authenticating = true
  sync.options.auth(credentials, host).then(function (access) {
    if (access) {
      sync.authenticated = true
      sync.authenticating = false

      callback()
      for (var i = 0; i < sync.unauthenticated.length; i++) {
        sync.onMessage(sync.unauthenticated[i])
      }
      sync.unauthenticated = []
    } else {
      sync.sendError('Wrong credentials', 'auth')
      sync.destroy()
    }
  })
}

module.exports = {

  sendConnect: function sendConnect () {
    var message = ['connect', this.protocol, this.host, this.otherSynced]
    if (this.options.credentials) message.push(this.options.credentials)
    if (this.options.fixTime) this.connectSended = this.log.timer()[0]
    this.startTimeout()
    this.send(message)
  },

  sendConnected: function sendConnected (start, end) {
    var message = ['connected', this.protocol, this.host, [start, end]]
    if (this.options.credentials) message.push(this.options.credentials)
    this.send(message)
  },

  connectMessage: function connectMessage (ver, host, synced, credentials) {
    this.otherHost = host
    this.otherProtocol = ver

    var major = this.protocol[0]
    if (major !== ver[0]) {
      this.sendError('Only ' + major + '.x protocols are supported, ' +
                     'but you use ' + ver.join('.'), 'protocol')
      this.destroy()
      return
    }

    var sync = this
    var start = this.log.timer()[0]
    auth(this, host, credentials, function () {
      sync.sendConnected(start, sync.log.timer()[0])
      sync.syncSince(synced)
    })
  },

  connectedMessage: function connectedMessage (ver, host, time, credentials) {
    this.endTimeout()
    this.otherHost = host
    this.otherProtocol = ver

    if (this.options.fixTime) {
      var now = this.log.timer()[0]
      var authTime = time[1] - time[0]
      var roundTrip = now - this.connectSended - authTime
      this.timeFix = this.connectSended - time[0] + roundTrip / 2
    }

    var sync = this
    auth(this, host, credentials, function () {
      sync.syncSince(sync.synced)
    })
  }

}
{'fraction_non_alphanumeric': 0.13345117101193107, 'fraction_numerical': 0.004860804242156429, 'mean_word_length': 2.82347972972973, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6095102', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1394, 'n_words': 317}