emailjs 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/smtp/smtp.js CHANGED
@@ -1,42 +1,78 @@
1
1
  /*
2
2
  * SMTP class written using python's (2.7) smtplib.py as a base
3
3
  */
4
- const net = require('net');
5
- const crypto = require('crypto');
6
- const os = require('os');
7
- const tls = require('tls');
4
+ const { Socket } = require('net');
5
+ const { createHmac } = require('crypto');
6
+ const { hostname } = require('os');
7
+ const { connect, createSecureContext, TLSSocket } = require('tls');
8
8
  const { EventEmitter } = require('events');
9
9
 
10
10
  const SMTPResponse = require('./response');
11
11
  const SMTPError = require('./error');
12
12
 
13
+ /**
14
+ * @readonly
15
+ * @type {5000}
16
+ */
17
+ const TIMEOUT = 5000;
18
+
19
+ /**
20
+ * @readonly
21
+ * @type {25}
22
+ */
13
23
  const SMTP_PORT = 25;
24
+
25
+ /**
26
+ * @readonly
27
+ * @type {465}
28
+ */
14
29
  const SMTP_SSL_PORT = 465;
30
+
31
+ /**
32
+ * @readonly
33
+ * @type {587}
34
+ */
15
35
  const SMTP_TLS_PORT = 587;
36
+
37
+ /**
38
+ * @readonly
39
+ * @type {'\r\n'}
40
+ */
16
41
  const CRLF = '\r\n';
42
+
43
+ /**
44
+ * @readonly
45
+ * @enum
46
+ */
17
47
  const AUTH_METHODS = {
18
- PLAIN: 'PLAIN',
19
- CRAM_MD5: 'CRAM-MD5',
20
- LOGIN: 'LOGIN',
21
- XOAUTH2: 'XOAUTH2',
48
+ PLAIN: /** @type {'PLAIN'} */ ('PLAIN'),
49
+ CRAM_MD5: /** @type {'CRAM-MD5'} */ ('CRAM-MD5'),
50
+ LOGIN: /** @type {'LOGIN'} */ ('LOGIN'),
51
+ XOAUTH2: /** @type {'XOAUTH2'} */ ('XOAUTH2'),
22
52
  };
23
53
 
24
- const TIMEOUT = 5000;
54
+ /**
55
+ * @type {0 | 1}
56
+ */
25
57
  let DEBUG = 0;
26
58
 
27
- const log = function() {
28
- if (DEBUG) {
29
- Array.prototype.slice.call(arguments).forEach(function(d) {
30
- console.log(d);
31
- });
59
+ /**
60
+ * @param {...string} args the message(s) to log
61
+ * @returns {void}
62
+ */
63
+ const log = (...args) => {
64
+ if (DEBUG === 1) {
65
+ args.forEach(d => console.log(d));
32
66
  }
33
67
  };
34
68
 
35
- const caller = function(callback) {
36
- if (typeof callback == 'function') {
37
- const args = Array.prototype.slice.call(arguments);
38
- args.shift();
39
-
69
+ /**
70
+ * @param {function(...*): void} callback the function to call
71
+ * @param {...*} args the arguments to apply to the function
72
+ * @returns {void}
73
+ */
74
+ const caller = (callback, ...args) => {
75
+ if (typeof callback === 'function') {
40
76
  callback.apply(null, args);
41
77
  }
42
78
  };
@@ -48,78 +84,166 @@ const SMTPState = {
48
84
  };
49
85
 
50
86
  class SMTP extends EventEmitter {
51
- constructor(options = {}) {
87
+ /**
88
+ * @typedef {Object} SMTPSocketOptions
89
+ * @property {string} key
90
+ * @property {string} ca
91
+ * @property {string} cert
92
+ *
93
+ * @typedef {Object} SMTPOptions
94
+ * @property {number} [timeout]
95
+ * @property {string} [user]
96
+ * @property {string} [password]
97
+ * @property {string} [domain]
98
+ * @property {string} [host]
99
+ * @property {number} [port]
100
+ * @property {boolean|SMTPSocketOptions} [ssl]
101
+ * @property {boolean|SMTPSocketOptions} [tls]
102
+ * @property {string[]} [authentication]
103
+ *
104
+ * @constructor
105
+ * @param {SMTPOptions} [options] instance options
106
+ */
107
+ constructor({
108
+ timeout,
109
+ host,
110
+ user,
111
+ password,
112
+ domain,
113
+ port,
114
+ ssl,
115
+ tls,
116
+ authentication,
117
+ } = {}) {
52
118
  super();
53
119
 
54
- const {
55
- timeout,
56
- user,
57
- password,
58
- domain,
59
- host,
60
- port,
61
- ssl,
62
- tls,
63
- authentication,
64
- } = Object.assign(
65
- {
66
- timeout: TIMEOUT,
67
- domain: os.hostname(),
68
- host: 'localhost',
69
- ssl: false,
70
- tls: false,
71
- authentication: [
72
- AUTH_METHODS.CRAM_MD5,
73
- AUTH_METHODS.LOGIN,
74
- AUTH_METHODS.PLAIN,
75
- AUTH_METHODS.XOAUTH2,
76
- ],
77
- },
78
- options
79
- );
80
-
120
+ /**
121
+ * @private
122
+ * @type {number}
123
+ */
81
124
  this._state = SMTPState.NOTCONNECTED;
125
+
126
+ /**
127
+ * @private
128
+ * @type {boolean}
129
+ */
82
130
  this._secure = false;
83
131
 
132
+ /**
133
+ * @type {Socket|TLSSocket}
134
+ */
84
135
  this.sock = null;
136
+
137
+ /**
138
+ * @type {{ [i: string]: string | boolean }}
139
+ */
85
140
  this.features = null;
86
- this.monitor = null;
87
141
 
88
- this.authentication = authentication;
89
- this.timeout = timeout;
90
- this.domain = domain;
91
- this.host = host;
92
- this.ssl = ssl;
93
- this.tls = tls;
142
+ /**
143
+ * @type {SMTPResponse.SMTPResponse}
144
+ */
145
+ this.monitor = null;
94
146
 
147
+ /**
148
+ * @type {string[]}
149
+ */
150
+ this.authentication = Array.isArray(authentication)
151
+ ? authentication
152
+ : [
153
+ AUTH_METHODS.CRAM_MD5,
154
+ AUTH_METHODS.LOGIN,
155
+ AUTH_METHODS.PLAIN,
156
+ AUTH_METHODS.XOAUTH2,
157
+ ];
158
+
159
+ /**
160
+ * @type {number} }
161
+ */
162
+ this.timeout = typeof timeout === 'number' ? timeout : TIMEOUT;
163
+
164
+ /**
165
+ * @type {string} }
166
+ */
167
+ this.domain = typeof domain === 'string' ? domain : hostname();
168
+
169
+ /**
170
+ * @type {string} }
171
+ */
172
+ this.host = typeof host === 'string' ? host : 'localhost';
173
+
174
+ /**
175
+ * @type {boolean|SMTPSocketOptions}
176
+ */
177
+ this.ssl =
178
+ ssl != null &&
179
+ (typeof ssl === 'boolean' ||
180
+ (typeof ssl === 'object' && Array.isArray(ssl) === false))
181
+ ? ssl
182
+ : false;
183
+
184
+ /**
185
+ * @type {boolean|SMTPSocketOptions}
186
+ */
187
+ this.tls =
188
+ tls != null &&
189
+ (typeof tls === 'boolean' ||
190
+ (typeof tls === 'object' && Array.isArray(tls) === false))
191
+ ? tls
192
+ : false;
193
+
194
+ /**
195
+ * @type {number}
196
+ */
95
197
  this.port = port || (ssl ? SMTP_SSL_PORT : tls ? SMTP_TLS_PORT : SMTP_PORT);
198
+
199
+ /**
200
+ * @type {boolean}
201
+ */
96
202
  this.loggedin = user && password ? false : true;
97
203
 
98
204
  // keep these strings hidden when quicky debugging/logging
99
- this.user = () => options.user;
100
- this.password = () => options.password;
205
+ this.user = /** @returns {string} */ () => user;
206
+ this.password = /** @returns {string} */ () => password;
101
207
  }
102
208
 
209
+ /**
210
+ * @param {0 | 1} level -
211
+ * @returns {void}
212
+ */
103
213
  debug(level) {
104
214
  DEBUG = level;
105
215
  }
106
216
 
217
+ /**
218
+ * @returns {number} the current state
219
+ */
107
220
  state() {
108
221
  return this._state;
109
222
  }
110
223
 
224
+ /**
225
+ * @returns {boolean} whether or not the instance is authorized
226
+ */
111
227
  authorized() {
112
228
  return this.loggedin;
113
229
  }
114
230
 
115
- connect(callback, port, host, options) {
116
- options = options || {};
117
-
118
- this.host = host || this.host;
119
- this.port = port || this.port;
231
+ /**
232
+ * @typedef {Object} ConnectOptions
233
+ * @property {boolean} [ssl]
234
+ *
235
+ * @param {function(...*): void} callback function to call after response
236
+ * @param {number} [port] the port to use for the connection
237
+ * @param {string} [host] the hostname to use for the connection
238
+ * @param {ConnectOptions} [options={}] the options
239
+ * @returns {void}
240
+ */
241
+ connect(callback, port = this.port, host = this.host, options = {}) {
242
+ this.port = port;
243
+ this.host = host;
120
244
  this.ssl = options.ssl || this.ssl;
121
245
 
122
- if (this._state != SMTPState.NOTCONNECTED) {
246
+ if (this._state !== SMTPState.NOTCONNECTED) {
123
247
  this.quit(() =>
124
248
  this.connect(
125
249
  callback,
@@ -128,23 +252,42 @@ class SMTP extends EventEmitter {
128
252
  options
129
253
  )
130
254
  );
131
- return;
132
255
  }
133
256
 
134
- const connected = err => {
135
- if (!err) {
136
- log(`connected: ${this.host}:${this.port}`);
137
-
138
- if (this.ssl && !this.tls) {
139
- // if key/ca/cert was passed in, check if connection is authorized
140
- if (typeof this.ssl != 'boolean' && !this.sock.authorized) {
141
- this.close(true);
142
- const msg = 'could not establish an ssl connection';
143
- caller(callback, SMTPError(msg, SMTPError.CONNECTIONAUTH, err));
144
- } else {
145
- this._secure = true;
146
- }
257
+ /**
258
+ * @returns {void}
259
+ */
260
+ const connected = () => {
261
+ log(`connected: ${this.host}:${this.port}`);
262
+
263
+ if (this.ssl && !this.tls) {
264
+ // if key/ca/cert was passed in, check if connection is authorized
265
+ if (
266
+ typeof this.ssl !== 'boolean' &&
267
+ this.sock instanceof TLSSocket &&
268
+ !this.sock.authorized
269
+ ) {
270
+ this.close(true);
271
+ caller(
272
+ callback,
273
+ SMTPError(
274
+ 'could not establish an ssl connection',
275
+ SMTPError.CONNECTIONAUTH
276
+ )
277
+ );
278
+ } else {
279
+ this._secure = true;
147
280
  }
281
+ }
282
+ };
283
+
284
+ /**
285
+ * @param {Error} err err
286
+ * @returns {void}
287
+ */
288
+ const connectedErrBack = err => {
289
+ if (!err) {
290
+ connected();
148
291
  } else {
149
292
  this.close(true);
150
293
  caller(
@@ -170,13 +313,15 @@ class SMTP extends EventEmitter {
170
313
  } else {
171
314
  log(`response (data): ${msg.data}`);
172
315
  this.quit(() => {
173
- const err = SMTPError(
174
- 'bad response on connection',
175
- SMTPError.BADRESPONSE,
176
- err,
177
- msg.data
316
+ caller(
317
+ callback,
318
+ SMTPError(
319
+ 'bad response on connection',
320
+ SMTPError.BADRESPONSE,
321
+ err,
322
+ msg.data
323
+ )
178
324
  );
179
- caller(callback, err);
180
325
  });
181
326
  }
182
327
  };
@@ -185,18 +330,18 @@ class SMTP extends EventEmitter {
185
330
  log(`connecting: ${this.host}:${this.port}`);
186
331
 
187
332
  if (this.ssl) {
188
- this.sock = tls.connect(
333
+ this.sock = connect(
189
334
  this.port,
190
335
  this.host,
191
- this.ssl,
336
+ typeof this.ssl === 'object' ? this.ssl : {},
192
337
  connected
193
338
  );
194
339
  } else {
195
- this.sock = new net.Socket();
340
+ this.sock = new Socket();
196
341
  this.sock.connect(
197
342
  this.port,
198
343
  this.host,
199
- connected
344
+ connectedErrBack
200
345
  );
201
346
  }
202
347
 
@@ -207,8 +352,13 @@ class SMTP extends EventEmitter {
207
352
  this.sock.once('error', response); // the socket could reset or throw, so let's handle it and let the user know
208
353
  }
209
354
 
355
+ /**
356
+ * @param {string} str the string to send
357
+ * @param {*} callback function to call after response
358
+ * @returns {void}
359
+ */
210
360
  send(str, callback) {
211
- if (this.sock && this._state == SMTPState.CONNECTED) {
361
+ if (this.sock && this._state === SMTPState.CONNECTED) {
212
362
  log(str);
213
363
 
214
364
  this.sock.once('response', (err, msg) => {
@@ -229,10 +379,16 @@ class SMTP extends EventEmitter {
229
379
  }
230
380
  }
231
381
 
232
- command(cmd, callback, codes, failed) {
233
- codes = Array.isArray(codes)
382
+ /**
383
+ * @param {string} cmd command to issue
384
+ * @param {function(...*): void} callback function to call after response
385
+ * @param {(number[] | number)} [codes=[250]] array codes
386
+ * @returns {void}
387
+ */
388
+ command(cmd, callback, codes = [250]) {
389
+ const codesArray = Array.isArray(codes)
234
390
  ? codes
235
- : typeof codes == 'number'
391
+ : typeof codes === 'number'
236
392
  ? [codes]
237
393
  : [250];
238
394
 
@@ -240,7 +396,7 @@ class SMTP extends EventEmitter {
240
396
  if (err) {
241
397
  caller(callback, err);
242
398
  } else {
243
- if (codes.indexOf(Number(msg.code)) != -1) {
399
+ if (codesArray.indexOf(Number(msg.code)) !== -1) {
244
400
  caller(callback, err, msg.data, msg.message);
245
401
  } else {
246
402
  const suffix = msg.message ? `: ${msg.message}` : '';
@@ -258,12 +414,17 @@ class SMTP extends EventEmitter {
258
414
  this.send(cmd + CRLF, response);
259
415
  }
260
416
 
417
+ /**
418
+ * SMTP 'helo' command.
419
+ *
420
+ * Hostname to send for self command defaults to the FQDN of the local
421
+ * host.
422
+ *
423
+ * @param {function(...*): void} callback function to call after response
424
+ * @param {string} domain the domain to associate with the 'helo' request
425
+ * @returns {void}
426
+ */
261
427
  helo(callback, domain) {
262
- /*
263
- * SMTP 'helo' command.
264
- * Hostname to send for self command defaults to the FQDN of the local
265
- * host.
266
- */
267
428
  this.command(`helo ${domain || this.domain}`, (err, data) => {
268
429
  if (err) {
269
430
  caller(callback, err);
@@ -274,70 +435,41 @@ class SMTP extends EventEmitter {
274
435
  });
275
436
  }
276
437
 
438
+ /**
439
+ * @param {function(...*): void} callback function to call after response
440
+ * @returns {void}
441
+ */
277
442
  starttls(callback) {
278
443
  const response = (err, msg) => {
279
444
  if (err) {
280
445
  err.message += ' while establishing a starttls session';
281
446
  caller(callback, err);
282
447
  } else {
283
- // support new API
284
- if (tls.TLSSocket) {
285
- const secured_socket = new tls.TLSSocket(this.sock, {
286
- secureContext: tls.createSecureContext
287
- ? tls.createSecureContext(this.tls)
288
- : crypto.createCredentials(this.tls),
289
- isServer: false, // older versions of node (0.12), do not default to false properly...
290
- });
291
-
292
- secured_socket.on('error', err => {
293
- this.close(true);
294
- caller(callback, err);
295
- });
448
+ const secureContext = createSecureContext(
449
+ typeof this.tls === 'object' ? this.tls : {}
450
+ );
451
+ const secureSocket = new TLSSocket(this.sock, { secureContext });
296
452
 
297
- this._secure = true;
298
- this.sock = secured_socket;
453
+ secureSocket.on('error', err => {
454
+ this.close(true);
455
+ caller(callback, err);
456
+ });
299
457
 
300
- SMTPResponse.monitor(this.sock, this.timeout, () => this.close(true));
301
- caller(callback, msg.data);
302
- } else {
303
- let secured_socket = null;
304
- const secured = () => {
305
- this._secure = true;
306
- this.sock = secured_socket;
458
+ this._secure = true;
459
+ this.sock = secureSocket;
307
460
 
308
- SMTPResponse.monitor(this.sock, this.timeout, () =>
309
- this.close(true)
310
- );
311
- caller(callback, msg.data);
312
- };
313
-
314
- const starttls = require('starttls');
315
- secured_socket = starttls(
316
- {
317
- socket: this.sock,
318
- host: this.host,
319
- port: this.port,
320
- pair: tls.createSecurePair(
321
- tls.createSecureContext
322
- ? tls.createSecureContext(this.tls)
323
- : crypto.createCredentials(this.tls),
324
- false
325
- ),
326
- },
327
- secured
328
- ).cleartext;
329
-
330
- secured_socket.on('error', err => {
331
- this.close(true);
332
- caller(callback, err);
333
- });
334
- }
461
+ SMTPResponse.monitor(this.sock, this.timeout, () => this.close(true));
462
+ caller(callback, msg.data);
335
463
  }
336
464
  };
337
465
 
338
466
  this.command('starttls', response, [220]);
339
467
  }
340
468
 
469
+ /**
470
+ * @param {string} data the string to parse for features
471
+ * @returns {void}
472
+ */
341
473
  parse_smtp_features(data) {
342
474
  // According to RFC1869 some (badly written)
343
475
  // MTA's will disconnect on an ehlo. Toss an exception if
@@ -353,7 +485,7 @@ class SMTP extends EventEmitter {
353
485
  // 2) There are some servers that only advertise the auth methods we
354
486
  // support using the old style.
355
487
 
356
- if (parse) {
488
+ if (parse != null) {
357
489
  // RFC 1869 requires a space between ehlo keyword and parameters.
358
490
  // It's actually stricter, in that only spaces are allowed between
359
491
  // parameters, but were not going to check for that here. Note
@@ -361,10 +493,13 @@ class SMTP extends EventEmitter {
361
493
  this.features[parse[1].toLowerCase()] = parse[2] || true;
362
494
  }
363
495
  });
364
-
365
- return;
366
496
  }
367
497
 
498
+ /**
499
+ * @param {function(...*): void} callback function to call after response
500
+ * @param {string} domain the domain to associate with the 'ehlo' request
501
+ * @returns {void}
502
+ */
368
503
  ehlo(callback, domain) {
369
504
  this.features = {};
370
505
  this.command(`ehlo ${domain || this.domain}`, (err, data) => {
@@ -382,58 +517,117 @@ class SMTP extends EventEmitter {
382
517
  });
383
518
  }
384
519
 
520
+ /**
521
+ * @param {string} opt the features keyname to check
522
+ * @returns {boolean} whether the extension exists
523
+ */
385
524
  has_extn(opt) {
386
525
  return this.features[opt.toLowerCase()] === undefined;
387
526
  }
388
527
 
389
- help(callback, args) {
390
- // SMTP 'help' command, returns text from the server
391
- this.command(args ? `help ${args}` : 'help', callback, [211, 214]);
528
+ /**
529
+ * SMTP 'help' command, returns text from the server
530
+ * @param {function(...*): void} callback function to call after response
531
+ * @param {string} domain the domain to associate with the 'help' request
532
+ * @returns {void}
533
+ */
534
+ help(callback, domain) {
535
+ this.command(domain ? `help ${domain}` : 'help', callback, [211, 214]);
392
536
  }
393
537
 
538
+ /**
539
+ * @param {function(...*): void} callback function to call after response
540
+ * @returns {void}
541
+ */
394
542
  rset(callback) {
395
543
  this.command('rset', callback);
396
544
  }
397
545
 
546
+ /**
547
+ * @param {function(...*): void} callback function to call after response
548
+ * @returns {void}
549
+ */
398
550
  noop(callback) {
399
551
  this.send('noop', callback);
400
552
  }
401
553
 
554
+ /**
555
+ * @param {function(...*): void} callback function to call after response
556
+ * @param {string} from the sender
557
+ * @returns {void}
558
+ */
402
559
  mail(callback, from) {
403
560
  this.command(`mail FROM:${from}`, callback);
404
561
  }
405
562
 
563
+ /**
564
+ * @param {function(...*): void} callback function to call after response
565
+ * @param {string} to the receiver
566
+ * @returns {void}
567
+ */
406
568
  rcpt(callback, to) {
407
569
  this.command(`RCPT TO:${to}`, callback, [250, 251]);
408
570
  }
409
571
 
572
+ /**
573
+ * @param {function(...*): void} callback function to call after response
574
+ * @returns {void}
575
+ */
410
576
  data(callback) {
411
577
  this.command('data', callback, [354]);
412
578
  }
413
579
 
580
+ /**
581
+ * @param {function(...*): void} callback function to call after response
582
+ * @returns {void}
583
+ */
414
584
  data_end(callback) {
415
585
  this.command(`${CRLF}.`, callback);
416
586
  }
417
587
 
588
+ /**
589
+ * @param {string} data the message to send
590
+ * @returns {void}
591
+ */
418
592
  message(data) {
419
593
  log(data);
420
594
  this.sock.write(data);
421
595
  }
422
596
 
597
+ /**
598
+ * SMTP 'verify' command -- checks for address validity.
599
+ *
600
+ * @param {string} address the address to validate
601
+ * @param {function(...*): void} callback function to call after response
602
+ * @returns {void}
603
+ */
423
604
  verify(address, callback) {
424
- // SMTP 'verify' command -- checks for address validity.
425
605
  this.command(`vrfy ${address}`, callback, [250, 251, 252]);
426
606
  }
427
607
 
608
+ /**
609
+ * SMTP 'expn' command -- expands a mailing list.
610
+ *
611
+ * @param {string} address the mailing list to expand
612
+ * @param {function(...*): void} callback function to call after response
613
+ * @returns {void}
614
+ */
428
615
  expn(address, callback) {
429
- // SMTP 'expn' command -- expands a mailing list.
430
616
  this.command(`expn ${address}`, callback);
431
617
  }
432
618
 
619
+ /**
620
+ * Calls this.ehlo() and, if an error occurs, this.helo().
621
+ *
622
+ * If there has been no previous EHLO or HELO command self session, self
623
+ * method tries ESMTP EHLO first.
624
+ *
625
+ * @param {function(...*): void} callback function to call after response
626
+ * @param {string} [domain] the domain to associate with the command
627
+ * @returns {void}
628
+ */
433
629
  ehlo_or_helo_if_needed(callback, domain) {
434
- // Call this.ehlo() and/or this.helo() if needed.
435
- // If there has been no previous EHLO or HELO command self session, self
436
- // method tries ESMTP EHLO first.
630
+ // is this code callable...?
437
631
  if (!this.features) {
438
632
  const response = (err, data) => caller(callback, err, data);
439
633
  this.ehlo((err, data) => {
@@ -446,6 +640,20 @@ class SMTP extends EventEmitter {
446
640
  }
447
641
  }
448
642
 
643
+ /**
644
+ * Log in on an SMTP server that requires authentication.
645
+ *
646
+ * If there has been no previous EHLO or HELO command self session, self
647
+ * method tries ESMTP EHLO first.
648
+ *
649
+ * This method will return normally if the authentication was successful.
650
+ *
651
+ * @param {function(...*): void} callback function to call after response
652
+ * @param {string} [user] the username to authenticate with
653
+ * @param {string} [password] the password for the authentication
654
+ * @param {{ method: string, domain: string }} [options] login options
655
+ * @returns {void}
656
+ */
449
657
  login(callback, user, password, options) {
450
658
  const login = {
451
659
  user: user ? () => user : this.user,
@@ -461,35 +669,32 @@ class SMTP extends EventEmitter {
461
669
  return;
462
670
  }
463
671
 
464
- /*
465
- * Log in on an SMTP server that requires authentication.
466
- *
467
- * The arguments are:
468
- * - user: The user name to authenticate with.
469
- * - password: The password for the authentication.
470
- *
471
- * If there has been no previous EHLO or HELO command self session, self
472
- * method tries ESMTP EHLO first.
473
- *
474
- * This method will return normally if the authentication was successful.
475
- */
476
-
477
672
  let method = null;
478
673
 
674
+ /**
675
+ * @param {string} challenge challenge
676
+ * @returns {string} base64 cram hash
677
+ */
479
678
  const encode_cram_md5 = challenge => {
480
- const hmac = crypto.createHmac('md5', login.password());
679
+ const hmac = createHmac('md5', login.password());
481
680
  hmac.update(Buffer.from(challenge, 'base64').toString('ascii'));
482
681
  return Buffer.from(`${login.user()} ${hmac.digest('hex')}`).toString(
483
682
  'base64'
484
683
  );
485
684
  };
486
685
 
686
+ /**
687
+ * @returns {string} base64 login/password
688
+ */
487
689
  const encode_plain = () =>
488
690
  Buffer.from(`\u0000${login.user()}\u0000${login.password()}`).toString(
489
691
  'base64'
490
692
  );
491
693
 
492
- // see: https://developers.google.com/gmail/xoauth2_protocol
694
+ /**
695
+ * @see https://developers.google.com/gmail/xoauth2_protocol
696
+ * @returns {string} base64 xoauth2 auth token
697
+ */
493
698
  const encode_xoauth2 = () =>
494
699
  Buffer.from(
495
700
  `user=${login.user()}\u0001auth=Bearer ${login.password()}\u0001\u0001`
@@ -515,7 +720,12 @@ class SMTP extends EventEmitter {
515
720
  }
516
721
  }
517
722
 
518
- // handle bad responses from command differently
723
+ /**
724
+ * handle bad responses from command differently
725
+ * @param {Error} err err
726
+ * @param {*} data data
727
+ * @returns {void}
728
+ */
519
729
  const failed = (err, data) => {
520
730
  this.loggedin = false;
521
731
  this.close(); // if auth is bad, close the connection, it won't get better by itself
@@ -525,6 +735,11 @@ class SMTP extends EventEmitter {
525
735
  );
526
736
  };
527
737
 
738
+ /**
739
+ * @param {Error} err err
740
+ * @param {*} data data
741
+ * @returns {void}
742
+ */
528
743
  const response = (err, data) => {
529
744
  if (err) {
530
745
  failed(err, data);
@@ -534,13 +749,19 @@ class SMTP extends EventEmitter {
534
749
  }
535
750
  };
536
751
 
752
+ /**
753
+ * @param {Error} err err
754
+ * @param {*} data data
755
+ * @param {string} msg msg
756
+ * @returns {void}
757
+ */
537
758
  const attempt = (err, data, msg) => {
538
759
  if (err) {
539
760
  failed(err, data);
540
761
  } else {
541
- if (method == AUTH_METHODS.CRAM_MD5) {
762
+ if (method === AUTH_METHODS.CRAM_MD5) {
542
763
  this.command(encode_cram_md5(msg), response, [235, 503]);
543
- } else if (method == AUTH_METHODS.LOGIN) {
764
+ } else if (method === AUTH_METHODS.LOGIN) {
544
765
  this.command(
545
766
  Buffer.from(login.password()).toString('base64'),
546
767
  response,
@@ -550,11 +771,17 @@ class SMTP extends EventEmitter {
550
771
  }
551
772
  };
552
773
 
774
+ /**
775
+ * @param {Error} err err
776
+ * @param {*} data data
777
+ * @param {string} msg msg
778
+ * @returns {void}
779
+ */
553
780
  const attempt_user = (err, data, msg) => {
554
781
  if (err) {
555
782
  failed(err, data);
556
783
  } else {
557
- if (method == AUTH_METHODS.LOGIN) {
784
+ if (method === AUTH_METHODS.LOGIN) {
558
785
  this.command(
559
786
  Buffer.from(login.user()).toString('base64'),
560
787
  attempt,
@@ -573,20 +800,14 @@ class SMTP extends EventEmitter {
573
800
  break;
574
801
  case AUTH_METHODS.PLAIN:
575
802
  this.command(
576
- `AUTH ${AUTH_METHODS.PLAIN} ${encode_plain(
577
- login.user(),
578
- login.password()
579
- )}`,
803
+ `AUTH ${AUTH_METHODS.PLAIN} ${encode_plain()}`,
580
804
  response,
581
805
  [235, 503]
582
806
  );
583
807
  break;
584
808
  case AUTH_METHODS.XOAUTH2:
585
809
  this.command(
586
- `AUTH ${AUTH_METHODS.XOAUTH2} ${encode_xoauth2(
587
- login.user(),
588
- login.password()
589
- )}`,
810
+ `AUTH ${AUTH_METHODS.XOAUTH2} ${encode_xoauth2()}`,
590
811
  response,
591
812
  [235, 503]
592
813
  );
@@ -602,7 +823,11 @@ class SMTP extends EventEmitter {
602
823
  this.ehlo_or_helo_if_needed(initiate, domain);
603
824
  }
604
825
 
605
- close(force) {
826
+ /**
827
+ * @param {boolean} [force=false] whether or not to force destroy the connection
828
+ * @returns {void}
829
+ */
830
+ close(force = false) {
606
831
  if (this.sock) {
607
832
  if (force) {
608
833
  log('smtp connection destroyed!');
@@ -625,6 +850,10 @@ class SMTP extends EventEmitter {
625
850
  this.loggedin = !(this.user() && this.password());
626
851
  }
627
852
 
853
+ /**
854
+ * @param {function(...*): void} [callback] function to call after response
855
+ * @returns {void}
856
+ */
628
857
  quit(callback) {
629
858
  this.command(
630
859
  'quit',
@@ -640,3 +869,4 @@ class SMTP extends EventEmitter {
640
869
  exports.SMTP = SMTP;
641
870
  exports.state = SMTPState;
642
871
  exports.authentication = AUTH_METHODS;
872
+ exports.DEFAULT_TIMEOUT = TIMEOUT;