decodes mime messages, supports streaming
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


decode mime messages. the decoder implements the interface of a writable stream ( v2 ) and a object readable stream ( v2 ). if the decoder encounters binary data like a file upload it will return a separate readable stream for every binary part which is found in the mime message. each of this streams must be consumed, else the decoding of the message will never finish.

If you need to decode mime messages from a http form request you should use the «ee-formdata-reader» module which is much simpler then this module.


npm install ee-mime-decoder

build status

Build Status



var   MimeDecoder       = require('ee-mime-decoder')
    , StreamCollector   = require('ee-stream-collector');

webserver.on('request', function(req, res){

    if (req.method === 'POST'){
        // if the message is already partially parsed you nede to set the content type header 
        // in the constructor, this is the case when parsing multipart messages sent by
        // the browser.
        // if you're parsing emails the headers are already embedded in the emails, so you 
        // don't have to provide them via the constructor.
        var decoder = new MimeDecoder(request.headers['Content-Type']); // multipart/mixed; boundary="----------------------------722570873451616639732247"

        // send data from request directly to the decoder

        // handle incoming decoded mime message parts
        decoder.on('data', function(obj){
            if (obj.isStream()) {
                // mime objet implementing the readable stream interface
                // we got a binary mime part ( e.g. attachment, file upload )

                if (iWantToCacheTheFileInMemory){
                    // collect data an store on the mime object

                    var collector = new StreamCollector();
                    collector.on('end', function(){
                        // store the data on the mime object
                    } );

                    // pipe data into collector
                else if (iWantToStoreTheFilesInTheFileSystem){
                    // store the file in the fs

                    // we need to know where the data was stored
                    obj.path = '/path/to/the/new/file';
                else {
                    // do whatever you want
            else {
                // mime object, we dont need to store the data because its stored on the mimeMessage object of the collector
                // {
                //    parts: (2):[
                //        0: {
                //            length: 19
                //            , data: ""
                //        }
                //        , 1: {
                //            length: 14
                //            , data: "securePassword"
                //        }
                //    ]
                //    , length: 2
        } );

        // all data was received, we can now work with it
        decoder.on('end', function(){
            var mimeMessage = decoder.getMessage();

                console.log(part.length, part.getHeader('content-type'));
                if (part.hasChildren()) console.log('the part has %s children',;
            } );
        } );
} );