Skip to content

Commit d33d7ec

Browse files
authored
feat(events): Emit a new headerLines event to gain access the raw headers (#364)
1 parent da665c8 commit d33d7ec

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/mail-parser.js

+4
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,10 @@ class MailParser extends Transform {
797797
}
798798
});
799799
this.emit('headers', node.headers);
800+
801+
if (node.headerLines) {
802+
this.emit('headerLines', node.headerLines);
803+
}
800804
}
801805

802806
if (data.contentType === 'message/rfc822' && data.messageNode) {

test/mail-parser-test.js

+27
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,33 @@ exports['General tests'] = {
141141
});
142142
},
143143

144+
'HeaderLines event': test => {
145+
let encodedText = 'X-Test: =?UTF-8?Q?=C3=95=C3=84?= =?UTF-8?Q?=C3=96=C3=9C?=\r\n' + 'Subject: ABCDEF',
146+
mail = Buffer.from(encodedText, 'utf-8');
147+
148+
test.expect(3);
149+
let mailparser = new MailParser();
150+
151+
mailparser.on('headerLines', headerLines => {
152+
test.equal(!!headerLines.find(({ line }) => line === 'X-Test: =?UTF-8?Q?=C3=95=C3=84?= =?UTF-8?Q?=C3=96=C3=9C?='), true);
153+
test.equal(!!headerLines.find(({ line }) => line === 'Subject: ABCDEF'), true);
154+
});
155+
156+
mailparser.end(mail);
157+
mailparser.on('data', data => {
158+
if (data && data.release) {
159+
data.content.on('data', () => false);
160+
data.content.on('end', () => false);
161+
data.release();
162+
}
163+
});
164+
165+
mailparser.on('end', () => {
166+
test.ok(1, 'Parsing ended');
167+
test.done();
168+
});
169+
},
170+
144171
'No priority': test => {
145172
let encodedText = 'Content-type: text/plain; charset=utf-8\r\nSubject: ÕÄÖÜ\n\r\n1234',
146173
mail = Buffer.from(encodedText, 'utf-8');

0 commit comments

Comments
 (0)