-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): added timeout middleware (#69)
- Loading branch information
1 parent
f75c142
commit 3f53640
Showing
6 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/app-server/src/modules/app/middlewares/timeout.middleware.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { Hono } from 'hono'; | ||
import { timeoutMiddleware } from './timeout.middleware'; | ||
import { registerErrorMiddleware } from './errors.middleware'; | ||
|
||
describe('middlewares', () => { | ||
describe('timeoutMiddleware', () => { | ||
test('when a request last longer than the config timeout, a 504 error is raised', async () => { | ||
const app = new Hono<{ Variables: { config: any } }>(); | ||
registerErrorMiddleware({ app: app as any }); | ||
|
||
app.use(async (context, next) => { | ||
context.set('config', { server: { routeTimeoutMs: 50 } }); | ||
|
||
await next(); | ||
}); | ||
|
||
app.get( | ||
'/should-timeout', | ||
timeoutMiddleware, | ||
async (context) => { | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
return context.json({ status: 'ok' }); | ||
}, | ||
); | ||
|
||
app.get( | ||
'/should-not-timeout', | ||
timeoutMiddleware, | ||
async (context) => { | ||
return context.json({ status: 'ok' }); | ||
}, | ||
); | ||
|
||
const response1 = await app.request('/should-timeout', { method: 'GET' }); | ||
|
||
expect(response1.status).to.eql(504); | ||
expect(await response1.json()).to.eql({ | ||
error: { | ||
code: 'api.timeout', | ||
message: 'The request timed out', | ||
}, | ||
}); | ||
|
||
const response2 = await app.request('/should-not-timeout', { method: 'GET' }); | ||
|
||
expect(response2.status).to.eql(200); | ||
expect(await response2.json()).to.eql({ status: 'ok' }); | ||
}); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/app-server/src/modules/app/middlewares/timeout.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { createMiddleware } from 'hono/factory'; | ||
import type { Context } from '../server.types'; | ||
import { createError } from '../../shared/errors/errors'; | ||
|
||
export const timeoutMiddleware = createMiddleware(async (context: Context, next) => { | ||
const { server: { routeTimeoutMs } } = context.get('config'); | ||
|
||
let timerId: NodeJS.Timeout | undefined; | ||
|
||
const timeoutPromise = new Promise((_, reject) => { | ||
timerId = setTimeout(() => reject( | ||
createError({ | ||
code: 'api.timeout', | ||
message: 'The request timed out', | ||
statusCode: 504, | ||
}), | ||
), routeTimeoutMs); | ||
}); | ||
|
||
try { | ||
await Promise.race([next(), timeoutPromise]); | ||
} finally { | ||
if (timerId !== undefined) { | ||
clearTimeout(timerId); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters