diff --git a/src/line/probe.ts b/src/line/probe.ts index d5f7755cd2b..0da983de465 100644 --- a/src/line/probe.ts +++ b/src/line/probe.ts @@ -1,5 +1,6 @@ import { messagingApi } from "@line/bot-sdk"; import type { LineProbeResult } from "./types.js"; +import { withTimeout } from "../utils/with-timeout.js"; export async function probeLineBot( channelAccessToken: string, @@ -30,18 +31,3 @@ export async function probeLineBot( return { ok: false, error: message }; } } - -function withTimeout(promise: Promise, timeoutMs: number): Promise { - if (!timeoutMs || timeoutMs <= 0) { - return promise; - } - let timer: NodeJS.Timeout | null = null; - const timeout = new Promise((_, reject) => { - timer = setTimeout(() => reject(new Error("timeout")), timeoutMs); - }); - return Promise.race([promise, timeout]).finally(() => { - if (timer) { - clearTimeout(timer); - } - }); -} diff --git a/src/slack/probe.ts b/src/slack/probe.ts index fa67b1f191e..22857ca2bc6 100644 --- a/src/slack/probe.ts +++ b/src/slack/probe.ts @@ -1,4 +1,5 @@ import type { BaseProbeResult } from "../channels/plugins/types.js"; +import { withTimeout } from "../utils/with-timeout.js"; import { createSlackWebClient } from "./client.js"; export type SlackProbe = BaseProbeResult & { @@ -8,21 +9,6 @@ export type SlackProbe = BaseProbeResult & { team?: { id?: string; name?: string }; }; -function withTimeout(promise: Promise, timeoutMs: number): Promise { - if (!timeoutMs || timeoutMs <= 0) { - return promise; - } - let timer: NodeJS.Timeout | null = null; - const timeout = new Promise((_, reject) => { - timer = setTimeout(() => reject(new Error("timeout")), timeoutMs); - }); - return Promise.race([promise, timeout]).finally(() => { - if (timer) { - clearTimeout(timer); - } - }); -} - export async function probeSlack(token: string, timeoutMs = 2500): Promise { const client = createSlackWebClient(token); const start = Date.now(); diff --git a/src/utils/with-timeout.ts b/src/utils/with-timeout.ts new file mode 100644 index 00000000000..7a2d8361df1 --- /dev/null +++ b/src/utils/with-timeout.ts @@ -0,0 +1,14 @@ +export function withTimeout(promise: Promise, timeoutMs: number): Promise { + if (!timeoutMs || timeoutMs <= 0) { + return promise; + } + let timer: NodeJS.Timeout | null = null; + const timeout = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error("timeout")), timeoutMs); + }); + return Promise.race([promise, timeout]).finally(() => { + if (timer) { + clearTimeout(timer); + } + }); +}