|
| 1 | +import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, statSync, unlinkSync, writeFileSync } from "node:fs"; |
| 2 | +import { homedir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | + |
| 5 | +const STATE_DIR = |
| 6 | + process.env.WEB_SHELL_STATE_DIR ?? join(homedir(), ".cache", "web-shell", "sessions"); |
| 7 | + |
| 8 | +export function ensureStateDir(): string { |
| 9 | + mkdirSync(STATE_DIR, { recursive: true }); |
| 10 | + return STATE_DIR; |
| 11 | +} |
| 12 | + |
| 13 | +export function socketPath(id: string): string { |
| 14 | + return join(ensureStateDir(), `${id}.sock`); |
| 15 | +} |
| 16 | + |
| 17 | +export function metaPath(id: string): string { |
| 18 | + return join(ensureStateDir(), `${id}.json`); |
| 19 | +} |
| 20 | + |
| 21 | +export function logPath(id: string): string { |
| 22 | + return join(ensureStateDir(), `${id}.log`); |
| 23 | +} |
| 24 | + |
| 25 | +export function pidPath(id: string): string { |
| 26 | + return join(ensureStateDir(), `${id}.pid`); |
| 27 | +} |
| 28 | + |
| 29 | +export interface SessionMeta { |
| 30 | + readonly id: string; |
| 31 | + readonly title: string; |
| 32 | + readonly shell: string; |
| 33 | + readonly createdAt: number; |
| 34 | +} |
| 35 | + |
| 36 | +export function writeMeta(meta: SessionMeta): void { |
| 37 | + try { |
| 38 | + writeFileSync(metaPath(meta.id), JSON.stringify(meta)); |
| 39 | + } catch { |
| 40 | + // ignore |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export function readMeta(id: string): SessionMeta | null { |
| 45 | + try { |
| 46 | + return JSON.parse(readFileSync(metaPath(id), "utf8")) as SessionMeta; |
| 47 | + } catch { |
| 48 | + return null; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function isSocket(p: string): boolean { |
| 53 | + try { |
| 54 | + return statSync(p).isSocket(); |
| 55 | + } catch { |
| 56 | + return false; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export function listSessionIds(): string[] { |
| 61 | + ensureStateDir(); |
| 62 | + try { |
| 63 | + return readdirSync(STATE_DIR) |
| 64 | + .filter((f) => f.endsWith(".sock")) |
| 65 | + .map((f) => f.slice(0, -".sock".length)) |
| 66 | + .filter((id) => isSocket(socketPath(id))); |
| 67 | + } catch { |
| 68 | + return []; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export function readLogTail(id: string, maxBytes: number): string { |
| 73 | + const p = logPath(id); |
| 74 | + try { |
| 75 | + const size = statSync(p).size; |
| 76 | + const start = Math.max(0, size - maxBytes); |
| 77 | + const len = size - start; |
| 78 | + const fd = openSync(p, "r"); |
| 79 | + try { |
| 80 | + const buf = Buffer.alloc(len); |
| 81 | + readSync(fd, buf, 0, len, start); |
| 82 | + return buf.toString("utf8"); |
| 83 | + } finally { |
| 84 | + closeSync(fd); |
| 85 | + } |
| 86 | + } catch { |
| 87 | + return ""; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export function writeLog(id: string, snapshot: string): void { |
| 92 | + try { |
| 93 | + writeFileSync(logPath(id), snapshot); |
| 94 | + } catch { |
| 95 | + // ignore |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export function appendLog(id: string, chunk: string, maxBytes: number): void { |
| 100 | + const p = logPath(id); |
| 101 | + try { |
| 102 | + const size = existsSync(p) ? statSync(p).size : 0; |
| 103 | + if (size + chunk.length > maxBytes * 2) { |
| 104 | + const tail = readLogTail(id, maxBytes); |
| 105 | + writeFileSync(p, tail + chunk); |
| 106 | + } else { |
| 107 | + appendFileSync(p, chunk); |
| 108 | + } |
| 109 | + } catch { |
| 110 | + // ignore |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +export function sessionExists(id: string): boolean { |
| 115 | + return existsSync(socketPath(id)) && isSocket(socketPath(id)); |
| 116 | +} |
| 117 | + |
| 118 | +export function readPid(id: string): number | null { |
| 119 | + try { |
| 120 | + const n = Number(readFileSync(pidPath(id), "utf8").trim()); |
| 121 | + return Number.isFinite(n) && n > 0 ? n : null; |
| 122 | + } catch { |
| 123 | + return null; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +export function cleanupFiles(id: string): void { |
| 128 | + for (const p of [socketPath(id), metaPath(id), logPath(id), pidPath(id)]) { |
| 129 | + try { |
| 130 | + unlinkSync(p); |
| 131 | + } catch { |
| 132 | + // ignore |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +export function killShell(id: string): void { |
| 138 | + const pid = readPid(id); |
| 139 | + if (pid) { |
| 140 | + try { |
| 141 | + process.kill(pid, "SIGHUP"); |
| 142 | + } catch { |
| 143 | + // already gone |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments