<commit_before>var util = require("./util");
var path = require("path");
var api  = require("./node");
var fs   = require("fs");
var _    = require("lodash");

module.exports = function (opts) {
  opts = _.defaults(opts || {}, {
    src: "assets",
    dest: "cache"
  });

  var cache = {};

  return function (req, res, next) {
    var url = req.url;
    if (!util.canCompile(url)) return next();

    var dest = path.join(opts.dest, url);
    var src  = path.join(opts.src, url);

    var write = function (transformed) {
      fs.writeFile(dest, transformed, function (err) {
        if (err) {
          next(err);
        } else {
          cache[url] = Date.now();
          next();
        }
      });
    };

    var compile = function () {
      var transformOpts = _.clone(opts.transform || {});

      api.transformFile(opts.dest, transformOpts, function (err, transformed) {
        if (err) return next(err);
        write(transformed);
      });
    };

    var destExists = function () {
      fs.stat(dest, function (err, stat) {
        if (err) return next(err);

        if (cache[url] < +stat.mtime) {
          compile();<commit_msg>change transform to options in middleware<commit_after>var util = require("./util");
var path = require("path");
var api  = require("./node");
var fs   = require("fs");
var _    = require("lodash");

module.exports = function (opts) {
  opts = _.defaults(opts || {}, {
    options: {},
    dest:    "cache",
    src:     "assets"
  });

  var cache = {};

  return function (req, res, next) {
    var url = req.url;
    if (!util.canCompile(url)) return next();

    var dest = path.join(opts.dest, url);
    var src  = path.join(opts.src, url);

    var write = function (transformed) {
      fs.writeFile(dest, transformed, function (err) {
        if (err) {
          next(err);
        } else {
          cache[url] = Date.now();
          next();
        }
      });
    };

    var compile = function () {
      var transformOpts = _.clone(opts.options);

      api.transformFile(opts.dest, transformOpts, function (err, transformed) {
        if (err) return next(err);
        write(transformed);
      });
    };

    var destExists = function () {
      fs.stat(dest, function (err, stat) {
        if (err) return next(err);

        if (cache[url] < +stat.mtime) {
          compile();
{'fraction_non_alphanumeric': 0.1649659863945578, 'fraction_numerical': 0.0, 'mean_word_length': 2.2455172413793103, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6474176', 'n_tokens_mistral': 744, 'n_tokens_neox': 723, 'n_words': 187}