index.ts
This file
Path
src/middleware/timeout/index.ts
58 lines 路 1.6 KiB 路 TypeScript
Last changed by
Not known.
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859/** * @module * Timeout Middleware for Hono. */
import type { Context } from '../../context'import { HTTPException } from '../../http-exception'import type { MiddlewareHandler } from '../../types'
export type HTTPExceptionFunction = (context: Context) => HTTPException
const defaultTimeoutException = new HTTPException(504, { message: 'Gateway Timeout',})
/** * Timeout Middleware for Hono. * * @param {number} duration - The timeout duration in milliseconds. * @param {HTTPExceptionFunction | HTTPException} [exception=defaultTimeoutException] - The exception to throw when the timeout occurs. Can be a function that returns an HTTPException or an HTTPException object. * @returns {MiddlewareHandler} The middleware handler function. * * @example * ```ts * const app = new Hono() * * app.use( * '/long-request', * timeout(5000) // Set timeout to 5 seconds * ) * * app.get('/long-request', async (c) => { * await someLongRunningFunction() * return c.text('Completed within time limit') * }) * ``` */export const timeout = ( duration: number, exception: HTTPExceptionFunction | HTTPException = defaultTimeoutException): MiddlewareHandler => { return async function timeout(context, next) { let timer: number | undefined const timeoutPromise = new Promise<void>((_, reject) => { timer = setTimeout(() => { reject(typeof exception === 'function' ? exception(context) : exception) }, duration) as unknown as number })
try { await Promise.race([next(), timeoutPromise]) } finally { if (timer !== undefined) { clearTimeout(timer) } } }}