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.

3 lines (2 loc) 15.2 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=function(e,t){return(r=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])})(e,t)};function e(e,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=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var t=function(){return(t=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 n(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 i(r,e){var t="function"==typeof Symbol&&r[Symbol.iterator];if(!t)return r;var n,i,o=t.call(r),s=[];try{for(;(void 0===e||e-- >0)&&!(n=o.next()).done;)s.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 s}function o(r,e){for(var t=0,n=e.length,i=r.length;t<n;t++,i++)r[i]=e[t];return r}var s,a=function(r){function t(e,t,n){var i=r.call(this,"")||this;return i.type=e,i.actual=t,i.expected=n,i}return e(t,r),t}(TypeError),u=function(r){function t(e,t,n,i,o){void 0===n&&(n=1),void 0===i&&(i="");var s=r.call(this,e)||this;return s.parsingType=t,s.parsingLineNumber=n,s.parsingLineContent=i,s.parsedValue=o,s}return e(t,r),t}(Error),p=function(r){return/[a-z]/i.test(r)},c=function(r){return/\d/.test(r)},l=function(r){return r.replace(/\.$/,"")},d=function(r){return l(r)+"."},f=function(r,e){return"string"==typeof e&&e.match(r)||[]},y=/(\d+)([a-z]+)/gim,h=function(r){return r.replace(/\\n/,"\n").replace(/\r\n?/g,"\n")},x=function(r){return h(r).split("\n")},v=function(r){return r.replace(/\n/g," ")},T=function(r,e){var t,i,o=e||/\s/g,s=!1,a=!1,u=[],p=[],c=r.split("");try{for(var l=n(c),d=l.next();!d.done;d=l.next()){var f=d.value,y=f.match(o);"'"!==f||a?'"'!==f||s?s||a||!y?u.push(f):u.length>0?(p.push(u.join("")),u=[]):e&&p.push(f):(u.push(f),a=!a):(u.push(f),s=!s)}}catch(r){t={error:r}}finally{try{d&&!d.done&&(i=l.return)&&i.call(l)}finally{if(t)throw t.error}}return u.length>0?p.push(u.join("")):e&&p.push(""),p},K=function(r){for(var e=!1,t=!1,n="",i="",o=function(){i+=v(n).replace(/\s+/gm," ")+"\n",n="",t=!1},s=0;s<r.length;++s){var a=r[s];"S"===a&&"O"===r[s+1]&&"A"===r[s+2]&&(t=!0,n=""),t?"("!==a||e?")"===a&&e?(o(),e=!1):"\n"!==a||e?n+=a:o():e=!0:i+=a}return i},A=function(r){return r.replace(/\\"|"(?:\\"|[^"])*"|((^|[^\\])\;.*)/g,(function(r,e){return e?"":r}))},g=function(r){var e=A(r),t=K(e);return[t,x(t).filter((function(r){return""!==r.trim()}))]},m=function(r,e){return function(n,i){var o=n.tokens,s=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}(n,["tokens"]),a=t({},s);return r.split(" ").forEach((function(r,e){a[r]=o[e]})),e?e(a,i):a}},E=function(r,e){var t=e.$ORIGIN;return r&&"@"!==r&&r!==t&&l(r)!==l(t)?!r.includes(t)&&r.endsWith(".")?r+t:r.includes(t)?r:r+"."+t:d(t)},O=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,t({lineContent:e.lineContent},r))}}return r.prototype.update=function(r,e){this.lineParts=T(r),this.index=e,this.lineContent=r},r}();exports.DirectiveKind=void 0,(s=exports.DirectiveKind||(exports.DirectiveKind={})).Origin="$ORIGIN",s.TimeToLive="$TTL";var N,L,S,R=["TXT","NS","A","AAAA","CNAME","MX","PTR","SPF","SRV","CAA","NAPTR"],I=o(["SOA"],i(R)),M=o(o([],i(R)),[exports.DirectiveKind.TimeToLive,exports.DirectiveKind.Origin]);exports.RecordMinLengthKind=void 0,(N=exports.RecordMinLengthKind||(exports.RecordMinLengthKind={}))[N.TXT=1]="TXT",N[N.CNAME=1]="CNAME",N[N.A=1]="A",N[N.AAAA=1]="AAAA",N[N.NS=1]="NS",N[N.PTR=1]="PTR",N[N.MX=2]="MX",N[N.SPF=1]="SPF",N[N.CAA=3]="CAA",N[N.SRV=4]="SRV",N[N.NAPTR=6]="NAPTR",N[N.SOA=7]="SOA",exports.RecordMaxLengthKind=void 0,(L=exports.RecordMaxLengthKind||(exports.RecordMaxLengthKind={}))[L.SPF=1/0]="SPF",L[L.SOA=1/0]="SOA",L[L.TXT=1]="TXT",L[L.CNAME=1]="CNAME",L[L.A=1]="A",L[L.AAAA=1]="AAAA",L[L.NS=1]="NS",L[L.PTR=1]="PTR",L[L.MX=2]="MX",L[L.CAA=3]="CAA",L[L.SRV=4]="SRV",L[L.NAPTR=6]="NAPTR",exports.ErrorKeyKind=void 0,(S=exports.ErrorKeyKind||(exports.ErrorKeyKind={})).Api="API",S.EmptyInput="EMPTY",S.NoRecords="NO_RECORDS_TO_CREATE",S.NoValidRecords="NO_VALID_RECORDS_TO_CREATE",S.BadDirective="BAD_DIRECTIVE",S.Duplicated="DUPLICATED",S.Unknown="UNKNOWN",S.Missing="",S.TtlInvalidFormat="TTL_INVALID_FORMAT",S.TtlOverflow="TTL_OVERFLOW",S.TtlMissingDefault="TTL_INVALID_FORMAT_MISSING_DEFAULT",S.WrongLength="WRONG_LENGTH",S.WrongNameArgsLength="EXTRA_NAME_TTL_ARGS";var P,b=[exports.ErrorKeyKind.Api,exports.ErrorKeyKind.EmptyInput,exports.ErrorKeyKind.NoRecords,exports.ErrorKeyKind.NoValidRecords,exports.ErrorKeyKind.BadDirective,exports.ErrorKeyKind.TtlInvalidFormat,exports.ErrorKeyKind.TtlOverflow,exports.ErrorKeyKind.TtlMissingDefault,exports.ErrorKeyKind.WrongLength,exports.ErrorKeyKind.WrongNameArgsLength,exports.ErrorKeyKind.Duplicated,exports.ErrorKeyKind.Unknown];exports.WarningKeyKind=void 0,(P=exports.WarningKeyKind||(exports.WarningKeyKind={})).NsIgnored="SOA_IGNORED",P.SoaIgnored="NS_IGNORED";var D;exports.TimeLetterKind=void 0,(D=exports.TimeLetterKind||(exports.TimeLetterKind={})).Second="S",D.Hour="H",D.Day="D",D.Week="W",D.Month="M",D.Year="Y";var _,w=[exports.TimeLetterKind.Day,exports.TimeLetterKind.Hour,exports.TimeLetterKind.Month,exports.TimeLetterKind.Second,exports.TimeLetterKind.Week,exports.TimeLetterKind.Year];exports.MultiplyByKind=void 0,(_=exports.MultiplyByKind||(exports.MultiplyByKind={}))[_.Second=1]="Second",_[_.Hour=3600]="Hour",_[_.Day=86400]="Day",_[_.Week=604800]="Week",_[_.Month=2419200]="Month",_[_.Year=29030400]="Year";var k=function(r){return[+f(/\d+/,r).join(""),f(/[a-z]+/i,r).join("").toUpperCase()]},C=function(r){var e,t,o=0;try{for(var s=n(r),a=s.next();!a.done;a=s.next()){var u=i(a.value,2),p=u[0];switch(u[1]){case exports.TimeLetterKind.Second:o+=p*exports.MultiplyByKind.Second;break;case exports.TimeLetterKind.Hour:o+=p*exports.MultiplyByKind.Hour;break;case exports.TimeLetterKind.Day:o+=p*exports.MultiplyByKind.Day;break;case exports.TimeLetterKind.Week:o+=p*exports.MultiplyByKind.Week;break;case exports.TimeLetterKind.Month:o+=p*exports.MultiplyByKind.Month;break;case exports.TimeLetterKind.Year:o+=p*exports.MultiplyByKind.Year}}}catch(r){e={error:r}}finally{try{a&&!a.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}return o},j=function(r){if("number"==typeof r)return[r];if("string"==typeof r&&!c(r))return[,exports.ErrorKeyKind.TtlInvalidFormat];var e=r,t=parseInt(e,10);if(!p(e)&&!Number.isNaN(t))return[t];if(Number.isNaN(t)||!y.test(e))return[,exports.ErrorKeyKind.TtlMissingDefault];var n=f(y,e).map(k);if(n.some((function(r){var e=i(r,2)[1];return e.length>1||!w.includes(e)})))return[t,exports.ErrorKeyKind.TtlInvalidFormat];var o=C(n);return o>=2147483647?[o,exports.ErrorKeyKind.TtlOverflow]:[o]},B=function(r){return!i(j(r),2)[1]},V=function(r,e,t,n){if(+t==+n&&t!==e||t>e)throw new a(r,e,t)},W=function(r,e,n,s,a){var u=e.lastIndexOf(n),p="";0!==u&&"IN"===e[u-1]||e.splice(u,0,"IN");var c=e.indexOf("IN");c!==e.lastIndexOf("IN")&&e.splice(c,1);var l=e.slice(e.lastIndexOf(n)+1),d=i(function(r,e,t){var n=e.length,i=exports.RecordMinLengthKind[r],o=exports.RecordMaxLengthKind[r];return[t.length-n-2,n,i,o]}(n,l,e)),f=d[0],y=d.slice(1);V.apply(void 0,o([n],i(y)));var h=function(){switch(f){case 1:var t=!1===/^\s+/.test(r);return{hasTtl:!t,hasName:t};case 2:var o=i(e,2),s=o[0],a=o[1],u=B(s),p=B(a),c=u||p;return u&&!p&&(e[0]=a,e[1]=s),{hasTtl:c,hasName:""!==e[0]};case 0:return{hasTtl:!1,hasName:!1};default:throw new Error(n)}}(),x=h.hasName,v=h.hasTtl,T={values:l,recordType:n,tokens:e};if(x)p=T.tokens[0];else{var K=s[n];a?T.tokens.unshift(a):Array.isArray(K)&&K.length>0?T.tokens.unshift(K[K.length-1].name||"@"):T.tokens.unshift("@")}v?T.tokens[1]=""+j(T.tokens[1])[0]:(T.tokens.unshift(T.tokens[0]),T.tokens[1]=void 0===s.$TTL?void 0:""+s.$TTL);var A=i(T.tokens,2),g=A[0],m=A[1],O=E(g,s);return t(t({},T),{name:O,ttl:"string"==typeof m?+m:void 0,previousName:p||a})},F=function(r,e){var t=r.values,n=e.zoneObj;return t.map((function(r){return"@"===r&&n.$ORIGIN?n.$ORIGIN:"\\@"===r?"@":r}))},$={NS:m("host",(function(r,e){return t(t({},r),{values:F(r,e)})})),A:m("ip",(function(r,e){return t(t({},r),{values:F(r,e)})})),AAAA:m("ip",(function(r,e){return t(t({},r),{values:F(r,e)})})),CNAME:m("alias",(function(r,e){return t(t({},r),{values:F(r,e)})})),MX:m("preference host",(function(r,e){return t(t({},r),{values:F(r,e)})})),TXT:m("txt"),PTR:m("host",(function(r,e){return t(t({},r),{values:F(r,e)})})),SRV:m("target port weight priority"),NAPTR:m("order preference flags services regexp replacement"),SPF:m("data",(function(r){return t(t({},r),{values:[r.values.join(" ").trim()]})})),CAA:m("flags tag data",(function(r){return t(t({},r),{data:r.data.replace(/^"(.+?)"$/,"$1")})})),SOA:m("minimum expire retry refresh serial rname mname")},X=function(r){return R.find((function(e){return r.includes(e)}))},U=[{isSatisfied:function(r){return 0===r.lineParts.indexOf("$ORIGIN")},visit:function(r,e){var t=e.lineParts,n=e.addMeta;return r.zoneObj.$ORIGIN=t[1],n({instructionType:exports.DirectiveKind.Origin,value:t[1]})}},{isSatisfied:function(r){return 0===r.lineParts.indexOf("$TTL")},visit:function(r,e){var t=r.zoneObj,n=e.addMeta,o=e.lineParts,s=i(j(o[1]),2),a=s[0],u=s[1];u?n({instructionType:exports.DirectiveKind.TimeToLive,value:a,errorType:u,error:u}):(t.$TTL=a,n({instructionType:exports.DirectiveKind.TimeToLive,value:a}))}},{isSatisfied:function(r){return r.lineParts.includes("SOA")},visit:function(r,e){var t=r.zoneObj,n=e.addMeta,i=e.lineParts;t.SOA=$.SOA({tokens:i,values:i.slice(i.indexOf("SOA")+1)}),n({instructionType:"SOA",value:t.SOA})}},{isSatisfied:function(r){var e=r.lineParts;return!!X(e)},visit:function(r,e){var t,n,i=e.addMeta,o=e.lineParts,s=e.lineContent,u=r.zoneObj,p=r.stack,c=X(o);Array.isArray(u[c])||(u[c]=[]);var l=u[c];try{var d=null!==(n=null===(t=p[p.length-1])||void 0===t?void 0:t.name)&&void 0!==n?n:"",f=W(s,o,c,u,d);p.push(f);var y=$[c](f,r);l.push(y),i({instructionType:c,value:y})}catch(r){i({instructionType:c,value:o.join(" "),error:r,errorType:r instanceof a?exports.ErrorKeyKind.WrongLength:exports.ErrorKeyKind.WrongNameArgsLength})}}},{isSatisfied:function(){return!0},visit:function(r,e){var t=e.addMeta,n=e.lineParts;t({instructionType:exports.ErrorKeyKind.Unknown,errorType:exports.ErrorKeyKind.BadDirective,value:n.join(" ")})}}],G=function(){function r(r){void 0===r&&(r=""),this.parsingMeta=new Map,this.stack=[],this.visitors=U,this.input={textInput:null,textInputFlattened:"",textInputSplitByLine:[]},this.zoneObj={$ORIGIN:r},this.input.$originName=r}return r.prototype.setText=function(r){var e=i(g(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,o=new O(this),s=function(r){return r.isSatisfied(o)};try{for(var a=n(this.input.textInputSplitByLine.entries()),u=a.next();!u.done;u=a.next()){var p=i(u.value,2),c=p[0],l=p[1];o.update(l,c),null===(t=this.visitors.find(s))||void 0===t||t.visit(this,o)}}catch(e){r={error:e}}finally{try{u&&!u.done&&(e=a.return)&&e.call(a)}finally{if(r)throw r.error}}return this},r}(),Y=function(r,e){if(""===r.input.textInput.replace(/\s+/,""))throw e.create({errorType:exports.ErrorKeyKind.EmptyInput,instructionType:exports.ErrorKeyKind.Unknown})},z=function(r,e){if(0===Object.keys(r.zoneObj).filter((function(r){return r!==exports.DirectiveKind.Origin&&r!==exports.DirectiveKind.TimeToLive})).length)throw e.create({errorType:exports.ErrorKeyKind.NoRecords,instructionType:exports.ErrorKeyKind.Unknown})},H=function(r,e){var t=e.entry.errorType;if(t&&b.includes(t))throw e.create(e.entry)},q=function(r,e){if(e.entry.errorType===exports.ErrorKeyKind.WrongLength){var t=e.entry.error,n=t.expected,i=t.type,o=t.actual;throw e.create(e.entry,{expected:n,actual:o,type:i})}},J=function(){return{loopValidators:[q,H],getErrorText:function(r){return r.errorType},preValidators:[Y],postValidators:[z]}},Q=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 n=t(t({parsingLineNumber:this.parsingLineNumber},this.entry),r),i=this.getErrorText(n,e);return new u(i,n.instructionType,n.parsingLineNumber,n.lineContent,n.value)},r}(),Z=function(){function r(r){void 0===r&&(r={}),this.errors=[],this.options=J(),this.errorBuilder=new Q,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]=o(o([],i(r[t])),i(e[t])))}))}(this.options,r),this.errorBuilder.getErrorText=this.options.getErrorText,this},r.prototype.validate=function(r){var e,t,o=this;this.options.preValidators.forEach((function(e){try{e(r,o.errorBuilder)}catch(r){o.errors.push(r)}}));try{for(var s=n(r.parsingMeta),a=s.next();!a.done;a=s.next()){var u=i(a.value,2),p=u[0],c=u[1];this.errorBuilder.update(p,c),this.options.loopValidators.forEach((function(e){try{e(r,o.errorBuilder)}catch(r){o.errors.push(r)}}))}}catch(r){e={error:r}}finally{try{a&&!a.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}return this.errorBuilder.update(0,{}),this.options.postValidators.forEach((function(e){try{e(r,o.errorBuilder)}catch(r){o.errors.push(r)}})),this},r}();exports.ALL_RECORD_TYPES=I,exports.ERROR_KEYS=b,exports.ErrorBuilder=Q,exports.INSTRUCTION_TYPES=M,exports.MAX_TTL_API_VALUE=2147483647,exports.Parser=G,exports.ParserInvalidArgumentsError=a,exports.ParserNode=O,exports.ParsingError=u,exports.SUPPORTED_RECORD_TYPES=R,exports.UNIX_NEW_LINE_CHAR="\n",exports.VALID_LETTERS=w,exports.Validator=Z,exports.assertRecordLengths=V,exports.createDefaultValidators=J,exports.flattenStartOfAuthority=K,exports.getHasValidTtl=B,exports.getNumberLetterPairSeconds=C,exports.getRecordNameUsingOrigin=E,exports.hasLetters=p,exports.hasNumbers=c,exports.match=f,exports.matchDigitWithNumber=y,exports.newLinesIntoSpaces=v,exports.parseTtl=j,exports.parsers=$,exports.removeComments=A,exports.splitArgs=T,exports.splitByNewLine=x,exports.transformInputText=g,exports.trimDot=l,exports.unifyNewLine=h,exports.visitors=U,exports.withDot=d,exports.zipArrayToObj=m; //# sourceMappingURL=index.cjs.js.map