Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

avatar-local-cache

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

avatar-local-cache

Saves a image URL for an avatar to the local file system (and optimizes the image). Read the [blog post that inspired this utility: A Featherweight Facepile](https://www.zachleat.com/web/featherweight-facepile/).

  • 2.0.6
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
16
increased by700%
Maintainers
1
Weekly downloads
 
Created
Source

Avatar Local Cache

Saves a image URL for an avatar to the local file system (and optimizes the image). Read the blog post that inspired this utility: A Featherweight Facepile.

  • Workaround for large avatar images on hosted services.
  • Image URLs won’t break in the future.
  • Only keeps the smallest images (webp if smallest and one of jpg or png)

Install

npm install avatar-local-cache

Usage

The default behavior will only keep webp if it has the smallest file size and then will pick one of jpg or png based on which one is the smallest of the two. To disable this, see the Keep all formats example below.

const AvatarLocalCache = require("avatar-local-cache");

let cache = new AvatarLocalCache();
cache.fetchUrl("https://opencollective-production.[…].jpeg", "nhoizey").then(function(files) {
    console.log( `Wrote ${files.map(entry => entry.path).join(", ")}.` );
});

The above writes three files but only keeps one: nhoizey.webp (20KB), nhoizey.jpg (22KB), and nhoizey.png (9KB).

  1. It only keeps the webp file if it is the smallest (it is not).
  2. It then picks the smaller of the jpg and the png (in this case, the png wins by 13KB).

This allows you to iterate over the object returned from the promise to create an img (if only one source remains) or a picture element (if the webp survived alongside a jpg or png).

Image Maximum Width

Images will be resized down to this width. Images smaller than this width will not be resized.

let cache = new AvatarLocalCache();
cache.width = 400;

Change formats

let cache = new AvatarLocalCache();
cache.formats = ["jpeg"];

// or
cache.formats = ["png"];

// or (order doesn’t matter)
cache.formats = ["webp", "jpeg"];

Keep all formats

To disable this file size comparison and file pruning, just set onlyKeepSmallestFormats to false.

let cache = new AvatarLocalCache();
cache.onlyKeepSmallestFormats = false;

Skip Metadata

Added in 2.0.6

Faster. Won’t return size or sharp metadata.

let cache = new AvatarLocalCache();
cache.skipMetadata = true;

Keywords

FAQs

Package last updated on 13 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc