<commit_before>/**
 * This module contains some common helpers shared between middlewares
 */

var mime = require('mime')
var _ = require('lodash')
var parseRange = require('range-parser')
var Buffer = require('safe-buffer').Buffer

var log = require('../logger').create('web-server')

var PromiseContainer = function () {
  var promise

  this.then = function (success, error) {
    error = error || _.noop
    return promise.then(success).catch(error)
  }

  this.set = function (newPromise) {
    promise = newPromise
  }
}

var serve404 = function (response, path) {
  log.warn('404: ' + path)
  response.writeHead(404)
  return response.end('NOT FOUND')
}

var createServeFile = function (fs, directory, config) {
  var cache = Object.create(null)

  return function (filepath, rangeHeader, response, transform, content, doNotCache) {
    var responseData

    var convertForRangeRequest = function () {
      var range = parseRange(responseData.length, rangeHeader)
      if (range === -2) {
        // malformed header string
        return 200
      } else if (range === -1) {
        // unsatisfiable range
        responseData = Buffer.alloc(0)
        return 416
      } else if (range.type === 'bytes') {
        responseData = Buffer.from(responseData)
        if (range.length === 1) {
          var start = range[0].start
          var end = range[0].end
          response.setHeader(
            'Content-Range',
            'bytes ' + start + '-' + end + '/' + responseData.length
          )
          response.setHeader('Accept-Ranges', 'bytes')
          response.setHeader('Content-Length', end - start + 1)
          responseData = responseData.slice(start, end + 1)
          return 206
        } else {
          // Multiple ranges are not supported. Maybe future?
          responseData = new Buffer(0)
          return 416
        }
      }
      // All other states, ignore
      return 200
    }

    if (directory) {
      filepath = directory + filepath
    }

    if (!content && cache[filepath]) {
      content = cache[filepath]
    }

    if (config && config.customHeaders && config.customHeaders.length > 0) {
      config.customHeaders.forEach(function (header) {
        var regex = new RegExp(header.match)
        if (regex.test(filepath)) {
          log.debug('setting header: ' + header.name + ' for: ' + filepath)
          response.setHeader(header.name, header.value)
        }
      })
    }

    // serve from cache
    if (content && !doNotCache) {
      response.setHeader('Content-Type', mime.lookup(filepath, 'text/plain'))

      // call custom transform fn to transform the data
      responseData = (transform && transform(content)) || content<commit_msg>fix(middleware): avoid using deprecated Buffer API<commit_after>/**
 * This module contains some common helpers shared between middlewares
 */

var mime = require('mime')
var _ = require('lodash')
var parseRange = require('range-parser')
var Buffer = require('safe-buffer').Buffer

var log = require('../logger').create('web-server')

var PromiseContainer = function () {
  var promise

  this.then = function (success, error) {
    error = error || _.noop
    return promise.then(success).catch(error)
  }

  this.set = function (newPromise) {
    promise = newPromise
  }
}

var serve404 = function (response, path) {
  log.warn('404: ' + path)
  response.writeHead(404)
  return response.end('NOT FOUND')
}

var createServeFile = function (fs, directory, config) {
  var cache = Object.create(null)

  return function (filepath, rangeHeader, response, transform, content, doNotCache) {
    var responseData

    var convertForRangeRequest = function () {
      var range = parseRange(responseData.length, rangeHeader)
      if (range === -2) {
        // malformed header string
        return 200
      } else if (range === -1) {
        // unsatisfiable range
        responseData = Buffer.alloc(0)
        return 416
      } else if (range.type === 'bytes') {
        responseData = Buffer.from(responseData)
        if (range.length === 1) {
          var start = range[0].start
          var end = range[0].end
          response.setHeader(
            'Content-Range',
            'bytes ' + start + '-' + end + '/' + responseData.length
          )
          response.setHeader('Accept-Ranges', 'bytes')
          response.setHeader('Content-Length', end - start + 1)
          responseData = responseData.slice(start, end + 1)
          return 206
        } else {
          // Multiple ranges are not supported. Maybe future?
          responseData = Buffer.alloc(0)
          return 416
        }
      }
      // All other states, ignore
      return 200
    }

    if (directory) {
      filepath = directory + filepath
    }

    if (!content && cache[filepath]) {
      content = cache[filepath]
    }

    if (config && config.customHeaders && config.customHeaders.length > 0) {
      config.customHeaders.forEach(function (header) {
        var regex = new RegExp(header.match)
        if (regex.test(filepath)) {
          log.debug('setting header: ' + header.name + ' for: ' + filepath)
          response.setHeader(header.name, header.value)
        }
      })
    }

    // serve from cache
    if (content && !doNotCache) {
      response.setHeader('Content-Type', mime.lookup(filepath, 'text/plain'))

      // call custom transform fn to transform the data
      responseData = (transform && transform(content)) || content
{'fraction_non_alphanumeric': 0.12383220370031141, 'fraction_numerical': 0.012456493863344935, 'mean_word_length': 2.416770963704631, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7427014', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1518, 'n_words': 405}