Skip to content

Commit 414b94f

Browse files
committed
🔧 fix: setting cookie and redirecting returns immutable
1 parent 013019b commit 414b94f

6 files changed

Lines changed: 65 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.4.3 - 5 Jan 2025
2+
Bug fix:
3+
- [#54](https://github.com/elysiajs/node/issues/54) update srvx to 0.10.0
4+
- [#53](https://github.com/elysiajs/node/issues/53) setting cookie and redirecting returns immutable
5+
16
# 1.4.2 - 4 Nov 2025
27
Bug fix:
38
- [#52](https://github.com/elysiajs/node/pull/52) incorrectly constructed responses for 204 status codes

bun.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ import { node } from '../src'
77
const app = new Elysia({
88
adapter: node()
99
})
10-
.use(cors())
10+
11+
app.use(cors())
1112
.use(openapi())
13+
.get('/redirect', ({ cookie, redirect }) => {
14+
cookie.a.value = 'cookie value'
15+
16+
return redirect('https://example.com')
17+
})
1218
.ws('/ws/:id', {
1319
open({ data, subscribe, isSubscribed }) {
20+
console.log(data)
1421
subscribe('welcome')
1522
},
1623
message(ws, message) {
@@ -29,5 +36,3 @@ const app = new Elysia({
2936
})
3037
.get('/', () => 'ok')
3138
.listen(3000)
32-
33-
// console.log(app.fetch.toString())

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"dependencies": {
1414
"crossws": "^0.4.1",
15-
"srvx": "^0.9.4"
15+
"srvx": "^0.10.0"
1616
},
1717
"peerDependencies": {
1818
"elysia": ">= 1.4.0"
@@ -22,7 +22,7 @@
2222
"@elysiajs/openapi": "^1.4.0",
2323
"@types/bun": "^1.3.1",
2424
"@types/node": "^22.10.2",
25-
"elysia": "^1.4.15",
25+
"elysia": "^1.4.21",
2626
"eslint": "9.17.0",
2727
"tsup": "^8.3.5",
2828
"tsx": "^4.19.2",

src/handle.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import { FastResponse as Response } from 'srvx'
44

55
import {
6-
createResponseHandler,
76
createStreamHandler,
87
handleSet,
98
responseToSetHeaders,
109
streamResponse
1110
} from 'elysia/adapter/utils'
12-
import { handleFile } from './utils'
11+
import { handleFile, createResponseHandler } from './utils'
1312

1413
import { ElysiaFile, mime } from 'elysia/universal/file'
1514
import { isNotEmpty } from 'elysia/utils'

src/utils.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import type { ReadStream } from 'fs'
33

44
import { isNotEmpty } from 'elysia/utils'
55
import type { Context } from 'elysia/context'
6+
import {
7+
createStreamHandler,
8+
responseToSetHeaders,
9+
streamResponse
10+
} from 'elysia/adapter/utils'
611

712
export const handleFile = (
813
response: ReadStream | File | Blob,
@@ -69,3 +74,40 @@ export const handleFile = (
6974
headers: defaultHeader
7075
})
7176
}
77+
78+
interface CreateHandlerParameter {
79+
mapResponse(
80+
response: unknown,
81+
set: Context['set'],
82+
request?: Request
83+
): Response
84+
mapCompactResponse(response: unknown, request?: Request): Response
85+
}
86+
87+
export const createResponseHandler = (handler: CreateHandlerParameter) => {
88+
const handleStream = createStreamHandler(handler)
89+
90+
return (response: Response, set: Context['set'], request?: Request) => {
91+
const newResponse = new Response(response.body, {
92+
headers: Object.assign(
93+
// @ts-ignore
94+
Object.fromEntries(response.headers.entries()),
95+
set.headers
96+
),
97+
status: response.status ?? set.status
98+
})
99+
100+
if (
101+
!(newResponse as Response).headers.has('content-length') &&
102+
(newResponse as Response).headers.get('transfer-encoding') ===
103+
'chunked'
104+
)
105+
return handleStream(
106+
streamResponse(newResponse as Response),
107+
responseToSetHeaders(newResponse as Response, set),
108+
request
109+
) as any
110+
111+
return newResponse
112+
}
113+
}

0 commit comments

Comments
 (0)