Skip to content

Commit

Permalink
Merge pull request #3 from demskie/major-refactor
Browse files Browse the repository at this point in the history
Major refactor
  • Loading branch information
demskie authored Jul 15, 2019
2 parents e4f23bd + bb1527f commit 761ee14
Show file tree
Hide file tree
Showing 29 changed files with 2,322 additions and 1,664 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"parser": "@typescript-eslint/parser",
"extends": ["plugin:@typescript-eslint/recommended", "prettier/@typescript-eslint", "plugin:prettier/recommended"],
"rules": {
"@typescript-eslint/explicit-function-return-type": false,
"@typescript-eslint/no-object-literal-type-assertion": false
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/no-object-literal-type-assertion": 0
}
}
104 changes: 74 additions & 30 deletions README.md

Large diffs are not rendered by default.

1,380 changes: 713 additions & 667 deletions package-lock.json

Large diffs are not rendered by default.

33 changes: 19 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"scripts": {
"build": "tsc && docts && jest --coverage",
"test": "tsc --noEmit && jest --coverage",
"bench": "ts-node -T **/*/index.bench.ts && ts-node -T **/*/match.bench.ts"
"bench": "npm run bench-index && npm run bench-match && npm run bench-sort",
"bench-index": "ts-node -T src/benchmarks/index.bench.ts",
"bench-match": "ts-node -T src/benchmarks/match.bench.ts",
"bench-sort": "ts-node -T src/benchmarks/sort.bench.ts"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -72,27 +75,29 @@
},
"devDependencies": {
"@types/benchmark": "^1.0.31",
"@types/jest": "^24.0.11",
"@types/ip-address": "^5.8.2",
"@types/jest": "^24.0.15",
"@types/netmask": "^1.0.30",
"@types/node": "^12.0.8",
"@typescript-eslint/eslint-plugin": "^1.6.0",
"@typescript-eslint/parser": "^1.6.0",
"@types/node": "^12.6.2",
"@typescript-eslint/eslint-plugin": "^1.12.0",
"@typescript-eslint/parser": "^1.12.0",
"benchmark": "^2.1.4",
"cidr-matcher": "^2.1.0",
"coveralls": "^3.0.3",
"coveralls": "^3.0.5",
"docts": "^0.2.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"eslint": "^6.0.1",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-prettier": "^3.1.0",
"ip-address": "^5.9.2",
"ipaddr.js": "^1.9.0",
"jest": "^24.7.1",
"jest": "^24.8.0",
"netmask": "^1.0.6",
"prettier": "^1.16.4",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"ts-node": "^8.2.0",
"ts-node": "^8.3.0",
"typedoc": "^0.14.2",
"typedoc-plugin-markdown": "^1.2.0",
"typescript": "^3.4.2"
"typedoc-plugin-markdown": "^2.0.8",
"typescript": "^3.5.3"
},
"dependencies": {}
}
35 changes: 8 additions & 27 deletions src/IPv4.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
import * as errors from "./errors";
import * as shared from "./shared";
import * as weight from "./weight";
import { Network } from "./network";
import { Address } from "./address";

export function addrToBytes(addr: string, throwErrors?: boolean) {
const ip = addr.split(".");
if (ip.length === 4) {
const bytes = new Uint8Array(4);
for (var i = 0; i < 4; i++) {
const val = parseInt(ip[i], 10);
if (val < 0 || val > 255) {
if (throwErrors) throw errors.AddrInvalidInteger;
return null;
}
bytes[i] = val;
}
return bytes;
}
if (throwErrors) throw errors.AddrNotFourElements;
return null;
}

export function bytesToAddr(bytes: Uint8Array, throwErrors?: boolean) {
if (bytes.length >= 4) {
return bytes.slice(bytes.length - 4, bytes.length).join(".");
}
export function bytesToAddr(bytes: number[], throwErrors?: boolean) {
if (bytes.length === 4) return `${bytes[0]}.${bytes[1]}.${bytes[2]}.${bytes[3]}`;
if (throwErrors) throw errors.BytesNotFourElements;
return null;
}

export function randomAddress() {
return bytesToAddr(Uint8Array.from(Array(4), () => Math.random() * 255));
return bytesToAddr(Array.from(Array(4), () => Math.floor(Math.random() * 256)));
}

const choices = Array.from(Array(31), (_, idx) => new weight.WeightedValue(Math.pow(2, idx), idx + 1));

export function randomNetwork() {
const bytes = Uint8Array.from(Array(4), () => Math.random() * 255);
const bytes = Array.from(Array(4), () => Math.floor(Math.random() * 256));
const addr = new Address().setBytes(bytes);
const cidr = weight.getValue(choices) as number;
shared.applySubnetMask(bytes, cidr);
return `${bytesToAddr(bytes)}/${cidr}`;
return new Network().from(addr, cidr);
}
91 changes: 9 additions & 82 deletions src/IPv6.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,10 @@
import * as shared from "./shared";
import * as errors from "./errors";
import * as weight from "./weight";
import { Network } from "./network";
import { Address } from "./address";

function padZeros(addr: string, throwErrors?: boolean) {
if (addr.length >= 2) {
if (addr.slice(0, 2) === "::") {
addr = "0" + addr;
}
if (addr.slice(addr.length - 2) === "::") {
addr += "0";
}
}
const splitAddr = addr.split("::");
if (splitAddr.length === 1) {
return addr;
} else if (splitAddr.length === 2) {
const hextetCount = splitAddr[0].split(":").length + splitAddr[1].split(":").length;
splitAddr[0] += shared.repeatString(":0", 8 - hextetCount);
return splitAddr.join(":");
}
if (throwErrors) throw errors.GenericPadZeros;
return null;
}

// Network-Specific Prefix IPv4 IPv4-embedded IPv6 address
// 2001:db8:122:344::/96 192.0.2.33 2001:db8:122:344::192.0.2.33
// https://tools.ietf.org/html/rfc6052

export function convertEmbeddedIPv4(addr: string) {
let hextets = addr.split(":");
const octets = hextets[hextets.length - 1].split(".");
if (octets.length === 4) {
const a = parseInt(octets[0], 10).toString(16);
const b = parseInt(octets[1], 10).toString(16);
const c = parseInt(octets[2], 10).toString(16);
const d = parseInt(octets[3], 10).toString(16);
hextets = hextets.slice(0, hextets.length - 1);
hextets.push(parseInt(a + b, 16).toString(16));
hextets.push(parseInt(c + d, 16).toString(16));
addr = hextets.join(":");
}
return addr;
}

export function addrToBytes(addr: string, throwErrors?: boolean) {
const padded = padZeros(addr);
if (padded !== null) {
const hextets = padded.split(":");
if (hextets.length === 8) {
const arr = new Uint8Array(16);
for (var j = 0; j < 8; j++) {
const hextet = hextets[j];
switch (hextet.length) {
case 1:
case 2:
arr[2 * j] = 0;
arr[2 * j + 1] = parseInt(hextet, 16);
break;
case 3:
case 4:
const val = parseInt(hextet, 16);
arr[2 * j] = Math.floor(val / 256);
arr[2 * j + 1] = val % 256;
break;
default:
if (throwErrors) throw errors.GenericAddrToBytes;
return null;
}
}
return arr;
}
}
if (throwErrors) throw errors.GenericAddrToBytes;
return null;
}

function findLongestZeroHextetChain(bytes: Uint8Array, throwErrors?: boolean) {
if (bytes.length >= 16) {
bytes = bytes.subarray(bytes.length - 16);
function findLongestZeroHextetChain(bytes: number[], throwErrors?: boolean) {
if (bytes.length === 16) {
const canidate = { start: 0, length: 0 };
const longest = { start: 0, length: 0 };
for (var i = 0; i < bytes.length; i += 2) {
Expand All @@ -101,7 +28,7 @@ function findLongestZeroHextetChain(bytes: Uint8Array, throwErrors?: boolean) {
return null;
}

export function bytesToAddr(bytes: Uint8Array, throwErrors?: boolean) {
export function bytesToAddr(bytes: number[], throwErrors?: boolean) {
const longestHextetChain = findLongestZeroHextetChain(bytes, throwErrors);
if (longestHextetChain !== null) {
var result = "";
Expand All @@ -121,14 +48,14 @@ export function bytesToAddr(bytes: Uint8Array, throwErrors?: boolean) {
}

export function randomAddress() {
return bytesToAddr(Uint8Array.from(Array(16), () => Math.random() * 255));
return bytesToAddr(Array.from(Array(16), () => Math.floor(Math.random() * 256)));
}

const choices = Array.from(Array(127), (_, idx) => new weight.WeightedValue(Math.pow(2, idx), idx + 1));

export function randomNetwork() {
const bytes = Uint8Array.from(Array(16), () => Math.random() * 255);
const bytes = Array.from(Array(16), () => Math.floor(Math.random() * 256));
const addr = new Address().setBytes(bytes);
const cidr = weight.getValue(choices) as number;
shared.applySubnetMask(bytes, cidr);
return `${bytesToAddr(bytes)}/${cidr}`;
return new Network().from(addr, cidr);
}
Loading

0 comments on commit 761ee14

Please sign in to comment.