UNPKG

amazon-route-53-dns-zone-file

Version:

Makes DNS Zone File easy. Parses and validates BIND zone files and can be extended for custom features. Functionality is modular. Features are made open for extension and closed for runtime mutation. Written in TypeScript.

17 lines (14 loc) 15.4 kB
var DNSBindZoneFileCore=function(r){"use strict"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */var e=function(r,t){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(r,e){r.__proto__=e}||function(r,e){for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(r[t]=e[t])})(r,t)};function t(r,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=r}e(r,t),r.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var n=function(){return(n=Object.assign||function(r){for(var e,t=1,n=arguments.length;t<n;t++)for(var i in e=arguments[t])Object.prototype.hasOwnProperty.call(e,i)&&(r[i]=e[i]);return r}).apply(this,arguments)};function i(r){var e="function"==typeof Symbol&&Symbol.iterator,t=e&&r[e],n=0;if(t)return t.call(r);if(r&&"number"==typeof r.length)return{next:function(){return r&&n>=r.length&&(r=void 0),{value:r&&r[n++],done:!r}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function o(r,e){var t="function"==typeof Symbol&&r[Symbol.iterator];if(!t)return r;var n,i,o=t.call(r),a=[];try{for(;(void 0===e||e-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(r){i={error:r}}finally{try{n&&!n.done&&(t=o.return)&&t.call(o)}finally{if(i)throw i.error}}return a}function a(r,e){for(var t=0,n=e.length,i=r.length;t<n;t++,i++)r[i]=e[t];return r}var u,s=function(r){function e(e,t,n){var i=r.call(this,"")||this;return i.type=e,i.actual=t,i.expected=n,i}return t(e,r),e}(TypeError),c=function(r){function e(e,t,n,i,o){void 0===n&&(n=1),void 0===i&&(i="");var a=r.call(this,e)||this;return a.parsingType=t,a.parsingLineNumber=n,a.parsingLineContent=i,a.parsedValue=o,a}return t(e,r),e}(Error),l=function(r){return/[a-z]/i.test(r)},d=function(r){return/\d/.test(r)},f=function(r){return r.replace(/\.$/,"")},p=function(r){return f(r)+"."},y=function(r,e){return"string"==typeof e&&e.match(r)||[]},h=/(\d+)([a-z]+)/gim,v="\n",T=function(r){return r.replace(/\\n/,v).replace(/\r\n?/g,v)},K=function(r){return T(r).split(v)},A=function(r){return r.replace(/\n/g," ")},g=function(r,e){var t,n,o=e||/\s/g,a=!1,u=!1,s=[],c=[],l=r.split("");try{for(var d=i(l),f=d.next();!f.done;f=d.next()){var p=f.value,y=p.match(o);"'"!==p||u?'"'!==p||a?a||u||!y?s.push(p):s.length>0?(c.push(s.join("")),s=[]):e&&c.push(p):(s.push(p),u=!u):(s.push(p),a=!a)}}catch(r){t={error:r}}finally{try{f&&!f.done&&(n=d.return)&&n.call(d)}finally{if(t)throw t.error}}return s.length>0?c.push(s.join("")):e&&c.push(""),c},m=function(r){for(var e=!1,t=!1,n="",i="",o=function(){i+=A(n).replace(/\s+/gm," ")+v,n="",t=!1},a=0;a<r.length;++a){var u=r[a];"S"===u&&"O"===r[a+1]&&"A"===r[a+2]&&(t=!0,n=""),t?"("!==u||e?")"===u&&e?(o(),e=!1):u!==v||e?n+=u:o():e=!0:i+=u}return i},E=function(r){return r.replace(/\\"|"(?:\\"|[^"])*"|((^|[^\\])\;.*)/g,(function(r,e){return e?"":r}))},O=function(r){var e=E(r),t=m(e);return[t,K(t).filter((function(r){return""!==r.trim()}))]},N=function(r,e){return function(t,i){var o=t.tokens,a=function(r,e){var t={};for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&e.indexOf(n)<0&&(t[n]=r[n]);if(null!=r&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(r);i<n.length;i++)e.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(r,n[i])&&(t[n[i]]=r[n[i]])}return t}(t,["tokens"]),u=n({},a);return r.split(" ").forEach((function(r,e){u[r]=o[e]})),e?e(u,i):u}},L=function(r,e){var t=e.$ORIGIN;return r&&"@"!==r&&r!==t&&f(r)!==f(t)?!r.includes(t)&&r.endsWith(".")?r+t:r.includes(t)?r:r+"."+t:p(t)},S=function(){function r(r){var e=this;this.parser=r,this.index=0,this.lineContent="",this.lineParts=[],this.addMeta=function(r){e.parser.parsingMeta.set(e.index+1,n({lineContent:e.lineContent},r))}}return r.prototype.update=function(r,e){this.lineParts=g(r),this.index=e,this.lineContent=r},r}();r.DirectiveKind=void 0,(u=r.DirectiveKind||(r.DirectiveKind={})).Origin="$ORIGIN",u.TimeToLive="$TTL";var R,I,M,P=["TXT","NS","A","AAAA","CNAME","MX","PTR","SPF","SRV","CAA","NAPTR"],x=a(["SOA"],o(P)),b=a(a([],o(P)),[r.DirectiveKind.TimeToLive,r.DirectiveKind.Origin]);r.RecordMinLengthKind=void 0,(R=r.RecordMinLengthKind||(r.RecordMinLengthKind={}))[R.TXT=1]="TXT",R[R.CNAME=1]="CNAME",R[R.A=1]="A",R[R.AAAA=1]="AAAA",R[R.NS=1]="NS",R[R.PTR=1]="PTR",R[R.MX=2]="MX",R[R.SPF=1]="SPF",R[R.CAA=3]="CAA",R[R.SRV=4]="SRV",R[R.NAPTR=6]="NAPTR",R[R.SOA=7]="SOA",r.RecordMaxLengthKind=void 0,(I=r.RecordMaxLengthKind||(r.RecordMaxLengthKind={}))[I.SPF=1/0]="SPF",I[I.SOA=1/0]="SOA",I[I.TXT=1]="TXT",I[I.CNAME=1]="CNAME",I[I.A=1]="A",I[I.AAAA=1]="AAAA",I[I.NS=1]="NS",I[I.PTR=1]="PTR",I[I.MX=2]="MX",I[I.CAA=3]="CAA",I[I.SRV=4]="SRV",I[I.NAPTR=6]="NAPTR",r.ErrorKeyKind=void 0,(M=r.ErrorKeyKind||(r.ErrorKeyKind={})).Api="API",M.EmptyInput="EMPTY",M.NoRecords="NO_RECORDS_TO_CREATE",M.NoValidRecords="NO_VALID_RECORDS_TO_CREATE",M.BadDirective="BAD_DIRECTIVE",M.Duplicated="DUPLICATED",M.Unknown="UNKNOWN",M.Missing="",M.TtlInvalidFormat="TTL_INVALID_FORMAT",M.TtlOverflow="TTL_OVERFLOW",M.TtlMissingDefault="TTL_INVALID_FORMAT_MISSING_DEFAULT",M.WrongLength="WRONG_LENGTH",M.WrongNameArgsLength="EXTRA_NAME_TTL_ARGS";var D,_=[r.ErrorKeyKind.Api,r.ErrorKeyKind.EmptyInput,r.ErrorKeyKind.NoRecords,r.ErrorKeyKind.NoValidRecords,r.ErrorKeyKind.BadDirective,r.ErrorKeyKind.TtlInvalidFormat,r.ErrorKeyKind.TtlOverflow,r.ErrorKeyKind.TtlMissingDefault,r.ErrorKeyKind.WrongLength,r.ErrorKeyKind.WrongNameArgsLength,r.ErrorKeyKind.Duplicated,r.ErrorKeyKind.Unknown];r.WarningKeyKind=void 0,(D=r.WarningKeyKind||(r.WarningKeyKind={})).NsIgnored="SOA_IGNORED",D.SoaIgnored="NS_IGNORED";var w,k=2147483647;r.TimeLetterKind=void 0,(w=r.TimeLetterKind||(r.TimeLetterKind={})).Second="S",w.Hour="H",w.Day="D",w.Week="W",w.Month="M",w.Year="Y";var C,j=[r.TimeLetterKind.Day,r.TimeLetterKind.Hour,r.TimeLetterKind.Month,r.TimeLetterKind.Second,r.TimeLetterKind.Week,r.TimeLetterKind.Year];r.MultiplyByKind=void 0,(C=r.MultiplyByKind||(r.MultiplyByKind={}))[C.Second=1]="Second",C[C.Hour=3600]="Hour",C[C.Day=86400]="Day",C[C.Week=604800]="Week",C[C.Month=2419200]="Month",C[C.Year=29030400]="Year";var B=function(r){return[+y(/\d+/,r).join(""),y(/[a-z]+/i,r).join("").toUpperCase()]},V=function(e){var t,n,a=0;try{for(var u=i(e),s=u.next();!s.done;s=u.next()){var c=o(s.value,2),l=c[0];switch(c[1]){case r.TimeLetterKind.Second:a+=l*r.MultiplyByKind.Second;break;case r.TimeLetterKind.Hour:a+=l*r.MultiplyByKind.Hour;break;case r.TimeLetterKind.Day:a+=l*r.MultiplyByKind.Day;break;case r.TimeLetterKind.Week:a+=l*r.MultiplyByKind.Week;break;case r.TimeLetterKind.Month:a+=l*r.MultiplyByKind.Month;break;case r.TimeLetterKind.Year:a+=l*r.MultiplyByKind.Year}}}catch(r){t={error:r}}finally{try{s&&!s.done&&(n=u.return)&&n.call(u)}finally{if(t)throw t.error}}return a},W=function(e){if("number"==typeof e)return[e];if("string"==typeof e&&!d(e))return[,r.ErrorKeyKind.TtlInvalidFormat];var t=e,n=parseInt(t,10);if(!l(t)&&!Number.isNaN(n))return[n];if(Number.isNaN(n)||!h.test(t))return[,r.ErrorKeyKind.TtlMissingDefault];var i=y(h,t).map(B);if(i.some((function(r){var e=o(r,2)[1];return e.length>1||!j.includes(e)})))return[n,r.ErrorKeyKind.TtlInvalidFormat];var a=V(i);return a>=k?[a,r.ErrorKeyKind.TtlOverflow]:[a]},F=function(r){return!o(W(r),2)[1]},$=function(r,e,t,n){if(+t==+n&&t!==e||t>e)throw new s(r,e,t)},X=function(e,t,i,u,s){var c=t.lastIndexOf(i),l="";0!==c&&"IN"===t[c-1]||t.splice(c,0,"IN");var d=t.indexOf("IN");d!==t.lastIndexOf("IN")&&t.splice(d,1);var f=t.slice(t.lastIndexOf(i)+1),p=o(function(e,t,n){var i=t.length,o=r.RecordMinLengthKind[e],a=r.RecordMaxLengthKind[e];return[n.length-i-2,i,o,a]}(i,f,t)),y=p[0],h=p.slice(1);$.apply(void 0,a([i],o(h)));var v=function(){switch(y){case 1:var r=!1===/^\s+/.test(e);return{hasTtl:!r,hasName:r};case 2:var n=o(t,2),a=n[0],u=n[1],s=F(a),c=F(u),l=s||c;return s&&!c&&(t[0]=u,t[1]=a),{hasTtl:l,hasName:""!==t[0]};case 0:return{hasTtl:!1,hasName:!1};default:throw new Error(i)}}(),T=v.hasName,K=v.hasTtl,A={values:f,recordType:i,tokens:t};if(T)l=A.tokens[0];else{var g=u[i];s?A.tokens.unshift(s):Array.isArray(g)&&g.length>0?A.tokens.unshift(g[g.length-1].name||"@"):A.tokens.unshift("@")}K?A.tokens[1]=""+W(A.tokens[1])[0]:(A.tokens.unshift(A.tokens[0]),A.tokens[1]=void 0===u.$TTL?void 0:""+u.$TTL);var m=o(A.tokens,2),E=m[0],O=m[1],N=L(E,u);return n(n({},A),{name:N,ttl:"string"==typeof O?+O:void 0,previousName:l||s})},U=function(r,e){var t=r.values,n=e.zoneObj;return t.map((function(r){return"@"===r&&n.$ORIGIN?n.$ORIGIN:"\\@"===r?"@":r}))},G={NS:N("host",(function(r,e){return n(n({},r),{values:U(r,e)})})),A:N("ip",(function(r,e){return n(n({},r),{values:U(r,e)})})),AAAA:N("ip",(function(r,e){return n(n({},r),{values:U(r,e)})})),CNAME:N("alias",(function(r,e){return n(n({},r),{values:U(r,e)})})),MX:N("preference host",(function(r,e){return n(n({},r),{values:U(r,e)})})),TXT:N("txt"),PTR:N("host",(function(r,e){return n(n({},r),{values:U(r,e)})})),SRV:N("target port weight priority"),NAPTR:N("order preference flags services regexp replacement"),SPF:N("data",(function(r){return n(n({},r),{values:[r.values.join(" ").trim()]})})),CAA:N("flags tag data",(function(r){return n(n({},r),{data:r.data.replace(/^"(.+?)"$/,"$1")})})),SOA:N("minimum expire retry refresh serial rname mname")},Y=function(r){return P.find((function(e){return r.includes(e)}))},z=[{isSatisfied:function(r){return 0===r.lineParts.indexOf("$ORIGIN")},visit:function(e,t){var n=t.lineParts,i=t.addMeta;return e.zoneObj.$ORIGIN=n[1],i({instructionType:r.DirectiveKind.Origin,value:n[1]})}},{isSatisfied:function(r){return 0===r.lineParts.indexOf("$TTL")},visit:function(e,t){var n=e.zoneObj,i=t.addMeta,a=t.lineParts,u=o(W(a[1]),2),s=u[0],c=u[1];c?i({instructionType:r.DirectiveKind.TimeToLive,value:s,errorType:c,error:c}):(n.$TTL=s,i({instructionType:r.DirectiveKind.TimeToLive,value:s}))}},{isSatisfied:function(r){return r.lineParts.includes("SOA")},visit:function(r,e){var t=r.zoneObj,n=e.addMeta,i=e.lineParts;t.SOA=G.SOA({tokens:i,values:i.slice(i.indexOf("SOA")+1)}),n({instructionType:"SOA",value:t.SOA})}},{isSatisfied:function(r){var e=r.lineParts;return!!Y(e)},visit:function(e,t){var n,i,o=t.addMeta,a=t.lineParts,u=t.lineContent,c=e.zoneObj,l=e.stack,d=Y(a);Array.isArray(c[d])||(c[d]=[]);var f=c[d];try{var p=null!==(i=null===(n=l[l.length-1])||void 0===n?void 0:n.name)&&void 0!==i?i:"",y=X(u,a,d,c,p);l.push(y);var h=G[d](y,e);f.push(h),o({instructionType:d,value:h})}catch(e){o({instructionType:d,value:a.join(" "),error:e,errorType:e instanceof s?r.ErrorKeyKind.WrongLength:r.ErrorKeyKind.WrongNameArgsLength})}}},{isSatisfied:function(){return!0},visit:function(e,t){var n=t.addMeta,i=t.lineParts;n({instructionType:r.ErrorKeyKind.Unknown,errorType:r.ErrorKeyKind.BadDirective,value:i.join(" ")})}}],H=function(){function r(r){void 0===r&&(r=""),this.parsingMeta=new Map,this.stack=[],this.visitors=z,this.input={textInput:null,textInputFlattened:"",textInputSplitByLine:[]},this.zoneObj={$ORIGIN:r},this.input.$originName=r}return r.prototype.setText=function(r){var e=o(O(r),2),t=e[0],n=e[1];return this.input.textInput=r,this.input.textInputFlattened=t,this.input.textInputSplitByLine=n,this},r.prototype.parse=function(){var r,e,t,n=new S(this),a=function(r){return r.isSatisfied(n)};try{for(var u=i(this.input.textInputSplitByLine.entries()),s=u.next();!s.done;s=u.next()){var c=o(s.value,2),l=c[0],d=c[1];n.update(d,l),null===(t=this.visitors.find(a))||void 0===t||t.visit(this,n)}}catch(e){r={error:e}}finally{try{s&&!s.done&&(e=u.return)&&e.call(u)}finally{if(r)throw r.error}}return this},r}(),Z=function(e,t){if(""===e.input.textInput.replace(/\s+/,""))throw t.create({errorType:r.ErrorKeyKind.EmptyInput,instructionType:r.ErrorKeyKind.Unknown})},q=function(e,t){if(0===Object.keys(e.zoneObj).filter((function(e){return e!==r.DirectiveKind.Origin&&e!==r.DirectiveKind.TimeToLive})).length)throw t.create({errorType:r.ErrorKeyKind.NoRecords,instructionType:r.ErrorKeyKind.Unknown})},J=function(r,e){var t=e.entry.errorType;if(t&&_.includes(t))throw e.create(e.entry)},Q=function(e,t){if(t.entry.errorType===r.ErrorKeyKind.WrongLength){var n=t.entry.error,i=n.expected,o=n.type,a=n.actual;throw t.create(t.entry,{expected:i,actual:a,type:o})}},rr=function(){return{loopValidators:[Q,J],getErrorText:function(r){return r.errorType},preValidators:[Z],postValidators:[q]}},er=function(){function r(){this.parsingLineNumber=0,this.entry=void 0,this.getErrorText=void 0}return r.prototype.update=function(r,e){return this.parsingLineNumber=r,this.entry=e,this},r.prototype.create=function(r,e){var t=n(n({parsingLineNumber:this.parsingLineNumber},this.entry),r),i=this.getErrorText(t,e);return new c(i,t.instructionType,t.parsingLineNumber,t.lineContent,t.value)},r}(),tr=function(){function r(r){void 0===r&&(r={}),this.errors=[],this.options=rr(),this.errorBuilder=new er,this.mergeOptions(r)}return r.prototype.mergeOptions=function(r){return function(r,e){Object.keys(e).forEach((function(t){"getErrorText"===t?r[t]=e[t]:Array.isArray(r[t])&&(r[t]=a(a([],o(r[t])),o(e[t])))}))}(this.options,r),this.errorBuilder.getErrorText=this.options.getErrorText,this},r.prototype.validate=function(r){var e,t,n=this;this.options.preValidators.forEach((function(e){try{e(r,n.errorBuilder)}catch(r){n.errors.push(r)}}));try{for(var a=i(r.parsingMeta),u=a.next();!u.done;u=a.next()){var s=o(u.value,2),c=s[0],l=s[1];this.errorBuilder.update(c,l),this.options.loopValidators.forEach((function(e){try{e(r,n.errorBuilder)}catch(r){n.errors.push(r)}}))}}catch(r){e={error:r}}finally{try{u&&!u.done&&(t=a.return)&&t.call(a)}finally{if(e)throw e.error}}return this.errorBuilder.update(0,{}),this.options.postValidators.forEach((function(e){try{e(r,n.errorBuilder)}catch(r){n.errors.push(r)}})),this},r}();return r.ALL_RECORD_TYPES=x,r.ERROR_KEYS=_,r.ErrorBuilder=er,r.INSTRUCTION_TYPES=b,r.MAX_TTL_API_VALUE=k,r.Parser=H,r.ParserInvalidArgumentsError=s,r.ParserNode=S,r.ParsingError=c,r.SUPPORTED_RECORD_TYPES=P,r.UNIX_NEW_LINE_CHAR=v,r.VALID_LETTERS=j,r.Validator=tr,r.assertRecordLengths=$,r.createDefaultValidators=rr,r.flattenStartOfAuthority=m,r.getHasValidTtl=F,r.getNumberLetterPairSeconds=V,r.getRecordNameUsingOrigin=L,r.hasLetters=l,r.hasNumbers=d,r.match=y,r.matchDigitWithNumber=h,r.newLinesIntoSpaces=A,r.parseTtl=W,r.parsers=G,r.removeComments=E,r.splitArgs=g,r.splitByNewLine=K,r.transformInputText=O,r.trimDot=f,r.unifyNewLine=T,r.visitors=z,r.withDot=p,r.zipArrayToObj=N,Object.defineProperty(r,"__esModule",{value:!0}),r}({}); //# sourceMappingURL=index.iife.js.map