fix(desktop): encode new-workspace attachments without a per-byte string

Merged
SU
11d ago
+2-10

Problem

The desktop renderer crashed with an out-of-memory error inside toBase64 while on the new-workspace screen. The V8 old space was full (about 4 GB) and the file buffers held about 860 MB of external memory.

Root cause

The new-workspace prompt uploads each attachment to the host as base64. Its local toBase64 built a binary string one byte at a time (binary += String.fromCharCode(bytes[i])) and then called btoa. Each append makes a new cons-string node in the heap. In Node 24, encoding 20 MB this way adds 32 heap bytes per input byte (Electron's compressed pointers make it about 20). This composer has no file-size cap, so one file of a few hundred MB fills the 4 GB renderer heap before btoa runs.

Fix

Remove the hand-rolled encoder and use the existing renderer/lib/file-to-base64 helper (FileReader.readAsDataURL). The clipboard hook already uses this helper. The browser builds the base64 string natively as one flat string of about 1.33× the file size, with no node per byte. Files larger than the host's inline limit (25 MB) now get the host's normal "Attachment exceeds" error and no longer crash the renderer.

Verification

  • node --expose-gc probe of the old loop on 20 MB: heap bytes per input byte before btoa: 32.0
  • bunx biome check store.ts: Checked 1 file in 50ms. No fixes applied.
  • cd apps/desktop && bun run typecheck: exit 0
  • bun test src/renderer/routes/_authenticated/components/DashboardNewWorkspaceModal: 35 pass, 0 fail

Refs DESKTOP-1KE


Summary by cubic

Fixes the renderer OOM crash on the new-workspace screen when uploading large attachments. The old toBase64 appended one character per byte, creating a heap node per byte that filled the renderer heap before btoa ran. It now uses the existing fileToBase64 helper, which builds the base64 string natively via FileReader.readAsDataURL. Files over the host's 25 MB inline limit now get the normal "Attachment exceeds" error instead of crashing the renderer.

Summary by CodeRabbit

  • Improvements
    • Standardized file handling for attachments uploaded through the host service. Cloud attachment uploads are unchanged.