#jpeg #cloudflare #optimization #http-2

bin+lib cloudflare-soos

Helper tool for Cloudflare’s enhanced HTTP/2 prioritization, which makes progressive images load faster. Supports JPEG, GIF, and PNG.

3 stable releases

2.1.0 Oct 7, 2021
2.0.0 Jan 28, 2021
1.18.0 Mar 20, 2020

#2 in #cloudflare

Download history 59/week @ 2021-07-04 76/week @ 2021-07-11 26/week @ 2021-07-18 38/week @ 2021-07-25 77/week @ 2021-08-01 81/week @ 2021-08-08 162/week @ 2021-08-15 103/week @ 2021-08-22 96/week @ 2021-08-29 128/week @ 2021-09-05 55/week @ 2021-09-12 40/week @ 2021-09-19 36/week @ 2021-09-26 70/week @ 2021-10-03 7/week @ 2021-10-10 19/week @ 2021-10-17

320 downloads per month
Used in mozjpeg-sys

MIT/Apache

78KB
1.5K SLoC

Scan Offsets of Scans

Progressive image parser for HTTP/2 parallel progressive streaming feature.

Computes optimal offsets for the cf-priority-change header to render images progressively with a "preview" and "good enough" stages. In JPEG it tries to render DC channels only (libjpeg-turbo has a special support for nicely blurring the DC-only stage) and then prioritize sending enough data for the image look fully loaded.

In case of GIF animations it prioritizes sending the first frame only. In interlaced PNG it roughly guesses progressive scans. For non-progressive images it tries to send image dimensions before any other data to aid page layout and (improves Cumulative Layout Shift (CLS) metric).

Building and usage

Install Rust 1.52 or later, and run:

cargo install cloudflare-soos
soos-cf-priority image.jpg

This will print to stdout Cloudflare's cf-priority (first line) and cf-priority-change (second line) headers with breakpoints for the image.jpg file, an error to stderr. The image is read from stdin if not path is specified. This tool requires progressive JPEG files, and doesn't do anything for baseline JPEG or other formats.

The same can be done programmatically by using this crate as a library and calling soos::Scans::from_file(jpeg_bytes)?.cf_priority_change_headers()?.

You can also build for WASM with rustup target add wasm32-unknown-unknown; cargo build --release --target=wasm32-unknown-unknown or use wasm-pack.

Example tools

cargo run --example cf-priority -- image.jpg
cargo run --example scan -- image.jpg

These tools show debug information used to generate the HTTP priority headers.

Credits

JPEG parsing is based on jpeg-decoder by Ulf Nilsson. GIF parsing is from Piston GIF crate.

Dependencies

~0–285KB