readSessions

tcmts.atproto.readSessions · #r3qqhecb · term · runs on the server

uses node:fs

The kept sign-ins: from a file this user owns, not a link, which only they can read (it's made so if others could).

import { Refused, sessionsFile } from "#codebase/tcmts/atproto";
import type { KeptSessions } from "#codebase/tcmts/atproto";
import fs from "node:fs";

export function readSessions(): KeptSessions {
  const file = sessionsFile();
  let st: fs.Stats;
  try {
    st = fs.lstatSync(file);
  } catch {
    return { sessions: {} };
  }
  if (!st.isFile() || (process.getuid !== undefined && st.uid !== process.getuid()))
    throw new Refused(
      `${file} isn't a file of yours: tcm won't read sign-ins from it (remove it to sign in again)`,
    );
  if (process.platform !== "win32" && st.mode & 0o077) fs.chmodSync(file, 0o600);
  try {
    return JSON.parse(fs.readFileSync(file, "utf8")) as KeptSessions;
  } catch {
    return { sessions: {} };
  }
}

uses

used by