chadxz / imap-simple

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

Uncaught exception #93

Closed dgurevych closed 3 years ago

dgurevych commented 3 years ago

Hi,

A short test below shows 'uncaughtException' event generation. Could you please advise if any workaround possible?

const imaps = require('imap-simple');

const config = {
      imap: {
        host: "64.233.167.108",
        port: 993,
        tls: true,
        tlsOptions: { servername: "64.233.167.108" },
        connTimeout:   30000,
        authTimeout:   30000,
        socketTimeout: 30000,
        user: "myname@gmail.com",
        password: "mypassword"
     }
};

process.on('uncaughtException', function(err) {
  console.log('Uncaught Exception - Service is exiting: %s', err.message);
  process.exit(1);
});

async function connect() {
  console.log(`Connect...`);
    try {
      const ret = await imaps.connect(config);
      console.log("Connected");
      return ret;
    } catch(e) {
      console.log(`Connect Exception: ${e.message}`);
    }
}

(async () => {
  try {
    await connect();
  } catch(e) {
    console.log(`Final Exception: ${e.message}`);
  }
})();

Output is:

Connect...
(node:34380) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permitted by RFC 6066. This will be ignored in a future version.
Uncaught Exception - Service is exiting: Hostname/IP does not match certificate's altnames: IP: 64.233.167.108 is not in the cert's list: 
dgurevych commented 3 years ago

The issue looks like depends on environment: NodeJS version, network configurations, etc. ENV: Node 12.16.0, Windows 10 Enterprise - reproducible ENV: Node 12.20.1. Linux iwdem-5c89d68575-59jtb 5.4.0-1035-azure #36~18.04.1-Ubuntu SMP Wed Dec 16 23:49:28 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux - reproducible ENV: Node 12.16.0. Windows 10 Home - not reproducible

dgurevych commented 3 years ago

A workaround that shows a root cause (excessive/unexpected event error emitted):

const imaps = require('imap-simple');
const domain = require('domain');

const config = {
      imap: {
        host: "64.233.167.108",
        port: 993,
        tls: true,
        tlsOptions: { servername: "64.233.167.108" },
        connTimeout:   30000,
        authTimeout:   30000,
        socketTimeout: 30000,
        user: "myname@gmail.com",
        password: "mypassword"
     }
};

process.on('uncaughtException', function(err) {
  console.log('Uncaught Exception - Service is exiting: %s', err.message);
  process.exit(1);
});

async function connect() {
  console.log(`Connect...`);
    try {
      let connError = [];
      const d = domain.create();
      d.on('error', function(err) {
        console.log('DOMAIN Error: %s', err.message);
        connError.push(err.message);
      });

      const ret = await d.run( async () => {
        try {
          return await imaps.connect(config);
        } catch(e) {
          console.log(`DOMAIN Connect Exception: ${e.message}`);
          connError.push(e.message);
        }
      } );
      d.exit();
      if (connError.length>0) throw new Error(connError.join('\n'));
      return ret;
    } catch(e) {
      console.log(`Connect Exception: ${e.message}`);
    }
}

(async () => {
  try {
    await connect();
  } catch(e) {
    console.log(`Final Exception: ${e.message}`);
  }
})();

Output:

Connect...                                                                                          (node:37772) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permitted by RFC 6066. This will be ignored in a future version.
DOMAIN Error: Hostname/IP does not match certificate's altnames: IP: 64.233.167.108 is not in the cert's list:
DOMAIN Connect Exception: Cannot call write after a stream was destroyed
Connect Exception: Hostname/IP does not match certificate's altnames: IP: 64.233.167.108 is not in the cert's list:
Cannot call write after a stream was destroyed