<commit_before>const { filter, map, waterfall } = require('./plus')

// Request Function
// ===============================
//
// Generates the actual HTTP requests to GitHub.
// Handles ETag caching, authentication headers, boolean requests, and paged results

// Simple jQuery.ajax() shim that returns a promise for a xhr object
let ajax = function (options, cb) {
  // Use the browser XMLHttpRequest if it exists. If not, then this is NodeJS
  // Pull this in for every request so sepia.js has a chance to override `window.XMLHTTPRequest`
  const XMLHttpRequest = require('./adapters/xhr')
  let xhr = new XMLHttpRequest()
  xhr.dataType = options.dataType
  if (options.mimeType) {
    __guardFunc__(xhr.overrideMimeType, f => f(options.mimeType))
  }
  xhr.open(options.type, options.url)

  if (options.data && options.type !== 'GET') {
    xhr.setRequestHeader('Content-Type', options.contentType)
  }

  for (let name in options.headers) {
    let value = options.headers[name]
    xhr.setRequestHeader(name, value)
  }

  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      __guardFunc__(__guard__(options.statusCode, x => x[xhr.status]), f1 => f1())

      // When disconnected, pass if the status is 0 so the cacheHandler has a chance to return the cached version
      if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304 || xhr.status === 302 || xhr.status === 0) {
        return cb(null, xhr)
      } else {
        return cb(xhr)
      }
    }
  }
  return xhr.send(options.data)
}

// # Construct the request function.
// It contains all the auth credentials passed in to the client constructor

let EVENT_ID = 0 // counter for the emitter so it is easier to match up requests

module.exports = class Requester {
  constructor (_instance, _clientOptions = {}, plugins) {
    // Provide an option to override the default URL
    this._instance = _instance
    this._clientOptions = _clientOptions
    if (this._clientOptions.rootURL == null) { this._clientOptions.rootURL = 'https://api.github.com' }
    if (this._clientOptions.useETags == null) { this._clientOptions.useETags = true }
    if (this._clientOptions.usePostInsteadOfPatch == null) { this._clientOptions.usePostInsteadOfPatch = false }

    // These are updated whenever a request is made (optional)
    if (typeof this._clientOptions.emitter === 'function') {
      this._emit = this._clientOptions.emitter
    }

    this._pluginMiddlewareAsync = map(filter(plugins, ({requestMiddlewareAsync}) => requestMiddlewareAsync), plugin => plugin.requestMiddlewareAsync.bind(plugin)
    )
    this._plugins = plugins
  }

  // HTTP Request Abstraction
  // =======
  //
  request (method, path, data, options = {isRaw: false, isBase64: false, isBoolean: false, contentType: 'application/json'}, cb) {
    if (typeof options === 'undefined' || options === null) { options = {} }
    if (options.isRaw == null) { options.isRaw = false }
    if (options.isBase64 == null) { options.isBase64 = false }
    if (options.isBoolean == null) { options.isBoolean = false }
    if (options.contentType == null) { options.contentType = 'application/json' }

    // console.log method, path, data, options, typeof cb

    // Only prefix the path when it does not begin with http.
    // This is so pagination works (which provides absolute URLs).
    if (!/^http/.test(path)) { path = `${this._clientOptions.rootURL}${path}` }

    let headers =
      {'Accept': this._clientOptions.acceptHeader || 'application/json'}

    if (typeof window === 'undefined' || window === null) {
      // Set the `User-Agent` because it is required and NodeJS
      // does not send one by default.
      // See http://developer.github.com/v3/#user-agent-required
      headers['User-Agent'] = 'octokat.js'
    }

    let acc = {method, path, headers, options, clientOptions: this._clientOptions}

    // To use async.waterfall we need to pass in the initial data (`acc`)
    // so we create an initial function that just takes a callback
    let initial = cb => cb(null, acc)<commit_msg>Accept a user agent<commit_after>const { filter, map, waterfall } = require('./plus')

// Request Function
// ===============================
//
// Generates the actual HTTP requests to GitHub.
// Handles ETag caching, authentication headers, boolean requests, and paged results

// Simple jQuery.ajax() shim that returns a promise for a xhr object
let ajax = function (options, cb) {
  // Use the browser XMLHttpRequest if it exists. If not, then this is NodeJS
  // Pull this in for every request so sepia.js has a chance to override `window.XMLHTTPRequest`
  const XMLHttpRequest = require('./adapters/xhr')
  let xhr = new XMLHttpRequest()
  xhr.dataType = options.dataType
  if (options.mimeType) {
    __guardFunc__(xhr.overrideMimeType, f => f(options.mimeType))
  }
  xhr.open(options.type, options.url)

  if (options.data && options.type !== 'GET') {
    xhr.setRequestHeader('Content-Type', options.contentType)
  }

  for (let name in options.headers) {
    let value = options.headers[name]
    xhr.setRequestHeader(name, value)
  }

  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      __guardFunc__(__guard__(options.statusCode, x => x[xhr.status]), f1 => f1())

      // When disconnected, pass if the status is 0 so the cacheHandler has a chance to return the cached version
      if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304 || xhr.status === 302 || xhr.status === 0) {
        return cb(null, xhr)
      } else {
        return cb(xhr)
      }
    }
  }
  return xhr.send(options.data)
}

// # Construct the request function.
// It contains all the auth credentials passed in to the client constructor

let EVENT_ID = 0 // counter for the emitter so it is easier to match up requests

module.exports = class Requester {
  constructor (_instance, _clientOptions = {}, plugins) {
    // Provide an option to override the default URL
    this._instance = _instance
    this._clientOptions = _clientOptions
    if (this._clientOptions.rootURL == null) { this._clientOptions.rootURL = 'https://api.github.com' }
    if (this._clientOptions.useETags == null) { this._clientOptions.useETags = true }
    if (this._clientOptions.usePostInsteadOfPatch == null) { this._clientOptions.usePostInsteadOfPatch = false }
    if (this._clientOptions.userAgent == null) {
      if (typeof window === 'undefined' || window === null) {
        // Set the `User-Agent` because it is required and NodeJS
        // does not send one by default.
        // See http://developer.github.com/v3/#user-agent-required
        this._clientOptions.userAgent = 'octokat.js'
      }
    }

    // These are updated whenever a request is made (optional)
    if (typeof this._clientOptions.emitter === 'function') {
      this._emit = this._clientOptions.emitter
    }

    this._pluginMiddlewareAsync = map(filter(plugins, ({requestMiddlewareAsync}) => requestMiddlewareAsync), plugin => plugin.requestMiddlewareAsync.bind(plugin)
    )
    this._plugins = plugins
  }

  // HTTP Request Abstraction
  // =======
  //
  request (method, path, data, options = {isRaw: false, isBase64: false, isBoolean: false, contentType: 'application/json'}, cb) {
    if (typeof options === 'undefined' || options === null) { options = {} }
    if (options.isRaw == null) { options.isRaw = false }
    if (options.isBase64 == null) { options.isBase64 = false }
    if (options.isBoolean == null) { options.isBoolean = false }
    if (options.contentType == null) { options.contentType = 'application/json' }

    // console.log method, path, data, options, typeof cb

    // Only prefix the path when it does not begin with http.
    // This is so pagination works (which provides absolute URLs).
    if (!/^http/.test(path)) { path = `${this._clientOptions.rootURL}${path}` }

    let headers =
      {'Accept': this._clientOptions.acceptHeader || 'application/json'}
    
    if (this._clientOptions.userAgent) {
      headers['User-Agent'] = this._clientOptions.userAgent
    }

    let acc = {method, path, headers, options, clientOptions: this._clientOptions}

    // To use async.waterfall we need to pass in the initial data (`acc`)
    // so we create an initial function that just takes a callback
    let initial = cb => cb(null, acc)
{'fraction_non_alphanumeric': 0.13114950803935685, 'fraction_numerical': 0.005999520038396928, 'mean_word_length': 3.280945043656908, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '6092365', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2389, 'n_words': 761}