Skip to content

Commit

Permalink
fix: [#1693] Adds CookieSameSiteEnum and IVirtualServer as exports to…
Browse files Browse the repository at this point in the history
… the index file
  • Loading branch information
capricorn86 committed Jan 20, 2025
1 parent 75c44a4 commit 8c0dedc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
15 changes: 9 additions & 6 deletions packages/happy-dom/src/cookie/CookieContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import URL from '../url/URL.js';
import DefaultCookie from './DefaultCookie.js';
import ICookie from './ICookie.js';
import ICookieContainer from './ICookieContainer.js';
import IOptionalCookie from './IOptionalCookie.js';
import CookieExpireUtility from './urilities/CookieExpireUtility.js';
import CookieURLUtility from './urilities/CookieURLUtility.js';

Expand All @@ -19,7 +20,7 @@ export default class CookieContainer implements ICookieContainer {
*
* @param cookies Cookies.
*/
public addCookies(cookies: ICookie[]): void {
public addCookies(cookies: IOptionalCookie[]): void {
const indexMap: { [k: string]: number } = {};
const getKey = (cookie: ICookie): string =>
`${cookie.key}-${cookie.originURL.hostname}-${cookie.path}-${typeof cookie.value}`;
Expand All @@ -30,22 +31,24 @@ export default class CookieContainer implements ICookieContainer {
}

for (const cookie of cookies) {
if (!cookie || !cookie.key || !cookie.originURL) {
const newCookie = Object.assign({}, DefaultCookie, cookie);

if (!newCookie || !newCookie.key || !newCookie.originURL) {
throw new Error(
"Failed to execute 'addCookies' on 'CookieContainer': The properties 'key' and 'originURL' are required."
);
}

// Remove existing cookie with same name, domain and path.
const index = indexMap[getKey(cookie)];
const index = indexMap[getKey(newCookie)];

if (index !== undefined) {
this.#cookies.splice(index, 1);
}

if (!CookieExpireUtility.hasExpired(cookie)) {
indexMap[getKey(cookie)] = this.#cookies.length;
this.#cookies.push(Object.assign(Object.assign({}, DefaultCookie, cookie)));
if (!CookieExpireUtility.hasExpired(newCookie)) {
indexMap[getKey(newCookie)] = this.#cookies.length;
this.#cookies.push(newCookie);
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions packages/happy-dom/src/cookie/ICookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ export default interface ICookie {
// Required
key: string;
originURL: URL;

// Optional
value?: string | null;
domain?: string;
path?: string;
expires?: Date | null;
httpOnly?: boolean;
secure?: boolean;
sameSite?: CookieSameSiteEnum;
value: string | null;
domain: string;
path: string;
expires: Date | null;
httpOnly: boolean;
secure: boolean;
sameSite: CookieSameSiteEnum;
}
16 changes: 16 additions & 0 deletions packages/happy-dom/src/cookie/IOptionalCookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import CookieSameSiteEnum from './enums/CookieSameSiteEnum.js';

export default interface IOptionalCookie {
// Required
key: string;
originURL: URL;

// Optional
value?: string | null;
domain?: string;
path?: string;
expires?: Date | null;
httpOnly?: boolean;
secure?: boolean;
sameSite?: CookieSameSiteEnum;
}
4 changes: 4 additions & 0 deletions packages/happy-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ import type IBrowserFrame from './browser/types/IBrowserFrame.js';
import type IBrowserPage from './browser/types/IBrowserPage.js';
import type IBrowserSettings from './browser/types/IBrowserSettings.js';
import type IOptionalBrowserSettings from './browser/types/IOptionalBrowserSettings.js';
import type ICookie from './cookie/ICookie.js';
import type IOptionalCookie from './cookie/IOptionalCookie.js';
import type IEventInit from './event/IEventInit.js';
import type ITouchInit from './event/ITouchInit.js';
import type IUIEventInit from './event/IUIEventInit.js';
Expand Down Expand Up @@ -217,6 +219,7 @@ export type {
IBrowserPage,
IBrowserSettings,
IClipboardEventInit,
ICookie,
ICustomEventInit,
IErrorEventInit,
IEventInit,
Expand All @@ -228,6 +231,7 @@ export type {
IMediaQueryListInit,
IMouseEventInit,
IOptionalBrowserSettings,
IOptionalCookie,
IProgressEventInit,
ISubmitEventInit,
ISyncResponse,
Expand Down

0 comments on commit 8c0dedc

Please sign in to comment.