szmarczak / cacheable-lookup

A cacheable dns.lookup(…) that respects TTL :tada:
MIT License
193 stars 29 forks source link

Does not resolve `localhost` on OS X #13

Closed larixer closed 4 years ago

larixer commented 4 years ago

Steps for reproduction: package.json:

{
  "name": "cacheable-lookup-localhost",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "cacheable-lookup": "^2.0.1"
  }
}

index.js:

const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();

(async () => {
  try {
    console.log(await cacheable.lookupAsync("localhost"));
  } catch (e) {
    console.log(e);
  }
})();

The output under OS X Mojave:

$  node .
Error: queryA ENOTFOUND localhost
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:207:19) {
  errno: undefined,
  code: 'ENOTFOUND',
  syscall: 'queryA',
  hostname: 'localhost'
}

This problem in consequence makes got to fail with dnsCache enabled on OS X when requests to http://localhost are issued.

P.S. Please note that issue reproduces only on OS X, it does not reproduce neither on Windows nor on Linux

larixer commented 4 years ago

The possible workaround is to initalize dnsCache like this:

const dnsCache = new Map([[
  'cached-lookup:localhost:4', JSON.stringify({
    value: [{
      address: '127.0.0.1',
      ttl: 268,
      family: 4,
      expires: Date.now() + 24 * 3600 * 1000,
    }],
    expires: Date.now() + 24 * 3600 * 1000,
  }),
]]);
larixer commented 4 years ago

@szmarczak Any thoughts about this issue, we are seeing this problem when running Yarn 2 integration tests on GitHub Actions Mac OS X matrix target.

szmarczak commented 4 years ago

Sorry for replying this late, somehow I didn't get a notification for the two previous comments :thinking:

Please make sure you're running cacheable-lookup@4.1.2. It reads the hosts file.

  1. What is the path to your hosts file? Is it /etc/hosts?
  2. Please copy the whole contents of the file and paste it here. Or just reply whether it has defined localhost or not.
larixer commented 4 years ago

@szmarczak Thanks! Indeed it works with 4.1.2. But the latest tag on npm is assigned to cacheable-lookup@2.0.1, is this intended?

$ yarn info cacheable-lookup
yarn info v1.22.4
{
  name: 'cacheable-lookup',
  'dist-tags': {
    latest: '2.0.1'
  },
szmarczak commented 4 years ago

But the latest tag on npm is assigned to cacheable-lookup@2.0.1, is this intended?

No. Doesn't npm update the latest tag automatically? I haven't altered with it in any way. Looking at this now.

szmarczak commented 4 years ago

@larixer Fixed. The latest points now to 4.1.2.

larixer commented 4 years ago

@szmarczak Cool, thank you very much!