mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.17k stars 382 forks source link

Connection.js - TypeError: Cannot read property 'type' of undefined #837

Open SashaJson opened 3 years ago

SashaJson commented 3 years ago

i make connect to box, but without close connect when total message === 0

  imap.once('ready', () => {
        openInbox((err, mailbox) => {
            if (err) return callback(err);
            if (mailbox.messages.total === 0) {
                return callback(null, 'Mail box is empty');
            }
            const f = imap.seq.fetch('1:*', {bodies: ''});
            f.on('message', (message, seqno) => {
                imap.seq.addFlags(seqno, 'Deleted', err => {
                    if (err) return callback(err);
                    imap.closeBox(true, () => {
                        imap.end();
                        return callback(null, 'All message were deleted in mailbox');
                    });
                });
            });
        });
    });
    imap.connect();

debug log when do not close connect

console.log [connection] Connected to host at TLSSocket.onconnect (node_modules/imap/lib/Connection.js:137:24) console.log <= ' OK IMAP4 ready' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A0 CAPABILITY' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' CAPABILITY IMAP4rev1 IDLE ID XLIST NAMESPACE AUTH=PLAIN' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A0 OK completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A1 LOGIN "email@email.net" "pass"' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= 'A1 OK LOGIN completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A2 CAPABILITY' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' CAPABILITY IMAP4rev1 IDLE ID XLIST NAMESPACE COMPRESS=DEFLATE' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A2 OK CAPABILITY completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A3 NAMESPACE' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' NAMESPACE (("" "/")) NIL NIL' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A3 OK NAMESPACE completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A4 LIST "" ""' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' LIST (\Noselect) "/" ""' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A4 OK LIST completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A5 SELECT "INBOX"' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' FLAGS (\Answered \Flagged \Deleted \Seen \Draft $Forwarded)' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 0 EXISTS' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 0 RECENT' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft $Forwarded)]' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' OK [UIDVALIDITY 1]' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A5 OK [READ-WRITE] SELECT completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A6 STATUS "INBOX" (MESSAGES RECENT UNSEEN UIDVALIDITY UIDNEXT)' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' STATUS "INBOX" (MESSAGES 0 RECENT 0 UNSEEN 0 UIDVALIDITY 1 UIDNEXT 1904)' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A6 OK status completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log Status mailBox: { name: 'INBOX', uidnext: 1904, uidvalidity: 1, messages: { total: 0, new: 0, unseen: 0 } } at Connection.imap.status (tests/client-imap/scripts/deleteAllMessageInMailBox.js:14:17) console.log => 'IDLE IDLE' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= '+ idling' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 1 EXISTS' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26)

and after that check email in box debug log for this step

console.log [connection] Connected to host at TLSSocket.onconnect (node_modules/imap/lib/Connection.js:137:24) console.log <= ' OK IMAP4 ready' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A0 CAPABILITY' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' CAPABILITY IMAP4rev1 IDLE ID XLIST NAMESPACE AUTH=PLAIN' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A0 OK completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A1 LOGIN "email@email.net" "pass"' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= 'A1 OK LOGIN completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A2 CAPABILITY' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' CAPABILITY IMAP4rev1 IDLE ID XLIST NAMESPACE COMPRESS=DEFLATE' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A2 OK CAPABILITY completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A3 NAMESPACE' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' NAMESPACE (("" "/")) NIL NIL' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A3 OK NAMESPACE completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A4 LIST "" ""' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' LIST (\Noselect) "/" ""' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A4 OK LIST completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A5 EXAMINE "INBOX"' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' FLAGS (\Answered \Flagged \Deleted \Seen \Draft $Forwarded)' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 1 EXISTS' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 0 RECENT' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft $Forwarded)]' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' OK [UIDVALIDITY 1]' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A5 OK [READ-ONLY] SELECT completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A6 STATUS "INBOX" (MESSAGES RECENT UNSEEN UIDVALIDITY UIDNEXT)' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' STATUS "INBOX" (MESSAGES 1 RECENT 0 UNSEEN 1 UIDVALIDITY 1 UIDNEXT 1905)' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A6 OK status completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log Status mailBox: { name: 'INBOX', uidnext: 1905, uidvalidity: 1, messages: { total: 1, new: 0, unseen: 1 } } at Connection.imap.status (tests/client-imap/scripts/checkMailInBox.js:16:17) console.log => 'A7 UID FETCH 1: (UID FLAGS INTERNALDATE BODY.PEEK[])' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' 1 FETCH (UID 1904 FLAGS () INTERNALDATE "05-Feb-2021 11:09:39 +0000" BODY[] {25264}' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 1 FETCH (UID 1904 FLAGS () INTERNALDATE "05-Feb-2021 11:09:39 +0000" )' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A7 OK FETCH completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'IDLE IDLE' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= '+ idling' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => DONE at Parser. (node_modules/imap/lib/Connection.js:236:28) console.log <= 'IDLE OK IDLE completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A8 LOGOUT' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= '* BYE logout' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A8 OK logout completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log [connection] Closed at Socket. (node_modules/imap/lib/Connection.js:175:24)

after that delete message and again check email in box, but this moment received Error debug log from this step

console.log <= ' 1 EXPUNGE' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 1 EXISTS' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) TypeError: Cannot read property 'type' of undefined at Connection.Object..Connection._resUntagged (/home/sasha-shulha/repositorTeradek/teradek-id-server/node_modules/imap/lib/Connection.js:1279:52) at Parser. (/home/sasha-shulha/repositorTeradek/teradek-id-server/node_modules/imap/lib/Connection.js:191:10) at Parser.emit (events.js:198:13) at Parser.Object..Parser._resUntagged (/home/sasha-shulha/repositorTeradek/teradek-id-server/node_modules/imap/lib/Parser.js:271:10) at Parser.Object..Parser._parse (/home/sasha-shulha/repositorTeradek/teradek-id-server/node_modules/imap/lib/Parser.js:137:16) at Parser.Object..Parser._tryread (/home/sasha-shulha/repositorTeradek/teradek-id-server/node_modules/imap/lib/Parser.js:82:15) at TLSSocket.Parser._cbReadable (/home/sasha-shulha/repositorTeradek/teradek-id-server/nodemodules/imap/lib/Parser.js:53:12) at TLSSocket.emit (events.js:198:13) at emitReadable (_stream_readable.js:555:12) at process._tickCallback (internal/process/next_tick.js:63:19) console.log [connection] Connected to host at TLSSocket.onconnect (node_modules/imap/lib/Connection.js:137:24) console.log <= ' OK IMAP4 ready' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A0 CAPABILITY' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' CAPABILITY IMAP4rev1 IDLE ID XLIST NAMESPACE AUTH=PLAIN' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A0 OK completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A1 LOGIN "email@email.net" "pass"' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= 'A1 OK LOGIN completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A2 CAPABILITY' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' CAPABILITY IMAP4rev1 IDLE ID XLIST NAMESPACE COMPRESS=DEFLATE' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A2 OK CAPABILITY completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A3 NAMESPACE' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' NAMESPACE (("" "/")) NIL NIL' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A3 OK NAMESPACE completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A4 LIST "" ""' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' LIST (\Noselect) "/" ""' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A4 OK LIST completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A5 EXAMINE "INBOX"' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' FLAGS (\Answered \Flagged \Deleted \Seen \Draft $Forwarded)' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 1 EXISTS' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 0 RECENT' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft $Forwarded)]' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' OK [UIDVALIDITY 1]' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A5 OK [READ-ONLY] SELECT completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'A6 STATUS "INBOX" (MESSAGES RECENT UNSEEN UIDVALIDITY UIDNEXT)' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' STATUS "INBOX" (MESSAGES 1 RECENT 0 UNSEEN 1 UIDVALIDITY 1 UIDNEXT 1906)' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A6 OK status completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log Status mailBox: { name: 'INBOX', uidnext: 1906, uidvalidity: 1, messages: { total: 1, new: 0, unseen: 1 } } at Connection.imap.status (tests/client-imap/scripts/checkMailInBox.js:16:17) console.log => 'A7 UID FETCH 1: (UID FLAGS INTERNALDATE BODY.PEEK[])' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= ' 1 FETCH (UID 1905 FLAGS () INTERNALDATE "05-Feb-2021 11:10:23 +0000" BODY[] {32164}' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= ' 1 FETCH (UID 1905 FLAGS () INTERNALDATE "05-Feb-2021 11:10:23 +0000" )' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log <= 'A7 OK FETCH completed' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => 'IDLE IDLE' at Connection.Object..Connection._processQueue (node_modules/imap/lib/Connection.js:1737:22) console.log <= '+ idling' at Parser.Object..Parser._parse (node_modules/imap/lib/Parser.js:132:26) console.log => DONE at Parser. (node_modules/imap/lib/Connection.js:236:28)

Connection.js line - 1279

Connection.prototype._resUntagged = function(info) { var type = info.type, i, len, box, attrs, key; if (type === 'bye') this._sock.end(); else if (type === 'namespace') this.namespaces = info.text; else if (type === 'id') this._curReq.cbargs.push(info.text); else if (type === 'capability') this._caps = info.text.map(function(v) { return v.toUpperCase(); }); else if (type === 'preauth') this.state = 'authenticated'; else if (type === 'sort' || type === 'thread' || type === 'esearch') this._curReq.cbargs.push(info.text); else if (type === 'search') { if (info.text.results !== undefined) { // CONDSTORE-modified search results this._curReq.cbargs.push(info.text.results); this._curReq.cbargs.push(info.text.modseq); } else this._curReq.cbargs.push(info.text); } else if (type === 'quota') { var cbargs = this._curReq.cbargs; if (!cbargs.length) cbargs.push([]); cbargs[0].push(info.text); } else if (type === 'recent') { if (!this._box && RE_OPENBOX.test(this._curReq.type)) this._createCurrentBox(); if (this._box) this._box.messages.new = info.num; } else if (type === 'flags') { if (!this._box && RE_OPENBOX.test(this._curReq.type)) this._createCurrentBox(); if (this._box) this._box.flags = info.text; } else if (type === 'bad' || type === 'no') { if (this.state === 'connected' && !this._curReq) { clearTimeout(this._tmrConn); clearTimeout(this._tmrAuth); var err = new Error('Received negative welcome: ' + info.text); err.source = 'protocol'; this.emit('error', err); this._sock.end(); } } else if (type === 'exists') { if (!this._box && RE_OPENBOX.test(this._curReq.type)) <<<----------------------- this._createCurrentBox(); if (this._box) { var prev = this._box.messages.total, now = info.num; this._box.messages.total = now; if (now > prev && this.state === 'authenticated') { this._box.messages.new = now - prev; this.emit('mail', this._box.messages.new); } }

vitorcodesalittle commented 2 years ago

Having the same issue. Have you managed to solve this? I believe it gets messy when trying to do multiple things in a single connection

humayyun00 commented 1 year ago

This happens mostly when you're making multiple connections .I've facing this issue for quite some time and the only possible solution I found is adding and If condition in the node_modules/imap/lib/Connection.js. This really is a headache hope the check will be officially added in the module

humayyun00 commented 1 year ago

here's my code: file name: node_modules/imap/lib/Connection.js.

parser.on('continue', function(info) { if(self._curReq) { var type = self._curReq.type; if (type === 'IDLE') { if (self._queue.length && self._idle.started === 0 && self._curReq && self._curReq.type === 'IDLE' && self._sock && self._sock.writable && !self._idle.enabled) { self.debug && self.debug('=> DONE'); self._sock.write('DONE' + CRLF); return; } // now idling self._idle.started = Date.now(); } else if (/^AUTHENTICATE XOAUTH/.test(self._curReq.fullcmd)) { self._curReq.oauthError = new Buffer(info.text, 'base64').toString('utf8'); self.debug && self.debug('=> ' + inspect(CRLF)); self._sock.write(CRLF); } else if (type === 'APPEND') { self._sockWriteAppendData(self._curReq.appendData); } else if (self._curReq.lines && self._curReq.lines.length) { var line = self._curReq.lines.shift() + '\r\n'; self.debug && self.debug('=> ' + inspect(line)); self._sock.write(line, 'binary'); }} else{ console.log("self._curReq.type not found") return; } });