From 3b336eb0f5a88a5ee0f5aaeb45373967445328c7 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 17 Nov 2020 12:09:32 -1000 Subject: [PATCH] Export an ES6 class The `new` keyword is now required. This is technically a breaking change, but since 2.0.0 was released less than 30 minutes ago, I'm considering this a bug fix. --- README.md | 10 ++++++---- index.js | 41 ++++++++++++++++++++--------------------- test/basic.js | 12 ++++++------ 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 1ccd338..3e16f8e 100644 --- a/README.md +++ b/README.md @@ -15,18 +15,20 @@ npm install ip-set ## usage ```js -var ipSet = require('ip-set')(/* optionally pass an array of IP addresses to seed the set with */) +const IPSet = require('ip-set') + +const ipSet = new IPSet(/* optionally pass an array of IP addresses to seed the set with */) ipSet.add(exampleBlockedIP1) ipSet.add(exampleBlockedIP2) -var isBlocked = ipSet.contains(exampleBlockedIP2) // isBlocked will be true +let isBlocked = ipSet.contains(exampleBlockedIP2) // isBlocked will be true ``` CIDR ip's are also supported ```js ipSet.add(`192.168.1.0/24`); -var isBlockedInList= ipSet.contains('192.168.1.0');// isBlockedInList will be true -isBlockedInList= ipSet.contains('192.168.1.255');// isBlockedInList will be true +let isBlockedInList = ipSet.contains('192.168.1.0');// isBlockedInList will be true +isBlockedInList = ipSet.contains('192.168.1.255');// isBlockedInList will be true ``` diff --git a/index.js b/index.js index fc78cf9..0eb8579 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ - const ip = require('ip') -class IPSet { +class IPSetNode { constructor (start, end) { this.start = start this.end = end @@ -23,7 +22,7 @@ class IPSet { update = this.left.add(start, end) if (update) this._balance() } else { - this.left = new IPSet(start, end) + this.left = new IPSetNode(start, end) update = true } } else if (d > 0) { @@ -31,7 +30,7 @@ class IPSet { update = this.right.add(start, end) if (update) this._balance() } else { - this.right = new IPSet(start, end) + this.right = new IPSetNode(start, end) update = true } } @@ -113,11 +112,17 @@ class IPSet { } } -module.exports = blocklist => { - let tree = null - const self = {} +class IPSet { + constructor (blocklist) { + this.tree = null + if (Array.isArray(blocklist)) { + blocklist.forEach(block => { + this.add(block) + }) + } + } - self.add = (start, end) => { + add (start, end) { if (!start) return if (typeof start === 'object') { end = start.end @@ -137,21 +142,15 @@ module.exports = blocklist => { if (start < 0 || end > 4294967295 || end < start) throw new Error('Invalid block range') - if (tree) tree.add(start, end) - else tree = new IPSet(start, end) + if (this.tree) this.tree.add(start, end) + else this.tree = new IPSetNode(start, end) } - self.contains = addr => { - if (!tree) return false + contains (addr) { + if (!this.tree) return false if (typeof addr !== 'number') addr = ip.toLong(addr) - return tree.contains(addr) - } - - if (Array.isArray(blocklist)) { - blocklist.forEach(block => { - self.add(block) - }) + return this.tree.contains(addr) } - - return self } + +module.exports = IPSet diff --git a/test/basic.js b/test/basic.js index b7247d0..2af6ece 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,9 +1,9 @@ const test = require('tape') -const ipSet = require('../') +const IPSet = require('../') test('ipSet.contains respects ipSet.add with only a start', t => { - const set = ipSet() + const set = new IPSet() const ip = '127.0.0.1' t.notOk(set.contains(ip)) @@ -13,7 +13,7 @@ test('ipSet.contains respects ipSet.add with only a start', t => { }) test('ipSet.contains respects ipSet.add with a start and an end', t => { - const set = ipSet() + const set = new IPSet() const ips = ['192.168.1.0', '192.168.1.255'] set.add(ips[0], ips[1]) @@ -26,7 +26,7 @@ test('ipSet.contains respects ipSet.add with a start and an end', t => { }) test('ipSet.contains respects ipSet.add with a cidr', t => { - const set = ipSet() + const set = new IPSet() const cidrIp = '192.168.1.0/24' set.add(cidrIp) @@ -40,7 +40,7 @@ test('ipSet.contains respects ipSet.add with a cidr', t => { }) test('IPv6', t => { - const set = ipSet() + const set = new 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' @@ -62,7 +62,7 @@ test('IPv6', t => { // Fails on checking it doesn't contain a value outside the range. test.skip('IPv6 range', t => { - const set = ipSet() + const set = new IPSet() const start = '0:0:0:0:0:0:0:1' const mid1 = '0:0:0:0:0:0:0:9' const mid2 = '0:0:0:0:0:0:0:a'