UNPKG

@authereum/resolution

Version:
112 lines (111 loc) 4.41 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var dnsRecordsError_1 = __importStar(require("./errors/dnsRecordsError")); var publicTypes_1 = require("./publicTypes"); var utils_1 = require("./utils"); var DnsUtils = /** @class */ (function () { function DnsUtils() { } DnsUtils.prototype.toList = function (record) { var _a; var _this = this; var dnsTypes = this.getAllDnsTypes(record); return (_a = []).concat.apply(_a, dnsTypes.map(function (type) { return _this.constructDnsRecords(record, type); })); }; DnsUtils.prototype.toCrypto = function (records) { var cryptoRecords = {}; for (var _i = 0, records_1 = records; _i < records_1.length; _i++) { var record = records_1[_i]; var type = record.type, TTL = record.TTL, data = record.data; var ttlInRecord = this.getJsonNumber(cryptoRecords["dns." + type + ".ttl"]); var dnsInRecord = this.getJsonArray(cryptoRecords, "dns." + type); if (dnsInRecord) { dnsInRecord.push(data); cryptoRecords["dns." + type] = JSON.stringify(dnsInRecord); } else { cryptoRecords["dns." + type] = JSON.stringify([data]); cryptoRecords["dns." + type + ".ttl"] = TTL.toString(10); } if (!!ttlInRecord && ttlInRecord !== TTL) { throw new dnsRecordsError_1.default(dnsRecordsError_1.DnsRecordsErrorCode.InconsistentTtl, { recordType: type }); } } return cryptoRecords; }; DnsUtils.prototype.protectFromCorruptRecord = function (rawRecord, type) { try { return rawRecord ? JSON.parse(rawRecord) : undefined; } catch (err) { if (err instanceof SyntaxError) { throw new dnsRecordsError_1.default(dnsRecordsError_1.DnsRecordsErrorCode.DnsRecordCorrupted, { recordType: type }); } throw err; } }; DnsUtils.prototype.getJsonArray = function (cryptoRecrods, key) { var rawRecord = cryptoRecrods[key]; var type = key.split('.')[1]; return this.protectFromCorruptRecord(rawRecord, type); }; DnsUtils.prototype.getJsonNumber = function (rawRecord) { return rawRecord ? parseInt(rawRecord, 10) : undefined; }; DnsUtils.prototype.getAllDnsTypes = function (records) { var keys = new Set(); Object.keys(records).forEach(function (key) { var chunks = key.split('.'); var type = chunks[1] && chunks[1] !== 'ttl'; if (type) { keys.add(publicTypes_1.DnsRecordType[chunks[1]]); } }); return Array.from(keys); }; DnsUtils.prototype.constructDnsRecords = function (cryptoData, type) { var TTL = this.parseTtl(cryptoData, type); var jsonValueString = cryptoData["dns." + type]; if (!jsonValueString) { return []; } var typeData = this.protectFromCorruptRecord(jsonValueString, type); if (!utils_1.isStringArray(typeData)) { return []; } return typeData.map(function (data) { return ({ TTL: TTL, data: data, type: type }); }); }; DnsUtils.prototype.parseTtl = function (data, type) { var defaultTtl = data['dns.ttl']; var recordTtl = data["dns." + type + ".ttl"]; if (recordTtl) { var parsedInt = this.parseIfNumber(recordTtl); if (parsedInt) { return parsedInt; } } if (defaultTtl) { var parsedInt = this.parseIfNumber(defaultTtl); if (parsedInt) { return parsedInt; } } return DnsUtils.DefaultTtl; }; DnsUtils.prototype.parseIfNumber = function (str) { var parsedInt = parseInt(str, 10); if (!isNaN(parsedInt)) { return parsedInt; } }; DnsUtils.DefaultTtl = 300; // 5 minutes return DnsUtils; }()); exports.default = DnsUtils;