Documentation
¶
Overview ¶
Package imageconv converts cached JPEG and PNG responses to WebP and AVIF (ADR-0043). The edge layer sends a request for a variant to the agent's image socket with X-Edgeweir-Image; Server passes it on to the origin layer it names and converts the answer in a child process (edgeweir-node imgconv), or answers with the original when it cannot.
Index ¶
- Constants
- Variables
- func Convert(data []byte, kind Kind, format Format, quality int, info Info, maxOutput int64) ([]byte, error)
- func DefaultWorkers() int
- func Estimate(format Format, pixels int64) int64
- func Orient(img image.Image, orientation int) image.Image
- func RunChild(args []string, stdin io.Reader, stdout, stderr io.Writer) int
- type Config
- type Format
- type Info
- type Kind
- type Params
- type Server
Constants ¶
const ( DefaultMemory int64 = 1 << 30 DefaultTimeout = 8 * time.Second // TransientTTL caps how long an original answered for want of a // conversion slot stays cached under the variant's key. TransientTTL = 60 )
Defaults of the node's converter flags (ADR-0043).
const ChildCommand = "imgconv"
ChildCommand is the hidden subcommand that converts one image.
const Header = "X-Edgeweir-Image"
Header is the request header the edge layer sets on variant requests (lua/edgeweir/imageconv.lua).
const SavedHeader = "X-Edgeweir-Image-Saved"
SavedHeader carries the bytes a variant saved against its original on converted responses; it is cached with them and hidden from clients (the edge layer counts it, MinuteStats.image_bytes_saved).
Variables ¶
var ErrUnsupported = errors.New("image not convertible")
ErrUnsupported marks images that are never converted: the original is served (and cached) as it is.
Functions ¶
func Convert ¶
func Convert(data []byte, kind Kind, format Format, quality int, info Info, maxOutput int64) ([]byte, error)
Convert decodes a JPEG or PNG image, turns it upright by its EXIF orientation and encodes it as format at quality. The output carries no metadata. Images that do not decode, whose size differs from what inspection read, or whose output would exceed maxOutput bytes are ErrUnsupported.
func Estimate ¶
Estimate returns the memory a conversion of pixels to format is assumed to need (measured peaks plus margin, ADR-0043).
func Orient ¶
Orient returns img turned upright for an EXIF orientation (2-8: mirror and / or rotate; anything else returns img unchanged).
func RunChild ¶
RunChild is the child process: it limits itself (address space for memory plus headroom, no files, a few descriptors, no capabilities, no_new_privs), reads the image from stdin and writes the converted image to stdout. Exit status 3 means the image is not convertible, which includes an output over --max-output bytes (nothing is written then).
Types ¶
type Config ¶
type Config struct {
// Upstreams maps the origin layers' upstream names to their sockets.
Upstreams map[string]string
// Workers bounds concurrent conversions, Memory the sum of their
// estimates; Timeout bounds one conversion (the child is killed).
Workers int
Memory int64
Timeout time.Duration
// Command returns the child process for args (the agent's own binary
// with ChildCommand) bound to ctx (exec.CommandContext: cancelling ctx
// kills it); tests run their own.
Command func(ctx context.Context, args []string) *exec.Cmd
Log *slog.Logger
}
Config configures a Server.
type Info ¶
type Info struct {
Width, Height int
// Orientation is the EXIF orientation (1-8; 1 when absent): the child
// turns the pixels so that the variant, which carries no EXIF, looks
// like the original does in browsers.
Orientation int
}
Info is what inspection learns from an image's headers.
func Inspect ¶
Inspect reads the headers of a JPEG or PNG image without decoding it and rejects (ErrUnsupported) what a conversion could not reproduce: other color spaces than sRGB (an ICC profile that is not sRGB, CMYK, a PNG gamma other than 1/2.2), animation (APNG), and JPEG flavours Go's decoder lacks (arithmetic coding, other precisions than 8 bits).
type Params ¶
type Params struct {
Format Format
Quality int
MinSize int64
MaxSize int64
MaxPixels int64
JPEG, PNG bool
// Upstream is the origin layer the request would have gone to
// ($edgeweir_origin_layer).
Upstream string
}
Params are a variant request's parameters.
func ParseHeader ¶
ParseHeader parses "<webp|avif>;q=<1-100>;min=<n>;max=<n>;px=<n>; src=<jpeg|png|jpeg,png>;up=<upstream>" (every field required, in any order after the format).