Skip to content

fix: serialize raw email content as base64 string to prevent RangeError on large emails#349

Open
vincentmaria wants to merge 1 commit into
LogicLabs-OU:mainfrom
vincentmaria:fix/raw-buffer-base64-serialization
Open

fix: serialize raw email content as base64 string to prevent RangeError on large emails#349
vincentmaria wants to merge 1 commit into
LogicLabs-OU:mainfrom
vincentmaria:fix/raw-buffer-base64-serialization

Conversation

@vincentmaria
Copy link
Copy Markdown

@vincentmaria vincentmaria commented Apr 9, 2026

Problem

Opening archived emails in the UI fails with HTTP 500 for emails with large raw EML content. The backend logs show:

RangeError: Invalid array length
    at Buffer.toJSON (node:buffer:1158:15)
    at JSON.stringify (<anonymous>)
    at ServerResponse.json (express/lib/response.js)
    at getArchivedEmailById (archived-email.controller.js:41)

The frontend displays "Email not found" for any non-200 response, so affected emails appear missing even though they exist in the database.

Root Cause

ArchivedEmailService.getArchivedEmailById includes the raw EML content as a Buffer in the JSON response. When Express calls JSON.stringify, Node.js internally invokes Buffer.toJSON() which attempts to create a plain JavaScript array with new Array(buffer.length). For large email files this throws RangeError: Invalid array length.

Fix

Convert the raw EML Buffer to a base64 string before including it in the API response. This avoids Buffer.toJSON() entirely and is the standard approach for transferring binary data over JSON REST APIs.

Both frontend consumers (EmailPreview.svelte and the email detail page) are updated to decode the base64 string back to Uint8Array before passing it to postal-mime for parsing. Backwards compatibility with the old {type: 'Buffer', data: [...]} format is retained for any existing clients.

Changes

  • packages/backend/src/services/ArchivedEmailService.ts: convert raw Buffer to base64 string via rawBuffer.toString('base64')
  • packages/types/src/archived-emails.types.ts: update raw field type from Buffer to string
  • packages/frontend/src/lib/components/custom/EmailPreview.svelte: handle base64 string input, decode with atob()
  • packages/frontend/src/routes/dashboard/archived-emails/[id]/+page.svelte: same base64 decoding for the attachment parser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant