@bobfrankston/mailx 1.0.221 → 1.0.222

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx",
3
- "version": "1.0.221",
3
+ "version": "1.0.222",
4
4
  "description": "Local-first email client with IMAP sync and standalone native app",
5
5
  "type": "module",
6
6
  "main": "bin/mailx.js",
@@ -24,7 +24,7 @@
24
24
  "@bobfrankston/iflow-node": "^0.1.2",
25
25
  "@bobfrankston/miscinfo": "^1.0.8",
26
26
  "@bobfrankston/oauthsupport": "^1.0.22",
27
- "@bobfrankston/msger": "^0.1.283",
27
+ "@bobfrankston/msger": "^0.1.284",
28
28
  "@capacitor/android": "^8.3.0",
29
29
  "@capacitor/cli": "^8.3.0",
30
30
  "@capacitor/core": "^8.3.0",
@@ -78,7 +78,7 @@
78
78
  "@bobfrankston/iflow-node": "^0.1.2",
79
79
  "@bobfrankston/miscinfo": "^1.0.8",
80
80
  "@bobfrankston/oauthsupport": "^1.0.22",
81
- "@bobfrankston/msger": "^0.1.283",
81
+ "@bobfrankston/msger": "^0.1.284",
82
82
  "@capacitor/android": "^8.3.0",
83
83
  "@capacitor/cli": "^8.3.0",
84
84
  "@capacitor/core": "^8.3.0",
@@ -946,20 +946,40 @@ export class ImapManager extends EventEmitter {
946
946
  console.log(` [api] ${accountId}/${folder.path}: ${messages.length} new messages`);
947
947
  this.storeApiMessages(accountId, folder.id, messages, highestUid);
948
948
  }
949
- // Reconcile deletions
949
+ // Reconcile deletions — messages present locally but not on the server.
950
+ // SAFETY: this used to silently wipe entire folders when getUids()
951
+ // returned a partial list (e.g. paginated fetch hit a rate limit and
952
+ // bailed). Multiple guards now:
953
+ // 1. getUids() flags partial results via _truncated — refuse to delete
954
+ // 2. If server list is empty but local isn't, assume a transient error
955
+ // 3. If reconcile would delete more than RECONCILE_DELETE_THRESHOLD of
956
+ // local messages, log and skip — safer to keep phantoms than to lose
957
+ // real messages. User can fix with `mailx -rebuild` if needed.
950
958
  try {
951
- const serverUids = new Set(await api.getUids(folder.path));
959
+ const serverUidsArr = await api.getUids(folder.path);
960
+ const serverUids = new Set(serverUidsArr);
952
961
  const localUids = this.db.getUidsForFolder(accountId, folder.id);
953
- let deleted = 0;
954
- for (const uid of localUids) {
955
- if (!serverUids.has(uid)) {
956
- this.db.deleteMessage(accountId, uid);
957
- this.bodyStore.deleteMessage(accountId, folder.id, uid).catch(() => { });
958
- deleted++;
962
+ if (serverUidsArr._truncated) {
963
+ console.log(` [api] ${accountId}/${folder.path}: reconcile skipped — server list truncated (${serverUidsArr.length} ids)`);
964
+ }
965
+ else if (serverUidsArr.length === 0 && localUids.length > 0) {
966
+ console.log(` [api] ${accountId}/${folder.path}: reconcile skipped — server list empty but local has ${localUids.length}`);
967
+ }
968
+ else {
969
+ const toDelete = localUids.filter(uid => !serverUids.has(uid));
970
+ const RECONCILE_DELETE_THRESHOLD = 0.5; // refuse to delete >50% in one pass
971
+ if (localUids.length > 0 && toDelete.length / localUids.length > RECONCILE_DELETE_THRESHOLD) {
972
+ console.log(` [api] ${accountId}/${folder.path}: reconcile refused — would delete ${toDelete.length}/${localUids.length} (${Math.round(toDelete.length / localUids.length * 100)}%) — probably a sync bug, skipping`);
973
+ }
974
+ else {
975
+ for (const uid of toDelete) {
976
+ this.db.deleteMessage(accountId, uid);
977
+ this.bodyStore.deleteMessage(accountId, folder.id, uid).catch(() => { });
978
+ }
979
+ if (toDelete.length > 0)
980
+ console.log(` [api] ${accountId}/${folder.path}: ${toDelete.length} deleted`);
959
981
  }
960
982
  }
961
- if (deleted > 0)
962
- console.log(` [api] ${accountId}/${folder.path}: ${deleted} deleted`);
963
983
  }
964
984
  catch (e) {
965
985
  console.error(` [api] ${accountId}/${folder.path}: reconciliation error: ${e.message}`);
@@ -970,12 +990,15 @@ export class ImapManager extends EventEmitter {
970
990
  }
971
991
  /** Store API-fetched messages to DB */
972
992
  storeApiMessages(accountId, folderId, msgs, highestUid) {
993
+ // highestUid kept for signature compatibility but no longer used to
994
+ // filter — Gmail message IDs aren't monotonic, so `msg.uid <= highestUid`
995
+ // would drop brand-new messages whose hash happens to be smaller than
996
+ // the previous high. upsertMessage's primary-key dedup handles it.
997
+ void highestUid;
973
998
  let stored = 0;
974
999
  this.db.beginTransaction();
975
1000
  try {
976
1001
  for (const msg of msgs) {
977
- if (msg.uid <= highestUid)
978
- continue;
979
1002
  const flags = [];
980
1003
  if (msg.seen)
981
1004
  flags.push("\\Seen");
@@ -8,7 +8,11 @@ export declare class GmailApiProvider implements MailProvider {
8
8
  constructor(tokenProvider: () => Promise<string>);
9
9
  private fetch;
10
10
  listFolders(): Promise<ProviderFolder[]>;
11
- /** List message IDs matching a query, handling pagination */
11
+ /** List message IDs matching a query, handling pagination.
12
+ * IMPORTANT: on any error we throw — do NOT return a partial list, because
13
+ * callers use this for sync reconciliation and a short list would delete
14
+ * real messages from the local DB. Returning [] silently caused the
15
+ * "INBOX empty in mailx" bug when a rate-limit hit mid-pagination. */
12
16
  private listMessageIds;
13
17
  /** Batch-fetch message metadata or full content */
14
18
  private batchFetch;
@@ -93,10 +93,15 @@ export class GmailApiProvider {
93
93
  }
94
94
  return folders;
95
95
  }
96
- /** List message IDs matching a query, handling pagination */
96
+ /** List message IDs matching a query, handling pagination.
97
+ * IMPORTANT: on any error we throw — do NOT return a partial list, because
98
+ * callers use this for sync reconciliation and a short list would delete
99
+ * real messages from the local DB. Returning [] silently caused the
100
+ * "INBOX empty in mailx" bug when a rate-limit hit mid-pagination. */
97
101
  async listMessageIds(query, maxResults = 500) {
98
102
  const ids = [];
99
103
  let pageToken = "";
104
+ let truncated = false;
100
105
  while (true) {
101
106
  const params = new URLSearchParams({ q: query, maxResults: String(Math.min(maxResults - ids.length, 500)) });
102
107
  if (pageToken)
@@ -105,10 +110,17 @@ export class GmailApiProvider {
105
110
  for (const msg of data.messages || []) {
106
111
  ids.push(msg.id);
107
112
  }
108
- if (!data.nextPageToken || ids.length >= maxResults)
113
+ if (!data.nextPageToken)
109
114
  break;
115
+ if (ids.length >= maxResults) {
116
+ // Hit the caller's cap but the server has more. Flag it so
117
+ // reconcile-style callers can refuse to treat this as complete.
118
+ truncated = true;
119
+ break;
120
+ }
110
121
  pageToken = data.nextPageToken;
111
122
  }
123
+ ids._truncated = truncated;
112
124
  return ids;
113
125
  }
114
126
  /** Batch-fetch message metadata or full content */
@@ -170,12 +182,15 @@ export class GmailApiProvider {
170
182
  };
171
183
  }
172
184
  async fetchSince(folder, sinceUid, options = {}) {
173
- // Gmail doesn't have UIDs use date-based query for incremental
174
- // For now, fetch recent messages and let the caller filter by UID
185
+ // Gmail message IDs are hash-derived, NOT monotonic filtering by
186
+ // `uid > sinceUid` silently drops new messages whose hash happens to
187
+ // fall below the high-water mark. Fetch the most recent page and let
188
+ // upsertMessage dedupe by (account, folder, uid). The sinceUid arg is
189
+ // kept for interface compatibility but no longer used for filtering.
190
+ void sinceUid;
175
191
  const query = `in:${this.folderToLabel(folder)}`;
176
192
  const ids = await this.listMessageIds(query, 200);
177
- const messages = await this.batchFetch(ids, options);
178
- return messages.filter(m => m.uid > sinceUid);
193
+ return this.batchFetch(ids, options);
179
194
  }
180
195
  async fetchByDate(folder, since, before, options = {}, onChunk) {
181
196
  const afterDate = this.formatDate(since);
@@ -213,7 +228,11 @@ export class GmailApiProvider {
213
228
  async getUids(folder) {
214
229
  const query = `in:${this.folderToLabel(folder)}`;
215
230
  const ids = await this.listMessageIds(query, 10000);
216
- return ids.map(idToUid);
231
+ const result = ids.map(idToUid);
232
+ // Propagate the truncation flag so reconcile can refuse to delete.
233
+ if (ids._truncated)
234
+ result._truncated = true;
235
+ return result;
217
236
  }
218
237
  async close() {
219
238
  // No persistent connection to close