@remit/imap-worker 0.0.65 → 0.0.66

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.65",
3
+ "version": "0.0.66",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
@@ -229,6 +229,25 @@ describe("handleMessageCopy", () => {
229
229
  assert.equal(called("threadMessage.update").length, 1);
230
230
  });
231
231
 
232
+ // Issue #1122. `deriveCopyMessageId` reuses the row, so copying the same
233
+ // message into the same folder twice issues a second server COPY and leaves
234
+ // the destination holding both. Nothing here can rule the older one out the
235
+ // way the trash probe's source check does — a COPY leaves the source
236
+ // untouched — so the row must settle on the highest match. Settling on 12
237
+ // points it at the earlier copy and orphans the one just made.
238
+ it("settles on the copy this event made, not on an earlier copy of the same message", async () => {
239
+ h.connection.copyMessages = async () => ({ uidMap: new Map() });
240
+ h.destinationHolds = [12, 42];
241
+
242
+ await handleMessageCopy(event, noopLog, 1, deps());
243
+
244
+ assert.deepEqual(called("message.updateUid")[0]?.args, [
245
+ "new-msg",
246
+ 42,
247
+ "dst-mbx",
248
+ ]);
249
+ });
250
+
232
251
  it("reconciles the optimistic row away when the destination does not hold the copy", async () => {
233
252
  h.connection.copyMessages = async () => ({ uidMap: new Map() });
234
253
  h.destinationHolds = [];
@@ -16,7 +16,7 @@ import type { MessageCopyEvent } from "../events.js";
16
16
  import { isNotFoundError } from "../is-not-found.js";
17
17
  import { withOAuthLifecycle } from "../with-oauth-lifecycle.js";
18
18
  import { buildLifecycleDeps } from "../with-oauth-lifecycle-deps.js";
19
- import { searchMailboxByMessageId } from "./message-move.js";
19
+ import { searchMailboxForHighestMessageIdUid } from "./message-move.js";
20
20
 
21
21
  export interface MessageCopyDeps {
22
22
  getClient: typeof getClient;
@@ -188,11 +188,18 @@ export const handleMessageCopy = async (
188
188
  // handle: a `guardConnectionCursor` wrap is bound to the SOURCE mailbox
189
189
  // snapshot alone, and opening the destination through it trips that
190
190
  // mailbox into a rebuild.
191
+ //
192
+ // This is the call site with no source-side gate to fall back on
193
+ // (#1122): a COPY leaves the source untouched, so nothing here can rule
194
+ // an older destination copy out the way `confirmTrashMoveUid` does. The
195
+ // fresh copy is the highest matching uid, and `deriveCopyMessageId`
196
+ // makes a repeated copy of the same message a second server COPY, so
197
+ // this probe must never bind to the earlier one.
191
198
  const probeDestination = async (
192
199
  connection: IImapConnection,
193
200
  ): Promise<CopyProbe> => {
194
201
  if (!copyRow.messageIdHeader) return { kind: "unprobeable" };
195
- const probedUid = await searchMailboxByMessageId(
202
+ const probedUid = await searchMailboxForHighestMessageIdUid(
196
203
  connection,
197
204
  destinationMailboxPath,
198
205
  copyRow.messageIdHeader,
@@ -516,6 +516,30 @@ describe("handleMessageDelete", () => {
516
516
  );
517
517
  });
518
518
 
519
+ // Issue #1122. Trash already holds an older copy of this Message-ID (a
520
+ // resend, a sieve `fileinto` + `keep`, an earlier delete of another
521
+ // folder's copy). Settling on uid 12 points the row at that copy and
522
+ // orphans the one this MOVE just delivered: Empty Trash then expunges by
523
+ // the wrong uid and the fresh copy survives, unreachable.
524
+ it("settles on the copy this move delivered, not on an older copy already in Trash", async () => {
525
+ h.connection.moveMessages = async () => ({ uidMap: new Map() });
526
+ sourceNoLongerHoldsTheUid();
527
+ h.destinationSearchUids = [12, 77];
528
+
529
+ await handleMessageDelete(moveEvent, noopLog, 1, deps());
530
+
531
+ assert.deepEqual(called("message.updateUid")[0]?.args, [
532
+ "msg-1",
533
+ 77,
534
+ "trash-mbx",
535
+ ]);
536
+ assert.deepEqual(called("threadMessage.update")[0]?.args[2], {
537
+ uid: 77,
538
+ mailboxId: "trash-mbx",
539
+ isDeleted: true,
540
+ });
541
+ });
542
+
519
543
  it("probes the DESTINATION mailbox, read-only, by Message-ID", async () => {
520
544
  h.connection.moveMessages = async () => ({ uidMap: new Map() });
521
545
  sourceNoLongerHoldsTheUid();
@@ -650,11 +674,11 @@ describe("handleMessageDelete", () => {
650
674
  assert.equal(called("emitEvent").length, 0);
651
675
  });
652
676
 
653
- // `searchMailboxByMessageId` returns the LOWEST matching uid, and one
654
- // Message-ID can have several server copies in one account while
677
+ // One Message-ID can have several server copies in one account while
655
678
  // `deriveMessageId` gives them one local row. A source that still holds
656
679
  // the uid proves the MOVE did not happen, so any hit at the destination
657
- // is a different copy.
680
+ // is a different copy — including the highest one #1122 now takes, since
681
+ // with no fresh copy at all the highest match is still an older copy.
658
682
  it("never takes a destination hit while the source still holds the uid (duplicate Message-ID)", async () => {
659
683
  h.connection.moveMessages = async () => ({ uidMap: new Map() });
660
684
  h.destinationSearchUids = [100];
@@ -23,7 +23,10 @@ import { isNotFoundError } from "../is-not-found.js";
23
23
  import { withOAuthLifecycle } from "../with-oauth-lifecycle.js";
24
24
  import { buildLifecycleDeps } from "../with-oauth-lifecycle-deps.js";
25
25
  import { resolveExhaustedMessageDeleteFailure } from "./message-delete-terminal.js";
26
- import { emitMoveResync, searchMailboxByMessageId } from "./message-move.js";
26
+ import {
27
+ emitMoveResync,
28
+ searchMailboxForHighestMessageIdUid,
29
+ } from "./message-move.js";
27
30
  import {
28
31
  buildThreadMessageMoveRevert,
29
32
  buildThreadMessageTrashUpdate,
@@ -70,14 +73,14 @@ export const deleteAllThreadMessagesForMessage = async (
70
73
  * never be opened through the source's guard.
71
74
  *
72
75
  * The source is asked first, and a source that still holds the uid ends it: the
73
- * MOVE did not happen, so nothing at the destination can be this message.
74
- * Skipping that question is not safe, because `searchMailboxByMessageId`
75
- * returns the LOWEST matching uid rather than the one that just arrived, and
76
- * one Message-ID can have several server copies in one account (a sieve
77
- * `fileinto` + `keep`, a multi-label store, a resend) while
78
- * `deriveMessageId` is folder-independent and gives them one local row. An
79
- * ungated probe can hand back an earlier copy's uid, and Empty Trash then
80
- * expunges by that uid. It also closes the second half of #912: an empty
76
+ * MOVE did not happen, so nothing at the destination can be this message. The
77
+ * gate survives #1122's switch to the highest matching uid and is not made
78
+ * redundant by it — highest picks the fresh copy out of several only once a
79
+ * fresh copy exists, and where the MOVE never ran every hit at the destination
80
+ * is an older copy of the same Message-ID (a sieve `fileinto` + `keep`, a
81
+ * multi-label store, a resend) that `deriveMessageId` folds into this one local
82
+ * row. An ungated probe hands back that copy's uid, and Empty Trash then
83
+ * expunges by it. The gate also closes the second half of #912: an empty
81
84
  * `uidMap` can mean the MOVE matched nothing at all.
82
85
  *
83
86
  * A row with no `messageIdHeader`, and a row that is already deleted, have
@@ -109,7 +112,7 @@ const confirmTrashMoveUid = async (
109
112
  if (!message) return { outcome: "row-gone" };
110
113
  if (!message.messageIdHeader) return { outcome: "unprobeable" };
111
114
 
112
- const probedUid = await searchMailboxByMessageId(
115
+ const probedUid = await searchMailboxForHighestMessageIdUid(
113
116
  destinationConnection,
114
117
  destinationMailboxPath,
115
118
  message.messageIdHeader,
@@ -12,7 +12,7 @@ import {
12
12
  handleMessageMove,
13
13
  MESSAGE_MOVE_MAX_ATTEMPTS,
14
14
  moveThenResync,
15
- searchMailboxByMessageId,
15
+ searchMailboxForHighestMessageIdUid,
16
16
  } from "./message-move.js";
17
17
 
18
18
  const silentLogger = (() => {
@@ -343,7 +343,7 @@ describe("handleMessageMove — the move's own pending state gates every attempt
343
343
  });
344
344
  });
345
345
 
346
- describe("searchMailboxByMessageId — the probe that binds a move to a UID (#912)", () => {
346
+ describe("searchMailboxForHighestMessageIdUid — the probe that binds a move to a UID (#912)", () => {
347
347
  const isMessageIdCriterion = (
348
348
  criterion: unknown,
349
349
  ): criterion is [string, string, string] =>
@@ -381,7 +381,7 @@ describe("searchMailboxByMessageId — the probe that binds a move to a UID (#91
381
381
  },
382
382
  } as unknown as IImapConnection;
383
383
 
384
- await searchMailboxByMessageId(
384
+ await searchMailboxForHighestMessageIdUid(
385
385
  destination,
386
386
  "Archive",
387
387
  '<a"b@example.com>\r\nUID 1',
@@ -392,14 +392,14 @@ describe("searchMailboxByMessageId — the probe that binds a move to a UID (#91
392
392
  ]);
393
393
  });
394
394
 
395
- it("answers null, not the lowest UID, when the folder holds no such message", async () => {
395
+ it("answers null, not another message's UID, when the folder holds no such message", async () => {
396
396
  const destination = buildDestination([
397
397
  { uid: 11, messageIdHeader: "<stranger-a@example.com>" },
398
398
  { uid: 12, messageIdHeader: "<stranger-b@example.com>" },
399
399
  ]);
400
400
 
401
401
  assert.strictEqual(
402
- await searchMailboxByMessageId(
402
+ await searchMailboxForHighestMessageIdUid(
403
403
  destination,
404
404
  "Archive",
405
405
  "<moved@example.com>",
@@ -415,7 +415,7 @@ describe("searchMailboxByMessageId — the probe that binds a move to a UID (#91
415
415
  ]);
416
416
 
417
417
  assert.strictEqual(
418
- await searchMailboxByMessageId(
418
+ await searchMailboxForHighestMessageIdUid(
419
419
  destination,
420
420
  "Archive",
421
421
  "<moved@example.com>",
@@ -423,4 +423,43 @@ describe("searchMailboxByMessageId — the probe that binds a move to a UID (#91
423
423
  12,
424
424
  );
425
425
  });
426
+
427
+ // Issue #1122. The folder already held an older copy of this Message-ID — a
428
+ // sieve `fileinto` + `keep`, a resend, an earlier copy of the same message —
429
+ // and the uid this probe hands back settles the row the fresh copy lives in.
430
+ // Answering 12 binds that row to mail this operation never touched and
431
+ // leaves the copy that just arrived unreachable by any later delete.
432
+ it("answers the newest copy's UID when the folder already held an older copy of it", async () => {
433
+ const destination = buildDestination([
434
+ { uid: 12, messageIdHeader: "<moved@example.com>" },
435
+ { uid: 40, messageIdHeader: "<moved@example.com>" },
436
+ ]);
437
+
438
+ assert.strictEqual(
439
+ await searchMailboxForHighestMessageIdUid(
440
+ destination,
441
+ "Archive",
442
+ "<moved@example.com>",
443
+ ),
444
+ 40,
445
+ );
446
+ });
447
+
448
+ // RFC 3501 does not order a SEARCH response, so the fresh copy is the
449
+ // highest uid returned rather than the last one returned.
450
+ it("takes the highest match whatever order the server lists them in", async () => {
451
+ const destination = buildDestination([
452
+ { uid: 40, messageIdHeader: "<moved@example.com>" },
453
+ { uid: 12, messageIdHeader: "<moved@example.com>" },
454
+ ]);
455
+
456
+ assert.strictEqual(
457
+ await searchMailboxForHighestMessageIdUid(
458
+ destination,
459
+ "Archive",
460
+ "<moved@example.com>",
461
+ ),
462
+ 40,
463
+ );
464
+ });
426
465
  });
@@ -53,11 +53,28 @@ export const emitMoveResync = async (
53
53
  };
54
54
 
55
55
  /**
56
- * SEARCH a mailbox for a message by its RFC822 Message-ID header. Read-only
57
- * (EXAMINE, not SELECT) — this is a verification probe, never a write.
58
- * Returns the first matching UID, or `null` if nothing matched.
56
+ * SEARCH a mailbox for copies of a message identified by its RFC822 Message-ID
57
+ * header and answer the HIGHEST matching uid. Read-only (EXAMINE, not SELECT) —
58
+ * this is a verification probe, never a write. `null` when nothing matched.
59
+ *
60
+ * The highest uid is the freshly delivered copy only when one exists; deciding
61
+ * that it does is the caller's job, not this function's. Every probe in this
62
+ * package wants the highest: uids ascend with arrival inside a UIDVALIDITY, so a
63
+ * copy that has just landed outranks every copy of the same Message-ID the
64
+ * folder already held. Where nothing was delivered, the highest is simply the
65
+ * newest pre-existing copy, and a caller that has not ruled that case out binds
66
+ * itself to unrelated mail.
67
+ * One Message-ID can have several server copies in one account (a sieve
68
+ * `fileinto` + `keep`, a resend, a repeated COPY — `deriveCopyMessageId` reuses
69
+ * the row, so the second copy is a second server COPY), while `deriveMessageId`
70
+ * is folder-independent and gives them one local row. Taking the lowest bound
71
+ * the fresh copy's row to an older copy's uid and orphaned the fresh one, which
72
+ * a later delete or Empty Trash then expunged by the wrong uid (issue #1122).
73
+ *
74
+ * The maximum is computed rather than read off the tail: RFC 3501 leaves the
75
+ * order of a SEARCH response unspecified, so "last returned" is not "highest".
59
76
  */
60
- export const searchMailboxByMessageId = async (
77
+ export const searchMailboxForHighestMessageIdUid = async (
61
78
  connection: IImapConnection,
62
79
  mailboxPath: string,
63
80
  messageIdHeader: string,
@@ -66,7 +83,8 @@ export const searchMailboxByMessageId = async (
66
83
  const uids = await connection.search([
67
84
  ["HEADER", "Message-ID", messageIdHeader],
68
85
  ]);
69
- return uids[0] ?? null;
86
+ if (uids.length === 0) return null;
87
+ return uids.reduce((highest, uid) => (uid > highest ? uid : highest));
70
88
  };
71
89
 
72
90
  /**
@@ -273,7 +291,7 @@ export const handleMessageMove = async (
273
291
  const newUid =
274
292
  result.uidMap.get(uid) ??
275
293
  (message.messageIdHeader
276
- ? await searchMailboxByMessageId(
294
+ ? await searchMailboxForHighestMessageIdUid(
277
295
  rawConnection,
278
296
  destinationMailboxPath,
279
297
  message.messageIdHeader,
@@ -156,6 +156,28 @@ describe("attemptMove — the IMAP push (issue #1271)", () => {
156
156
  assert.equal(outcome.newUid, 77);
157
157
  });
158
158
 
159
+ // Issue #1122. The destination already held an older copy of this
160
+ // Message-ID; the marker must bind to the copy this MOVE delivered, or
161
+ // the placement row names mail this push never touched.
162
+ it("moved: binds to the newest copy when the destination already held an older one", async () => {
163
+ const connection = buildConnection({
164
+ uidMap: new Map(),
165
+ destinationSearchUids: [15, 77],
166
+ });
167
+
168
+ const outcome = await attemptMove(
169
+ connection,
170
+ connection,
171
+ "Junk",
172
+ "INBOX",
173
+ 42,
174
+ MESSAGE_ID_HEADER,
175
+ );
176
+
177
+ assert.equal(outcome.kind, "moved");
178
+ assert.equal(outcome.newUid, 77);
179
+ });
180
+
159
181
  it("throws (never deletes) when unconfirmed at the destination but STILL present at the source", async () => {
160
182
  const connection = buildConnection({
161
183
  uidMap: new Map(),
@@ -21,7 +21,7 @@ import { buildLifecycleDeps } from "../with-oauth-lifecycle-deps.js";
21
21
  import {
22
22
  buildThreadMessageMoveUpdate,
23
23
  emitMoveResync,
24
- searchMailboxByMessageId,
24
+ searchMailboxForHighestMessageIdUid,
25
25
  } from "./message-move.js";
26
26
 
27
27
  export const getPlacementMoveMaxAttempts = (
@@ -97,9 +97,16 @@ export const attemptMove = async (
97
97
  }
98
98
 
99
99
  // Unconfirmed: either no COPYUID entry, or the server explicitly claimed
100
- // "not found" — never trust either without independent verification.
100
+ // "not found" — never trust either without independent verification. A
101
+ // message this MOVE delivered is the newest copy of that Message-ID at the
102
+ // destination, so the probe binds to the highest matching uid (#1122).
103
+ // Unlike `confirmTrashMoveUid`, this probe is ungated: where the MOVE never
104
+ // ran and an older copy filed there earlier is the only match, the marker
105
+ // binds to that unrelated mail. Pre-existing, and not narrowed by taking the
106
+ // highest — the source is not asked first here, so nothing rules the case
107
+ // out.
101
108
  if (messageIdHeader) {
102
- const destinationUid = await searchMailboxByMessageId(
109
+ const destinationUid = await searchMailboxForHighestMessageIdUid(
103
110
  destinationConnection,
104
111
  destinationMailboxPath,
105
112
  messageIdHeader,