UNPKG

@corvina/cidr

Version:

CIDR Operation helper

1 lines 4.61 kB
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 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`${i(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),i=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(".")},s=t=>("number"==typeof t&&(t=i(t)),t.split(".").map((t=>parseInt(t)))),a=t=>i(o(t)+1),I=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:i,commonCidr:t=>{const r=t.map(o);return n(r)},toHex:t=>("string"==typeof t&&(t=o(t)),t.toString(16)),toOctets:s,toBinary:t=>{const r=s(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=i(t));const r=s(t);return`${r[3]}.${r[2]}.${r[1]}.${r[0]}.in-addr.arpa`},previous:t=>i(o(t)-1),next:a,toCidr:t=>("number"==typeof t&&(t=i(t)),`${t}/32`),validate:I,binaryToIpString:t=>t.split(".").map((t=>parseInt(t,2))).join(".")},p=t=>t.split("/")[0],u=t=>t.split("/")[1],l=t=>parseInt(u(t)),A=t=>[o(c(t)),o(D(t))],c=t=>{const r=p(t),n=o(r),e=n%Math.pow(2,32-l(t));return e>0?i(n-e):r},D=t=>{let r=o(c(t)),n=Math.pow(2,32-l(t));return i(r+n-1)},L=t=>{let r=[];const n=o(D(t));let e=p(t);for(;o(e)<=n;)r.push(e),e=a(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=u(t);if(!e)return[];if("32"===e)return[p(t)];if("31"===e)return L(t);const s=[];let a=o(c(t))+1;const I=o(D(t));if(n)return[i(a),i(I-1)];for(;a<I;)s.push(i(a)),a+=1;return s},toIntRange:A,commonCidr:t=>{const r=t.map((t=>A(t))),e=[].concat.apply([],r).sort();return n(e)},max:D,min:c,count:t=>Math.pow(2,32-l(t)),netmask:t=>i(Math.pow(2,32)-Math.pow(2,32-l(t))),wildcardmask:t=>i(Math.pow(2,32-l(t))-1),broadcast:t=>D(t),subnets:(t,r,n)=>{l(t);let e=o(p(t)),s=o(D(t)),a=[],I=Math.pow(2,32-r);for(n&&(n=e+n*I)<s&&(s=n);e<s;)a.push(`${i(e)}/${r}`),e+=I;return a},ips:L,includes:(t,r)=>{const n=o(r);return n>=o(c(t))&&n<=o(D(t))},random:t=>{const[r,n]=A(t);return i(Math.floor(Math.random()*(n-r+1))+r)},next:t=>`${i(o(p(t))+Math.pow(2,32-l(t)))}/${l(t)}`,previous:t=>`${i(o(p(t))-Math.pow(2,32-l(t)))}/${l(t)}`,nth:(t,r)=>`${i(o(p(t))+Math.pow(2,32-l(t))*r)}/${l(t)}`,address:p,mask:l,validate:t=>{const n=p(t),e=I(n);if(null!==e)return e;const o=u(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,anonymizeIp:function(t,r){const n=_.toBinary(t).replace(/\./g,"").slice(0,r).padEnd(32,"0").replace(/(.{8})(?=.)/g,"$1.");return _.binaryToIpString(n)}});export{h as cidr,S as endpoint};