Why I moved my file tools to the browser (and what broke along the way)
Every "convert HEIC to PNG" or "compare two files" site I used had the same quiet problem: my file went to someone's server. For a holiday photo, fine. For a client contract or a production config dump, not fine. So whe
Every "convert HEIC to PNG" or "compare two files" site I used had the same quiet problem: my file went to someone's server. For a holiday photo, fine. For a client contract or a production config dump, not fine.
So when I started building deviTools, I set one rule: if the browser can do the work, the file never leaves the tab. Here's what that actually takes, and where the rule had to bend.
1. Image conversion: Canvas gets you most of the way
The naive version is almost embarrassingly short:
async function convert(file, type = 'image/png', quality = 0.92) {
const bitmap = await createImageBitmap(file);
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
canvas.getContext('2d').drawImage(bitmap, 0, 0);
return canvas.convertToBlob({ type, quality });
}
createImageBitmap + OffscreenCanvas means the decode and the encode can both happen inside a Worker, so a 40 MP image doesn't freeze the UI. convertToBlob handles PNG, JPEG and WebP in every current browser.
Three things bit me:
Alpha channel loss. Converting a transparent PNG to JPEG silently composites onto black. You have to fill the canvas yourself before drawing:
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, bitmap.width, bitmap.height);
EXIF orientation. createImageBitmap ignores it unless you ask:
await createImageBitmap(file, { imageOrientation: 'from-image' });
Without that flag, phone photos come out rotated and users assume your tool is broken.
HEIC. Safari decodes it, nothing else does. This is the first place the rule bent — I ship a WASM decoder for HEIC/HEIF rather than uploading the file, which costs a ~1 MB lazy-loaded chunk but keeps the privacy promise. I only fetch it when a .heic actually lands in the picker. (That path is live on the image converter if you want to see what the tradeoff feels like in practice.)
AVIF encoding is still the gap. Decode support is broad; convertToBlob({ type: 'image/avif' }) is not. Feature-detect instead of assuming:
const blob = await canvas.convertToBlob({ type: 'image/avif' });
const supported = blob.type === 'image/avif'; // otherwise you got a PNG
That last line matters — browsers don't throw, they just hand back a PNG with a straight face.
2. Diffing: the algorithm is the easy part
I expected the diff itself to be the hard bit. Myers diff is well documented and a line-level implementation over a few thousand lines runs in single-digit milliseconds.
The expensive part is rendering. My first version built one DOM node per line per side. At 5,000 lines that's 10,000 nodes plus intra-line highlight spans, and scrolling died.
What fixed it:
- Virtualise the viewport. Render ~60 rows, translate a spacer. Obvious in hindsight, easy to skip when your test file is 200 lines.
-
Diff on a Worker, render on the main thread. Post back a compact array of ops (
[type, aStart, aEnd, bStart, bEnd]), not formatted HTML. Structured clone of a big HTML string is its own bottleneck. - Compute intra-line character diffs lazily, only for rows currently visible. Users look at a handful of changed lines; computing word-level diffs for all 400 changed rows upfront is wasted work.
-
Normalise line endings first. Half of all "these files are different but look identical" reports are
\r\nvs\n. Detect it and say so explicitly instead of showing every line as changed.
The file comparison tool is where all of that ended up.
3. JSON: JSON.parse is not a validator
JSON.parse tells you that it failed. It is terrible at telling you where, and the message differs per engine. V8 now includes a position; older messages don't.
If you want a usable error you need your own tokenizer, or you extract the position and reconstruct line/column yourself:
try {
JSON.parse(text);
} catch (err) {
const m = /position (\d+)/.exec(err.message);
if (m) {
const upto = text.slice(0, +m[1]);
const line = upto.split('\n').length;
const col = m[1] - upto.lastIndexOf('\n');
// now you can actually point at it
}
}
Also worth knowing: JSON.parse accepts duplicate keys and keeps the last one. Perfectly valid JSON, and a genuinely common source of "why is my config ignoring this setting". A format/validate tool that doesn't warn about duplicates is hiding real bugs — mine does, in the JSON toolkit.
And JSON.stringify(obj, null, 2) is not a formatter for user input: it reorders nothing but it drops comments, loses number formatting (1.0 becomes 1), and throws on BigInt. For a pretty-printer you want to format the token stream, not round-trip through objects.
4. Where I gave up on client-side
Being honest about the limits:
- Usage limits. I enforce per-tool daily quotas, and anything enforced in the browser is a suggestion. That lives server-side, keyed by account or IP.
- Very large files. Past a few hundred MB you're fighting memory ceilings that vary by device. I cap and say so rather than letting the tab die.
- Anything needing a real font/layout engine. Faithful document rendering in the browser means shipping a lot of WASM. Sometimes the honest answer is "this one has a server step".
The takeaway
Client-side-first isn't a purity contest, it's a default. Every file that doesn't get uploaded is a file you don't have to store, secure, or explain in a privacy policy. Most of the time the platform APIs are already enough — the work is in the boring edges: orientation flags, alpha channels, line endings, duplicate keys.
If you want to poke at the result, the tools live at devitools.com. I'm building it a piece at a time and I'd genuinely rather hear what's broken than what's good — the EXIF bug above got found by a user, not by me.
What's the edge case that cost you the most hours? I'll take suggestions for what to build next.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.