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.
Files changed (52) hide show
  1. package/binding.gyp +20 -6
  2. package/core/include/html_extractor.h +100 -0
  3. package/core/include/html_parser.h +13 -0
  4. package/core/src/html_extractor.cpp +662 -0
  5. package/core/src/html_parser.cpp +221 -0
  6. package/deps/gumbo-parser/src/attribute.c +44 -0
  7. package/deps/gumbo-parser/src/attribute.h +37 -0
  8. package/deps/gumbo-parser/src/char_ref.c +301 -0
  9. package/deps/gumbo-parser/src/char_ref.h +70 -0
  10. package/deps/gumbo-parser/src/char_ref_gperf.c +11126 -0
  11. package/deps/gumbo-parser/src/error.c +287 -0
  12. package/deps/gumbo-parser/src/error.h +225 -0
  13. package/deps/gumbo-parser/src/gumbo.h +683 -0
  14. package/deps/gumbo-parser/src/insertion_mode.h +55 -0
  15. package/deps/gumbo-parser/src/parser.c +4433 -0
  16. package/deps/gumbo-parser/src/parser.h +57 -0
  17. package/deps/gumbo-parser/src/string_buffer.c +110 -0
  18. package/deps/gumbo-parser/src/string_buffer.h +84 -0
  19. package/deps/gumbo-parser/src/string_piece.c +48 -0
  20. package/deps/gumbo-parser/src/string_piece.h +38 -0
  21. package/deps/gumbo-parser/src/tag.c +125 -0
  22. package/deps/gumbo-parser/src/tag_enum.h +155 -0
  23. package/deps/gumbo-parser/src/tag_gperf.h +350 -0
  24. package/deps/gumbo-parser/src/tag_sizes.h +2 -0
  25. package/deps/gumbo-parser/src/tag_strings.h +155 -0
  26. package/deps/gumbo-parser/src/token_type.h +42 -0
  27. package/deps/gumbo-parser/src/tokenizer.c +3003 -0
  28. package/deps/gumbo-parser/src/tokenizer.h +123 -0
  29. package/deps/gumbo-parser/src/tokenizer_states.h +104 -0
  30. package/deps/gumbo-parser/src/utf8.c +270 -0
  31. package/deps/gumbo-parser/src/utf8.h +132 -0
  32. package/deps/gumbo-parser/src/util.c +58 -0
  33. package/deps/gumbo-parser/src/util.h +60 -0
  34. package/deps/gumbo-parser/src/vector.c +128 -0
  35. package/deps/gumbo-parser/src/vector.h +70 -0
  36. package/dist/cjs/index.cjs +63 -0
  37. package/dist/cjs/index.d.cts +9 -0
  38. package/dist/cjs/index.d.cts.map +1 -0
  39. package/dist/cjs/index.d.ts.map +1 -1
  40. package/dist/cjs/index.js +32 -3
  41. package/dist/cjs/parser.d.ts +4 -0
  42. package/dist/cjs/parser.d.ts.map +1 -0
  43. package/dist/cjs/parser.js +118 -0
  44. package/dist/index.cjs +63 -0
  45. package/dist/index.d.cts +9 -0
  46. package/dist/index.d.cts.map +1 -0
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +32 -3
  49. package/dist/parser.d.ts +4 -0
  50. package/dist/parser.d.ts.map +1 -0
  51. package/dist/parser.js +81 -0
  52. package/package.json +11 -3
@@ -0,0 +1,60 @@
1
+ // Copyright 2010 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Author: jdtang@google.com (Jonathan Tang)
16
+ //
17
+ // This contains some utility functions that didn't fit into any of the other
18
+ // headers.
19
+
20
+ #ifndef GUMBO_UTIL_H_
21
+ #define GUMBO_UTIL_H_
22
+ #ifdef _MSC_VER
23
+ #define _CRT_SECURE_NO_WARNINGS
24
+ #endif
25
+ #include <stdbool.h>
26
+ #include <stddef.h>
27
+
28
+ #ifdef __cplusplus
29
+ extern "C" {
30
+ #endif
31
+
32
+ // Forward declaration since it's passed into some of the functions in this
33
+ // header.
34
+ struct GumboInternalParser;
35
+
36
+ // Utility function for allocating & copying a null-terminated string into a
37
+ // freshly-allocated buffer. This is necessary for proper memory management; we
38
+ // have the convention that all const char* in parse tree structures are
39
+ // freshly-allocated, so if we didn't copy, we'd try to delete a literal string
40
+ // when the parse tree is destroyed.
41
+ char* gumbo_copy_stringz(struct GumboInternalParser* parser, const char* str);
42
+
43
+ // Allocate a chunk of memory, using the allocator specified in the Parser's
44
+ // config options.
45
+ void* gumbo_parser_allocate(
46
+ struct GumboInternalParser* parser, size_t num_bytes);
47
+
48
+ // Deallocate a chunk of memory, using the deallocator specified in the Parser's
49
+ // config options.
50
+ void gumbo_parser_deallocate(struct GumboInternalParser* parser, void* ptr);
51
+
52
+ // Debug wrapper for printf, to make it easier to turn off debugging info when
53
+ // required.
54
+ void gumbo_debug(const char* format, ...);
55
+
56
+ #ifdef __cplusplus
57
+ }
58
+ #endif
59
+
60
+ #endif // GUMBO_UTIL_H_
@@ -0,0 +1,128 @@
1
+ // Copyright 2010 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Author: jdtang@google.com (Jonathan Tang)
16
+
17
+ #include "vector.h"
18
+
19
+ #include <assert.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+ #include <strings.h>
23
+
24
+ #include "util.h"
25
+
26
+ struct GumboInternalParser;
27
+
28
+ const GumboVector kGumboEmptyVector = {NULL, 0, 0};
29
+
30
+ void gumbo_vector_init(struct GumboInternalParser* parser,
31
+ size_t initial_capacity, GumboVector* vector) {
32
+ vector->length = 0;
33
+ vector->capacity = initial_capacity;
34
+ if (initial_capacity > 0) {
35
+ vector->data =
36
+ gumbo_parser_allocate(parser, sizeof(void*) * initial_capacity);
37
+ } else {
38
+ vector->data = NULL;
39
+ }
40
+ }
41
+
42
+ void gumbo_vector_clear(
43
+ struct GumboInternalParser* parser, GumboVector* vector) {
44
+ vector->length = 0;
45
+ }
46
+
47
+ void gumbo_vector_destroy(
48
+ struct GumboInternalParser* parser, GumboVector* vector) {
49
+ if (vector->capacity > 0) {
50
+ gumbo_parser_deallocate(parser, vector->data);
51
+ }
52
+ }
53
+
54
+ static void enlarge_vector_if_full(
55
+ struct GumboInternalParser* parser, GumboVector* vector) {
56
+ if (vector->length >= vector->capacity) {
57
+ if (vector->capacity) {
58
+ size_t old_num_bytes = sizeof(void*) * vector->capacity;
59
+ vector->capacity *= 2;
60
+ size_t num_bytes = sizeof(void*) * vector->capacity;
61
+ void** temp = gumbo_parser_allocate(parser, num_bytes);
62
+ memcpy(temp, vector->data, old_num_bytes);
63
+ gumbo_parser_deallocate(parser, vector->data);
64
+ vector->data = temp;
65
+ } else {
66
+ // 0-capacity vector; no previous array to deallocate.
67
+ vector->capacity = 2;
68
+ vector->data =
69
+ gumbo_parser_allocate(parser, sizeof(void*) * vector->capacity);
70
+ }
71
+ }
72
+ }
73
+
74
+ void gumbo_vector_add(
75
+ struct GumboInternalParser* parser, void* element, GumboVector* vector) {
76
+ enlarge_vector_if_full(parser, vector);
77
+ assert(vector->data);
78
+ assert(vector->length < vector->capacity);
79
+ vector->data[vector->length++] = element;
80
+ }
81
+
82
+ void* gumbo_vector_pop(
83
+ struct GumboInternalParser* parser, GumboVector* vector) {
84
+ if (vector->length == 0) {
85
+ return NULL;
86
+ }
87
+ return vector->data[--vector->length];
88
+ }
89
+
90
+ int gumbo_vector_index_of(GumboVector* vector, const void* element) {
91
+ for (unsigned int i = 0; i < vector->length; ++i) {
92
+ if (vector->data[i] == element) {
93
+ return i;
94
+ }
95
+ }
96
+ return -1;
97
+ }
98
+
99
+ void gumbo_vector_insert_at(struct GumboInternalParser* parser, void* element,
100
+ unsigned int index, GumboVector* vector) {
101
+ assert(index >= 0);
102
+ assert(index <= vector->length);
103
+ enlarge_vector_if_full(parser, vector);
104
+ ++vector->length;
105
+ memmove(&vector->data[index + 1], &vector->data[index],
106
+ sizeof(void*) * (vector->length - index - 1));
107
+ vector->data[index] = element;
108
+ }
109
+
110
+ void gumbo_vector_remove(
111
+ struct GumboInternalParser* parser, void* node, GumboVector* vector) {
112
+ int index = gumbo_vector_index_of(vector, node);
113
+ if (index == -1) {
114
+ return;
115
+ }
116
+ gumbo_vector_remove_at(parser, index, vector);
117
+ }
118
+
119
+ void* gumbo_vector_remove_at(struct GumboInternalParser* parser,
120
+ unsigned int index, GumboVector* vector) {
121
+ assert(index >= 0);
122
+ assert(index < vector->length);
123
+ void* result = vector->data[index];
124
+ memmove(&vector->data[index], &vector->data[index + 1],
125
+ sizeof(void*) * (vector->length - index - 1));
126
+ --vector->length;
127
+ return result;
128
+ }
@@ -0,0 +1,70 @@
1
+ // Copyright 2010 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Author: jdtang@google.com (Jonathan Tang)
16
+
17
+ #ifndef GUMBO_VECTOR_H_
18
+ #define GUMBO_VECTOR_H_
19
+
20
+ #include "gumbo.h"
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ // Forward declaration since it's passed into some of the functions in this
27
+ // header.
28
+ struct GumboInternalParser;
29
+
30
+ // Initializes a new GumboVector with the specified initial capacity.
31
+ void gumbo_vector_init(struct GumboInternalParser* parser,
32
+ size_t initial_capacity, GumboVector* vector);
33
+
34
+ void gumbo_vector_clear(
35
+ struct GumboInternalParser* parser, GumboVector* vector);
36
+
37
+ // Frees the memory used by an GumboVector. Does not free the contained
38
+ // pointers.
39
+ void gumbo_vector_destroy(
40
+ struct GumboInternalParser* parser, GumboVector* vector);
41
+
42
+ // Adds a new element to an GumboVector.
43
+ void gumbo_vector_add(
44
+ struct GumboInternalParser* parser, void* element, GumboVector* vector);
45
+
46
+ // Removes and returns the element most recently added to the GumboVector.
47
+ // Ownership is transferred to caller. Capacity is unchanged. If the vector is
48
+ // empty, NULL is returned.
49
+ void* gumbo_vector_pop(struct GumboInternalParser* parser, GumboVector* vector);
50
+
51
+ // Inserts an element at a specific index. This is potentially O(N) time, but
52
+ // is necessary for some of the spec's behavior.
53
+ void gumbo_vector_insert_at(struct GumboInternalParser* parser, void* element,
54
+ unsigned int index, GumboVector* vector);
55
+
56
+ // Removes an element from the vector, or does nothing if the element is not in
57
+ // the vector.
58
+ void gumbo_vector_remove(
59
+ struct GumboInternalParser* parser, void* element, GumboVector* vector);
60
+
61
+ // Removes and returns an element at a specific index. Note that this is
62
+ // potentially O(N) time and should be used sparingly.
63
+ void* gumbo_vector_remove_at(struct GumboInternalParser* parser,
64
+ unsigned int index, GumboVector* vector);
65
+
66
+ #ifdef __cplusplus
67
+ }
68
+ #endif
69
+
70
+ #endif // GUMBO_VECTOR_H_
@@ -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"}