Options
All
  • Public
  • Public/Protected
  • All
Menu

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.

Full API doc at zetlen.github.io/hastily

TODO

  • implement resize and crop mappers
  • throw on unsupported
  • implement enable and disable for upscaling in resize
  • implement format, auto=webp, and quality params in post-manip phase
  • 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

Index

Type aliases

Orienter

Orienter: function

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

Variables

Const QUALITY

QUALITY: "bg-color" | "blur" | "brightness" | "canvas" | "crop" | "contrast" | "disable" | "dpr" | "enable" | "fit" | "height" | "orient" | "pad" | "resize-filter" | "saturation" | "sharpen" | "trim" | "width" = 'quality' as Param

Const UNSET

UNSET: unique symbol = Symbol('unset')

Const cacheControlNoTransformRegExp

cacheControlNoTransformRegExp: RegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/

Const debug

debug: Debugger = makeDebug('hastily:options')

Const imageExtensionRE

imageExtensionRE: RegExp = new RegExp(`\\.(?:${[...streamableFileExtensions].join('|')})$`)

Const mappers

mappers: Array<[Param, Mapper]> = [['width', resize],['height', resize],['dpr', resize],['fit', resize],['disable', resize],['enable', resize],['resize-filter', resize],['crop', extractCrop],['canvas', resizeCanvas],['pad', extend],['orient', orient],['bg-color', bgFlatten],['blur', blur],['brightness', unsupported('brightness', 'absolute brightness adjustment')],['contrast', unsupported('contrast', 'absolute contrast adjustment')],['saturation', unsupported('saturation', 'absolute saturation adjustment')],['sharpen', unsupported('sharpen', 'unsharp mask')],['trim', unsupported('trim', 'relative trimming from all four sides')]]

Const streamableFileExtensions

streamableFileExtensions: Set<string> = new Set(Object.keys(sharp.format).filter(ext => sharp.format[ext as keyof FormatEnum].input.stream))

Functions

Private addListeners

  • addListeners(stream: IMutableResponse | IWorkStream, on: any, listeners: Listener[]): void
  • Add bufferred listeners to stream

    Parameters

    • stream: IMutableResponse | IWorkStream
    • on: any
    • listeners: Listener[]

    Returns void

chunkLength

  • chunkLength(chunk: any, encoding: any): number

Const exif

  • exif(sharp: Sharp): Sharp

Const flipH

  • flipH(sharp: Sharp): Sharp

Const flipHV

  • flipHV(sharp: Sharp): Sharp

Const flipV

  • flipV(sharp: Sharp): Sharp

Const hasSupportedExtension

  • hasSupportedExtension(req: Request<ParamsDictionary>): boolean
  • Use the sharp.format manifest to determine if the current request's file extension matches a format that sharp can stream in to optimize.

    Parameters

    • req: Request<ParamsDictionary>

    Returns boolean

imageopto

  • Returns a new imageopto middleware for use in Express app.use(). Won't do anything if the Express app isn't already serving images!

    Parameters

    Returns Middleware

Const left

  • left(sharp: Sharp): Sharp

Const right

  • right(sharp: Sharp): Sharp

Private shouldTransform

  • shouldTransform(req: Request, res: IMutableResponse, options: ImageOptoOptions, debug: DebugLogger): boolean
  • Determine if the entity should be transformed.

    Parameters

    • req: Request
    • res: IMutableResponse
    • options: ImageOptoOptions
    • debug: DebugLogger

    Returns boolean

Object literals

Const HASTILY_HEADER

HASTILY_HEADER: object

NAME

NAME: string = "X-Optimized"

VALUE

VALUE: string = "hastily"

Const formatters

formatters: object

gif

gif: function = unsupported('format' as Param, 'GIF output unsupported by node-hastily')

Type declaration

    • (transform: Sharp, params: IFastlyParams): Sharp | false
    • Parameters

      • transform: Sharp
      • params: IFastlyParams

      Returns Sharp | false

jpg

  • jpg(transform: Sharp, params: IFastlyParams): Sharp

pjpg

  • pjpg(transform: Sharp, params: IFastlyParams): Sharp

png

  • png(transform: Sharp, params: IFastlyParams): Sharp

png8

  • png8(transform: Sharp, params: IFastlyParams): Sharp

webp

  • webp(transform: Sharp, params: IFastlyParams): Sharp

webpll

  • webpll(transform: Sharp, params: IFastlyParams): Sharp

webply

  • webply(transform: Sharp, params: IFastlyParams): Sharp

Const optoFitToSharp

optoFitToSharp: object

bounds

bounds: "inside" = "inside"

cover

cover: "outside" = "outside"

crop

crop: "cover" = "cover"

Const optoResizeFilterToSharp

optoResizeFilterToSharp: object

bicubic

bicubic: "cubic" = "cubic"

bilinear

bilinear: "mitchell" = "mitchell"

cubic

cubic: "cubic" = "cubic"

lanczos

lanczos: "lanczos3" = "lanczos3"

lanczos2

lanczos2: "lanczos2" = "lanczos2"

lanczos3

lanczos3: "lanczos3" = "lanczos3"

linear

linear: "mitchell" = "mitchell"

nearest

nearest: "nearest" = "nearest"

Const orienters

orienters: object

1

1: function = exif

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

2

2: function = flipH

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

3

3: function = flipHV

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

4

4: function = flipV

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

6

6: function = right

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

8

8: function = left

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

h

h: function = flipH

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

hv

hv: function = flipHV

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

l

l: function = left

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

r

r: function = right

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

v

v: function = flipV

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

vh

vh: function = flipHV

Type declaration

    • (x: Sharp): Sharp
    • Parameters

      • x: Sharp

      Returns Sharp

5

  • 5(sharp: Sharp): Sharp

7

  • 7(sharp: Sharp): Sharp

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc