npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@protontech/jsmimeparser

v3.0.1

Published

An asynchronous MIME parser written in JavaScript

Downloads

4,763

Readme

This is a fork of mozilla-comm/jsmime that has been updated to include the changes made in mozilla/releases-comm-central (incl. UTF-7 support). Further, the library now uses ES6 modules and exposes a user-friendly parseMail function.

Code Layout

JSMime is a MIME parsing and composition library that is written completely in JavaScript using ES6 functionality and WebAPIs (where such APIs exist). There are a few features for which a standardized WebAPI does not exist; for these, external JavaScript libraries are used.

The MIME parser consists of three logical phases of translation:

  1. Build the MIME (and pseudo-MIME) tree.
  2. Convert the MIME tree into a list of body parts and attachments.
  3. Use the result to drive a displayed version of the message.

The first stage is located in rawMimeParser.js, the second in mailParser.js (in particular, the parseMail function). The latter stage is left to the applications.

Install

npm i @protontech/jsmimeparser

Usage

The parseMail function is designed to be user-friendly but remains bare-bones in the sense that it does not add metadata or information that is not found in the original message (e.g. no automatic contentID or checksum generation for the attachments, unlike Nodemailer's MailParser).

import { parseMail } from '@protontech/jsmimeparser';

const eml = `Message-Id: <[email protected]>
Received: from source ([69.9.251.177]) by exprod5mx37.postini.com ...
From: "Bob Example" <[email protected]>
To: "Alice Example" <[email protected]>
Date: Wed, 20 Aug 2003 16:02:43 -0500
Subject: Test message
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="XXXXboundary text"

This is a multipart message in MIME format.

--XXXXboundary text
Content-Type: text/plain

Hello Alice.
This is a test message with 5 lines in the message body
and an attachment.
Your friend,
Bob
--XXXXboundary text
Content-Type: image/gif
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=smile.gif

R0lGODlhyADIAMIAAP...+lmxwBLZ7FjJNkKsbcbyuGq0vKpH7bO50klqJ7YSmCYn4Yrrn4+elGsurYeoKy67e/ZqrrfogivvvONu4i6B8CJ6L77nguKigD0O7FK+mhhskoZIEhzwJwpjxLCFUy7co8ANH1xwxhY/LIpdIB/qmr6Hhvztfih+XPLKJ6c4HsYtK2ByvShb9UQCADs=

--XXXXboundary text--`

const {
  attachments, // [{ contentType: 'image/gif', fileName: 'smile.gif', content: Uint8Array[71, 73, 70..], ... }]
  body, // { text: 'Hello Alice.\nThis is..', html: '' }
  subject, // 'Test message'
  from, // // { name: 'Bob Example', email: '[email protected]' }
  to, // [{ name: 'Alice Example', email: '[email protected]' }]
  date, // Date('Wed, 20 Aug 2003 16:02:43 -0500')
  ...rest // headers and more
} = parseMail(eml);

See test/test_mail_parser.ts for other examples with different MIME messages. Type information can be found in index.d.ts.

Aside from parseMail, several lower-level functions are exported by lib/jsmime and lib/mailParser (mostly unchanged from the original jsmime & mozilla repos).

Testing

Headless Chrome (or Chromium), Firefox and Webkit are used for the tests. To install any missing browsers automatically, you can run npx playwright install-deps <chromium|firefox|webkit>. Alternatively, you can install them manually as you normally would on your platform. If you'd like to test on a subset of browsers, use e.g. npm test -- --browsers ChromeHeadless,FirefoxHeadless.