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
13 changes: 10 additions & 3 deletions js/server_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 undici = require("undici");
const Log = require("logger");

const startUp = new Date();
Expand Down Expand Up @@ -98,9 +98,16 @@ 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 undici.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 });
const response = await undici.fetch(url, { dispatcher, headers: headersToSend });
if (response.ok) {
for (const header of expectedReceivedHeaders) {
const headerValue = response.headers.get(header);
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/functions/server_functions_spec.js
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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 },
Expand Down
Loading