UNPKG

@cardos/service-bus-portal

Version:

A modular, composable cross-context communication system for Web Workers, iframes, Shared Workers, and Service Workers

223 lines (222 loc) 6.78 kB
//#region src/types.d.ts /** * Communication portal abstract interface */ interface CommunicationPortal { readonly id: string; readonly type: PortalType; send(message: PortalMessage): Promise<void>; onMessage(handler: (message: PortalMessage) => void): void; connect(): Promise<void>; disconnect(): Promise<void>; isConnected(): boolean; getTargetInfo(): PortalTargetInfo; generateMessageId(): string; } /** * Portal type enumeration */ type PortalType = 'window-to-window' | 'window-to-worker' | 'window-to-iframe' | 'worker-to-window' | 'iframe-to-window' | 'shared-worker' | 'service-worker'; /** * Portal message format */ interface PortalMessage { readonly id: string; readonly type: 'invoke' | 'result' | 'error'; readonly timestamp: number; readonly source: string; readonly target: string; readonly data: { key?: string; args?: unknown[]; result?: unknown; error?: string; }; readonly metadata?: Record<string, unknown>; } /** * Portal target information */ interface PortalTargetInfo { readonly type: PortalType; readonly origin?: string; readonly capabilities?: string[]; readonly metadata?: Record<string, unknown>; } /** * Portal configuration */ interface PortalConfig { readonly id: string; readonly type: PortalType; readonly timeoutMs?: number; readonly retryAttempts?: number; readonly security?: PortalSecurityConfig; readonly metadata?: Record<string, unknown>; } /** * Security configuration */ interface PortalSecurityConfig { readonly allowedOrigins?: string[]; readonly allowedTargets?: string[]; readonly requireAuthentication?: boolean; readonly encryptionRequired?: boolean; } //# sourceMappingURL=types.d.ts.map //#endregion //#region src/core.d.ts /** * Base portal implementation */ declare abstract class BasePortal implements CommunicationPortal { readonly id: string; readonly type: PortalType; protected config: PortalConfig; protected messageHandlers: Array<(message: PortalMessage) => void>; protected connected: boolean; protected messageIdCounter: number; constructor(id: string, type: PortalType, config: PortalConfig); abstract send(message: PortalMessage): Promise<void>; abstract connect(): Promise<void>; abstract disconnect(): Promise<void>; abstract getTargetInfo(): PortalTargetInfo; onMessage(handler: (message: PortalMessage) => void): void; isConnected(): boolean; generateMessageId(): string; protected notifyHandlers(message: PortalMessage): void; protected validateMessage(message: PortalMessage): boolean; } /** * PostMessage portal implementation */ declare class PostMessagePortal extends BasePortal { private target; private messageListener; constructor(id: string, direction: PortalType, target: Window | Worker, config: PortalConfig); send(message: PortalMessage): Promise<void>; connect(): Promise<void>; disconnect(): Promise<void>; getTargetInfo(): PortalTargetInfo; private getEventTarget; private getTargetOrigin; } /** * EventTarget portal implementation */ declare class EventTargetPortal extends BasePortal { private eventTarget; private channel; private listener; constructor(id: string, direction: PortalType, eventTarget: EventTarget, channel: string, config: PortalConfig); send(message: PortalMessage): Promise<void>; connect(): Promise<void>; disconnect(): Promise<void>; getTargetInfo(): PortalTargetInfo; } //# sourceMappingURL=core.d.ts.map //#endregion //#region src/service-bus.d.ts /** * Service bus connector based on portal */ declare class PortalServiceBusConnector { private portal; private serviceBus; constructor(portal: CommunicationPortal, serviceBus: { invoke: (key: string, ...args: unknown[]) => unknown; }); connect(): Promise<void>; disconnect(): Promise<void>; private setupMessageHandling; private handleInvoke; } /** * Service bus proxy based on portal */ declare class PortalServiceBusProxy<T = unknown> { private portal; private pending; constructor(portal: CommunicationPortal); connect(): Promise<void>; disconnect(): Promise<void>; private setupMessageHandling; private invoke; createProxy(): T; } //# sourceMappingURL=service-bus.d.ts.map //#endregion //#region src/factory.d.ts /** * Portal factory for creating different types of portals */ declare class PortalFactory { /** * Create a PostMessage-based portal */ static createPostMessagePortal(id: string, direction: PortalType, target: Window | Worker, config?: Partial<PortalConfig>): PostMessagePortal; /** * Create an EventTarget-based portal */ static createEventTargetPortal(id: string, direction: PortalType, eventTarget: EventTarget, channel: string, config?: Partial<PortalConfig>): EventTargetPortal; /** * Create a portal for Web Worker communication * Automatically determines the correct configuration based on current context */ static createWorkerPortal(worker: Worker, config?: Partial<PortalConfig>, portalId?: string): PostMessagePortal; /** * Create a portal for window communication from worker context */ static createWindowPortal(config?: Partial<PortalConfig>, portalId?: string): PostMessagePortal; /** * Create a portal for iframe communication * Automatically determines the correct configuration based on current context */ static createIframePortal(iframe: HTMLIFrameElement, config?: Partial<PortalConfig>): PostMessagePortal; } /** * Portal composer for managing multiple portals */ declare class PortalComposer { private portals; private connectors; private proxies; /** * Add a portal to the composer */ addPortal(portal: CommunicationPortal): void; /** * Remove a portal from the composer */ removePortal(portalId: string): void; /** * Create a service bus connector for a portal */ createConnector(portalId: string, serviceBus: { invoke: (key: string, ...args: unknown[]) => unknown; }): PortalServiceBusConnector; /** * Create a service bus proxy for a portal */ createProxy<T = unknown>(portalId: string): PortalServiceBusProxy<T>; /** * Connect all portals */ connectAll(): Promise<void>; /** * Disconnect all portals */ disconnectAll(): Promise<void>; /** * Get a portal by ID */ getPortal(portalId: string): CommunicationPortal | undefined; /** * List all portals */ listPortals(): CommunicationPortal[]; } //# sourceMappingURL=factory.d.ts.map //#endregion export { BasePortal, CommunicationPortal, EventTargetPortal, PortalComposer, PortalConfig, PortalFactory, PortalMessage, PortalSecurityConfig, PortalServiceBusConnector, PortalServiceBusProxy, PortalTargetInfo, PortalType, PostMessagePortal }; //# sourceMappingURL=index.d.mts.map