Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 92f36fdb5b | |||
| 721fe2dcf8 | |||
| 4dfd70f6a7 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.30.5",
|
"version": "0.30.7",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
10
src/ip.ts
10
src/ip.ts
@@ -45,18 +45,18 @@ export function reservedIp(ip: string): boolean {
|
|||||||
/**
|
/**
|
||||||
* Check if IP is within a reserved range
|
* Check if IP is within a reserved range
|
||||||
* @param {string} ip
|
* @param {string} ip
|
||||||
* @returns {string | null}
|
* @returns {null | 'Invalid' | 'Host' | 'Loopback' | 'Private (Class A)' | 'Private (Class B)' | 'Link-Local' | 'IETF Protocol Assignments' | 'Documentation (TEST-NET-1)' | 'Benchmarking' | 'Documentation (TEST-NET-2)' | 'Documentation (TEST-NET-3)' | 'Multicast' | 'Reserved (Class E)'} null if public, class name if reserved
|
||||||
*/
|
*/
|
||||||
export function isReserved(ip: string) {
|
export function isReserved(ip: string): null | 'Invalid' | 'Host' | 'Loopback' | 'Private (Class A)' | 'Private (Class B)' | 'Link-Local' | 'IETF Protocol Assignments' | 'Documentation (TEST-NET-1)' | 'Benchmarking' | 'Documentation (TEST-NET-2)' | 'Documentation (TEST-NET-3)' | 'Multicast' | 'Reserved (Class E)' {
|
||||||
const ipToNumber = (ip: string) => {
|
const ipToNumber = (ip: string) => {
|
||||||
return ip.split('.').reduce((acc: number, octet: string) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
|
return ip.split('.').reduce((acc: number, octet: string) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ipNum = ipToNumber(ip);
|
const ipNum = ipToNumber(ip);
|
||||||
if (ipNum === null) return 'invalid';
|
if (ipNum === null) return 'Invalid';
|
||||||
|
|
||||||
const reserved = [
|
const reserved = [
|
||||||
{ name: "This host", start: ipToNumber("0.0.0.0"), end: ipToNumber("0.255.255.255") },
|
{ name: "Host", start: ipToNumber("0.0.0.0"), end: ipToNumber("0.255.255.255") },
|
||||||
{ name: "Loopback", start: ipToNumber("127.0.0.0"), end: ipToNumber("127.255.255.255") },
|
{ name: "Loopback", start: ipToNumber("127.0.0.0"), end: ipToNumber("127.255.255.255") },
|
||||||
{ name: "Private (Class A)", start: ipToNumber("10.0.0.0"), end: ipToNumber("10.255.255.255") },
|
{ name: "Private (Class A)", start: ipToNumber("10.0.0.0"), end: ipToNumber("10.255.255.255") },
|
||||||
{ name: "CGNAT", start: ipToNumber("100.64.0.0"), end: ipToNumber("100.127.255.255") },
|
{ name: "CGNAT", start: ipToNumber("100.64.0.0"), end: ipToNumber("100.127.255.255") },
|
||||||
@@ -73,5 +73,5 @@ export function isReserved(ip: string) {
|
|||||||
];
|
];
|
||||||
|
|
||||||
const match = reserved.find(r => ipNum >= r.start && ipNum <= r.end);
|
const match = reserved.find(r => ipNum >= r.start && ipNum <= r.end);
|
||||||
return match ? match.name : null;
|
return match ? <any>match.name : null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,8 +97,10 @@ export function deepCopy<T>(value: T): T {
|
|||||||
* @return {any} The des
|
* @return {any} The des
|
||||||
*/
|
*/
|
||||||
export function deepMerge<T>(target: any, ...sources: any[]): T {
|
export function deepMerge<T>(target: any, ...sources: any[]): T {
|
||||||
|
const BLOCKED = new Set(['__proto__', 'constructor', 'prototype']);
|
||||||
sources.forEach(s => {
|
sources.forEach(s => {
|
||||||
for(const key in s) {
|
for(const key in s) {
|
||||||
|
if(BLOCKED.has(key) || !Object.prototype.hasOwnProperty.call(s, key)) continue;
|
||||||
if(s[key] && typeof s[key] == 'object' && !Array.isArray(s[key])) {
|
if(s[key] && typeof s[key] == 'object' && !Array.isArray(s[key])) {
|
||||||
if(!target[key]) target[key] = {};
|
if(!target[key]) target[key] = {};
|
||||||
deepMerge(target[key], s[key]);
|
deepMerge(target[key], s[key]);
|
||||||
|
|||||||
Reference in New Issue
Block a user