postal-mime 2.0.0 → 2.0.2

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.
@@ -0,0 +1,268 @@
1
+ export const textEncoder = new TextEncoder();
2
+
3
+ const decoders = new Map();
4
+
5
+ const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
6
+
7
+ // Use a lookup table to find the index.
8
+ const base64Lookup = new Uint8Array(256);
9
+ for (var i = 0; i < base64Chars.length; i++) {
10
+ base64Lookup[base64Chars.charCodeAt(i)] = i;
11
+ }
12
+
13
+ export function decodeBase64(base64) {
14
+ let bufferLength = Math.ceil(base64.length / 4) * 3;
15
+ const len = base64.length;
16
+
17
+ let p = 0;
18
+
19
+ if (base64.length % 4 === 3) {
20
+ bufferLength--;
21
+ } else if (base64.length % 4 === 2) {
22
+ bufferLength -= 2;
23
+ } else if (base64[base64.length - 1] === '=') {
24
+ bufferLength--;
25
+ if (base64[base64.length - 2] === '=') {
26
+ bufferLength--;
27
+ }
28
+ }
29
+
30
+ const arrayBuffer = new ArrayBuffer(bufferLength);
31
+ const bytes = new Uint8Array(arrayBuffer);
32
+
33
+ for (let i = 0; i < len; i += 4) {
34
+ let encoded1 = base64Lookup[base64.charCodeAt(i)];
35
+ let encoded2 = base64Lookup[base64.charCodeAt(i + 1)];
36
+ let encoded3 = base64Lookup[base64.charCodeAt(i + 2)];
37
+ let encoded4 = base64Lookup[base64.charCodeAt(i + 3)];
38
+
39
+ bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
40
+ bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
41
+ bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
42
+ }
43
+
44
+ return arrayBuffer;
45
+ }
46
+
47
+ export function getDecoder(charset) {
48
+ charset = charset || 'utf8';
49
+ if (decoders.has(charset)) {
50
+ return decoders.get(charset);
51
+ }
52
+ let decoder;
53
+ try {
54
+ decoder = new TextDecoder(charset);
55
+ } catch (err) {
56
+ if (charset === 'utf8') {
57
+ // is this even possible?
58
+ throw err;
59
+ }
60
+ // use default
61
+ return getDecoder();
62
+ }
63
+
64
+ decoders.set(charset, decoder);
65
+ return decoder;
66
+ }
67
+
68
+ /**
69
+ * Converts a Blob into an ArrayBuffer
70
+ * @param {Blob} blob Blob to convert
71
+ * @returns {ArrayBuffer} Converted value
72
+ */
73
+ export async function blobToArrayBuffer(blob) {
74
+ if ('arrayBuffer' in blob) {
75
+ return await blob.arrayBuffer();
76
+ }
77
+
78
+ const fr = new FileReader();
79
+
80
+ return new Promise((resolve, reject) => {
81
+ fr.onload = function (e) {
82
+ resolve(e.target.result);
83
+ };
84
+
85
+ fr.onerror = function (e) {
86
+ reject(fr.error);
87
+ };
88
+
89
+ fr.readAsArrayBuffer(blob);
90
+ });
91
+ }
92
+
93
+ export function getHex(c) {
94
+ if ((c >= 0x30 /* 0 */ && c <= 0x39) /* 9 */ || (c >= 0x61 /* a */ && c <= 0x66) /* f */ || (c >= 0x41 /* A */ && c <= 0x46) /* F */) {
95
+ return String.fromCharCode(c);
96
+ }
97
+ return false;
98
+ }
99
+
100
+ /**
101
+ * Decode a complete mime word encoded string
102
+ *
103
+ * @param {String} str Mime word encoded string
104
+ * @return {String} Decoded unicode string
105
+ */
106
+ export function decodeWord(charset, encoding, str) {
107
+ // RFC2231 added language tag to the encoding
108
+ // see: https://tools.ietf.org/html/rfc2231#section-5
109
+ // this implementation silently ignores this tag
110
+ let splitPos = charset.indexOf('*');
111
+ if (splitPos >= 0) {
112
+ charset = charset.substr(0, splitPos);
113
+ }
114
+
115
+ encoding = encoding.toUpperCase();
116
+
117
+ let byteStr;
118
+
119
+ if (encoding === 'Q') {
120
+ str = str
121
+ // remove spaces between = and hex char, this might indicate invalidly applied line splitting
122
+ .replace(/=\s+([0-9a-fA-F])/g, '=$1')
123
+ // convert all underscores to spaces
124
+ .replace(/[_\s]/g, ' ');
125
+
126
+ let buf = textEncoder.encode(str);
127
+ let encodedBytes = [];
128
+ for (let i = 0, len = buf.length; i < len; i++) {
129
+ let c = buf[i];
130
+ if (i <= len - 2 && c === 0x3d /* = */) {
131
+ let c1 = getHex(buf[i + 1]);
132
+ let c2 = getHex(buf[i + 2]);
133
+ if (c1 && c2) {
134
+ let c = parseInt(c1 + c2, 16);
135
+ encodedBytes.push(c);
136
+ i += 2;
137
+ continue;
138
+ }
139
+ }
140
+ encodedBytes.push(c);
141
+ }
142
+ byteStr = new ArrayBuffer(encodedBytes.length);
143
+ let dataView = new DataView(byteStr);
144
+ for (let i = 0, len = encodedBytes.length; i < len; i++) {
145
+ dataView.setUint8(i, encodedBytes[i]);
146
+ }
147
+ } else if (encoding === 'B') {
148
+ byteStr = decodeBase64(str.replace(/[^a-zA-Z0-9\+\/=]+/g, ''));
149
+ } else {
150
+ // keep as is, convert ArrayBuffer to unicode string, assume utf8
151
+ byteStr = textEncoder.encode(str);
152
+ }
153
+
154
+ return getDecoder(charset).decode(byteStr);
155
+ }
156
+
157
+ export function decodeWords(str) {
158
+ return (
159
+ (str || '')
160
+ .toString()
161
+ // find base64 words that can be joined
162
+ .replace(/(=\?([^?]+)\?[Bb]\?[^?]*\?=)\s*(?==\?([^?]+)\?[Bb]\?[^?]*\?=)/g, (match, left, chLeft, chRight) => {
163
+ // only mark b64 chunks to be joined if charsets match
164
+ if (chLeft === chRight) {
165
+ // set a joiner marker
166
+ return left + '__\x00JOIN\x00__';
167
+ }
168
+ return match;
169
+ })
170
+ // find QP words that can be joined
171
+ .replace(/(=\?([^?]+)\?[Qq]\?[^?]*\?=)\s*(?==\?([^?]+)\?[Qq]\?[^?]*\?=)/g, (match, left, chLeft, chRight) => {
172
+ // only mark QP chunks to be joined if charsets match
173
+ if (chLeft === chRight) {
174
+ // set a joiner marker
175
+ return left + '__\x00JOIN\x00__';
176
+ }
177
+ return match;
178
+ })
179
+ // join base64 encoded words
180
+ .replace(/(\?=)?__\x00JOIN\x00__(=\?([^?]+)\?[QqBb]\?)?/g, '')
181
+ // remove spaces between mime encoded words
182
+ .replace(/(=\?[^?]+\?[QqBb]\?[^?]*\?=)\s+(?==\?[^?]+\?[QqBb]\?[^?]*\?=)/g, '$1')
183
+ // decode words
184
+ .replace(/=\?([\w_\-*]+)\?([QqBb])\?([^?]*)\?=/g, (m, charset, encoding, text) => decodeWord(charset, encoding, text))
185
+ );
186
+ }
187
+
188
+ export function decodeURIComponentWithCharset(encodedStr, charset) {
189
+ charset = charset || 'utf-8';
190
+
191
+ let encodedBytes = [];
192
+ for (let i = 0; i < encodedStr.length; i++) {
193
+ let c = encodedStr.charAt(i);
194
+ if (c === '%' && /^[a-f0-9]{2}/i.test(encodedStr.substr(i + 1, 2))) {
195
+ // encoded sequence
196
+ let byte = encodedStr.substr(i + 1, 2);
197
+ i += 2;
198
+ encodedBytes.push(parseInt(byte, 16));
199
+ } else if (c.charCodeAt(0) > 126) {
200
+ c = textEncoder.encode(c);
201
+ for (let j = 0; j < c.length; j++) {
202
+ encodedBytes.push(c[j]);
203
+ }
204
+ } else {
205
+ // "normal" char
206
+ encodedBytes.push(c.charCodeAt(0));
207
+ }
208
+ }
209
+
210
+ const byteStr = new ArrayBuffer(encodedBytes.length);
211
+ const dataView = new DataView(byteStr);
212
+ for (let i = 0, len = encodedBytes.length; i < len; i++) {
213
+ dataView.setUint8(i, encodedBytes[i]);
214
+ }
215
+
216
+ return getDecoder(charset).decode(byteStr);
217
+ }
218
+
219
+ export function decodeParameterValueContinuations(header) {
220
+ // handle parameter value continuations
221
+ // https://tools.ietf.org/html/rfc2231#section-3
222
+
223
+ // preprocess values
224
+ let paramKeys = new Map();
225
+
226
+ Object.keys(header.params).forEach(key => {
227
+ let match = key.match(/\*((\d+)\*?)?$/);
228
+ if (!match) {
229
+ // nothing to do here, does not seem like a continuation param
230
+ return;
231
+ }
232
+
233
+ let actualKey = key.substr(0, match.index).toLowerCase();
234
+ let nr = Number(match[2]) || 0;
235
+
236
+ let paramVal;
237
+ if (!paramKeys.has(actualKey)) {
238
+ paramVal = {
239
+ charset: false,
240
+ values: []
241
+ };
242
+ paramKeys.set(actualKey, paramVal);
243
+ } else {
244
+ paramVal = paramKeys.get(actualKey);
245
+ }
246
+
247
+ let value = header.params[key];
248
+ if (nr === 0 && match[0].charAt(match[0].length - 1) === '*' && (match = value.match(/^([^']*)'[^']*'(.*)$/))) {
249
+ paramVal.charset = match[1] || 'utf-8';
250
+ value = match[2];
251
+ }
252
+
253
+ paramVal.values.push({ nr, value });
254
+
255
+ // remove the old reference
256
+ delete header.params[key];
257
+ });
258
+
259
+ paramKeys.forEach((paramVal, key) => {
260
+ header.params[key] = decodeURIComponentWithCharset(
261
+ paramVal.values
262
+ .sort((a, b) => a.nr - b.nr)
263
+ .map(a => a.value)
264
+ .join(''),
265
+ paramVal.charset
266
+ );
267
+ });
268
+ }