From b75d07218762f3e3aa0af92768111dbcc0133c12 Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Thu, 14 Sep 2023 18:02:17 +0400 Subject: [PATCH] pkg: add type defs for tslint. --- lib/dns.js | 47 +++++++++++++++++++++++++++++++++++++++++------ package-lock.json | 11 +++++++++-- package.json | 4 +++- test/dns-test.js | 12 ++++++++++-- tsconfig.json | 31 +++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 tsconfig.json diff --git a/lib/dns.js b/lib/dns.js index 9f36325..69872bd 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -41,6 +41,9 @@ exports.unsupported = false; */ exports.Resolver = class Resolver { + /** @type {dns.Resolver} */ + dns; + constructor() { if (!dns.Resolver) throw new Error('DNS resolver not available.'); @@ -50,16 +53,42 @@ exports.Resolver = class Resolver { getServers() { return this.dns.getServers(); } + + /** + * @param {String[]} addrs + * @returns {Resolver} + */ + setServers(addrs) { this.dns.setServers(addrs); return this; } + + /** + * @param {String} host + * @param {String} [record=A] + * @param {Number} [timeout=5000] + * @returns {Promise} + */ + resolve(host, record, timeout) { return _resolve(this.dns, host, record, timeout); } + + /** + * @param {String} addr + * @param {Number} [timeout=5000] + * @returns {Promise} + */ + reverse(addr, timeout) { return _reverse(this.dns, addr, timeout); } + + /** + * @returns {Resolver} + */ + cancel() { this.dns.cancel(); return this; @@ -71,7 +100,7 @@ exports.Resolver = class Resolver { * @param {String} host * @param {String} [record=A] * @param {Number} [timeout=5000] - * @returns {Promise} + * @returns {Promise} */ exports.resolve = async function resolve(host, record, timeout) { @@ -86,7 +115,7 @@ exports.resolve = async function resolve(host, record, timeout) { * Reverse DNS lookup. * @param {String} addr * @param {Number} [timeout=5000] - * @returns {Promise} + * @returns {Promise} */ exports.reverse = async function reverse(addr, timeout) { @@ -102,7 +131,7 @@ exports.reverse = async function reverse(addr, timeout) { * @param {String} host * @param {Number} [family=null] * @param {Number} [timeout=5000] - * @returns {Promise} + * @returns {Promise} */ exports.lookup = async function lookup(host, family, timeout) { @@ -144,12 +173,18 @@ exports.lookup = async function lookup(host, family, timeout) { }); }; +/** + * @typedef {Object} LookupResults + * @property {String} [hostname] + * @property {String} [service] + */ + /** * Lookup name (getnameinfo). * @param {String} addr * @param {Number} [port=80] * @param {Number} [timeout=5000] - * @returns {Promise} + * @returns {Promise} */ exports.lookupService = async function lookupService(addr, port, timeout) { @@ -177,7 +212,7 @@ exports.lookupService = async function lookupService(addr, port, timeout) { /** * Resolve IPv4 address from myip.opendns.com. * @param {Number} [timeout=5000] - * @returns {Promise} + * @returns {Promise} */ exports.getIPv4 = async function getIPv4(timeout) { @@ -190,7 +225,7 @@ exports.getIPv4 = async function getIPv4(timeout) { /** * Resolve IPv6 address from myip.opendns.com. * @param {Number} [timeout=5000] - * @returns {Promise} + * @returns {Promise} */ exports.getIPv6 = async function getIPv6(timeout) { diff --git a/package-lock.json b/package-lock.json index 6366cab..3cd6224 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,10 +9,11 @@ "version": "0.1.5", "license": "MIT", "dependencies": { - "bsert": "~0.0.10" + "bsert": "~0.0.12" }, "devDependencies": { - "bmocha": "^2.1.0" + "bmocha": "^2.1.8", + "bts-type-deps": "^0.0.3" }, "engines": { "node": ">=8.0.0" @@ -38,6 +39,12 @@ "engines": { "node": ">=8.0.0" } + }, + "node_modules/bts-type-deps": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/bts-type-deps/-/bts-type-deps-0.0.3.tgz", + "integrity": "sha512-OQHGWhX5amae6Vj6ShlGaQu0sNCICgJ5YspNZPRzfR5RobrD+wjm5vkZK/J3EH5b/ymxqSWo9VkiFNpCxjaG2Q==", + "dev": true } } } diff --git a/package.json b/package.json index 6378103..0680e66 100644 --- a/package.json +++ b/package.json @@ -20,13 +20,15 @@ "main": "./lib/bdns.js", "scripts": { "lint": "eslint lib/ test/", + "lint-types": "tsc -p .", "test": "bmocha --reporter spec test/*-test.js" }, "dependencies": { "bsert": "~0.0.12" }, "devDependencies": { - "bmocha": "^2.1.8" + "bmocha": "^2.1.8", + "bts-type-deps": "^0.0.3" }, "engines": { "node": ">=8.0.0" diff --git a/test/dns-test.js b/test/dns-test.js index f31cb0e..3a5b6f0 100644 --- a/test/dns-test.js +++ b/test/dns-test.js @@ -12,6 +12,9 @@ describe('DNS', function() { const records = await dns.lookup('example.com', null, TIMEOUT); assert(records.length > 0); + const recordsA = await dns.lookup('example.com', 4, TIMEOUT); + assert(recordsA.length > 0); + const recordsAAAA = await dns.lookup('example.com', 6, TIMEOUT); assert(recordsAAAA.length > 0); }); @@ -24,7 +27,12 @@ describe('DNS', function() { }); it('should lookupService a name', async () => { - const result = await dns.lookupService('127.0.0.1', 80, TIMEOUT); - assert(result.hostname === 'localhost'); + const result = await dns.lookupService('127.0.0.1', 443, TIMEOUT / 2); + assert.strictEqual(result.hostname, 'localhost'); + }); + + it('should reverse lookup', async () => { + const result = await dns.reverse('1.1.1.1', TIMEOUT); + assert(result); }); }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..da7a94b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,31 @@ +{ + "include": [ + "lib/**/*.js" + ], + "compilerOptions": { + "rootDir": ".", + "target": "ES2020", + "lib": [ + "ES2020" + ], + + "noEmit": true, + + "allowJs": true, + "checkJs": true, + "maxNodeModuleJsDepth": 10, + + "module": "commonjs", + "moduleResolution": "node", + "resolveJsonModule": true, + + "stripInternal": true, + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": false, + + "typeRoots": [ + "node_modules/bts-type-deps/types" + ] + } +}