Files
smartregistry/ts/classes.smartregistry.ts

223 lines
7.8 KiB
TypeScript
Raw Normal View History

2025-11-19 15:32:00 +00:00
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';
import { MavenRegistry } from './maven/classes.mavenregistry.js';
import { CargoRegistry } from './cargo/classes.cargoregistry.js';
import { ComposerRegistry } from './composer/classes.composerregistry.js';
import { PypiRegistry } from './pypi/classes.pypiregistry.js';
import { RubyGemsRegistry } from './rubygems/classes.rubygemsregistry.js';
2025-11-19 15:16:20 +00:00
/**
2025-11-19 15:32:00 +00:00
* Main registry orchestrator
* Routes requests to appropriate protocol handlers (OCI, NPM, Maven, Cargo, Composer, PyPI, or RubyGems)
2025-11-19 15:16:20 +00:00
*/
export class SmartRegistry {
private storage: RegistryStorage;
2025-11-19 15:32:00 +00:00
private authManager: AuthManager;
private registries: Map<string, BaseRegistry> = new Map();
2025-11-19 15:16:20 +00:00
private config: IRegistryConfig;
private initialized: boolean = false;
constructor(config: IRegistryConfig) {
this.config = config;
this.storage = new RegistryStorage(config.storage);
2025-11-19 15:32:00 +00:00
this.authManager = new AuthManager(config.auth);
2025-11-19 15:16:20 +00:00
}
/**
2025-11-19 15:32:00 +00:00
* Initialize the registry system
2025-11-19 15:16:20 +00:00
*/
public async init(): Promise<void> {
if (this.initialized) return;
2025-11-19 15:32:00 +00:00
// Initialize storage
await this.storage.init();
2025-11-19 15:16:20 +00:00
2025-11-19 15:32:00 +00:00
// Initialize auth manager
await this.authManager.init();
2025-11-19 15:16:20 +00:00
2025-11-19 15:32:00 +00:00
// Initialize OCI registry if enabled
if (this.config.oci?.enabled) {
const ociBasePath = this.config.oci.basePath ?? '/oci';
2025-11-19 15:32:00 +00:00
const ociRegistry = new OciRegistry(this.storage, this.authManager, ociBasePath);
await ociRegistry.init();
this.registries.set('oci', ociRegistry);
2025-11-19 15:16:20 +00:00
}
2025-11-19 15:32:00 +00:00
// Initialize NPM registry if enabled
if (this.config.npm?.enabled) {
const npmBasePath = this.config.npm.basePath ?? '/npm';
2025-11-19 15:32:00 +00:00
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);
2025-11-19 15:16:20 +00:00
}
// Initialize Maven registry if enabled
if (this.config.maven?.enabled) {
const mavenBasePath = this.config.maven.basePath ?? '/maven';
const registryUrl = `http://localhost:5000${mavenBasePath}`; // TODO: Make configurable
const mavenRegistry = new MavenRegistry(this.storage, this.authManager, mavenBasePath, registryUrl);
await mavenRegistry.init();
this.registries.set('maven', mavenRegistry);
}
// Initialize Cargo registry if enabled
if (this.config.cargo?.enabled) {
const cargoBasePath = this.config.cargo.basePath ?? '/cargo';
const registryUrl = `http://localhost:5000${cargoBasePath}`; // TODO: Make configurable
const cargoRegistry = new CargoRegistry(this.storage, this.authManager, cargoBasePath, registryUrl);
await cargoRegistry.init();
this.registries.set('cargo', cargoRegistry);
}
// Initialize Composer registry if enabled
if (this.config.composer?.enabled) {
const composerBasePath = this.config.composer.basePath ?? '/composer';
const registryUrl = `http://localhost:5000${composerBasePath}`; // TODO: Make configurable
const composerRegistry = new ComposerRegistry(this.storage, this.authManager, composerBasePath, registryUrl);
await composerRegistry.init();
this.registries.set('composer', composerRegistry);
}
// Initialize PyPI registry if enabled
if (this.config.pypi?.enabled) {
const pypiBasePath = this.config.pypi.basePath ?? '/pypi';
const registryUrl = `http://localhost:5000`; // TODO: Make configurable
const pypiRegistry = new PypiRegistry(this.storage, this.authManager, pypiBasePath, registryUrl);
await pypiRegistry.init();
this.registries.set('pypi', pypiRegistry);
}
// Initialize RubyGems registry if enabled
if (this.config.rubygems?.enabled) {
const rubygemsBasePath = this.config.rubygems.basePath ?? '/rubygems';
const registryUrl = `http://localhost:5000${rubygemsBasePath}`; // TODO: Make configurable
const rubygemsRegistry = new RubyGemsRegistry(this.storage, this.authManager, rubygemsBasePath, registryUrl);
await rubygemsRegistry.init();
this.registries.set('rubygems', rubygemsRegistry);
}
2025-11-19 15:32:00 +00:00
this.initialized = true;
2025-11-19 15:16:20 +00:00
}
/**
2025-11-19 15:32:00 +00:00
* Handle an incoming HTTP request
* Routes to the appropriate protocol handler based on path
2025-11-19 15:16:20 +00:00
*/
2025-11-19 15:32:00 +00:00
public async handleRequest(context: IRequestContext): Promise<IResponse> {
const path = context.path;
2025-11-19 15:16:20 +00:00
2025-11-19 15:32:00 +00:00
// 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);
2025-11-19 15:16:20 +00:00
}
}
2025-11-19 15:32:00 +00:00
// 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);
2025-11-19 15:16:20 +00:00
}
}
// Route to Maven registry
if (this.config.maven?.enabled && path.startsWith(this.config.maven.basePath)) {
const mavenRegistry = this.registries.get('maven');
if (mavenRegistry) {
return mavenRegistry.handleRequest(context);
}
}
// Route to Cargo registry
if (this.config.cargo?.enabled && path.startsWith(this.config.cargo.basePath)) {
const cargoRegistry = this.registries.get('cargo');
if (cargoRegistry) {
return cargoRegistry.handleRequest(context);
}
}
// Route to Composer registry
if (this.config.composer?.enabled && path.startsWith(this.config.composer.basePath)) {
const composerRegistry = this.registries.get('composer');
if (composerRegistry) {
return composerRegistry.handleRequest(context);
}
}
// Route to PyPI registry (also handles /simple prefix)
if (this.config.pypi?.enabled) {
const pypiBasePath = this.config.pypi.basePath ?? '/pypi';
if (path.startsWith(pypiBasePath) || path.startsWith('/simple')) {
const pypiRegistry = this.registries.get('pypi');
if (pypiRegistry) {
return pypiRegistry.handleRequest(context);
}
}
}
// Route to RubyGems registry
if (this.config.rubygems?.enabled && path.startsWith(this.config.rubygems.basePath)) {
const rubygemsRegistry = this.registries.get('rubygems');
if (rubygemsRegistry) {
return rubygemsRegistry.handleRequest(context);
}
}
2025-11-19 15:32:00 +00:00
// No matching registry
2025-11-19 15:16:20 +00:00
return {
2025-11-19 15:32:00 +00:00
status: 404,
headers: { 'Content-Type': 'application/json' },
body: {
error: 'NOT_FOUND',
message: 'No registry handler for this path',
},
2025-11-19 15:16:20 +00:00
};
}
/**
2025-11-19 15:32:00 +00:00
* Get the storage instance (for testing/advanced use)
2025-11-19 15:16:20 +00:00
*/
2025-11-19 15:32:00 +00:00
public getStorage(): RegistryStorage {
return this.storage;
2025-11-19 15:16:20 +00:00
}
/**
2025-11-19 15:32:00 +00:00
* Get the auth manager instance (for testing/advanced use)
2025-11-19 15:16:20 +00:00
*/
2025-11-19 15:32:00 +00:00
public getAuthManager(): AuthManager {
return this.authManager;
2025-11-19 15:16:20 +00:00
}
/**
2025-11-19 15:32:00 +00:00
* Get a specific registry handler
2025-11-19 15:16:20 +00:00
*/
public getRegistry(protocol: 'oci' | 'npm' | 'maven' | 'cargo' | 'composer' | 'pypi' | 'rubygems'): BaseRegistry | undefined {
2025-11-19 15:32:00 +00:00
return this.registries.get(protocol);
2025-11-19 15:16:20 +00:00
}
/**
2025-11-19 15:32:00 +00:00
* Check if the registry is initialized
2025-11-19 15:16:20 +00:00
*/
2025-11-19 15:32:00 +00:00
public isInitialized(): boolean {
return this.initialized;
2025-11-19 15:16:20 +00:00
}
/**
* Clean up resources (timers, connections, etc.)
*/
public destroy(): void {
for (const registry of this.registries.values()) {
if (typeof (registry as any).destroy === 'function') {
(registry as any).destroy();
}
}
}
2025-11-19 15:16:20 +00:00
}