From 04ae023bf301cd68b458fecf8d37d0629bd6769e Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 27 Apr 2026 01:56:21 -0400 Subject: [PATCH] feat: add timeout utility --- src/utils/timeout.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/utils/timeout.ts diff --git a/src/utils/timeout.ts b/src/utils/timeout.ts new file mode 100644 index 0000000..35a053b --- /dev/null +++ b/src/utils/timeout.ts @@ -0,0 +1,23 @@ +export function withTimeout(promise: Promise, ms: number, message: string): Promise { + let timer: any; + return Promise.race([ + promise, + new Promise((_, reject) => { + timer = setTimeout(() => reject(message), ms); + }), + ]).finally(() => clearTimeout(timer)); +} + +export function parseTimeout(input: string): number { + const match = input.match(/^(\d+)(ms|s|m)$/); + if (!match) return 30000; + const [, value, unit] = match; + switch (unit) { + case "ms": + return +value; + case "s": + return +value * 1000; + case "m": + return +value * 60000; + } +}