-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
15 changed files
with
446 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ root: true | |
|
||
parserOptions: | ||
ecmaVersion: 2020 | ||
sourceType: module | ||
|
||
extends: | ||
- eslint:recommended | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}', | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
*/ |
Oops, something went wrong.