fix: serialize raw email content as base64 string to prevent RangeError on large emails#349
Open
vincentmaria wants to merge 1 commit into
Open
Conversation
…or on large emails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Opening archived emails in the UI fails with HTTP 500 for emails with large raw EML content. The backend logs show:
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.getArchivedEmailByIdincludes the raw EML content as aBufferin the JSON response. When Express callsJSON.stringify, Node.js internally invokesBuffer.toJSON()which attempts to create a plain JavaScript array withnew Array(buffer.length). For large email files this throwsRangeError: Invalid array length.Fix
Convert the raw EML
Bufferto a base64 string before including it in the API response. This avoidsBuffer.toJSON()entirely and is the standard approach for transferring binary data over JSON REST APIs.Both frontend consumers (
EmailPreview.svelteand the email detail page) are updated to decode the base64 string back toUint8Arraybefore passing it topostal-mimefor 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 viarawBuffer.toString('base64')packages/types/src/archived-emails.types.ts: updaterawfield type fromBuffertostringpackages/frontend/src/lib/components/custom/EmailPreview.svelte: handle base64 string input, decode withatob()packages/frontend/src/routes/dashboard/archived-emails/[id]/+page.svelte: same base64 decoding for the attachment parser