lite-email-parser 2.0.0 → 2.0.1

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 CHANGED
@@ -5,18 +5,29 @@
5
5
  "sources": [
6
6
  "src/addon.cpp",
7
7
  "../core/src/html_parser.cpp",
8
- "../core/src/html_extractor.cpp"
8
+ "../core/src/html_extractor.cpp",
9
+ "../deps/gumbo-parser/src/attribute.c",
10
+ "../deps/gumbo-parser/src/char_ref.c",
11
+ "../deps/gumbo-parser/src/char_ref_gperf.c",
12
+ "../deps/gumbo-parser/src/error.c",
13
+ "../deps/gumbo-parser/src/parser.c",
14
+ "../deps/gumbo-parser/src/string_buffer.c",
15
+ "../deps/gumbo-parser/src/string_piece.c",
16
+ "../deps/gumbo-parser/src/tag.c",
17
+ "../deps/gumbo-parser/src/tokenizer.c",
18
+ "../deps/gumbo-parser/src/utf8.c",
19
+ "../deps/gumbo-parser/src/util.c",
20
+ "../deps/gumbo-parser/src/vector.c"
9
21
  ],
10
22
  "include_dirs": [
11
23
  "<!@(node -p \"require('node-addon-api').include\")",
12
- "../core/include"
24
+ "../core/include",
25
+ "../deps/gumbo-parser/src"
13
26
  ],
14
27
  "dependencies": [
15
28
  "<!(node -p \"require('node-addon-api').targets\"):node_addon_api"
16
29
  ],
17
- "libraries": [
18
- "-lgumbo"
19
- ],
30
+ "libraries": [],
20
31
  "defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
21
32
  "cflags!": [ "-fno-exceptions" ],
22
33
  "cflags_cc!": [ "-fno-exceptions" ],
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.parseEmail = parseEmail;
37
+ exports.cleanHtml = cleanHtml;
38
+ exports.removeSignature = removeSignature;
39
+ exports.removeReplies = removeReplies;
40
+ exports.removeDividers = removeDividers;
41
+ exports.replaceSrc = replaceSrc;
42
+ const path = __importStar(require("path"));
43
+ // Load native addon - CJS uses __dirname
44
+ const packageRoot = path.resolve(__dirname, '..');
45
+ const addon = require(path.join(packageRoot, 'build', 'Release', 'parser.node'));
46
+ async function parseEmail(buffer) {
47
+ return addon.parseEmail(buffer);
48
+ }
49
+ function cleanHtml(html) {
50
+ return addon.cleanHtml(html);
51
+ }
52
+ function removeSignature(html) {
53
+ return addon.removeSignature(html);
54
+ }
55
+ function removeReplies(html) {
56
+ return addon.removeReplies(html);
57
+ }
58
+ function removeDividers(html) {
59
+ return addon.removeDividers(html);
60
+ }
61
+ function replaceSrc(html, files) {
62
+ return addon.replaceSrc(html, files);
63
+ }
@@ -0,0 +1,9 @@
1
+ import { IFile, ParseEmailResult } from './types.js';
2
+ export declare function parseEmail(buffer: Buffer): Promise<ParseEmailResult>;
3
+ export declare function cleanHtml(html: string): string;
4
+ export declare function removeSignature(html: string): string;
5
+ export declare function removeReplies(html: string): string;
6
+ export declare function removeDividers(html: string): string;
7
+ export declare function replaceSrc(html: string, files: IFile[]): string;
8
+ export type { IFile, ParseEmailResult } from './types.js';
9
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/index.cts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAMrD,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAE1E;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAE/D;AAGD,YAAY,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAUrD,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAE1E;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAE/D;AAGD,YAAY,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAyCrD,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAE1E;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAE/D;AAGD,YAAY,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
package/dist/cjs/index.js CHANGED
@@ -39,10 +39,39 @@ exports.removeSignature = removeSignature;
39
39
  exports.removeReplies = removeReplies;
40
40
  exports.removeDividers = removeDividers;
41
41
  exports.replaceSrc = replaceSrc;
42
+ const module_1 = require("module");
42
43
  const path = __importStar(require("path"));
43
- const addonPath = path.resolve(__dirname, '..', 'build', 'Release', 'parser.node');
44
- // eslint-disable-next-line @typescript-eslint/no-require-imports
45
- const addon = require(addonPath);
44
+ const _require = typeof require !== 'undefined'
45
+ ? require
46
+ : (0, module_1.createRequire)(process.cwd());
47
+ const getDirname = () => {
48
+ if (typeof __dirname !== 'undefined') {
49
+ return __dirname;
50
+ }
51
+ const err = new Error();
52
+ const stackLines = err.stack?.split('\n') || [];
53
+ for (const line of stackLines) {
54
+ const match = line.match(/(?:at\s+|\()((?:file:\/\/)?\/[^:]+)/);
55
+ if (match) {
56
+ const p = match[1];
57
+ if (p.includes('index.js') || p.includes('index.ts')) {
58
+ let cleanPath = p;
59
+ if (cleanPath.startsWith('file://')) {
60
+ const fileURLToPath = _require('url').fileURLToPath;
61
+ cleanPath = fileURLToPath(cleanPath);
62
+ }
63
+ return path.dirname(cleanPath);
64
+ }
65
+ }
66
+ }
67
+ return process.cwd();
68
+ };
69
+ const dirname = getDirname();
70
+ let addonPath = path.resolve(dirname, '../build/Release/parser.node');
71
+ if (!_require('fs').existsSync(addonPath)) {
72
+ addonPath = path.resolve(dirname, '../../build/Release/parser.node');
73
+ }
74
+ const addon = _require(addonPath);
46
75
  async function parseEmail(buffer) {
47
76
  return addon.parseEmail(buffer);
48
77
  }
@@ -0,0 +1,4 @@
1
+ import { IFile, ParseEmailResult } from './types.js';
2
+ export declare function parseEmail(buffer: Buffer | string): Promise<ParseEmailResult>;
3
+ export declare function replaceSrc(html: string, files: IFile[]): string;
4
+ //# sourceMappingURL=parser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/parser.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAyCrD,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,GACtB,OAAO,CAAC,gBAAgB,CAAC,CAsC3B;AAKD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAW/D"}
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.parseEmail = parseEmail;
37
+ exports.replaceSrc = replaceSrc;
38
+ const mailparser_1 = require("mailparser");
39
+ const cheerio = __importStar(require("cheerio"));
40
+ const module_1 = require("module");
41
+ const path = __importStar(require("path"));
42
+ const _require = typeof require !== 'undefined'
43
+ ? require
44
+ : (0, module_1.createRequire)(process.cwd());
45
+ const getDirname = () => {
46
+ if (typeof __dirname !== 'undefined') {
47
+ return __dirname;
48
+ }
49
+ const err = new Error();
50
+ const stackLines = err.stack?.split('\n') || [];
51
+ for (const line of stackLines) {
52
+ const match = line.match(/(?:at\s+|\()((?:file:\/\/)?\/[^:]+)/);
53
+ if (match) {
54
+ const p = match[1];
55
+ if (p.includes('parser.js') || p.includes('parser.ts')) {
56
+ let cleanPath = p;
57
+ if (cleanPath.startsWith('file://')) {
58
+ const fileURLToPath = _require('url').fileURLToPath;
59
+ cleanPath = fileURLToPath(cleanPath);
60
+ }
61
+ return path.dirname(cleanPath);
62
+ }
63
+ }
64
+ }
65
+ return process.cwd();
66
+ };
67
+ const dirname = getDirname();
68
+ let addonPath = path.resolve(dirname, '../build/Release/parser.node');
69
+ if (!_require('fs').existsSync(addonPath)) {
70
+ addonPath = path.resolve(dirname, '../../build/Release/parser.node');
71
+ }
72
+ const addon = _require(addonPath);
73
+ async function parseEmail(buffer) {
74
+ const parsedEmail = await (0, mailparser_1.simpleParser)(buffer);
75
+ let content = parsedEmail.html;
76
+ if (!content) {
77
+ content = parsedEmail.text || '';
78
+ }
79
+ const cleanedContent = extractFirstMessage(content);
80
+ const attachments = [];
81
+ const inlineAttachments = [];
82
+ for (const att of parsedEmail.attachments || []) {
83
+ const filename = att.filename || new Date().toDateString();
84
+ const contentType = att.contentType || 'application/octet-stream';
85
+ const file = {
86
+ name: filename,
87
+ type: contentType,
88
+ size: att.size,
89
+ buffer: att.content,
90
+ };
91
+ if (att.related) {
92
+ const base64Data = att.content.toString('base64');
93
+ file.originalSrc = `data:${contentType};base64,${base64Data}`;
94
+ inlineAttachments.push(file);
95
+ }
96
+ else {
97
+ attachments.push(file);
98
+ }
99
+ }
100
+ return {
101
+ lastMessage: cleanedContent,
102
+ attachments,
103
+ inlineAttachments,
104
+ };
105
+ }
106
+ // Replace src attributes with the uploaded file URLs
107
+ function replaceSrc(html, files) {
108
+ const $ = cheerio.load(html);
109
+ for (const file of files) {
110
+ if (file.originalSrc && file.src) {
111
+ $(`img[src="${file.originalSrc}"]`).attr('src', file.src);
112
+ }
113
+ }
114
+ return $.html();
115
+ }
116
+ function extractFirstMessage(html) {
117
+ return addon.cleanHtml(html);
118
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.parseEmail = parseEmail;
37
+ exports.cleanHtml = cleanHtml;
38
+ exports.removeSignature = removeSignature;
39
+ exports.removeReplies = removeReplies;
40
+ exports.removeDividers = removeDividers;
41
+ exports.replaceSrc = replaceSrc;
42
+ const path = __importStar(require("path"));
43
+ // Load native addon - CJS uses __dirname
44
+ const packageRoot = path.resolve(__dirname, '..');
45
+ const addon = require(path.join(packageRoot, 'build', 'Release', 'parser.node'));
46
+ async function parseEmail(buffer) {
47
+ return addon.parseEmail(buffer);
48
+ }
49
+ function cleanHtml(html) {
50
+ return addon.cleanHtml(html);
51
+ }
52
+ function removeSignature(html) {
53
+ return addon.removeSignature(html);
54
+ }
55
+ function removeReplies(html) {
56
+ return addon.removeReplies(html);
57
+ }
58
+ function removeDividers(html) {
59
+ return addon.removeDividers(html);
60
+ }
61
+ function replaceSrc(html, files) {
62
+ return addon.replaceSrc(html, files);
63
+ }
@@ -0,0 +1,9 @@
1
+ import { IFile, ParseEmailResult } from './types.js';
2
+ export declare function parseEmail(buffer: Buffer): Promise<ParseEmailResult>;
3
+ export declare function cleanHtml(html: string): string;
4
+ export declare function removeSignature(html: string): string;
5
+ export declare function removeReplies(html: string): string;
6
+ export declare function removeDividers(html: string): string;
7
+ export declare function replaceSrc(html: string, files: IFile[]): string;
8
+ export type { IFile, ParseEmailResult } from './types.js';
9
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.cts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAMrD,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAE1E;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAE/D;AAGD,YAAY,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAUrD,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAE1E;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAE/D;AAGD,YAAY,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAyCrD,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAE1E;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAE/D;AAGD,YAAY,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,7 +1,36 @@
1
+ import { createRequire } from 'module';
1
2
  import * as path from 'path';
2
- const addonPath = path.resolve(__dirname, '..', 'build', 'Release', 'parser.node');
3
- // eslint-disable-next-line @typescript-eslint/no-require-imports
4
- const addon = require(addonPath);
3
+ const _require = typeof require !== 'undefined'
4
+ ? require
5
+ : createRequire(process.cwd());
6
+ const getDirname = () => {
7
+ if (typeof __dirname !== 'undefined') {
8
+ return __dirname;
9
+ }
10
+ const err = new Error();
11
+ const stackLines = err.stack?.split('\n') || [];
12
+ for (const line of stackLines) {
13
+ const match = line.match(/(?:at\s+|\()((?:file:\/\/)?\/[^:]+)/);
14
+ if (match) {
15
+ const p = match[1];
16
+ if (p.includes('index.js') || p.includes('index.ts')) {
17
+ let cleanPath = p;
18
+ if (cleanPath.startsWith('file://')) {
19
+ const fileURLToPath = _require('url').fileURLToPath;
20
+ cleanPath = fileURLToPath(cleanPath);
21
+ }
22
+ return path.dirname(cleanPath);
23
+ }
24
+ }
25
+ }
26
+ return process.cwd();
27
+ };
28
+ const dirname = getDirname();
29
+ let addonPath = path.resolve(dirname, '../build/Release/parser.node');
30
+ if (!_require('fs').existsSync(addonPath)) {
31
+ addonPath = path.resolve(dirname, '../../build/Release/parser.node');
32
+ }
33
+ const addon = _require(addonPath);
5
34
  export async function parseEmail(buffer) {
6
35
  return addon.parseEmail(buffer);
7
36
  }
@@ -0,0 +1,4 @@
1
+ import { IFile, ParseEmailResult } from './types.js';
2
+ export declare function parseEmail(buffer: Buffer | string): Promise<ParseEmailResult>;
3
+ export declare function replaceSrc(html: string, files: IFile[]): string;
4
+ //# sourceMappingURL=parser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAyCrD,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,GACtB,OAAO,CAAC,gBAAgB,CAAC,CAsC3B;AAKD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAW/D"}
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.0",
3
+ "version": "2.0.1",
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",