Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/middleware/timeout/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('Timeout API', () => {
const exception500: HTTPExceptionFunction = (context: Context) =>
new HTTPException(500, { message: `Internal Server Error at ${context.req.path}` })
app.use('/slow-endpoint/error', timeout(1200, exception500))
app.use('/slow-endpoint/override', timeout(1300))
app.use('/normal-endpoint', timeout(1000))

app.get('/slow-endpoint', async (c) => {
Expand All @@ -35,6 +36,11 @@ describe('Timeout API', () => {
return c.text('This should not show up')
})

app.get('/slow-endpoint/override', timeout(1100), async (c) => {
await new Promise((resolve) => setTimeout(resolve, 1200))
return c.text('This should not show up')
})

app.get('/normal-endpoint', async (c) => {
await new Promise((resolve) => setTimeout(resolve, 900))
return c.text('This should not show up')
Expand All @@ -61,6 +67,13 @@ describe('Timeout API', () => {
expect(await res.text()).toContain('Internal Server Error at /slow-endpoint/error')
})

it('Old timeout should be overriden', async () => {
const res = await app.request('http://localhost/slow-endpoint/override')
expect(res).not.toBeNull()
expect(res.status).toBe(504)
expect(await res.text()).toContain('Gateway Timeout')
})

it('No Timeout should pass', async () => {
const res = await app.request('http://localhost/normal-endpoint')
expect(res).not.toBeNull()
Expand Down
16 changes: 14 additions & 2 deletions src/middleware/timeout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,37 @@ const defaultTimeoutException = new HTTPException(504, {
* await someLongRunningFunction()
* return c.text('Completed within time limit')
* })
*
* app.use('/user', timeout(5000)) // Set a generic timeout for all user routes
*
* app.use('/user/export', timeout(30000), async (c, next) => {
* await someLongRunningFunction()
* return c.text('Completed within 30 seconds')
* })
* ```
*/
export const timeout = (
duration: number,
exception: HTTPExceptionFunction | HTTPException = defaultTimeoutException
): MiddlewareHandler => {
): MiddlewareHandler<{ Variables: { timeoutTimer: number | undefined } }> => {
return async function timeout(context, next) {
let timer: number | undefined
let timer = context.get('timeoutTimer')
if (timer !== undefined) {
clearTimeout(timer)
}
const timeoutPromise = new Promise<void>((_, reject) => {
timer = setTimeout(() => {
reject(typeof exception === 'function' ? exception(context) : exception)
}, duration) as unknown as number
context.set('timeoutTimer', timer)
})

try {
await Promise.race([next(), timeoutPromise])
} finally {
if (timer !== undefined) {
clearTimeout(timer)
context.set('timeoutTimer', undefined)
}
}
}
Expand Down
Loading