From 0dd6e9a795def7ea1740ef38e279311392e77227 Mon Sep 17 00:00:00 2001 From: "dobby-yivi-agent[bot]" <275734547+dobby-yivi-agent[bot]@users.noreply.github.com> Date: Thu, 4 Jun 2026 23:45:13 +0000 Subject: [PATCH] fix(decrypt): await postal-mime import in parseMail to avoid race MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous fire-and-forget dynamic import left `PostalMime` undefined until the network round-trip resolved. Callers that hit parseMail before that (e.g. a hard-reload onto `/decrypt#` on a slow connection) threw `Cannot read properties of undefined (reading 'default')`, which the surrounding catch swallowed into a generic "decryption failed" — even though decryption itself had succeeded. Replace with a memoized import promise that parseMail awaits at every call. All call sites already used await/.then so this is a no-op for them. Closes #253 --- src/lib/components/fallback/email.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lib/components/fallback/email.js b/src/lib/components/fallback/email.js index 2903c53..af48627 100644 --- a/src/lib/components/fallback/email.js +++ b/src/lib/components/fallback/email.js @@ -1,17 +1,20 @@ import { browser } from '$app/environment' -let PostalMime +let postalMimePromise -// postalmime only works in browser -if (browser) { - import('postal-mime').then((module) => { - PostalMime = module - }) +function loadPostalMime() { + if (!browser) + return Promise.reject(new Error('postal-mime is browser-only')) + if (!postalMimePromise) { + postalMimePromise = import('postal-mime') + } + return postalMimePromise } // parse email using postalmime -export function parseMail(unparsed) { - const parser = new PostalMime.default() +export async function parseMail(unparsed) { + const module = await loadPostalMime() + const parser = new module.default() return parser.parse(unparsed) }