<commit_before>var http = require('http')
  , url = require('url')

var memento = require('memento-client')
  , request = require('hyperquest')

var WAYBACK_HOST = 'web.archive.org'
  , isAsset = /\/web\/\d{14}/

module.exports = peabody

function peabody(timestamp) {
  return http.createServer(handler)

  function handler(req, res) {
    if(isAsset.test(req.url)) {
      return request.get(
          url.format({
              host: WAYBACK_HOST
            , pathname: url.parse(req.url).path
            , protocol: 'http'
          })
        , assetRespond
      )
    }

    memento(req.url, timestamp, checkSites)

    function checkSites(err, sites) {
      if(err || !sites.length || !timeNear(sites[1].datetime, timestamp)) {
        res.writeHead(404, {'content-type': 'text/plain'})

        return res.end('Site not available')
      }

      request.get(sites[1].href, respond)
    }

    function respond(err, response) {
      if(err) {
        res.writeHead(404, {'content-type': 'text/plain'})

        return res.end('Site not available')
      }

      response.pipe(res)
      res.writeHead(response.statusCode, response.headers)
    }

    function assetRespond(err, response) {
      if(err) {
        res.writeHead(404, {'content-type': 'text/plain'})<commit_msg>minor bugfix<commit_after>var http = require('http')
  , url = require('url')

var memento = require('memento-client')
  , request = require('hyperquest')

var WAYBACK_HOST = 'web.archive.org'
  , isAsset = /\/web\/\d{14}/

module.exports = peabody

function peabody(timestamp) {
  return http.createServer(handler)

  function handler(req, res) {
    if(isAsset.test(req.url)) {
      return request.get(
          url.format({
              host: WAYBACK_HOST
            , pathname: url.parse(req.url).path
            , protocol: 'http'
          })
        , assetRespond
      )
    }

    memento(req.url, timestamp, checkSites)

    function checkSites(err, sites) {
      if(err || sites.length < 2 || !timeNear(sites[1].datetime, timestamp)) {
        res.writeHead(404, {'content-type': 'text/plain'})

        return res.end('Site not available')
      }

      request.get(sites[1].href, respond)
    }

    function respond(err, response) {
      if(err) {
        res.writeHead(404, {'content-type': 'text/plain'})

        return res.end('Site not available')
      }

      response.pipe(res)
      res.writeHead(response.statusCode, response.headers)
    }

    function assetRespond(err, response) {
      if(err) {
        res.writeHead(404, {'content-type': 'text/plain'})
{'fraction_non_alphanumeric': 0.14783281733746131, 'fraction_numerical': 0.01044891640866873, 'mean_word_length': 2.5217983651226157, 'pattern_counts': {'":': 0, '<': 4, '<?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': '4563170', 'n_tokens_mistral': 871, 'n_tokens_neox': 821, 'n_words': 151}