@corvina/cidr
Version:
CIDR Operation helper
1 lines • 4.39 kB
JavaScript
const t=/^.*?(?=[\^#%&$\*:<>\?\/\{\|\}\\\s[a-zA-Z]).*$/;var r;!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"}(r||(r={}));const n=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 s=Math.pow(2,32-n);if(s-1>=e){r[0]%s!=0&&(o=r[0]-r[0]%s),r[r.length-1]>o+s&&n--;break}}return`${s(o)}/${n}`},e=(t,r,n)=>{for(;t.length<n;)t=r+t;return t},o=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(".")},I=t=>("number"==typeof t&&(t=s(t)),t.split(".").map((t=>parseInt(t)))),i=t=>s(o(t)+1),a=n=>{if(t.test(n))return r.INVALID_IP_ILLEGAL_CHARACTER;const e=n.split(".");if(4!==e.length)return r.INVALID_ADDRESS_NOT_ENOUGH_QUADS;for(let t=0;t<e.length;t++){const n=parseInt(e[t]);if(isNaN(n))return r.INVALID_IP_INVALID_QUAD;if(n>255)return r.INVALID_IP_QUAD_TOO_LARGE;if(n<0)return r.INVALID_IP_QUAD_TOO_SMALL}return null},_={toInt:o,toString:s,commonCidr:t=>{const r=t.map(o);return n(r)},toHex:t=>("string"==typeof t&&(t=o(t)),t.toString(16)),toOctets:I,toBinary:t=>{const r=I(t);let n=[];for(let t=0;t<4;t++)n[t]=e((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=I(t);return`${r[3]}.${r[2]}.${r[1]}.${r[0]}.in-addr.arpa`},previous:t=>s(o(t)-1),next:i,toCidr:t=>("number"==typeof t&&(t=s(t)),`${t}/32`),validate:a},u=t=>t.split("/")[0],A=t=>t.split("/")[1],l=t=>parseInt(A(t)),p=t=>[o(c(t)),o(D(t))],c=t=>{const r=u(t),n=o(r),e=n%Math.pow(2,32-l(t));return e>0?s(n-e):r},D=t=>{let r=o(c(t)),n=Math.pow(2,32-l(t));return s(r+n-1)},L=t=>{let r=[];const n=o(D(t));let e=u(t);for(;o(e)<=n;)r.push(e),e=i(e);return r},f=["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"],N={toRange:t=>[c(t),D(t)],usable:(t,r)=>{const{startStopOnly:n=!1}=r||{};let e=A(t);if(!e)return[];if("32"===e)return[u(t)];if("31"===e)return L(t);const I=[];let i=o(c(t))+1;const a=o(D(t));if(n)return[s(i),s(a-1)];for(;i<a;)I.push(s(i)),i+=1;return I},toIntRange:p,commonCidr:t=>{const r=t.map((t=>p(t))),e=[].concat.apply([],r).sort();return n(e)},max:D,min:c,count:t=>Math.pow(2,32-l(t)),netmask:t=>s(Math.pow(2,32)-Math.pow(2,32-l(t))),wildcardmask:t=>s(Math.pow(2,32-l(t))-1),broadcast:t=>D(t),subnets:(t,r,n)=>{l(t);let e=o(u(t)),I=o(D(t)),i=[],a=Math.pow(2,32-r);for(n&&(n=e+n*a)<I&&(I=n);e<I;)i.push(`${s(e)}/${r}`),e+=a;return i},ips:L,includes:(t,r)=>{const n=o(r);return n>=o(c(t))&&n<=o(D(t))},random:t=>{const[r,n]=p(t);return s(Math.floor(Math.random()*(n-r+1))+r)},next:t=>`${s(o(u(t))+Math.pow(2,32-l(t)))}/${l(t)}`,previous:t=>`${s(o(u(t))-Math.pow(2,32-l(t)))}/${l(t)}`,nth:(t,r)=>`${s(o(u(t))+Math.pow(2,32-l(t))*r)}/${l(t)}`,address:u,mask:l,validate:t=>{const n=u(t),e=a(n);if(null!==e)return e;const o=A(t);return f.includes(o)?n!==c(t)?r.INVALID_CIDR_BETTER_EXPRESSION:null:r.INVALID_BITMASK_VALUE}};var h=Object.freeze({__proto__:null,get ErrorMessages(){return r},ip:_,cidr:N});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 d(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=_.validate(n);if(e)throw new Error(e);n=d(n).toString(),t=`${r}/${n}`}t=`${N.min(t)}/${n}`;const e=N.validate(t);if(e)throw new Error(e);return t}{const n=_.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:d});export{h as cidr,S as endpoint};