1
0
mirror of https://github.com/eleith/emailjs.git synced 2024-07-05 20:10:37 +00:00

fix type errors in smtp

This commit is contained in:
Zack Schuster 2018-06-27 19:57:53 -07:00
parent ce1e73eba3
commit e2f7072631

View File

@ -53,8 +53,19 @@ const SMTPState = {
class SMTP extends EventEmitter { class SMTP extends EventEmitter {
/** /**
* @typedef {Object} SMTPOptions
* @property {number} [timeout]
* @property {string} [user]
* @property {string} [password]
* @property {string} [domain]
* @property {string} [host]
* @property {number} [port]
* @property {boolean} [ssl]
* @property {boolean} [tls]
* @property {string[]} [authentication]
*
* @constructor * @constructor
* @param {{ timeout: (number | undefined), user: (string | undefined), password: (string | undefined), domain: (string | undefined), host: (string | undefined), port: (number | undefined), ssl: (boolean | undefined), tls: (boolean | undefined), authentication: (string[]) }} [options] instance options * @param {SMTPOptions} [options] instance options
*/ */
constructor(options = {}) { constructor(options = {}) {
super(); super();
@ -101,18 +112,18 @@ class SMTP extends EventEmitter {
this._secure = false; this._secure = false;
/** /**
* @type {Socket} * @type {Socket|TLSSocket}
*/ */
this.sock = null; this.sock = null;
/** /**
* @type {{} [string]: string | boolean } * @type {{ [i: string]: string | boolean }}
*/ */
this.features = null; this.features = null;
this.monitor = null; this.monitor = null;
/** /**
* @type {string}[] } * @type {string[]}
*/ */
this.authentication = authentication; this.authentication = authentication;
@ -179,10 +190,13 @@ class SMTP extends EventEmitter {
} }
/** /**
* @typedef {Object} ConnectOptions
* @property {boolean} [ssl]
*
* @param {Function} callback function to call after response * @param {Function} callback function to call after response
* @param {number} [port] the port to use for the connection * @param {number} [port] the port to use for the connection
* @param {string} [host] the hostname to use for the connection * @param {string} [host] the hostname to use for the connection
* @param {{ ssl: boolean }} [options={}] the options * @param {ConnectOptions} [options={}] the options
* @returns {void} * @returns {void}
*/ */
connect(callback, port = this.port, host = this.host, options = {}) { connect(callback, port = this.port, host = this.host, options = {}) {
@ -207,10 +221,20 @@ class SMTP extends EventEmitter {
if (this.ssl && !this.tls) { if (this.ssl && !this.tls) {
// if key/ca/cert was passed in, check if connection is authorized // if key/ca/cert was passed in, check if connection is authorized
if (typeof this.ssl !== 'boolean' && !this.sock.authorized) { if (
typeof this.ssl !== 'boolean' &&
this.sock instanceof TLSSocket &&
!this.sock.authorized
) {
this.close(true); this.close(true);
const msg = 'could not establish an ssl connection'; caller(
caller(callback, SMTPError(msg, SMTPError.CONNECTIONAUTH, err)); callback,
SMTPError(
'could not establish an ssl connection',
SMTPError.CONNECTIONAUTH,
err
)
);
} else { } else {
this._secure = true; this._secure = true;
} }
@ -240,13 +264,15 @@ class SMTP extends EventEmitter {
} else { } else {
log(`response (data): ${msg.data}`); log(`response (data): ${msg.data}`);
this.quit(() => { this.quit(() => {
const err = SMTPError( caller(
'bad response on connection', callback,
SMTPError.BADRESPONSE, SMTPError(
err, 'bad response on connection',
msg.data SMTPError.BADRESPONSE,
err,
msg.data
)
); );
caller(callback, err);
}); });
} }
}; };
@ -258,7 +284,7 @@ class SMTP extends EventEmitter {
this.sock = connect( this.sock = connect(
this.port, this.port,
this.host, this.host,
this.ssl, {},
connected connected
); );
} else { } else {
@ -311,7 +337,7 @@ class SMTP extends EventEmitter {
* @returns {void} * @returns {void}
*/ */
command(cmd, callback, codes = [250]) { command(cmd, callback, codes = [250]) {
codes = Array.isArray(codes) const codesArray = Array.isArray(codes)
? codes ? codes
: typeof codes === 'number' : typeof codes === 'number'
? [codes] ? [codes]
@ -321,7 +347,7 @@ class SMTP extends EventEmitter {
if (err) { if (err) {
caller(callback, err); caller(callback, err);
} else { } else {
if (codes.indexOf(Number(msg.code)) !== -1) { if (codesArray.indexOf(Number(msg.code)) !== -1) {
caller(callback, err, msg.data, msg.message); caller(callback, err, msg.data, msg.message);
} else { } else {
const suffix = msg.message ? `: ${msg.message}` : ''; const suffix = msg.message ? `: ${msg.message}` : '';
@ -370,7 +396,7 @@ class SMTP extends EventEmitter {
err.message += ' while establishing a starttls session'; err.message += ' while establishing a starttls session';
caller(callback, err); caller(callback, err);
} else { } else {
const secureContext = createSecureContext(this.tls); const secureContext = createSecureContext({});
const secureSocket = new TLSSocket(this.sock, { secureContext }); const secureSocket = new TLSSocket(this.sock, { secureContext });
secureSocket.on('error', err => { secureSocket.on('error', err => {
@ -691,20 +717,14 @@ class SMTP extends EventEmitter {
break; break;
case AUTH_METHODS.PLAIN: case AUTH_METHODS.PLAIN:
this.command( this.command(
`AUTH ${AUTH_METHODS.PLAIN} ${encode_plain( `AUTH ${AUTH_METHODS.PLAIN} ${encode_plain()}`,
login.user(),
login.password()
)}`,
response, response,
[235, 503] [235, 503]
); );
break; break;
case AUTH_METHODS.XOAUTH2: case AUTH_METHODS.XOAUTH2:
this.command( this.command(
`AUTH ${AUTH_METHODS.XOAUTH2} ${encode_xoauth2( `AUTH ${AUTH_METHODS.XOAUTH2} ${encode_xoauth2()}`,
login.user(),
login.password()
)}`,
response, response,
[235, 503] [235, 503]
); );