Create type definition for 'lookup-dns-cache' (#4051)

This commit is contained in:
Acid Chicken (硫酸鶏) 2019-01-31 17:53:49 +09:00 committed by GitHub
parent bbcdf1bb8a
commit 76569bfb08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

17
src/@types/lookup-dns-cache.d.ts vendored Normal file
View file

@ -0,0 +1,17 @@
declare module 'lookup-dns-cache' {
type IPv4 = 4;
type IPv6 = 6;
type Family = IPv4 | IPv6 | undefined;
interface IRunOptions {
family?: Family;
all?: boolean;
}
type RunCallback = (error: Error | null, address?: string | string[], family?: Family) => void;
export function lookup(hostname: string, options: IRunOptions | Family, callback: RunCallback): {} | undefined;
export function lookup(hostname: string, callback: RunCallback): {} | undefined;
}

View file

@ -3,7 +3,7 @@ const { sign } = require('http-signature');
import { URL } from 'url';
import * as debug from 'debug';
import * as crypto from 'crypto';
const { lookup } = require('lookup-dns-cache');
import { lookup, IRunOptions } from 'lookup-dns-cache';
import * as promiseAny from 'promise-any';
import config from '../../config';
@ -89,16 +89,16 @@ export default (user: ILocalUser, url: string, object: any) => new Promise(async
async function resolveAddr(domain: string) {
// v4/v6で先に取得できた方を採用する
return await promiseAny([
resolveAddrInner(domain, { ipv6: false }),
resolveAddrInner(domain, { ipv6: true })
resolveAddrInner(domain, { family: 4 }),
resolveAddrInner(domain, { family: 6 })
]);
}
function resolveAddrInner(domain: string, options = { }): Promise<string> {
function resolveAddrInner(domain: string, options: IRunOptions = {}): Promise<string> {
return new Promise((res, rej) => {
lookup(domain, options, (error: any, address: string) => {
lookup(domain, options, (error: any, address: string | string[]) => {
if (error) return rej(error);
return res(address);
return res(Array.isArray(address) ? address[0] : address);
});
});
}