feat: cookie jar data is persisted and loaded

This commit is contained in:
Andrew Bastin
2023-10-25 18:34:32 +05:30
parent d068da1341
commit 3f1ca8f20b
4 changed files with 47 additions and 16 deletions

View File

@@ -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",

View File

@@ -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()
}
/**

View File

@@ -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)

6
pnpm-lock.yaml generated
View File

@@ -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==}