@unstoppabledomains/resolution
Version:
Domain Resolution for blockchain domains
137 lines (136 loc) • 5.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var dnsRecordsError_1 = __importStar(require("../errors/dnsRecordsError"));
var publicTypes_1 = require("../types/publicTypes");
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.".concat(type, ".ttl")]);
var dnsInRecord = this.getJsonArray(cryptoRecords, "dns.".concat(type));
if (dnsInRecord) {
dnsInRecord.push(data);
cryptoRecords["dns.".concat(type)] = JSON.stringify(dnsInRecord);
}
else {
cryptoRecords["dns.".concat(type)] = JSON.stringify([data]);
cryptoRecords["dns.".concat(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.".concat(type)];
if (!jsonValueString) {
return [];
}
var typeData = this.protectFromCorruptRecord(jsonValueString, type);
if (!this.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.".concat(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.prototype.isStringArray = function (value) {
if (value instanceof Array) {
return value.every(function (item) { return typeof item === 'string'; });
}
return false;
};
DnsUtils.DefaultTtl = 300; // 5 minutes
return DnsUtils;
}());
exports.default = DnsUtils;