Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/link previews: fix title and description retrieval

Adorian Doran6beb498b256e
diff --git a/apps/server/src/services/safe_fetch.spec.ts b/apps/server/src/services/safe_fetch.spec.ts
index e0ec1aa86a..45fbc13f14 100644
--- a/apps/server/src/services/safe_fetch.spec.ts
+++ b/apps/server/src/services/safe_fetch.spec.ts
@@ -8,7 +8,7 @@ interface MockAgent {
close(): void;
}
-const { agentInstances, MockAgent } = vi.hoisted(() => {
+const { agentInstances, MockAgent, undiciFetch } = vi.hoisted(() => {
const agentInstances: MockAgent[] = [];
class MockAgent {
@@ -25,10 +25,12 @@ const { agentInstances, MockAgent } = vi.hoisted(() => {
}
}
- return { agentInstances, MockAgent };
+ return { agentInstances, MockAgent, undiciFetch: vi.fn() };
});
-vi.mock("undici", () => ({ Agent: MockAgent }));
+// safeFetch calls undici's own `fetch` (not the global one) so that it and the `Agent` it passes as
+// a dispatcher come from the same undici copy — see the comment in safe_fetch.ts.
+vi.mock("undici", () => ({ Agent: MockAgent, fetch: undiciFetch }));
import { safeFetch, validateHostResolution, validateUrl } from "./safe_fetch.js";
@@ -133,13 +135,12 @@ describe("safeFetch", () => {
beforeEach(() => {
agentInstances.length = 0;
- fetchMock = vi.fn();
- vi.stubGlobal("fetch", fetchMock);
+ fetchMock = undiciFetch;
+ fetchMock.mockReset();
// DNS literal so no actual DNS lookup is attempted.
});
afterEach(() => {
- vi.unstubAllGlobals();
vi.restoreAllMocks();
});
diff --git a/apps/server/src/services/safe_fetch.ts b/apps/server/src/services/safe_fetch.ts
index 483c825238..1cc5ff8beb 100644
--- a/apps/server/src/services/safe_fetch.ts
+++ b/apps/server/src/services/safe_fetch.ts
@@ -3,7 +3,7 @@ import net from "node:net";
import { ValidationError } from "@triliumnext/core";
import ipaddr from "ipaddr.js";
-import { Agent } from "undici";
+import { Agent, fetch as undiciFetch, type RequestInit as UndiciRequestInit, type Response as UndiciResponse } from "undici";
const FETCH_TIMEOUT_MS = 5000;
const MAX_REDIRECTS = 5;
@@ -107,13 +107,20 @@ function createPinnedLookup(validatedAddresses: dns.LookupAddress[]) {
/**
* Wraps a Response so that reading/cancelling the body automatically
- * closes the associated undici dispatcher afterwards.
+ * closes the associated undici dispatcher afterwards. Re-emits undici's response
+ * as a standard `Response` so callers stay decoupled from undici's own types.
*/
-function withDispatcherCleanup(response: Response, dispatcher: Agent): Response {
+function withDispatcherCleanup(response: UndiciResponse, dispatcher: Agent): Response {
+ const init: ResponseInit = {
+ status: response.status,
+ statusText: response.statusText,
+ headers: [...response.headers]
+ };
+
const originalBody = response.body;
if (!originalBody) {
void dispatcher.close();
- return response;
+ return new Response(null, init);
}
let closed = false;
@@ -146,11 +153,7 @@ function withDispatcherCleanup(response: Response, dispatcher: Agent): Response
}
});
- return new Response(wrappedBody, {
- status: response.status,
- statusText: response.statusText,
- headers: response.headers
- });
+ return new Response(wrappedBody, init);
}
/**
@@ -173,14 +176,17 @@ async function safeFetch(url: string, options: RequestInit = {}): Promise<Respon
});
// URL and resolved IPs are validated above and pinned via the custom dispatcher.
- // Node.js fetch supports `dispatcher` at runtime (undici), but the type isn't in RequestInit.
+ // undici's own `fetch` is used rather than the global one: the dispatcher must come from the
+ // same undici copy as the fetch consuming it. Node's built-in fetch is a *different*, newer
+ // undici (8.x on Node 26) whose internal request handler the bundled 6.x `Agent` rejects
+ // ("invalid onError method"), which would make every request here fail.
const fetchOptions = {
...options,
redirect: "manual" as const,
signal: options.signal ?? AbortSignal.timeout(FETCH_TIMEOUT_MS),
dispatcher
- };
- const response = await fetch(currentUrl, fetchOptions as RequestInit); // codeql[js/request-forgery]
+ } as UndiciRequestInit;
+ const response = await undiciFetch(currentUrl, fetchOptions); // codeql[js/request-forgery]
if (response.status >= 300 && response.status < 400) {
const location = response.headers.get("location");