Urgent.News

What's breaking now, across hundreds of outlets.

Tech

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 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.…

In his journey to move file tools to the browser, the author encountered several challenges. Initially, they aimed to build deviTools with the rule that if the browser can handle the task, the file should never leave the tab. This approach has its advantages, such as reduced server storage and improved privacy.

The author first tackled image conversion using Canvas, which allows decode and encode operations to occur within a Worker. This enables the processing of large images like 40 MP photos without freezing the user interface. However, some issues arose during this process. For instance, converting a transparent PNG to JPEG caused alpha channel loss, as JPEG silently composites transparent areas onto black.

To address this, the author had to manually fill the canvas with white before drawing. Additionally, EXIF orientation information was not automatically preserved when using createImageBitmap; it required a specific flag to be set. Furthermore, HEIC files are only decoded by Safari without additional decoding, which led to the creation of a WASM decoder for HEIC/HEIF to maintain the privacy promise.

Moving on to diffing, the author discovered that building one DOM node per line for each side proved inefficient. Scaling to 5,000 lines would result in 10,000 nodes plus intra-line highlight spans, causing scrolling issues. The solution was to virtualize the viewport, rendering around 60 rows and translating a spacer to accommodate the entire dataset.

The main thread handled rendering, while the Worker performed the diff algorithm, which is relatively straightforward. To improve performance further, the author computed intra-line character diffs only for rows currently visible and normalized line endings before comparison to avoid confusion between different line ending formats.

When it came to JSON handling, the author learned that JSON.parse is not an ideal validator. While it tells you if parsing failed, it provides minimal information about where the error occurred, and the error messages differ across engines. To create a more user-friendly error message, the author implemented their own tokenizer or extracted the position from the error message and reconstructed the line and column information.

Additionally, JSON.parse accepts duplicate keys, which can lead to unexpected behavior in configuration files. Implementing a format/validate tool that explicitly warns about duplicate keys can prevent real bugs from going unnoticed.

Lastly, the author acknowledged the limitations of client-side processing. Large files exceeding a few hundred MB may challenge memory limitations on different devices, so the author caps file sizes and informs users about these restrictions. Furthermore, certain tasks like rendering complex documents require a real font/layout engine, which is not feasible entirely in the browser.

In such cases, the author suggests using a server step to handle these specific requirements. Overall, the author emphasizes that client-side-first development is a practical approach, focusing on leveraging platform APIs and addressing the more challenging aspects, such as orientation flags, alpha channels, line endings, and duplicate keys.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

I benchmarked 5 ways to auto-tune a robot arm's PID gains. Random search broke the safety limits in 73.8% of trials. Safe BO: 1.8%.

Hand-tuning PID gains is slow, and every payload change means starting over. Letting an optimiser explore on the real machine is tempting — but a "trial" there isn't a wasted data point, it's a torque…

  • Random search broke safety limits in 73.8% of trials
  • Safe Bayesian Optimization achieved only 1.8% unsafe trials
  • Warm-starting proved more effective than advanced acquisition functions

Stop Data Leaks in Multi-Tenant Apps with PostgreSQL Row-Level Security

Most multi-tenant SaaS bugs are not complex logic failures. They are simple, silent data leaks. They happen when a query is missing a single WHERE tenant_id = $1 clause.

  • Multi-tenant apps leak data due to missing WHERE clauses
  • PostgreSQL's Row-Level Security (RLS) provides isolation
  • Implement RLS by enabling it and defining policies on tables

Analyzing doctors data using SQL

Today, I focused on analyzing hospital records from a doctor's perspective. Instead of writing queries just to retrieve rows, I started asking meaningful questions that someone in hospital management might actually ask. I explored questions such as: How many doctors are available in the hospital?

  • Hospital doctors' data analyzed using SQL to answer management questions
  • Queries written to find available doctors, patients per doctor, gender distribution
  • Concepts like GROUP BY, COUNT(), ORDER BY, WHERE, ILIKE used for analysis

More from Tuesday 24 November →