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/.eslintrc.json +6 -5
- package/email.js +1 -0
- package/package.json +7 -9
- package/smtp/client.js +185 -54
- package/smtp/date.js +38 -0
- package/smtp/error.js +63 -2
- package/smtp/message.js +241 -48
- package/smtp/response.js +47 -1
- package/smtp/smtp.js +414 -184
- package/yarn.lock +1105 -0
package/smtp/message.js
CHANGED
|
@@ -1,17 +1,38 @@
|
|
|
1
|
-
const { Stream } = require('stream');
|
|
2
1
|
const fs = require('fs');
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const moment = require('moment');
|
|
6
|
-
const mimeWordEncode = require('emailjs-mime-codec').mimeWordEncode;
|
|
2
|
+
const { hostname } = require('os');
|
|
3
|
+
const { Stream } = require('stream');
|
|
7
4
|
const addressparser = require('addressparser');
|
|
5
|
+
const { mimeWordEncode } = require('emailjs-mime-codec');
|
|
6
|
+
const { getRFC2822Date } = require('./date');
|
|
7
|
+
|
|
8
8
|
const CRLF = '\r\n';
|
|
9
|
-
const MIMECHUNK = 76; // MIME standard wants 76 char chunks when sending out.
|
|
10
|
-
const MIME64CHUNK = MIMECHUNK * 6; // meets both base64 and mime divisibility
|
|
11
|
-
const BUFFERSIZE = MIMECHUNK * 24 * 7; // size of the message stream buffer
|
|
12
9
|
|
|
10
|
+
/**
|
|
11
|
+
* MIME standard wants 76 char chunks when sending out.
|
|
12
|
+
* @type {76}
|
|
13
|
+
*/
|
|
14
|
+
const MIMECHUNK = 76;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* meets both base64 and mime divisibility
|
|
18
|
+
* @type {456}
|
|
19
|
+
*/
|
|
20
|
+
const MIME64CHUNK = /** @type {456} */ (MIMECHUNK * 6);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* size of the message stream buffer
|
|
24
|
+
* @type {12768}
|
|
25
|
+
*/
|
|
26
|
+
const BUFFERSIZE = /** @type {12768} */ (MIMECHUNK * 24 * 7);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @type {number}
|
|
30
|
+
*/
|
|
13
31
|
let counter = 0;
|
|
14
32
|
|
|
33
|
+
/**
|
|
34
|
+
* @returns {string} the generated boundary
|
|
35
|
+
*/
|
|
15
36
|
function generate_boundary() {
|
|
16
37
|
let text = '';
|
|
17
38
|
const possible =
|
|
@@ -24,6 +45,10 @@ function generate_boundary() {
|
|
|
24
45
|
return text;
|
|
25
46
|
}
|
|
26
47
|
|
|
48
|
+
/**
|
|
49
|
+
* @param {string} l the person to parse into an address
|
|
50
|
+
* @returns {string} the parsed address
|
|
51
|
+
*/
|
|
27
52
|
function person2address(l) {
|
|
28
53
|
return addressparser(l)
|
|
29
54
|
.map(({ name, address }) => {
|
|
@@ -34,6 +59,10 @@ function person2address(l) {
|
|
|
34
59
|
.join(', ');
|
|
35
60
|
}
|
|
36
61
|
|
|
62
|
+
/**
|
|
63
|
+
* @param {string} header_name the header name to fix
|
|
64
|
+
* @returns {string} the fixed header name
|
|
65
|
+
*/
|
|
37
66
|
function fix_header_name_case(header_name) {
|
|
38
67
|
return header_name
|
|
39
68
|
.toLowerCase()
|
|
@@ -41,16 +70,26 @@ function fix_header_name_case(header_name) {
|
|
|
41
70
|
}
|
|
42
71
|
|
|
43
72
|
class Message {
|
|
73
|
+
/**
|
|
74
|
+
* @typedef {Object} MessageHeaders
|
|
75
|
+
* @property {string?} content-type
|
|
76
|
+
* @property {string} [subject]
|
|
77
|
+
* @property {string} [text]
|
|
78
|
+
* @property {MessageAttachment} [attachment]
|
|
79
|
+
* @param {MessageHeaders} headers hash of message headers
|
|
80
|
+
*/
|
|
44
81
|
constructor(headers) {
|
|
45
82
|
this.attachments = [];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @type {MessageAttachment}
|
|
86
|
+
*/
|
|
46
87
|
this.alternative = null;
|
|
47
88
|
this.header = {
|
|
48
89
|
'message-id': `<${new Date().getTime()}.${counter++}.${
|
|
49
90
|
process.pid
|
|
50
|
-
}@${
|
|
51
|
-
date:
|
|
52
|
-
.locale('en')
|
|
53
|
-
.format('ddd, DD MMM YYYY HH:mm:ss ZZ'),
|
|
91
|
+
}@${hostname()}>`,
|
|
92
|
+
date: getRFC2822Date(),
|
|
54
93
|
};
|
|
55
94
|
|
|
56
95
|
this.content = 'text/plain; charset=utf-8';
|
|
@@ -64,12 +103,13 @@ class Message {
|
|
|
64
103
|
header === 'attachment' &&
|
|
65
104
|
typeof headers[header] === 'object'
|
|
66
105
|
) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
106
|
+
const attachment = headers[header];
|
|
107
|
+
if (Array.isArray(attachment)) {
|
|
108
|
+
for (let i = 0; i < attachment.length; i++) {
|
|
109
|
+
this.attach(attachment[i]);
|
|
70
110
|
}
|
|
71
111
|
} else {
|
|
72
|
-
this.attach(
|
|
112
|
+
this.attach(attachment);
|
|
73
113
|
}
|
|
74
114
|
} else if (header === 'subject') {
|
|
75
115
|
this.header.subject = mimeWordEncode(headers.subject);
|
|
@@ -83,13 +123,21 @@ class Message {
|
|
|
83
123
|
}
|
|
84
124
|
}
|
|
85
125
|
|
|
126
|
+
/**
|
|
127
|
+
* @param {MessageAttachment} options attachment options
|
|
128
|
+
* @returns {Message} the current instance for chaining
|
|
129
|
+
*/
|
|
86
130
|
attach(options) {
|
|
87
131
|
/*
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (arguments.length > 1) {
|
|
92
|
-
options = {
|
|
132
|
+
legacy support, will remove eventually...
|
|
133
|
+
arguments -> (path, type, name, headers)
|
|
134
|
+
*/
|
|
135
|
+
if (typeof options === 'string' && arguments.length > 1) {
|
|
136
|
+
options = {
|
|
137
|
+
path: options,
|
|
138
|
+
type: arguments[1],
|
|
139
|
+
name: arguments[2],
|
|
140
|
+
};
|
|
93
141
|
}
|
|
94
142
|
|
|
95
143
|
// sender can specify an attachment as an alternative
|
|
@@ -105,10 +153,13 @@ class Message {
|
|
|
105
153
|
return this;
|
|
106
154
|
}
|
|
107
155
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
156
|
+
/**
|
|
157
|
+
* legacy support, will remove eventually...
|
|
158
|
+
* should use Message.attach() instead
|
|
159
|
+
* @param {string} html html data
|
|
160
|
+
* @param {string} [charset='utf-8'] the charset to encode as
|
|
161
|
+
* @returns {Message} the current Message instance
|
|
162
|
+
*/
|
|
112
163
|
attach_alternative(html, charset) {
|
|
113
164
|
this.alternative = {
|
|
114
165
|
data: html,
|
|
@@ -120,6 +171,10 @@ class Message {
|
|
|
120
171
|
return this;
|
|
121
172
|
}
|
|
122
173
|
|
|
174
|
+
/**
|
|
175
|
+
* @param {function(boolean, string): void} callback This callback is displayed as part of the Requester class.
|
|
176
|
+
* @returns {void}
|
|
177
|
+
*/
|
|
123
178
|
valid(callback) {
|
|
124
179
|
if (!this.header.from) {
|
|
125
180
|
callback(false, 'message does not have a valid sender');
|
|
@@ -128,14 +183,13 @@ class Message {
|
|
|
128
183
|
if (!(this.header.to || this.header.cc || this.header.bcc)) {
|
|
129
184
|
callback(false, 'message does not have a valid recipient');
|
|
130
185
|
} else if (this.attachments.length === 0) {
|
|
131
|
-
callback(true);
|
|
186
|
+
callback(true, undefined);
|
|
132
187
|
} else {
|
|
133
188
|
const failed = [];
|
|
134
189
|
|
|
135
190
|
this.attachments.forEach(attachment => {
|
|
136
191
|
if (attachment.path) {
|
|
137
|
-
|
|
138
|
-
if (!(fs.existsSync || path.existsSync)(attachment.path)) {
|
|
192
|
+
if (fs.existsSync(attachment.path) == false) {
|
|
139
193
|
failed.push(`${attachment.path} does not exist`);
|
|
140
194
|
}
|
|
141
195
|
} else if (attachment.stream) {
|
|
@@ -151,10 +205,18 @@ class Message {
|
|
|
151
205
|
}
|
|
152
206
|
}
|
|
153
207
|
|
|
208
|
+
/**
|
|
209
|
+
* returns a stream of the current message
|
|
210
|
+
* @returns {MessageStream} a stream of the current message
|
|
211
|
+
*/
|
|
154
212
|
stream() {
|
|
155
213
|
return new MessageStream(this);
|
|
156
214
|
}
|
|
157
215
|
|
|
216
|
+
/**
|
|
217
|
+
* @param {function(Error, string): void} callback the function to call with the error and buffer
|
|
218
|
+
* @returns {void}
|
|
219
|
+
*/
|
|
158
220
|
read(callback) {
|
|
159
221
|
let buffer = '';
|
|
160
222
|
const str = this.stream();
|
|
@@ -164,23 +226,71 @@ class Message {
|
|
|
164
226
|
}
|
|
165
227
|
}
|
|
166
228
|
|
|
229
|
+
/**
|
|
230
|
+
* @typedef {Object} MessageAttachmentHeaders
|
|
231
|
+
* @property {string} content-type
|
|
232
|
+
* @property {string} content-transfer-encoding
|
|
233
|
+
* @property {string} content-disposition
|
|
234
|
+
*/
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @typedef {Object} MessageAttachment
|
|
238
|
+
* @property {string} [name]
|
|
239
|
+
* @property {string} [type]
|
|
240
|
+
* @property {string} [charset]
|
|
241
|
+
* @property {string} [method]
|
|
242
|
+
* @property {string} [path]
|
|
243
|
+
* @property {NodeJS.ReadWriteStream} [stream]
|
|
244
|
+
* @property {boolean} [inline]
|
|
245
|
+
* @property {MessageAttachment} [alternative]
|
|
246
|
+
* @property {MessageAttachment[]} [related]
|
|
247
|
+
* @property {*} [encoded]
|
|
248
|
+
* @property {*} [data]
|
|
249
|
+
* @property {MessageAttachmentHeaders} [headers]
|
|
250
|
+
*/
|
|
251
|
+
|
|
167
252
|
class MessageStream extends Stream {
|
|
253
|
+
/**
|
|
254
|
+
* @param {Message} message the message to stream
|
|
255
|
+
*/
|
|
168
256
|
constructor(message) {
|
|
169
257
|
super();
|
|
170
258
|
|
|
259
|
+
/**
|
|
260
|
+
* @type {Message}
|
|
261
|
+
*/
|
|
171
262
|
this.message = message;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @type {boolean}
|
|
266
|
+
*/
|
|
172
267
|
this.readable = true;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @type {boolean}
|
|
271
|
+
*/
|
|
173
272
|
this.paused = false;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* @type {Buffer}
|
|
276
|
+
*/
|
|
174
277
|
this.buffer = Buffer.alloc(MIMECHUNK * 24 * 7);
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* @type {number}
|
|
281
|
+
*/
|
|
175
282
|
this.bufferIndex = 0;
|
|
176
283
|
|
|
284
|
+
/**
|
|
285
|
+
* @returns {void}
|
|
286
|
+
*/
|
|
177
287
|
const output_mixed = () => {
|
|
178
288
|
const boundary = generate_boundary();
|
|
179
289
|
output(
|
|
180
290
|
`Content-Type: multipart/mixed; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`
|
|
181
291
|
);
|
|
182
292
|
|
|
183
|
-
if (
|
|
293
|
+
if (this.message.alternative == null) {
|
|
184
294
|
output_text(this.message);
|
|
185
295
|
output_message(boundary, this.message.attachments, 0, close);
|
|
186
296
|
} else {
|
|
@@ -190,6 +300,13 @@ class MessageStream extends Stream {
|
|
|
190
300
|
}
|
|
191
301
|
};
|
|
192
302
|
|
|
303
|
+
/**
|
|
304
|
+
* @param {string} boundary the boundary text between outputs
|
|
305
|
+
* @param {MessageAttachment[]} list the list of potential messages to output
|
|
306
|
+
* @param {number} index the index of the list item to output
|
|
307
|
+
* @param {function(): void} callback the function to call if index is greater than upper bound
|
|
308
|
+
* @returns {void}
|
|
309
|
+
*/
|
|
193
310
|
const output_message = (boundary, list, index, callback) => {
|
|
194
311
|
if (index < list.length) {
|
|
195
312
|
output(`--${boundary}${CRLF}`);
|
|
@@ -208,6 +325,10 @@ class MessageStream extends Stream {
|
|
|
208
325
|
}
|
|
209
326
|
};
|
|
210
327
|
|
|
328
|
+
/**
|
|
329
|
+
* @param {MessageAttachment} attachment the metadata to use as headers
|
|
330
|
+
* @returns {void}
|
|
331
|
+
*/
|
|
211
332
|
const output_attachment_headers = attachment => {
|
|
212
333
|
let data = [];
|
|
213
334
|
const headers = {
|
|
@@ -221,8 +342,8 @@ class MessageStream extends Stream {
|
|
|
221
342
|
: `attachment; filename="${mimeWordEncode(attachment.name)}"`,
|
|
222
343
|
};
|
|
223
344
|
|
|
345
|
+
// allow sender to override default headers
|
|
224
346
|
for (const header in attachment.headers || {}) {
|
|
225
|
-
// allow sender to override default headers
|
|
226
347
|
headers[header.toLowerCase()] = attachment.headers[header];
|
|
227
348
|
}
|
|
228
349
|
|
|
@@ -238,6 +359,11 @@ class MessageStream extends Stream {
|
|
|
238
359
|
output(data.concat([CRLF]).join(''));
|
|
239
360
|
};
|
|
240
361
|
|
|
362
|
+
/**
|
|
363
|
+
* @param {MessageAttachment} attachment the metadata to use as headers
|
|
364
|
+
* @param {function(): void} callback the function to call after output is finished
|
|
365
|
+
* @returns {void}
|
|
366
|
+
*/
|
|
241
367
|
const output_attachment = (attachment, callback) => {
|
|
242
368
|
const build = attachment.path
|
|
243
369
|
? output_file
|
|
@@ -248,6 +374,11 @@ class MessageStream extends Stream {
|
|
|
248
374
|
build(attachment, callback);
|
|
249
375
|
};
|
|
250
376
|
|
|
377
|
+
/**
|
|
378
|
+
* @param {MessageAttachment} attachment the metadata to use as headers
|
|
379
|
+
* @param {function(): void} callback the function to call after output is finished
|
|
380
|
+
* @returns {void}
|
|
381
|
+
*/
|
|
251
382
|
const output_data = (attachment, callback) => {
|
|
252
383
|
output_base64(
|
|
253
384
|
attachment.encoded
|
|
@@ -257,15 +388,21 @@ class MessageStream extends Stream {
|
|
|
257
388
|
);
|
|
258
389
|
};
|
|
259
390
|
|
|
391
|
+
/**
|
|
392
|
+
* @param {MessageAttachment} attachment the metadata to use as headers
|
|
393
|
+
* @param {function(NodeJS.ErrnoException): void} next the function to call when the file is closed
|
|
394
|
+
* @returns {void}
|
|
395
|
+
*/
|
|
260
396
|
const output_file = (attachment, next) => {
|
|
261
397
|
const chunk = MIME64CHUNK * 16;
|
|
262
398
|
const buffer = Buffer.alloc(chunk);
|
|
263
|
-
const closed = fd =>
|
|
264
|
-
if (fs.closeSync) {
|
|
265
|
-
fs.closeSync(fd);
|
|
266
|
-
}
|
|
267
|
-
};
|
|
399
|
+
const closed = fd => fs.closeSync(fd);
|
|
268
400
|
|
|
401
|
+
/**
|
|
402
|
+
* @param {Error} err the error to emit
|
|
403
|
+
* @param {number} fd the file descriptor
|
|
404
|
+
* @returns {void}
|
|
405
|
+
*/
|
|
269
406
|
const opened = (err, fd) => {
|
|
270
407
|
if (!err) {
|
|
271
408
|
const read = (err, bytes) => {
|
|
@@ -310,35 +447,37 @@ class MessageStream extends Stream {
|
|
|
310
447
|
fs.open(attachment.path, 'r', opened);
|
|
311
448
|
};
|
|
312
449
|
|
|
450
|
+
/**
|
|
451
|
+
* @param {MessageAttachment} attachment the metadata to use as headers
|
|
452
|
+
* @param {function(): void} callback the function to call after output is finished
|
|
453
|
+
* @returns {void}
|
|
454
|
+
*/
|
|
313
455
|
const output_stream = (attachment, callback) => {
|
|
314
456
|
if (attachment.stream.readable) {
|
|
315
|
-
let previous =
|
|
457
|
+
let previous = Buffer.alloc(0);
|
|
316
458
|
|
|
317
459
|
attachment.stream.resume();
|
|
318
460
|
|
|
319
461
|
attachment.stream.on('end', () => {
|
|
320
|
-
output_base64(
|
|
321
|
-
(previous || Buffer.from(0)).toString('base64'),
|
|
322
|
-
callback
|
|
323
|
-
);
|
|
462
|
+
output_base64(previous.toString('base64'), callback);
|
|
324
463
|
this.removeListener('pause', attachment.stream.pause);
|
|
325
464
|
this.removeListener('resume', attachment.stream.resume);
|
|
326
465
|
this.removeListener('error', attachment.stream.resume);
|
|
327
466
|
});
|
|
328
467
|
|
|
329
|
-
attachment.stream.on('data',
|
|
468
|
+
attachment.stream.on('data', buff => {
|
|
330
469
|
// do we have bytes from a previous stream data event?
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
buffer =
|
|
335
|
-
buffer = buffer2;
|
|
470
|
+
let buffer = Buffer.isBuffer(buff) ? buff : Buffer.from(buff);
|
|
471
|
+
|
|
472
|
+
if (previous.byteLength > 0) {
|
|
473
|
+
buffer = Buffer.concat([previous, buffer]);
|
|
336
474
|
}
|
|
337
475
|
|
|
338
476
|
const padded = buffer.length % MIME64CHUNK;
|
|
477
|
+
previous = Buffer.alloc(padded);
|
|
478
|
+
|
|
339
479
|
// encode as much of the buffer to base64 without empty bytes
|
|
340
|
-
if (padded) {
|
|
341
|
-
previous = Buffer.alloc(padded);
|
|
480
|
+
if (padded > 0) {
|
|
342
481
|
// copy dangling bytes into previous buffer
|
|
343
482
|
buffer.copy(previous, 0, buffer.length - padded);
|
|
344
483
|
}
|
|
@@ -353,6 +492,11 @@ class MessageStream extends Stream {
|
|
|
353
492
|
}
|
|
354
493
|
};
|
|
355
494
|
|
|
495
|
+
/**
|
|
496
|
+
* @param {string} data the data to output as base64
|
|
497
|
+
* @param {function(): void} [callback] the function to call after output is finished
|
|
498
|
+
* @returns {void}
|
|
499
|
+
*/
|
|
356
500
|
const output_base64 = (data, callback) => {
|
|
357
501
|
const loops = Math.ceil(data.length / MIMECHUNK);
|
|
358
502
|
let loop = 0;
|
|
@@ -365,6 +509,10 @@ class MessageStream extends Stream {
|
|
|
365
509
|
}
|
|
366
510
|
};
|
|
367
511
|
|
|
512
|
+
/**
|
|
513
|
+
* @param {Message} message the message to output
|
|
514
|
+
* @returns {void}
|
|
515
|
+
*/
|
|
368
516
|
const output_text = message => {
|
|
369
517
|
let data = [];
|
|
370
518
|
|
|
@@ -381,6 +529,11 @@ class MessageStream extends Stream {
|
|
|
381
529
|
output(data.join(''));
|
|
382
530
|
};
|
|
383
531
|
|
|
532
|
+
/**
|
|
533
|
+
* @param {Message} message the message to output
|
|
534
|
+
* @param {function(): void} callback the function to call after output is finished
|
|
535
|
+
* @returns {void}
|
|
536
|
+
*/
|
|
384
537
|
const output_alternative = (message, callback) => {
|
|
385
538
|
const boundary = generate_boundary();
|
|
386
539
|
output(
|
|
@@ -389,6 +542,9 @@ class MessageStream extends Stream {
|
|
|
389
542
|
output_text(message);
|
|
390
543
|
output(`--${boundary}${CRLF}`);
|
|
391
544
|
|
|
545
|
+
/**
|
|
546
|
+
* @returns {void}
|
|
547
|
+
*/
|
|
392
548
|
const finish = () => {
|
|
393
549
|
output([CRLF, '--', boundary, '--', CRLF, CRLF].join(''));
|
|
394
550
|
callback();
|
|
@@ -401,6 +557,11 @@ class MessageStream extends Stream {
|
|
|
401
557
|
}
|
|
402
558
|
};
|
|
403
559
|
|
|
560
|
+
/**
|
|
561
|
+
* @param {MessageAttachment} message the message to output
|
|
562
|
+
* @param {function(): void} callback the function to call after output is finished
|
|
563
|
+
* @returns {void}
|
|
564
|
+
*/
|
|
404
565
|
const output_related = (message, callback) => {
|
|
405
566
|
const boundary = generate_boundary();
|
|
406
567
|
output(
|
|
@@ -414,6 +575,9 @@ class MessageStream extends Stream {
|
|
|
414
575
|
});
|
|
415
576
|
};
|
|
416
577
|
|
|
578
|
+
/**
|
|
579
|
+
* @returns {void}
|
|
580
|
+
*/
|
|
417
581
|
const output_header_data = () => {
|
|
418
582
|
if (this.message.attachments.length || this.message.alternative) {
|
|
419
583
|
output(`MIME-Version: 1.0${CRLF}`);
|
|
@@ -425,6 +589,9 @@ class MessageStream extends Stream {
|
|
|
425
589
|
}
|
|
426
590
|
};
|
|
427
591
|
|
|
592
|
+
/**
|
|
593
|
+
* @returns {void}
|
|
594
|
+
*/
|
|
428
595
|
const output_header = () => {
|
|
429
596
|
let data = [];
|
|
430
597
|
|
|
@@ -447,6 +614,12 @@ class MessageStream extends Stream {
|
|
|
447
614
|
output_header_data();
|
|
448
615
|
};
|
|
449
616
|
|
|
617
|
+
/**
|
|
618
|
+
* @param {string} data the data to output
|
|
619
|
+
* @param {function(...args): void} [callback] the function
|
|
620
|
+
* @param {*[]} [args] array of arguments to pass to the callback
|
|
621
|
+
* @returns {void}
|
|
622
|
+
*/
|
|
450
623
|
const output = (data, callback, args) => {
|
|
451
624
|
const bytes = Buffer.byteLength(data);
|
|
452
625
|
|
|
@@ -496,6 +669,10 @@ class MessageStream extends Stream {
|
|
|
496
669
|
}
|
|
497
670
|
};
|
|
498
671
|
|
|
672
|
+
/**
|
|
673
|
+
* @param {*} [err] the error to emit
|
|
674
|
+
* @returns {void}
|
|
675
|
+
*/
|
|
499
676
|
const close = err => {
|
|
500
677
|
if (err) {
|
|
501
678
|
this.emit('error', err);
|
|
@@ -517,16 +694,28 @@ class MessageStream extends Stream {
|
|
|
517
694
|
process.nextTick(output_header);
|
|
518
695
|
}
|
|
519
696
|
|
|
697
|
+
/**
|
|
698
|
+
* pause the stream
|
|
699
|
+
* @returns {void}
|
|
700
|
+
*/
|
|
520
701
|
pause() {
|
|
521
702
|
this.paused = true;
|
|
522
703
|
this.emit('pause');
|
|
523
704
|
}
|
|
524
705
|
|
|
706
|
+
/**
|
|
707
|
+
* resume the stream
|
|
708
|
+
* @returns {void}
|
|
709
|
+
*/
|
|
525
710
|
resume() {
|
|
526
711
|
this.paused = false;
|
|
527
712
|
this.emit('resume');
|
|
528
713
|
}
|
|
529
714
|
|
|
715
|
+
/**
|
|
716
|
+
* destroy the stream
|
|
717
|
+
* @returns {void}
|
|
718
|
+
*/
|
|
530
719
|
destroy() {
|
|
531
720
|
this.emit(
|
|
532
721
|
'destroy',
|
|
@@ -534,6 +723,10 @@ class MessageStream extends Stream {
|
|
|
534
723
|
);
|
|
535
724
|
}
|
|
536
725
|
|
|
726
|
+
/**
|
|
727
|
+
* destroy the stream at first opportunity
|
|
728
|
+
* @returns {void}
|
|
729
|
+
*/
|
|
537
730
|
destroySoon() {
|
|
538
731
|
this.emit('destroy');
|
|
539
732
|
}
|
package/smtp/response.js
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
const SMTPError = require('./error');
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import('net').Socket} Socket
|
|
5
|
+
* @typedef {import('tls').TLSSocket} TLSSocket
|
|
6
|
+
*/
|
|
7
|
+
|
|
3
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
|
+
*/
|
|
4
15
|
constructor(stream, timeout, onerror) {
|
|
5
16
|
let buffer = '';
|
|
6
17
|
|
|
18
|
+
/**
|
|
19
|
+
* @returns {void}
|
|
20
|
+
*/
|
|
7
21
|
const notify = () => {
|
|
8
22
|
if (buffer.length) {
|
|
9
23
|
// parse buffer for response codes
|
|
@@ -29,6 +43,10 @@ class SMTPResponse {
|
|
|
29
43
|
}
|
|
30
44
|
};
|
|
31
45
|
|
|
46
|
+
/**
|
|
47
|
+
* @param {Error} err the error object
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*/
|
|
32
50
|
const error = err => {
|
|
33
51
|
stream.emit(
|
|
34
52
|
'response',
|
|
@@ -36,6 +54,10 @@ class SMTPResponse {
|
|
|
36
54
|
);
|
|
37
55
|
};
|
|
38
56
|
|
|
57
|
+
/**
|
|
58
|
+
* @param {Error} err the error object
|
|
59
|
+
* @returns {void}
|
|
60
|
+
*/
|
|
39
61
|
const timedout = err => {
|
|
40
62
|
stream.end();
|
|
41
63
|
stream.emit(
|
|
@@ -48,6 +70,10 @@ class SMTPResponse {
|
|
|
48
70
|
);
|
|
49
71
|
};
|
|
50
72
|
|
|
73
|
+
/**
|
|
74
|
+
* @param {string | Buffer} data the data
|
|
75
|
+
* @returns {void}
|
|
76
|
+
*/
|
|
51
77
|
const watch = data => {
|
|
52
78
|
if (data !== null) {
|
|
53
79
|
buffer += data.toString();
|
|
@@ -55,6 +81,10 @@ class SMTPResponse {
|
|
|
55
81
|
}
|
|
56
82
|
};
|
|
57
83
|
|
|
84
|
+
/**
|
|
85
|
+
* @param {Error} err the error object
|
|
86
|
+
* @returns {void}
|
|
87
|
+
*/
|
|
58
88
|
const close = err => {
|
|
59
89
|
stream.emit(
|
|
60
90
|
'response',
|
|
@@ -62,6 +92,10 @@ class SMTPResponse {
|
|
|
62
92
|
);
|
|
63
93
|
};
|
|
64
94
|
|
|
95
|
+
/**
|
|
96
|
+
* @param {Error} err the error object
|
|
97
|
+
* @returns {void}
|
|
98
|
+
*/
|
|
65
99
|
const end = err => {
|
|
66
100
|
stream.emit(
|
|
67
101
|
'response',
|
|
@@ -69,6 +103,10 @@ class SMTPResponse {
|
|
|
69
103
|
);
|
|
70
104
|
};
|
|
71
105
|
|
|
106
|
+
/**
|
|
107
|
+
* @param {Error} [err] the error object
|
|
108
|
+
* @returns {void}
|
|
109
|
+
*/
|
|
72
110
|
this.stop = err => {
|
|
73
111
|
stream.removeAllListeners('response');
|
|
74
112
|
stream.removeListener('data', watch);
|
|
@@ -76,7 +114,7 @@ class SMTPResponse {
|
|
|
76
114
|
stream.removeListener('close', close);
|
|
77
115
|
stream.removeListener('error', error);
|
|
78
116
|
|
|
79
|
-
if (err && typeof onerror === 'function') {
|
|
117
|
+
if (err != null && typeof onerror === 'function') {
|
|
80
118
|
onerror(err);
|
|
81
119
|
}
|
|
82
120
|
};
|
|
@@ -89,5 +127,13 @@ class SMTPResponse {
|
|
|
89
127
|
}
|
|
90
128
|
}
|
|
91
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
|
+
*/
|
|
92
138
|
exports.monitor = (stream, timeout, onerror) =>
|
|
93
139
|
new SMTPResponse(stream, timeout, onerror);
|