{"_id":"@dnsquery/dns-packet","_rev":"4577494","name":"@dnsquery/dns-packet","description":"An abstract-encoding compliant module for encoding / decoding DNS packets","dist-tags":{"latest":"6.1.1"},"maintainers":[{"name":"leichtgewicht","email":"martin.heidegger@gmail.com"}],"time":{"modified":"2026-04-10T15:47:43.000Z","created":"2022-12-02T17:25:14.901Z","6.1.1":"2022-12-02T17:48:42.605Z","6.1.0":"2022-12-02T17:25:14.901Z"},"users":{},"author":{"name":"Mathias Buus"},"repository":{"type":"git","url":"git+https://github.com/dnsquery/dns-packet.git"},"versions":{"6.1.1":{"name":"@dnsquery/dns-packet","version":"6.1.1","description":"An abstract-encoding compliant module for encoding / decoding DNS packets","author":{"name":"Mathias Buus"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/dnsquery/dns-packet.git"},"homepage":"https://github.com/dnsquery/dns-packet","main":"./index.js","module":"./index.mjs","types":"./types/index.d.ts","exports":{".":{"types":"./types/index.d.ts","import":"./index.mjs","require":"./index.js"},"./buffer_utils.js":{"types":"./buffer_utils.mjs","import":"./buffer_utils.mjs","require":"./buffer_utils.js"},"./classes.js":{"types":"./classes.d.ts","import":"./classes.mjs","require":"./classes.js"},"./opcodes.js":{"types":"./opcodes.d.ts","import":"./opcodes.mjs","require":"./opcodes.js"},"./optioncodes.js":{"types":"./optioncodes.d.ts","import":"./optioncodes.mjs","require":"./optioncodes.js"},"./rcodes.js":{"types":"./rcodes.d.ts","import":"./rcodes.mjs","require":"./rcodes.js"},"./types.js":{"types":"./types.d.ts","import":"./types.mjs","require":"./types.js"}},"engines":{"node":">=6"},"scripts":{"clean":"rm -rf coverage","lint":"standard && dtslint types --localTs node_modules/typescript/lib","test":"npm run lint && npm run unit","unit":"tape test.mjs","coverage":"c8 -r html npm test","prepare":"npx @leichtgewicht/esm2umd"},"dependencies":{"@leichtgewicht/ip-codec":"^2.0.4","utf8-codec":"^1.0.0"},"devDependencies":{"@definitelytyped/dtslint":"^0.0.119","@leichtgewicht/esm2umd":"^0.4.0","c8":"^7.12.0","standard":"^17.0.0","tape":"^5.6.1","typescript":"^4.9.3"},"keywords":["dns","packet","encodings","encoding","encoder","abstract-encoding"],"gitHead":"0c2b70267dfd46a05915481286a6b5d76b58d741","bugs":{"url":"https://github.com/dnsquery/dns-packet/issues"},"_id":"@dnsquery/dns-packet@6.1.1","_nodeVersion":"19.0.0","_npmVersion":"8.19.2","dist":{"shasum":"3abf2c7ebb6dece40a75f429e24f9de23671058a","size":42983,"noattachment":false,"key":"/@dnsquery/dns-packet/-/@dnsquery/dns-packet-6.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/@dnsquery/dns-packet/download/@dnsquery/dns-packet-6.1.1.tgz"},"_npmUser":{"name":"leichtgewicht","email":"martin.heidegger@gmail.com"},"directories":{},"maintainers":[{"name":"leichtgewicht","email":"martin.heidegger@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dns-packet_6.1.1_1670003322420_0.15093126662894418"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-12-02T17:50:00.405Z","publish_time":1670003322605,"_cnpm_publish_time":1670003322605},"6.1.0":{"name":"@dnsquery/dns-packet","version":"6.1.0","description":"An abstract-encoding compliant module for encoding / decoding DNS packets","author":{"name":"Mathias Buus"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/dnsquery/dns-packet.git"},"homepage":"https://github.com/dnsquery/dns-packet","main":"./index.js","module":"./index.mjs","types":"./types/index.d.ts","exports":{".":{"types":"./types/index.d.ts","import":"./index.mjs","require":"./index.js"},"./buffer_utils.js":{"types":"./buffer_utils.mjs","import":"./buffer_utils.mjs","require":"./buffer_utils.js"},"./classes.js":{"types":"./classes.d.ts","import":"./classes.mjs","require":"./classes.js"},"./opcodes.js":{"types":"./opcodes.d.ts","import":"./opcodes.mjs","require":"./opcodes.js"},"./optioncodes.js":{"types":"./optioncodes.d.ts","import":"./optioncodes.mjs","require":"./optioncodes.js"},"./rcodes.js":{"types":"./rcodes.d.ts","import":"./rcodes.mjs","require":"./rcodes.js"},"./types.js":{"types":"./types.d.ts","import":"./types.mjs","require":"./types.js"}},"engines":{"node":">=6"},"scripts":{"clean":"rm -rf coverage","lint":"standard && dtslint types --localTs node_modules/typescript/lib","test":"npm run lint && npm run unit","unit":"tape test.mjs","coverage":"c8 -r html npm test","prepare":"npx @leichtgewicht/esm2umd"},"dependencies":{"@leichtgewicht/ip-codec":"^2.0.4","utf8-codec":"^1.0.0"},"devDependencies":{"@definitelytyped/dtslint":"^0.0.119","@leichtgewicht/esm2umd":"^0.4.0","c8":"^7.12.0","standard":"^17.0.0","tape":"^5.6.1","typescript":"^4.9.3"},"keywords":["dns","packet","encodings","encoding","encoder","abstract-encoding"],"gitHead":"ddb846e8adf89dbeae3f235ab0425bae85c5ce67","bugs":{"url":"https://github.com/dnsquery/dns-packet/issues"},"_id":"@dnsquery/dns-packet@6.1.0","_nodeVersion":"19.0.0","_npmVersion":"8.19.2","dist":{"shasum":"284777a3215a89a148c154a7f0734d438afd9d21","size":42982,"noattachment":false,"key":"/@dnsquery/dns-packet/-/@dnsquery/dns-packet-6.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@dnsquery/dns-packet/download/@dnsquery/dns-packet-6.1.0.tgz"},"_npmUser":{"name":"leichtgewicht","email":"martin.heidegger@gmail.com"},"directories":{},"maintainers":[{"name":"leichtgewicht","email":"martin.heidegger@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dns-packet_6.1.0_1670001914679_0.625354963096385"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-12-02T17:27:21.055Z","publish_time":1670001914901,"_cnpm_publish_time":1670001914901}},"readme":"# @dnsquery/dns-packet\n[![](https://img.shields.io/npm/v/@dnsquery/dns-packet.svg?style=flat)](https://www.npmjs.org/package/@dnsquery/dns-packet) [![](https://img.shields.io/npm/dm/@dnsquery/dns-packet.svg)](https://www.npmjs.org/package/@dnsquery/dns-packet) [![Tests](https://github.com/dnsquery/dns-packet/actions/workflows/test.yml/badge.svg)](https://github.com/dnsquery/dns-packet/actions/workflows/test.yml)\n\nAn [abstract-encoding](https://github.com/mafintosh/abstract-encoding) compliant module for encoding / decoding DNS packets. Lifted out of [multicast-dns](https://github.com/mafintosh/multicast-dns) as a separate module.\n\n**This is a fork** of [dns-packet][] that comes with typescript types, javascript modules and does\nnot use Node.js primitives. Works well in browsers/react-native.\n\n[dns-packet]: https://github.com/mafintosh/dns-packet\n\n```\nnpm install @dnsquery/dns-packet\n```\n\n## UDP Usage\n\n``` js\nimport * as dnsPacket from '@dnsquery/dns-packet'\nimport dgram from 'dgram'\n\nconst socket = dgram.createSocket('udp4')\n\nconst buf = dnsPacket.encode({\n  type: 'query',\n  id: 1,\n  flags: dnsPacket.RECURSION_DESIRED,\n  questions: [{\n    type: 'A',\n    name: 'google.com'\n  }]\n})\n\nsocket.on('message', message => {\n  console.log(dnsPacket.decode(message)) // prints out a response from google dns\n})\n\nsocket.send(buf, 0, buf.length, 53, '8.8.8.8')\n```\n\nAlso see [the UDP example](examples/udp.js).\n\n## TCP, TLS, HTTPS\n\nWhile DNS has traditionally been used over a datagram transport, it is increasingly being carried over TCP for larger responses commonly including DNSSEC responses and TLS or HTTPS for enhanced security. See below examples on how to use `dns-packet` to wrap DNS packets in these protocols:\n\n- [TCP](examples/tcp.js)\n- [DNS over TLS](examples/tls.js)\n- [DNS over HTTPS](examples/doh.js)\n\n## API\n\n#### `var buf = packets.encode(packet, [buf], [offset])`\n\nEncodes a DNS packet into a buffer containing a UDP payload.\n\n#### `var packet = packets.decode(buf, [offset])`\n\nDecode a DNS packet from a buffer containing a UDP payload.\n\n#### `var buf = packets.streamEncode(packet, [buf], [offset])`\n\nEncodes a DNS packet into a buffer containing a TCP payload.\n\n#### `var packet = packets.streamDecode(buf, [offset])`\n\nDecode a DNS packet from a buffer containing a TCP payload.\n\n#### `var len = packets.encodingLength(packet)`\n\nReturns how many bytes are needed to encode the DNS packet\n\n## Packets\n\nPackets look like this\n\n``` js\n{\n  type: 'query|response',\n  id: optionalIdNumber,\n  flags: optionalBitFlags,\n  questions: [...],\n  answers: [...],\n  additionals: [...],\n  authorities: [...]\n}\n```\n\nThe bit flags available are\n\n``` js\nimport {\n  RECURSION_DESIRED,\n  RECURSION_AVAILABLE,\n  TRUNCATED_RESPONSE,\n  AUTHORITATIVE_ANSWER,\n  AUTHENTIC_DATA,\n  CHECKING_DISABLED\n} from '@dnsquery/dns-packet'\n```\n\nTo use more than one flag bitwise-or them together\n\n``` js\nvar flags = packet.RECURSION_DESIRED | packet.RECURSION_AVAILABLE\n```\n\nAnd to check for a flag use bitwise-and\n\n``` js\nvar isRecursive = message.flags & packet.RECURSION_DESIRED\n```\n\nA question looks like this\n\n``` js\n{\n  type: 'A', // or SRV, AAAA, etc\n  class: 'IN', // one of IN, CS, CH, HS, ANY. Default: IN\n  name: 'google.com' // which record are you looking for\n}\n```\n\nAnd an answer, additional, or authority looks like this\n\n``` js\n{\n  type: 'A', // or SRV, AAAA, etc\n  class: 'IN', // one of IN, CS, CH, HS\n  name: 'google.com', // which name is this record for\n  ttl: optionalTimeToLiveInSeconds,\n  (record specific data, see below)\n}\n```\n\n## Supported record types\n\n#### `A`\n\n``` js\n{\n  data: 'IPv4 address' // fx 127.0.0.1\n}\n```\n\n#### `AAAA`\n\n``` js\n{\n  data: 'IPv6 address' // fx fe80::1\n}\n```\n\n#### `CAA`\n\n``` js\n{\n  flags: 128, // octet\n  tag: 'issue|issuewild|iodef',\n  value: 'ca.example.net',\n  issuerCritical: false\n}\n```\n\n#### `CNAME`\n\n``` js\n{\n  data: 'cname.to.another.record'\n}\n```\n\n#### `DNAME`\n\n``` js\n{\n  data: 'dname.to.another.record'\n}\n```\n\n#### `DNSKEY`\n\n``` js\n{\n  flags: 257, // 16 bits\n  algorithm: 1, // octet\n  key: Buffer\n}\n```\n\n#### `DS`\n\n``` js\n{\n  keyTag: 12345,\n  algorithm: 8,\n  digestType: 1,\n  digest: Buffer\n}\n```\n\n#### `HINFO`\n\n``` js\n{\n  data: {\n    cpu: 'cpu info',\n    os: 'os info'\n  }\n}\n```\n\n#### `MX`\n\n``` js\n{\n  preference: 10,\n  exchange: 'mail.example.net'\n}\n```\n\n#### `NS`\n\n``` js\n{\n  data: nameServer\n}\n```\n\n#### `NSEC`\n\n``` js\n{\n  nextDomain: 'a.domain',\n  rrtypes: ['A', 'TXT', 'RRSIG']\n}\n```\n\n#### `NSEC3`\n\n``` js\n{\n  algorithm: 1,\n  flags: 0,\n  iterations: 2,\n  salt: Buffer,\n  nextDomain: Buffer, // Hashed per RFC5155\n  rrtypes: ['A', 'TXT', 'RRSIG']\n}\n```\n\n#### `NULL`\n\n``` js\n{\n  data: Buffer('any binary data')\n}\n```\n\n#### `OPT`\n\n[EDNS0](https://tools.ietf.org/html/rfc6891) options.\n\n``` js\n{\n  type: 'OPT',\n  name: '.',\n  udpPayloadSize: 4096,\n  flags: packet.DNSSEC_OK,\n  options: [{\n    // pass in any code/data for generic EDNS0 options\n    code: 12,\n    data: Buffer.alloc(31)\n  }, {\n    // Several EDNS0 options have enhanced support\n    code: 'PADDING',\n    length: 31,\n  }, {\n    code: 'CLIENT_SUBNET',\n    family: 2, // 1 for IPv4, 2 for IPv6\n    sourcePrefixLength: 64, // used to truncate IP address\n    scopePrefixLength: 0,\n    ip: 'fe80::',\n  }, {\n    code: 'TCP_KEEPALIVE',\n    timeout: 150 // increments of 100ms.  This means 15s.\n  }, {\n    code: 'KEY_TAG',\n    tags: [1, 2, 3],\n  }]\n}\n```\n\nThe options `PADDING`, `CLIENT_SUBNET`, `TCP_KEEPALIVE` and `KEY_TAG` support enhanced de/encoding. See [optionscodes.js](https://github.com/mafintosh/dns-packet/blob/master/optioncodes.js) for all supported option codes. If the `data` property is present on a option, it takes precedence. On decoding, `data` will always be defined.\n\n#### `PTR`\n\n``` js\n{\n  data: 'points.to.another.record'\n}\n```\n\n#### `RP`\n\n``` js\n{\n  mbox: 'admin.example.com',\n  txt: 'txt.example.com'\n}\n```\n\n#### `SSHFP`\n\n``` js\n{\n  algorithm: 1,\n  hash: 1,\n  fingerprint: 'A108C9F834354D5B37AF988141C9294822F5BC00'\n}\n````\n\n#### `RRSIG`\n\n``` js\n{\n  typeCovered: 'A',\n  algorithm: 8,\n  labels: 1,\n  originalTTL: 3600,\n  expiration: timestamp,\n  inception: timestamp,\n  keyTag: 12345,\n  signersName: 'a.name',\n  signature: Buffer\n}\n```\n\n#### `SOA`\n\n``` js\n{\n  data:\n    {\n      mname: domainName,\n      rname: mailbox,\n      serial: zoneSerial,\n      refresh: refreshInterval,\n      retry: retryInterval,\n      expire: expireInterval,\n      minimum: minimumTTL\n    }\n}\n```\n\n#### `SRV`\n\n``` js\n{\n  data: {\n    port: servicePort,\n    target: serviceHostName,\n    priority: optionalServicePriority,\n    weight: optionalServiceWeight\n  }\n}\n```\n\n#### `TXT`\n\n``` js\n{\n  data: 'text' || Buffer || [ Buffer || 'text' ]\n}\n```\n\nWhen encoding, scalar values are converted to an array and strings are converted to UTF-8 encoded Buffers. When decoding, the return value will always be an array of Buffer.\n\nIf you need another record type, open an issue and we'll try to add it.\n\n## License\n\nMIT\n","_attachments":{},"homepage":"https://github.com/dnsquery/dns-packet","bugs":{"url":"https://github.com/dnsquery/dns-packet/issues"},"license":"MIT"}