<commit_before>const querystring = require('querystring');
const https = require('https');
const zlib = require('zlib');
const Agent = require('agentkeepalive').HttpsAgent;

const cache = require('./cache');

function getCurrentMemoryUsage() {
  const memoryUsage = process.memoryUsage();
  return memoryUsage.rss;
}

const keepAliveAgent = new Agent({
});

const WCL_MAINTENANCE_STRING = 'Warcraft Logs is down for maintenance';

module.exports = function (req, res) {
  // This allows users to cache bust, this is useful when live logging. It stores the result in the regular (uncachebusted) spot so that future requests for the regular request are also updated.
  let cacheBust = false;
  if (req.query._) {
    console.log('Cache busting...');
    cacheBust = true;
    delete req.query._;
  }

  // Set header already so that all request, good or bad, have it
  res.setHeader('Access-Control-Allow-Origin', '*');

  const requestUrl = `${req.params[0]}?${querystring.stringify(req.query)}`;
  const jsonString = !cacheBust && cache.get(requestUrl);
  if (jsonString) {
    console.log('++++++cache hit', requestUrl);
    res.setHeader('Content-Type', 'application/json; charset=utf-8');
    res.send(jsonString);
  } else {
    console.log('------cache miss', requestUrl);
    const query = Object.assign({}, req.query, {
      // Allow users to provide their own API key. This is required during development so that other developers don't lock out the production in case they mess something up.
      api_key: req.query.api_key || '97c84db1a100b32a6d5abb763711244e',
    });

    const options = {
      host: 'www.warcraftlogs.com',
      path: `/v1/${req.params[0]}?${querystring.stringify(query)}`,
      headers: {
        'User-Agent': 'WoWAnalyzer.com API',
        'Accept-Encoding': 'gzip', // using gzip is 80% quicker
      },
      agent: keepAliveAgent,
    };
    console.log('GET', options.path);
    const wclStart = Date.now();
    https
      .get(options, wclResponse => {
        if (wclResponse.statusCode >= 500 && wclResponse.statusCode < 600) {
          const msg = 'WCL Error (' + wclResponse.statusCode + '): ' + wclResponse.statusMessage;
          console.error(msg);
          res.status(500).send(msg);
          return;
        }

        const gunzip = zlib.createGunzip();
        wclResponse.pipe(gunzip);<commit_msg>Change API to share cache with different API keys<commit_after>const querystring = require('querystring');
const https = require('https');
const zlib = require('zlib');
const Agent = require('agentkeepalive').HttpsAgent;

const cache = require('./cache');

function getCurrentMemoryUsage() {
  const memoryUsage = process.memoryUsage();
  return memoryUsage.rss;
}

const keepAliveAgent = new Agent({
});

const WCL_MAINTENANCE_STRING = 'Warcraft Logs is down for maintenance';

module.exports = function (req, res) {
  // This allows users to cache bust, this is useful when live logging. It stores the result in the regular (uncachebusted) spot so that future requests for the regular request are also updated.
  let cacheBust = false;
  if (req.query._) {
    console.log('Cache busting...');
    cacheBust = true;
    delete req.query._;
  }
  // Allow users to provide their own API key. This is required during development so that other developers don't lock out the production in case they mess something up.
  const api_key = req.query.api_key || '97c84db1a100b32a6d5abb763711244e';
  delete req.query.api_key; // don't use a separate cache for different API keys

  // Set header already so that all request, good or bad, have it
  res.setHeader('Access-Control-Allow-Origin', '*');

  const requestUrl = `${req.params[0]}?${querystring.stringify(req.query)}`;
  const jsonString = !cacheBust && cache.get(requestUrl);
  if (jsonString) {
    console.log('cache HIT', requestUrl);
    res.setHeader('Content-Type', 'application/json; charset=utf-8');
    res.send(jsonString);
  } else {
    console.log('cache MISS', requestUrl);
    const query = Object.assign({}, req.query, { api_key });

    const options = {
      host: 'www.warcraftlogs.com',
      path: `/v1/${req.params[0]}?${querystring.stringify(query)}`,
      headers: {
        'User-Agent': 'WoWAnalyzer.com API',
        'Accept-Encoding': 'gzip', // using gzip is 80% quicker
      },
      agent: keepAliveAgent,
    };
    console.log('GET', options.path);
    const wclStart = Date.now();
    https
      .get(options, wclResponse => {
        if (wclResponse.statusCode >= 500 && wclResponse.statusCode < 600) {
          const msg = 'WCL Error (' + wclResponse.statusCode + '): ' + wclResponse.statusMessage;
          console.error(msg);
          res.status(500).send(msg);
          return;
        }

        const gunzip = zlib.createGunzip();
        wclResponse.pipe(gunzip);
{'fraction_non_alphanumeric': 0.1343840729987557, 'fraction_numerical': 0.01493156366652841, 'mean_word_length': 3.3845454545454547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3251577', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1457, 'n_words': 408}