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

imapfetch-collect

v0.1.3

Published

Simplified abstraction for the `fetch` method of the `imap` module. Get a callback instead of streams within streams.

Downloads

5

Readme

imapfetch-collect

Simplified abstraction for the fetch method of the imap module. Get a callback instead of streams within streams.

API

This module exports one function:

imapFetchCollect(fetcher, [opts,] whenFetched)

  • fetcher: The ImapFetch object returned by the imap module's fetch method.
  • opts: Optional options object, see below.
  • whenFetched: Your callback (nodeback). It will receive two arguments: (err, msgs), where msgs is an Array of messages that were received successfully (example data), and err is the first error that was encountered, or some false-y value on success.

If you need progress information, you can add your own event listeners to the fetcher. imapFetchCollect's event handlers shouldn't interfere with others.

Options

  • translateHeaderNames: Whether and how to rewrite header field names, using the transkey module.

    • false, null, undefined (default): Don't.
    • a Function: Custom synchronous translater function.
    • "dash2camel": Translate to camelCase, e.g. from, to, xOriginalTo, messageId, contentTransferEncoding
  • maxDecodeBytes: How much of a text buffer to decode automatically. Can be

    • any false-y value (e.g. 0): Use some default value of a few megabytes.
    • a positive Number: Up to that many bytes.
    • true: Decode ALL the text.
  • simpleUniqueHeaders: Whether to unpack header value arrays that contain only one value. Boolean, default: true

Usage

var ImapConnection = require('imap'),
  imapFetchCollect = require('imapfetch-collect');

function onMailFetched(err, msgs) {
  if (err) { throw err; }
  console.log('fetched', msgs.length, 'message(s)');
  var msg1 = msgs[0];
  console.log('first mail headers:', Object.keys(msg1.rawHeaders));
  console.log('first body:', msg1.bodies[0].text);
}

function checkMail() {
  // … login, search, …
  function onSearchSuccess(foundUIDs) {
    var fetcher = imapConn.fetch(foundUIDs, fetchOpts);
    imapFetchCollect(fetcher, onMailFetched);
  }
}

Simplification drawbacks

  • In case of multiple errors within the same fetch attempt, all but one are silently ignored.
  • All messages are buffered into memory. You don't get a chance to ignore some message body based on the message's size or headers.
  • Only the first message body is auto-decoded, because I haven't encountered any mail with multiple (top-level) message bodies yet. If you have more complicated mail, you can work with raw headers and bodies.

Working with raw headers and bodies

Each message object has the methods getRawHeaders and getRawBodies which understand optional arguments, ([n[, enc[, maxBytes]]]).

  • When n is not a number (e.g. undefined / missing), they'll return an Array whose items each have a getRawBuffer method.
  • With a number n, they'll return the n-th item (starting at index 0) or false.
    • If you also set enc to either null or "buffer", they'll return that item's original (entire) buffer. (Regardless of maxDecodeBytes, since there's no decoding.)
    • If you set enc to some other non-empty string, they'll attempt to decode up to maxDecodeBytes of that buffer into a string using encoding enc.
    • maxBytes should be either:
      • missing, undefined, 0, false: No override.
      • a positive number: Temporarily override the maxDecodeBytes option.
      • true: Override: Decode entire buffer.

Known issues

  • needs more/better tests and docs

 

License

ISC