Barazo AppView backend barazo.forum
at main 89 lines 2.3 kB view raw
1import type { FastifyInstance } from 'fastify' 2 3export interface RegistryPlugin { 4 name: string 5 displayName: string 6 description: string 7 version: string 8 source: 'core' | 'official' | 'community' | 'experimental' 9 category: string 10 barazoVersion: string 11 author: { name: string; url?: string } 12 license: string 13 npmUrl: string 14 repositoryUrl?: string 15 approved: boolean 16 featured: boolean 17 downloads: number 18} 19 20interface RegistryIndex { 21 version: number 22 updatedAt: string 23 plugins: RegistryPlugin[] 24} 25 26const REGISTRY_URL = 'https://registry.barazo.forum/index.json' 27const CACHE_KEY = 'plugin:registry:index' 28const CACHE_TTL = 3600 // 1 hour 29 30export async function getRegistryIndex(app: FastifyInstance): Promise<RegistryPlugin[]> { 31 const { cache } = app 32 33 const cached = await cache.get(CACHE_KEY) 34 if (cached) { 35 try { 36 const parsed = JSON.parse(cached) as RegistryIndex 37 return parsed.plugins 38 } catch { 39 // Invalid cache entry, fetch fresh 40 } 41 } 42 43 try { 44 const response = await fetch(REGISTRY_URL, { 45 signal: AbortSignal.timeout(10_000), 46 }) 47 if (!response.ok) { 48 app.log.warn({ status: response.status }, 'Failed to fetch plugin registry') 49 return [] 50 } 51 const data = (await response.json()) as RegistryIndex 52 await cache.set(CACHE_KEY, JSON.stringify(data), 'EX', CACHE_TTL) 53 return data.plugins 54 } catch (error) { 55 app.log.warn({ error }, 'Failed to fetch plugin registry') 56 return [] 57 } 58} 59 60export function searchRegistryPlugins( 61 plugins: RegistryPlugin[], 62 params: { q?: string | undefined; category?: string | undefined; source?: string | undefined } 63): RegistryPlugin[] { 64 let results = plugins 65 66 if (params.q) { 67 const query = params.q.toLowerCase() 68 results = results.filter( 69 (p) => 70 p.name.toLowerCase().includes(query) || 71 p.displayName.toLowerCase().includes(query) || 72 p.description.toLowerCase().includes(query) 73 ) 74 } 75 76 if (params.category) { 77 results = results.filter((p) => p.category === params.category) 78 } 79 80 if (params.source) { 81 results = results.filter((p) => p.source === params.source) 82 } 83 84 return results 85} 86 87export function getFeaturedPlugins(plugins: RegistryPlugin[]): RegistryPlugin[] { 88 return plugins.filter((p) => p.featured) 89}