UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

58 lines (56 loc) • 1.97 kB
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { homedir } from "node:os"; //#region src/auth-session.ts const SESSION_FILE_NAME = "session.json"; function getSessionFilePath(configDir) { return join(configDir, ".bos", SESSION_FILE_NAME); } function readSessionHandle(configDir) { const filePath = getSessionFilePath(configDir); if (!existsSync(filePath)) return null; try { const parsed = JSON.parse(readFileSync(filePath, "utf-8")); if (parsed.version !== 1) return null; return { version: 1, credential: parsed.credential ?? null, publishKey: parsed.publishKey ?? null, delegateKey: parsed.delegateKey ?? null }; } catch { return null; } } function writeSessionHandle(configDir, session) { const bosDir = join(configDir, ".bos"); mkdirSync(bosDir, { recursive: true }); writeFileSync(getSessionFilePath(configDir), `${JSON.stringify(session, null, 2)}\n`, { mode: 384 }); } function updateSessionHandle(configDir, update) { const next = update(readSessionHandle(configDir) ?? { version: 1, credential: null, publishKey: null, delegateKey: null }); writeSessionHandle(configDir, next); return next; } function deleteSessionHandle(configDir) { const filePath = getSessionFilePath(configDir); if (existsSync(filePath)) rmSync(filePath); } function nearCredentialsPath(network, account) { return join(homedir(), ".near-credentials", network, `${account}.json`); } function removePublishedKeyFile(network, account, publicKey) { const filePath = nearCredentialsPath(network, account); if (!existsSync(filePath)) return; try { if (JSON.parse(readFileSync(filePath, "utf-8")).public_key === publicKey) rmSync(filePath); } catch {} } //#endregion export { deleteSessionHandle, getSessionFilePath, nearCredentialsPath, readSessionHandle, removePublishedKeyFile, updateSessionHandle, writeSessionHandle }; //# sourceMappingURL=auth-session.mjs.map