Created
February 20, 2026 12:52
-
-
Save Nooshu/0dd55a4ba67da0a6d0053dc0e2884ba8 to your computer and use it in GitHub Desktop.
The core compression code I use on my blog for compressing HTML (and other text assets) to Brotli level 11.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { brotliCompressSync } from 'zlib'; | |
| /** Default Brotli compression level (0–11). Level 11 gives best ratio, slowest. */ | |
| export const BROTLI_LEVEL = 11; | |
| /** | |
| * Compress a buffer with Brotli. | |
| * @param {Buffer | Uint8Array | string} input - Input buffer, TypedArray, or string (UTF-8) | |
| * @param {number} [level=BROTLI_LEVEL] - Brotli level 0–11 | |
| * @returns {Buffer} Compressed buffer | |
| */ | |
| export function brotliCompress(input, level = BROTLI_LEVEL) { | |
| const buffer = Buffer.isBuffer(input) ? input : Buffer.from(input); | |
| return brotliCompressSync(buffer, { level }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment