@corvina/cidr
Version:
CIDR Operation helper
1 lines • 4.89 kB
JavaScript
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((t="undefined"!=typeof globalThis?globalThis:t||self).$corvina={})}(this,(function(t){"use strict";const r=/^.*?(?=[\^#%&$\*:<>\?\/\{\|\}\\\s[a-zA-Z]).*$/;var n;!function(t){t.INVALID_IP_ILLEGAL_CHARACTER="INVALID_IP_ILLEGAL_CHARACTER",t.INVALID_ADDRESS_NOT_ENOUGH_QUADS="INVALID_ADDRESS_NOT_ENOUGH_QUADS",t.INVALID_IP_INVALID_QUAD="INVALID_IP_INVALID_QUAD",t.INVALID_IP_QUAD_TOO_LARGE="INVALID_IP_QUAD_TOO_LARGE",t.INVALID_IP_QUAD_TOO_SMALL="INVALID_IP_QUAD_TOO_SMALL",t.INVALID_BITMASK_VALUE="INVALID_BITMASK_VALUE",t.INVALID_CIDR_BETTER_EXPRESSION="INVALID_CIDR_BETTER_EXPRESSION"}(n||(n={}));const e=t=>{const r=t.sort();let n=0;const e=r[r.length-1]-r[0];let o=r[0];for(let t=0;t<=32;t++){n=32-t;const i=Math.pow(2,32-n);if(i-1>=e){r[0]%i!=0&&(o=r[0]-r[0]%i),r[r.length-1]>o+i&&n--;break}}return`${s(o)}/${n}`},o=(t,r,n)=>{for(;t.length<n;)t=r+t;return t},i=t=>t.split(".").reduce(((t,r,n)=>t+parseInt(r)*Math.pow(256,3-n)),0),s=t=>{let r=t,n=[];for(let t=0;t<4;t++)0!=r?(n.push(Math.floor(r/Math.pow(256,3-t))),r%=Math.pow(256,3-t)):n.push(0);return n.join(".")},a=t=>("number"==typeof t&&(t=s(t)),t.split(".").map((t=>parseInt(t)))),I=t=>s(i(t)+1),_=t=>{if(r.test(t))return n.INVALID_IP_ILLEGAL_CHARACTER;const e=t.split(".");if(4!==e.length)return n.INVALID_ADDRESS_NOT_ENOUGH_QUADS;for(let t=0;t<e.length;t++){const r=parseInt(e[t]);if(isNaN(r))return n.INVALID_IP_INVALID_QUAD;if(r>255)return n.INVALID_IP_QUAD_TOO_LARGE;if(r<0)return n.INVALID_IP_QUAD_TOO_SMALL}return null},u={toInt:i,toString:s,commonCidr:t=>{const r=t.map(i);return e(r)},toHex:t=>("string"==typeof t&&(t=i(t)),t.toString(16)),toOctets:a,toBinary:t=>{const r=a(t);let n=[];for(let t=0;t<4;t++)n[t]=o((r[t]>>>0).toString(2),"0",8);return`${n[0]}.${n[1]}.${n[2]}.${n[3]}`},reverse:t=>{"number"==typeof t&&(t=s(t));const r=a(t);return`${r[3]}.${r[2]}.${r[1]}.${r[0]}.in-addr.arpa`},previous:t=>s(i(t)-1),next:I,toCidr:t=>("number"==typeof t&&(t=s(t)),`${t}/32`),validate:_,binaryToIpString:t=>t.split(".").map((t=>parseInt(t,2))).join(".")},p=t=>t.split("/")[0],l=t=>t.split("/")[1],A=t=>parseInt(l(t)),c=t=>[i(f(t)),i(d(t))],f=t=>{const r=p(t),n=i(r),e=n%Math.pow(2,32-A(t));return e>0?s(n-e):r},d=t=>{let r=i(f(t)),n=Math.pow(2,32-A(t));return s(r+n-1)},D=t=>{let r=[];const n=i(d(t));let e=p(t);for(;i(e)<=n;)r.push(e),e=I(e);return r},L=["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32"],h={toRange:t=>[f(t),d(t)],usable:(t,r)=>{const{startStopOnly:n=!1}=r||{};let e=l(t);if(!e)return[];if("32"===e)return[p(t)];if("31"===e)return D(t);const o=[];let a=i(f(t))+1;const I=i(d(t));if(n)return[s(a),s(I-1)];for(;a<I;)o.push(s(a)),a+=1;return o},toIntRange:c,commonCidr:t=>{const r=t.map((t=>c(t))),n=[].concat.apply([],r).sort();return e(n)},max:d,min:f,count:t=>Math.pow(2,32-A(t)),netmask:t=>s(Math.pow(2,32)-Math.pow(2,32-A(t))),wildcardmask:t=>s(Math.pow(2,32-A(t))-1),broadcast:t=>d(t),subnets:(t,r,n)=>{A(t);let e=i(p(t)),o=i(d(t)),a=[],I=Math.pow(2,32-r);for(n&&(n=e+n*I)<o&&(o=n);e<o;)a.push(`${s(e)}/${r}`),e+=I;return a},ips:D,includes:(t,r)=>{const n=i(r);return n>=i(f(t))&&n<=i(d(t))},random:t=>{const[r,n]=c(t);return s(Math.floor(Math.random()*(n-r+1))+r)},next:t=>`${s(i(p(t))+Math.pow(2,32-A(t)))}/${A(t)}`,previous:t=>`${s(i(p(t))-Math.pow(2,32-A(t)))}/${A(t)}`,nth:(t,r)=>`${s(i(p(t))+Math.pow(2,32-A(t))*r)}/${A(t)}`,address:p,mask:A,validate:t=>{const r=p(t),e=_(r);if(null!==e)return e;const o=l(t);return L.includes(o)?r!==f(t)?n.INVALID_CIDR_BETTER_EXPRESSION:null:n.INVALID_BITMASK_VALUE}};var N=Object.freeze({__proto__:null,get ErrorMessages(){return n},ip:u,cidr:h});function E(t){var r,n,e=[];for(r=0;r<4;r++)n=Math.min(t,8),e.push(256-Math.pow(2,8-n)),t-=n;return e.join(".")}function m(t){return t.split(".").map((t=>parseInt(t,10).toString(2))).join("").split("").filter((t=>"1"===t)).length}var S=Object.freeze({__proto__:null,validateIpAddressForEndpoint:function(t){if(!t)throw new Error("Invalid");let[r,n]=t.split("/");if(n){if(n.includes(".")){const e=u.validate(n);if(e)throw new Error(e);n=m(n).toString(),t=`${r}/${n}`}t=`${h.min(t)}/${n}`;const e=h.validate(t);if(e)throw new Error(e);return t}{const n=u.validate(r);if(n)throw new Error(n);return t}},convertNetworkAddressWithBitmaskToNetmask:function(t){if(t.includes("/")){let[r,n]=t.split("/");if(!n)return t;if(!n.includes("."))return n=E(parseInt(n)).toString(),t=`${r}/${n}`}return t},convertBitmaskToNetmask:E,convertNetmaskToBitmask:m,anonymizeIp:function(t,r){const n=u.toBinary(t).replace(/\./g,"").slice(0,r).padEnd(32,"0").replace(/(.{8})(?=.)/g,"$1.");return u.binaryToIpString(n)}});t.cidr=N,t.endpoint=S,Object.defineProperty(t,"__esModule",{value:!0})}));