// 本机凭据:~/.config/streamline/credentials.json(目录 0700、文件 0600,先写临时文件再改名)。 // 按 API 地址分开存,同一台电脑可以同时连生产和自建环境。 // 里面有登录会话的明文 —— 命令的输出里永远不打印它。 import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; export const DEFAULT_API = "https://streamline-internal-api.bytedance.net"; export const DEFAULT_WEB = "https://streamline.bytedance.net"; export function apiBase() { return (process.env.STREAMLINE_API_URL || DEFAULT_API).replace(/\/+$/, ""); } export function webBase() { return (process.env.STREAMLINE_WEB_URL || DEFAULT_WEB).replace(/\/+$/, ""); } function dir() { const base = process.env.XDG_CONFIG_HOME || join(homedir(), ".config"); return join(base, "streamline"); } function file() { return join(dir(), "credentials.json"); } function readAll() { if (!existsSync(file())) return {}; try { return JSON.parse(readFileSync(file(), "utf8")) || {}; } catch { return {}; } } function writeAll(all) { mkdirSync(dir(), { recursive: true, mode: 0o700 }); chmodSync(dir(), 0o700); const tmp = `${file()}.${process.pid}.tmp`; writeFileSync(tmp, JSON.stringify(all, null, 2) + "\n", { mode: 0o600 }); renameSync(tmp, file()); } /** 当前 API 地址下的记录:{ session?: {token,email,expiresAt} }(老版本存过的 keys 字段不再使用,原样留着无害)。 */ export function load() { return readAll()[apiBase()] || {}; } export function save(update) { const all = readAll(); all[apiBase()] = update(all[apiBase()] || {}); writeAll(all); } export function credentialsPath() { return file(); }