s3si.ts/src/state.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-10-18 08:08:26 -04:00
export type LoginState = {
2022-10-18 20:36:58 -04:00
sessionToken?: string;
gToken?: string;
bulletToken?: string;
2022-10-18 08:08:26 -04:00
};
2022-10-18 09:16:51 -04:00
export type State = {
loginState?: LoginState;
2022-10-18 20:36:58 -04:00
fGen: string;
appUserAgent?: string;
2022-10-19 04:56:18 -04:00
userLang?: string;
userCountry?: string;
2022-10-20 09:45:59 -04:00
cacheDir: string;
// Exporter config
statInkApiKey?: string;
fileExportPath: string;
2022-10-22 18:52:08 -04:00
monitorInterval: number;
2022-10-18 09:16:51 -04:00
};
2022-10-18 20:36:58 -04:00
export const DEFAULT_STATE: State = {
2022-10-20 09:45:59 -04:00
cacheDir: "./cache",
2022-10-18 20:36:58 -04:00
fGen: "https://api.imink.app/f",
2022-10-20 09:45:59 -04:00
fileExportPath: "./export",
2022-10-22 18:52:08 -04:00
monitorInterval: 500,
2022-10-18 20:36:58 -04:00
};
2022-10-23 04:25:10 -04:00
export type StateBackend = {
read: () => Promise<State>;
write: (newState: State) => Promise<void>;
};
export class FileStateBackend implements StateBackend {
constructor(private path: string) {}
async read(): Promise<State> {
2022-10-24 08:47:25 -04:00
const data = await Deno.readTextFile(this.path);
const json = JSON.parse(data);
2022-10-23 04:25:10 -04:00
return json;
}
async write(newState: State): Promise<void> {
2022-10-24 08:47:25 -04:00
const data = JSON.stringify(newState, undefined, 2);
2022-10-23 04:25:10 -04:00
const swapPath = `${this.path}.swap`;
2022-10-24 08:47:25 -04:00
await Deno.writeTextFile(swapPath, data);
2022-10-23 04:25:10 -04:00
await Deno.rename(swapPath, this.path);
}
}