dns-txt.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DnsTxt = void 0;
  4. class DnsTxt {
  5. constructor(opts = {}) {
  6. this.binary = opts ? opts.binary : false;
  7. }
  8. encode(data = {}) {
  9. return Object.entries(data)
  10. .map(([key, value]) => {
  11. let item = `${key}=${value}`;
  12. return Buffer.from(item);
  13. });
  14. }
  15. decode(buffer) {
  16. var data = {};
  17. try {
  18. let format = buffer.toString();
  19. let parts = format.split(/=(.+)/);
  20. let key = parts[0];
  21. let value = parts[1];
  22. data[key] = value;
  23. }
  24. catch (_) { }
  25. return data;
  26. }
  27. decodeAll(buffer) {
  28. return buffer
  29. .filter(i => i.length > 1)
  30. .map(i => this.decode(i))
  31. .reduce((prev, curr) => {
  32. var obj = prev;
  33. let [key] = Object.keys(curr);
  34. let [value] = Object.values(curr);
  35. obj[key] = value;
  36. return obj;
  37. }, {});
  38. }
  39. }
  40. exports.DnsTxt = DnsTxt;
  41. exports.default = DnsTxt;
  42. //# sourceMappingURL=dns-txt.js.map