nodemailer-smtp-pool
Advanced tools
Comparing version 1.1.5 to 1.1.6
@@ -1,11 +0,9 @@ | ||
module.exports = function(grunt) { | ||
'use strict'; | ||
'use strict'; | ||
module.exports = function (grunt) { | ||
// Project configuration. | ||
grunt.initConfig({ | ||
jshint: { | ||
all: ['src/*.js', 'test/*.js'], | ||
options: { | ||
jshintrc: '.jshintrc' | ||
} | ||
eslint: { | ||
all: ['src/*.js', 'test/*.js', 'Gruntfile.js', '.eslintrc.js'] | ||
}, | ||
@@ -24,7 +22,7 @@ | ||
// Load the plugin(s) | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
grunt.loadNpmTasks('grunt-eslint'); | ||
grunt.loadNpmTasks('grunt-mocha-test'); | ||
// Tasks | ||
grunt.registerTask('default', ['jshint', 'mochaTest']); | ||
}; | ||
grunt.registerTask('default', ['eslint', 'mochaTest']); | ||
}; |
{ | ||
"name": "nodemailer-smtp-pool", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "SMTP transport for Nodemailer", | ||
@@ -26,3 +26,3 @@ "main": "src/smtp-pool.js", | ||
"nodemailer-wellknown": "^0.1.7", | ||
"smtp-connection": "^1.3.2" | ||
"smtp-connection": "^1.3.3" | ||
}, | ||
@@ -32,3 +32,3 @@ "devDependencies": { | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jshint": "^0.11.3", | ||
"grunt-eslint": "^17.3.1", | ||
"grunt-mocha-test": "^0.12.7", | ||
@@ -35,0 +35,0 @@ "mocha": "^2.3.4", |
@@ -7,3 +7,3 @@ 'use strict'; | ||
var clone = require('clone'); | ||
var PoolResource = require('./pool-resource'); | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -13,3 +13,3 @@ var util = require('util'); | ||
// expose to the world | ||
module.exports = function(options) { | ||
module.exports = function (options) { | ||
return new SMTPPool(options); | ||
@@ -34,3 +34,3 @@ }; | ||
if (this.options.service && (hostData = wellknown(this.options.service))) { | ||
Object.keys(hostData).forEach(function(key) { | ||
Object.keys(hostData).forEach(function (key) { | ||
if (!(key in this.options)) { | ||
@@ -67,15 +67,6 @@ this.options[key] = hostData[key]; | ||
*/ | ||
SMTPPool.prototype.send = function(mail, callback) { | ||
var called = false; | ||
SMTPPool.prototype.send = function (mail, callback) { | ||
this._queue.push({ | ||
mail: mail, | ||
callback: function(){ | ||
// callback might me fired twice, depending on how connection error is handled | ||
// so we enforce strict limit of single run only | ||
if(called){ | ||
return; | ||
} | ||
called = true; | ||
callback.apply(null, Array.prototype.slice.call(arguments)); | ||
} | ||
callback: callback | ||
}); | ||
@@ -89,4 +80,5 @@ this._processMessages(); | ||
*/ | ||
SMTPPool.prototype.close = function() { | ||
SMTPPool.prototype.close = function () { | ||
var connection; | ||
this._closed = true; | ||
@@ -122,4 +114,4 @@ | ||
*/ | ||
SMTPPool.prototype._processMessages = function() { | ||
var connection, element; | ||
SMTPPool.prototype._processMessages = function () { | ||
var connection; | ||
@@ -146,3 +138,3 @@ if (!this._queue.length || this._closed) { | ||
element = this._queue.shift(); | ||
var element = connection.queueElement = this._queue.shift(); | ||
connection.available = false; | ||
@@ -164,6 +156,8 @@ | ||
connection.once('error', element.callback); | ||
connection.send(element.mail, function (err, info) { | ||
connection.removeListener('error', element.callback); | ||
element.callback(err, info); | ||
// only process callback if current handler is not changed | ||
if (element === connection.queueElement) { | ||
element.callback(err, info); | ||
connection.queueElement = false; | ||
} | ||
}); | ||
@@ -175,4 +169,5 @@ }; | ||
*/ | ||
SMTPPool.prototype._createConnection = function() { | ||
SMTPPool.prototype._createConnection = function () { | ||
var connection = new PoolResource(this); | ||
connection.id = ++this._connectionCounter; | ||
@@ -187,3 +182,3 @@ | ||
connection.on('log', function(log) { | ||
connection.on('log', function (log) { | ||
this.emit('log', log); | ||
@@ -193,3 +188,3 @@ }.bind(this)); | ||
// resource comes available | ||
connection.on('available', function() { | ||
connection.on('available', function () { | ||
if (this.options.debug) { | ||
@@ -212,3 +207,3 @@ this.emit('log', { | ||
// resource is terminated with an error | ||
connection.once('error', function(err) { | ||
connection.once('error', function (err) { | ||
if (this.options.debug) { | ||
@@ -221,2 +216,7 @@ this.emit('log', { | ||
if (connection.queueElement) { | ||
connection.queueElement.callback(err); | ||
connection.queueElement = false; | ||
} | ||
// remove the erroneus connection from connections list | ||
@@ -228,3 +228,3 @@ this._removeConnection(connection); | ||
connection.once('close', function() { | ||
connection.once('close', function () { | ||
if (this.options.debug) { | ||
@@ -238,3 +238,17 @@ this.emit('log', { | ||
this._removeConnection(connection); | ||
this._continueProcessing(); | ||
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._queue.unshift(connection.queueElement); | ||
connection.queueElement = false; | ||
} | ||
this._continueProcessing(); | ||
}.bind(this), 50); | ||
} else { | ||
this._continueProcessing(); | ||
} | ||
}.bind(this)); | ||
@@ -250,3 +264,3 @@ | ||
*/ | ||
SMTPPool.prototype._continueProcessing = function() { | ||
SMTPPool.prototype._continueProcessing = function () { | ||
if (this._closed) { | ||
@@ -264,8 +278,7 @@ this.close(); | ||
*/ | ||
SMTPPool.prototype._removeConnection = function(connection) { | ||
for (var i = 0, len = this._connections.length; i < len; i++) { | ||
if (this._connections[i] === connection) { | ||
this._connections.splice(i, 1); | ||
break; | ||
} | ||
SMTPPool.prototype._removeConnection = function (connection) { | ||
var index = this._connections.indexOf(connection); | ||
if (index !== -1) { | ||
this._connections.splice(index, 1); | ||
} | ||
@@ -279,3 +292,3 @@ }; | ||
*/ | ||
SMTPPool.prototype._checkRateLimit = function(callback) { | ||
SMTPPool.prototype._checkRateLimit = function (callback) { | ||
if (!this.options.rateLimit) { | ||
@@ -304,3 +317,3 @@ return callback(); | ||
*/ | ||
SMTPPool.prototype._clearRateLimit = function() { | ||
SMTPPool.prototype._clearRateLimit = function () { | ||
clearTimeout(this._rateLimit.timeout); | ||
@@ -317,141 +330,1 @@ this._rateLimit.timeout = null; | ||
}; | ||
/** | ||
* Creates an element for the pool | ||
* | ||
* @constructor | ||
* @param {Object} options SMTPPool instance | ||
*/ | ||
function PoolResource(pool) { | ||
EventEmitter.call(this); | ||
this.pool = pool; | ||
this.options = pool.options; | ||
this._connection = false; | ||
this._connected = false; | ||
this.messages = 0; | ||
this.available = true; | ||
} | ||
util.inherits(PoolResource, EventEmitter); | ||
/** | ||
* Initiates a connection to the SMTP server | ||
* | ||
* @param {Function} callback Callback function to run once the connection is established or failed | ||
*/ | ||
PoolResource.prototype.connect = function(callback) { | ||
var returned = false; | ||
if (!this.connection) { | ||
this.connection = new SMTPConnection(this.options); | ||
} | ||
this.connection.on('log', function(log) { | ||
this.emit('log', log); | ||
}.bind(this)); | ||
this.connection.once('error', function(err) { | ||
this.emit('error', err); | ||
if (returned) { | ||
return; | ||
} | ||
returned = true; | ||
return callback(err); | ||
}.bind(this)); | ||
this.connection.once('end', function() { | ||
this.close(); | ||
if (returned) { | ||
return; | ||
} | ||
returned = true; | ||
return callback(); | ||
}.bind(this)); | ||
this.connection.connect(function() { | ||
if (returned) { | ||
return; | ||
} | ||
if (this.options.auth) { | ||
this.connection.login(this.options.auth, function(err) { | ||
if (returned) { | ||
return; | ||
} | ||
returned = true; | ||
if (err) { | ||
this.connection.close(); | ||
this.emit('error', err); | ||
return callback(err); | ||
} | ||
this._connected = true; | ||
callback(null, true); | ||
}.bind(this)); | ||
} else { | ||
returned = true; | ||
this._connected = true; | ||
callback(null, true); | ||
} | ||
}.bind(this)); | ||
}; | ||
/** | ||
* Sends an e-mail to be sent using the selected settings | ||
* | ||
* @param {Object} mail Mail object | ||
* @param {Function} callback Callback function | ||
*/ | ||
PoolResource.prototype.send = function(mail, callback) { | ||
if (!this._connected) { | ||
this.connect(function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
this.send(mail, callback); | ||
}.bind(this)); | ||
return; | ||
} | ||
this.connection.send(mail.data.envelope || mail.message.getEnvelope(), mail.message.createReadStream(), function(err, info) { | ||
var envelope; | ||
this.messages++; | ||
if (err) { | ||
this.connection.close(); | ||
this.emit('error', err); | ||
return callback(err); | ||
} else { | ||
envelope = mail.data.envelope || mail.message.getEnvelope(); | ||
info.envelope = { | ||
from: envelope.from, | ||
to: envelope.to | ||
}; | ||
info.messageId = (mail.message.getHeader('message-id') || '').replace(/[<>\s]/g, ''); | ||
callback(null, info); | ||
} | ||
if (this.messages >= this.options.maxMessages) { | ||
this.connection.close(); | ||
this.emit('error', new Error('Resource exhausted')); | ||
} else { | ||
this.pool._checkRateLimit(function() { | ||
this.available = true; | ||
this.emit('available'); | ||
}.bind(this)); | ||
} | ||
}.bind(this)); | ||
}; | ||
/** | ||
* Closes the connection | ||
*/ | ||
PoolResource.prototype.close = function() { | ||
this._connected = false; | ||
if (this.connection) { | ||
this.connection.close(); | ||
} | ||
this.emit('close'); | ||
}; |
Sorry, the diff of this file is not supported yet
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
21533
8
466
Updatedsmtp-connection@^1.3.3