Skip to content

Commit

Permalink
Merge branch 'master' of github.com:crisp-oss/email-reply-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriansaliou committed Feb 2, 2025
2 parents ef7e7f4 + 3248264 commit 948fb84
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ node_modules/
package-lock.json

.npm

.idea/
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Install the project using NPM:
npm install --save email-reply-parser
```

## RE2 Support

By default, the library relies on the [RE2](https://github.com/uhop/node-re2) regex engine, which provides better performance and avoids issues like [ReDOS](https://en.wikipedia.org/wiki/ReDoS). By default, RE2 will be installed as a peer dependency.

If you want to explicitly exclude RE2, then `npm uninstall re2`.

## Features

This library is used at [Crisp](https://crisp.chat/) everyday with around 1 million inbound emails. Over the years, we improved this library so it can work with most emails.
Expand Down
24 changes: 19 additions & 5 deletions lib/regex.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
var RE2 = require("re2");
let RE2;
let hasRE2;

class RegexList {
constructor() {
// inspiration: https://github.com/spamscanner/url-regex-safe/blob/6c1e2c3b5557709633a2cc971d599469ea395061/src/index.js#L37-L49
this.SafeRegExp = hasRE2 !== false
? (() => {
if (typeof RE2 === 'function') return RE2;
try {
RE2 = require('re2');
return typeof RE2 === 'function' ? RE2 : RegExp;
} catch {
hasRE2 = false;
return RegExp;
}
})()
: RegExp;

this.quoteHeadersRegex = this.buildRe2([
/^-*\s*(On\s.+\s.+\n?wrote:{0,1})\s{0,1}-*$/m, // On DATE, NAME <EMAIL> wrote:
/^-*\s*(Le\s.+\s.+\n?écrit\s?:{0,1})\s{0,1}-*$/m, // Le DATE, NAME <EMAIL> a écrit :
Expand Down Expand Up @@ -70,14 +85,13 @@ class RegexList {
/^Enviado desde (?:\s*.+)$/, // es,

// NL
/^Verzonden vanaf (?:\s*.+)$/, // nl - e.g. Verzonden vanaf Outlook voor Android<https://aka.ms/12345>
/^Verzonden vanaf (?:\s*.+)$/, // nl - e.g. Verzonden vanaf (Outlook voor Android<https://aka.ms/12345>|mijn iPad)
/^Verstuurd vanaf (?:\s*.+)$/, // nl - e.g. Verstuurd vanaf mijn iPad/iPhone
]);
}

buildRe2(regexList) {
return regexList.map((regex) => {
return new RE2(regex);
});
return regexList.map((regex) => new this.SafeRegExp(regex));
}
}

Expand Down
Loading

0 comments on commit 948fb84

Please sign in to comment.