repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/bak/state.ts
RawFile outline
- HostIdentity line 13
- hostId line 14
- alias line 15
- SessionRecord line 18
- id line 19
- key line 20
- name line 21
- cwd line 22
- created line 23
- HostIndex line 26
- version line 27
- sessions line 28
- HostCatalog line 31
- version line 32
- hosts line 33
- CatalogCache line 36
- refreshedAt line 37
- catalog line 38
- indexes line 39
- StatePaths line 42
- identity line 43
- cache line 44
- pendingDir line 45
- platformRoot line 48
- statePaths line 84
- normalizeAlias line 101
- newIdentity line 108
- readJson line 112
- PendingEntry line 121
- id line 122
- path line 123
- pendingMarker line 126
- markPending line 130
- clearPending line 139
- readPending line 148
- id line 159
- path line 159
- writeJsonExclusive line 172
- atomicWriteJson line 189
import { randomUUID } from "node:crypto";
import {
mkdir,
readdir,
readFile,
rename,
unlink,
writeFile,
} from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, join, posix, win32 } from "node:path";
export interface HostIdentity {
hostId: string;
alias: string;
}
export interface SessionRecord {
id: string;
key: string;
name?: string;
cwd: string;
created: string;
}
export interface HostIndex extends HostIdentity {
version: 1;
sessions: SessionRecord[];
}
export interface HostCatalog {
version: 1;
hosts: HostIdentity[];
}
export interface CatalogCache {
refreshedAt: string;
catalog: HostCatalog;
indexes: HostIndex[];
}
export interface StatePaths {
identity: string;
cache: string;
pendingDir: string;
}
function platformRoot(
kind: "state" | "cache",
env: NodeJS.ProcessEnv,
platform: NodeJS.Platform,
home: string,
): string {
if (platform === "win32") {
const paths = win32;
const override = kind === "state" ? env.XDG_STATE_HOME : env.XDG_CACHE_HOME;
if (override?.trim()) return paths.join(override, "pi", "bak");
return paths.join(
env.LOCALAPPDATA ?? paths.join(home, "AppData", "Local"),
"pi",
"bak",
kind,
);
}
if (platform === "darwin") {
const override = kind === "state" ? env.XDG_STATE_HOME : env.XDG_CACHE_HOME;
if (override?.trim()) return posix.join(override, "pi", "bak");
return posix.join(
home,
"Library",
kind === "state" ? "Application Support" : "Caches",
"pi",
"bak",
);
}
const override = kind === "state" ? env.XDG_STATE_HOME : env.XDG_CACHE_HOME;
const fallback =
kind === "state"
? posix.join(home, ".local", "state")
: posix.join(home, ".cache");
return posix.join(override?.trim() || fallback, "pi", "bak");
}
export function statePaths(
env: NodeJS.ProcessEnv = process.env,
platform: NodeJS.Platform = process.platform,
home = homedir(),
): StatePaths {
const paths = platform === "win32" ? win32 : posix;
const state = platformRoot("state", env, platform, home);
return {
identity: paths.join(state, "identity.json"),
cache: paths.join(
platformRoot("cache", env, platform, home),
"catalog.json",
),
pendingDir: paths.join(state, "pending"),
};
}
export function normalizeAlias(value: string): string | undefined {
const alias = value.trim().toLowerCase();
return /^[a-z0-9](?:[a-z0-9._-]{0,62}[a-z0-9])?$/.test(alias)
? alias
: undefined;
}
export function newIdentity(alias: string): HostIdentity {
return { hostId: randomUUID(), alias };
}
export async function readJson<T>(path: string): Promise<T | undefined> {
try {
return JSON.parse(await readFile(path, "utf8")) as T;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
throw error;
}
}
export interface PendingEntry {
id: string;
path: string;
}
function pendingMarker(directory: string, id: string): string {
return join(directory, `${id}.json`);
}
export async function markPending(
directory: string,
path: string,
): Promise<PendingEntry> {
const entry = { id: randomUUID(), path };
await atomicWriteJson(pendingMarker(directory, entry.id), entry);
return entry;
}
export async function clearPending(
directory: string,
id: string,
): Promise<void> {
await unlink(pendingMarker(directory, id)).catch((error) => {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
});
}
export async function readPending(directory: string): Promise<PendingEntry[]> {
let names: string[];
try {
names = await readdir(directory);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
throw error;
}
const entries: PendingEntry[] = [];
for (const name of names) {
if (!name.endsWith(".json")) continue;
const marker = await readJson<{ id?: unknown; path?: unknown }>(
join(directory, name),
);
if (
typeof marker?.id === "string" &&
`${marker.id}.json` === name &&
typeof marker.path === "string"
)
entries.push({ id: marker.id, path: marker.path });
}
return entries;
}
export async function writeJsonExclusive(
path: string,
value: unknown,
): Promise<boolean> {
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
try {
await writeFile(path, `${JSON.stringify(value)}\n`, {
mode: 0o600,
flag: "wx",
});
return true;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "EEXIST") return false;
throw error;
}
}
export async function atomicWriteJson(
path: string,
value: unknown,
): Promise<void> {
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
const temporary = join(dirname(path), `.${process.pid}-${randomUUID()}.tmp`);
await writeFile(temporary, `${JSON.stringify(value)}\n`, { mode: 0o600 });
await rename(temporary, path);
}