UNPKG

dns-packet-typescript

Version:

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

47 lines 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.encodingLength = exports.decode = exports.encode = void 0; var roption = require("./roption"); var utils_1 = require("./utils"); function encode(options, buf, offset) { if (offset === void 0) { offset = 0; } if (!buf) { buf = Buffer.allocUnsafe(encodingLength(options)); } if (!offset) { offset = 0; } var oldOffset = offset; var rdlen = utils_1.encodingLengthList(options, roption); buf.writeUInt16BE(rdlen, offset); offset = utils_1.encodeList(options, roption, buf, offset + 2); encode.bytes = offset - oldOffset; return buf; } exports.encode = encode; encode.bytes = 0; function decode(buf, offset) { if (offset === void 0) { offset = 0; } if (!offset) { offset = 0; } var oldOffset = offset; var options = []; var rdlen = buf.readUInt16BE(offset); offset += 2; var o = 0; while (rdlen > 0) { options[o++] = roption.decode(buf, offset); offset += roption.decode.bytes; rdlen -= roption.decode.bytes; } decode.bytes = offset - oldOffset; return options; } exports.decode = decode; decode.bytes = 0; function encodingLength(options) { return 2 + utils_1.encodingLengthList(options || [], roption); } exports.encodingLength = encodingLength; //# sourceMappingURL=ropt.js.map