@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,329 @@
1
+ /**
2
+ * Deterministic random MIME tree generator for the body-part mapper
3
+ * property test (issue #395 PR C).
4
+ *
5
+ * Three pieces:
6
+ *
7
+ * 1. `createRng(seed)` — small xorshift32 PRNG. Same seed → same sequence
8
+ * so failing CI runs reproduce locally via `MAPPER_PROPERTY_SEED`.
9
+ *
10
+ * 2. `generateMimeTree(rng, opts)` — synthesise a random tree:
11
+ * - root is either a leaf or a `multipart/<mixed|alternative|related>`
12
+ * - multiparts have N ∈ [1, 5] children
13
+ * - each child is leaf or multipart, depth capped at `maxDepth` (4)
14
+ * - leaves draw uniformly from a fixed set of media types
15
+ * - some leaves get a `dispositionFilename`, some a `contentId`,
16
+ * some both, most neither
17
+ * - leaves are mostly non-empty; a small fraction get zero bytes to
18
+ * exercise the empty-leaf short-circuit in the mapper
19
+ *
20
+ * 3. `renderEml(tree)` + `treeToBodyParts(tree)` — produce a paired
21
+ * `.eml` string and the `MapperInput[]` rows the mime-walker would
22
+ * emit for that tree. Both walk the same tree, so partPath, content
23
+ * type, filename and contentId stay aligned by construction.
24
+ *
25
+ * No external deps. Built on Node primitives only (`node:buffer`,
26
+ * `node:string_decoder` via `Buffer.from(..., 'base64')`).
27
+ */
28
+ import { Buffer } from "node:buffer";
29
+ export const createRng = (seed) => {
30
+ // xorshift32 needs a non-zero 32-bit state.
31
+ let state = (seed | 0) === 0 ? 0xc0dec0de | 0 : seed | 0;
32
+ const next = () => {
33
+ state ^= state << 13;
34
+ state ^= state >>> 17;
35
+ state ^= state << 5;
36
+ // Map signed-int range to [0, 1).
37
+ return ((state >>> 0) % 0xffffffff) / 0xffffffff;
38
+ };
39
+ return {
40
+ next,
41
+ int: (maxExclusive) => Math.floor(next() * maxExclusive),
42
+ pick: (items) => items[Math.floor(next() * items.length)],
43
+ bool: (probability) => next() < probability,
44
+ };
45
+ };
46
+ const LEAF_CONTENT_TYPES = [
47
+ "text/plain",
48
+ "text/html",
49
+ "application/pdf",
50
+ "image/png",
51
+ "application/octet-stream",
52
+ ];
53
+ const MULTIPART_SUBTYPES = [
54
+ "mixed",
55
+ "alternative",
56
+ "related",
57
+ ];
58
+ const DEFAULT_OPTS = {
59
+ maxDepth: 4,
60
+ rootMultipartProb: 0.85,
61
+ emptyLeafProb: 0.05,
62
+ };
63
+ const extensionFor = (contentType) => {
64
+ switch (contentType) {
65
+ case "text/plain":
66
+ return "txt";
67
+ case "text/html":
68
+ return "html";
69
+ case "application/pdf":
70
+ return "pdf";
71
+ case "image/png":
72
+ return "png";
73
+ case "application/octet-stream":
74
+ return "bin";
75
+ }
76
+ };
77
+ const randomBasename = (rng) => {
78
+ // Short ASCII basename — keeps the EML readable.
79
+ const letters = "abcdefghijklmnopqrstuvwxyz";
80
+ const len = 3 + rng.int(5);
81
+ let s = "";
82
+ for (let i = 0; i < len; i++)
83
+ s += letters[rng.int(letters.length)];
84
+ return s;
85
+ };
86
+ const generateLeafBytes = (rng, partPath, contentType, emptyLeafProb) => {
87
+ if (rng.bool(emptyLeafProb))
88
+ return Buffer.alloc(0);
89
+ return Buffer.from(`leaf-${partPath}-${contentType}`, "utf8");
90
+ };
91
+ const buildLeaf = (rng, partPath, emptyLeafProb) => {
92
+ const contentType = rng.pick(LEAF_CONTENT_TYPES);
93
+ const bytes = generateLeafBytes(rng, partPath, contentType, emptyLeafProb);
94
+ const leaf = { kind: "leaf", contentType, bytes };
95
+ const ext = extensionFor(contentType);
96
+ const isTextStar = contentType.startsWith("text/");
97
+ // Random attachment metadata:
98
+ // - ~25% get a filename (more common on non-text leaves)
99
+ // - ~10% get a contentId (image/inline-style)
100
+ // - text/plain and text/html mostly stay bare (they route via parsed.text/html)
101
+ const wantsFilename = isTextStar ? rng.bool(0.05) : rng.bool(0.45);
102
+ const wantsContentId = rng.bool(0.15);
103
+ if (wantsFilename) {
104
+ leaf.filename = `${randomBasename(rng)}.${ext}`;
105
+ leaf.disposition = rng.bool(0.5) ? "attachment" : "inline";
106
+ }
107
+ if (wantsContentId) {
108
+ leaf.contentId = `${randomBasename(rng)}@example.org`;
109
+ // inline content-id attachments are conventionally inline.
110
+ if (!leaf.disposition)
111
+ leaf.disposition = "inline";
112
+ }
113
+ return leaf;
114
+ };
115
+ /**
116
+ * Decay child probability with depth: at depth 4 (max), all children are
117
+ * leaves to guarantee termination.
118
+ */
119
+ const childMultipartProb = (depth, maxDepth) => {
120
+ if (depth >= maxDepth - 1)
121
+ return 0;
122
+ // 0.6 → 0.4 → 0.2 → 0 from depth 0 outward.
123
+ const slots = maxDepth - 1;
124
+ const remaining = slots - depth;
125
+ return Math.max(0, (remaining / slots) * 0.6);
126
+ };
127
+ const buildMultipart = (rng, depth, maxDepth, emptyLeafProb, partPath) => {
128
+ const subtype = rng.pick(MULTIPART_SUBTYPES);
129
+ const childCount = 1 + rng.int(5); // [1, 5]
130
+ const childMpProb = childMultipartProb(depth, maxDepth);
131
+ const children = [];
132
+ for (let i = 0; i < childCount; i++) {
133
+ const childPath = partPath === "0" ? String(i + 1) : `${partPath}.${i + 1}`;
134
+ const childIsMultipart = rng.bool(childMpProb);
135
+ if (childIsMultipart) {
136
+ children.push(buildMultipart(rng, depth + 1, maxDepth, emptyLeafProb, childPath));
137
+ }
138
+ else {
139
+ children.push(buildLeaf(rng, childPath, emptyLeafProb));
140
+ }
141
+ }
142
+ return { kind: "multipart", subtype, children };
143
+ };
144
+ export const generateMimeTree = (rng, opts = {}) => {
145
+ const o = { ...DEFAULT_OPTS, ...opts };
146
+ const rootIsMultipart = rng.bool(o.rootMultipartProb);
147
+ if (!rootIsMultipart) {
148
+ return buildLeaf(rng, "0", o.emptyLeafProb);
149
+ }
150
+ return buildMultipart(rng, 0, o.maxDepth, o.emptyLeafProb, "0");
151
+ };
152
+ /* -------------------------------------------------------------------- */
153
+ /* BodyPart projection */
154
+ /* -------------------------------------------------------------------- */
155
+ const splitContentType = (contentType) => {
156
+ const [top, sub] = contentType.split("/");
157
+ const map = {
158
+ text: "TEXT",
159
+ image: "IMAGE",
160
+ application: "APPLICATION",
161
+ multipart: "MULTIPART",
162
+ };
163
+ const mediaType = map[top];
164
+ if (!mediaType) {
165
+ throw new Error(`mime-tree-generator: unknown top-level type "${top}"`);
166
+ }
167
+ return { mediaType, mediaSubtype: sub };
168
+ };
169
+ /**
170
+ * Walk the tree the same way `mime-walker.ts` would and emit
171
+ * `MapperInput[]` in declaration order. Root path is "0"; subsequent
172
+ * leaves are "1", "2", "1.1", "1.2", ... — matches `walkMimeStructure`.
173
+ */
174
+ export const treeToBodyParts = (tree) => {
175
+ const out = [];
176
+ const visit = (node, partPath) => {
177
+ if (node.kind === "leaf") {
178
+ const { mediaType, mediaSubtype } = splitContentType(node.contentType);
179
+ const row = {
180
+ partPath,
181
+ isMultipart: false,
182
+ mediaType,
183
+ mediaSubtype,
184
+ sizeOctets: node.bytes.length,
185
+ ...(node.filename ? { dispositionFilename: node.filename } : {}),
186
+ ...(node.contentId ? { contentId: node.contentId } : {}),
187
+ ...(node.disposition ? { disposition: node.disposition } : {}),
188
+ };
189
+ out.push(row);
190
+ return;
191
+ }
192
+ out.push({
193
+ partPath,
194
+ isMultipart: true,
195
+ mediaType: "MULTIPART",
196
+ mediaSubtype: node.subtype,
197
+ });
198
+ for (let i = 0; i < node.children.length; i++) {
199
+ const childPath = partPath === "0" ? String(i + 1) : `${partPath}.${i + 1}`;
200
+ visit(node.children[i], childPath);
201
+ }
202
+ };
203
+ visit(tree, "0");
204
+ return out;
205
+ };
206
+ /* -------------------------------------------------------------------- */
207
+ /* EML renderer */
208
+ /* -------------------------------------------------------------------- */
209
+ /**
210
+ * Render a buffer as quoted-printable in a very conservative dialect:
211
+ * ASCII printable stays raw, anything else hex-encoded. Line length
212
+ * limit ignored — fixtures are short enough that no fold is needed for
213
+ * our property test. (mailparser tolerates long QP lines.)
214
+ */
215
+ const encodeQuotedPrintable = (buf) => {
216
+ let out = "";
217
+ for (const b of buf) {
218
+ if (b === 0x09 || b === 0x20 || (b >= 0x21 && b <= 0x7e && b !== 0x3d)) {
219
+ out += String.fromCharCode(b);
220
+ }
221
+ else if (b === 0x0a) {
222
+ out += "\r\n";
223
+ }
224
+ else {
225
+ out += `=${b.toString(16).toUpperCase().padStart(2, "0")}`;
226
+ }
227
+ }
228
+ return out;
229
+ };
230
+ const encodeBase64 = (buf) => {
231
+ const b64 = buf.toString("base64");
232
+ // Wrap at 76 columns per RFC 2045.
233
+ const lines = [];
234
+ for (let i = 0; i < b64.length; i += 76)
235
+ lines.push(b64.slice(i, i + 76));
236
+ return lines.join("\r\n");
237
+ };
238
+ const transferEncodingFor = (contentType) => {
239
+ if (contentType === "text/plain")
240
+ return "quoted-printable";
241
+ if (contentType === "text/html")
242
+ return "7bit";
243
+ return "base64";
244
+ };
245
+ const renderLeaf = (leaf) => {
246
+ const lines = [];
247
+ const params = [];
248
+ if (leaf.contentType.startsWith("text/"))
249
+ params.push("charset=us-ascii");
250
+ if (leaf.filename)
251
+ params.push(`name="${leaf.filename}"`);
252
+ const ctypeLine = params.length > 0
253
+ ? `Content-Type: ${leaf.contentType}; ${params.join("; ")}`
254
+ : `Content-Type: ${leaf.contentType}`;
255
+ lines.push(ctypeLine);
256
+ const cte = transferEncodingFor(leaf.contentType);
257
+ lines.push(`Content-Transfer-Encoding: ${cte}`);
258
+ if (leaf.disposition) {
259
+ const dispLine = leaf.filename
260
+ ? `Content-Disposition: ${leaf.disposition}; filename="${leaf.filename}"`
261
+ : `Content-Disposition: ${leaf.disposition}`;
262
+ lines.push(dispLine);
263
+ }
264
+ if (leaf.contentId) {
265
+ lines.push(`Content-ID: <${leaf.contentId}>`);
266
+ }
267
+ let body = "";
268
+ if (leaf.bytes.length > 0) {
269
+ if (cte === "base64")
270
+ body = encodeBase64(leaf.bytes);
271
+ else if (cte === "quoted-printable")
272
+ body = encodeQuotedPrintable(leaf.bytes);
273
+ else
274
+ body = leaf.bytes.toString("utf8");
275
+ }
276
+ lines.push("");
277
+ lines.push(body);
278
+ return lines.join("\r\n");
279
+ };
280
+ /**
281
+ * Deterministic boundary derived from the seed + node path. Each
282
+ * multipart node gets its own unique boundary so nested rendering is
283
+ * unambiguous.
284
+ */
285
+ const boundaryFor = (seed, partPath) => `=_b_${seed.toString(16)}_${partPath.replace(/\./g, "_")}_=`;
286
+ const renderNode = (node, partPath, seed) => {
287
+ if (node.kind === "leaf")
288
+ return renderLeaf(node);
289
+ const boundary = boundaryFor(seed, partPath);
290
+ const out = [];
291
+ out.push(`Content-Type: multipart/${node.subtype}; boundary="${boundary}"`);
292
+ out.push("");
293
+ for (let i = 0; i < node.children.length; i++) {
294
+ const childPath = partPath === "0" ? String(i + 1) : `${partPath}.${i + 1}`;
295
+ out.push(`--${boundary}`);
296
+ out.push(renderNode(node.children[i], childPath, seed));
297
+ }
298
+ out.push(`--${boundary}--`);
299
+ out.push("");
300
+ return out.join("\r\n");
301
+ };
302
+ export const renderEml = (tree, opts) => {
303
+ const headers = [];
304
+ headers.push(`From: ${opts.from ?? "alice@example.org"}`);
305
+ headers.push(`To: ${opts.to ?? "bob@example.org"}`);
306
+ headers.push(`Subject: ${opts.subject ?? `property-${opts.seed}`}`);
307
+ headers.push("Date: Thu, 28 May 2026 09:00:00 +0000");
308
+ headers.push(`Message-Id: <${opts.messageId ?? `property-${opts.seed}@example.org`}>`);
309
+ headers.push("MIME-Version: 1.0");
310
+ if (tree.kind === "leaf") {
311
+ // Flat single-leaf message: leaf headers merge with the envelope.
312
+ const leafRendered = renderLeaf(tree);
313
+ // Drop the duplicate trailing newlines and merge.
314
+ return `${headers.join("\r\n")}\r\n${leafRendered}\r\n`;
315
+ }
316
+ const boundary = boundaryFor(opts.seed, "0");
317
+ const out = [];
318
+ out.push(...headers);
319
+ out.push(`Content-Type: multipart/${tree.subtype}; boundary="${boundary}"`);
320
+ out.push("");
321
+ for (let i = 0; i < tree.children.length; i++) {
322
+ const childPath = String(i + 1);
323
+ out.push(`--${boundary}`);
324
+ out.push(renderNode(tree.children[i], childPath, opts.seed));
325
+ }
326
+ out.push(`--${boundary}--`);
327
+ out.push("");
328
+ return out.join("\r\n");
329
+ };
@@ -0,0 +1,69 @@
1
+ import type { AccountItem, CreateMessageInput, CreateThreadMessageInput, MailboxItem, MailboxSpecialUseValue, MessageItem, ThreadMessageItem } from "@remit/data-ports";
2
+ import type { StorageService } from "@remit/storage-service";
3
+ import type { FakeImapServer } from "./fake-imap.js";
4
+ export { failingQueue, type RecordedMessage, type RecordingQueue, recordingQueue, } from "@remit/data-ports/recording-queue";
5
+ export declare const openStore: () => Promise<{
6
+ storage: () => StorageService;
7
+ brokenStorage: () => StorageService;
8
+ close: () => Promise<void>;
9
+ accountConfig: import("@remit/drizzle-service").AccountConfigRepo;
10
+ account: import("@remit/drizzle-service").AccountRepo;
11
+ accountSetting: import("@remit/drizzle-service").AccountSettingRepo;
12
+ address: import("@remit/drizzle-service").AddressRepo;
13
+ mailbox: import("@remit/drizzle-service").MailboxRepo;
14
+ mailboxSpecialUse: import("@remit/drizzle-service").MailboxSpecialUseRepo;
15
+ mailboxLock: import("@remit/drizzle-service").MailboxLockRepo;
16
+ message: import("@remit/drizzle-service").DrizzleMessageRepository;
17
+ messageFlag: import("@remit/drizzle-service").DrizzleMessageFlagRepository;
18
+ outboxMessage: import("@remit/drizzle-service").OutboxMessageRepo;
19
+ outboxAttachment: import("@remit/drizzle-service").OutboxAttachmentRepo;
20
+ threadMessage: import("@remit/drizzle-service").DrizzleThreadMessageRepository;
21
+ envelope: import("@remit/drizzle-service").DrizzleEnvelopeRepository;
22
+ accountExportRequest: import("@remit/drizzle-service").AccountExportRequestRepo;
23
+ configImport: import("@remit/drizzle-service").ConfigImportRepo;
24
+ quarantine: import("@remit/drizzle-service").QuarantineRepo;
25
+ syncWarning: import("@remit/drizzle-service").SyncWarningRepo;
26
+ organizeJobRequest: import("@remit/drizzle-service").OrganizeJobRequestRepo;
27
+ placementMove: import("@remit/drizzle-service").MessagePlacementMoveRepo;
28
+ flagPush: import("@remit/drizzle-service").MessageFlagPushRepo;
29
+ filter: import("@remit/drizzle-service").FilterRepo;
30
+ label: import("@remit/drizzle-service").LabelRepo;
31
+ messageLabel: import("@remit/drizzle-service").MessageLabelRepo;
32
+ senderSignerStanding: import("@remit/drizzle-service").SenderSignerStandingRepo;
33
+ calendarCollection: import("@remit/drizzle-service").CalendarCollectionRepo;
34
+ calendarObject: import("@remit/drizzle-service").CalendarObjectRepo;
35
+ calendarEventIndex: import("@remit/drizzle-service").CalendarEventIndexRepo;
36
+ calendarFeedToken: import("@remit/drizzle-service").CalendarFeedTokenRepo;
37
+ calendarSuggestion: import("@remit/drizzle-service").CalendarSuggestionRepo;
38
+ calendarStore: import("@remit/drizzle-service").DrizzleCalendarStore;
39
+ }>;
40
+ export type Store = Awaited<ReturnType<typeof openStore>>;
41
+ export declare const seedAccount: (store: Store, overrides?: Partial<AccountItem>) => Promise<AccountItem>;
42
+ export declare const seedMailbox: (store: Store, account: Pick<AccountItem, "accountId">, fullPath: string, options?: {
43
+ specialUse?: MailboxSpecialUseValue;
44
+ overrides?: Partial<MailboxItem>;
45
+ }) => Promise<MailboxItem>;
46
+ export interface SeededMessage {
47
+ message: MessageItem;
48
+ threadMessage: ThreadMessageItem;
49
+ }
50
+ export declare const seedMessage: (store: Store, account: Pick<AccountItem, "accountConfigId">, mailbox: Pick<MailboxItem, "mailboxId">, uid: number, overrides?: {
51
+ message?: Partial<CreateMessageInput>;
52
+ threadMessage?: Partial<CreateThreadMessageInput>;
53
+ }) => Promise<SeededMessage>;
54
+ export declare const silentLog: {
55
+ info: () => void;
56
+ warn: () => void;
57
+ error: () => void;
58
+ debug: () => void;
59
+ };
60
+ export declare const syncFolder: (store: Store, account: Pick<AccountItem, "accountId" | "accountConfigId">, mailbox: Pick<MailboxItem, "mailboxId">, imap: FakeImapServer) => Promise<string[]>;
61
+ export declare const messageAtUid: (store: Store, mailbox: Pick<MailboxItem, "mailboxId">, uid: number) => Promise<MessageItem>;
62
+ export declare const arriveAt: (store: Store, imap: FakeImapServer, account: Pick<AccountItem, "accountId" | "accountConfigId">, mailbox: Pick<MailboxItem, "mailboxId" | "fullPath">, raw: Buffer) => Promise<string>;
63
+ export declare const storeBody: (store: Store, storage: StorageService, account: Pick<AccountItem, "accountId" | "accountConfigId">, messageId: string, raw: Buffer) => Promise<void>;
64
+ /**
65
+ * Another writer landing between a service's read and its write: `action` runs,
66
+ * for real, the next time `method` is called on `target`, and the call then
67
+ * proceeds as normal.
68
+ */
69
+ export declare const beforeNext: <T extends object, K extends keyof T>(target: T, method: K, action: () => Promise<unknown>) => void;
@@ -0,0 +1,193 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { copyFileSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { AccountAuthType, ConnectionState } from "@remit/domain-enums";
6
+ import { createSqliteRepositories, openSqliteDatabases, } from "@remit/drizzle-service";
7
+ import { applyShippedMigrations } from "@remit/drizzle-service/test-sqlite";
8
+ import { createFilesystemStorageService } from "@remit/storage-service/filesystem";
9
+ import Database from "better-sqlite3";
10
+ import { MessageSyncService } from "../message-sync.js";
11
+ import { SyncWarningService } from "../sync-warning.js";
12
+ export { failingQueue, recordingQueue, } from "@remit/data-ports/recording-queue";
13
+ const UPLOAD_URLS = {
14
+ origin: "https://reader.test",
15
+ signingSecret: "test-signing-secret",
16
+ };
17
+ /**
18
+ * The store the app opens: the committed migrations applied to a file, then
19
+ * that file opened the way the app opens it — as the app, mail and search
20
+ * database at once — with every repo the app composes.
21
+ * Storage made from it lives in the same directory and goes with it.
22
+ */
23
+ let migrated;
24
+ const migratedTemplate = () => {
25
+ if (migrated)
26
+ return migrated;
27
+ const dir = mkdtempSync(join(tmpdir(), "mbx-template-"));
28
+ const filename = join(dir, "template.sqlite");
29
+ const migrating = new Database(filename);
30
+ migrating.pragma("synchronous = OFF");
31
+ applyShippedMigrations(migrating);
32
+ migrating.close();
33
+ process.once("exit", () => rmSync(dir, { recursive: true, force: true }));
34
+ migrated = filename;
35
+ return filename;
36
+ };
37
+ export const openStore = async () => {
38
+ const dir = mkdtempSync(join(tmpdir(), "mbx-store-"));
39
+ const filename = join(dir, "store.sqlite");
40
+ copyFileSync(migratedTemplate(), filename);
41
+ const { close, ...databases } = await openSqliteDatabases({
42
+ app: filename,
43
+ mail: filename,
44
+ search: filename,
45
+ });
46
+ let storages = 0;
47
+ return {
48
+ ...createSqliteRepositories(databases),
49
+ storage: () => createFilesystemStorageService(join(dir, `storage-${++storages}`), UPLOAD_URLS),
50
+ brokenStorage: () => {
51
+ const root = join(dir, `broken-${++storages}`);
52
+ writeFileSync(root, "");
53
+ return createFilesystemStorageService(root);
54
+ },
55
+ close: async () => {
56
+ await close();
57
+ rmSync(dir, { recursive: true, force: true });
58
+ },
59
+ };
60
+ };
61
+ export const seedAccount = async (store, overrides = {}) => {
62
+ const config = await store.accountConfig.create({
63
+ accountConfigId: overrides.accountConfigId,
64
+ userId: "user-1",
65
+ name: "Me",
66
+ });
67
+ return store.account.create({
68
+ username: "me@example.com",
69
+ email: "me@example.com",
70
+ authType: AccountAuthType.Password,
71
+ passwordHash: '{"ciphertext":"x"}',
72
+ imapHost: "imap.example.com",
73
+ imapPort: 993,
74
+ imapTls: true,
75
+ imapStartTls: false,
76
+ isActive: true,
77
+ connectionState: ConnectionState.Authenticated,
78
+ ...overrides,
79
+ accountConfigId: config.accountConfigId,
80
+ });
81
+ };
82
+ export const seedMailbox = async (store, account, fullPath, options = {}) => {
83
+ const created = await store.mailbox.create({
84
+ accountId: account.accountId,
85
+ namespacePrefix: "",
86
+ hierarchyDelimiter: "/",
87
+ fullPath,
88
+ uidValidity: 7,
89
+ uidNext: 1,
90
+ highestModseq: "0",
91
+ messageCount: 0,
92
+ unseenCount: 0,
93
+ deletedCount: 0,
94
+ totalSize: 0,
95
+ lastSyncUid: 0,
96
+ highWaterMarkUid: 0,
97
+ lastMessageSyncAt: 0,
98
+ ...(options.specialUse ? { specialUse: [options.specialUse] } : {}),
99
+ ...options.overrides,
100
+ });
101
+ if (created.outcome !== "Created") {
102
+ throw new Error(`${fullPath} already exists`);
103
+ }
104
+ if (options.specialUse) {
105
+ await store.mailboxSpecialUse.create(created.mailbox.mailboxId, options.specialUse);
106
+ }
107
+ return created.mailbox;
108
+ };
109
+ export const seedMessage = async (store, account, mailbox, uid, overrides = {}) => {
110
+ const messageId = overrides.message?.messageId ?? randomUUID();
111
+ const message = await store.message.create({
112
+ messageId,
113
+ mailboxId: mailbox.mailboxId,
114
+ uid,
115
+ sequenceNumber: uid,
116
+ rfc822Size: 100,
117
+ internalDate: 1_700_000_000_000 + uid,
118
+ envelopeId: randomUUID(),
119
+ rootBodyPartId: randomUUID(),
120
+ status: "active",
121
+ syncStatus: "synced",
122
+ ...overrides.message,
123
+ });
124
+ const threadMessage = await store.threadMessage.create({
125
+ accountConfigId: account.accountConfigId,
126
+ threadId: messageId,
127
+ messageId,
128
+ mailboxId: message.mailboxId,
129
+ uid: message.uid,
130
+ referenceOrder: 0,
131
+ internalDate: message.internalDate,
132
+ sentDate: message.internalDate,
133
+ isRead: false,
134
+ isDeleted: false,
135
+ hasAttachment: false,
136
+ hasStars: false,
137
+ ...overrides.threadMessage,
138
+ });
139
+ return { message, threadMessage };
140
+ };
141
+ export const silentLog = {
142
+ info: () => { },
143
+ warn: () => { },
144
+ error: () => { },
145
+ debug: () => { },
146
+ };
147
+ export const syncFolder = async (store, account, mailbox, imap) => {
148
+ const connection = imap.connection();
149
+ const service = new MessageSyncService({ getConnection: () => connection, close: async () => { } }, store.mailbox, store.mailboxSpecialUse, store.message, store.envelope, store.address, store.threadMessage, new SyncWarningService(store.syncWarning), silentLog, store.flagPush, store.messageFlag);
150
+ const synced = [];
151
+ for (;;) {
152
+ const result = await service.syncMessages(mailbox.mailboxId, account.accountId, account.accountConfigId);
153
+ synced.push(...result.syncedMessageIds);
154
+ if (!result.hasMore)
155
+ return synced;
156
+ }
157
+ };
158
+ export const messageAtUid = async (store, mailbox, uid) => {
159
+ const found = (await store.message.listAllByMailbox(mailbox.mailboxId)).find((message) => message.uid === uid);
160
+ if (!found)
161
+ throw new Error(`no message at uid ${uid}`);
162
+ return found;
163
+ };
164
+ export const arriveAt = async (store, imap, account, mailbox, raw) => {
165
+ const uid = imap.deliver(mailbox.fullPath, raw);
166
+ await syncFolder(store, account, mailbox, imap);
167
+ return (await messageAtUid(store, mailbox, uid)).messageId;
168
+ };
169
+ export const storeBody = async (store, storage, account, messageId, raw) => {
170
+ const ref = await storage.storeMessageBody({
171
+ accountConfigId: account.accountConfigId,
172
+ accountId: account.accountId,
173
+ messageId,
174
+ content: raw,
175
+ });
176
+ await store.message.update(messageId, { bodyStorageKey: ref.uri });
177
+ };
178
+ /**
179
+ * Another writer landing between a service's read and its write: `action` runs,
180
+ * for real, the next time `method` is called on `target`, and the call then
181
+ * proceeds as normal.
182
+ */
183
+ export const beforeNext = (target, method, action) => {
184
+ const original = target[method];
185
+ let pending = true;
186
+ target[method] = (async (...args) => {
187
+ if (pending) {
188
+ pending = false;
189
+ await action();
190
+ }
191
+ return original.apply(target, args);
192
+ });
193
+ };
@@ -0,0 +1,20 @@
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
+ export type SupportedLanguage = "en" | "de" | "fr" | "es" | "it" | "nl" | "pt";
8
+ export interface NormalizeOptions {
9
+ language?: SupportedLanguage;
10
+ stem?: boolean;
11
+ removeStopwords?: boolean;
12
+ }
13
+ export interface TextNormalizer {
14
+ detectLanguage(text: string): SupportedLanguage;
15
+ normalize(text: string, options?: NormalizeOptions): string;
16
+ tokenize(text: string): string[];
17
+ stem(word: string, language?: SupportedLanguage): string;
18
+ removeStopwords(words: string[], language?: SupportedLanguage): string[];
19
+ }
20
+ export declare const createTextNormalizer: () => TextNormalizer;