Compare commits

...

3 Commits

Author SHA1 Message Date
92f36fdb5b Bump 0.30.7
All checks were successful
Build / Publish Docs (push) Successful in 54s
Build / Build NPM Project (push) Successful in 34s
Build / Tag Version (push) Successful in 9s
2026-08-17 14:56:41 -04:00
721fe2dcf8 Patched prototype pollution
Some checks failed
Build / Tag Version (push) Has been cancelled
Build / Build NPM Project (push) Has been cancelled
Build / Publish Docs (push) Has been cancelled
2026-08-17 14:56:18 -04:00
4dfd70f6a7 Added type helpers to isReserved
All checks were successful
Build / Publish Docs (push) Successful in 35s
Build / Build NPM Project (push) Successful in 39s
Build / Tag Version (push) Successful in 9s
2026-08-08 10:51:56 -04:00
3 changed files with 362 additions and 360 deletions

View File

@@ -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",

View File

@@ -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;
} }

View File

@@ -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]);