<commit_before>// Native imports
var fs = require('fs');
var path = require('path');
var url = require('url');

// Vendor imports
var cssesc = require('cssesc');
var mime = require('mime');
var sizeOf = require('image-size');
var SVGEncoder = require('directory-encoder/lib/svg-uri-encoder.js');

// Local imports
var mapFunctions = require('./lib/map-functions');
var unescapeCss = require('./lib/unescape-css');

var Assets = function (options) {
  var self = this;
  this.options = options || {};
  this.options.baseUrl = this.options.baseUrl || '/';
  if (this.options.basePath) {
    this.options.basePath = path.resolve(this.options.basePath);
  } else {
    this.options.basePath = process.cwd();
  }
  if (this.options.loadPaths) {
    this.options.loadPaths = this.options.loadPaths.map(function (loadPath) {
      return path.resolve(self.options.basePath, loadPath);
    });
  } else {
    this.options.loadPaths = [];
  }
  this.options.loadPaths.unshift(this.options.basePath);
  if (this.options.relativeTo) {
    this.options.relativeTo = path.resolve(this.options.relativeTo);
  } else {
    this.options.relativeTo = false;
  }
  if (this.options.cachebuster === true) {
    this.options.cachebuster = function (path) {
      var mtime = fs.statSync(path).mtime;
      return mtime.getTime().toString(16);
    };
  }
  this.postcss = this.postcss.bind(this);
};

Assets.prototype.getImageSize = function (assetStr, density) {
  var assetPath, err, size;
  assetPath = this.resolvePath(assetStr.value);
  try {
    size = sizeOf(assetPath);
    if (density !== undefined) {
      density = parseFloat(density.value, 10);
      size.width  = Number((size.width  / density).toFixed(4));
      size.height = Number((size.height / density).toFixed(4));
    }
    return size;
  } catch (exception) {
    err = new Error('Image corrupted: ' + assetPath);
    err.name = 'ECORRUPT';
    throw err;
  }
};

Assets.prototype.matchPath = function (assetPath) {
  var exception, isFound, loadPaths, matchingPath;
  if (typeof this.inputPath === 'string') {
    loadPaths = [path.dirname(this.inputPath)].concat(this.options.loadPaths);
  } else {
    loadPaths = this.options.loadPaths;
  }
  isFound = loadPaths.some(function (loadPath) {
    matchingPath = path.join(loadPath, assetPath);
    return fs.existsSync(matchingPath);
  });
  if (!isFound) {
    exception = new Error('Asset not found or unreadable: ' + assetPath);<commit_msg>Leave `sizeOf` method only inside the `try` section<commit_after>// Native imports
var fs = require('fs');
var path = require('path');
var url = require('url');

// Vendor imports
var cssesc = require('cssesc');
var mime = require('mime');
var sizeOf = require('image-size');
var SVGEncoder = require('directory-encoder/lib/svg-uri-encoder.js');

// Local imports
var mapFunctions = require('./lib/map-functions');
var unescapeCss = require('./lib/unescape-css');

var Assets = function (options) {
  var self = this;
  this.options = options || {};
  this.options.baseUrl = this.options.baseUrl || '/';
  if (this.options.basePath) {
    this.options.basePath = path.resolve(this.options.basePath);
  } else {
    this.options.basePath = process.cwd();
  }
  if (this.options.loadPaths) {
    this.options.loadPaths = this.options.loadPaths.map(function (loadPath) {
      return path.resolve(self.options.basePath, loadPath);
    });
  } else {
    this.options.loadPaths = [];
  }
  this.options.loadPaths.unshift(this.options.basePath);
  if (this.options.relativeTo) {
    this.options.relativeTo = path.resolve(this.options.relativeTo);
  } else {
    this.options.relativeTo = false;
  }
  if (this.options.cachebuster === true) {
    this.options.cachebuster = function (path) {
      var mtime = fs.statSync(path).mtime;
      return mtime.getTime().toString(16);
    };
  }
  this.postcss = this.postcss.bind(this);
};

Assets.prototype.getImageSize = function (assetStr, density) {
  var assetPath, err, size;
  assetPath = this.resolvePath(assetStr.value);
  try {
    size = sizeOf(assetPath);
  } catch (exception) {
    err = new Error('Image corrupted: ' + assetPath);
    err.name = 'ECORRUPT';
    throw err;
  }
  if (density !== undefined) {
    density = parseFloat(density.value, 10);
    size.width  = Number((size.width  / density).toFixed(4));
    size.height = Number((size.height / density).toFixed(4));
  }
  return size;
};

Assets.prototype.matchPath = function (assetPath) {
  var exception, isFound, loadPaths, matchingPath;
  if (typeof this.inputPath === 'string') {
    loadPaths = [path.dirname(this.inputPath)].concat(this.options.loadPaths);
  } else {
    loadPaths = this.options.loadPaths;
  }
  isFound = loadPaths.some(function (loadPath) {
    matchingPath = path.join(loadPath, assetPath);
    return fs.existsSync(matchingPath);
  });
  if (!isFound) {
    exception = new Error('Asset not found or unreadable: ' + assetPath);
{'fraction_non_alphanumeric': 0.15453436356242375, 'fraction_numerical': 0.0024400162667751117, 'mean_word_length': 3.684761904761905, 'pattern_counts': {'":': 0, '<': 3, '<?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': '269684', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1584, 'n_words': 318}