# dns-txt

> Encode/decode DNS-SD TXT record RDATA fields

Latest version **2.0.2** (published 2016-04-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install dns-txt
pnpm add dns-txt
yarn add dns-txt
bun add dns-txt
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2016-04-24 |
| First published | 2016-02-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/dns-txt) |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | Thomas Watson Steen |
| Maintainers | watson |
| Keywords | rfc6763, 6763, rfc6762, 6762, dns, mdns, multicast, txt, rdata, dns-sd, encode, decode, parse, encoder, decoder, parser, service, discovery |

## Links

- npm: https://www.npmjs.com/package/dns-txt
- Repository: https://github.com/watson/dns-txt
- Issues: https://github.com/watson/dns-txt/issues
- npm.io page: https://npm.io/package/dns-txt

## Dependencies (1)

- [buffer-indexof](https://npm.io/package/buffer-indexof.md) ^1.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2016-04-24
- 2.0.1 — 2016-02-29

## README

# dns-txt

Encode or decode the RDATA field in multicast DNS TXT records. For use
with DNS-Based Service Discovery. For details see [RFC
6763](https://tools.ietf.org/html/rfc6763).

[![Build status](https://travis-ci.org/watson/dns-txt.svg?branch=master)](https://travis-ci.org/watson/dns-txt)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
[![abstract-encoding](https://img.shields.io/badge/abstract--encoding-compliant-brightgreen.svg?style=flat)](https://github.com/mafintosh/abstract-encoding)

## Installation

```
npm install dns-txt
```

## Usage

```js
var txt = require('dns-txt')()

var obj = {
  foo: 1,
  bar: 2
}

var enc = txt.encode(obj) // <Buffer 05 66 6f 6f 3d 31 05 62 61 72 3d 32>

txt.decode(enc) // { foo: '1', bar: '2' }
```

## API

The encoder and decoder conforms to [RFC 6763](https://tools.ietf.org/html/rfc6763).

### Initialize

The module exposes a constructor function which can be called with an
optional options object:

```js
var txt = require('dns-txt')({ binary: true })
```

The options are:

- `binary` - If set to `true` all values will be returned as `Buffer`
  objects. The default behavior is to turn all values into strings. But
  according to the RFC the values can be any binary data. If you expect
  binary data, use this option.

#### `txt.encode(obj, [buffer], [offset])`

Takes a key/value object and returns a buffer with the encoded TXT
record. If a buffer is passed as the second argument the object should
be encoded into that buffer. Otherwise a new buffer should be allocated
If an offset is passed as the third argument the object should be
encoded at that byte offset. The byte offset defaults to `0`.

This module does not actively validate the key/value pairs, but keep the
following in rules in mind:

- To be RFC compliant, each key should conform with the rules as
  specified in [section
  6.4](https://tools.ietf.org/html/rfc6763#section-6.4).

- To be RFC compliant, each value should conform with the rules as
  specified in [section
  6.5](https://tools.ietf.org/html/rfc6763#section-6.5).

After encoding `txt.encode.bytes` is set to the amount of bytes used to
encode the object.

#### `txt.decode(buffer, [offset], [length])`

Takes a buffer and returns a decoded key/value object. If an offset is
passed as the second argument the object should be decoded from that
byte offset. The byte offset defaults to `0`. Note that all keys will be
lowercased and all values will be Buffer objects.

After decoding `txt.decode.bytes` is set to the amount of bytes used to
decode the object.

#### `txt.encodingLength(obj)`

Takes a single key/value object and returns the number of bytes that the given
object would require if encoded.

## License

MIT

---
_Source: https://npm.io/package/dns-txt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
