Skip to content

Commit e0c0f54

Browse files
committed
Make setCookie= slightly more efficient
1 parent 136c7aa commit e0c0f54

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

packages/headers/src/lib/super-headers.ts

+16-21
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const SetCookieKey = 'set-cookie';
146146
*/
147147
export class SuperHeaders extends Headers {
148148
#map: Map<string, string | HeaderValue>;
149-
#setCookieValues: (string | SetCookie)[] = [];
149+
#setCookies: (string | SetCookie)[] = [];
150150

151151
constructor(init?: string | SuperHeadersInit | Headers) {
152152
super();
@@ -190,10 +190,9 @@ export class SuperHeaders extends Headers {
190190
append(name: string, value: string): void {
191191
let key = name.toLowerCase();
192192
if (key === SetCookieKey) {
193-
this.#setCookieValues.push(value);
193+
this.#setCookies.push(value);
194194
} else {
195195
let existingValue = this.#map.get(key);
196-
// TODO: check if it's an empty string
197196
this.#map.set(key, existingValue ? `${existingValue}, ${value}` : value);
198197
}
199198
}
@@ -206,7 +205,7 @@ export class SuperHeaders extends Headers {
206205
delete(name: string): void {
207206
let key = name.toLowerCase();
208207
if (key === SetCookieKey) {
209-
this.#setCookieValues = [];
208+
this.#setCookies = [];
210209
} else {
211210
this.#map.delete(key);
212211
}
@@ -242,9 +241,7 @@ export class SuperHeaders extends Headers {
242241
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie)
243242
*/
244243
getSetCookie(): string[] {
245-
return this.#setCookieValues.map((value) =>
246-
typeof value === 'string' ? value : value.toString(),
247-
);
244+
return this.#setCookies.map((value) => (typeof value === 'string' ? value : value.toString()));
248245
}
249246

250247
/**
@@ -255,7 +252,7 @@ export class SuperHeaders extends Headers {
255252
has(name: string): boolean {
256253
let key = name.toLowerCase();
257254
if (key === SetCookieKey) {
258-
return this.#setCookieValues.length > 0;
255+
return this.#setCookies.length > 0;
259256
} else {
260257
return this.#map.has(key);
261258
}
@@ -270,7 +267,7 @@ export class SuperHeaders extends Headers {
270267
set(name: string, value: string): void {
271268
let key = name.toLowerCase();
272269
if (key === SetCookieKey) {
273-
this.#setCookieValues = [value];
270+
this.#setCookies = [value];
274271
} else {
275272
this.#map.set(key, value);
276273
}
@@ -698,27 +695,25 @@ export class SuperHeaders extends Headers {
698695
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1)
699696
*/
700697
get setCookie(): SetCookie[] {
701-
for (let i = 0; i < this.#setCookieValues.length; ++i) {
702-
let value = this.#setCookieValues[i];
698+
for (let i = 0; i < this.#setCookies.length; ++i) {
699+
let value = this.#setCookies[i];
703700
if (typeof value === 'string') {
704-
this.#setCookieValues[i] = new SetCookie(value);
701+
this.#setCookies[i] = new SetCookie(value);
705702
}
706703
}
707704

708-
return this.#setCookieValues as SetCookie[];
705+
return this.#setCookies as SetCookie[];
709706
}
710707

711-
set setCookie(values: (string | SetCookieInit)[] | string | SetCookieInit | undefined | null) {
712-
if (values != null) {
713-
if (Array.isArray(values)) {
714-
this.#setCookieValues = values.map((value) =>
715-
typeof value === 'string' || value instanceof SetCookie ? value : new SetCookie(value),
716-
);
708+
set setCookie(value: (string | SetCookieInit)[] | string | SetCookieInit | undefined | null) {
709+
if (value != null) {
710+
if (Array.isArray(value)) {
711+
this.#setCookies = value.map((v) => (typeof v === 'string' ? v : new SetCookie(v)));
717712
} else {
718-
this.#setCookieValues = [new SetCookie(values)];
713+
this.#setCookies = [typeof value === 'string' ? value : new SetCookie(value)];
719714
}
720715
} else {
721-
this.#setCookieValues = [];
716+
this.#setCookies = [];
722717
}
723718
}
724719

0 commit comments

Comments
 (0)