chadxz / imap-simple

Wrapper over node-imap, providing a simpler api for common use cases
MIT License
243 stars 80 forks source link

Issue when missing connection #65

Closed Adibla closed 3 years ago

Adibla commented 5 years ago

Hi, I've an issue. I tried to do some tests and I saw that if I establish the connection with my imap server and I disconnect my computer from the internet connection and then I reconnect to it, the library seems blocked, and no one Promise.catch are triggered. I don't find the problem and I've no idea how to catch it. Thank you.

k2s commented 4 years ago

My findings:

this is my current working test

  async connect () {
    const me = this
    try {
      if (!this._c) {
        this._c = await imaps.connect(this.config)
        this.log.info(`IMAP connected to ${this.config.imap.host}`)
        this._c.on('error', err => {
          // THIS IS REQUIRED not to crash the process on socket error
          this.log.error(err)
        })
        this._c.imap.on('close', () => {
          this._c = null
          setTimeout(() => {
            this.log.debug('IMAP reconnecting after error')
            me.connect()
          }, RECONNECT_TIMEOUT)
        })
      }

      await this._c.openBox('INBOX')
      this.log.info('IMAP INBOX opened')
    } catch (ex) {
      this.log.error(ex)
      setTimeout(() => {
        this.log.debug('IMAP initial reconnect')
        me.connect()
      }, RECONNECT_TIMEOUT)
    }
  }