@@ -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,129 @@ 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+ if ( this . cookie . expires === expires ) return
190+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
191+ this . jar [ this . name ] . expires = expires
168192 }
169193
170194 get maxAge ( ) {
171195 return this . cookie . maxAge
172196 }
173197
174- set maxAge ( maxAge ) {
175- this . setCookie . maxAge = maxAge
198+ set maxAge ( maxAge : number | undefined ) {
199+ if ( this . cookie . maxAge === maxAge ) return
200+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
201+ this . jar [ this . name ] . maxAge = maxAge
176202 }
177203
178204 get domain ( ) {
179205 return this . cookie . domain
180206 }
181207
182- set domain ( domain ) {
183- this . setCookie . domain = domain
208+ set domain ( domain : string | undefined ) {
209+ if ( this . cookie . domain === domain ) return
210+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
211+ this . jar [ this . name ] . domain = domain
184212 }
185213
186214 get path ( ) {
187215 return this . cookie . path
188216 }
189217
190- set path ( path ) {
191- this . setCookie . path = path
218+ set path ( path : string | undefined ) {
219+ if ( this . cookie . path === path ) return
220+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
221+ this . jar [ this . name ] . path = path
192222 }
193223
194224 get secure ( ) {
195225 return this . cookie . secure
196226 }
197227
198- set secure ( secure ) {
199- this . setCookie . secure = secure
228+ set secure ( secure : boolean | undefined ) {
229+ if ( this . cookie . secure === secure ) return
230+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
231+ this . jar [ this . name ] . secure = secure
200232 }
201233
202234 get httpOnly ( ) {
203235 return this . cookie . httpOnly
204236 }
205237
206- set httpOnly ( httpOnly ) {
207- this . setCookie . httpOnly = httpOnly
238+ set httpOnly ( httpOnly : boolean | undefined ) {
239+ if ( this . cookie . httpOnly === httpOnly ) return
240+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
241+ this . jar [ this . name ] . httpOnly = httpOnly
208242 }
209243
210244 get sameSite ( ) {
211245 return this . cookie . sameSite
212246 }
213247
214- set sameSite ( sameSite ) {
215- this . setCookie . sameSite = sameSite
248+ set sameSite ( sameSite : true | false | 'lax' | 'strict' | 'none' | undefined ) {
249+ if ( this . cookie . sameSite === sameSite ) return
250+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
251+ this . jar [ this . name ] . sameSite = sameSite
216252 }
217253
218254 get priority ( ) {
219255 return this . cookie . priority
220256 }
221257
222- set priority ( priority ) {
223- this . setCookie . priority = priority
258+ set priority ( priority : 'low' | 'medium' | 'high' | undefined ) {
259+ if ( this . cookie . priority === priority ) return
260+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
261+ this . jar [ this . name ] . priority = priority
224262 }
225263
226264 get partitioned ( ) {
227265 return this . cookie . partitioned
228266 }
229267
230- set partitioned ( partitioned ) {
231- this . setCookie . partitioned = partitioned
268+ set partitioned ( partitioned : boolean | undefined ) {
269+ if ( this . cookie . partitioned === partitioned ) return
270+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
271+ this . jar [ this . name ] . partitioned = partitioned
232272 }
233273
234274 get secrets ( ) {
235275 return this . cookie . secrets
236276 }
237277
238- set secrets ( secrets ) {
239- this . setCookie . secrets = secrets
278+ set secrets ( secrets : string | string [ ] | undefined ) {
279+ if ( this . cookie . secrets === secrets ) return
280+ if ( ! ( this . name in this . jar ) ) this . jar [ this . name ] = { ...this . initial }
281+ this . jar [ this . name ] . secrets = secrets
240282 }
241283
242284 update ( config : Updater < Partial < ElysiaCookie > > ) {
0 commit comments