test: try undici 8 (#14166)

* test: try undici 8

* test: another test

* test: another test

* test: get rid of debug test

* test: don't fail on local, in dev mode or test

tests were failing on windows without this

* testing without setGlobalDispatcher

* Revert "testing without setGlobalDispatcher"

This reverts commit 6903e9f3ca.
This commit is contained in:
Barış Uşaklı
2026-04-11 18:29:45 -04:00
committed by GitHub
parent cef9a351f5
commit ae16a44ad0
2 changed files with 23 additions and 17 deletions

View File

@@ -151,7 +151,7 @@
"tinycon": "0.6.8",
"toobusy-js": "0.5.1",
"tough-cookie": "6.0.1",
"undici": "^7.10.0",
"undici": "8.0.2",
"validator": "13.15.35",
"webpack": "5.106.1",
"webpack-merge": "6.0.1",

View File

@@ -1,8 +1,7 @@
'use strict';
const dns = require('dns').promises;
require('undici'); // keep this here, needed for SSRF (see `lookup()`)
const { Agent, setGlobalDispatcher } = require('undici');
const nconf = require('nconf');
const ipaddr = require('ipaddr.js');
const { CookieJar } = require('tough-cookie');
@@ -57,6 +56,9 @@ function lookup(hostname, options, callback) {
// trusted, do regular lookup
dns.lookup(hostname, options).then((addresses) => {
callback(null, addresses);
}).catch((err) => {
console.log('lookup error', err);
callback(err);
});
return;
}
@@ -72,7 +74,24 @@ function lookup(hostname, options, callback) {
});
}
// Initialize fetch - somewhat hacky, but it's required for globalDispatcher to be available
class NodeBBAgent extends Agent {
dispatch(opts, handler) {
if (opts.headers) {
delete opts.headers['sec-fetch-mode'];
}
return super.dispatch(opts, handler);
}
}
const isDevOrTest = process.env.NODE_ENV === 'development' || process.env.CI === 'true';
const dispatcher = new NodeBBAgent({
connect: {
lookup,
rejectUnauthorized: !isDevOrTest,
},
});
setGlobalDispatcher(dispatcher);
async function call(url, method, { body, timeout, jar, ...config } = {}) {
const { ok } = await check(url);
if (!ok) {
@@ -104,21 +123,8 @@ async function call(url, method, { body, timeout, jar, ...config } = {}) {
opts.body = body;
}
}
// Workaround for https://github.com/nodejs/undici/issues/1305
if (global[Symbol.for('undici.globalDispatcher.1')] !== undefined) {
class FetchAgent extends global[Symbol.for('undici.globalDispatcher.1')].constructor {
dispatch(opts, handler) {
delete opts.headers['sec-fetch-mode'];
return super.dispatch(opts, handler);
}
}
opts.dispatcher = new FetchAgent({
connect: { lookup },
});
}
const response = await fetchImpl(url, opts);
const { headers } = response;
const contentType = headers.get('content-type');
const isJSON = contentType && jsonTest.test(contentType);