Brotli
Brotli compression of files and text: smaller than gzip for HTML, CSS and JavaScript served pre-compressed.
Version 1.0.0 · 4 functions · 2 crates
Functions
What use brotli.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use brotli_file, unbrotli_file from brotli (Quillo 0.6).
brotli_file(from: str, to: str, quality: int) → intor an errorCompresses the file at from into to (often from + ".br"), at quality 0 (fastest) to 11 (smallest, for files made once and served often); the size written, in bytes.
unbrotli_file(from: str, to: str) → intor an errorUnpacks the brotli file at from into to: the size written, in bytes. Stops with an error past 16 GB.
brotli_text(text: str, quality: int) → stror an errortext compressed with brotli, as base64 (a value cannot hold bytes), at quality 0 to 11.
unbrotli_text(b64: str) → stror an errorBase64 of brotli-compressed text, back to the text; up to 64 MB of it.
Get it
$ quillo install brotli # brotli.qwer into this folder$ quillo install brotli --example # and its example, brotli.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/brotli.qwer$ curl -O https://quillolang.org/plugins/files/brotli.qweThen, in your program, all of it or only what you call:
use brotli.qweruse brotli_file, unbrotli_file from brotli // Quillo 0.6: only theseIt uses these crates, which cargo fetches the first time the plugin is built:
brotli = { version = "8", default-features = false, features = ["std"] }base64 = "0.22"Example
// A style sheet compressed with brotli for a CDN, and text both ways.//// quillo run brotli.qwe then open http://localhost:8080/packuse "brotli.qwer"route GET /pack { let rules = for i in range(400) => `.card-${i} { padding: ${i % 16}px; color: #333; border-radius: 4px; }` write_file("data/site.css", join(rules, "\n")) let size = brotli_file("data/site.css", "data/site.css.br", 11)? let packed = brotli_text("hello, hello, hello", 11)? { css_bytes: len(read_file("data/site.css")), br_bytes: size, unpacked_bytes: unbrotli_file("data/site.css.br", "data/site.copy.css")?, text: packed, back: unbrotli_text(packed)?, }}$ quillo run brotli.qwe$ curl 'localhost:8080/pack'Source
//! Brotli compression of files and text: smaller than gzip for HTML, CSS and JavaScript, for assets served pre-compressed.//! version: 1.0.0//!//! [dependencies]//! brotli = { version = "8", default-features = false, features = ["std"] }//! base64 = "0.22"use base64::engine::general_purpose::STANDARD;use base64::Engine;use std::io::{Read, Write};const MAX_FILE: u64 = 16 * 1024 * 1024 * 1024;const MAX_TEXT: u64 = 64 * 1024 * 1024;fn clamp_quality(q: i64) -> u32 { q.clamp(0, 11) as u32}fn create(to: &str) -> Result<std::fs::File, String> { if let Some(dir) = std::path::Path::new(to).parent().filter(|d| !d.as_os_str().is_empty()) { std::fs::create_dir_all(dir).map_err(|e| format!("{}: {}", dir.display(), e))?; } std::fs::File::create(to).map_err(|e| format!("{}: {}", to, e))}/// Compresses the file at `from` into `to` (often `from` + ".br"), at `quality` 0 (fastest) to 11 (smallest, for files made once and served often); the size written, in bytes.pub fn brotli_file(from: &str, to: &str, quality: i64) -> Result<i64, String> { let mut input = std::fs::File::open(from).map_err(|e| format!("{}: {}", from, e))?; { let out = create(to)?; let mut w = brotli::CompressorWriter::new(out, 64 * 1024, clamp_quality(quality), 22); std::io::copy(&mut input, &mut w).map_err(|e| e.to_string())?; w.flush().map_err(|e| e.to_string())?; } Ok(std::fs::metadata(to).map_err(|e| e.to_string())?.len() as i64)}/// Unpacks the brotli file at `from` into `to`: the size written, in bytes. Stops with an error past 16 GB.pub fn unbrotli_file(from: &str, to: &str) -> Result<i64, String> { let input = std::fs::File::open(from).map_err(|e| format!("{}: {}", from, e))?; let mut out = create(to)?; let n = std::io::copy(&mut brotli::Decompressor::new(input, 64 * 1024).take(MAX_FILE + 1), &mut out).map_err(|e| format!("{}: {}", from, e))?; if n > MAX_FILE { return Err(format!("{}: more than 16 GB unpacked", from)); } Ok(n as i64)}/// `text` compressed with brotli, as base64 (a value cannot hold bytes), at `quality` 0 to 11.pub fn brotli_text(text: &str, quality: i64) -> Result<String, String> { let mut out = Vec::new(); { let mut w = brotli::CompressorWriter::new(&mut out, 4096, clamp_quality(quality), 22); w.write_all(text.as_bytes()).map_err(|e| e.to_string())?; } Ok(STANDARD.encode(out))}/// Base64 of brotli-compressed text, back to the text; up to 64 MB of it.pub fn unbrotli_text(b64: &str) -> Result<String, String> { let bytes = STANDARD.decode(b64.trim()).map_err(|e| e.to_string())?; let mut out = Vec::new(); brotli::Decompressor::new(&bytes[..], 4096).take(MAX_TEXT + 1).read_to_end(&mut out).map_err(|e| e.to_string())?; if out.len() as u64 > MAX_TEXT { return Err("more than 64 MB of text".into()); } String::from_utf8(out).map_err(|_| "the data is not UTF-8 text".to_string())}Related
sevenz.qwer · 1.0.07-Zip archives listed, one file read, opened safely into a folder, and made from a folder.7 views · 8 downloadsbzip2 bzip2.qwer · 1.0.0bzip2 compression of files and text in pure Rust, .bz2 dumps unpacked, and their first lines read without unpacking them all.6 views · 7 downloadsFont subsets font_subset.qwer · 1.0.0TrueType fonts cut down to the characters a page or a PDF uses, as .ttf, as .woff for the web, or as a data URL for CSS, with the characters the font lacks.6 views · 7 downloadsLZ4 lz4.qwer · 1.0.0LZ4 compression of files and text in the standard .lz4 frame format: the fastest to pack and unpack, for caches, logs and data passed between services.6 views · 7 downloadsTar tar.qwer · 1.0.0Tar archives, gzipped or not: made from files and folders, listed, one file read, and opened safely into a folder.7 views · 8 downloadsxz xz.qwer · 1.0.0xz (LZMA2) compression of files and text, and old .lzma files read: the smallest archives, for backups and downloads.6 views · 8 downloadsThread 0 posts
On Brotli: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.