@bobfrankston/mailx-imap 0.1.100 → 0.1.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +21 -1
  2. package/index.js +132 -88
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -271,6 +271,26 @@ export declare class ImapManager extends EventEmitter {
271
271
  * the first caller finishes — important for outbox flushes that
272
272
  * expect their syncFolder for `Sent` to actually run. */
273
273
  private syncFolderLocks;
274
+ /** Per-folder timestamp of the last set-difference reconcile. Drives the
275
+ * throttle for the QRESYNC-path gap backfill (see fetchServerOnlyUids /
276
+ * syncFolder QRESYNC branch). Empty at boot, so the first sync of each
277
+ * folder after a restart always runs a reconcile — which is exactly when
278
+ * a daemon-was-down gap needs to be healed. */
279
+ private lastReconcileMs;
280
+ /**
281
+ * Set-difference backfill. Fetch every server UID (within the history
282
+ * window) that we don't already have locally and aren't fetching this
283
+ * cycle. This is the ONLY mechanism that recovers a message which is on
284
+ * the server but missing locally — the high-water-mark fetch
285
+ * (fetchMessagesSinceUid) cannot, because it only looks above the local
286
+ * highest UID. Gaps it heals: a message that arrived while the daemon was
287
+ * down (so it sits below the watermark once the next poll advances it), or
288
+ * one dropped mid-stream by a response desync. Returns the recovered
289
+ * messages (caller stores them) plus the server UID list it fetched, so the
290
+ * caller's deletion-reconcile can reuse it instead of paying for a second
291
+ * UID SEARCH round-trip.
292
+ */
293
+ private fetchServerOnlyUids;
274
294
  syncFolder(accountId: string, folderId: number, client?: any): Promise<number>;
275
295
  private _syncFolderImpl;
276
296
  /** Sync all folders for all accounts */
@@ -379,7 +399,7 @@ export declare class ImapManager extends EventEmitter {
379
399
  * Server fetch goes through the unified ops queue on the fast lane —
380
400
  * the user clicked, they're waiting, this jumps ahead of any background
381
401
  * prefetch sitting in the slow lane. */
382
- fetchMessageBody(accountId: string, folderId: number, uid: number): Promise<Buffer | null>;
402
+ fetchMessageBody(accountId: string, folderId: number, uid: number, force?: boolean): Promise<Buffer | null>;
383
403
  /** Fetch message body via Gmail/Outlook API.
384
404
  * Throws `MessageNotFoundError` when the server says the message is gone
385
405
  * (deleted from another device, for example). The caller uses that to
package/index.js CHANGED
@@ -21,6 +21,11 @@ const SMTP_PORT_IMPLICIT_TLS = 465;
21
21
  * the same file is retried. Gives the server time to settle so a retry after a
22
22
  * lost-ack doesn't arrive while the first copy is still being processed. */
23
23
  const OUTBOX_RETRY_DELAY_MS = 60000;
24
+ /** How often the QRESYNC fast path runs a full set-difference reconcile to
25
+ * heal gaps the high-water-mark fetch can't see. The first sync of a folder
26
+ * after boot always runs one (the map is empty); thereafter it's throttled to
27
+ * this interval so the QRESYNC speedup is preserved in steady state. */
28
+ const RECONCILE_THROTTLE_MS = 15 * 60 * 1000;
24
29
  /** Parse X-Mailx-Retry* tracking headers from a raw RFC822 message. */
25
30
  function parseRetryInfo(raw) {
26
31
  const headerEnd = raw.search(/\r?\n\r?\n/);
@@ -1307,6 +1312,77 @@ export class ImapManager extends EventEmitter {
1307
1312
  * the first caller finishes — important for outbox flushes that
1308
1313
  * expect their syncFolder for `Sent` to actually run. */
1309
1314
  syncFolderLocks = new Map();
1315
+ /** Per-folder timestamp of the last set-difference reconcile. Drives the
1316
+ * throttle for the QRESYNC-path gap backfill (see fetchServerOnlyUids /
1317
+ * syncFolder QRESYNC branch). Empty at boot, so the first sync of each
1318
+ * folder after a restart always runs a reconcile — which is exactly when
1319
+ * a daemon-was-down gap needs to be healed. */
1320
+ lastReconcileMs = new Map();
1321
+ /**
1322
+ * Set-difference backfill. Fetch every server UID (within the history
1323
+ * window) that we don't already have locally and aren't fetching this
1324
+ * cycle. This is the ONLY mechanism that recovers a message which is on
1325
+ * the server but missing locally — the high-water-mark fetch
1326
+ * (fetchMessagesSinceUid) cannot, because it only looks above the local
1327
+ * highest UID. Gaps it heals: a message that arrived while the daemon was
1328
+ * down (so it sits below the watermark once the next poll advances it), or
1329
+ * one dropped mid-stream by a response desync. Returns the recovered
1330
+ * messages (caller stores them) plus the server UID list it fetched, so the
1331
+ * caller's deletion-reconcile can reuse it instead of paying for a second
1332
+ * UID SEARCH round-trip.
1333
+ */
1334
+ async fetchServerOnlyUids(client, accountId, folderId, folderPath, statusMessageCount, excludeUids) {
1335
+ const existingUids = this.db.getUidsForFolder(accountId, folderId);
1336
+ // Small folders (Drafts, Sent, …) get a FULL UID fetch so server-side
1337
+ // deletions of OLDER messages are reflected; large folders (134k INBOX)
1338
+ // get a date-bounded query so we don't enumerate the whole mailbox.
1339
+ const SMALL_FOLDER_THRESHOLD = 500;
1340
+ const isSmallFolder = statusMessageCount !== null && statusMessageCount <= SMALL_FOLDER_THRESHOLD;
1341
+ const historyDays = getHistoryDays(accountId);
1342
+ const SINCE_DAYS = historyDays > 0 ? historyDays : 1825;
1343
+ const sinceDate = new Date(Date.now() - SINCE_DAYS * 86400000);
1344
+ let serverUids;
1345
+ let dateBounded;
1346
+ const __t0 = Date.now();
1347
+ if (isSmallFolder) {
1348
+ console.log(` [sync-uids] ${accountId}/${folderPath}: small folder (server=${statusMessageCount}) — getUids ALL`);
1349
+ serverUids = await client.getUids(folderPath);
1350
+ dateBounded = false;
1351
+ }
1352
+ else {
1353
+ console.log(` [sync-uids] ${accountId}/${folderPath}: getUidsSince ${sinceDate.toISOString().slice(0, 10)}...`);
1354
+ serverUids = typeof client.getUidsSince === "function"
1355
+ ? await client.getUidsSince(folderPath, sinceDate)
1356
+ : await client.getUids(folderPath);
1357
+ dateBounded = true;
1358
+ }
1359
+ console.log(` [sync-uids] ${accountId}/${folderPath}: ${serverUids.length} UIDs in ${Date.now() - __t0}ms`);
1360
+ const existingSet = new Set(existingUids);
1361
+ const missingUids = serverUids.filter((uid) => !existingSet.has(uid) && !excludeUids.has(uid));
1362
+ const recovered = [];
1363
+ if (missingUids.length === 0)
1364
+ return { recovered, serverUids, dateBounded };
1365
+ // Cap so a misbehaving response / brand-new account doesn't pull tens
1366
+ // of thousands at once; the rest resumes next cycle. Higher UID ≈ more
1367
+ // recent under stable UIDVALIDITY, so prefer newest when capping.
1368
+ const BACKFILL_CHUNK_SIZE = 100;
1369
+ const RECONCILE_FETCH_CAP = 5000;
1370
+ let toFetch = missingUids;
1371
+ if (missingUids.length > RECONCILE_FETCH_CAP) {
1372
+ console.log(` ${folderPath}: ${missingUids.length} server-only UIDs — capped at ${RECONCILE_FETCH_CAP}; will resume next cycle`);
1373
+ toFetch = missingUids.slice().sort((a, b) => b - a).slice(0, RECONCILE_FETCH_CAP);
1374
+ }
1375
+ else {
1376
+ console.log(` ${folderPath}: ${missingUids.length} server-only UIDs — fetching`);
1377
+ }
1378
+ for (let i = 0; i < toFetch.length; i += BACKFILL_CHUNK_SIZE) {
1379
+ const chunk = toFetch.slice(i, i + BACKFILL_CHUNK_SIZE);
1380
+ const got = await client.fetchMessages(folderPath, chunk.join(","), { source: false });
1381
+ recovered.push(...got);
1382
+ console.log(` ${folderPath}: backfill ${recovered.length}/${toFetch.length}`);
1383
+ }
1384
+ return { recovered, serverUids, dateBounded };
1385
+ }
1310
1386
  async syncFolder(accountId, folderId, client) {
1311
1387
  const lockKey = `${accountId}:${folderId}`;
1312
1388
  const inflight = this.syncFolderLocks.get(lockKey);
@@ -1501,6 +1577,35 @@ export class ImapManager extends EventEmitter {
1501
1577
  await this.storeMessages(accountId, folderId, folder, newOnes, highestUid);
1502
1578
  console.log(` [qr-phase] ${folder.path}: storeMessages(${newOnes.length}) in ${Date.now() - __t2}ms`);
1503
1579
  }
1580
+ // SET-DIFF GAP RECOVERY (bounded + throttled). The
1581
+ // fetchMessagesSinceUid above is a high-water-mark fetch —
1582
+ // it CANNOT recover a server UID that's missing below our
1583
+ // highest (a message that arrived while the daemon was down,
1584
+ // now under the watermark the next poll advances) or one
1585
+ // dropped mid-stream by a response desync. QRESYNC otherwise
1586
+ // returns here, making such gaps permanent (Bob 2026-06-20:
1587
+ // "messages before 7am are missing" — daemon crashed
1588
+ // overnight; the catch-up since-fetch lost the low UIDs of
1589
+ // the range and QRESYNC never reconciled). Run a full
1590
+ // set-diff on the first sync after boot, then at most once
1591
+ // per RECONCILE_THROTTLE_MS so the fast path stays fast.
1592
+ let backfilledCount = 0;
1593
+ const lastRecon = this.lastReconcileMs.get(folderId) ?? 0;
1594
+ if (Date.now() - lastRecon > RECONCILE_THROTTLE_MS) {
1595
+ this.lastReconcileMs.set(folderId, Date.now());
1596
+ try {
1597
+ const sd = await this.fetchServerOnlyUids(client, accountId, folderId, folder.path, statusMessageCount, new Set(newOnes.map((m) => m.uid)));
1598
+ if (sd.recovered.length > 0) {
1599
+ backfilledCount = await this.storeMessages(accountId, folderId, folder, sd.recovered, highestUid);
1600
+ console.log(` [qresync] ${accountId}/${folder.path}: set-diff backfilled ${backfilledCount} server-only UID(s)`);
1601
+ this.db.recalcFolderCounts(folderId);
1602
+ this.emit("folderCountsChanged", accountId, {});
1603
+ }
1604
+ }
1605
+ catch (e) {
1606
+ console.error(` [qresync] ${accountId}/${folder.path}: set-diff backfill failed: ${e?.message || e}`);
1607
+ }
1608
+ }
1504
1609
  // Persist new watermark — next resync starts here.
1505
1610
  if (qr.newHighestModSeq !== undefined) {
1506
1611
  this.db.updateFolderSync(folderId, qr.exists ? prevUidValidity : prevUidValidity, String(qr.newHighestModSeq));
@@ -1515,7 +1620,7 @@ export class ImapManager extends EventEmitter {
1515
1620
  this.sweepPhantomRows(accountId, folderId, folder.path, serverUidNext);
1516
1621
  this.emit("syncProgress", accountId, `sync:${folder.path}`, 100);
1517
1622
  console.log(` [qresync] ${accountId}/${folder.path}: done in ${Date.now() - __sfStart}ms (path: QRESYNC)`);
1518
- return newOnes.length;
1623
+ return newOnes.length + backfilledCount;
1519
1624
  }
1520
1625
  }
1521
1626
  catch (qrErr) {
@@ -1571,96 +1676,22 @@ export class ImapManager extends EventEmitter {
1571
1676
  // brand-new account doesn't try to pull tens of thousands of
1572
1677
  // historical messages in one cycle. Hit the cap and the next
1573
1678
  // sync picks up the rest.
1574
- const existingUids = this.db.getUidsForFolder(accountId, folderId);
1575
1679
  try {
1576
- // Date-bound the server UID query when we have a history
1577
- // window. UID SEARCH ALL on a 134k-message INBOX returns
1578
- // 134k integers and triggers a heavyweight full-folder
1579
- // scan on the server; UID SEARCH SINCE <date> returns only
1580
- // UIDs in the window we actually care about. For
1581
- // historyDays=0 ("keep everything") we still bound to the
1582
- // last 5 years on incremental syncs enough that we never
1583
- // miss an in-flight message, without enumerating decades of
1584
- // archive every cycle. First sync gets all UIDs (no anchor
1585
- // yet so we have to compare against the empty local set
1586
- // anyway).
1587
- // Small folders (Drafts, Sent, Outbox, Trash, …) get a FULL
1588
- // UID fetch instead of date-bounded. Without this, server-side
1589
- // deletions of OLDER messages (e.g., user emptied Trash from
1590
- // Thunderbird) are never reflected — date-bound filters them
1591
- // out of the comparison entirely. Bob 2026-05-12: "I deleted
1592
- // my drafts and Thunderbird shows that but rmfmail is not
1593
- // acknowledging the deletions." Threshold tuned conservatively
1594
- // — UID SEARCH ALL on a 500-message folder is sub-second; the
1595
- // pathology being avoided is the same call on 130k+ INBOX.
1596
- const SMALL_FOLDER_THRESHOLD = 500;
1597
- const isSmallFolder = statusMessageCount !== null && statusMessageCount <= SMALL_FOLDER_THRESHOLD;
1598
- const SINCE_DAYS = effectiveDays > 0 ? effectiveDays : 1825;
1599
- const sinceDate = new Date(Date.now() - SINCE_DAYS * 86400000);
1600
- let allServerUids;
1601
- const __uidsT0 = Date.now();
1602
- if (isSmallFolder) {
1603
- console.log(` [sync-uids] ${accountId}/${folder.path}: small folder (server=${statusMessageCount}) — getUids ALL`);
1604
- allServerUids = await client.getUids(folder.path);
1605
- serverUidsAreDateBounded = false;
1606
- }
1607
- else {
1608
- console.log(` [sync-uids] ${accountId}/${folder.path}: getUidsSince ${sinceDate.toISOString().slice(0, 10)}...`);
1609
- allServerUids = typeof client.getUidsSince === "function"
1610
- ? await client.getUidsSince(folder.path, sinceDate)
1611
- : await client.getUids(folder.path);
1612
- serverUidsAreDateBounded = true;
1613
- }
1614
- console.log(` [sync-uids] ${accountId}/${folder.path}: ${allServerUids.length} UIDs in ${Date.now() - __uidsT0}ms`);
1615
- // Stash for the deletion-reconciliation block below — we
1616
- // already have the server UID list, no point hitting the
1617
- // server a second time.
1618
- serverUidsCached = allServerUids;
1619
- const existingSet = new Set(existingUids);
1620
- const newSet = new Set(messages.map(m => m.uid));
1621
- const missingUids = allServerUids.filter((uid) => !existingSet.has(uid) && !newSet.has(uid));
1622
- // Backfill chunk size. Use the passed-in `client` directly
1623
- // (NOT a nested withConnection) — syncFolder is now wrapped
1624
- // in withConnection at the call site, so the slow lane is
1625
- // already locked for our duration. A nested withConnection
1626
- // would deadlock waiting for the slot we hold.
1627
- const BACKFILL_CHUNK_SIZE = 100;
1628
- if (missingUids.length > 0 && missingUids.length <= 5000) {
1629
- let minU = existingUids[0] ?? 0;
1630
- for (let i = 1; i < existingUids.length; i++)
1631
- if (existingUids[i] < minU)
1632
- minU = existingUids[i];
1633
- console.log(` ${folder.path}: ${missingUids.length} server-only UIDs (local lowest=${minU}, highest=${highestUid}) — fetching`);
1634
- let recoveredTotal = 0;
1635
- for (let i = 0; i < missingUids.length; i += BACKFILL_CHUNK_SIZE) {
1636
- const chunk = missingUids.slice(i, i + BACKFILL_CHUNK_SIZE);
1637
- const range = chunk.join(",");
1638
- const recovered = await client.fetchMessages(folder.path, range, { source: false });
1639
- messages.push(...recovered);
1640
- recoveredTotal += recovered.length;
1641
- console.log(` ${folder.path}: fetch ${recoveredTotal}/${missingUids.length}`);
1642
- }
1643
- }
1644
- else if (missingUids.length > 5000) {
1645
- console.log(` ${folder.path}: ${missingUids.length} server-only UIDs — capped; will resume next cycle`);
1646
- // Vanilla IMAP under stable UIDVALIDITY: higher UID =
1647
- // later assignment ≈ more recent message (Dovecot/
1648
- // Cyrus). Gmail-API path is separate (no temporal
1649
- // meaning to its hash-UIDs).
1650
- const cappedSlice = missingUids.sort((a, b) => b - a).slice(0, 5000);
1651
- let recoveredTotal = 0;
1652
- for (let i = 0; i < cappedSlice.length; i += BACKFILL_CHUNK_SIZE) {
1653
- const chunk = cappedSlice.slice(i, i + BACKFILL_CHUNK_SIZE);
1654
- const recovered = await client.fetchMessages(folder.path, chunk.join(","), { source: false });
1655
- messages.push(...recovered);
1656
- recoveredTotal += recovered.length;
1657
- console.log(` ${folder.path}: fetch ${recoveredTotal}/5000 (capped)`);
1658
- }
1659
- }
1680
+ // Shared with the QRESYNC fast path (fetchServerOnlyUids).
1681
+ // Exclude the UIDs we just fetched via fetchMessagesSinceUid
1682
+ // they're in `messages` but not yet in the DB, so they'd
1683
+ // otherwise look "missing" and get re-fetched.
1684
+ const sd = await this.fetchServerOnlyUids(client, accountId, folderId, folder.path, statusMessageCount, new Set(messages.map(m => m.uid)));
1685
+ messages.push(...sd.recovered);
1686
+ // Stash the server UID list for the deletion-reconciliation
1687
+ // block below no point hitting the server a second time.
1688
+ serverUidsCached = sd.serverUids;
1689
+ serverUidsAreDateBounded = sd.dateBounded;
1660
1690
  }
1661
1691
  catch (e) {
1662
1692
  console.error(` ${folder.path}: reconciliation failed: ${e.message}`);
1663
1693
  }
1694
+ this.lastReconcileMs.set(folderId, Date.now());
1664
1695
  // Date-based backfill via SEARCH SINCE was here. Removed —
1665
1696
  // set-diff above already covers the same case (any server UID
1666
1697
  // not in our local set gets fetched, regardless of whether it's
@@ -3017,7 +3048,7 @@ export class ImapManager extends EventEmitter {
3017
3048
  * Server fetch goes through the unified ops queue on the fast lane —
3018
3049
  * the user clicked, they're waiting, this jumps ahead of any background
3019
3050
  * prefetch sitting in the slow lane. */
3020
- async fetchMessageBody(accountId, folderId, uid) {
3051
+ async fetchMessageBody(accountId, folderId, uid, force = false) {
3021
3052
  // Belt-and-braces against `UID FETCH 0`. The IMAP server rejects it as
3022
3053
  // BAD "Invalid uidset" and the connection slot is consumed for the
3023
3054
  // round-trip. The enqueue path now guards too — this catches direct
@@ -3042,7 +3073,17 @@ export class ImapManager extends EventEmitter {
3042
3073
  // delayed until the client timed out. The 5min→12h backoff (shared with
3043
3074
  // prefetch via isPrefetchEmpty) stops the tight re-fetch loop; the body
3044
3075
  // simply shows as unavailable until the backoff lapses and one retry runs.
3045
- if (this.isPrefetchEmpty(accountId, folderId, uid))
3076
+ // EXCEPT an interactive click (`force`): the user is staring at a blank
3077
+ // preview demanding THIS body NOW. Honoring a *background* prefetch
3078
+ // failure's backoff for an explicit user action is a local-first
3079
+ // violation — it left a real INBOX message (bobma uid 4967554,
3080
+ // 2026-06-19) showing an eternal "getting body" spinner for up to 12 h
3081
+ // because one earlier prefetch chunk returned 0 bodies (an iflow parser
3082
+ // miss, not a real expunge — siblings recovered on retry). A forced
3083
+ // single-UID fetch re-attempts immediately; if it genuinely fails it
3084
+ // re-arms the backoff below and the reconciler emits bodyFetchError so
3085
+ // the viewer shows a banner instead of spinning forever.
3086
+ if (!force && this.isPrefetchEmpty(accountId, folderId, uid))
3046
3087
  return null;
3047
3088
  const folder = this.db.getFolders(accountId).find(f => f.id === folderId);
3048
3089
  if (!folder)
@@ -3067,6 +3108,9 @@ export class ImapManager extends EventEmitter {
3067
3108
  return null;
3068
3109
  const bodyPath = await this.bodyStore.putMessage(accountId, folderId, uid, raw);
3069
3110
  this.db.updateBodyPath(accountId, folderId, uid, bodyPath);
3111
+ // A forced fetch that beat a stale backoff just healed the row;
3112
+ // drop the prefetch-empty record so it doesn't suppress the next tick.
3113
+ this.clearPrefetchEmpty(accountId, folderId, uid);
3070
3114
  this.emit("bodyCached", accountId, uid);
3071
3115
  return raw;
3072
3116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-imap",
3
- "version": "0.1.100",
3
+ "version": "0.1.102",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",