From 3f1ca8f20b2a59ff0e95831f8feb8fd723a4c1e4 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Wed, 25 Oct 2023 18:34:32 +0530 Subject: [PATCH] feat: cookie jar data is persisted and loaded --- packages/hoppscotch-common/package.json | 3 +- .../src/newstore/localpersistence.ts | 34 +++++++++++++++++++ .../src/services/cookie-jar.service.ts | 20 ++++------- pnpm-lock.yaml | 6 ++-- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/packages/hoppscotch-common/package.json b/packages/hoppscotch-common/package.json index e26e5f749..920fe41f1 100644 --- a/packages/hoppscotch-common/package.json +++ b/packages/hoppscotch-common/package.json @@ -101,7 +101,8 @@ "wonka": "^6.3.4", "workbox-window": "^7.0.0", "xml-formatter": "^3.5.0", - "yargs-parser": "^21.1.1" + "yargs-parser": "^21.1.1", + "zod": "^3.22.2" }, "devDependencies": { "@esbuild-plugins/node-globals-polyfill": "^0.2.3", diff --git a/packages/hoppscotch-common/src/newstore/localpersistence.ts b/packages/hoppscotch-common/src/newstore/localpersistence.ts index e3308df8d..1911f0053 100644 --- a/packages/hoppscotch-common/src/newstore/localpersistence.ts +++ b/packages/hoppscotch-common/src/newstore/localpersistence.ts @@ -47,6 +47,9 @@ import { StorageLike, watchDebounced } from "@vueuse/core" import { getService } from "~/modules/dioc" import { RESTTabService } from "~/services/tab/rest" import { GQLTabService } from "~/services/tab/graphql" +import { z } from "zod" +import { CookieJarService } from "~/services/cookie-jar.service" +import { watch } from "vue" function checkAndMigrateOldSettings() { if (window.localStorage.getItem("selectedEnvIndex")) { @@ -182,6 +185,35 @@ function setupHistoryPersistence() { }) } +const cookieSchema = z.record(z.array(z.string())) + +function setupCookiesPersistence() { + const cookieJarService = getService(CookieJarService) + + try { + const cookieData = JSON.parse( + window.localStorage.getItem("cookieJar") || "{}" + ) + + const parseResult = cookieSchema.safeParse(cookieData) + + if (parseResult.success) { + for (const domain in parseResult.data) { + cookieJarService.bulkApplyCookiesToDomain( + parseResult.data[domain], + domain + ) + } + } + } catch (e) {} + + watch(cookieJarService.cookieJar, (cookieJar) => { + const data = JSON.stringify(Object.fromEntries(cookieJar.entries())) + + window.localStorage.setItem("cookieJar", data) + }) +} + function setupCollectionsPersistence() { const restCollectionData = JSON.parse( window.localStorage.getItem("collections") || "[]" @@ -382,6 +414,8 @@ export function setupLocalPersistence() { setupSocketIOPersistence() setupSSEPersistence() setupMQTTPersistence() + + setupCookiesPersistence() } /** diff --git a/packages/hoppscotch-common/src/services/cookie-jar.service.ts b/packages/hoppscotch-common/src/services/cookie-jar.service.ts index 48e958ab1..b41340a7a 100644 --- a/packages/hoppscotch-common/src/services/cookie-jar.service.ts +++ b/packages/hoppscotch-common/src/services/cookie-jar.service.ts @@ -22,25 +22,19 @@ export class CookieJarService extends Service { constructor() { super() - - // TODO: Remove this, only for testing - this.cookieJar.value.set("hoppscotch.io", [ - "cookie1=value1;", - "cookie2=value2;", - "cookie6=value6; Expires=Mon, 23 Oct 2023 14:53:22 GMT", - ]) - - this.cookieJar.value.set("echo.hoppscotch.io", [ - "cookie3=value3;", - "cookie4=value4; Path=/test", - "cookie5=value5; Expires=Mon, 23 Oct 2023 12:23:22 GMT", - ]) } public parseSetCookieString(setCookieString: string) { return setCookieParse(setCookieString) } + public bulkApplyCookiesToDomain(cookies: string[], domain: string) { + const existingDomainEntries = this.cookieJar.value.get(domain) ?? [] + existingDomainEntries.push(...cookies) + + this.cookieJar.value.set(domain, existingDomainEntries) + } + public getCookiesForURL(url: URL) { const relevantDomains = Array.from(this.cookieJar.value.keys()).filter( (domain) => url.hostname.endsWith(domain) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fd18b84a1..34ce0c1b1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -610,6 +610,9 @@ importers: yargs-parser: specifier: ^21.1.1 version: 21.1.1 + zod: + specifier: ^3.22.2 + version: 3.22.2 devDependencies: '@esbuild-plugins/node-globals-polyfill': specifier: ^0.2.3 @@ -25502,7 +25505,7 @@ packages: deprecated: The library contains critical security issues and should not be used for production! The maintenance of the project has been discontinued. Consider migrating your code to isolated-vm. hasBin: true dependencies: - acorn: 8.10.0 + acorn: 8.11.2 acorn-walk: 8.2.0 dev: false @@ -26943,7 +26946,6 @@ packages: /zod@3.22.2: resolution: {integrity: sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==} - dev: true /zod@3.22.4: resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==}