email-forward-parser 1.8.0 → 1.8.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/README.md CHANGED
@@ -27,6 +27,22 @@ This library is used at [Crisp](https://crisp.chat/) everyday with around 1 mill
27
27
  * Supported clients: Apple Mail, Gmail, Outlook Live / 365, Outlook 2013, Outlook 2019, New Outlook 2019, Yahoo Mail, Thunderbird, Missive, HubSpot, IONOS by 1 & 1, MailMate
28
28
  * Supported locales: Croatian, Czech, Danish, Dutch, English, French, Finnish, German, Hungarian, Italian, Japanese, Norwegian, Polish, Portuguese (Brazil), Portuguese (Portugal), Romanian, Russian, Slovak, Spanish, Swedish, Turkish, Ukrainian
29
29
 
30
+ ## RE2 Support
31
+
32
+ This library uses the [RE2](https://github.com/uhop/node-re2) regex engine when available for better performance and [ReDoS](https://en.wikipedia.org/wiki/ReDoS) protection. It automatically falls back to native JavaScript `RegExp` if RE2 is unavailable or fails to load.
33
+
34
+ RE2 is an optional dependency installed by default. If you encounter native compilation issues or want to exclude it:
35
+
36
+ ```bash
37
+ npm uninstall re2
38
+ ```
39
+
40
+ After a Node.js upgrade, you may need to rebuild RE2:
41
+
42
+ ```bash
43
+ npm rebuild re2
44
+ ```
45
+
30
46
  ## Usage
31
47
 
32
48
  ```js
package/dist/parser.js CHANGED
@@ -7,10 +7,8 @@
7
7
  /**************************************************************************
8
8
  * IMPORTS
9
9
  ***************************************************************************/
10
- // NPM
11
- import RE2 from "re2";
12
10
  // PROJECT: LIB
13
- import { loopRegexes, reconciliateSplitMatch, trimString } from "./utils.js";
11
+ import { buildSafeRegExp, loopRegexes, reconciliateSplitMatch, trimString } from "./utils.js";
14
12
  /**************************************************************************
15
13
  * CONSTANTS
16
14
  ***************************************************************************/
@@ -517,7 +515,7 @@ class Parser {
517
515
  const regexLine = this.buildLineRegex(regex);
518
516
  builtLine.push(regexLine);
519
517
  }
520
- built.push(new RE2(regex));
518
+ built.push(buildSafeRegExp(regex));
521
519
  }
522
520
  regexesMap[key] = built;
523
521
  if (LINE_REGEXES.includes(key)) {
@@ -530,7 +528,7 @@ class Parser {
530
528
  if (LINE_REGEXES.includes(key)) {
531
529
  regexesMap[keyLine] = this.buildLineRegex(single);
532
530
  }
533
- regexesMap[key] = new RE2(single);
531
+ regexesMap[key] = buildSafeRegExp(single);
534
532
  }
535
533
  }
536
534
  }
@@ -546,7 +544,7 @@ class Parser {
546
544
  // properties correctly populated.
547
545
  const source = `(${regex.source})`;
548
546
  const flags = regex.flags;
549
- return new RE2(source, flags);
547
+ return buildSafeRegExp(source, flags);
550
548
  }
551
549
  /**
552
550
  * Parses the body part
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- /**************************************************************************
2
- * FUNCTIONS
3
- ***************************************************************************/
1
+ /**
2
+ * Builds a safe regular expression (RE2 when available, RegExp otherwise)
3
+ */
4
+ export declare function buildSafeRegExp(source: string | RegExp, flags?: string): RegExp;
4
5
  type LoopRegexesMode = "match" | "split" | "replace";
5
6
  type LoopRegexesResult<Mode extends LoopRegexesMode> = (Mode extends "replace" ? string : RegExpMatchArray);
6
7
  /**
package/dist/utils.js CHANGED
@@ -8,7 +8,59 @@
8
8
  * IMPORTS
9
9
  ***************************************************************************/
10
10
  // NPM
11
- import RE2 from "re2";
11
+ import { createRequire } from "module";
12
+ const require = createRequire(import.meta.url);
13
+ /**************************************************************************
14
+ * INSTANCES
15
+ ***************************************************************************/
16
+ const RE2Engine = detectRE2();
17
+ /**************************************************************************
18
+ * FUNCTIONS
19
+ ***************************************************************************/
20
+ /**
21
+ * Detects whether the RE2 engine is available
22
+ */
23
+ function detectRE2() {
24
+ // Notice: RE2 can be explicitly disabled, to test the RegExp fallback
25
+ if (process.env.EMAIL_FORWARD_PARSER_DISABLE_RE2) {
26
+ return null;
27
+ }
28
+ try {
29
+ return require("re2");
30
+ }
31
+ catch (_a) {
32
+ return null;
33
+ }
34
+ }
35
+ /**
36
+ * Builds a safe regular expression (RE2 when available, RegExp otherwise)
37
+ */
38
+ export function buildSafeRegExp(source, flags) {
39
+ // Important: never pass an undefined 'flags', as RE2 would drop the flags
40
+ if (RE2Engine !== null) {
41
+ return ((flags === undefined)
42
+ ? new RE2Engine(source)
43
+ : new RE2Engine(source, flags));
44
+ }
45
+ return ((flags === undefined) ? new RegExp(source) : new RegExp(source, flags));
46
+ }
47
+ /**
48
+ * Executes a regular expression on a string, in the given mode
49
+ */
50
+ function executeRegex(regex, str, mode) {
51
+ // Notice: call String methods, as they support both RE2 and RegExp
52
+ switch (mode) {
53
+ case "replace": {
54
+ return str.replace(regex, "");
55
+ }
56
+ case "split": {
57
+ return str.split(regex);
58
+ }
59
+ default: {
60
+ return str.match(regex);
61
+ }
62
+ }
63
+ }
12
64
  /**
13
65
  * Executes multiple regular expressions until one succeeds
14
66
  */
@@ -20,10 +72,9 @@ export function loopRegexes(regexes, str, mode = "match", highestPosition = true
20
72
  let regex = regexes[i];
21
73
  if (typeof regex === "string") {
22
74
  // Build the regex
23
- regex = new RE2(regex);
75
+ regex = buildSafeRegExp(regex);
24
76
  }
25
- const activeMode = mode;
26
- const currentMatch = (regex[activeMode](str || "", (mode === "replace") ? "" : undefined));
77
+ const currentMatch = executeRegex(regex, (str || ""), mode);
27
78
  if (mode === "replace") {
28
79
  match = currentMatch;
29
80
  // A successfull replace must return a smaller string than the input
@@ -43,7 +94,8 @@ export function loopRegexes(regexes, str, mode = "match", highestPosition = true
43
94
  else {
44
95
  let higher = false;
45
96
  if (mode === "match") {
46
- higher = match.index > currentMatch.index;
97
+ higher = (match.index >
98
+ currentMatch.index);
47
99
  }
48
100
  else if (mode === "split") {
49
101
  higher = match[0].length > currentMatch[0].length;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "email-forward-parser",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
4
4
  "description": "Parses forwarded emails and extract content",
5
5
  "author": "Eliott Vincent <eliott@crisp.chat>",
6
6
  "main": "dist/index.js",
@@ -29,7 +29,9 @@
29
29
  "build": "npx tsc",
30
30
  "lint": "eslint .",
31
31
  "unit": "node ./node_modules/nodeunit-x/bin/nodeunit test/index.js",
32
- "test": "npm run unit && npm run lint"
32
+ "unit:regexp": "EMAIL_FORWARD_PARSER_DISABLE_RE2=1 npm run unit",
33
+ "pretest": "npm run build",
34
+ "test": "npm run unit && npm run unit:regexp && npm run lint"
33
35
  },
34
36
  "keywords": [
35
37
  "parser",
@@ -45,11 +47,11 @@
45
47
  "eslint-plugin-crisp": "1.4.12",
46
48
  "eslint-plugin-jsdoc": "54.1.0",
47
49
  "nodeunit-x": "0.15.0",
48
- "re2": "1.22.3",
50
+ "re2": "1.26.0",
49
51
  "typescript": "5.9.2"
50
52
  },
51
53
  "peerDependencies": {
52
- "re2": "1.22.3"
54
+ "re2": "1.26.0"
53
55
  },
54
56
  "peerDependenciesMeta": {
55
57
  "re2": {