@nocobase/plugin-workflow-mailer 2.3.0-beta.13 → 2.3.0-beta.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/externalVersion.js +6 -6
- package/dist/node_modules/joi/package.json +1 -1
- package/dist/node_modules/nodemailer/dist/cjs/addressparser/index.d.ts +59 -0
- package/dist/node_modules/nodemailer/dist/cjs/addressparser/index.js +790 -0
- package/dist/node_modules/nodemailer/dist/cjs/base64/index.d.ts +36 -0
- package/dist/node_modules/nodemailer/dist/cjs/base64/index.js +119 -0
- package/dist/node_modules/nodemailer/dist/cjs/dkim/index.d.ts +52 -0
- package/dist/node_modules/nodemailer/dist/cjs/dkim/index.js +228 -0
- package/dist/node_modules/nodemailer/dist/cjs/dkim/message-parser.d.ts +48 -0
- package/dist/node_modules/nodemailer/dist/cjs/dkim/message-parser.js +180 -0
- package/dist/node_modules/nodemailer/dist/cjs/dkim/relaxed-body.d.ts +36 -0
- package/dist/node_modules/nodemailer/dist/cjs/dkim/relaxed-body.js +148 -0
- package/dist/node_modules/nodemailer/dist/cjs/dkim/sign.d.ts +52 -0
- package/dist/node_modules/nodemailer/dist/cjs/dkim/sign.js +165 -0
- package/dist/node_modules/nodemailer/dist/cjs/errors.d.ts +96 -0
- package/dist/node_modules/nodemailer/dist/cjs/errors.js +70 -0
- package/dist/node_modules/nodemailer/dist/cjs/fetch/cookies.d.ts +107 -0
- package/dist/node_modules/nodemailer/dist/cjs/fetch/cookies.js +282 -0
- package/dist/node_modules/nodemailer/dist/cjs/fetch/index.d.ts +65 -0
- package/dist/node_modules/nodemailer/dist/cjs/fetch/index.js +384 -0
- package/dist/node_modules/nodemailer/dist/cjs/json-transport/index.d.ts +64 -0
- package/dist/node_modules/nodemailer/dist/cjs/json-transport/index.js +99 -0
- package/dist/node_modules/nodemailer/dist/cjs/mail-composer/index.d.ts +174 -0
- package/dist/node_modules/nodemailer/dist/cjs/mail-composer/index.js +613 -0
- package/dist/node_modules/nodemailer/dist/cjs/mailer/index.d.ts +237 -0
- package/dist/node_modules/nodemailer/dist/cjs/mailer/index.js +415 -0
- package/dist/node_modules/nodemailer/dist/cjs/mailer/mail-message.d.ts +97 -0
- package/dist/node_modules/nodemailer/dist/cjs/mailer/mail-message.js +342 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-funcs/index.d.ts +161 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-funcs/index.js +787 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-funcs/mime-types.d.ts +2 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-funcs/mime-types.js +2103 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-node/index.d.ts +333 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-node/index.js +1493 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-node/last-newline.d.ts +5 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-node/last-newline.js +32 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-node/le-unix.d.ts +9 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-node/le-unix.js +40 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-node/le-windows.d.ts +10 -0
- package/dist/node_modules/nodemailer/dist/cjs/mime-node/le-windows.js +47 -0
- package/dist/node_modules/nodemailer/dist/cjs/nodemailer.d.ts +124 -0
- package/dist/node_modules/nodemailer/dist/cjs/nodemailer.js +1 -0
- package/dist/node_modules/nodemailer/dist/cjs/package-info.d.ts +3 -0
- package/dist/node_modules/nodemailer/dist/cjs/package-info.js +7 -0
- package/dist/node_modules/nodemailer/dist/cjs/package.json +3 -0
- package/dist/node_modules/nodemailer/dist/cjs/punycode/index.d.ts +73 -0
- package/dist/node_modules/nodemailer/{lib → dist/cjs}/punycode/index.js +62 -111
- package/dist/node_modules/nodemailer/dist/cjs/qp/index.d.ts +32 -0
- package/dist/node_modules/nodemailer/dist/cjs/qp/index.js +207 -0
- package/dist/node_modules/nodemailer/dist/cjs/sendmail-transport/index.d.ts +69 -0
- package/dist/node_modules/nodemailer/dist/cjs/sendmail-transport/index.js +210 -0
- package/dist/node_modules/nodemailer/dist/cjs/ses-transport/index.d.ts +112 -0
- package/dist/node_modules/nodemailer/dist/cjs/ses-transport/index.js +252 -0
- package/dist/node_modules/nodemailer/dist/cjs/shared/index.d.ts +260 -0
- package/dist/node_modules/nodemailer/dist/cjs/shared/index.js +680 -0
- package/dist/node_modules/nodemailer/dist/cjs/shared/objects.d.ts +23 -0
- package/dist/node_modules/nodemailer/dist/cjs/shared/objects.js +43 -0
- package/dist/node_modules/nodemailer/dist/cjs/shared/url.d.ts +21 -0
- package/dist/node_modules/nodemailer/dist/cjs/shared/url.js +255 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-connection/data-stream.d.ts +15 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-connection/data-stream.js +108 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-connection/http-proxy-client.d.ts +47 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-connection/http-proxy-client.js +189 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-connection/index.d.ts +416 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-connection/index.js +1837 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-pool/index.d.ts +133 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-pool/index.js +569 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-pool/pool-resource.d.ts +62 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-pool/pool-resource.js +257 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-transport/index.d.ts +156 -0
- package/dist/node_modules/nodemailer/dist/cjs/smtp-transport/index.js +371 -0
- package/dist/node_modules/nodemailer/dist/cjs/stream-transport/index.d.ts +65 -0
- package/dist/node_modules/nodemailer/dist/cjs/stream-transport/index.js +146 -0
- package/dist/node_modules/nodemailer/dist/cjs/well-known/index.d.ts +35 -0
- package/dist/node_modules/nodemailer/dist/cjs/well-known/index.js +41 -0
- package/dist/node_modules/nodemailer/dist/cjs/well-known/services.d.ts +2 -0
- package/dist/node_modules/nodemailer/dist/cjs/well-known/services.js +674 -0
- package/dist/node_modules/nodemailer/dist/cjs/xoauth2/index.d.ts +201 -0
- package/dist/node_modules/nodemailer/dist/cjs/xoauth2/index.js +426 -0
- package/dist/node_modules/nodemailer/dist/esm/addressparser/index.d.ts +59 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/addressparser/index.js +361 -131
- package/dist/node_modules/nodemailer/dist/esm/base64/index.d.ts +36 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/base64/index.js +31 -56
- package/dist/node_modules/nodemailer/dist/esm/dkim/index.d.ts +52 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/dkim/index.js +49 -78
- package/dist/node_modules/nodemailer/dist/esm/dkim/message-parser.d.ts +48 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/dkim/message-parser.js +64 -42
- package/dist/node_modules/nodemailer/dist/esm/dkim/relaxed-body.d.ts +36 -0
- package/dist/node_modules/nodemailer/dist/esm/dkim/relaxed-body.js +141 -0
- package/dist/node_modules/nodemailer/dist/esm/dkim/sign.d.ts +52 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/dkim/sign.js +50 -46
- package/dist/node_modules/nodemailer/dist/esm/errors.d.ts +96 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/errors.js +26 -18
- package/dist/node_modules/nodemailer/dist/esm/fetch/cookies.d.ts +107 -0
- package/dist/node_modules/nodemailer/dist/esm/fetch/cookies.js +242 -0
- package/dist/node_modules/nodemailer/dist/esm/fetch/index.d.ts +65 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/fetch/index.js +80 -147
- package/dist/node_modules/nodemailer/dist/esm/json-transport/index.d.ts +64 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/json-transport/index.js +19 -39
- package/dist/node_modules/nodemailer/dist/esm/mail-composer/index.d.ts +174 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mail-composer/index.js +199 -265
- package/dist/node_modules/nodemailer/dist/esm/mailer/index.d.ts +237 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mailer/index.js +128 -234
- package/dist/node_modules/nodemailer/dist/esm/mailer/mail-message.d.ts +97 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mailer/mail-message.js +62 -114
- package/dist/node_modules/nodemailer/dist/esm/mime-funcs/index.d.ts +161 -0
- package/dist/node_modules/nodemailer/dist/esm/mime-funcs/index.js +738 -0
- package/dist/node_modules/nodemailer/dist/esm/mime-funcs/mime-types.d.ts +2 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mime-funcs/mime-types.js +29 -42
- package/dist/node_modules/nodemailer/dist/esm/mime-node/index.d.ts +333 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mime-node/index.js +259 -372
- package/dist/node_modules/nodemailer/dist/esm/mime-node/last-newline.d.ts +5 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mime-node/last-newline.js +4 -10
- package/dist/node_modules/nodemailer/dist/esm/mime-node/le-unix.d.ts +9 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mime-node/le-unix.js +6 -11
- package/dist/node_modules/nodemailer/dist/esm/mime-node/le-windows.d.ts +10 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mime-node/le-windows.js +6 -13
- package/dist/node_modules/nodemailer/dist/esm/nodemailer.d.ts +124 -0
- package/dist/node_modules/nodemailer/dist/esm/nodemailer.js +166 -0
- package/dist/node_modules/nodemailer/dist/esm/package-info.d.ts +3 -0
- package/dist/node_modules/nodemailer/dist/esm/package-info.js +4 -0
- package/dist/node_modules/nodemailer/dist/esm/package.json +3 -0
- package/dist/node_modules/nodemailer/dist/esm/punycode/index.d.ts +73 -0
- package/dist/node_modules/nodemailer/dist/esm/punycode/index.js +405 -0
- package/dist/node_modules/nodemailer/dist/esm/qp/index.d.ts +32 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/qp/index.js +38 -67
- package/dist/node_modules/nodemailer/dist/esm/sendmail-transport/index.d.ts +69 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/sendmail-transport/index.js +51 -101
- package/dist/node_modules/nodemailer/dist/esm/ses-transport/index.d.ts +112 -0
- package/dist/node_modules/nodemailer/dist/esm/ses-transport/index.js +212 -0
- package/dist/node_modules/nodemailer/dist/esm/shared/index.d.ts +260 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/shared/index.js +156 -272
- package/dist/node_modules/nodemailer/dist/esm/shared/objects.d.ts +23 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/shared/objects.js +13 -17
- package/dist/node_modules/nodemailer/dist/esm/shared/url.d.ts +21 -0
- package/dist/node_modules/nodemailer/dist/esm/shared/url.js +214 -0
- package/dist/node_modules/nodemailer/dist/esm/smtp-connection/data-stream.d.ts +15 -0
- package/dist/node_modules/nodemailer/dist/esm/smtp-connection/data-stream.js +103 -0
- package/dist/node_modules/nodemailer/dist/esm/smtp-connection/http-proxy-client.d.ts +47 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/smtp-connection/http-proxy-client.js +49 -74
- package/dist/node_modules/nodemailer/dist/esm/smtp-connection/index.d.ts +416 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/smtp-connection/index.js +574 -794
- package/dist/node_modules/nodemailer/dist/esm/smtp-pool/index.d.ts +133 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/smtp-pool/index.js +172 -284
- package/dist/node_modules/nodemailer/dist/esm/smtp-pool/pool-resource.d.ts +62 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/smtp-pool/pool-resource.js +57 -97
- package/dist/node_modules/nodemailer/dist/esm/smtp-transport/index.d.ts +156 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/smtp-transport/index.js +81 -179
- package/dist/node_modules/nodemailer/dist/esm/stream-transport/index.d.ts +65 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/stream-transport/index.js +36 -74
- package/dist/node_modules/nodemailer/dist/esm/well-known/index.d.ts +35 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/well-known/index.js +5 -16
- package/dist/node_modules/nodemailer/dist/esm/well-known/services.d.ts +2 -0
- package/dist/node_modules/nodemailer/dist/esm/well-known/services.js +671 -0
- package/dist/node_modules/nodemailer/dist/esm/xoauth2/index.d.ts +201 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/xoauth2/index.js +125 -187
- package/dist/node_modules/nodemailer/{lib → dist}/well-known/services.json +9 -2
- package/dist/node_modules/nodemailer/package.json +1 -1
- package/package.json +3 -3
- package/dist/node_modules/nodemailer/.gitattributes +0 -6
- package/dist/node_modules/nodemailer/.ncurc.js +0 -9
- package/dist/node_modules/nodemailer/.prettierignore +0 -8
- package/dist/node_modules/nodemailer/.prettierrc +0 -12
- package/dist/node_modules/nodemailer/.prettierrc.js +0 -10
- package/dist/node_modules/nodemailer/.release-please-config.json +0 -9
- package/dist/node_modules/nodemailer/eslint.config.js +0 -88
- package/dist/node_modules/nodemailer/lib/dkim/relaxed-body.js +0 -154
- package/dist/node_modules/nodemailer/lib/fetch/cookies.js +0 -276
- package/dist/node_modules/nodemailer/lib/mime-funcs/index.js +0 -671
- package/dist/node_modules/nodemailer/lib/nodemailer.js +0 -1
- package/dist/node_modules/nodemailer/lib/ses-transport/index.js +0 -266
- package/dist/node_modules/nodemailer/lib/shared/url.js +0 -151
- package/dist/node_modules/nodemailer/lib/smtp-connection/data-stream.js +0 -105
|
@@ -1,671 +0,0 @@
|
|
|
1
|
-
/* eslint no-control-regex:0 */
|
|
2
|
-
|
|
3
|
-
'use strict';
|
|
4
|
-
|
|
5
|
-
const base64 = require('../base64');
|
|
6
|
-
const qp = require('../qp');
|
|
7
|
-
const mimeTypes = require('./mime-types');
|
|
8
|
-
const { isProtoKey } = require('../shared/objects');
|
|
9
|
-
|
|
10
|
-
module.exports = {
|
|
11
|
-
/**
|
|
12
|
-
* Checks if a value is plaintext string (uses only printable 7bit chars)
|
|
13
|
-
*
|
|
14
|
-
* When isParam is set the value is destined for a header parameter, so HT, CR and LF
|
|
15
|
-
* are not plaintext either: a header parameter has no way to carry them. HT is a valid
|
|
16
|
-
* fold point, so folding and unfolding a header would rewrite it as a space, and CR/LF
|
|
17
|
-
* cannot appear in a header value at all. DEL is neither a token character nor qtext,
|
|
18
|
-
* so it can not be carried bare or quoted. Such values have to go through the rfc2231
|
|
19
|
-
* parameter continuation encoding instead, the same way a quote already does.
|
|
20
|
-
*
|
|
21
|
-
* @param {String} value String to be tested
|
|
22
|
-
* @param {Boolean} [isParam] Set to true if the value is a header parameter value
|
|
23
|
-
* @returns {Boolean} true if it is a plaintext string
|
|
24
|
-
*/
|
|
25
|
-
isPlainText(value, isParam) {
|
|
26
|
-
const re = isParam ? /[\x00-\x1f\x7f"\u0080-\uFFFF]/ : /[\x00-\x08\x0b\x0c\x0e-\x1f\u0080-\uFFFF]/;
|
|
27
|
-
return typeof value === 'string' && !re.test(value);
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Wraps a value into a quoted-string. Inside one a quote would end the string early
|
|
32
|
-
* and a backslash would escape whatever follows it, so both go out as quoted-pairs.
|
|
33
|
-
*
|
|
34
|
-
* @param {String} value String to be quoted
|
|
35
|
-
* @returns {String} The value as a quoted-string, quotes included
|
|
36
|
-
*/
|
|
37
|
-
quoteString(value) {
|
|
38
|
-
return '"' + (value || '').toString().replace(/["\\]/g, '\\$&') + '"';
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Checks if a multi line string containes lines longer than the selected value.
|
|
43
|
-
*
|
|
44
|
-
* Useful when detecting if a mail message needs any processing at all –
|
|
45
|
-
* if only plaintext characters are used and lines are short, then there is
|
|
46
|
-
* no need to encode the values in any way. If the value is plaintext but has
|
|
47
|
-
* longer lines then allowed, then use format=flowed
|
|
48
|
-
*
|
|
49
|
-
* @param {Number} lineLength Max line length to check for
|
|
50
|
-
* @returns {Boolean} Returns true if there is at least one line longer than lineLength chars
|
|
51
|
-
*/
|
|
52
|
-
hasLongerLines(str, lineLength) {
|
|
53
|
-
if (str.length > 128 * 1024) {
|
|
54
|
-
// do not test strings longer than 128kB
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
return new RegExp('^.{' + (lineLength + 1) + ',}', 'm').test(str);
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Encodes a string or an Buffer to an UTF-8 MIME Word (rfc2047)
|
|
62
|
-
*
|
|
63
|
-
* @param {String|Buffer} data String to be encoded
|
|
64
|
-
* @param {String} mimeWordEncoding='Q' Encoding for the mime word, either Q or B
|
|
65
|
-
* @param {Number} [maxLength=0] If set, split mime words into several chunks if needed
|
|
66
|
-
* @return {String} Single or several mime words joined together
|
|
67
|
-
*/
|
|
68
|
-
encodeWord(data, mimeWordEncoding, maxLength) {
|
|
69
|
-
mimeWordEncoding = (mimeWordEncoding || 'Q').toString().toUpperCase().trim().charAt(0);
|
|
70
|
-
maxLength = maxLength || 0;
|
|
71
|
-
|
|
72
|
-
let encodedStr;
|
|
73
|
-
const toCharset = 'UTF-8';
|
|
74
|
-
|
|
75
|
-
if (maxLength && maxLength > 7 + toCharset.length) {
|
|
76
|
-
maxLength -= 7 + toCharset.length;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (mimeWordEncoding === 'Q') {
|
|
80
|
-
// https://tools.ietf.org/html/rfc2047#section-5 rule (3)
|
|
81
|
-
encodedStr = qp.encode(data).replace(/[^a-z0-9!*+\-/=]/gi, chr => {
|
|
82
|
-
const ord = chr.charCodeAt(0).toString(16).toUpperCase();
|
|
83
|
-
if (chr === ' ') {
|
|
84
|
-
return '_';
|
|
85
|
-
}
|
|
86
|
-
return '=' + (ord.length === 1 ? '0' + ord : ord);
|
|
87
|
-
});
|
|
88
|
-
} else if (mimeWordEncoding === 'B') {
|
|
89
|
-
encodedStr = typeof data === 'string' ? data : base64.encode(data);
|
|
90
|
-
maxLength = maxLength ? Math.max(3, ((maxLength - (maxLength % 4)) / 4) * 3) : 0;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (maxLength && (mimeWordEncoding !== 'B' ? encodedStr : base64.encode(data)).length > maxLength) {
|
|
94
|
-
if (mimeWordEncoding === 'Q') {
|
|
95
|
-
encodedStr = this.splitMimeEncodedString(encodedStr, maxLength).join('?= =?' + toCharset + '?' + mimeWordEncoding + '?');
|
|
96
|
-
} else {
|
|
97
|
-
// RFC2047 6.3 (2) states that encoded-word must include an integral number of characters, so no chopping unicode sequences
|
|
98
|
-
const parts = [];
|
|
99
|
-
let lpart = '';
|
|
100
|
-
for (let i = 0, len = encodedStr.length; i < len; i++) {
|
|
101
|
-
let chr = encodedStr.charAt(i);
|
|
102
|
-
|
|
103
|
-
if (/[\ud800-\udbff]/.test(chr) && /[\udc00-\udfff]/.test(encodedStr.charAt(i + 1))) {
|
|
104
|
-
// leading surrogate, so add the trailing surrogate as well
|
|
105
|
-
// an unpaired one must not swallow the next unit, that would destroy
|
|
106
|
-
// a valid pair following it
|
|
107
|
-
chr += encodedStr.charAt(++i);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// check if we can add this character to the existing string
|
|
111
|
-
// without breaking byte length limit
|
|
112
|
-
if (Buffer.byteLength(lpart + chr) <= maxLength || i === 0) {
|
|
113
|
-
lpart += chr;
|
|
114
|
-
} else {
|
|
115
|
-
// we hit the length limit, so push the existing string and start over
|
|
116
|
-
parts.push(base64.encode(lpart));
|
|
117
|
-
lpart = chr;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
if (lpart) {
|
|
121
|
-
parts.push(base64.encode(lpart));
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (parts.length > 1) {
|
|
125
|
-
encodedStr = parts.join('?= =?' + toCharset + '?' + mimeWordEncoding + '?');
|
|
126
|
-
} else {
|
|
127
|
-
encodedStr = parts.join('');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
} else if (mimeWordEncoding === 'B') {
|
|
131
|
-
encodedStr = base64.encode(data);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return '=?' + toCharset + '?' + mimeWordEncoding + '?' + encodedStr + (encodedStr.substr(-2) === '?=' ? '' : '?=');
|
|
135
|
-
},
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Finds word sequences with non ascii text and converts these to mime words
|
|
139
|
-
*
|
|
140
|
-
* @param {String} value String to be encoded
|
|
141
|
-
* @param {String} mimeWordEncoding='Q' Encoding for the mime word, either Q or B
|
|
142
|
-
* @param {Number} [maxLength=0] If set, split mime words into several chunks if needed
|
|
143
|
-
* @param {Boolean} [encodeAll=false] If true and the value needs encoding then encodes entire string, not just the smallest match
|
|
144
|
-
* @return {String} String with possible mime words
|
|
145
|
-
*/
|
|
146
|
-
encodeWords(value, mimeWordEncoding, maxLength, encodeAll) {
|
|
147
|
-
maxLength = maxLength || 0;
|
|
148
|
-
|
|
149
|
-
// find first word with a non-printable ascii or special symbol in it
|
|
150
|
-
const firstMatch = value.match(/(?:^|\s)([^\s]*["\u0080-\uFFFF])/);
|
|
151
|
-
if (!firstMatch) {
|
|
152
|
-
return value;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (encodeAll) {
|
|
156
|
-
// if it is requested to encode everything or the string contains something that resebles encoded word, then encode everything
|
|
157
|
-
return this.encodeWord(value, mimeWordEncoding, maxLength);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// find the last word with a non-printable ascii in it
|
|
161
|
-
const lastMatch = value.match(/(["\u0080-\uFFFF][^\s]*)[^"\u0080-\uFFFF]*$/);
|
|
162
|
-
if (!lastMatch) {
|
|
163
|
-
// should not happen
|
|
164
|
-
return value;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const startIndex =
|
|
168
|
-
firstMatch.index +
|
|
169
|
-
(
|
|
170
|
-
firstMatch[0].match(/[^\s]/) || {
|
|
171
|
-
index: 0
|
|
172
|
-
}
|
|
173
|
-
).index;
|
|
174
|
-
const endIndex = lastMatch.index + (lastMatch[1] || '').length;
|
|
175
|
-
|
|
176
|
-
return (
|
|
177
|
-
(startIndex ? value.substr(0, startIndex) : '') +
|
|
178
|
-
this.encodeWord(value.substring(startIndex, endIndex), mimeWordEncoding || 'Q', maxLength) +
|
|
179
|
-
(endIndex < value.length ? value.substr(endIndex) : '')
|
|
180
|
-
);
|
|
181
|
-
},
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Joins parsed header value together as 'value; param1=value1; param2=value2'
|
|
185
|
-
* PS: We are following RFC 822 for the list of special characters that we need to keep in quotes.
|
|
186
|
-
* Refer: https://www.w3.org/Protocols/rfc1341/4_Content-Type.html
|
|
187
|
-
* @param {Object} structured Parsed header value
|
|
188
|
-
* @return {String} joined header value
|
|
189
|
-
*/
|
|
190
|
-
buildHeaderValue(structured) {
|
|
191
|
-
const paramsArray = [];
|
|
192
|
-
|
|
193
|
-
Object.keys(structured.params || {}).forEach(key => {
|
|
194
|
-
// filename might include unicode characters so it is a special case
|
|
195
|
-
// other values probably do not
|
|
196
|
-
const value = structured.params[key];
|
|
197
|
-
// a parameter name is a token too and it is emitted without any quoting around it
|
|
198
|
-
const param = key.replace(/[\x00-\x1f\x7f]/g, '');
|
|
199
|
-
if (!this.isPlainText(value, true) || value.length >= 75) {
|
|
200
|
-
this.buildHeaderParam(param, value, 50).forEach(encodedParam => {
|
|
201
|
-
if (!/[\s"\\;:/=(),<>@[\]?]|^[-']|'$/.test(encodedParam.value) || encodedParam.key.substr(-1) === '*') {
|
|
202
|
-
paramsArray.push(encodedParam.key + '=' + encodedParam.value);
|
|
203
|
-
} else {
|
|
204
|
-
paramsArray.push(encodedParam.key + '=' + JSON.stringify(encodedParam.value));
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
} else if (/[\s'"\\;:/=(),<>@[\]?]|^-/.test(value)) {
|
|
208
|
-
paramsArray.push(param + '=' + JSON.stringify(value));
|
|
209
|
-
} else {
|
|
210
|
-
paramsArray.push(param + '=' + value);
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// the value ahead of the parameters is a token, it has no way to carry a control
|
|
215
|
-
// char or DEL and there is no quoting construct around it to escape one into
|
|
216
|
-
const value = typeof structured.value === 'string' ? structured.value.replace(/[\x00-\x1f\x7f]/g, '') : structured.value;
|
|
217
|
-
|
|
218
|
-
return value + (paramsArray.length ? '; ' + paramsArray.join('; ') : '');
|
|
219
|
-
},
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Encodes a string or an Buffer to an UTF-8 Parameter Value Continuation encoding (rfc2231)
|
|
223
|
-
* Useful for splitting long parameter values.
|
|
224
|
-
*
|
|
225
|
-
* For example
|
|
226
|
-
* title="unicode string"
|
|
227
|
-
* becomes
|
|
228
|
-
* title*0*=utf-8''unicode
|
|
229
|
-
* title*1*=%20string
|
|
230
|
-
*
|
|
231
|
-
* @param {String|Buffer} data String to be encoded
|
|
232
|
-
* @param {Number} [maxLength=50] Max length for generated chunks
|
|
233
|
-
* @param {String} [fromCharset='UTF-8'] Source sharacter set
|
|
234
|
-
* @return {Array} A list of encoded keys and headers
|
|
235
|
-
*/
|
|
236
|
-
buildHeaderParam(key, data, maxLength) {
|
|
237
|
-
const list = [];
|
|
238
|
-
let encodedStr = typeof data === 'string' ? data : (data || '').toString();
|
|
239
|
-
let chr;
|
|
240
|
-
let line;
|
|
241
|
-
let startPos = 0;
|
|
242
|
-
let i, len;
|
|
243
|
-
|
|
244
|
-
maxLength = maxLength || 50;
|
|
245
|
-
|
|
246
|
-
// process ascii only text
|
|
247
|
-
if (this.isPlainText(data, true)) {
|
|
248
|
-
// check if conversion is even needed
|
|
249
|
-
if (encodedStr.length <= maxLength) {
|
|
250
|
-
return [
|
|
251
|
-
{
|
|
252
|
-
key,
|
|
253
|
-
value: encodedStr
|
|
254
|
-
}
|
|
255
|
-
];
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
encodedStr = encodedStr.replace(new RegExp('.{' + maxLength + '}', 'g'), str => {
|
|
259
|
-
list.push({
|
|
260
|
-
line: str
|
|
261
|
-
});
|
|
262
|
-
return '';
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
if (encodedStr) {
|
|
266
|
-
list.push({
|
|
267
|
-
line: encodedStr
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
} else {
|
|
271
|
-
if (/[\uD800-\uDBFF]/.test(encodedStr)) {
|
|
272
|
-
// string containts surrogate pairs, so normalize it to an array of bytes
|
|
273
|
-
const encodedStrArr = [];
|
|
274
|
-
for (i = 0, len = encodedStr.length; i < len; i++) {
|
|
275
|
-
chr = encodedStr.charAt(i);
|
|
276
|
-
if (/[\ud800-\udbff]/.test(chr) && /[\udc00-\udfff]/.test(encodedStr.charAt(i + 1))) {
|
|
277
|
-
// an unpaired leading surrogate must not consume the next unit, that
|
|
278
|
-
// would tear apart a valid pair following it
|
|
279
|
-
chr += encodedStr.charAt(i + 1);
|
|
280
|
-
encodedStrArr.push(chr);
|
|
281
|
-
i++;
|
|
282
|
-
} else {
|
|
283
|
-
encodedStrArr.push(chr);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
encodedStr = encodedStrArr;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// first line includes the charset and language info and needs to be encoded
|
|
290
|
-
// even if it does not contain any unicode characters
|
|
291
|
-
line = "utf-8''";
|
|
292
|
-
let encoded = true;
|
|
293
|
-
startPos = 0;
|
|
294
|
-
|
|
295
|
-
// process text with unicode or special chars
|
|
296
|
-
for (i = 0, len = encodedStr.length; i < len; i++) {
|
|
297
|
-
chr = encodedStr[i];
|
|
298
|
-
|
|
299
|
-
if (encoded) {
|
|
300
|
-
chr = this.safeEncodeURIComponent(chr);
|
|
301
|
-
} else {
|
|
302
|
-
// try to urlencode current char
|
|
303
|
-
chr = chr === ' ' ? chr : this.safeEncodeURIComponent(chr);
|
|
304
|
-
// By default it is not required to encode a line, the need
|
|
305
|
-
// only appears when the string contains unicode or special chars
|
|
306
|
-
// in this case we start processing the line over and encode all chars
|
|
307
|
-
if (chr !== encodedStr[i]) {
|
|
308
|
-
// Check if it is even possible to add the encoded char to the line
|
|
309
|
-
// If not, there is no reason to use this line, just push it to the list
|
|
310
|
-
// and start a new line with the char that needs encoding
|
|
311
|
-
if ((this.safeEncodeURIComponent(line) + chr).length >= maxLength) {
|
|
312
|
-
list.push({
|
|
313
|
-
line,
|
|
314
|
-
encoded
|
|
315
|
-
});
|
|
316
|
-
// the line we start here holds an encoded char, so it has to be
|
|
317
|
-
// flagged as one. otherwise it gets an unstarred continuation key
|
|
318
|
-
// and a receiver reads the percent escapes as literal text
|
|
319
|
-
line = '';
|
|
320
|
-
encoded = true;
|
|
321
|
-
} else {
|
|
322
|
-
encoded = true;
|
|
323
|
-
i = startPos;
|
|
324
|
-
line = '';
|
|
325
|
-
continue;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
// if the line is already too long, push it to the list and start a new one
|
|
331
|
-
if ((line + chr).length >= maxLength) {
|
|
332
|
-
list.push({
|
|
333
|
-
line,
|
|
334
|
-
encoded
|
|
335
|
-
});
|
|
336
|
-
line = chr = encodedStr[i] === ' ' ? ' ' : this.safeEncodeURIComponent(encodedStr[i]);
|
|
337
|
-
if (chr === encodedStr[i]) {
|
|
338
|
-
encoded = false;
|
|
339
|
-
startPos = i - 1;
|
|
340
|
-
} else {
|
|
341
|
-
encoded = true;
|
|
342
|
-
}
|
|
343
|
-
} else {
|
|
344
|
-
line += chr;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
if (line) {
|
|
349
|
-
list.push({
|
|
350
|
-
line,
|
|
351
|
-
encoded
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
return list.map((item, i) => ({
|
|
357
|
-
// encoded lines: {name}*{part}*
|
|
358
|
-
// unencoded lines: {name}*{part}
|
|
359
|
-
// if any line needs to be encoded then the first line (part==0) is always encoded
|
|
360
|
-
key: key + '*' + i + (item.encoded ? '*' : ''),
|
|
361
|
-
value: item.line
|
|
362
|
-
}));
|
|
363
|
-
},
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Parses a header value with key=value arguments into a structured
|
|
367
|
-
* object.
|
|
368
|
-
*
|
|
369
|
-
* parseHeaderValue('content-type: text/plain; CHARSET='UTF-8'') ->
|
|
370
|
-
* {
|
|
371
|
-
* 'value': 'text/plain',
|
|
372
|
-
* 'params': {
|
|
373
|
-
* 'charset': 'UTF-8'
|
|
374
|
-
* }
|
|
375
|
-
* }
|
|
376
|
-
*
|
|
377
|
-
* @param {String} str Header value
|
|
378
|
-
* @return {Object} Header value as a parsed structure
|
|
379
|
-
*/
|
|
380
|
-
parseHeaderValue(str) {
|
|
381
|
-
const response = {
|
|
382
|
-
value: false,
|
|
383
|
-
params: {}
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
// Parameter names come from a caller supplied contentType/contentDisposition. A
|
|
387
|
-
// "__proto__" name would target the prototype chain of the params object instead of
|
|
388
|
-
// an own property of it, and read back as Object.prototype, so it is dropped.
|
|
389
|
-
const setParam = (name, value) => {
|
|
390
|
-
if (!isProtoKey(name)) {
|
|
391
|
-
response.params[name] = value;
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
let key = false;
|
|
396
|
-
let value = '';
|
|
397
|
-
let type = 'value';
|
|
398
|
-
let quote = false;
|
|
399
|
-
let escaped = false;
|
|
400
|
-
let chr;
|
|
401
|
-
|
|
402
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
|
403
|
-
chr = str.charAt(i);
|
|
404
|
-
if (type === 'key') {
|
|
405
|
-
if (chr === '=') {
|
|
406
|
-
key = value.trim().toLowerCase();
|
|
407
|
-
type = 'value';
|
|
408
|
-
value = '';
|
|
409
|
-
continue;
|
|
410
|
-
}
|
|
411
|
-
value += chr;
|
|
412
|
-
} else {
|
|
413
|
-
if (escaped) {
|
|
414
|
-
value += chr;
|
|
415
|
-
} else if (chr === '\\') {
|
|
416
|
-
escaped = true;
|
|
417
|
-
continue;
|
|
418
|
-
} else if (quote && chr === quote) {
|
|
419
|
-
quote = false;
|
|
420
|
-
} else if (!quote && chr === '"') {
|
|
421
|
-
quote = chr;
|
|
422
|
-
} else if (!quote && chr === ';') {
|
|
423
|
-
if (key === false) {
|
|
424
|
-
response.value = value.trim();
|
|
425
|
-
} else {
|
|
426
|
-
setParam(key, value.trim());
|
|
427
|
-
}
|
|
428
|
-
type = 'key';
|
|
429
|
-
value = '';
|
|
430
|
-
} else {
|
|
431
|
-
value += chr;
|
|
432
|
-
}
|
|
433
|
-
escaped = false;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
if (type === 'value') {
|
|
438
|
-
if (key === false) {
|
|
439
|
-
response.value = value.trim();
|
|
440
|
-
} else {
|
|
441
|
-
setParam(key, value.trim());
|
|
442
|
-
}
|
|
443
|
-
} else if (value.trim()) {
|
|
444
|
-
setParam(value.trim().toLowerCase(), '');
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
// handle parameter value continuations
|
|
448
|
-
// https://tools.ietf.org/html/rfc2231#section-3
|
|
449
|
-
|
|
450
|
-
// preprocess values
|
|
451
|
-
Object.keys(response.params).forEach(key => {
|
|
452
|
-
let actualKey, nr, match, value;
|
|
453
|
-
if ((match = key.match(/(\*(\d+)|\*(\d+)\*|\*)$/))) {
|
|
454
|
-
actualKey = key.substr(0, match.index);
|
|
455
|
-
nr = Number(match[2] || match[3]) || 0;
|
|
456
|
-
|
|
457
|
-
if (isProtoKey(actualKey)) {
|
|
458
|
-
// see setParam. Reading it back would yield Object.prototype, which is
|
|
459
|
-
// an object, so the initializer below would be skipped and the write
|
|
460
|
-
// that follows would throw out of a header build the caller can not catch
|
|
461
|
-
delete response.params[key];
|
|
462
|
-
return;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
if (!response.params[actualKey] || typeof response.params[actualKey] !== 'object') {
|
|
466
|
-
response.params[actualKey] = {
|
|
467
|
-
charset: false,
|
|
468
|
-
values: []
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
value = response.params[key];
|
|
473
|
-
|
|
474
|
-
if (nr === 0 && match[0].substr(-1) === '*' && (match = value.match(/^([^']*)'[^']*'(.*)$/))) {
|
|
475
|
-
response.params[actualKey].charset = match[1] || 'iso-8859-1';
|
|
476
|
-
value = match[2];
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
response.params[actualKey].values[nr] = value;
|
|
480
|
-
|
|
481
|
-
// remove the old reference
|
|
482
|
-
delete response.params[key];
|
|
483
|
-
}
|
|
484
|
-
});
|
|
485
|
-
|
|
486
|
-
// concatenate split rfc2231 strings and convert encoded strings to mime encoded words
|
|
487
|
-
Object.keys(response.params).forEach(key => {
|
|
488
|
-
let value;
|
|
489
|
-
if (response.params[key] && Array.isArray(response.params[key].values)) {
|
|
490
|
-
value = response.params[key].values.map(val => val || '').join('');
|
|
491
|
-
|
|
492
|
-
if (response.params[key].charset) {
|
|
493
|
-
// convert "%AB" to "=?charset?Q?=AB?="
|
|
494
|
-
response.params[key] =
|
|
495
|
-
'=?' +
|
|
496
|
-
response.params[key].charset +
|
|
497
|
-
'?Q?' +
|
|
498
|
-
value
|
|
499
|
-
// fix invalidly encoded chars
|
|
500
|
-
.replace(/[=?_\s]/g, s => {
|
|
501
|
-
const c = s.charCodeAt(0).toString(16);
|
|
502
|
-
if (s === ' ') {
|
|
503
|
-
return '_';
|
|
504
|
-
}
|
|
505
|
-
return '%' + (c.length < 2 ? '0' : '') + c;
|
|
506
|
-
})
|
|
507
|
-
// change from urlencoding to percent encoding
|
|
508
|
-
.replace(/%/g, '=') +
|
|
509
|
-
'?=';
|
|
510
|
-
} else {
|
|
511
|
-
response.params[key] = value;
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
});
|
|
515
|
-
|
|
516
|
-
return response;
|
|
517
|
-
},
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* Returns file extension for a content type string. If no suitable extensions
|
|
521
|
-
* are found, 'bin' is used as the default extension
|
|
522
|
-
*
|
|
523
|
-
* @param {String} mimeType Content type to be checked for
|
|
524
|
-
* @return {String} File extension
|
|
525
|
-
*/
|
|
526
|
-
detectExtension: mimeType => mimeTypes.detectExtension(mimeType),
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* Returns content type for a file extension. If no suitable content types
|
|
530
|
-
* are found, 'application/octet-stream' is used as the default content type
|
|
531
|
-
*
|
|
532
|
-
* @param {String} extension Extension to be checked for
|
|
533
|
-
* @return {String} File extension
|
|
534
|
-
*/
|
|
535
|
-
detectMimeType: extension => mimeTypes.detectMimeType(extension),
|
|
536
|
-
|
|
537
|
-
/**
|
|
538
|
-
* Folds long lines, useful for folding header lines (afterSpace=false) and
|
|
539
|
-
* flowed text (afterSpace=true)
|
|
540
|
-
*
|
|
541
|
-
* @param {String} str String to be folded
|
|
542
|
-
* @param {Number} [lineLength=76] Maximum length of a line
|
|
543
|
-
* @param {Boolean} afterSpace If true, leave a space in th end of a line
|
|
544
|
-
* @return {String} String with folded lines
|
|
545
|
-
*/
|
|
546
|
-
foldLines(str, lineLength, afterSpace) {
|
|
547
|
-
str = (str || '').toString();
|
|
548
|
-
lineLength = lineLength || 76;
|
|
549
|
-
|
|
550
|
-
let pos = 0;
|
|
551
|
-
const len = str.length;
|
|
552
|
-
let result = '';
|
|
553
|
-
let line, match;
|
|
554
|
-
|
|
555
|
-
while (pos < len) {
|
|
556
|
-
line = str.substr(pos, lineLength);
|
|
557
|
-
if (line.length < lineLength) {
|
|
558
|
-
result += line;
|
|
559
|
-
break;
|
|
560
|
-
}
|
|
561
|
-
if ((match = line.match(/^[^\n\r]*(\r?\n|\r)/))) {
|
|
562
|
-
line = match[0];
|
|
563
|
-
result += line;
|
|
564
|
-
pos += line.length;
|
|
565
|
-
continue;
|
|
566
|
-
} else if ((match = line.match(/(\s+)[^\s]*$/)) && match[0].length - (afterSpace ? (match[1] || '').length : 0) < line.length) {
|
|
567
|
-
line = line.substr(0, line.length - (match[0].length - (afterSpace ? (match[1] || '').length : 0)));
|
|
568
|
-
} else if ((match = str.substr(pos + line.length).match(/^[^\s]+(\s*)/))) {
|
|
569
|
-
line = line + match[0].substr(0, match[0].length - (!afterSpace ? (match[1] || '').length : 0));
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
result += line;
|
|
573
|
-
pos += line.length;
|
|
574
|
-
if (pos < len) {
|
|
575
|
-
result += '\r\n';
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
return result;
|
|
580
|
-
},
|
|
581
|
-
|
|
582
|
-
/**
|
|
583
|
-
* Splits a mime encoded string. Needed for dividing mime words into smaller chunks
|
|
584
|
-
*
|
|
585
|
-
* @param {String} str Mime encoded string to be split up
|
|
586
|
-
* @param {Number} maxlen Maximum length of characters for one part (minimum 12)
|
|
587
|
-
* @return {Array} Split string
|
|
588
|
-
*/
|
|
589
|
-
splitMimeEncodedString: (str, maxlen) => {
|
|
590
|
-
const lines = [];
|
|
591
|
-
let curLine, fallbackLine, match, chr, done;
|
|
592
|
-
|
|
593
|
-
// require at least 12 symbols to fit possible 4 octet UTF-8 sequences
|
|
594
|
-
maxlen = Math.max(maxlen || 0, 12);
|
|
595
|
-
|
|
596
|
-
while (str.length) {
|
|
597
|
-
curLine = str.substr(0, maxlen);
|
|
598
|
-
|
|
599
|
-
// move incomplete escaped char back to main
|
|
600
|
-
if ((match = curLine.match(/[=][0-9A-F]?$/i))) {
|
|
601
|
-
curLine = curLine.substr(0, match.index);
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
// Malformed input (a run of stray UTF-8 continuation bytes) has no split point
|
|
605
|
-
// that keeps a character sequence whole, so the loop below walks back to an
|
|
606
|
-
// empty line looking for one. Keep the widest chunk that at least does not cut
|
|
607
|
-
// a "=XX" escape in half, so the part stays a decodable encoded word.
|
|
608
|
-
fallbackLine = curLine.length ? curLine : str.substr(0, maxlen);
|
|
609
|
-
|
|
610
|
-
done = false;
|
|
611
|
-
while (!done && curLine.length) {
|
|
612
|
-
done = true;
|
|
613
|
-
// check if not middle of a unicode char sequence
|
|
614
|
-
if ((match = str.substr(curLine.length).match(/^[=]([0-9A-F]{2})/i))) {
|
|
615
|
-
chr = parseInt(match[1], 16);
|
|
616
|
-
// invalid sequence, move one char back anc recheck
|
|
617
|
-
if (chr < 0xc2 && chr > 0x7f) {
|
|
618
|
-
curLine = curLine.substr(0, curLine.length - 3);
|
|
619
|
-
done = false;
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
if (!curLine.length) {
|
|
625
|
-
curLine = fallbackLine;
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
lines.push(curLine);
|
|
629
|
-
str = str.substr(curLine.length);
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
return lines;
|
|
633
|
-
},
|
|
634
|
-
|
|
635
|
-
encodeURICharComponent: chr => {
|
|
636
|
-
let res = '';
|
|
637
|
-
let ord = chr.charCodeAt(0).toString(16).toUpperCase();
|
|
638
|
-
|
|
639
|
-
if (ord.length % 2) {
|
|
640
|
-
ord = '0' + ord;
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
if (ord.length > 2) {
|
|
644
|
-
for (let i = 0, len = ord.length / 2; i < len; i++) {
|
|
645
|
-
res += '%' + ord.substr(i, 2);
|
|
646
|
-
}
|
|
647
|
-
} else {
|
|
648
|
-
res += '%' + ord;
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
return res;
|
|
652
|
-
},
|
|
653
|
-
|
|
654
|
-
safeEncodeURIComponent(str) {
|
|
655
|
-
str = (str || '').toString();
|
|
656
|
-
|
|
657
|
-
try {
|
|
658
|
-
// might throw if we try to encode invalid sequences, eg. partial emoji
|
|
659
|
-
str = encodeURIComponent(str);
|
|
660
|
-
} catch (_E) {
|
|
661
|
-
// an unpaired surrogate has no utf-8 representation, so run the value through a
|
|
662
|
-
// utf-8 roundtrip to get the same U+FFFD every other encoder here produces and
|
|
663
|
-
// retry. the value must never come back unencoded, it goes into a header parameter
|
|
664
|
-
// where a bare quote or semicolon would break it out into a parameter of its own
|
|
665
|
-
str = encodeURIComponent(Buffer.from(str, 'utf-8').toString('utf-8'));
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
// ensure chars that are not handled by encodeURICompent are converted as well
|
|
669
|
-
return str.replace(/[\x00-\x1F *'()<>@,;:\\"[\]?=\u007F-\uFFFF]/g, chr => this.encodeURICharComponent(chr));
|
|
670
|
-
}
|
|
671
|
-
};
|