From e1f97f76cc8cdf02fc8c02e02b3a4299d13c3fa5 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 7 Jun 2024 13:55:39 +0100 Subject: [PATCH] fix: `fetch('').catch()` triggers unhandled rejection in debugger (#15) --- src/index.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 218c0cb..8c2b5c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,6 +76,14 @@ function contentLengthFromResponseHeaders(headers: Buffer[]) { return undefined; } +async function loadFetch() { + try { + await fetch(''); + } catch (_) { + // + } +} + // A combination of https://github.com/elastic/apm-agent-nodejs and // https://github.com/gadget-inc/opentelemetry-instrumentations/blob/main/packages/opentelemetry-instrumentation-undici/src/index.ts export class FetchInstrumentation implements Instrumentation { @@ -110,7 +118,7 @@ export class FetchInstrumentation implements Instrumentation { constructor(config: FetchInstrumentationConfig) { // Force load fetch API (since it's lazy loaded in Node 18) - fetch('').catch(() => {}); + loadFetch(); this.channelSubs = []; this.meter = metrics.getMeter(this.instrumentationName, this.instrumentationVersion); this.tracer = trace.getTracer(this.instrumentationName, this.instrumentationVersion);