<commit_before>'use strict'

var hijackResponse = require('hijackresponse')
var streamBuffers = require('stream-buffers')
var without = require('lodash/without')

require('express-negotiate')

function middleware (options, req, res, next) {
  req.negotiate({
    html: function () {
      // remove all request header sent from the client which are not required
      without(Object.keys(req.headers), 'host', 'x-forwarded-host', 'x-forwarded-proto').forEach(function (name) {
        delete req.headers[name]
      })

      // set html middleware request headers for the handler
      req.headers['accept'] = options.render.accept

      hijackResponse(res, function (err, res) {
        if (err) {
          res.unhijack()

          return next(err)
        }

        var graphBuffer = new streamBuffers.WritableStreamBuffer()

        graphBuffer.on('finish', function () {
          var graphString = graphBuffer.getContentsAsString('utf8')

          var locals = {
            iri: req.absoluteUrl(),
            statusCode: res.statusCode,
            graph: graphString
          }

          // remove all response headers sent from handler
          if (res._headers) {
            Object.keys(res._headers).forEach(function (name) {
              res.removeHeader(name)
            })
          }

          // set new response headers
          res.setHeader('content-type', 'text/html')

          // use renderer to build body
          if (res.statusCode === 200) {
            res.end(options.render(locals))
          } else {
            // use .error method if renderer has one
            if (options.render.error) {
              res.end(options.render.error(locals))
            } else {
              res.end(options.render(locals))
            }
          }
        })

        res.pipe(graphBuffer)
      })

      next()
    },

    default: function () {
      next()
    }
  })
}
<commit_msg>added support for async renderer<commit_after>'use strict'

var hijackResponse = require('hijackresponse')
var streamBuffers = require('stream-buffers')
var without = require('lodash/without')

require('express-negotiate')

function render (res, locals, callback) {
  return Promise.resolve().then(function () {
    return callback(locals)
  }).then(function (output) {
    res.end(output)
  })
}

function middleware (options, req, res, next) {
  req.negotiate({
    html: function () {
      // remove all request header sent from the client which are not required
      without(Object.keys(req.headers), 'host', 'x-forwarded-host', 'x-forwarded-proto').forEach(function (name) {
        delete req.headers[name]
      })

      // set html middleware request headers for the handler
      req.headers['accept'] = options.render.accept

      hijackResponse(res, function (err, res) {
        if (err) {
          res.unhijack()

          return next(err)
        }

        var graphBuffer = new streamBuffers.WritableStreamBuffer()

        graphBuffer.on('finish', function () {
          var graphString = graphBuffer.getContentsAsString('utf8')

          var locals = {
            iri: req.absoluteUrl(),
            statusCode: res.statusCode,
            graph: graphString
          }

          // remove all response headers sent from handler
          if (res._headers) {
            Object.keys(res._headers).forEach(function (name) {
              res.removeHeader(name)
            })
          }

          // set new response headers
          res.setHeader('content-type', 'text/html')

          // use renderer to build body
          if (res.statusCode === 200) {
            render(res, locals, options.render)
          } else {
            // use .error method if renderer has one
            if (options.render.error) {
              render(res, locals, options.render.error)
            } else {
              render(res, locals, options.render)
            }
          }
        })

        res.pipe(graphBuffer)
      })

      next()
    },

    default: function () {
      next()
    }
  })
}

{'fraction_non_alphanumeric': 0.11577388299185386, 'fraction_numerical': 0.001974821031843989, 'mean_word_length': 1.9383611312545324, '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': '3481366', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1089, 'n_words': 264}