mailparser icon indicating copy to clipboard operation
mailparser copied to clipboard

Mailparser cannot be used with Deno

Open davidpanic opened this issue 5 years ago • 7 comments

Using the latest Deno (v1.6.2) throws this error when requiring mailparser. I think this is related to this issue

Check file:///home/david/code/deno/mailparser_bug/src/index.ts
error: Uncaught TypeError: buffer.hasOwnProperty is not a function
  if (!buffer.hasOwnProperty(key)) continue
              ^
    at Object.<anonymous> (file:///home/david/code/deno/mailparser_bug/node_modules/safer-buffer/safer.js:13:15)
    at Module._compile (module.ts:161:36)
    at Object.Module._extensions..js (module.ts:1084:10)
    at Module.load (module.ts:140:34)
    at Function._load (module.ts:406:14)
    at Module.require (module.ts:126:21)
    at require (module.ts:1133:16)
    at Object.<anonymous> (file:///home/david/code/deno/mailparser_bug/node_modules/mailsplit/node_modules/iconv-lite/lib/index.js:5:14)
    at Module._compile (module.ts:161:36)
    at Object.Module._extensions..js (module.ts:1084:10)

Run with: deno run --unstable --allow-read --allow-env src/index.ts

Code used:

import { createRequire } from "https://deno.land/[email protected]/node/module.ts";

const require = createRequire(import.meta.url);
const mailparser = require('mailparser');

davidpanic avatar Dec 30 '20 09:12 davidpanic

Maybe you could try this instead https://www.npmjs.com/package/postal-mime

arafatamim avatar Apr 18 '21 16:04 arafatamim

Now that Deno has better npm module support, this works for me:

import { MailParser } from 'npm:mailparser';
const parser = new MailParser();

canac avatar Jan 02 '23 17:01 canac

Is it just me or is MailParser unusably slow with Deno though? I can't quite figure out what's causing the slowdown... it seems to sit somewhere in deno:ext/web/02_timer.js? 🤷

zuk avatar Jan 27 '23 03:01 zuk

I ran into that slowness also. I ended up using a Rust mail parsing library and calling it via WASM and it parses messages in the single digit milliseconds range. Here's my code, but it only extracts a few fields from the email.

canac avatar Jan 27 '23 15:01 canac

Thanks for that tip @canac . I ended up switching to https://github.com/emailjs/emailjs-mime-parser which seems to be much faster, but took a fair bit of work to get it to produce somethign similar to mailparser's output. I may yet go the same route as you with Rust, but I think this is good enough for now.

I'm really curious if this is a Deno issue though, or if this happens under Node too. I don't have the time to try it out myself, but I couldn't find anyone else complaining about similar performance issues with mailparser under Node.

I really want to ditch Node in favour of Deno, but I'm starting to worry about running into more and more issues like this the further I get.

zuk avatar Jan 27 '23 17:01 zuk

I don’t know much about Deno, but can you run my other email parsing library meant for browsers in Deno? https://github.com/postalsys/postal-mime

andris9 avatar Jan 27 '23 17:01 andris9