惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
小众软件
小众软件
博客园_首页
博客园 - 聂微东
罗磊的独立博客
Recent Announcements
Recent Announcements
U
Unit 42
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
D
DataBreaches.Net
Last Week in AI
Last Week in AI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
JavaScript: Handling Large Files in the Browser. Part 1/2...
Alexey Boyko · 2026-04-26 · via DEV Community

The DGRM.net online whiteboard stores data in PNG images. With attachments, the files become large. I’ll explain how data is stored in PNG files.

Fig. 1. DGRM.net opens diagrams from PNG images
Fig. 1. DGRM.net opens diagrams from PNG images

PNG file format

A PNG file consists of blocks. These blocks contain various information. For example, the tIME block contains the editing date.

At the end comes the required IEND block. After the IEND, you can append your own data to the file without breaking the image. This is what DGRM uses: it appends its data to the end of the PNG file.

The resulting file looks like this: Figure 2.

Fig. 2. Image file with DGRM data
Fig. 2. Image file with DGRM data

PNG block structure — Fig. 3.

Fig. 3. PNG block structure
Fig. 3. PNG block structure

Storage in DGRM Data is also organized into blocks, albeit in a slightly different format. The first block is for JSON figures, followed by attachments.

Reading from a file without loading the entire file into memory

You can get a reference to a file on the user’s device using HTMLInputElement. This method will not load the file data into memory (Listing 1).

/**
 * @param {string} accept
 * @param {FileCallback} callBack
 * @param {(evt:Event)=>void} cancelCallBack
 */
const fileInputOpen = (accept, callBack, cancelCallBack) => {
    const input = document.createElement('input');
    input.type = 'file';
    input.multiple = false;
    input.accept = accept;
    input.style.display = 'none';
    document.body.appendChild(input);

    const dispose = () => input?.remove();

    input.oncancel = evt => {
        cancelCallBack(evt);
        dispose();
    };
    input.onchange = () => {
        callBack((!input.files?.length) ? null : input.files[0]);
        dispose();
    };

    input.click();
}

Enter fullscreen mode Exit fullscreen mode

Listing 1. Getting a link to a file

When opening a file, you need to find where the DGRM data begins, i.e., the IEND block.

To search, you need to iterate through the blocks from the beginning of the file to the desired block. However, it’s not advisable to load the entire file into memory.

The pngChunkDataPositionGet function reads only 8 bytes at a time (length + header) and scrolls to the next block until it finds the desired one (Listing 2).

// IEND
const PNG_CHUNK_END_NAME_UINT32 = 1229278788;

/**
 * @param {Blob} pngFile, @param {number} chankNameUint32
 * @returns {Promise<[startBytePosition:number, endBytePosition:number]>}
 */
const pngChunkDataPositionGet = async (pngFile, chankNameUint32) => {
    /** @param {number} pos */
    const uint32Get =
        async pos => uint32From4BytesBlob(pngFile.slice(pos, pos + 4));

    /** @type {number} */ let chunkPosition = 8; // 8 byte - png signature
    /** @type {number} */ let chunkLenght;
    /** @type {number} */ let chunkName;
    /** @type {number} */ let chunkDataStart;
    /** @type {number} */ let chunkDataEnd;

    do {
        chunkLenght = await uint32Get(chunkPosition);
        chunkName = await uint32Get(chunkPosition + 4);
        chunkDataStart = chunkPosition + 8;
        chunkDataEnd = chunkDataStart + chunkLenght;

        if (chunkName === chankNameUint32) {
            return [chunkDataStart, chunkDataEnd];
        }

        chunkPosition = chunkDataEnd + 4;
    } while (chunkName !== PNG_CHUNK_END_NAME_UINT32);

    // looking for end chunk
    if (chunkName === chankNameUint32) {
        return [chunkDataStart, chunkDataEnd];
    }

    return null;
};

Enter fullscreen mode Exit fullscreen mode

Listing 2. Searching for a block in a PNG file

Scrolling large PNGs to the IEND is slow. Therefore, it makes sense to add a custom block at the beginning of the file. In this block, specify the number of bytes to the end of the DGRM Data, i.e., the size of the PNG image without the additional DGRM Data.

Figure 4. Custom dgRp block at the beginning of the PNG file. Indicates the beginning of the DGRM data
Figure 4. Custom dgRp block at the beginning of the PNG file. Indicates the beginning of the DGRM data

In DGRM Data, the first block is JSON figures, then come the attachment blocks — Fig. 5.

Figure 5. DGRM Data blocks
Figure 5. DGRM Data blocks

The JSON block is loaded into memory in its entirety. Attachments are loaded only if they aren’t in the cache.

Attachments can be large, so loading them entirely into memory is also not recommended. More details in the second part of the article.

The second part of the article discusses generating large files in the browser.