Skip to content

Commit 040f14b

Browse files
committed
fix(browser): cap node runtime timeouts
1 parent 8c53d10 commit 040f14b

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
2+
import { describe, expect, it, vi } from "vitest";
3+
import { withTimeout } from "./sdk-node-runtime.js";
4+
5+
describe("withTimeout", () => {
6+
it("caps oversized timeouts before arming the abort timer", async () => {
7+
const timeoutSpy = vi
8+
.spyOn(globalThis, "setTimeout")
9+
.mockReturnValue(1 as unknown as ReturnType<typeof setTimeout>);
10+
vi.spyOn(globalThis, "clearTimeout").mockImplementation(() => undefined);
11+
12+
await expect(
13+
withTimeout(async () => "ok", Number.MAX_SAFE_INTEGER, "browser request"),
14+
).resolves.toBe("ok");
15+
16+
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
17+
});
18+
});

extensions/browser/src/sdk-node-runtime.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ export {
2323
type LazyPluginServiceHandle,
2424
} from "openclaw/plugin-sdk/plugin-runtime";
2525
export { defaultRuntime } from "openclaw/plugin-sdk/runtime-env";
26+
import { clampTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
2627

2728
function normalizeTimeoutMs(timeoutMs: number | undefined): number | undefined {
28-
return typeof timeoutMs === "number" && Number.isFinite(timeoutMs)
29-
? Math.max(1, Math.floor(timeoutMs))
30-
: undefined;
29+
return clampTimerTimeoutMs(timeoutMs);
3130
}
3231

3332
function createTimeoutAbortSignal(timeoutMs: number, label: string | undefined) {
@@ -38,7 +37,10 @@ function createTimeoutAbortSignal(timeoutMs: number, label: string | undefined)
3837
return { controller, error, timer };
3938
}
4039

41-
function waitForAbort(signal: AbortSignal, fallback: Error): {
40+
function waitForAbort(
41+
signal: AbortSignal,
42+
fallback: Error,
43+
): {
4244
promise: Promise<never>;
4345
cleanup: () => void;
4446
} {

0 commit comments

Comments
 (0)