From 29b555fb7c988ae0078a8838911a98d065ccffd1 Mon Sep 17 00:00:00 2001 From: mei23 Date: Mon, 18 Sep 2023 09:54:02 +0900 Subject: [PATCH] =?UTF-8?q?Node=20v20=E3=81=A7=E5=8B=95=E3=81=8B=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=81=AE=E3=82=92=E4=BF=AE=E6=AD=A3=20Resolve=20#2480?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + pnpm-lock.yaml | 8 ++++++++ src/misc/dns.ts | 33 --------------------------------- src/misc/fetch.ts | 12 ++++++++---- 4 files changed, 17 insertions(+), 37 deletions(-) delete mode 100644 src/misc/dns.ts diff --git a/package.json b/package.json index 62459a2190..a4e32483b2 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "bootstrap": "4.6.2", "bootstrap-vue": "2.23.1", "bull": "4.10.4", + "cacheable-lookup": "6.1.0", "cafy": "15.2.1", "cbor": "8.1.0", "chalk": "4.1.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 07a013e27a..ee7d50279f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -75,6 +75,9 @@ dependencies: bull: specifier: 4.10.4 version: 4.10.4 + cacheable-lookup: + specifier: 6.1.0 + version: 6.1.0 cafy: specifier: 15.2.1 version: 15.2.1 @@ -2793,6 +2796,11 @@ packages: engines: {node: '>=10.6.0'} dev: false + /cacheable-lookup@6.1.0: + resolution: {integrity: sha512-KJ/Dmo1lDDhmW2XDPMo+9oiy/CeqosPguPCrgcVzKyZrL6pM1gU2GmPY/xo6OQPTUaA/c0kwHuywB4E6nmT9ww==} + engines: {node: '>=10.6.0'} + dev: false + /cacheable-request@7.0.2: resolution: {integrity: sha512-pouW8/FmiPQbuGpkXQ9BAPv/Mo5xDGANgSNXzTzJ8DrKGuXOssM4wIQRjfanNRh3Yu5cfYPvcorqbhg2KIJtew==} engines: {node: '>=8'} diff --git a/src/misc/dns.ts b/src/misc/dns.ts deleted file mode 100644 index 149aeccd4c..0000000000 --- a/src/misc/dns.ts +++ /dev/null @@ -1,33 +0,0 @@ -import * as dns from 'dns'; - -type LookupOneCallback = (err: Error | null, address?: string, family?: number) => void; - -export function lookup(hostname: string, options: { family?: number, hints?: number }, callback: LookupOneCallback) { - if (options.family === 4) { - dns.promises.resolve4(hostname).then(addresses => { - callback(null, pickRandom(addresses), 4); - }).catch(err => { - callback(err); - }); - } else if (options.family === 6) { - dns.promises.resolve6(hostname).then(addresses => { - callback(null, pickRandom(addresses), 6); - }).catch(err => { - callback(err); - }); - } else { - dns.promises.resolve4(hostname).then(addresses => { - callback(null, pickRandom(addresses), 4); - }).catch(err4 => { - dns.promises.resolve6(hostname).then(addresses => { - callback(null, pickRandom(addresses), 6); - }).catch(err6 => { - callback(err6); - }); - }); - } -} - -function pickRandom(s: string[]) { - return s[Math.floor(Math.random() * s.length)]; -} diff --git a/src/misc/fetch.ts b/src/misc/fetch.ts index 4863bdbf66..83b46bb998 100644 --- a/src/misc/fetch.ts +++ b/src/misc/fetch.ts @@ -1,7 +1,7 @@ import * as http from 'http'; import * as https from 'https'; -import { lookup } from './dns'; import fetch from 'node-fetch'; +import CacheableLookup from 'cacheable-lookup'; import { HttpProxyAgent } from 'http-proxy-agent'; import { HttpsProxyAgent } from 'https-proxy-agent'; import config from '../config'; @@ -75,7 +75,11 @@ function objectAssignWithLcKey(a: Record, b: Record, b: Record