From 572a2144c96cd45c04d83b202e7926246b185c77 Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Fri, 6 Sep 2024 09:16:32 -0600 Subject: [PATCH] Turn on strict in dohdecl-cli --- pkg/dohdec-cli/lib/cli.js | 23 +++++++++++++++++++---- pkg/dohdec-cli/tsconfig.json | 1 + pkg/dohdec/lib/dnsUtils.js | 2 +- pkg/dohdec/test/dnsUtils.ava.js | 3 +++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/dohdec-cli/lib/cli.js b/pkg/dohdec-cli/lib/cli.js index 3037d8b..45dd192 100644 --- a/pkg/dohdec-cli/lib/cli.js +++ b/pkg/dohdec-cli/lib/cli.js @@ -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. * @@ -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} */ this.std = { in: process.stdin, out: process.stdout, @@ -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); @@ -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); @@ -234,6 +246,7 @@ For more debug information: } } } catch (er) { + assertIsError(er); this.transport.verbose(1, er) || this.transport.verbose(0, () => (er.message ? er.message : er)); throw er; @@ -241,6 +254,8 @@ For more debug information: } async prompt() { + assert(this.transport); + let errors = 0; let total = 0; const rl = readline.createInterface({ @@ -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 diff --git a/pkg/dohdec-cli/tsconfig.json b/pkg/dohdec-cli/tsconfig.json index 174bbe9..8f3d7d4 100644 --- a/pkg/dohdec-cli/tsconfig.json +++ b/pkg/dohdec-cli/tsconfig.json @@ -10,6 +10,7 @@ "moduleResolution": "node", "module": "es2020", "noImplicitAny": true, + "strict": true, "skipLibCheck": true, "outDir": "types", "target": "ES2022", diff --git a/pkg/dohdec/lib/dnsUtils.js b/pkg/dohdec/lib/dnsUtils.js index 4df3ef4..933612d 100644 --- a/pkg/dohdec/lib/dnsUtils.js +++ b/pkg/dohdec/lib/dnsUtils.js @@ -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')) { diff --git a/pkg/dohdec/test/dnsUtils.ava.js b/pkg/dohdec/test/dnsUtils.ava.js index f8f0f03..c990532 100644 --- a/pkg/dohdec/test/dnsUtils.ava.js +++ b/pkg/dohdec/test/dnsUtils.ava.js @@ -227,4 +227,7 @@ test('DNSError', t => { er = DNSError.getError({}); t.falsy(er); + + t.throws(() => DNSError.getError(null)); + t.throws(() => DNSError.getError(4)); });