Quickstart — JavaScript / TypeScript
npm install dusimagezRequires Node 18+. In the browser the same core runs as WebAssembly — try it on the Playground (your files never leave the page).
Compress
Section titled “Compress”import { readFile, writeFile } from 'node:fs/promises';import { compressImage } from 'dusimagez';
const input = await readFile('input.jpg');const output = await compressImage(input, { quality: 80 });await writeFile('output.jpg', output);Options are validated with Zod — an invalid option (for example,
quality: 0) fails before any encoding starts.
Compress to a target size
Section titled “Compress to a target size”import { compressToSize } from 'dusimagez';
// 200 KB or less, or a thrown error with code TARGET_SIZE_UNREACHABLE.const output = await compressToSize(input, 200_000, {});Report and info
Section titled “Report and info”import { compressWithReport, getImageInfo } from 'dusimagez';
const info = await getImageInfo(input);console.log(info.format, info.width, info.height);
const report = await compressWithReport(input, { preset: 'Web' });console.log( `${report.originalSize} -> ${report.outputSize} bytes, ` + `saved ${report.savedPercent.toFixed(1)}%, quality ${report.qualityUsed}`,);Errors
Section titled “Errors”Failures are thrown as JSON strings:
{"ok": false, "code": "EMPTY_INPUT", "message_en": "No input bytes were provided.", "message_hi": "कोई इनपुट बाइट्स नहीं दिए गए।"}Parse the thrown string and branch on code — see
Error codes for all 9 codes.
Options
Section titled “Options”Option names are camelCase in JavaScript (maxWidth, targetBytes,
pngCompressionLevel, stripMetadata, autoOrient, maxPixels,
maxDimension) and map 1:1 to the Rust CompressOptions fields — see the
Options reference.