@nocobase/plugin-workflow-mailer 2.4.0-alpha.1 → 2.4.0-alpha.10
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/dist/joi-browser.min.js +1 -1
- package/dist/node_modules/joi/lib/index.js +1 -1
- package/dist/node_modules/joi/lib/messages.js +35 -6
- package/dist/node_modules/joi/lib/types/keys.js +18 -1
- package/dist/node_modules/joi/lib/types/link.js +18 -2
- package/dist/node_modules/joi/lib/types/string.js +2 -2
- 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 +63 -112
- 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/dist/esm/addressparser/index.js +786 -0
- package/dist/node_modules/nodemailer/dist/esm/base64/index.d.ts +36 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/base64/index.js +39 -68
- package/dist/node_modules/nodemailer/dist/esm/dkim/index.d.ts +52 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/dkim/index.js +62 -92
- package/dist/node_modules/nodemailer/dist/esm/dkim/message-parser.d.ts +48 -0
- package/dist/node_modules/nodemailer/dist/esm/dkim/message-parser.js +176 -0
- 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/dist/esm/dkim/sign.js +125 -0
- package/dist/node_modules/nodemailer/dist/esm/errors.d.ts +96 -0
- package/dist/node_modules/nodemailer/dist/esm/errors.js +67 -0
- 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/dist/esm/fetch/index.js +344 -0
- package/dist/node_modules/nodemailer/dist/esm/json-transport/index.d.ts +64 -0
- package/dist/node_modules/nodemailer/dist/esm/json-transport/index.js +62 -0
- 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 +283 -275
- package/dist/node_modules/nodemailer/dist/esm/mailer/index.d.ts +237 -0
- package/dist/node_modules/nodemailer/{lib → dist/esm}/mailer/index.js +151 -205
- 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 +111 -125
- 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 +43 -49
- 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 +576 -437
- 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 +7 -15
- 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 -16
- 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 +59 -77
- package/dist/node_modules/nodemailer/dist/esm/sendmail-transport/index.d.ts +69 -0
- package/dist/node_modules/nodemailer/dist/esm/sendmail-transport/index.js +170 -0
- 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/dist/esm/shared/index.js +631 -0
- package/dist/node_modules/nodemailer/dist/esm/shared/objects.d.ts +23 -0
- package/dist/node_modules/nodemailer/dist/esm/shared/objects.js +38 -0
- 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/dist/esm/smtp-connection/http-proxy-client.js +149 -0
- package/dist/node_modules/nodemailer/dist/esm/smtp-connection/index.d.ts +416 -0
- package/dist/node_modules/nodemailer/dist/esm/smtp-connection/index.js +1797 -0
- 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 +195 -314
- 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 +70 -107
- package/dist/node_modules/nodemailer/dist/esm/smtp-transport/index.d.ts +156 -0
- package/dist/node_modules/nodemailer/dist/esm/smtp-transport/index.js +331 -0
- package/dist/node_modules/nodemailer/dist/esm/stream-transport/index.d.ts +65 -0
- package/dist/node_modules/nodemailer/dist/esm/stream-transport/index.js +106 -0
- 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 +12 -23
- 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/dist/esm/xoauth2/index.js +386 -0
- package/dist/node_modules/nodemailer/dist/well-known/services.json +640 -0
- package/dist/node_modules/nodemailer/package.json +1 -1
- package/package.json +4 -4
- package/dist/node_modules/nodemailer/.gitattributes +0 -6
- package/dist/node_modules/nodemailer/.ncurc.js +0 -7
- package/dist/node_modules/nodemailer/.prettierrc.js +0 -8
- package/dist/node_modules/nodemailer/lib/addressparser/index.js +0 -313
- package/dist/node_modules/nodemailer/lib/dkim/message-parser.js +0 -155
- package/dist/node_modules/nodemailer/lib/dkim/relaxed-body.js +0 -154
- package/dist/node_modules/nodemailer/lib/dkim/sign.js +0 -117
- package/dist/node_modules/nodemailer/lib/fetch/cookies.js +0 -281
- package/dist/node_modules/nodemailer/lib/fetch/index.js +0 -274
- package/dist/node_modules/nodemailer/lib/json-transport/index.js +0 -82
- package/dist/node_modules/nodemailer/lib/mime-funcs/index.js +0 -625
- package/dist/node_modules/nodemailer/lib/nodemailer.js +0 -1
- package/dist/node_modules/nodemailer/lib/sendmail-transport/index.js +0 -210
- package/dist/node_modules/nodemailer/lib/ses-transport/index.js +0 -349
- package/dist/node_modules/nodemailer/lib/shared/index.js +0 -688
- package/dist/node_modules/nodemailer/lib/smtp-connection/data-stream.js +0 -108
- package/dist/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +0 -143
- package/dist/node_modules/nodemailer/lib/smtp-connection/index.js +0 -1825
- package/dist/node_modules/nodemailer/lib/smtp-transport/index.js +0 -416
- package/dist/node_modules/nodemailer/lib/stream-transport/index.js +0 -135
- package/dist/node_modules/nodemailer/lib/well-known/services.json +0 -343
- package/dist/node_modules/nodemailer/lib/xoauth2/index.js +0 -376
|
@@ -0,0 +1,787 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint no-control-regex:0 */
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.isPlainText = isPlainText;
|
|
38
|
+
exports.quoteString = quoteString;
|
|
39
|
+
exports.hasLongerLines = hasLongerLines;
|
|
40
|
+
exports.encodeWord = encodeWord;
|
|
41
|
+
exports.encodeWords = encodeWords;
|
|
42
|
+
exports.buildHeaderValue = buildHeaderValue;
|
|
43
|
+
exports.buildHeaderParam = buildHeaderParam;
|
|
44
|
+
exports.parseHeaderValue = parseHeaderValue;
|
|
45
|
+
exports.detectExtension = detectExtension;
|
|
46
|
+
exports.detectMimeType = detectMimeType;
|
|
47
|
+
exports.foldLines = foldLines;
|
|
48
|
+
exports.splitMimeEncodedString = splitMimeEncodedString;
|
|
49
|
+
exports.encodeURICharComponent = encodeURICharComponent;
|
|
50
|
+
exports.safeEncodeURIComponent = safeEncodeURIComponent;
|
|
51
|
+
const base64 = __importStar(require("../base64/index.js"));
|
|
52
|
+
const qp = __importStar(require("../qp/index.js"));
|
|
53
|
+
const mimeTypes = __importStar(require("./mime-types.js"));
|
|
54
|
+
const objects_js_1 = require("../shared/objects.js");
|
|
55
|
+
/**
|
|
56
|
+
* Checks if a value is plaintext string (uses only printable 7bit chars)
|
|
57
|
+
*
|
|
58
|
+
* When isParam is set the value is destined for a header parameter, so HT, CR and LF
|
|
59
|
+
* are not plaintext either: a header parameter has no way to carry them. HT is a valid
|
|
60
|
+
* fold point, so folding and unfolding a header would rewrite it as a space, and CR/LF
|
|
61
|
+
* cannot appear in a header value at all. DEL is neither a token character nor qtext,
|
|
62
|
+
* so it can not be carried bare or quoted. Such values have to go through the rfc2231
|
|
63
|
+
* parameter continuation encoding instead, the same way a quote already does.
|
|
64
|
+
*
|
|
65
|
+
* @param value String to be tested
|
|
66
|
+
* @param [isParam] Set to true if the value is a header parameter value
|
|
67
|
+
* @returns true if it is a plaintext string
|
|
68
|
+
*/
|
|
69
|
+
function isPlainText(value, isParam) {
|
|
70
|
+
const re = isParam ? /[\x00-\x1f\x7f"\u0080-\uFFFF]/ : /[\x00-\x08\x0b\x0c\x0e-\x1f\u0080-\uFFFF]/;
|
|
71
|
+
return typeof value === 'string' && !re.test(value);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Wraps a value into a quoted-string. Inside one a quote would end the string early
|
|
75
|
+
* and a backslash would escape whatever follows it, so both go out as quoted-pairs.
|
|
76
|
+
*
|
|
77
|
+
* @param value String to be quoted
|
|
78
|
+
* @returns The value as a quoted-string, quotes included
|
|
79
|
+
*/
|
|
80
|
+
function quoteString(value) {
|
|
81
|
+
return '"' + (value || '').toString().replace(/["\\]/g, '\\$&') + '"';
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Checks if a multi line string containes lines longer than the selected value.
|
|
85
|
+
*
|
|
86
|
+
* Useful when detecting if a mail message needs any processing at all:
|
|
87
|
+
* if only plaintext characters are used and lines are short, then there is
|
|
88
|
+
* no need to encode the values in any way. If the value is plaintext but has
|
|
89
|
+
* longer lines then allowed, then use format=flowed
|
|
90
|
+
*
|
|
91
|
+
* @param str Multi line string to check
|
|
92
|
+
* @param lineLength Max line length to check for
|
|
93
|
+
* @returns Returns true if there is at least one line longer than lineLength chars
|
|
94
|
+
*/
|
|
95
|
+
function hasLongerLines(str, lineLength) {
|
|
96
|
+
if (str.length > 128 * 1024) {
|
|
97
|
+
// do not test strings longer than 128kB
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
return new RegExp('^.{' + (lineLength + 1) + ',}', 'm').test(str);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Encodes a string or an Buffer to an UTF-8 MIME Word (rfc2047)
|
|
104
|
+
*
|
|
105
|
+
* @param data String to be encoded
|
|
106
|
+
* @param mimeWordEncoding='Q' Encoding for the mime word, either Q or B
|
|
107
|
+
* @param [maxLength=0] If set, split mime words into several chunks if needed
|
|
108
|
+
* @return Single or several mime words joined together
|
|
109
|
+
*/
|
|
110
|
+
function encodeWord(data, mimeWordEncoding, maxLength) {
|
|
111
|
+
mimeWordEncoding = (mimeWordEncoding || 'Q').toString().toUpperCase().trim().charAt(0);
|
|
112
|
+
maxLength = maxLength || 0;
|
|
113
|
+
let encodedStr;
|
|
114
|
+
const toCharset = 'UTF-8';
|
|
115
|
+
if (maxLength && maxLength > 7 + toCharset.length) {
|
|
116
|
+
maxLength -= 7 + toCharset.length;
|
|
117
|
+
}
|
|
118
|
+
if (mimeWordEncoding === 'Q') {
|
|
119
|
+
// https://tools.ietf.org/html/rfc2047#section-5 rule (3)
|
|
120
|
+
encodedStr = qp.encode(data).replace(/[^a-z0-9!*+\-/=]/gi, chr => {
|
|
121
|
+
const ord = chr.charCodeAt(0).toString(16).toUpperCase();
|
|
122
|
+
if (chr === ' ') {
|
|
123
|
+
return '_';
|
|
124
|
+
}
|
|
125
|
+
return '=' + (ord.length === 1 ? '0' + ord : ord);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
else if (mimeWordEncoding === 'B') {
|
|
129
|
+
// the chunking loop below splits raw text and base64 encodes each part, so a
|
|
130
|
+
// Buffer goes in as its UTF-8 string: handing it the base64 of the whole input
|
|
131
|
+
// would encode the encoding itself and decode back to base64 text
|
|
132
|
+
encodedStr = typeof data === 'string' ? data : data.toString('utf-8');
|
|
133
|
+
maxLength = maxLength ? Math.max(3, ((maxLength - (maxLength % 4)) / 4) * 3) : 0;
|
|
134
|
+
}
|
|
135
|
+
if (maxLength && (mimeWordEncoding !== 'B' ? encodedStr : base64.encode(data)).length > maxLength) {
|
|
136
|
+
if (mimeWordEncoding === 'Q') {
|
|
137
|
+
encodedStr = splitMimeEncodedString(encodedStr, maxLength).join('?= =?' + toCharset + '?' + mimeWordEncoding + '?');
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
// RFC2047 6.3 (2) states that encoded-word must include an integral number of characters, so no chopping unicode sequences
|
|
141
|
+
const parts = [];
|
|
142
|
+
let lpart = '';
|
|
143
|
+
for (let i = 0, len = encodedStr.length; i < len; i++) {
|
|
144
|
+
let chr = encodedStr.charAt(i);
|
|
145
|
+
if (/[\ud800-\udbff]/.test(chr) && /[\udc00-\udfff]/.test(encodedStr.charAt(i + 1))) {
|
|
146
|
+
// leading surrogate, so add the trailing surrogate as well
|
|
147
|
+
// an unpaired one must not swallow the next unit, that would destroy
|
|
148
|
+
// a valid pair following it
|
|
149
|
+
chr += encodedStr.charAt(++i);
|
|
150
|
+
}
|
|
151
|
+
// check if we can add this character to the existing string
|
|
152
|
+
// without breaking byte length limit
|
|
153
|
+
if (Buffer.byteLength(lpart + chr) <= maxLength || i === 0) {
|
|
154
|
+
lpart += chr;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
// we hit the length limit, so push the existing string and start over
|
|
158
|
+
parts.push(base64.encode(lpart));
|
|
159
|
+
lpart = chr;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (lpart) {
|
|
163
|
+
parts.push(base64.encode(lpart));
|
|
164
|
+
}
|
|
165
|
+
if (parts.length > 1) {
|
|
166
|
+
encodedStr = parts.join('?= =?' + toCharset + '?' + mimeWordEncoding + '?');
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
encodedStr = parts.join('');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else if (mimeWordEncoding === 'B') {
|
|
174
|
+
encodedStr = base64.encode(data);
|
|
175
|
+
}
|
|
176
|
+
return '=?' + toCharset + '?' + mimeWordEncoding + '?' + encodedStr + (encodedStr.substr(-2) === '?=' ? '' : '?=');
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Finds word sequences with non ascii text and converts these to mime words
|
|
180
|
+
*
|
|
181
|
+
* @param value String to be encoded
|
|
182
|
+
* @param mimeWordEncoding='Q' Encoding for the mime word, either Q or B
|
|
183
|
+
* @param [maxLength=0] If set, split mime words into several chunks if needed
|
|
184
|
+
* @param [encodeAll=false] If true and the value needs encoding then encodes entire string, not just the smallest match
|
|
185
|
+
* @return String with possible mime words
|
|
186
|
+
*/
|
|
187
|
+
function encodeWords(value, mimeWordEncoding, maxLength, encodeAll) {
|
|
188
|
+
maxLength = maxLength || 0;
|
|
189
|
+
// find first word with a non-printable ascii or special symbol in it
|
|
190
|
+
const firstMatch = value.match(/(?:^|\s)([^\s]*["\u0080-\uFFFF])/);
|
|
191
|
+
if (!firstMatch) {
|
|
192
|
+
return value;
|
|
193
|
+
}
|
|
194
|
+
if (encodeAll) {
|
|
195
|
+
// if it is requested to encode everything or the string contains something that resebles encoded word, then encode everything
|
|
196
|
+
return encodeWord(value, mimeWordEncoding, maxLength);
|
|
197
|
+
}
|
|
198
|
+
// find the last word with a non-printable ascii in it
|
|
199
|
+
const lastMatch = value.match(/(["\u0080-\uFFFF][^\s]*)[^"\u0080-\uFFFF]*$/);
|
|
200
|
+
if (!lastMatch) {
|
|
201
|
+
// should not happen
|
|
202
|
+
return value;
|
|
203
|
+
}
|
|
204
|
+
const startIndex = firstMatch.index +
|
|
205
|
+
(firstMatch[0].match(/[^\s]/) || {
|
|
206
|
+
index: 0
|
|
207
|
+
}).index;
|
|
208
|
+
const endIndex = lastMatch.index + (lastMatch[1] || '').length;
|
|
209
|
+
return ((startIndex ? value.substr(0, startIndex) : '') +
|
|
210
|
+
encodeWord(value.substring(startIndex, endIndex), mimeWordEncoding || 'Q', maxLength) +
|
|
211
|
+
(endIndex < value.length ? value.substr(endIndex) : ''));
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Joins parsed header value together as 'value; param1=value1; param2=value2'
|
|
215
|
+
* PS: We are following RFC 822 for the list of special characters that we need to keep in quotes.
|
|
216
|
+
* Refer: https://www.w3.org/Protocols/rfc1341/4_Content-Type.html
|
|
217
|
+
* @param structured Parsed header value
|
|
218
|
+
* @return joined header value
|
|
219
|
+
*/
|
|
220
|
+
function buildHeaderValue(structured) {
|
|
221
|
+
const paramsArray = [];
|
|
222
|
+
Object.keys(structured.params || {}).forEach(key => {
|
|
223
|
+
// filename might include unicode characters so it is a special case
|
|
224
|
+
// other values probably do not
|
|
225
|
+
const value = structured.params[key];
|
|
226
|
+
// a parameter name is a token too and it is emitted without any quoting around it
|
|
227
|
+
const param = key.replace(/[\x00-\x1f\x7f]/g, '');
|
|
228
|
+
if (!isPlainText(value, true) || value.length >= 75) {
|
|
229
|
+
buildHeaderParam(param, value, 50).forEach(encodedParam => {
|
|
230
|
+
if (!/[\s"\\;:/=(),<>@[\]?]|^[-']|'$/.test(encodedParam.value) || encodedParam.key.substr(-1) === '*') {
|
|
231
|
+
paramsArray.push(encodedParam.key + '=' + encodedParam.value);
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
paramsArray.push(encodedParam.key + '=' + JSON.stringify(encodedParam.value));
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
else if (!value.length || /[\s'"\\;:/=(),<>@[\]?]|^-/.test(value)) {
|
|
239
|
+
// a parameter value is a token or a quoted-string and a token is never empty, so
|
|
240
|
+
// a valueless parameter such as the 'flag' of 'multipart/mixed; flag; boundary=b'
|
|
241
|
+
// goes out as 'flag=""' rather than as the 'flag=' that parses as neither
|
|
242
|
+
paramsArray.push(param + '=' + JSON.stringify(value));
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
paramsArray.push(param + '=' + value);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
// the value ahead of the parameters is a token, it has no way to carry a control
|
|
249
|
+
// char or DEL and there is no quoting construct around it to escape one into
|
|
250
|
+
const value = typeof structured.value === 'string' ? structured.value.replace(/[\x00-\x1f\x7f]/g, '') : structured.value;
|
|
251
|
+
return value + (paramsArray.length ? '; ' + paramsArray.join('; ') : '');
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Encodes a string or an Buffer to an UTF-8 Parameter Value Continuation encoding (rfc2231)
|
|
255
|
+
* Useful for splitting long parameter values.
|
|
256
|
+
*
|
|
257
|
+
* For example
|
|
258
|
+
* title="unicode string"
|
|
259
|
+
* becomes
|
|
260
|
+
* title*0*=utf-8''unicode
|
|
261
|
+
* title*1*=%20string
|
|
262
|
+
*
|
|
263
|
+
* @param key Parameter name the generated keys are built from, for example title
|
|
264
|
+
* @param data String to be encoded
|
|
265
|
+
* @param [maxLength=50] Max length for generated chunks
|
|
266
|
+
* @return A list of encoded keys and headers
|
|
267
|
+
*/
|
|
268
|
+
function buildHeaderParam(key, data, maxLength) {
|
|
269
|
+
const list = [];
|
|
270
|
+
let encodedStr = typeof data === 'string' ? data : (data || '').toString();
|
|
271
|
+
let chr;
|
|
272
|
+
let line;
|
|
273
|
+
let startPos = 0;
|
|
274
|
+
let i, len;
|
|
275
|
+
maxLength = maxLength || 50;
|
|
276
|
+
// process ascii only text
|
|
277
|
+
if (isPlainText(data, true)) {
|
|
278
|
+
// check if conversion is even needed
|
|
279
|
+
if (encodedStr.length <= maxLength) {
|
|
280
|
+
return [
|
|
281
|
+
{
|
|
282
|
+
key,
|
|
283
|
+
value: encodedStr
|
|
284
|
+
}
|
|
285
|
+
];
|
|
286
|
+
}
|
|
287
|
+
encodedStr = encodedStr.replace(new RegExp('.{' + maxLength + '}', 'g'), str => {
|
|
288
|
+
list.push({
|
|
289
|
+
line: str
|
|
290
|
+
});
|
|
291
|
+
return '';
|
|
292
|
+
});
|
|
293
|
+
if (encodedStr) {
|
|
294
|
+
list.push({
|
|
295
|
+
line: encodedStr
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
if (/[\uD800-\uDBFF]/.test(encodedStr)) {
|
|
301
|
+
// string containts surrogate pairs, so normalize it to an array of bytes
|
|
302
|
+
const encodedStrArr = [];
|
|
303
|
+
for (i = 0, len = encodedStr.length; i < len; i++) {
|
|
304
|
+
chr = encodedStr.charAt(i);
|
|
305
|
+
if (/[\ud800-\udbff]/.test(chr) && /[\udc00-\udfff]/.test(encodedStr.charAt(i + 1))) {
|
|
306
|
+
// an unpaired leading surrogate must not consume the next unit, that
|
|
307
|
+
// would tear apart a valid pair following it
|
|
308
|
+
chr += encodedStr.charAt(i + 1);
|
|
309
|
+
encodedStrArr.push(chr);
|
|
310
|
+
i++;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
encodedStrArr.push(chr);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
encodedStr = encodedStrArr;
|
|
317
|
+
}
|
|
318
|
+
// first line includes the charset and language info and needs to be encoded
|
|
319
|
+
// even if it does not contain any unicode characters
|
|
320
|
+
line = "utf-8''";
|
|
321
|
+
let encoded = true;
|
|
322
|
+
startPos = 0;
|
|
323
|
+
// process text with unicode or special chars
|
|
324
|
+
for (i = 0, len = encodedStr.length; i < len; i++) {
|
|
325
|
+
chr = encodedStr[i];
|
|
326
|
+
if (encoded) {
|
|
327
|
+
chr = safeEncodeURIComponent(chr);
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
// try to urlencode current char
|
|
331
|
+
chr = chr === ' ' ? chr : safeEncodeURIComponent(chr);
|
|
332
|
+
// By default it is not required to encode a line, the need
|
|
333
|
+
// only appears when the string contains unicode or special chars
|
|
334
|
+
// in this case we start processing the line over and encode all chars
|
|
335
|
+
if (chr !== encodedStr[i]) {
|
|
336
|
+
// Check if it is even possible to add the encoded char to the line
|
|
337
|
+
// If not, there is no reason to use this line, just push it to the list
|
|
338
|
+
// and start a new line with the char that needs encoding
|
|
339
|
+
if ((safeEncodeURIComponent(line) + chr).length >= maxLength) {
|
|
340
|
+
list.push({
|
|
341
|
+
line,
|
|
342
|
+
encoded
|
|
343
|
+
});
|
|
344
|
+
// the line we start here holds an encoded char, so it has to be
|
|
345
|
+
// flagged as one. otherwise it gets an unstarred continuation key
|
|
346
|
+
// and a receiver reads the percent escapes as literal text
|
|
347
|
+
line = '';
|
|
348
|
+
encoded = true;
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
encoded = true;
|
|
352
|
+
i = startPos;
|
|
353
|
+
line = '';
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
// if the line is already too long, push it to the list and start a new one
|
|
359
|
+
if ((line + chr).length >= maxLength) {
|
|
360
|
+
list.push({
|
|
361
|
+
line,
|
|
362
|
+
encoded
|
|
363
|
+
});
|
|
364
|
+
line = chr = encodedStr[i] === ' ' ? ' ' : safeEncodeURIComponent(encodedStr[i]);
|
|
365
|
+
if (chr === encodedStr[i]) {
|
|
366
|
+
encoded = false;
|
|
367
|
+
startPos = i - 1;
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
encoded = true;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
line += chr;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (line) {
|
|
378
|
+
list.push({
|
|
379
|
+
line,
|
|
380
|
+
encoded
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return list.map((item, i) => ({
|
|
385
|
+
// encoded lines: {name}*{part}*
|
|
386
|
+
// unencoded lines: {name}*{part}
|
|
387
|
+
// if any line needs to be encoded then the first line (part==0) is always encoded
|
|
388
|
+
key: key + '*' + i + (item.encoded ? '*' : ''),
|
|
389
|
+
value: item.line
|
|
390
|
+
}));
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* An RFC 2045 token: printable ASCII without SPACE, the control characters, DEL and the
|
|
394
|
+
* tspecials. A charset name is one, and the name a continuation carries is written into
|
|
395
|
+
* the encoded word the parameter value becomes, so it is checked against this before it
|
|
396
|
+
* goes in. Whitespace used to come off it only because the value was trimmed first.
|
|
397
|
+
*/
|
|
398
|
+
const TOKEN = /^[^\x00-\x20\x7f()<>@,;:\\"/[\]?=]+$/;
|
|
399
|
+
/**
|
|
400
|
+
* Whether a string can be a header parameter name.
|
|
401
|
+
*
|
|
402
|
+
* A parameter name is a token, so it is never empty. A "__proto__" name would target the
|
|
403
|
+
* prototype chain of the params object instead of an own property of it and read back as
|
|
404
|
+
* Object.prototype, so it is no name either.
|
|
405
|
+
*
|
|
406
|
+
* @param name Candidate parameter name, already lowercased
|
|
407
|
+
* @return true when the name can be used
|
|
408
|
+
*/
|
|
409
|
+
function _isParamName(name) {
|
|
410
|
+
return !!name && !(0, objects_js_1.isProtoKey)(name);
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Parses a header value with key=value arguments into a structured
|
|
414
|
+
* object.
|
|
415
|
+
*
|
|
416
|
+
* parseHeaderValue('content-type: text/plain; CHARSET='UTF-8'') ->
|
|
417
|
+
* {
|
|
418
|
+
* 'value': 'text/plain',
|
|
419
|
+
* 'params': {
|
|
420
|
+
* 'charset': 'UTF-8'
|
|
421
|
+
* }
|
|
422
|
+
* }
|
|
423
|
+
*
|
|
424
|
+
* @param str Header value
|
|
425
|
+
* @return Header value as a parsed structure
|
|
426
|
+
*/
|
|
427
|
+
function parseHeaderValue(str) {
|
|
428
|
+
const response = {
|
|
429
|
+
value: '',
|
|
430
|
+
params: {}
|
|
431
|
+
};
|
|
432
|
+
// A duplicated parameter resolves to its first occurrence, the way a duplicated header
|
|
433
|
+
// does. Letting the last one win disagrees with the receivers that take the first, and
|
|
434
|
+
// the two readings of 'boundary="b"; boundary="c"' name different delimiters. The
|
|
435
|
+
// continuation join below is the only writer of the name it builds, so it tests the
|
|
436
|
+
// name with _isParamName directly rather than taking that rule along from here.
|
|
437
|
+
const setParam = (name, value) => {
|
|
438
|
+
name = name.toLowerCase();
|
|
439
|
+
if (_isParamName(name) && !Object.prototype.hasOwnProperty.call(response.params, name)) {
|
|
440
|
+
response.params[name] = value;
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
let key = false;
|
|
444
|
+
let value = '';
|
|
445
|
+
let stage = 'value';
|
|
446
|
+
let quote = false;
|
|
447
|
+
let escaped = false;
|
|
448
|
+
let chr;
|
|
449
|
+
// Whitespace seen outside a quoted string is held back until a significant character
|
|
450
|
+
// follows it, so the whitespace around a value is dropped without trimming spaces the
|
|
451
|
+
// sender quoted on purpose. Trimming the stored value instead loses the trailing space
|
|
452
|
+
// of 'filename*0="Annual Report "', which the next continuation section is appended to.
|
|
453
|
+
let pendingSpace = '';
|
|
454
|
+
let quoteClosed = false;
|
|
455
|
+
// Whitespace ahead of the first character of a value is padding and is dropped, the
|
|
456
|
+
// whitespace between two characters of it is content
|
|
457
|
+
const flushSpace = () => {
|
|
458
|
+
if (value.length) {
|
|
459
|
+
value += pendingSpace;
|
|
460
|
+
}
|
|
461
|
+
pendingSpace = '';
|
|
462
|
+
};
|
|
463
|
+
const addChr = (c) => {
|
|
464
|
+
flushSpace();
|
|
465
|
+
value += c;
|
|
466
|
+
};
|
|
467
|
+
const takeValue = () => {
|
|
468
|
+
const taken = value;
|
|
469
|
+
value = '';
|
|
470
|
+
pendingSpace = '';
|
|
471
|
+
quoteClosed = false;
|
|
472
|
+
return taken;
|
|
473
|
+
};
|
|
474
|
+
const storeValue = () => {
|
|
475
|
+
const taken = takeValue();
|
|
476
|
+
if (key === false) {
|
|
477
|
+
response.value = taken;
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
setParam(key, taken);
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
// A parameter name with no '=' is a valueless parameter, not the start of the next one.
|
|
484
|
+
// Without this the name keeps growing across the ';' and swallows whatever follows, which
|
|
485
|
+
// is how 'multipart/mixed; flag; boundary="AAA"' lost its boundary to a parameter named
|
|
486
|
+
// 'flag; boundary' and left the node declaring the generated boundary beside the asked
|
|
487
|
+
// for one, so a receiver reading the first of the two found no delimiter it matched.
|
|
488
|
+
const storeEmptyKey = () => {
|
|
489
|
+
setParam(takeValue().trim(), '');
|
|
490
|
+
};
|
|
491
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
|
492
|
+
chr = str.charAt(i);
|
|
493
|
+
if (stage === 'key') {
|
|
494
|
+
if (chr === '=') {
|
|
495
|
+
key = takeValue().trim();
|
|
496
|
+
stage = 'value';
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
if (chr === ';') {
|
|
500
|
+
storeEmptyKey();
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
value += chr;
|
|
504
|
+
}
|
|
505
|
+
else {
|
|
506
|
+
if (quoteClosed && chr !== ';') {
|
|
507
|
+
// Nothing behind a closed quoted string reaches the value. RFC 2045 says a
|
|
508
|
+
// parameter value is a token or a quoted string and not both, so what follows
|
|
509
|
+
// one is junk and only the ';' that ends the parameter still counts. Tested
|
|
510
|
+
// ahead of the branches rather than beside the append at the foot of them,
|
|
511
|
+
// where each branch above was a way around it: a second '"' reopened quoting
|
|
512
|
+
// and swallowed the rest of the header, so 'boundary="AAA" "; boundary=BBB"'
|
|
513
|
+
// read as a single boundary of 'AAA ', and the junk of
|
|
514
|
+
// 'boundary="AAA" (unterminated comment' still joined the declared boundary
|
|
515
|
+
// through the escape branch. quoteClosed is only ever set while no quote is
|
|
516
|
+
// open, and this is what keeps one from being opened afterwards.
|
|
517
|
+
escaped = false;
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
if (escaped) {
|
|
521
|
+
addChr(chr);
|
|
522
|
+
}
|
|
523
|
+
else if (quote && chr === '\\') {
|
|
524
|
+
// a backslash only escapes inside a quoted string, everywhere else it is an
|
|
525
|
+
// ordinary character. Treating it as an escape turns the parameter value
|
|
526
|
+
// 'C:\Users\me\report.txt' into 'C:Usersmereport.txt'
|
|
527
|
+
escaped = true;
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
else if (quote && chr === quote) {
|
|
531
|
+
quote = false;
|
|
532
|
+
quoteClosed = true;
|
|
533
|
+
}
|
|
534
|
+
else if (!quote && chr === '"') {
|
|
535
|
+
quote = chr;
|
|
536
|
+
flushSpace();
|
|
537
|
+
}
|
|
538
|
+
else if (!quote && chr === ';') {
|
|
539
|
+
storeValue();
|
|
540
|
+
stage = 'key';
|
|
541
|
+
}
|
|
542
|
+
else if (!quote && (chr === ' ' || chr === '\t')) {
|
|
543
|
+
pendingSpace += chr;
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
addChr(chr);
|
|
547
|
+
}
|
|
548
|
+
escaped = false;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (stage === 'value') {
|
|
552
|
+
storeValue();
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
// a key with no value, as in 'Header-Key: somevalue; key=value; emptykey'
|
|
556
|
+
storeEmptyKey();
|
|
557
|
+
}
|
|
558
|
+
// handle parameter value continuations
|
|
559
|
+
// https://tools.ietf.org/html/rfc2231#section-3
|
|
560
|
+
// Sections are collected in a list and ordered below rather than written into an array
|
|
561
|
+
// at their own section number. An index write makes the array as long as the number the
|
|
562
|
+
// header asked for, and the join that follows walks all of it, so the 55 byte value
|
|
563
|
+
// "attachment; filename*0*=utf-8''a; filename*4000000000=b" held a core for over two
|
|
564
|
+
// minutes.
|
|
565
|
+
const continuations = new Map();
|
|
566
|
+
Object.keys(response.params).forEach(key => {
|
|
567
|
+
const match = key.match(/(\*(\d+)|\*(\d+)\*|\*)$/);
|
|
568
|
+
if (!match) {
|
|
569
|
+
// not a continuation parameter, there is nothing to join
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
const actualKey = key.substr(0, match.index);
|
|
573
|
+
const nr = Number(match[2] || match[3]) || 0;
|
|
574
|
+
// RFC 2231 section 4.1: only a section whose name ends in '*' is percent encoded
|
|
575
|
+
const encoded = match[0].substr(-1) === '*';
|
|
576
|
+
// remove the old reference
|
|
577
|
+
let value = response.params[key];
|
|
578
|
+
delete response.params[key];
|
|
579
|
+
if (!_isParamName(actualKey)) {
|
|
580
|
+
// the joined value can not be written back under this name. It is empty when the
|
|
581
|
+
// continuation suffix was all there was of it, as in the bare '*' of 'text/plain; a;*'
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
let continuation = continuations.get(actualKey);
|
|
585
|
+
if (!continuation) {
|
|
586
|
+
continuation = { charset: false, sections: [] };
|
|
587
|
+
continuations.set(actualKey, continuation);
|
|
588
|
+
}
|
|
589
|
+
const charsetMatch = nr === 0 && encoded ? value.match(/^([^']*)'[^']*'(.*)$/) : null;
|
|
590
|
+
if (charsetMatch) {
|
|
591
|
+
// the charset is a token, and anything else named as one is no charset a consumer
|
|
592
|
+
// could resolve, so it reads as the unnamed case rather than being carried into
|
|
593
|
+
// the encoded word below. A "\r\n" of a prefix would otherwise reach a consumer
|
|
594
|
+
// of the parsed value as the charset of a word that no decoder can act on
|
|
595
|
+
continuation.charset = TOKEN.test(charsetMatch[1]) ? charsetMatch[1] : 'iso-8859-1';
|
|
596
|
+
value = charsetMatch[2];
|
|
597
|
+
}
|
|
598
|
+
continuation.sections.push({ nr, value, encoded });
|
|
599
|
+
});
|
|
600
|
+
// concatenate split rfc2231 strings and convert encoded strings to mime encoded words
|
|
601
|
+
continuations.forEach((continuation, key) => {
|
|
602
|
+
if (Object.prototype.hasOwnProperty.call(response.params, key)) {
|
|
603
|
+
// The same name was also given as a plain parameter, which the starred keys were
|
|
604
|
+
// just deleted from around, so this write would be the only one in the function
|
|
605
|
+
// to override a name already taken. 'filename=plain.txt; filename*0=evil.txt'
|
|
606
|
+
// resolves to the plain parameter either way round, so the reading does not come
|
|
607
|
+
// down to which of the two spellings the sender put first
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
continuation.sections.sort((a, b) => a.nr - b.nr);
|
|
611
|
+
if (!continuation.charset) {
|
|
612
|
+
// nothing said which charset the percent escapes of an encoded section are in,
|
|
613
|
+
// so every section is passed on as the text it already is
|
|
614
|
+
response.params[key] = continuation.sections.map(section => section.value).join('');
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
// convert "%AB" to "=?charset?Q?=AB?="
|
|
618
|
+
response.params[key] = '=?' + continuation.charset + '?Q?' + continuation.sections.map(_encodeContinuationSection).join('') + '?=';
|
|
619
|
+
});
|
|
620
|
+
return response;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Renders one parameter value continuation section as the payload of a Q encoded word.
|
|
624
|
+
*
|
|
625
|
+
* A section whose name ends in '*' is percent encoded and its escapes carry the bytes of
|
|
626
|
+
* the value, so they only have to be rewritten into the "=AB" spelling a Q encoded word
|
|
627
|
+
* uses. A section without the '*' is literal text (RFC 2231 section 4.1), so its '%' is a
|
|
628
|
+
* '%' and is escaped along with the characters a Q encoded word can not carry bare.
|
|
629
|
+
* Decoding a literal section invents bytes that never appeared on the wire: it is how the
|
|
630
|
+
* value 'filename*0*=utf-8''safe; filename*1=%2F..%2F..%2Fetc%2Fpasswd' was emitted as a
|
|
631
|
+
* filename every receiving client reads back as 'safe/../../etc/passwd'.
|
|
632
|
+
*
|
|
633
|
+
* @param section One collected continuation section
|
|
634
|
+
* @return The section as Q encoded word payload
|
|
635
|
+
*/
|
|
636
|
+
function _encodeContinuationSection(section) {
|
|
637
|
+
const specials = section.encoded ? /[=?_\s]/g : /[=?_\s%]/g;
|
|
638
|
+
return (section.value
|
|
639
|
+
// fix invalidly encoded chars
|
|
640
|
+
.replace(specials, s => {
|
|
641
|
+
const c = s.charCodeAt(0).toString(16);
|
|
642
|
+
if (s === ' ') {
|
|
643
|
+
return '_';
|
|
644
|
+
}
|
|
645
|
+
return '%' + (c.length < 2 ? '0' : '') + c;
|
|
646
|
+
})
|
|
647
|
+
// change from urlencoding to percent encoding
|
|
648
|
+
.replace(/%/g, '='));
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Returns file extension for a content type string. If no suitable extensions
|
|
652
|
+
* are found, 'bin' is used as the default extension
|
|
653
|
+
*
|
|
654
|
+
* @param mimeType Content type to be checked for
|
|
655
|
+
* @return File extension
|
|
656
|
+
*/
|
|
657
|
+
function detectExtension(mimeType) {
|
|
658
|
+
return mimeTypes.detectExtension(mimeType);
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Returns content type for a file extension. If no suitable content types
|
|
662
|
+
* are found, 'application/octet-stream' is used as the default content type
|
|
663
|
+
*
|
|
664
|
+
* @param extension Extension to be checked for
|
|
665
|
+
* @return File extension
|
|
666
|
+
*/
|
|
667
|
+
function detectMimeType(extension) {
|
|
668
|
+
return mimeTypes.detectMimeType(extension);
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Folds long lines, useful for folding header lines (afterSpace=false) and
|
|
672
|
+
* flowed text (afterSpace=true)
|
|
673
|
+
*
|
|
674
|
+
* @param str String to be folded
|
|
675
|
+
* @param [lineLength=76] Maximum length of a line
|
|
676
|
+
* @param afterSpace If true, leave a space in th end of a line
|
|
677
|
+
* @return String with folded lines
|
|
678
|
+
*/
|
|
679
|
+
function foldLines(str, lineLength, afterSpace) {
|
|
680
|
+
str = (str || '').toString();
|
|
681
|
+
lineLength = lineLength || 76;
|
|
682
|
+
let pos = 0;
|
|
683
|
+
const len = str.length;
|
|
684
|
+
let result = '';
|
|
685
|
+
let line, match;
|
|
686
|
+
while (pos < len) {
|
|
687
|
+
line = str.substr(pos, lineLength);
|
|
688
|
+
if (line.length < lineLength) {
|
|
689
|
+
result += line;
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
if ((match = line.match(/^[^\n\r]*(\r?\n|\r)/))) {
|
|
693
|
+
line = match[0];
|
|
694
|
+
result += line;
|
|
695
|
+
pos += line.length;
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
698
|
+
else if ((match = line.match(/(\s+)[^\s]*$/)) && match[0].length - (afterSpace ? (match[1] || '').length : 0) < line.length) {
|
|
699
|
+
line = line.substr(0, line.length - (match[0].length - (afterSpace ? (match[1] || '').length : 0)));
|
|
700
|
+
}
|
|
701
|
+
else if ((match = str.substr(pos + line.length).match(/^[^\s]+(\s*)/))) {
|
|
702
|
+
line = line + match[0].substr(0, match[0].length - (!afterSpace ? (match[1] || '').length : 0));
|
|
703
|
+
}
|
|
704
|
+
result += line;
|
|
705
|
+
pos += line.length;
|
|
706
|
+
if (pos < len) {
|
|
707
|
+
result += '\r\n';
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return result;
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Splits a mime encoded string. Needed for dividing mime words into smaller chunks
|
|
714
|
+
*
|
|
715
|
+
* @param str Mime encoded string to be split up
|
|
716
|
+
* @param maxlen Maximum length of characters for one part (minimum 12)
|
|
717
|
+
* @return Split string
|
|
718
|
+
*/
|
|
719
|
+
function splitMimeEncodedString(str, maxlen) {
|
|
720
|
+
const lines = [];
|
|
721
|
+
let curLine, fallbackLine, match, chr, done;
|
|
722
|
+
// require at least 12 symbols to fit possible 4 octet UTF-8 sequences
|
|
723
|
+
maxlen = Math.max(maxlen || 0, 12);
|
|
724
|
+
while (str.length) {
|
|
725
|
+
curLine = str.substr(0, maxlen);
|
|
726
|
+
// move incomplete escaped char back to main
|
|
727
|
+
if ((match = curLine.match(/[=][0-9A-F]?$/i))) {
|
|
728
|
+
curLine = curLine.substr(0, match.index);
|
|
729
|
+
}
|
|
730
|
+
// Malformed input (a run of stray UTF-8 continuation bytes) has no split point
|
|
731
|
+
// that keeps a character sequence whole, so the loop below walks back to an
|
|
732
|
+
// empty line looking for one. Keep the widest chunk that at least does not cut
|
|
733
|
+
// a "=XX" escape in half, so the part stays a decodable encoded word.
|
|
734
|
+
fallbackLine = curLine.length ? curLine : str.substr(0, maxlen);
|
|
735
|
+
done = false;
|
|
736
|
+
while (!done && curLine.length) {
|
|
737
|
+
done = true;
|
|
738
|
+
// check if not middle of a unicode char sequence
|
|
739
|
+
if ((match = str.substr(curLine.length).match(/^[=]([0-9A-F]{2})/i))) {
|
|
740
|
+
chr = parseInt(match[1], 16);
|
|
741
|
+
// invalid sequence, move one char back anc recheck
|
|
742
|
+
if (chr < 0xc2 && chr > 0x7f) {
|
|
743
|
+
curLine = curLine.substr(0, curLine.length - 3);
|
|
744
|
+
done = false;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
if (!curLine.length) {
|
|
749
|
+
curLine = fallbackLine;
|
|
750
|
+
}
|
|
751
|
+
lines.push(curLine);
|
|
752
|
+
str = str.substr(curLine.length);
|
|
753
|
+
}
|
|
754
|
+
return lines;
|
|
755
|
+
}
|
|
756
|
+
function encodeURICharComponent(chr) {
|
|
757
|
+
let res = '';
|
|
758
|
+
let ord = chr.charCodeAt(0).toString(16).toUpperCase();
|
|
759
|
+
if (ord.length % 2) {
|
|
760
|
+
ord = '0' + ord;
|
|
761
|
+
}
|
|
762
|
+
if (ord.length > 2) {
|
|
763
|
+
for (let i = 0, len = ord.length / 2; i < len; i++) {
|
|
764
|
+
res += '%' + ord.substr(i, 2);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
else {
|
|
768
|
+
res += '%' + ord;
|
|
769
|
+
}
|
|
770
|
+
return res;
|
|
771
|
+
}
|
|
772
|
+
function safeEncodeURIComponent(str) {
|
|
773
|
+
str = (str || '').toString();
|
|
774
|
+
try {
|
|
775
|
+
// might throw if we try to encode invalid sequences, eg. partial emoji
|
|
776
|
+
str = encodeURIComponent(str);
|
|
777
|
+
}
|
|
778
|
+
catch (_E) {
|
|
779
|
+
// an unpaired surrogate has no utf-8 representation, so run the value through a
|
|
780
|
+
// utf-8 roundtrip to get the same U+FFFD every other encoder here produces and
|
|
781
|
+
// retry. the value must never come back unencoded, it goes into a header parameter
|
|
782
|
+
// where a bare quote or semicolon would break it out into a parameter of its own
|
|
783
|
+
str = encodeURIComponent(Buffer.from(str, 'utf-8').toString('utf-8'));
|
|
784
|
+
}
|
|
785
|
+
// ensure chars that are not handled by encodeURICompent are converted as well
|
|
786
|
+
return str.replace(/[\x00-\x1F *'()<>@,;:\\"[\]?=\u007F-\uFFFF]/g, chr => encodeURICharComponent(chr));
|
|
787
|
+
}
|