Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix(common): defer CookieJarService binding (#6478)

Shreyasafc2cf181c16
diff --git a/packages/hoppscotch-common/src/helpers/RequestRunner.ts b/packages/hoppscotch-common/src/helpers/RequestRunner.ts
index 2fea0e63a..c83622e13 100644
--- a/packages/hoppscotch-common/src/helpers/RequestRunner.ts
+++ b/packages/hoppscotch-common/src/helpers/RequestRunner.ts
@@ -70,7 +70,11 @@ import { applyScriptRequestUpdates } from "./experimental-sandbox-integration"
const secretEnvironmentService = getService(SecretEnvironmentService)
const currentEnvironmentValueService = getService(CurrentValueService)
-const cookieJarService = getService(CookieJarService)
+// `getService(CookieJarService)` at module top level would construct
+// the service during ESM evaluation. `onServiceInit` then reads
+// `window.__KERNEL__.store` and throws because `createHoppApp` has
+// not yet called `initKernel(...)` at that point.
+const getCookieJarService = () => getService(CookieJarService)
const kernelInterceptorService = getService(KernelInterceptorService)
const EXPERIMENTAL_SCRIPTING_SANDBOX = useSetting(
@@ -803,7 +807,7 @@ const getCookieJarEntries = () => {
// and returns the same array would produce a pre-vs-post delta
// of "identical" and the mutation would silently drop.
const cookieJarEntries = cloneDeep(
- Array.from(cookieJarService.cookieJar.value.values()).flatMap(
+ Array.from(getCookieJarService().cookieJar.value.values()).flatMap(
(cookies) => cookies
)
)
@@ -812,7 +816,7 @@ const getCookieJarEntries = () => {
}
const cookieKey = (c: { domain: string; name: string; path?: string }) =>
- `${cookieJarService.canonStoreDomain(c.domain)}\u0000${c.name}\u0000${c.path && c.path.length > 0 ? c.path : "/"}`
+ `${getCookieJarService().canonStoreDomain(c.domain)}\u0000${c.name}\u0000${c.path && c.path.length > 0 ? c.path : "/"}`
const applyScriptCookieDelta = async (
preScript: Cookie[],
@@ -843,10 +847,10 @@ const applyScriptCookieDelta = async (
}
if (mutated.length > 0) {
- await cookieJarService.upsertCookies(mutated)
+ await getCookieJarService().upsertCookies(mutated)
}
if (removed.length > 0) {
- await cookieJarService.deleteCookies(removed)
+ await getCookieJarService().deleteCookies(removed)
}
}
diff --git a/packages/hoppscotch-common/src/helpers/__tests__/RequestRunner-cookie-jar-laziness.spec.ts b/packages/hoppscotch-common/src/helpers/__tests__/RequestRunner-cookie-jar-laziness.spec.ts
new file mode 100644
index 000000000..441b67f2c
--- /dev/null
+++ b/packages/hoppscotch-common/src/helpers/__tests__/RequestRunner-cookie-jar-laziness.spec.ts
@@ -0,0 +1,39 @@
+import { describe, expect, test } from "vitest"
+import { readFileSync } from "node:fs"
+import { fileURLToPath } from "node:url"
+import { dirname, resolve } from "node:path"
+
+// `getService(CookieJarService)` at module top level would construct
+// the service during ESM evaluation. `onServiceInit` then calls
+// `Store.init()`, which reads `window.__KERNEL__.store` and throws
+// because `createHoppApp` has not yet called `initKernel(...)`. The
+// catch in `onServiceInit` degrades the service to in-memory mode
+// but logs `[CookieJar] Kernel store unavailable` on every browser
+// load. The lazy form `() => getService(...)` defers construction to
+// first call, by which point `initKernel(...)` has run and
+// `Store.init()` succeeds.
+//
+// This spec pins the source form. A re-import test would re-trigger
+// RequestRunner's circular `i18n → newstore/history → RequestRunner`
+// cycle and fail under vitest's module evaluation.
+//
+// `fileURLToPath(import.meta.url)` keeps the path resolution
+// ESM-portable. A bare `__dirname` works under vitest today but
+// depends on a CJS global the package's `module: "ESNext"` config
+// does not guarantee. `new URL("../X", import.meta.url)` would hit
+// Vite's asset-URL transform and resolve against the dev server
+// origin (`http://localhost:3000/...`).
+const here = dirname(fileURLToPath(import.meta.url))
+
+describe("RequestRunner cookie-jar binding", () => {
+ test("binds CookieJarService lazily on first call", () => {
+ const source = readFileSync(resolve(here, "../RequestRunner.ts"), "utf8")
+ // The optional `\s*:\s*\S+` group catches a typed eager binding
+ // like `const cookieJarService: CookieJarService = getService(...)`
+ // which a plain `^const\s+\w+\s*=` would skip.
+ expect(source).not.toMatch(
+ /^const\s+\w+(?:\s*:\s*\S+)?\s*=\s*getService\s*\(\s*CookieJarService\s*\)/m
+ )
+ expect(source).toMatch(/=>\s*getService\s*\(\s*CookieJarService\s*\)/)
+ })
+})