From a987e8800be7c97ad2dce3fb8b2a453a4bb07497 Mon Sep 17 00:00:00 2001 From: Mateus Saggin Date: Mon, 27 Jul 2026 23:35:07 -0300 Subject: [PATCH 1/2] fix: guard global.metrics in singleFlightMiddleware --- src/HttpClient/middlewares/inflight.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/HttpClient/middlewares/inflight.ts b/src/HttpClient/middlewares/inflight.ts index 4197e010b..e4aa22708 100644 --- a/src/HttpClient/middlewares/inflight.ts +++ b/src/HttpClient/middlewares/inflight.ts @@ -17,10 +17,13 @@ export const singleFlightMiddleware = async (ctx: MiddlewareContext, next: () => // cancel any request ctx.config.cancelToken = undefined - if (!metricsAdded) { + // global.metrics is only initialized by startApp() in the IO service + // runtime. Standalone consumers of HttpClient (e.g. the toolbelt CLI) + // run without it, so guard the bare global reference. + if (!metricsAdded && typeof metrics !== 'undefined') { metrics.addOnFlushMetric(() => ({ name: 'node-vtex-api-inflight-map-size', - size: inflight.entries.length, + size: inflight.size, })) metricsAdded = true } @@ -41,11 +44,9 @@ export const singleFlightMiddleware = async (ctx: MiddlewareContext, next: () => cacheHit: ctx.cacheHit!, response: ctx.response!, }) - } - catch (err) { + } catch (err) { reject(err) - } - finally { + } finally { inflight.delete(key) } }) @@ -54,6 +55,7 @@ export const singleFlightMiddleware = async (ctx: MiddlewareContext, next: () => } } -export const inflightURL: InflightKeyGenerator = ({baseURL, url}: RequestConfig) => baseURL! + url! +export const inflightURL: InflightKeyGenerator = ({ baseURL, url }: RequestConfig) => baseURL! + url! -export const inflightUrlWithQuery: InflightKeyGenerator = ({baseURL, url, params}: RequestConfig) => baseURL! + url! + stringify(params, {arrayFormat: 'repeat', addQueryPrefix: true}) +export const inflightUrlWithQuery: InflightKeyGenerator = ({ baseURL, url, params }: RequestConfig) => + baseURL! + url! + stringify(params, { arrayFormat: 'repeat', addQueryPrefix: true }) From e647cee9ff2e012c9626a08d51899f2118df4480 Mon Sep 17 00:00:00 2001 From: Mateus Saggin Date: Mon, 27 Jul 2026 23:39:54 -0300 Subject: [PATCH 2/2] Undo lint --- src/HttpClient/middlewares/inflight.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/HttpClient/middlewares/inflight.ts b/src/HttpClient/middlewares/inflight.ts index e4aa22708..5b17df1d1 100644 --- a/src/HttpClient/middlewares/inflight.ts +++ b/src/HttpClient/middlewares/inflight.ts @@ -44,9 +44,11 @@ export const singleFlightMiddleware = async (ctx: MiddlewareContext, next: () => cacheHit: ctx.cacheHit!, response: ctx.response!, }) - } catch (err) { + } + catch (err) { reject(err) - } finally { + } + finally { inflight.delete(key) } }) @@ -55,7 +57,6 @@ export const singleFlightMiddleware = async (ctx: MiddlewareContext, next: () => } } -export const inflightURL: InflightKeyGenerator = ({ baseURL, url }: RequestConfig) => baseURL! + url! +export const inflightURL: InflightKeyGenerator = ({baseURL, url}: RequestConfig) => baseURL! + url! -export const inflightUrlWithQuery: InflightKeyGenerator = ({ baseURL, url, params }: RequestConfig) => - baseURL! + url! + stringify(params, { arrayFormat: 'repeat', addQueryPrefix: true }) +export const inflightUrlWithQuery: InflightKeyGenerator = ({baseURL, url, params}: RequestConfig) => baseURL! + url! + stringify(params, {arrayFormat: 'repeat', addQueryPrefix: true})