-
Notifications
You must be signed in to change notification settings - Fork 779
/
Copy pathparser.js
120 lines (105 loc) · 3.47 KB
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
const fs = require('fs');
const { MailParser } = require('mailparser');
const {
classes: {
Attachment,
},
} = require('@sendgrid/helpers');
/**
* Normalises attachment files retrieved from file system or parsed raw email
*
* @param {Object} file The file object returned by file system or parsed email
* @return {Object} A Twilio SendGrid Attachment object with the file data
*/
const createAttachment = (file) => {
const {originalname, fileName, mimetype, contentType, content} = file;
const attachment = new Attachment();
attachment.setFilename(originalname || fileName);
attachment.setType(mimetype || contentType);
attachment.setContent(content.toString('base64'));
return attachment;
};
/**
* Simple class that parses data received from the Twilio SendGrid Inbound Parse Webhook
*
*/
class Parse {
/**
* @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 || request.payload || {};
this.files = request.files || [];
}
/**
* 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));
}
/**
* Whether the payload contains the raw email (Only applies to raw payloads)
* @return {Boolean}
*/
hasRawEmail() {
return !!this.payload.email;
}
/**
* 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;
if (!this.hasRawEmail()) {
return callback(null);
}
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
*/
attachments(callback) {
return this[`_getAttachments${this.hasRawEmail() ? 'Raw' : ''}`](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
*/
_getAttachmentsRaw(callback) {
this.getRawEmail(parsedEmail => {
const attachments = (parsedEmail || {}).attachments || [];
callback(attachments.map(createAttachment));
});
}
/**
* 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);
})
);
}
}
module.exports = Parse;