{"size": 2139, "ext": "js", "max_stars_repo_path": "modules/image-proxy/lib/services/weserv.js", "max_stars_repo_name": "pixelastic/norska", "max_stars_repo_stars_event_min_datetime": "2019-10-09T12:57:39.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-27T08:44:37.000Z", "max_issues_repo_path": "modules/image-proxy/lib/services/weserv.js", "max_issues_repo_name": "pixelastic/norska", "max_issues_repo_issues_event_min_datetime": "2019-11-29T14:22:12.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-05T02:00:47.000Z", "max_forks_repo_path": "modules/image-proxy/lib/services/weserv.js", "max_forks_repo_name": "pixelastic/norska", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 6, "max_issues_count": 125, "max_forks_count": null, "avg_line_length": 28.9054054054, "max_line_length": 78, "alphanum_fraction": 0.639083684}
/**
 * Pass an image url through the weserv.nl image proxy
 * @param {string} url Image url
 * @param {object} userOptions Options to transform the image
 * - width {number}
 *   Resize image to this width
 *   Can be either a number of pixels, or a ratio (number between 0 and 1)
 * - height {number}
 *   Resize image to this height
 *   Can be either a number of pixels, or a ratio (number between 0 and 1)
 * - quality {number}
 *   Reduce quality of the image, for smaller size
 *   Should be a number between 1 (lowest quality) and 100 (highest quality)
 * - blur {number}
 *   Apply a blur effect on the image
 *   Should be a number between 1 and 2000
 * - grayscale {boolean}
 *   Pass the image in shades of gray
 *
 *  @returns {string} Full url with transforms applied
 **/
module.exports = function (url, userOptions = {}) {
  const baseUrl = 'https://images.weserv.nl';
  // Fail fast and do nothing if url is already through an image proxy
  const isAlreadyProxyfied = url.startsWith(baseUrl);
  if (isAlreadyProxyfied) {
    return url;
  }

  // Throw an error if not a remote url
  const isRemote = url.startsWith('http');
  if (!isRemote) {
    const error = new Error(`URL ${url} is not valid`);
    error.code = 'IMAGE_PROXY_NOT_URL';
    throw error;
  }
  const originUrl = encodeURIComponent(url);

  const options = {
    compressPng: true,
    progressive: true,
    ...userOptions,
  };

  const availableOptions = {
    blur: 'blur',
    compressPng: 'af',
    grayscale: 'filt=greyscale',
    height: 'h',
    progressive: 'il',
    quality: 'q',
    width: 'w',
  };

  const parsedOptions = Object.keys(options)
    .map((key) => {
      const prefix = availableOptions[key];
      if (!prefix) {
        return false;
      }
      const value = options[key];
      // If the value is "true", we simply add the prefix
      if (value === true) {
        return prefix;
      }
      return `${prefix}=${value}`;
    })
    .filter(Boolean);
  parsedOptions.sort();
  const optionsAsString = parsedOptions.length ? parsedOptions.join('&') : '';

  return `${baseUrl}?url=${originUrl}&${optionsAsString}`;
};
