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.
- package/.prettierrc.cjs +8 -0
- package/CHANGELOG.md +15 -0
- package/README.md +0 -2
- package/package.json +4 -4
- package/postal-mime.d.ts +1 -1
- package/src/address-parser.js +321 -0
- package/src/base64-decoder.js +50 -0
- package/src/decode-strings.js +268 -0
- package/src/html-entities.js +2236 -0
- package/src/mime-node.js +271 -0
- package/src/package.json +3 -0
- package/src/pass-through-decoder.js +17 -0
- package/src/postal-mime.js +395 -0
- package/src/qp-decoder.js +96 -0
- package/src/text-format.js +334 -0
- package/.github/workflows/release.yaml +0 -37
- /package/{.eslintrc.js → .eslintrc.cjs} +0 -0
package/.prettierrc.cjs
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.0.2](https://github.com/postalsys/postal-mime/compare/v2.0.1...v2.0.2) (2023-12-08)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **test:** Added a tests runner and some tests ([8c6f7fb](https://github.com/postalsys/postal-mime/commit/8c6f7fb495b0158756fc11482a717e8081cede86))
|
|
9
|
+
* **test:** Added test action ([c43c086](https://github.com/postalsys/postal-mime/commit/c43c0865dae74a7f20e32885a5860d8654f0c932))
|
|
10
|
+
|
|
11
|
+
## [2.0.1](https://github.com/postalsys/postal-mime/compare/v2.0.0...v2.0.1) (2023-11-05)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
* **npm:** DO not ignore src folder when publishing to npm ([ef8a2df](https://github.com/postalsys/postal-mime/commit/ef8a2df8d65be3dcfc52784c5c73c79f820c1c82))
|
|
17
|
+
|
|
3
18
|
## [2.0.0](https://github.com/postalsys/postal-mime/compare/v1.1.0...v2.0.0) (2023-11-03)
|
|
4
19
|
|
|
5
20
|
|
package/README.md
CHANGED
|
@@ -4,8 +4,6 @@ Email parser for browser environments.
|
|
|
4
4
|
|
|
5
5
|
PostalMime can be run in the main web thread or from Web Workers.
|
|
6
6
|
|
|
7
|
-
PostalMime can be bundled using WebPack. In fact the distribution file is also built with WebPack.
|
|
8
|
-
|
|
9
7
|
## Source
|
|
10
8
|
|
|
11
9
|
Source code is available from [Github](https://github.com/postalsys/postal-mime).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postal-mime",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Email parser for browser environments",
|
|
5
5
|
"main": "./src/postal-mime.js",
|
|
6
6
|
"exports": {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"type": "module",
|
|
10
10
|
"types": "postal-mime.d.ts",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"test": "eslint",
|
|
12
|
+
"test": "eslint && node --test",
|
|
13
13
|
"update": "rm -rf node_modules package-lock.json && ncu -u && npm install"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
@@ -27,9 +27,9 @@
|
|
|
27
27
|
"license": "MIT-0",
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"cross-blob": "3.0.2",
|
|
30
|
-
"eslint": "8.
|
|
30
|
+
"eslint": "8.55.0",
|
|
31
31
|
"eslint-cli": "1.1.1",
|
|
32
|
-
"iframe-resizer": "4.3.
|
|
32
|
+
"iframe-resizer": "4.3.9",
|
|
33
33
|
"cross-env": "7.0.3"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/postal-mime.d.ts
CHANGED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { decodeWords } from './decode-strings.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts tokens for a single address into an address object
|
|
5
|
+
*
|
|
6
|
+
* @param {Array} tokens Tokens object
|
|
7
|
+
* @return {Object} Address object
|
|
8
|
+
*/
|
|
9
|
+
function _handleAddress(tokens) {
|
|
10
|
+
let token;
|
|
11
|
+
let isGroup = false;
|
|
12
|
+
let state = 'text';
|
|
13
|
+
let address;
|
|
14
|
+
let addresses = [];
|
|
15
|
+
let data = {
|
|
16
|
+
address: [],
|
|
17
|
+
comment: [],
|
|
18
|
+
group: [],
|
|
19
|
+
text: []
|
|
20
|
+
};
|
|
21
|
+
let i;
|
|
22
|
+
let len;
|
|
23
|
+
|
|
24
|
+
// Filter out <addresses>, (comments) and regular text
|
|
25
|
+
for (i = 0, len = tokens.length; i < len; i++) {
|
|
26
|
+
token = tokens[i];
|
|
27
|
+
if (token.type === 'operator') {
|
|
28
|
+
switch (token.value) {
|
|
29
|
+
case '<':
|
|
30
|
+
state = 'address';
|
|
31
|
+
break;
|
|
32
|
+
case '(':
|
|
33
|
+
state = 'comment';
|
|
34
|
+
break;
|
|
35
|
+
case ':':
|
|
36
|
+
state = 'group';
|
|
37
|
+
isGroup = true;
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
state = 'text';
|
|
41
|
+
}
|
|
42
|
+
} else if (token.value) {
|
|
43
|
+
if (state === 'address') {
|
|
44
|
+
// handle use case where unquoted name includes a "<"
|
|
45
|
+
// Apple Mail truncates everything between an unexpected < and an address
|
|
46
|
+
// and so will we
|
|
47
|
+
token.value = token.value.replace(/^[^<]*<\s*/, '');
|
|
48
|
+
}
|
|
49
|
+
data[state].push(token.value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// If there is no text but a comment, replace the two
|
|
54
|
+
if (!data.text.length && data.comment.length) {
|
|
55
|
+
data.text = data.comment;
|
|
56
|
+
data.comment = [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (isGroup) {
|
|
60
|
+
// http://tools.ietf.org/html/rfc2822#appendix-A.1.3
|
|
61
|
+
data.text = data.text.join(' ');
|
|
62
|
+
addresses.push({
|
|
63
|
+
name: decodeWords(data.text || (address && address.name)),
|
|
64
|
+
group: data.group.length ? addressParser(data.group.join(',')) : []
|
|
65
|
+
});
|
|
66
|
+
} else {
|
|
67
|
+
// If no address was found, try to detect one from regular text
|
|
68
|
+
if (!data.address.length && data.text.length) {
|
|
69
|
+
for (i = data.text.length - 1; i >= 0; i--) {
|
|
70
|
+
if (data.text[i].match(/^[^@\s]+@[^@\s]+$/)) {
|
|
71
|
+
data.address = data.text.splice(i, 1);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let _regexHandler = function (address) {
|
|
77
|
+
if (!data.address.length) {
|
|
78
|
+
data.address = [address.trim()];
|
|
79
|
+
return ' ';
|
|
80
|
+
} else {
|
|
81
|
+
return address;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// still no address
|
|
86
|
+
if (!data.address.length) {
|
|
87
|
+
for (i = data.text.length - 1; i >= 0; i--) {
|
|
88
|
+
// fixed the regex to parse email address correctly when email address has more than one @
|
|
89
|
+
data.text[i] = data.text[i].replace(/\s*\b[^@\s]+@[^\s]+\b\s*/, _regexHandler).trim();
|
|
90
|
+
if (data.address.length) {
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// If there's still is no text but a comment exixts, replace the two
|
|
98
|
+
if (!data.text.length && data.comment.length) {
|
|
99
|
+
data.text = data.comment;
|
|
100
|
+
data.comment = [];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Keep only the first address occurence, push others to regular text
|
|
104
|
+
if (data.address.length > 1) {
|
|
105
|
+
data.text = data.text.concat(data.address.splice(1));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Join values with spaces
|
|
109
|
+
data.text = data.text.join(' ');
|
|
110
|
+
data.address = data.address.join(' ');
|
|
111
|
+
|
|
112
|
+
if (!data.address && /^=\?[^=]+?=$/.test(data.text.trim())) {
|
|
113
|
+
// try to extract words from text content
|
|
114
|
+
const parsedSubAddresses = addressParser(decodeWords(data.text));
|
|
115
|
+
if (parsedSubAddresses && parsedSubAddresses.length) {
|
|
116
|
+
return parsedSubAddresses;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!data.address && isGroup) {
|
|
121
|
+
return [];
|
|
122
|
+
} else {
|
|
123
|
+
address = {
|
|
124
|
+
address: data.address || data.text || '',
|
|
125
|
+
name: decodeWords(data.text || data.address || '')
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
if (address.address === address.name) {
|
|
129
|
+
if ((address.address || '').match(/@/)) {
|
|
130
|
+
address.name = '';
|
|
131
|
+
} else {
|
|
132
|
+
address.address = '';
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
addresses.push(address);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return addresses;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Creates a Tokenizer object for tokenizing address field strings
|
|
145
|
+
*
|
|
146
|
+
* @constructor
|
|
147
|
+
* @param {String} str Address field string
|
|
148
|
+
*/
|
|
149
|
+
class Tokenizer {
|
|
150
|
+
constructor(str) {
|
|
151
|
+
this.str = (str || '').toString();
|
|
152
|
+
this.operatorCurrent = '';
|
|
153
|
+
this.operatorExpecting = '';
|
|
154
|
+
this.node = null;
|
|
155
|
+
this.escaped = false;
|
|
156
|
+
|
|
157
|
+
this.list = [];
|
|
158
|
+
/**
|
|
159
|
+
* Operator tokens and which tokens are expected to end the sequence
|
|
160
|
+
*/
|
|
161
|
+
this.operators = {
|
|
162
|
+
'"': '"',
|
|
163
|
+
'(': ')',
|
|
164
|
+
'<': '>',
|
|
165
|
+
',': '',
|
|
166
|
+
':': ';',
|
|
167
|
+
// Semicolons are not a legal delimiter per the RFC2822 grammar other
|
|
168
|
+
// than for terminating a group, but they are also not valid for any
|
|
169
|
+
// other use in this context. Given that some mail clients have
|
|
170
|
+
// historically allowed the semicolon as a delimiter equivalent to the
|
|
171
|
+
// comma in their UI, it makes sense to treat them the same as a comma
|
|
172
|
+
// when used outside of a group.
|
|
173
|
+
';': ''
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Tokenizes the original input string
|
|
179
|
+
*
|
|
180
|
+
* @return {Array} An array of operator|text tokens
|
|
181
|
+
*/
|
|
182
|
+
tokenize() {
|
|
183
|
+
let chr,
|
|
184
|
+
list = [];
|
|
185
|
+
for (let i = 0, len = this.str.length; i < len; i++) {
|
|
186
|
+
chr = this.str.charAt(i);
|
|
187
|
+
this.checkChar(chr);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
this.list.forEach(node => {
|
|
191
|
+
node.value = (node.value || '').toString().trim();
|
|
192
|
+
if (node.value) {
|
|
193
|
+
list.push(node);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
return list;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Checks if a character is an operator or text and acts accordingly
|
|
202
|
+
*
|
|
203
|
+
* @param {String} chr Character from the address field
|
|
204
|
+
*/
|
|
205
|
+
checkChar(chr) {
|
|
206
|
+
if (this.escaped) {
|
|
207
|
+
// ignore next condition blocks
|
|
208
|
+
} else if (chr === this.operatorExpecting) {
|
|
209
|
+
this.node = {
|
|
210
|
+
type: 'operator',
|
|
211
|
+
value: chr
|
|
212
|
+
};
|
|
213
|
+
this.list.push(this.node);
|
|
214
|
+
this.node = null;
|
|
215
|
+
this.operatorExpecting = '';
|
|
216
|
+
this.escaped = false;
|
|
217
|
+
return;
|
|
218
|
+
} else if (!this.operatorExpecting && chr in this.operators) {
|
|
219
|
+
this.node = {
|
|
220
|
+
type: 'operator',
|
|
221
|
+
value: chr
|
|
222
|
+
};
|
|
223
|
+
this.list.push(this.node);
|
|
224
|
+
this.node = null;
|
|
225
|
+
this.operatorExpecting = this.operators[chr];
|
|
226
|
+
this.escaped = false;
|
|
227
|
+
return;
|
|
228
|
+
} else if (['"', "'"].includes(this.operatorExpecting) && chr === '\\') {
|
|
229
|
+
this.escaped = true;
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (!this.node) {
|
|
234
|
+
this.node = {
|
|
235
|
+
type: 'text',
|
|
236
|
+
value: ''
|
|
237
|
+
};
|
|
238
|
+
this.list.push(this.node);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (chr === '\n') {
|
|
242
|
+
// Convert newlines to spaces. Carriage return is ignored as \r and \n usually
|
|
243
|
+
// go together anyway and there already is a WS for \n. Lone \r means something is fishy.
|
|
244
|
+
chr = ' ';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (chr.charCodeAt(0) >= 0x21 || [' ', '\t'].includes(chr)) {
|
|
248
|
+
// skip command bytes
|
|
249
|
+
this.node.value += chr;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
this.escaped = false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Parses structured e-mail addresses from an address field
|
|
258
|
+
*
|
|
259
|
+
* Example:
|
|
260
|
+
*
|
|
261
|
+
* 'Name <address@domain>'
|
|
262
|
+
*
|
|
263
|
+
* will be converted to
|
|
264
|
+
*
|
|
265
|
+
* [{name: 'Name', address: 'address@domain'}]
|
|
266
|
+
*
|
|
267
|
+
* @param {String} str Address field
|
|
268
|
+
* @return {Array} An array of address objects
|
|
269
|
+
*/
|
|
270
|
+
function addressParser(str, options) {
|
|
271
|
+
options = options || {};
|
|
272
|
+
|
|
273
|
+
let tokenizer = new Tokenizer(str);
|
|
274
|
+
let tokens = tokenizer.tokenize();
|
|
275
|
+
|
|
276
|
+
let addresses = [];
|
|
277
|
+
let address = [];
|
|
278
|
+
let parsedAddresses = [];
|
|
279
|
+
|
|
280
|
+
tokens.forEach(token => {
|
|
281
|
+
if (token.type === 'operator' && (token.value === ',' || token.value === ';')) {
|
|
282
|
+
if (address.length) {
|
|
283
|
+
addresses.push(address);
|
|
284
|
+
}
|
|
285
|
+
address = [];
|
|
286
|
+
} else {
|
|
287
|
+
address.push(token);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
if (address.length) {
|
|
292
|
+
addresses.push(address);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
addresses.forEach(address => {
|
|
296
|
+
address = _handleAddress(address);
|
|
297
|
+
if (address.length) {
|
|
298
|
+
parsedAddresses = parsedAddresses.concat(address);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
if (options.flatten) {
|
|
303
|
+
let addresses = [];
|
|
304
|
+
let walkAddressList = list => {
|
|
305
|
+
list.forEach(address => {
|
|
306
|
+
if (address.group) {
|
|
307
|
+
return walkAddressList(address.group);
|
|
308
|
+
} else {
|
|
309
|
+
addresses.push(address);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
walkAddressList(parsedAddresses);
|
|
314
|
+
return addresses;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return parsedAddresses;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// expose to the world
|
|
321
|
+
export default addressParser;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { decodeBase64, blobToArrayBuffer } from './decode-strings.js';
|
|
2
|
+
|
|
3
|
+
export default class Base64Decoder {
|
|
4
|
+
constructor(opts) {
|
|
5
|
+
opts = opts || {};
|
|
6
|
+
|
|
7
|
+
this.decoder = opts.decoder || new TextDecoder();
|
|
8
|
+
|
|
9
|
+
this.maxChunkSize = 100 * 1024;
|
|
10
|
+
|
|
11
|
+
this.chunks = [];
|
|
12
|
+
|
|
13
|
+
this.remainder = '';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
update(buffer) {
|
|
17
|
+
let str = this.decoder.decode(buffer);
|
|
18
|
+
|
|
19
|
+
if (/[^a-zA-Z0-9+\/]/.test(str)) {
|
|
20
|
+
str = str.replace(/[^a-zA-Z0-9+\/]+/g, '');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this.remainder += str;
|
|
24
|
+
|
|
25
|
+
if (this.remainder.length >= this.maxChunkSize) {
|
|
26
|
+
let allowedBytes = Math.floor(this.remainder.length / 4) * 4;
|
|
27
|
+
let base64Str;
|
|
28
|
+
|
|
29
|
+
if (allowedBytes === this.remainder.length) {
|
|
30
|
+
base64Str = this.remainder;
|
|
31
|
+
this.remainder = '';
|
|
32
|
+
} else {
|
|
33
|
+
base64Str = this.remainder.substr(0, allowedBytes);
|
|
34
|
+
this.remainder = this.remainder.substr(allowedBytes);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (base64Str.length) {
|
|
38
|
+
this.chunks.push(decodeBase64(base64Str));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
finalize() {
|
|
44
|
+
if (this.remainder && !/^=+$/.test(this.remainder)) {
|
|
45
|
+
this.chunks.push(decodeBase64(this.remainder));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return blobToArrayBuffer(new Blob(this.chunks, { type: 'application/octet-stream' }));
|
|
49
|
+
}
|
|
50
|
+
}
|