@remit/imap-worker 0.0.56 → 0.0.58

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": "@remit/imap-worker",
3
- "version": "0.0.56",
3
+ "version": "0.0.58",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
@@ -689,7 +689,6 @@ describe("handleMessageDelete", () => {
689
689
  assert.equal(called("message.delete").length, 0);
690
690
  assert.equal(called("threadMessage.deleteMany").length, 0);
691
691
  assert.equal(called("threadMessage.update").length, 0);
692
- assert.equal(called("emitEvent").length, 0);
693
692
 
694
693
  // The row's mailbox and uid stay put, but `status` must leave
695
694
  // `moving`: `isPlacementUnsettled` reads exactly that value, so a row
@@ -702,6 +701,27 @@ describe("handleMessageDelete", () => {
702
701
  assert.equal(await imapFailures("MESSAGE_DELETE_EXHAUSTED"), 1);
703
702
  });
704
703
 
704
+ // The optimistic `updateForMove` already pointed the row at Trash. The
705
+ // server has now confirmed the message never left the source, so the row
706
+ // and the server disagree about where the mail is, and only a resync of
707
+ // both folders settles that. Without it the user is shown the message in
708
+ // Trash indefinitely while it sits in the source folder.
709
+ it("resyncs both folders when the server confirms the message never left the source", async () => {
710
+ h.connection.moveMessages = async () => ({ uidMap: new Map() });
711
+ h.destinationSearchUids = [100];
712
+
713
+ await handleMessageDelete(moveEvent, noopLog, 3, deps());
714
+
715
+ assert.deepEqual(
716
+ called("emitEvent").map((c) => c.args[0]),
717
+ [
718
+ { type: "SYNC_MESSAGES", accountId: "acc-1", mailboxId: "src-mbx" },
719
+ { type: "SYNC_MESSAGES", accountId: "acc-1", mailboxId: "trash-mbx" },
720
+ ],
721
+ "a confirmed divergence resyncs on the broken verdict too, not only on the reconciled one",
722
+ );
723
+ });
724
+
705
725
  // Issue #980, the failure the budget exists for: every redelivery
706
726
  // re-MOVEs a uid the source no longer holds and throws identically. The
707
727
  // ceiling lives in the error catch, so a throwing `moveMessages` settles
@@ -357,9 +357,16 @@ export const handleMessageDelete = async (
357
357
 
358
358
  if (outcome === "broken") {
359
359
  recordImapFailure("MESSAGE_DELETE_EXHAUSTED", "other");
360
- return;
361
360
  }
362
361
 
362
+ // Both verdicts end in a row the server has contradicted, so this delete
363
+ // reconciles rather than waits (R2): whichever folder actually holds the
364
+ // message re-projects it with the server's own uid. RECONCILED, the local
365
+ // rows are gone and the resync rebuilds them. BROKEN, the server has just
366
+ // confirmed the message is still at the source while the optimistic
367
+ // `updateForMove` left the row pointing at Trash — without the resync the
368
+ // user sees it in Trash for as long as that row stands, which is the
369
+ // silent misdirection the settle exists to end.
363
370
  if (destinationMailboxId) {
364
371
  await emitMoveResync(emitEvent, {
365
372
  accountId,
@@ -2,9 +2,11 @@ import assert from "node:assert/strict";
2
2
  import { describe, it } from "node:test";
3
3
  import {
4
4
  DEFAULT_MAILBOX_FRESHNESS_MS,
5
+ emitSyncMessagesEvents,
5
6
  MAILBOX_FRESHNESS_MS,
6
7
  mailboxNeedsSync,
7
8
  resolveMailboxFreshnessMs,
9
+ splitInboxFirst,
8
10
  } from "./sync-mailboxes.js";
9
11
 
10
12
  const NOW = 1_700_000_000_000;
@@ -84,3 +86,99 @@ describe("resolveMailboxFreshnessMs", () => {
84
86
  );
85
87
  });
86
88
  });
89
+
90
+ describe("splitInboxFirst", () => {
91
+ it("pulls INBOX out of the list whatever case it is spelled in", () => {
92
+ const { inbox, rest } = splitInboxFirst([
93
+ { mailboxId: "mb-sent", fullPath: "Sent" },
94
+ { mailboxId: "mb-inbox", fullPath: "Inbox" },
95
+ { mailboxId: "mb-junk", fullPath: "Junk" },
96
+ ]);
97
+
98
+ assert.equal(inbox?.mailboxId, "mb-inbox");
99
+ assert.deepEqual(
100
+ rest.map((mailbox) => mailbox.mailboxId),
101
+ ["mb-sent", "mb-junk"],
102
+ );
103
+ });
104
+
105
+ it("leaves an account without an INBOX intact", () => {
106
+ const { inbox, rest } = splitInboxFirst([
107
+ { mailboxId: "mb-sent", fullPath: "Sent" },
108
+ ]);
109
+
110
+ assert.equal(inbox, undefined);
111
+ assert.deepEqual(
112
+ rest.map((mailbox) => mailbox.mailboxId),
113
+ ["mb-sent"],
114
+ );
115
+ });
116
+ });
117
+
118
+ describe("emitSyncMessagesEvents", () => {
119
+ // Every event of an account shares one FIFO group, so arrival order is
120
+ // service order. pMap only bounds concurrency: it used to race all the
121
+ // emits, letting INBOX queue behind up to nineteen other folders while a
122
+ // person waited for their new mail.
123
+ it("emits INBOX before any other folder", async () => {
124
+ const emitted: string[] = [];
125
+ const mailboxes = Array.from({ length: 25 }, (_, index) => ({
126
+ mailboxId: `mb-${index}`,
127
+ fullPath: `Folder ${index}`,
128
+ }));
129
+ mailboxes.push({ mailboxId: "mb-inbox", fullPath: "INBOX" });
130
+
131
+ await emitSyncMessagesEvents("acc-1", mailboxes, async (event) => {
132
+ emitted.push(event.mailboxId);
133
+ });
134
+
135
+ assert.equal(emitted[0], "mb-inbox");
136
+ assert.equal(emitted.length, 26);
137
+ });
138
+
139
+ // Emitting INBOX and awaiting it is the whole fix: a fire-and-forget emit
140
+ // would leave it racing the fan-out again.
141
+ it("waits for the INBOX emit to resolve before emitting the rest", async () => {
142
+ const emitted: string[] = [];
143
+ let releaseInbox: () => void = () => {};
144
+ const inboxEmitted = new Promise<void>((resolve) => {
145
+ releaseInbox = resolve;
146
+ });
147
+
148
+ const done = emitSyncMessagesEvents(
149
+ "acc-1",
150
+ [
151
+ { mailboxId: "mb-inbox", fullPath: "INBOX" },
152
+ { mailboxId: "mb-sent", fullPath: "Sent" },
153
+ ],
154
+ async (event) => {
155
+ emitted.push(event.mailboxId);
156
+ if (event.mailboxId === "mb-inbox") await inboxEmitted;
157
+ },
158
+ );
159
+
160
+ await new Promise((resolve) => setImmediate(resolve));
161
+ assert.deepEqual(emitted, ["mb-inbox"]);
162
+
163
+ releaseInbox();
164
+ await done;
165
+ assert.deepEqual(emitted, ["mb-inbox", "mb-sent"]);
166
+ });
167
+
168
+ it("emits every folder for an account with no INBOX", async () => {
169
+ const emitted: string[] = [];
170
+
171
+ await emitSyncMessagesEvents(
172
+ "acc-1",
173
+ [
174
+ { mailboxId: "mb-sent", fullPath: "Sent" },
175
+ { mailboxId: "mb-junk", fullPath: "Junk" },
176
+ ],
177
+ async (event) => {
178
+ emitted.push(event.mailboxId);
179
+ },
180
+ );
181
+
182
+ assert.deepEqual(emitted.sort(), ["mb-junk", "mb-sent"]);
183
+ });
184
+ });
@@ -31,9 +31,9 @@ const EVENT_EMIT_CONCURRENCY = 20;
31
31
  * client produces when it loads (`GET /config` triggers a sync per account)
32
32
  * into one round of IMAP work.
33
33
  *
34
- * The web client floors its automatic poll at this same window
35
- * (`MIN_POLL_INTERVAL_MS` in useStaleAccountSync), so the one caller that
36
- * skips this gate on a timer still cannot drive a fan-out faster than it.
34
+ * This gates side-effect triggers only. The web client's automatic poll asks
35
+ * by name and so skips it; what bounds that one is its own floor
36
+ * (`MIN_POLL_INTERVAL_MS` in useStaleAccountSync, 30s).
37
37
  */
38
38
  export const DEFAULT_MAILBOX_FRESHNESS_MS = 60_000;
39
39
 
@@ -264,20 +264,54 @@ const syncMailboxesForAccount = async (
264
264
  mailboxCountSynced: skipped,
265
265
  });
266
266
 
267
- // Emit events in parallel with concurrency limit
268
- // INBOX is first in the sorted list, so it gets priority
269
- await pMap(
270
- mailboxes,
271
- ({ mailboxId }) => {
272
- const syncEvent: Omit<SyncMessagesEvent, "eventId" | "timestamp"> = {
273
- type: "SYNC_MESSAGES",
274
- accountId,
275
- mailboxId,
276
- };
277
- return emitEvent(syncEvent);
278
- },
279
- { concurrency: EVENT_EMIT_CONCURRENCY },
267
+ await emitSyncMessagesEvents(accountId, mailboxes, emitEvent);
268
+ };
269
+
270
+ type SyncMessagesInput = Omit<SyncMessagesEvent, "eventId" | "timestamp">;
271
+
272
+ /**
273
+ * Separate INBOX from the rest of the fan-out. `collectAllMailboxes` sorts it
274
+ * first, but sort order alone decides nothing once the emits go out together.
275
+ */
276
+ export const splitInboxFirst = <T extends { fullPath: string }>(
277
+ mailboxes: readonly T[],
278
+ ): { inbox: T | undefined; rest: T[] } => {
279
+ const at = mailboxes.findIndex(
280
+ (mailbox) => mailbox.fullPath.toUpperCase() === "INBOX",
280
281
  );
282
+ if (at === -1) return { inbox: undefined, rest: [...mailboxes] };
283
+ return {
284
+ inbox: mailboxes[at],
285
+ rest: mailboxes.filter((_, index) => index !== at),
286
+ };
287
+ };
288
+
289
+ /**
290
+ * Emit INBOX's SYNC_MESSAGES event and wait for it before emitting the rest.
291
+ *
292
+ * Every event of an account shares one FIFO group (`MessageGroupId =
293
+ * accountId`), so the queue serves them strictly in arrival order. Emitting
294
+ * the whole list through `pMap` only bounds concurrency — it races the writes,
295
+ * and INBOX could land behind up to `EVENT_EMIT_CONCURRENCY - 1` other
296
+ * folders. The mail a person pressed refresh for then arrived only after every
297
+ * one of those folders had finished syncing.
298
+ */
299
+ export const emitSyncMessagesEvents = async (
300
+ accountId: string,
301
+ mailboxes: readonly { mailboxId: string; fullPath: string }[],
302
+ emit: (event: SyncMessagesInput) => Promise<unknown>,
303
+ ): Promise<void> => {
304
+ const eventFor = (mailboxId: string): SyncMessagesInput => ({
305
+ type: "SYNC_MESSAGES",
306
+ accountId,
307
+ mailboxId,
308
+ });
309
+
310
+ const { inbox, rest } = splitInboxFirst(mailboxes);
311
+ if (inbox) await emit(eventFor(inbox.mailboxId));
312
+ await pMap(rest, ({ mailboxId }) => emit(eventFor(mailboxId)), {
313
+ concurrency: EVENT_EMIT_CONCURRENCY,
314
+ });
281
315
  };
282
316
 
283
317
  type MailboxSortEntry = {