Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
8c3620a63a
commit
9fe7d78aca
@@ -55,29 +55,44 @@ export const Route = createFileRoute("/hooks/poll-imap")({
|
||||
const sinceUid = (settings.last_uid ?? 0) + 1;
|
||||
const range = `${sinceUid}:*`;
|
||||
|
||||
// First pass: fetch lightweight envelopes only to learn the
|
||||
// size of each message. Then fetch full source one-by-one,
|
||||
// skipping anything over the size cap.
|
||||
const candidates: Array<{ uid: number; size: number }> = [];
|
||||
for await (const meta of client.fetch(
|
||||
range,
|
||||
{ uid: true, size: true },
|
||||
// First pass: collect just UIDs via search (no FETCH stream).
|
||||
// This avoids any large per-message data crossing the socket
|
||||
// before we know which messages to skip.
|
||||
const uidList = await client.search(
|
||||
{ uid: range },
|
||||
{ uid: true },
|
||||
)) {
|
||||
if (meta.uid <= (settings.last_uid ?? 0)) continue;
|
||||
candidates.push({ uid: meta.uid, size: meta.size ?? 0 });
|
||||
if (candidates.length >= MAX_MESSAGES_PER_RUN * 4) break;
|
||||
}
|
||||
);
|
||||
const candidateUids = (uidList ?? [])
|
||||
.filter((u) => u > (settings.last_uid ?? 0))
|
||||
.sort((a, b) => a - b)
|
||||
.slice(0, MAX_MESSAGES_PER_RUN * 4);
|
||||
|
||||
for (const cand of candidates) {
|
||||
for (const uid of candidateUids) {
|
||||
if (imported >= MAX_MESSAGES_PER_RUN) break;
|
||||
|
||||
// Always advance highestUid so we don't re-attempt skipped messages.
|
||||
if (cand.uid > highestUid) highestUid = cand.uid;
|
||||
if (uid > highestUid) highestUid = uid;
|
||||
|
||||
if (cand.size > MAX_MESSAGE_SIZE_BYTES) {
|
||||
// Per-message size probe so we can skip oversize messages
|
||||
// before downloading the full source.
|
||||
let probedSize = 0;
|
||||
try {
|
||||
for await (const meta of client.fetch(
|
||||
String(uid),
|
||||
{ uid: true, size: true },
|
||||
{ uid: true },
|
||||
)) {
|
||||
probedSize = meta.size ?? 0;
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Size probe failed uid", uid, e);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (probedSize > MAX_MESSAGE_SIZE_BYTES) {
|
||||
console.warn(
|
||||
`Skipping IMAP uid ${cand.uid} (${cand.size} bytes > ${MAX_MESSAGE_SIZE_BYTES})`,
|
||||
`Skipping IMAP uid ${uid} (${probedSize} bytes > ${MAX_MESSAGE_SIZE_BYTES})`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
@@ -85,7 +100,7 @@ export const Route = createFileRoute("/hooks/poll-imap")({
|
||||
let msg: { uid: number; source: Buffer; size?: number } | null = null;
|
||||
try {
|
||||
for await (const m of client.fetch(
|
||||
String(cand.uid),
|
||||
String(uid),
|
||||
{ uid: true, source: true, size: true },
|
||||
{ uid: true },
|
||||
)) {
|
||||
@@ -93,7 +108,7 @@ export const Route = createFileRoute("/hooks/poll-imap")({
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Per-message fetch failed uid", cand.uid, e);
|
||||
console.error("Per-message fetch failed uid", uid, e);
|
||||
continue;
|
||||
}
|
||||
if (!msg) continue;
|
||||
|
||||
Reference in New Issue
Block a user