/** * Base Stream Endpoint * * Abstract base class for stream endpoints, following patterns from BaseBackendDomainService. * Each endpoint manages a set of channels and their event handlers. * * @example * ```typescript * class FilesStreamEndpoint extends BaseStreamEndpoint { * static readonly endpointKey = 'files'; * * protected createChannelController(): BaseChannelController { * return new FilesChannelController(); * } * * initialize(): void { * this.channelController.initialize(this.broadcaster); * this.logger.info('FilesStreamEndpoint initialized'); * } * * static async create( * config: StreamEndpointConfig, * options: StreamEndpointCreateOptions * ): Promise { * return new FilesStreamEndpoint(config, options); * } * } * ``` */ import { CoreLogger } from '@plyaz/logger'; import type { StreamEndpointConfig, StreamEndpointCreateOptions, StreamBroadcasterInterface, StreamResponse, StreamErrorEvent, StreamChannel, StreamConnection, StreamAuthResult } from '@plyaz/types/core'; import type { BaseChannelController } from '../channels/BaseChannelController'; /** * BaseStreamEndpoint - Abstract base for stream endpoints * * Similar to BaseBackendDomainService but for real-time streaming. * Provides: * - Static create() for initialization (like domain services) * - Lifecycle hooks (beforeSubscribe, afterSubscribe, etc.) * - Channel controller management * - Error handling with standard ErrorResponse format * - Response formatting consistent with API patterns */ export declare abstract class BaseStreamEndpoint { /** * Static endpoint key (like serviceKey in domain services). * Override in subclass. */ static readonly endpointKey: string; /** Logger instance */ protected readonly logger: CoreLogger; /** Endpoint configuration */ protected readonly config: StreamEndpointConfig; /** Broadcaster interface */ protected readonly broadcaster: StreamBroadcasterInterface; /** Channel controller for this endpoint */ protected readonly channelController: BaseChannelController; /** Unsubscribe functions for cleanup */ protected readonly unsubscribers: Array<() => void>; constructor(config: StreamEndpointConfig, options: StreamEndpointCreateOptions); /** * Create the channel controller for this endpoint. * Called during construction. */ protected abstract createChannelController(): BaseChannelController; /** * Initialize event subscriptions. * Called by StreamRegistry during initialization. */ abstract initialize(): void; /** * Create endpoint instance. * Override in subclass with proper typing. * * @param config - Endpoint configuration * @param options - Create options (broadcaster, auth, etc.) * @returns Promise resolving to endpoint instance */ static create(_config: StreamEndpointConfig, _options: StreamEndpointCreateOptions): Promise; /** * Called before a connection subscribes to a channel. * Return false to prevent subscription. * * @param channel - Channel being subscribed to * @param connection - Connection subscribing * @param authResult - Authentication result for the connection * @returns true to allow subscription, false to deny */ protected beforeSubscribe?(channel: StreamChannel, connection: StreamConnection, authResult: StreamAuthResult): Promise; /** * Called after a connection subscribes to a channel. * * @param channel - Channel subscribed to * @param connection - Connection that subscribed */ protected afterSubscribe?(channel: StreamChannel, connection: StreamConnection): Promise; /** * Called before a connection unsubscribes from a channel. * * @param channel - Channel being unsubscribed from * @param connectionId - Connection ID unsubscribing */ protected beforeUnsubscribe?(channel: StreamChannel, connectionId: string): Promise; /** * Called when a new connection is established. * * @param connection - The new connection */ protected onConnect?(connection: StreamConnection): Promise; /** * Called when a connection is closed. * * @param connectionId - ID of the closed connection */ protected onDisconnect?(connectionId: string): Promise; /** * Create a success response. * * @param data - Response data payload * @param channel - Channel this response is for * @param message - Human-readable message (default: 'Success') * @returns Formatted StreamResponse */ protected createResponse(data: T, channel: string, message?: string): StreamResponse; /** * Create an error response using standard ErrorResponse format. * * @param errors - Array of error details * @param channel - Channel this error occurred on * @returns Formatted StreamErrorEvent */ protected createErrorResponse(errors: Array<{ errorCode: string; message: string; field?: string; }>, channel: string): StreamErrorEvent; /** * Get endpoint configuration. */ getConfig(): StreamEndpointConfig; /** * Get the endpoint key. */ getEndpointKey(): string; /** * Get channel controller. */ getChannelController(): BaseChannelController; /** * Check if endpoint handles a channel. * * @param channel - Channel to check * @returns true if this endpoint handles the channel */ handlesChannel(channel: StreamChannel): boolean; /** * Check if endpoint is enabled. */ isEnabled(): boolean; /** * Check if endpoint supports the current runtime. * * @param runtime - Runtime to check * @returns true if supported */ supportsRuntime(runtime: 'node' | 'bun' | 'deno' | 'edge'): boolean; /** * Dispose endpoint and cleanup. */ dispose(): void; /** * Add an unsubscribe function to be called on dispose. * * @param unsub - Unsubscribe function */ protected addUnsubscriber(unsub: () => void): void; } //# sourceMappingURL=BaseStreamEndpoint.d.ts.map