DNS Zone File Parser and Generator

1.0.0 · abandoned · verified Sun Apr 19

The `zone-file` package provides an RFC1035-compliant JavaScript library for both parsing and generating DNS zone files. It supports a comprehensive range of common DNS record types, including Start of Authority (SOA), Name Server (NS), A records (IPv4), AAAA records (IPv6), Canonical Name (CNAME), Mail Exchange (MX), Pointer (PTR), Service (SRV), Text (TXT), and Uniform Resource Identifier (URI) records, alongside the critical `$ORIGIN` keyword for defining the zone's base domain. The current and only stable version available on npm is 1.0.0, which was last published in January 2019. While the library itself is functional for its stated purpose, the lack of new releases under the `zone-file` npm package for several years, despite related code being developed under the `stacks-network/zone-file-js` GitHub repository, indicates that the specific npm package `zone-file` is effectively abandoned. Developers should be aware of this infrequent update cadence and consider potential alternatives for ongoing maintenance and feature support.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to parse an existing DNS zone file string into a JavaScript object, modify the object by adding a new CNAME record, and then generate an updated zone file string.

import { parseZone, generate } from 'zone-file';

const zoneFileContent = `
$ORIGIN example.com.
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
  2023010101 ; Serial
  7200       ; Refresh
  3600       ; Retry
  1209600    ; Expire
  3600       ; Minimum TTL
)
@ IN NS ns1.example.com.
@ IN NS ns2.example.com.
@ IN A 192.0.2.1
www IN A 192.0.2.2
mail IN MX 10 mail.example.com.
mail IN A 192.0.2.3
`;

try {
  // Parse the zone file content into a JavaScript object
  const parsedZone = parseZone(zoneFileContent);
  console.log('Parsed Zone Object:');
  console.log(JSON.stringify(parsedZone, null, 2));

  // Add a new record to the parsed object
  parsedZone.cname = parsedZone.cname || [];
  parsedZone.cname.push({ name: 'blog', alias: 'www' });

  // Generate a new zone file string from the modified object
  const generatedZone = generate(parsedZone);
  console.log('\nGenerated Zone File with new CNAME record:');
  console.log(generatedZone);

} catch (error) {
  console.error('Error processing zone file:', error.message);
}

view raw JSON →