Comparing version 1.0.2 to 2.0.0
205
index.js
var ip = require('ip') | ||
const ip = require('ip') | ||
function IPSet (start, end) { | ||
this.start = start | ||
this.end = end | ||
this.max = end | ||
this.depth = 1 | ||
this.left = null | ||
this.right = null | ||
} | ||
class IPSet { | ||
constructor (start, end) { | ||
this.start = start | ||
this.end = end | ||
this.max = end | ||
this.depth = 1 | ||
this.left = null | ||
this.right = null | ||
} | ||
IPSet.prototype.add = function (start, end) { | ||
var d = start - this.start | ||
var update = false | ||
add (start, end) { | ||
const d = start - this.start | ||
let update = false | ||
if (d === 0 && this.end < end) { | ||
this.end = end | ||
update = true | ||
} else if (d < 0) { | ||
if (this.left) { | ||
update = this.left.add(start, end) | ||
if (update) this._balance() | ||
} else { | ||
this.left = new IPSet(start, end) | ||
if (d === 0 && this.end < end) { | ||
this.end = end | ||
update = true | ||
} else if (d < 0) { | ||
if (this.left) { | ||
update = this.left.add(start, end) | ||
if (update) this._balance() | ||
} else { | ||
this.left = new IPSet(start, end) | ||
update = true | ||
} | ||
} else if (d > 0) { | ||
if (this.right) { | ||
update = this.right.add(start, end) | ||
if (update) this._balance() | ||
} else { | ||
this.right = new IPSet(start, end) | ||
update = true | ||
} | ||
} | ||
} else if (d > 0) { | ||
if (this.right) { | ||
update = this.right.add(start, end) | ||
if (update) this._balance() | ||
} else { | ||
this.right = new IPSet(start, end) | ||
update = true | ||
} | ||
if (update) this._update() | ||
return update | ||
} | ||
if (update) this._update() | ||
return update | ||
} | ||
IPSet.prototype.contains = function (addr) { | ||
var node = this | ||
while (node && !(addr >= node.start && addr <= node.end)) { | ||
if (node.left && node.left.max >= addr) node = node.left | ||
else node = node.right | ||
contains (addr) { | ||
let node = this | ||
while (node && !(addr >= node.start && addr <= node.end)) { | ||
if (node.left && node.left.max >= addr) node = node.left | ||
else node = node.right | ||
} | ||
return !!node | ||
} | ||
return !!node | ||
} | ||
IPSet.prototype._balance = function () { | ||
var ldepth = this.left ? this.left.depth : 0 | ||
var rdepth = this.right ? this.right.depth : 0 | ||
_balance () { | ||
const ldepth = this.left ? this.left.depth : 0 | ||
const rdepth = this.right ? this.right.depth : 0 | ||
if (ldepth > rdepth + 1) { | ||
var lldepth = this.left.left ? this.left.left.depth : 0 | ||
var lrdepth = this.left.right ? this.left.right.depth : 0 | ||
if (lldepth < lrdepth) this.left._rotateRR() | ||
this._rotateLL() | ||
} else if (ldepth + 1 < rdepth) { | ||
var rrdepth = this.right.right ? this.right.right.depth : 0 | ||
var rldepth = this.right.left ? this.right.left.depth : 0 | ||
if (rldepth > rrdepth) this.right._rotateLL() | ||
this._rotateRR() | ||
if (ldepth > rdepth + 1) { | ||
const lldepth = this.left.left ? this.left.left.depth : 0 | ||
const lrdepth = this.left.right ? this.left.right.depth : 0 | ||
if (lldepth < lrdepth) this.left._rotateRR() | ||
this._rotateLL() | ||
} else if (ldepth + 1 < rdepth) { | ||
const rrdepth = this.right.right ? this.right.right.depth : 0 | ||
const rldepth = this.right.left ? this.right.left.depth : 0 | ||
if (rldepth > rrdepth) this.right._rotateLL() | ||
this._rotateRR() | ||
} | ||
} | ||
} | ||
IPSet.prototype._rotateLL = function () { | ||
var _start = this.start | ||
var _end = this.end | ||
var _right = this.right | ||
_rotateLL () { | ||
const _start = this.start | ||
const _end = this.end | ||
const _right = this.right | ||
this.start = this.left.start | ||
this.end = this.left.end | ||
this.right = this.left | ||
this.left = this.left.left | ||
this.start = this.left.start | ||
this.end = this.left.end | ||
this.right = this.left | ||
this.left = this.left.left | ||
this.right.left = this.right.right | ||
this.right.right = _right | ||
this.right.start = _start | ||
this.right.end = _end | ||
this.right.left = this.right.right | ||
this.right.right = _right | ||
this.right.start = _start | ||
this.right.end = _end | ||
this.right._update() | ||
this._update() | ||
} | ||
this.right._update() | ||
this._update() | ||
} | ||
IPSet.prototype._rotateRR = function () { | ||
var _start = this.start | ||
var _end = this.end | ||
var _left = this.left | ||
_rotateRR () { | ||
const _start = this.start | ||
const _end = this.end | ||
const _left = this.left | ||
this.start = this.right.start | ||
this.end = this.right.end | ||
this.end = this.right.end | ||
this.left = this.right | ||
this.right = this.right.right | ||
this.start = this.right.start | ||
this.end = this.right.end | ||
this.end = this.right.end | ||
this.left = this.right | ||
this.right = this.right.right | ||
this.left.right = this.left.left | ||
this.left.left = _left | ||
this.left.start = _start | ||
this.left.end = _end | ||
this.left.right = this.left.left | ||
this.left.left = _left | ||
this.left.start = _start | ||
this.left.end = _end | ||
this.left._update() | ||
this._update() | ||
} | ||
this.left._update() | ||
this._update() | ||
} | ||
IPSet.prototype._update = function () { | ||
this.depth = 1 | ||
if (this.left) this.depth = this.left.depth + 1 | ||
if (this.right && this.depth <= this.right.depth) this.depth = this.right.depth + 1 | ||
this.max = Math.max(this.end, this.left ? this.left.max : 0, this.right ? this.right.max : 0) | ||
_update () { | ||
this.depth = 1 | ||
if (this.left) this.depth = this.left.depth + 1 | ||
if (this.right && this.depth <= this.right.depth) this.depth = this.right.depth + 1 | ||
this.max = Math.max(this.end, this.left ? this.left.max : 0, this.right ? this.right.max : 0) | ||
} | ||
} | ||
module.exports = function (blocklist) { | ||
var tree = null | ||
var self = {} | ||
module.exports = blocklist => { | ||
let tree = null | ||
const self = {} | ||
self.add = function (start, end) { | ||
self.add = (start, end) => { | ||
if (!start) return | ||
@@ -125,10 +127,10 @@ if (typeof start === 'object') { | ||
var cidrStr = /\/\d{1,2}/; | ||
const cidrStr = /\/\d{1,2}/ | ||
if (typeof start === 'string' && cidrStr.test(start)) { | ||
var ipSubnet = ip.cidrSubnet(start); | ||
start = ipSubnet.networkAddress; | ||
end = ipSubnet.broadcastAddress; | ||
const ipSubnet = ip.cidrSubnet(start) | ||
start = ipSubnet.networkAddress | ||
end = ipSubnet.broadcastAddress | ||
} | ||
if (typeof start !== 'number') start = ip.toLong(start) | ||
if (!end) end = start | ||
@@ -143,3 +145,3 @@ if (typeof end !== 'number') end = ip.toLong(end) | ||
self.contains = function (addr) { | ||
self.contains = addr => { | ||
if (!tree) return false | ||
@@ -151,3 +153,3 @@ if (typeof addr !== 'number') addr = ip.toLong(addr) | ||
if (Array.isArray(blocklist)) { | ||
blocklist.forEach(function (block) { | ||
blocklist.forEach(block => { | ||
self.add(block) | ||
@@ -159,2 +161,1 @@ }) | ||
} | ||
{ | ||
"name": "ip-set", | ||
"description": "Efficient mutable set for IP addresses", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"author": "Travis Fischer <fisch0920@gmail.com>", | ||
@@ -10,3 +10,2 @@ "dependencies": { | ||
"devDependencies": { | ||
"mocha": "^5.2.0", | ||
"tape": "^4.6.0" | ||
@@ -13,0 +12,0 @@ }, |
@@ -51,1 +51,3 @@ # ip-set [![travis](https://img.shields.io/travis/transitive-bullshit/ip-set.svg)](https://travis-ci.org/transitive-bullshit/ip-set) [![npm](https://img.shields.io/npm/v/ip-set.svg)](https://npmjs.org/package/ip-set) | ||
MIT. Copyright (c) Travis Fischer | ||
Support my OSS work by <a href="https://twitter.com/transitive_bs">following me on twitter <img src="https://storage.googleapis.com/saasify-assets/twitter-logo.svg" alt="twitter" height="24px" align="center"></a> |
const test = require('tape') | ||
const ipSet = require('../'); | ||
const ipSet = require('../') | ||
test('ipSet.contains respects ipSet.add with only a start', function(t) { | ||
const set = ipSet(); | ||
const ip = '127.0.0.1'; | ||
test('ipSet.contains respects ipSet.add with only a start', t => { | ||
const set = ipSet() | ||
const ip = '127.0.0.1' | ||
t.notOk(set.contains(ip)) | ||
@@ -13,8 +13,8 @@ set.add(ip) | ||
t.end() | ||
}); | ||
}) | ||
test('ipSet.contains respects ipSet.add with a start and an end', function(t) { | ||
const set = ipSet(); | ||
const ips = ['192.168.1.0', '192.168.1.255']; | ||
test('ipSet.contains respects ipSet.add with a start and an end', t => { | ||
const set = ipSet() | ||
const ips = ['192.168.1.0', '192.168.1.255'] | ||
set.add(ips[0], ips[1]) | ||
@@ -27,9 +27,9 @@ t.ok(set.contains(ips[0])) | ||
t.end() | ||
}); | ||
}) | ||
test('ipSet.contains respects ipSet.add with a cidr', function(t) { | ||
const set = ipSet(); | ||
const cidrIp = '192.168.1.0/24'; | ||
set.add(cidrIp); | ||
test('ipSet.contains respects ipSet.add with a cidr', t => { | ||
const set = ipSet() | ||
const cidrIp = '192.168.1.0/24' | ||
set.add(cidrIp) | ||
t.ok(set.contains('192.168.1.0')) | ||
@@ -42,10 +42,10 @@ t.ok(set.contains('192.168.1.5')) | ||
t.end() | ||
}); | ||
}) | ||
test('IPv6', function(t) { | ||
const set = ipSet(); | ||
test('IPv6', t => { | ||
const set = ipSet() | ||
const ip = '0:0:0:0:0:ffff:7f00:1' // 127.0.0.1 | ||
const publicIP = '2607:f8b0:4004:811::200e'; // google | ||
const ipv4 = '127.0.0.1'; | ||
const publicIP = '2607:f8b0:4004:811::200e' // google | ||
const ipv4 = '127.0.0.1' | ||
t.notOk(set.contains(ip)) | ||
@@ -64,7 +64,7 @@ set.add(ip) | ||
t.end() | ||
}); | ||
}) | ||
// Fails on checking it doesn't contain a value outside the range. | ||
test.skip('IPv6 range', function(t) { | ||
const set = ipSet(); | ||
test.skip('IPv6 range', t => { | ||
const set = ipSet() | ||
const start = '0:0:0:0:0:0:0:1' | ||
@@ -75,3 +75,3 @@ const mid1 = '0:0:0:0:0:0:0:9' | ||
const outside = '0:0:0:0:0:0:1:0' | ||
set.add(start, end) | ||
@@ -87,2 +87,1 @@ t.ok(set.contains(start)) | ||
}) | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9859
1
195
53