andris9/inbox — pull request #57 by ahw (2013-12-31 04:14:09+00:00)
Title: Parses message stream into JSON.

MessageParser transforms raw messages to JSON
---

In summary, the MessageParser module takes the readable message stream from the IMAP client and transforms it into JSON. Because the MessageParse is itself a readable/writable stream, integrating it into the existing logic is simple:

if (returnAsJson) {
// If we're rendering as JSON, then pipe the raw message stream
// through the MessageParser, which itself is a readable stream of
// JSON data.
return stream.pipe(new MessageParser());
} else {
return stream;
}

The backend processing is straight-forward, using state machines at both the tokenization and parsing level. Header information in the message is passed to a HeaderTokenizer, which then bubbles up to the HeaderParser, and finally to the MessageParser. Once the header lines have been parsed, stream chunks are directed to the BodyParser, which, for now, is simply a pass-through. In the future, the BodyParser could possibly do some decoding work or other convenient logic.

Unit test coverage of all the stuff (both old and new) in lib/ (after running with istanbul):

- Statements: 93.01% (346 / 372)
- Branches: 82.79% (178 / 215)
- Functions: 96.97% (32 / 33)
- Lines: 93.01% (346 / 372)