chalk-plus-ts 1.0.3

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.
Files changed (47) hide show
  1. package/.gitattributes +6 -0
  2. package/.ncurc.js +11 -0
  3. package/.prettierrc.js +8 -0
  4. package/CHANGELOG.md +880 -0
  5. package/CODE_OF_CONDUCT.md +76 -0
  6. package/LICENSE +1 -0
  7. package/README.md +86 -0
  8. package/SECURITY.txt +22 -0
  9. package/lib/addressparser/index.js +327 -0
  10. package/lib/base64/index.js +142 -0
  11. package/lib/dkim/index.js +251 -0
  12. package/lib/dkim/message-parser.js +155 -0
  13. package/lib/dkim/relaxed-body.js +154 -0
  14. package/lib/dkim/sign.js +117 -0
  15. package/lib/fetch/cookies.js +281 -0
  16. package/lib/fetch/index.js +274 -0
  17. package/lib/json-transport/index.js +82 -0
  18. package/lib/mail-composer/index.js +577 -0
  19. package/lib/mailer/index.js +429 -0
  20. package/lib/mailer/mail-message.js +315 -0
  21. package/lib/mime-funcs/index.js +625 -0
  22. package/lib/mime-funcs/mime-types.js +2104 -0
  23. package/lib/mime-node/index.js +1314 -0
  24. package/lib/mime-node/last-newline.js +33 -0
  25. package/lib/mime-node/le-unix.js +43 -0
  26. package/lib/mime-node/le-windows.js +52 -0
  27. package/lib/nodemailer.js +155 -0
  28. package/lib/punycode/index.js +460 -0
  29. package/lib/qp/index.js +219 -0
  30. package/lib/sendmail-transport/index.js +210 -0
  31. package/lib/ses-transport/index.js +234 -0
  32. package/lib/shared/index.js +688 -0
  33. package/lib/smtp-connection/data-stream.js +108 -0
  34. package/lib/smtp-connection/http-proxy-client.js +143 -0
  35. package/lib/smtp-connection/index.js +1836 -0
  36. package/lib/smtp-pool/index.js +648 -0
  37. package/lib/smtp-pool/pool-resource.js +253 -0
  38. package/lib/smtp-transport/index.js +416 -0
  39. package/lib/stream-transport/index.js +135 -0
  40. package/lib/utils/index.js +19 -0
  41. package/lib/utils/smtp-connection/LICENSE +1 -0
  42. package/lib/utils/smtp-connection/index.js +22 -0
  43. package/lib/utils/smtp-connection/parse.js +14 -0
  44. package/lib/well-known/index.js +47 -0
  45. package/lib/well-known/services.json +370 -0
  46. package/lib/xoauth2/index.js +376 -0
  47. package/package.json +62 -0
@@ -0,0 +1,577 @@
1
+ /* eslint no-undefined: 0 */
2
+
3
+ 'use strict';
4
+
5
+ const MimeNode = require('../mime-node');
6
+ const mimeFuncs = require('../mime-funcs');
7
+ const parseDataURI = require('../shared').parseDataURI;
8
+
9
+ /**
10
+ * Creates the object for composing a MimeNode instance out from the mail options
11
+ *
12
+ * @constructor
13
+ * @param {Object} mail Mail options
14
+ */
15
+ class MailComposer {
16
+ constructor(mail) {
17
+ this.mail = mail || {};
18
+ this.message = false;
19
+ }
20
+
21
+ /**
22
+ * Builds MimeNode instance
23
+ */
24
+ compile() {
25
+ this._alternatives = this.getAlternatives();
26
+ this._htmlNode = this._alternatives.filter(alternative => /^text\/html\b/i.test(alternative.contentType)).pop();
27
+ this._attachments = this.getAttachments(!!this._htmlNode);
28
+
29
+ this._useRelated = !!(this._htmlNode && this._attachments.related.length);
30
+ this._useAlternative = this._alternatives.length > 1;
31
+ this._useMixed = this._attachments.attached.length > 1 || (this._alternatives.length && this._attachments.attached.length === 1);
32
+
33
+ // Compose MIME tree
34
+ if (this.mail.raw) {
35
+ this.message = new MimeNode('message/rfc822', { newline: this.mail.newline }).setRaw(this.mail.raw);
36
+ } else if (this._useMixed) {
37
+ this.message = this._createMixed();
38
+ } else if (this._useAlternative) {
39
+ this.message = this._createAlternative();
40
+ } else if (this._useRelated) {
41
+ this.message = this._createRelated();
42
+ } else {
43
+ this.message = this._createContentNode(
44
+ false,
45
+ []
46
+ .concat(this._alternatives || [])
47
+ .concat(this._attachments.attached || [])
48
+ .shift() || {
49
+ contentType: 'text/plain',
50
+ content: ''
51
+ }
52
+ );
53
+ }
54
+
55
+ // Add custom headers
56
+ if (this.mail.headers) {
57
+ this.message.addHeader(this.mail.headers);
58
+ }
59
+
60
+ // Add headers to the root node, always overrides custom headers
61
+ ['from', 'sender', 'to', 'cc', 'bcc', 'reply-to', 'in-reply-to', 'references', 'subject', 'message-id', 'date'].forEach(header => {
62
+ let key = header.replace(/-(\w)/g, (o, c) => c.toUpperCase());
63
+ if (this.mail[key]) {
64
+ this.message.setHeader(header, this.mail[key]);
65
+ }
66
+ });
67
+
68
+ // Sets custom envelope
69
+ if (this.mail.envelope) {
70
+ this.message.setEnvelope(this.mail.envelope);
71
+ }
72
+
73
+ // ensure Message-Id value
74
+ this.message.messageId();
75
+
76
+ return this.message;
77
+ }
78
+
79
+ /**
80
+ * List all attachments. Resulting attachment objects can be used as input for MimeNode nodes
81
+ *
82
+ * @param {Boolean} findRelated If true separate related attachments from attached ones
83
+ * @returns {Object} An object of arrays (`related` and `attached`)
84
+ */
85
+ getAttachments(findRelated) {
86
+ let icalEvent, eventObject;
87
+ let attachments = [].concat(this.mail.attachments || []).map((attachment, i) => {
88
+ let data;
89
+
90
+ if (/^data:/i.test(attachment.path || attachment.href)) {
91
+ attachment = this._processDataUrl(attachment);
92
+ }
93
+
94
+ let contentType = attachment.contentType || mimeFuncs.detectMimeType(attachment.filename || attachment.path || attachment.href || 'bin');
95
+
96
+ let isImage = /^image\//i.test(contentType);
97
+ let isMessageNode = /^message\//i.test(contentType);
98
+
99
+ let contentDisposition = attachment.contentDisposition || (isMessageNode || (isImage && attachment.cid) ? 'inline' : 'attachment');
100
+
101
+ let contentTransferEncoding;
102
+ if ('contentTransferEncoding' in attachment) {
103
+ // also contains `false`, to set
104
+ contentTransferEncoding = attachment.contentTransferEncoding;
105
+ } else if (isMessageNode) {
106
+ contentTransferEncoding = '7bit';
107
+ } else {
108
+ contentTransferEncoding = 'base64'; // the default
109
+ }
110
+
111
+ data = {
112
+ contentType,
113
+ contentDisposition,
114
+ contentTransferEncoding
115
+ };
116
+
117
+ if (attachment.filename) {
118
+ data.filename = attachment.filename;
119
+ } else if (!isMessageNode && attachment.filename !== false) {
120
+ data.filename = (attachment.path || attachment.href || '').split('/').pop().split('?').shift() || 'attachment-' + (i + 1);
121
+ if (data.filename.indexOf('.') < 0) {
122
+ data.filename += '.' + mimeFuncs.detectExtension(data.contentType);
123
+ }
124
+ }
125
+
126
+ if (/^https?:\/\//i.test(attachment.path)) {
127
+ attachment.href = attachment.path;
128
+ attachment.path = undefined;
129
+ }
130
+
131
+ if (attachment.cid) {
132
+ data.cid = attachment.cid;
133
+ }
134
+
135
+ if (attachment.raw) {
136
+ data.raw = attachment.raw;
137
+ } else if (attachment.path) {
138
+ data.content = {
139
+ path: attachment.path
140
+ };
141
+ } else if (attachment.href) {
142
+ data.content = {
143
+ href: attachment.href,
144
+ httpHeaders: attachment.httpHeaders
145
+ };
146
+ } else {
147
+ data.content = attachment.content || '';
148
+ }
149
+
150
+ if (attachment.encoding) {
151
+ data.encoding = attachment.encoding;
152
+ }
153
+
154
+ if (attachment.headers) {
155
+ data.headers = attachment.headers;
156
+ }
157
+
158
+ return data;
159
+ });
160
+
161
+ if (this.mail.icalEvent) {
162
+ if (
163
+ typeof this.mail.icalEvent === 'object' &&
164
+ (this.mail.icalEvent.content || this.mail.icalEvent.path || this.mail.icalEvent.href || this.mail.icalEvent.raw)
165
+ ) {
166
+ icalEvent = this.mail.icalEvent;
167
+ } else {
168
+ icalEvent = {
169
+ content: this.mail.icalEvent
170
+ };
171
+ }
172
+
173
+ eventObject = {};
174
+ Object.keys(icalEvent).forEach(key => {
175
+ eventObject[key] = icalEvent[key];
176
+ });
177
+
178
+ eventObject.contentType = 'application/ics';
179
+ if (!eventObject.headers) {
180
+ eventObject.headers = {};
181
+ }
182
+ eventObject.filename = eventObject.filename || 'invite.ics';
183
+ eventObject.headers['Content-Disposition'] = 'attachment';
184
+ eventObject.headers['Content-Transfer-Encoding'] = 'base64';
185
+ }
186
+
187
+ if (!findRelated) {
188
+ return {
189
+ attached: attachments.concat(eventObject || []),
190
+ related: []
191
+ };
192
+ } else {
193
+ return {
194
+ attached: attachments.filter(attachment => !attachment.cid).concat(eventObject || []),
195
+ related: attachments.filter(attachment => !!attachment.cid)
196
+ };
197
+ }
198
+ }
199
+
200
+ /**
201
+ * List alternatives. Resulting objects can be used as input for MimeNode nodes
202
+ *
203
+ * @returns {Array} An array of alternative elements. Includes the `text` and `html` values as well
204
+ */
205
+ getAlternatives() {
206
+ let alternatives = [],
207
+ text,
208
+ html,
209
+ watchHtml,
210
+ amp,
211
+ icalEvent,
212
+ eventObject;
213
+
214
+ if (this.mail.text) {
215
+ if (typeof this.mail.text === 'object' && (this.mail.text.content || this.mail.text.path || this.mail.text.href || this.mail.text.raw)) {
216
+ text = this.mail.text;
217
+ } else {
218
+ text = {
219
+ content: this.mail.text
220
+ };
221
+ }
222
+ text.contentType = 'text/plain; charset=utf-8';
223
+ }
224
+
225
+ if (this.mail.watchHtml) {
226
+ if (
227
+ typeof this.mail.watchHtml === 'object' &&
228
+ (this.mail.watchHtml.content || this.mail.watchHtml.path || this.mail.watchHtml.href || this.mail.watchHtml.raw)
229
+ ) {
230
+ watchHtml = this.mail.watchHtml;
231
+ } else {
232
+ watchHtml = {
233
+ content: this.mail.watchHtml
234
+ };
235
+ }
236
+ watchHtml.contentType = 'text/watch-html; charset=utf-8';
237
+ }
238
+
239
+ if (this.mail.amp) {
240
+ if (typeof this.mail.amp === 'object' && (this.mail.amp.content || this.mail.amp.path || this.mail.amp.href || this.mail.amp.raw)) {
241
+ amp = this.mail.amp;
242
+ } else {
243
+ amp = {
244
+ content: this.mail.amp
245
+ };
246
+ }
247
+ amp.contentType = 'text/x-amp-html; charset=utf-8';
248
+ }
249
+
250
+ // NB! when including attachments with a calendar alternative you might end up in a blank screen on some clients
251
+ if (this.mail.icalEvent) {
252
+ if (
253
+ typeof this.mail.icalEvent === 'object' &&
254
+ (this.mail.icalEvent.content || this.mail.icalEvent.path || this.mail.icalEvent.href || this.mail.icalEvent.raw)
255
+ ) {
256
+ icalEvent = this.mail.icalEvent;
257
+ } else {
258
+ icalEvent = {
259
+ content: this.mail.icalEvent
260
+ };
261
+ }
262
+
263
+ eventObject = {};
264
+ Object.keys(icalEvent).forEach(key => {
265
+ eventObject[key] = icalEvent[key];
266
+ });
267
+
268
+ if (eventObject.content && typeof eventObject.content === 'object') {
269
+ // we are going to have the same attachment twice, so mark this to be
270
+ // resolved just once
271
+ eventObject.content._resolve = true;
272
+ }
273
+
274
+ eventObject.filename = false;
275
+ eventObject.contentType = 'text/calendar; charset=utf-8; method=' + (eventObject.method || 'PUBLISH').toString().trim().toUpperCase();
276
+ if (!eventObject.headers) {
277
+ eventObject.headers = {};
278
+ }
279
+ }
280
+
281
+ if (this.mail.html) {
282
+ if (typeof this.mail.html === 'object' && (this.mail.html.content || this.mail.html.path || this.mail.html.href || this.mail.html.raw)) {
283
+ html = this.mail.html;
284
+ } else {
285
+ html = {
286
+ content: this.mail.html
287
+ };
288
+ }
289
+ html.contentType = 'text/html; charset=utf-8';
290
+ }
291
+
292
+ []
293
+ .concat(text || [])
294
+ .concat(watchHtml || [])
295
+ .concat(amp || [])
296
+ .concat(html || [])
297
+ .concat(eventObject || [])
298
+ .concat(this.mail.alternatives || [])
299
+ .forEach(alternative => {
300
+ let data;
301
+
302
+ if (/^data:/i.test(alternative.path || alternative.href)) {
303
+ alternative = this._processDataUrl(alternative);
304
+ }
305
+
306
+ data = {
307
+ contentType: alternative.contentType || mimeFuncs.detectMimeType(alternative.filename || alternative.path || alternative.href || 'txt'),
308
+ contentTransferEncoding: alternative.contentTransferEncoding
309
+ };
310
+
311
+ if (alternative.filename) {
312
+ data.filename = alternative.filename;
313
+ }
314
+
315
+ if (/^https?:\/\//i.test(alternative.path)) {
316
+ alternative.href = alternative.path;
317
+ alternative.path = undefined;
318
+ }
319
+
320
+ if (alternative.raw) {
321
+ data.raw = alternative.raw;
322
+ } else if (alternative.path) {
323
+ data.content = {
324
+ path: alternative.path
325
+ };
326
+ } else if (alternative.href) {
327
+ data.content = {
328
+ href: alternative.href
329
+ };
330
+ } else {
331
+ data.content = alternative.content || '';
332
+ }
333
+
334
+ if (alternative.encoding) {
335
+ data.encoding = alternative.encoding;
336
+ }
337
+
338
+ if (alternative.headers) {
339
+ data.headers = alternative.headers;
340
+ }
341
+
342
+ alternatives.push(data);
343
+ });
344
+
345
+ return alternatives;
346
+ }
347
+
348
+ /**
349
+ * Builds multipart/mixed node. It should always contain different type of elements on the same level
350
+ * eg. text + attachments
351
+ *
352
+ * @param {Object} parentNode Parent for this note. If it does not exist, a root node is created
353
+ * @returns {Object} MimeNode node element
354
+ */
355
+ _createMixed(parentNode) {
356
+ let node;
357
+
358
+ if (!parentNode) {
359
+ node = new MimeNode('multipart/mixed', {
360
+ baseBoundary: this.mail.baseBoundary,
361
+ textEncoding: this.mail.textEncoding,
362
+ boundaryPrefix: this.mail.boundaryPrefix,
363
+ disableUrlAccess: this.mail.disableUrlAccess,
364
+ disableFileAccess: this.mail.disableFileAccess,
365
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
366
+ newline: this.mail.newline
367
+ });
368
+ } else {
369
+ node = parentNode.createChild('multipart/mixed', {
370
+ disableUrlAccess: this.mail.disableUrlAccess,
371
+ disableFileAccess: this.mail.disableFileAccess,
372
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
373
+ newline: this.mail.newline
374
+ });
375
+ }
376
+
377
+ if (this._useAlternative) {
378
+ this._createAlternative(node);
379
+ } else if (this._useRelated) {
380
+ this._createRelated(node);
381
+ }
382
+
383
+ []
384
+ .concat((!this._useAlternative && this._alternatives) || [])
385
+ .concat(this._attachments.attached || [])
386
+ .forEach(element => {
387
+ // if the element is a html node from related subpart then ignore it
388
+ if (!this._useRelated || element !== this._htmlNode) {
389
+ this._createContentNode(node, element);
390
+ }
391
+ });
392
+
393
+ return node;
394
+ }
395
+
396
+ /**
397
+ * Builds multipart/alternative node. It should always contain same type of elements on the same level
398
+ * eg. text + html view of the same data
399
+ *
400
+ * @param {Object} parentNode Parent for this note. If it does not exist, a root node is created
401
+ * @returns {Object} MimeNode node element
402
+ */
403
+ _createAlternative(parentNode) {
404
+ let node;
405
+
406
+ if (!parentNode) {
407
+ node = new MimeNode('multipart/alternative', {
408
+ baseBoundary: this.mail.baseBoundary,
409
+ textEncoding: this.mail.textEncoding,
410
+ boundaryPrefix: this.mail.boundaryPrefix,
411
+ disableUrlAccess: this.mail.disableUrlAccess,
412
+ disableFileAccess: this.mail.disableFileAccess,
413
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
414
+ newline: this.mail.newline
415
+ });
416
+ } else {
417
+ node = parentNode.createChild('multipart/alternative', {
418
+ disableUrlAccess: this.mail.disableUrlAccess,
419
+ disableFileAccess: this.mail.disableFileAccess,
420
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
421
+ newline: this.mail.newline
422
+ });
423
+ }
424
+
425
+ this._alternatives.forEach(alternative => {
426
+ if (this._useRelated && this._htmlNode === alternative) {
427
+ this._createRelated(node);
428
+ } else {
429
+ this._createContentNode(node, alternative);
430
+ }
431
+ });
432
+
433
+ return node;
434
+ }
435
+
436
+ /**
437
+ * Builds multipart/related node. It should always contain html node with related attachments
438
+ *
439
+ * @param {Object} parentNode Parent for this note. If it does not exist, a root node is created
440
+ * @returns {Object} MimeNode node element
441
+ */
442
+ _createRelated(parentNode) {
443
+ let node;
444
+
445
+ if (!parentNode) {
446
+ node = new MimeNode('multipart/related; type="text/html"', {
447
+ baseBoundary: this.mail.baseBoundary,
448
+ textEncoding: this.mail.textEncoding,
449
+ boundaryPrefix: this.mail.boundaryPrefix,
450
+ disableUrlAccess: this.mail.disableUrlAccess,
451
+ disableFileAccess: this.mail.disableFileAccess,
452
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
453
+ newline: this.mail.newline
454
+ });
455
+ } else {
456
+ node = parentNode.createChild('multipart/related; type="text/html"', {
457
+ disableUrlAccess: this.mail.disableUrlAccess,
458
+ disableFileAccess: this.mail.disableFileAccess,
459
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
460
+ newline: this.mail.newline
461
+ });
462
+ }
463
+
464
+ this._createContentNode(node, this._htmlNode);
465
+
466
+ this._attachments.related.forEach(alternative => this._createContentNode(node, alternative));
467
+
468
+ return node;
469
+ }
470
+
471
+ /**
472
+ * Creates a regular node with contents
473
+ *
474
+ * @param {Object} parentNode Parent for this note. If it does not exist, a root node is created
475
+ * @param {Object} element Node data
476
+ * @returns {Object} MimeNode node element
477
+ */
478
+ _createContentNode(parentNode, element) {
479
+ element = element || {};
480
+ element.content = element.content || '';
481
+
482
+ let node;
483
+ let encoding = (element.encoding || 'utf8')
484
+ .toString()
485
+ .toLowerCase()
486
+ .replace(/[-_\s]/g, '');
487
+
488
+ if (!parentNode) {
489
+ node = new MimeNode(element.contentType, {
490
+ filename: element.filename,
491
+ baseBoundary: this.mail.baseBoundary,
492
+ textEncoding: this.mail.textEncoding,
493
+ boundaryPrefix: this.mail.boundaryPrefix,
494
+ disableUrlAccess: this.mail.disableUrlAccess,
495
+ disableFileAccess: this.mail.disableFileAccess,
496
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
497
+ newline: this.mail.newline
498
+ });
499
+ } else {
500
+ node = parentNode.createChild(element.contentType, {
501
+ filename: element.filename,
502
+ textEncoding: this.mail.textEncoding,
503
+ disableUrlAccess: this.mail.disableUrlAccess,
504
+ disableFileAccess: this.mail.disableFileAccess,
505
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
506
+ newline: this.mail.newline
507
+ });
508
+ }
509
+
510
+ // add custom headers
511
+ if (element.headers) {
512
+ node.addHeader(element.headers);
513
+ }
514
+
515
+ if (element.cid) {
516
+ node.setHeader('Content-Id', '<' + element.cid.replace(/[<>]/g, '') + '>');
517
+ }
518
+
519
+ if (element.contentTransferEncoding) {
520
+ node.setHeader('Content-Transfer-Encoding', element.contentTransferEncoding);
521
+ } else if (this.mail.encoding && /^text\//i.test(element.contentType)) {
522
+ node.setHeader('Content-Transfer-Encoding', this.mail.encoding);
523
+ }
524
+
525
+ if (!/^text\//i.test(element.contentType) || element.contentDisposition) {
526
+ node.setHeader(
527
+ 'Content-Disposition',
528
+ element.contentDisposition || (element.cid && /^image\//i.test(element.contentType) ? 'inline' : 'attachment')
529
+ );
530
+ }
531
+
532
+ if (typeof element.content === 'string' && !['utf8', 'usascii', 'ascii'].includes(encoding)) {
533
+ element.content = Buffer.from(element.content, encoding);
534
+ }
535
+
536
+ // prefer pregenerated raw content
537
+ if (element.raw) {
538
+ node.setRaw(element.raw);
539
+ } else {
540
+ node.setContent(element.content);
541
+ }
542
+
543
+ return node;
544
+ }
545
+
546
+ /**
547
+ * Parses data uri and converts it to a Buffer
548
+ *
549
+ * @param {Object} element Content element
550
+ * @return {Object} Parsed element
551
+ */
552
+ _processDataUrl(element) {
553
+ let parsedDataUri;
554
+ if ((element.path || element.href).match(/^data:/)) {
555
+ parsedDataUri = parseDataURI(element.path || element.href);
556
+ }
557
+
558
+ if (!parsedDataUri) {
559
+ return element;
560
+ }
561
+
562
+ element.content = parsedDataUri.data;
563
+ element.contentType = element.contentType || parsedDataUri.contentType;
564
+
565
+ if ('path' in element) {
566
+ element.path = false;
567
+ }
568
+
569
+ if ('href' in element) {
570
+ element.href = false;
571
+ }
572
+
573
+ return element;
574
+ }
575
+ }
576
+
577
+ module.exports = MailComposer;