0

I have this code:

Meteor.startup(function() {
    console.log("Starting IMAP");
    var Imap = Meteor.npmRequire('imap'),
        inspect = Meteor.npmRequire('util').inspect;

    var imap = new Imap({
        user: 'user@domain.com',
        password: 'xxx',
        host: 'imap.gmail.com',
        port: 993,
        tls: true
    });

    function findAttachmentParts(struct, attachments) {
        attachments = attachments ||  [];
        for (var i = 0, len = struct.length, r; i < len; ++i) {
            if (Array.isArray(struct[i])) {
                findAttachmentParts(struct[i], attachments);
            } else {
                if (struct[i].disposition && ['INLINE', 'ATTACHMENT'].indexOf(struct[i].disposition.type) > -1) {
                    attachments.push(struct[i]);
                }
            }
        }
        return attachments;
    }
    function buildAttMessageFunction(attachment) {
        var filename = attachment.params.name;
        var encoding = attachment.encoding;

        console.log("encoding: " + encoding);
        debugger;

    }
    function openInbox(cb) {
        imap.openBox('INBOX', true, cb);
    }

    imap.once('ready', function() {
        console.log("Imap is ready");
        openInbox(function(err, box) {
            if (err) throw err;
            //var f = imap.seq.fetch('1:30', {
            //bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
            //struct: true
            //});
            imap.search(['ALL',['SUBJECT', 'expertaerts']], function(err, results) {
                if (err) throw err;
                var f = imap.fetch(results, {
                    bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE BODY)'],
                    struct: true
                });
                f.on('message', function(msg, seqno) {
                    console.log('Message #%d', seqno);
                    var prefix = '(#' + seqno + ') ';
                    msg.on('body', function(stream, info) {
                        if (info.which === 'TEXT')
                            console.log(prefix + 'Body [%s] found, %d total bytes', inspect(info.which), info.size);
                        var buffer = '', count = 0;
                        stream.on('data', function(chunk) {
                            count += chunk.length;
                            buffer += chunk.toString('utf8');

                            console.log("BUFFER", buffer)

                        });
                        stream.once('end', function() {
                            console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
                            Meteor.call("createSharedocFDL", buffer.subject);
                        });
                    });
                    msg.once('end', function() {
                        console.log(prefix + 'Finished');
                    });
                });
                f.once('error', function(err) {
                    console.log('Fetch error: ' + err);
                });
                f.once('end', function() {
                    console.log('Done fetching all messages!');
                    imap.end();
                });
            });
        });
    });

    imap.once('error', function(err) {
        console.log(err);
    });

    imap.once('end', function() {
        console.log('Connection ended');
    });

    imap.connect();

});

It generate following error message:

W20150129-09:38:16.270(1)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20150129-09:38:16.270(1)? (STDERR)     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20150129-09:38:16.270(1)? (STDERR)     at [object Object]._.extend.get (packages/meteor/dynamics_nodejs.js:21:1)
W20150129-09:38:16.270(1)? (STDERR)     at [object Object]._.extend.apply (packages/ddp/livedata_server.js:1495:1)
W20150129-09:38:16.270(1)? (STDERR)     at [object Object]._.extend.call (packages/ddp/livedata_server.js:1455:1)
W20150129-09:38:16.270(1)? (STDERR)     at Readable.<anonymous> (app/server/imap.js:70:36)
W20150129-09:38:16.271(1)? (STDERR)     at Readable.g (events.js:180:16)
W20150129-09:38:16.271(1)? (STDERR)     at Readable.emit (events.js:92:17)
W20150129-09:38:16.271(1)? (STDERR)     at _stream_readable.js:943:16
W20150129-09:38:16.271(1)? (STDERR)     at process._tickCallback (node.js:419:13)

How can I solve it?

ndemoreau
  • 3,849
  • 4
  • 43
  • 55
  • http://stackoverflow.com/a/18541825/1626821 – imslavko Jan 29 '15 at 08:44
  • possible duplicate of ["Meteor code must always run within a Fiber" when calling Collection.insert on server](http://stackoverflow.com/questions/10192938/meteor-code-must-always-run-within-a-fiber-when-calling-collection-insert-on-s) – 11684 Jan 29 '15 at 08:49
  • I've seen this post but I don't know how to apply it to my code. Should I encapsulate all the code within Meteor.Startup? Or only imap.once? I don't understand this Fiber stuff... – ndemoreau Jan 29 '15 at 09:20

1 Answers1

0

This is the way I finally solved it:

stream.once('end', function() {
    console.log("Priour entering fiber");
    Fiber(function(){
         console.log("entering fiber");
         console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
         Meteor.call("createSharedocFDL", buffer.subject);
    }).run();
});

The code inside the Fiber is never executed:

I20150130-07:20:33.246(1)? Priour entering fiber
I20150130-07:20:33.246(1)? (#151) Finished
I20150130-07:20:33.248(1)? Done fetching all messages!
I20150130-07:20:33.496(1)? { [Error: read ECONNRESET]
I20150130-07:20:33.496(1)?   code: 'ECONNRESET',
I20150130-07:20:33.496(1)?   errno: 'ECONNRESET',
I20150130-07:20:33.496(1)?   syscall: 'read',
I20150130-07:20:33.496(1)?   source: 'socket' }

I'd prefer to follow @imslavko 's suggestion and use a bindEnvironment but when I tried that, it kept requiring a fiber.

In my opinion, this fiber thing is not the Meteor way. Meteor is supposed to be easy to use. This is everything but easy.

ndemoreau
  • 3,849
  • 4
  • 43
  • 55