Image Pipeline

Last updated


This page walks through every step GET /c/img/{path} takes from request to response, and what happens when a step fails.

Steps


StepBehaviorImplementation
1. Parse parametersReads s/size, w/width, h/height, q/quality, o/origin, d/dark, t/type; the short name winsGetFromPath.ts
2. Map the originalReplaces /c/img/ with storage/image/upload/ and drops the query stringSame
3. PDF / SVGFor .pdf / .svg, streams the original and stopsstreamFile()
4. Normalize formattype outside jpeg/jpg/png/avif/webp becomes webp; jpeg becomes jpgSame
5. Build cache pathBuilds the cache filename from parameters; see Caching LayersSame
6. Original modeWith o=1, streams the original and stopsstreamFile()
7. Cache hitReads the cache file with readFileSync and returns it on success; Content-Type follows the original's extension (.jpg/.jpeg → image/jpeg, .png → image/png, otherwise image/webp), not the requested tSame
8. Convertsharp reads the original and its metadata, calls resize() per the Sizing Rules, then encodessharp
9. Respond and writeSends the response first, then writes the cache file with writeFileSyncSame

Encoding


Formatsharp callDefault qualityContent-Type
webpimage.webp({ quality })75image/webp
avifimage.avif({ quality })50image/avif
jpgimage.jpeg({ quality })75image/jpeg
pngimage.png()Not usedimage/png

Quality is parseInt-ed and clamped to 0–100.

Streaming Originals


.pdf, .svg, and o=1 stream through createReadStream (8 KB highWaterMark) with Transfer-Encoding: chunked. For o=1, Content-Type follows the extension: .jpg/.jpeg → image/jpeg, .png → image/png, .webp → image/webp, otherwise application/octet-stream. A read error returns an empty 404.

Failure Handling


CaseResponse
Original missingstorage/static/404-light.svg (404-dark.svg with d=1), HTTP status 200
sharp cannot decode or a parameter is invalid (e.g. w=abc)Same
Response headersContent-Type: image/svg+xml, Cache-Control: no-cache, Expires: -1, Pragma: no-cache


中文