eml-parser-qaap 1.1.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/package.json ADDED
@@ -0,0 +1,107 @@
1
+ {
2
+ "name": "eml-parser-qaap",
3
+ "version": "1.1.15",
4
+ "description": "format EML file in browser env",
5
+ "main": "./lib/bundle.umd.js",
6
+ "module": "./lib/bundle.esm.js",
7
+ "es2015": "./lib/bundle.esm.js",
8
+ "esm5": "./lib/bundle.esm.js",
9
+ "typings": "./dist/index.d.ts",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "node": "./lib/bundle.cjs.js",
15
+ "require": "./lib/bundle.cjs.js",
16
+ "es2015": "./lib/bundle.esm.js",
17
+ "default": "./lib/bundle.esm.js"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "build:es": "tsc -p ./tsconfig.es2015.json",
23
+ "build:umd": "tsc -p ./tsconfig.umd.json",
24
+ "build:amd": "tsc -p ./tsconfig.amd.json",
25
+ "build:ts": "npm run clean:dist && npm run build && npm run build:es -- --declaration false && npm run build:umd -- --declaration false && npm run build:amd",
26
+ "build:rollup": "rollup --config",
27
+ "build:publish": "npm run clean:dist && npm run build && npm run build:rollup",
28
+ "clean:dist": "rimraf dist",
29
+ "eslint:init": "eslint --init",
30
+ "lint:ci": "eslint --ext .ts src/",
31
+ "lint": "eslint --fix --ext .ts src/",
32
+ "link:lib": "rimraf node_modules/eml-format-js && linklocal",
33
+ "prettier": "prettier --parser typescript --write src/*ts && npm run lint",
34
+ "prepublishOnly": "npm run prettier && npm run build:publish && npm run test",
35
+ "test": "mocha --reporter spec",
36
+ "ci": "npm install"
37
+ },
38
+ "files": [
39
+ "dist/index.d.ts",
40
+ "dist/charset.d.ts",
41
+ "dist/utils.d.ts",
42
+ "dist/interface.d.ts",
43
+ "dist/addressparser.d.ts",
44
+ "src",
45
+ "lib",
46
+ "LICENSE"
47
+ ],
48
+ "private": false,
49
+ "repository": {
50
+ "type": "git",
51
+ "url": ""
52
+ },
53
+ "keywords": [
54
+ "eml",
55
+ "eml-parse",
56
+ "eml-format-js",
57
+ "eml-parse-js",
58
+ "typescript"
59
+ ],
60
+ "author": "bean",
61
+ "license": "MIT",
62
+ "homepage": "http://sajjad Mousavi",
63
+ "contributors": [
64
+ "Charlie Harding (https://github.com/c-harding)",
65
+ "Pádraig Weeks (https://github.com/pmweeks98)",
66
+ "Thomas Oeser (https://github.com/thomasoeser)",
67
+ "Robert Scheinpflug (https://github.com/neversun)",
68
+ "Bean Q (https://github.com/MQpeng)"
69
+ ],
70
+ "devDependencies": {
71
+ "@types/node": "^17.0.21",
72
+ "@types/ramda": "^0.26.36",
73
+ "@typescript-eslint/eslint-plugin": "^2.11.0",
74
+ "@typescript-eslint/parser": "^2.11.0",
75
+ "chai": "^4.2.0",
76
+ "eslint": "^6.7.2",
77
+ "eslint-plugin-prettier": "^3.1.1",
78
+ "linklocal": "^2.8.2",
79
+ "lint-staged": "^10.5.3",
80
+ "mocha": "^6.2.2",
81
+ "prettier": "1.19.1",
82
+ "rimraf": "^3.0.0",
83
+ "rollup": "^1.27.12",
84
+ "rollup-plugin-commonjs": "^10.1.0",
85
+ "rollup-plugin-node-resolve": "^5.2.0",
86
+ "rollup-plugin-typescript": "^1.0.1",
87
+ "typescript": "^3.7.3"
88
+ },
89
+ "dependencies": {
90
+ "@sinonjs/text-encoding": "^0.7.2",
91
+ "js-base64": "^3.7.2"
92
+ },
93
+ "gitHooks": {
94
+ "pre-commit": "lint-staged"
95
+ },
96
+ "lint-staged": {
97
+ "src/**/*.ts": [
98
+ "npm run lint",
99
+ "prettier --parser typescript --write",
100
+ "git add"
101
+ ]
102
+ },
103
+ "directories": {
104
+ "lib": "lib",
105
+ "test": "test"
106
+ }
107
+ }
@@ -0,0 +1,317 @@
1
+ 'use strict';
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 = [] as any[];
15
+ let data: any = {
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: 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 && isGroup) {
113
+ return [];
114
+ } else {
115
+ address = {
116
+ address: data.address || data.text || '',
117
+ name: data.text || data.address || '',
118
+ };
119
+
120
+ if (address.address === address.name) {
121
+ if ((address.address || '').match(/@/)) {
122
+ address.name = '';
123
+ } else {
124
+ address.address = '';
125
+ }
126
+ }
127
+
128
+ addresses.push(address);
129
+ }
130
+ }
131
+
132
+ return addresses;
133
+ }
134
+
135
+ /**
136
+ * Creates a Tokenizer object for tokenizing address field strings
137
+ *
138
+ * @constructor
139
+ * @param {String} str Address field string
140
+ */
141
+ export class Tokenizer {
142
+ str: string;
143
+ operatorCurrent: string;
144
+ operatorExpecting: string;
145
+ node: any;
146
+ escaped: boolean;
147
+ list: any[];
148
+ operators: any;
149
+ constructor(str) {
150
+ this.str = (str || '').toString();
151
+ this.operatorCurrent = '';
152
+ this.operatorExpecting = '';
153
+ this.node = null;
154
+ this.escaped = false;
155
+
156
+ this.list = [];
157
+ /**
158
+ * Operator tokens and which tokens are expected to end the sequence
159
+ */
160
+ this.operators = {
161
+ '"': '"',
162
+ '(': ')',
163
+ '<': '>',
164
+ ',': '',
165
+ ':': ';',
166
+ // Semicolons are not a legal delimiter per the RFC2822 grammar other
167
+ // than for terminating a group, but they are also not valid for any
168
+ // other use in this context. Given that some mail clients have
169
+ // historically allowed the semicolon as a delimiter equivalent to the
170
+ // comma in their UI, it makes sense to treat them the same as a comma
171
+ // when used outside of a group.
172
+ ';': '',
173
+ };
174
+ }
175
+
176
+ /**
177
+ * Tokenizes the original input string
178
+ *
179
+ * @return {Array} An array of operator|text tokens
180
+ */
181
+ tokenize() {
182
+ let chr,
183
+ list: any[] = [];
184
+ for (let i = 0, len = this.str.length; i < len; i++) {
185
+ chr = this.str.charAt(i);
186
+ this.checkChar(chr);
187
+ }
188
+
189
+ this.list.forEach(node => {
190
+ node.value = (node.value || '').toString().trim();
191
+ if (node.value) {
192
+ list.push(node);
193
+ }
194
+ });
195
+
196
+ return list;
197
+ }
198
+
199
+ /**
200
+ * Checks if a character is an operator or text and acts accordingly
201
+ *
202
+ * @param {String} chr Character from the address field
203
+ */
204
+ checkChar(chr) {
205
+ if (this.escaped) {
206
+ // ignore next condition blocks
207
+ } else if (chr === this.operatorExpecting) {
208
+ this.node = {
209
+ type: 'operator',
210
+ value: chr,
211
+ };
212
+ this.list.push(this.node);
213
+ this.node = null;
214
+ this.operatorExpecting = '';
215
+ this.escaped = false;
216
+ return;
217
+ } else if (!this.operatorExpecting && chr in this.operators) {
218
+ this.node = {
219
+ type: 'operator',
220
+ value: chr,
221
+ };
222
+ this.list.push(this.node);
223
+ this.node = null;
224
+ this.operatorExpecting = this.operators[chr];
225
+ this.escaped = false;
226
+ return;
227
+ } else if (['"', "'"].includes(this.operatorExpecting) && chr === '\\') {
228
+ this.escaped = true;
229
+ return;
230
+ }
231
+
232
+ if (!this.node) {
233
+ this.node = {
234
+ type: 'text',
235
+ value: '',
236
+ };
237
+ this.list.push(this.node);
238
+ }
239
+
240
+ if (chr === '\n') {
241
+ // Convert newlines to spaces. Carriage return is ignored as \r and \n usually
242
+ // go together anyway and there already is a WS for \n. Lone \r means something is fishy.
243
+ chr = ' ';
244
+ }
245
+
246
+ if (chr.charCodeAt(0) >= 0x21 || [' ', '\t'].includes(chr)) {
247
+ // skip command bytes
248
+ this.node.value += chr;
249
+ }
250
+
251
+ this.escaped = false;
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Parses structured e-mail addresses from an address field
257
+ *
258
+ * Example:
259
+ *
260
+ * 'Name <address@domain>'
261
+ *
262
+ * will be converted to
263
+ *
264
+ * [{name: 'Name', address: 'address@domain'}]
265
+ *
266
+ * @param {String} str Address field
267
+ * @return {Array} An array of address objects
268
+ */
269
+ export function addressparser(str, options?: any): { name?: string; address?: string }[] {
270
+ options = options || {};
271
+
272
+ let tokenizer = new Tokenizer(str);
273
+ let tokens = tokenizer.tokenize();
274
+
275
+ let addresses = [] as any[];
276
+ let address = [] as any[];
277
+ let parsedAddresses = [] as any[];
278
+
279
+ tokens.forEach(token => {
280
+ if (token.type === 'operator' && (token.value === ',' || token.value === ';')) {
281
+ if (address.length) {
282
+ addresses.push(address);
283
+ }
284
+ address = [];
285
+ } else {
286
+ address.push(token);
287
+ }
288
+ });
289
+
290
+ if (address.length) {
291
+ addresses.push(address);
292
+ }
293
+
294
+ addresses.forEach(address => {
295
+ address = _handleAddress(address);
296
+ if (address.length) {
297
+ parsedAddresses = parsedAddresses.concat(address);
298
+ }
299
+ });
300
+
301
+ if (options.flatten) {
302
+ let addresses = [] as any[];
303
+ let walkAddressList = list => {
304
+ list.forEach(address => {
305
+ if (address.group) {
306
+ return walkAddressList(address.group);
307
+ } else {
308
+ addresses.push(address);
309
+ }
310
+ });
311
+ };
312
+ walkAddressList(parsedAddresses);
313
+ return addresses;
314
+ }
315
+
316
+ return parsedAddresses;
317
+ }
package/src/charset.ts ADDED
@@ -0,0 +1,72 @@
1
+ import { TextDecoder, TextEncoder } from '@sinonjs/text-encoding';
2
+
3
+ /**
4
+ * Encodes an unicode string into an Uint8Array object as UTF-8
5
+ *
6
+ * @param {String} str String to be encoded
7
+ * @return {Uint8Array} UTF-8 encoded typed array
8
+ */
9
+ export const encode = (str: string, fromCharset = 'utf-8'): Uint8Array => new TextEncoder(fromCharset).encode(str);
10
+
11
+ export const arr2str = (arr: Uint8Array) => {
12
+ const CHUNK_SZ = 0x8000;
13
+ const strs = [] as any[];
14
+
15
+ for (let i = 0; i < arr.length; i += CHUNK_SZ) {
16
+ strs.push(String.fromCharCode.apply(null, arr.subarray(i, i + CHUNK_SZ) as any));
17
+ }
18
+
19
+ return strs.join('');
20
+ };
21
+
22
+ /**
23
+ * Decodes a string from Uint8Array to an unicode string using specified encoding
24
+ *
25
+ * @param {Uint8Array} buf Binary data to be decoded
26
+ * @param {String} Binary data is decoded into string using this charset
27
+ * @return {String} Decoded string
28
+ */
29
+ export function decode(buf: Uint8Array, fromCharset = 'utf-8'): string {
30
+ const charsets = [
31
+ { charset: normalizeCharset(fromCharset), fatal: false },
32
+ { charset: 'utf-8', fatal: true },
33
+ { charset: 'iso-8859-15', fatal: false },
34
+ ];
35
+
36
+ for (const { charset, fatal } of charsets) {
37
+ try {
38
+ return new TextDecoder(charset, { fatal }).decode(buf);
39
+ // eslint-disable-next-line no-empty
40
+ } catch (e) {}
41
+ }
42
+
43
+ return arr2str(buf); // all else fails, treat it as binary
44
+ }
45
+
46
+ /**
47
+ * Convert a string from specific encoding to UTF-8 Uint8Array
48
+ *
49
+ * @param {String|Uint8Array} data Data to be encoded
50
+ * @param {String} Source encoding for the string (optional for data of type String)
51
+ * @return {Uint8Array} UTF-8 encoded typed array
52
+ */
53
+ export const convert = (data: string | Uint8Array, fromCharset?: string | undefined): Uint8Array =>
54
+ typeof data === 'string' ? encode(data) : encode(decode(data, fromCharset));
55
+
56
+ function normalizeCharset(charset = 'utf-8') {
57
+ let match;
58
+
59
+ if ((match = charset.match(/^utf[-_]?(\d+)$/i))) {
60
+ return 'UTF-' + match[1];
61
+ }
62
+
63
+ if ((match = charset.match(/^win[-_]?(\d+)$/i))) {
64
+ return 'WINDOWS-' + match[1];
65
+ }
66
+
67
+ if ((match = charset.match(/^latin[-_]?(\d+)$/i))) {
68
+ return 'ISO-8859-' + match[1];
69
+ }
70
+
71
+ return charset;
72
+ }