Skip to content

Commit

Permalink
Rewrite project to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Sonnefraud authored and Vatoth committed Aug 4, 2021
1 parent 6486341 commit 2b3d775
Show file tree
Hide file tree
Showing 14 changed files with 2,251 additions and 167 deletions.
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"root": true,
"extends": [
"eslint:recommended",
"prettier",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"plugin:mocha/recommended"
],
"env": {
"es2021": true,
"node": true,
"mocha": true
},
"parser": "@typescript-eslint/parser",
"plugins": ["prettier", "mocha"],
"parserOptions": {
"ecmaVersion": 2021,
"project": "./tsconfig.json"
}
}
37 changes: 37 additions & 0 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: build-test

on:
pull_request:
push:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: ["12", "14", "16"]
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
lint:
runs-on: ubuntu-latest
strategy:
matrix:
node: ["16"]
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm run lint
105 changes: 94 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,35 +1,118 @@
#### joe made this: https://goel.io/joe

#####=== Node ===#####

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules
# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test
.env.production

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Debug log from npm
npm-debug.log
# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# IP Range Check

[![Build Status](https://travis-ci.org/danielcompton/ip-range-check.svg?branch=master)](https://travis-ci.org/danielcompton/ip-range-check)
[![Tests Status](https://github.com/danielcompton/ip-range-check/workflows/build-test/badge.svg?branch=master)](https://github.com/Vatoth/ip-range-check/actions?query=workflow%3Abuild-test+branch%3Amaster)

[![NPM](https://nodei.co/npm/ip-range-check.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/ip-range-check)

This module lets you check if an IP matches one or more IP's or [CIDR](http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) ranges. It handles IPv6, IPv4, and IPv4-mapped over IPv6 addresses.

It accepts either:

* A single CIDR or IP string, e.g. `"125.19.23.0/24"`, or `"2001:cdba::3257:9652"`, or `"62.230.58.1"`
* An array of CIDR and/or IP strings, e.g. `["125.19.23.0/24", "2001:cdba::3257:9652", "62.230.58.1"]`
- A single CIDR or IP string, e.g. `"125.19.23.0/24"`, or `"2001:cdba::3257:9652"`, or `"62.230.58.1"`
- An array of CIDR and/or IP strings, e.g. `["125.19.23.0/24", "2001:cdba::3257:9652", "62.230.58.1"]`

Importantly, it cannot match an IPv4 address to an IPv6 CIDR or vice versa, (IPv4-mapped IPv6 addresses notwithstanding).

Expand All @@ -22,48 +22,49 @@ npm install ip-range-check --save
## IPv4

```js
var ipRangeCheck = require("ip-range-check");
import { ipRangeCheck } from "ip-range-check";

// Checks CIDR
ipRangeCheck("192.168.1.1", "102.1.5.2/24")
ipRangeCheck("192.168.1.1", "102.1.5.2/24");
// > false
ipRangeCheck("192.168.1.1", "192.168.1.0/24")
ipRangeCheck("192.168.1.1", "192.168.1.0/24");
// > true

// Checks if IP matches string
ipRangeCheck("192.168.1.1", "192.168.1.1")
ipRangeCheck("192.168.1.1", "192.168.1.1");
// > true

// Checks array of CIDR's and string
ipRangeCheck("192.168.1.1", ["102.1.5.2/24", "192.168.1.0/24", "106.1.180.84"])
ipRangeCheck("192.168.1.1", ["102.1.5.2/24", "192.168.1.0/24", "106.1.180.84"]);
// > true

// Compare IPv6 with IPv4
ipRangeCheck("195.58.1.62", ["::1/128", "125.92.12.53"])
ipRangeCheck("195.58.1.62", ["::1/128", "125.92.12.53"]);
// > false

```

## IPv6

```js
var ipRangeCheck = require("ip-range-check");
import { ipRangeCheck } from "ip-range-check";

// Handles IPv6 in the same fashion as IPv4
ipRangeCheck("::1", "::2/128")
ipRangeCheck("::1", "::2/128");
// > false
ipRangeCheck("::1", ["::2", "::3/128"])
ipRangeCheck("::1", ["::2", "::3/128"]);
// > false
ipRangeCheck("2001:cdba::3257:9652", "2001:cdba::3257:9652/128")
ipRangeCheck("2001:cdba::3257:9652", "2001:cdba::3257:9652/128");
// > true

// IPv4-mapped IPv6 addresses are automatically converted back to IPv4 addresses
// and will match against IPv4 CIDR/IP's.
ipRangeCheck("0:0:0:0:0:FFFF:222.1.41.90", "222.1.41.0/24")
ipRangeCheck("0:0:0:0:0:FFFF:222.1.41.90", "222.1.41.0/24");
// > true

// IPv6 addresses/CIDR's are normalised
ipRangeCheck("2001:cdba:0000:0000:0000:0000:3257:9652", ["2001:cdba::3257:9652"])
ipRangeCheck("2001:cdba:0000:0000:0000:0000:3257:9652", [
"2001:cdba::3257:9652",
]);
// > true
```

Expand Down
16 changes: 0 additions & 16 deletions index.d.ts

This file was deleted.

40 changes: 0 additions & 40 deletions index.js

This file was deleted.

36 changes: 36 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { process, parseCIDR } from "ipaddr.js";

function check_single_cidr(addr: string, cidr: string) {
try {
const parsed_addr = process(addr);
if (cidr.indexOf("/") === -1) {
const parsed_cidr_as_ip = process(cidr);
if (
parsed_addr.kind() === "ipv6" &&
parsed_cidr_as_ip.kind() === "ipv6"
) {
return (
parsed_addr.toNormalizedString() ===
parsed_cidr_as_ip.toNormalizedString()
);
}
return parsed_addr.toString() === parsed_cidr_as_ip.toString();
} else {
const parsed_range = parseCIDR(cidr);
return parsed_addr.match(parsed_range);
}
} catch (e) {
return false;
}
}

export function ipRangeCheck(addr: string, range: string | string[]): boolean {
if (typeof range === "string") {
return check_single_cidr(addr, range);
} else if (Array.isArray(range)) {
return range.find((cidr) => check_single_cidr(addr, cidr)) !== undefined;
}
return false;
}

export default ipRangeCheck;
Loading

0 comments on commit 2b3d775

Please sign in to comment.