You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import type { BrowserContext } from "playwright";
|
|
import { DEFAULT_RPA_STORAGE_STATE_PATH } from "@/lib/rpa/env";
|
|
|
|
export function getStorageStatePath(): string {
|
|
return process.env.RPA_STORAGE_STATE_PATH ?? DEFAULT_RPA_STORAGE_STATE_PATH;
|
|
}
|
|
|
|
export function invalidateStorageState(): void {
|
|
const statePath = getStorageStatePath();
|
|
if (fs.existsSync(statePath)) {
|
|
fs.unlinkSync(statePath);
|
|
}
|
|
}
|
|
|
|
export async function persistContext(context: BrowserContext): Promise<void> {
|
|
const statePath = getStorageStatePath();
|
|
const dir = path.dirname(statePath);
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
await context.storageState({ path: statePath });
|
|
}
|
|
|
|
export function storageStateExists(): boolean {
|
|
return fs.existsSync(getStorageStatePath());
|
|
}
|
|
|
|
export function getStorageStateForContext(
|
|
overridePath?: string,
|
|
): { storageState: string } | Record<string, never> {
|
|
const candidate = overridePath?.trim() || getStorageStatePath();
|
|
if (fs.existsSync(candidate)) {
|
|
return { storageState: candidate };
|
|
}
|
|
return {};
|
|
}
|