From b5a069901a9545772deaa9c491f2075261da0189 Mon Sep 17 00:00:00 2001 From: Fallenbagel <98979876+Fallenbagel@users.noreply.github.com> Date: Thu, 13 Jun 2024 04:53:12 +0500 Subject: [PATCH] fix: bypass cache-able lookups when resolving localhost (#813) * fix: bypass cache-able lookups when resolving localhost * fix: bypass cacheable-lookup when resolving localhost --------- Co-authored-by: Gauthier --- server/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server/index.ts b/server/index.ts index b62080778..a9a746562 100644 --- a/server/index.ts +++ b/server/index.ts @@ -27,6 +27,7 @@ import type CacheableLookupType from 'cacheable-lookup'; import { TypeormStore } from 'connect-typeorm/out'; import cookieParser from 'cookie-parser'; import csurf from 'csurf'; +import { lookup } from 'dns'; import type { NextFunction, Request, Response } from 'express'; import express from 'express'; import * as OpenApiValidator from 'express-openapi-validator'; @@ -54,6 +55,19 @@ app const CacheableLookup = (await _importDynamic('cacheable-lookup')) .default as typeof CacheableLookupType; const cacheable = new CacheableLookup(); + + const originalLookup = cacheable.lookup; + + // if hostname is localhost use dns.lookup instead of cacheable-lookup + cacheable.lookup = (...args: any) => { + const [hostname] = args; + if (hostname === 'localhost') { + lookup(...(args as Parameters)); + } else { + originalLookup(...(args as Parameters)); + } + }; + cacheable.install(http.globalAgent); cacheable.install(https.globalAgent);