mail-parser
mail-parser copied to clipboard
Supports wasm and can be used in the browser or node
I am a front-end developer, and there is a lack of high-performance mail-related parsers. A zero-dependency mail parser is just right! expect
I tried embedding mail-parser as a wasm module in a Vite React+TS project and it was frictionless and took very little time.
- Replace
lib.rswith:
use mail_parser::MessageParser;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn from(raw_message: &str) -> Option<String> {
if let Some(message) = MessageParser::default().parse(raw_message) {
if let Some(from) = message.from() {
if let Some(first) = from.first() {
if let Some(addr) = &first.address {
return Some(addr.clone().into_owned());
}
}
};
}
return None;
}
-
Compile with
wasm-pack build --target weband you get build artifacts inpkg. -
Create a Vite React+TS project, place the contents of a sample email in
public, movepkgto the project's source, import and use.
import "./App.css";
import init, { from } from "./pkg/wasm";
init();
function App() {
return (
<>
<button
onClick={async () => {
const content = await fetch("email").then((res) => res.text());
alert(from(content));
}}
>
Parse
</button>
</>
);
}
Is there a standalone npm wasm package available?
Is there a standalone npm wasm package available?
I think it would be counter-intuitive, since the wasm embedding process is usually concerned with bundle size and I'm not sure if a wasm package would be tree-shakeable in terms of the compiled wasm code.