@sendgrid/inbound-mail-parser
Advanced tools
Comparing version 6.1.4 to 6.2.0
{ | ||
"name": "@sendgrid/inbound-mail-parser", | ||
"description": "SendGrid NodeJS inbound mail parser", | ||
"version": "6.1.4", | ||
"version": "6.2.0", | ||
"author": "SendGrid <dx@sendgrid.com> (sendgrid.com)", | ||
@@ -29,3 +29,3 @@ "contributors": [ | ||
"dependencies": { | ||
"@sendgrid/helpers": "^6.1.3", | ||
"@sendgrid/helpers": "^6.2.0", | ||
"mailparser": "^0.6.1" | ||
@@ -32,0 +32,0 @@ }, |
@@ -40,5 +40,5 @@ [![BuildStatus](https://travis-ci.org/sendgrid/sendgrid-nodejs.svg?branch=master)](https://travis-ci.org/sendgrid/sendgrid-nodejs) | ||
* [Feature Request](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#feature_request) | ||
* [Bug Reports](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#submit_a_bug_report) | ||
* [Improvements to the Codebase](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#improvements_to_the_codebase) | ||
* [Feature Request](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#feature-request) | ||
* [Bug Reports](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#submit-a-bug-report) | ||
* [Improvements to the Codebase](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#improvements-to-the-codebase) | ||
@@ -45,0 +45,0 @@ <a name="troubleshooting"></a> |
'use strict'; | ||
const fs = require('fs'); | ||
const MailParser = require('mailparser').MailParser; | ||
const { MailParser } = require('mailparser'); | ||
const { | ||
@@ -17,8 +17,9 @@ classes: { | ||
*/ | ||
function createAttachment(file) { | ||
var attachment = new Attachment(); | ||
const createAttachment = (file) => { | ||
const {originalname, fileName, mimetype, contentType, content} = file; | ||
const attachment = new Attachment(); | ||
attachment.setFilename(file.originalname || file.fileName); | ||
attachment.setType(file.mimetype || file.contentType); | ||
attachment.setContent(file.content.toString('base64')); | ||
attachment.setFilename(originalname || fileName); | ||
attachment.setType(mimetype || contentType); | ||
attachment.setContent(content.toString('base64')); | ||
@@ -31,110 +32,91 @@ return attachment; | ||
* | ||
* @constructor | ||
* @param {Object} config inbound configuration object | ||
* @param {Object} request request object of the parse webhook payload | ||
*/ | ||
function Parse(config, request) { | ||
this.keys = config.keys; | ||
this.request = request; | ||
this.payload = request.body || {}; | ||
this.files = request.files || []; | ||
} | ||
class Parse { | ||
/** | ||
* Return an object literal of key/values in the payload received from webhook | ||
* @return {Object} Valid key/values in the webhook payload | ||
*/ | ||
Parse.prototype.keyValues = function() { | ||
var keyValues = {}; | ||
var key; | ||
/** | ||
* @constructor | ||
* @param {Object} config inbound configuration object | ||
* @param {Object} request request object of the parse webhook payload | ||
*/ | ||
constructor(config, request) { | ||
this.keys = config.keys; | ||
this.request = request; | ||
this.payload = request.body || {}; | ||
this.files = request.files || []; | ||
} | ||
for (var index in this.keys) { | ||
key = this.keys[index]; | ||
/** | ||
* Return an object literal of key/values in the payload received from webhook | ||
* @return {Object} Valid key/values in the webhook payload | ||
*/ | ||
keyValues() { | ||
return this.keys | ||
.filter(key => this.payload[key]) | ||
.map(key => ({ [key]: this.payload[key] })) | ||
.reduce((keyValues, keyPayload) => Object.assign(keyValues, keyPayload)); | ||
} | ||
if (this.payload[key]) { | ||
keyValues[key] = this.payload[key]; | ||
} | ||
/** | ||
* Whether the payload contains the raw email (Only applies to raw payloads) | ||
* @return {Boolean} | ||
*/ | ||
hasRawEmail() { | ||
return !!this.payload.email; | ||
} | ||
return keyValues; | ||
}; | ||
/** | ||
* Parses the raw email and returns the mail object in a callback (Only applies to raw payloads) | ||
* @param {Function} callback Function which will receive the parsed email object as the sole argument | ||
*/ | ||
getRawEmail(callback) { | ||
const mailparser = new MailParser(); | ||
const { rawEmail } = this.payload; | ||
/** | ||
* Whether the payload contains the raw email (Only applies to raw payloads) | ||
* @return {Boolean} | ||
*/ | ||
Parse.prototype.hasRawEmail = function() { | ||
return Boolean(this.payload.email); | ||
}; | ||
if (!this.hasRawEmail()) { | ||
return callback(null); | ||
} | ||
/** | ||
* Parses the raw email and returns the mail object in a callback (Only applies to raw payloads) | ||
* @param {Function} callback Function which will receive the parsed email object as the sole argument | ||
*/ | ||
Parse.prototype.getRawEmail = function(callback) { | ||
var mailparser = new MailParser(); | ||
var rawEmail = this.payload.email; | ||
if (!rawEmail) { | ||
return callback(null); | ||
mailparser.on('end', callback); | ||
mailparser.write(rawEmail); | ||
mailparser.end(); | ||
} | ||
mailparser.on('end', callback); | ||
mailparser.write(rawEmail); | ||
mailparser.end(); | ||
}; | ||
/** | ||
* Retrieves all attachments received from the webhook payload | ||
* @param {Function} callback Function which will receive an array, of attachments found, as the sole argument | ||
*/ | ||
Parse.prototype.attachments = function(callback) { | ||
if (this.hasRawEmail()) { | ||
return this._getAttachmentsRaw(callback); | ||
/** | ||
* Retrieves all attachments received from the webhook payload | ||
* @param {Function} callback Function which will receive an array, of attachments found, as the sole argument | ||
*/ | ||
attachments(callback) { | ||
return this[`_getAttachments${this.hasRawEmail() ? 'Raw' : ''}`](callback); | ||
} | ||
this._getAttachments(callback); | ||
}; | ||
/** | ||
* Parses raw email to retrieve any encoded attachments (Only applies to raw payloads) | ||
* @private | ||
* @param {Function} callback Function which will receive an array, of attachments found, as the sole argument | ||
*/ | ||
Parse.prototype._getAttachmentsRaw = function(callback) { | ||
this.getRawEmail(function(parsedEmail) { | ||
if (!parsedEmail || !parsedEmail.attachments) { | ||
return callback([]); | ||
} | ||
var attachments = parsedEmail.attachments.map(function(file) { | ||
return createAttachment(file); | ||
/** | ||
* Parses raw email to retrieve any encoded attachments (Only applies to raw payloads) | ||
* @private | ||
* @param {Function} callback Function which will receive an array, of attachments found, as the sole argument | ||
*/ | ||
_getAttachmentsRaw(callback) { | ||
this.getRawEmail(parsedEmail => { | ||
const attachments = (parsedEmail || {}).attachments || []; | ||
callback(attachments.map(createAttachment)); | ||
}); | ||
} | ||
callback(attachments); | ||
}); | ||
}; | ||
/** | ||
* Retrieves webhook payload files from the file system (Only applies to non raw payloads) | ||
* @private | ||
* @param {Function} callback Function which will receive an array, of attachments found, as the sole argument | ||
*/ | ||
Parse.prototype._getAttachments = function(callback) { | ||
var file; | ||
var attachments = []; | ||
for (var index in this.files) { | ||
file = this.files[index]; | ||
if (fs.existsSync(file.path)) { | ||
file.content = fs.readFileSync(file.path); | ||
attachments.push(createAttachment(file)); | ||
} | ||
/** | ||
* Retrieves webhook payload files from the file system (Only applies to non raw payloads) | ||
* @private | ||
* @param {Function} callback Function which will receive an array, of attachments found, as the sole argument | ||
*/ | ||
_getAttachments(callback) { | ||
return callback(this.files | ||
.filter(file => fs.existsSync(file.path)) | ||
.map((exists, idx) => [exists, this.files[idx]]) | ||
.filter(([exists, _]) => exists) | ||
.map(([_, file]) => { | ||
file.content = fs.readFileSync(file.path); | ||
return createAttachment(file); | ||
}) | ||
); | ||
} | ||
} | ||
return callback(attachments); | ||
}; | ||
module.exports = Parse; |
@@ -1,10 +0,10 @@ | ||
var Parse = require('./parser'); | ||
const Parse = require('./parser'); | ||
describe('test_parse', function() { | ||
describe('test_parse_key_values', function() { | ||
it('should return the key values specified in the config from the payload', function() { | ||
var config = { | ||
describe('test_parse', () => { | ||
describe('test_parse_key_values', () => { | ||
it('should return the key values specified in the config from the payload', () => { | ||
const config = { | ||
keys: ['to', 'from'], | ||
}; | ||
var request = { | ||
const request = { | ||
body: { | ||
@@ -17,5 +17,5 @@ to: 'inbound@inbound.example.com', | ||
var parse = new Parse(config, request); | ||
var keyValues = parse.keyValues(); | ||
var expectedValues = { | ||
const parse = new Parse(config, request); | ||
const keyValues = parse.keyValues(); | ||
const expectedValues = { | ||
to: 'inbound@inbound.example.com', | ||
@@ -30,5 +30,5 @@ from: 'Test User <test@example.com>', | ||
describe('test_parse_get_raw_email', function() { | ||
it('should return null if no raw email property in payload', function(done) { | ||
var parse = new Parse({}, {}); | ||
describe('test_parse_get_raw_email', () => { | ||
it('should return null if no raw email property in payload', (done) => { | ||
const parse = new Parse({}, {}); | ||
@@ -43,4 +43,4 @@ function callback(email) { | ||
it('should parse raw email from payload and return a mail object', function(done) { | ||
var request = { | ||
it('should parse raw email from payload and return a mail object', (done) => { | ||
const request = { | ||
body: { | ||
@@ -51,3 +51,3 @@ email: 'MIME-Version: 1.0\r\nReceived: by 0.0.0.0 with HTTP; Wed, 10 Aug 2016 14:44:21 -0700 (PDT)\r\nFrom: Example User <test@example.com>\r\nDate: Wed, 10 Aug 2016 14:44:21 -0700\r\nSubject: Inbound Parse Test Raw Data\r\nTo: inbound@inbound.inbound.com\r\nContent-Type: multipart/alternative; boundary=001a113ee97c89842f0539be8e7a\r\n\r\n--001a113ee97c89842f0539be8e7a\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\nHello SendGrid!\r\n\r\n--001a113ee97c89842f0539be8e7a\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n<html><body><strong>Hello SendGrid!</body></html>\r\n\r\n--001a113ee97c89842f0539be8e7a--\r\n', | ||
var parse = new Parse({}, request); | ||
const parse = new Parse({}, request); | ||
@@ -54,0 +54,0 @@ function callback(email) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12291
188
Updated@sendgrid/helpers@^6.2.0