Skip to content

Commit

Permalink
Export an ES6 class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
feross committed Nov 17, 2020
1 parent fef21ff commit 3b336eb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
41 changes: 20 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

const ip = require('ip')

class IPSet {
class IPSetNode {
constructor (start, end) {
this.start = start
this.end = end
Expand All @@ -23,15 +22,15 @@ 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) {
if (this.right) {
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
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
12 changes: 6 additions & 6 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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'
Expand All @@ -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'
Expand Down

0 comments on commit 3b336eb

Please sign in to comment.