-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from demskie/major-refactor
Major refactor
- Loading branch information
Showing
29 changed files
with
2,322 additions
and
1,664 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.