@depup/nodemailer 7.0.12-depup.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.
Files changed (47) hide show
  1. package/.gitattributes +6 -0
  2. package/.ncurc.js +9 -0
  3. package/.prettierignore +8 -0
  4. package/.prettierrc +12 -0
  5. package/.prettierrc.js +10 -0
  6. package/.release-please-config.json +9 -0
  7. package/CHANGELOG.md +929 -0
  8. package/CODE_OF_CONDUCT.md +76 -0
  9. package/LICENSE +16 -0
  10. package/README.md +86 -0
  11. package/SECURITY.txt +22 -0
  12. package/eslint.config.js +88 -0
  13. package/lib/addressparser/index.js +383 -0
  14. package/lib/base64/index.js +139 -0
  15. package/lib/dkim/index.js +253 -0
  16. package/lib/dkim/message-parser.js +155 -0
  17. package/lib/dkim/relaxed-body.js +154 -0
  18. package/lib/dkim/sign.js +117 -0
  19. package/lib/fetch/cookies.js +281 -0
  20. package/lib/fetch/index.js +280 -0
  21. package/lib/json-transport/index.js +82 -0
  22. package/lib/mail-composer/index.js +629 -0
  23. package/lib/mailer/index.js +441 -0
  24. package/lib/mailer/mail-message.js +316 -0
  25. package/lib/mime-funcs/index.js +625 -0
  26. package/lib/mime-funcs/mime-types.js +2113 -0
  27. package/lib/mime-node/index.js +1316 -0
  28. package/lib/mime-node/last-newline.js +33 -0
  29. package/lib/mime-node/le-unix.js +43 -0
  30. package/lib/mime-node/le-windows.js +52 -0
  31. package/lib/nodemailer.js +157 -0
  32. package/lib/punycode/index.js +460 -0
  33. package/lib/qp/index.js +227 -0
  34. package/lib/sendmail-transport/index.js +210 -0
  35. package/lib/ses-transport/index.js +234 -0
  36. package/lib/shared/index.js +754 -0
  37. package/lib/smtp-connection/data-stream.js +108 -0
  38. package/lib/smtp-connection/http-proxy-client.js +143 -0
  39. package/lib/smtp-connection/index.js +1865 -0
  40. package/lib/smtp-pool/index.js +652 -0
  41. package/lib/smtp-pool/pool-resource.js +259 -0
  42. package/lib/smtp-transport/index.js +421 -0
  43. package/lib/stream-transport/index.js +135 -0
  44. package/lib/well-known/index.js +47 -0
  45. package/lib/well-known/services.json +611 -0
  46. package/lib/xoauth2/index.js +427 -0
  47. package/package.json +47 -0
@@ -0,0 +1,754 @@
1
+ /* eslint no-console: 0 */
2
+
3
+ 'use strict';
4
+
5
+ const urllib = require('url');
6
+ const util = require('util');
7
+ const fs = require('fs');
8
+ const nmfetch = require('../fetch');
9
+ const dns = require('dns');
10
+ const net = require('net');
11
+ const os = require('os');
12
+
13
+ const DNS_TTL = 5 * 60 * 1000;
14
+ const CACHE_CLEANUP_INTERVAL = 30 * 1000; // Minimum 30 seconds between cleanups
15
+ const MAX_CACHE_SIZE = 1000; // Maximum number of entries in cache
16
+
17
+ let lastCacheCleanup = 0;
18
+ module.exports._lastCacheCleanup = () => lastCacheCleanup;
19
+ module.exports._resetCacheCleanup = () => {
20
+ lastCacheCleanup = 0;
21
+ };
22
+
23
+ let networkInterfaces;
24
+ try {
25
+ networkInterfaces = os.networkInterfaces();
26
+ } catch (_err) {
27
+ // fails on some systems
28
+ }
29
+
30
+ module.exports.networkInterfaces = networkInterfaces;
31
+
32
+ const isFamilySupported = (family, allowInternal) => {
33
+ let networkInterfaces = module.exports.networkInterfaces;
34
+ if (!networkInterfaces) {
35
+ // hope for the best
36
+ return true;
37
+ }
38
+
39
+ const familySupported =
40
+ // crux that replaces Object.values(networkInterfaces) as Object.values is not supported in nodejs v6
41
+ Object.keys(networkInterfaces)
42
+ .map(key => networkInterfaces[key])
43
+ // crux that replaces .flat() as it is not supported in older Node versions (v10 and older)
44
+ .reduce((acc, val) => acc.concat(val), [])
45
+ .filter(i => !i.internal || allowInternal)
46
+ .filter(i => i.family === 'IPv' + family || i.family === family).length > 0;
47
+
48
+ return familySupported;
49
+ };
50
+
51
+ const resolver = (family, hostname, options, callback) => {
52
+ options = options || {};
53
+ const familySupported = isFamilySupported(family, options.allowInternalNetworkInterfaces);
54
+
55
+ if (!familySupported) {
56
+ return callback(null, []);
57
+ }
58
+
59
+ const resolver = dns.Resolver ? new dns.Resolver(options) : dns;
60
+ resolver['resolve' + family](hostname, (err, addresses) => {
61
+ if (err) {
62
+ switch (err.code) {
63
+ case dns.NODATA:
64
+ case dns.NOTFOUND:
65
+ case dns.NOTIMP:
66
+ case dns.SERVFAIL:
67
+ case dns.CONNREFUSED:
68
+ case dns.REFUSED:
69
+ case 'EAI_AGAIN':
70
+ return callback(null, []);
71
+ }
72
+ return callback(err);
73
+ }
74
+ return callback(null, Array.isArray(addresses) ? addresses : [].concat(addresses || []));
75
+ });
76
+ };
77
+
78
+ const dnsCache = (module.exports.dnsCache = new Map());
79
+
80
+ const formatDNSValue = (value, extra) => {
81
+ if (!value) {
82
+ return Object.assign({}, extra || {});
83
+ }
84
+
85
+ return Object.assign(
86
+ {
87
+ servername: value.servername,
88
+ host:
89
+ !value.addresses || !value.addresses.length
90
+ ? null
91
+ : value.addresses.length === 1
92
+ ? value.addresses[0]
93
+ : value.addresses[Math.floor(Math.random() * value.addresses.length)]
94
+ },
95
+ extra || {}
96
+ );
97
+ };
98
+
99
+ module.exports.resolveHostname = (options, callback) => {
100
+ options = options || {};
101
+
102
+ if (!options.host && options.servername) {
103
+ options.host = options.servername;
104
+ }
105
+
106
+ if (!options.host || net.isIP(options.host)) {
107
+ // nothing to do here
108
+ let value = {
109
+ addresses: [options.host],
110
+ servername: options.servername || false
111
+ };
112
+ return callback(
113
+ null,
114
+ formatDNSValue(value, {
115
+ cached: false
116
+ })
117
+ );
118
+ }
119
+
120
+ let cached;
121
+ if (dnsCache.has(options.host)) {
122
+ cached = dnsCache.get(options.host);
123
+
124
+ // Lazy cleanup with time throttling
125
+ const now = Date.now();
126
+ if (now - lastCacheCleanup > CACHE_CLEANUP_INTERVAL) {
127
+ lastCacheCleanup = now;
128
+
129
+ // Clean up expired entries
130
+ for (const [host, entry] of dnsCache.entries()) {
131
+ if (entry.expires && entry.expires < now) {
132
+ dnsCache.delete(host);
133
+ }
134
+ }
135
+
136
+ // If cache is still too large, remove oldest entries
137
+ if (dnsCache.size > MAX_CACHE_SIZE) {
138
+ const toDelete = Math.floor(MAX_CACHE_SIZE * 0.1); // Remove 10% of entries
139
+ const keys = Array.from(dnsCache.keys()).slice(0, toDelete);
140
+ keys.forEach(key => dnsCache.delete(key));
141
+ }
142
+ }
143
+
144
+ if (!cached.expires || cached.expires >= now) {
145
+ return callback(
146
+ null,
147
+ formatDNSValue(cached.value, {
148
+ cached: true
149
+ })
150
+ );
151
+ }
152
+ }
153
+
154
+ resolver(4, options.host, options, (err, addresses) => {
155
+ if (err) {
156
+ if (cached) {
157
+ dnsCache.set(options.host, {
158
+ value: cached.value,
159
+ expires: Date.now() + (options.dnsTtl || DNS_TTL)
160
+ });
161
+
162
+ return callback(
163
+ null,
164
+ formatDNSValue(cached.value, {
165
+ cached: true,
166
+ error: err
167
+ })
168
+ );
169
+ }
170
+ return callback(err);
171
+ }
172
+
173
+ if (addresses && addresses.length) {
174
+ let value = {
175
+ addresses,
176
+ servername: options.servername || options.host
177
+ };
178
+
179
+ dnsCache.set(options.host, {
180
+ value,
181
+ expires: Date.now() + (options.dnsTtl || DNS_TTL)
182
+ });
183
+
184
+ return callback(
185
+ null,
186
+ formatDNSValue(value, {
187
+ cached: false
188
+ })
189
+ );
190
+ }
191
+
192
+ resolver(6, options.host, options, (err, addresses) => {
193
+ if (err) {
194
+ if (cached) {
195
+ dnsCache.set(options.host, {
196
+ value: cached.value,
197
+ expires: Date.now() + (options.dnsTtl || DNS_TTL)
198
+ });
199
+
200
+ return callback(
201
+ null,
202
+ formatDNSValue(cached.value, {
203
+ cached: true,
204
+ error: err
205
+ })
206
+ );
207
+ }
208
+ return callback(err);
209
+ }
210
+
211
+ if (addresses && addresses.length) {
212
+ let value = {
213
+ addresses,
214
+ servername: options.servername || options.host
215
+ };
216
+
217
+ dnsCache.set(options.host, {
218
+ value,
219
+ expires: Date.now() + (options.dnsTtl || DNS_TTL)
220
+ });
221
+
222
+ return callback(
223
+ null,
224
+ formatDNSValue(value, {
225
+ cached: false
226
+ })
227
+ );
228
+ }
229
+
230
+ try {
231
+ dns.lookup(options.host, { all: true }, (err, addresses) => {
232
+ if (err) {
233
+ if (cached) {
234
+ dnsCache.set(options.host, {
235
+ value: cached.value,
236
+ expires: Date.now() + (options.dnsTtl || DNS_TTL)
237
+ });
238
+
239
+ return callback(
240
+ null,
241
+ formatDNSValue(cached.value, {
242
+ cached: true,
243
+ error: err
244
+ })
245
+ );
246
+ }
247
+ return callback(err);
248
+ }
249
+
250
+ let address = addresses
251
+ ? addresses
252
+ .filter(addr => isFamilySupported(addr.family))
253
+ .map(addr => addr.address)
254
+ .shift()
255
+ : false;
256
+
257
+ if (addresses && addresses.length && !address) {
258
+ // there are addresses but none can be used
259
+ console.warn(`Failed to resolve IPv${addresses[0].family} addresses with current network`);
260
+ }
261
+
262
+ if (!address && cached) {
263
+ // nothing was found, fallback to cached value
264
+ return callback(
265
+ null,
266
+ formatDNSValue(cached.value, {
267
+ cached: true
268
+ })
269
+ );
270
+ }
271
+
272
+ let value = {
273
+ addresses: address ? [address] : [options.host],
274
+ servername: options.servername || options.host
275
+ };
276
+
277
+ dnsCache.set(options.host, {
278
+ value,
279
+ expires: Date.now() + (options.dnsTtl || DNS_TTL)
280
+ });
281
+
282
+ return callback(
283
+ null,
284
+ formatDNSValue(value, {
285
+ cached: false
286
+ })
287
+ );
288
+ });
289
+ } catch (_err) {
290
+ if (cached) {
291
+ dnsCache.set(options.host, {
292
+ value: cached.value,
293
+ expires: Date.now() + (options.dnsTtl || DNS_TTL)
294
+ });
295
+
296
+ return callback(
297
+ null,
298
+ formatDNSValue(cached.value, {
299
+ cached: true,
300
+ error: err
301
+ })
302
+ );
303
+ }
304
+ return callback(err);
305
+ }
306
+ });
307
+ });
308
+ };
309
+ /**
310
+ * Parses connection url to a structured configuration object
311
+ *
312
+ * @param {String} str Connection url
313
+ * @return {Object} Configuration object
314
+ */
315
+ module.exports.parseConnectionUrl = str => {
316
+ str = str || '';
317
+ let options = {};
318
+
319
+ [urllib.parse(str, true)].forEach(url => {
320
+ let auth;
321
+
322
+ switch (url.protocol) {
323
+ case 'smtp:':
324
+ options.secure = false;
325
+ break;
326
+ case 'smtps:':
327
+ options.secure = true;
328
+ break;
329
+ case 'direct:':
330
+ options.direct = true;
331
+ break;
332
+ }
333
+
334
+ if (!isNaN(url.port) && Number(url.port)) {
335
+ options.port = Number(url.port);
336
+ }
337
+
338
+ if (url.hostname) {
339
+ options.host = url.hostname;
340
+ }
341
+
342
+ if (url.auth) {
343
+ auth = url.auth.split(':');
344
+
345
+ if (!options.auth) {
346
+ options.auth = {};
347
+ }
348
+
349
+ options.auth.user = auth.shift();
350
+ options.auth.pass = auth.join(':');
351
+ }
352
+
353
+ Object.keys(url.query || {}).forEach(key => {
354
+ let obj = options;
355
+ let lKey = key;
356
+ let value = url.query[key];
357
+
358
+ if (!isNaN(value)) {
359
+ value = Number(value);
360
+ }
361
+
362
+ switch (value) {
363
+ case 'true':
364
+ value = true;
365
+ break;
366
+ case 'false':
367
+ value = false;
368
+ break;
369
+ }
370
+
371
+ // tls is nested object
372
+ if (key.indexOf('tls.') === 0) {
373
+ lKey = key.substr(4);
374
+ if (!options.tls) {
375
+ options.tls = {};
376
+ }
377
+ obj = options.tls;
378
+ } else if (key.indexOf('.') >= 0) {
379
+ // ignore nested properties besides tls
380
+ return;
381
+ }
382
+
383
+ if (!(lKey in obj)) {
384
+ obj[lKey] = value;
385
+ }
386
+ });
387
+ });
388
+
389
+ return options;
390
+ };
391
+
392
+ module.exports._logFunc = (logger, level, defaults, data, message, ...args) => {
393
+ let entry = {};
394
+
395
+ Object.keys(defaults || {}).forEach(key => {
396
+ if (key !== 'level') {
397
+ entry[key] = defaults[key];
398
+ }
399
+ });
400
+
401
+ Object.keys(data || {}).forEach(key => {
402
+ if (key !== 'level') {
403
+ entry[key] = data[key];
404
+ }
405
+ });
406
+
407
+ logger[level](entry, message, ...args);
408
+ };
409
+
410
+ /**
411
+ * Returns a bunyan-compatible logger interface. Uses either provided logger or
412
+ * creates a default console logger
413
+ *
414
+ * @param {Object} [options] Options object that might include 'logger' value
415
+ * @return {Object} bunyan compatible logger
416
+ */
417
+ module.exports.getLogger = (options, defaults) => {
418
+ options = options || {};
419
+
420
+ let response = {};
421
+ let levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
422
+
423
+ if (!options.logger) {
424
+ // use vanity logger
425
+ levels.forEach(level => {
426
+ response[level] = () => false;
427
+ });
428
+ return response;
429
+ }
430
+
431
+ let logger = options.logger;
432
+
433
+ if (options.logger === true) {
434
+ // create console logger
435
+ logger = createDefaultLogger(levels);
436
+ }
437
+
438
+ levels.forEach(level => {
439
+ response[level] = (data, message, ...args) => {
440
+ module.exports._logFunc(logger, level, defaults, data, message, ...args);
441
+ };
442
+ });
443
+
444
+ return response;
445
+ };
446
+
447
+ /**
448
+ * Wrapper for creating a callback that either resolves or rejects a promise
449
+ * based on input
450
+ *
451
+ * @param {Function} resolve Function to run if callback is called
452
+ * @param {Function} reject Function to run if callback ends with an error
453
+ */
454
+ module.exports.callbackPromise = (resolve, reject) =>
455
+ function () {
456
+ let args = Array.from(arguments);
457
+ let err = args.shift();
458
+ if (err) {
459
+ reject(err);
460
+ } else {
461
+ resolve(...args);
462
+ }
463
+ };
464
+
465
+ module.exports.parseDataURI = uri => {
466
+ if (typeof uri !== 'string') {
467
+ return null;
468
+ }
469
+
470
+ // Early return for non-data URIs to avoid unnecessary processing
471
+ if (!uri.startsWith('data:')) {
472
+ return null;
473
+ }
474
+
475
+ // Find the first comma safely - this prevents ReDoS
476
+ const commaPos = uri.indexOf(',');
477
+ if (commaPos === -1) {
478
+ return null;
479
+ }
480
+
481
+ const data = uri.substring(commaPos + 1);
482
+ const metaStr = uri.substring('data:'.length, commaPos);
483
+
484
+ let encoding;
485
+ const metaEntries = metaStr.split(';');
486
+
487
+ if (metaEntries.length > 0) {
488
+ const lastEntry = metaEntries[metaEntries.length - 1].toLowerCase().trim();
489
+ // Only recognize valid encoding types to prevent manipulation
490
+ if (['base64', 'utf8', 'utf-8'].includes(lastEntry) && lastEntry.indexOf('=') === -1) {
491
+ encoding = lastEntry;
492
+ metaEntries.pop();
493
+ }
494
+ }
495
+
496
+ const contentType = metaEntries.length > 0 ? metaEntries.shift() : 'application/octet-stream';
497
+ const params = {};
498
+
499
+ for (let i = 0; i < metaEntries.length; i++) {
500
+ const entry = metaEntries[i];
501
+ const sepPos = entry.indexOf('=');
502
+ if (sepPos > 0) {
503
+ // Ensure there's a key before the '='
504
+ const key = entry.substring(0, sepPos).trim();
505
+ const value = entry.substring(sepPos + 1).trim();
506
+ if (key) {
507
+ params[key] = value;
508
+ }
509
+ }
510
+ }
511
+
512
+ // Decode data based on encoding with proper error handling
513
+ let bufferData;
514
+ try {
515
+ if (encoding === 'base64') {
516
+ bufferData = Buffer.from(data, 'base64');
517
+ } else {
518
+ try {
519
+ bufferData = Buffer.from(decodeURIComponent(data));
520
+ } catch (_decodeError) {
521
+ bufferData = Buffer.from(data);
522
+ }
523
+ }
524
+ } catch (_bufferError) {
525
+ bufferData = Buffer.alloc(0);
526
+ }
527
+
528
+ return {
529
+ data: bufferData,
530
+ encoding: encoding || null,
531
+ contentType: contentType || 'application/octet-stream',
532
+ params
533
+ };
534
+ };
535
+
536
+ /**
537
+ * Resolves a String or a Buffer value for content value. Useful if the value
538
+ * is a Stream or a file or an URL. If the value is a Stream, overwrites
539
+ * the stream object with the resolved value (you can't stream a value twice).
540
+ *
541
+ * This is useful when you want to create a plugin that needs a content value,
542
+ * for example the `html` or `text` value as a String or a Buffer but not as
543
+ * a file path or an URL.
544
+ *
545
+ * @param {Object} data An object or an Array you want to resolve an element for
546
+ * @param {String|Number} key Property name or an Array index
547
+ * @param {Function} callback Callback function with (err, value)
548
+ */
549
+ module.exports.resolveContent = (data, key, callback) => {
550
+ let promise;
551
+
552
+ if (!callback) {
553
+ promise = new Promise((resolve, reject) => {
554
+ callback = module.exports.callbackPromise(resolve, reject);
555
+ });
556
+ }
557
+
558
+ let content = (data && data[key] && data[key].content) || data[key];
559
+ let contentStream;
560
+ let encoding = ((typeof data[key] === 'object' && data[key].encoding) || 'utf8')
561
+ .toString()
562
+ .toLowerCase()
563
+ .replace(/[-_\s]/g, '');
564
+
565
+ if (!content) {
566
+ return callback(null, content);
567
+ }
568
+
569
+ if (typeof content === 'object') {
570
+ if (typeof content.pipe === 'function') {
571
+ return resolveStream(content, (err, value) => {
572
+ if (err) {
573
+ return callback(err);
574
+ }
575
+ // we can't stream twice the same content, so we need
576
+ // to replace the stream object with the streaming result
577
+ if (data[key].content) {
578
+ data[key].content = value;
579
+ } else {
580
+ data[key] = value;
581
+ }
582
+ callback(null, value);
583
+ });
584
+ } else if (/^https?:\/\//i.test(content.path || content.href)) {
585
+ contentStream = nmfetch(content.path || content.href);
586
+ return resolveStream(contentStream, callback);
587
+ } else if (/^data:/i.test(content.path || content.href)) {
588
+ let parsedDataUri = module.exports.parseDataURI(content.path || content.href);
589
+
590
+ if (!parsedDataUri || !parsedDataUri.data) {
591
+ return callback(null, Buffer.from(0));
592
+ }
593
+ return callback(null, parsedDataUri.data);
594
+ } else if (content.path) {
595
+ return resolveStream(fs.createReadStream(content.path), callback);
596
+ }
597
+ }
598
+
599
+ if (typeof data[key].content === 'string' && !['utf8', 'usascii', 'ascii'].includes(encoding)) {
600
+ content = Buffer.from(data[key].content, encoding);
601
+ }
602
+
603
+ // default action, return as is
604
+ setImmediate(() => callback(null, content));
605
+
606
+ return promise;
607
+ };
608
+
609
+ /**
610
+ * Copies properties from source objects to target objects
611
+ */
612
+ module.exports.assign = function (/* target, ... sources */) {
613
+ let args = Array.from(arguments);
614
+ let target = args.shift() || {};
615
+
616
+ args.forEach(source => {
617
+ Object.keys(source || {}).forEach(key => {
618
+ if (['tls', 'auth'].includes(key) && source[key] && typeof source[key] === 'object') {
619
+ // tls and auth are special keys that need to be enumerated separately
620
+ // other objects are passed as is
621
+ if (!target[key]) {
622
+ // ensure that target has this key
623
+ target[key] = {};
624
+ }
625
+ Object.keys(source[key]).forEach(subKey => {
626
+ target[key][subKey] = source[key][subKey];
627
+ });
628
+ } else {
629
+ target[key] = source[key];
630
+ }
631
+ });
632
+ });
633
+ return target;
634
+ };
635
+
636
+ module.exports.encodeXText = str => {
637
+ // ! 0x21
638
+ // + 0x2B
639
+ // = 0x3D
640
+ // ~ 0x7E
641
+ if (!/[^\x21-\x2A\x2C-\x3C\x3E-\x7E]/.test(str)) {
642
+ return str;
643
+ }
644
+ let buf = Buffer.from(str);
645
+ let result = '';
646
+ for (let i = 0, len = buf.length; i < len; i++) {
647
+ let c = buf[i];
648
+ if (c < 0x21 || c > 0x7e || c === 0x2b || c === 0x3d) {
649
+ result += '+' + (c < 0x10 ? '0' : '') + c.toString(16).toUpperCase();
650
+ } else {
651
+ result += String.fromCharCode(c);
652
+ }
653
+ }
654
+ return result;
655
+ };
656
+
657
+ /**
658
+ * Streams a stream value into a Buffer
659
+ *
660
+ * @param {Object} stream Readable stream
661
+ * @param {Function} callback Callback function with (err, value)
662
+ */
663
+ function resolveStream(stream, callback) {
664
+ let responded = false;
665
+ let chunks = [];
666
+ let chunklen = 0;
667
+
668
+ stream.on('error', err => {
669
+ if (responded) {
670
+ return;
671
+ }
672
+
673
+ responded = true;
674
+ callback(err);
675
+ });
676
+
677
+ stream.on('readable', () => {
678
+ let chunk;
679
+ while ((chunk = stream.read()) !== null) {
680
+ chunks.push(chunk);
681
+ chunklen += chunk.length;
682
+ }
683
+ });
684
+
685
+ stream.on('end', () => {
686
+ if (responded) {
687
+ return;
688
+ }
689
+ responded = true;
690
+
691
+ let value;
692
+
693
+ try {
694
+ value = Buffer.concat(chunks, chunklen);
695
+ } catch (E) {
696
+ return callback(E);
697
+ }
698
+ callback(null, value);
699
+ });
700
+ }
701
+
702
+ /**
703
+ * Generates a bunyan-like logger that prints to console
704
+ *
705
+ * @returns {Object} Bunyan logger instance
706
+ */
707
+ function createDefaultLogger(levels) {
708
+ let levelMaxLen = 0;
709
+ let levelNames = new Map();
710
+ levels.forEach(level => {
711
+ if (level.length > levelMaxLen) {
712
+ levelMaxLen = level.length;
713
+ }
714
+ });
715
+
716
+ levels.forEach(level => {
717
+ let levelName = level.toUpperCase();
718
+ if (levelName.length < levelMaxLen) {
719
+ levelName += ' '.repeat(levelMaxLen - levelName.length);
720
+ }
721
+ levelNames.set(level, levelName);
722
+ });
723
+
724
+ let print = (level, entry, message, ...args) => {
725
+ let prefix = '';
726
+ if (entry) {
727
+ if (entry.tnx === 'server') {
728
+ prefix = 'S: ';
729
+ } else if (entry.tnx === 'client') {
730
+ prefix = 'C: ';
731
+ }
732
+
733
+ if (entry.sid) {
734
+ prefix = '[' + entry.sid + '] ' + prefix;
735
+ }
736
+
737
+ if (entry.cid) {
738
+ prefix = '[#' + entry.cid + '] ' + prefix;
739
+ }
740
+ }
741
+
742
+ message = util.format(message, ...args);
743
+ message.split(/\r?\n/).forEach(line => {
744
+ console.log('[%s] %s %s', new Date().toISOString().substr(0, 19).replace(/T/, ' '), levelNames.get(level), prefix + line);
745
+ });
746
+ };
747
+
748
+ let logger = {};
749
+ levels.forEach(level => {
750
+ logger[level] = print.bind(null, level);
751
+ });
752
+
753
+ return logger;
754
+ }