UNPKG

@corvina/cidr

Version:

CIDR Operation helper

1 lines 4.67 kB
!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 e;!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"}(e||(e={}));const n=t=>{const r=t.sort();let e=0;const n=r[r.length-1]-r[0];let o=r[0];for(let t=0;t<=32;t++){e=32-t;const i=Math.pow(2,32-e);if(i-1>=n){r[0]%i!=0&&(o=r[0]-r[0]%i),r[r.length-1]>o+i&&e--;break}}return`${s(o)}/${e}`},o=(t,r,e)=>{for(;t.length<e;)t=r+t;return t},i=t=>t.split(".").reduce(((t,r,e)=>t+parseInt(r)*Math.pow(256,3-e)),0),s=t=>{let r=t,e=[];for(let t=0;t<4;t++)0!=r?(e.push(Math.floor(r/Math.pow(256,3-t))),r%=Math.pow(256,3-t)):e.push(0);return e.join(".")},I=t=>("number"==typeof t&&(t=s(t)),t.split(".").map((t=>parseInt(t)))),a=t=>s(i(t)+1),_=t=>{if(r.test(t))return e.INVALID_IP_ILLEGAL_CHARACTER;const n=t.split(".");if(4!==n.length)return e.INVALID_ADDRESS_NOT_ENOUGH_QUADS;for(let t=0;t<n.length;t++){const r=parseInt(n[t]);if(isNaN(r))return e.INVALID_IP_INVALID_QUAD;if(r>255)return e.INVALID_IP_QUAD_TOO_LARGE;if(r<0)return e.INVALID_IP_QUAD_TOO_SMALL}return null},u={toInt:i,toString:s,commonCidr:t=>{const r=t.map(i);return n(r)},toHex:t=>("string"==typeof t&&(t=i(t)),t.toString(16)),toOctets:I,toBinary:t=>{const r=I(t);let e=[];for(let t=0;t<4;t++)e[t]=o((r[t]>>>0).toString(2),"0",8);return`${e[0]}.${e[1]}.${e[2]}.${e[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(i(t)-1),next:a,toCidr:t=>("number"==typeof t&&(t=s(t)),`${t}/32`),validate:_},l=t=>t.split("/")[0],p=t=>t.split("/")[1],A=t=>parseInt(p(t)),f=t=>[i(c(t)),i(D(t))],c=t=>{const r=l(t),e=i(r),n=e%Math.pow(2,32-A(t));return n>0?s(e-n):r},D=t=>{let r=i(c(t)),e=Math.pow(2,32-A(t));return s(r+e-1)},L=t=>{let r=[];const e=i(D(t));let n=l(t);for(;i(n)<=e;)r.push(n),n=a(n);return r},d=["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=>[c(t),D(t)],usable:(t,r)=>{const{startStopOnly:e=!1}=r||{};let n=p(t);if(!n)return[];if("32"===n)return[l(t)];if("31"===n)return L(t);const o=[];let I=i(c(t))+1;const a=i(D(t));if(e)return[s(I),s(a-1)];for(;I<a;)o.push(s(I)),I+=1;return o},toIntRange:f,commonCidr:t=>{const r=t.map((t=>f(t))),e=[].concat.apply([],r).sort();return n(e)},max:D,min:c,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,e)=>{A(t);let n=i(l(t)),o=i(D(t)),I=[],a=Math.pow(2,32-r);for(e&&(e=n+e*a)<o&&(o=e);n<o;)I.push(`${s(n)}/${r}`),n+=a;return I},ips:L,includes:(t,r)=>{const e=i(r);return e>=i(c(t))&&e<=i(D(t))},random:t=>{const[r,e]=f(t);return s(Math.floor(Math.random()*(e-r+1))+r)},next:t=>`${s(i(l(t))+Math.pow(2,32-A(t)))}/${A(t)}`,previous:t=>`${s(i(l(t))-Math.pow(2,32-A(t)))}/${A(t)}`,nth:(t,r)=>`${s(i(l(t))+Math.pow(2,32-A(t))*r)}/${A(t)}`,address:l,mask:A,validate:t=>{const r=l(t),n=_(r);if(null!==n)return n;const o=p(t);return d.includes(o)?r!==c(t)?e.INVALID_CIDR_BETTER_EXPRESSION:null:e.INVALID_BITMASK_VALUE}};var N=Object.freeze({__proto__:null,get ErrorMessages(){return e},ip:u,cidr:h});function E(t){var r,e,n=[];for(r=0;r<4;r++)e=Math.min(t,8),n.push(256-Math.pow(2,8-e)),t-=e;return n.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,e]=t.split("/");if(e){if(e.includes(".")){const n=u.validate(e);if(n)throw new Error(n);e=m(e).toString(),t=`${r}/${e}`}t=`${h.min(t)}/${e}`;const n=h.validate(t);if(n)throw new Error(n);return t}{const e=u.validate(r);if(e)throw new Error(e);return t}},convertNetworkAddressWithBitmaskToNetmask:function(t){if(t.includes("/")){let[r,e]=t.split("/");if(!e)return t;if(!e.includes("."))return e=E(parseInt(e)).toString(),t=`${r}/${e}`}return t},convertBitmaskToNetmask:E,convertNetmaskToBitmask:m});t.cidr=N,t.endpoint=S,Object.defineProperty(t,"__esModule",{value:!0})}));