import type { LogContext } from '@rocicorp/logger';
import WebSocket from 'ws';
import type { Downstream } from '../../../zero-protocol/src/down.ts';
import type { ErrorBody } from '../../../zero-protocol/src/error.ts';
import { type Upstream } from '../../../zero-protocol/src/up.ts';
import type { Source } from '../types/streams.ts';
import type { ConnectParams } from './connect-params.ts';
import { type ProtocolError } from '../../../zero-protocol/src/error.ts';
export type HandlerResult = {
    type: 'ok';
} | {
    type: 'fatal';
    error: ErrorBody;
} | {
    type: 'transient';
    errors: ErrorBody[];
} | StreamResult;
export type StreamResult = {
    type: 'stream';
    source: 'viewSyncer' | 'pusher';
    stream: Source<Downstream>;
};
export interface MessageHandler {
    handleMessage(msg: Upstream): Promise<HandlerResult[]>;
}
/**
 * Represents a connection between the client and server.
 *
 * Handles incoming messages on the connection and dispatches
 * them to the correct service.
 *
 * Listens to the ViewSyncer and sends messages to the client.
 */
export declare class Connection {
    #private;
    constructor(lc: LogContext, connectParams: ConnectParams, ws: WebSocket, messageHandler: MessageHandler, onClose: () => void);
    /**
     * Checks the protocol version and errors for unsupported protocols,
     * sending the initial `connected` response on success.
     *
     * This is early in the connection lifecycle because {@link #handleMessage}
     * will only parse messages with schema(s) of supported protocol versions.
     */
    init(): boolean;
    close(reason: string, ...args: unknown[]): void;
    handleInitConnection(initConnectionMsg: string): Promise<void>;
    send(data: Downstream, callback: ((err?: Error | null) => void) | 'ignore-backpressure'): void;
    sendError(errorBody: ErrorBody, thrown?: unknown): void;
}
export type WebSocketLike = Pick<WebSocket, 'readyState'> & {
    send(data: string, cb?: (err?: Error) => void): void;
};
export declare function send(lc: LogContext, ws: WebSocketLike, data: Downstream, callback: ((err?: Error | null) => void) | 'ignore-backpressure'): void;
export declare function sendError(lc: LogContext, ws: WebSocket, errorBody: ErrorBody, thrown?: unknown): void;
export declare function findProtocolError(error: unknown): ProtocolError | undefined;
//# sourceMappingURL=connection.d.ts.map