Skip to content

Commit 61fc1a1

Browse files
authored
Merge pull request #1567 from akim-bow/improve/remove-cookie-cache
improve: remove cookie cache
2 parents a6ea431 + 89001d1 commit 61fc1a1

File tree

3 files changed

+17
-62
lines changed

3 files changed

+17
-62
lines changed

src/compose.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AnyElysia } from './index'
1+
import { AnyElysia, Cookie } from './index'
22

33
import { Value, TransformDecodeError } from '@sinclair/typebox/value'
44
import {
@@ -1573,7 +1573,7 @@ export const composeHandler = ({
15731573
if (validator.cookie.hasTransform)
15741574
fnLiteral += coerceTransformDecodeError(
15751575
`for(const [key,value] of Object.entries(validator.cookie.Decode(cookieValue))){` +
1576-
`c.cookie[key].value=value` +
1576+
`c.cookie[key].cookie.value = value` +
15771577
`}`,
15781578
'cookie',
15791579
allowUnsafeValidationDetails
@@ -2131,6 +2131,7 @@ export const composeHandler = ({
21312131
ERROR_CODE,
21322132
parseCookie: hasCookie ? parseCookie : undefined,
21332133
signCookie: hasCookie ? signCookie : undefined,
2134+
Cookie: hasCookie ? Cookie : undefined,
21342135
decodeURIComponent: hasQuery ? decode : undefined,
21352136
ElysiaCustomStatusResponse,
21362137
ELYSIA_TRACE: hasTrace ? ELYSIA_TRACE : undefined,

src/cookies.ts

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class Cookie<T> implements ElysiaCookie {
142142
}
143143

144144
protected get setCookie() {
145-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
145+
if (!(this.name in this.jar)) this.jar[this.name] = this.initial
146146

147147
return this.jar[this.name]
148148
}
@@ -156,93 +156,55 @@ export class Cookie<T> implements ElysiaCookie {
156156
}
157157

158158
set value(value: T) {
159-
// Check if value actually changed before creating entry in jar
160-
const current = this.cookie.value
161-
162-
// Simple equality check
163-
if (current === value) return
164-
165-
// For objects, do a deep equality check
166-
if (
167-
typeof current === 'object' &&
168-
current !== null &&
169-
typeof value === 'object' &&
170-
value !== null
171-
) {
172-
try {
173-
if (JSON.stringify(current) === JSON.stringify(value)) return
174-
} catch {
175-
// If stringify fails, proceed with setting the value
176-
}
177-
}
178-
179-
// Only create entry in jar if value actually changed
180-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
181-
this.jar[this.name].value = value
159+
this.setCookie.value = value;
182160
}
183161

184162
get expires() {
185163
return this.cookie.expires
186164
}
187165

188166
set expires(expires: Date | undefined) {
189-
// Handle undefined values and compare timestamps instead of Date objects
190-
const currentExpires = this.cookie.expires
191-
if (currentExpires === undefined && expires === undefined) return
192-
if (currentExpires?.getTime() === expires?.getTime()) return
193-
194-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
195-
this.jar[this.name].expires = expires
167+
this.setCookie.expires = expires
196168
}
197169

198170
get maxAge() {
199171
return this.cookie.maxAge
200172
}
201173

202174
set maxAge(maxAge: number | undefined) {
203-
if (this.cookie.maxAge === maxAge) return
204-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
205-
this.jar[this.name].maxAge = maxAge
175+
this.setCookie.maxAge = maxAge;
206176
}
207177

208178
get domain() {
209179
return this.cookie.domain
210180
}
211181

212182
set domain(domain: string | undefined) {
213-
if (this.cookie.domain === domain) return
214-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
215-
this.jar[this.name].domain = domain
183+
this.setCookie.domain = domain;
216184
}
217185

218186
get path() {
219187
return this.cookie.path
220188
}
221189

222190
set path(path: string | undefined) {
223-
if (this.cookie.path === path) return
224-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
225-
this.jar[this.name].path = path
191+
this.setCookie.path = path
226192
}
227193

228194
get secure() {
229195
return this.cookie.secure
230196
}
231197

232198
set secure(secure: boolean | undefined) {
233-
if (this.cookie.secure === secure) return
234-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
235-
this.jar[this.name].secure = secure
199+
this.setCookie.secure = secure
236200
}
237201

238202
get httpOnly() {
239203
return this.cookie.httpOnly
240204
}
241205

242206
set httpOnly(httpOnly: boolean | undefined) {
243-
if (this.cookie.httpOnly === httpOnly) return
244-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
245-
this.jar[this.name].httpOnly = httpOnly
207+
this.setCookie.httpOnly = httpOnly
246208
}
247209

248210
get sameSite() {
@@ -252,39 +214,31 @@ export class Cookie<T> implements ElysiaCookie {
252214
set sameSite(
253215
sameSite: true | false | 'lax' | 'strict' | 'none' | undefined
254216
) {
255-
if (this.cookie.sameSite === sameSite) return
256-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
257-
this.jar[this.name].sameSite = sameSite
217+
this.setCookie.sameSite = sameSite
258218
}
259219

260220
get priority() {
261221
return this.cookie.priority
262222
}
263223

264224
set priority(priority: 'low' | 'medium' | 'high' | undefined) {
265-
if (this.cookie.priority === priority) return
266-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
267-
this.jar[this.name].priority = priority
225+
this.setCookie.priority = priority
268226
}
269227

270228
get partitioned() {
271229
return this.cookie.partitioned
272230
}
273231

274232
set partitioned(partitioned: boolean | undefined) {
275-
if (this.cookie.partitioned === partitioned) return
276-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
277-
this.jar[this.name].partitioned = partitioned
233+
this.setCookie.partitioned = partitioned
278234
}
279235

280236
get secrets() {
281237
return this.cookie.secrets
282238
}
283239

284240
set secrets(secrets: string | string[] | undefined) {
285-
if (this.cookie.secrets === secrets) return
286-
if (!(this.name in this.jar)) this.jar[this.name] = { ...this.initial }
287-
this.jar[this.name].secrets = secrets
241+
this.setCookie.secrets = secrets
288242
}
289243

290244
update(config: Updater<Partial<ElysiaCookie>>) {

test/cookie/unchanged.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('Cookie - Unchanged Values', () => {
7272
).toBeGreaterThan(0)
7373
})
7474

75-
it('should not send set-cookie header when setting same value', async () => {
75+
it('should send set-cookie header when setting same value', async () => {
7676
const app = new Elysia().get('/same', ({ cookie: { session } }) => {
7777
// Setting the same value that came from request
7878
session.value = 'existing'
@@ -87,7 +87,7 @@ describe('Cookie - Unchanged Values', () => {
8787
})
8888
)
8989

90-
expect(response.headers.getAll('set-cookie').length).toBe(0)
90+
expect(response.headers.getAll('set-cookie').length).toBeGreaterThan(0)
9191
})
9292

9393
it('should send set-cookie header when value actually changes', async () => {

0 commit comments

Comments
 (0)