-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.cjs
1285 lines (1126 loc) · 42.5 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const josk = require('josk');
const merge = require('deepmerge');
const crypto = require('crypto');
const debug = (isDebug, ...args) => {
if (isDebug) {
console.info.call(console, '[DEBUG] [mail-time]', `${new Date}`, ...args);
}
};
const logError = (...args) => {
console.error.call(console, '[ERROR] [mail-time]', `${new Date}`, ...args);
};
/**
* Ensure (create) index on MongoDB collection, catch and log exception if thrown
* @function ensureIndex
* @param {Collection} collection - Mongo's driver Collection instance
* @param {object} keys - Field and value pairs where the field is the index key and the value describes the type of index for that field
* @param {object} opts - Set of options that controls the creation of the index
* @returns {void 0}
*/
const ensureIndex = async (collection, keys, opts) => {
try {
await collection.createIndex(keys, opts);
} catch (e) {
if (e.code === 85) {
let indexName;
const indexes = await collection.indexes();
for (const index of indexes) {
let drop = true;
for (const indexKey of Object.keys(keys)) {
if (typeof index.key[indexKey] === 'undefined') {
drop = false;
break;
}
}
for (const indexKey of Object.keys(index.key)) {
if (typeof keys[indexKey] === 'undefined') {
drop = false;
break;
}
}
if (drop) {
indexName = index.name;
break;
}
}
if (indexName) {
await collection.dropIndex(indexName);
await collection.createIndex(keys, opts);
}
} else {
logError(`[ensureIndex] Can not set ${Object.keys(keys).join(' + ')} index on "${collection._name || 'MongoDB'}" collection`, { keys, opts, details: e });
}
}
};
/** Class representing MongoDB Queue for MailTime */
class MongoQueue {
/**
* Create a MongoQueue instance
* @param {object} opts - configuration object
* @param {Db} opts.db - Required, Mongo's `Db` instance, like one returned from `MongoClient#db()` method
* @param {string} [opts.prefix] - Optional prefix for scope isolation; use when creating multiple MailTime instances within the single application
*/
constructor (opts) {
this.name = 'mongo-queue';
if (!opts || typeof opts !== 'object' || opts === null) {
throw new TypeError('[mail-time] Configuration object must be passed into MongoQueue constructor');
}
if (!opts.db) {
throw new Error('[mail-time] [MongoQueue] requires MongoDB database {db} option, like returned from `MongoClient.connect`');
}
this.prefix = (typeof opts.prefix === 'string') ? opts.prefix : '';
this.db = opts.db;
this.collection = opts.db.collection(`__mailTimeQueue__${this.prefix}`);
ensureIndex(this.collection, { uuid: 1 }, { background: false });
ensureIndex(this.collection, { isSent: 1, isFailed: 1, isCancelled: 1, to: 1, sendAt: 1 }, { background: false });
ensureIndex(this.collection, { isSent: 1, isFailed: 1, isCancelled: 1, sendAt: 1, tries: 1 }, { background: false });
// MongoDB Collection Schema:
// _id
// uuid {string}
// to {string|[string]}
// tries {number} - qty of send attempts
// sendAt {number} - When letter should be sent
// isSent {boolean} - Email status
// isCancelled {boolean} - `true` if email was cancelled before it was sent
// isFailed {boolean} - `true` if email has failed to send
// template {string} - Template for this email
// transport {number} - Last used transport
// concatSubject {string|boolean} - Email concatenation subject
// ---
// mailOptions {[object]} - Array of nodeMailer's `mailOptions`
// mailOptions.to {string|[string]} - [REQUIRED]
// mailOptions.from {string}
// mailOptions.text {string|boolean}
// mailOptions.html {string}
// mailOptions.subject {string}
// mailOptions.Other nodeMailer `sendMail` options...
}
/**
* @async
* @memberOf MongoQueue
* @name ping
* @description Check connection to Storage
* @returns {Promise<object>}
*/
async ping() {
if (!this.mailTimeInstance) {
return {
status: 'Service Unavailable',
code: 503,
statusCode: 503,
error: new Error('MailTime instance not yet assigned to {mailTimeInstance} of Queue Adapter context'),
};
}
try {
const ping = await this.db.command({ ping: 1 });
if (ping?.ok === 1) {
return {
status: 'OK',
code: 200,
statusCode: 200,
};
}
} catch (pingError) {
return {
status: 'Internal Server Error',
code: 500,
statusCode: 500,
error: pingError
};
}
return {
status: 'Service Unavailable',
code: 503,
statusCode: 503,
error: new Error('Service Unavailable')
};
}
/**
* @memberOf MongoQueue
* @name iterate
* @description iterate over queued tasks passing to `mailTimeInstance.___send` method
* @returns {void 0}
*/
async iterate() {
try {
const cursor = this.collection.find({
isSent: false,
isFailed: false,
isCancelled: false,
sendAt: {
$lte: Date.now()
},
tries: {
$lt: this.mailTimeInstance.maxTries
}
}, {
projection: {
_id: 1,
uuid: 1,
tries: 1,
template: 1,
transport: 1,
isSent: 1,
isFailed: 1,
isCancelled: 1,
mailOptions: 1,
concatSubject: 1,
}
});
while (await cursor.hasNext()) {
await this.mailTimeInstance.___send(await cursor.next());
}
await cursor.close();
} catch (iterateError) {
logError('[iterate] [while/await] [iterateError]', iterateError);
}
}
/**
* @async
* @memberOf MongoQueue
* @name getPendingTo
* @description get queued task by `to` field (addressee)
* @param to {string} - email address
* @param sendAt {number} - timestamp
* @returns {Promise<object|null>}
*/
async getPendingTo(to, sendAt) {
if (typeof to !== 'string' || typeof sendAt !== 'number') {
return null;
}
return await this.collection.findOne({
to: to,
isSent: false,
isFailed: false,
isCancelled: false,
sendAt: {
$lte: sendAt
}
}, {
projection: {
_id: 1,
to: 1,
uuid: 1,
tries: 1,
isSent: 1,
isFailed: 1,
isCancelled: 1,
mailOptions: 1,
}
});
}
/**
* @async
* @memberOf MongoQueue
* @name push
* @description push task to the queue/storage
* @param task {object} - task's object
* @returns {Promise<void 0>}
*/
async push(task) {
if (typeof task !== 'object') {
return;
}
if (task.sendAt instanceof Date) {
task.sendAt = +task.sendAt;
}
await this.collection.insertOne(task);
}
/**
* @async
* @memberOf MongoQueue
* @name cancel
* @description cancel scheduled email
* @param uuid {string} - email's uuid
* @returns {Promise<boolean>} returns `true` if cancelled or `false` if not found, was sent, or was cancelled previously
*/
async cancel(uuid) {
if (typeof uuid !== 'string') {
return false;
}
const task = await this.collection.findOne({ uuid }, {
projection: {
_id: 1,
uuid: 1,
isSent: 1,
isCancelled: 1,
}
});
if (!task || task.isSent === true || task.isCancelled === true) {
return false;
}
if (!this.mailTimeInstance.keepHistory) {
return await this.remove(task);
}
return await this.update(task, {
isCancelled: true,
});
}
/**
* @async
* @memberOf MongoQueue
* @name remove
* @description remove task from queue
* @param task {object} - task's object
* @returns {Promise<boolean>} returns `true` if removed or `false` if not found
*/
async remove(task) {
if (typeof task !== 'object') {
return false;
}
return (await this.collection.deleteOne({ _id: task._id }))?.deletedCount >= 1;
}
/**
* @async
* @memberOf MongoQueue
* @name update
* @description remove task from queue
* @param task {object} - task's object
* @param updateObj {object} - fields with new values to update
* @returns {Promise<boolean>} returns `true` if updated or `false` if not found or no changes was made
*/
async update(task, updateObj) {
if (typeof task !== 'object' || typeof updateObj !== 'object') {
return false;
}
return (await this.collection.updateOne({
_id: task._id
}, {
$set: updateObj
}))?.modifiedCount >= 1;
}
}
/** Class representing Redis Queue for MailTime */
class RedisQueue {
/**
* Create a RedisQueue instance
* @param {object} opts - configuration object
* @param {RedisClient} opts.client - Required, Redis'es `RedisClient` instance, like one returned from `await redis.createClient().connect()` method
* @param {string} [opts.prefix] - Optional prefix for scope isolation; use when creating multiple MailTime instances within the single application
*/
constructor (opts) {
this.name = 'redis-queue';
if (!opts || typeof opts !== 'object' || opts === null) {
throw new TypeError('[mail-time] Configuration object must be passed into RedisQueue constructor');
}
if (!opts.client) {
throw new Error('[mail-time] [RedisQueue] required {client} option is missing, e.g. returned from `redis.createClient()` or `redis.createCluster()` method');
}
this.prefix = (typeof opts.prefix === 'string') ? opts.prefix : 'default';
this.uniqueName = `mailtime:${this.prefix}`;
this.client = opts.client;
// 3 types of keys are stored in Redis:
// 'letter' - JSON with email details
// 'sendat' - Timestamp used to scan/find/iterate over scheduled emails
// 'concatletter' — uuid of "pending" email for concatenation used when {concatEmails: true}
// Stored JSON structure:
// to {string|[string]}
// uuid {string}
// tries {number} - qty of send attempts
// sendAt {number} - When letter should be sent
// isSent {boolean} - Email status
// isCancelled {boolean} - `true` if email was cancelled before it was sent
// isFailed {boolean} - `true` if email has failed to send
// template {string} - Template for this email
// transport {number} - Last used transport
// concatSubject {string|boolean} - Email concatenation subject
// ---
// mailOptions {[object]} - Array of nodeMailer's `mailOptions`
// mailOptions.to {string|[string]} - [REQUIRED]
// mailOptions.from {string}
// mailOptions.text {string|boolean}
// mailOptions.html {string}
// mailOptions.subject {string}
// mailOptions.Other nodeMailer `sendMail` options...
}
/**
* @async
* @memberOf RedisQueue
* @name ping
* @description Check connection to Storage
* @returns {Promise<object>}
*/
async ping() {
if (!this.mailTimeInstance) {
return {
status: 'Service Unavailable',
code: 503,
statusCode: 503,
error: new Error('MailTime instance not yet assigned to {mailTimeInstance} of Queue Adapter context'),
};
}
try {
const ping = await this.client.ping();
if (ping === 'PONG') {
return {
status: 'OK',
code: 200,
statusCode: 200,
};
}
} catch (pingError) {
return {
status: 'Internal Server Error',
code: 500,
statusCode: 500,
error: pingError
};
}
return {
status: 'Service Unavailable',
code: 503,
statusCode: 503,
error: new Error('Service Unavailable')
};
}
/**
* @memberOf RedisQueue
* @name iterate
* @description iterate over queued tasks passing to `mailTimeInstance.___send` method
* @returns {void 0}
*/
async iterate() {
try {
const now = Date.now();
const cursor = this.client.scanIterator({
TYPE: 'string',
MATCH: this.__getKey('*', 'sendat'),
COUNT: 9999,
});
for await (const sendatKey of cursor) {
if (parseInt(await this.client.get(sendatKey)) <= now) {
await this.mailTimeInstance.___send(JSON.parse(await this.client.get(this.__getKey(sendatKey.split(':')[3]))));
}
}
} catch (iterateError) {
logError('[iterate] [for/await] [iterateError]', iterateError);
}
}
/**
* @async
* @memberOf RedisQueue
* @name getPendingTo
* @description get queued task by `to` field (addressee)
* @param to {string} - email address
* @param sendAt {number} - timestamp
* @returns {Promise<object|null>}
*/
async getPendingTo(to, sendAt) {
if (typeof to !== 'string' || typeof sendAt !== 'number') {
return null;
}
const concatKey = this.__getKey(to, 'concatletter');
let exists = await this.client.exists(concatKey);
if (!exists) {
return null;
}
const uuid = await this.client.get(concatKey);
const letterKey = this.__getKey(uuid, 'letter');
exists = await this.client.exists(letterKey);
if (!exists) {
return null;
}
const task = JSON.parse(await this.client.get(letterKey));
if (!task || task.isSent === true || task.isCancelled === true || task.isFailed === true) {
return null;
}
return task;
}
/**
* @async
* @memberOf RedisQueue
* @name push
* @description push task to the queue/storage
* @param task {object} - task's object
* @returns {Promise<void 0>}
*/
async push(task) {
if (typeof task !== 'object') {
return;
}
if (task.sendAt instanceof Date) {
task.sendAt = +task.sendAt;
}
await this.client.set(this.__getKey(task.uuid, 'letter'), JSON.stringify(task));
await this.client.set(this.__getKey(task.uuid, 'sendat'), `${task.sendAt}`);
if (task.to) {
await this.client.set(this.__getKey(task.to, 'concatletter'), task.uuid, {
PXAT: task.sendAt - 128
});
}
}
/**
* @async
* @memberOf RedisQueue
* @name cancel
* @description cancel scheduled email
* @param uuid {string} - email's uuid
* @returns {Promise<boolean>} returns `true` if cancelled or `false` if not found, was sent, or was cancelled previously
*/
async cancel(uuid) {
if (typeof uuid !== 'string') {
return false;
}
await this.client.del(this.__getKey(uuid, 'sendat'));
const letterKey = this.__getKey(uuid, 'letter');
const exists = await this.client.exists(letterKey);
if (!exists) {
return false;
}
const task = JSON.parse(await this.client.get(letterKey));
if (!task || task.isSent === true || task.isCancelled === true) {
return false;
}
if (!this.mailTimeInstance.keepHistory) {
return await this.remove(task);
}
return await this.update(task, {
isCancelled: true,
});
}
/**
* @async
* @memberOf RedisQueue
* @name remove
* @description remove task from queue
* @param task {object} - task's object
* @returns {Promise<boolean>} returns `true` if removed or `false` if not found
*/
async remove(task) {
if (typeof task !== 'object' || typeof task.uuid !== 'string') {
return false;
}
const letterKey = this.__getKey(task.uuid, 'letter');
const exists = await this.client.exists(letterKey);
if (!exists) {
return false;
}
await this.client.del([
letterKey,
this.__getKey(task.uuid, 'sendat'),
]);
return true;
}
/**
* @async
* @memberOf RedisQueue
* @name update
* @description remove task from queue
* @param task {object} - task's object
* @param updateObj {object} - fields with new values to update
* @returns {Promise<boolean>} returns `true` if updated or `false` if not found or no changes was made
*/
async update(task, updateObj) {
if (typeof task !== 'object' || typeof task.uuid !== 'string' || typeof updateObj !== 'object') {
return false;
}
const letterKey = this.__getKey(task.uuid, 'letter');
try {
const exists = await this.client.exists(letterKey);
if (!exists) {
return false;
}
const updatedTask = { ...task, ...updateObj };
await this.client.set(letterKey, JSON.stringify(updatedTask));
const sendatKey = this.__getKey(task.uuid, 'sendat');
if (updatedTask.isSent === true || updatedTask.isFailed === true || updatedTask.isCancelled === true) {
await this.client.del(sendatKey);
} else if (task.sendAt) {
await this.client.set(sendatKey, `${+task.sendAt}`);
}
return true;
} catch (opError) {
logError('[update] [try/catch] [opError]', opError);
return false;
}
}
/**
* @memberOf RedisQueue
* @name __getKey
* @description helper to generate scoped key
* @param uuid {string} - letter's uuid
* @param type {string} - "letter" or "sendat" or "concatletter"
* @returns {string} returns key used by Redis
*/
__getKey(uuid, type = 'letter') {
if (!this.__keyTypes.includes(type)) {
throw new Error(`[mail-time] [RedisQueue] [__getKey] unsupported key "${type}" passed into the second argument`);
}
return `${this.uniqueName}:${type}:${uuid}`;
}
/**
* @memberOf RedisQueue
* @name __keyTypes
* @description list of supported key type
* @returns {string} returns key used by Redis
*/
__keyTypes = ['letter', 'sendat', 'concatletter'];
}
const noop = () => {};
/**
* Check if entities of various types are equal
* including edge cases like unordered Objects and Array
* @function equals
* @param {mix} a
* @param {mix} b
* @returns {boolean}
*/
const equals = (a, b) => {
let i;
if (a === b) {
return true;
}
if (!a || !b) {
return false;
}
if (!(typeof a === 'object' && typeof b === 'object')) {
return false;
}
if (a instanceof Date && b instanceof Date) {
return a.valueOf() === b.valueOf();
}
if (a instanceof Array) {
if (!(b instanceof Array)) {
return false;
}
if (a.length !== b.length) {
return false;
}
const _a = a.slice();
const _b = b.slice();
let j;
for (i = _a.length - 1; i >= 0; i--) {
let result = false;
for (j = _b.length - 1; j >= 0; j--) {
if (equals(_a[i], _b[j])) {
result = true;
_a.splice(i, 1);
_b.splice(j, 1);
break;
}
}
if (!result) {
return false;
}
}
return true;
}
i = 0;
if (typeof a === 'object' && typeof b === 'object') {
const akeys = Object.keys(a);
const bkeys = Object.keys(b);
if (akeys.length !== bkeys.length) {
return false;
}
for (i = akeys.length - 1; i >= 0; i--) {
if (!hasOwnProperty.call(b, akeys[i])) {
return false;
}
if (!equals(a[akeys[i]], b[akeys[i]])) {
return false;
}
}
return true;
}
return false;
};
let DEFAULT_TEMPLATE = '<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml><meta content="text/html; charset=utf-8"http-equiv=Content-Type><meta content="width=device-width,initial-scale=1"name=viewport><title>{{subject}}</title><style>body{-webkit-font-smoothing:antialiased;-webkit-text-size-adjust:none;font-family:Tiempos,Georgia,Times,serif;font-weight:400;width:100%;height:100%;background:#fff;font-size:15px;color:#000;line-height:1.5}a{text-decoration:underline;border:0;color:#000;outline:0;color:inherit}a:hover{text-decoration:none}a[href^=sms],a[href^=tel]{text-decoration:none;color:#000;cursor:default}a img{border:none;text-decoration:none}td{font-family:Tiempos,Georgia,Times,serif;font-weight:400}hr{height:1px;border:none;width:100%;margin:0;margin-top:25px;margin-bottom:25px;background-color:#ECECEC}h1,h2,h3,h4,h5,h6{font-family:HelveticaNeue,"Helvetica Neue",Helvetica,Arial,sans-serif;font-weight:300;line-height:normal;margin-top:35px;margin-bottom:4px;margin-left:0;margin-right:0}h1{margin:23px 15px;font-size:25px}h2{margin-top:15px;font-size:21px}h3{font-weight:400;font-size:19px;border-bottom:1px solid #ECECEC}h4{font-weight:400;font-size:18px}h5{font-weight:400;font-size:17px}h6{font-weight:600;font-size:16px}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{text-decoration:none}pre{font-family:Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,sans-serif;display:block;font-size:13px;padding:9.5px;margin:0 0 10px;line-height:1.42;color:#333;word-break:break-all;word-wrap:break-word;background-color:#f5f5f5;border:1px solid #ccc;border-radius:4px;text-align:left!important;max-width:100%;white-space:pre-wrap;width:auto;overflow:auto}code{font-size:13px;font-family:font-family: Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,sans-serif;border:1px solid rgba(0,0,0,.223);border-radius:2px;padding:1px 2px;word-break:break-all;word-wrap:break-word}pre code{padding:0;font-size:inherit;color:inherit;white-space:pre-wrap;background-color:transparent;border:none;border-radius:0;word-break:break-all;word-wrap:break-word}td{text-align:center}table{border-collapse:collapse!important}.force-full-width{width:100%!important}</style><style media=screen>@media screen{h1,h2,h3,h4,h5,h6{font-family:\'Helvetica Neue\',Arial,sans-serif!important}td{font-family:Tiempos,Georgia,Times,serif!important}code,pre{font-family:Consolas,Menlo,Monaco,\'Lucida Console\',\'Liberation Mono\',\'DejaVu Sans Mono\',\'Bitstream Vera Sans Mono\',\'Courier New\',monospace,sans-serif!important}}</style><style media="only screen and (max-width:480px)">@media only screen and (max-width:480px){table[class=w320]{width:100%!important}}</style><body bgcolor=#FFFFFF class=body style=padding:0;margin:0;display:block;background:#fff;-webkit-text-size-adjust:none><table cellpadding=0 cellspacing=0 width=100% align=center><tr><td align=center valign=top bgcolor=#FFFFFF width=100%><center><table cellpadding=0 cellspacing=0 width=600 style="margin:0 auto"class=w320><tr><td align=center valign=top><table cellpadding=0 cellspacing=0 width=100% style="margin:0 auto;border-bottom:1px solid #ddd"bgcolor=#ECECEC><tr><td><h1>{{{subject}}}</h1></table><table cellpadding=0 cellspacing=0 width=100% style="margin:0 auto"bgcolor=#F2F2F2><tr><td><center><table cellpadding=0 cellspacing=0 width=100% style="margin:0 auto"><tr><td align=left style="text-align:left;padding:30px 25px">{{{html}}}</table></center></table></table></center></table>';
/** Class of MailTime */
class MailTime {
/**
* Create a MailTime instance
* @param {object} opts - configuration object
* @param {RedisQueue|MongoQueue|CustomQueue} opts.queue - Queue Storage Driver instance
* @param {string} [opts.type] - "server" or "client" type of MailTime instance
* @param {function} [opts.from] - A function returning *String* for `from` header, format: `"MyApp" <user@example.com>`
* @param {[object]} [opts.transports] - An array of `nodemailer`'s transports, returned from `nodemailer.createTransport({})`; Required for `{type: 'server'}`
* @param {string} [opts.strategy] - `backup` or `balancer`
* @param {number} [opts.failsToNext] - After how many failed "send attempts" switch to the next transport? Applied only for `backup` strategy, default - `4`
* @param {number} [opts.retries] - How many times resend failed emails
* @param {number} [opts.retryDelay] - Interval in milliseconds between send re-tries
* @param {boolean} [opts.keepHistory] - Keep queue task as it is in the database
* @param {boolean} [opts.concatEmails] - Concatenate email by `to` field, default - `false`
* @param {string} [opts.concatSubject] - Email subject used in concatenated email, default - `Multiple notifications`
* @param {string} [opts.concatDelimiter] - HTML or plain string delimiter used between concatenated email, default - `<hr>`
* @param {number} [opts.concatDelay] - HTML or plain string delimiter used between concatenated email, default - `<hr>`
* @param {number} [opts.revolvingInterval] - Interval in *milliseconds* in between queue checks, default - `256`
* @param {object|RedisAdapter|MongoAdapter|CustomAdapter} [opts.josk.adapter] - Interval in milliseconds between send re-tries
* @param {string} [opts.josk.adapter.type] - One of `mongo` or `redis`
* @param {string} [opts.prefix] - Optional prefix for scope isolation; use when creating multiple MailTime instances within the single application; By default prefix inherited from MailTime instance
*/
constructor (opts) {
if (!opts || typeof opts !== 'object' || opts === null) {
throw new TypeError('[mail-time] Configuration object must be passed into MailTime constructor');
}
if (!opts.queue || typeof opts.queue !== 'object') {
throw new Error('[mail-time] {queue} option is required', {
description: 'MailTime requires MongoQueue, RedisQueue, or CustomQueue to connect to an intermediate database'
});
}
this.queue = opts.queue;
this.queue.mailTimeInstance = this;
const queueMethods = ['ping', 'iterate', 'getPendingTo', 'push', 'remove', 'update', 'cancel'];
for (let i = queueMethods.length - 1; i >= 0; i--) {
if (typeof this.queue[queueMethods[i]] !== 'function') {
throw new Error(`{queue} instance is missing {${queueMethods[i]}} method that is required!`);
}
}
this.debug = (opts.debug !== true) ? false : true;
this._debug = (...args) => {
debug(this.debug, ...args);
};
this.type = (typeof opts.type !== 'string' || (opts.type !== 'client' && opts.type !== 'server')) ? 'server' : opts.type;
this.prefix = (typeof opts.prefix === 'string') ? opts.prefix : '';
if (typeof opts.retries === 'number') {
this.maxTries = opts.retries + 1;
} else if (typeof opts.maxTries === 'number') {
this.maxTries = (opts.maxTries < 1) ? 1 : 0;
} else {
this.maxTries = 60;
}
if (typeof opts.retryDelay === 'number') {
this.retryDelay = opts.retryDelay;
} else if (typeof opts.interval === 'number') {
this.retryDelay = opts.interval * 1000;
} else {
this.retryDelay = 60000;
}
this.template = (typeof opts.template === 'string') ? opts.template : '{{{html}}}';
this.keepHistory = (typeof opts.keepHistory === 'boolean') ? opts.keepHistory : false;
this.onSent = opts.onSent || noop;
this.onError = opts.onError || noop;
this.revolvingInterval = opts.revolvingInterval || 1536;
this.failsToNext = (typeof opts.failsToNext === 'number') ? opts.failsToNext : 4;
this.strategy = (opts.strategy === 'backup' || opts.strategy === 'balancer') ? opts.strategy : 'backup';
this.transports = opts.transports || [];
this.transport = 0;
this.from = (() => {
if (typeof opts.from === 'string') {
return () => {
return opts.from;
};
}
if (typeof opts.from === 'function') {
return opts.from;
}
return false;
})();
this.concatEmails = (opts.concatEmails !== true) ? false : true;
this.concatSubject = (opts.concatSubject && typeof opts.concatSubject === 'string') ? opts.concatSubject : 'Multiple notifications';
this.concatDelimiter = (opts.concatDelimiter && typeof opts.concatDelimiter === 'string') ? opts.concatDelimiter : '<hr>';
if (typeof opts.concatDelay === 'number') {
this.concatDelay = opts.concatDelay;
} else if (typeof opts.concatThrottling === 'number') {
this.concatDelay = opts.concatThrottling * 1000;
} else {
this.concatDelay = 60000;
}
this._debug('DEBUG ON {debug: true}');
this._debug(`INITIALIZING [type: ${this.type}]`);
this._debug(`INITIALIZING [strategy: ${this.strategy}]`);
this._debug(`INITIALIZING [josk.adapter: ${opts?.josk?.adapter}]`);
this._debug(`INITIALIZING [prefix: ${this.prefix}]`);
this._debug(`INITIALIZING [retries: ${this.retries}]`);
this._debug(`INITIALIZING [failsToNext: ${this.failsToNext}]`);
this._debug(`INITIALIZING [onError: ${this.onError}]`);
this._debug(`INITIALIZING [onSent: ${this.onSent}]`);
/** SERVER-SPECIFIC CHECKS AND CONFIG */
if (this.type === 'server') {
if (this.transports.constructor !== Array || !this.transports.length) {
throw new Error('[mail-time] {transports} is required for {type: "server"} and must be an Array, like returned from `nodemailer.createTransport`');
}
if (typeof opts.josk !== 'object') {
throw new Error('[mail-time] {josk} option is required {object} for {type: "server"}');
}
if (typeof opts.josk.adapter !== 'object') {
throw new Error('[mail-time] {josk.adapter} option is required {object} *or* custom adapter Class');
}
this.josk = { ...opts.josk };
if (typeof opts.josk.adapter?.type === 'string' && opts.josk.adapter?.type === 'mongo') {
if (!opts.josk.adapter.db) {
throw new Error('[mail-time] {josk.adapter.db} option required for {josk.adapter.type: "mongo"}');
}
this.josk.adapter = new josk.MongoAdapter({ prefix: `mailTimeQueue${this.prefix}`, ...opts.josk.adapter });
}
if (typeof opts.josk.adapter?.type === 'string' && opts.josk.adapter?.type === 'redis') {
if (!opts.josk.adapter.client) {
throw new Error('[mail-time] {josk.adapter.client} option required for {josk.adapter.type: "redis"}');
}
this.josk.adapter = new josk.RedisAdapter({ prefix: `mailTimeQueue${this.prefix}`, ...opts.josk.adapter });
}
this.josk.minRevolvingDelay = (typeof opts.josk.minRevolvingDelay === 'number') ? opts.josk.minRevolvingDelay : 512;
this.josk.maxRevolvingDelay = (typeof opts.josk.maxRevolvingDelay === 'number') ? opts.josk.maxRevolvingDelay : 2048;
this.josk.zombieTime = (typeof opts.josk.zombieTime === 'number') ? opts.josk.zombieTime : 32786;
this.scheduler = new josk.JoSk({
debug: this.debug,
...this.josk,
});
process.nextTick(async () => {
if ((await this.scheduler.ping()).status !== 'OK') {
throw new Error('[mail-time] [MailTime#ping] can not connect to storage, make sure it is available and properly configured');
}
});
this.scheduler.setInterval(this.___iterate.bind(this), this.revolvingInterval, `mailTimeQueue${this.prefix}`);
}
}
static get Template() {
return DEFAULT_TEMPLATE;
}
static set Template(newVal) {
DEFAULT_TEMPLATE = newVal;
}
/**
* @async
* @memberOf MailTime
* @name ping
* @description Check package readiness and connection to Storage
* @returns {Promise<object>}
* @throws {mix}
*/
async ping() {
this._debug('[ping]');
const schedulerPing = await this.scheduler.ping();
if (schedulerPing.status !== 'OK') {
return schedulerPing;
}
return await this.queue.ping();
}
/**
* @memberOf MailTime
* @name send
* @description alias of `sendMail`
* @returns {Promise<string>} uuid of the email
*/
async send(opts) {
this._debug('[send]', opts);
return await this.sendMail(opts);
}
/**
* @async
* @memberOf MailTime
* @name sendMail
* @description add email to the queue or append to existing letter if {concatEmails: true}
* @param opts {object} - email options
* @param opts.sendAt {Date|number} - When email should be sent
* @param opts.template {string} - Email-specific template
* @param opts[key] {mix} - Other NodeMailer's options
* @returns {Promise<string>} uuid of the email
* @throws {Error}
*/
async sendMail(opts = {}) {
this._debug('[sendMail]', opts);
if (!opts.html && !opts.text) {
throw new Error('`html` nor `text` field is presented, at least one of those fields is required');
}
let sendAt = opts.sendAt;
if (!sendAt) {
sendAt = Date.now();
}
if (sendAt instanceof Date) {
sendAt = +sendAt;
}
if (typeof sendAt !== 'number') {
sendAt = Date.now();
}
let template = opts.template;
if (typeof template !== 'string') {
template = false;
}
let concatSubject = opts.concatSubject;
if (typeof concatSubject !== 'string') {
concatSubject = false;
}
const mailOptions = { ...opts };
delete mailOptions.sendAt;
delete mailOptions.template;
delete mailOptions.concatSubject;
if (typeof mailOptions.to !== 'string' && (!(mailOptions.to instanceof Array) || !mailOptions.to.length)) {
throw new Error('[mail-time] `mailOptions.to` is required and must be a string or non-empty Array');
}
if (this.concatEmails) {
sendAt = sendAt + this.concatDelay;
const task = await this.queue.getPendingTo(mailOptions.to, sendAt);
if (task) {
const pendingMailOptions = task.mailOptions || [];
for (let i = 0; i < pendingMailOptions.length; i++) {
if (equals(pendingMailOptions[i], mailOptions)) {
return task.uuid;
}
}
pendingMailOptions.push(opts);
await this.queue.update(task, {
mailOptions: pendingMailOptions
});
return task.uuid;
}
}
return await this.___addToQueue({
sendAt,
template,
concatSubject,
mailOptions: mailOptions,
});
}
/**
* @memberOf MailTime
* @name cancel
* @description alias of `cancelMail`
* @returns {Promise<boolean>} returns `true` if cancelled or `false` if not found was sent or was cancelled previously
*/
async cancel(uuid) {
this._debug('[cancel]', uuid);
return await this.cancelMail(uuid);
}
/**
* @async
* @memberOf MailTime
* @name cancelMail