Skip to content

Commit dafd31b

Browse files
committed
🔧 fix: #1369 don't block microtask queue in SSE
1 parent 7a70873 commit dafd31b

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 1.3.21 - 31 Aug 2025
2+
Bug fix:
3+
- [#1356](https://github.com/elysiajs/elysia/pull/1356) webSocket validation error handling in BunAdapter
4+
- [#1358](https://github.com/elysiajs/elysia/pull/1358) allow overriding websocket handler with listen options
5+
- [#1365](https://github.com/elysiajs/elysia/pull/1365) check if the plugin.constructor (fix import module in Bun 1.2.21)
6+
- [#1367](https://github.com/elysiajs/elysia/pull/1367) .trace hooks (onAfterResponse, etc...) not being called
7+
- [#1369](https://github.com/elysiajs/elysia/issues/1369) don't block microtask queue in SSE
8+
19
# 1.3.20 - 24 Aug 2025
210
Change:
311
- mime is undefined when using `Elysia.file` in Web Standard Adapter

example/a.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { Elysia, sse, t } from '../src'
2-
import { req } from '../test/utils'
1+
import { Elysia } from '../src'
2+
3+
const plugin1 = new Elysia().derive({ as: 'scoped' }, () => ({
4+
hello: 'world'
5+
}))
6+
7+
const plugin2 = new Elysia()
8+
.use(plugin1)
9+
.derive({ as: 'scoped' }, ({ hello }) => ({ hello }))
310

411
const app = new Elysia()
5-
.get('/', function* () {
6-
yield sse('a')
7-
yield sse('b')
8-
})
9-
.get('/proxy', () => app.handle(new Request('http://localhost')))
12+
.use(plugin2)
13+
// This is undefined
14+
.get('/', ({ hello }) => typeof hello)
1015
.listen(3000)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.3.20",
4+
"version": "1.3.21",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/adapter/utils.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,24 +247,34 @@ export const createStreamHandler =
247247
if (chunk === undefined || chunk === null) continue
248248

249249
// @ts-ignore
250-
if (chunk.toSSE)
250+
if (chunk.toSSE) {
251251
// @ts-ignore
252252
controller.enqueue(chunk.toSSE())
253-
else if (typeof chunk === 'object')
254-
try {
255-
controller.enqueue(
256-
format(JSON.stringify(chunk))
257-
)
258-
} catch {
253+
} else {
254+
if (typeof chunk === 'object')
255+
try {
256+
controller.enqueue(
257+
format(JSON.stringify(chunk))
258+
)
259+
} catch {
260+
controller.enqueue(
261+
format(chunk.toString())
262+
)
263+
}
264+
else
259265
controller.enqueue(format(chunk.toString()))
260-
}
261-
else controller.enqueue(format(chunk.toString()))
262-
263-
// Wait for the next event loop
264-
// Otherwise the data will be mixed up
265-
await new Promise<void>((resolve) =>
266-
setTimeout(() => resolve(), 0)
267-
)
266+
267+
if (!isSSE)
268+
/**
269+
* Wait for the next event loop
270+
* otherwise the data will be mixed up
271+
*
272+
* @see https://github.com/elysiajs/elysia/issues/741
273+
*/
274+
await new Promise<void>((resolve) =>
275+
setTimeout(() => resolve(), 0)
276+
)
277+
}
268278
}
269279
} catch (error) {
270280
console.warn(error)
@@ -357,7 +367,6 @@ export const createResponseHandler = (handler: CreateHandlerParameter) => {
357367
status: set.status as number
358368
})
359369

360-
361370
if (
362371
!(newResponse as Response).headers.has('content-length') &&
363372
(newResponse as Response).headers.get(

src/compose.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ export const composeHandler = ({
907907
endUnit()
908908
}
909909
}
910-
910+
911911
reporter.resolve()
912912

913913
afterResponse += '})\n'
@@ -1678,6 +1678,8 @@ export const composeHandler = ({
16781678
mapResponse('resolved') +
16791679
`}` +
16801680
`else Object.assign(c, resolved)\n`
1681+
1682+
endUnit()
16811683
} else if (!returning) {
16821684
fnLiteral += isAsync(beforeHandle)
16831685
? `await e.beforeHandle[${i}](c)\n`
@@ -2517,7 +2519,7 @@ export const composeErrorHandler = (app: AnyElysia) => {
25172519
total: hooks.afterResponse?.length,
25182520
name: 'context'
25192521
})
2520-
2522+
25212523
if (hooks.afterResponse?.length && hooks.afterResponse) {
25222524
for (let i = 0; i < hooks.afterResponse.length; i++) {
25232525
const fn = hooks.afterResponse[i].fn

0 commit comments

Comments
 (0)