fix(browser): cap node runtime timeouts · openclaw/openclaw@040f14b
steipete
·
2026-05-30
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
|
| 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 | +}); |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -23,11 +23,10 @@ export {
|
23 | 23 | type LazyPluginServiceHandle, |
24 | 24 | } from "openclaw/plugin-sdk/plugin-runtime"; |
25 | 25 | export { defaultRuntime } from "openclaw/plugin-sdk/runtime-env"; |
| 26 | +import { clampTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime"; |
26 | 27 | |
27 | 28 | 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); |
31 | 30 | } |
32 | 31 | |
33 | 32 | function createTimeoutAbortSignal(timeoutMs: number, label: string | undefined) { |
@@ -38,7 +37,10 @@ function createTimeoutAbortSignal(timeoutMs: number, label: string | undefined)
|
38 | 37 | return { controller, error, timer }; |
39 | 38 | } |
40 | 39 | |
41 | | -function waitForAbort(signal: AbortSignal, fallback: Error): { |
| 40 | +function waitForAbort( |
| 41 | +signal: AbortSignal, |
| 42 | +fallback: Error, |
| 43 | +): { |
42 | 44 | promise: Promise<never>; |
43 | 45 | cleanup: () => void; |
44 | 46 | } { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。