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/.editorconfig +10 -0
- package/.eslintrc.json +40 -0
- package/CHANGELOG.md +9 -0
- package/email.js +2 -0
- package/package.json +21 -16
- package/smtp/client.js +323 -206
- package/smtp/date.js +38 -0
- package/smtp/error.js +73 -7
- package/smtp/message.js +728 -619
- package/smtp/response.js +126 -68
- package/smtp/smtp.js +852 -569
- package/yarn.lock +1105 -0
package/smtp/response.js
CHANGED
|
@@ -1,81 +1,139 @@
|
|
|
1
|
-
|
|
1
|
+
const SMTPError = require('./error');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
{
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import('net').Socket} Socket
|
|
5
|
+
* @typedef {import('tls').TLSSocket} TLSSocket
|
|
6
|
+
*/
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
20
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
},
|
|
41
|
+
stream.emit('response', null, data);
|
|
42
|
+
buffer = '';
|
|
43
|
+
}
|
|
44
|
+
};
|
|
34
45
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
44
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
67
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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);
|