lite-email-parser 2.0.0 → 2.0.3
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/binding.gyp +20 -6
- package/core/include/html_extractor.h +100 -0
- package/core/include/html_parser.h +13 -0
- package/core/src/html_extractor.cpp +662 -0
- package/core/src/html_parser.cpp +221 -0
- package/deps/gumbo-parser/src/attribute.c +44 -0
- package/deps/gumbo-parser/src/attribute.h +37 -0
- package/deps/gumbo-parser/src/char_ref.c +301 -0
- package/deps/gumbo-parser/src/char_ref.h +70 -0
- package/deps/gumbo-parser/src/char_ref_gperf.c +11126 -0
- package/deps/gumbo-parser/src/error.c +287 -0
- package/deps/gumbo-parser/src/error.h +225 -0
- package/deps/gumbo-parser/src/gumbo.h +683 -0
- package/deps/gumbo-parser/src/insertion_mode.h +55 -0
- package/deps/gumbo-parser/src/parser.c +4433 -0
- package/deps/gumbo-parser/src/parser.h +57 -0
- package/deps/gumbo-parser/src/string_buffer.c +110 -0
- package/deps/gumbo-parser/src/string_buffer.h +84 -0
- package/deps/gumbo-parser/src/string_piece.c +48 -0
- package/deps/gumbo-parser/src/string_piece.h +38 -0
- package/deps/gumbo-parser/src/tag.c +125 -0
- package/deps/gumbo-parser/src/tag_enum.h +155 -0
- package/deps/gumbo-parser/src/tag_gperf.h +350 -0
- package/deps/gumbo-parser/src/tag_sizes.h +2 -0
- package/deps/gumbo-parser/src/tag_strings.h +155 -0
- package/deps/gumbo-parser/src/token_type.h +42 -0
- package/deps/gumbo-parser/src/tokenizer.c +3003 -0
- package/deps/gumbo-parser/src/tokenizer.h +123 -0
- package/deps/gumbo-parser/src/tokenizer_states.h +104 -0
- package/deps/gumbo-parser/src/utf8.c +270 -0
- package/deps/gumbo-parser/src/utf8.h +132 -0
- package/deps/gumbo-parser/src/util.c +58 -0
- package/deps/gumbo-parser/src/util.h +60 -0
- package/deps/gumbo-parser/src/vector.c +128 -0
- package/deps/gumbo-parser/src/vector.h +70 -0
- package/dist/cjs/index.cjs +63 -0
- package/dist/cjs/index.d.cts +9 -0
- package/dist/cjs/index.d.cts.map +1 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +32 -3
- package/dist/cjs/parser.d.ts +4 -0
- package/dist/cjs/parser.d.ts.map +1 -0
- package/dist/cjs/parser.js +118 -0
- package/dist/index.cjs +63 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -3
- package/dist/parser.d.ts +4 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +81 -0
- package/package.json +11 -3
package/dist/parser.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { simpleParser } from 'mailparser';
|
|
2
|
+
import * as cheerio from 'cheerio';
|
|
3
|
+
import { createRequire } from 'module';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
const _require = typeof require !== 'undefined'
|
|
6
|
+
? require
|
|
7
|
+
: createRequire(process.cwd());
|
|
8
|
+
const getDirname = () => {
|
|
9
|
+
if (typeof __dirname !== 'undefined') {
|
|
10
|
+
return __dirname;
|
|
11
|
+
}
|
|
12
|
+
const err = new Error();
|
|
13
|
+
const stackLines = err.stack?.split('\n') || [];
|
|
14
|
+
for (const line of stackLines) {
|
|
15
|
+
const match = line.match(/(?:at\s+|\()((?:file:\/\/)?\/[^:]+)/);
|
|
16
|
+
if (match) {
|
|
17
|
+
const p = match[1];
|
|
18
|
+
if (p.includes('parser.js') || p.includes('parser.ts')) {
|
|
19
|
+
let cleanPath = p;
|
|
20
|
+
if (cleanPath.startsWith('file://')) {
|
|
21
|
+
const fileURLToPath = _require('url').fileURLToPath;
|
|
22
|
+
cleanPath = fileURLToPath(cleanPath);
|
|
23
|
+
}
|
|
24
|
+
return path.dirname(cleanPath);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return process.cwd();
|
|
29
|
+
};
|
|
30
|
+
const dirname = getDirname();
|
|
31
|
+
let addonPath = path.resolve(dirname, '../build/Release/parser.node');
|
|
32
|
+
if (!_require('fs').existsSync(addonPath)) {
|
|
33
|
+
addonPath = path.resolve(dirname, '../../build/Release/parser.node');
|
|
34
|
+
}
|
|
35
|
+
const addon = _require(addonPath);
|
|
36
|
+
export async function parseEmail(buffer) {
|
|
37
|
+
const parsedEmail = await simpleParser(buffer);
|
|
38
|
+
let content = parsedEmail.html;
|
|
39
|
+
if (!content) {
|
|
40
|
+
content = parsedEmail.text || '';
|
|
41
|
+
}
|
|
42
|
+
const cleanedContent = extractFirstMessage(content);
|
|
43
|
+
const attachments = [];
|
|
44
|
+
const inlineAttachments = [];
|
|
45
|
+
for (const att of parsedEmail.attachments || []) {
|
|
46
|
+
const filename = att.filename || new Date().toDateString();
|
|
47
|
+
const contentType = att.contentType || 'application/octet-stream';
|
|
48
|
+
const file = {
|
|
49
|
+
name: filename,
|
|
50
|
+
type: contentType,
|
|
51
|
+
size: att.size,
|
|
52
|
+
buffer: att.content,
|
|
53
|
+
};
|
|
54
|
+
if (att.related) {
|
|
55
|
+
const base64Data = att.content.toString('base64');
|
|
56
|
+
file.originalSrc = `data:${contentType};base64,${base64Data}`;
|
|
57
|
+
inlineAttachments.push(file);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
attachments.push(file);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
lastMessage: cleanedContent,
|
|
65
|
+
attachments,
|
|
66
|
+
inlineAttachments,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// Replace src attributes with the uploaded file URLs
|
|
70
|
+
export function replaceSrc(html, files) {
|
|
71
|
+
const $ = cheerio.load(html);
|
|
72
|
+
for (const file of files) {
|
|
73
|
+
if (file.originalSrc && file.src) {
|
|
74
|
+
$(`img[src="${file.originalSrc}"]`).attr('src', file.src);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return $.html();
|
|
78
|
+
}
|
|
79
|
+
function extractFirstMessage(html) {
|
|
80
|
+
return addon.cleanHtml(html);
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lite-email-parser",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Parse email to get signature, replies, attachments and inline images",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -20,7 +20,11 @@
|
|
|
20
20
|
"files": [
|
|
21
21
|
"dist/",
|
|
22
22
|
"binding.gyp",
|
|
23
|
-
"src/addon.cpp"
|
|
23
|
+
"src/addon.cpp",
|
|
24
|
+
"core/include/",
|
|
25
|
+
"core/src/*.cpp",
|
|
26
|
+
"deps/gumbo-parser/src/*.c",
|
|
27
|
+
"deps/gumbo-parser/src/*.h"
|
|
24
28
|
],
|
|
25
29
|
"binary": {
|
|
26
30
|
"module_name": "parser",
|
|
@@ -28,7 +32,9 @@
|
|
|
28
32
|
"host": "https://github.com/philipedc/lite-email-parser/releases/download/",
|
|
29
33
|
"remote_path": "{version}",
|
|
30
34
|
"package_name": "{module_name}-v{version}-napi-v9-{platform}-{arch}.tar.gz",
|
|
31
|
-
"napi_versions": [
|
|
35
|
+
"napi_versions": [
|
|
36
|
+
9
|
|
37
|
+
]
|
|
32
38
|
},
|
|
33
39
|
"scripts": {
|
|
34
40
|
"install": "prebuild-install || node-gyp rebuild",
|
|
@@ -36,6 +42,8 @@
|
|
|
36
42
|
"build:ts": "npx tsc && npx tsc --module commonjs --outDir dist/cjs --declaration && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
|
|
37
43
|
"build": "npm run build:native && npm run build:ts",
|
|
38
44
|
"prebuild": "node-gyp rebuild && tar -czf parser-v2.0.0-napi-v9-linux-x64.tar.gz -C build/Release parser.node",
|
|
45
|
+
"prepublishOnly": "cp -r ../core ./core && mkdir -p ./deps && cp -r ../deps/gumbo-parser ./deps/gumbo-parser",
|
|
46
|
+
"postpublish": "rm -rf ./core ./deps",
|
|
39
47
|
"test": "npx -y tsx samples/sample.ts"
|
|
40
48
|
},
|
|
41
49
|
"contributors": [
|