UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

235 lines 8.71 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionStorage = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); const session_types_1 = require("../types/session.types"); class SessionStorage { constructor(config) { this.config = { ...session_types_1.DEFAULT_SESSION_CONFIG, ...config }; this.sessionDir = this.getSessionDirectory(); } getSessionDirectory() { const homeDir = process.env['HOME'] || os.homedir(); return path.join(homeDir, this.config.sessionDir); } ensureSessionDir() { if (!fs.existsSync(this.sessionDir)) { fs.mkdirSync(this.sessionDir, { recursive: true, mode: 0o700 }); } } generateTerminalId() { const sessionId = process.env['TERM_SESSION_ID']; if (sessionId) { const hash = this.simpleHash(sessionId); return `term-${hash}`; } const term = process.env['TERM'] || 'unknown'; const shell = process.env['SHELL'] || ''; const ppid = process.ppid || 0; const baseId = `${ppid}-${term}`; const hash = this.simpleHash(baseId + shell); return `term-${hash}`; } simpleHash(str) { let hash = 0; for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } return Math.abs(hash).toString(36); } getSessionFilePath() { const terminalId = this.generateTerminalId(); const fileName = `${this.config.filePrefix}-${terminalId}.json`; return path.join(this.sessionDir, fileName); } setSessionAccount(accountName) { try { process.env[this.config.envVarName] = accountName; const sessionState = { accountName, processId: process.pid, terminalId: this.generateTerminalId(), setAt: new Date(), ...(this.config.expirationMs && { expiresAt: new Date(Date.now() + this.config.expirationMs) }) }; this.ensureSessionDir(); const sessionFile = this.getSessionFilePath(); fs.writeFileSync(sessionFile, JSON.stringify(sessionState, null, 2), { mode: 0o600 }); return true; } catch (error) { console.warn(`Warning: Could not persist session state: ${error instanceof Error ? error.message : 'Unknown error'}`); return false; } } getSessionAccount() { try { const envAccount = process.env[this.config.envVarName]; if (envAccount) { return envAccount; } const sessionFile = this.getSessionFilePath(); if (fs.existsSync(sessionFile)) { const sessionData = JSON.parse(fs.readFileSync(sessionFile, 'utf8')); const sessionState = this.parseSessionState(sessionData); if (sessionState && this.isSessionValid(sessionState)) { process.env[this.config.envVarName] = sessionState.accountName; return sessionState.accountName; } } return null; } catch (error) { console.warn(`Warning: Could not read session state: ${error instanceof Error ? error.message : 'Unknown error'}`); return null; } } clearSessionAccount() { try { delete process.env[this.config.envVarName]; const sessionFile = this.getSessionFilePath(); if (fs.existsSync(sessionFile)) { fs.unlinkSync(sessionFile); } return true; } catch (error) { console.warn(`Warning: Could not clear session state: ${error instanceof Error ? error.message : 'Unknown error'}`); return false; } } getSessionState() { try { const accountName = this.getSessionAccount(); if (!accountName) { return null; } const sessionFile = this.getSessionFilePath(); if (fs.existsSync(sessionFile)) { const sessionData = JSON.parse(fs.readFileSync(sessionFile, 'utf8')); return this.parseSessionState(sessionData); } return { accountName, processId: process.pid, terminalId: this.generateTerminalId(), setAt: new Date() }; } catch (error) { console.warn(`Warning: Could not get session state: ${error instanceof Error ? error.message : 'Unknown error'}`); return null; } } parseSessionState(data) { try { if (!data || typeof data !== 'object') { return null; } const sessionState = { accountName: data.accountName, processId: data.processId, terminalId: data.terminalId, setAt: new Date(data.setAt), ...(data.expiresAt && { expiresAt: new Date(data.expiresAt) }) }; return this.isSessionValid(sessionState) ? sessionState : null; } catch { return null; } } isSessionValid(sessionState) { try { if (!sessionState.accountName || !sessionState.processId || !sessionState.terminalId) { return false; } if (sessionState.expiresAt && sessionState.expiresAt < new Date()) { return false; } return true; } catch { return false; } } cleanupExpiredSessions() { try { if (!fs.existsSync(this.sessionDir)) { return 0; } const files = fs.readdirSync(this.sessionDir); let cleanedCount = 0; for (const file of files) { if (!file.startsWith(this.config.filePrefix)) { continue; } const filePath = path.join(this.sessionDir, file); try { const sessionData = JSON.parse(fs.readFileSync(filePath, 'utf8')); const sessionState = this.parseSessionState(sessionData); if (!sessionState || !this.isSessionValid(sessionState)) { fs.unlinkSync(filePath); cleanedCount++; } } catch { fs.unlinkSync(filePath); cleanedCount++; } } return cleanedCount; } catch (error) { console.warn(`Warning: Could not cleanup expired sessions: ${error instanceof Error ? error.message : 'Unknown error'}`); return 0; } } getSessionDir() { return this.sessionDir; } getEnvVarName() { return this.config.envVarName; } } exports.SessionStorage = SessionStorage; //# sourceMappingURL=session-storage.js.map