UNPKG

dns-packet-typescript

Version:

An abstract-encoding compliant module for encoding / decoding DNS packets

106 lines 4.43 kB
'use strict'; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.streamDecode = exports.streamEncode = exports.encodingLength = exports.decode = exports.encode = exports.DNSSEC_OK = exports.CHECKING_DISABLED = exports.AUTHENTIC_DATA = exports.RECURSION_AVAILABLE = exports.RECURSION_DESIRED = exports.TRUNCATED_RESPONSE = exports.AUTHORITATIVE_ANSWER = exports.NOT_FLUSH_MASK = exports.FLUSH_MASK = exports.string = exports.name = exports.header = exports.question = exports.answer = void 0; var header = require("./header"); exports.header = header; var name = require("./name"); exports.name = name; var string = require("./string"); exports.string = string; var answer = require("./answer"); exports.answer = answer; var question = require("./question"); exports.question = question; var utils_1 = require("./utils"); __exportStar(require("./renc"), exports); exports.FLUSH_MASK = 1 << 15; exports.NOT_FLUSH_MASK = ~exports.FLUSH_MASK; exports.AUTHORITATIVE_ANSWER = 1 << 10; exports.TRUNCATED_RESPONSE = 1 << 9; exports.RECURSION_DESIRED = 1 << 8; exports.RECURSION_AVAILABLE = 1 << 7; exports.AUTHENTIC_DATA = 1 << 5; exports.CHECKING_DISABLED = 1 << 4; exports.DNSSEC_OK = 1 << 15; function encode(result, buf, offset) { if (offset === void 0) { offset = 0; } if (!buf) buf = Buffer.allocUnsafe(encodingLength(result)); if (!offset) offset = 0; var oldOffset = offset; if (!result.questions) result.questions = []; if (!result.answers) result.answers = []; if (!result.authorities) result.authorities = []; if (!result.additionals) result.additionals = []; header.encode(result, buf, offset); offset += header.encode.bytes; offset = utils_1.encodeList(result.questions, question, buf, offset); offset = utils_1.encodeList(result.answers, answer, buf, offset); offset = utils_1.encodeList(result.authorities, answer, buf, offset); offset = utils_1.encodeList(result.additionals, answer, buf, offset); encode.bytes = offset - oldOffset; return buf; } exports.encode = encode; encode.bytes = 0; function decode(buf, offset) { if (offset === void 0) { offset = 0; } var oldOffset = offset; var result = header.decode(buf, offset); offset += header.decode.bytes; offset = utils_1.decodeList(result.questions, question, buf, offset); offset = utils_1.decodeList(result.answers, answer, buf, offset); offset = utils_1.decodeList(result.authorities, answer, buf, offset); offset = utils_1.decodeList(result.additionals, answer, buf, offset); decode.bytes = offset - oldOffset; return result; } exports.decode = decode; decode.bytes = 0; function encodingLength(result) { var _a, _b, _c, _d; return header.encodingLength() + utils_1.encodingLengthList((_a = result.questions) !== null && _a !== void 0 ? _a : [], question) + utils_1.encodingLengthList((_b = result.answers) !== null && _b !== void 0 ? _b : [], answer) + utils_1.encodingLengthList((_c = result.authorities) !== null && _c !== void 0 ? _c : [], answer) + utils_1.encodingLengthList((_d = result.additionals) !== null && _d !== void 0 ? _d : [], answer); } exports.encodingLength = encodingLength; function streamEncode(result) { var buf = encode(result); var sbuf = Buffer.allocUnsafe(2); sbuf.writeUInt16BE(buf.byteLength); var combine = Buffer.concat([sbuf, buf]); streamEncode.bytes = combine.byteLength; return combine; } exports.streamEncode = streamEncode; streamEncode.bytes = 0; function streamDecode(sbuf) { var len = sbuf.readUInt16BE(0); if (sbuf.byteLength < len + 2) { // not enough data return null; } var result = decode(sbuf.slice(2)); streamDecode.bytes = decode.bytes; return result; } exports.streamDecode = streamDecode; streamDecode.bytes = 0; //# sourceMappingURL=index.js.map