UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

207 lines 8.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionStateManager = void 0; const session_types_1 = require("../types/session.types"); const session_storage_1 = require("./session-storage"); const account_config_1 = require("./account-config"); class SessionStateManager { constructor(accountManager, sessionStorage) { this.accountManager = accountManager || new account_config_1.AccountConfigManager(); this.sessionStorage = sessionStorage || new session_storage_1.SessionStorage(); } setSessionAccount(accountName) { try { this.validateAccountName(accountName); if (!this.accountManager.accountExists(accountName)) { const availableAccounts = this.accountManager.listAccounts(); const suggestion = availableAccounts.length > 0 ? `\n\n可用账号: ${availableAccounts.map(acc => acc.name).join(', ')}\n使用 'polyv-cli account add <account-name>' 添加新账号。` : '\n\n当前没有配置任何账号。使用 \'polyv-cli account add <account-name>\' 添加账号。'; return { success: false, message: `账号 '${accountName}' 不存在。${suggestion}` }; } const success = this.sessionStorage.setSessionAccount(accountName); if (!success) { return { success: false, message: '设置会话账号失败,请检查文件权限。' }; } const sessionState = this.sessionStorage.getSessionState(); return { success: true, message: `已切换到账号 '${accountName}',当前终端会话将使用此账号。`, ...(sessionState && { sessionState }) }; } catch (error) { return { success: false, message: error instanceof Error ? error.message : '设置会话账号时发生未知错误' }; } } getCurrentSessionAccount() { return this.sessionStorage.getSessionAccount(); } clearSessionAccount() { try { const currentAccount = this.getCurrentSessionAccount(); if (!currentAccount) { return { success: false, message: '当前终端没有设置会话账号。' }; } const success = this.sessionStorage.clearSessionAccount(); if (!success) { return { success: false, message: '清除会话账号失败,请检查文件权限。' }; } return { success: true, message: `已清除会话账号 '${currentAccount}',当前终端将使用默认配置。` }; } catch (error) { return { success: false, message: error instanceof Error ? error.message : '清除会话账号时发生未知错误' }; } } getSessionState() { return this.sessionStorage.getSessionState(); } getAuthSource(commandLineArgs) { if (commandLineArgs && (commandLineArgs.appId || commandLineArgs.appSecret)) { return { type: 'command-line', description: '命令行参数', priority: 1 }; } const sessionAccount = this.getCurrentSessionAccount(); if (sessionAccount) { return { type: 'session', description: '当前会话账号', accountName: sessionAccount, priority: 2 }; } if (process.env['POLYV_APP_ID'] || process.env['POLYV_APP_SECRET']) { return { type: 'environment', description: '环境变量', priority: 3 }; } return { type: 'config', description: '全局配置', priority: 4 }; } getAuthCredentials(commandLineArgs) { const authSource = this.getAuthSource(commandLineArgs); try { switch (authSource.type) { case 'command-line': { if (commandLineArgs?.appId && commandLineArgs?.appSecret) { return { appId: commandLineArgs.appId, appSecret: commandLineArgs.appSecret, ...(commandLineArgs.userId && { userId: commandLineArgs.userId }), source: authSource }; } break; } case 'session': { if (authSource.accountName) { const account = this.accountManager.getAccount(authSource.accountName); if (account) { return { appId: account.appId, appSecret: account.appSecret, ...(account.userId && { userId: account.userId }), source: authSource }; } } break; } case 'environment': { const envAppId = process.env['POLYV_APP_ID']; const envAppSecret = process.env['POLYV_APP_SECRET']; if (envAppId && envAppSecret) { return { appId: envAppId, appSecret: envAppSecret, ...(process.env['POLYV_USER_ID'] && { userId: process.env['POLYV_USER_ID'] }), source: authSource }; } break; } case 'config': break; } return null; } catch (error) { console.warn(`Warning: Could not get auth credentials from ${authSource.type}: ${error instanceof Error ? error.message : 'Unknown error'}`); return null; } } getAuthStatusMessage(commandLineArgs) { const credentials = this.getAuthCredentials(commandLineArgs); if (credentials) { const sourceDesc = credentials.source.accountName ? `${credentials.source.description} (${credentials.source.accountName})` : credentials.source.description; return `使用认证来源: ${sourceDesc}`; } const sessionAccount = this.getCurrentSessionAccount(); const availableAccounts = this.accountManager.listAccounts(); if (sessionAccount && !this.accountManager.accountExists(sessionAccount)) { return `当前会话账号 '${sessionAccount}' 不存在。请使用 'polyv-cli use <account-name>' 切换到有效账号,或使用 'polyv-cli account add' 添加账号。`; } if (availableAccounts.length === 0) { return '未找到认证信息。请使用以下方式之一设置认证:\n' + '1. 添加账号: polyv-cli account add <account-name>\n' + '2. 设置环境变量: POLYV_APP_ID, POLYV_APP_SECRET\n' + '3. 使用命令行参数: --app-id, --app-secret'; } const accountList = availableAccounts.map(acc => acc.name).join(', '); return `未设置当前会话账号。可用账号: ${accountList}\n` + `使用 'polyv-cli use <account-name>' 切换账号。`; } validateAccountName(accountName) { if (!accountName || typeof accountName !== 'string') { throw new Error('账号名称不能为空'); } if (!session_types_1.SessionStateValidation.accountName.pattern.test(accountName)) { throw new Error('账号名称只能包含字母、数字、下划线和连字符'); } if (accountName.length > session_types_1.SessionStateValidation.accountName.maxLength) { throw new Error(`账号名称不能超过 ${session_types_1.SessionStateValidation.accountName.maxLength} 个字符`); } } cleanupExpiredSessions() { return this.sessionStorage.cleanupExpiredSessions(); } getSessionDir() { return this.sessionStorage.getSessionDir(); } getEnvVarName() { return this.sessionStorage.getEnvVarName(); } } exports.SessionStateManager = SessionStateManager; //# sourceMappingURL=session-state.js.map