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

imap-scan-many-boxes

v1.0.1

Published

Scan multiple IMAP boxes and emit for every message found.

Downloads

9

Readme

imap-scan-box

This module takes an instance of the imap module, and an array of box names (also known as "folders"), and returns an emitter which emits for each message in each box, and an end event when all box scans are complete.

general use

const Imap = require('imap')
const scanBoxes = require('imap-scan-many-boxes')

const imap = new Imap({
	user: '[email protected]',
	password: 'abc123',
	host: 'imap.gmail.com',
	port: 993,
	tls: true
})

const boxes = [
	'INBOX',
	'INBOX.neat-folder'
]
const range = '1:10' // earliest 10 messages
const fetch = { // data to include in message bodies
	bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)'
}

// must wait until imap is ready
imap.once('ready', () => {
	const scanner = scanBoxes({ imap, boxes, range, fetch })
	// emitted messages emit the streaming object
	scanner.on('message', ({ stream, box }) => {
		console.log(box) // => 'INBOX' or 'INBOX.neat-folder'

		let body = ''

		message.on('body', (stream, info) => {
			stream.on('data', chunk => body += chunk.toString('utf8'))
		})

		message.once('attributes', attributes => {
			console.log(attributes)
		})

		message.once('end', () => {
			console.log(body)
		})
	})
})

scanBoxes(Object)

The module takes an object with the following properties:

imap

The instance of imap provided must be instantiated and have already emitted the ready event.

Note: The IMAP protocol (or at least the imap module) does not support scanning multiple boxes simultaneously. 😭

boxes (array of strings)

An array of box names, e.g. [ 'INBOX', 'INBOX.my-folder' ].

range (string, default 1:*)

The range string for which messages to return.

fetch (object, optional)

The fetch object expected by the imap module.

Default value is:

options.fetch = {
	bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
	struct: true
}

emitted events

The returned emitter will emit the following events:

error (object)

Each error object emitted has the following properties:

  • action (string) The action which caused the error.
  • error (object|string) The thrown error for that action.

The following error actions exist:

  • open: Opening the IMAP box threw an error.
  • fetch: Opened the box, but fetching the messages threw an error.
  • error: Closing the IMAP box after fetching completed threw an error.

opened (opened)

Emitted after each IMAP box is opened. The emitted object is the box object given by the imap module.

message (object)

Each message found during the fetch will be emitted with the following properties:

  • sequenceNumber (integer): The sequence number of that fetch.
  • stream (stream): The message data, as a stream object.
  • box (string): The name of the box the message is from.

The stream object is the actual streamed message object as given by the imap module.

end

Emitted after all actions have completed. Either by way of all box scans completing, or an error occuring which prevents further scanning.

license

Published and released under the VOL.