<commit_before>const path = require('path');
const npm = require('npm');
const fs = require('fs');
const util = require('util');

exports.expressCreateServer = function (hook_name, args, cb) {
  args.app.get('/tests/frontend/specs_list.js', async (req, res) => {
    const [coreTests, pluginTests] = await Promise.all([
      exports.getCoreTests(),
      exports.getPluginTests(),
    ]);

    // merge the two sets of results
    let files = [].concat(coreTests, pluginTests).sort();

    // Keep only *.js files
    files = files.filter((f) => f.endsWith('.js'));

    console.debug('Sent browser the following test specs:', files);
    res.setHeader('content-type', 'text/javascript');
    res.end(`var specs_list = ${JSON.stringify(files)};\n`);
  });

  // path.join seems to normalize by default, but we'll just be explicit
  const rootTestFolder = path.normalize(path.join(npm.root, '../tests/frontend/'));

  const url2FilePath = function (url) {
    let subPath = url.substr('/tests/frontend'.length);
    if (subPath == '') {
      subPath = 'index.html';
    }
    subPath = subPath.split('?')[0];

    let filePath = path.normalize(path.join(rootTestFolder, subPath));

    // make sure we jail the paths to the test folder, otherwise serve index
    if (filePath.indexOf(rootTestFolder) !== 0) {
      filePath = path.join(rootTestFolder, 'index.html');
    }
    return filePath;
  };

  args.app.get('/tests/frontend/specs/*', (req, res) => {
    const specFilePath = url2FilePath(req.url);
    const specFileName = path.basename(specFilePath);

    fs.readFile(specFilePath, (err, content) => {
      if (err) { return res.send(500); }

      content = `describe(${JSON.stringify(specFileName)}, function(){   ${content}   });`;

      res.send(content);
    });
  });

  args.app.get('/tests/frontend/*', (req, res) => {
    const filePath = url2FilePath(req.url);
    res.sendFile(filePath);<commit_msg>send the test files with the correct content-type header<commit_after>const path = require('path');
const npm = require('npm');
const fs = require('fs');
const util = require('util');

exports.expressCreateServer = function (hook_name, args, cb) {
  args.app.get('/tests/frontend/specs_list.js', async (req, res) => {
    const [coreTests, pluginTests] = await Promise.all([
      exports.getCoreTests(),
      exports.getPluginTests(),
    ]);

    // merge the two sets of results
    let files = [].concat(coreTests, pluginTests).sort();

    // Keep only *.js files
    files = files.filter((f) => f.endsWith('.js'));

    console.debug('Sent browser the following test specs:', files);
    res.setHeader('content-type', 'application/javascript');
    res.end(`var specs_list = ${JSON.stringify(files)};\n`);
  });

  // path.join seems to normalize by default, but we'll just be explicit
  const rootTestFolder = path.normalize(path.join(npm.root, '../tests/frontend/'));

  const url2FilePath = function (url) {
    let subPath = url.substr('/tests/frontend'.length);
    if (subPath == '') {
      subPath = 'index.html';
    }
    subPath = subPath.split('?')[0];

    let filePath = path.normalize(path.join(rootTestFolder, subPath));

    // make sure we jail the paths to the test folder, otherwise serve index
    if (filePath.indexOf(rootTestFolder) !== 0) {
      filePath = path.join(rootTestFolder, 'index.html');
    }
    return filePath;
  };

  args.app.get('/tests/frontend/specs/*', (req, res) => {
    const specFilePath = url2FilePath(req.url);
    const specFileName = path.basename(specFilePath);

    fs.readFile(specFilePath, (err, content) => {
      if (err) { return res.send(500); }

      content = `describe(${JSON.stringify(specFileName)}, function(){   ${content}   });`;

      if(!specFilePath.endsWith('index.html')) res.setHeader('content-type', 'application/javascript');

      res.send(content);
    });
  });

  args.app.get('/tests/frontend/*', (req, res) => {
    const filePath = url2FilePath(req.url);
    res.sendFile(filePath);
{'fraction_non_alphanumeric': 0.17029257314328583, 'fraction_numerical': 0.004001000250062516, 'mean_word_length': 3.519774011299435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2113243', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1228, 'n_words': 286}