s3si.ts/src/state.ts

51 lines
1.2 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> {
const decoder = new TextDecoder();
const data = await Deno.readFile(this.path);
const json = JSON.parse(decoder.decode(data));
return json;
}
async write(newState: State): Promise<void> {
const encoder = new TextEncoder();
const data = encoder.encode(JSON.stringify(newState, undefined, 2));
const swapPath = `${this.path}.swap`;
await Deno.writeFile(swapPath, data);
await Deno.rename(swapPath, this.path);
}
}