Files
MyIP/common/fetch-with-timeout.js
jason5ng32andClaude Opus 5.5 c29e48687b Refactor(layout): split backend-only code out of common/, group frontend utils
common/ had become the backend's library: 30 of its 41 modules were
imported by nothing in the frontend, so the name no longer said what
belonged there. It now holds only what the frontend or the build imports
too; backend-only code moved to server/, flat except for datasets/ (the
dataset engine and its readers). The *-db/ dataset directories stay in
common/: self-hosting docs and Docker volumes mount those paths.

frontend/utils/ keeps its app-wide modules at the root and gains ip/
(IP / ASN shaping and validation), report/ (the report pipeline) and
features/ (the pure layer of a single feature). Paths only — no logic
changes. The Dockerfile copies server/, and the AGENTS.md files say where
a new module goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:10:02 +08:00

68 lines
2.8 KiB
JavaScript

// Single-source fetch wrapper with an AbortController-based timeout.
// Shared by both front-end (imported via a thin re-export at
// frontend/utils/fetch-with-timeout.js) and back-end API handlers.
//
// Two named exports:
// - fetchWithTimeout(url, init) — default 5s timeout. Front-end default,
// suits client-to-server latency.
// - fetchUpstream(url, init) — default 8s timeout. Back-end preset for
// server-to-server calls into third-party IP / ASN / map providers.
//
// Both accept a `timeoutMs` in init to override, and both chain any
// caller-provided `signal` so aborting either one aborts the underlying
// fetch. Timeouts surface as AbortError — callers already handle that
// via try/catch. Written without AbortSignal.timeout() for broader
// browser compatibility.
export async function fetchWithTimeout(url, init = {}) {
const { timeoutMs = 5000, ...rest } = init;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
const callerSignal = rest.signal;
if (callerSignal) {
if (callerSignal.aborted) {
controller.abort();
} else {
callerSignal.addEventListener('abort', () => controller.abort(), { once: true });
}
}
try {
return await fetch(url, { ...rest, signal: controller.signal });
} finally {
clearTimeout(timer);
}
}
// Optional User-Agent for fetchUpstream calls. Backend boot injects a
// project-identifying UA (see server/upstream-ua.js) because some upstream
// WAFs hard-block undici's default `User-Agent: node`. Injection keeps this
// file browser-safe: the frontend bundle never touches fs / process.
let upstreamUserAgent = null;
export const setUpstreamUserAgent = (ua) => {
upstreamUserAgent = ua || null;
};
// True when caller-supplied headers already carry a User-Agent (any casing;
// plain object or Headers instance) — pass-through handlers forward the
// visitor's headers and must not have them overridden or duplicated. The
// array-of-pairs headers form isn't used in this repo; treat it as opting
// out of injection rather than attempting a merge.
const headersCarryUA = (h) => {
if (!h) return false;
if (typeof h.has === 'function') return h.has('user-agent');
if (Array.isArray(h)) return true;
return Object.keys(h).some((k) => k.toLowerCase() === 'user-agent');
};
// Back-end preset: 8s default for server-to-server upstream calls, plus the
// default User-Agent above unless the caller already set one.
export const fetchUpstream = (url, init = {}) => {
const headers = upstreamUserAgent && !headersCarryUA(init.headers)
? { ...init.headers, 'User-Agent': upstreamUserAgent }
: init.headers;
return fetchWithTimeout(url, { timeoutMs: 8000, ...init, headers });
};