Skip to content

Commit

Permalink
2.0.0 - ES modules
Browse files Browse the repository at this point in the history
  • Loading branch information
arty-name committed Jan 10, 2021
1 parent 14bb869 commit 9f933c5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2,394 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
language: node_js
node_js:
- stable
- '10'
- '12'
- '13'
- '14'
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Artemy Tregubenko
Copyright (c) 2015 Tom Adler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import isMartianIP from 'martian-cidr'
console.log(isMartianIP('10.0.0.1')) // true
```

## ES modules only

Version 2.0.0 introduces a breaking change:
the support for CommonJS is removed, minimal Node.js version becomes 14.

## Install

npm install --save martian-cidr
Expand All @@ -27,7 +32,7 @@ console.log(isMartianIP('10.0.0.1')) // true

## Author

[Artemy Tregubenko](https://arty.name/)
[Tom Adler](https://arty.name/)

## License

Expand Down
21 changes: 7 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "martian-cidr",
"version": "1.0.6",
"version": "2.0.0",
"description": "Check if an IP address is from reserved subnet",
"main": "index.js",
"module": "src/cidrs.js",
"type": "module",
"engines": {
"node": ">=14"
},
"scripts": {
"prepublish": "babel --out-dir es5/ src/cidrs.js src/index.js",
"test": "babel-node src/test.js"
"test": "node src/test.js"
},
"keywords": [
"martian",
Expand All @@ -19,7 +21,7 @@
"ipv6"
],
"author": {
"name": "Artemy Tregubenko",
"name": "Tom Adler",
"email": "[email protected]",
"url": "https://arty.name/"
},
Expand All @@ -29,15 +31,6 @@
"ipaddr.js": "^1.0.3"
},
"devDependencies": {
"@babel/cli": "^7.12.7",
"@babel/core": "^7.12.7",
"@babel/node": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"tape": "^5.0.1"
},
"babel": {
"presets": [
"@babel/preset-env"
]
}
}
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import ipaddr from 'ipaddr.js'
import {IPv4, IPv6, IPv6Exception} from './cidrs'
import {IPv4, IPv6, IPv6Exception} from './cidrs.js'

function parseCIDR(cidr) {
return ipaddr.parseCIDR(cidr)
}

let IPv4Subnets = IPv4.map(parseCIDR)
let IPv6Subnets = IPv6.map(parseCIDR)
let exceptionalSubnet = parseCIDR(IPv6Exception)
const IPv4Subnets = IPv4.map(parseCIDR)
const IPv6Subnets = IPv6.map(parseCIDR)
const exceptionalSubnet = parseCIDR(IPv6Exception)

export default function isMartianIP(ip) {
if (!ipaddr.isValid(ip)) {
throw new Error(`Expected valid IP address, called with "${ip}"`)
}
let parsedIP = ipaddr.parse(ip)

const parsedIP = ipaddr.parse(ip)
function matchIP(subnet) {
return parsedIP.match(subnet)
}

if (ipaddr.IPv4.isIPv4(ip)) {
return IPv4Subnets.some(matchIP)
}

return IPv6Subnets.some(matchIP) && !parsedIP.match(exceptionalSubnet)
}
2 changes: 1 addition & 1 deletion src/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape'
import isMartianIP from './index'
import isMartianIP from './index.js'

test('Martian CIDR', function(t) {
t.equal(isMartianIP('8.8.8.8'), false, 'should return false for public IPv4 addresses')
Expand Down
Loading

0 comments on commit 9f933c5

Please sign in to comment.