1 | 'use strict';
|
2 |
|
3 | var AWS = require('aws-sdk');
|
4 | var packageData = require('../package.json');
|
5 |
|
6 |
|
7 | module.exports = function (options) {
|
8 | return new SESTransport(options);
|
9 | };
|
10 |
|
11 | var THROTTLE_DELAY = 5;
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function SESTransport(options) {
|
29 | options = options || {};
|
30 | var serviceUrlRegion = [].concat(/(.*)email(.*)\.(.*).amazonaws.com/i.exec(options.ServiceUrl) || [])[3];
|
31 |
|
32 | this.options = options;
|
33 |
|
34 | if (!options.ses) {
|
35 | this.options.accessKeyId = options.accessKeyId || options.AWSAccessKeyID;
|
36 | this.options.secretAccessKey = options.secretAccessKey || options.AWSSecretKey;
|
37 | this.options.sessionToken = options.sessionToken || options.AWSSecurityToken;
|
38 | this.options.apiVersion = '2010-12-01';
|
39 | this.options.region = options.region || serviceUrlRegion || 'us-east-1';
|
40 |
|
41 | if (options.httpOptions) {
|
42 | this.options.httpOptions = options.httpOptions;
|
43 | }
|
44 | this.ses = new AWS.SES(this.options);
|
45 | } else {
|
46 | this.ses = options.ses;
|
47 | }
|
48 |
|
49 | this.rateLimit = Number(options.rateLimit) || false;
|
50 | this.queue = [];
|
51 | this.sending = false;
|
52 | this.currentConnections = 0;
|
53 | this.maxConnections = Number(options.maxConnections) || Infinity;
|
54 |
|
55 | this.name = 'SES';
|
56 | this.version = packageData.version;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | SESTransport.prototype.send = function (mail, callback) {
|
66 |
|
67 | mail.message.keepBcc = true;
|
68 |
|
69 | if (this.rateLimit) {
|
70 | this.queue.push({
|
71 | mail: mail,
|
72 | callback: callback
|
73 | });
|
74 | this.processQueue();
|
75 | } else if (this.currentConnections < this.maxConnections) {
|
76 | this.sendMessage(mail, callback);
|
77 | } else {
|
78 | setTimeout(this.send.bind(this, mail, callback), THROTTLE_DELAY);
|
79 | }
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | SESTransport.prototype.processQueue = function () {
|
86 | if (this.sending) {
|
87 | return;
|
88 | }
|
89 |
|
90 | if (!this.queue.length) {
|
91 | return;
|
92 | }
|
93 |
|
94 | this.sending = true;
|
95 | var item = this.queue.shift();
|
96 |
|
97 | this.sendMessage(item.mail, function () {
|
98 | var args = Array.prototype.slice.call(arguments);
|
99 |
|
100 | if (typeof item.callback === 'function') {
|
101 | setImmediate(function () {
|
102 | item.callback.apply(null, args);
|
103 | });
|
104 | }
|
105 | }.bind(this));
|
106 |
|
107 | setTimeout(function sendNextMail() {
|
108 | if (this.currentConnections < this.maxConnections) {
|
109 | this.sending = false;
|
110 | this.processQueue();
|
111 | } else {
|
112 | setTimeout(sendNextMail.bind(this), THROTTLE_DELAY);
|
113 | }
|
114 | }.bind(this), Math.ceil(1000 / this.rateLimit));
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | SESTransport.prototype.sendMessage = function (mail, callback) {
|
124 | this.generateMessage(mail.message.createReadStream(), (function (err, raw) {
|
125 | if (err) {
|
126 | return typeof callback === 'function' && callback(err);
|
127 | }
|
128 | this.handleMessage(mail, raw, callback);
|
129 | }).bind(this));
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | SESTransport.prototype.handleMessage = function (mail, raw, callback) {
|
140 | var params = {
|
141 | RawMessage: {
|
142 | Data: new Buffer(raw, 'utf-8')
|
143 | }
|
144 | };
|
145 | if (this.options.source) {
|
146 | params.Source = this.options.source;
|
147 | }
|
148 | if (mail.data.Destinations) {
|
149 | params.Destinations = mail.data.Destinations;
|
150 | }
|
151 | this.currentConnections++;
|
152 | this.ses.sendRawEmail(params, function (err, data) {
|
153 | this.currentConnections--;
|
154 | this.responseHandler(err, mail, data, callback);
|
155 | }.bind(this));
|
156 | };
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | SESTransport.prototype.responseHandler = function (err, mail, data, callback) {
|
167 | if (err) {
|
168 | if (!(err instanceof Error)) {
|
169 | err = new Error('Email failed: ' + err);
|
170 | }
|
171 | return typeof callback === 'function' && callback(err, null);
|
172 | }
|
173 | return typeof callback === 'function' && callback(null, {
|
174 | envelope: mail.data.envelope || mail.message.getEnvelope(),
|
175 | messageId: data && data.MessageId && data.MessageId + '@' + this.options.region + '.amazonses.com'
|
176 | });
|
177 | };
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | SESTransport.prototype.generateMessage = function (mailStream, callback) {
|
189 | var chunks = [];
|
190 | var chunklen = 0;
|
191 |
|
192 | mailStream.on('data', function (chunk) {
|
193 | chunks.push(chunk);
|
194 | chunklen += chunk.length;
|
195 | });
|
196 |
|
197 | mailStream.on('error', function(err) {
|
198 | callback(err);
|
199 | });
|
200 |
|
201 | mailStream.on('end', function () {
|
202 | callback(null, Buffer.concat(chunks, chunklen).toString());
|
203 | });
|
204 | };
|