code wiki / _hdl_build / nx_cms_image.nx

nx_cms_image.nx source

↩ module page · 56 lines · 2599 B

1// nx_cms_image.nx -- CMS IMAGE OPTIMIZATION + CDN POLICY (sovereign). The Smush/Jetpack-CDN class, made 2// Nishi-native: the pixel codec/resize is the only last-mile boundary; the OPTIMIZATION POLICY engine is 3// sovereign and deterministic. It generates a responsive srcset from a fixed breakpoint ladder that NEVER 4// upscales past the original, negotiates the best modern format the client accepts (AVIF > WebP > JPEG), 5// and addresses every asset by a CONTENT HASH (via canonical nx_fnv) so the URL is immutable and changing 6// the image AUTOMATICALLY busts the cache -- no manual purge, no stale-image bug (the exceed angle over a 7// path-based cloud CDN). Images stay ON-BOX (privacy-native). Ladder = data-driven seam (rule 11). 8// license_tier: ORIGINAL 9import "nx_fnv.nx" 10import "nx_syscalls.nx" 11const NX_MAGIC_1280: i64 = 1280 12const NX_MAGIC_1920: i64 = 1920 13 14// image formats, ordered by preference (higher = more modern/preferred) 15const NX_IMG_JPEG: i64 = 0 16const NX_IMG_WEBP: i64 = 1 17const NX_IMG_AVIF: i64 = 2 18 19// responsive breakpoint ladder (px). out[] receives every ladder width <= orig_w (no upscaling); 20// returns the count written. Widths are emitted in ascending order. 21func img_srcset_widths(orig_w: i64, out: *i64) -> i64 { 22 let ladder: *i64 = sys_mmap(64) as *i64 23 ladder[0] = 320; ladder[1] = 640; ladder[2] = 960; ladder[3] = NX_MAGIC_1280; ladder[4] = NX_MAGIC_1920 24 var c: i64 = 0 25 var i: i64 = 0 26 while i < 5 { 27 if ladder[i] <= orig_w { out[c] = ladder[i]; c = c + 1 } 28 i = i + 1 29 } 30 return c 31} 32 33// negotiate the best format the client accepts: AVIF if offered, else WebP, else JPEG (modern-first). 34func img_pick_format(accepts_avif: i64, accepts_webp: i64) -> i64 { 35 if accepts_avif == 1 { return NX_IMG_AVIF } 36 if accepts_webp == 1 { return NX_IMG_WEBP } 37 return NX_IMG_JPEG 38} 39 40// content-addressed cache key: canonical FNV-1a over the asset bytes, sign-folded. Same bytes -> same key 41// (cacheable forever); any change -> a different key (automatic cache-busting, no purge). 42func img_cache_key(content: *u8, n: i64) -> i64 { 43 return fnv1a(content, n) & 0x7fffffffffffffff 44} 45 46// is it OK to serve target_w without upscaling? Only when target_w <= orig_w (never enlarge = no quality loss). 47func img_scale_ok(orig_w: i64, target_w: i64) -> i64 { 48 if target_w <= orig_w { return 1 } 49 return 0 50} 51 52// may we mark the response cache-control: immutable? Only a content-addressed asset is safe to pin forever. 53func img_immutable(content_addressed: i64) -> i64 { 54 if content_addressed == 1 { return 1 } 55 return 0 56}