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.

30 lines
851 B

import fs from "node:fs";
import { DEFAULT_RPA_STORAGE_STATE_PATH } from "@/lib/rpa/env";
export type PlaywrightStorageState = {
cookies: Array<{
name: string;
value: string;
domain?: string;
}>;
};
export function resolveStorageStatePath(): string {
return process.env.RPA_STORAGE_STATE_PATH?.trim() || DEFAULT_RPA_STORAGE_STATE_PATH;
}
export function loadStorageState(
path = resolveStorageStatePath(),
): PlaywrightStorageState {
if (!fs.existsSync(path)) {
throw new Error(
`缺少 MotherShip 会话文件 ${path};请先运行 npm run record:mothership 录制 cookies`,
);
}
return JSON.parse(fs.readFileSync(path, "utf8")) as PlaywrightStorageState;
}
export function buildCookieHeader(state: PlaywrightStorageState): string {
return state.cookies.map((c) => `${c.name}=${c.value}`).join("; ");
}