-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
230 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
const {stat, readFile} = require('fs').promises; | ||
|
||
const isWindows = process.platform === 'win32'; | ||
const hostsPath = isWindows ? 'C:\\Windows\\System32\\drivers\\etc\\hosts' : '/etc/hosts'; | ||
|
||
const fileOptions = { | ||
encoding: 'utf8' | ||
}; | ||
|
||
const create = (customHostsPath = hostsPath) => { | ||
if (customHostsPath === false) { | ||
return { | ||
hosts: {}, | ||
updateHosts: () => {} | ||
}; | ||
} | ||
|
||
let lastModifiedTime; | ||
let localHosts = {}; | ||
|
||
const ipAndHost = /^\s*(?<address>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(?<hostname>[a-zA-Z0-9-.]{1,63})/; | ||
|
||
const updateHosts = async () => { | ||
const {mtimeMs} = await stat(customHostsPath); | ||
|
||
if (mtimeMs === lastModifiedTime) { | ||
return localHosts; | ||
} | ||
|
||
lastModifiedTime = mtimeMs; | ||
|
||
localHosts = {}; | ||
|
||
const lines = (await readFile(customHostsPath, fileOptions)).split('\n'); | ||
for (let line of lines) { | ||
const commentIndex = line.indexOf('#'); | ||
|
||
if (commentIndex !== -1) { | ||
line = line.slice(0, commentIndex); | ||
} | ||
|
||
const result = line.match(ipAndHost); | ||
|
||
if (result) { | ||
const {address, hostname} = result.groups; | ||
|
||
localHosts[hostname] = [ | ||
{ | ||
address, | ||
family: 4, | ||
expires: Infinity, | ||
ttl: Infinity | ||
} | ||
]; | ||
} | ||
} | ||
|
||
return localHosts; | ||
}; | ||
|
||
return { | ||
get hosts() { | ||
return localHosts; | ||
}, | ||
updateHosts | ||
}; | ||
}; | ||
|
||
module.exports = create; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# this is a comment | ||
127.0.0.1 helloworld | ||
127.0.0.1 foobar # this is a comment | ||
# this is yet another comment 127.0.0.1 woofwoof | ||
noiphere | ||
127.0.0.1 # no host here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
import {expectType} from 'tsd'; | ||
import CacheableLookup, {EntryObject} from '.'; | ||
|
||
const cacheable = new CacheableLookup(); | ||
|
||
expectType<string[]>(cacheable.servers); | ||
|
||
expectType<EntryObject>(await cacheable.lookupAsync('localhost', 4)); | ||
expectType<EntryObject>(await cacheable.lookupAsync('localhost', {all: false})); | ||
expectType<ReadonlyArray<EntryObject>>(await cacheable.lookupAsync('localhost', {all: true})); | ||
|
||
cacheable.lookup('localhost', 6, (error, address, family) => { | ||
expectType<NodeJS.ErrnoException>(error); | ||
expectType<string>(address); | ||
expectType<4 | 6>(family); | ||
}); | ||
|
||
cacheable.lookup('localhost', {all: false}, (error, address, family) => { | ||
expectType<NodeJS.ErrnoException>(error); | ||
expectType<string>(address); | ||
expectType<4 | 6>(family); | ||
}); | ||
|
||
cacheable.lookup('localhost', {all: true}, (error, results) => { | ||
expectType<NodeJS.ErrnoException>(error); | ||
expectType<ReadonlyArray<EntryObject>>(results); | ||
}); | ||
|
||
expectType<ReadonlyArray<EntryObject>>(await cacheable.query('localhost')); | ||
expectType<ReadonlyArray<EntryObject>>(await cacheable.queryAndCache('localhost')); | ||
|
||
expectType<void>(cacheable.updateInterfaceInfo()); | ||
expectType<void>(cacheable.tick()); | ||
expectType<void>(cacheable.clear()); | ||
(async () => { | ||
const cacheable = new CacheableLookup(); | ||
|
||
expectType<string[]>(cacheable.servers); | ||
|
||
expectType<EntryObject>(await cacheable.lookupAsync('localhost', 4)); | ||
expectType<EntryObject>(await cacheable.lookupAsync('localhost', {all: false})); | ||
expectType<ReadonlyArray<EntryObject>>(await cacheable.lookupAsync('localhost', {all: true})); | ||
|
||
cacheable.lookup('localhost', 6, (error, address, family) => { | ||
expectType<NodeJS.ErrnoException>(error); | ||
expectType<string>(address); | ||
expectType<4 | 6>(family); | ||
}); | ||
|
||
cacheable.lookup('localhost', {all: false}, (error, address, family) => { | ||
expectType<NodeJS.ErrnoException>(error); | ||
expectType<string>(address); | ||
expectType<4 | 6>(family); | ||
}); | ||
|
||
cacheable.lookup('localhost', {all: true}, (error, results) => { | ||
expectType<NodeJS.ErrnoException>(error); | ||
expectType<ReadonlyArray<EntryObject>>(results); | ||
}); | ||
|
||
expectType<ReadonlyArray<EntryObject>>(await cacheable.query('localhost')); | ||
expectType<ReadonlyArray<EntryObject>>(await cacheable.queryAndCache('localhost')); | ||
|
||
expectType<void>(cacheable.updateInterfaceInfo()); | ||
expectType<void>(cacheable.tick()); | ||
expectType<void>(cacheable.clear()); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters