check / check (push) Successful in 41s
Client.fromJSON checks every snapshot field and each key's decoded length and throws an error naming the bad field. toJSON reads the token through a new ApiClient.getAuthToken and throws when there is none. logout zeroes the key buffers in place; collectionsSince re-checks for logout after its request so it never decrypts with zeroed keys. The CLI now reports a corrupt session file separately from a missing one. Model: opus-5-5
27 lines
971 B
TypeScript
27 lines
971 B
TypeScript
// How the CLI reads its saved session file back into a `Client`.
|
|
//
|
|
// A missing file means "not logged in" and returns null. A file that exists but
|
|
// cannot be read back into a client (bad JSON, a missing field, a key of the
|
|
// wrong length) throws an error saying the session file is corrupt, so the CLI
|
|
// can tell the user which of the two it is. Needs `init()` first.
|
|
|
|
import { existsSync, readFileSync } from "node:fs";
|
|
import type { ApiClientOptions } from "./api/client.js";
|
|
import { Client } from "./client.js";
|
|
|
|
export const loadSession = (
|
|
path: string,
|
|
apiOptions?: ApiClientOptions,
|
|
): Client | null => {
|
|
if (!existsSync(path)) return null;
|
|
try {
|
|
return Client.fromJSON(
|
|
JSON.parse(readFileSync(path, "utf-8")),
|
|
apiOptions,
|
|
);
|
|
} catch (err) {
|
|
const reason = err instanceof Error ? err.message : String(err);
|
|
throw new Error(`Session file ${path} is corrupt: ${reason}`);
|
|
}
|
|
};
|