Skip to content

Commit 22b79ce

Browse files
committed
🔧 fix: set-cookie resent without set
1 parent cccd591 commit 22b79ce

File tree

6 files changed

+82
-29
lines changed

6 files changed

+82
-29
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.22 - 13 Oct 2024
2+
Bug fix:
3+
- Fix `set-cookie` to resent if value is accessed even without set
4+
15
# 1.1.21 - 13 Oct 2024
26
Improvement:
37
- infer 200 response from handle if not specified

example/a.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import { Elysia, t } from '../src'
22

3-
const main = new Elysia().get('/', () => 'a', {
4-
response: { 200: t.Number({
5-
default: () => 'a'
6-
}), 500: t.String() }
7-
})
3+
const app = new Elysia()
4+
.derive(({ cookie: { test } }) => {
5+
if (!test.value) {
6+
test.value = 'Hello, world!'
7+
}
88

9-
type A = (typeof main)['_routes']['index']['get']['response']
9+
return {}
10+
})
11+
.get('/', () => 'Hello, world!')
12+
13+
app.handle(
14+
new Request('http://localhost:3000/', {
15+
headers: {
16+
cookie: 'test=Hello, world!'
17+
}
18+
})
19+
)
20+
.then((x) => x.headers)
21+
.then(console.log)

example/cookie.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ const app = new Elysia({
1414
name: 'Rin',
1515
affilation: 'Administration'
1616
}
17-
])
17+
]),
18+
{
19+
cookie: t.Cookie({
20+
council: t.Array(
21+
t.Object({
22+
name: t.String(),
23+
affilation: t.String()
24+
})
25+
)
26+
})
27+
}
1828
)
1929
.get('/create', ({ cookie: { name } }) => (name.value = 'Himari'))
2030
.get(

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.1.21",
4+
"version": "1.1.22",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/cookies.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ export class Cookie<T> implements ElysiaCookie {
133133
) {}
134134

135135
get cookie() {
136-
if (!(this.name in this.jar)) this.jar[this.name] = this.initial
137-
138-
return this.jar[this.name]
136+
return this.jar[this.name] ?? this.initial
139137
}
140138

141139
set cookie(jar: ElysiaCookie) {
@@ -144,98 +142,106 @@ export class Cookie<T> implements ElysiaCookie {
144142
this.jar[this.name] = jar
145143
}
146144

145+
protected get setCookie() {
146+
if (!(this.name in this.jar)) this.jar[this.name] = this.initial
147+
148+
return this.jar[this.name]
149+
}
150+
151+
protected set setCookie(jar: ElysiaCookie) {
152+
this.cookie = jar
153+
}
154+
147155
get value(): T {
148156
return this.cookie.value as T
149157
}
150158

151159
set value(value: T) {
152-
this.cookie.value = value
160+
this.setCookie.value = value
153161
}
154162

155163
get expires() {
156164
return this.cookie.expires
157165
}
158166

159167
set expires(expires) {
160-
this.cookie.expires = expires
161-
162-
console.log(this.cookie)
168+
this.setCookie.expires = expires
163169
}
164170

165171
get maxAge() {
166172
return this.cookie.maxAge
167173
}
168174

169175
set maxAge(maxAge) {
170-
this.cookie.maxAge = maxAge
176+
this.setCookie.maxAge = maxAge
171177
}
172178

173179
get domain() {
174180
return this.cookie.domain
175181
}
176182

177183
set domain(domain) {
178-
this.cookie.domain = domain
184+
this.setCookie.domain = domain
179185
}
180186

181187
get path() {
182188
return this.cookie.path
183189
}
184190

185191
set path(path) {
186-
this.cookie.path = path
192+
this.setCookie.path = path
187193
}
188194

189195
get secure() {
190196
return this.cookie.secure
191197
}
192198

193199
set secure(secure) {
194-
this.cookie.secure = secure
200+
this.setCookie.secure = secure
195201
}
196202

197203
get httpOnly() {
198204
return this.cookie.httpOnly
199205
}
200206

201207
set httpOnly(httpOnly) {
202-
this.cookie.httpOnly = httpOnly
208+
this.setCookie.httpOnly = httpOnly
203209
}
204210

205211
get sameSite() {
206212
return this.cookie.sameSite
207213
}
208214

209215
set sameSite(sameSite) {
210-
this.cookie.sameSite = sameSite
216+
this.setCookie.sameSite = sameSite
211217
}
212218

213219
get priority() {
214220
return this.cookie.priority
215221
}
216222

217223
set priority(priority) {
218-
this.cookie.priority = priority
224+
this.setCookie.priority = priority
219225
}
220226

221227
get partitioned() {
222228
return this.cookie.partitioned
223229
}
224230

225231
set partitioned(partitioned) {
226-
this.cookie.partitioned = partitioned
232+
this.setCookie.partitioned = partitioned
227233
}
228234

229235
get secrets() {
230236
return this.cookie.secrets
231237
}
232238

233239
set secrets(secrets) {
234-
this.cookie.secrets = secrets
240+
this.setCookie.secrets = secrets
235241
}
236242

237243
update(config: Updater<Partial<ElysiaCookie>>) {
238-
this.cookie = Object.assign(
244+
this.setCookie = Object.assign(
239245
this.cookie,
240246
typeof config === 'function' ? config(this.cookie) : config
241247
)
@@ -244,7 +250,7 @@ export class Cookie<T> implements ElysiaCookie {
244250
}
245251

246252
set(config: Updater<Partial<ElysiaCookie>>) {
247-
this.cookie = Object.assign(
253+
this.setCookie = Object.assign(
248254
{
249255
...this.initial,
250256
value: this.value

test/cookie/response.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,7 @@ describe('Cookie Response', () => {
363363
})
364364

365365
it('set cookie attribute before value', async () => {
366-
const date = new Date(
367-
Date.now() + 1000 * 60 * 60 * 24
368-
)
366+
const date = new Date(Date.now() + 1000 * 60 * 60 * 24)
369367

370368
const app = new Elysia().get('/', ({ cookie }) => {
371369
cookie.my_cookie.expires = date
@@ -382,4 +380,27 @@ describe('Cookie Response', () => {
382380
`my_cookie=my_cookie_value; Path=/; Expires=${date.toUTCString()}`
383381
])
384382
})
383+
384+
it('should not set if value is duplicated', async () => {
385+
const app = new Elysia()
386+
.derive(({ cookie: { test } }) => {
387+
if (!test.value) {
388+
test.value = 'Hello, world!'
389+
}
390+
391+
return {}
392+
})
393+
.get('/', () => 'Hello, world!')
394+
395+
const res = await app.handle(
396+
new Request('http://localhost:3000/', {
397+
headers: {
398+
cookie: 'test=Hello, world!'
399+
}
400+
})
401+
)
402+
.then((x) => x.headers)
403+
404+
expect(res.getSetCookie()).toEqual([])
405+
})
385406
})

0 commit comments

Comments
 (0)