emailjs 1.0.11 → 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/response.js CHANGED
@@ -1,81 +1,139 @@
1
- var SMTPError = require('./error');
1
+ const SMTPError = require('./error');
2
2
 
3
- var SMTPResponse = function(stream, timeout, onerror)
4
- {
5
- var buffer = '',
3
+ /**
4
+ * @typedef {import('net').Socket} Socket
5
+ * @typedef {import('tls').TLSSocket} TLSSocket
6
+ */
6
7
 
7
- notify = function()
8
- {
9
- if(buffer.length)
10
- {
11
- // parse buffer for response codes
12
- var line = buffer.replace("\r", '');
13
-
14
- if(!line.trim().split(/\n/).pop().match(/^(\d{3})\s/))
15
- return;
16
-
17
- var match = line ? line.match(/(\d+)\s?(.*)/) : null;
8
+ class SMTPResponse {
9
+ /**
10
+ * @constructor
11
+ * @param {Socket | TLSSocket} stream the open socket to stream a response from
12
+ * @param {number} timeout the time to wait (in milliseconds) before closing the socket
13
+ * @param {function(Error): void} onerror the function to call on error
14
+ */
15
+ constructor(stream, timeout, onerror) {
16
+ let buffer = '';
18
17
 
19
- stream.emit('response', null, match ? {code:match[1], message:match[2], data:line} : {code:-1, data:line});
20
- buffer = '';
21
- }
22
- },
18
+ /**
19
+ * @returns {void}
20
+ */
21
+ const notify = () => {
22
+ if (buffer.length) {
23
+ // parse buffer for response codes
24
+ const line = buffer.replace('\r', '');
25
+ if (
26
+ !line
27
+ .trim()
28
+ .split(/\n/)
29
+ .pop()
30
+ .match(/^(\d{3})\s/)
31
+ ) {
32
+ return;
33
+ }
23
34
 
24
- error = function(err)
25
- {
26
- stream.emit('response', SMTPError('connection encountered an error', SMTPError.ERROR, err));
27
- },
35
+ const match = line ? line.match(/(\d+)\s?(.*)/) : null;
36
+ const data =
37
+ match !== null
38
+ ? { code: match[1], message: match[2], data: line }
39
+ : { code: -1, data: line };
28
40
 
29
- timedout = function(err)
30
- {
31
- stream.end();
32
- stream.emit('response', SMTPError('timedout while connecting to smtp server', SMTPError.TIMEDOUT, err));
33
- },
41
+ stream.emit('response', null, data);
42
+ buffer = '';
43
+ }
44
+ };
34
45
 
35
- watch = function(data)
36
- {
37
- //var data = stream.read();
38
- if (data !== null) {
39
- var decoded = data.toString();
40
- var emit = false;
41
- var code = 0;
46
+ /**
47
+ * @param {Error} err the error object
48
+ * @returns {void}
49
+ */
50
+ const error = err => {
51
+ stream.emit(
52
+ 'response',
53
+ SMTPError('connection encountered an error', SMTPError.ERROR, err)
54
+ );
55
+ };
42
56
 
43
- buffer += decoded;
44
- notify();
45
- }
46
- },
57
+ /**
58
+ * @param {Error} err the error object
59
+ * @returns {void}
60
+ */
61
+ const timedout = err => {
62
+ stream.end();
63
+ stream.emit(
64
+ 'response',
65
+ SMTPError(
66
+ 'timedout while connecting to smtp server',
67
+ SMTPError.TIMEDOUT,
68
+ err
69
+ )
70
+ );
71
+ };
47
72
 
48
- close = function(err)
49
- {
50
- stream.emit('response', SMTPError('connection has closed', SMTPError.CONNECTIONCLOSED, err));
51
- },
73
+ /**
74
+ * @param {string | Buffer} data the data
75
+ * @returns {void}
76
+ */
77
+ const watch = data => {
78
+ if (data !== null) {
79
+ buffer += data.toString();
80
+ notify();
81
+ }
82
+ };
52
83
 
53
- end = function(err)
54
- {
55
- stream.emit('response', SMTPError('connection has ended', SMTPError.CONNECTIONENDED, err));
56
- };
84
+ /**
85
+ * @param {Error} err the error object
86
+ * @returns {void}
87
+ */
88
+ const close = err => {
89
+ stream.emit(
90
+ 'response',
91
+ SMTPError('connection has closed', SMTPError.CONNECTIONCLOSED, err)
92
+ );
93
+ };
57
94
 
58
- this.stop = function(err) {
59
- stream.removeAllListeners('response');
60
- //stream.removeListener('readable', watch);
61
- stream.removeListener('data', watch);
62
- stream.removeListener('end', end);
63
- stream.removeListener('close', close);
64
- stream.removeListener('error', error);
95
+ /**
96
+ * @param {Error} err the error object
97
+ * @returns {void}
98
+ */
99
+ const end = err => {
100
+ stream.emit(
101
+ 'response',
102
+ SMTPError('connection has ended', SMTPError.CONNECTIONENDED, err)
103
+ );
104
+ };
65
105
 
66
- if(err && typeof(onerror) == "function")
67
- onerror(err);
68
- };
106
+ /**
107
+ * @param {Error} [err] the error object
108
+ * @returns {void}
109
+ */
110
+ this.stop = err => {
111
+ stream.removeAllListeners('response');
112
+ stream.removeListener('data', watch);
113
+ stream.removeListener('end', end);
114
+ stream.removeListener('close', close);
115
+ stream.removeListener('error', error);
69
116
 
70
- //stream.on('readable', watch);
71
- stream.on('data', watch);
72
- stream.on('end', end);
73
- stream.on('close', close);
74
- stream.on('error', error);
75
- stream.setTimeout(timeout, timedout);
76
- };
117
+ if (err != null && typeof onerror === 'function') {
118
+ onerror(err);
119
+ }
120
+ };
77
121
 
78
- exports.monitor = function(stream, timeout, onerror)
79
- {
80
- return new SMTPResponse(stream, timeout, onerror);
81
- };
122
+ stream.on('data', watch);
123
+ stream.on('end', end);
124
+ stream.on('close', close);
125
+ stream.on('error', error);
126
+ stream.setTimeout(timeout, timedout);
127
+ }
128
+ }
129
+
130
+ exports.SMTPResponse = SMTPResponse;
131
+
132
+ /**
133
+ * @param {Socket | TLSSocket} stream the open socket to stream a response from
134
+ * @param {number} timeout the time to wait (in milliseconds) before closing the socket
135
+ * @param {function(Error): void} onerror the function to call on error
136
+ * @returns {SMTPResponse} the smtp response
137
+ */
138
+ exports.monitor = (stream, timeout, onerror) =>
139
+ new SMTPResponse(stream, timeout, onerror);