Skip to content

Supports wasm and can be used in the browser or node #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hehehai opened this issue Apr 28, 2024 · 3 comments
Open

Supports wasm and can be used in the browser or node #83

hehehai opened this issue Apr 28, 2024 · 3 comments

Comments

@hehehai
Copy link

hehehai commented Apr 28, 2024

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

@yasamoka
Copy link

yasamoka commented Apr 21, 2025

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>
    </>
  );
}

@hehehai
Copy link
Author

hehehai commented May 4, 2025

Is there a standalone npm wasm package available?

@yasamoka
Copy link

yasamoka commented May 4, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants