Skip to content

Commit

Permalink
🔧 fix: server timing hang
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed May 2, 2024
1 parent 588fcec commit ff73f80
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.16 - 2 May 2024
Bug fix:
- [ratelimit#28](https://github.com/rayriffy/elysia-rate-limit/issues/28) trace hang when using server-timing with rate-limit plugin

# 1.0.15 - 27 Apr 2024
Feature:
- add `redirect` function to `Context`
Expand Down
Binary file modified bun.lockb
Binary file not shown.
40 changes: 28 additions & 12 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { Elysia, t } from '../src'
import { Elysia } from '../src'
import { rateLimit } from 'elysia-rate-limit'
import { req } from '../test/utils'
import { serverTiming } from '@elysiajs/server-timing'

const app = new Elysia({ name: 'Example' })
.get(
'/',
async (c) => {
const id = c.query.id
const cookie = c.cookie
return { cookie, id }
}
)
const app = new Elysia()
.use((app) => {
app.use(
// @ts-expect-error
serverTiming()
)

app.use(
// @ts-expect-error
rateLimit({
max: 3
})
)

return app
})
.get('/', 'hello')
.listen(3000)

await Promise.all([
app.handle(req('/')),
app.handle(req('/')),
app.handle(req('/'))
])

app.handle(req('/'))
.then((x) => x.headers)
// .then(console.log)
.then((x) => x.text())
.then(console.log)
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@
"release:local": "npm run build && npm run test && npm publish"
},
"dependencies": {
"@elysiajs/server-timing": "^1.0.2",
"@sinclair/typebox": "^0.32.15",
"cookie": "^0.6.0",
"elysia": "1.0.15",
"elysia-rate-limit": "^3.2.2",
"eventemitter3": "^5.0.1",
"fast-decode-uri-component": "^1.0.1",
"fast-querystring": "^1.1.2",
Expand Down
17 changes: 8 additions & 9 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ export const composeHandler = ({
case 'application/json':
if (hasErrorHandler)
fnLiteral += `const tempBody = await c.request.text()
try {
c.body = JSON.parse(tempBody)
} catch {
Expand All @@ -768,12 +768,12 @@ export const composeHandler = ({
case 'formdata':
case 'multipart/form-data':
fnLiteral += `c.body = {}
const form = await c.request.formData()
for (const key of form.keys()) {
if (c.body[key])
continue
const value = form.getAll(key)
if (value.length === 1)
c.body[key] = value[0]
Expand Down Expand Up @@ -1065,10 +1065,10 @@ export const composeHandler = ({

fnLiteral += `if(be !== undefined) {\n`
endBeforeHandle()
const endAfterHandle = report('afterHandle', {
unit: hooks.transform.length
})
if (hooks.afterHandle) {
if (hooks.afterHandle?.length) {
const endAfterHandle = report('afterHandle', {
unit: hooks.transform.length
})
report('handle', {
name: isHandleFn
? (handler as Function).name
Expand Down Expand Up @@ -1100,10 +1100,9 @@ export const composeHandler = ({

endUnit()
}
endAfterHandle()
}

endAfterHandle()

if (validator.response)
fnLiteral += composeResponseValidation('be')

Expand Down

0 comments on commit ff73f80

Please sign in to comment.