Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg: add type lints. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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<String[]>}
*/

resolve(host, record, timeout) {
return _resolve(this.dns, host, record, timeout);
}

/**
* @param {String} addr
* @param {Number} [timeout=5000]
* @returns {Promise<String[]>}
*/

reverse(addr, timeout) {
return _reverse(this.dns, addr, timeout);
}

/**
* @returns {Resolver}
*/

cancel() {
this.dns.cancel();
return this;
Expand All @@ -71,7 +100,7 @@ exports.Resolver = class Resolver {
* @param {String} host
* @param {String} [record=A]
* @param {Number} [timeout=5000]
* @returns {Promise}
* @returns {Promise<String[]>}
*/

exports.resolve = async function resolve(host, record, timeout) {
Expand All @@ -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<String[]>}
*/

exports.reverse = async function reverse(addr, timeout) {
Expand All @@ -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<String[]>}
*/

exports.lookup = async function lookup(host, family, timeout) {
Expand Down Expand Up @@ -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<LookupResults>}
*/

exports.lookupService = async function lookupService(addr, port, timeout) {
Expand Down Expand Up @@ -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<String | null>}
*/

exports.getIPv4 = async function getIPv4(timeout) {
Expand All @@ -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<String | null>}
*/

exports.getIPv6 = async function getIPv6(timeout) {
Expand Down
11 changes: 9 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions test/dns-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
});
31 changes: 31 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}