Skip to content

Commit

Permalink
Turn on strict in dohdecl-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Sep 6, 2024
1 parent b3f95fc commit 572a214
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
23 changes: 19 additions & 4 deletions pkg/dohdec-cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ function assertIsPacket(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 @@ -71,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 @@ -171,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 Down Expand Up @@ -198,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 Down Expand Up @@ -234,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 @@ -257,7 +272,7 @@ For more debug information:
const [name, rrtype] = line.split(/\s+/);
await this.get(
name,
/** @type {import('dns-packet').RecordType | undefined} */(rrtype)
/** @type {import('dns-packet').RecordType} */(rrtype)
);
} catch (ignored) {
// Catches all errors. get() printed them already
Expand Down
1 change: 1 addition & 0 deletions pkg/dohdec-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"moduleResolution": "node",
"module": "es2020",
"noImplicitAny": true,
"strict": true,
"skipLibCheck": true,
"outDir": "types",
"target": "ES2022",
Expand Down
2 changes: 1 addition & 1 deletion pkg/dohdec/lib/dnsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export class DNSError extends Error {
* @returns {DNSError|undefined}
*/
static getError(pkt) {
if ((typeof packet !== 'object') || !pkt) {
if ((typeof pkt !== 'object') || !pkt) {
throw new TypeError('Invalid packet');
}
if (Object.prototype.hasOwnProperty.call(pkt, 'rcode')) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/dohdec/test/dnsUtils.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,7 @@ test('DNSError', t => {

er = DNSError.getError({});
t.falsy(er);

t.throws(() => DNSError.getError(null));
t.throws(() => DNSError.getError(4));
});

0 comments on commit 572a214

Please sign in to comment.