From 03421270582e60f7e23ca3d150e6c37b553f7735 Mon Sep 17 00:00:00 2001 From: fallenbagel <98979876+Fallenbagel@users.noreply.github.com> Date: Thu, 13 Jun 2024 01:13:07 +0500 Subject: [PATCH] fix: bypass cache-able lookups when resolving localhost --- server/index.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/server/index.ts b/server/index.ts index b6208077..6d970c80 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,22 @@ 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 = ( + hostname: string, + options: any, + callback?: any + ): void => { + if (hostname === 'localhost') { + return lookup(hostname, options); + } else { + return originalLookup(hostname, options, callback); + } + }; + cacheable.install(http.globalAgent); cacheable.install(https.globalAgent);