@remit/mailbox-service 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -0
- package/package.json +65 -0
- package/src/account-credentials.ts +128 -0
- package/src/adversarial-mime.e2e.test.ts +311 -0
- package/src/attribute-mapper.test.ts +78 -0
- package/src/attribute-mapper.ts +147 -0
- package/src/body-part-mapper.property.test.ts +354 -0
- package/src/body-part-mapper.ts +360 -0
- package/src/body-sync-queue.test.ts +124 -0
- package/src/body-sync-queue.ts +131 -0
- package/src/body-sync-terminal.test.ts +265 -0
- package/src/body-sync-terminal.ts +172 -0
- package/src/body-sync.ts +1202 -0
- package/src/connection-factory.ts +102 -0
- package/src/connection-test.ts +208 -0
- package/src/credentials.test.ts +181 -0
- package/src/filters/match.test.ts +181 -0
- package/src/filters/match.ts +143 -0
- package/src/filters/pipeline.ts +243 -0
- package/src/flag-push-terminal.test.ts +156 -0
- package/src/flag-push-terminal.ts +106 -0
- package/src/flag-push.test.ts +408 -0
- package/src/flag-push.ts +198 -0
- package/src/flag-queue.ts +500 -0
- package/src/heuristics/classifyByHeaders.test.ts +578 -0
- package/src/heuristics/classifyByHeaders.ts +369 -0
- package/src/heuristics/classifyPlacement.ts +96 -0
- package/src/heuristics/socialDomains.ts +23 -0
- package/src/heuristics/transactionalDomains.ts +26 -0
- package/src/imapflow-connect-retry.test.ts +108 -0
- package/src/imapflow-connection.e2e.test.ts +355 -0
- package/src/imapflow-connection.integ.test.ts +327 -0
- package/src/imapflow-connection.test.ts +161 -0
- package/src/imapflow-connection.ts +1374 -0
- package/src/index.ts +214 -0
- package/src/mailbox-cursor-rebuild.test.ts +171 -0
- package/src/mailbox-cursor-rebuild.ts +146 -0
- package/src/mailbox-cursor.test.ts +249 -0
- package/src/mailbox-cursor.ts +177 -0
- package/src/mailbox-management.test.ts +103 -0
- package/src/mailbox-management.ts +235 -0
- package/src/mailbox-queue.ts +258 -0
- package/src/mailbox-sync.test.ts +153 -0
- package/src/mailbox-sync.ts +590 -0
- package/src/message-move.ts +825 -0
- package/src/message-parser.ts +26 -0
- package/src/message-sync.ts +1021 -0
- package/src/mime-walker.test.ts +311 -0
- package/src/mime-walker.ts +305 -0
- package/src/outbox-queue.ts +294 -0
- package/src/pass-through-unit-of-work.ts +17 -0
- package/src/placement-move-terminal.test.ts +179 -0
- package/src/placement-move-terminal.ts +104 -0
- package/src/placement-move.ts +224 -0
- package/src/snippet.test.ts +213 -0
- package/src/snippet.ts +152 -0
- package/src/stale-message-reconcile.test.ts +82 -0
- package/src/stale-message-reconcile.ts +55 -0
- package/src/test-helpers/isolated-mailbox.ts +44 -0
- package/src/test-helpers/mailfuzz-connection.ts +27 -0
- package/src/test-helpers/mime-tree-generator.ts +461 -0
- package/src/text/normalizer.test.ts +223 -0
- package/src/text/normalizer.ts +140 -0
- package/src/types/mailparser-augment.d.ts +15 -0
- package/src/types/natural-porter-stemmer.d.ts +50 -0
- package/src/types/stopword.d.ts +69 -0
- package/src/types.ts +349 -0
- package/test/fixtures/mime/01-flat-text-plain.bodyparts.json +8 -0
- package/test/fixtures/mime/01-flat-text-plain.eml +10 -0
- package/test/fixtures/mime/01-flat-text-plain.expected.json +10 -0
- package/test/fixtures/mime/02-flat-text-html.bodyparts.json +8 -0
- package/test/fixtures/mime/02-flat-text-html.eml +10 -0
- package/test/fixtures/mime/02-flat-text-html.expected.json +10 -0
- package/test/fixtures/mime/03-alternative-text-html.bodyparts.json +20 -0
- package/test/fixtures/mime/03-alternative-text-html.eml +19 -0
- package/test/fixtures/mime/03-alternative-text-html.expected.json +16 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.bodyparts.json +22 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.eml +20 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.expected.json +16 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.bodyparts.json +22 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.eml +21 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.expected.json +16 -0
- package/test/fixtures/mime/06-related-html-cid-image.bodyparts.json +23 -0
- package/test/fixtures/mime/06-related-html-cid-image.eml +21 -0
- package/test/fixtures/mime/06-related-html-cid-image.expected.json +16 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.bodyparts.json +55 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.eml +47 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.expected.json +34 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.bodyparts.json +30 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.eml +26 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.expected.json +22 -0
- package/test/fixtures/mime/09-attachment-no-filename.bodyparts.json +21 -0
- package/test/fixtures/mime/09-attachment-no-filename.eml +20 -0
- package/test/fixtures/mime/09-attachment-no-filename.expected.json +16 -0
- package/test/fixtures/mime/10-calendar-invite.bodyparts.json +22 -0
- package/test/fixtures/mime/10-calendar-invite.eml +33 -0
- package/test/fixtures/mime/10-calendar-invite.expected.json +16 -0
- package/test/fixtures/mime/11-quoted-printable-text.bodyparts.json +8 -0
- package/test/fixtures/mime/11-quoted-printable-text.eml +10 -0
- package/test/fixtures/mime/11-quoted-printable-text.expected.json +10 -0
- package/test/fixtures/mime/12-base64-binary.bodyparts.json +22 -0
- package/test/fixtures/mime/12-base64-binary.eml +20 -0
- package/test/fixtures/mime/12-base64-binary.expected.json +16 -0
- package/test/fixtures/mime/13-message-rfc822-forward.bodyparts.json +22 -0
- package/test/fixtures/mime/13-message-rfc822-forward.eml +28 -0
- package/test/fixtures/mime/13-message-rfc822-forward.expected.json +16 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.bodyparts.json +38 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.eml +34 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.expected.json +10 -0
- package/test/fixtures/mime/15-empty-text-part.bodyparts.json +21 -0
- package/test/fixtures/mime/15-empty-text-part.eml +18 -0
- package/test/fixtures/mime/15-empty-text-part.expected.json +16 -0
- package/test/fixtures/mime/16-empty-non-text-part.bodyparts.json +30 -0
- package/test/fixtures/mime/16-empty-non-text-part.eml +24 -0
- package/test/fixtures/mime/16-empty-non-text-part.expected.json +22 -0
- package/test/fixtures/mime-adversarial-e2e/01-octet-stream-pdf.eml +21 -0
- package/test/fixtures/mime-adversarial-e2e/02-nested-related-cid-image.eml +31 -0
- package/test/fixtures/mime-adversarial-e2e/03-calendar-invite.eml +33 -0
- package/test/fixtures/mime-adversarial-e2e/04-forwarded-rfc822-with-attachments.eml +42 -0
- package/test/fixtures/mime-adversarial-e2e/05-tnef-winmail.eml +25 -0
- package/test/fixtures/mime-adversarial-e2e/06-8bit-non-ascii.eml +15 -0
- package/test/fixtures/mime-adversarial-e2e/07-quoted-printable-soft-breaks.eml +17 -0
- package/test/fixtures/mime-adversarial-e2e/08-alternative-empty-text.eml +18 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,311 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,305 @@
|
|
|
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**: some IMAP servers (and the `message/rfc822`
|
|
267
|
+
* inner-body convention) return child nodes with an empty `part` field.
|
|
268
|
+
* Assigning ROOT_PART_PATH to every such node would produce duplicate keys
|
|
269
|
+
* and cause a DynamoDB "multiple operations on one item" error in
|
|
270
|
+
* `upsertBodyParts`. Non-root nodes without a `part` therefore receive a
|
|
271
|
+
* synthetic path `<parentPath>.<siblingIndex>` that is stable across
|
|
272
|
+
* repeated syncs of the same message.
|
|
273
|
+
*/
|
|
274
|
+
export const walkMimeStructure = (root: MimeNode): BodyPartRecord[] => {
|
|
275
|
+
const out: BodyPartRecord[] = [];
|
|
276
|
+
|
|
277
|
+
const visit = (
|
|
278
|
+
node: MimeNode,
|
|
279
|
+
parentPath: string | null,
|
|
280
|
+
siblingIndex: number,
|
|
281
|
+
) => {
|
|
282
|
+
let partPath: string;
|
|
283
|
+
if (node.part && node.part.length > 0) {
|
|
284
|
+
partPath = node.part;
|
|
285
|
+
} else if (parentPath === null) {
|
|
286
|
+
// True root of the BODYSTRUCTURE tree.
|
|
287
|
+
partPath = ROOT_PART_PATH;
|
|
288
|
+
} else {
|
|
289
|
+
// Non-root node without an IMAP part path — synthesise one so
|
|
290
|
+
// the DynamoDB keys remain unique. This happens most commonly
|
|
291
|
+
// for the body of a message/rfc822 attachment, whose inner
|
|
292
|
+
// structure imapflow attaches as a childNode with part="".
|
|
293
|
+
partPath = `${parentPath}.${siblingIndex}`;
|
|
294
|
+
}
|
|
295
|
+
out.push(toRecord(node, partPath, parentPath));
|
|
296
|
+
const children = node.childNodes ?? [];
|
|
297
|
+
for (let i = 0; i < children.length; i++) {
|
|
298
|
+
// biome-ignore lint/style/noNonNullAssertion: value is guaranteed by caller contract
|
|
299
|
+
visit(children[i]!, partPath, i + 1);
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
visit(root, null, 0);
|
|
304
|
+
return out;
|
|
305
|
+
};
|