






















@@ -1,3 +1,16 @@
1+import { createSubsystemLogger } from "../logging/subsystem.js";
2+3+const log = createSubsystemLogger("fetch-timeout");
4+const LOG_URL_MAX_CHARS = 500;
5+const URL_SECRET_SUFFIX_PATTERN = /[?#]/;
6+7+type TimeoutAbortSignalParams = {
8+timeoutMs?: number;
9+signal?: AbortSignal;
10+operation?: string;
11+url?: string;
12+};
13+114/**
215 * Relay abort without forwarding the Event argument as the abort reason.
316 * Using .bind() avoids closure scope capture (memory leak prevention).
@@ -11,7 +24,56 @@ export function bindAbortRelay(controller: AbortController): () => void {
1124return relayAbort.bind(controller);
1225}
132614-export function buildTimeoutAbortSignal(params: { timeoutMs?: number; signal?: AbortSignal }): {
27+function sanitizeTimeoutLogUrl(rawUrl: string | undefined): string | undefined {
28+const trimmed = rawUrl?.trim();
29+if (!trimmed) {
30+return undefined;
31+}
32+try {
33+const parsed = new URL(trimmed);
34+parsed.username = "";
35+parsed.password = "";
36+parsed.search = "";
37+parsed.hash = "";
38+const value = parsed.toString();
39+return value.length > LOG_URL_MAX_CHARS ? `${value.slice(0, LOG_URL_MAX_CHARS)}...` : value;
40+} catch {
41+const withoutQueryOrHash = trimmed.split(URL_SECRET_SUFFIX_PATTERN, 1)[0] ?? "";
42+const cleaned = withoutQueryOrHash
43+.replace(/[\r\n\u2028\u2029]+/g, " ")
44+.replace(/\p{Cc}+/gu, " ")
45+.replace(/\s+/g, " ")
46+.trim();
47+if (!cleaned) {
48+return undefined;
49+}
50+return cleaned.length > LOG_URL_MAX_CHARS
51+ ? `${cleaned.slice(0, LOG_URL_MAX_CHARS)}...`
52+ : cleaned;
53+}
54+}
55+56+function abortDueToTimeout(
57+controller: AbortController,
58+timeoutMs: number,
59+startedAtMs: number,
60+operation?: string,
61+url?: string,
62+) {
63+if (controller.signal.aborted) {
64+return;
65+}
66+const sanitizedUrl = sanitizeTimeoutLogUrl(url);
67+log.warn("fetch timeout reached; aborting operation", {
68+ timeoutMs,
69+elapsedMs: Math.max(0, Date.now() - startedAtMs),
70+ ...(operation ? { operation } : {}),
71+ ...(sanitizedUrl ? { url: sanitizedUrl } : {}),
72+});
73+controller.abort();
74+}
75+76+export function buildTimeoutAbortSignal(params: TimeoutAbortSignalParams): {
1577signal?: AbortSignal;
1678cleanup: () => void;
1779} {
@@ -24,7 +86,16 @@ export function buildTimeoutAbortSignal(params: { timeoutMs?: number; signal?: A
2486}
25872688const controller = new AbortController();
27-const timeoutId = setTimeout(controller.abort.bind(controller), timeoutMs);
89+const normalizedTimeoutMs = Math.max(1, Math.floor(timeoutMs));
90+const timeoutId = setTimeout(
91+abortDueToTimeout,
92+normalizedTimeoutMs,
93+controller,
94+normalizedTimeoutMs,
95+Date.now(),
96+params.operation,
97+params.url,
98+);
2899const onAbort = bindAbortRelay(controller);
29100if (signal) {
30101if (signal.aborted) {
@@ -63,6 +134,8 @@ export async function fetchWithTimeout(
63134): Promise<Response> {
64135const { signal, cleanup } = buildTimeoutAbortSignal({
65136timeoutMs: Math.max(1, timeoutMs),
137+operation: "fetchWithTimeout",
138+ url,
66139});
67140try {
68141return await fetchFn(url, { ...init, signal });
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。