Skip to content

Commit

Permalink
refactor: update browser strategy implementations and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mherod committed Jan 3, 2025
1 parent 92946ae commit ad7e035
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 57 deletions.
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { homedir } from "os";

import { config } from "dotenv";
import { z } from "zod";

Expand Down Expand Up @@ -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(),
});
19 changes: 9 additions & 10 deletions src/core/browsers/chrome/ChromeApplicationSupport.ts
Original file line number Diff line number Diff line change
@@ -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");
})();
20 changes: 8 additions & 12 deletions src/core/browsers/firefox/FirefoxCookieQueryStrategy.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
Expand Down
31 changes: 8 additions & 23 deletions src/core/browsers/safari/SafariCookieQueryStrategy.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -115,12 +99,13 @@ export class SafariCookieQueryStrategy implements CookieQueryStrategy {
name: string,
domain: string,
): Promise<ExportedCookie[]> {
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));
}
}
16 changes: 5 additions & 11 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { homedir } from "os";

import { merge } from "lodash-es";

/**
Expand All @@ -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()");
}

0 comments on commit ad7e035

Please sign in to comment.