npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hastily

v0.5.0

Published

express middleware to simulate fastly cdn

Downloads

13,535

Readme

hastily

npm downloads CircleCI codecov snyk peer dependency npm downloads

Drop-in compatible Express middleware to replicate the Fastly Image Optimization API

Usage

The middleware works a lot like compression, a standard Express middleware which transparently compresses any text-based response body that emits from something in the middleware chain.

So hastily works as an add-on to an Express server or middleware that is already serving images.

import express, { static } from 'express';
import { imageopto } from 'hastily';

const app = express();
app.use('/images', imageopto(), static('/www/images'));

app.listen(8000);

You now have an app which can serve any image from /www/images, and optimize it with URL parameters from the Fastly Image Optimization API.

Behavior

Request Filtering

Hastily is meant to be registered as a middleware within an app that may be handling non-image requests as well as image requests. By default, it will only attempt to transform responses that appear to be uncompressed images. Hastily verifies this in several steps.

  1. By URL: The default filter function for the imageopto middleware is hastily.hasSupportedExtension(req: Request) It. checks a request's URL path for an extension that indicates a Sharp-supported file type. It reads the supported extensions from Sharp itself. If a file does not have an image-file extension, it will do nothing.

    Supply an alternative function as the filter property of imageopto options to override this behavior.

    import { imageopto, hasSupportedExtension } from 'hastily';
    // Don't require an extension. Require a certain base directory and query param.
    imageopto({
      filter: (req) => req.path.startsWith('/media/') && 'optimize' in req.query,
    });
    // Require an extension, OR an 'imageServer' path.
    hastily.imageopto({
      filter: (req) => hasSupportedExtension(req) || req.path === '/imageServer',
    });
  2. By method and status: Hastily will not transform any response unless the request was a GET and the response code is in the 2xx range.

  3. By Cache-control: If the response has a Cache-Control header containing no-transform, Hastily will respect that.

  4. By dedupe: Both Hastily and Fastly (which Hastily emulates) set telltale headers on responses after processing them. If a fastly-io-info header is present, OR an X-Optimized: hastily header is present, Hastily will assume a transform has already occurred and won't attempt another. This behavior can be overridden by options.force, but you shouldn't do that.

  5. By content type: Hastily will try to get the Content-Type header of the response. If it cannot detect a content type, OR if Sharp does not support the content type, Hastily will not attempt to transform.

  6. By content encoding: Hastily will not attempt to decompress gzipped responses. Images shouldn't be gzipped (or deflated, or brotli'd, or what have you) and it's a mistake to configure a server to do so by default. The available gzip algorithms are designed for text data, and they don't compress binary data hardly at all. If your server is gzipping images before Hastily handles them, fix this configuration for the highest speeds.

Debugging

The Hastily middleware logs debugs, warnings and errors to standard error, which is what web server stacks expect. When NODE_ENV is production, only warnings will be logged, as parseable JSON lines.

When NODE_ENV is not production, Hastily respects the Node convention of path syntax in the DEBUG environment variable to determine log level. DEBUG=hastily:* will show all debug data in a pretty format. You can limit logging to subject areas by using paths like DEBUG=hastily:request,hastily:params,hastily:splice instead.

Full API doc at zetlen.github.io/hastily

TODO

  • [x] implement resize and crop mappers
  • [x] throw on unsupported
  • [x] implement enable and disable for upscaling in resize
  • [x] implement format, auto=webp, and quality params in post-manip phase
  • [x] add unit tests
  • [ ] add image-diff automated testing
  • [ ] implement sharpen, mapping [amt, radius, threshold] to libvips sharpen params
  • [ ] implement brightness, contrast, saturation by figuring out percentage to multiplier mapping
  • [ ] use image.metadata() to implement relative and context-based methods
  • [ ] add header-based methods
    • [ ] montage
    • [ ] overlay