Skip to content

Commit

Permalink
feat(events): Emit a new headerLines event to gain access the raw hea…
Browse files Browse the repository at this point in the history
…ders (#364)
  • Loading branch information
katlimruiz authored Apr 1, 2024
1 parent da665c8 commit d33d7ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/mail-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,10 @@ class MailParser extends Transform {
}
});
this.emit('headers', node.headers);

if (node.headerLines) {
this.emit('headerLines', node.headerLines);
}
}

if (data.contentType === 'message/rfc822' && data.messageNode) {
Expand Down
27 changes: 27 additions & 0 deletions test/mail-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ exports['General tests'] = {
});
},

'HeaderLines event': test => {
let encodedText = 'X-Test: =?UTF-8?Q?=C3=95=C3=84?= =?UTF-8?Q?=C3=96=C3=9C?=\r\n' + 'Subject: ABCDEF',
mail = Buffer.from(encodedText, 'utf-8');

test.expect(3);
let mailparser = new MailParser();

mailparser.on('headerLines', headerLines => {
test.equal(!!headerLines.find(({ line }) => line === 'X-Test: =?UTF-8?Q?=C3=95=C3=84?= =?UTF-8?Q?=C3=96=C3=9C?='), true);
test.equal(!!headerLines.find(({ line }) => line === 'Subject: ABCDEF'), true);
});

mailparser.end(mail);
mailparser.on('data', data => {
if (data && data.release) {
data.content.on('data', () => false);
data.content.on('end', () => false);
data.release();
}
});

mailparser.on('end', () => {
test.ok(1, 'Parsing ended');
test.done();
});
},

'No priority': test => {
let encodedText = 'Content-type: text/plain; charset=utf-8\r\nSubject: ÕÄÖÜ\n\r\n1234',
mail = Buffer.from(encodedText, 'utf-8');
Expand Down

0 comments on commit d33d7ec

Please sign in to comment.