Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions js/http_fetcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { EventEmitter } = require("node:events");
const { Agent } = require("undici");
const { fetch: undiciFetch, Agent } = require("undici");
const Log = require("logger");
const { getUserAgent } = require("#server_functions");

Expand Down Expand Up @@ -263,8 +263,13 @@ class HTTPFetcher extends EventEmitter {
const timeoutId = setTimeout(() => controller.abort(), this.timeout);

try {
const response = await fetch(this.url, {
...this.getRequestOptions(),
const requestOptions = this.getRequestOptions();
// Use undici.fetch when a custom dispatcher is present (e.g. selfSignedCert),
// because Node's global fetch and npm undici@8 Agents are incompatible.
// For regular requests, use globalThis.fetch so MSW and other interceptors work.
const fetchFn = requestOptions.dispatcher ? undiciFetch : globalThis.fetch;
const response = await fetchFn(this.url, {
...requestOptions,
signal: controller.signal
});

Expand Down
29 changes: 29 additions & 0 deletions tests/unit/functions/http_fetcher_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,32 @@ describe("fetch() method", () => {
expect(errorInfo.errorType).toBe("NETWORK_ERROR");
});
});

describe("selfSignedCert dispatcher", () => {
const { Agent } = require("undici");

it("should set rejectUnauthorized=false when selfSignedCert is true", () => {
fetcher = new HTTPFetcher(TEST_URL, {
reloadInterval: 60000,
selfSignedCert: true
});

const options = fetcher.getRequestOptions();

expect(options.dispatcher).toBeInstanceOf(Agent);
const agentOptionsSymbol = Object.getOwnPropertySymbols(options.dispatcher).find((s) => s.description === "options");
const dispatcherOptions = options.dispatcher[agentOptionsSymbol];
expect(dispatcherOptions.connect.rejectUnauthorized).toBe(false);
});

it("should not set a dispatcher when selfSignedCert is false", () => {
fetcher = new HTTPFetcher(TEST_URL, {
reloadInterval: 60000,
selfSignedCert: false
});

const options = fetcher.getRequestOptions();

expect(options.dispatcher).toBeUndefined();
});
});
Loading