From c1d45bc6d078fb9dbdebcd8bf47e5d073c113b22 Mon Sep 17 00:00:00 2001 From: Karsten Hassel Date: Sun, 5 Apr 2026 00:30:26 +0200 Subject: [PATCH 1/2] fix cors function for alpine linux --- js/server_functions.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/js/server_functions.js b/js/server_functions.js index 9d4aff4704..171b813fe1 100644 --- a/js/server_functions.js +++ b/js/server_functions.js @@ -2,7 +2,7 @@ const dns = require("node:dns"); const fs = require("node:fs"); const path = require("node:path"); const ipaddr = require("ipaddr.js"); -const { Agent } = require("undici"); +const { fetch, Agent } = require("undici"); const Log = require("logger"); const startUp = new Date(); @@ -98,7 +98,14 @@ async function cors (req, res) { } // Pin the validated IP — fetch() reuses it instead of doing its own DNS lookup - const dispatcher = new Agent({ connect: { lookup: (_h, _o, cb) => cb(null, address, family) } }); + const dispatcher = new Agent({ + connect: { + lookup: (_h, _o, cb) => { + const addresses = [{ address: address, family: family }]; + process.nextTick(() => cb(null, addresses)); + } + } + }); const response = await fetch(url, { dispatcher, headers: headersToSend }); if (response.ok) { From 1fea5a35c4af9744288f0c4b593223107419bc0f Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Sun, 5 Apr 2026 01:05:45 +0200 Subject: [PATCH 2/2] fix(cors): fix tests broken by destructured undici import --- js/server_functions.js | 6 +++--- tests/unit/functions/server_functions_spec.js | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/js/server_functions.js b/js/server_functions.js index 171b813fe1..9650257c27 100644 --- a/js/server_functions.js +++ b/js/server_functions.js @@ -2,7 +2,7 @@ const dns = require("node:dns"); const fs = require("node:fs"); const path = require("node:path"); const ipaddr = require("ipaddr.js"); -const { fetch, Agent } = require("undici"); +const undici = require("undici"); const Log = require("logger"); const startUp = new Date(); @@ -98,7 +98,7 @@ async function cors (req, res) { } // Pin the validated IP — fetch() reuses it instead of doing its own DNS lookup - const dispatcher = new Agent({ + const dispatcher = new undici.Agent({ connect: { lookup: (_h, _o, cb) => { const addresses = [{ address: address, family: family }]; @@ -107,7 +107,7 @@ async function cors (req, res) { } }); - const response = await fetch(url, { dispatcher, headers: headersToSend }); + const response = await undici.fetch(url, { dispatcher, headers: headersToSend }); if (response.ok) { for (const header of expectedReceivedHeaders) { const headerValue = response.headers.get(header); diff --git a/tests/unit/functions/server_functions_spec.js b/tests/unit/functions/server_functions_spec.js index ebb1d67893..779daaf08e 100644 --- a/tests/unit/functions/server_functions_spec.js +++ b/tests/unit/functions/server_functions_spec.js @@ -1,9 +1,10 @@ -// Tests use vi.spyOn on shared module objects (dns, global.fetch). +// Tests use vi.spyOn on shared module objects (dns, undici). // vi.spyOn modifies the object property directly on the cached module instance, so it // is intercepted by server_functions.js regardless of the Module.prototype.require override // in vitest-setup.js. restoreAllMocks:true auto-restores spies, but may reuse the same // spy instance — mockClear() is called explicitly in beforeEach to reset call history. const dns = require("node:dns"); +const undici = require("undici"); const { cors, getUserAgent, replaceSecretPlaceholder } = require("#server_functions"); describe("server_functions tests", () => { @@ -41,7 +42,7 @@ describe("server_functions tests", () => { // vi.spyOn may return the same spy instance across tests when restoreAllMocks // restores-but-reuses; mockClear() explicitly resets call history each time. - fetchSpy = vi.spyOn(global, "fetch"); + fetchSpy = vi.spyOn(undici, "fetch"); fetchSpy.mockClear(); fetchSpy.mockImplementation(() => Promise.resolve({ headers: { get: fetchResponseHeadersGet },