mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-05 16:42:55 +00:00
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import { resolveTimerTimeoutMs } from "@openclaw/normalization-core/number-coercion";
|
|
|
|
export async function withTimeout<T>(
|
|
work: (signal: AbortSignal | undefined) => Promise<T>,
|
|
timeoutMs?: number,
|
|
label?: string,
|
|
): Promise<T> {
|
|
const resolved = timeoutMs === undefined ? undefined : resolveTimerTimeoutMs(timeoutMs, 1);
|
|
if (!resolved) {
|
|
return await work(undefined);
|
|
}
|
|
|
|
const abortCtrl = new AbortController();
|
|
const timeoutError = new Error(`${label ?? "request"} timed out`);
|
|
const timer = setTimeout(() => abortCtrl.abort(timeoutError), resolved);
|
|
timer.unref?.();
|
|
|
|
let abortListener: (() => void) | undefined;
|
|
const abortPromise: Promise<never> = abortCtrl.signal.aborted
|
|
? Promise.reject(abortCtrl.signal.reason ?? timeoutError)
|
|
: new Promise((_, reject) => {
|
|
abortListener = () => reject(abortCtrl.signal.reason ?? timeoutError);
|
|
abortCtrl.signal.addEventListener("abort", abortListener, { once: true });
|
|
});
|
|
|
|
try {
|
|
return await Promise.race([work(abortCtrl.signal), abortPromise]);
|
|
} finally {
|
|
clearTimeout(timer);
|
|
if (abortListener) {
|
|
abortCtrl.signal.removeEventListener("abort", abortListener);
|
|
}
|
|
}
|
|
}
|