@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,78 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { MailboxSpecialUse } from "@remit/domain-enums";
|
|
4
|
+
import {
|
|
5
|
+
hasChildren,
|
|
6
|
+
isNoSelect,
|
|
7
|
+
parseImapAttributes,
|
|
8
|
+
} from "./attribute-mapper.js";
|
|
9
|
+
|
|
10
|
+
describe("parseImapAttributes special-use", () => {
|
|
11
|
+
it("maps an IMAP \\Junk attribute to the canonical bare enum value", () => {
|
|
12
|
+
const parsed = parseImapAttributes(["\\Junk"]);
|
|
13
|
+
assert.deepStrictEqual(parsed.specialUse, [MailboxSpecialUse.Junk]);
|
|
14
|
+
assert.strictEqual(parsed.specialUse[0], "Junk");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("normalizes case-insensitive special-use attributes", () => {
|
|
18
|
+
const parsed = parseImapAttributes(["\\junk", "\\SENT", "\\Drafts"]);
|
|
19
|
+
assert.deepStrictEqual(parsed.specialUse, [
|
|
20
|
+
MailboxSpecialUse.Junk,
|
|
21
|
+
MailboxSpecialUse.Sent,
|
|
22
|
+
MailboxSpecialUse.Drafts,
|
|
23
|
+
]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("emits every value in the bare form the runtime stores and the spec validates", () => {
|
|
27
|
+
const parsed = parseImapAttributes([
|
|
28
|
+
"\\All",
|
|
29
|
+
"\\Archive",
|
|
30
|
+
"\\Drafts",
|
|
31
|
+
"\\Flagged",
|
|
32
|
+
"\\Junk",
|
|
33
|
+
"\\Sent",
|
|
34
|
+
"\\Trash",
|
|
35
|
+
"\\Important",
|
|
36
|
+
]);
|
|
37
|
+
assert.deepStrictEqual(parsed.specialUse, [
|
|
38
|
+
"All",
|
|
39
|
+
"Archive",
|
|
40
|
+
"Drafts",
|
|
41
|
+
"Flagged",
|
|
42
|
+
"Junk",
|
|
43
|
+
"Sent",
|
|
44
|
+
"Trash",
|
|
45
|
+
"Important",
|
|
46
|
+
]);
|
|
47
|
+
assert.ok(parsed.specialUse.every((v) => !v.startsWith("\\")));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("separates standard attributes from special-use", () => {
|
|
51
|
+
const parsed = parseImapAttributes(["\\HasChildren", "\\Sent"]);
|
|
52
|
+
assert.deepStrictEqual(parsed.specialUse, [MailboxSpecialUse.Sent]);
|
|
53
|
+
assert.deepStrictEqual(parsed.attributes, ["HasChildren"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("collects unknown attributes without dropping them", () => {
|
|
57
|
+
const parsed = parseImapAttributes(["\\Junk", "\\SomethingNovel"]);
|
|
58
|
+
assert.deepStrictEqual(parsed.specialUse, [MailboxSpecialUse.Junk]);
|
|
59
|
+
assert.deepStrictEqual(parsed.unknown, ["\\SomethingNovel"]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("yields no special-use for a plain folder", () => {
|
|
63
|
+
const parsed = parseImapAttributes(["\\HasNoChildren"]);
|
|
64
|
+
assert.deepStrictEqual(parsed.specialUse, []);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("attribute predicates", () => {
|
|
69
|
+
it("detects no-select mailboxes", () => {
|
|
70
|
+
assert.strictEqual(isNoSelect(["\\Noselect"]), true);
|
|
71
|
+
assert.strictEqual(isNoSelect(["\\HasChildren"]), false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("detects mailboxes with children", () => {
|
|
75
|
+
assert.strictEqual(hasChildren(["\\HasChildren"]), true);
|
|
76
|
+
assert.strictEqual(hasChildren(["\\HasNoChildren"]), false);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps IMAP mailbox attributes to entity enum values
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { MailboxAttribute, MailboxSpecialUse } from "@remit/domain-enums";
|
|
6
|
+
|
|
7
|
+
// Type for enum values
|
|
8
|
+
type MailboxAttributeValue =
|
|
9
|
+
(typeof MailboxAttribute)[keyof typeof MailboxAttribute];
|
|
10
|
+
type MailboxSpecialUseValue =
|
|
11
|
+
(typeof MailboxSpecialUse)[keyof typeof MailboxSpecialUse];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Map from IMAP attribute strings to MailboxAttribute enum values
|
|
15
|
+
* IMAP attributes have backslash prefix, our enum values don't
|
|
16
|
+
*/
|
|
17
|
+
const ATTRIBUTE_MAP: Record<string, MailboxAttributeValue> = {
|
|
18
|
+
"\\NonExistent": MailboxAttribute.NonExistent,
|
|
19
|
+
"\\Noinferiors": MailboxAttribute.NoInferiors,
|
|
20
|
+
"\\Noselect": MailboxAttribute.NoSelect,
|
|
21
|
+
"\\HasChildren": MailboxAttribute.HasChildren,
|
|
22
|
+
"\\HasNoChildren": MailboxAttribute.HasNoChildren,
|
|
23
|
+
"\\Marked": MailboxAttribute.Marked,
|
|
24
|
+
"\\Unmarked": MailboxAttribute.Unmarked,
|
|
25
|
+
"\\Subscribed": MailboxAttribute.Subscribed,
|
|
26
|
+
"\\Remote": MailboxAttribute.Remote,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Map from IMAP special-use attribute strings to MailboxSpecialUse enum values
|
|
31
|
+
*/
|
|
32
|
+
const SPECIAL_USE_MAP: Record<string, MailboxSpecialUseValue> = {
|
|
33
|
+
"\\All": MailboxSpecialUse.All,
|
|
34
|
+
"\\Archive": MailboxSpecialUse.Archive,
|
|
35
|
+
"\\Drafts": MailboxSpecialUse.Drafts,
|
|
36
|
+
"\\Flagged": MailboxSpecialUse.Flagged,
|
|
37
|
+
"\\Junk": MailboxSpecialUse.Junk,
|
|
38
|
+
"\\Sent": MailboxSpecialUse.Sent,
|
|
39
|
+
"\\Trash": MailboxSpecialUse.Trash,
|
|
40
|
+
"\\Important": MailboxSpecialUse.Important,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Set of all known special-use attributes for quick lookup
|
|
45
|
+
*/
|
|
46
|
+
const SPECIAL_USE_ATTRS = new Set(Object.keys(SPECIAL_USE_MAP));
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Result of parsing IMAP attributes
|
|
50
|
+
*/
|
|
51
|
+
export interface ParsedAttributes {
|
|
52
|
+
/** Standard mailbox attributes */
|
|
53
|
+
attributes: MailboxAttributeValue[];
|
|
54
|
+
/** Special-use designations */
|
|
55
|
+
specialUse: MailboxSpecialUseValue[];
|
|
56
|
+
/** Unknown attributes that couldn't be mapped */
|
|
57
|
+
unknown: string[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Parse IMAP mailbox attributes into our enum types
|
|
62
|
+
*
|
|
63
|
+
* IMAP attributes come as strings like "\\Noselect", "\\Drafts", etc.
|
|
64
|
+
* This function categorizes them into:
|
|
65
|
+
* - Standard attributes (MailboxAttribute)
|
|
66
|
+
* - Special-use attributes (MailboxSpecialUse)
|
|
67
|
+
* - Unknown attributes
|
|
68
|
+
*/
|
|
69
|
+
export const parseImapAttributes = (attribs: string[]): ParsedAttributes => {
|
|
70
|
+
const result: ParsedAttributes = {
|
|
71
|
+
attributes: [],
|
|
72
|
+
specialUse: [],
|
|
73
|
+
unknown: [],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
for (const attr of attribs) {
|
|
77
|
+
// Normalize attribute (IMAP attributes are case-insensitive)
|
|
78
|
+
const normalized = normalizeAttribute(attr);
|
|
79
|
+
|
|
80
|
+
if (SPECIAL_USE_ATTRS.has(normalized)) {
|
|
81
|
+
const specialUse = SPECIAL_USE_MAP[normalized];
|
|
82
|
+
if (specialUse) {
|
|
83
|
+
result.specialUse.push(specialUse);
|
|
84
|
+
}
|
|
85
|
+
} else if (ATTRIBUTE_MAP[normalized]) {
|
|
86
|
+
result.attributes.push(ATTRIBUTE_MAP[normalized]);
|
|
87
|
+
} else {
|
|
88
|
+
result.unknown.push(attr);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return result;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Normalize an IMAP attribute to canonical form
|
|
97
|
+
*
|
|
98
|
+
* IMAP attributes are case-insensitive per RFC 9051.
|
|
99
|
+
* We normalize to the canonical form used in the RFCs.
|
|
100
|
+
*/
|
|
101
|
+
const normalizeAttribute = (attr: string): string => {
|
|
102
|
+
// Handle common variations
|
|
103
|
+
const lower = attr.toLowerCase();
|
|
104
|
+
|
|
105
|
+
// Map common case variations to canonical form
|
|
106
|
+
const canonicalMap: Record<string, string> = {
|
|
107
|
+
"\\nonexistent": "\\NonExistent",
|
|
108
|
+
"\\noinferiors": "\\Noinferiors",
|
|
109
|
+
"\\noselect": "\\Noselect",
|
|
110
|
+
"\\haschildren": "\\HasChildren",
|
|
111
|
+
"\\hasnochildren": "\\HasNoChildren",
|
|
112
|
+
"\\marked": "\\Marked",
|
|
113
|
+
"\\unmarked": "\\Unmarked",
|
|
114
|
+
"\\subscribed": "\\Subscribed",
|
|
115
|
+
"\\remote": "\\Remote",
|
|
116
|
+
"\\all": "\\All",
|
|
117
|
+
"\\archive": "\\Archive",
|
|
118
|
+
"\\drafts": "\\Drafts",
|
|
119
|
+
"\\flagged": "\\Flagged",
|
|
120
|
+
"\\junk": "\\Junk",
|
|
121
|
+
"\\sent": "\\Sent",
|
|
122
|
+
"\\trash": "\\Trash",
|
|
123
|
+
"\\important": "\\Important",
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
return canonicalMap[lower] ?? attr;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Check if an attribute indicates the mailbox cannot be selected
|
|
131
|
+
*/
|
|
132
|
+
export const isNoSelect = (attribs: string[]): boolean => {
|
|
133
|
+
return attribs.some((attr) => {
|
|
134
|
+
const normalized = normalizeAttribute(attr);
|
|
135
|
+
return normalized === "\\Noselect" || normalized === "\\NonExistent";
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Check if an attribute indicates the mailbox has children
|
|
141
|
+
*/
|
|
142
|
+
export const hasChildren = (attribs: string[]): boolean => {
|
|
143
|
+
return attribs.some((attr) => {
|
|
144
|
+
const normalized = normalizeAttribute(attr);
|
|
145
|
+
return normalized === "\\HasChildren";
|
|
146
|
+
});
|
|
147
|
+
};
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Property test for the total body-part mapper (issue #395 PR C).
|
|
3
|
+
*
|
|
4
|
+
* Generates 10,000 random MIME trees (depth ≤ 4, mixed/alternative/related
|
|
5
|
+
* multiparts, five leaf content types, optional filenames/contentIds,
|
|
6
|
+
* occasional empty leaves), renders each to an `.eml`, parses it via
|
|
7
|
+
* `simpleParser`, walks the synthetic tree into `MapperInput[]`, and runs
|
|
8
|
+
* `mapBodyPartsToContent`.
|
|
9
|
+
*
|
|
10
|
+
* Per-iteration assertions:
|
|
11
|
+
* 1. Totality — `pairs.length === leafCount`.
|
|
12
|
+
* 2. Buffer-ness — every `pair.content` is a Buffer (never undefined/null).
|
|
13
|
+
* 3. No-throw — wrapping the call site never escapes.
|
|
14
|
+
* 4. PartPath cover — every leaf's `partPath` appears exactly once in `pairs`.
|
|
15
|
+
*
|
|
16
|
+
* Reproducibility: seed via `MAPPER_PROPERTY_SEED` env var. Defaults to
|
|
17
|
+
* `0xC0DEC0DE` so CI runs are deterministic. The generator's per-iteration
|
|
18
|
+
* RNG is derived from `(baseSeed ^ iterationIndex)` so a failure on
|
|
19
|
+
* iteration `N` can be reproduced as a single-iteration run with the same
|
|
20
|
+
* combined seed.
|
|
21
|
+
*
|
|
22
|
+
* Targeted property blocks below cover PR B reviewer's three gaps:
|
|
23
|
+
* - Case-insensitive `dispositionFilename` matching.
|
|
24
|
+
* - Negative routing: `application/octet-stream` without a filename
|
|
25
|
+
* doesn't swallow a text/html attachment.
|
|
26
|
+
* - Inline `cid:` casing + angle-bracket insensitivity.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import assert from "node:assert/strict";
|
|
30
|
+
import { Buffer } from "node:buffer";
|
|
31
|
+
import { describe, it } from "node:test";
|
|
32
|
+
import { simpleParser } from "mailparser";
|
|
33
|
+
import { type MapperInput, mapBodyPartsToContent } from "./body-part-mapper.js";
|
|
34
|
+
import {
|
|
35
|
+
createRng,
|
|
36
|
+
generateMimeTree,
|
|
37
|
+
type MultipartNode,
|
|
38
|
+
renderEml,
|
|
39
|
+
treeToBodyParts,
|
|
40
|
+
} from "./test-helpers/mime-tree-generator.js";
|
|
41
|
+
|
|
42
|
+
const DEFAULT_SEED = 0xc0dec0de;
|
|
43
|
+
const ITERATIONS = 10_000;
|
|
44
|
+
|
|
45
|
+
const parseSeed = (): number => {
|
|
46
|
+
const raw = process.env.MAPPER_PROPERTY_SEED;
|
|
47
|
+
if (!raw) return DEFAULT_SEED;
|
|
48
|
+
const n = Number.parseInt(raw, 10);
|
|
49
|
+
if (Number.isNaN(n)) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`MAPPER_PROPERTY_SEED is not a valid integer: ${JSON.stringify(raw)}`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return n | 0;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const collectLeafPaths = (parts: readonly MapperInput[]): string[] =>
|
|
58
|
+
parts.filter((p) => !p.isMultipart).map((p) => p.partPath);
|
|
59
|
+
|
|
60
|
+
describe("body-part-mapper property: random MIME trees", () => {
|
|
61
|
+
it(`pairs every leaf for ${ITERATIONS} random trees (seed-reproducible)`, async () => {
|
|
62
|
+
const baseSeed = parseSeed();
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < ITERATIONS; i++) {
|
|
65
|
+
const iterSeed = (baseSeed ^ i) | 0;
|
|
66
|
+
const rng = createRng(iterSeed);
|
|
67
|
+
const tree = generateMimeTree(rng);
|
|
68
|
+
const eml = renderEml(tree, { seed: iterSeed });
|
|
69
|
+
const bodyParts = treeToBodyParts(tree);
|
|
70
|
+
const leafPaths = collectLeafPaths(bodyParts);
|
|
71
|
+
|
|
72
|
+
let parsed: Awaited<ReturnType<typeof simpleParser>>;
|
|
73
|
+
try {
|
|
74
|
+
parsed = await simpleParser(eml);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
// If mailparser can't parse, the generated EML is bad — surface
|
|
77
|
+
// it loudly with the seed so we can reproduce.
|
|
78
|
+
assert.fail(
|
|
79
|
+
`simpleParser threw for seed=${iterSeed} (iteration ${i}): ${(err as Error).message}`,
|
|
80
|
+
);
|
|
81
|
+
throw err; // unreachable; satisfies the control-flow analysis below.
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Assertion 3: mapper never throws.
|
|
85
|
+
let pairs: ReturnType<typeof mapBodyPartsToContent>;
|
|
86
|
+
try {
|
|
87
|
+
pairs = mapBodyPartsToContent(bodyParts, parsed);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
assert.fail(
|
|
90
|
+
`mapBodyPartsToContent threw for seed=${iterSeed} (iteration ${i}): ${(err as Error).message}`,
|
|
91
|
+
);
|
|
92
|
+
throw err; // unreachable
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Assertion 1: totality.
|
|
96
|
+
assert.equal(
|
|
97
|
+
pairs.length,
|
|
98
|
+
leafPaths.length,
|
|
99
|
+
`totality violated for seed=${iterSeed} (iteration ${i}): pairs=${pairs.length} leaves=${leafPaths.length}`,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
// Assertion 2: every content is a Buffer.
|
|
103
|
+
for (const p of pairs) {
|
|
104
|
+
assert.ok(
|
|
105
|
+
Buffer.isBuffer(p.content),
|
|
106
|
+
`pair.content is not a Buffer for seed=${iterSeed} (iteration ${i}), partPath=${p.partPath}`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Assertion 4: every leaf partPath appears exactly once.
|
|
111
|
+
const seen = new Map<string, number>();
|
|
112
|
+
for (const p of pairs)
|
|
113
|
+
seen.set(p.partPath, (seen.get(p.partPath) ?? 0) + 1);
|
|
114
|
+
for (const path of leafPaths) {
|
|
115
|
+
assert.equal(
|
|
116
|
+
seen.get(path),
|
|
117
|
+
1,
|
|
118
|
+
`leaf partPath="${path}" not paired exactly once for seed=${iterSeed} (iteration ${i})`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/* -------------------------------------------------------------------- */
|
|
126
|
+
/* Targeted properties: PR B reviewer's three gaps */
|
|
127
|
+
/* -------------------------------------------------------------------- */
|
|
128
|
+
|
|
129
|
+
const TARGETED_ITERATIONS = 100;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Random-case filename matching. Generates a tree with one html leaf and
|
|
133
|
+
* one PDF attachment whose filename has a random mix of upper/lower-case
|
|
134
|
+
* letters. The BODYSTRUCTURE row records the filename verbatim; mailparser
|
|
135
|
+
* preserves the same string on the attachment. The dispositionFilename
|
|
136
|
+
* fallback must match case-insensitively (PR B's `findByFilename` lowercases
|
|
137
|
+
* both sides). With `partId === partPath` doing structural pairing first
|
|
138
|
+
* this looks easy — but if mailparser ever drifts on partId we want the
|
|
139
|
+
* filename fallback to keep working.
|
|
140
|
+
*/
|
|
141
|
+
describe("body-part-mapper property: case-insensitive filename match", () => {
|
|
142
|
+
it("pairs a PDF leaf when the filename has random-case extension", async () => {
|
|
143
|
+
const baseSeed = parseSeed() ^ 0xa1a1a1a1;
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < TARGETED_ITERATIONS; i++) {
|
|
146
|
+
const rng = createRng((baseSeed ^ i) | 0);
|
|
147
|
+
const randomCase = (s: string): string =>
|
|
148
|
+
[...s]
|
|
149
|
+
.map((c) => (rng.bool(0.5) ? c.toUpperCase() : c.toLowerCase()))
|
|
150
|
+
.join("");
|
|
151
|
+
const filename = randomCase("invoice.pdf");
|
|
152
|
+
|
|
153
|
+
const tree: MultipartNode = {
|
|
154
|
+
kind: "multipart",
|
|
155
|
+
subtype: "mixed",
|
|
156
|
+
children: [
|
|
157
|
+
{
|
|
158
|
+
kind: "leaf",
|
|
159
|
+
contentType: "text/html",
|
|
160
|
+
bytes: Buffer.from("<p>see attached</p>", "utf8"),
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
kind: "leaf",
|
|
164
|
+
// BODYSTRUCTURE will declare octet-stream; mailparser sniffs PDF
|
|
165
|
+
// from the filename. Filename-case insensitivity is the
|
|
166
|
+
// load-bearing step.
|
|
167
|
+
contentType: "application/octet-stream",
|
|
168
|
+
bytes: Buffer.from("%PDF-fake-bytes", "utf8"),
|
|
169
|
+
filename,
|
|
170
|
+
disposition: "attachment",
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const seed = (baseSeed ^ i) | 0;
|
|
176
|
+
const eml = renderEml(tree, { seed });
|
|
177
|
+
const bodyParts = treeToBodyParts(tree);
|
|
178
|
+
const parsed = await simpleParser(eml);
|
|
179
|
+
const pairs = mapBodyPartsToContent(bodyParts, parsed);
|
|
180
|
+
|
|
181
|
+
assert.equal(pairs.length, 2, `seed=${seed}: expected 2 pairs`);
|
|
182
|
+
const pdfPair = pairs.find((p) => p.partPath === "2");
|
|
183
|
+
assert.ok(pdfPair, `seed=${seed}: missing partPath=2 pair`);
|
|
184
|
+
// PDF bytes (from mailparser's attachment) — must equal the rendered
|
|
185
|
+
// leaf body, not an empty buffer and not the html bytes.
|
|
186
|
+
assert.equal(
|
|
187
|
+
pdfPair.content.toString("utf8"),
|
|
188
|
+
"%PDF-fake-bytes",
|
|
189
|
+
`seed=${seed}: PDF pair bytes mismatch (filename=${filename})`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Negative routing: an `application/octet-stream` leaf with no filename
|
|
197
|
+
* must not consume a `text/html` attachment from mailparser. With PR B's
|
|
198
|
+
* mapper, the html leaf is routed via `parsed.html` (text-routing slot)
|
|
199
|
+
* before the octet-stream leaf reaches the non-text pairing pipeline,
|
|
200
|
+
* so the html bytes stay with their proper leaf.
|
|
201
|
+
*
|
|
202
|
+
* Shape: multipart/mixed with [html-leaf, octet-stream-leaf]. The
|
|
203
|
+
* octet-stream has no filename, no contentId — only the positional
|
|
204
|
+
* fallback can pair it. We assert that the octet-stream pair does NOT
|
|
205
|
+
* receive the html body's bytes.
|
|
206
|
+
*/
|
|
207
|
+
describe("body-part-mapper property: negative routing for octet-stream", () => {
|
|
208
|
+
it("does not pair an unnamed octet-stream leaf with html bytes", async () => {
|
|
209
|
+
const baseSeed = parseSeed() ^ 0xb2b2b2b2;
|
|
210
|
+
|
|
211
|
+
for (let i = 0; i < TARGETED_ITERATIONS; i++) {
|
|
212
|
+
const seed = (baseSeed ^ i) | 0;
|
|
213
|
+
const rng = createRng(seed);
|
|
214
|
+
|
|
215
|
+
const htmlBody = `<html><body><p>seed-${seed}-${randomTag(rng)}</p></body></html>`;
|
|
216
|
+
const octetBody = `octet-${seed}-${randomTag(rng)}`;
|
|
217
|
+
|
|
218
|
+
const tree: MultipartNode = {
|
|
219
|
+
kind: "multipart",
|
|
220
|
+
subtype: "mixed",
|
|
221
|
+
children: [
|
|
222
|
+
{
|
|
223
|
+
kind: "leaf",
|
|
224
|
+
contentType: "text/html",
|
|
225
|
+
bytes: Buffer.from(htmlBody, "utf8"),
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
kind: "leaf",
|
|
229
|
+
contentType: "application/octet-stream",
|
|
230
|
+
bytes: Buffer.from(octetBody, "utf8"),
|
|
231
|
+
// no filename, no contentId, no disposition
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const eml = renderEml(tree, { seed });
|
|
237
|
+
const bodyParts = treeToBodyParts(tree);
|
|
238
|
+
const parsed = await simpleParser(eml);
|
|
239
|
+
const pairs = mapBodyPartsToContent(bodyParts, parsed);
|
|
240
|
+
|
|
241
|
+
assert.equal(pairs.length, 2, `seed=${seed}: expected 2 pairs`);
|
|
242
|
+
|
|
243
|
+
const htmlPair = pairs.find((p) => p.partPath === "1");
|
|
244
|
+
const octetPair = pairs.find((p) => p.partPath === "2");
|
|
245
|
+
assert.ok(htmlPair, `seed=${seed}: missing html pair`);
|
|
246
|
+
assert.ok(octetPair, `seed=${seed}: missing octet pair`);
|
|
247
|
+
|
|
248
|
+
// html leaf gets the html body (text-routing slot).
|
|
249
|
+
const htmlStr = htmlPair.content.toString("utf8");
|
|
250
|
+
assert.ok(
|
|
251
|
+
htmlStr.includes(`seed-${seed}-`),
|
|
252
|
+
`seed=${seed}: html pair lost its body (got "${htmlStr.slice(0, 80)}")`,
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
// octet leaf must NOT contain the html body's marker.
|
|
256
|
+
const octetStr = octetPair.content.toString("utf8");
|
|
257
|
+
assert.ok(
|
|
258
|
+
!octetStr.includes("<html>"),
|
|
259
|
+
`seed=${seed}: octet pair swallowed html bytes (got "${octetStr.slice(0, 80)}")`,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Inline `cid:` casing + angle-bracket insensitivity. PR B's
|
|
267
|
+
* `findByContentId` strips angle brackets and lowercases both sides, so a
|
|
268
|
+
* BODYSTRUCTURE row with `contentId: "Logo@Example.ORG"` should still pair
|
|
269
|
+
* with a mailparser attachment whose `cid` is `<logo@example.org>` (or any
|
|
270
|
+
* mixed-case variant with/without brackets). We assert the image leaf
|
|
271
|
+
* receives the image bytes — not the html ones — across random
|
|
272
|
+
* permutations.
|
|
273
|
+
*/
|
|
274
|
+
describe("body-part-mapper property: cid casing + angle-bracket insensitivity", () => {
|
|
275
|
+
it("pairs inline image with mixed-case bracketed contentId", async () => {
|
|
276
|
+
const baseSeed = parseSeed() ^ 0xc3c3c3c3;
|
|
277
|
+
|
|
278
|
+
for (let i = 0; i < TARGETED_ITERATIONS; i++) {
|
|
279
|
+
const seed = (baseSeed ^ i) | 0;
|
|
280
|
+
const rng = createRng(seed);
|
|
281
|
+
|
|
282
|
+
const localPart = `hero-${randomTag(rng)}`;
|
|
283
|
+
const domain = "example.org";
|
|
284
|
+
const baseCid = `${localPart}@${domain}`;
|
|
285
|
+
|
|
286
|
+
// Random-case for the BodyPart row's contentId. Sometimes wrapped in
|
|
287
|
+
// `< >`, sometimes not. The leaf's actual Content-ID header (rendered
|
|
288
|
+
// into the EML) is always lower-case with angles — that's what
|
|
289
|
+
// mailparser sees.
|
|
290
|
+
const rowCid = (() => {
|
|
291
|
+
const cased = [...baseCid]
|
|
292
|
+
.map((c) => (rng.bool(0.4) ? c.toUpperCase() : c.toLowerCase()))
|
|
293
|
+
.join("");
|
|
294
|
+
return rng.bool(0.5) ? `<${cased}>` : cased;
|
|
295
|
+
})();
|
|
296
|
+
|
|
297
|
+
const imageBytes = Buffer.from(`png-${seed}-${randomTag(rng)}`, "utf8");
|
|
298
|
+
|
|
299
|
+
const tree: MultipartNode = {
|
|
300
|
+
kind: "multipart",
|
|
301
|
+
subtype: "related",
|
|
302
|
+
children: [
|
|
303
|
+
{
|
|
304
|
+
kind: "leaf",
|
|
305
|
+
contentType: "text/html",
|
|
306
|
+
bytes: Buffer.from(`<img src="cid:${baseCid}">`, "utf8"),
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
kind: "leaf",
|
|
310
|
+
contentType: "image/png",
|
|
311
|
+
bytes: imageBytes,
|
|
312
|
+
contentId: baseCid, // leaf header always normalised; mailparser sees this
|
|
313
|
+
disposition: "inline",
|
|
314
|
+
},
|
|
315
|
+
],
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const eml = renderEml(tree, { seed });
|
|
319
|
+
// Swap the row's contentId for the random-cased / bracketed variant —
|
|
320
|
+
// simulating what BODYSTRUCTURE might persist after a sloppy sender.
|
|
321
|
+
// MapperInput rows are typed as readonly via `Pick<BodyPartItem,...>`
|
|
322
|
+
// so we rebuild the row rather than mutating in place.
|
|
323
|
+
const bodyParts = treeToBodyParts(tree).map((row) =>
|
|
324
|
+
row.mediaType === "IMAGE" ? { ...row, contentId: rowCid } : row,
|
|
325
|
+
);
|
|
326
|
+
const imageRow = bodyParts.find((p) => p.mediaType === "IMAGE");
|
|
327
|
+
assert.ok(imageRow, `seed=${seed}: missing image row in body parts`);
|
|
328
|
+
|
|
329
|
+
const parsed = await simpleParser(eml);
|
|
330
|
+
const pairs = mapBodyPartsToContent(bodyParts, parsed);
|
|
331
|
+
|
|
332
|
+
assert.equal(pairs.length, 2, `seed=${seed}: expected 2 pairs`);
|
|
333
|
+
|
|
334
|
+
const imagePair = pairs.find((p) => p.partPath === "2");
|
|
335
|
+
assert.ok(imagePair, `seed=${seed}: missing image pair`);
|
|
336
|
+
assert.equal(
|
|
337
|
+
imagePair.content.toString("utf8"),
|
|
338
|
+
imageBytes.toString("utf8"),
|
|
339
|
+
`seed=${seed}: image pair did not receive image bytes (rowCid=${rowCid})`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
/* -------------------------------------------------------------------- */
|
|
346
|
+
/* Helpers */
|
|
347
|
+
/* -------------------------------------------------------------------- */
|
|
348
|
+
|
|
349
|
+
const randomTag = (rng: { int: (n: number) => number }): string => {
|
|
350
|
+
const letters = "abcdefghijklmnopqrstuvwxyz";
|
|
351
|
+
let s = "";
|
|
352
|
+
for (let i = 0; i < 6; i++) s += letters[rng.int(letters.length)];
|
|
353
|
+
return s;
|
|
354
|
+
};
|