<commit_before>import xhr from 'xhr'
import { remote } from 'electron'
import hyperquest from 'hyperquest'
import querystring from 'querystring'

const osmServerHost = 'http://' + remote.getGlobal('osmServerHost')

module.exports = {
  stop,
  destroy,
  leave,
  listen,
  join,
  clear,
  peers,
  peerStream,
  start,
  createMedia,
  create,
  update,
  del,
  list,
  convert
}

function destroy (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/destroy`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    return cb(null, body)
  })
}

function leave (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/leave`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    return cb(null, body)
  })
}

function join (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/join`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    return cb(null, body)
  })
}


function listen (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/listen`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    return cb(null, body)
  })
}

function peerStream (opts) {
  if (!opts) opts = { interval: 1000}
  return hyperquest({
    method: 'GET',
    uri: `${osmServerHost}/sync/peers?interval=${opts.interval}`
  })
}

function peers (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/peers`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    try {
      var data = JSON.parse(body)
      if (data.topic === 'peers') return cb(null, data.message)
      else return cb(new Error('unknown response', data))
    } catch (err) {
      return cb(err)
    }
  })
}

function stop (target, cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/stop`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    cb(null, body)
  })
}

function start (target, opts) {
  if (!opts) opts = {}
  return hyperquest({
    method: 'GET',
    uri: `${osmServerHost}/sync/start?${querystring.stringify(target)}&interval=${opts.interval}`
  })
}

function clear (cb) {
  var opts = {
    url: `${osmServerHost}/sync/clear`,
    method: 'GET'
  }
  xhr(opts, cb)
}

function createMedia (buf, cb) {
  var opts = {
    url: `${osmServerHost}/media`,
    method: 'POST',
    body: buf
  }
  xhr(opts, cb)<commit_msg>use clearState api<commit_after>import xhr from 'xhr'
import { remote } from 'electron'
import hyperquest from 'hyperquest'
import querystring from 'querystring'

const osmServerHost = 'http://' + remote.getGlobal('osmServerHost')

module.exports = {
  stop,
  destroy,
  leave,
  listen,
  join,
  clearState,
  peers,
  peerStream,
  start,
  createMedia,
  create,
  update,
  del,
  list,
  convert
}

function destroy (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/destroy`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    return cb(null, body)
  })
}

function leave (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/leave`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    return cb(null, body)
  })
}

function join (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/join`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    return cb(null, body)
  })
}


function listen (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/listen`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    return cb(null, body)
  })
}

function peerStream (opts) {
  if (!opts) opts = { interval: 1000}
  return hyperquest({
    method: 'GET',
    uri: `${osmServerHost}/sync/peers?interval=${opts.interval}`
  })
}

function peers (cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/peers`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    try {
      var data = JSON.parse(body)
      if (data.topic === 'peers') return cb(null, data.message)
      else return cb(new Error('unknown response', data))
    } catch (err) {
      return cb(err)
    }
  })
}

function stop (target, cb) {
  var opts = {
    method: 'GET',
    url: `${osmServerHost}/sync/stop`
  }
  xhr(opts, function (err, res, body) {
    if (err) return cb(err)
    cb(null, body)
  })
}

function start (target, opts) {
  if (!opts) opts = {}
  return hyperquest({
    method: 'GET',
    uri: `${osmServerHost}/sync/start?${querystring.stringify(target)}&interval=${opts.interval}`
  })
}

function clearState (cb) {
  var opts = {
    url: `${osmServerHost}/sync/clearState`,
    method: 'GET'
  }
  xhr(opts, cb)
}

function createMedia (buf, cb) {
  var opts = {
    url: `${osmServerHost}/media`,
    method: 'POST',
    body: buf
  }
  xhr(opts, cb)
{'fraction_non_alphanumeric': 0.16393442622950818, 'fraction_numerical': 0.001639344262295082, 'mean_word_length': 2.97799511002445, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5353728', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1829, 'n_words': 463}