diff --git a/source/hosts-resolver.js b/source/hosts-resolver.js index eaf80b4..256e436 100644 --- a/source/hosts-resolver.js +++ b/source/hosts-resolver.js @@ -55,7 +55,7 @@ class HostsResolver { lines = lines.replace(whitespaceRegExp, ' '); lines = lines.replace(tabRegExp, ' '); lines = lines.replace(startsWithWhitespaceRegExp, ''); - lines = lines.split('\n'); + lines = lines.split('\n').map(line => line.trim()); this._hosts = {}; diff --git a/tests/hosts.txt b/tests/hosts.txt index 5cc9694..86b90bf 100644 --- a/tests/hosts.txt +++ b/tests/hosts.txt @@ -10,5 +10,7 @@ noiphere 127.0.0.1 foo4 127.0.0.2 foo4 127.0.0.1 manywhitespaces - 127.0.0.1 startswithwhitespace +127.0.0.1 startswithwhitespace 127.0.0.1 tab +127.0.0.1 has-windows-newline + diff --git a/tests/test.js b/tests/test.js index fabba5a..ad19102 100644 --- a/tests/test.js +++ b/tests/test.js @@ -6,6 +6,7 @@ const path = require('path'); const test = require('ava'); const Keyv = require('keyv'); const proxyquire = require('proxyquire'); +const fs = require('fs').promises; const makeRequest = options => new Promise((resolve, reject) => { http.get(options, resolve).once('error', reject); @@ -782,6 +783,23 @@ test.serial('double tick() has no effect', t => { }); test('respects the `hosts` file', async t => { + const hostFile = path.join(__dirname, 'hosts.txt'); + // Ensure that at least one line ends with a windows CRLF + const textContent = await fs.readFile(hostFile, {encoding: 'utf-8'}); + const lines = textContent.split('\n').map(l => l.trim()); + const altered = lines.map(line => { + if (line.includes('has-windows-newline')) { + return `${line}\r`; + } + + return line; + }); + if (!altered.find(line => line.includes('has-windows-newline'))) { + throw new Error('Can\'t find "has-windows-newline" hosts entry'); + } + + await fs.writeFile(hostFile, Buffer.from(altered.join('\n'))); + const cacheable = new CacheableLookup({ customHostsPath: path.resolve(__dirname, 'hosts.txt') }); @@ -805,6 +823,7 @@ test('respects the `hosts` file', async t => { t.is(await getAddress('manywhitespaces'), '127.0.0.1'); t.is(await getAddress('startswithwhitespace'), '127.0.0.1'); t.is(await getAddress('tab'), '127.0.0.1'); + t.is(await getAddress('has-windows-newline'), '127.0.0.1'); { const entry = await cacheable.lookupAsync('foo3', {family: 4});