@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
@@ -1,311 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { describe, it } from "node:test";
3
- import { MediaType, TransferEncoding } from "@remit/domain-enums";
4
- import {
5
- type MimeNode,
6
- ROOT_PART_PATH,
7
- walkMimeStructure,
8
- } from "./mime-walker.js";
9
-
10
- describe("walkMimeStructure", () => {
11
- it("flattens a simple text/plain message into a single record", () => {
12
- const root: MimeNode = {
13
- type: "text/plain",
14
- parameters: { charset: "utf-8" },
15
- encoding: "7bit",
16
- size: 42,
17
- lineCount: 3,
18
- };
19
-
20
- const records = walkMimeStructure(root);
21
-
22
- assert.equal(records.length, 1);
23
- const [only] = records;
24
- assert.equal(only.partPath, ROOT_PART_PATH);
25
- assert.equal(only.parentPartPath, null);
26
- assert.equal(only.mediaType, "TEXT");
27
- assert.equal(only.mediaSubtype, "plain");
28
- assert.equal(only.transferEncoding, "7BIT");
29
- assert.equal(only.sizeOctets, 42);
30
- assert.equal(only.lineCount, 3);
31
- assert.equal(only.isMultipart, false);
32
- assert.equal(only.disposition, undefined);
33
- assert.deepEqual(only.parameters, [
34
- { parameterName: "charset", parameterValue: "utf-8" },
35
- ]);
36
- });
37
-
38
- it("walks multipart/alternative + multipart/mixed + attachment + inline image (alice/bob fixture)", () => {
39
- // multipart/mixed
40
- // ├── multipart/alternative (1)
41
- // │ ├── text/plain (1.1)
42
- // │ └── text/html (1.2)
43
- // ├── application/pdf (attachment) (2)
44
- // └── multipart/related (3)
45
- // ├── text/html (3.1)
46
- // └── image/png (inline) (3.2)
47
- const root: MimeNode = {
48
- type: "multipart/mixed",
49
- parameters: { boundary: "outer" },
50
- childNodes: [
51
- {
52
- part: "1",
53
- type: "multipart/alternative",
54
- parameters: { boundary: "alt" },
55
- childNodes: [
56
- {
57
- part: "1.1",
58
- type: "text/plain",
59
- parameters: { charset: "utf-8" },
60
- encoding: "7bit",
61
- size: 120,
62
- lineCount: 4,
63
- },
64
- {
65
- part: "1.2",
66
- type: "text/html",
67
- parameters: { charset: "utf-8" },
68
- encoding: "quoted-printable",
69
- size: 350,
70
- lineCount: 6,
71
- },
72
- ],
73
- },
74
- {
75
- part: "2",
76
- type: "application/pdf",
77
- parameters: { name: "alice-resume.pdf" },
78
- encoding: "base64",
79
- size: 9001,
80
- disposition: "attachment",
81
- dispositionParameters: { filename: "alice-resume.pdf" },
82
- md5: "deadbeef",
83
- },
84
- {
85
- part: "3",
86
- type: "multipart/related",
87
- parameters: { boundary: "rel" },
88
- childNodes: [
89
- {
90
- part: "3.1",
91
- type: "text/html",
92
- parameters: { charset: "utf-8" },
93
- encoding: "quoted-printable",
94
- size: 200,
95
- lineCount: 5,
96
- },
97
- {
98
- part: "3.2",
99
- type: "image/png",
100
- parameters: { name: "bob-avatar.png" },
101
- id: "<bob-avatar@example.com>",
102
- encoding: "base64",
103
- size: 4096,
104
- disposition: "inline",
105
- dispositionParameters: { filename: "bob-avatar.png" },
106
- },
107
- ],
108
- },
109
- ],
110
- };
111
-
112
- const records = walkMimeStructure(root);
113
-
114
- const byPath = new Map(records.map((r) => [r.partPath, r]));
115
-
116
- assert.equal(records.length, 8);
117
-
118
- const rootRec = byPath.get(ROOT_PART_PATH);
119
- assert.ok(rootRec, "root present");
120
- assert.equal(rootRec.parentPartPath, null);
121
- assert.equal(rootRec.isMultipart, true);
122
- assert.equal(rootRec.mediaType, "MULTIPART");
123
- assert.equal(rootRec.mediaSubtype, "mixed");
124
- assert.equal(rootRec.multipartSubtype, "mixed");
125
-
126
- const alt = byPath.get("1");
127
- assert.ok(alt);
128
- assert.equal(alt.parentPartPath, ROOT_PART_PATH);
129
- assert.equal(alt.isMultipart, true);
130
- assert.equal(alt.multipartSubtype, "alternative");
131
-
132
- const text = byPath.get("1.1");
133
- assert.ok(text);
134
- assert.equal(text.parentPartPath, "1");
135
- assert.equal(text.mediaType, "TEXT");
136
- assert.equal(text.mediaSubtype, "plain");
137
- assert.equal(text.transferEncoding, "7BIT");
138
-
139
- const html = byPath.get("1.2");
140
- assert.ok(html);
141
- assert.equal(html.parentPartPath, "1");
142
- assert.equal(html.transferEncoding, "QUOTED-PRINTABLE");
143
-
144
- const pdf = byPath.get("2");
145
- assert.ok(pdf);
146
- assert.equal(pdf.parentPartPath, ROOT_PART_PATH);
147
- assert.equal(pdf.mediaType, "APPLICATION");
148
- assert.equal(pdf.disposition, "attachment");
149
- assert.equal(pdf.dispositionFilename, "alice-resume.pdf");
150
- assert.equal(pdf.md5Hash, "deadbeef");
151
- assert.equal(pdf.sizeOctets, 9001);
152
-
153
- const related = byPath.get("3");
154
- assert.ok(related);
155
- assert.equal(related.multipartSubtype, "related");
156
-
157
- const inlineImg = byPath.get("3.2");
158
- assert.ok(inlineImg);
159
- assert.equal(inlineImg.parentPartPath, "3");
160
- assert.equal(inlineImg.disposition, "inline");
161
- assert.equal(inlineImg.contentId, "bob-avatar@example.com");
162
- assert.equal(inlineImg.dispositionFilename, "bob-avatar.png");
163
- });
164
-
165
- it("falls back to Content-Type 'name' parameter when no Content-Disposition filename is set", () => {
166
- const root: MimeNode = {
167
- type: "application/octet-stream",
168
- parameters: { name: "alice-data.bin" },
169
- encoding: "base64",
170
- size: 100,
171
- disposition: "attachment",
172
- };
173
-
174
- const [rec] = walkMimeStructure(root);
175
- assert.equal(rec.dispositionFilename, "alice-data.bin");
176
- });
177
-
178
- it("emits stable, sorted parameters (no input-order dependence)", () => {
179
- const root: MimeNode = {
180
- type: "text/plain",
181
- parameters: { charset: "utf-8", format: "flowed", name: "alice.txt" },
182
- encoding: "7bit",
183
- size: 1,
184
- };
185
-
186
- const [rec] = walkMimeStructure(root);
187
- assert.deepEqual(
188
- rec.parameters.map((p) => p.parameterName),
189
- ["charset", "format", "name"],
190
- );
191
- });
192
-
193
- it("falls back to application/octet-stream on an unknown MIME top-level type", () => {
194
- const [rec] = walkMimeStructure({
195
- type: "carrierpigeon/scroll",
196
- encoding: "7bit",
197
- size: 1,
198
- });
199
- assert.equal(rec.mediaType, MediaType.Application);
200
- assert.equal(rec.mediaSubtype, "scroll");
201
- });
202
-
203
- it("falls back to application/octet-stream on a Content-Type with no '/' separator", () => {
204
- const [rec] = walkMimeStructure({
205
- type: "text",
206
- encoding: "7bit",
207
- size: 1,
208
- });
209
- assert.equal(rec.mediaType, MediaType.Application);
210
- assert.equal(rec.mediaSubtype, "octet-stream");
211
- });
212
-
213
- it("falls back to octet-stream subtype on a Content-Type missing the subtype", () => {
214
- const [rec] = walkMimeStructure({
215
- type: "text/",
216
- encoding: "7bit",
217
- size: 1,
218
- });
219
- assert.equal(rec.mediaType, MediaType.Text);
220
- assert.equal(rec.mediaSubtype, "octet-stream");
221
- });
222
-
223
- it("falls back to BINARY on an unknown transfer encoding", () => {
224
- const [rec] = walkMimeStructure({
225
- type: "text/plain",
226
- encoding: "rot13",
227
- size: 1,
228
- });
229
- assert.equal(rec.transferEncoding, TransferEncoding.Binary);
230
- });
231
-
232
- it("drops an unknown Content-Disposition rather than throwing", () => {
233
- const [rec] = walkMimeStructure({
234
- type: "application/pdf",
235
- encoding: "base64",
236
- size: 1,
237
- disposition: "secretly-mine",
238
- });
239
- assert.equal(rec.disposition, undefined);
240
- });
241
-
242
- it("keeps an empty Content-Type total (application/octet-stream)", () => {
243
- const [rec] = walkMimeStructure({ type: "", encoding: "7bit", size: 1 });
244
- assert.equal(rec.mediaType, MediaType.Application);
245
- assert.equal(rec.mediaSubtype, "octet-stream");
246
- });
247
-
248
- it("defaults transferEncoding to 7BIT and sizeOctets to 0 when absent", () => {
249
- const [rec] = walkMimeStructure({ type: "text/plain" });
250
- assert.equal(rec.transferEncoding, "7BIT");
251
- assert.equal(rec.sizeOctets, 0);
252
- });
253
-
254
- // Regression test for the DynamoDB "multiple operations on one item" flake.
255
- // When imapflow returns a message/rfc822 attachment, it may attach the inner
256
- // message body as a childNode with part="" (no path). The old code gave every
257
- // such node ROOT_PART_PATH ("0"), colliding with the outer root. The fix
258
- // assigns a synthetic path "<parentPath>.<siblingIndex>" to keep keys unique.
259
- it("assigns unique partPaths to message/rfc822 inner-body nodes that have no part field", () => {
260
- const rfc822: MimeNode = {
261
- type: "multipart/mixed",
262
- // root has no part
263
- childNodes: [
264
- {
265
- type: "text/plain",
266
- part: "1",
267
- encoding: "7bit",
268
- size: 20,
269
- },
270
- {
271
- type: "message/rfc822",
272
- part: "2",
273
- size: 300,
274
- // imapflow surfaces the inner body as a child with no part
275
- childNodes: [
276
- {
277
- type: "text/plain",
278
- // no part — this was the bug
279
- encoding: "7bit",
280
- size: 50,
281
- },
282
- ],
283
- },
284
- ],
285
- };
286
-
287
- const records = walkMimeStructure(rfc822);
288
- const paths = records.map((r) => r.partPath);
289
-
290
- // All four paths must be unique.
291
- assert.equal(
292
- new Set(paths).size,
293
- records.length,
294
- `Duplicate partPaths detected: ${paths.join(", ")}`,
295
- );
296
-
297
- // Root gets the canonical ROOT_PART_PATH.
298
- assert.equal(paths[0], ROOT_PART_PATH);
299
-
300
- // The inner body without a part field gets a synthetic path, not "0".
301
- const innerBody = records.find(
302
- (r) => r.mediaSubtype === "plain" && r.partPath !== "1",
303
- );
304
- assert.ok(innerBody, "inner text/plain should be in the records");
305
- assert.notEqual(
306
- innerBody.partPath,
307
- ROOT_PART_PATH,
308
- `inner body should not reuse ROOT_PART_PATH but got ${innerBody.partPath}`,
309
- );
310
- });
311
- });
@@ -1,306 +0,0 @@
1
- /**
2
- * MIME structure walker.
3
- *
4
- * Flattens an IMAP BODYSTRUCTURE tree (as produced by ImapFlow's
5
- * `MessageStructureObject`) into a list of `BodyPartRecord`s ready to be
6
- * persisted to the BodyPart / BodyPartParameter tables.
7
- *
8
- * Inputs are intentionally typed against a structural subset rather than
9
- * pulling the imapflow types directly so the walker can be unit-tested
10
- * without spinning up an IMAP connection.
11
- */
12
-
13
- import { ROOT_PART_PATH } from "@remit/data-ports/id";
14
- import {
15
- ContentDisposition,
16
- MediaType,
17
- MultipartSubtype,
18
- TransferEncoding,
19
- } from "@remit/domain-enums";
20
-
21
- export { ROOT_PART_PATH };
22
-
23
- export type MediaTypeValue = (typeof MediaType)[keyof typeof MediaType];
24
- export type TransferEncodingValue =
25
- (typeof TransferEncoding)[keyof typeof TransferEncoding];
26
- export type ContentDispositionValue =
27
- (typeof ContentDisposition)[keyof typeof ContentDisposition];
28
- export type MultipartSubtypeValue =
29
- (typeof MultipartSubtype)[keyof typeof MultipartSubtype];
30
-
31
- /**
32
- * Structural shape of an IMAP BODYSTRUCTURE node. Mirrors imapflow's
33
- * `MessageStructureObject` minus the fields we don't read so this module
34
- * stays free of an imapflow dependency.
35
- */
36
- export interface MimeNode {
37
- /**
38
- * Dot-numbered MIME path (e.g., "1", "1.1", "2"). Empty/undefined for
39
- * the root node — the walker assigns "0" to it for storage stability.
40
- */
41
- part?: string;
42
- /** Full Content-Type (e.g., "text/plain", "multipart/mixed"). */
43
- type: string;
44
- /** Content-Type parameters (charset, boundary, name, ...). */
45
- parameters?: Record<string, string>;
46
- /** Content-ID header value (without angle brackets). */
47
- id?: string;
48
- /** Content-Description header value. */
49
- description?: string;
50
- /** Transfer encoding (7bit, 8bit, base64, quoted-printable, binary). */
51
- encoding?: string;
52
- /** Encoded byte size of this part. */
53
- size?: number;
54
- /** Line count for text/* parts. */
55
- lineCount?: number;
56
- /** MD5 hash. */
57
- md5?: string;
58
- /** Content-Disposition (inline | attachment). */
59
- disposition?: string;
60
- /** Content-Disposition parameters (filename, ...). */
61
- dispositionParameters?: Record<string, string>;
62
- /** Content-Language values. */
63
- language?: string[];
64
- /** Content-Location URI. */
65
- location?: string;
66
- /** Children for multipart nodes. */
67
- childNodes?: MimeNode[];
68
- }
69
-
70
- export interface BodyPartParameterRecord {
71
- parameterName: string;
72
- parameterValue: string;
73
- }
74
-
75
- /**
76
- * Flattened MIME node ready to upsert. `parentPartPath` is the parent
77
- * node's `partPath` (or null for the root) so the caller can map paths to
78
- * the deterministic `bodyPartId`s without re-walking the tree.
79
- */
80
- export interface BodyPartRecord {
81
- partPath: string;
82
- parentPartPath: string | null;
83
- mediaType: MediaTypeValue;
84
- mediaSubtype: string;
85
- contentId?: string;
86
- contentDescription?: string;
87
- transferEncoding: TransferEncodingValue;
88
- sizeOctets: number;
89
- lineCount?: number;
90
- md5Hash?: string;
91
- disposition?: ContentDispositionValue;
92
- dispositionFilename?: string;
93
- language?: string;
94
- location?: string;
95
- isMultipart: boolean;
96
- multipartSubtype?: MultipartSubtypeValue;
97
- parameters: BodyPartParameterRecord[];
98
- }
99
-
100
- const MEDIA_TYPE_BY_TOP_LEVEL: Record<string, MediaTypeValue> = {
101
- text: MediaType.Text,
102
- image: MediaType.Image,
103
- audio: MediaType.Audio,
104
- video: MediaType.Video,
105
- application: MediaType.Application,
106
- multipart: MediaType.Multipart,
107
- message: MediaType.Message,
108
- };
109
-
110
- const MULTIPART_SUBTYPE_BY_VALUE: Record<string, MultipartSubtypeValue> = {
111
- mixed: MultipartSubtype.Mixed,
112
- alternative: MultipartSubtype.Alternative,
113
- digest: MultipartSubtype.Digest,
114
- parallel: MultipartSubtype.Parallel,
115
- related: MultipartSubtype.Related,
116
- signed: MultipartSubtype.Signed,
117
- encrypted: MultipartSubtype.Encrypted,
118
- "form-data": MultipartSubtype.FormData,
119
- report: MultipartSubtype.Report,
120
- };
121
-
122
- const TRANSFER_ENCODING_BY_VALUE: Record<string, TransferEncodingValue> = {
123
- "7bit": TransferEncoding.SevenBit,
124
- "8bit": TransferEncoding.EightBit,
125
- binary: TransferEncoding.Binary,
126
- base64: TransferEncoding.Base64,
127
- "quoted-printable": TransferEncoding.QuotedPrintable,
128
- };
129
-
130
- const DISPOSITION_BY_VALUE: Record<string, ContentDispositionValue> = {
131
- inline: ContentDisposition.Inline,
132
- attachment: ContentDisposition.Attachment,
133
- };
134
-
135
- const FALLBACK_MEDIA_SUBTYPE = "octet-stream";
136
-
137
- /**
138
- * Split a Content-Type into a known media type and its subtype, tolerating
139
- * malformed values. The Content-Type is copied from an untrusted header, so a
140
- * missing "/", an unknown top-level type, or an empty subtype must not throw
141
- * (that would drop the whole message). RFC 2046 §4.5.3 makes
142
- * `application/octet-stream` the defined default for an unrecognized type, so
143
- * that is the fallback; the raw subtype is preserved where present.
144
- */
145
- const splitContentType = (
146
- type: string,
147
- ): { mediaType: MediaTypeValue; mediaSubtype: string } => {
148
- const slashIndex = type.indexOf("/");
149
- if (slashIndex < 0) {
150
- return {
151
- mediaType: MediaType.Application,
152
- mediaSubtype: FALLBACK_MEDIA_SUBTYPE,
153
- };
154
- }
155
- const topRaw = type.slice(0, slashIndex);
156
- const subRaw = type.slice(slashIndex + 1);
157
- const top = topRaw.toLowerCase();
158
- const mediaType = MEDIA_TYPE_BY_TOP_LEVEL[top] ?? MediaType.Application;
159
- const subLower = subRaw.toLowerCase();
160
- const mediaSubtype = subLower.length > 0 ? subLower : FALLBACK_MEDIA_SUBTYPE;
161
- return { mediaType, mediaSubtype };
162
- };
163
-
164
- /**
165
- * An absent Content-Transfer-Encoding means 7bit (RFC 2045 §6.1); an
166
- * unrecognized token is treated as opaque bytes rather than throwing, so a
167
- * message carrying an exotic encoding still syncs.
168
- */
169
- const mapTransferEncoding = (
170
- encoding: string | undefined,
171
- ): TransferEncodingValue => {
172
- if (!encoding) return TransferEncoding.SevenBit;
173
- return (
174
- TRANSFER_ENCODING_BY_VALUE[encoding.toLowerCase()] ??
175
- TransferEncoding.Binary
176
- );
177
- };
178
-
179
- /**
180
- * An unknown Content-Disposition is treated as absent (undefined) rather than
181
- * throwing, so a message with a malformed disposition still syncs.
182
- */
183
- const mapDisposition = (
184
- disposition: string | undefined,
185
- ): ContentDispositionValue | undefined => {
186
- if (!disposition) return undefined;
187
- return DISPOSITION_BY_VALUE[disposition.toLowerCase()];
188
- };
189
-
190
- const mapMultipartSubtype = (
191
- subtype: string,
192
- ): MultipartSubtypeValue | undefined => {
193
- const mapped = MULTIPART_SUBTYPE_BY_VALUE[subtype.toLowerCase()];
194
- if (!mapped) return undefined;
195
- return mapped;
196
- };
197
-
198
- const stripAngles = (value: string): string => value.replace(/^<+|>+$/g, "");
199
-
200
- const flattenParameters = (
201
- params: Record<string, string> | undefined,
202
- ): BodyPartParameterRecord[] => {
203
- if (!params) return [];
204
- const entries = Object.entries(params).filter(([name, value]) => {
205
- if (typeof name !== "string" || name.length === 0) return false;
206
- if (typeof value !== "string") return false;
207
- return true;
208
- });
209
- entries.sort((a, b) => a[0].localeCompare(b[0]));
210
- return entries.map(([parameterName, parameterValue]) => ({
211
- parameterName,
212
- parameterValue,
213
- }));
214
- };
215
-
216
- const toRecord = (
217
- node: MimeNode,
218
- partPath: string,
219
- parentPartPath: string | null,
220
- ): BodyPartRecord => {
221
- const { mediaType, mediaSubtype } = splitContentType(node.type);
222
- const isMultipart = mediaType === MediaType.Multipart;
223
- const multipartSubtype = isMultipart
224
- ? mapMultipartSubtype(mediaSubtype)
225
- : undefined;
226
-
227
- const dispositionFilename =
228
- node.dispositionParameters?.filename ?? node.parameters?.name ?? undefined;
229
-
230
- const language = node.language?.[0];
231
-
232
- const record: BodyPartRecord = {
233
- partPath,
234
- parentPartPath,
235
- mediaType,
236
- mediaSubtype,
237
- transferEncoding: mapTransferEncoding(node.encoding),
238
- sizeOctets: typeof node.size === "number" ? node.size : 0,
239
- isMultipart,
240
- parameters: flattenParameters(node.parameters),
241
- };
242
-
243
- if (node.id) record.contentId = stripAngles(node.id);
244
- if (node.description) record.contentDescription = node.description;
245
- if (typeof node.lineCount === "number") record.lineCount = node.lineCount;
246
- if (node.md5) record.md5Hash = node.md5;
247
- const mappedDisposition = mapDisposition(node.disposition);
248
- if (mappedDisposition) record.disposition = mappedDisposition;
249
- if (dispositionFilename) record.dispositionFilename = dispositionFilename;
250
- if (language) record.language = language;
251
- if (node.location) record.location = node.location;
252
- if (multipartSubtype) record.multipartSubtype = multipartSubtype;
253
-
254
- return record;
255
- };
256
-
257
- /**
258
- * Walk an IMAP BODYSTRUCTURE tree depth-first and return a flat list of
259
- * `BodyPartRecord`s. The root node uses `ROOT_PART_PATH` ("0"); all other
260
- * nodes use the dot-numbered IMAP path that ImapFlow assigns (e.g., "1",
261
- * "1.2", "2.1.3"). Every field comes from an untrusted BODYSTRUCTURE, so an
262
- * unrecognized MIME type, transfer encoding, or Content-Disposition maps to a
263
- * safe default rather than throwing — one malformed part must never drop the
264
- * whole message.
265
- *
266
- * **Part-path uniqueness**: imapflow gives the root of a message/rfc822
267
- * node's encapsulated message the same `part` as the node itself, and some
268
- * servers give it none. That root is stored as `<messagePath>.0` — the
269
- * encapsulated message's own ROOT_PART_PATH — so it can neither overwrite the
270
- * message/rfc822 row nor collide with its children (`<messagePath>.1`, ...).
271
- * Any other non-root node without a `part` receives a synthetic path
272
- * `<parentPath>.<siblingIndex>` that is stable across repeated syncs.
273
- */
274
- export const walkMimeStructure = (root: MimeNode): BodyPartRecord[] => {
275
- const out: BodyPartRecord[] = [];
276
-
277
- const visit = (
278
- node: MimeNode,
279
- parentPath: string | null,
280
- parentIsMessage: boolean,
281
- siblingIndex: number,
282
- ) => {
283
- let partPath: string;
284
- if (parentIsMessage && parentPath !== null) {
285
- partPath = `${parentPath}.${ROOT_PART_PATH}`;
286
- } else if (node.part && node.part.length > 0) {
287
- partPath = node.part;
288
- } else if (parentPath === null) {
289
- // True root of the BODYSTRUCTURE tree.
290
- partPath = ROOT_PART_PATH;
291
- } else {
292
- partPath = `${parentPath}.${siblingIndex}`;
293
- }
294
- const record = toRecord(node, partPath, parentPath);
295
- out.push(record);
296
- const isMessage = record.mediaType === MediaType.Message;
297
- const children = node.childNodes ?? [];
298
- for (let i = 0; i < children.length; i++) {
299
- // biome-ignore lint/style/noNonNullAssertion: value is guaranteed by caller contract
300
- visit(children[i]!, partPath, isMessage, i + 1);
301
- }
302
- };
303
-
304
- visit(root, null, false, 0);
305
- return out;
306
- };
@@ -1,93 +0,0 @@
1
- import { SendMessageCommand, type SQSClient } from "@aws-sdk/client-sqs";
2
- import type {
3
- CreateOrganizeJobRequestInput,
4
- IOrganizeJobRequestRepository,
5
- OrganizeJobRequestItem,
6
- } from "@remit/data-ports";
7
- import { FilterMatchOperator } from "@remit/domain-enums";
8
- import { createQueueProducer } from "@remit/sqs-client/producer";
9
- import { NO_ACTION } from "./filters/match.js";
10
-
11
- const ORGANIZE_JOB_TTL_SECONDS = 7 * 24 * 60 * 60;
12
-
13
- export interface OrganizeJobOwner {
14
- accountConfigId: string;
15
- userId: string;
16
- }
17
-
18
- export type OrganizeJobPredicate = Pick<
19
- CreateOrganizeJobRequestInput,
20
- "matchOperator" | "literalClauses" | "actionLabelId" | "actionMailboxId"
21
- >;
22
-
23
- export type OrganizeJobRequest = OrganizeJobOwner &
24
- ({ predicate: OrganizeJobPredicate } | { filterId: string });
25
-
26
- export interface OrganizeJobLogger {
27
- info(obj: Record<string, unknown>, msg: string): void;
28
- }
29
-
30
- export interface OrganizeJobDeps {
31
- organizeJobRequest: Pick<IOrganizeJobRequestRepository, "create">;
32
- sqsQueueUrl: string;
33
- logger: OrganizeJobLogger;
34
- }
35
-
36
- const producers = new Map<string, SQSClient>();
37
-
38
- const producerFor = (queueUrl: string): SQSClient => {
39
- const existing = producers.get(queueUrl);
40
- if (existing) return existing;
41
- const producer = createQueueProducer({ queueUrl });
42
- producers.set(queueUrl, producer);
43
- return producer;
44
- };
45
-
46
- const jobColumns = (
47
- request: OrganizeJobRequest,
48
- ): Omit<CreateOrganizeJobRequestInput, keyof OrganizeJobOwner | "ttl"> => {
49
- if ("filterId" in request) {
50
- return {
51
- filterId: request.filterId,
52
- matchOperator: FilterMatchOperator.And,
53
- literalClauses: [],
54
- actionLabelId: NO_ACTION,
55
- actionMailboxId: NO_ACTION,
56
- };
57
- }
58
- return { filterId: NO_ACTION, ...request.predicate };
59
- };
60
-
61
- // A filter job names only the filter; the worker reads it when it runs.
62
- export const queueOrganizeJob = async (
63
- deps: OrganizeJobDeps,
64
- request: OrganizeJobRequest,
65
- ): Promise<OrganizeJobRequestItem> => {
66
- const job = await deps.organizeJobRequest.create({
67
- accountConfigId: request.accountConfigId,
68
- userId: request.userId,
69
- ...jobColumns(request),
70
- ttl: Math.floor(Date.now() / 1000) + ORGANIZE_JOB_TTL_SECONDS,
71
- });
72
-
73
- await producerFor(deps.sqsQueueUrl).send(
74
- new SendMessageCommand({
75
- QueueUrl: deps.sqsQueueUrl,
76
- MessageBody: JSON.stringify({
77
- type: "OrganizeJob",
78
- accountConfigId: job.accountConfigId,
79
- organizeJobId: job.organizeJobId,
80
- }),
81
- }),
82
- );
83
-
84
- deps.logger.info(
85
- {
86
- accountConfigId: job.accountConfigId,
87
- organizeJobId: job.organizeJobId,
88
- filterId: job.filterId,
89
- },
90
- "Organize back-apply job initiated",
91
- );
92
- return job;
93
- };