emailjs 3.5.0 → 3.8.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/message.ts CHANGED
@@ -1,4 +1,11 @@
1
- import fs, { PathLike } from 'fs';
1
+ import {
2
+ PathLike,
3
+ existsSync,
4
+ open as openFile,
5
+ close as closeFile,
6
+ closeSync as closeFileSync,
7
+ read as readFile,
8
+ } from 'fs';
2
9
  import { hostname } from 'os';
3
10
  import { Stream } from 'stream';
4
11
  import type { Readable } from 'stream';
@@ -61,19 +68,20 @@ export interface MessageHeaders {
61
68
  | string
62
69
  | string[]
63
70
  | null
71
+ | undefined
64
72
  | MessageAttachment
65
73
  | MessageAttachment[];
66
- 'content-type': string;
67
- 'message-id': string;
68
- 'return-path': string | null;
69
- date: string;
74
+ 'content-type'?: string;
75
+ 'message-id'?: string;
76
+ 'return-path'?: string | null;
77
+ date?: string;
70
78
  from: string | string[];
71
79
  to: string | string[];
72
- cc: string | string[];
73
- bcc: string | string[];
80
+ cc?: string | string[];
81
+ bcc?: string | string[];
74
82
  subject: string;
75
83
  text: string | null;
76
- attachment: MessageAttachment | MessageAttachment[];
84
+ attachment?: MessageAttachment | MessageAttachment[];
77
85
  }
78
86
 
79
87
  let counter = 0;
@@ -186,16 +194,20 @@ export class Message {
186
194
 
187
195
  /**
188
196
  * @public
189
- * @param {function(isValid: boolean, invalidReason: string): void} callback .
190
- * @returns {void}
197
+ * @returns {{ isValid: boolean, validationError: (string | undefined) }} an object specifying whether this message is validly formatted, and the first validation error if it is not.
191
198
  */
192
- public valid(callback: (isValid: boolean, invalidReason?: string) => void) {
199
+ public checkValidity() {
193
200
  if (
194
201
  typeof this.header.from !== 'string' &&
195
202
  Array.isArray(this.header.from) === false
196
203
  ) {
197
- callback(false, 'Message must have a `from` header');
198
- } else if (
204
+ return {
205
+ isValid: false,
206
+ validationError: 'Message must have a `from` header',
207
+ };
208
+ }
209
+
210
+ if (
199
211
  typeof this.header.to !== 'string' &&
200
212
  Array.isArray(this.header.to) === false &&
201
213
  typeof this.header.cc !== 'string' &&
@@ -203,18 +215,19 @@ export class Message {
203
215
  typeof this.header.bcc !== 'string' &&
204
216
  Array.isArray(this.header.bcc) === false
205
217
  ) {
206
- callback(
207
- false,
208
- 'Message must have at least one `to`, `cc`, or `bcc` header'
209
- );
210
- } else if (this.attachments.length === 0) {
211
- callback(true, undefined);
212
- } else {
218
+ return {
219
+ isValid: false,
220
+ validationError:
221
+ 'Message must have at least one `to`, `cc`, or `bcc` header',
222
+ };
223
+ }
224
+
225
+ if (this.attachments.length > 0) {
213
226
  const failed: string[] = [];
214
227
 
215
228
  this.attachments.forEach((attachment) => {
216
229
  if (attachment.path) {
217
- if (fs.existsSync(attachment.path) == false) {
230
+ if (existsSync(attachment.path) === false) {
218
231
  failed.push(`${attachment.path} does not exist`);
219
232
  }
220
233
  } else if (attachment.stream) {
@@ -225,9 +238,24 @@ export class Message {
225
238
  failed.push('attachment has no data associated with it');
226
239
  }
227
240
  });
228
-
229
- callback(failed.length === 0, failed.join(', '));
241
+ return {
242
+ isValid: failed.length === 0,
243
+ validationError: failed.join(', '),
244
+ };
230
245
  }
246
+
247
+ return { isValid: true, validationError: undefined };
248
+ }
249
+
250
+ /**
251
+ * @public
252
+ * @deprecated does not conform to the `errback` style followed by the rest of the library, and will be removed in the next major version. use `checkValidity` instead.
253
+ * @param {function(isValid: boolean, invalidReason: (string | undefined)): void} callback .
254
+ * @returns {void}
255
+ */
256
+ public valid(callback: (isValid: boolean, invalidReason?: string) => void) {
257
+ const { isValid, validationError } = this.checkValidity();
258
+ callback(isValid, validationError);
231
259
  }
232
260
 
233
261
  /**
@@ -250,6 +278,18 @@ export class Message {
250
278
  str.on('end', (err) => callback(err, buffer));
251
279
  str.on('error', (err) => callback(err, buffer));
252
280
  }
281
+
282
+ public readAsync() {
283
+ return new Promise<string>((resolve, reject) => {
284
+ this.read((err, buffer) => {
285
+ if (err != null) {
286
+ reject(err);
287
+ } else {
288
+ resolve(buffer);
289
+ }
290
+ });
291
+ });
292
+ }
253
293
  }
254
294
 
255
295
  class MessageStream extends Stream {
@@ -379,7 +419,15 @@ class MessageStream extends Stream {
379
419
  ) => {
380
420
  const chunk = MIME64CHUNK * 16;
381
421
  const buffer = Buffer.alloc(chunk);
382
- const closed = (fd: number) => fs.closeSync(fd);
422
+
423
+ const inputEncoding =
424
+ attachment?.headers?.['content-transfer-encoding'] || 'base64';
425
+ const encoding =
426
+ inputEncoding === '7bit'
427
+ ? 'ascii'
428
+ : inputEncoding === '8bit'
429
+ ? 'binary'
430
+ : inputEncoding;
383
431
 
384
432
  /**
385
433
  * @param {Error} err the error to emit
@@ -387,47 +435,38 @@ class MessageStream extends Stream {
387
435
  * @returns {void}
388
436
  */
389
437
  const opened = (err: NodeJS.ErrnoException | null, fd: number) => {
390
- if (!err) {
391
- const read = (err: NodeJS.ErrnoException | null, bytes: number) => {
392
- if (!err && this.readable) {
393
- let encoding =
394
- attachment && attachment.headers
395
- ? attachment.headers['content-transfer-encoding'] || 'base64'
396
- : 'base64';
397
- if (encoding === 'ascii' || encoding === '7bit') {
398
- encoding = 'ascii';
399
- } else if (encoding === 'binary' || encoding === '8bit') {
400
- encoding = 'binary';
401
- } else {
402
- encoding = 'base64';
403
- }
404
- // guaranteed to be encoded without padding unless it is our last read
405
- outputBase64(buffer.toString(encoding, 0, bytes), () => {
406
- if (bytes == chunk) {
407
- // we read a full chunk, there might be more
408
- fs.read(fd, buffer, 0, chunk, null, read);
409
- } // that was the last chunk, we are done reading the file
410
- else {
411
- this.removeListener('error', closed);
412
- fs.close(fd, next);
413
- }
414
- });
415
- } else {
416
- this.emit(
417
- 'error',
418
- err || { message: 'message stream was interrupted somehow!' }
419
- );
420
- }
421
- };
422
-
423
- fs.read(fd, buffer, 0, chunk, null, read);
424
- this.once('error', closed);
425
- } else {
438
+ if (err) {
426
439
  this.emit('error', err);
440
+ return;
427
441
  }
442
+ const readBytes = (
443
+ err: NodeJS.ErrnoException | null,
444
+ bytes: number
445
+ ) => {
446
+ if (err || this.readable === false) {
447
+ this.emit(
448
+ 'error',
449
+ err || new Error('message stream was interrupted somehow!')
450
+ );
451
+ return;
452
+ }
453
+ // guaranteed to be encoded without padding unless it is our last read
454
+ outputBase64(buffer.toString(encoding, 0, bytes), () => {
455
+ if (bytes == chunk) {
456
+ // we read a full chunk, there might be more
457
+ readFile(fd, buffer, 0, chunk, null, readBytes);
458
+ } // that was the last chunk, we are done reading the file
459
+ else {
460
+ this.removeListener('error', closeFileSync);
461
+ closeFile(fd, next);
462
+ }
463
+ });
464
+ };
465
+ readFile(fd, buffer, 0, chunk, null, readBytes);
466
+ this.once('error', closeFileSync);
428
467
  };
429
468
 
430
- fs.open(attachment.path as PathLike, 'r', opened);
469
+ openFile(attachment.path as PathLike, 'r', opened);
431
470
  };
432
471
 
433
472
  /**
package/CHANGELOG.md DELETED
@@ -1,101 +0,0 @@
1
- # Changelog
2
- All notable changes to this project will be documented in this file.
3
-
4
- The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
- and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
-
7
- ## [3.5.0] - TBD
8
- ### Added
9
- - support `tsc --noPropertyAccessFromIndexSignature` [#290](https://github.com/eleith/emailjs/pull/290)
10
-
11
- ### Fixed
12
- - use `engines` field in `package.json` to signal node version support
13
-
14
- ## [3.4.0] - 2020-12-01
15
- ### Added
16
- - `SMTPClient#sendAsync` API [#267](https://github.com/eleith/emailjs/issues/267)
17
- - `isRFC2822Date` API
18
-
19
- ### Changed
20
- - use `WeakSet` instead of `WeakMap` for greylist tracking
21
-
22
- ### Fixed
23
- - use camelCase style for internal function names
24
- - use correct types in jsdoc comments
25
-
26
- ## [3.3.0] - 2020-08-08
27
- ### Added
28
- - greylist support [#202](https://github.com/eleith/emailjs/issues/202)
29
-
30
- ### Fixed
31
- - check socket is writable before sending [#205](https://github.com/eleith/emailjs/issues/205)
32
-
33
- ## [3.2.1] - 2020-06-27
34
- ### Fixed
35
- - use correct type for `MessageAttachment.stream` [#261](https://github.com/eleith/emailjs/issues/261)
36
- - add missing types in mime functions [#262](https://github.com/eleith/emailjs/pull/262)
37
-
38
- ## [3.2.0] - 2020-06-19
39
- ### Added
40
- - `addressparser` API (forked from dropped dependency) [#259](https://github.com/eleith/emailjs/issues/259)
41
- - `mimeEncode`/`mimeWordEncode` APIs (forked from dropped dependency) [#247](https://github.com/eleith/emailjs/issues/247)
42
-
43
- ### Changed
44
- - drop dependency on `addressparser` [#259](https://github.com/eleith/emailjs/issues/259)
45
- - drop dependency on `emailjs-mime-codec` [#247](https://github.com/eleith/emailjs/issues/247)
46
-
47
- ### Fixed
48
- - make `MessageAttachment` interface usable [#254](https://github.com/eleith/emailjs/issues/254)
49
- - mend regression in address type validation [#252](https://github.com/eleith/emailjs/pull/252)
50
-
51
- ## [3.1.0] - 2020-06-19 [YANKED]
52
-
53
- ## [3.0.0] - 2020-05-28
54
- ### Added
55
- - convert source to strict typescript, listed under the `types` field in `package.json`
56
- - support "dual-package" ESM + CJS via [conditional exports](https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_conditional_exports) & `rollup`-generated bundles
57
- - `SMTPClient#creatMessageStack` API [#229](https://github.com/eleith/emailjs/issues/229)
58
- - `SMTPError` API
59
-
60
- ### Changed
61
- - simplify public API [#249](https://github.com/eleith/emailjs/issues/249)
62
- - rename `Client` -> `SMTPClient` [#249](https://github.com/eleith/emailjs/issues/249)
63
- - rename `SMTPResponse` -> `SMTPResponseMonitor` [#249](https://github.com/eleith/emailjs/issues/249)
64
-
65
- ### Removed
66
- - `Message#attach_alternative` API
67
- - `makeSMTPError` API
68
-
69
- ### Fixed
70
- - filter duplicate message recipients [#242](https://github.com/eleith/emailjs/issues/242)
71
- - error when passing `password` without `user` [#199](https://github.com/eleith/emailjs/issues/199)
72
- - trim `host` before connecting [#136](https://github.com/eleith/emailjs/issues/136)
73
-
74
- ## [2.2.0] - 2018-07-06
75
- ### Added
76
- - expose rfc2822 date module
77
- - annotate code with typescript-compatible jsdoc tags
78
-
79
- ### Changed
80
- - drop dependency on `moment`
81
- - drop dependency on `starttls`
82
-
83
- ### Fixed
84
- - ensure timeout is set to default value [#225](https://github.com/eleith/emailjs/issues/225)
85
-
86
- ## [2.1.0] - 2018-06-09
87
- ### Added
88
- - expose error module
89
-
90
- ### Changed
91
- - handle errors with `fs.closeSync` instead of `fs.close`
92
- - refactor to ES2015+ constructs
93
- - lint & format with eslint + prettier
94
- - drop optional dependency on `bufferjs`
95
-
96
- ### Fixed
97
- - remove `new Buffer` calls
98
-
99
- ## [2.0.1] - 2018-02-11
100
- ### Added
101
- - a new changelog