@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,460 @@
1
+ /*
2
+
3
+ Copied from https://github.com/mathiasbynens/punycode.js/blob/ef3505c8abb5143a00d53ce59077c9f7f4b2ac47/punycode.js
4
+
5
+ Copyright Mathias Bynens <https://mathiasbynens.be/>
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ */
27
+ /* eslint callback-return: 0, no-bitwise: 0, eqeqeq: 0, prefer-arrow-callback: 0, object-shorthand: 0 */
28
+
29
+ 'use strict';
30
+
31
+ /** Highest positive signed 32-bit float value */
32
+ const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1
33
+
34
+ /** Bootstring parameters */
35
+ const base = 36;
36
+ const tMin = 1;
37
+ const tMax = 26;
38
+ const skew = 38;
39
+ const damp = 700;
40
+ const initialBias = 72;
41
+ const initialN = 128; // 0x80
42
+ const delimiter = '-'; // '\x2D'
43
+
44
+ /** Regular expressions */
45
+ const regexPunycode = /^xn--/;
46
+ const regexNonASCII = /[^\0-\x7F]/; // Note: U+007F DEL is excluded too.
47
+ const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators
48
+
49
+ /** Error messages */
50
+ const errors = {
51
+ overflow: 'Overflow: input needs wider integers to process',
52
+ 'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
53
+ 'invalid-input': 'Invalid input'
54
+ };
55
+
56
+ /** Convenience shortcuts */
57
+ const baseMinusTMin = base - tMin;
58
+ const floor = Math.floor;
59
+ const stringFromCharCode = String.fromCharCode;
60
+
61
+ /*--------------------------------------------------------------------------*/
62
+
63
+ /**
64
+ * A generic error utility function.
65
+ * @private
66
+ * @param {String} type The error type.
67
+ * @returns {Error} Throws a `RangeError` with the applicable error message.
68
+ */
69
+ function error(type) {
70
+ throw new RangeError(errors[type]);
71
+ }
72
+
73
+ /**
74
+ * A generic `Array#map` utility function.
75
+ * @private
76
+ * @param {Array} array The array to iterate over.
77
+ * @param {Function} callback The function that gets called for every array
78
+ * item.
79
+ * @returns {Array} A new array of values returned by the callback function.
80
+ */
81
+ function map(array, callback) {
82
+ const result = [];
83
+ let length = array.length;
84
+ while (length--) {
85
+ result[length] = callback(array[length]);
86
+ }
87
+ return result;
88
+ }
89
+
90
+ /**
91
+ * A simple `Array#map`-like wrapper to work with domain name strings or email
92
+ * addresses.
93
+ * @private
94
+ * @param {String} domain The domain name or email address.
95
+ * @param {Function} callback The function that gets called for every
96
+ * character.
97
+ * @returns {String} A new string of characters returned by the callback
98
+ * function.
99
+ */
100
+ function mapDomain(domain, callback) {
101
+ const parts = domain.split('@');
102
+ let result = '';
103
+ if (parts.length > 1) {
104
+ // In email addresses, only the domain name should be punycoded. Leave
105
+ // the local part (i.e. everything up to `@`) intact.
106
+ result = parts[0] + '@';
107
+ domain = parts[1];
108
+ }
109
+ // Avoid `split(regex)` for IE8 compatibility. See #17.
110
+ domain = domain.replace(regexSeparators, '\x2E');
111
+ const labels = domain.split('.');
112
+ const encoded = map(labels, callback).join('.');
113
+ return result + encoded;
114
+ }
115
+
116
+ /**
117
+ * Creates an array containing the numeric code points of each Unicode
118
+ * character in the string. While JavaScript uses UCS-2 internally,
119
+ * this function will convert a pair of surrogate halves (each of which
120
+ * UCS-2 exposes as separate characters) into a single code point,
121
+ * matching UTF-16.
122
+ * @see `punycode.ucs2.encode`
123
+ * @see <https://mathiasbynens.be/notes/javascript-encoding>
124
+ * @memberOf punycode.ucs2
125
+ * @name decode
126
+ * @param {String} string The Unicode input string (UCS-2).
127
+ * @returns {Array} The new array of code points.
128
+ */
129
+ function ucs2decode(string) {
130
+ const output = [];
131
+ let counter = 0;
132
+ const length = string.length;
133
+ while (counter < length) {
134
+ const value = string.charCodeAt(counter++);
135
+ if (value >= 0xd800 && value <= 0xdbff && counter < length) {
136
+ // It's a high surrogate, and there is a next character.
137
+ const extra = string.charCodeAt(counter++);
138
+ if ((extra & 0xfc00) == 0xdc00) {
139
+ // Low surrogate.
140
+ output.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
141
+ } else {
142
+ // It's an unmatched surrogate; only append this code unit, in case the
143
+ // next code unit is the high surrogate of a surrogate pair.
144
+ output.push(value);
145
+ counter--;
146
+ }
147
+ } else {
148
+ output.push(value);
149
+ }
150
+ }
151
+ return output;
152
+ }
153
+
154
+ /**
155
+ * Creates a string based on an array of numeric code points.
156
+ * @see `punycode.ucs2.decode`
157
+ * @memberOf punycode.ucs2
158
+ * @name encode
159
+ * @param {Array} codePoints The array of numeric code points.
160
+ * @returns {String} The new Unicode string (UCS-2).
161
+ */
162
+ const ucs2encode = codePoints => String.fromCodePoint(...codePoints);
163
+
164
+ /**
165
+ * Converts a basic code point into a digit/integer.
166
+ * @see `digitToBasic()`
167
+ * @private
168
+ * @param {Number} codePoint The basic numeric code point value.
169
+ * @returns {Number} The numeric value of a basic code point (for use in
170
+ * representing integers) in the range `0` to `base - 1`, or `base` if
171
+ * the code point does not represent a value.
172
+ */
173
+ const basicToDigit = function (codePoint) {
174
+ if (codePoint >= 0x30 && codePoint < 0x3a) {
175
+ return 26 + (codePoint - 0x30);
176
+ }
177
+ if (codePoint >= 0x41 && codePoint < 0x5b) {
178
+ return codePoint - 0x41;
179
+ }
180
+ if (codePoint >= 0x61 && codePoint < 0x7b) {
181
+ return codePoint - 0x61;
182
+ }
183
+ return base;
184
+ };
185
+
186
+ /**
187
+ * Converts a digit/integer into a basic code point.
188
+ * @see `basicToDigit()`
189
+ * @private
190
+ * @param {Number} digit The numeric value of a basic code point.
191
+ * @returns {Number} The basic code point whose value (when used for
192
+ * representing integers) is `digit`, which needs to be in the range
193
+ * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
194
+ * used; else, the lowercase form is used. The behavior is undefined
195
+ * if `flag` is non-zero and `digit` has no uppercase form.
196
+ */
197
+ const digitToBasic = function (digit, flag) {
198
+ // 0..25 map to ASCII a..z or A..Z
199
+ // 26..35 map to ASCII 0..9
200
+ return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);
201
+ };
202
+
203
+ /**
204
+ * Bias adaptation function as per section 3.4 of RFC 3492.
205
+ * https://tools.ietf.org/html/rfc3492#section-3.4
206
+ * @private
207
+ */
208
+ const adapt = function (delta, numPoints, firstTime) {
209
+ let k = 0;
210
+ delta = firstTime ? floor(delta / damp) : delta >> 1;
211
+ delta += floor(delta / numPoints);
212
+ for (; /* no initialization */ delta > (baseMinusTMin * tMax) >> 1; k += base) {
213
+ delta = floor(delta / baseMinusTMin);
214
+ }
215
+ return floor(k + ((baseMinusTMin + 1) * delta) / (delta + skew));
216
+ };
217
+
218
+ /**
219
+ * Converts a Punycode string of ASCII-only symbols to a string of Unicode
220
+ * symbols.
221
+ * @memberOf punycode
222
+ * @param {String} input The Punycode string of ASCII-only symbols.
223
+ * @returns {String} The resulting string of Unicode symbols.
224
+ */
225
+ const decode = function (input) {
226
+ // Don't use UCS-2.
227
+ const output = [];
228
+ const inputLength = input.length;
229
+ let i = 0;
230
+ let n = initialN;
231
+ let bias = initialBias;
232
+
233
+ // Handle the basic code points: let `basic` be the number of input code
234
+ // points before the last delimiter, or `0` if there is none, then copy
235
+ // the first basic code points to the output.
236
+
237
+ let basic = input.lastIndexOf(delimiter);
238
+ if (basic < 0) {
239
+ basic = 0;
240
+ }
241
+
242
+ for (let j = 0; j < basic; ++j) {
243
+ // if it's not a basic code point
244
+ if (input.charCodeAt(j) >= 0x80) {
245
+ error('not-basic');
246
+ }
247
+ output.push(input.charCodeAt(j));
248
+ }
249
+
250
+ // Main decoding loop: start just after the last delimiter if any basic code
251
+ // points were copied; start at the beginning otherwise.
252
+
253
+ for (let index = basic > 0 ? basic + 1 : 0; index < inputLength /* no final expression */; ) {
254
+ // `index` is the index of the next character to be consumed.
255
+ // Decode a generalized variable-length integer into `delta`,
256
+ // which gets added to `i`. The overflow checking is easier
257
+ // if we increase `i` as we go, then subtract off its starting
258
+ // value at the end to obtain `delta`.
259
+ const oldi = i;
260
+ for (let w = 1, k = base /* no condition */; ; k += base) {
261
+ if (index >= inputLength) {
262
+ error('invalid-input');
263
+ }
264
+
265
+ const digit = basicToDigit(input.charCodeAt(index++));
266
+
267
+ if (digit >= base) {
268
+ error('invalid-input');
269
+ }
270
+ if (digit > floor((maxInt - i) / w)) {
271
+ error('overflow');
272
+ }
273
+
274
+ i += digit * w;
275
+ const t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias;
276
+
277
+ if (digit < t) {
278
+ break;
279
+ }
280
+
281
+ const baseMinusT = base - t;
282
+ if (w > floor(maxInt / baseMinusT)) {
283
+ error('overflow');
284
+ }
285
+
286
+ w *= baseMinusT;
287
+ }
288
+
289
+ const out = output.length + 1;
290
+ bias = adapt(i - oldi, out, oldi == 0);
291
+
292
+ // `i` was supposed to wrap around from `out` to `0`,
293
+ // incrementing `n` each time, so we'll fix that now:
294
+ if (floor(i / out) > maxInt - n) {
295
+ error('overflow');
296
+ }
297
+
298
+ n += floor(i / out);
299
+ i %= out;
300
+
301
+ // Insert `n` at position `i` of the output.
302
+ output.splice(i++, 0, n);
303
+ }
304
+
305
+ return String.fromCodePoint(...output);
306
+ };
307
+
308
+ /**
309
+ * Converts a string of Unicode symbols (e.g. a domain name label) to a
310
+ * Punycode string of ASCII-only symbols.
311
+ * @memberOf punycode
312
+ * @param {String} input The string of Unicode symbols.
313
+ * @returns {String} The resulting Punycode string of ASCII-only symbols.
314
+ */
315
+ const encode = function (input) {
316
+ const output = [];
317
+
318
+ // Convert the input in UCS-2 to an array of Unicode code points.
319
+ input = ucs2decode(input);
320
+
321
+ // Cache the length.
322
+ const inputLength = input.length;
323
+
324
+ // Initialize the state.
325
+ let n = initialN;
326
+ let delta = 0;
327
+ let bias = initialBias;
328
+
329
+ // Handle the basic code points.
330
+ for (const currentValue of input) {
331
+ if (currentValue < 0x80) {
332
+ output.push(stringFromCharCode(currentValue));
333
+ }
334
+ }
335
+
336
+ const basicLength = output.length;
337
+ let handledCPCount = basicLength;
338
+
339
+ // `handledCPCount` is the number of code points that have been handled;
340
+ // `basicLength` is the number of basic code points.
341
+
342
+ // Finish the basic string with a delimiter unless it's empty.
343
+ if (basicLength) {
344
+ output.push(delimiter);
345
+ }
346
+
347
+ // Main encoding loop:
348
+ while (handledCPCount < inputLength) {
349
+ // All non-basic code points < n have been handled already. Find the next
350
+ // larger one:
351
+ let m = maxInt;
352
+ for (const currentValue of input) {
353
+ if (currentValue >= n && currentValue < m) {
354
+ m = currentValue;
355
+ }
356
+ }
357
+
358
+ // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>,
359
+ // but guard against overflow.
360
+ const handledCPCountPlusOne = handledCPCount + 1;
361
+ if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
362
+ error('overflow');
363
+ }
364
+
365
+ delta += (m - n) * handledCPCountPlusOne;
366
+ n = m;
367
+
368
+ for (const currentValue of input) {
369
+ if (currentValue < n && ++delta > maxInt) {
370
+ error('overflow');
371
+ }
372
+ if (currentValue === n) {
373
+ // Represent delta as a generalized variable-length integer.
374
+ let q = delta;
375
+ for (let k = base /* no condition */; ; k += base) {
376
+ const t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias;
377
+ if (q < t) {
378
+ break;
379
+ }
380
+ const qMinusT = q - t;
381
+ const baseMinusT = base - t;
382
+ output.push(stringFromCharCode(digitToBasic(t + (qMinusT % baseMinusT), 0)));
383
+ q = floor(qMinusT / baseMinusT);
384
+ }
385
+
386
+ output.push(stringFromCharCode(digitToBasic(q, 0)));
387
+ bias = adapt(delta, handledCPCountPlusOne, handledCPCount === basicLength);
388
+ delta = 0;
389
+ ++handledCPCount;
390
+ }
391
+ }
392
+
393
+ ++delta;
394
+ ++n;
395
+ }
396
+ return output.join('');
397
+ };
398
+
399
+ /**
400
+ * Converts a Punycode string representing a domain name or an email address
401
+ * to Unicode. Only the Punycoded parts of the input will be converted, i.e.
402
+ * it doesn't matter if you call it on a string that has already been
403
+ * converted to Unicode.
404
+ * @memberOf punycode
405
+ * @param {String} input The Punycoded domain name or email address to
406
+ * convert to Unicode.
407
+ * @returns {String} The Unicode representation of the given Punycode
408
+ * string.
409
+ */
410
+ const toUnicode = function (input) {
411
+ return mapDomain(input, function (string) {
412
+ return regexPunycode.test(string) ? decode(string.slice(4).toLowerCase()) : string;
413
+ });
414
+ };
415
+
416
+ /**
417
+ * Converts a Unicode string representing a domain name or an email address to
418
+ * Punycode. Only the non-ASCII parts of the domain name will be converted,
419
+ * i.e. it doesn't matter if you call it with a domain that's already in
420
+ * ASCII.
421
+ * @memberOf punycode
422
+ * @param {String} input The domain name or email address to convert, as a
423
+ * Unicode string.
424
+ * @returns {String} The Punycode representation of the given domain name or
425
+ * email address.
426
+ */
427
+ const toASCII = function (input) {
428
+ return mapDomain(input, function (string) {
429
+ return regexNonASCII.test(string) ? 'xn--' + encode(string) : string;
430
+ });
431
+ };
432
+
433
+ /*--------------------------------------------------------------------------*/
434
+
435
+ /** Define the public API */
436
+ const punycode = {
437
+ /**
438
+ * A string representing the current Punycode.js version number.
439
+ * @memberOf punycode
440
+ * @type String
441
+ */
442
+ version: '2.3.1',
443
+ /**
444
+ * An object of methods to convert from JavaScript's internal character
445
+ * representation (UCS-2) to Unicode code points, and back.
446
+ * @see <https://mathiasbynens.be/notes/javascript-encoding>
447
+ * @memberOf punycode
448
+ * @type Object
449
+ */
450
+ ucs2: {
451
+ decode: ucs2decode,
452
+ encode: ucs2encode
453
+ },
454
+ decode: decode,
455
+ encode: encode,
456
+ toASCII: toASCII,
457
+ toUnicode: toUnicode
458
+ };
459
+
460
+ module.exports = punycode;
@@ -0,0 +1,227 @@
1
+ 'use strict';
2
+
3
+ const Transform = require('stream').Transform;
4
+
5
+ /**
6
+ * Encodes a Buffer into a Quoted-Printable encoded string
7
+ *
8
+ * @param {Buffer} buffer Buffer to convert
9
+ * @returns {String} Quoted-Printable encoded string
10
+ */
11
+ function encode(buffer) {
12
+ if (typeof buffer === 'string') {
13
+ buffer = Buffer.from(buffer, 'utf-8');
14
+ }
15
+
16
+ // usable characters that do not need encoding
17
+ let ranges = [
18
+ // https://tools.ietf.org/html/rfc2045#section-6.7
19
+ [0x09], // <TAB>
20
+ [0x0a], // <LF>
21
+ [0x0d], // <CR>
22
+ [0x20, 0x3c], // <SP>!"#$%&'()*+,-./0123456789:;
23
+ [0x3e, 0x7e] // >?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}
24
+ ];
25
+ let result = '';
26
+ let ord;
27
+
28
+ for (let i = 0, len = buffer.length; i < len; i++) {
29
+ ord = buffer[i];
30
+ // if the char is in allowed range, then keep as is, unless it is a WS in the end of a line
31
+ if (
32
+ checkRanges(ord, ranges) &&
33
+ !((ord === 0x20 || ord === 0x09) && (i === len - 1 || buffer[i + 1] === 0x0a || buffer[i + 1] === 0x0d))
34
+ ) {
35
+ result += String.fromCharCode(ord);
36
+ continue;
37
+ }
38
+ result += '=' + (ord < 0x10 ? '0' : '') + ord.toString(16).toUpperCase();
39
+ }
40
+
41
+ return result;
42
+ }
43
+
44
+ /**
45
+ * Adds soft line breaks to a Quoted-Printable string
46
+ *
47
+ * @param {String} str Quoted-Printable encoded string that might need line wrapping
48
+ * @param {Number} [lineLength=76] Maximum allowed length for a line
49
+ * @returns {String} Soft-wrapped Quoted-Printable encoded string
50
+ */
51
+ function wrap(str, lineLength) {
52
+ str = (str || '').toString();
53
+ lineLength = lineLength || 76;
54
+
55
+ if (str.length <= lineLength) {
56
+ return str;
57
+ }
58
+
59
+ let pos = 0;
60
+ let len = str.length;
61
+ let match, code, line;
62
+ let lineMargin = Math.floor(lineLength / 3);
63
+ let result = '';
64
+
65
+ // insert soft linebreaks where needed
66
+ while (pos < len) {
67
+ line = str.substr(pos, lineLength);
68
+ if ((match = line.match(/\r\n/))) {
69
+ line = line.substr(0, match.index + match[0].length);
70
+ result += line;
71
+ pos += line.length;
72
+ continue;
73
+ }
74
+
75
+ if (line.substr(-1) === '\n') {
76
+ // nothing to change here
77
+ result += line;
78
+ pos += line.length;
79
+ continue;
80
+ } else if ((match = line.substr(-lineMargin).match(/\n.*?$/))) {
81
+ // truncate to nearest line break
82
+ line = line.substr(0, line.length - (match[0].length - 1));
83
+ result += line;
84
+ pos += line.length;
85
+ continue;
86
+ } else if (line.length > lineLength - lineMargin && (match = line.substr(-lineMargin).match(/[ \t.,!?][^ \t.,!?]*$/))) {
87
+ // truncate to nearest space
88
+ line = line.substr(0, line.length - (match[0].length - 1));
89
+ } else if (line.match(/[=][\da-f]{0,2}$/i)) {
90
+ // push incomplete encoding sequences to the next line
91
+ if ((match = line.match(/[=][\da-f]{0,1}$/i))) {
92
+ line = line.substr(0, line.length - match[0].length);
93
+ }
94
+
95
+ // ensure that utf-8 sequences are not split
96
+ while (
97
+ line.length > 3 &&
98
+ line.length < len - pos &&
99
+ !line.match(/^(?:=[\da-f]{2}){1,4}$/i) &&
100
+ (match = line.match(/[=][\da-f]{2}$/gi))
101
+ ) {
102
+ code = parseInt(match[0].substr(1, 2), 16);
103
+ if (code < 128) {
104
+ break;
105
+ }
106
+
107
+ line = line.substr(0, line.length - 3);
108
+
109
+ if (code >= 0xc0) {
110
+ break;
111
+ }
112
+ }
113
+ }
114
+
115
+ if (pos + line.length < len && line.substr(-1) !== '\n') {
116
+ if (line.length === lineLength && line.match(/[=][\da-f]{2}$/i)) {
117
+ line = line.substr(0, line.length - 3);
118
+ } else if (line.length === lineLength) {
119
+ line = line.substr(0, line.length - 1);
120
+ }
121
+ pos += line.length;
122
+ line += '=\r\n';
123
+ } else {
124
+ pos += line.length;
125
+ }
126
+
127
+ result += line;
128
+ }
129
+
130
+ return result;
131
+ }
132
+
133
+ /**
134
+ * Helper function to check if a number is inside provided ranges
135
+ *
136
+ * @param {Number} nr Number to check for
137
+ * @param {Array} ranges An Array of allowed values
138
+ * @returns {Boolean} True if the value was found inside allowed ranges, false otherwise
139
+ */
140
+ function checkRanges(nr, ranges) {
141
+ for (let i = ranges.length - 1; i >= 0; i--) {
142
+ if (!ranges[i].length) {
143
+ continue;
144
+ }
145
+ if (ranges[i].length === 1 && nr === ranges[i][0]) {
146
+ return true;
147
+ }
148
+ if (ranges[i].length === 2 && nr >= ranges[i][0] && nr <= ranges[i][1]) {
149
+ return true;
150
+ }
151
+ }
152
+ return false;
153
+ }
154
+
155
+ /**
156
+ * Creates a transform stream for encoding data to Quoted-Printable encoding
157
+ *
158
+ * @constructor
159
+ * @param {Object} options Stream options
160
+ * @param {Number} [options.lineLength=76] Maximum length for lines, set to false to disable wrapping
161
+ */
162
+ class Encoder extends Transform {
163
+ constructor(options) {
164
+ super();
165
+
166
+ // init Transform
167
+ this.options = options || {};
168
+
169
+ if (this.options.lineLength !== false) {
170
+ this.options.lineLength = this.options.lineLength || 76;
171
+ }
172
+
173
+ this._curLine = '';
174
+
175
+ this.inputBytes = 0;
176
+ this.outputBytes = 0;
177
+ }
178
+
179
+ _transform(chunk, encoding, done) {
180
+ let qp;
181
+
182
+ if (encoding !== 'buffer') {
183
+ chunk = Buffer.from(chunk, encoding);
184
+ }
185
+
186
+ if (!chunk || !chunk.length) {
187
+ return done();
188
+ }
189
+
190
+ this.inputBytes += chunk.length;
191
+
192
+ if (this.options.lineLength) {
193
+ qp = this._curLine + encode(chunk);
194
+ qp = wrap(qp, this.options.lineLength);
195
+ qp = qp.replace(/(^|\n)([^\n]*)$/, (match, lineBreak, lastLine) => {
196
+ this._curLine = lastLine;
197
+ return lineBreak;
198
+ });
199
+
200
+ if (qp) {
201
+ this.outputBytes += qp.length;
202
+ this.push(qp);
203
+ }
204
+ } else {
205
+ qp = encode(chunk);
206
+ this.outputBytes += qp.length;
207
+ this.push(qp, 'ascii');
208
+ }
209
+
210
+ done();
211
+ }
212
+
213
+ _flush(done) {
214
+ if (this._curLine) {
215
+ this.outputBytes += this._curLine.length;
216
+ this.push(this._curLine, 'ascii');
217
+ }
218
+ done();
219
+ }
220
+ }
221
+
222
+ // expose to the world
223
+ module.exports = {
224
+ encode,
225
+ wrap,
226
+ Encoder
227
+ };