import { RegistryStorage } from './core/classes.registrystorage.js'; import { AuthManager } from './core/classes.authmanager.js'; import { BaseRegistry } from './core/classes.baseregistry.js'; import type { IRegistryConfig, IRequestContext, IResponse } from './core/interfaces.core.js'; import { OciRegistry } from './oci/classes.ociregistry.js'; import { NpmRegistry } from './npm/classes.npmregistry.js'; /** * Main registry orchestrator * Routes requests to appropriate protocol handlers (OCI or NPM) */ export class SmartRegistry { private storage: RegistryStorage; private authManager: AuthManager; private registries: Map = new Map(); private config: IRegistryConfig; private initialized: boolean = false; constructor(config: IRegistryConfig) { this.config = config; this.storage = new RegistryStorage(config.storage); this.authManager = new AuthManager(config.auth); } /** * Initialize the registry system */ public async init(): Promise { if (this.initialized) return; // Initialize storage await this.storage.init(); // Initialize auth manager await this.authManager.init(); // Initialize OCI registry if enabled if (this.config.oci?.enabled) { const ociBasePath = this.config.oci.basePath || '/oci'; const ociRegistry = new OciRegistry(this.storage, this.authManager, ociBasePath); await ociRegistry.init(); this.registries.set('oci', ociRegistry); } // Initialize NPM registry if enabled if (this.config.npm?.enabled) { const npmBasePath = this.config.npm.basePath || '/npm'; const registryUrl = `http://localhost:5000${npmBasePath}`; // TODO: Make configurable const npmRegistry = new NpmRegistry(this.storage, this.authManager, npmBasePath, registryUrl); await npmRegistry.init(); this.registries.set('npm', npmRegistry); } this.initialized = true; } /** * Handle an incoming HTTP request * Routes to the appropriate protocol handler based on path */ public async handleRequest(context: IRequestContext): Promise { const path = context.path; // Route to OCI registry if (this.config.oci?.enabled && path.startsWith(this.config.oci.basePath)) { const ociRegistry = this.registries.get('oci'); if (ociRegistry) { return ociRegistry.handleRequest(context); } } // Route to NPM registry if (this.config.npm?.enabled && path.startsWith(this.config.npm.basePath)) { const npmRegistry = this.registries.get('npm'); if (npmRegistry) { return npmRegistry.handleRequest(context); } } // No matching registry return { status: 404, headers: { 'Content-Type': 'application/json' }, body: { error: 'NOT_FOUND', message: 'No registry handler for this path', }, }; } /** * Get the storage instance (for testing/advanced use) */ public getStorage(): RegistryStorage { return this.storage; } /** * Get the auth manager instance (for testing/advanced use) */ public getAuthManager(): AuthManager { return this.authManager; } /** * Get a specific registry handler */ public getRegistry(protocol: 'oci' | 'npm'): BaseRegistry | undefined { return this.registries.get(protocol); } /** * Check if the registry is initialized */ public isInitialized(): boolean { return this.initialized; } }