feat: cookie jar data is persisted and loaded
This commit is contained in:
@@ -101,7 +101,8 @@
|
|||||||
"wonka": "^6.3.4",
|
"wonka": "^6.3.4",
|
||||||
"workbox-window": "^7.0.0",
|
"workbox-window": "^7.0.0",
|
||||||
"xml-formatter": "^3.5.0",
|
"xml-formatter": "^3.5.0",
|
||||||
"yargs-parser": "^21.1.1"
|
"yargs-parser": "^21.1.1",
|
||||||
|
"zod": "^3.22.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
|
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ import { StorageLike, watchDebounced } from "@vueuse/core"
|
|||||||
import { getService } from "~/modules/dioc"
|
import { getService } from "~/modules/dioc"
|
||||||
import { RESTTabService } from "~/services/tab/rest"
|
import { RESTTabService } from "~/services/tab/rest"
|
||||||
import { GQLTabService } from "~/services/tab/graphql"
|
import { GQLTabService } from "~/services/tab/graphql"
|
||||||
|
import { z } from "zod"
|
||||||
|
import { CookieJarService } from "~/services/cookie-jar.service"
|
||||||
|
import { watch } from "vue"
|
||||||
|
|
||||||
function checkAndMigrateOldSettings() {
|
function checkAndMigrateOldSettings() {
|
||||||
if (window.localStorage.getItem("selectedEnvIndex")) {
|
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() {
|
function setupCollectionsPersistence() {
|
||||||
const restCollectionData = JSON.parse(
|
const restCollectionData = JSON.parse(
|
||||||
window.localStorage.getItem("collections") || "[]"
|
window.localStorage.getItem("collections") || "[]"
|
||||||
@@ -382,6 +414,8 @@ export function setupLocalPersistence() {
|
|||||||
setupSocketIOPersistence()
|
setupSocketIOPersistence()
|
||||||
setupSSEPersistence()
|
setupSSEPersistence()
|
||||||
setupMQTTPersistence()
|
setupMQTTPersistence()
|
||||||
|
|
||||||
|
setupCookiesPersistence()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,25 +22,19 @@ export class CookieJarService extends Service {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
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) {
|
public parseSetCookieString(setCookieString: string) {
|
||||||
return setCookieParse(setCookieString)
|
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) {
|
public getCookiesForURL(url: URL) {
|
||||||
const relevantDomains = Array.from(this.cookieJar.value.keys()).filter(
|
const relevantDomains = Array.from(this.cookieJar.value.keys()).filter(
|
||||||
(domain) => url.hostname.endsWith(domain)
|
(domain) => url.hostname.endsWith(domain)
|
||||||
|
|||||||
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
@@ -610,6 +610,9 @@ importers:
|
|||||||
yargs-parser:
|
yargs-parser:
|
||||||
specifier: ^21.1.1
|
specifier: ^21.1.1
|
||||||
version: 21.1.1
|
version: 21.1.1
|
||||||
|
zod:
|
||||||
|
specifier: ^3.22.2
|
||||||
|
version: 3.22.2
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@esbuild-plugins/node-globals-polyfill':
|
'@esbuild-plugins/node-globals-polyfill':
|
||||||
specifier: ^0.2.3
|
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.
|
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
|
hasBin: true
|
||||||
dependencies:
|
dependencies:
|
||||||
acorn: 8.10.0
|
acorn: 8.11.2
|
||||||
acorn-walk: 8.2.0
|
acorn-walk: 8.2.0
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
@@ -26943,7 +26946,6 @@ packages:
|
|||||||
|
|
||||||
/zod@3.22.2:
|
/zod@3.22.2:
|
||||||
resolution: {integrity: sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==}
|
resolution: {integrity: sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==}
|
||||||
dev: true
|
|
||||||
|
|
||||||
/zod@3.22.4:
|
/zod@3.22.4:
|
||||||
resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==}
|
resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==}
|
||||||
|
|||||||
Reference in New Issue
Block a user