Skip to content

Commit

Permalink
Merge pull request #56 from hildjj/typescript
Browse files Browse the repository at this point in the history
Typescript
  • Loading branch information
hildjj authored Sep 6, 2024
2 parents 9fd922c + 572a214 commit bca96bf
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 92 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
},
"devDependencies": {
"@cto.af/eslint-config": "^4.1.6",
"@types/ip": "1.1.3",
"ava": "^6.1.3",
"c8": "^10.1.2",
"eslint": "^9.9.1",
Expand Down
56 changes: 47 additions & 9 deletions pkg/dohdec-cli/lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Command, InvalidArgumentError} from 'commander';
import {DNSError, DNSutils} from 'dohdec/lib/dnsUtils.js';
import {DNSoverHTTPS, DNSoverTLS} from 'dohdec';
import {DNSError, DNSoverHTTPS, DNSoverTLS, DNSutils} from 'dohdec';
import {Buffer} from 'node:buffer';
import assert from 'node:assert';
import net from 'node:net';
import readline from 'node:readline';
import util from 'node:util';
Expand All @@ -21,6 +22,28 @@ function myParseInt(value) {
return parsedValue;
}

/**
* @param {any} pkt
* @returns {asserts pkt is import('dns-packet').Packet}
* @private
*/
function assertIsPacket(pkt) {
assert(pkt);
assert(typeof pkt === 'object');
assert(!Buffer.isBuffer(pkt));
assert(!Array.isArray(pkt));
}

/**
* @param {any} er
* @returns {asserts er is Error}
*/
function assertIsError(er) {
assert(er);
assert(typeof er === 'object');
assert(Object.prototype.hasOwnProperty.call(er, 'message'));
}

/**
* Parse an IPv4/IPv6 address or throw if invalid.
*
Expand Down Expand Up @@ -58,10 +81,10 @@ export class DnsCli extends Command {
constructor(args, stdio) {
super();

/** @type {DNSoverHTTPS|DNSoverTLS} */
this.transport = null;
/** @type {DNSoverHTTPS|DNSoverTLS|undefined} */
this.transport = undefined;

/** @type {Stdio} */
/** @type {Required<Stdio>} */
this.std = {
in: process.stdin,
out: process.stdout,
Expand Down Expand Up @@ -158,6 +181,7 @@ For more debug information:
* Run the CLI.
*/
async main() {
assert(this.transport);
try {
if (this.argv.name) {
await this.get(this.argv.name, this.argv.rrtype);
Expand All @@ -170,6 +194,10 @@ For more debug information:
}
}

/**
* @param {string} name
* @param {import('dns-packet').RecordType} rrtype
*/
async get(name, rrtype) {
const opts = {
name,
Expand All @@ -181,6 +209,7 @@ For more debug information:
dnssec: this.argv.dnssec,
dnssecCheckingDisabled: this.argv.dnssecCheckingDisabled,
};
assert(this.transport);
try {
if (net.isIP(opts.name)) {
opts.name = DNSutils.reverse(opts.name);
Expand All @@ -191,16 +220,18 @@ For more debug information:
let resp = await this.transport.lookup(opts);
if (this.argv.decode) {
if (!this.argv.full) {
assertIsPacket(resp);
const er = DNSError.getError(resp);
if (er) {
// This isn't ideal, since a) this is normal operation and
// b) we're just going to re-raise the error below. However,
// this turned out to be easier to test.
throw er;
}
// Avoid typescript errors
// eslint-disable-next-line dot-notation
resp = resp['answers'] || resp['Answer'] || resp;
resp =
resp.answers ||
/** @type {Record<string, any>}*/ (resp).Answer ||
resp;
}
this.std.out.write(util.inspect(DNSutils.buffersToB64(resp), {
depth: Infinity,
Expand All @@ -215,13 +246,16 @@ For more debug information:
}
}
} catch (er) {
assertIsError(er);
this.transport.verbose(1, er) ||
this.transport.verbose(0, () => (er.message ? er.message : er));
throw er;
}
}

async prompt() {
assert(this.transport);

let errors = 0;
let total = 0;
const rl = readline.createInterface({
Expand All @@ -235,7 +269,11 @@ For more debug information:
if (line.length > 0) {
total++;
try {
await this.get(...line.split(/\s+/));
const [name, rrtype] = line.split(/\s+/);
await this.get(
name,
/** @type {import('dns-packet').RecordType} */(rrtype)
);
} catch (ignored) {
// Catches all errors. get() printed them already
errors++;
Expand Down
1 change: 1 addition & 0 deletions pkg/dohdec-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dohdec": "6.0.1"
},
"devDependencies": {
"@types/dns-packet": "5.6.5",
"@types/node": "^22.5.4",
"jsrsasign": "^11.1.0",
"minami": "^1.2.3",
Expand Down
6 changes: 4 additions & 2 deletions pkg/dohdec-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
"emitDeclarationOnly": true,
"moduleResolution": "node",
"module": "es2020",
// "noImplicitAny": true,
"noImplicitAny": true,
"strict": true,
"skipLibCheck": true,
"outDir": "types",
"target": "ES2020",
"target": "ES2022",
"resolveJsonModule": true
}
}
1 change: 1 addition & 0 deletions pkg/dohdec/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage/
test/
tsconfig.json
typedoc.config.cjs
typings.d.ts
Loading

0 comments on commit bca96bf

Please sign in to comment.