From 2cb370882b5c53c3d6518f3fa061c0f0b44be727 Mon Sep 17 00:00:00 2001 From: Opliko Date: Mon, 22 Jan 2024 23:12:19 +0100 Subject: [PATCH] fix: add workaround for nodejs/undici#1305 required to remove sec-fetch-mode header --- src/request.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/request.js b/src/request.js index bb1a406ebd..fa68e203df 100644 --- a/src/request.js +++ b/src/request.js @@ -7,6 +7,17 @@ exports.jar = function () { return new CookieJar(); }; +// Initialize fetch - required for globalDispatcher to be available +fetch().catch(() => null); +// Workaround for https://github.com/nodejs/undici/issues/1305 +class FetchAgent extends global[Symbol.for('undici.globalDispatcher.1')].constructor { + dispatch(opts, handler) { + delete opts.headers['sec-fetch-mode']; + return super.dispatch(opts, handler); + } +} +const fetchAgent = new FetchAgent(); + async function call(url, method, { body, timeout, jar, ...config } = {}) { let fetchImpl = fetch; if (jar) { @@ -21,6 +32,7 @@ async function call(url, method, { body, timeout, jar, ...config } = {}) { 'content-type': 'application/json', ...config.headers, }, + dispatcher: fetchAgent, }; if (timeout > 0) { opts.signal = AbortSignal.timeout(timeout);