-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
133 lines (104 loc) · 3.13 KB
/
index.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
121
122
123
124
125
126
127
128
129
130
131
132
133
var nodemailer = require('nodemailer');
exports = module.exports = {};
var transporter;
var options;
var lastSendTime;
var errorMessages;
var throttler;
var sendCallback;
/******************************************************
*
* PRIVATE FUNCTIONS
*
******************************************************/
function _setupSmtpTransporter(smtpConfig) {
transporter = nodemailer.createTransport(smtpConfig);
}
function _setMailOptions(options) {
if (!options.fromEmail) throw new Error('no from e-mail specified');
if (!options.toEmail) throw new Error('no toEmail specified');
}
function _throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
return true;
}
};
}
function _sendMail() {
var fromName = options.fromName || "Node Error Mailer";
var to = [].concat(options.toEmail);
var subject = (errorMessages.length == 1) ? 'Error Notification for ' + options.appName : 'Error Notifications (' + errorMessages.length + ') for ' + options.appName
if (errorMessages.length > 1) {
}
var mailOptions = {
from: fromName + ' <' + options.fromEmail + '>',
to: to.join(", "),
subject: subject,
text: errorMessages.join(","),
html: errorMessages.join(",")
}
transporter.sendMail(mailOptions, function (err, info) {
if (err) return sendCallback(err);
return sendCallback(err, info);
});
errorMessages = [];
}
/******************************************************
*
* PUBLIC FUNCTIONS
*
******************************************************/
/**
* Sends message to the initiated e-mailaddress
*
* @param {string} errorMessage The message that will be send
* @param {function} callback Optional callback
*/
function send(errorMessage, callback) {
//set public callback
sendCallback = callback || function () { };
if (!options) callback('no options set');
if (!transporter) callback('no transporter set');
if (errorMessages.length == 0) lastSendTime = new Date();
errorMessages.push(errorMessage);
if(!options.throttleTime){
_sendMail(callback);
}else{
//if it's throttled, callback without error
if(!throttler()){
return callback(null, 'throttling');
}
}
}
/**
* Initiates the modudule
*
* @param {object} options
*/
function init(_options) {
options = _options;
_setupSmtpTransporter(options.smtp);
errorMessages = [];
lastSendTime = 0;
if(options.throttleTime){
throttler = _throttle(_sendMail, options.throttleTime);
}
}
exports.send = send;
exports.init = init;