@remit/imap-worker 0.0.57 → 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
|
@@ -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
|
|
35
|
-
*
|
|
36
|
-
*
|
|
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
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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 = {
|