@@ -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,87 +156,135 @@ export class Cookie<T> implements ElysiaCookie {
156156 }
157157
158158 set value ( value : T ) {
159- this . setCookie . value = value
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
160182 }
161183
162184 get expires ( ) {
163185 return this . cookie . expires
164186 }
165187
166- set expires ( expires ) {
167- this . setCookie . expires = expires
188+ 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
168196 }
169197
170198 get maxAge ( ) {
171199 return this . cookie . maxAge
172200 }
173201
174- set maxAge ( maxAge ) {
175- this . setCookie . maxAge = maxAge
202+ 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
176206 }
177207
178208 get domain ( ) {
179209 return this . cookie . domain
180210 }
181211
182- set domain ( domain ) {
183- this . setCookie . domain = domain
212+ 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
184216 }
185217
186218 get path ( ) {
187219 return this . cookie . path
188220 }
189221
190- set path ( path ) {
191- this . setCookie . path = path
222+ 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
192226 }
193227
194228 get secure ( ) {
195229 return this . cookie . secure
196230 }
197231
198- set secure ( secure ) {
199- this . setCookie . secure = secure
232+ 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
200236 }
201237
202238 get httpOnly ( ) {
203239 return this . cookie . httpOnly
204240 }
205241
206- set httpOnly ( httpOnly ) {
207- this . setCookie . httpOnly = httpOnly
242+ 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
208246 }
209247
210248 get sameSite ( ) {
211249 return this . cookie . sameSite
212250 }
213251
214- set sameSite ( sameSite ) {
215- this . setCookie . sameSite = sameSite
252+ set sameSite (
253+ sameSite : true | false | 'lax' | 'strict' | 'none' | undefined
254+ ) {
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
216258 }
217259
218260 get priority ( ) {
219261 return this . cookie . priority
220262 }
221263
222- set priority ( priority ) {
223- this . setCookie . priority = priority
264+ 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
224268 }
225269
226270 get partitioned ( ) {
227271 return this . cookie . partitioned
228272 }
229273
230- set partitioned ( partitioned ) {
231- this . setCookie . partitioned = partitioned
274+ 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
232278 }
233279
234280 get secrets ( ) {
235281 return this . cookie . secrets
236282 }
237283
238- set secrets ( secrets ) {
239- this . setCookie . secrets = secrets
284+ 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
240288 }
241289
242290 update ( config : Updater < Partial < ElysiaCookie > > ) {
0 commit comments