{"size": 836, "ext": "js", "max_stars_repo_path": "packages/lib/neft-request/native/browser/xhr.js", "max_stars_repo_name": "Neft-io/neftio", "max_stars_repo_stars_event_min_datetime": "2016-02-24T16:26:58.000Z", "max_stars_repo_stars_event_max_datetime": "2019-02-27T09:05:09.000Z", "max_issues_repo_path": "packages/lib/neft-request/native/browser/xhr.js", "max_issues_repo_name": "Neft-io/neft-cli", "max_issues_repo_issues_event_min_datetime": "2016-02-18T18:23:38.000Z", "max_issues_repo_issues_event_max_datetime": "2018-09-01T16:28:33.000Z", "max_forks_repo_path": "packages/lib/neft-request/native/browser/xhr.js", "max_forks_repo_name": "neftjs/neft", "max_forks_repo_forks_event_min_datetime": "2016-03-31T10:17:47.000Z", "max_forks_repo_forks_event_max_datetime": "2016-03-31T10:17:47.000Z"}
{"max_stars_count": 23, "max_issues_count": 255, "max_forks_count": 1, "avg_line_length": 25.3333333333, "max_line_length": 71, "alphanum_fraction": 0.6076555024}
const getHeadersFromXhr = (xhr) => {
  const result = {}
  const headers = xhr.getAllResponseHeaders()
  const lines = headers.trim().split(/[\r\n]+/)
  lines.forEach((line) => {
    const parts = line.split(': ')
    const header = parts.shift()
    const value = parts.join(': ')
    result[header] = value
  })
  return result
}

exports.send = ({
  uri, method, headers, body, timeout,
}, callback) => {
  const xhr = new global.XMLHttpRequest()
  xhr.open(method, uri, true)
  xhr.timeout = timeout
  Object.keys(headers).forEach((key) => {
    xhr.setRequestHeader(key, headers[key])
  })
  xhr.onload = () => {
    callback(null, {
      statusCode: xhr.status,
      body: xhr.response,
      headers: getHeadersFromXhr(xhr),
    })
  }
  xhr.onerror = () => { callback(`${xhr.status} ${xhr.response}`, {}) }
  xhr.send(body)
}
