@remit/mailbox-service 0.0.112 → 0.0.114

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.
Files changed (270) hide show
  1. package/dist/account-credentials.d.ts +83 -0
  2. package/dist/account-credentials.js +104 -0
  3. package/dist/attribute-mapper.d.ts +36 -0
  4. package/dist/attribute-mapper.js +119 -0
  5. package/dist/body-parse.d.ts +28 -0
  6. package/{src/body-parse.ts → dist/body-parse.js} +18 -26
  7. package/dist/body-part-mapper.d.ts +92 -0
  8. package/dist/body-part-mapper.js +301 -0
  9. package/dist/body-sync-queue.d.ts +59 -0
  10. package/dist/body-sync-queue.js +56 -0
  11. package/dist/body-sync-terminal.d.ts +58 -0
  12. package/dist/body-sync-terminal.js +95 -0
  13. package/dist/body-sync.d.ts +351 -0
  14. package/dist/body-sync.js +1204 -0
  15. package/dist/calendar-parts.d.ts +23 -0
  16. package/dist/calendar-parts.js +18 -0
  17. package/dist/classification-backfill.d.ts +91 -0
  18. package/dist/classification-backfill.js +180 -0
  19. package/dist/classify-message.d.ts +31 -0
  20. package/dist/classify-message.js +89 -0
  21. package/dist/connection-factory.d.ts +54 -0
  22. package/dist/connection-factory.js +54 -0
  23. package/dist/connection-test.d.ts +41 -0
  24. package/dist/connection-test.js +152 -0
  25. package/dist/external-move.d.ts +94 -0
  26. package/dist/external-move.js +173 -0
  27. package/dist/filter-operations.d.ts +42 -0
  28. package/dist/filter-operations.js +211 -0
  29. package/dist/filters/filter-config.d.ts +14 -0
  30. package/dist/filters/filter-config.js +10 -0
  31. package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
  32. package/dist/filters/list-id.js +26 -0
  33. package/dist/filters/match.d.ts +69 -0
  34. package/dist/filters/match.js +165 -0
  35. package/dist/filters/pipeline.d.ts +64 -0
  36. package/dist/filters/pipeline.js +100 -0
  37. package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
  38. package/dist/flag-push-terminal.js +63 -0
  39. package/dist/flag-push.d.ts +69 -0
  40. package/dist/flag-push.js +111 -0
  41. package/dist/flag-queue.d.ts +175 -0
  42. package/dist/flag-queue.js +291 -0
  43. package/dist/folder-operations.d.ts +39 -0
  44. package/dist/folder-operations.js +112 -0
  45. package/dist/folder-placement.d.ts +13 -0
  46. package/dist/folder-placement.js +23 -0
  47. package/dist/heuristics/authenticationResults.d.ts +10 -0
  48. package/dist/heuristics/authenticationResults.js +187 -0
  49. package/dist/heuristics/classifyByHeaders.d.ts +82 -0
  50. package/dist/heuristics/classifyByHeaders.js +365 -0
  51. package/dist/heuristics/classifyPlacement.d.ts +67 -0
  52. package/dist/heuristics/classifyPlacement.js +153 -0
  53. package/dist/heuristics/machineSenders.d.ts +21 -0
  54. package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
  55. package/dist/heuristics/senderMismatch.d.ts +81 -0
  56. package/dist/heuristics/senderMismatch.js +192 -0
  57. package/dist/heuristics/socialDomains.d.ts +14 -0
  58. package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
  59. package/dist/heuristics/transactionalDomains.d.ts +18 -0
  60. package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
  61. package/dist/imapflow-connection.d.ts +377 -0
  62. package/dist/imapflow-connection.js +1147 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.js +65 -0
  65. package/dist/list-id-backfill.d.ts +83 -0
  66. package/dist/list-id-backfill.js +132 -0
  67. package/dist/mailbox-cursor-rebuild.d.ts +74 -0
  68. package/dist/mailbox-cursor-rebuild.js +71 -0
  69. package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
  70. package/dist/mailbox-cursor.js +102 -0
  71. package/dist/mailbox-intent.d.ts +41 -0
  72. package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
  73. package/dist/mailbox-management.d.ts +240 -0
  74. package/dist/mailbox-management.js +576 -0
  75. package/dist/mailbox-presence.d.ts +52 -0
  76. package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
  77. package/dist/mailbox-queue.d.ts +129 -0
  78. package/dist/mailbox-queue.js +216 -0
  79. package/dist/mailbox-sync.d.ts +93 -0
  80. package/dist/mailbox-sync.js +490 -0
  81. package/dist/mailbox-upstream.d.ts +38 -0
  82. package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
  83. package/dist/message-category.d.ts +51 -0
  84. package/{src/message-category.ts → dist/message-category.js} +28 -71
  85. package/dist/message-move.d.ts +285 -0
  86. package/dist/message-move.js +858 -0
  87. package/dist/message-parser.d.ts +14 -0
  88. package/dist/message-parser.js +17 -0
  89. package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
  90. package/dist/message-presence.js +29 -0
  91. package/dist/message-read.d.ts +51 -0
  92. package/dist/message-read.js +31 -0
  93. package/dist/message-sync.d.ts +345 -0
  94. package/dist/message-sync.js +1358 -0
  95. package/dist/mime-walker.d.ts +102 -0
  96. package/dist/mime-walker.js +198 -0
  97. package/dist/organize-job.d.ts +20 -0
  98. package/dist/organize-job.js +37 -0
  99. package/dist/outbox-attachment-content.d.ts +37 -0
  100. package/dist/outbox-attachment-content.js +50 -0
  101. package/dist/outbox-attachment-filename.d.ts +19 -0
  102. package/dist/outbox-attachment-filename.js +64 -0
  103. package/dist/outbox-attachment.d.ts +148 -0
  104. package/dist/outbox-attachment.js +273 -0
  105. package/dist/outbox-queue.d.ts +73 -0
  106. package/dist/outbox-queue.js +280 -0
  107. package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
  108. package/dist/outbox-status.js +18 -0
  109. package/dist/ownership.d.ts +24 -0
  110. package/dist/ownership.js +83 -0
  111. package/dist/placement-move-terminal.d.ts +67 -0
  112. package/dist/placement-move-terminal.js +88 -0
  113. package/dist/placement-move.d.ts +83 -0
  114. package/dist/placement-move.js +216 -0
  115. package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
  116. package/dist/placement-settled.js +75 -0
  117. package/dist/quarantine.d.ts +95 -0
  118. package/dist/quarantine.js +131 -0
  119. package/dist/refusal.d.ts +31 -0
  120. package/dist/refusal.js +3 -0
  121. package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
  122. package/dist/restore-source-placement.js +79 -0
  123. package/dist/search-text-backfill.d.ts +15 -0
  124. package/dist/search-text-backfill.js +33 -0
  125. package/dist/snippet.d.ts +61 -0
  126. package/dist/snippet.js +129 -0
  127. package/dist/spam-report.d.ts +86 -0
  128. package/dist/spam-report.js +241 -0
  129. package/dist/sync-warning.d.ts +21 -0
  130. package/dist/sync-warning.js +26 -0
  131. package/dist/sync-watermarks.d.ts +138 -0
  132. package/dist/sync-watermarks.js +205 -0
  133. package/dist/test-helpers/fake-imap.d.ts +151 -0
  134. package/dist/test-helpers/fake-imap.js +508 -0
  135. package/dist/test-helpers/folder-roles.d.ts +34 -0
  136. package/dist/test-helpers/folder-roles.js +47 -0
  137. package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
  138. package/dist/test-helpers/mime-tree-generator.js +329 -0
  139. package/dist/test-helpers/test-store.d.ts +69 -0
  140. package/dist/test-helpers/test-store.js +193 -0
  141. package/dist/text/normalizer.d.ts +20 -0
  142. package/dist/text/normalizer.js +102 -0
  143. package/dist/thread-message-rows.d.ts +69 -0
  144. package/dist/thread-message-rows.js +40 -0
  145. package/dist/thread-root.d.ts +7 -0
  146. package/dist/thread-root.js +25 -0
  147. package/dist/types.d.ts +343 -0
  148. package/dist/types.js +19 -0
  149. package/package.json +86 -88
  150. package/src/account-credentials.ts +0 -210
  151. package/src/attribute-mapper.test.ts +0 -104
  152. package/src/attribute-mapper.ts +0 -148
  153. package/src/body-parse.test.ts +0 -55
  154. package/src/body-part-mapper.property.test.ts +0 -354
  155. package/src/body-part-mapper.ts +0 -410
  156. package/src/body-sync-queue.test.ts +0 -64
  157. package/src/body-sync-queue.ts +0 -131
  158. package/src/body-sync-terminal.test.ts +0 -148
  159. package/src/body-sync-terminal.ts +0 -174
  160. package/src/body-sync.test.ts +0 -925
  161. package/src/body-sync.ts +0 -1891
  162. package/src/calendar-parts.ts +0 -41
  163. package/src/classification-backfill.test.ts +0 -285
  164. package/src/classification-backfill.ts +0 -363
  165. package/src/classify-message.test.ts +0 -142
  166. package/src/classify-message.ts +0 -116
  167. package/src/connection-factory.ts +0 -102
  168. package/src/connection-test.ts +0 -208
  169. package/src/credentials.test.ts +0 -181
  170. package/src/external-move.test.ts +0 -262
  171. package/src/external-move.ts +0 -285
  172. package/src/filter-operations.test.ts +0 -319
  173. package/src/filter-operations.ts +0 -377
  174. package/src/filter-update.test.ts +0 -313
  175. package/src/filters/filter-config.test.ts +0 -87
  176. package/src/filters/filter-config.ts +0 -34
  177. package/src/filters/list-id.test.ts +0 -50
  178. package/src/filters/match.test.ts +0 -392
  179. package/src/filters/match.ts +0 -225
  180. package/src/filters/pipeline.test.ts +0 -199
  181. package/src/filters/pipeline.ts +0 -170
  182. package/src/flag-push.test.ts +0 -112
  183. package/src/flag-push.ts +0 -198
  184. package/src/flag-queue.test.ts +0 -197
  185. package/src/flag-queue.ts +0 -509
  186. package/src/folder-operations.test.ts +0 -273
  187. package/src/folder-operations.ts +0 -217
  188. package/src/folder-placement.ts +0 -56
  189. package/src/forwarded-message.test.ts +0 -122
  190. package/src/heuristics/authenticationResults.test.ts +0 -189
  191. package/src/heuristics/authenticationResults.ts +0 -212
  192. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  193. package/src/heuristics/classifyByHeaders.ts +0 -467
  194. package/src/heuristics/classifyPlacement.test.ts +0 -366
  195. package/src/heuristics/classifyPlacement.ts +0 -211
  196. package/src/heuristics/classifyRealMail.test.ts +0 -242
  197. package/src/heuristics/machineSenders.test.ts +0 -101
  198. package/src/heuristics/senderMismatch.test.ts +0 -346
  199. package/src/heuristics/senderMismatch.ts +0 -259
  200. package/src/imapflow-connect-retry.test.ts +0 -97
  201. package/src/imapflow-connection.test.ts +0 -503
  202. package/src/imapflow-connection.ts +0 -1476
  203. package/src/index.ts +0 -397
  204. package/src/list-id-backfill.test.ts +0 -207
  205. package/src/list-id-backfill.ts +0 -274
  206. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  207. package/src/mailbox-cursor-rebuild.ts +0 -146
  208. package/src/mailbox-cursor.test.ts +0 -141
  209. package/src/mailbox-delete-intent.test.ts +0 -119
  210. package/src/mailbox-management.test.ts +0 -243
  211. package/src/mailbox-management.ts +0 -838
  212. package/src/mailbox-presence.test.ts +0 -74
  213. package/src/mailbox-queue.ts +0 -397
  214. package/src/mailbox-rename-intent.test.ts +0 -220
  215. package/src/mailbox-rename-subtree.test.ts +0 -287
  216. package/src/mailbox-sync.test.ts +0 -286
  217. package/src/mailbox-sync.ts +0 -724
  218. package/src/message-move.test.ts +0 -726
  219. package/src/message-move.ts +0 -1518
  220. package/src/message-parser.ts +0 -26
  221. package/src/message-presence.test.ts +0 -50
  222. package/src/message-read.ts +0 -87
  223. package/src/message-save-retry.test.ts +0 -90
  224. package/src/message-sync-flags.test.ts +0 -167
  225. package/src/message-sync.test.ts +0 -762
  226. package/src/message-sync.ts +0 -2133
  227. package/src/mime-walker.test.ts +0 -311
  228. package/src/mime-walker.ts +0 -306
  229. package/src/organize-job.ts +0 -93
  230. package/src/outbox-attachment-content.test.ts +0 -153
  231. package/src/outbox-attachment-content.ts +0 -104
  232. package/src/outbox-attachment-filename.test.ts +0 -132
  233. package/src/outbox-attachment-filename.ts +0 -76
  234. package/src/outbox-attachment.test.ts +0 -626
  235. package/src/outbox-attachment.ts +0 -536
  236. package/src/outbox-failed-recovery.test.ts +0 -299
  237. package/src/outbox-queue.test.ts +0 -315
  238. package/src/outbox-queue.ts +0 -525
  239. package/src/ownership.ts +0 -140
  240. package/src/placement-move-terminal.ts +0 -145
  241. package/src/placement-move.ts +0 -363
  242. package/src/placement-settled.test.ts +0 -147
  243. package/src/placement.test.ts +0 -404
  244. package/src/quarantine.test.ts +0 -133
  245. package/src/quarantine.ts +0 -229
  246. package/src/refusal.ts +0 -29
  247. package/src/snippet.test.ts +0 -213
  248. package/src/snippet.ts +0 -152
  249. package/src/spam-report.test.ts +0 -329
  250. package/src/spam-report.ts +0 -357
  251. package/src/sync-warning.test.ts +0 -213
  252. package/src/sync-warning.ts +0 -56
  253. package/src/sync-watermarks.test.ts +0 -409
  254. package/src/sync-watermarks.ts +0 -319
  255. package/src/system-flag-wire-format.test.ts +0 -77
  256. package/src/test-helpers/fake-imap.ts +0 -690
  257. package/src/test-helpers/folder-roles.ts +0 -66
  258. package/src/test-helpers/mime-tree-generator.ts +0 -461
  259. package/src/test-helpers/test-queue.ts +0 -56
  260. package/src/test-helpers/test-store.ts +0 -299
  261. package/src/text/normalizer.test.ts +0 -223
  262. package/src/text/normalizer.ts +0 -140
  263. package/src/thread-message-rows.ts +0 -77
  264. package/src/thread-root.test.ts +0 -29
  265. package/src/thread-root.ts +0 -51
  266. package/src/types/mailparser-augment.d.ts +0 -23
  267. package/src/types/natural-porter-stemmer.d.ts +0 -50
  268. package/src/types/stopword.d.ts +0 -69
  269. package/src/types.ts +0 -371
  270. package/tsconfig.json +0 -8
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Multilingual text normalizer for email processing.
3
+ *
4
+ * Uses franc for language detection, natural for stemming,
5
+ * and stopword for stopword removal.
6
+ */
7
+ /// <reference path="../types/natural-porter-stemmer.d.ts" />
8
+ import { franc } from "franc";
9
+ // Import each Porter stemmer file directly instead of the `natural` package
10
+ // root or its `stemmers` submodule barrel: the root barrel
11
+ // (`natural/lib/natural/index.js`) loads WordNet and the Brill POS tagger,
12
+ // which pull in `mongoose` and — via its Postgres storage plugin —
13
+ // `pg`/`dotenv` (`dotenv.config()` at module scope on every cold start, see
14
+ // #1244/#1247); the `stemmers` barrel is CJS and unconditionally requires
15
+ // every stemmer it lists, including Japanese (which drags in its own
16
+ // tokenizer) and Indonesian, neither used here. This module only needs the
17
+ // seven Porter stemmers below — see
18
+ // ../types/natural-porter-stemmer.d.ts for their (hand-written, since
19
+ // `natural` ships none for individual files) type declarations.
20
+ import PorterStemmer from "natural/lib/natural/stemmers/porter_stemmer.js";
21
+ import PorterStemmerDe from "natural/lib/natural/stemmers/porter_stemmer_de.js";
22
+ import PorterStemmerEs from "natural/lib/natural/stemmers/porter_stemmer_es.js";
23
+ import PorterStemmerFr from "natural/lib/natural/stemmers/porter_stemmer_fr.js";
24
+ import PorterStemmerIt from "natural/lib/natural/stemmers/porter_stemmer_it.js";
25
+ import PorterStemmerNl from "natural/lib/natural/stemmers/porter_stemmer_nl.js";
26
+ import PorterStemmerPt from "natural/lib/natural/stemmers/porter_stemmer_pt.js";
27
+ import { deu, eng, fra, ita, nld, por, removeStopwords, spa } from "stopword";
28
+ // ISO 639-3 (franc) → ISO 639-1 (our SupportedLanguage)
29
+ const iso3ToIso1 = {
30
+ eng: "en",
31
+ deu: "de",
32
+ fra: "fr",
33
+ nld: "nl",
34
+ spa: "es",
35
+ ita: "it",
36
+ por: "pt",
37
+ };
38
+ // Restrict franc to only detect languages we can stem
39
+ const supportedIso3 = Object.keys(iso3ToIso1);
40
+ const stemmers = {
41
+ en: PorterStemmer,
42
+ de: PorterStemmerDe,
43
+ fr: PorterStemmerFr,
44
+ es: PorterStemmerEs,
45
+ it: PorterStemmerIt,
46
+ nl: PorterStemmerNl,
47
+ pt: PorterStemmerPt,
48
+ };
49
+ const stopwordLists = {
50
+ en: eng,
51
+ de: deu,
52
+ fr: fra,
53
+ es: spa,
54
+ it: ita,
55
+ nl: nld,
56
+ pt: por,
57
+ };
58
+ export const createTextNormalizer = () => ({
59
+ detectLanguage: (text) => {
60
+ // Use franc with trigram analysis, restricted to supported languages
61
+ const detected = franc(text, { only: supportedIso3, minLength: 10 });
62
+ // Map to our language codes, default to English
63
+ return iso3ToIso1[detected] ?? "en";
64
+ },
65
+ normalize: (text, options = {}) => {
66
+ const { language = "en", stem = true, removeStopwords: removeStop = true, } = options;
67
+ // Normalize unicode and lowercase
68
+ let normalized = text.normalize("NFKC").toLowerCase();
69
+ // Keep Unicode letters and numbers, remove punctuation
70
+ normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, " ");
71
+ // Tokenize
72
+ let words = normalized.split(/\s+/).filter((w) => w.length > 0);
73
+ // Remove stopwords
74
+ if (removeStop) {
75
+ const list = stopwordLists[language] ?? eng;
76
+ words = removeStopwords(words, list);
77
+ }
78
+ // Stem
79
+ if (stem) {
80
+ const stemmer = stemmers[language] ?? stemmers.en;
81
+ words = words.map((w) => stemmer.stem(w));
82
+ }
83
+ // Dedupe consecutive words
84
+ return words.filter((w, i, arr) => i === 0 || w !== arr[i - 1]).join(" ");
85
+ },
86
+ tokenize: (text) => {
87
+ return text
88
+ .normalize("NFKC")
89
+ .toLowerCase()
90
+ .replace(/[^\p{L}\p{N}\s]/gu, " ")
91
+ .split(/\s+/)
92
+ .filter((w) => w.length > 0);
93
+ },
94
+ stem: (word, language = "en") => {
95
+ const stemmer = stemmers[language] ?? stemmers.en;
96
+ return stemmer.stem(word.toLowerCase());
97
+ },
98
+ removeStopwords: (words, language = "en") => {
99
+ const list = stopwordLists[language] ?? eng;
100
+ return removeStopwords(words, list);
101
+ },
102
+ });
@@ -0,0 +1,69 @@
1
+ import type { ThreadMessageItem } from "@remit/data-ports";
2
+ /**
3
+ * Build the `set` and `composites` payload for the ThreadMessage update on a
4
+ * MESSAGE_DELETE move-to-trash.
5
+ *
6
+ * The CURRENT row state goes in `composites`; the NEW values go in `set`.
7
+ * ElectroDB uses `composites` to run the conditional check on the existing row
8
+ * AND to compute the previous sort-key values needed to recompute the new ones.
9
+ * Passing the NEW values in `composites` makes the conditional check fail with
10
+ * ConditionalCheckFailedException, which ElectroDB wraps as NotFoundError, and
11
+ * the caller silently drops the update. Same root cause as PR #186 fixed for
12
+ * `flag-queue.ts`.
13
+ */
14
+ export type ThreadMessageRowState = Pick<ThreadMessageItem, "sentDate" | "mailboxId" | "isRead" | "isDeleted" | "hasStars" | "hasAttachment">;
15
+ export declare const buildThreadMessageTrashUpdate: (threadMessage: ThreadMessageRowState, newUid: number, destinationMailboxId: string) => {
16
+ set: {
17
+ uid: number;
18
+ mailboxId: string;
19
+ isDeleted: boolean;
20
+ };
21
+ composites: {
22
+ sentDate: number;
23
+ mailboxId: string;
24
+ isRead: boolean;
25
+ isDeleted: boolean;
26
+ hasStars: boolean;
27
+ hasAttachment: boolean;
28
+ };
29
+ };
30
+ /**
31
+ * The inverse payload: put the thread row back where the message still is on
32
+ * the server. The delete was recorded optimistically, so a delete that gives up
33
+ * with the message still at its source has to hand the row back rather than
34
+ * leave it claiming Trash — an invisible `failed` on a row the user cannot see
35
+ * is the shape of the incident this whole change is about.
36
+ */
37
+ export declare const buildThreadMessageMoveRevert: (threadMessage: ThreadMessageRowState, sourceUid: number, sourceMailboxId: string) => {
38
+ set: {
39
+ uid: number;
40
+ mailboxId: string;
41
+ isDeleted: boolean;
42
+ };
43
+ composites: {
44
+ sentDate: number;
45
+ mailboxId: string;
46
+ isRead: boolean;
47
+ isDeleted: boolean;
48
+ hasStars: boolean;
49
+ hasAttachment: boolean;
50
+ };
51
+ };
52
+ /**
53
+ * Hand a row back after an abandoned expunge. The mail never left Trash, so
54
+ * only the deletion mark reverts — the uid and mailbox on the row are still
55
+ * where the server has it.
56
+ */
57
+ export declare const buildThreadMessageUndelete: (threadMessage: ThreadMessageRowState) => {
58
+ set: {
59
+ isDeleted: boolean;
60
+ };
61
+ composites: {
62
+ sentDate: number;
63
+ mailboxId: string;
64
+ isRead: boolean;
65
+ isDeleted: boolean;
66
+ hasStars: boolean;
67
+ hasAttachment: boolean;
68
+ };
69
+ };
@@ -0,0 +1,40 @@
1
+ const currentComposites = (threadMessage) => ({
2
+ sentDate: threadMessage.sentDate,
3
+ mailboxId: threadMessage.mailboxId,
4
+ isRead: threadMessage.isRead,
5
+ isDeleted: threadMessage.isDeleted,
6
+ hasStars: threadMessage.hasStars,
7
+ hasAttachment: threadMessage.hasAttachment,
8
+ });
9
+ export const buildThreadMessageTrashUpdate = (threadMessage, newUid, destinationMailboxId) => ({
10
+ set: {
11
+ uid: newUid,
12
+ mailboxId: destinationMailboxId,
13
+ isDeleted: true,
14
+ },
15
+ composites: currentComposites(threadMessage),
16
+ });
17
+ /**
18
+ * The inverse payload: put the thread row back where the message still is on
19
+ * the server. The delete was recorded optimistically, so a delete that gives up
20
+ * with the message still at its source has to hand the row back rather than
21
+ * leave it claiming Trash — an invisible `failed` on a row the user cannot see
22
+ * is the shape of the incident this whole change is about.
23
+ */
24
+ export const buildThreadMessageMoveRevert = (threadMessage, sourceUid, sourceMailboxId) => ({
25
+ set: {
26
+ uid: sourceUid,
27
+ mailboxId: sourceMailboxId,
28
+ isDeleted: false,
29
+ },
30
+ composites: currentComposites(threadMessage),
31
+ });
32
+ /**
33
+ * Hand a row back after an abandoned expunge. The mail never left Trash, so
34
+ * only the deletion mark reverts — the uid and mailbox on the row are still
35
+ * where the server has it.
36
+ */
37
+ export const buildThreadMessageUndelete = (threadMessage) => ({
38
+ set: { isDeleted: false },
39
+ composites: currentComposites(threadMessage),
40
+ });
@@ -0,0 +1,7 @@
1
+ import type { MessageReferenceInput } from "@remit/data-ports";
2
+ import type { ImapEnvelope } from "./types.js";
3
+ type ThreadingHeaders = Pick<ImapEnvelope, "messageId" | "inReplyTo">;
4
+ export declare const threadRootOf: (envelope: ThreadingHeaders, references: readonly string[] | undefined, messageId: string) => string;
5
+ export declare const parentOf: (envelope: ThreadingHeaders, references: readonly string[] | undefined) => string | undefined;
6
+ export declare const messageReferencesOf: (envelope: ThreadingHeaders, references: readonly string[] | undefined) => MessageReferenceInput[];
7
+ export {};
@@ -0,0 +1,25 @@
1
+ import { bareMessageId, isValidMessageId, wireMessageId, } from "@remit/data-ports/id";
2
+ import { ReferenceType } from "@remit/domain-enums";
3
+ const firstUsable = (candidates) => {
4
+ const found = candidates.find(isValidMessageId);
5
+ return found === undefined ? undefined : wireMessageId(found);
6
+ };
7
+ export const threadRootOf = (envelope, references, messageId) => firstUsable([references?.[0], envelope.inReplyTo, envelope.messageId]) ??
8
+ messageId;
9
+ export const parentOf = (envelope, references) => firstUsable([envelope.inReplyTo, references?.[references.length - 1]]);
10
+ export const messageReferencesOf = (envelope, references) => [
11
+ ...(isValidMessageId(envelope.inReplyTo)
12
+ ? [
13
+ {
14
+ messageIdValue: bareMessageId(envelope.inReplyTo),
15
+ referenceType: ReferenceType.InReplyTo,
16
+ referenceOrder: 0,
17
+ },
18
+ ]
19
+ : []),
20
+ ...(references ?? []).filter(isValidMessageId).map((reference, index) => ({
21
+ messageIdValue: bareMessageId(reference),
22
+ referenceType: ReferenceType.References,
23
+ referenceOrder: index,
24
+ })),
25
+ ];
@@ -0,0 +1,343 @@
1
+ /**
2
+ * Types for the remit-mailbox-service
3
+ */
4
+ import type { Readable } from "node:stream";
5
+ /**
6
+ * Discriminated union of mail authentication credentials.
7
+ *
8
+ * Exactly one credential kind can be present at a time — the union makes
9
+ * "both password and accessToken simultaneously" unrepresentable.
10
+ */
11
+ export type MailCredentials = {
12
+ kind: "password";
13
+ password: string;
14
+ } | {
15
+ kind: "accessToken";
16
+ accessToken: string;
17
+ };
18
+ /**
19
+ * Classification of mail connection errors.
20
+ */
21
+ export type MailConnectionErrorKind = "auth" | "network";
22
+ /**
23
+ * Typed error for IMAP/SMTP connection failures.
24
+ *
25
+ * `kind: "auth"` — authentication rejected (bad credentials / token expired).
26
+ * `kind: "network"` — transport-level failure (ECONNREFUSED, ETIMEDOUT, etc.).
27
+ *
28
+ * IMPORTANT: access tokens must NEVER appear in error messages or causes.
29
+ */
30
+ export declare class MailConnectionError extends Error {
31
+ readonly kind: MailConnectionErrorKind;
32
+ constructor(kind: MailConnectionErrorKind, message: string, cause?: unknown);
33
+ }
34
+ /**
35
+ * IMAP connection configuration
36
+ */
37
+ export interface ImapConnectionConfig {
38
+ host: string;
39
+ port: number;
40
+ user: string;
41
+ credentials: MailCredentials;
42
+ tls: boolean;
43
+ tlsOptions?: {
44
+ rejectUnauthorized?: boolean;
45
+ servername?: string;
46
+ };
47
+ /** Connection timeout in milliseconds */
48
+ connTimeout?: number;
49
+ /** Authentication timeout in milliseconds */
50
+ authTimeout?: number;
51
+ }
52
+ /**
53
+ * IMAP connection state
54
+ */
55
+ export type ImapConnectionState = "disconnected" | "connecting" | "connected" | "authenticated" | "error";
56
+ /**
57
+ * Namespace structure from IMAP NAMESPACE command
58
+ */
59
+ export interface ImapNamespace {
60
+ prefix: string;
61
+ delimiter: string | false;
62
+ extensions?: Array<{
63
+ name: string;
64
+ params: string[] | null;
65
+ }>;
66
+ }
67
+ /**
68
+ * IMAP namespaces grouped by type
69
+ */
70
+ export interface ImapNamespaces {
71
+ personal: ImapNamespace[];
72
+ other: ImapNamespace[];
73
+ shared: ImapNamespace[];
74
+ }
75
+ /**
76
+ * IMAP mailbox status after opening
77
+ */
78
+ export interface ImapBoxStatus {
79
+ name: string;
80
+ readOnly: boolean;
81
+ uidvalidity: number;
82
+ uidnext: number;
83
+ flags: string[];
84
+ permFlags: string[];
85
+ persistentUIDs: boolean;
86
+ messages: {
87
+ total: number;
88
+ new: number;
89
+ };
90
+ newKeywords: boolean;
91
+ }
92
+ /**
93
+ * IMAP mailbox status from STATUS command
94
+ */
95
+ export interface ImapMailboxStatus {
96
+ messages: number;
97
+ recent: number;
98
+ unseen: number;
99
+ uidNext: number;
100
+ uidValidity: number;
101
+ /** Highest modification sequence (CONDSTORE) as decimal digits of an unsigned 64-bit value. "0" if the server does not support CONDSTORE. */
102
+ highestModseq: string;
103
+ /** Count of messages flagged \Deleted but not yet expunged. */
104
+ deletedCount: number;
105
+ }
106
+ /**
107
+ * Flattened mailbox info for processing
108
+ */
109
+ export interface FlatMailboxInfo {
110
+ fullPath: string;
111
+ name: string;
112
+ delimiter: string;
113
+ attributes: string[];
114
+ parentPath: string | null;
115
+ }
116
+ export interface ImapAddress {
117
+ name?: string;
118
+ mailbox: string;
119
+ host: string;
120
+ }
121
+ export interface ImapEnvelope {
122
+ date: string;
123
+ subject: string;
124
+ from: ImapAddress[];
125
+ sender: ImapAddress[];
126
+ replyTo: ImapAddress[];
127
+ to: ImapAddress[];
128
+ cc: ImapAddress[];
129
+ bcc: ImapAddress[];
130
+ inReplyTo: string;
131
+ messageId: string;
132
+ }
133
+ /**
134
+ * Subset of imapflow's `MessageStructureObject` we depend on. Restated
135
+ * here so non-imapflow code (and tests) can build fixtures without pulling
136
+ * the imapflow types in.
137
+ */
138
+ export interface ImapBodyStructure {
139
+ /** Dot-numbered MIME path; absent on the root node. */
140
+ part?: string;
141
+ /** Full Content-Type, e.g. "text/plain" or "multipart/mixed". */
142
+ type: string;
143
+ parameters?: Record<string, string>;
144
+ id?: string;
145
+ description?: string;
146
+ encoding?: string;
147
+ size?: number;
148
+ lineCount?: number;
149
+ md5?: string;
150
+ disposition?: string;
151
+ dispositionParameters?: Record<string, string>;
152
+ language?: string[];
153
+ location?: string;
154
+ childNodes?: ImapBodyStructure[];
155
+ }
156
+ /**
157
+ * One server message from the cheap envelope-only FETCH the UIDVALIDITY
158
+ * cursor rebuild uses (#1272): UID + Message-ID + INTERNALDATE, nothing
159
+ * else — no flags, no BODYSTRUCTURE, no body fetch.
160
+ */
161
+ export interface ImapEnvelopeSnapshot {
162
+ uid: number;
163
+ /** Raw RFC 822 Message-ID header; empty string when absent/unparseable. */
164
+ messageId: string;
165
+ internalDate: Date;
166
+ }
167
+ export interface ImapMessage {
168
+ uid: number;
169
+ seq: number;
170
+ flags: string[];
171
+ internalDate: Date;
172
+ size: number;
173
+ envelope?: ImapEnvelope;
174
+ /**
175
+ * References header from RFC 2822.
176
+ * Contains Message-IDs of ancestor messages, with the first being the thread root.
177
+ */
178
+ references?: string[];
179
+ /** Parsed BODYSTRUCTURE tree (RFC 9051 Section 7.5.2). */
180
+ bodyStructure?: ImapBodyStructure;
181
+ /**
182
+ * Per-message MODSEQ (RFC 7162) as decimal digits of an unsigned 64-bit
183
+ * value. Absent when the session has no CONDSTORE.
184
+ */
185
+ modseq?: string;
186
+ }
187
+ /**
188
+ * Result of a mailbox sync operation
189
+ */
190
+ export interface MailboxSyncResult {
191
+ created: number;
192
+ updated: number;
193
+ deleted: number;
194
+ }
195
+ /**
196
+ * Common interface for IMAP connections
197
+ */
198
+ export interface IImapConnection {
199
+ readonly state: ImapConnectionState;
200
+ readonly isConnected: boolean;
201
+ connect(): Promise<void>;
202
+ disconnect(): Promise<void>;
203
+ getNamespaces(): Promise<ImapNamespaces>;
204
+ listMailboxes(nsPrefix?: string): Promise<FlatMailboxInfo[]>;
205
+ openBox(mailboxPath: string, readOnly?: boolean): Promise<ImapBoxStatus>;
206
+ closeBox(expunge?: boolean): Promise<void>;
207
+ search(criteria: unknown[]): Promise<number[]>;
208
+ fetchMessages(uids: number[]): Promise<ImapMessage[]>;
209
+ /**
210
+ * True when the session negotiated CONDSTORE (RFC 7162) AND the currently
211
+ * open mailbox keeps persistent mod-sequences. A mailbox that answered
212
+ * NOMODSEQ on SELECT reports false, as does any server that never
213
+ * advertised the extension. Requires a mailbox to be open.
214
+ */
215
+ supportsCondstore(): boolean;
216
+ /**
217
+ * FETCH every message in the open mailbox whose MODSEQ is strictly greater
218
+ * than `sinceModseq` (RFC 7162 CHANGEDSINCE) — both messages that arrived
219
+ * and messages whose metadata changed. Requires a mailbox to be open.
220
+ *
221
+ * Throws when CONDSTORE is unavailable: the underlying client drops the
222
+ * modifier in that case, which would silently turn the call into a fetch of
223
+ * the entire mailbox.
224
+ */
225
+ fetchMessagesChangedSince(sinceModseq: bigint): Promise<ImapMessage[]>;
226
+ /**
227
+ * Cheap envelope-only pass for the UIDVALIDITY cursor rebuild (#1272): UID
228
+ * + Message-ID + INTERNALDATE for every UID, no BODYSTRUCTURE, no flags,
229
+ * no body. Requires a mailbox to be open.
230
+ */
231
+ fetchEnvelopeSnapshots(uids: number[]): Promise<ImapEnvelopeSnapshot[]>;
232
+ fetchMessageBody(uid: number): Promise<Buffer | null>;
233
+ /**
234
+ * Fetch full message bodies for many UIDs in ONE pipelined ranged UID FETCH.
235
+ * Yields `{ uid, source }` per message; `source` is a readable stream the
236
+ * caller must consume before requesting the next item. Requires a mailbox to
237
+ * be open (one SELECT for the whole batch).
238
+ */
239
+ fetchMessageBodies(uids: number[]): AsyncGenerator<{
240
+ uid: number;
241
+ source: Readable;
242
+ }>;
243
+ /**
244
+ * Add flags to messages by UID.
245
+ * Requires mailbox to be open.
246
+ */
247
+ addFlags(uids: number[], flags: string[]): Promise<void>;
248
+ /**
249
+ * Remove flags from messages by UID.
250
+ * Requires mailbox to be open.
251
+ */
252
+ removeFlags(uids: number[], flags: string[]): Promise<void>;
253
+ /**
254
+ * Replace all flags on messages by UID.
255
+ * Requires mailbox to be open.
256
+ */
257
+ setFlags(uids: number[], flags: string[]): Promise<void>;
258
+ /**
259
+ * Create a new mailbox.
260
+ */
261
+ createMailbox(path: string): Promise<{
262
+ path: string;
263
+ created: boolean;
264
+ }>;
265
+ /**
266
+ * Delete a mailbox.
267
+ */
268
+ deleteMailbox(path: string): Promise<{
269
+ path: string;
270
+ }>;
271
+ /**
272
+ * Rename a mailbox.
273
+ */
274
+ renameMailbox(oldPath: string, newPath: string): Promise<{
275
+ path: string;
276
+ newPath: string;
277
+ }>;
278
+ /**
279
+ * Subscribe to a mailbox.
280
+ */
281
+ subscribeMailbox(path: string): Promise<void>;
282
+ /**
283
+ * Unsubscribe from a mailbox.
284
+ */
285
+ unsubscribeMailbox(path: string): Promise<void>;
286
+ /**
287
+ * Move messages to another mailbox.
288
+ * Returns mapping of source UIDs to destination UIDs.
289
+ * Requires mailbox to be open.
290
+ *
291
+ * @param uids - Array of message UIDs to move
292
+ * @param destination - Destination mailbox path
293
+ * @returns Object with destination path, uidValidity, and uidMap
294
+ */
295
+ moveMessages(uids: number[], destination: string): Promise<{
296
+ destination: string;
297
+ uidValidity: number;
298
+ uidMap: Map<number, number>;
299
+ }>;
300
+ /**
301
+ * Copy messages to another mailbox.
302
+ * Returns mapping of source UIDs to destination UIDs.
303
+ * Requires mailbox to be open.
304
+ *
305
+ * @param uids - Array of message UIDs to copy
306
+ * @param destination - Destination mailbox path
307
+ * @returns Object with destination path, uidValidity, and uidMap
308
+ */
309
+ copyMessages(uids: number[], destination: string): Promise<{
310
+ destination: string;
311
+ uidValidity: number;
312
+ uidMap: Map<number, number>;
313
+ }>;
314
+ /**
315
+ * Permanently delete messages (mark \Deleted + expunge).
316
+ * Requires mailbox to be open.
317
+ *
318
+ * @param uids - Array of message UIDs to delete
319
+ * @returns Number of messages deleted
320
+ */
321
+ deleteMessages(uids: number[]): Promise<number>;
322
+ /**
323
+ * Get mailbox status without opening it.
324
+ * Uses IMAP STATUS command to get message counts.
325
+ *
326
+ * @param mailboxPath - Full path to the mailbox
327
+ * @returns Mailbox status including unseen count
328
+ */
329
+ getMailboxStatus(mailboxPath: string): Promise<ImapMailboxStatus>;
330
+ /**
331
+ * Append a message to a mailbox via IMAP APPEND command.
332
+ *
333
+ * @param mailbox - Full path of the target mailbox
334
+ * @param message - RFC 822 message content
335
+ * @param flags - Optional flags to set on the message
336
+ * @returns Object with destination path, uidValidity, and uid of the appended message
337
+ */
338
+ append(mailbox: string, message: string | Buffer, flags?: string[]): Promise<{
339
+ destination: string;
340
+ uidValidity: number;
341
+ uid: number;
342
+ }>;
343
+ }
package/dist/types.js ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Types for the remit-mailbox-service
3
+ */
4
+ /**
5
+ * Typed error for IMAP/SMTP connection failures.
6
+ *
7
+ * `kind: "auth"` — authentication rejected (bad credentials / token expired).
8
+ * `kind: "network"` — transport-level failure (ECONNREFUSED, ETIMEDOUT, etc.).
9
+ *
10
+ * IMPORTANT: access tokens must NEVER appear in error messages or causes.
11
+ */
12
+ export class MailConnectionError extends Error {
13
+ kind;
14
+ constructor(kind, message, cause) {
15
+ super(message, { cause });
16
+ this.name = "MailConnectionError";
17
+ this.kind = kind;
18
+ }
19
+ }