/** * This module provides a pair of classes for creating readable and writable streams * over the Chrome Extension Runtime messaging API. * * These streams utilize `chrome.runtime.sendMessage` for sending data and * `chrome.runtime.onMessage.addListener` for receiving data. This allows for * communication between different parts of a Chrome extension (e.g., background scripts, * content scripts, and popup pages). * * Note that unlike e.g. the `MessagePort` API, the Chrome Extension Runtime messaging API * doesn't have a built-in way to close the connection. The streams will continue to operate * as long as the extension is running, unless manually ended. * * @module ChromeRuntime streams */ import type { Json } from "@metamask/utils"; import type { ChromeRuntime } from "./chrome.d.cjs"; import { BaseDuplexStream } from "../BaseDuplexStream.cjs"; import type { BaseReaderArgs, ValidateInput, BaseWriterArgs } from "../BaseStream.cjs"; import { BaseReader, BaseWriter } from "../BaseStream.cjs"; export type ChromeRuntimeTarget = 'background' | 'offscreen' | 'popup'; export type MessageEnvelope = { target: ChromeRuntimeTarget; source: ChromeRuntimeTarget; payload: Payload; }; /** * A readable stream over the Chrome Extension Runtime messaging API. * * This class is a naive passthrough mechanism for data using `chrome.runtime.onMessage`. * Expects exclusive read access to the messaging API. * * @see * - {@link ChromeRuntimeWriter} for the corresponding writable stream. * - The module-level documentation for more details. */ export declare class ChromeRuntimeReader extends BaseReader { #private; /** * Constructs a new {@link ChromeRuntimeReader}. * * @param runtime - The Chrome runtime API object. * @param target - The target context (e.g., 'background', 'offscreen', 'popup'). * @param source - The source context that messages are expected from. * @param options - Options bag for configuring the reader. * @param options.validateInput - A function that validates input from the transport. * @param options.onEnd - A function that is called when the stream ends. */ constructor(runtime: ChromeRuntime, target: ChromeRuntimeTarget, source: ChromeRuntimeTarget, { validateInput, onEnd }?: BaseReaderArgs); } /** * A writable stream over the Chrome Extension Runtime messaging API. * * This class is a naive passthrough mechanism for data using `chrome.runtime.sendMessage`. * * @see * - {@link ChromeRuntimeReader} for the corresponding readable stream. * - The module-level documentation for more details. */ export declare class ChromeRuntimeWriter extends BaseWriter { /** * Constructs a new {@link ChromeRuntimeWriter}. * * @param runtime - The Chrome runtime API object. * @param target - The target context to send messages to. * @param source - The source context identifying where messages originate. * @param options - Options bag for configuring the writer. * @param options.name - The name of the stream, for logging purposes. * @param options.onEnd - A function that is called when the stream ends. */ constructor(runtime: ChromeRuntime, target: ChromeRuntimeTarget, source: ChromeRuntimeTarget, { name, onEnd }?: Omit, 'onDispatch'>); } /** * A duplex stream over the Chrome Extension Runtime messaging API. * * This class is a naive passthrough mechanism for data using `chrome.runtime.onMessage`. * * @see * - {@link ChromeRuntimeReader} for the corresponding readable stream. * - {@link ChromeRuntimeWriter} for the corresponding writable stream. */ export declare class ChromeRuntimeDuplexStream extends BaseDuplexStream, Write, ChromeRuntimeWriter> { /** * Constructs a new {@link ChromeRuntimeDuplexStream}. * * @param runtime - The Chrome runtime API object. * @param localTarget - The local target context for this stream. * @param remoteTarget - The remote target context to communicate with. * @param validateInput - A function that validates input from the transport. */ constructor(runtime: ChromeRuntime, localTarget: ChromeRuntimeTarget, remoteTarget: ChromeRuntimeTarget, validateInput?: ValidateInput); /** * Creates and synchronizes a new {@link ChromeRuntimeDuplexStream}. * * @param runtime - The Chrome runtime API object. * @param localTarget - The local target context for this stream. * @param remoteTarget - The remote target context to communicate with. * @param validateInput - A function that validates input from the transport. * @returns A synchronized duplex stream. */ static make(runtime: ChromeRuntime, localTarget: ChromeRuntimeTarget, remoteTarget: ChromeRuntimeTarget, validateInput?: ValidateInput): Promise>; } //# sourceMappingURL=ChromeRuntimeStream.d.cts.map