Skip to content

Commit 9b97add

Browse files
fix cors function for alpine linux (#4091)
I provide docker images with alpine linux and tested the new cors approach. It didn't work because after calling ```js const dispatcher = new Agent({ connect: { lookup: (_h, _o, cb) => cb(null, address, family) } }); ``` the dispatcher variable was undefined. This PR solves this and I tested this under debian too. The mix of internal fetch and newer undici did not work and alpine needs additionally the `process.nextTick`. --------- Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
1 parent 0da343b commit 9b97add

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

js/server_functions.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const dns = require("node:dns");
22
const fs = require("node:fs");
33
const path = require("node:path");
44
const ipaddr = require("ipaddr.js");
5-
const { Agent } = require("undici");
5+
const undici = require("undici");
66
const Log = require("logger");
77

88
const startUp = new Date();
@@ -98,9 +98,16 @@ async function cors (req, res) {
9898
}
9999

100100
// Pin the validated IP — fetch() reuses it instead of doing its own DNS lookup
101-
const dispatcher = new Agent({ connect: { lookup: (_h, _o, cb) => cb(null, address, family) } });
101+
const dispatcher = new undici.Agent({
102+
connect: {
103+
lookup: (_h, _o, cb) => {
104+
const addresses = [{ address: address, family: family }];
105+
process.nextTick(() => cb(null, addresses));
106+
}
107+
}
108+
});
102109

103-
const response = await fetch(url, { dispatcher, headers: headersToSend });
110+
const response = await undici.fetch(url, { dispatcher, headers: headersToSend });
104111
if (response.ok) {
105112
for (const header of expectedReceivedHeaders) {
106113
const headerValue = response.headers.get(header);

tests/unit/functions/server_functions_spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
// Tests use vi.spyOn on shared module objects (dns, global.fetch).
1+
// Tests use vi.spyOn on shared module objects (dns, undici).
22
// vi.spyOn modifies the object property directly on the cached module instance, so it
33
// is intercepted by server_functions.js regardless of the Module.prototype.require override
44
// in vitest-setup.js. restoreAllMocks:true auto-restores spies, but may reuse the same
55
// spy instance — mockClear() is called explicitly in beforeEach to reset call history.
66
const dns = require("node:dns");
7+
const undici = require("undici");
78
const { cors, getUserAgent, replaceSecretPlaceholder } = require("#server_functions");
89

910
describe("server_functions tests", () => {
@@ -41,7 +42,7 @@ describe("server_functions tests", () => {
4142

4243
// vi.spyOn may return the same spy instance across tests when restoreAllMocks
4344
// restores-but-reuses; mockClear() explicitly resets call history each time.
44-
fetchSpy = vi.spyOn(global, "fetch");
45+
fetchSpy = vi.spyOn(undici, "fetch");
4546
fetchSpy.mockClear();
4647
fetchSpy.mockImplementation(() => Promise.resolve({
4748
headers: { get: fetchResponseHeadersGet },

0 commit comments

Comments
 (0)