/**
 * Server Bridge
 * Connects Service Worker requests to virtual HTTP servers
 */
import { Server, ResponseData } from './shims/http';
import { EventEmitter } from './shims/events';
import { Buffer } from './shims/stream';
/**
 * Interface for virtual servers that can be registered with the bridge
 */
export interface IVirtualServer {
    listening: boolean;
    address(): {
        port: number;
        address: string;
        family: string;
    } | null;
    handleRequest(method: string, url: string, headers: Record<string, string>, body?: Buffer | string): Promise<ResponseData>;
}
export interface VirtualServer {
    server: Server | IVirtualServer;
    port: number;
    hostname: string;
    metadata?: ServerRegistrationMetadata;
}
export interface ServerRegistrationMetadata {
    purpose?: 'workspace-preview' | 'auxiliary';
    framework?: 'next' | 'vite' | 'wrangler' | 'wrangler-pages' | string;
    root?: string;
    name?: string;
    /** Container instance that owns this server (ContainerInstance.id). */
    ownerId?: string;
}
export interface BridgeOptions {
    baseUrl?: string;
    /** Base path prefix for subpath deployments (e.g. '/almostnode' for GitHub Pages) */
    basePath?: string;
    onServerReady?: (port: number, url: string, metadata?: ServerRegistrationMetadata) => void;
}
type ModuleRequestHandler = (url: string) => Promise<ResponseData>;
export type RequestMiddleware = (port: number, method: string, url: string, headers: Record<string, string>, body?: ArrayBuffer) => Promise<ResponseData | null>;
export interface InitServiceWorkerOptions {
    /**
     * The URL path to the service worker file
     * @default '/__sw__.js'
     */
    swUrl?: string;
    /**
     * Reload the page the first time the service worker takes control.
     * This is useful for static hosts that need the service worker to inject
     * navigation-level isolation headers, but command-time module imports must
     * opt out so the running process is not destroyed.
     * @default true
     */
    reloadOnFirstControl?: boolean;
}
/**
 * Server Bridge manages virtual HTTP servers and routes requests
 */
export declare class ServerBridge extends EventEmitter {
    static DEBUG: boolean;
    private servers;
    private moduleProviders;
    private baseUrl;
    private basePath;
    private options;
    private middlewares;
    private messageChannel;
    private serviceWorkerReady;
    private keepaliveInterval;
    private swInitPromise;
    constructor(options?: BridgeOptions);
    /**
     * Register a server on a port
     */
    registerServer(server: Server | IVirtualServer, port: number, hostname?: string, metadata?: ServerRegistrationMetadata): void;
    /**
     * Unregister a server
     */
    unregisterServer(port: number): void;
    /**
     * Unregister every server registered with the given owner id
     * (ServerRegistrationMetadata.ownerId). Used by ContainerInstance.dispose().
     */
    unregisterServersByOwner(ownerId: string): void;
    /**
     * Get the registration metadata for a server, if any
     */
    getServerMetadata(port: number): ServerRegistrationMetadata | undefined;
    /**
     * Find the first unregistered port at or above the preferred port
     */
    findFreePort(preferred: number): number;
    /**
     * Get the base path prefix (e.g. '/almostnode' for GitHub Pages subpath deployments)
     */
    getBasePath(): string;
    /**
     * Get server URL for a port
     */
    getServerUrl(port: number): string;
    /**
     * Get all registered server ports
     */
    getServerPorts(): number[];
    /**
     * Register a request middleware that can intercept requests before the virtual server
     */
    registerMiddleware(mw: RequestMiddleware): void;
    /**
     * Unregister a previously registered middleware
     */
    unregisterMiddleware(mw: RequestMiddleware): void;
    /**
     * Handle an incoming request from Service Worker
     */
    handleRequest(port: number, method: string, url: string, headers: Record<string, string>, body?: ArrayBuffer): Promise<ResponseData>;
    /**
     * Initialize Service Worker communication
     * @param options - Configuration options for the service worker
     * @param options.swUrl - Custom URL path to the service worker file (default: '/__sw__.js')
     */
    initServiceWorker(options?: InitServiceWorkerOptions): Promise<void>;
    ensureServiceWorkerReady(options?: InitServiceWorkerOptions): Promise<void>;
    isServiceWorkerReady(): boolean;
    registerModuleProvider(runtimeId: string, handler: ModuleRequestHandler): void;
    unregisterModuleProvider(runtimeId: string): void;
    /**
     * Handle messages from Service Worker
     */
    private handleServiceWorkerMessage;
    private handleModuleRequest;
    /**
     * Handle a streaming request - sends chunks as they arrive
     */
    private handleStreamingRequest;
    /**
     * Send message to Service Worker
     */
    private notifyServiceWorker;
    /**
     * Create a mock request handler for testing without Service Worker
     */
    createFetchHandler(): (request: Request) => Promise<Response>;
}
/**
 * Get or create the global server bridge
 */
export declare function getServerBridge(options?: BridgeOptions): ServerBridge;
/**
 * Reset the global bridge (for testing)
 */
export declare function resetServerBridge(): void;
export default ServerBridge;
//# sourceMappingURL=server-bridge.d.ts.map