Skip to content

Commit

Permalink
Supprt headers as String or Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Khalid committed Feb 19, 2018
1 parent b86846d commit e090814
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const headerParse = require('header-parse');

/**
* Convert header names to lower case and normalize value to string.
* @param {Object} headers - Key/value object of headers
Expand All @@ -18,7 +20,9 @@ const normalizedHeaders = (headers) => {
* @return {Boolean} - True if autoreply, false otherwise
*/
module.exports = (headers) => {
// TODO: maybe if headers is a string, we can parse it as email headers using a NPM module
if(typeof headers === 'string' || Buffer.isBuffer(headers)) {
headers = headerParse.parseHeaders(headers);
}

headers = normalizedHeaders(headers);

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"bugs": {
"url": "https://github.com/walling/is-autoreply/issues"
},
"homepage": "https://github.com/walling/is-autoreply#readme"
"homepage": "https://github.com/walling/is-autoreply#readme",
"dependencies": {
"header-parse": "~0.1.0"
}
}
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,17 @@ assert.ok(isAutoreply({ 'X-Auto-Response-Suppress': 'OOF' }));
assert.ok(isAutoreply({ 'x-auto-response-suppress': 'oof' }));
assert.ok(isAutoreply({ 'x-auto-response-suppress': 'RN, NRN' }));
assert.ok(isAutoreply({ 'x-auto-response-suppress': '' }));

// Test string headers
assert.ok(isAutoreply('Delivered-To: Autoresponder'));
assert.ok(isAutoreply('Delivered-To: Autoresponder '));
assert.ok(isAutoreply('X-POST-MessageClass: 9; autoresponder'));
assert.ok(isAutoreply('X-POST-MessageClass: 9; autoresponder '));
assert.ok(!isAutoreply('MIME-Version: 1.0'));

// Test buffer headers
assert.ok(isAutoreply(Buffer.from('Delivered-To: Autoresponder')));
assert.ok(isAutoreply(Buffer.from('Delivered-To: Autoresponder ')));
assert.ok(isAutoreply(Buffer.from('X-POST-MessageClass: 9; autoresponder')));
assert.ok(isAutoreply(Buffer.from('X-POST-MessageClass: 9; autoresponder ')));
assert.ok(!isAutoreply(Buffer.from('MIME-Version: 1.0')));

0 comments on commit e090814

Please sign in to comment.