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.

102 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHasValidTtl = exports.parseTtl = exports.getNumberLetterPairSeconds = void 0; /** * @see https://github.com/date-fns/date-fns/pull/348 * @see https://github.com/date-fns/date-fns/pull/364 * @see https://github.com/date-fns/date-fns/issues/277 * @see https://github.com/date-fns/date-fns/issues/284 * @see https://www.npmjs.com/package/pomeranian-durations * @see https://github.com/webpapaya/zeitgeist.js * @see https://stackoverflow.com/questions/54103133/parse-time-1hr-30m-4s-as-time-ahead-in-javascript * * @see ISO_8601 * @note it accepts `1ssssss`, but not `1ymd` or `1yyyy` (in prod) * * @example valid * - '1w1d' * - '1w' * - '1m' * - '1d' * - '1s' * - '1m1w1d1h1000s' * - '1M1w1D1h1S' * - '10000' * * @example invalid * - '1yyy' * - '10y' (_too big_) * - '5y1m1w1d1h1s' (_too big_) */ const utils_generic_1 = require("../shared/utils_generic"); const constants_domain_specific_1 = require("../shared/constants_domain_specific"); const ttl_constants_1 = require("./ttl_constants"); /** @perf optimize */ const splitNumberAndLetter = (x) => [ +utils_generic_1.match(/\d+/, x).join(''), utils_generic_1.match(/[a-z]+/i, x) .join('') .toUpperCase(), ]; const getNumberLetterPairSeconds = (matches) => { let inSeconds = 0; for (const [digits, letter] of matches) { switch (letter) { case ttl_constants_1.TimeLetterKind.Second: inSeconds += digits * ttl_constants_1.MultiplyByKind.Second; break; case ttl_constants_1.TimeLetterKind.Hour: inSeconds += digits * ttl_constants_1.MultiplyByKind.Hour; break; case ttl_constants_1.TimeLetterKind.Day: inSeconds += digits * ttl_constants_1.MultiplyByKind.Day; break; case ttl_constants_1.TimeLetterKind.Week: inSeconds += digits * ttl_constants_1.MultiplyByKind.Week; break; case ttl_constants_1.TimeLetterKind.Month: inSeconds += digits * ttl_constants_1.MultiplyByKind.Month; break; case ttl_constants_1.TimeLetterKind.Year: inSeconds += digits * ttl_constants_1.MultiplyByKind.Year; break; } } return inSeconds; }; exports.getNumberLetterPairSeconds = getNumberLetterPairSeconds; /** @perf could keep TTL always a string */ const parseTtl = (valueInput) => { if (typeof valueInput === 'number') { return [valueInput]; } else if (typeof valueInput === 'string' && !utils_generic_1.hasNumbers(valueInput)) { return [, constants_domain_specific_1.ErrorKeyKind.TtlInvalidFormat]; } const value = valueInput; const ttl = parseInt(value, 10); if (!utils_generic_1.hasLetters(value) && !Number.isNaN(ttl)) { return [ttl]; } else if (Number.isNaN(ttl) || !utils_generic_1.matchDigitWithNumber.test(value)) { return [, constants_domain_specific_1.ErrorKeyKind.TtlMissingDefault]; } const matches = utils_generic_1.match(utils_generic_1.matchDigitWithNumber, value).map(splitNumberAndLetter); if (matches.some(([, letter]) => letter.length > 1 || !ttl_constants_1.VALID_LETTERS.includes(letter))) { return [ttl, constants_domain_specific_1.ErrorKeyKind.TtlInvalidFormat]; } const seconds = exports.getNumberLetterPairSeconds(matches); if (seconds >= ttl_constants_1.MAX_TTL_API_VALUE) { return [seconds, constants_domain_specific_1.ErrorKeyKind.TtlOverflow]; } else { return [seconds]; } }; exports.parseTtl = parseTtl; const getHasValidTtl = (value) => { const [, error] = exports.parseTtl(value); return !error; }; exports.getHasValidTtl = getHasValidTtl; //# sourceMappingURL=parse_ttl.js.map