You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.7 KiB

4 years ago
  1. # dns-txt
  2. Encode or decode the RDATA field in multicast DNS TXT records. For use
  3. with DNS-Based Service Discovery. For details see [RFC
  4. 6763](https://tools.ietf.org/html/rfc6763).
  5. [![Build status](https://travis-ci.org/watson/dns-txt.svg?branch=master)](https://travis-ci.org/watson/dns-txt)
  6. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
  7. [![abstract-encoding](https://img.shields.io/badge/abstract--encoding-compliant-brightgreen.svg?style=flat)](https://github.com/mafintosh/abstract-encoding)
  8. ## Installation
  9. ```
  10. npm install dns-txt
  11. ```
  12. ## Usage
  13. ```js
  14. var txt = require('dns-txt')()
  15. var obj = {
  16. foo: 1,
  17. bar: 2
  18. }
  19. var enc = txt.encode(obj) // <Buffer 05 66 6f 6f 3d 31 05 62 61 72 3d 32>
  20. txt.decode(enc) // { foo: '1', bar: '2' }
  21. ```
  22. ## API
  23. The encoder and decoder conforms to [RFC 6763](https://tools.ietf.org/html/rfc6763).
  24. ### Initialize
  25. The module exposes a constructor function which can be called with an
  26. optional options object:
  27. ```js
  28. var txt = require('dns-txt')({ binary: true })
  29. ```
  30. The options are:
  31. - `binary` - If set to `true` all values will be returned as `Buffer`
  32. objects. The default behavior is to turn all values into strings. But
  33. according to the RFC the values can be any binary data. If you expect
  34. binary data, use this option.
  35. #### `txt.encode(obj, [buffer], [offset])`
  36. Takes a key/value object and returns a buffer with the encoded TXT
  37. record. If a buffer is passed as the second argument the object should
  38. be encoded into that buffer. Otherwise a new buffer should be allocated
  39. If an offset is passed as the third argument the object should be
  40. encoded at that byte offset. The byte offset defaults to `0`.
  41. This module does not actively validate the key/value pairs, but keep the
  42. following in rules in mind:
  43. - To be RFC compliant, each key should conform with the rules as
  44. specified in [section
  45. 6.4](https://tools.ietf.org/html/rfc6763#section-6.4).
  46. - To be RFC compliant, each value should conform with the rules as
  47. specified in [section
  48. 6.5](https://tools.ietf.org/html/rfc6763#section-6.5).
  49. After encoding `txt.encode.bytes` is set to the amount of bytes used to
  50. encode the object.
  51. #### `txt.decode(buffer, [offset], [length])`
  52. Takes a buffer and returns a decoded key/value object. If an offset is
  53. passed as the second argument the object should be decoded from that
  54. byte offset. The byte offset defaults to `0`. Note that all keys will be
  55. lowercased and all values will be Buffer objects.
  56. After decoding `txt.decode.bytes` is set to the amount of bytes used to
  57. decode the object.
  58. #### `txt.encodingLength(obj)`
  59. Takes a single key/value object and returns the number of bytes that the given
  60. object would require if encoded.
  61. ## License
  62. MIT