Skip to content

Commit

Permalink
Merge 84aaa66 into 6ed7e37
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarfd committed Apr 28, 2020
2 parents 6ed7e37 + 84aaa66 commit 633e81e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -106,6 +106,13 @@ Default: `undefined` (OS-specific)

The full path to the `hosts` file. Set this to `false` to prevent loading entries from the `hosts` file.

##### options.firstEntry

Type: `boolean`<br>
Default: `false`

If the host resolves to multiple ip's, return only the first one.

### Entry object

Type: `object`
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Expand Up @@ -35,6 +35,12 @@ export interface Options {
* @default '/etc/hosts'
*/
customHostsPath?: string | false;
/**
* If the host resolves to multiple ip's, return each time a random ip address.
* Set this to `false` to always return the same ip.
* @default false
*/
firstEntry: false;
/**
* The lifetime of the entries received from the OS (TTL in seconds).
*
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Expand Up @@ -11,6 +11,7 @@ import CacheableLookup, {EntryObject} from '.';
new CacheableLookup({
cache: new Keyv(),
customHostsPath: false,
firstEntry: false,
fallbackTtl: 0,
errorTtl: 0,
maxTtl: 0,
Expand Down
4 changes: 3 additions & 1 deletion source/index.js
Expand Up @@ -57,6 +57,7 @@ const ttl = {ttl: true};
class CacheableLookup {
constructor({
customHostsPath,
firstEntry = false,
cache = new Map(),
maxTtl = Infinity,
resolver = new AsyncResolver(),
Expand All @@ -66,6 +67,7 @@ class CacheableLookup {
this.maxTtl = maxTtl;
this.fallbackTtl = fallbackTtl;
this.errorTtl = errorTtl;
this.firstEntry = firstEntry;

// This value is in milliseconds
this._lockTime = Math.max(Math.floor(Math.min(this.fallbackTtl * 1000, this.errorTtl * 1000)), 10);
Expand Down Expand Up @@ -165,7 +167,7 @@ class CacheableLookup {
return cached;
}

if (cached.length === 1) {
if (cached.length === 1 || this.firstEntry) {
return cached[0];
}

Expand Down
30 changes: 30 additions & 0 deletions tests/test.js
Expand Up @@ -256,6 +256,36 @@ test.serial('multiple entries', async t => {
Math.random = random;
});

test.serial('multiple entries when `options.firstEntry` is true, then we always resolve to the first entry', async t => {
const cacheable = new CacheableLookup({resolver, customHostsPath: false, firstEntry: true});

const {random} = Math;

{
// Let's fool the destiny
Math.random = () => 0;
const entry = await cacheable.lookupAsync('multiple');

verify(t, entry, {
address: '127.0.0.127',
family: 4
});
}

{
// Let's fool the destiny
Math.random = () => 0.6;
const entry = await cacheable.lookupAsync('multiple');

verify(t, entry, {
address: '127.0.0.127',
family: 4
});
}

Math.random = random;
});

test('if `options.all` is falsy, then `options.family` is 4 when not defined', async t => {
const cacheable = new CacheableLookup({resolver, customHostsPath: false});

Expand Down

0 comments on commit 633e81e

Please sign in to comment.