From 76569bfb08b781ca2596a271408d22d9ea719385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Thu, 31 Jan 2019 17:53:49 +0900 Subject: [PATCH] Create type definition for 'lookup-dns-cache' (#4051) --- src/@types/lookup-dns-cache.d.ts | 17 +++++++++++++++++ src/remote/activitypub/request.ts | 12 ++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 src/@types/lookup-dns-cache.d.ts diff --git a/src/@types/lookup-dns-cache.d.ts b/src/@types/lookup-dns-cache.d.ts new file mode 100644 index 000000000..ae93cc539 --- /dev/null +++ b/src/@types/lookup-dns-cache.d.ts @@ -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; +} diff --git a/src/remote/activitypub/request.ts b/src/remote/activitypub/request.ts index 7e487f76e..9baab8374 100644 --- a/src/remote/activitypub/request.ts +++ b/src/remote/activitypub/request.ts @@ -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 { +function resolveAddrInner(domain: string, options: IRunOptions = {}): Promise { 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); }); }); }