+29
-14
| '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); | ||
| }) | ||
| } | ||
| }; |
+5
-3
| { | ||
| "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": "*" |
+12
-9
| # get-hosts [](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) |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
3126
15.22%31
82.35%34
9.68%1
Infinity%3
50%+ Added
+ Added