From: Justin Wind Date: Fri, 2 Jun 2023 00:22:04 +0000 (-0700) Subject: use native dns promises X-Git-Tag: v1.3.0~5 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=03cdf23c601ed191fb3d23f5d5bd7b3c7198e578;p=squeep-indieauth-helper use native dns promises --- diff --git a/.eslintrc.json b/.eslintrc.json index 697136a..a4d211a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -11,7 +11,7 @@ "plugin:sonarjs/recommended" ], "parserOptions": { - "ecmaVersion": 2018 + "ecmaVersion": "latest" }, "plugins": [ "node", diff --git a/lib/communication.js b/lib/communication.js index 2a69e34..ce2bc4e 100644 --- a/lib/communication.js +++ b/lib/communication.js @@ -11,7 +11,6 @@ const { promisify } = require('util'); const randomBytesAsync = promisify(randomBytes); const { Address4, Address6 } = require('ip-address'); const dns = require('dns'); -dns.lookupAsync = dns.lookupAsync || promisify(dns.lookup); const common = require('./common'); const Enum = require('./enum'); const { ValidationError } = require('./errors'); @@ -439,7 +438,7 @@ class Communication { if (!urlObj.hostname.endsWith('.')) { hostnames.push(urlObj.hostname + '.'); } - const settledResolutions = await Promise.allSettled(hostnames.map((hostname) => dns.lookupAsync(hostname, { + const settledResolutions = await Promise.allSettled(hostnames.map((hostname) => dns.promises.lookup(hostname, { all: true, verbatim: true, }))); @@ -519,7 +518,7 @@ class Communication { Communication._urlValidScheme(profile); Communication._urlPartsDisallowed(profile, ['hash', 'username', 'password', 'port']); Communication._urlPathNoDots(url); - Communication._urlNamedHost(profile, options.allowLoopback, options.resolveHostname); + await Communication._urlNamedHost(profile, options.allowLoopback, options.resolveHostname); } catch (e) { this.logger.debug(_scope, 'profile url not valid', { url, error: e }); throw new ValidationError(`${errorScope}: ${e.message}`); @@ -607,7 +606,7 @@ class Communication { let urlMatched = false; const itemType = item.type || []; if ((itemType.includes('h-app') || itemType.includes('h-x-app')) - && (item.properties && item.properties.url)) { + && (item?.properties?.url)) { item.properties.url.forEach((url) => { try { const hUrl = new URL(url); @@ -671,7 +670,7 @@ class Communication { // and populate profile fields with first-encountered card values. if (mfData && 'items' in mfData) { const hCards = mfData.items.filter((item) => - item.type && item.type.includes('h-card') && + item?.type?.includes('h-card') && item.properties && item.properties.url && item.properties.url.includes(urlObj.href)); hCards.forEach((hCard) => { Object.keys(profile).forEach((key) => { @@ -800,7 +799,7 @@ class Communication { * @returns {Object} */ async redeemProfileCode(urlObj, code, codeVerifier, clientId, redirectURI) { - return await this.redeemCode(urlObj, code, codeVerifier, clientId, redirectURI); + return this.redeemCode(urlObj, code, codeVerifier, clientId, redirectURI); } diff --git a/test/lib/communication.js b/test/lib/communication.js index a94bf90..7ecad8e 100644 --- a/test/lib/communication.js +++ b/test/lib/communication.js @@ -528,7 +528,7 @@ describe('Communication', function () { beforeEach(function () { url = 'https://example.com/'; validationOptions = {}; - sinon.stub(dns, 'lookupAsync').resolves([{ family: 4, address: '10.11.12.14' }]); + sinon.stub(dns.promises, 'lookup').resolves([{ family: 4, address: '10.11.12.14' }]); }); it('rejects invalid url', async function () { url = 'bad url'; @@ -550,7 +550,7 @@ describe('Communication', function () { beforeEach(function () { url = 'https://example.com/'; validationOptions = {}; - sinon.stub(dns, 'lookupAsync').resolves([{ family: 4, address: '10.11.12.13' }]); + sinon.stub(dns.promises, 'lookup').resolves([{ family: 4, address: '10.11.12.13' }]); }); it('rejects invalid url', async function () { await assert.rejects(() => communication.validateClientIdentifier('bad url'), ValidationError); @@ -594,12 +594,12 @@ describe('Communication', function () { assert.strictEqual(result.isLoopback, true); }); it('accepts resolved ipv4 loopback', async function () { - dns.lookupAsync.resolves([{ family: 4, address: '127.0.0.1' }]); + dns.promises.lookup.resolves([{ family: 4, address: '127.0.0.1' }]); const result = await communication.validateClientIdentifier(url, validationOptions); assert.strictEqual(result.isLoopback, true); }); it('accepts resolved ipv6 loopback', async function () { - dns.lookupAsync.resolves([{ family: 6, address: '::1' }]); + dns.promises.lookup.resolves([{ family: 6, address: '::1' }]); const result = await communication.validateClientIdentifier(url, validationOptions); assert.strictEqual(result.isLoopback, true); }); @@ -608,15 +608,15 @@ describe('Communication', function () { assert.strictEqual(result.isLoopback, false); }); it('rejects resolution failure', async function () { - dns.lookupAsync.rejects(new Error('oh no')); + dns.promises.lookup.rejects(new Error('oh no')); await assert.rejects(() => communication.validateClientIdentifier(url, validationOptions), ValidationError); }); it('rejects mismatched resolutions', async function () { - dns.lookupAsync.onCall(1).resolves([{ family: 4, address: '10.9.8.7' }]); + dns.promises.lookup.onCall(1).resolves([{ family: 4, address: '10.9.8.7' }]); await assert.rejects(() => communication.validateClientIdentifier(url, validationOptions), ValidationError); }); it('ignores unknown dns family', async function () { - dns.lookupAsync.resolves([{ family: 5, address: '10.9.8.7' }]); + dns.promises.lookup.resolves([{ family: 5, address: '10.9.8.7' }]); const result = await communication.validateClientIdentifier(url, validationOptions); assert.strictEqual(result.isLoopback, false); }); @@ -626,7 +626,7 @@ describe('Communication', function () { assert.strictEqual(result.isLoopback, false); }); it('covers unresolved', async function () { - dns.lookupAsync.resolves(); + dns.promises.lookup.resolves(); const result = await communication.validateClientIdentifier(url, validationOptions); assert.strictEqual(result.isLoopback, false); });