/** * Codec format negotiation. * * After the initial handshake (which always uses SCALE for backwards * compatibility) the product may request a codec upgrade via the * `host_codec_upgrade` protocol method. The host responds with the * format it selected (intersection of what both sides support). * * If the upgrade times out or the host doesn't support it, the * connection stays on the current codec (typically SCALE). * * ## Compatibility matrix * * | Product | Host | Wire format | * |---------|------|----------------------------------------| * | Old | Old | SCALE | * | Old | New | SCALE (old product never sends upgrade) | * | New | Old | SCALE (upgrade times out) | * | New | New | Structured clone (upgrade succeeds) | */ import type { CodecAdapter } from "./adapter.js"; import type { Transport } from "../transport/transport.js"; /** Wire-format identifiers. */ export type CodecFormat = "scale" | "structured_clone"; /** Sent by the product after handshake. */ export type CodecUpgradeRequest = { supportedFormats: CodecFormat[]; }; /** Returned by the host. */ export type CodecUpgradeResponse = { selectedFormat: CodecFormat; }; /** * Maximum time (ms) to wait for the host to reply to a codec upgrade * request before falling back to the current format. */ export declare const UPGRADE_TIMEOUT = 1000; /** * Map from format name to the CodecAdapter that implements it. */ export type CodecAdapterMap = Partial>; /** * Request a codec upgrade from the product side. * * Call this AFTER the handshake has completed (`transport.whenReady()` * resolved). Sends a `host_codec_upgrade` request with the * list of formats the product supports. If the host responds with a * format both sides support, both swap their codec adapters. * * If the request times out or the host returns an unknown format, * the transport stays on its current codec. This is safe — the * product just keeps using whatever it was using before. * * @param transport - The product-side transport (must be connected). * @param adapters - Map of format → CodecAdapter the product supports. * @returns The format that was selected, or `undefined` if the upgrade * failed and the transport stays on the current codec. */ export declare function requestCodecUpgrade(transport: Transport, adapters: CodecAdapterMap): Promise; /** * Register a handler for codec upgrade requests on the host side. * * When the product sends `host_codec_upgrade`, this handler picks * the best format from the intersection of what both sides support * and responds with the selection. * * If structured clone is selected, the host swaps its outgoing codec * BEFORE sending the response. This means the response itself arrives * as a structured clone message, which forces the product's transport * to auto-upgrade its outgoing codec via `decodeIncoming`. This * eliminates the race condition: even if the product's timeout has * already fired, the next message from the host (in structured clone) * will trigger the product's auto-upgrade. * * @param transport - The host-side transport. * @param adapters - Map of format → CodecAdapter the host supports. * @returns An unsubscribe function that removes the handler. */ export declare function handleCodecUpgrade(transport: Transport, adapters: CodecAdapterMap): () => void; //# sourceMappingURL=negotiation.d.ts.map