hosts-parser
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "hosts-parser", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "hosts file parser", | ||
@@ -29,3 +29,6 @@ "main": "index.js", | ||
"mocha": "^2.1.0" | ||
}, | ||
"dependencies": { | ||
"lodash": "^3.0.1" | ||
} | ||
} |
@@ -9,4 +9,5 @@ # hosts-parser | ||
var fs = require('fs'); | ||
var parser = require('hosts-parser'); | ||
console.log(parser(fs.readFileSync('/etc/hosts', 'utf8'))); | ||
var Hosts = require('hosts-parser'); | ||
var hosts = new Hosts(fs.readFileSync('/etc/hosts', 'utf8')); | ||
console.log(hosts.toJSON()); | ||
/** | ||
@@ -20,5 +21,28 @@ [ | ||
**/ | ||
console.log(hosts.resolve('localhost')); | ||
// 127.0.0.1 | ||
``` | ||
## What is Hosts file | ||
## API | ||
### Hosts(hosts) and Hosts.prototype.parse(hosts) | ||
``` | ||
var file = fs.readFileSync('/etc/hosts') | ||
var hosts = new Hosts(file); | ||
// or (new Hosts()).parse(file); | ||
// return a parsed Hosts object | ||
``` | ||
### Hosts.prototype.resolve(hostname) | ||
``` | ||
hosts.resolve(hostname); | ||
// return the ip | ||
``` | ||
### Hosts.prototype.toJSON() | ||
``` | ||
hosts.toJSON(); | ||
// return a json formatted object | ||
``` | ||
## What is the Hosts file | ||
[hosts (file)](http://en.wikipedia.org/wiki/Hosts_%28file%29) | ||
@@ -25,0 +49,0 @@ |
@@ -1,3 +0,26 @@ | ||
module.exports = function parseHosts(hosts) { | ||
var parsed = []; | ||
var _ = require('lodash'); | ||
var Hosts = function (hosts) { | ||
if (!(this instanceof Hosts)) { | ||
return new Hosts(hosts); | ||
} | ||
this._origin = []; | ||
return this.parse(hosts); | ||
}; | ||
Hosts.prototype.toJSON = function () { | ||
return this._origin; | ||
}; | ||
Hosts.prototype.resolve = function (hostname) { | ||
var matching = _.findLast(this._origin, function (rule) { | ||
return rule.hostname === hostname; | ||
}); | ||
if (matching && matching.ip) { | ||
return matching.ip; | ||
} | ||
}; | ||
Hosts.prototype.parse = function (hosts) { | ||
var self = this; | ||
hosts = (hosts || '').split('\n'); | ||
@@ -21,3 +44,3 @@ hosts.forEach(function (line) { | ||
hostnames.forEach(function (hostname) { | ||
parsed.push({ | ||
self._origin.push({ | ||
ip: ip, | ||
@@ -28,3 +51,5 @@ hostname: hostname | ||
}); | ||
return parsed; | ||
} | ||
return self; | ||
}; | ||
module.exports = Hosts; |
@@ -5,3 +5,3 @@ var fs = require('fs'); | ||
var parser = require('..'); | ||
var Hosts = require('..'); | ||
@@ -16,3 +16,3 @@ function asset (filename) { | ||
it('shoule be ok', function () { | ||
expect(parser('127.0.0.1 www.example.com')) | ||
expect((new Hosts('127.0.0.1 www.example.com')).toJSON()) | ||
.to.be.deep.equal([ | ||
@@ -28,3 +28,3 @@ { | ||
it('shoule be ok', function () { | ||
expect(parser('127.0.0.1 www.example.com www.foobar.com')) | ||
expect((new Hosts('127.0.0.1 www.example.com www.foobar.com')).toJSON()) | ||
.to.be.deep.equal([ | ||
@@ -45,3 +45,3 @@ { | ||
it('shoule be nothing', function () { | ||
expect(parser('#127.0.0.1 www.example.com')) | ||
expect((new Hosts('#127.0.0.1 www.example.com')).toJSON()) | ||
.to.be.deep.equal([]); | ||
@@ -52,3 +52,3 @@ }); | ||
it('shoule be one group', function () { | ||
expect(parser('127.0.0.1 www.example.com #comments')) | ||
expect((new Hosts('127.0.0.1 www.example.com #comments')).toJSON()) | ||
.to.be.deep.equal([ | ||
@@ -67,3 +67,3 @@ { | ||
it('shoule be ok', function () { | ||
expect(parser('127.0.0.1 www.example.com')) | ||
expect((new Hosts('127.0.0.1 www.example.com')).toJSON()) | ||
.to.be.deep.equal([ | ||
@@ -79,3 +79,3 @@ { | ||
it('shoule be ok', function () { | ||
expect(parser('127.0.0.1 www.example.com')) | ||
expect((new Hosts('127.0.0.1 www.example.com')).toJSON()) | ||
.to.be.deep.equal([ | ||
@@ -91,3 +91,3 @@ { | ||
it('shoule be ok', function () { | ||
expect(parser('127.0.0.1\twww.example.com')) | ||
expect((new Hosts('127.0.0.1\twww.example.com')).toJSON()) | ||
.to.be.deep.equal([ | ||
@@ -103,3 +103,3 @@ { | ||
it('shoule be ok', function () { | ||
expect(parser('127.0.0.1\t\twww.example.com')) | ||
expect((new Hosts('127.0.0.1\t\twww.example.com')).toJSON()) | ||
.to.be.deep.equal([ | ||
@@ -114,6 +114,28 @@ { | ||
}); | ||
describe('resolve', function () { | ||
describe('repetition hostname', function () { | ||
it('should be the last one', function () { | ||
var hosts = '\ | ||
127.0.0.1 www.example.com \n\ | ||
192.168.1.1 www.example.com\ | ||
'; | ||
expect((new Hosts(hosts)).resolve('www.example.com')) | ||
.to.be.equal('192.168.1.1'); | ||
}); | ||
}); | ||
describe('mismatching hostname', function () { | ||
it('should return an undefined', function () { | ||
var hosts = '\ | ||
127.0.0.1 www.example.com\ | ||
'; | ||
expect((new Hosts(hosts)).resolve('mismatching.com')) | ||
.to.be.an.undefined; | ||
}); | ||
}); | ||
}); | ||
describe('file', function () { | ||
describe('example hosts file', function () { | ||
it('should be ok', function () { | ||
expect(parser(asset('./assets/hosts'))) | ||
var hosts = asset('./assets/hosts'); | ||
expect((new Hosts(hosts)).toJSON()) | ||
.to.be.deep.equal([ | ||
@@ -120,0 +142,0 @@ { |
8122
184
50
1
+ Addedlodash@^3.0.1
+ Addedlodash@3.10.1(transitive)