Skip to content

Commit

Permalink
fix: add workaround for nodejs/undici#1305 required to remove sec-fet…
Browse files Browse the repository at this point in the history
…ch-mode header
  • Loading branch information
oplik0 committed Jan 22, 2024
1 parent 9885f94 commit 6a9dc38
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 6a9dc38

Please sign in to comment.