diff --git a/src/config.ts b/src/config.ts index 76a6cc6..598d5f4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,3 +1,5 @@ +import { homedir } from "os"; + import { config } from "dotenv"; import { z } from "zod"; @@ -26,5 +28,5 @@ const EnvironmentSchema = z.object({ */ export const env = EnvironmentSchema.parse({ LOG_LEVEL: process.env.LOG_LEVEL, - HOME: process.env.HOME, + HOME: homedir(), }); diff --git a/src/core/browsers/chrome/ChromeApplicationSupport.ts b/src/core/browsers/chrome/ChromeApplicationSupport.ts index 6e5ec95..ab8b454 100644 --- a/src/core/browsers/chrome/ChromeApplicationSupport.ts +++ b/src/core/browsers/chrome/ChromeApplicationSupport.ts @@ -1,16 +1,15 @@ +import { homedir } from "os"; import { join } from "path"; -import { HOME } from "../../../global"; - /** * The path to Chrome's application support directory on macOS * This constant is used to locate Chrome's profile and cookie storage directories - * @example + * @throws {Error} If unable to determine user's home directory */ -export const chromeApplicationSupport = join( - HOME ?? "", - "Library", - "Application Support", - "Google", - "Chrome", -); +export const chromeApplicationSupport = (() => { + const home = homedir(); + if (!home) { + throw new Error("Unable to determine user home directory"); + } + return join(home, "Library", "Application Support", "Google", "Chrome"); +})(); diff --git a/src/core/browsers/firefox/FirefoxCookieQueryStrategy.ts b/src/core/browsers/firefox/FirefoxCookieQueryStrategy.ts index 7896971..6cf6aa7 100644 --- a/src/core/browsers/firefox/FirefoxCookieQueryStrategy.ts +++ b/src/core/browsers/firefox/FirefoxCookieQueryStrategy.ts @@ -1,10 +1,10 @@ +import { homedir } from "os"; import { join } from "path"; import { sync } from "glob"; import { logDebug, logWarn } from "@utils/logHelpers"; -import { env } from "../../../config"; import { BrowserName, CookieQueryStrategy, @@ -24,22 +24,18 @@ interface FirefoxCookieRow { * @returns An array of file paths to Firefox cookie databases */ function findFirefoxCookieFiles(): string[] { - const files: string[] = []; - const homedir = env.HOME; - - if (typeof homedir !== "string" || homedir.length === 0) { - logWarn("FirefoxCookieQuery", "HOME environment variable not set"); - return files; + const home = homedir(); + if (!home) { + logWarn("FirefoxCookieQuery", "Failed to get home directory"); + return []; } const patterns = [ - join( - homedir, - "Library/Application Support/Firefox/Profiles/*/cookies.sqlite", - ), - join(homedir, ".mozilla/firefox/*/cookies.sqlite"), + join(home, "Library/Application Support/Firefox/Profiles/*/cookies.sqlite"), + join(home, ".mozilla/firefox/*/cookies.sqlite"), ]; + const files: string[] = []; for (const pattern of patterns) { const matches = sync(pattern); files.push(...matches); diff --git a/src/core/browsers/safari/SafariCookieQueryStrategy.ts b/src/core/browsers/safari/SafariCookieQueryStrategy.ts index 9fd6b7a..03a73ec 100644 --- a/src/core/browsers/safari/SafariCookieQueryStrategy.ts +++ b/src/core/browsers/safari/SafariCookieQueryStrategy.ts @@ -1,8 +1,8 @@ +import { homedir } from "os"; import { join } from "path"; import { logError } from "@utils/logHelpers"; -import { env } from "../../../config"; import type { BrowserName, CookieQueryStrategy, @@ -19,30 +19,14 @@ export class SafariCookieQueryStrategy implements CookieQueryStrategy { */ public readonly browserName: BrowserName = "Safari"; - /** - * Gets the user's home directory - * @returns The home directory path or empty string if not found - */ - private getHomeDir(): string { - const homeDir = env.HOME; - if (typeof homeDir !== "string" || homeDir.trim().length === 0) { - logError( - "SafariCookieQueryStrategy", - "HOME environment variable not set", - ); - return ""; - } - return homeDir; - } - /** * Gets the path to Safari's cookie database - * @param homeDir - The user's home directory + * @param home - The user's home directory * @returns Path to the cookie database */ - private getCookieDbPath(homeDir: string): string { + private getCookieDbPath(home: string): string { return join( - homeDir, + home, "Library", "Containers", "com.apple.Safari", @@ -115,12 +99,13 @@ export class SafariCookieQueryStrategy implements CookieQueryStrategy { name: string, domain: string, ): Promise { - const homeDir = this.getHomeDir(); - if (typeof homeDir !== "string" || homeDir.length === 0) { + const home = homedir(); + if (typeof home !== "string" || home.length === 0) { + logError("SafariCookieQueryStrategy", "Failed to get home directory"); return Promise.resolve([]); } - const cookieDbPath = this.getCookieDbPath(homeDir); + const cookieDbPath = this.getCookieDbPath(home); return Promise.resolve(this.decodeCookies(cookieDbPath, name, domain)); } } diff --git a/src/global.ts b/src/global.ts index 24363fc..ad2f32a 100644 --- a/src/global.ts +++ b/src/global.ts @@ -1,3 +1,5 @@ +import { homedir } from "os"; + import { merge } from "lodash-es"; /** @@ -19,23 +21,15 @@ export const env: { [key: string]: string | undefined } = {}; merge(env, process.env); /** - * User's home directory path from environment variables - * @throws Error if HOME environment variable is not set + * User's home directory path from os.homedir() * @example * // Access home directory path * const homePath = HOME; // '/home/username' or 'C:\Users\username' * * // Use in file path construction * const configPath = `${homePath}/.config/app`; - * - * // Error handling - * if (typeof homePath === 'string' && homePath.length > 0) { - * // Use home path - * } else { - * throw new Error('HOME environment variable is not set or empty'); - * } */ -export const HOME: string | undefined = env["HOME"]; +export const HOME = homedir(); if (typeof HOME !== "string" || HOME.length === 0) { - throw new Error("HOME environment variable is not set or empty"); + throw new Error("Failed to get home directory from os.homedir()"); }