@remit/mailbox-service 0.0.112 → 0.0.113
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/dist/account-credentials.d.ts +83 -0
- package/dist/account-credentials.js +104 -0
- package/dist/attribute-mapper.d.ts +36 -0
- package/dist/attribute-mapper.js +119 -0
- package/dist/body-parse.d.ts +28 -0
- package/{src/body-parse.ts → dist/body-parse.js} +18 -26
- package/dist/body-part-mapper.d.ts +92 -0
- package/dist/body-part-mapper.js +301 -0
- package/dist/body-sync-queue.d.ts +59 -0
- package/dist/body-sync-queue.js +56 -0
- package/dist/body-sync-terminal.d.ts +58 -0
- package/dist/body-sync-terminal.js +95 -0
- package/dist/body-sync.d.ts +351 -0
- package/dist/body-sync.js +1204 -0
- package/dist/calendar-parts.d.ts +23 -0
- package/dist/calendar-parts.js +18 -0
- package/dist/classification-backfill.d.ts +91 -0
- package/dist/classification-backfill.js +180 -0
- package/dist/classify-message.d.ts +31 -0
- package/dist/classify-message.js +89 -0
- package/dist/connection-factory.d.ts +54 -0
- package/dist/connection-factory.js +54 -0
- package/dist/connection-test.d.ts +41 -0
- package/dist/connection-test.js +152 -0
- package/dist/external-move.d.ts +94 -0
- package/dist/external-move.js +173 -0
- package/dist/filter-operations.d.ts +42 -0
- package/dist/filter-operations.js +211 -0
- package/dist/filters/filter-config.d.ts +14 -0
- package/dist/filters/filter-config.js +10 -0
- package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
- package/dist/filters/list-id.js +26 -0
- package/dist/filters/match.d.ts +69 -0
- package/dist/filters/match.js +165 -0
- package/dist/filters/pipeline.d.ts +64 -0
- package/dist/filters/pipeline.js +100 -0
- package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
- package/dist/flag-push-terminal.js +63 -0
- package/dist/flag-push.d.ts +69 -0
- package/dist/flag-push.js +111 -0
- package/dist/flag-queue.d.ts +175 -0
- package/dist/flag-queue.js +291 -0
- package/dist/folder-operations.d.ts +39 -0
- package/dist/folder-operations.js +112 -0
- package/dist/folder-placement.d.ts +13 -0
- package/dist/folder-placement.js +23 -0
- package/dist/heuristics/authenticationResults.d.ts +10 -0
- package/dist/heuristics/authenticationResults.js +187 -0
- package/dist/heuristics/classifyByHeaders.d.ts +82 -0
- package/dist/heuristics/classifyByHeaders.js +365 -0
- package/dist/heuristics/classifyPlacement.d.ts +67 -0
- package/dist/heuristics/classifyPlacement.js +153 -0
- package/dist/heuristics/machineSenders.d.ts +21 -0
- package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
- package/dist/heuristics/senderMismatch.d.ts +81 -0
- package/dist/heuristics/senderMismatch.js +192 -0
- package/dist/heuristics/socialDomains.d.ts +14 -0
- package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
- package/dist/heuristics/transactionalDomains.d.ts +18 -0
- package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
- package/dist/imapflow-connection.d.ts +377 -0
- package/dist/imapflow-connection.js +1147 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +65 -0
- package/dist/list-id-backfill.d.ts +83 -0
- package/dist/list-id-backfill.js +132 -0
- package/dist/mailbox-cursor-rebuild.d.ts +74 -0
- package/dist/mailbox-cursor-rebuild.js +71 -0
- package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
- package/dist/mailbox-cursor.js +102 -0
- package/dist/mailbox-intent.d.ts +41 -0
- package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
- package/dist/mailbox-management.d.ts +240 -0
- package/dist/mailbox-management.js +576 -0
- package/dist/mailbox-presence.d.ts +52 -0
- package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
- package/dist/mailbox-queue.d.ts +126 -0
- package/dist/mailbox-queue.js +214 -0
- package/dist/mailbox-sync.d.ts +93 -0
- package/dist/mailbox-sync.js +490 -0
- package/dist/mailbox-upstream.d.ts +38 -0
- package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
- package/dist/message-category.d.ts +51 -0
- package/{src/message-category.ts → dist/message-category.js} +28 -71
- package/dist/message-move.d.ts +285 -0
- package/dist/message-move.js +858 -0
- package/dist/message-parser.d.ts +14 -0
- package/dist/message-parser.js +17 -0
- package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
- package/dist/message-presence.js +29 -0
- package/dist/message-read.d.ts +51 -0
- package/dist/message-read.js +31 -0
- package/dist/message-sync.d.ts +345 -0
- package/dist/message-sync.js +1358 -0
- package/dist/mime-walker.d.ts +102 -0
- package/dist/mime-walker.js +198 -0
- package/dist/organize-job.d.ts +20 -0
- package/dist/organize-job.js +37 -0
- package/dist/outbox-attachment-content.d.ts +37 -0
- package/dist/outbox-attachment-content.js +50 -0
- package/dist/outbox-attachment-filename.d.ts +19 -0
- package/dist/outbox-attachment-filename.js +64 -0
- package/dist/outbox-attachment.d.ts +148 -0
- package/dist/outbox-attachment.js +273 -0
- package/dist/outbox-queue.d.ts +73 -0
- package/dist/outbox-queue.js +284 -0
- package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
- package/dist/outbox-status.js +18 -0
- package/dist/ownership.d.ts +24 -0
- package/dist/ownership.js +83 -0
- package/dist/placement-move-terminal.d.ts +67 -0
- package/dist/placement-move-terminal.js +88 -0
- package/dist/placement-move.d.ts +83 -0
- package/dist/placement-move.js +216 -0
- package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
- package/dist/placement-settled.js +75 -0
- package/dist/quarantine.d.ts +95 -0
- package/dist/quarantine.js +131 -0
- package/dist/refusal.d.ts +31 -0
- package/dist/refusal.js +3 -0
- package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
- package/dist/restore-source-placement.js +79 -0
- package/dist/search-text-backfill.d.ts +15 -0
- package/dist/search-text-backfill.js +33 -0
- package/dist/snippet.d.ts +61 -0
- package/dist/snippet.js +129 -0
- package/dist/spam-report.d.ts +86 -0
- package/dist/spam-report.js +241 -0
- package/dist/sync-warning.d.ts +21 -0
- package/dist/sync-warning.js +26 -0
- package/dist/sync-watermarks.d.ts +138 -0
- package/dist/sync-watermarks.js +205 -0
- package/dist/test-helpers/fake-imap.d.ts +151 -0
- package/dist/test-helpers/fake-imap.js +508 -0
- package/dist/test-helpers/folder-roles.d.ts +34 -0
- package/dist/test-helpers/folder-roles.js +47 -0
- package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
- package/dist/test-helpers/mime-tree-generator.js +329 -0
- package/dist/test-helpers/test-store.d.ts +69 -0
- package/dist/test-helpers/test-store.js +193 -0
- package/dist/text/normalizer.d.ts +20 -0
- package/dist/text/normalizer.js +102 -0
- package/dist/thread-message-rows.d.ts +69 -0
- package/dist/thread-message-rows.js +40 -0
- package/dist/thread-root.d.ts +7 -0
- package/dist/thread-root.js +25 -0
- package/dist/types.d.ts +343 -0
- package/dist/types.js +19 -0
- package/package.json +86 -88
- package/src/account-credentials.ts +0 -210
- package/src/attribute-mapper.test.ts +0 -104
- package/src/attribute-mapper.ts +0 -148
- package/src/body-parse.test.ts +0 -55
- package/src/body-part-mapper.property.test.ts +0 -354
- package/src/body-part-mapper.ts +0 -410
- package/src/body-sync-queue.test.ts +0 -64
- package/src/body-sync-queue.ts +0 -131
- package/src/body-sync-terminal.test.ts +0 -148
- package/src/body-sync-terminal.ts +0 -174
- package/src/body-sync.test.ts +0 -925
- package/src/body-sync.ts +0 -1891
- package/src/calendar-parts.ts +0 -41
- package/src/classification-backfill.test.ts +0 -285
- package/src/classification-backfill.ts +0 -363
- package/src/classify-message.test.ts +0 -142
- package/src/classify-message.ts +0 -116
- package/src/connection-factory.ts +0 -102
- package/src/connection-test.ts +0 -208
- package/src/credentials.test.ts +0 -181
- package/src/external-move.test.ts +0 -262
- package/src/external-move.ts +0 -285
- package/src/filter-operations.test.ts +0 -319
- package/src/filter-operations.ts +0 -377
- package/src/filter-update.test.ts +0 -313
- package/src/filters/filter-config.test.ts +0 -87
- package/src/filters/filter-config.ts +0 -34
- package/src/filters/list-id.test.ts +0 -50
- package/src/filters/match.test.ts +0 -392
- package/src/filters/match.ts +0 -225
- package/src/filters/pipeline.test.ts +0 -199
- package/src/filters/pipeline.ts +0 -170
- package/src/flag-push.test.ts +0 -112
- package/src/flag-push.ts +0 -198
- package/src/flag-queue.test.ts +0 -197
- package/src/flag-queue.ts +0 -509
- package/src/folder-operations.test.ts +0 -273
- package/src/folder-operations.ts +0 -217
- package/src/folder-placement.ts +0 -56
- package/src/forwarded-message.test.ts +0 -122
- package/src/heuristics/authenticationResults.test.ts +0 -189
- package/src/heuristics/authenticationResults.ts +0 -212
- package/src/heuristics/classifyByHeaders.test.ts +0 -889
- package/src/heuristics/classifyByHeaders.ts +0 -467
- package/src/heuristics/classifyPlacement.test.ts +0 -366
- package/src/heuristics/classifyPlacement.ts +0 -211
- package/src/heuristics/classifyRealMail.test.ts +0 -242
- package/src/heuristics/machineSenders.test.ts +0 -101
- package/src/heuristics/senderMismatch.test.ts +0 -346
- package/src/heuristics/senderMismatch.ts +0 -259
- package/src/imapflow-connect-retry.test.ts +0 -97
- package/src/imapflow-connection.test.ts +0 -503
- package/src/imapflow-connection.ts +0 -1476
- package/src/index.ts +0 -397
- package/src/list-id-backfill.test.ts +0 -207
- package/src/list-id-backfill.ts +0 -274
- package/src/mailbox-cursor-rebuild.test.ts +0 -171
- package/src/mailbox-cursor-rebuild.ts +0 -146
- package/src/mailbox-cursor.test.ts +0 -141
- package/src/mailbox-delete-intent.test.ts +0 -119
- package/src/mailbox-management.test.ts +0 -243
- package/src/mailbox-management.ts +0 -838
- package/src/mailbox-presence.test.ts +0 -74
- package/src/mailbox-queue.ts +0 -397
- package/src/mailbox-rename-intent.test.ts +0 -220
- package/src/mailbox-rename-subtree.test.ts +0 -287
- package/src/mailbox-sync.test.ts +0 -286
- package/src/mailbox-sync.ts +0 -724
- package/src/message-move.test.ts +0 -726
- package/src/message-move.ts +0 -1518
- package/src/message-parser.ts +0 -26
- package/src/message-presence.test.ts +0 -50
- package/src/message-read.ts +0 -87
- package/src/message-save-retry.test.ts +0 -90
- package/src/message-sync-flags.test.ts +0 -167
- package/src/message-sync.test.ts +0 -762
- package/src/message-sync.ts +0 -2133
- package/src/mime-walker.test.ts +0 -311
- package/src/mime-walker.ts +0 -306
- package/src/organize-job.ts +0 -93
- package/src/outbox-attachment-content.test.ts +0 -153
- package/src/outbox-attachment-content.ts +0 -104
- package/src/outbox-attachment-filename.test.ts +0 -132
- package/src/outbox-attachment-filename.ts +0 -76
- package/src/outbox-attachment.test.ts +0 -626
- package/src/outbox-attachment.ts +0 -536
- package/src/outbox-failed-recovery.test.ts +0 -299
- package/src/outbox-queue.test.ts +0 -315
- package/src/outbox-queue.ts +0 -525
- package/src/ownership.ts +0 -140
- package/src/placement-move-terminal.ts +0 -145
- package/src/placement-move.ts +0 -363
- package/src/placement-settled.test.ts +0 -147
- package/src/placement.test.ts +0 -404
- package/src/quarantine.test.ts +0 -133
- package/src/quarantine.ts +0 -229
- package/src/refusal.ts +0 -29
- package/src/snippet.test.ts +0 -213
- package/src/snippet.ts +0 -152
- package/src/spam-report.test.ts +0 -329
- package/src/spam-report.ts +0 -357
- package/src/sync-warning.test.ts +0 -213
- package/src/sync-warning.ts +0 -56
- package/src/sync-watermarks.test.ts +0 -409
- package/src/sync-watermarks.ts +0 -319
- package/src/system-flag-wire-format.test.ts +0 -77
- package/src/test-helpers/fake-imap.ts +0 -690
- package/src/test-helpers/folder-roles.ts +0 -66
- package/src/test-helpers/mime-tree-generator.ts +0 -461
- package/src/test-helpers/test-queue.ts +0 -56
- package/src/test-helpers/test-store.ts +0 -299
- package/src/text/normalizer.test.ts +0 -223
- package/src/text/normalizer.ts +0 -140
- package/src/thread-message-rows.ts +0 -77
- package/src/thread-root.test.ts +0 -29
- package/src/thread-root.ts +0 -51
- package/src/types/mailparser-augment.d.ts +0 -23
- package/src/types/natural-porter-stemmer.d.ts +0 -50
- package/src/types/stopword.d.ts +0 -69
- package/src/types.ts +0 -371
- package/tsconfig.json +0 -8
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
3
|
-
import type { AccountItem, MailboxItem } from "@remit/data-ports";
|
|
4
|
-
import type { StorageService } from "@remit/storage-service";
|
|
5
|
-
import {
|
|
6
|
-
isMessageBodySyncBroken,
|
|
7
|
-
resolveExhaustedBodySyncFailures,
|
|
8
|
-
} from "./body-sync-terminal.js";
|
|
9
|
-
import { eml, FakeImapServer } from "./test-helpers/fake-imap.js";
|
|
10
|
-
import {
|
|
11
|
-
messageAtUid,
|
|
12
|
-
openStore,
|
|
13
|
-
type Store,
|
|
14
|
-
seedAccount,
|
|
15
|
-
seedMailbox,
|
|
16
|
-
silentLog,
|
|
17
|
-
syncFolder,
|
|
18
|
-
} from "./test-helpers/test-store.js";
|
|
19
|
-
|
|
20
|
-
let store: Store;
|
|
21
|
-
let storage: StorageService;
|
|
22
|
-
let imap: FakeImapServer;
|
|
23
|
-
let account: AccountItem;
|
|
24
|
-
let inbox: MailboxItem;
|
|
25
|
-
|
|
26
|
-
beforeEach(async () => {
|
|
27
|
-
store = await openStore();
|
|
28
|
-
storage = store.storage();
|
|
29
|
-
imap = new FakeImapServer();
|
|
30
|
-
account = await seedAccount(store);
|
|
31
|
-
inbox = await seedMailbox(store, account, "INBOX");
|
|
32
|
-
imap.addFolder("INBOX");
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
afterEach(() => store.close());
|
|
36
|
-
|
|
37
|
-
const arrive = async (subject: string) => {
|
|
38
|
-
const uid = imap.deliver("INBOX", eml({ Subject: subject }));
|
|
39
|
-
await syncFolder(store, account, inbox, imap);
|
|
40
|
-
return { uid, messageId: (await messageAtUid(store, inbox, uid)).messageId };
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const resolve = (failedMessageIds: string[], connection = imap.connection()) =>
|
|
44
|
-
resolveExhaustedBodySyncFailures(
|
|
45
|
-
{
|
|
46
|
-
messageService: store.message,
|
|
47
|
-
storageService: storage,
|
|
48
|
-
log: silentLog,
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
accountId: account.accountId,
|
|
52
|
-
accountConfigId: account.accountConfigId,
|
|
53
|
-
mailboxId: inbox.mailboxId,
|
|
54
|
-
mailboxPath: "INBOX",
|
|
55
|
-
failedMessageIds,
|
|
56
|
-
getConnection: async () => connection,
|
|
57
|
-
},
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
const isBroken = (messageId: string) =>
|
|
61
|
-
isMessageBodySyncBroken(
|
|
62
|
-
storage,
|
|
63
|
-
account.accountConfigId,
|
|
64
|
-
account.accountId,
|
|
65
|
-
messageId,
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
describe("a body sync that exhausted its retries", () => {
|
|
69
|
-
it("removes the row and every thread copy of a message the server expunged", async () => {
|
|
70
|
-
const { uid, messageId } = await arrive("gone");
|
|
71
|
-
const [row] = await store.threadMessage.findAllByMessageId(
|
|
72
|
-
account.accountConfigId,
|
|
73
|
-
messageId,
|
|
74
|
-
);
|
|
75
|
-
await store.threadMessage.create({ ...row, threadId: "drifted-root" });
|
|
76
|
-
imap.expunge("INBOX", uid);
|
|
77
|
-
|
|
78
|
-
const result = await resolve([messageId]);
|
|
79
|
-
|
|
80
|
-
assert.deepEqual(result, {
|
|
81
|
-
reconciledMessageIds: [messageId],
|
|
82
|
-
brokenMessageIds: [],
|
|
83
|
-
});
|
|
84
|
-
await assert.rejects(store.message.get(messageId));
|
|
85
|
-
assert.deepEqual(
|
|
86
|
-
await store.threadMessage.findAllByMessageId(
|
|
87
|
-
account.accountConfigId,
|
|
88
|
-
messageId,
|
|
89
|
-
),
|
|
90
|
-
[],
|
|
91
|
-
);
|
|
92
|
-
assert.equal(await isBroken(messageId), false);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("marks a message the server still holds as broken and keeps its row", async () => {
|
|
96
|
-
const { messageId } = await arrive("broken");
|
|
97
|
-
|
|
98
|
-
const result = await resolve([messageId]);
|
|
99
|
-
|
|
100
|
-
assert.deepEqual(result.brokenMessageIds, [messageId]);
|
|
101
|
-
assert.ok(await store.message.get(messageId));
|
|
102
|
-
assert.equal(await isBroken(messageId), true);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it("does not read a dropped FETCH row as absence", async () => {
|
|
106
|
-
const first = await arrive("first");
|
|
107
|
-
const second = await arrive("second");
|
|
108
|
-
const connection = imap.connection();
|
|
109
|
-
const fetch = connection.fetchMessages;
|
|
110
|
-
connection.fetchMessages = async (uids) =>
|
|
111
|
-
uids.includes(second.uid) ? [] : fetch(uids);
|
|
112
|
-
|
|
113
|
-
const result = await resolve(
|
|
114
|
-
[first.messageId, second.messageId],
|
|
115
|
-
connection,
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
assert.deepEqual(result.reconciledMessageIds, []);
|
|
119
|
-
assert.deepEqual(result.brokenMessageIds, [
|
|
120
|
-
first.messageId,
|
|
121
|
-
second.messageId,
|
|
122
|
-
]);
|
|
123
|
-
assert.ok(await store.message.get(second.messageId));
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it("never opens a connection when nothing failed", async () => {
|
|
127
|
-
const result = await resolveExhaustedBodySyncFailures(
|
|
128
|
-
{
|
|
129
|
-
messageService: store.message,
|
|
130
|
-
storageService: storage,
|
|
131
|
-
log: silentLog,
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
accountId: account.accountId,
|
|
135
|
-
accountConfigId: account.accountConfigId,
|
|
136
|
-
mailboxId: inbox.mailboxId,
|
|
137
|
-
mailboxPath: "INBOX",
|
|
138
|
-
failedMessageIds: [],
|
|
139
|
-
getConnection: async () => assert.fail("no connection is needed"),
|
|
140
|
-
},
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
assert.deepEqual(result, {
|
|
144
|
-
reconciledMessageIds: [],
|
|
145
|
-
brokenMessageIds: [],
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
});
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import type { IMessageRepository } from "@remit/data-ports";
|
|
2
|
-
import type { StorageService } from "@remit/storage-service";
|
|
3
|
-
import type { BodySyncLogger } from "./body-sync.js";
|
|
4
|
-
import { isMessageGoneFromOpenMailbox } from "./message-presence.js";
|
|
5
|
-
import type { IImapConnection } from "./types.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Body-part sentinel path a permanently-broken message's failure context is
|
|
9
|
-
* written under, mirroring the `.materialized` deferred-parts sentinel in
|
|
10
|
-
* `body-sync.ts`. Using the existing per-message body-part storage (instead
|
|
11
|
-
* of a new Message-row column) means no schema change: the read path checks
|
|
12
|
-
* for it with the same `bodyPartExists` HEAD it already issues.
|
|
13
|
-
*/
|
|
14
|
-
const BODY_SYNC_FAILED_MARKER_PATH = ".sync-failed";
|
|
15
|
-
|
|
16
|
-
export interface ResolveExhaustedBodySyncDeps {
|
|
17
|
-
messageService: Pick<IMessageRepository, "get" | "deleteIfPlacement">;
|
|
18
|
-
storageService: Pick<StorageService, "storeBodyPart" | "bodyPartExists">;
|
|
19
|
-
log: BodySyncLogger;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface ResolveExhaustedBodySyncInput {
|
|
23
|
-
accountId: string;
|
|
24
|
-
accountConfigId: string;
|
|
25
|
-
mailboxId: string;
|
|
26
|
-
mailboxPath: string;
|
|
27
|
-
failedMessageIds: string[];
|
|
28
|
-
getConnection: () => Promise<IImapConnection>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface ResolveExhaustedBodySyncResult {
|
|
32
|
-
/** Outcome 1 (EXPECTED): message gone upstream, row deleted. */
|
|
33
|
-
reconciledMessageIds: string[];
|
|
34
|
-
/** Outcome 2 (BROKEN): message exists but is unfetchable/unparseable. */
|
|
35
|
-
brokenMessageIds: string[];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Whether a message's body-sync has already been marked permanently failed
|
|
40
|
-
* (outcome 2 of {@link resolveExhaustedBodySyncFailures}). The read path
|
|
41
|
-
* calls this before attempting or re-arming any fetch so a client that opens
|
|
42
|
-
* a broken message gets the explicit unrecoverable error immediately instead
|
|
43
|
-
* of looping through another 202 or another IMAP round-trip.
|
|
44
|
-
*/
|
|
45
|
-
export const isMessageBodySyncBroken = (
|
|
46
|
-
storageService: Pick<StorageService, "bodyPartExists">,
|
|
47
|
-
accountConfigId: string,
|
|
48
|
-
accountId: string,
|
|
49
|
-
messageId: string,
|
|
50
|
-
): Promise<boolean> =>
|
|
51
|
-
storageService.bodyPartExists(
|
|
52
|
-
accountConfigId,
|
|
53
|
-
accountId,
|
|
54
|
-
messageId,
|
|
55
|
-
BODY_SYNC_FAILED_MARKER_PATH,
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
const markMessageBodySyncFailed = async (
|
|
59
|
-
storageService: Pick<StorageService, "storeBodyPart">,
|
|
60
|
-
accountConfigId: string,
|
|
61
|
-
accountId: string,
|
|
62
|
-
messageId: string,
|
|
63
|
-
context: Record<string, unknown>,
|
|
64
|
-
): Promise<void> => {
|
|
65
|
-
await storageService.storeBodyPart({
|
|
66
|
-
accountConfigId,
|
|
67
|
-
accountId,
|
|
68
|
-
messageId,
|
|
69
|
-
partPath: BODY_SYNC_FAILED_MARKER_PATH,
|
|
70
|
-
content: Buffer.from(JSON.stringify({ ...context, failedAt: Date.now() })),
|
|
71
|
-
contentType: "application/json",
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Resolve every SYNC_MESSAGE_BODY failure that has exhausted the body
|
|
77
|
-
* queue's redelivery budget (see `BODY_SYNC_MAX_ATTEMPTS` in
|
|
78
|
-
* `sync-message-body.ts`) into exactly one of the two terminal outcomes
|
|
79
|
-
* issue #1270 / epic #1281 invariant 3 require. There is no third, softer
|
|
80
|
-
* outcome — every failed id lands in one of the two result lists.
|
|
81
|
-
*
|
|
82
|
-
* 1. EXPECTED — the message no longer exists on IMAP (expunged, or a
|
|
83
|
-
* UIDVALIDITY change moved it, #1272), confirmed by
|
|
84
|
-
* {@link isMessageGoneFromOpenMailbox}. This loop is the most exposed of
|
|
85
|
-
* the four sites that used to read an empty FETCH as absence: it issues
|
|
86
|
-
* back-to-back single-UID FETCHes on one connection, which is the
|
|
87
|
-
* condition imapflow #408 drops rows under. The stale row is deleted via
|
|
88
|
-
* `deleteIfPlacement` so the existing missing-row 404 path
|
|
89
|
-
* takes over. Callers should emit a metric only — this is routine, not an
|
|
90
|
-
* incident.
|
|
91
|
-
* 2. BROKEN — the message still exists on IMAP but its body could not be
|
|
92
|
-
* fetched or parsed. The failure is persisted as a body-part sentinel
|
|
93
|
-
* (survives across invocations and warm/cold starts) and logged with an
|
|
94
|
-
* `alert`-shaped entry so an operator alarm can key off it. Callers
|
|
95
|
-
* should treat this as loud: the message is unrecoverable until an
|
|
96
|
-
* operator investigates.
|
|
97
|
-
*
|
|
98
|
-
* Neither outcome re-enqueues: both are terminal, so the caller acks the SQS
|
|
99
|
-
* message either way — retrying a stale or broken message can never succeed.
|
|
100
|
-
*/
|
|
101
|
-
export const resolveExhaustedBodySyncFailures = async (
|
|
102
|
-
deps: ResolveExhaustedBodySyncDeps,
|
|
103
|
-
input: ResolveExhaustedBodySyncInput,
|
|
104
|
-
): Promise<ResolveExhaustedBodySyncResult> => {
|
|
105
|
-
const {
|
|
106
|
-
accountId,
|
|
107
|
-
accountConfigId,
|
|
108
|
-
mailboxId,
|
|
109
|
-
mailboxPath,
|
|
110
|
-
failedMessageIds,
|
|
111
|
-
getConnection,
|
|
112
|
-
} = input;
|
|
113
|
-
|
|
114
|
-
const reconciledMessageIds: string[] = [];
|
|
115
|
-
const brokenMessageIds: string[] = [];
|
|
116
|
-
|
|
117
|
-
if (failedMessageIds.length === 0) {
|
|
118
|
-
return { reconciledMessageIds, brokenMessageIds };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const connection = await getConnection();
|
|
122
|
-
await connection.openBox(mailboxPath);
|
|
123
|
-
|
|
124
|
-
for (const messageId of failedMessageIds) {
|
|
125
|
-
const message = await deps.messageService.get(messageId);
|
|
126
|
-
|
|
127
|
-
if (await isMessageGoneFromOpenMailbox(connection, message.uid)) {
|
|
128
|
-
const reconciled = await deps.messageService.deleteIfPlacement(
|
|
129
|
-
messageId,
|
|
130
|
-
{
|
|
131
|
-
status: message.status,
|
|
132
|
-
mailboxId: message.mailboxId,
|
|
133
|
-
uid: message.uid,
|
|
134
|
-
},
|
|
135
|
-
);
|
|
136
|
-
deps.log.info(
|
|
137
|
-
{
|
|
138
|
-
metric: "body_sync_stale_row_reconciled",
|
|
139
|
-
accountId,
|
|
140
|
-
accountConfigId,
|
|
141
|
-
mailboxId,
|
|
142
|
-
messageId,
|
|
143
|
-
uid: message.uid,
|
|
144
|
-
reconciled,
|
|
145
|
-
},
|
|
146
|
-
"Message no longer exists on IMAP after retry exhaustion; stale row reconciled",
|
|
147
|
-
);
|
|
148
|
-
reconciledMessageIds.push(messageId);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
await markMessageBodySyncFailed(
|
|
153
|
-
deps.storageService,
|
|
154
|
-
accountConfigId,
|
|
155
|
-
accountId,
|
|
156
|
-
messageId,
|
|
157
|
-
{ accountId, accountConfigId, mailboxId, messageId, uid: message.uid },
|
|
158
|
-
);
|
|
159
|
-
deps.log.error?.(
|
|
160
|
-
{
|
|
161
|
-
alert: "body_sync_message_broken",
|
|
162
|
-
accountId,
|
|
163
|
-
accountConfigId,
|
|
164
|
-
mailboxId,
|
|
165
|
-
messageId,
|
|
166
|
-
uid: message.uid,
|
|
167
|
-
},
|
|
168
|
-
"Message body could not be fetched/parsed after retry exhaustion; message exists on IMAP but is unrecoverable",
|
|
169
|
-
);
|
|
170
|
-
brokenMessageIds.push(messageId);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return { reconciledMessageIds, brokenMessageIds };
|
|
174
|
-
};
|