<commit_before>'use strict';

var request = require('request')
  , config = require('../config')
  , url = require('url')
  , cache = require('../lib/cache')
  , proxy = config('proxy')
  , REGISTRY = config('registry')
  ;

// set default proxy, if supplied
if (proxy) {
  console.log('> using proxy: "' + proxy + '"\n');
  request = request.defaults({ proxy: proxy });
}

var replaceTarballUrls = function (obj) {
  Object.keys(obj.versions).forEach(function (v) {
    var parsed = url.parse(obj.versions[v].dist.tarball);
    obj.versions[v].dist.tarball = url.resolve(process.env.DOMAIN || config('host'), parsed.path);
  });
};

exports.metadata = function (req, res) {
  cache.get(req.params.pkg, function (err, doc) {
    if (err) {
      request(REGISTRY + req.path, function (err, proxy, b) {
        if (err) {
          res.status(500).json(err);
        } else {
          try {
            var body = JSON.parse(b);
            // forward npmjs errors
            if (!body.error) {
              replaceTarballUrls(body);
              cache.put(body._id, body);
            }
            res.status(proxy.statusCode).set(proxy.headers).json(body);
          } catch (e) {
            res.status(500).json({
              error: e.message,
              body: b
            });
          }
        }
      });
    } else {
      req.log('getting ' + req.params.pkg + ' from cache...');
      res.status(200).json(doc);
    }
  });
};

exports.tarball = function (req, res) {
  cache.get(req.params.tar, function (err, body) {
    if (err) {
      request(REGISTRY + req.path, { encoding: null }, function (err, resp, body) {
        cache.put(req.params.tar, body);
      }).pipe(res);
    } else {
      req.log('getting ' + req.params.tar + ' from cache...');
      res.send(new Buffer(body, 'binary'));
    }
  });
};<commit_msg>Updated proxy to only cache 200 responses.<commit_after>'use strict';

var request = require('request')
  , config = require('../config')
  , url = require('url')
  , cache = require('../lib/cache')
  , proxy = config('proxy')
  , REGISTRY = config('registry')
  ;

// set default proxy, if supplied
if (proxy) {
  console.log('> using proxy: "' + proxy + '"\n');
  request = request.defaults({ proxy: proxy });
}

var replaceTarballUrls = function (obj) {
  Object.keys(obj.versions).forEach(function (v) {
    var parsed = url.parse(obj.versions[v].dist.tarball);
    obj.versions[v].dist.tarball = url.resolve(process.env.DOMAIN || config('host'), parsed.path);
  });
};

exports.metadata = function (req, res) {
  cache.get(req.params.pkg, function (err, doc) {
    if (err) {
      request(REGISTRY + req.path, function (err, proxy, b) {
        if (err) {
          res.status(500).json(err);
        } else {
          try {
            var body = JSON.parse(b);
            // forward npmjs errors
            if (!body.error) {
              replaceTarballUrls(body);
              cache.put(body._id, body);
            }
            res.status(proxy.statusCode).set(proxy.headers).json(body);
          } catch (e) {
            res.status(500).json({
              error: e.message,
              body: b
            });
          }
        }
      });
    } else {
      req.log('getting ' + req.params.pkg + ' from cache...');
      res.status(200).json(doc);
    }
  });
};

exports.tarball = function (req, res) {
  cache.get(req.params.tar, function (err, body) {
    if (err) {
      request(REGISTRY + req.path, { encoding: null }, function (err, resp, body) {
        if (resp.statusCode === 200) {
          cache.put(req.params.tar, body);
        }
      }).pipe(res);
    } else {
      req.log('getting ' + req.params.tar + ' from cache...');
      res.send(new Buffer(body, 'binary'));
    }
  });
};
{'fraction_non_alphanumeric': 0.17682926829268292, 'fraction_numerical': 0.006362672322375398, 'mean_word_length': 2.120760959470637, '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': '3975258', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1209, 'n_words': 247}