Spaces:
Sleeping
Sleeping
Download src/core/plugins/plugin-storage.service.ts from jinksQspider/openWa: direct link, hf CLI and curl.
- Browser
- Download file 6.23 kB
-
https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/core/plugins/plugin-storage.service.ts
- Command line
-
hf download hf://spaces/jinksQspider/openWa/src/core/plugins/plugin-storage.service.ts
-
curl -L -o plugin-storage.service.ts https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/core/plugins/plugin-storage.service.ts
6.23 kB
| import { Injectable } from '@nestjs/common'; | |
| import { ConfigService } from '@nestjs/config'; | |
| import * as fs from 'fs'; | |
| import * as path from 'path'; | |
| import { createLogger } from '../../common/services/logger.service'; | |
| import { PluginStatus, PluginStorage, PluginRegistryEntry } from './plugin.interfaces'; | |
| () | |
| export class PluginStorageService { | |
| private readonly logger = createLogger('PluginStorageService'); | |
| private readonly dataDir: string; | |
| private readonly registryPath: string; | |
| private registry: Map<string, PluginRegistryEntry> = new Map(); | |
| constructor(private readonly configService: ConfigService) { | |
| this.dataDir = this.configService.get<string>('dataDir') ?? './data'; | |
| this.registryPath = path.join(this.dataDir, 'plugins', 'registry.json'); | |
| this.loadRegistry(); | |
| } | |
| private loadRegistry(): void { | |
| try { | |
| if (fs.existsSync(this.registryPath)) { | |
| const content = fs.readFileSync(this.registryPath, 'utf-8'); | |
| const entries = JSON.parse(content) as PluginRegistryEntry[]; | |
| this.registry = new Map(entries.map(e => [e.id, e])); | |
| this.logger.debug(`Loaded ${this.registry.size} plugins from registry`, { | |
| action: 'registry_loaded', | |
| }); | |
| } | |
| } catch (error) { | |
| this.logger.error('Failed to load plugin registry', String(error), { | |
| action: 'registry_load_failed', | |
| }); | |
| } | |
| } | |
| private saveRegistry(): void { | |
| try { | |
| const dir = path.dirname(this.registryPath); | |
| if (!fs.existsSync(dir)) { | |
| fs.mkdirSync(dir, { recursive: true }); | |
| } | |
| const entries = Array.from(this.registry.values()); | |
| fs.writeFileSync(this.registryPath, JSON.stringify(entries, null, 2)); | |
| } catch (error) { | |
| this.logger.error('Failed to save plugin registry', String(error), { | |
| action: 'registry_save_failed', | |
| }); | |
| } | |
| } | |
| // ============================================================================ | |
| // Registry Methods | |
| // ============================================================================ | |
| getPluginEntry(pluginId: string): PluginRegistryEntry | undefined { | |
| return this.registry.get(pluginId); | |
| } | |
| setPluginEntry(entry: PluginRegistryEntry): void { | |
| entry.updatedAt = new Date(); | |
| this.registry.set(entry.id, entry); | |
| this.saveRegistry(); | |
| } | |
| deletePluginEntry(pluginId: string): void { | |
| this.registry.delete(pluginId); | |
| this.saveRegistry(); | |
| } | |
| getAllEntries(): PluginRegistryEntry[] { | |
| return Array.from(this.registry.values()); | |
| } | |
| // ============================================================================ | |
| // Status Management | |
| // ============================================================================ | |
| getPluginStatus(pluginId: string): PluginStatus | null { | |
| const entry = this.registry.get(pluginId); | |
| return entry?.status ?? null; | |
| } | |
| setPluginStatus(pluginId: string, status: PluginStatus): void { | |
| const entry = this.registry.get(pluginId); | |
| if (entry) { | |
| entry.status = status; | |
| entry.updatedAt = new Date(); | |
| this.saveRegistry(); | |
| } | |
| } | |
| // ============================================================================ | |
| // Config Management | |
| // ============================================================================ | |
| getPluginConfig(pluginId: string): Record<string, unknown> | null { | |
| const entry = this.registry.get(pluginId); | |
| return entry?.config ?? null; | |
| } | |
| setPluginConfig(pluginId: string, config: Record<string, unknown>): void { | |
| const entry = this.registry.get(pluginId); | |
| if (entry) { | |
| entry.config = config; | |
| entry.updatedAt = new Date(); | |
| this.saveRegistry(); | |
| } | |
| } | |
| // ============================================================================ | |
| // Plugin Data Storage (sandboxed per-plugin storage) | |
| // ============================================================================ | |
| createPluginStorage(pluginId: string): PluginStorage { | |
| const pluginDataDir = path.join(this.dataDir, 'plugins', pluginId); | |
| // Ensure directory exists | |
| if (!fs.existsSync(pluginDataDir)) { | |
| fs.mkdirSync(pluginDataDir, { recursive: true }); | |
| } | |
| const logger = this.logger; | |
| return { | |
| get: <T = unknown>(key: string): Promise<T | null> => { | |
| const filePath = path.join(pluginDataDir, `${key}.json`); | |
| try { | |
| if (fs.existsSync(filePath)) { | |
| const content = fs.readFileSync(filePath, 'utf-8'); | |
| return Promise.resolve(JSON.parse(content) as T); | |
| } | |
| } catch (error) { | |
| logger.error(`Failed to read plugin data: ${pluginId}/${key}`, String(error)); | |
| } | |
| return Promise.resolve(null); | |
| }, | |
| set: <T = unknown>(key: string, value: T): Promise<void> => { | |
| const filePath = path.join(pluginDataDir, `${key}.json`); | |
| try { | |
| fs.writeFileSync(filePath, JSON.stringify(value, null, 2)); | |
| return Promise.resolve(); | |
| } catch (error) { | |
| logger.error(`Failed to write plugin data: ${pluginId}/${key}`, String(error)); | |
| return Promise.reject(new Error(error instanceof Error ? error.message : String(error))); | |
| } | |
| }, | |
| delete: (key: string): Promise<void> => { | |
| const filePath = path.join(pluginDataDir, `${key}.json`); | |
| try { | |
| if (fs.existsSync(filePath)) { | |
| fs.unlinkSync(filePath); | |
| } | |
| return Promise.resolve(); | |
| } catch (error) { | |
| logger.error(`Failed to delete plugin data: ${pluginId}/${key}`, String(error)); | |
| return Promise.reject(new Error(error instanceof Error ? error.message : String(error))); | |
| } | |
| }, | |
| list: (prefix?: string): Promise<string[]> => { | |
| try { | |
| const files = fs.readdirSync(pluginDataDir); | |
| let keys = files.filter(f => f.endsWith('.json')).map(f => f.replace('.json', '')); | |
| if (prefix) { | |
| keys = keys.filter(k => k.startsWith(prefix)); | |
| } | |
| return Promise.resolve(keys); | |
| } catch (error) { | |
| logger.error(`Failed to list plugin data: ${pluginId}`, String(error)); | |
| return Promise.resolve([]); | |
| } | |
| }, | |
| }; | |
| } | |
| } | |