mail-parser icon indicating copy to clipboard operation
mail-parser copied to clipboard

Supports wasm and can be used in the browser or node

Open hehehai opened this issue 1 year ago • 3 comments

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

hehehai avatar Apr 28 '24 08:04 hehehai

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.rs with:
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 web and you get build artifacts in pkg.

  • Create a Vite React+TS project, place the contents of a sample email in public, move pkg to 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>
    </>
  );
}

yasamoka avatar Apr 21 '25 13:04 yasamoka

Is there a standalone npm wasm package available?

hehehai avatar May 04 '25 13:05 hehehai

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.

yasamoka avatar May 04 '25 15:05 yasamoka