Skip to content

Commit 920c6f1

Browse files
fix(decrypt): await postal-mime import in parseMail to avoid race (#256)
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#<envelope>` 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 Co-authored-by: dobby-yivi-agent[bot] <275734547+dobby-yivi-agent[bot]@users.noreply.github.com>
1 parent 277712b commit 920c6f1

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

src/lib/components/fallback/email.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { browser } from '$app/environment'
22

3-
let PostalMime
3+
let postalMimePromise
44

5-
// postalmime only works in browser
6-
if (browser) {
7-
import('postal-mime').then((module) => {
8-
PostalMime = module
9-
})
5+
function loadPostalMime() {
6+
if (!browser)
7+
return Promise.reject(new Error('postal-mime is browser-only'))
8+
if (!postalMimePromise) {
9+
postalMimePromise = import('postal-mime')
10+
}
11+
return postalMimePromise
1012
}
1113

1214
// parse email using postalmime
13-
export function parseMail(unparsed) {
14-
const parser = new PostalMime.default()
15+
export async function parseMail(unparsed) {
16+
const module = await loadPostalMime()
17+
const parser = new module.default()
1518
return parser.parse(unparsed)
1619
}
1720

0 commit comments

Comments
 (0)