diff --git a/README.md b/README.md index ffbafbc..12b6ce4 100644 --- a/README.md +++ b/README.md @@ -394,7 +394,10 @@ Connection Instance Methods * **autotls** - _string_ - Set to 'always' to always attempt connection upgrades via STARTTLS, 'required' only if upgrading is required, or 'never' to never attempt upgrading. **Default:** 'never' * **connTimeout** - _integer_ - Number of milliseconds to wait for a connection to be established. **Default:** 10000 * **authTimeout** - _integer_ - Number of milliseconds to wait to be authenticated after a connection has been established. **Default:** 5000 - * **keepalive** - _boolean_ - Enable the keepalive mechanism. **Default:** true + * **keepalive** - _mixed_ - Configures the keepalive mechanism. Set to `true` to enable keepalive with defaults or set to object to enable and configure keepalive behavior: **Default:** true + * **interval** - _integer_ - This is the interval (in milliseconds) at which NOOPs are sent and the interval at which `idleInterval` is checked. **Default:** 10000 + * **idleInterval** - _integer_ - This is the interval (in milliseconds) at which an IDLE command (for servers that support IDLE) is re-sent. **Default:** 300000 (5 mins) + * **forceNoop** - _boolean_ - Set to `true` to force use of NOOP keepalive on servers also support IDLE. **Default:** false * **debug** - _function_ - If set, the function will be called with one argument, a string containing some debug info **Default:** (no debug output) * **connect**() - _(void)_ - Attempts to connect and authenticate with the IMAP server. diff --git a/lib/Connection.js b/lib/Connection.js index cae61b6..c4c43cf 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -67,9 +67,9 @@ function Connection(config) { xoauth2: config.xoauth2, connTimeout: config.connTimeout || 10000, authTimeout: config.authTimeout || 5000, - keepalive: (typeof config.keepalive === 'boolean' - ? config.keepalive - : true) + keepalive: (config.keepalive === undefined || config.keepalive === null + ? true + : config.keepalive) }; this._sock = config.socket || undefined; @@ -1532,10 +1532,15 @@ Connection.prototype._createCurrentBox = function() { Connection.prototype._doKeepaliveTimer = function(immediate) { var self = this, + interval = this._config.keepalive.interval || KEEPALIVE_INTERVAL, + idleWait = this._config.keepalive.idleInterval || MAX_IDLE_WAIT, + forceNoop = this._config.keepalive.forceNoop || false, timerfn = function() { if (self._idle.enabled) { // unlike NOOP, IDLE is only a valid command after authenticating - if (!self.serverSupports('IDLE') || self.state !== 'authenticated') + if (!self.serverSupports('IDLE') + || self.state !== 'authenticated' + || forceNoop) self._enqueue('NOOP', true); else { if (self._idle.started === undefined) { @@ -1543,14 +1548,14 @@ Connection.prototype._doKeepaliveTimer = function(immediate) { self._enqueue('IDLE', true); } else if (self._idle.started > 0) { var timeDiff = Date.now() - self._idle.started; - if (timeDiff >= MAX_IDLE_WAIT) { + if (timeDiff >= idleWait) { self._idle.enabled = false; self.debug && self.debug('=> DONE'); self._sock.write('DONE' + CRLF); return; } } - self._tmrKeepalive = setTimeout(timerfn, KEEPALIVE_INTERVAL); + self._tmrKeepalive = setTimeout(timerfn, interval); } } }; @@ -1558,7 +1563,7 @@ Connection.prototype._doKeepaliveTimer = function(immediate) { if (immediate) timerfn(); else - this._tmrKeepalive = setTimeout(timerfn, KEEPALIVE_INTERVAL); + this._tmrKeepalive = setTimeout(timerfn, interval); }; Connection.prototype._login = function() {