Skip to content

Commit

Permalink
release 0.4.0 (#6)
Browse files Browse the repository at this point in the history
- style: convert from CJS to ESM
- reimplement parsers in pure JS (bye nearley & moo)
- nsd: add value cleanups
- knot: handle array-type items
- test(bind): greatly improved test
- feat: start on nt-ns.js
- switch test coverage report nyc -> c8
  • Loading branch information
msimerson committed Apr 10, 2022
1 parent 4496497 commit b2b3d3c
Show file tree
Hide file tree
Showing 15 changed files with 446 additions and 100 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ root: true

parserOptions:
ecmaVersion: 2020
sourceType: module

extends:
- eslint:recommended
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
- name: npm install
run: |
npm install
npm install --no-save nyc codecov
npm install --no-save c8 codecov
- name: run coverage
run: npx nyc --reporter=lcovonly npm run test
run: npx c8 --reporter=lcovonly npm run test
env:
NODE_ENV: cov

Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ jobs:
fetch-depth: 0

- name: Setup node.js ${{ env.node_version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: ${{ env.node_version }}
registry-url: https://registry.npmjs.org/

- run: npm install

- name: Run tests
run: npm run test
- run: npm run test

- name: publish to NPM
run: npm publish --access public
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [0.4.0](https://github.com/NicTool/dns-nameserver/compare/v0.1.0...v0.4.0) (2022-04-10)


### ⚠ BREAKING CHANGES

* convert from CJS to ESM

### Features

* add maradns config parser ([#5](https://github.com/NicTool/dns-nameserver/issues/5)) ([4496497](https://github.com/NicTool/dns-nameserver/commit/449649784901ed02a3753a1b85f25d5617566492))
* parsers added for Knot ([#2](https://github.com/NicTool/dns-nameserver/issues/2)) ([139235e](https://github.com/NicTool/dns-nameserver/commit/139235e8acd105ce872cddb22224e1470c972cbe))


* convert from CJS to ESM ([11b66b2](https://github.com/NicTool/dns-nameserver/commit/11b66b2b83416fdb3338f9d537c9643814a13960))

## [0.3.0](https://github.com/NicTool/dns-nameserver/compare/v0.1.0...v0.3.0) (2022-04-09)


Expand Down
111 changes: 111 additions & 0 deletions bin/nt-ns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!node

// import fs from 'node:fs/promises'
// const path = require('path')
// const os = require('os')

// eslint-disable-next-line node/shebang
import chalk from 'chalk'

import cmdLineArgs from 'command-line-args'
import cmdLineUsage from 'command-line-usage'

function usage () {
console.error(cmdLineUsage(usageSections()))
// eslint-disable-next-line no-process-exit
process.exit(1)
}

// CLI argument processing
const opts = cmdLineArgs(usageOptions())._all
if (opts.verbose) console.error(opts)
if (opts.help) usage()


function usageOptions () {
return [
{
name : 'import',
alias : 'i',
defaultValue: 'bind',
type : String,
typeLabel : '<bind | knot | maradns | nsd | tinydns>',
description : 'nameserver type',
group : 'io',
},
{
name : 'export',
alias : 'e',
defaultValue: 'js',
type : String,
typeLabel : '<bind | knot | maradns | nsd>',
description : 'nameserver type',
group : 'io',
},
{
name : 'file',
alias : 'f',
// defaultValue: '',
type : String,
typeLabel : '<file path>',
description : 'source of DNS server config file',
group : 'io',
},
{
name : 'verbose',
alias : 'v',
description: 'Show status messages during processing',
type : Boolean,
},
{
name : 'help',
description: 'Display this usage guide',
alias : 'h',
type : Boolean,
},
]
}

function usageSections () {
return [
{
content: chalk.blue(` +-+-+-+ +-+-+-+-+-+-+-+-+-+-+\n |D|N|S| |N|A|M|E|S|E|R|V|E|R|\n +-+-+-+ +-+-+-+-+-+-+-+-+-+-+`),
raw : true,
},
{
header : 'I/O',
optionList: usageOptions(),
group : 'io',
},
{
header : 'NS Settings',
optionList: usageOptions(),
group : 'main',
},
{
header : 'Misc',
optionList: usageOptions(),
group : '_none',
},
{
header : 'Examples',
content: [
{
desc : '1. ',
example: './bin/nt-ns.js ',
},
{
desc : '2. ',
example: './bin/nt-ns.js ',
},
{
desc : '3. ',
example: './bin/nt-ns.js ',
},
],
},
{
content: 'Project home: {underline https://github.com/NicTool/dns-nameserver}',
},
]
}
15 changes: 4 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@



export function valueCleanup (str) {








exports.valueCleanup = function (str) {
// strip double quotes
if (str.charAt(0) === '"' && str.charAt(str.length -1) === '"') {
str = str.substr(1,str.length -2)
if (str.startsWith('"') && str.endsWith('"')) {
str = str.substr(1,str.length -2) // strip double quotes
}

if (/^[0-9.]+$/.test(str) && Number(str).toString() === str) {
Expand All @@ -24,3 +16,4 @@ exports.valueCleanup = function (str) {

return str
}

95 changes: 84 additions & 11 deletions lib/bind.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,98 @@
// https://bind9.readthedocs.io/en/latest/reference.html

const os = require('os')
import { valueCleanup } from '../index.js'

const nearley = require('nearley')

exports.parseConfig = async str => {
export function parseConfig (str) {

const re = {
open : /^\s*([\w\t -]+?)\s*{\s*$/, // logging, options, etc.
openzone: /^\s*zone\s*"([^"]+)"\s*(in|IN)?{/,
close : /^\s*(};)\s*$/,
key_val : /^[ \t]+([^\s]+?)\s+([^\r\n]+?)\s*(\/\/[^\r\n]+)?$/,
keyOnly : /^[ \t]+([^\s]+?)\s*;$/,
blank : /^\s*?$/,
comment : /^\s*(?:\/\/)[^\r\n]*?$/,
}

const res = {}
const statement = []
let inner = {}

for (const line of str.split(/[\r\n]/)) {
if (re.blank.test(line)) continue
if (re.comment.test(line)) continue

// eslint-disable-next-line node/no-unpublished-require
const open = line.match(re.open)
if (open) {
statement.push(open[1])
continue
}

const openzone = line.match(re.openzone)
if (openzone) {
statement.push('zone')
inner.name = openzone[1]
continue
}

const close = line.match(re.close)
if (close) {
const statementName = statement.pop()

if (statementName === 'zone') {
if (res.zone === undefined) res.zone = []
res.zone.push(inner)
inner = {}
}
else {
let target = res
for (const s of statement) {
if (target[s] === undefined) target[s] = {}
target = target[s]
}
if (Object.keys(inner).length) {
target[statementName] = inner
}
}

inner = {}
continue
}

const kv = line.match(re.key_val)
const ko = line.match(re.keyOnly)

if (kv) {
inner[kv[1]] = valueCleanup(kv[2].slice(0,-1))
}
else if (ko) {
inner[ko[1]] = ''
}
else {
console.error(line)
throw 'parser failed'
}
}

return res
}



/*
const os = require('os')
const nearley = require('nearley')
exports.parseConfig = async str => {
const grammar = nearley.Grammar.fromCompiled(require('../dist/bind.js'))
grammar.start = 'main'

const parser = new nearley.Parser(grammar)
parser.feed(str)
if (!str.endsWith(os.EOL)) parser.feed(os.EOL)

if (parser.length > 1) {
console.error(`ERROR: ambigious parser rule`)
}

if (parser.length > 1) console.error(`ERROR: ambigious parser rule`)
if (parser.results.length === 0) return []

return parser.results[0]
.filter(z => z[0] && z[0].type) // weed out nulls
.map(z => z[0]) // remove array nesting
}
*/
Loading

0 comments on commit b2b3d3c

Please sign in to comment.