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