UNPKG

msw

Version:

Seamless REST/GraphQL API mocking library for browser and Node.js.

157 lines 4.31 kB
import { invariant } from "outvariant"; import { isNodeProcess } from "is-node-process"; import toughCookie from "@bundled-es-modules/tough-cookie"; const { Cookie, CookieJar, Store, MemoryCookieStore, domainMatch, pathMatch } = toughCookie; class WebStorageCookieStore extends Store { storage; storageKey; constructor() { super(); invariant( typeof localStorage !== "undefined", "Failed to create a WebStorageCookieStore: `localStorage` is not available in this environment. This is likely an issue with MSW. Please report it on GitHub: https://github.com/mswjs/msw/issues" ); this.synchronous = true; this.storage = localStorage; this.storageKey = "__msw-cookie-store__"; } findCookie(domain, path, key, callback) { try { const store2 = this.getStore(); const cookies = this.filterCookiesFromList(store2, { domain, path, key }); callback(null, cookies[0] || null); } catch (error) { if (error instanceof Error) { callback(error, null); } } } findCookies(domain, path, allowSpecialUseDomain, callback) { if (!domain) { callback(null, []); return; } try { const store2 = this.getStore(); const results = this.filterCookiesFromList(store2, { domain, path }); callback(null, results); } catch (error) { if (error instanceof Error) { callback(error, []); } } } putCookie(cookie, callback) { try { if (cookie.maxAge === 0) { return; } const store2 = this.getStore(); store2.push(cookie); this.updateStore(store2); } catch (error) { if (error instanceof Error) { callback(error); } } } updateCookie(oldCookie, newCookie, callback) { if (newCookie.maxAge === 0) { this.removeCookie( newCookie.domain || "", newCookie.path || "", newCookie.key, callback ); return; } this.putCookie(newCookie, callback); } removeCookie(domain, path, key, callback) { try { const store2 = this.getStore(); const nextStore = this.deleteCookiesFromList(store2, { domain, path, key }); this.updateStore(nextStore); callback(null); } catch (error) { if (error instanceof Error) { callback(error); } } } removeCookies(domain, path, callback) { try { const store2 = this.getStore(); const nextStore = this.deleteCookiesFromList(store2, { domain, path }); this.updateStore(nextStore); callback(null); } catch (error) { if (error instanceof Error) { callback(error); } } } getAllCookies(callback) { try { callback(null, this.getStore()); } catch (error) { if (error instanceof Error) { callback(error, []); } } } getStore() { try { const json = this.storage.getItem(this.storageKey); if (json == null) { return []; } const rawCookies = JSON.parse(json); const cookies = []; for (const rawCookie of rawCookies) { const cookie = Cookie.fromJSON(rawCookie); if (cookie != null) { cookies.push(cookie); } } return cookies; } catch { return []; } } updateStore(nextStore) { this.storage.setItem( this.storageKey, JSON.stringify(nextStore.map((cookie) => cookie.toJSON())) ); } filterCookiesFromList(cookies, matches) { const result = []; for (const cookie of cookies) { if (matches.domain && !domainMatch(matches.domain, cookie.domain || "")) { continue; } if (matches.path && !pathMatch(matches.path, cookie.path || "")) { continue; } if (matches.key && cookie.key !== matches.key) { continue; } result.push(cookie); } return result; } deleteCookiesFromList(cookies, matches) { const matchingCookies = this.filterCookiesFromList(cookies, matches); return cookies.filter((cookie) => !matchingCookies.includes(cookie)); } } const store = isNodeProcess() ? new MemoryCookieStore() : new WebStorageCookieStore(); const cookieStore = new CookieJar(store); export { cookieStore }; //# sourceMappingURL=cookieStore.mjs.map