smtp-pool.js
13.1 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
'use strict';
var SMTPConnection = require('smtp-connection');
var packageData = require('../package.json');
var wellknown = require('nodemailer-wellknown');
var assign = require('./assign');
var PoolResource = require('./pool-resource');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var shared = require('nodemailer-shared');
// expose to the world
module.exports = function (options) {
return new SMTPPool(options);
};
/**
* Creates a SMTP pool transport object for Nodemailer
*
* @constructor
* @param {Object} options SMTP Connection options
*/
function SMTPPool(options) {
EventEmitter.call(this);
options = options || {};
if (typeof options === 'string') {
options = {
url: options
};
}
var urlData;
var service = options.service;
if (typeof options.getSocket === 'function') {
this.getSocket = options.getSocket;
}
if (options.url) {
urlData = shared.parseConnectionUrl(options.url);
service = service || urlData.service;
}
this.options = assign(
false, // create new object
options, // regular options
urlData, // url options
service && wellknown(service) // wellknown options
);
this.options.maxConnections = this.options.maxConnections || 5;
this.options.maxMessages = this.options.maxMessages || 100;
this.logger = this.options.logger = shared.getLogger(this.options);
// temporary object
var connection = new SMTPConnection(this.options);
this.name = 'SMTP (pool)';
this.version = packageData.version + '[client:' + connection.version + ']';
this._rateLimit = {
counter: 0,
timeout: null,
waiting: [],
checkpoint: false
};
this._closed = false;
this._queue = [];
this._connections = [];
this._connectionCounter = 0;
this.idling = true;
setImmediate(function () {
if (this.idling) {
this.emit('idle');
}
}.bind(this));
}
util.inherits(SMTPPool, EventEmitter);
/**
* Placeholder function for creating proxy sockets. This method immediatelly returns
* without a socket
*
* @param {Object} options Connection options
* @param {Function} callback Callback function to run with the socket keys
*/
SMTPPool.prototype.getSocket = function (options, callback) {
// return immediatelly
return callback(null, false);
};
/**
* Queues an e-mail to be sent using the selected settings
*
* @param {Object} mail Mail object
* @param {Function} callback Callback function
*/
SMTPPool.prototype.send = function (mail, callback) {
if (this._closed) {
return false;
}
this._queue.push({
mail: mail,
callback: callback
});
if (this.idling && this._queue.length >= this.options.maxConnections) {
this.idling = false;
}
setImmediate(this._processMessages.bind(this));
return true;
};
/**
* Closes all connections in the pool. If there is a message being sent, the connection
* is closed later
*/
SMTPPool.prototype.close = function () {
var connection;
var len = this._connections.length;
this._closed = true;
// clear rate limit timer if it exists
clearTimeout(this._rateLimit.timeout);
// remove all available connections
for (var i = len - 1; i >= 0; i--) {
if (this._connections[i] && this._connections[i].available) {
connection = this._connections[i];
connection.close();
this.logger.info('Connection #%s removed', connection.id);
}
}
if (len && !this._connections.length) {
this.logger.debug('All connections removed');
}
// make sure that entire queue would be cleaned
var invokeCallbacks = function () {
if (!this._queue.length) {
this.logger.debug('Pending queue elements cleared');
return;
}
var element = this._queue.shift();
if (element && typeof element.callback === 'function') {
try {
element.callback(new Error('Connection pool was closed'));
} catch (E) {
this.logger.error('Callback error for #%s: %s', connection.id, E.message);
}
}
setImmediate(invokeCallbacks);
}.bind(this);
setImmediate(invokeCallbacks);
};
/**
* Check the queue and available connections. If there is a message to be sent and there is
* an available connection, then use this connection to send the mail
*/
SMTPPool.prototype._processMessages = function () {
var connection;
var i, len;
// do nothing if already closed
if (this._closed) {
return;
}
// do nothing if queue is empty
if (!this._queue.length) {
if (!this.idling) {
// no pending jobs
this.idling = true;
this.emit('idle');
}
return;
}
// find first available connection
for (i = 0, len = this._connections.length; i < len; i++) {
if (this._connections[i].available) {
connection = this._connections[i];
break;
}
}
if (!connection && this._connections.length < this.options.maxConnections) {
connection = this._createConnection();
}
if (!connection) {
// no more free connection slots available
this.idling = false;
return;
}
// check if there is free space in the processing queue
if (!this.idling && this._queue.length < this.options.maxConnections) {
this.idling = true;
this.emit('idle');
}
var element = connection.queueElement = this._queue.shift();
element.messageId = (connection.queueElement.mail.message.getHeader('message-id') || '').replace(/[<>\s]/g, '');
connection.available = false;
this.logger.debug('Assigned message <%s> to #%s (%s)', element.messageId, connection.id, connection.messages + 1);
if (this.options.rateLimit) {
this._rateLimit.counter++;
if (!this._rateLimit.checkpoint) {
this._rateLimit.checkpoint = Date.now();
}
}
connection.send(element.mail, function (err, info) {
// only process callback if current handler is not changed
if (element === connection.queueElement) {
try {
element.callback(err, info);
} catch (E) {
this.logger.error('Callback error for #%s: %s', connection.id, E.message);
}
connection.queueElement = false;
}
}.bind(this));
};
/**
* Creates a new pool resource
*/
SMTPPool.prototype._createConnection = function () {
var connection = new PoolResource(this);
connection.id = ++this._connectionCounter;
this.logger.info('Created new pool resource #%s', connection.id);
// resource comes available
connection.on('available', function () {
this.logger.debug('Connection #%s became available', connection.id);
if (this._closed) {
// if already closed run close() that will remove this connections from connections list
this.close();
} else {
// check if there's anything else to send
this._processMessages();
}
}.bind(this));
// resource is terminated with an error
connection.once('error', function (err) {
if (err.code !== 'EMAXLIMIT') {
this.logger.error('Pool Error for #%s: %s', connection.id, err.message);
} else {
this.logger.debug('Max messages limit exchausted for #%s', connection.id);
}
if (connection.queueElement) {
try {
connection.queueElement.callback(err);
} catch (E) {
this.logger.error('Callback error for #%s: %s', connection.id, E.message);
}
connection.queueElement = false;
}
// remove the erroneus connection from connections list
this._removeConnection(connection);
this._continueProcessing();
}.bind(this));
connection.once('close', function () {
this.logger.info('Connection #%s was closed', connection.id);
this._removeConnection(connection);
if (connection.queueElement) {
// If the connection closed when sending, add the message to the queue again
// Note that we must wait a bit.. because the callback of the 'error' handler might be called
// in the next event loop
setTimeout(function () {
if (connection.queueElement) {
this.logger.debug('Re-queued message <%s> for #%s', connection.queueElement.messageId, connection.id);
this._queue.unshift(connection.queueElement);
connection.queueElement = false;
}
this._continueProcessing();
}.bind(this), 50);
} else {
this._continueProcessing();
}
}.bind(this));
this._connections.push(connection);
return connection;
};
/**
* Continue to process message if the pool hasn't closed
*/
SMTPPool.prototype._continueProcessing = function () {
if (this._closed) {
this.close();
} else {
setTimeout(this._processMessages.bind(this), 100);
}
};
/**
* Remove resource from pool
*
* @param {Object} connection The PoolResource to remove
*/
SMTPPool.prototype._removeConnection = function (connection) {
var index = this._connections.indexOf(connection);
if (index !== -1) {
this._connections.splice(index, 1);
}
};
/**
* Checks if connections have hit current rate limit and if so, queues the availability callback
*
* @param {Function} callback Callback function to run once rate limiter has been cleared
*/
SMTPPool.prototype._checkRateLimit = function (callback) {
if (!this.options.rateLimit) {
return callback();
}
var now = Date.now();
if (this._rateLimit.counter < this.options.rateLimit) {
return callback();
}
this._rateLimit.waiting.push(callback);
if (this._rateLimit.checkpoint <= now - 1000) {
return this._clearRateLimit();
} else if (!this._rateLimit.timeout) {
this._rateLimit.timeout = setTimeout(this._clearRateLimit.bind(this), 1000 - (now - this._rateLimit.checkpoint));
this._rateLimit.checkpoint = now;
}
};
/**
* Clears current rate limit limitation and runs paused callback
*/
SMTPPool.prototype._clearRateLimit = function () {
clearTimeout(this._rateLimit.timeout);
this._rateLimit.timeout = null;
this._rateLimit.counter = 0;
this._rateLimit.checkpoint = false;
// resume all paused connections
while (this._rateLimit.waiting.length) {
var cb = this._rateLimit.waiting.shift();
setImmediate(cb);
}
};
/**
* Returns true if there are free slots in the queue
*/
SMTPPool.prototype.isIdle = function () {
return this.idling;
};
/**
* Verifies SMTP configuration
*
* @param {Function} callback Callback function
*/
SMTPPool.prototype.verify = function (callback) {
var promise;
if (!callback && typeof Promise === 'function') {
promise = new Promise(function (resolve, reject) {
callback = shared.callbackPromise(resolve, reject);
});
}
this.getSocket(this.options, function (err, socketOptions) {
if (err) {
return callback(err);
}
var options = this.options;
if (socketOptions && socketOptions.connection) {
this.logger.info('Using proxied socket from %s:%s to %s:%s', socketOptions.connection.remoteAddress, socketOptions.connection.remotePort, options.host || '', options.port || '');
options = assign(false, options);
Object.keys(socketOptions).forEach(function (key) {
options[key] = socketOptions[key];
});
}
var connection = new SMTPConnection(options);
var returned = false;
connection.once('error', function (err) {
if (returned) {
return;
}
returned = true;
connection.close();
return callback(err);
});
connection.once('end', function () {
if (returned) {
return;
}
returned = true;
return callback(new Error('Connection closed'));
});
var finalize = function () {
if (returned) {
return;
}
returned = true;
connection.quit();
return callback(null, true);
};
connection.connect(function () {
if (returned) {
return;
}
if (this.options.auth) {
connection.login(this.options.auth, function (err) {
if (returned) {
return;
}
if (err) {
returned = true;
connection.close();
return callback(err);
}
finalize();
});
} else {
finalize();
}
}.bind(this));
}.bind(this));
return promise;
};