Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

get-hosts

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-hosts - npm Package Compare versions

Comparing version
1.0.0
to
2.0.0
+29
-14
index.js
'use strict';
var HOSTS = process.platform === 'win32'
? 'C:/Windows/System32/drivers/etc/hosts'
: '/etc/hosts'
var readFileSync = require('fs').readFileSync;
module.exports = function () {
return readFileSync(HOSTS, { encoding: 'utf8' })
.split(/\r?\n/)
.map(function(line){
// R.E from feross/hostile
var matches = /^\s*?([^#]+?)\s+([^#]+?)$/.exec(line)
if (matches && matches.length === 3) {
return [matches[1],matches[2]]
}
})
.filter(function(h){return !!h}); // Need to avoid this.
var readFile = require('fs').readFile;
var HOSTS = require('hosts-path')();
var massageItem = function(line){
// R.E from feross/hostile
var matches = /^\s*?([^#]+?)\s+([^#]+?)$/.exec(line)
if (matches && matches.length === 3) {
var hostMap = {};
hostMap[matches[2]] = matches[1]; // host:ip
return hostMap;
}
};
var massageData = function(data) {
return data.split(/\r?\n/)
.map(massageItem)
.filter(Boolean);
};
module.exports = function(cb) {
if (typeof cb !== 'function') {
return massageData(readFileSync(HOSTS, {
encoding: 'utf8'
}));
} else {
readFile(HOSTS,'utf-8',function(err,data) {
var res = massageData(data);
if(res) return cb(null,res);
return cb(new Error("Couldn't process data"),false);
})
}
};
{
"name": "get-hosts",
"version": "1.0.0",
"description": "`etc/hosts` as an array of arrays.",
"version": "2.0.0",
"description": "`etc/hosts` as an array of objects.",
"license": "MIT",

@@ -26,3 +26,5 @@ "repository": "hemanth/get-hosts",

],
"dependencies": {},
"dependencies": {
"hosts-path": "^1.0.0"
},
"devDependencies": {

@@ -29,0 +31,0 @@ "mocha": "*"

# get-hosts [![Build Status](https://travis-ci.org/hemanth/get-hosts.svg?branch=master)](https://travis-ci.org/hemanth/get-hosts)
> `etc/hosts` as an array of arrays.
> `etc/hosts` as an array of objects.

@@ -18,14 +18,17 @@

getHosts();
/*
[ [ '127.0.0.1', 'local.dev' ],
[ '127.0.0.1', 'localhost' ],
[ '255.255.255.255', 'broadcasthost' ],
[ '::1', 'localhost~ ' ] ] */
getHosts(); // sync
getHosts(function(err,hosts){
if(!err) console.log(hosts);
});
/*[ { 'local.dev': '127.0.0.1' },
{ localhost: '127.0.0.1' },
{ broadcasthost: '255.255.255.255' },
{ 'localhost~ ': '::1' } ]*/
```
P.S: It's sync file read operation here, given the size of the file.
## License
MIT © [Hemanth.HM](http://h3manth.com)