email-forward-parser 1.7.2 → 1.8.0
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/dist/index.d.ts +43 -0
- package/dist/index.js +67 -0
- package/dist/parser.d.ts +85 -0
- package/dist/parser.js +829 -0
- package/dist/utils.d.ts +18 -0
- package/dist/utils.js +98 -0
- package/package.json +35 -12
- package/lib/index.d.ts +0 -46
- package/lib/index.js +0 -74
- package/lib/parser.js +0 -1107
- package/lib/utils.js +0 -148
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**************************************************************************
|
|
2
|
+
* FUNCTIONS
|
|
3
|
+
***************************************************************************/
|
|
4
|
+
type LoopRegexesMode = "match" | "split" | "replace";
|
|
5
|
+
type LoopRegexesResult<Mode extends LoopRegexesMode> = (Mode extends "replace" ? string : RegExpMatchArray);
|
|
6
|
+
/**
|
|
7
|
+
* Executes multiple regular expressions until one succeeds
|
|
8
|
+
*/
|
|
9
|
+
export declare function loopRegexes<Mode extends LoopRegexesMode = "match">(regexes: RegExp[], str: string, mode?: Mode, highestPosition?: boolean): LoopRegexesResult<Mode>;
|
|
10
|
+
/**
|
|
11
|
+
* Reconciliates match substrings after a "split" operation
|
|
12
|
+
*/
|
|
13
|
+
export declare function reconciliateSplitMatch(match: string[], minSubstrings: number, defaultSubstrings: number[], fnExclude?: ((index: number) => boolean) | null): string;
|
|
14
|
+
/**
|
|
15
|
+
* Trims a string
|
|
16
|
+
*/
|
|
17
|
+
export declare function trimString(str: string | null): string;
|
|
18
|
+
export {};
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of email-forward-parser
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2026 Crisp IM SAS
|
|
5
|
+
* All rights belong to Crisp IM SAS
|
|
6
|
+
*/
|
|
7
|
+
/**************************************************************************
|
|
8
|
+
* IMPORTS
|
|
9
|
+
***************************************************************************/
|
|
10
|
+
// NPM
|
|
11
|
+
import RE2 from "re2";
|
|
12
|
+
/**
|
|
13
|
+
* Executes multiple regular expressions until one succeeds
|
|
14
|
+
*/
|
|
15
|
+
export function loopRegexes(regexes, str, mode = "match", highestPosition = true) {
|
|
16
|
+
let match = null;
|
|
17
|
+
const minLength = (mode === "split") ? 1 : 0;
|
|
18
|
+
const maxLength = str.length;
|
|
19
|
+
for (let i = 0; i < regexes.length; i++) {
|
|
20
|
+
let regex = regexes[i];
|
|
21
|
+
if (typeof regex === "string") {
|
|
22
|
+
// Build the regex
|
|
23
|
+
regex = new RE2(regex);
|
|
24
|
+
}
|
|
25
|
+
const activeMode = mode;
|
|
26
|
+
const currentMatch = (regex[activeMode](str || "", (mode === "replace") ? "" : undefined));
|
|
27
|
+
if (mode === "replace") {
|
|
28
|
+
match = currentMatch;
|
|
29
|
+
// A successfull replace must return a smaller string than the input
|
|
30
|
+
if (match.length < maxLength) {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else if (mode === "split" || mode === "match") {
|
|
35
|
+
// A successfull split must return an array with at least two items
|
|
36
|
+
if (((currentMatch === null || currentMatch === void 0 ? void 0 : currentMatch.length) || 0) > minLength) {
|
|
37
|
+
if (highestPosition === true) {
|
|
38
|
+
// No previous match?
|
|
39
|
+
if (!match) {
|
|
40
|
+
// Save match and continue looking for other matches
|
|
41
|
+
match = currentMatch;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
let higher = false;
|
|
45
|
+
if (mode === "match") {
|
|
46
|
+
higher = match.index > currentMatch.index;
|
|
47
|
+
}
|
|
48
|
+
else if (mode === "split") {
|
|
49
|
+
higher = match[0].length > currentMatch[0].length;
|
|
50
|
+
}
|
|
51
|
+
// Match positioned higher than previous one?
|
|
52
|
+
if (higher) {
|
|
53
|
+
// Replace match and continue looking for other matches
|
|
54
|
+
match = currentMatch;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// Save match and stop
|
|
60
|
+
match = currentMatch;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return ((mode === "replace") ? (match || "") : (match || []));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Reconciliates match substrings after a "split" operation
|
|
70
|
+
*/
|
|
71
|
+
export function reconciliateSplitMatch(match, minSubstrings, defaultSubstrings, fnExclude = null) {
|
|
72
|
+
let currentString = "";
|
|
73
|
+
// Add default substrings
|
|
74
|
+
for (let i = 0; i < defaultSubstrings.length; i++) {
|
|
75
|
+
currentString += match[defaultSubstrings[i]];
|
|
76
|
+
}
|
|
77
|
+
// More substrings than expected?
|
|
78
|
+
if (match.length > minSubstrings) {
|
|
79
|
+
// Reconciliate them
|
|
80
|
+
for (let i = minSubstrings; i < match.length; i++) {
|
|
81
|
+
let exclude = false;
|
|
82
|
+
// Exclude the substring?
|
|
83
|
+
if (typeof fnExclude === "function") {
|
|
84
|
+
exclude = fnExclude(i);
|
|
85
|
+
}
|
|
86
|
+
if (exclude === false) {
|
|
87
|
+
currentString += match[i];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return currentString;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Trims a string
|
|
95
|
+
*/
|
|
96
|
+
export function trimString(str) {
|
|
97
|
+
return ((str || "").trim() || "");
|
|
98
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "email-forward-parser",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Parses forwarded emails and extract content",
|
|
5
5
|
"author": "Eliott Vincent <eliott@crisp.chat>",
|
|
6
|
-
"main": "
|
|
7
|
-
"types": "
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
8
|
"homepage": "https://github.com/crisp-oss/email-forward-parser",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
9
17
|
"repository": {
|
|
10
18
|
"type": "git",
|
|
11
19
|
"url": "git://github.com/crisp-oss/email-forward-parser"
|
|
@@ -15,16 +23,13 @@
|
|
|
15
23
|
},
|
|
16
24
|
"engineStrict": true,
|
|
17
25
|
"engines": {
|
|
18
|
-
"node": ">=
|
|
26
|
+
"node": ">= 20.0.0"
|
|
19
27
|
},
|
|
20
28
|
"scripts": {
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
},
|
|
26
|
-
"dependencies": {
|
|
27
|
-
"re2": "1.22.3"
|
|
29
|
+
"build": "npx tsc",
|
|
30
|
+
"lint": "eslint .",
|
|
31
|
+
"unit": "node ./node_modules/nodeunit-x/bin/nodeunit test/index.js",
|
|
32
|
+
"test": "npm run unit && npm run lint"
|
|
28
33
|
},
|
|
29
34
|
"keywords": [
|
|
30
35
|
"parser",
|
|
@@ -32,5 +37,23 @@
|
|
|
32
37
|
"email",
|
|
33
38
|
"forward"
|
|
34
39
|
],
|
|
35
|
-
"license": "MIT"
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "24.13.3",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "8.39.1",
|
|
44
|
+
"eslint": "9.39.1",
|
|
45
|
+
"eslint-plugin-crisp": "1.4.12",
|
|
46
|
+
"eslint-plugin-jsdoc": "54.1.0",
|
|
47
|
+
"nodeunit-x": "0.15.0",
|
|
48
|
+
"re2": "1.22.3",
|
|
49
|
+
"typescript": "5.9.2"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"re2": "1.22.3"
|
|
53
|
+
},
|
|
54
|
+
"peerDependenciesMeta": {
|
|
55
|
+
"re2": {
|
|
56
|
+
"optional": true
|
|
57
|
+
}
|
|
58
|
+
}
|
|
36
59
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
declare module "email-forward-parser" {
|
|
2
|
-
class EmailForwardParser {
|
|
3
|
-
constructor()
|
|
4
|
-
|
|
5
|
-
read(
|
|
6
|
-
body: string,
|
|
7
|
-
subject?: string,
|
|
8
|
-
): {
|
|
9
|
-
forwarded: boolean
|
|
10
|
-
message: string | null
|
|
11
|
-
email: {
|
|
12
|
-
body: string
|
|
13
|
-
from: {
|
|
14
|
-
address: string
|
|
15
|
-
name: string | null
|
|
16
|
-
}
|
|
17
|
-
to:
|
|
18
|
-
| {
|
|
19
|
-
address: string
|
|
20
|
-
name: string | null
|
|
21
|
-
}
|
|
22
|
-
| {
|
|
23
|
-
address: string
|
|
24
|
-
name: string | null
|
|
25
|
-
}[]
|
|
26
|
-
| null
|
|
27
|
-
| undefined
|
|
28
|
-
cc:
|
|
29
|
-
| {
|
|
30
|
-
address: string
|
|
31
|
-
name: string | null
|
|
32
|
-
}
|
|
33
|
-
| {
|
|
34
|
-
address: string
|
|
35
|
-
name: string | null
|
|
36
|
-
}[]
|
|
37
|
-
| null
|
|
38
|
-
| undefined
|
|
39
|
-
subject: string | null
|
|
40
|
-
date: string | null
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export default EmailForwardParser
|
|
46
|
-
}
|
package/lib/index.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var Parser = require("./parser");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* EmailForwardParser
|
|
9
|
-
* @class
|
|
10
|
-
*/
|
|
11
|
-
class EmailForwardParser {
|
|
12
|
-
/**
|
|
13
|
-
* Constructor
|
|
14
|
-
*/
|
|
15
|
-
constructor() {
|
|
16
|
-
this.parser = new Parser();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Attempts to parse a forwarded email
|
|
21
|
-
* @public
|
|
22
|
-
* @param {string} body
|
|
23
|
-
* @param {string} subject
|
|
24
|
-
* @return {object} The parsed email
|
|
25
|
-
*/
|
|
26
|
-
read(body, subject = null) {
|
|
27
|
-
var _subject = null;
|
|
28
|
-
var _email = {};
|
|
29
|
-
var _result = {};
|
|
30
|
-
|
|
31
|
-
// Check if email was forwarded or not (via the subject)
|
|
32
|
-
if (subject) {
|
|
33
|
-
_subject = this.parser.parseSubject(subject);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
var _forwarded = ((subject && _subject !== null) && true) || false;
|
|
37
|
-
|
|
38
|
-
// Check if email was forwarded or not (via the body)
|
|
39
|
-
if (!subject || _forwarded) {
|
|
40
|
-
_result = this.parser.parseBody(body, _forwarded);
|
|
41
|
-
|
|
42
|
-
if (_result.email) {
|
|
43
|
-
_forwarded = true;
|
|
44
|
-
|
|
45
|
-
_email = this.parser.parseOriginalEmail(_result.email, _result.body);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
forwarded : _forwarded,
|
|
51
|
-
|
|
52
|
-
message : _result.message || null,
|
|
53
|
-
|
|
54
|
-
email : {
|
|
55
|
-
body : _email.body,
|
|
56
|
-
|
|
57
|
-
from : {
|
|
58
|
-
address : (_email.from || {}).address,
|
|
59
|
-
name : (_email.from || {}).name
|
|
60
|
-
},
|
|
61
|
-
|
|
62
|
-
to : _email.to,
|
|
63
|
-
|
|
64
|
-
cc : _email.cc,
|
|
65
|
-
|
|
66
|
-
subject : _subject || _email.subject,
|
|
67
|
-
date : _email.date
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
module.exports = EmailForwardParser;
|